1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2011 by Amaury Pouly
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
24 #include "mkimxboot.h"
29 struct imx_fw_variant_desc_t
31 /* Offset within file */
33 /* Total size of the firmware */
40 enum imx_model_t model
;
41 /* md5sum of the file */
43 /* Variant descriptions */
44 struct imx_fw_variant_desc_t fw_variants
[VARIANT_COUNT
];
47 struct imx_model_desc_t
49 /* Descriptive name of this model */
50 const char *model_name
;
51 /* Dualboot code for this model */
52 const unsigned char *dualboot
;
53 /* Size of dualboot functions for this model */
55 /* Model name used in the Rockbox header in ".sansa" files - these match the
56 -add parameter to the "scramble" tool */
57 const char *rb_model_name
;
58 /* Model number used to initialise the checksum in the Rockbox header in
59 ".sansa" files - these are the same as MODEL_NUMBER in config-target.h */
60 const int rb_model_num
;
61 /* Number of keys needed to decrypt/encrypt */
64 struct crypto_key_t
*keys
;
65 /* Dualboot load address */
66 uint32_t dualboot_addr
;
67 /* Bootloader load address */
68 uint32_t bootloader_addr
;
71 static const char *imx_fw_variant
[] =
73 [VARIANT_DEFAULT
] = "default",
74 [VARIANT_ZENXFI2_RECOVERY
] = "ZEN X-Fi2 Recovery",
75 [VARIANT_ZENXFI2_NAND
] = "ZEN X-Fi2 NAND",
76 [VARIANT_ZENXFI2_SD
] = "ZEN X-Fi2 eMMC/SD",
79 static const struct imx_md5sum_t imx_sums
[] =
83 MODEL_FUZEPLUS
, "c3e27620a877dc6b200b97dcb3e0ecc7",
84 { [VARIANT_DEFAULT
] = { 0, 34652624 } }
87 /* Version 1.23.01e */
88 MODEL_ZENXFI2
, "e37e2c24abdff8e624d0a29f79157850",
91 /* Version 1.23.01e */
92 MODEL_ZENXFI2
, "2beff2168212d332f13cfc36ca46989d",
93 { [VARIANT_ZENXFI2_RECOVERY
] = { 0x93010, 684192},
94 [VARIANT_ZENXFI2_NAND
] = { 0x13a0b0, 42410704 },
95 [VARIANT_ZENXFI2_SD
] = { 0x29ac380, 42304208 }
99 /* Version 1.00.22e */
100 MODEL_ZENXFI3
, "658a24eeef5f7186ca731085d8822a87",
101 { [VARIANT_DEFAULT
] = {0, 18110576} }
105 static struct crypto_key_t zero_key
=
107 .method
= CRYPTO_KEY
,
111 static const struct imx_model_desc_t imx_models
[] =
113 [MODEL_FUZEPLUS
] = { "Fuze+", dualboot_fuzeplus
, sizeof(dualboot_fuzeplus
), "fuz+", 72,
114 1, &zero_key
, 0, 0x40000000 },
115 [MODEL_ZENXFI2
] = {"Zen X-Fi2", dualboot_zenxfi2
, sizeof(dualboot_zenxfi2
), "zxf2", 82,
116 1, &zero_key
, 0, 0x40000000 },
117 [MODEL_ZENXFI3
] = {"Zen X-Fi3", dualboot_zenxfi3
, sizeof(dualboot_zenxfi3
), "zxf3", 83,
118 1, &zero_key
, 0, 0x40000000 },
121 #define NR_IMX_SUMS (sizeof(imx_sums) / sizeof(imx_sums[0]))
122 #define NR_IMX_MODELS (sizeof(imx_models) / sizeof(imx_models[0]))
124 #define MAGIC_ROCK 0x726f636b /* 'rock' */
125 #define MAGIC_RECOVERY 0xfee1dead
126 #define MAGIC_NORMAL 0xcafebabe
128 static enum imx_error_t
patch_std_zero_host_play(int jump_before
, int model
,
129 enum imx_output_type_t type
, struct sb_file_t
*sb_file
, void *boot
, size_t boot_sz
)
131 /* We assume the file has three boot sections: ____, host, play and one
132 * resource section rsrc.
136 * We patch the file by inserting the dualboot code before the <jump_before>th
137 * call in the ____ section. We give it as argument the section name 'rock'
138 * and add a section called 'rock' after rsrc which contains the bootloader.
140 * Single Boot & Recovery:
141 * -----------------------
142 * We patch the file by inserting the bootloader code after the <jump_before>th
143 * call in the ____ section and get rid of everything else. In recovery mode,
144 * we give 0xfee1dead as argument */
146 /* Do not override real key and IV */
147 sb_file
->override_crypto_iv
= false;
148 sb_file
->override_real_key
= false;
150 /* first locate the good instruction */
151 struct sb_section_t
*sec
= &sb_file
->sections
[0];
153 while(jump_idx
< sec
->nr_insts
&& jump_before
> 0)
154 if(sec
->insts
[jump_idx
++].inst
== SB_INST_CALL
)
156 if(jump_idx
== sec
->nr_insts
)
158 printf("[ERR] Cannot locate call in section ____\n");
159 return IMX_DONT_KNOW_HOW_TO_PATCH
;
162 if(type
== IMX_DUALBOOT
)
164 /* create a new instruction array with a hole for two instructions */
165 struct sb_inst_t
*new_insts
= xmalloc(sizeof(struct sb_inst_t
) * (sec
->nr_insts
+ 2));
166 memcpy(new_insts
, sec
->insts
, sizeof(struct sb_inst_t
) * jump_idx
);
167 memcpy(new_insts
+ jump_idx
+ 2, sec
->insts
+ jump_idx
,
168 sizeof(struct sb_inst_t
) * (sec
->nr_insts
- jump_idx
));
169 /* first instruction is be a load */
170 struct sb_inst_t
*load
= &new_insts
[jump_idx
];
171 memset(load
, 0, sizeof(struct sb_inst_t
));
172 load
->inst
= SB_INST_LOAD
;
173 load
->size
= imx_models
[model
].dualboot_size
;
174 load
->addr
= imx_models
[model
].dualboot_addr
;
175 /* duplicate memory because it will be free'd */
176 load
->data
= memdup(imx_models
[model
].dualboot
, imx_models
[model
].dualboot_size
);
177 /* second instruction is a call */
178 struct sb_inst_t
*call
= &new_insts
[jump_idx
+ 1];
179 memset(call
, 0, sizeof(struct sb_inst_t
));
180 call
->inst
= SB_INST_CALL
;
181 call
->addr
= imx_models
[model
].dualboot_addr
;
182 call
->argument
= MAGIC_ROCK
;
183 /* free old instruction array */
185 sec
->insts
= new_insts
;
188 /* create a new section */
189 struct sb_section_t rock_sec
;
190 memset(&rock_sec
, 0, sizeof(rock_sec
));
191 /* section has two instructions: load and call */
192 rock_sec
.identifier
= MAGIC_ROCK
;
193 rock_sec
.alignment
= BLOCK_SIZE
;
194 rock_sec
.nr_insts
= 2;
195 rock_sec
.insts
= xmalloc(2 * sizeof(struct sb_inst_t
));
196 memset(rock_sec
.insts
, 0, 2 * sizeof(struct sb_inst_t
));
197 rock_sec
.insts
[0].inst
= SB_INST_LOAD
;
198 rock_sec
.insts
[0].size
= boot_sz
;
199 rock_sec
.insts
[0].data
= memdup(boot
, boot_sz
);
200 rock_sec
.insts
[0].addr
= imx_models
[model
].bootloader_addr
;
201 rock_sec
.insts
[1].inst
= SB_INST_JUMP
;
202 rock_sec
.insts
[1].addr
= imx_models
[model
].bootloader_addr
;
203 rock_sec
.insts
[1].argument
= MAGIC_NORMAL
;
205 sb_file
->sections
= augment_array(sb_file
->sections
,
206 sizeof(struct sb_section_t
), sb_file
->nr_sections
,
208 sb_file
->nr_sections
++;
212 else if(type
== IMX_SINGLEBOOT
|| type
== IMX_RECOVERY
)
214 bool recovery
= type
== IMX_RECOVERY
;
215 /* remove everything after the call and add two instructions: load and call */
216 struct sb_inst_t
*new_insts
= xmalloc(sizeof(struct sb_inst_t
) * (jump_idx
+ 2));
217 memcpy(new_insts
, sec
->insts
, sizeof(struct sb_inst_t
) * jump_idx
);
218 for(int i
= jump_idx
; i
< sec
->nr_insts
; i
++)
219 sb_free_instruction(sec
->insts
[i
]);
220 memset(new_insts
+ jump_idx
, 0, 2 * sizeof(struct sb_inst_t
));
221 new_insts
[jump_idx
+ 0].inst
= SB_INST_LOAD
;
222 new_insts
[jump_idx
+ 0].size
= boot_sz
;
223 new_insts
[jump_idx
+ 0].data
= memdup(boot
, boot_sz
);
224 new_insts
[jump_idx
+ 0].addr
= imx_models
[model
].bootloader_addr
;
225 new_insts
[jump_idx
+ 1].inst
= SB_INST_JUMP
;
226 new_insts
[jump_idx
+ 1].addr
= imx_models
[model
].bootloader_addr
;
227 new_insts
[jump_idx
+ 1].argument
= recovery
? MAGIC_RECOVERY
: MAGIC_NORMAL
;
230 sec
->insts
= new_insts
;
231 sec
->nr_insts
= jump_idx
+ 2;
232 /* remove all other sections */
233 for(int i
= 1; i
< sb_file
->nr_sections
; i
++)
234 sb_free_section(sb_file
->sections
[i
]);
235 struct sb_section_t
*new_sec
= xmalloc(sizeof(struct sb_section_t
));
236 memcpy(new_sec
, &sb_file
->sections
[0], sizeof(struct sb_section_t
));
237 free(sb_file
->sections
);
238 sb_file
->sections
= new_sec
;
239 sb_file
->nr_sections
= 1;
245 printf("[ERR] Bad output type !\n");
246 return IMX_DONT_KNOW_HOW_TO_PATCH
;
250 static enum imx_error_t
patch_firmware(enum imx_model_t model
,
251 enum imx_firmware_variant_t variant
, enum imx_output_type_t type
,
252 struct sb_file_t
*sb_file
, void *boot
, size_t boot_sz
)
257 /* The Fuze+ uses the standard ____, host, play sections, patch after third
258 * call in ____ section */
259 return patch_std_zero_host_play(3, model
, type
, sb_file
, boot
, boot_sz
);
261 /* The ZEN X-Fi3 uses the standard ____, hSst, pSay sections, patch after third
262 * call in ____ section. Although sections names use the S variant, they are standard. */
263 return patch_std_zero_host_play(3, model
, type
, sb_file
, boot
, boot_sz
);
265 /* The ZEN X-Fi2 has two types of firmware: recovery and normal.
266 * Normal uses the standard ___, host, play sections and recovery only ____ */
269 case VARIANT_ZENXFI2_RECOVERY
:
270 case VARIANT_ZENXFI2_NAND
:
271 case VARIANT_ZENXFI2_SD
:
272 return patch_std_zero_host_play(1, model
, type
, sb_file
, boot
, boot_sz
);
274 return IMX_DONT_KNOW_HOW_TO_PATCH
;
278 return IMX_DONT_KNOW_HOW_TO_PATCH
;
282 static void imx_printf(void *user
, bool error
, color_t c
, const char *fmt
, ...)
298 static uint32_t get_uint32be(unsigned char *p
)
300 return (p
[0] << 24) | (p
[1] << 16) | (p
[2] << 8) | p
[3];
303 void dump_imx_dev_info(const char *prefix
)
305 printf("%smkimxboot models:\n", prefix
);
306 for(int i
= 0; i
< NR_IMX_MODELS
; i
++)
308 printf("%s %s: idx=%d rb_model=%s rb_num=%d\n", prefix
,
309 imx_models
[i
].model_name
, i
, imx_models
[i
].rb_model_name
,
310 imx_models
[i
].rb_model_num
);
312 printf("%smkimxboot variants:\n", prefix
);
313 for(int i
= 0; i
< VARIANT_COUNT
; i
++)
315 printf("%s %d: %s\n", prefix
, i
, imx_fw_variant
[i
]);
317 printf("%smkimxboot mapping:\n", prefix
);
318 for(int i
= 0; i
< NR_IMX_SUMS
; i
++)
320 printf("%s md5sum=%s -> idx=%d\n", prefix
, imx_sums
[i
].md5sum
,
322 for(int j
= 0; j
< VARIANT_COUNT
; j
++)
323 if(imx_sums
[i
].fw_variants
[j
].size
)
324 printf("%s variant=%d -> offset=%#x size=%#x\n", prefix
,
325 j
, (unsigned)imx_sums
[i
].fw_variants
[j
].offset
,
326 (unsigned)imx_sums
[i
].fw_variants
[j
].size
);
330 enum imx_error_t
mkimxboot(const char *infile
, const char *bootfile
,
331 const char *outfile
, struct imx_option_t opt
)
334 if(opt
.fw_variant
> VARIANT_COUNT
) {
337 dump_imx_dev_info("[INFO] ");
338 /* compute MD5 sum of the file */
339 uint8_t file_md5sum
[16];
342 FILE *f
= fopen(infile
, "rb");
345 printf("[ERR] Cannot open input file\n");
346 return IMX_OPEN_ERROR
;
348 fseek(f
, 0, SEEK_END
);
349 size_t sz
= ftell(f
);
350 fseek(f
, 0, SEEK_SET
);
351 void *buf
= xmalloc(sz
);
352 if(fread(buf
, sz
, 1, f
) != 1)
356 printf("[ERR] Cannot read file\n");
357 return IMX_READ_ERROR
;
362 md5_update(&ctx
, buf
, sz
);
363 md5_finish(&ctx
, file_md5sum
);
366 printf("[INFO] MD5 sum of the file: ");
367 print_hex(file_md5sum
, 16, true);
369 enum imx_model_t model
;
374 while(i
< NR_IMX_SUMS
)
377 if(strlen(imx_sums
[i
].md5sum
) != 32)
379 printf("[INFO] Invalid MD5 sum in imx_sums\n");
382 for(int j
= 0; j
< 16; j
++)
385 if(convxdigit(imx_sums
[i
].md5sum
[2 * j
], &a
) || convxdigit(imx_sums
[i
].md5sum
[2 * j
+ 1], &b
))
387 md5
[j
] = (a
<< 4) | b
;
389 if(memcmp(file_md5sum
, md5
, 16) == 0)
395 printf("[ERR] MD5 sum doesn't match any known file\n");
398 model
= imx_sums
[i
].model
;
401 printf("[INFO] File is for model %d (%s)\n", model
, imx_models
[model
].model_name
);
402 /* load rockbox file */
407 FILE *f
= fopen(bootfile
, "rb");
410 printf("[ERR] Cannot open boot file\n");
411 return IMX_OPEN_ERROR
;
413 fseek(f
, 0, SEEK_END
);
414 boot_size
= ftell(f
);
415 fseek(f
, 0, SEEK_SET
);
416 boot
= xmalloc(boot_size
);
417 if(fread(boot
, boot_size
, 1, f
) != 1)
421 printf("[ERR] Cannot read boot file\n");
422 return IMX_READ_ERROR
;
426 /* Check boot file */
431 printf("[ERR] Bootloader file is too small to be valid\n");
433 return IMX_BOOT_INVALID
;
435 /* check model name */
436 uint8_t *name
= boot
+ 4;
437 if(memcmp(name
, imx_models
[model
].rb_model_name
, 4) != 0)
439 printf("[ERR] Bootloader model doesn't match found model for input file\n");
441 return IMX_BOOT_MISMATCH
;
444 uint32_t sum
= imx_models
[model
].rb_model_num
;
445 for(int i
= 8; i
< boot_size
; i
++)
447 if(sum
!= get_uint32be(boot
))
449 printf("[ERR] Bootloader checksum mismatch\n");
451 return IMX_BOOT_CHECKSUM_ERROR
;
455 struct sb_file_t
*sb_file
;
458 if(imx_sums
[md5_idx
].fw_variants
[opt
.fw_variant
].size
== 0)
460 printf("[ERR] Input file does not contain variant '%s'\n", imx_fw_variant
[opt
.fw_variant
]);
462 return IMX_VARIANT_MISMATCH
;
467 add_keys(imx_models
[model
].keys
, imx_models
[model
].nr_keys
);
468 sb_file
= sb_read_file_ex(infile
, imx_sums
[md5_idx
].fw_variants
[opt
.fw_variant
].offset
,
469 imx_sums
[md5_idx
].fw_variants
[opt
.fw_variant
].size
, false, NULL
, &imx_printf
, &err
);
474 return IMX_FIRST_SB_ERROR
+ err
;
478 enum imx_error_t ret
= patch_firmware(model
, opt
.fw_variant
, opt
.output
, sb_file
, boot
+ 8, boot_size
- 8);
479 if(ret
== IMX_SUCCESS
)
480 ret
= sb_write_file(sb_file
, outfile
);