system/extras
修订版 | 12f58b35548b24280f696498aa797457e461a904 (tree) |
---|---|
时间 | 2016-10-06 08:15:09 |
作者 | Jaap Jan Meijer <jjmeijer88@gmai...> |
Commiter | Jaap Jan Meijer |
Merge remote-tracking branch 'cm/cm-14.0' into cm-14.0-x86
@@ -0,0 +1 @@ | ||
1 | +/su |
@@ -80,6 +80,15 @@ LOCAL_SHARED_LIBRARIES := \ | ||
80 | 80 | libsparse \ |
81 | 81 | libz |
82 | 82 | LOCAL_CFLAGS := -DREAL_UUID |
83 | + | |
84 | +ifeq ($(BOARD_SUPPRESS_EMMC_WIPE),true) | |
85 | + LOCAL_CFLAGS += -DSUPPRESS_EMMC_WIPE | |
86 | +endif | |
87 | + | |
88 | +ifeq ($(BOARD_NO_SECURE_DISCARD),true) | |
89 | + LOCAL_CFLAGS += -DNO_SECURE_DISCARD | |
90 | +endif | |
91 | + | |
83 | 92 | include $(BUILD_SHARED_LIBRARY) |
84 | 93 | |
85 | 94 |
@@ -95,6 +104,15 @@ LOCAL_STATIC_LIBRARIES := \ | ||
95 | 104 | libsparse_static \ |
96 | 105 | libselinux \ |
97 | 106 | libbase |
107 | + | |
108 | +ifeq ($(BOARD_SUPPRESS_EMMC_WIPE),true) | |
109 | + LOCAL_CFLAGS += -DSUPPRESS_EMMC_WIPE | |
110 | +endif | |
111 | + | |
112 | +ifeq ($(BOARD_NO_SECURE_DISCARD),true) | |
113 | + LOCAL_CFLAGS += -DNO_SECURE_DISCARD | |
114 | +endif | |
115 | + | |
98 | 116 | include $(BUILD_STATIC_LIBRARY) |
99 | 117 | |
100 | 118 |
@@ -57,9 +57,11 @@ extern int force; | ||
57 | 57 | |
58 | 58 | #define EXT4_JNL_BACKUP_BLOCKS 1 |
59 | 59 | |
60 | +#ifndef __cplusplus | |
60 | 61 | #ifndef min /* already defined by windows.h */ |
61 | 62 | #define min(a, b) ((a) < (b) ? (a) : (b)) |
62 | 63 | #endif |
64 | +#endif | |
63 | 65 | |
64 | 66 | #define DIV_ROUND_UP(x, y) (((x) + (y) - 1)/(y)) |
65 | 67 | #define EXT4_ALIGN(x, y) ((y) * DIV_ROUND_UP((x), (y))) |
@@ -806,6 +806,7 @@ int ext4fixup_internal(char *fsdev, int v_flag, int n_flag, | ||
806 | 806 | } |
807 | 807 | |
808 | 808 | close(fd); |
809 | + free(dirbuf); | |
809 | 810 | |
810 | 811 | return 0; |
811 | 812 | } |
@@ -34,6 +34,7 @@ | ||
34 | 34 | |
35 | 35 | int wipe_block_device(int fd, s64 len) |
36 | 36 | { |
37 | +#ifndef SUPPRESS_EMMC_WIPE | |
37 | 38 | u64 range[2]; |
38 | 39 | int ret; |
39 | 40 |
@@ -42,10 +43,12 @@ int wipe_block_device(int fd, s64 len) | ||
42 | 43 | return 0; |
43 | 44 | } |
44 | 45 | |
46 | +#ifndef NO_SECURE_DISCARD | |
45 | 47 | range[0] = 0; |
46 | 48 | range[1] = len; |
47 | 49 | ret = ioctl(fd, BLKSECDISCARD, &range); |
48 | 50 | if (ret < 0) { |
51 | +#endif /* NO_SECURE_DISCARD */ | |
49 | 52 | range[0] = 0; |
50 | 53 | range[1] = len; |
51 | 54 | ret = ioctl(fd, BLKDISCARD, &range); |
@@ -56,9 +59,14 @@ int wipe_block_device(int fd, s64 len) | ||
56 | 59 | warn("Wipe via secure discard failed, used discard instead\n"); |
57 | 60 | return 0; |
58 | 61 | } |
62 | +#ifndef NO_SECURE_DISCARD | |
59 | 63 | } |
60 | - | |
64 | +#endif /* NO_SECURE_DISCARD */ | |
61 | 65 | return 0; |
66 | +#else | |
67 | + warn("Wipe via secure discard suppressed due to bug in EMMC firmware\n"); | |
68 | + return 1; | |
69 | +#endif /* SUPPRESS_EMMC_WIPE */ | |
62 | 70 | } |
63 | 71 | |
64 | 72 | #else /* __linux__ */ |
@@ -2,7 +2,7 @@ | ||
2 | 2 | |
3 | 3 | LOCAL_PATH:= $(call my-dir) |
4 | 4 | |
5 | -ifeq ($(HOST_OS),linux) | |
5 | +ifneq (,$(filter linux darwin,$(HOST_OS))) | |
6 | 6 | |
7 | 7 | include $(CLEAR_VARS) |
8 | 8 | LOCAL_MODULE := libf2fs_utils_host |
@@ -41,7 +41,7 @@ LOCAL_MODULE := make_f2fs | ||
41 | 41 | LOCAL_LDFLAGS := -ldl -rdynamic |
42 | 42 | # The following libf2fs_* are from system/extras/f2fs_utils, |
43 | 43 | # and do not use code in external/f2fs-tools. |
44 | -LOCAL_STATIC_LIBRARIES := libf2fs_utils_host libf2fs_ioutils_host libf2fs_dlutils_host | |
44 | +LOCAL_STATIC_LIBRARIES := libf2fs_utils_host libf2fs_dlutils_host | |
45 | 45 | LOCAL_REQUIRED_MODULES := libf2fs_fmt_host_dyn |
46 | 46 | LOCAL_STATIC_LIBRARIES += \ |
47 | 47 | libsparse_host \ |
@@ -76,6 +76,15 @@ endif | ||
76 | 76 | include $(CLEAR_VARS) |
77 | 77 | LOCAL_MODULE := libf2fs_sparseblock |
78 | 78 | LOCAL_SRC_FILES := f2fs_sparseblock.c |
79 | +LOCAL_CFLAGS += -DSTATIC_LIB | |
80 | +LOCAL_STATIC_LIBRARIES := libcutils | |
81 | +LOCAL_C_INCLUDES := external/f2fs-tools/include \ | |
82 | + system/core/include/log | |
83 | +include $(BUILD_STATIC_LIBRARY) | |
84 | + | |
85 | +include $(CLEAR_VARS) | |
86 | +LOCAL_MODULE := libf2fs_sparseblock | |
87 | +LOCAL_SRC_FILES := f2fs_sparseblock.c | |
79 | 88 | LOCAL_SHARED_LIBRARIES := libcutils |
80 | 89 | LOCAL_C_INCLUDES := external/f2fs-tools/include \ |
81 | 90 | system/core/include/log |
@@ -35,10 +35,20 @@ | ||
35 | 35 | |
36 | 36 | #include <f2fs_fs.h> |
37 | 37 | #include <f2fs_format_utils.h> |
38 | +#if defined(__linux__) | |
38 | 39 | #define F2FS_DYN_LIB "libf2fs_fmt_host_dyn.so" |
40 | +#elif defined(__APPLE__) && defined(__MACH__) | |
41 | +#define F2FS_DYN_LIB "libf2fs_fmt_host_dyn.dylib" | |
42 | +#else | |
43 | +#error "Not supported OS" | |
44 | +#endif | |
39 | 45 | |
40 | 46 | int (*f2fs_format_device_dl)(void); |
41 | 47 | void (*f2fs_init_configuration_dl)(struct f2fs_configuration *); |
48 | +void (*flush_sparse_buffs_dl)(void); | |
49 | +void (*init_sparse_file_dl)(unsigned int, int64_t); | |
50 | +void (*finalize_sparse_file_dl)(int); | |
51 | +struct f2fs_configuration *f2fs_config; | |
42 | 52 | |
43 | 53 | int f2fs_format_device(void) { |
44 | 54 | assert(f2fs_format_device_dl); |
@@ -48,7 +58,18 @@ void f2fs_init_configuration(struct f2fs_configuration *config) { | ||
48 | 58 | assert(f2fs_init_configuration_dl); |
49 | 59 | f2fs_init_configuration_dl(config); |
50 | 60 | } |
51 | - | |
61 | +void flush_sparse_buffs(void) { | |
62 | + assert(flush_sparse_buffs_dl); | |
63 | + return flush_sparse_buffs_dl(); | |
64 | +} | |
65 | +void init_sparse_file(unsigned int block_size, int64_t len) { | |
66 | + assert(init_sparse_file_dl); | |
67 | + return init_sparse_file_dl(block_size, len); | |
68 | +} | |
69 | +void finalize_sparse_file(int fd) { | |
70 | + assert(finalize_sparse_file_dl); | |
71 | + return finalize_sparse_file_dl(fd); | |
72 | +} | |
52 | 73 | int dlopenf2fs() { |
53 | 74 | void* f2fs_lib; |
54 | 75 |
@@ -58,7 +79,13 @@ int dlopenf2fs() { | ||
58 | 79 | } |
59 | 80 | f2fs_format_device_dl = dlsym(f2fs_lib, "f2fs_format_device"); |
60 | 81 | f2fs_init_configuration_dl = dlsym(f2fs_lib, "f2fs_init_configuration"); |
61 | - if (!f2fs_format_device_dl || !f2fs_init_configuration_dl) { | |
82 | + flush_sparse_buffs_dl = dlsym(f2fs_lib, "flush_sparse_buffs"); | |
83 | + init_sparse_file_dl = dlsym(f2fs_lib, "init_sparse_file"); | |
84 | + finalize_sparse_file_dl = dlsym(f2fs_lib, "finalize_sparse_file"); | |
85 | + f2fs_config = dlsym(f2fs_lib, "config"); | |
86 | + if (!f2fs_format_device_dl || !f2fs_init_configuration_dl || | |
87 | + !flush_sparse_buffs_dl || !f2fs_config || | |
88 | + !init_sparse_file_dl || !finalize_sparse_file_dl) { | |
62 | 89 | return -1; |
63 | 90 | } |
64 | 91 | return 0; |
@@ -29,11 +29,13 @@ | ||
29 | 29 | #define _LARGEFILE64_SOURCE |
30 | 30 | |
31 | 31 | #include <assert.h> |
32 | -#include <asm/types.h> | |
33 | 32 | #include <dlfcn.h> |
34 | 33 | #include <errno.h> |
35 | 34 | #include <fcntl.h> |
35 | +#ifdef __linux__ | |
36 | +#include <asm/types.h> | |
36 | 37 | #include <linux/fs.h> |
38 | +#endif | |
37 | 39 | #include <stdio.h> |
38 | 40 | #include <stdlib.h> |
39 | 41 | #include <string.h> /* memset() */ |
@@ -138,6 +140,17 @@ static int dev_write_sparse(void *buf, __u64 byte_offset, size_t byte_len) | ||
138 | 140 | return 0; |
139 | 141 | } |
140 | 142 | |
143 | +void init_sparse_file(unsigned int block_size, int64_t len) | |
144 | +{ | |
145 | + f2fs_sparse_file = sparse_file_new(block_size, len); | |
146 | +} | |
147 | + | |
148 | +void finalize_sparse_file(int fd) | |
149 | +{ | |
150 | + sparse_file_write(f2fs_sparse_file, fd, /*gzip*/0, /*sparse*/1, /*crc*/0); | |
151 | + sparse_file_destroy(f2fs_sparse_file); | |
152 | +} | |
153 | + | |
141 | 154 | void f2fs_finalize_device(struct f2fs_configuration *c) |
142 | 155 | { |
143 | 156 | } |
@@ -26,9 +26,9 @@ | ||
26 | 26 | #member, le64_to_cpu((ptr)->member), le64_to_cpu((ptr)->member) ); \ |
27 | 27 | } while (0); |
28 | 28 | |
29 | -#define segno_in_journal(sum, i) (sum->sit_j.entries[i].segno) | |
29 | +#define segno_in_journal(jnl, i) (jnl->sit_j.entries[i].segno) | |
30 | 30 | |
31 | -#define sit_in_journal(sum, i) (sum->sit_j.entries[i].se) | |
31 | +#define sit_in_journal(jnl, i) (jnl->sit_j.entries[i].se) | |
32 | 32 | |
33 | 33 | static void dbg_print_raw_sb_info(struct f2fs_super_block *sb) |
34 | 34 | { |
@@ -137,10 +137,10 @@ static void dbg_print_info_struct(struct f2fs_info *info) | ||
137 | 137 | SLOGD("blocks_per_sit: %"PRIu64, info->blocks_per_sit); |
138 | 138 | SLOGD("sit_blocks loc: %p", info->sit_blocks); |
139 | 139 | SLOGD("sit_sums loc: %p", info->sit_sums); |
140 | - SLOGD("sit_sums num: %d", le16_to_cpu(info->sit_sums->n_sits)); | |
140 | + SLOGD("sit_sums num: %d", le16_to_cpu(info->journal->n_sits)); | |
141 | 141 | unsigned int i; |
142 | - for(i = 0; i < (le16_to_cpu(info->sit_sums->n_sits)); i++) { | |
143 | - SLOGD("entry %d in journal entries is for segment %d",i, le32_to_cpu(segno_in_journal(info->sit_sums, i))); | |
142 | + for(i = 0; i < (le16_to_cpu(info->journal->n_sits)); i++) { | |
143 | + SLOGD("entry %d in journal entries is for segment %d",i, le32_to_cpu(segno_in_journal(info->journal, i))); | |
144 | 144 | } |
145 | 145 | |
146 | 146 | SLOGD("cp_blkaddr: %"PRIu64, info->cp_blkaddr); |
@@ -360,7 +360,7 @@ static int get_sit_summary(int fd, struct f2fs_info *info, struct f2fs_checkpoin | ||
360 | 360 | if (is_set_ckpt_flags(cp, CP_COMPACT_SUM_FLAG)) { |
361 | 361 | if (read_structure_blk(fd, info->cp_valid_cp_blkaddr + le32_to_cpu(cp->cp_pack_start_sum), buffer, 1)) |
362 | 362 | return -1; |
363 | - memcpy(&info->sit_sums->n_sits, &buffer[SUM_JOURNAL_SIZE], SUM_JOURNAL_SIZE); | |
363 | + memcpy(&info->sit_sums->journal.n_sits, &buffer[SUM_JOURNAL_SIZE], SUM_JOURNAL_SIZE); | |
364 | 364 | } else { |
365 | 365 | u64 blk_addr; |
366 | 366 | if (is_set_ckpt_flags(cp, CP_UMOUNT_FLAG)) |
@@ -388,6 +388,13 @@ struct f2fs_info *generate_f2fs_info(int fd) | ||
388 | 388 | return NULL; |
389 | 389 | } |
390 | 390 | |
391 | + info->journal = calloc(1, sizeof(struct f2fs_journal)); | |
392 | + if (!info->journal) { | |
393 | + SLOGE("Out of memory!"); | |
394 | + free(info); | |
395 | + return NULL; | |
396 | + } | |
397 | + | |
391 | 398 | sb = malloc(sizeof(*sb)); |
392 | 399 | if(!sb) { |
393 | 400 | SLOGE("Out of memory!"); |
@@ -463,6 +470,9 @@ void free_f2fs_info(struct f2fs_info *info) | ||
463 | 470 | |
464 | 471 | free(info->sit_sums); |
465 | 472 | info->sit_sums = NULL; |
473 | + | |
474 | + free(info->journal); | |
475 | + info->journal = NULL; | |
466 | 476 | } |
467 | 477 | free(info); |
468 | 478 | } |
@@ -503,9 +513,9 @@ int run_on_used_blocks(u64 startblock, struct f2fs_info *info, int (*func)(u64 p | ||
503 | 513 | |
504 | 514 | /* check the SIT entries in the journal */ |
505 | 515 | found = 0; |
506 | - for(i = 0; i < le16_to_cpu(info->sit_sums->n_sits); i++) { | |
507 | - if (le32_to_cpu(segno_in_journal(info->sit_sums, i)) == segnum) { | |
508 | - sit_entry = &sit_in_journal(info->sit_sums, i); | |
516 | + for(i = 0; i < le16_to_cpu(info->journal->n_sits); i++) { | |
517 | + if (le32_to_cpu(segno_in_journal(info->journal, i)) == segnum) { | |
518 | + sit_entry = &sit_in_journal(info->journal, i); | |
509 | 519 | found = 1; |
510 | 520 | break; |
511 | 521 | } |
@@ -588,6 +598,7 @@ int copy_used(u64 pos, void *data) | ||
588 | 598 | return 0; |
589 | 599 | } |
590 | 600 | |
601 | +#ifndef STATIC_LIB | |
591 | 602 | int main(int argc, char **argv) |
592 | 603 | { |
593 | 604 | if (argc != 3) |
@@ -631,3 +642,4 @@ int main(int argc, char **argv) | ||
631 | 642 | close(outfd); |
632 | 643 | return 0; |
633 | 644 | } |
645 | +#endif |
@@ -35,6 +35,7 @@ extern "C" { | ||
35 | 35 | |
36 | 36 | struct f2fs_sit_block; |
37 | 37 | struct f2fs_summary_block; |
38 | +struct f2fs_journal; | |
38 | 39 | |
39 | 40 | struct f2fs_info { |
40 | 41 | u_int64_t blocks_per_segment; |
@@ -45,6 +46,7 @@ struct f2fs_info { | ||
45 | 46 | u_int64_t blocks_per_sit; |
46 | 47 | struct f2fs_sit_block *sit_blocks; |
47 | 48 | struct f2fs_summary_block *sit_sums; |
49 | + struct f2fs_journal *journal; | |
48 | 50 | |
49 | 51 | u_int64_t cp_blkaddr; |
50 | 52 | u_int64_t cp_valid_cp_blkaddr; |
@@ -41,20 +41,16 @@ struct selabel_handle; | ||
41 | 41 | #include "make_f2fs.h" |
42 | 42 | |
43 | 43 | extern void flush_sparse_buffs(); |
44 | +extern void init_sparse_file(unsigned int block_size, int64_t len); | |
45 | +extern void finalize_sparse_file(int fd); | |
44 | 46 | |
45 | -struct f2fs_configuration config; | |
46 | -struct sparse_file *f2fs_sparse_file; | |
47 | +extern struct f2fs_configuration *f2fs_config; | |
47 | 48 | extern int dlopenf2fs(); |
48 | 49 | |
49 | 50 | static void reset_f2fs_info() { |
50 | - // Reset all the global data structures used by make_f2fs so it | |
51 | - // can be called again. | |
52 | - memset(&config, 0, sizeof(config)); | |
53 | - config.fd = -1; | |
54 | - if (f2fs_sparse_file) { | |
55 | - sparse_file_destroy(f2fs_sparse_file); | |
56 | - f2fs_sparse_file = NULL; | |
57 | - } | |
51 | + memset(f2fs_config, 0, sizeof(*f2fs_config)); | |
52 | + f2fs_config->fd = -1; | |
53 | + f2fs_config->kd = -1; | |
58 | 54 | } |
59 | 55 | |
60 | 56 | int make_f2fs_sparse_fd(int fd, long long len, |
@@ -64,15 +60,13 @@ int make_f2fs_sparse_fd(int fd, long long len, | ||
64 | 60 | return -1; |
65 | 61 | } |
66 | 62 | reset_f2fs_info(); |
67 | - f2fs_init_configuration(&config); | |
63 | + f2fs_init_configuration(f2fs_config); | |
68 | 64 | len &= ~((__u64)(F2FS_BLKSIZE - 1)); |
69 | - config.total_sectors = len / config.sector_size; | |
70 | - config.start_sector = 0; | |
71 | - f2fs_sparse_file = sparse_file_new(F2FS_BLKSIZE, len); | |
65 | + f2fs_config->total_sectors = len / f2fs_config->sector_size; | |
66 | + f2fs_config->start_sector = 0; | |
67 | + init_sparse_file(F2FS_BLKSIZE, len); | |
72 | 68 | f2fs_format_device(); |
73 | - sparse_file_write(f2fs_sparse_file, fd, /*gzip*/0, /*sparse*/1, /*crc*/0); | |
74 | - sparse_file_destroy(f2fs_sparse_file); | |
69 | + finalize_sparse_file(fd); | |
75 | 70 | flush_sparse_buffs(); |
76 | - f2fs_sparse_file = NULL; | |
77 | 71 | return 0; |
78 | 72 | } |
@@ -17,7 +17,7 @@ common_src_files := \ | ||
17 | 17 | fec_process.cpp |
18 | 18 | |
19 | 19 | common_static_libraries := \ |
20 | - libmincrypt \ | |
20 | + libcrypto_utils_static \ | |
21 | 21 | libcrypto_static \ |
22 | 22 | libcutils \ |
23 | 23 | libbase |
@@ -23,17 +23,17 @@ | ||
23 | 23 | #include <new> |
24 | 24 | #include <pthread.h> |
25 | 25 | #include <stdio.h> |
26 | -#include <string> | |
27 | 26 | #include <string.h> |
27 | +#include <string> | |
28 | 28 | #include <sys/syscall.h> |
29 | 29 | #include <unistd.h> |
30 | 30 | #include <vector> |
31 | 31 | |
32 | -#include <utils/Compat.h> | |
33 | -#include <mincrypt/rsa.h> | |
34 | -#include <openssl/sha.h> | |
35 | -#include <fec/io.h> | |
32 | +#include <crypto_utils/android_pubkey.h> | |
36 | 33 | #include <fec/ecc.h> |
34 | +#include <fec/io.h> | |
35 | +#include <openssl/sha.h> | |
36 | +#include <utils/Compat.h> | |
37 | 37 | |
38 | 38 | /* processing parameters */ |
39 | 39 | #define WORK_MIN_THREADS 1 |
@@ -59,7 +59,7 @@ | ||
59 | 59 | struct verity_header { |
60 | 60 | uint32_t magic; |
61 | 61 | uint32_t version; |
62 | - uint8_t signature[RSANUMBYTES]; | |
62 | + uint8_t signature[ANDROID_PUBKEY_MODULUS_SIZE]; | |
63 | 63 | uint32_t length; |
64 | 64 | }; |
65 | 65 |
@@ -24,7 +24,8 @@ | ||
24 | 24 | #include <stdio.h> |
25 | 25 | #include <sys/types.h> |
26 | 26 | #include <unistd.h> |
27 | -#include <mincrypt/rsa.h> | |
27 | + | |
28 | +#include <crypto_utils/android_pubkey.h> | |
28 | 29 | |
29 | 30 | #ifdef __cplusplus |
30 | 31 | extern "C" { |
@@ -70,8 +71,8 @@ struct fec_ecc_metadata { | ||
70 | 71 | struct fec_verity_metadata { |
71 | 72 | bool disabled; |
72 | 73 | uint64_t data_size; |
73 | - uint8_t signature[RSANUMBYTES]; | |
74 | - uint8_t ecc_signature[RSANUMBYTES]; | |
74 | + uint8_t signature[ANDROID_PUBKEY_MODULUS_SIZE]; | |
75 | + uint8_t ecc_signature[ANDROID_PUBKEY_MODULUS_SIZE]; | |
75 | 76 | const char *table; |
76 | 77 | uint32_t table_length; |
77 | 78 | }; |
@@ -11,6 +11,7 @@ LOCAL_MODULE_TAGS := optional | ||
11 | 11 | LOCAL_STATIC_LIBRARIES := \ |
12 | 12 | libfec_host \ |
13 | 13 | libfec_rs_host \ |
14 | + libcrypto_utils_static \ | |
14 | 15 | libcrypto_static \ |
15 | 16 | libext4_utils_host \ |
16 | 17 | libsquashfs_utils_host \ |
@@ -0,0 +1,8 @@ | ||
1 | +LOCAL_PATH:= $(call my-dir) | |
2 | +include $(CLEAR_VARS) | |
3 | + | |
4 | +LOCAL_SRC_FILES := mkimage.c crc32.c | |
5 | +LOCAL_MODULE_TAGS := optional | |
6 | +LOCAL_MODULE := mkimage | |
7 | + | |
8 | +include $(BUILD_HOST_EXECUTABLE) |
@@ -0,0 +1,194 @@ | ||
1 | +/* | |
2 | + * This file is derived from crc32.c from the zlib-1.1.3 distribution | |
3 | + * by Jean-loup Gailly and Mark Adler. | |
4 | + */ | |
5 | + | |
6 | +/* crc32.c -- compute the CRC-32 of a data stream | |
7 | + * Copyright (C) 1995-1998 Mark Adler | |
8 | + * For conditions of distribution and use, see copyright notice in zlib.h | |
9 | + */ | |
10 | + | |
11 | +#include "zlib.h" | |
12 | + | |
13 | +#define local static | |
14 | +#define ZEXPORT /* empty */ | |
15 | +unsigned long crc32 (unsigned long, const unsigned char *, unsigned int); | |
16 | + | |
17 | +#ifdef DYNAMIC_CRC_TABLE | |
18 | + | |
19 | +local int crc_table_empty = 1; | |
20 | +local uLongf crc_table[256]; | |
21 | +local void make_crc_table OF((void)); | |
22 | + | |
23 | +/* | |
24 | + Generate a table for a byte-wise 32-bit CRC calculation on the polynomial: | |
25 | + x^32+x^26+x^23+x^22+x^16+x^12+x^11+x^10+x^8+x^7+x^5+x^4+x^2+x+1. | |
26 | + | |
27 | + Polynomials over GF(2) are represented in binary, one bit per coefficient, | |
28 | + with the lowest powers in the most significant bit. Then adding polynomials | |
29 | + is just exclusive-or, and multiplying a polynomial by x is a right shift by | |
30 | + one. If we call the above polynomial p, and represent a byte as the | |
31 | + polynomial q, also with the lowest power in the most significant bit (so the | |
32 | + byte 0xb1 is the polynomial x^7+x^3+x+1), then the CRC is (q*x^32) mod p, | |
33 | + where a mod b means the remainder after dividing a by b. | |
34 | + | |
35 | + This calculation is done using the shift-register method of multiplying and | |
36 | + taking the remainder. The register is initialized to zero, and for each | |
37 | + incoming bit, x^32 is added mod p to the register if the bit is a one (where | |
38 | + x^32 mod p is p+x^32 = x^26+...+1), and the register is multiplied mod p by | |
39 | + x (which is shifting right by one and adding x^32 mod p if the bit shifted | |
40 | + out is a one). We start with the highest power (least significant bit) of | |
41 | + q and repeat for all eight bits of q. | |
42 | + | |
43 | + The table is simply the CRC of all possible eight bit values. This is all | |
44 | + the information needed to generate CRC's on data a byte at a time for all | |
45 | + combinations of CRC register values and incoming bytes. | |
46 | +*/ | |
47 | +local void make_crc_table() | |
48 | +{ | |
49 | + uLong c; | |
50 | + int n, k; | |
51 | + uLong poly; /* polynomial exclusive-or pattern */ | |
52 | + /* terms of polynomial defining this crc (except x^32): */ | |
53 | + static const Byte p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26}; | |
54 | + | |
55 | + /* make exclusive-or pattern from polynomial (0xedb88320L) */ | |
56 | + poly = 0L; | |
57 | + for (n = 0; n < sizeof(p)/sizeof(Byte); n++) | |
58 | + poly |= 1L << (31 - p[n]); | |
59 | + | |
60 | + for (n = 0; n < 256; n++) | |
61 | + { | |
62 | + c = (uLong)n; | |
63 | + for (k = 0; k < 8; k++) | |
64 | + c = c & 1 ? poly ^ (c >> 1) : c >> 1; | |
65 | + crc_table[n] = c; | |
66 | + } | |
67 | + crc_table_empty = 0; | |
68 | +} | |
69 | +#else | |
70 | +/* ======================================================================== | |
71 | + * Table of CRC-32's of all single-byte values (made by make_crc_table) | |
72 | + */ | |
73 | +local const uLongf crc_table[256] = { | |
74 | + 0x00000000L, 0x77073096L, 0xee0e612cL, 0x990951baL, 0x076dc419L, | |
75 | + 0x706af48fL, 0xe963a535L, 0x9e6495a3L, 0x0edb8832L, 0x79dcb8a4L, | |
76 | + 0xe0d5e91eL, 0x97d2d988L, 0x09b64c2bL, 0x7eb17cbdL, 0xe7b82d07L, | |
77 | + 0x90bf1d91L, 0x1db71064L, 0x6ab020f2L, 0xf3b97148L, 0x84be41deL, | |
78 | + 0x1adad47dL, 0x6ddde4ebL, 0xf4d4b551L, 0x83d385c7L, 0x136c9856L, | |
79 | + 0x646ba8c0L, 0xfd62f97aL, 0x8a65c9ecL, 0x14015c4fL, 0x63066cd9L, | |
80 | + 0xfa0f3d63L, 0x8d080df5L, 0x3b6e20c8L, 0x4c69105eL, 0xd56041e4L, | |
81 | + 0xa2677172L, 0x3c03e4d1L, 0x4b04d447L, 0xd20d85fdL, 0xa50ab56bL, | |
82 | + 0x35b5a8faL, 0x42b2986cL, 0xdbbbc9d6L, 0xacbcf940L, 0x32d86ce3L, | |
83 | + 0x45df5c75L, 0xdcd60dcfL, 0xabd13d59L, 0x26d930acL, 0x51de003aL, | |
84 | + 0xc8d75180L, 0xbfd06116L, 0x21b4f4b5L, 0x56b3c423L, 0xcfba9599L, | |
85 | + 0xb8bda50fL, 0x2802b89eL, 0x5f058808L, 0xc60cd9b2L, 0xb10be924L, | |
86 | + 0x2f6f7c87L, 0x58684c11L, 0xc1611dabL, 0xb6662d3dL, 0x76dc4190L, | |
87 | + 0x01db7106L, 0x98d220bcL, 0xefd5102aL, 0x71b18589L, 0x06b6b51fL, | |
88 | + 0x9fbfe4a5L, 0xe8b8d433L, 0x7807c9a2L, 0x0f00f934L, 0x9609a88eL, | |
89 | + 0xe10e9818L, 0x7f6a0dbbL, 0x086d3d2dL, 0x91646c97L, 0xe6635c01L, | |
90 | + 0x6b6b51f4L, 0x1c6c6162L, 0x856530d8L, 0xf262004eL, 0x6c0695edL, | |
91 | + 0x1b01a57bL, 0x8208f4c1L, 0xf50fc457L, 0x65b0d9c6L, 0x12b7e950L, | |
92 | + 0x8bbeb8eaL, 0xfcb9887cL, 0x62dd1ddfL, 0x15da2d49L, 0x8cd37cf3L, | |
93 | + 0xfbd44c65L, 0x4db26158L, 0x3ab551ceL, 0xa3bc0074L, 0xd4bb30e2L, | |
94 | + 0x4adfa541L, 0x3dd895d7L, 0xa4d1c46dL, 0xd3d6f4fbL, 0x4369e96aL, | |
95 | + 0x346ed9fcL, 0xad678846L, 0xda60b8d0L, 0x44042d73L, 0x33031de5L, | |
96 | + 0xaa0a4c5fL, 0xdd0d7cc9L, 0x5005713cL, 0x270241aaL, 0xbe0b1010L, | |
97 | + 0xc90c2086L, 0x5768b525L, 0x206f85b3L, 0xb966d409L, 0xce61e49fL, | |
98 | + 0x5edef90eL, 0x29d9c998L, 0xb0d09822L, 0xc7d7a8b4L, 0x59b33d17L, | |
99 | + 0x2eb40d81L, 0xb7bd5c3bL, 0xc0ba6cadL, 0xedb88320L, 0x9abfb3b6L, | |
100 | + 0x03b6e20cL, 0x74b1d29aL, 0xead54739L, 0x9dd277afL, 0x04db2615L, | |
101 | + 0x73dc1683L, 0xe3630b12L, 0x94643b84L, 0x0d6d6a3eL, 0x7a6a5aa8L, | |
102 | + 0xe40ecf0bL, 0x9309ff9dL, 0x0a00ae27L, 0x7d079eb1L, 0xf00f9344L, | |
103 | + 0x8708a3d2L, 0x1e01f268L, 0x6906c2feL, 0xf762575dL, 0x806567cbL, | |
104 | + 0x196c3671L, 0x6e6b06e7L, 0xfed41b76L, 0x89d32be0L, 0x10da7a5aL, | |
105 | + 0x67dd4accL, 0xf9b9df6fL, 0x8ebeeff9L, 0x17b7be43L, 0x60b08ed5L, | |
106 | + 0xd6d6a3e8L, 0xa1d1937eL, 0x38d8c2c4L, 0x4fdff252L, 0xd1bb67f1L, | |
107 | + 0xa6bc5767L, 0x3fb506ddL, 0x48b2364bL, 0xd80d2bdaL, 0xaf0a1b4cL, | |
108 | + 0x36034af6L, 0x41047a60L, 0xdf60efc3L, 0xa867df55L, 0x316e8eefL, | |
109 | + 0x4669be79L, 0xcb61b38cL, 0xbc66831aL, 0x256fd2a0L, 0x5268e236L, | |
110 | + 0xcc0c7795L, 0xbb0b4703L, 0x220216b9L, 0x5505262fL, 0xc5ba3bbeL, | |
111 | + 0xb2bd0b28L, 0x2bb45a92L, 0x5cb36a04L, 0xc2d7ffa7L, 0xb5d0cf31L, | |
112 | + 0x2cd99e8bL, 0x5bdeae1dL, 0x9b64c2b0L, 0xec63f226L, 0x756aa39cL, | |
113 | + 0x026d930aL, 0x9c0906a9L, 0xeb0e363fL, 0x72076785L, 0x05005713L, | |
114 | + 0x95bf4a82L, 0xe2b87a14L, 0x7bb12baeL, 0x0cb61b38L, 0x92d28e9bL, | |
115 | + 0xe5d5be0dL, 0x7cdcefb7L, 0x0bdbdf21L, 0x86d3d2d4L, 0xf1d4e242L, | |
116 | + 0x68ddb3f8L, 0x1fda836eL, 0x81be16cdL, 0xf6b9265bL, 0x6fb077e1L, | |
117 | + 0x18b74777L, 0x88085ae6L, 0xff0f6a70L, 0x66063bcaL, 0x11010b5cL, | |
118 | + 0x8f659effL, 0xf862ae69L, 0x616bffd3L, 0x166ccf45L, 0xa00ae278L, | |
119 | + 0xd70dd2eeL, 0x4e048354L, 0x3903b3c2L, 0xa7672661L, 0xd06016f7L, | |
120 | + 0x4969474dL, 0x3e6e77dbL, 0xaed16a4aL, 0xd9d65adcL, 0x40df0b66L, | |
121 | + 0x37d83bf0L, 0xa9bcae53L, 0xdebb9ec5L, 0x47b2cf7fL, 0x30b5ffe9L, | |
122 | + 0xbdbdf21cL, 0xcabac28aL, 0x53b39330L, 0x24b4a3a6L, 0xbad03605L, | |
123 | + 0xcdd70693L, 0x54de5729L, 0x23d967bfL, 0xb3667a2eL, 0xc4614ab8L, | |
124 | + 0x5d681b02L, 0x2a6f2b94L, 0xb40bbe37L, 0xc30c8ea1L, 0x5a05df1bL, | |
125 | + 0x2d02ef8dL | |
126 | +}; | |
127 | +#endif | |
128 | + | |
129 | +#if 0 | |
130 | +/* ========================================================================= | |
131 | + * This function can be used by asm versions of crc32() | |
132 | + */ | |
133 | +const uLongf * ZEXPORT get_crc_table() | |
134 | +{ | |
135 | +#ifdef DYNAMIC_CRC_TABLE | |
136 | + if (crc_table_empty) make_crc_table(); | |
137 | +#endif | |
138 | + return (const uLongf *)crc_table; | |
139 | +} | |
140 | +#endif | |
141 | + | |
142 | +/* ========================================================================= */ | |
143 | +#define DO1(buf) crc = crc_table[((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8); | |
144 | +#define DO2(buf) DO1(buf); DO1(buf); | |
145 | +#define DO4(buf) DO2(buf); DO2(buf); | |
146 | +#define DO8(buf) DO4(buf); DO4(buf); | |
147 | + | |
148 | +/* ========================================================================= */ | |
149 | +uLong ZEXPORT crc32(crc, buf, len) | |
150 | + uLong crc; | |
151 | + const Bytef *buf; | |
152 | + uInt len; | |
153 | +{ | |
154 | +#ifdef DYNAMIC_CRC_TABLE | |
155 | + if (crc_table_empty) | |
156 | + make_crc_table(); | |
157 | +#endif | |
158 | + crc = crc ^ 0xffffffffL; | |
159 | + while (len >= 8) | |
160 | + { | |
161 | + DO8(buf); | |
162 | + len -= 8; | |
163 | + } | |
164 | + if (len) do { | |
165 | + DO1(buf); | |
166 | + } while (--len); | |
167 | + return crc ^ 0xffffffffL; | |
168 | +} | |
169 | + | |
170 | +#if (CONFIG_COMMANDS & CFG_CMD_JFFS2) || \ | |
171 | + ((CONFIG_COMMANDS & CFG_CMD_NAND) && !defined(CFG_NAND_LEGACY)) | |
172 | + | |
173 | +/* No ones complement version. JFFS2 (and other things ?) | |
174 | + * don't use ones compliment in their CRC calculations. | |
175 | + */ | |
176 | +uLong ZEXPORT crc32_no_comp(uLong crc, const Bytef *buf, uInt len) | |
177 | +{ | |
178 | +#ifdef DYNAMIC_CRC_TABLE | |
179 | + if (crc_table_empty) | |
180 | + make_crc_table(); | |
181 | +#endif | |
182 | + while (len >= 8) | |
183 | + { | |
184 | + DO8(buf); | |
185 | + len -= 8; | |
186 | + } | |
187 | + if (len) do { | |
188 | + DO1(buf); | |
189 | + } while (--len); | |
190 | + | |
191 | + return crc; | |
192 | +} | |
193 | + | |
194 | +#endif /* CFG_CMD_JFFS2 */ |
@@ -0,0 +1,158 @@ | ||
1 | +/* | |
2 | + * (C) Copyright 2000-2005 | |
3 | + * Wolfgang Denk, DENX Software Engineering, wd@denx.de. | |
4 | + * | |
5 | + * See file CREDITS for list of people who contributed to this | |
6 | + * project. | |
7 | + * | |
8 | + * This program is free software; you can redistribute it and/or | |
9 | + * modify it under the terms of the GNU General Public License as | |
10 | + * published by the Free Software Foundation; either version 2 of | |
11 | + * the License, or (at your option) any later version. | |
12 | + * | |
13 | + * This program is distributed in the hope that it will be useful, | |
14 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | + * GNU General Public License for more details. | |
17 | + * | |
18 | + * You should have received a copy of the GNU General Public License | |
19 | + * along with this program; if not, write to the Free Software | |
20 | + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, | |
21 | + * MA 02111-1307 USA | |
22 | + * | |
23 | + ******************************************************************** | |
24 | + * NOTE: This header file defines an interface to U-Boot. Including | |
25 | + * this (unmodified) header file in another file is considered normal | |
26 | + * use of U-Boot, and does *not* fall under the heading of "derived | |
27 | + * work". | |
28 | + ******************************************************************** | |
29 | + */ | |
30 | + | |
31 | +#ifndef __IMAGE_H__ | |
32 | +#define __IMAGE_H__ | |
33 | + | |
34 | +/* | |
35 | + * Operating System Codes | |
36 | + */ | |
37 | +#define IH_OS_INVALID 0 /* Invalid OS */ | |
38 | +#define IH_OS_OPENBSD 1 /* OpenBSD */ | |
39 | +#define IH_OS_NETBSD 2 /* NetBSD */ | |
40 | +#define IH_OS_FREEBSD 3 /* FreeBSD */ | |
41 | +#define IH_OS_4_4BSD 4 /* 4.4BSD */ | |
42 | +#define IH_OS_LINUX 5 /* Linux */ | |
43 | +#define IH_OS_SVR4 6 /* SVR4 */ | |
44 | +#define IH_OS_ESIX 7 /* Esix */ | |
45 | +#define IH_OS_SOLARIS 8 /* Solaris */ | |
46 | +#define IH_OS_IRIX 9 /* Irix */ | |
47 | +#define IH_OS_SCO 10 /* SCO */ | |
48 | +#define IH_OS_DELL 11 /* Dell */ | |
49 | +#define IH_OS_NCR 12 /* NCR */ | |
50 | +#define IH_OS_LYNXOS 13 /* LynxOS */ | |
51 | +#define IH_OS_VXWORKS 14 /* VxWorks */ | |
52 | +#define IH_OS_PSOS 15 /* pSOS */ | |
53 | +#define IH_OS_QNX 16 /* QNX */ | |
54 | +#define IH_OS_U_BOOT 17 /* Firmware */ | |
55 | +#define IH_OS_RTEMS 18 /* RTEMS */ | |
56 | +#define IH_OS_ARTOS 19 /* ARTOS */ | |
57 | +#define IH_OS_UNITY 20 /* Unity OS */ | |
58 | + | |
59 | +/* | |
60 | + * CPU Architecture Codes (supported by Linux) | |
61 | + */ | |
62 | +#define IH_CPU_INVALID 0 /* Invalid CPU */ | |
63 | +#define IH_CPU_ALPHA 1 /* Alpha */ | |
64 | +#define IH_CPU_ARM 2 /* ARM */ | |
65 | +#define IH_CPU_I386 3 /* Intel x86 */ | |
66 | +#define IH_CPU_IA64 4 /* IA64 */ | |
67 | +#define IH_CPU_MIPS 5 /* MIPS */ | |
68 | +#define IH_CPU_MIPS64 6 /* MIPS 64 Bit */ | |
69 | +#define IH_CPU_PPC 7 /* PowerPC */ | |
70 | +#define IH_CPU_S390 8 /* IBM S390 */ | |
71 | +#define IH_CPU_SH 9 /* SuperH */ | |
72 | +#define IH_CPU_SPARC 10 /* Sparc */ | |
73 | +#define IH_CPU_SPARC64 11 /* Sparc 64 Bit */ | |
74 | +#define IH_CPU_M68K 12 /* M68K */ | |
75 | +#define IH_CPU_NIOS 13 /* Nios-32 */ | |
76 | +#define IH_CPU_MICROBLAZE 14 /* MicroBlaze */ | |
77 | +#define IH_CPU_NIOS2 15 /* Nios-II */ | |
78 | +#define IH_CPU_BLACKFIN 16 /* Blackfin */ | |
79 | + | |
80 | +/* | |
81 | + * Image Types | |
82 | + * | |
83 | + * "Standalone Programs" are directly runnable in the environment | |
84 | + * provided by U-Boot; it is expected that (if they behave | |
85 | + * well) you can continue to work in U-Boot after return from | |
86 | + * the Standalone Program. | |
87 | + * "OS Kernel Images" are usually images of some Embedded OS which | |
88 | + * will take over control completely. Usually these programs | |
89 | + * will install their own set of exception handlers, device | |
90 | + * drivers, set up the MMU, etc. - this means, that you cannot | |
91 | + * expect to re-enter U-Boot except by resetting the CPU. | |
92 | + * "RAMDisk Images" are more or less just data blocks, and their | |
93 | + * parameters (address, size) are passed to an OS kernel that is | |
94 | + * being started. | |
95 | + * "Multi-File Images" contain several images, typically an OS | |
96 | + * (Linux) kernel image and one or more data images like | |
97 | + * RAMDisks. This construct is useful for instance when you want | |
98 | + * to boot over the network using BOOTP etc., where the boot | |
99 | + * server provides just a single image file, but you want to get | |
100 | + * for instance an OS kernel and a RAMDisk image. | |
101 | + * | |
102 | + * "Multi-File Images" start with a list of image sizes, each | |
103 | + * image size (in bytes) specified by an "uint32_t" in network | |
104 | + * byte order. This list is terminated by an "(uint32_t)0". | |
105 | + * Immediately after the terminating 0 follow the images, one by | |
106 | + * one, all aligned on "uint32_t" boundaries (size rounded up to | |
107 | + * a multiple of 4 bytes - except for the last file). | |
108 | + * | |
109 | + * "Firmware Images" are binary images containing firmware (like | |
110 | + * U-Boot or FPGA images) which usually will be programmed to | |
111 | + * flash memory. | |
112 | + * | |
113 | + * "Script files" are command sequences that will be executed by | |
114 | + * U-Boot's command interpreter; this feature is especially | |
115 | + * useful when you configure U-Boot to use a real shell (hush) | |
116 | + * as command interpreter (=> Shell Scripts). | |
117 | + */ | |
118 | + | |
119 | +#define IH_TYPE_INVALID 0 /* Invalid Image */ | |
120 | +#define IH_TYPE_STANDALONE 1 /* Standalone Program */ | |
121 | +#define IH_TYPE_KERNEL 2 /* OS Kernel Image */ | |
122 | +#define IH_TYPE_RAMDISK 3 /* RAMDisk Image */ | |
123 | +#define IH_TYPE_MULTI 4 /* Multi-File Image */ | |
124 | +#define IH_TYPE_FIRMWARE 5 /* Firmware Image */ | |
125 | +#define IH_TYPE_SCRIPT 6 /* Script file */ | |
126 | +#define IH_TYPE_FILESYSTEM 7 /* Filesystem Image (any type) */ | |
127 | + | |
128 | +/* | |
129 | + * Compression Types | |
130 | + */ | |
131 | +#define IH_COMP_NONE 0 /* No Compression Used */ | |
132 | +#define IH_COMP_GZIP 1 /* gzip Compression Used */ | |
133 | +#define IH_COMP_BZIP2 2 /* bzip2 Compression Used */ | |
134 | + | |
135 | +#define IH_MAGIC 0x27051956 /* Image Magic Number */ | |
136 | +#define IH_NMLEN 32 /* Image Name Length */ | |
137 | + | |
138 | +/* | |
139 | + * all data in network byte order (aka natural aka bigendian) | |
140 | + */ | |
141 | + | |
142 | +typedef struct image_header { | |
143 | + uint32_t ih_magic; /* Image Header Magic Number */ | |
144 | + uint32_t ih_hcrc; /* Image Header CRC Checksum */ | |
145 | + uint32_t ih_time; /* Image Creation Timestamp */ | |
146 | + uint32_t ih_size; /* Image Data Size */ | |
147 | + uint32_t ih_load; /* Data Load Address */ | |
148 | + uint32_t ih_ep; /* Entry Point Address */ | |
149 | + uint32_t ih_dcrc; /* Image Data CRC Checksum */ | |
150 | + uint8_t ih_os; /* Operating System */ | |
151 | + uint8_t ih_arch; /* CPU architecture */ | |
152 | + uint8_t ih_type; /* Image Type */ | |
153 | + uint8_t ih_comp; /* Compression Type */ | |
154 | + uint8_t ih_name[IH_NMLEN]; /* Image Name */ | |
155 | +} image_header_t; | |
156 | + | |
157 | + | |
158 | +#endif /* __IMAGE_H__ */ |
@@ -0,0 +1,742 @@ | ||
1 | +/* | |
2 | + * (C) Copyright 2000-2004 | |
3 | + * DENX Software Engineering | |
4 | + * Wolfgang Denk, wd@denx.de | |
5 | + * All rights reserved. | |
6 | + * | |
7 | + * This program is free software; you can redistribute it and/or | |
8 | + * modify it under the terms of the GNU General Public License as | |
9 | + * published by the Free Software Foundation; either version 2 of | |
10 | + * the License, or (at your option) any later version. | |
11 | + * | |
12 | + * This program is distributed in the hope that it will be useful, | |
13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | + * GNU General Public License for more details. | |
16 | + * | |
17 | + * You should have received a copy of the GNU General Public License | |
18 | + * along with this program; if not, write to the Free Software | |
19 | + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, | |
20 | + * MA 02111-1307 USA | |
21 | + */ | |
22 | + | |
23 | +#include <errno.h> | |
24 | +#include <fcntl.h> | |
25 | +#include <stdio.h> | |
26 | +#include <stdlib.h> | |
27 | +#include <string.h> | |
28 | +#ifndef __WIN32__ | |
29 | +#include <netinet/in.h> /* for host / network byte order conversions */ | |
30 | +#endif | |
31 | +#include <sys/mman.h> | |
32 | +#include <sys/stat.h> | |
33 | +#include <sys/types.h> | |
34 | +#include <time.h> | |
35 | +#include <unistd.h> | |
36 | + | |
37 | +#if defined(__BEOS__) || defined(__NetBSD__) || defined(__APPLE__) | |
38 | +#include <inttypes.h> | |
39 | +#endif | |
40 | + | |
41 | +#ifdef __WIN32__ | |
42 | +typedef unsigned int __u32; | |
43 | + | |
44 | +#define SWAP_LONG(x) \ | |
45 | + ((__u32)( \ | |
46 | + (((__u32)(x) & (__u32)0x000000ffUL) << 24) | \ | |
47 | + (((__u32)(x) & (__u32)0x0000ff00UL) << 8) | \ | |
48 | + (((__u32)(x) & (__u32)0x00ff0000UL) >> 8) | \ | |
49 | + (((__u32)(x) & (__u32)0xff000000UL) >> 24) )) | |
50 | +typedef unsigned char uint8_t; | |
51 | +typedef unsigned short uint16_t; | |
52 | +typedef unsigned int uint32_t; | |
53 | + | |
54 | +#define ntohl(a) SWAP_LONG(a) | |
55 | +#define htonl(a) SWAP_LONG(a) | |
56 | +#endif /* __WIN32__ */ | |
57 | + | |
58 | +#ifndef O_BINARY /* should be define'd on __WIN32__ */ | |
59 | +#define O_BINARY 0 | |
60 | +#endif | |
61 | + | |
62 | +#include "image.h" | |
63 | + | |
64 | +extern int errno; | |
65 | + | |
66 | +#ifndef MAP_FAILED | |
67 | +#define MAP_FAILED (-1) | |
68 | +#endif | |
69 | + | |
70 | +char *cmdname; | |
71 | + | |
72 | +extern unsigned long crc32 (unsigned long crc, const char *buf, unsigned int len); | |
73 | + | |
74 | +typedef struct table_entry { | |
75 | + int val; /* as defined in image.h */ | |
76 | + char *sname; /* short (input) name */ | |
77 | + char *lname; /* long (output) name */ | |
78 | +} table_entry_t; | |
79 | + | |
80 | +table_entry_t arch_name[] = { | |
81 | + { IH_CPU_INVALID, NULL, "Invalid CPU", }, | |
82 | + { IH_CPU_ALPHA, "alpha", "Alpha", }, | |
83 | + { IH_CPU_ARM, "arm", "ARM", }, | |
84 | + { IH_CPU_I386, "x86", "Intel x86", }, | |
85 | + { IH_CPU_IA64, "ia64", "IA64", }, | |
86 | + { IH_CPU_M68K, "m68k", "MC68000", }, | |
87 | + { IH_CPU_MICROBLAZE, "microblaze", "MicroBlaze", }, | |
88 | + { IH_CPU_MIPS, "mips", "MIPS", }, | |
89 | + { IH_CPU_MIPS64, "mips64", "MIPS 64 Bit", }, | |
90 | + { IH_CPU_NIOS, "nios", "NIOS", }, | |
91 | + { IH_CPU_NIOS2, "nios2", "NIOS II", }, | |
92 | + { IH_CPU_PPC, "ppc", "PowerPC", }, | |
93 | + { IH_CPU_S390, "s390", "IBM S390", }, | |
94 | + { IH_CPU_SH, "sh", "SuperH", }, | |
95 | + { IH_CPU_SPARC, "sparc", "SPARC", }, | |
96 | + { IH_CPU_SPARC64, "sparc64", "SPARC 64 Bit", }, | |
97 | + { IH_CPU_BLACKFIN, "blackfin", "Blackfin", }, | |
98 | + { -1, "", "", }, | |
99 | +}; | |
100 | + | |
101 | +table_entry_t os_name[] = { | |
102 | + { IH_OS_INVALID, NULL, "Invalid OS", }, | |
103 | + { IH_OS_4_4BSD, "4_4bsd", "4_4BSD", }, | |
104 | + { IH_OS_ARTOS, "artos", "ARTOS", }, | |
105 | + { IH_OS_DELL, "dell", "Dell", }, | |
106 | + { IH_OS_ESIX, "esix", "Esix", }, | |
107 | + { IH_OS_FREEBSD, "freebsd", "FreeBSD", }, | |
108 | + { IH_OS_IRIX, "irix", "Irix", }, | |
109 | + { IH_OS_LINUX, "linux", "Linux", }, | |
110 | + { IH_OS_LYNXOS, "lynxos", "LynxOS", }, | |
111 | + { IH_OS_NCR, "ncr", "NCR", }, | |
112 | + { IH_OS_NETBSD, "netbsd", "NetBSD", }, | |
113 | + { IH_OS_OPENBSD, "openbsd", "OpenBSD", }, | |
114 | + { IH_OS_PSOS, "psos", "pSOS", }, | |
115 | + { IH_OS_QNX, "qnx", "QNX", }, | |
116 | + { IH_OS_RTEMS, "rtems", "RTEMS", }, | |
117 | + { IH_OS_SCO, "sco", "SCO", }, | |
118 | + { IH_OS_SOLARIS, "solaris", "Solaris", }, | |
119 | + { IH_OS_SVR4, "svr4", "SVR4", }, | |
120 | + { IH_OS_U_BOOT, "u-boot", "U-Boot", }, | |
121 | + { IH_OS_VXWORKS, "vxworks", "VxWorks", }, | |
122 | + { -1, "", "", }, | |
123 | +}; | |
124 | + | |
125 | +table_entry_t type_name[] = { | |
126 | + { IH_TYPE_INVALID, NULL, "Invalid Image", }, | |
127 | + { IH_TYPE_FILESYSTEM, "filesystem", "Filesystem Image", }, | |
128 | + { IH_TYPE_FIRMWARE, "firmware", "Firmware", }, | |
129 | + { IH_TYPE_KERNEL, "kernel", "Kernel Image", }, | |
130 | + { IH_TYPE_MULTI, "multi", "Multi-File Image", }, | |
131 | + { IH_TYPE_RAMDISK, "ramdisk", "RAMDisk Image", }, | |
132 | + { IH_TYPE_SCRIPT, "script", "Script", }, | |
133 | + { IH_TYPE_STANDALONE, "standalone", "Standalone Program", }, | |
134 | + { -1, "", "", }, | |
135 | +}; | |
136 | + | |
137 | +table_entry_t comp_name[] = { | |
138 | + { IH_COMP_NONE, "none", "uncompressed", }, | |
139 | + { IH_COMP_BZIP2, "bzip2", "bzip2 compressed", }, | |
140 | + { IH_COMP_GZIP, "gzip", "gzip compressed", }, | |
141 | + { -1, "", "", }, | |
142 | +}; | |
143 | + | |
144 | +static void copy_file (int, const char *, int); | |
145 | +static void usage (void); | |
146 | +static void print_header (image_header_t *); | |
147 | +static void print_type (image_header_t *); | |
148 | +static char *put_table_entry (table_entry_t *, char *, int); | |
149 | +static char *put_arch (int); | |
150 | +static char *put_type (int); | |
151 | +static char *put_os (int); | |
152 | +static char *put_comp (int); | |
153 | +static int get_table_entry (table_entry_t *, char *, char *); | |
154 | +static int get_arch(char *); | |
155 | +static int get_comp(char *); | |
156 | +static int get_os (char *); | |
157 | +static int get_type(char *); | |
158 | + | |
159 | + | |
160 | +char *datafile; | |
161 | +char *imagefile; | |
162 | + | |
163 | +int dflag = 0; | |
164 | +int eflag = 0; | |
165 | +int lflag = 0; | |
166 | +int vflag = 0; | |
167 | +int xflag = 0; | |
168 | +int opt_os = IH_OS_LINUX; | |
169 | +int opt_arch = IH_CPU_PPC; | |
170 | +int opt_type = IH_TYPE_KERNEL; | |
171 | +int opt_comp = IH_COMP_GZIP; | |
172 | + | |
173 | +image_header_t header; | |
174 | +image_header_t *hdr = &header; | |
175 | + | |
176 | +int | |
177 | +main (int argc, char **argv) | |
178 | +{ | |
179 | + int ifd; | |
180 | + uint32_t checksum; | |
181 | + uint32_t addr; | |
182 | + uint32_t ep; | |
183 | + struct stat sbuf; | |
184 | + unsigned char *ptr; | |
185 | + char *name = ""; | |
186 | + | |
187 | + cmdname = *argv; | |
188 | + | |
189 | + addr = ep = 0; | |
190 | + | |
191 | + while (--argc > 0 && **++argv == '-') { | |
192 | + while (*++*argv) { | |
193 | + switch (**argv) { | |
194 | + case 'l': | |
195 | + lflag = 1; | |
196 | + break; | |
197 | + case 'A': | |
198 | + if ((--argc <= 0) || | |
199 | + (opt_arch = get_arch(*++argv)) < 0) | |
200 | + usage (); | |
201 | + goto NXTARG; | |
202 | + case 'C': | |
203 | + if ((--argc <= 0) || | |
204 | + (opt_comp = get_comp(*++argv)) < 0) | |
205 | + usage (); | |
206 | + goto NXTARG; | |
207 | + case 'O': | |
208 | + if ((--argc <= 0) || | |
209 | + (opt_os = get_os(*++argv)) < 0) | |
210 | + usage (); | |
211 | + goto NXTARG; | |
212 | + case 'T': | |
213 | + if ((--argc <= 0) || | |
214 | + (opt_type = get_type(*++argv)) < 0) | |
215 | + usage (); | |
216 | + goto NXTARG; | |
217 | + | |
218 | + case 'a': | |
219 | + if (--argc <= 0) | |
220 | + usage (); | |
221 | + addr = strtoul (*++argv, (char **)&ptr, 16); | |
222 | + if (*ptr) { | |
223 | + fprintf (stderr, | |
224 | + "%s: invalid load address %s\n", | |
225 | + cmdname, *argv); | |
226 | + exit (EXIT_FAILURE); | |
227 | + } | |
228 | + goto NXTARG; | |
229 | + case 'd': | |
230 | + if (--argc <= 0) | |
231 | + usage (); | |
232 | + datafile = *++argv; | |
233 | + dflag = 1; | |
234 | + goto NXTARG; | |
235 | + case 'e': | |
236 | + if (--argc <= 0) | |
237 | + usage (); | |
238 | + ep = strtoul (*++argv, (char **)&ptr, 16); | |
239 | + if (*ptr) { | |
240 | + fprintf (stderr, | |
241 | + "%s: invalid entry point %s\n", | |
242 | + cmdname, *argv); | |
243 | + exit (EXIT_FAILURE); | |
244 | + } | |
245 | + eflag = 1; | |
246 | + goto NXTARG; | |
247 | + case 'n': | |
248 | + if (--argc <= 0) | |
249 | + usage (); | |
250 | + name = *++argv; | |
251 | + goto NXTARG; | |
252 | + case 'v': | |
253 | + vflag++; | |
254 | + break; | |
255 | + case 'x': | |
256 | + xflag++; | |
257 | + break; | |
258 | + default: | |
259 | + usage (); | |
260 | + } | |
261 | + } | |
262 | +NXTARG: ; | |
263 | + } | |
264 | + | |
265 | + if ((argc != 1) || ((lflag ^ dflag) == 0)) | |
266 | + usage(); | |
267 | + | |
268 | + if (!eflag) { | |
269 | + ep = addr; | |
270 | + /* If XIP, entry point must be after the U-Boot header */ | |
271 | + if (xflag) | |
272 | + ep += sizeof(image_header_t); | |
273 | + } | |
274 | + | |
275 | + /* | |
276 | + * If XIP, ensure the entry point is equal to the load address plus | |
277 | + * the size of the U-Boot header. | |
278 | + */ | |
279 | + if (xflag) { | |
280 | + if (ep != addr + sizeof(image_header_t)) { | |
281 | + fprintf (stderr, | |
282 | + "%s: For XIP, the entry point must be the load addr + %lu\n", | |
283 | + cmdname, | |
284 | + (unsigned long)sizeof(image_header_t)); | |
285 | + exit (EXIT_FAILURE); | |
286 | + } | |
287 | + } | |
288 | + | |
289 | + imagefile = *argv; | |
290 | + | |
291 | + if (lflag) { | |
292 | + ifd = open(imagefile, O_RDONLY|O_BINARY); | |
293 | + } else { | |
294 | + ifd = open(imagefile, O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666); | |
295 | + } | |
296 | + | |
297 | + if (ifd < 0) { | |
298 | + fprintf (stderr, "%s: Can't open %s: %s\n", | |
299 | + cmdname, imagefile, strerror(errno)); | |
300 | + exit (EXIT_FAILURE); | |
301 | + } | |
302 | + | |
303 | + if (lflag) { | |
304 | + int len; | |
305 | + char *data; | |
306 | + /* | |
307 | + * list header information of existing image | |
308 | + */ | |
309 | + if (fstat(ifd, &sbuf) < 0) { | |
310 | + fprintf (stderr, "%s: Can't stat %s: %s\n", | |
311 | + cmdname, imagefile, strerror(errno)); | |
312 | + exit (EXIT_FAILURE); | |
313 | + } | |
314 | + | |
315 | + if ((unsigned)sbuf.st_size < sizeof(image_header_t)) { | |
316 | + fprintf (stderr, | |
317 | + "%s: Bad size: \"%s\" is no valid image\n", | |
318 | + cmdname, imagefile); | |
319 | + exit (EXIT_FAILURE); | |
320 | + } | |
321 | + | |
322 | + ptr = (unsigned char *)mmap(0, sbuf.st_size, | |
323 | + PROT_READ, MAP_SHARED, ifd, 0); | |
324 | + if ((caddr_t)ptr == (caddr_t)-1) { | |
325 | + fprintf (stderr, "%s: Can't read %s: %s\n", | |
326 | + cmdname, imagefile, strerror(errno)); | |
327 | + exit (EXIT_FAILURE); | |
328 | + } | |
329 | + | |
330 | + /* | |
331 | + * create copy of header so that we can blank out the | |
332 | + * checksum field for checking - this can't be done | |
333 | + * on the PROT_READ mapped data. | |
334 | + */ | |
335 | + memcpy (hdr, ptr, sizeof(image_header_t)); | |
336 | + | |
337 | + if (ntohl(hdr->ih_magic) != IH_MAGIC) { | |
338 | + fprintf (stderr, | |
339 | + "%s: Bad Magic Number: \"%s\" is no valid image\n", | |
340 | + cmdname, imagefile); | |
341 | + exit (EXIT_FAILURE); | |
342 | + } | |
343 | + | |
344 | + data = (char *)hdr; | |
345 | + len = sizeof(image_header_t); | |
346 | + | |
347 | + checksum = ntohl(hdr->ih_hcrc); | |
348 | + hdr->ih_hcrc = htonl(0); /* clear for re-calculation */ | |
349 | + | |
350 | + if (crc32 (0, data, len) != checksum) { | |
351 | + fprintf (stderr, | |
352 | + "%s: ERROR: \"%s\" has bad header checksum!\n", | |
353 | + cmdname, imagefile); | |
354 | + exit (EXIT_FAILURE); | |
355 | + } | |
356 | + | |
357 | + data = (char *)(ptr + sizeof(image_header_t)); | |
358 | + len = sbuf.st_size - sizeof(image_header_t) ; | |
359 | + | |
360 | + if (crc32 (0, data, len) != ntohl(hdr->ih_dcrc)) { | |
361 | + fprintf (stderr, | |
362 | + "%s: ERROR: \"%s\" has corrupted data!\n", | |
363 | + cmdname, imagefile); | |
364 | + exit (EXIT_FAILURE); | |
365 | + } | |
366 | + | |
367 | + /* for multi-file images we need the data part, too */ | |
368 | + print_header ((image_header_t *)ptr); | |
369 | + | |
370 | + (void) munmap((void *)ptr, sbuf.st_size); | |
371 | + (void) close (ifd); | |
372 | + | |
373 | + exit (EXIT_SUCCESS); | |
374 | + } | |
375 | + | |
376 | + /* | |
377 | + * Must be -w then: | |
378 | + * | |
379 | + * write dummy header, to be fixed later | |
380 | + */ | |
381 | + memset (hdr, 0, sizeof(image_header_t)); | |
382 | + | |
383 | + if (write(ifd, hdr, sizeof(image_header_t)) != sizeof(image_header_t)) { | |
384 | + fprintf (stderr, "%s: Write error on %s: %s\n", | |
385 | + cmdname, imagefile, strerror(errno)); | |
386 | + exit (EXIT_FAILURE); | |
387 | + } | |
388 | + | |
389 | + if (opt_type == IH_TYPE_MULTI || opt_type == IH_TYPE_SCRIPT) { | |
390 | + char *file = datafile; | |
391 | + uint32_t size; | |
392 | + | |
393 | + for (;;) { | |
394 | + char *sep = NULL; | |
395 | + | |
396 | + if (file) { | |
397 | + if ((sep = strchr(file, ':')) != NULL) { | |
398 | + *sep = '\0'; | |
399 | + } | |
400 | + | |
401 | + if (stat (file, &sbuf) < 0) { | |
402 | + fprintf (stderr, "%s: Can't stat %s: %s\n", | |
403 | + cmdname, file, strerror(errno)); | |
404 | + exit (EXIT_FAILURE); | |
405 | + } | |
406 | + size = htonl(sbuf.st_size); | |
407 | + } else { | |
408 | + size = 0; | |
409 | + } | |
410 | + | |
411 | + if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) { | |
412 | + fprintf (stderr, "%s: Write error on %s: %s\n", | |
413 | + cmdname, imagefile, strerror(errno)); | |
414 | + exit (EXIT_FAILURE); | |
415 | + } | |
416 | + | |
417 | + if (!file) { | |
418 | + break; | |
419 | + } | |
420 | + | |
421 | + if (sep) { | |
422 | + *sep = ':'; | |
423 | + file = sep + 1; | |
424 | + } else { | |
425 | + file = NULL; | |
426 | + } | |
427 | + } | |
428 | + | |
429 | + file = datafile; | |
430 | + | |
431 | + for (;;) { | |
432 | + char *sep = strchr(file, ':'); | |
433 | + if (sep) { | |
434 | + *sep = '\0'; | |
435 | + copy_file (ifd, file, 1); | |
436 | + *sep++ = ':'; | |
437 | + file = sep; | |
438 | + } else { | |
439 | + copy_file (ifd, file, 0); | |
440 | + break; | |
441 | + } | |
442 | + } | |
443 | + } else { | |
444 | + copy_file (ifd, datafile, 0); | |
445 | + } | |
446 | + | |
447 | + /* We're a bit of paranoid */ | |
448 | + (void) fsync (ifd); | |
449 | + | |
450 | + if (fstat(ifd, &sbuf) < 0) { | |
451 | + fprintf (stderr, "%s: Can't stat %s: %s\n", | |
452 | + cmdname, imagefile, strerror(errno)); | |
453 | + exit (EXIT_FAILURE); | |
454 | + } | |
455 | + | |
456 | + ptr = (unsigned char *)mmap(0, sbuf.st_size, | |
457 | + PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0); | |
458 | + if (ptr == (unsigned char *)MAP_FAILED) { | |
459 | + fprintf (stderr, "%s: Can't map %s: %s\n", | |
460 | + cmdname, imagefile, strerror(errno)); | |
461 | + exit (EXIT_FAILURE); | |
462 | + } | |
463 | + | |
464 | + hdr = (image_header_t *)ptr; | |
465 | + | |
466 | + checksum = crc32 (0, | |
467 | + (const char *)(ptr + sizeof(image_header_t)), | |
468 | + sbuf.st_size - sizeof(image_header_t) | |
469 | + ); | |
470 | + | |
471 | + /* Build new header */ | |
472 | + hdr->ih_magic = htonl(IH_MAGIC); | |
473 | + hdr->ih_time = htonl(sbuf.st_mtime); | |
474 | + hdr->ih_size = htonl(sbuf.st_size - sizeof(image_header_t)); | |
475 | + hdr->ih_load = htonl(addr); | |
476 | + hdr->ih_ep = htonl(ep); | |
477 | + hdr->ih_dcrc = htonl(checksum); | |
478 | + hdr->ih_os = opt_os; | |
479 | + hdr->ih_arch = opt_arch; | |
480 | + hdr->ih_type = opt_type; | |
481 | + hdr->ih_comp = opt_comp; | |
482 | + | |
483 | + strncpy((char *)hdr->ih_name, name, IH_NMLEN); | |
484 | + | |
485 | + checksum = crc32(0,(const char *)hdr,sizeof(image_header_t)); | |
486 | + | |
487 | + hdr->ih_hcrc = htonl(checksum); | |
488 | + | |
489 | + print_header (hdr); | |
490 | + | |
491 | + (void) munmap((void *)ptr, sbuf.st_size); | |
492 | + | |
493 | + /* We're a bit of paranoid */ | |
494 | + (void) fsync (ifd); | |
495 | + | |
496 | + if (close(ifd)) { | |
497 | + fprintf (stderr, "%s: Write error on %s: %s\n", | |
498 | + cmdname, imagefile, strerror(errno)); | |
499 | + exit (EXIT_FAILURE); | |
500 | + } | |
501 | + | |
502 | + exit (EXIT_SUCCESS); | |
503 | +} | |
504 | + | |
505 | +static void | |
506 | +copy_file (int ifd, const char *datafile, int pad) | |
507 | +{ | |
508 | + int dfd; | |
509 | + struct stat sbuf; | |
510 | + unsigned char *ptr; | |
511 | + int tail; | |
512 | + int zero = 0; | |
513 | + int offset = 0; | |
514 | + int size; | |
515 | + | |
516 | + if (vflag) { | |
517 | + fprintf (stderr, "Adding Image %s\n", datafile); | |
518 | + } | |
519 | + | |
520 | + if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) { | |
521 | + fprintf (stderr, "%s: Can't open %s: %s\n", | |
522 | + cmdname, datafile, strerror(errno)); | |
523 | + exit (EXIT_FAILURE); | |
524 | + } | |
525 | + | |
526 | + if (fstat(dfd, &sbuf) < 0) { | |
527 | + fprintf (stderr, "%s: Can't stat %s: %s\n", | |
528 | + cmdname, datafile, strerror(errno)); | |
529 | + exit (EXIT_FAILURE); | |
530 | + } | |
531 | + | |
532 | + ptr = (unsigned char *)mmap(0, sbuf.st_size, | |
533 | + PROT_READ, MAP_SHARED, dfd, 0); | |
534 | + if (ptr == (unsigned char *)MAP_FAILED) { | |
535 | + fprintf (stderr, "%s: Can't read %s: %s\n", | |
536 | + cmdname, datafile, strerror(errno)); | |
537 | + exit (EXIT_FAILURE); | |
538 | + } | |
539 | + | |
540 | + if (xflag) { | |
541 | + unsigned char *p = NULL; | |
542 | + /* | |
543 | + * XIP: do not append the image_header_t at the | |
544 | + * beginning of the file, but consume the space | |
545 | + * reserved for it. | |
546 | + */ | |
547 | + | |
548 | + if ((unsigned)sbuf.st_size < sizeof(image_header_t)) { | |
549 | + fprintf (stderr, | |
550 | + "%s: Bad size: \"%s\" is too small for XIP\n", | |
551 | + cmdname, datafile); | |
552 | + exit (EXIT_FAILURE); | |
553 | + } | |
554 | + | |
555 | + for (p=ptr; p < ptr+sizeof(image_header_t); p++) { | |
556 | + if ( *p != 0xff ) { | |
557 | + fprintf (stderr, | |
558 | + "%s: Bad file: \"%s\" has invalid buffer for XIP\n", | |
559 | + cmdname, datafile); | |
560 | + exit (EXIT_FAILURE); | |
561 | + } | |
562 | + } | |
563 | + | |
564 | + offset = sizeof(image_header_t); | |
565 | + } | |
566 | + | |
567 | + size = sbuf.st_size - offset; | |
568 | + if (write(ifd, ptr + offset, size) != size) { | |
569 | + fprintf (stderr, "%s: Write error on %s: %s\n", | |
570 | + cmdname, imagefile, strerror(errno)); | |
571 | + exit (EXIT_FAILURE); | |
572 | + } | |
573 | + | |
574 | + if (pad && ((tail = size % 4) != 0)) { | |
575 | + | |
576 | + if (write(ifd, (char *)&zero, 4-tail) != 4-tail) { | |
577 | + fprintf (stderr, "%s: Write error on %s: %s\n", | |
578 | + cmdname, imagefile, strerror(errno)); | |
579 | + exit (EXIT_FAILURE); | |
580 | + } | |
581 | + } | |
582 | + | |
583 | + (void) munmap((void *)ptr, sbuf.st_size); | |
584 | + (void) close (dfd); | |
585 | +} | |
586 | + | |
587 | +void | |
588 | +usage () | |
589 | +{ | |
590 | + fprintf (stderr, "Usage: %s -l image\n" | |
591 | + " -l ==> list image header information\n" | |
592 | + " %s [-x] -A arch -O os -T type -C comp " | |
593 | + "-a addr -e ep -n name -d data_file[:data_file...] image\n", | |
594 | + cmdname, cmdname); | |
595 | + fprintf (stderr, " -A ==> set architecture to 'arch'\n" | |
596 | + " -O ==> set operating system to 'os'\n" | |
597 | + " -T ==> set image type to 'type'\n" | |
598 | + " -C ==> set compression type 'comp'\n" | |
599 | + " -a ==> set load address to 'addr' (hex)\n" | |
600 | + " -e ==> set entry point to 'ep' (hex)\n" | |
601 | + " -n ==> set image name to 'name'\n" | |
602 | + " -d ==> use image data from 'datafile'\n" | |
603 | + " -x ==> set XIP (execute in place)\n" | |
604 | + ); | |
605 | + exit (EXIT_FAILURE); | |
606 | +} | |
607 | + | |
608 | +static void | |
609 | +print_header (image_header_t *hdr) | |
610 | +{ | |
611 | + time_t timestamp; | |
612 | + uint32_t size; | |
613 | + | |
614 | + timestamp = (time_t)ntohl(hdr->ih_time); | |
615 | + size = ntohl(hdr->ih_size); | |
616 | + | |
617 | + printf ("Image Name: %.*s\n", IH_NMLEN, hdr->ih_name); | |
618 | + printf ("Created: %s", ctime(×tamp)); | |
619 | + printf ("Image Type: "); print_type(hdr); | |
620 | + printf ("Data Size: %d Bytes = %.2f kB = %.2f MB\n", | |
621 | + size, (double)size / 1.024e3, (double)size / 1.048576e6 ); | |
622 | + printf ("Load Address: 0x%08X\n", ntohl(hdr->ih_load)); | |
623 | + printf ("Entry Point: 0x%08X\n", ntohl(hdr->ih_ep)); | |
624 | + | |
625 | + if (hdr->ih_type == IH_TYPE_MULTI || hdr->ih_type == IH_TYPE_SCRIPT) { | |
626 | + int i, ptrs; | |
627 | + uint32_t pos; | |
628 | + unsigned long *len_ptr = (unsigned long *) ( | |
629 | + (unsigned long)hdr + sizeof(image_header_t) | |
630 | + ); | |
631 | + | |
632 | + /* determine number of images first (to calculate image offsets) */ | |
633 | + for (i=0; len_ptr[i]; ++i) /* null pointer terminates list */ | |
634 | + ; | |
635 | + ptrs = i; /* null pointer terminates list */ | |
636 | + | |
637 | + pos = sizeof(image_header_t) + ptrs * sizeof(long); | |
638 | + printf ("Contents:\n"); | |
639 | + for (i=0; len_ptr[i]; ++i) { | |
640 | + size = ntohl(len_ptr[i]); | |
641 | + | |
642 | + printf (" Image %d: %8d Bytes = %4d kB = %d MB\n", | |
643 | + i, size, size>>10, size>>20); | |
644 | + if (hdr->ih_type == IH_TYPE_SCRIPT && i > 0) { | |
645 | + /* | |
646 | + * the user may need to know offsets | |
647 | + * if planning to do something with | |
648 | + * multiple files | |
649 | + */ | |
650 | + printf (" Offset = %08X\n", pos); | |
651 | + } | |
652 | + /* copy_file() will pad the first files to even word align */ | |
653 | + size += 3; | |
654 | + size &= ~3; | |
655 | + pos += size; | |
656 | + } | |
657 | + } | |
658 | +} | |
659 | + | |
660 | + | |
661 | +static void | |
662 | +print_type (image_header_t *hdr) | |
663 | +{ | |
664 | + printf ("%s %s %s (%s)\n", | |
665 | + put_arch (hdr->ih_arch), | |
666 | + put_os (hdr->ih_os ), | |
667 | + put_type (hdr->ih_type), | |
668 | + put_comp (hdr->ih_comp) | |
669 | + ); | |
670 | +} | |
671 | + | |
672 | +static char *put_arch (int arch) | |
673 | +{ | |
674 | + return (put_table_entry(arch_name, "Unknown Architecture", arch)); | |
675 | +} | |
676 | + | |
677 | +static char *put_os (int os) | |
678 | +{ | |
679 | + return (put_table_entry(os_name, "Unknown OS", os)); | |
680 | +} | |
681 | + | |
682 | +static char *put_type (int type) | |
683 | +{ | |
684 | + return (put_table_entry(type_name, "Unknown Image", type)); | |
685 | +} | |
686 | + | |
687 | +static char *put_comp (int comp) | |
688 | +{ | |
689 | + return (put_table_entry(comp_name, "Unknown Compression", comp)); | |
690 | +} | |
691 | + | |
692 | +static char *put_table_entry (table_entry_t *table, char *msg, int type) | |
693 | +{ | |
694 | + for (; table->val>=0; ++table) { | |
695 | + if (table->val == type) | |
696 | + return (table->lname); | |
697 | + } | |
698 | + return (msg); | |
699 | +} | |
700 | + | |
701 | +static int get_arch(char *name) | |
702 | +{ | |
703 | + return (get_table_entry(arch_name, "CPU", name)); | |
704 | +} | |
705 | + | |
706 | + | |
707 | +static int get_comp(char *name) | |
708 | +{ | |
709 | + return (get_table_entry(comp_name, "Compression", name)); | |
710 | +} | |
711 | + | |
712 | + | |
713 | +static int get_os (char *name) | |
714 | +{ | |
715 | + return (get_table_entry(os_name, "OS", name)); | |
716 | +} | |
717 | + | |
718 | + | |
719 | +static int get_type(char *name) | |
720 | +{ | |
721 | + return (get_table_entry(type_name, "Image", name)); | |
722 | +} | |
723 | + | |
724 | +static int get_table_entry (table_entry_t *table, char *msg, char *name) | |
725 | +{ | |
726 | + table_entry_t *t; | |
727 | + int first = 1; | |
728 | + | |
729 | + for (t=table; t->val>=0; ++t) { | |
730 | + if (t->sname && strcasecmp(t->sname, name)==0) | |
731 | + return (t->val); | |
732 | + } | |
733 | + fprintf (stderr, "\nInvalid %s Type - valid names are", msg); | |
734 | + for (t=table; t->val>=0; ++t) { | |
735 | + if (t->sname == NULL) | |
736 | + continue; | |
737 | + fprintf (stderr, "%c %s", (first) ? ':' : ',', t->sname); | |
738 | + first = 0; | |
739 | + } | |
740 | + fprintf (stderr, "\n"); | |
741 | + return (-1); | |
742 | +} |
@@ -1,190 +0,0 @@ | ||
1 | - | |
2 | - Copyright (c) 2005-2008, The Android Open Source Project | |
3 | - | |
4 | - Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | - you may not use this file except in compliance with the License. | |
6 | - | |
7 | - Unless required by applicable law or agreed to in writing, software | |
8 | - distributed under the License is distributed on an "AS IS" BASIS, | |
9 | - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
10 | - See the License for the specific language governing permissions and | |
11 | - limitations under the License. | |
12 | - | |
13 | - | |
14 | - Apache License | |
15 | - Version 2.0, January 2004 | |
16 | - http://www.apache.org/licenses/ | |
17 | - | |
18 | - TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION | |
19 | - | |
20 | - 1. Definitions. | |
21 | - | |
22 | - "License" shall mean the terms and conditions for use, reproduction, | |
23 | - and distribution as defined by Sections 1 through 9 of this document. | |
24 | - | |
25 | - "Licensor" shall mean the copyright owner or entity authorized by | |
26 | - the copyright owner that is granting the License. | |
27 | - | |
28 | - "Legal Entity" shall mean the union of the acting entity and all | |
29 | - other entities that control, are controlled by, or are under common | |
30 | - control with that entity. For the purposes of this definition, | |
31 | - "control" means (i) the power, direct or indirect, to cause the | |
32 | - direction or management of such entity, whether by contract or | |
33 | - otherwise, or (ii) ownership of fifty percent (50%) or more of the | |
34 | - outstanding shares, or (iii) beneficial ownership of such entity. | |
35 | - | |
36 | - "You" (or "Your") shall mean an individual or Legal Entity | |
37 | - exercising permissions granted by this License. | |
38 | - | |
39 | - "Source" form shall mean the preferred form for making modifications, | |
40 | - including but not limited to software source code, documentation | |
41 | - source, and configuration files. | |
42 | - | |
43 | - "Object" form shall mean any form resulting from mechanical | |
44 | - transformation or translation of a Source form, including but | |
45 | - not limited to compiled object code, generated documentation, | |
46 | - and conversions to other media types. | |
47 | - | |
48 | - "Work" shall mean the work of authorship, whether in Source or | |
49 | - Object form, made available under the License, as indicated by a | |
50 | - copyright notice that is included in or attached to the work | |
51 | - (an example is provided in the Appendix below). | |
52 | - | |
53 | - "Derivative Works" shall mean any work, whether in Source or Object | |
54 | - form, that is based on (or derived from) the Work and for which the | |
55 | - editorial revisions, annotations, elaborations, or other modifications | |
56 | - represent, as a whole, an original work of authorship. For the purposes | |
57 | - of this License, Derivative Works shall not include works that remain | |
58 | - separable from, or merely link (or bind by name) to the interfaces of, | |
59 | - the Work and Derivative Works thereof. | |
60 | - | |
61 | - "Contribution" shall mean any work of authorship, including | |
62 | - the original version of the Work and any modifications or additions | |
63 | - to that Work or Derivative Works thereof, that is intentionally | |
64 | - submitted to Licensor for inclusion in the Work by the copyright owner | |
65 | - or by an individual or Legal Entity authorized to submit on behalf of | |
66 | - the copyright owner. For the purposes of this definition, "submitted" | |
67 | - means any form of electronic, verbal, or written communication sent | |
68 | - to the Licensor or its representatives, including but not limited to | |
69 | - communication on electronic mailing lists, source code control systems, | |
70 | - and issue tracking systems that are managed by, or on behalf of, the | |
71 | - Licensor for the purpose of discussing and improving the Work, but | |
72 | - excluding communication that is conspicuously marked or otherwise | |
73 | - designated in writing by the copyright owner as "Not a Contribution." | |
74 | - | |
75 | - "Contributor" shall mean Licensor and any individual or Legal Entity | |
76 | - on behalf of whom a Contribution has been received by Licensor and | |
77 | - subsequently incorporated within the Work. | |
78 | - | |
79 | - 2. Grant of Copyright License. Subject to the terms and conditions of | |
80 | - this License, each Contributor hereby grants to You a perpetual, | |
81 | - worldwide, non-exclusive, no-charge, royalty-free, irrevocable | |
82 | - copyright license to reproduce, prepare Derivative Works of, | |
83 | - publicly display, publicly perform, sublicense, and distribute the | |
84 | - Work and such Derivative Works in Source or Object form. | |
85 | - | |
86 | - 3. Grant of Patent License. Subject to the terms and conditions of | |
87 | - this License, each Contributor hereby grants to You a perpetual, | |
88 | - worldwide, non-exclusive, no-charge, royalty-free, irrevocable | |
89 | - (except as stated in this section) patent license to make, have made, | |
90 | - use, offer to sell, sell, import, and otherwise transfer the Work, | |
91 | - where such license applies only to those patent claims licensable | |
92 | - by such Contributor that are necessarily infringed by their | |
93 | - Contribution(s) alone or by combination of their Contribution(s) | |
94 | - with the Work to which such Contribution(s) was submitted. If You | |
95 | - institute patent litigation against any entity (including a | |
96 | - cross-claim or counterclaim in a lawsuit) alleging that the Work | |
97 | - or a Contribution incorporated within the Work constitutes direct | |
98 | - or contributory patent infringement, then any patent licenses | |
99 | - granted to You under this License for that Work shall terminate | |
100 | - as of the date such litigation is filed. | |
101 | - | |
102 | - 4. Redistribution. You may reproduce and distribute copies of the | |
103 | - Work or Derivative Works thereof in any medium, with or without | |
104 | - modifications, and in Source or Object form, provided that You | |
105 | - meet the following conditions: | |
106 | - | |
107 | - (a) You must give any other recipients of the Work or | |
108 | - Derivative Works a copy of this License; and | |
109 | - | |
110 | - (b) You must cause any modified files to carry prominent notices | |
111 | - stating that You changed the files; and | |
112 | - | |
113 | - (c) You must retain, in the Source form of any Derivative Works | |
114 | - that You distribute, all copyright, patent, trademark, and | |
115 | - attribution notices from the Source form of the Work, | |
116 | - excluding those notices that do not pertain to any part of | |
117 | - the Derivative Works; and | |
118 | - | |
119 | - (d) If the Work includes a "NOTICE" text file as part of its | |
120 | - distribution, then any Derivative Works that You distribute must | |
121 | - include a readable copy of the attribution notices contained | |
122 | - within such NOTICE file, excluding those notices that do not | |
123 | - pertain to any part of the Derivative Works, in at least one | |
124 | - of the following places: within a NOTICE text file distributed | |
125 | - as part of the Derivative Works; within the Source form or | |
126 | - documentation, if provided along with the Derivative Works; or, | |
127 | - within a display generated by the Derivative Works, if and | |
128 | - wherever such third-party notices normally appear. The contents | |
129 | - of the NOTICE file are for informational purposes only and | |
130 | - do not modify the License. You may add Your own attribution | |
131 | - notices within Derivative Works that You distribute, alongside | |
132 | - or as an addendum to the NOTICE text from the Work, provided | |
133 | - that such additional attribution notices cannot be construed | |
134 | - as modifying the License. | |
135 | - | |
136 | - You may add Your own copyright statement to Your modifications and | |
137 | - may provide additional or different license terms and conditions | |
138 | - for use, reproduction, or distribution of Your modifications, or | |
139 | - for any such Derivative Works as a whole, provided Your use, | |
140 | - reproduction, and distribution of the Work otherwise complies with | |
141 | - the conditions stated in this License. | |
142 | - | |
143 | - 5. Submission of Contributions. Unless You explicitly state otherwise, | |
144 | - any Contribution intentionally submitted for inclusion in the Work | |
145 | - by You to the Licensor shall be under the terms and conditions of | |
146 | - this License, without any additional terms or conditions. | |
147 | - Notwithstanding the above, nothing herein shall supersede or modify | |
148 | - the terms of any separate license agreement you may have executed | |
149 | - with Licensor regarding such Contributions. | |
150 | - | |
151 | - 6. Trademarks. This License does not grant permission to use the trade | |
152 | - names, trademarks, service marks, or product names of the Licensor, | |
153 | - except as required for reasonable and customary use in describing the | |
154 | - origin of the Work and reproducing the content of the NOTICE file. | |
155 | - | |
156 | - 7. Disclaimer of Warranty. Unless required by applicable law or | |
157 | - agreed to in writing, Licensor provides the Work (and each | |
158 | - Contributor provides its Contributions) on an "AS IS" BASIS, | |
159 | - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | |
160 | - implied, including, without limitation, any warranties or conditions | |
161 | - of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A | |
162 | - PARTICULAR PURPOSE. You are solely responsible for determining the | |
163 | - appropriateness of using or redistributing the Work and assume any | |
164 | - risks associated with Your exercise of permissions under this License. | |
165 | - | |
166 | - 8. Limitation of Liability. In no event and under no legal theory, | |
167 | - whether in tort (including negligence), contract, or otherwise, | |
168 | - unless required by applicable law (such as deliberate and grossly | |
169 | - negligent acts) or agreed to in writing, shall any Contributor be | |
170 | - liable to You for damages, including any direct, indirect, special, | |
171 | - incidental, or consequential damages of any character arising as a | |
172 | - result of this License or out of the use or inability to use the | |
173 | - Work (including but not limited to damages for loss of goodwill, | |
174 | - work stoppage, computer failure or malfunction, or any and all | |
175 | - other commercial damages or losses), even if such Contributor | |
176 | - has been advised of the possibility of such damages. | |
177 | - | |
178 | - 9. Accepting Warranty or Additional Liability. While redistributing | |
179 | - the Work or Derivative Works thereof, You may choose to offer, | |
180 | - and charge a fee for, acceptance of support, warranty, indemnity, | |
181 | - or other liability obligations and/or rights consistent with this | |
182 | - License. However, in accepting such obligations, You may act only | |
183 | - on Your own behalf and on Your sole responsibility, not on behalf | |
184 | - of any other Contributor, and only if You agree to indemnify, | |
185 | - defend, and hold each Contributor harmless for any liability | |
186 | - incurred by, or claims asserted against, such Contributor by reason | |
187 | - of your accepting any such warranty or additional liability. | |
188 | - | |
189 | - END OF TERMS AND CONDITIONS | |
190 | - |
@@ -1,145 +0,0 @@ | ||
1 | -/* | |
2 | - * Copyright (C) 2008 The Android Open Source Project | |
3 | - * | |
4 | - * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | - * you may not use this file except in compliance with the License. | |
6 | - * You may obtain a copy of the License at | |
7 | - * | |
8 | - * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | - * | |
10 | - * Unless required by applicable law or agreed to in writing, software | |
11 | - * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | - * See the License for the specific language governing permissions and | |
14 | - * limitations under the License. | |
15 | - */ | |
16 | - | |
17 | -#include <errno.h> | |
18 | -#include <error.h> | |
19 | -#include <getopt.h> | |
20 | -#include <paths.h> | |
21 | -#include <pwd.h> | |
22 | -#include <stdbool.h> | |
23 | -#include <stdio.h> | |
24 | -#include <stdlib.h> | |
25 | -#include <string.h> | |
26 | -#include <unistd.h> | |
27 | - | |
28 | -#include <private/android_filesystem_config.h> | |
29 | - | |
30 | -void pwtoid(const char* tok, uid_t* uid, gid_t* gid) { | |
31 | - struct passwd* pw = getpwnam(tok); | |
32 | - if (pw) { | |
33 | - if (uid) *uid = pw->pw_uid; | |
34 | - if (gid) *gid = pw->pw_gid; | |
35 | - } else { | |
36 | - char* end; | |
37 | - errno = 0; | |
38 | - uid_t tmpid = strtoul(tok, &end, 10); | |
39 | - if (errno != 0 || end == tok) error(1, errno, "invalid uid/gid '%s'", tok); | |
40 | - if (uid) *uid = tmpid; | |
41 | - if (gid) *gid = tmpid; | |
42 | - } | |
43 | -} | |
44 | - | |
45 | -void extract_uidgids(const char* uidgids, uid_t* uid, gid_t* gid, gid_t* gids, int* gids_count) { | |
46 | - char *clobberablegids; | |
47 | - char *nexttok; | |
48 | - char *tok; | |
49 | - int gids_found; | |
50 | - | |
51 | - if (!uidgids || !*uidgids) { | |
52 | - *gid = *uid = 0; | |
53 | - *gids_count = 0; | |
54 | - return; | |
55 | - } | |
56 | - | |
57 | - clobberablegids = strdup(uidgids); | |
58 | - strcpy(clobberablegids, uidgids); | |
59 | - nexttok = clobberablegids; | |
60 | - tok = strsep(&nexttok, ","); | |
61 | - pwtoid(tok, uid, gid); | |
62 | - tok = strsep(&nexttok, ","); | |
63 | - if (!tok) { | |
64 | - /* gid is already set above */ | |
65 | - *gids_count = 0; | |
66 | - free(clobberablegids); | |
67 | - return; | |
68 | - } | |
69 | - pwtoid(tok, NULL, gid); | |
70 | - gids_found = 0; | |
71 | - while ((gids_found < *gids_count) && (tok = strsep(&nexttok, ","))) { | |
72 | - pwtoid(tok, NULL, gids); | |
73 | - gids_found++; | |
74 | - gids++; | |
75 | - } | |
76 | - if (nexttok && gids_found == *gids_count) { | |
77 | - fprintf(stderr, "too many group ids\n"); | |
78 | - } | |
79 | - *gids_count = gids_found; | |
80 | - free(clobberablegids); | |
81 | -} | |
82 | - | |
83 | -int main(int argc, char** argv) { | |
84 | - uid_t current_uid = getuid(); | |
85 | - if (current_uid != AID_ROOT && current_uid != AID_SHELL) error(1, 0, "not allowed"); | |
86 | - | |
87 | - // Handle -h and --help. | |
88 | - ++argv; | |
89 | - if (*argv && (strcmp(*argv, "--help") == 0 || strcmp(*argv, "-h") == 0)) { | |
90 | - fprintf(stderr, | |
91 | - "usage: su [UID[,GID[,GID2]...]] [COMMAND [ARG...]]\n" | |
92 | - "\n" | |
93 | - "Switch to WHO (default 'root') and run the given command (default sh).\n" | |
94 | - "\n" | |
95 | - "where WHO is a comma-separated list of user, group,\n" | |
96 | - "and supplementary groups in that order.\n" | |
97 | - "\n"); | |
98 | - return 0; | |
99 | - } | |
100 | - | |
101 | - // The default user is root. | |
102 | - uid_t uid = 0; | |
103 | - gid_t gid = 0; | |
104 | - | |
105 | - // If there are any arguments, the first argument is the uid/gid/supplementary groups. | |
106 | - if (*argv) { | |
107 | - gid_t gids[10]; | |
108 | - int gids_count = sizeof(gids)/sizeof(gids[0]); | |
109 | - extract_uidgids(*argv, &uid, &gid, gids, &gids_count); | |
110 | - if (gids_count) { | |
111 | - if (setgroups(gids_count, gids)) { | |
112 | - error(1, errno, "setgroups failed"); | |
113 | - } | |
114 | - } | |
115 | - ++argv; | |
116 | - } | |
117 | - | |
118 | - if (setgid(gid)) error(1, errno, "setgid failed"); | |
119 | - if (setuid(uid)) error(1, errno, "setuid failed"); | |
120 | - | |
121 | - // Reset parts of the environment. | |
122 | - setenv("PATH", _PATH_DEFPATH, 1); | |
123 | - unsetenv("IFS"); | |
124 | - struct passwd* pw = getpwuid(uid); | |
125 | - if (pw) { | |
126 | - setenv("LOGNAME", pw->pw_name, 1); | |
127 | - setenv("USER", pw->pw_name, 1); | |
128 | - } else { | |
129 | - unsetenv("LOGNAME"); | |
130 | - unsetenv("USER"); | |
131 | - } | |
132 | - | |
133 | - // Set up the arguments for exec. | |
134 | - char* exec_args[argc + 1]; // Having too much space is fine. | |
135 | - size_t i = 0; | |
136 | - for (; *argv != NULL; ++i) { | |
137 | - exec_args[i] = *argv++; | |
138 | - } | |
139 | - // Default to the standard shell. | |
140 | - if (i == 0) exec_args[i++] = "/system/bin/sh"; | |
141 | - exec_args[i] = NULL; | |
142 | - | |
143 | - execvp(exec_args[0], exec_args); | |
144 | - error(1, errno, "failed to exec %s", exec_args[0]); | |
145 | -} |
@@ -18,7 +18,7 @@ LOCAL_MODULE := generate_verity_key | ||
18 | 18 | LOCAL_SRC_FILES := generate_verity_key.c |
19 | 19 | LOCAL_MODULE_CLASS := EXECUTABLES |
20 | 20 | LOCAL_MODULE_TAGS := optional |
21 | -LOCAL_SHARED_LIBRARIES := libcrypto-host | |
21 | +LOCAL_SHARED_LIBRARIES := libcrypto_utils libcrypto-host | |
22 | 22 | include $(BUILD_HOST_EXECUTABLE) |
23 | 23 | |
24 | 24 | include $(CLEAR_VARS) |
@@ -203,11 +203,13 @@ public class BootSignature extends ASN1Object | ||
203 | 203 | int secondSize = image.getInt(); |
204 | 204 | image.getLong(); // second_addr + tags_addr |
205 | 205 | int pageSize = image.getInt(); |
206 | + int dtSize = image.getInt(); //device tree size | |
206 | 207 | |
207 | 208 | int length = pageSize // include the page aligned image header |
208 | 209 | + ((kernelSize + pageSize - 1) / pageSize) * pageSize |
209 | 210 | + ((ramdskSize + pageSize - 1) / pageSize) * pageSize |
210 | - + ((secondSize + pageSize - 1) / pageSize) * pageSize; | |
211 | + + ((secondSize + pageSize - 1) / pageSize) * pageSize | |
212 | + + ((dtSize + pageSize - 1) / pageSize) * pageSize; | |
211 | 213 | |
212 | 214 | length = ((length + pageSize - 1) / pageSize) * pageSize; |
213 | 215 |
@@ -18,6 +18,7 @@ package com.android.verity; | ||
18 | 18 | |
19 | 19 | import java.lang.reflect.Constructor; |
20 | 20 | import java.io.File; |
21 | +import java.io.BufferedReader; | |
21 | 22 | import java.io.ByteArrayInputStream; |
22 | 23 | import java.io.Console; |
23 | 24 | import java.io.FileInputStream; |
@@ -61,6 +62,7 @@ public class Utils { | ||
61 | 62 | |
62 | 63 | private static final Map<String, String> ID_TO_ALG; |
63 | 64 | private static final Map<String, String> ALG_TO_ID; |
65 | + private static String mKeyPath; | |
64 | 66 | |
65 | 67 | static { |
66 | 68 | ID_TO_ALG = new HashMap<String, String>(); |
@@ -145,7 +147,19 @@ public class Utils { | ||
145 | 147 | return null; |
146 | 148 | } |
147 | 149 | |
148 | - char[] password = System.console().readPassword("Password for the private key file: "); | |
150 | + char[] password = null; | |
151 | + if (System.console() == null) { | |
152 | + System.out.print("Enter password for " + mKeyPath + " (password will not be hidden): "); | |
153 | + System.out.flush(); | |
154 | + BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); | |
155 | + try { | |
156 | + password = stdin.readLine().toCharArray(); | |
157 | + } catch (IOException ex) { | |
158 | + return null; | |
159 | + } | |
160 | + } else { | |
161 | + password = System.console().readPassword("Enter password for " + mKeyPath + " key>"); | |
162 | + } | |
149 | 163 | |
150 | 164 | SecretKeyFactory skFactory = SecretKeyFactory.getInstance(epkInfo.getAlgName()); |
151 | 165 | Key key = skFactory.generateSecret(new PBEKeySpec(password)); |
@@ -182,10 +196,12 @@ public class Utils { | ||
182 | 196 | } |
183 | 197 | |
184 | 198 | static PrivateKey loadPEMPrivateKeyFromFile(String keyFname) throws Exception { |
199 | + mKeyPath = keyFname.replace(".pk8",""); | |
185 | 200 | return loadPEMPrivateKey(read(keyFname)); |
186 | 201 | } |
187 | 202 | |
188 | 203 | static PrivateKey loadDERPrivateKeyFromFile(String keyFname) throws Exception { |
204 | + mKeyPath = keyFname.replace(".pk8",""); | |
189 | 205 | return loadDERPrivateKey(read(keyFname)); |
190 | 206 | } |
191 | 207 |
@@ -203,10 +219,12 @@ public class Utils { | ||
203 | 219 | } |
204 | 220 | |
205 | 221 | static PublicKey loadPEMPublicKeyFromFile(String keyFname) throws Exception { |
222 | + mKeyPath = keyFname.replace(".x509.pem",""); | |
206 | 223 | return loadPEMPublicKey(read(keyFname)); |
207 | 224 | } |
208 | 225 | |
209 | 226 | static PublicKey loadDERPublicKeyFromFile(String keyFname) throws Exception { |
227 | + mKeyPath = keyFname.replace(".x509.der",""); | |
210 | 228 | return loadDERPublicKey(read(keyFname)); |
211 | 229 | } |
212 | 230 |
@@ -61,7 +61,7 @@ public class VeritySigner { | ||
61 | 61 | |
62 | 62 | System.exit(1); |
63 | 63 | } else { |
64 | - PrivateKey privateKey = Utils.loadDERPrivateKey(Utils.read(args[1])); | |
64 | + PrivateKey privateKey = Utils.loadDERPrivateKeyFromFile(args[1]); | |
65 | 65 | byte[] signature = Utils.sign(privateKey, content); |
66 | 66 | Utils.write(signature, args[2]); |
67 | 67 | } |
@@ -5,6 +5,7 @@ import sys | ||
5 | 5 | import struct |
6 | 6 | import tempfile |
7 | 7 | import commands |
8 | +import subprocess | |
8 | 9 | |
9 | 10 | VERSION = 0 |
10 | 11 | MAGIC_NUMBER = 0xb001b001 |
@@ -34,7 +35,9 @@ def sign_verity_table(table, signer_path, key_path): | ||
34 | 35 | table_file.flush() |
35 | 36 | cmd = " ".join((signer_path, table_file.name, key_path, signature_file.name)) |
36 | 37 | print cmd |
37 | - run(cmd) | |
38 | + runcmd = cmd.split() | |
39 | + sp = subprocess.Popen(runcmd) | |
40 | + sp.wait() | |
38 | 41 | return signature_file.read() |
39 | 42 | |
40 | 43 | def build_verity_table(block_device, data_blocks, root_hash, salt): |
@@ -11,6 +11,7 @@ LOCAL_MODULE_TAGS := optional | ||
11 | 11 | LOCAL_STATIC_LIBRARIES := \ |
12 | 12 | libsparse_host \ |
13 | 13 | libz \ |
14 | + libcrypto_utils_static \ | |
14 | 15 | libcrypto_static \ |
15 | 16 | libfec_host \ |
16 | 17 | libfec_rs_host \ |
@@ -29,6 +30,7 @@ LOCAL_FORCE_STATIC_EXECUTABLE := true | ||
29 | 30 | LOCAL_SRC_FILES := main.cpp image.cpp |
30 | 31 | LOCAL_MODULE_TAGS := optional |
31 | 32 | LOCAL_STATIC_LIBRARIES := \ |
33 | + libcrypto_utils_static \ | |
32 | 34 | libcrypto_static \ |
33 | 35 | libfec \ |
34 | 36 | libfec_rs \ |
@@ -23,11 +23,7 @@ | ||
23 | 23 | #include <sys/types.h> |
24 | 24 | #include <unistd.h> |
25 | 25 | |
26 | -/* HACK: we need the RSAPublicKey struct | |
27 | - * but RSA_verify conflits with openssl */ | |
28 | -#define RSA_verify RSA_verify_mincrypt | |
29 | -#include "mincrypt/rsa.h" | |
30 | -#undef RSA_verify | |
26 | +#include <crypto_utils/android_pubkey.h> | |
31 | 27 | |
32 | 28 | #include <openssl/evp.h> |
33 | 29 | #include <openssl/objects.h> |
@@ -35,58 +31,9 @@ | ||
35 | 31 | #include <openssl/rsa.h> |
36 | 32 | #include <openssl/sha.h> |
37 | 33 | |
38 | -// Convert OpenSSL RSA private key to android pre-computed RSAPublicKey format. | |
39 | -// Lifted from secure adb's mincrypt key generation. | |
40 | -static int convert_to_mincrypt_format(RSA *rsa, RSAPublicKey *pkey) | |
41 | -{ | |
42 | - int ret = -1; | |
43 | - unsigned int i; | |
44 | - | |
45 | - if (RSA_size(rsa) != RSANUMBYTES) | |
46 | - goto out; | |
47 | - | |
48 | - BN_CTX* ctx = BN_CTX_new(); | |
49 | - BIGNUM* r32 = BN_new(); | |
50 | - BIGNUM* rr = BN_new(); | |
51 | - BIGNUM* r = BN_new(); | |
52 | - BIGNUM* rem = BN_new(); | |
53 | - BIGNUM* n = BN_new(); | |
54 | - BIGNUM* n0inv = BN_new(); | |
55 | - | |
56 | - BN_set_bit(r32, 32); | |
57 | - BN_copy(n, rsa->n); | |
58 | - BN_set_bit(r, RSANUMWORDS * 32); | |
59 | - BN_mod_sqr(rr, r, n, ctx); | |
60 | - BN_div(NULL, rem, n, r32, ctx); | |
61 | - BN_mod_inverse(n0inv, rem, r32, ctx); | |
62 | - | |
63 | - pkey->len = RSANUMWORDS; | |
64 | - pkey->n0inv = 0 - BN_get_word(n0inv); | |
65 | - for (i = 0; i < RSANUMWORDS; i++) { | |
66 | - BN_div(rr, rem, rr, r32, ctx); | |
67 | - pkey->rr[i] = BN_get_word(rem); | |
68 | - BN_div(n, rem, n, r32, ctx); | |
69 | - pkey->n[i] = BN_get_word(rem); | |
70 | - } | |
71 | - pkey->exponent = BN_get_word(rsa->e); | |
72 | - | |
73 | - ret = 0; | |
74 | - | |
75 | - BN_free(n0inv); | |
76 | - BN_free(n); | |
77 | - BN_free(rem); | |
78 | - BN_free(r); | |
79 | - BN_free(rr); | |
80 | - BN_free(r32); | |
81 | - BN_CTX_free(ctx); | |
82 | - | |
83 | -out: | |
84 | - return ret; | |
85 | -} | |
86 | - | |
87 | 34 | static int write_public_keyfile(RSA *private_key, const char *private_key_path) |
88 | 35 | { |
89 | - RSAPublicKey pkey; | |
36 | + uint8_t key_data[ANDROID_PUBKEY_ENCODED_SIZE]; | |
90 | 37 | BIO *bfile = NULL; |
91 | 38 | char *path = NULL; |
92 | 39 | int ret = -1; |
@@ -94,14 +41,14 @@ static int write_public_keyfile(RSA *private_key, const char *private_key_path) | ||
94 | 41 | if (asprintf(&path, "%s.pub", private_key_path) < 0) |
95 | 42 | goto out; |
96 | 43 | |
97 | - if (convert_to_mincrypt_format(private_key, &pkey) < 0) | |
44 | + if (!android_pubkey_encode(private_key, key_data, sizeof(key_data))) | |
98 | 45 | goto out; |
99 | 46 | |
100 | 47 | bfile = BIO_new_file(path, "w"); |
101 | 48 | if (!bfile) |
102 | 49 | goto out; |
103 | 50 | |
104 | - BIO_write(bfile, &pkey, sizeof(pkey)); | |
51 | + BIO_write(bfile, key_data, sizeof(key_data)); | |
105 | 52 | BIO_flush(bfile); |
106 | 53 | |
107 | 54 | ret = 0; |