diff options
author | Amaury Pouly <amaury.pouly@gmail.com> | 2012-12-02 11:48:57 +0100 |
---|---|---|
committer | Amaury Pouly <amaury.pouly@gmail.com> | 2012-12-02 11:51:50 +0100 |
commit | 9f19209c77b2131e89d3e7fb12771fb26ef491b3 (patch) | |
tree | 54d2e66bff0cdf391803da76db03a78b25571a59 | |
parent | a6713a5e3632523de67e2354799e4359efbc7773 (diff) | |
download | rockbox-9f19209c77b2131e89d3e7fb12771fb26ef491b3.tar.gz rockbox-9f19209c77b2131e89d3e7fb12771fb26ef491b3.zip |
rsrctool: produce an actually usuable entry list of the rsrc file
Change-Id: I6c8e5f3faf04741e4a13c1e705e9e869ccf8cfec
-rw-r--r-- | utils/imxtools/sbtools/misc.c | 20 | ||||
-rw-r--r-- | utils/imxtools/sbtools/misc.h | 2 | ||||
-rw-r--r-- | utils/imxtools/sbtools/rsrc.c | 36 | ||||
-rw-r--r-- | utils/imxtools/sbtools/rsrc.h | 19 | ||||
-rw-r--r-- | utils/imxtools/sbtools/rsrctool.c | 6 |
5 files changed, 71 insertions, 12 deletions
diff --git a/utils/imxtools/sbtools/misc.c b/utils/imxtools/sbtools/misc.c index d88b2bc858..dae4f92121 100644 --- a/utils/imxtools/sbtools/misc.c +++ b/utils/imxtools/sbtools/misc.c | |||
@@ -84,6 +84,26 @@ void *augment_array(void *arr, size_t elem_sz, size_t cnt, void *aug, size_t aug | |||
84 | return p; | 84 | return p; |
85 | } | 85 | } |
86 | 86 | ||
87 | void augment_array_ex(void **arr, size_t elem_sz, int *cnt, int *capacity, | ||
88 | void *aug, int aug_cnt) | ||
89 | { | ||
90 | /* if capacity is not large enough, double it */ | ||
91 | if(*cnt + aug_cnt > *capacity) | ||
92 | { | ||
93 | if(*capacity == 0) | ||
94 | *capacity = 1; | ||
95 | while(*cnt + aug_cnt > *capacity) | ||
96 | *capacity *= 2; | ||
97 | void *p = xmalloc(elem_sz * (*capacity)); | ||
98 | memcpy(p, *arr, elem_sz * (*cnt)); | ||
99 | free(*arr); | ||
100 | *arr = p; | ||
101 | } | ||
102 | /* copy elements */ | ||
103 | memcpy(*arr + elem_sz * (*cnt), aug, elem_sz * aug_cnt); | ||
104 | *cnt += aug_cnt; | ||
105 | } | ||
106 | |||
87 | /** | 107 | /** |
88 | * Key file parsing | 108 | * Key file parsing |
89 | */ | 109 | */ |
diff --git a/utils/imxtools/sbtools/misc.h b/utils/imxtools/sbtools/misc.h index f5bba9164f..438338f4bb 100644 --- a/utils/imxtools/sbtools/misc.h +++ b/utils/imxtools/sbtools/misc.h | |||
@@ -41,6 +41,8 @@ key_array_t g_key_array; | |||
41 | 41 | ||
42 | void *memdup(const void *p, size_t len); | 42 | void *memdup(const void *p, size_t len); |
43 | void *augment_array(void *arr, size_t elem_sz, size_t cnt, void *aug, size_t aug_cnt); | 43 | void *augment_array(void *arr, size_t elem_sz, size_t cnt, void *aug, size_t aug_cnt); |
44 | void augment_array_ex(void **arr, size_t elem_sz, int *cnt, int *capacity, | ||
45 | void *aug, int aug_cnt); | ||
44 | void generate_random_data(void *buf, size_t sz); | 46 | void generate_random_data(void *buf, size_t sz); |
45 | void *xmalloc(size_t s); | 47 | void *xmalloc(size_t s); |
46 | int convxdigit(char digit, byte *val); | 48 | int convxdigit(char digit, byte *val); |
diff --git a/utils/imxtools/sbtools/rsrc.c b/utils/imxtools/sbtools/rsrc.c index da3c418d2c..1d20c5fce8 100644 --- a/utils/imxtools/sbtools/rsrc.c +++ b/utils/imxtools/sbtools/rsrc.c | |||
@@ -103,7 +103,7 @@ static const char *rsrc_table_entry_type_str(int type) | |||
103 | } | 103 | } |
104 | } | 104 | } |
105 | 105 | ||
106 | static bool read_entries(void *buf, int filesize, void *u, | 106 | static bool read_entries(struct rsrc_file_t *f, void *u, |
107 | rsrc_color_printf cprintf, enum rsrc_error_t *err, | 107 | rsrc_color_printf cprintf, enum rsrc_error_t *err, |
108 | int offset, uint32_t base_index, int level, char *prefix) | 108 | int offset, uint32_t base_index, int level, char *prefix) |
109 | { | 109 | { |
@@ -113,24 +113,37 @@ static bool read_entries(void *buf, int filesize, void *u, | |||
113 | cprintf(u, true, GREY, __VA_ARGS__); \ | 113 | cprintf(u, true, GREY, __VA_ARGS__); \ |
114 | return e; } while(0) | 114 | return e; } while(0) |
115 | 115 | ||
116 | if(offset >= filesize) | 116 | if(offset >= f->size) |
117 | fatal(RSRC_FORMAT_ERROR, "Out of bounds at off=%x base=%x level=%d\n ouch\n"); | 117 | fatal(RSRC_FORMAT_ERROR, "Out of bounds at off=%x base=%x level=%d\n ouch\n"); |
118 | if(level < 0) | 118 | if(level < 0) |
119 | fatal(RSRC_FORMAT_ERROR, "Out of levels at off=%x base=%x level=%d\n aie\n"); | 119 | fatal(RSRC_FORMAT_ERROR, "Out of levels at off=%x base=%x level=%d\n aie\n"); |
120 | for(int i = 0; i < 256; i++) | 120 | for(int i = 0; i < 256; i++) |
121 | { | 121 | { |
122 | uint32_t te = *(uint32_t *)(buf + offset + 4 * i); | 122 | uint32_t te = *(uint32_t *)(f->data + offset + 4 * i); |
123 | if(RSRC_TABLE_ENTRY_TYPE(te) == RSRC_TYPE_NONE) | 123 | if(RSRC_TABLE_ENTRY_TYPE(te) == RSRC_TYPE_NONE) |
124 | continue; | 124 | continue; |
125 | uint32_t sz = 0; | 125 | uint32_t sz = 0; |
126 | uint32_t off_off = 0; | ||
126 | if(RSRC_TABLE_ENTRY_TYPE(te) == RSRC_TYPE_VALUE) | 127 | if(RSRC_TABLE_ENTRY_TYPE(te) == RSRC_TYPE_VALUE) |
127 | sz = 2; | 128 | sz = 2; |
128 | else if(RSRC_TABLE_ENTRY_TYPE(te) == RSRC_TYPE_NESTED) | 129 | else if(RSRC_TABLE_ENTRY_TYPE(te) == RSRC_TYPE_NESTED) |
129 | sz = 4 * 256; | 130 | sz = 4 * 256; |
130 | else | 131 | else |
131 | sz = *(uint32_t *)(buf + RSRC_TABLE_ENTRY_OFFSET(te)); | 132 | { |
133 | sz = *(uint32_t *)(f->data + RSRC_TABLE_ENTRY_OFFSET(te)); | ||
134 | off_off = 4; | ||
135 | } | ||
132 | 136 | ||
133 | uint32_t index = base_index | i << (level * 8); | 137 | uint32_t index = base_index | i << (level * 8); |
138 | |||
139 | struct rsrc_entry_t ent; | ||
140 | memset(&ent, 0, sizeof(ent)); | ||
141 | ent.id = index; | ||
142 | ent.offset = RSRC_TABLE_ENTRY_OFFSET(te) + off_off; | ||
143 | ent.size = sz; | ||
144 | |||
145 | augment_array_ex((void **)&f->entries, sizeof(ent), &f->nr_entries, &f->capacity, &ent, 1); | ||
146 | |||
134 | printf(OFF, "%s+-%s%#08x %s[%s]%s[size=%#x]\n", prefix, YELLOW, index, BLUE, | 147 | printf(OFF, "%s+-%s%#08x %s[%s]%s[size=%#x]\n", prefix, YELLOW, index, BLUE, |
135 | rsrc_table_entry_type_str(RSRC_TABLE_ENTRY_TYPE(te)), | 148 | rsrc_table_entry_type_str(RSRC_TABLE_ENTRY_TYPE(te)), |
136 | GREEN, sz); | 149 | GREEN, sz); |
@@ -140,7 +153,7 @@ static bool read_entries(void *buf, int filesize, void *u, | |||
140 | char *p = prefix + strlen(prefix); | 153 | char *p = prefix + strlen(prefix); |
141 | sprintf(p, "%s| ", RED); | 154 | sprintf(p, "%s| ", RED); |
142 | 155 | ||
143 | bool ok = read_entries(buf, filesize, u, cprintf, err, | 156 | bool ok = read_entries(f, u, cprintf, err, |
144 | RSRC_TABLE_ENTRY_OFFSET(te), index, | 157 | RSRC_TABLE_ENTRY_OFFSET(te), index, |
145 | level - 1, prefix); | 158 | level - 1, prefix); |
146 | if(!ok) | 159 | if(!ok) |
@@ -183,17 +196,16 @@ struct rsrc_file_t *rsrc_read_memory(void *_buf, size_t filesize, void *u, | |||
183 | fatal(RSRC_FORMAT_ERROR, "Missing RSRC signature\n"); | 196 | fatal(RSRC_FORMAT_ERROR, "Missing RSRC signature\n"); |
184 | } | 197 | } |
185 | 198 | ||
199 | rsrc_file->data = malloc(filesize); | ||
200 | memcpy(rsrc_file->data, _buf, filesize); | ||
201 | rsrc_file->size = filesize; | ||
202 | |||
186 | printf(BLUE, "Entries\n"); | 203 | printf(BLUE, "Entries\n"); |
187 | char prefix[1024]; | 204 | char prefix[1024]; |
188 | sprintf(prefix, "%s", RED); | 205 | sprintf(prefix, "%s", RED); |
189 | bool ok = read_entries(buf, filesize, u, cprintf, err, | 206 | bool ok = read_entries(rsrc_file, u, cprintf, err, RSRC_SECTOR_SIZE, 0, 3, prefix); |
190 | RSRC_SECTOR_SIZE, 0, 3, prefix); | ||
191 | if(!ok) | 207 | if(!ok) |
192 | fatal(*err, "Error while parsing rsrc table\n"); | 208 | fatal(*err, "Error while parsing rsrc table\n"); |
193 | |||
194 | rsrc_file->data = malloc(filesize); | ||
195 | memcpy(rsrc_file->data, _buf, filesize); | ||
196 | rsrc_file->size = filesize; | ||
197 | 209 | ||
198 | return rsrc_file; | 210 | return rsrc_file; |
199 | #undef printf | 211 | #undef printf |
@@ -204,6 +216,8 @@ struct rsrc_file_t *rsrc_read_memory(void *_buf, size_t filesize, void *u, | |||
204 | void rsrc_free(struct rsrc_file_t *file) | 216 | void rsrc_free(struct rsrc_file_t *file) |
205 | { | 217 | { |
206 | if(!file) return; | 218 | if(!file) return; |
219 | free(file->data); | ||
220 | free(file->entries); | ||
207 | free(file); | 221 | free(file); |
208 | } | 222 | } |
209 | 223 | ||
diff --git a/utils/imxtools/sbtools/rsrc.h b/utils/imxtools/sbtools/rsrc.h index fc310e348d..3e03d97d73 100644 --- a/utils/imxtools/sbtools/rsrc.h +++ b/utils/imxtools/sbtools/rsrc.h | |||
@@ -26,6 +26,10 @@ | |||
26 | 26 | ||
27 | #include "misc.h" | 27 | #include "misc.h" |
28 | 28 | ||
29 | /** | ||
30 | * Low-Level | ||
31 | **/ | ||
32 | |||
29 | #define RSRC_SECTOR_SIZE 2048 | 33 | #define RSRC_SECTOR_SIZE 2048 |
30 | 34 | ||
31 | #define RSRC_TABLE_ENTRY_TYPE(e) ((e) >> 28) | 35 | #define RSRC_TABLE_ENTRY_TYPE(e) ((e) >> 28) |
@@ -38,10 +42,25 @@ | |||
38 | #define RSRC_TYPE_AUDIO 4 /* audio entry */ | 42 | #define RSRC_TYPE_AUDIO 4 /* audio entry */ |
39 | #define RSRC_TYPE_DATA 5 /* data entry */ | 43 | #define RSRC_TYPE_DATA 5 /* data entry */ |
40 | 44 | ||
45 | /** | ||
46 | * API | ||
47 | **/ | ||
48 | |||
49 | struct rsrc_entry_t | ||
50 | { | ||
51 | uint32_t id; | ||
52 | uint32_t offset; // contains value of RSRC_TYPE_VALUE | ||
53 | int size; | ||
54 | }; | ||
55 | |||
41 | struct rsrc_file_t | 56 | struct rsrc_file_t |
42 | { | 57 | { |
43 | void *data; | 58 | void *data; |
44 | int size; | 59 | int size; |
60 | |||
61 | int nr_entries; | ||
62 | int capacity; | ||
63 | struct rsrc_entry_t *entries; | ||
45 | }; | 64 | }; |
46 | 65 | ||
47 | enum rsrc_error_t | 66 | enum rsrc_error_t |
diff --git a/utils/imxtools/sbtools/rsrctool.c b/utils/imxtools/sbtools/rsrctool.c index cb0582245a..53235365ae 100644 --- a/utils/imxtools/sbtools/rsrctool.c +++ b/utils/imxtools/sbtools/rsrctool.c | |||
@@ -52,7 +52,7 @@ char *g_out_prefix; | |||
52 | 52 | ||
53 | static void extract_rsrc_file(struct rsrc_file_t *file) | 53 | static void extract_rsrc_file(struct rsrc_file_t *file) |
54 | { | 54 | { |
55 | 55 | (void) file; | |
56 | } | 56 | } |
57 | 57 | ||
58 | static void usage(void) | 58 | static void usage(void) |
@@ -76,6 +76,8 @@ static void rsrc_printf(void *user, bool error, color_t c, const char *fmt, ...) | |||
76 | { | 76 | { |
77 | (void) user; | 77 | (void) user; |
78 | (void) error; | 78 | (void) error; |
79 | if(!g_debug) | ||
80 | return; | ||
79 | va_list args; | 81 | va_list args; |
80 | va_start(args, fmt); | 82 | va_start(args, fmt); |
81 | color(c); | 83 | color(c); |
@@ -176,6 +178,8 @@ int main(int argc, char **argv) | |||
176 | printf("RSRC read failed: %d\n", err); | 178 | printf("RSRC read failed: %d\n", err); |
177 | return 1; | 179 | return 1; |
178 | } | 180 | } |
181 | if(g_debug) | ||
182 | printf("%d entries read from file\n", file->nr_entries); | ||
179 | 183 | ||
180 | color(OFF); | 184 | color(OFF); |
181 | if(g_out_prefix) | 185 | if(g_out_prefix) |