Amaury Pouly | 2e0c558 | 2013-10-22 00:48:33 +0200 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id$ |
| 9 | * |
| 10 | * Copyright (C) 2008 by Maurus Cuelenaere |
| 11 | * Based on zenutils by Rasmus Ry <rasmus.ry{at}gmail.com> |
| 12 | * Copyright (C) 2013 by Amaury Pouly |
| 13 | * |
| 14 | * This program is free software; you can redistribute it and/or |
| 15 | * modify it under the terms of the GNU General Public License |
| 16 | * as published by the Free Software Foundation; either version 2 |
| 17 | * of the License, or (at your option) any later version. |
| 18 | * |
| 19 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 20 | * KIND, either express or implied. |
| 21 | * |
| 22 | ****************************************************************************/ |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <stdarg.h> |
| 26 | #include <string.h> |
| 27 | #include "mkzenboot.h" |
| 28 | #include "utils.h" |
| 29 | #include "dualboot.h" |
| 30 | |
| 31 | /** |
| 32 | * Keys used by players |
| 33 | */ |
| 34 | static const char null_key_v1[] = "CTL:N0MAD|PDE0.SIGN."; |
| 35 | static const char null_key_v2[] = "CTL:N0MAD|PDE0.DPMP."; |
| 36 | static const char null_key_v3[] = "CTL:N0MAD|PDE0.DPFP."; |
| 37 | static const char null_key_v4[] = "CTL:Z3N07|PDE0.DPMP."; |
| 38 | |
| 39 | static const char tl_zvm_key[] = "1sN0TM3D az u~may th1nk*" |
| 40 | "Creative Zen Vision:M"; |
| 41 | static const char tl_zvm60_key[] = "1sN0TM3D az u~may th1nk*" |
| 42 | "Creative Zen Vision:M (D" |
| 43 | "VP-HD0004)"; |
| 44 | static const char tl_zen_key[] = "1sN0TM3D az u~may th1nk*" |
| 45 | "Creative ZEN"; |
| 46 | static const char tl_zenxf_key[] = "1sN0TM3D az u~may th1nk*" |
| 47 | "Creative ZEN X-Fi"; |
| 48 | static const char tl_zenmo_key[] = "1sN0TM3D az u~may th1nk*" |
| 49 | "Creative ZEN Mozaic"; |
| 50 | static const char tl_zv_key[] = "1sN0TM3D az u~may th1nk*" |
| 51 | "Creative Zen Vision"; |
| 52 | static const char tl_zvw_key[] = "1sN0TM3D az u~may th1nk*" |
| 53 | "Creative ZEN Vision W"; |
| 54 | static const char tl_zm_key[] = "1sN0TM3D az u~may th1nk*" |
| 55 | "Creative Zen Micro"; |
| 56 | static const char tl_zmp_key[] = "1sN0TM3D az u~may th1nk*" |
| 57 | "Creative Zen MicroPhoto"; |
| 58 | static const char tl_zs_key[] = "1sN0TM3D az u~may th1nk*" |
| 59 | "Creative Zen Sleek"; |
| 60 | static const char tl_zsp_key[] = "1sN0TM3D az u~may th1nk*" |
| 61 | "Creative Zen Sleek Photo"; |
| 62 | static const char tl_zt_key[] = "1sN0TM3D az u~may th1nk*" |
| 63 | "Creative Zen Touch"; |
| 64 | static const char tl_zx_key[] = "1sN0TM3D az u~may th1nk*" |
| 65 | "NOMAD Jukebox Zen Xtra"; |
| 66 | static const char tl_zenv_key[] = "1sN0TM3D az u~may th1nk*" |
| 67 | "Creative ZEN V"; |
| 68 | static const char tl_zenvp_key[] = "1sN0TM3D az u~may th1nk*" |
| 69 | "Creative ZEN V Plus"; |
| 70 | static const char tl_zenvv_key[] = "1sN0TM3D az u~may th1nk*" |
| 71 | "Creative ZEN V (Video)"; |
| 72 | |
| 73 | struct player_info_t |
| 74 | { |
| 75 | const char* name; |
| 76 | const char* null_key; /* HMAC-SHA1 key */ |
| 77 | const char* tl_key; /* BlowFish key */ |
| 78 | bool big_endian; |
| 79 | char *cinf; |
| 80 | }; |
| 81 | |
| 82 | static struct player_info_t zen_players[] = |
| 83 | { |
| 84 | {"Zen Vision:M", null_key_v2, tl_zvm_key, false, NULL}, |
| 85 | {"Zen Vision:M 60GB", null_key_v2, tl_zvm60_key, false, NULL}, |
| 86 | {"Zen", null_key_v4, tl_zen_key, false, "Creative ZEN"}, |
| 87 | {"Zen X-Fi", null_key_v4, tl_zenxf_key, false, "Creative ZEN X-Fi"}, |
| 88 | {"Zen Mozaic", null_key_v4, tl_zenmo_key, false, "Creative ZEN Mozaic"}, |
| 89 | {"Zen Vision", null_key_v2, tl_zv_key, false, NULL}, |
| 90 | {"Zen Vision W", null_key_v2, tl_zvw_key, false, NULL}, |
| 91 | {"Zen Micro", null_key_v1, tl_zm_key, true, NULL}, |
| 92 | {"Zen MicroPhoto", null_key_v1, tl_zmp_key, true, NULL}, |
| 93 | {"Zen Sleek", null_key_v1, tl_zs_key, true, NULL}, |
| 94 | {"Zen SleekPhoto", null_key_v1, tl_zsp_key, true, NULL}, |
| 95 | {"Zen Touch", null_key_v1, tl_zt_key, true, NULL}, |
| 96 | {"Zen Xtra", null_key_v1, tl_zx_key, true, NULL}, |
Amaury Pouly | 977a6c3 | 2013-11-18 20:07:02 +0000 | [diff] [blame] | 97 | {"Zen V", null_key_v3, tl_zenv_key, false, "Creative ZEN V"}, |
Amaury Pouly | 2e0c558 | 2013-10-22 00:48:33 +0200 | [diff] [blame] | 98 | {"Zen V Plus", null_key_v3, tl_zenvp_key, false, NULL}, |
| 99 | {"Zen V Video", null_key_v3, tl_zenvv_key, false, NULL}, |
| 100 | {NULL, NULL, NULL, false, NULL} |
| 101 | }; |
| 102 | |
| 103 | /** |
| 104 | * Information on how to patch firmwares |
| 105 | */ |
| 106 | struct zen_model_desc_t |
| 107 | { |
| 108 | /* Descriptive name of this model (must match player in zen_players[]) */ |
| 109 | const char *model_name; |
| 110 | /* Model name used in the Rockbox header in ".zen" files - these match the |
| 111 | -add parameter to the "scramble" tool */ |
| 112 | const char *rb_model_name; |
| 113 | /* Model number used to initialise the checksum in the Rockbox header in |
| 114 | ".zen" files - these are the same as MODEL_NUMBER in config-target.h */ |
| 115 | const int rb_model_num; |
| 116 | /* Bootloader load address */ |
| 117 | uint32_t bootloader_addr; |
| 118 | /* Dualboot code for this model */ |
| 119 | const unsigned char *dualboot; |
| 120 | /* Size of dualboot functions for this model */ |
| 121 | int dualboot_size; |
| 122 | }; |
| 123 | |
| 124 | /* keep this consistent with the address in dualboot.lds */ |
| 125 | static const struct zen_model_desc_t zen_models[] = |
| 126 | { |
| 127 | [MODEL_UNKNOWN] = |
| 128 | { |
| 129 | "Unknown", " ", 0, 0, NULL, 0 |
| 130 | }, |
| 131 | [MODEL_ZENV] = |
| 132 | { |
Amaury Pouly | cddf388 | 2014-01-21 19:01:08 +0100 | [diff] [blame] | 133 | "Zen V", "zenv", 92, 0x61000000, dualboot_zenv, sizeof(dualboot_zenv) |
Amaury Pouly | 2e0c558 | 2013-10-22 00:48:33 +0200 | [diff] [blame] | 134 | }, |
| 135 | [MODEL_ZENXFI] = |
| 136 | { |
| 137 | "Zen X-Fi", "zxfi", 86, 0x41000000, dualboot_zenxfi, sizeof(dualboot_zenxfi) |
| 138 | }, |
| 139 | [MODEL_ZENMOZAIC] = |
| 140 | { |
| 141 | "Zen Mozaic", "zmoz", 87, 0x41000000, dualboot_zenmozaic, sizeof(dualboot_zenmozaic) |
| 142 | }, |
| 143 | [MODEL_ZEN] = |
| 144 | { |
| 145 | "Zen", "zen", 90, 0x41000000, dualboot_zen, sizeof(dualboot_zen) |
| 146 | }, |
| 147 | }; |
| 148 | |
| 149 | /** |
| 150 | * MD5 knowledge base |
| 151 | */ |
| 152 | |
| 153 | struct zen_md5sum_t |
| 154 | { |
| 155 | /* Device model */ |
| 156 | enum zen_model_t model; |
| 157 | /* md5sum of the file */ |
| 158 | char *md5sum; |
| 159 | /* Version string */ |
| 160 | const char *version; |
| 161 | }; |
| 162 | |
| 163 | static const struct zen_md5sum_t zen_sums[] = |
| 164 | { |
| 165 | /** Zen Mozaic */ |
| 166 | { |
| 167 | /* Version 1.06.01e */ |
| 168 | MODEL_ZENMOZAIC, "88a856f8273b2bc3fcacf1f067a44aa8", "1.06.01e" |
| 169 | }, |
| 170 | /** Zen X-Fi */ |
| 171 | { |
| 172 | /* Version 1.04.08e */ |
| 173 | MODEL_ZENXFI, "f07e2e75069289a2aa14c6583bd9643b", "1.04.08e" |
| 174 | }, |
Amaury Pouly | 5e43c05 | 2013-11-11 20:34:55 +0000 | [diff] [blame] | 175 | { |
| 176 | /* Version 1.04.08 */ |
| 177 | MODEL_ZENXFI, "c3cddf8468d8c8982e93aa9986c5a152", "1.04.08" |
| 178 | }, |
Amaury Pouly | 2e0c558 | 2013-10-22 00:48:33 +0200 | [diff] [blame] | 179 | /** Zen V */ |
| 180 | { |
| 181 | /* Version 1.32.01e */ |
| 182 | MODEL_ZENV, "2f6d3e619557583c30132ac87221bc3e", "1.32.01e" |
| 183 | }, |
| 184 | /** Zen */ |
| 185 | { |
| 186 | /* Version 1.21.03e */ |
| 187 | MODEL_ZEN, "1fe28f587f87ac3c280281db28c42465", "1.21.03e" |
| 188 | } |
| 189 | }; |
| 190 | |
| 191 | #define NR_ZEN_PLAYERS (sizeof(zen_players) / sizeof(zen_players[0])) |
| 192 | #define NR_ZEN_SUMS (sizeof(zen_sums) / sizeof(zen_sums[0])) |
| 193 | #define NR_ZEN_MODELS (sizeof(zen_models) / sizeof(zen_models[0])) |
| 194 | |
| 195 | #define MAGIC_ROCK 0x726f636b /* 'rock' */ |
| 196 | #define MAGIC_RECOVERY 0xfee1dead |
| 197 | #define MAGIC_NORMAL 0xcafebabe |
| 198 | |
| 199 | /** |
| 200 | * Stolen from various places in our codebase |
| 201 | */ |
| 202 | |
| 203 | /** |
| 204 | * EDOC file format |
| 205 | */ |
| 206 | struct edoc_header_t |
| 207 | { |
| 208 | char magic[4]; |
| 209 | uint32_t total_size; |
| 210 | uint32_t zero; |
| 211 | }; |
| 212 | |
| 213 | struct edoc_section_header_t |
| 214 | { |
| 215 | uint32_t addr; |
| 216 | uint32_t size; |
| 217 | uint32_t checksum; |
| 218 | }; |
| 219 | |
| 220 | uint32_t edoc_checksum(void *buffer, size_t size) |
| 221 | { |
| 222 | uint32_t c = 0; |
| 223 | uint32_t *p = buffer; |
| 224 | while(size >= 4) |
| 225 | { |
| 226 | c += *p + (*p >> 16); |
| 227 | p++; |
| 228 | size -= 4; |
| 229 | } |
| 230 | if(size != 0) |
| 231 | printf("[WARN] EDOC Checksum section size is not a multiple of 4 bytes, result is undefined!\n"); |
| 232 | return c & 0xffff; |
| 233 | } |
| 234 | |
| 235 | #define errorf(err, ...) do { printf(__VA_ARGS__); return err; } while(0) |
| 236 | |
| 237 | /** |
| 238 | * How does patching code work |
| 239 | * --------------------------- |
| 240 | * |
| 241 | * All Creative firmwares work the same: they start at 0 and the code sequence at |
| 242 | * 0 always contains the vector table with ldr with offsets: |
| 243 | * 0: e59ff018 ldr pc, [pc, #24] ; 0x20 |
| 244 | * 4: e59ff018 ldr pc, [pc, #24] ; 0x24 |
| 245 | * 8: e59ff018 ldr pc, [pc, #24] ; 0x28 |
| 246 | * c: e59ff018 ldr pc, [pc, #24] ; 0x2c |
| 247 | * 10: e59ff018 ldr pc, [pc, #24] ; 0x30 |
| 248 | * 14: e59ff018 ldr pc, [pc, #24] ; 0x34 |
| 249 | * 18: e59ff018 ldr pc, [pc, #24] ; 0x38 |
| 250 | * 1c: e59ff018 ldr pc, [pc, #24] ; 0x3c |
| 251 | * 20: 0000dbd4 .word start |
| 252 | * 24: 0000dcac .word undef_instr_handler |
| 253 | * 28: 0000dcb0 .word software_int_handler |
| 254 | * 2c: 0000dcb4 .word prefetch_abort_handler |
| 255 | * 30: 0000dcb8 .word data_abort_handler |
| 256 | * 34: 0000dcbc .word reserved_handler |
| 257 | * 38: 0000dcc0 .word irq_handler |
| 258 | * 3c: 0000dd08 .word fiq_handler |
| 259 | * |
| 260 | * To build a dual-boot image, we modify the start address to point to some |
| 261 | * code we added to the image. Specifically we first add the stub, then |
| 262 | * the rockbox image. We also write the old start address to this |
| 263 | * stub so that it can either decide to run rockbox or patch back the |
| 264 | * start address and jump to 0. |
| 265 | * Singleboot and recovery is handled the same way except that both targets use |
| 266 | * the same address and we drop the OF, so we create a fake vector table! |
| 267 | */ |
| 268 | |
| 269 | struct dualboot_footer_t |
| 270 | { |
| 271 | uint32_t magic; |
| 272 | uint32_t of_addr; |
| 273 | uint32_t rb_addr; |
| 274 | uint32_t boot_arg; |
| 275 | } __attribute__((packed)); |
| 276 | |
| 277 | #define FOOTER_MAGIC 0x1ceb00da |
| 278 | |
| 279 | static enum zen_error_t create_fake_image(uint8_t **fw, uint32_t *fw_size) |
| 280 | { |
| 281 | /** We need to create a fake EDOC image, so first a header and one section |
| 282 | * header with one data chunk. */ |
| 283 | /** The fake image is as follows: |
| 284 | * 0: e59ff018 ldr pc, [pc, #24] ; 0x20 |
| 285 | * 4: e59ff018 ldr pc, [pc, #24] ; 0x24 |
| 286 | * 8: e59ff018 ldr pc, [pc, #24] ; 0x28 |
| 287 | * c: e59ff018 ldr pc, [pc, #24] ; 0x2c |
| 288 | * 10: e59ff018 ldr pc, [pc, #24] ; 0x30 |
| 289 | * 14: e59ff018 ldr pc, [pc, #24] ; 0x34 |
| 290 | * 18: e59ff018 ldr pc, [pc, #24] ; 0x38 |
| 291 | * 1c: e59ff018 ldr pc, [pc, #24] ; 0x3c |
| 292 | * 20: 00000040 .word hang |
| 293 | * 24: 00000040 .word hang |
| 294 | * 28: 00000040 .word hang |
| 295 | * 2c: 00000040 .word hang |
| 296 | * 30: 00000040 .word hang |
| 297 | * 34: 00000040 .word hang |
| 298 | * 38: 00000040 .word hang |
| 299 | * 3c: 00000040 .word hang |
| 300 | * 40 <hang>: |
| 301 | * 40: eafffffe b 40 <hang> */ |
| 302 | *fw_size = sizeof(struct edoc_header_t) + sizeof(struct edoc_section_header_t) + 0x44; |
| 303 | *fw = malloc(*fw_size); |
| 304 | if(*fw == NULL) |
| 305 | errorf(ZEN_ERROR, "[ERR] Allocation failed"); |
| 306 | struct edoc_header_t *hdr = (void *)*fw; |
| 307 | memcpy(hdr->magic, "EDOC", 4); |
| 308 | hdr->total_size = *fw_size - sizeof(struct edoc_header_t) + 4; |
| 309 | hdr->zero = 0; |
| 310 | struct edoc_section_header_t *sec = (void *)(hdr + 1); |
| 311 | sec->addr = 0; |
| 312 | sec->size = 0x44; |
| 313 | uint32_t *p = (void *)(sec + 1); |
| 314 | p[0] = p[1] = p[2] = p[3] = p[4] = p[5] = p[6] = p[7] = 0xe59ff018; |
| 315 | p[8] = p[9] = p[10] = p[11] = p[12] = p[13] = p[14] = p[15] = 0x40; |
| 316 | p[16] = 0xeafffffe; |
| 317 | sec->checksum = edoc_checksum(p, 0x44); |
| 318 | return ZEN_SUCCESS; |
| 319 | } |
| 320 | |
| 321 | static enum zen_error_t patch_firmware(uint8_t **fw, uint32_t *fw_size, |
| 322 | void *boot, size_t boot_size, struct zen_option_t opt) |
| 323 | { |
| 324 | /* check if dualboot stub is available */ |
| 325 | const void *dualboot = zen_models[opt.model].dualboot; |
| 326 | int dualboot_size = zen_models[opt.model].dualboot_size; |
| 327 | uint32_t dualboot_addr = zen_models[opt.model].bootloader_addr; |
| 328 | if(dualboot == NULL) |
| 329 | errorf(ZEN_DONT_KNOW_HOW_TO_PATCH, "[ERR] I don't have a dualboot stub for this model\n"); |
| 330 | /* if not asked to dualboot, drop OF and create a fake image */ |
| 331 | if(opt.output != ZEN_DUALBOOT) |
| 332 | { |
| 333 | enum zen_error_t ret = create_fake_image(fw, fw_size); |
| 334 | if(ret != ZEN_SUCCESS) |
| 335 | return ret; |
| 336 | } |
| 337 | /* compute final image size: add stub + bootloader in one block as a section */ |
| 338 | int extra_size = sizeof(struct edoc_section_header_t) + dualboot_size + boot_size; |
| 339 | *fw_size += extra_size; |
| 340 | *fw = realloc(*fw, *fw_size); |
| 341 | if(*fw == NULL) |
| 342 | errorf(ZEN_ERROR, "[ERR] Allocation failed"); |
| 343 | /* sanity check */ |
| 344 | struct edoc_header_t *hdr = (void *)*fw; |
| 345 | if(memcmp(hdr->magic, "EDOC", 4) != 0) |
| 346 | errorf(ZEN_FW_INVALID, "[ERR] Firmware doesn't use EDOC format\n"); |
| 347 | /* validate image and find OF start addr */ |
| 348 | uint32_t of_addr = 0; |
| 349 | struct edoc_section_header_t *sec_hdr = (void *)(hdr + 1); |
| 350 | while((void *)sec_hdr - (void *)&hdr->zero < hdr->total_size) |
| 351 | { |
| 352 | if(sec_hdr->checksum != edoc_checksum(sec_hdr + 1, sec_hdr->size)) |
| 353 | errorf(ZEN_FW_INVALID, "[ERR] Firmware checksum error\n"); |
| 354 | if(sec_hdr->addr == 0) |
| 355 | { |
| 356 | uint32_t *start_vector = ((void *)(sec_hdr + 1) + 0x20); |
| 357 | /* extract address */ |
| 358 | of_addr = *(uint32_t *)start_vector; |
| 359 | /* patch vector */ |
| 360 | *start_vector = dualboot_addr; |
| 361 | /* fix checksum */ |
| 362 | sec_hdr->checksum = edoc_checksum(sec_hdr + 1, sec_hdr->size); |
| 363 | } |
| 364 | sec_hdr = (void *)(sec_hdr + 1) + sec_hdr->size; |
| 365 | } |
| 366 | if(of_addr == 0) |
| 367 | errorf(ZEN_FW_INVALID, "[ERR] Firmware doesn't have the expected format\n"); |
| 368 | printf("[INFO] OF start address: %#x\n", of_addr); |
| 369 | /* add extra section */ |
| 370 | sec_hdr->addr = dualboot_addr; |
| 371 | sec_hdr->size = dualboot_size + boot_size; |
| 372 | /* add extra data */ |
| 373 | memcpy(sec_hdr + 1, dualboot, dualboot_size); |
| 374 | memcpy((void *)(sec_hdr + 1) + dualboot_size, boot, boot_size); |
| 375 | /* locate and patch dualboot footer */ |
| 376 | struct dualboot_footer_t *footer = (void *)(sec_hdr + 1) + dualboot_size - |
| 377 | sizeof(struct dualboot_footer_t); |
| 378 | if(footer->magic != FOOTER_MAGIC) |
| 379 | errorf(ZEN_FW_INVALID, "[ERR] Footer magic mismatch\n"); |
| 380 | uint32_t rb_addr = dualboot_addr + dualboot_size; |
| 381 | printf("[INFO] RB start address: %#x\n", rb_addr); |
| 382 | footer->of_addr = opt.output == ZEN_DUALBOOT ? of_addr : rb_addr; |
| 383 | footer->rb_addr = rb_addr; |
| 384 | footer->boot_arg = opt.output == ZEN_RECOVERY ? 0xfee1dead : 0xcafebabe; |
| 385 | printf("[INFO] Footer: 0x%08x 0x%08x 0x%08x\n", footer->of_addr, footer->rb_addr, |
| 386 | footer->boot_arg); |
| 387 | /* fix image */ |
| 388 | sec_hdr->checksum = edoc_checksum(sec_hdr + 1, sec_hdr->size); |
| 389 | hdr->total_size += extra_size; |
| 390 | return ZEN_SUCCESS; |
| 391 | } |
| 392 | |
| 393 | struct player_info_t *get_player_info(enum zen_model_t model) |
| 394 | { |
| 395 | for(int i = 0; zen_players[i].name; i++) |
| 396 | if(strcmp(zen_models[model].model_name, zen_players[i].name) == 0) |
| 397 | return &zen_players[i]; |
| 398 | return NULL; |
| 399 | } |
| 400 | |
| 401 | enum zen_error_t build_firmware(void *exec, size_t exec_size, void *boot, size_t boot_size, |
| 402 | const char *outfile, struct zen_option_t opt) |
| 403 | { |
| 404 | uint8_t *buffer = exec; |
| 405 | /** find player info */ |
| 406 | struct player_info_t *player = get_player_info(opt.model); |
| 407 | if(player == NULL) |
| 408 | errorf(ZEN_UNSUPPORTED, "[ERR] There is no player info for this model\n"); |
| 409 | if(player->big_endian) |
| 410 | errorf(ZEN_UNSUPPORTED, "[ERR] Big-endian players are currently unsupported\n"); |
| 411 | |
| 412 | /** Find Win32 PE .data section */ |
| 413 | uint32_t data_ptr; |
| 414 | uint32_t data_size; |
| 415 | enum zen_error_t err = find_pe_data(exec, exec_size, &data_ptr, &data_size); |
| 416 | if(err != ZEN_SUCCESS) |
| 417 | errorf(err, "[ERR] Cannot find .data section\n"); |
| 418 | printf("[INFO] .data section is at 0x%x with size 0x%x\n", data_ptr, data_size); |
| 419 | |
| 420 | /** look for firmware and key in data section */ |
| 421 | uint32_t fw_offset = find_firmware_offset(&buffer[data_ptr], data_size); |
| 422 | if(fw_offset == 0) |
| 423 | errorf(ZEN_FW_INVALID, "[ERR] Couldn't find firmware offset\n"); |
| 424 | uint32_t fw_size = le2int(&buffer[data_ptr + fw_offset]); |
| 425 | printf("[INFO] Firmware offset is at 0x%x with size 0x%x\n", data_ptr + fw_offset, fw_size); |
| 426 | const char *fw_key = find_firmware_key(exec, exec_size); |
| 427 | if(fw_key == NULL) |
| 428 | errorf(ZEN_FW_INVALID, "[ERR] Couldn't find firmware key\n"); |
| 429 | printf("[INFO] Firmware key is %s\n", fw_key); |
| 430 | |
| 431 | /** descramble firmware */ |
| 432 | printf("[INFO] Descrambling firmware... "); |
| 433 | if(!crypt_firmware(fw_key, &buffer[data_ptr + fw_offset + 4], fw_size)) |
| 434 | errorf(ZEN_ERROR, "Fail!\n"); |
| 435 | else |
| 436 | printf("Done!\n"); |
| 437 | /** decompress it */ |
| 438 | uint8_t *out_buffer = malloc(fw_size * 2); |
| 439 | if(out_buffer == NULL) |
| 440 | errorf(ZEN_ERROR, "[ERR] Couldn't allocate memory"); |
| 441 | memset(out_buffer, 0, fw_size * 2); |
| 442 | printf("[INFO] Decompressing firmware... "); |
| 443 | char *err_msg; |
| 444 | if(!inflate_to_buffer(&buffer[data_ptr + fw_offset + 4], fw_size, out_buffer, |
| 445 | fw_size * 2, &err_msg)) |
| 446 | errorf(ZEN_ERROR, "Fail!\n[ERR] ZLib error: %s\n", err_msg); |
| 447 | else |
| 448 | printf("Done!\n"); |
| 449 | |
| 450 | /** check format and resize the buffer */ |
| 451 | if(memcmp(out_buffer, "FFIC", 4) != 0) |
| 452 | errorf(ZEN_FW_INVALID, "[ERR] CIFF header doesn't match\n"); |
| 453 | uint32_t ciff_size = le2int(&out_buffer[4]) + 8 + 28; /* CIFF block + NULL block*/ |
| 454 | printf("[INFO] Total firmware size: %d\n", ciff_size); |
| 455 | out_buffer = realloc(out_buffer, ciff_size); |
| 456 | if(out_buffer == NULL) |
| 457 | errorf(ZEN_ERROR, "[ERR] Cannot resize memory block\n"); |
| 458 | |
| 459 | /** look for firmware file */ |
| 460 | printf("[INFO] Locating encoded block... "); |
| 461 | uint32_t fw_off = 8; |
| 462 | uint8_t *cinf_ptr = NULL; |
| 463 | while(memcmp(&out_buffer[fw_off], " LT\xa9", 4) != 0 && fw_off < ciff_size) |
| 464 | { |
| 465 | if(memcmp(&out_buffer[fw_off], "FNIC", 4) == 0) |
| 466 | { |
| 467 | cinf_ptr = &out_buffer[fw_off + 8]; |
| 468 | fw_off += 4 + 4 + 96; |
| 469 | } |
| 470 | else if(memcmp(&out_buffer[fw_off], "ATAD", 4) == 0) |
| 471 | { |
| 472 | fw_off += 4; |
| 473 | fw_off += le2int(&out_buffer[fw_off]); |
| 474 | fw_off += 4; |
| 475 | } |
| 476 | else |
| 477 | errorf(ZEN_FW_INVALID, "Fail!\n[ERR] Unknown block\n"); |
| 478 | } |
| 479 | if(fw_off >= ciff_size || memcmp(&out_buffer[fw_off], " LT\xa9", 4) != 0) |
| 480 | errorf(ZEN_FW_INVALID, "Fail!\n[ERR] Couldn't find encoded block\n"); |
| 481 | if(!cinf_ptr) |
| 482 | errorf(ZEN_FW_INVALID, "Fail!\n[ERR] Couldn't find CINF\n"); |
| 483 | printf("Done!\n"); |
| 484 | |
| 485 | /** validate player if possible */ |
| 486 | printf("[INFO] Checking player model..."); |
| 487 | if(player->cinf) |
| 488 | { |
| 489 | char cinf_ascii[96]; |
| 490 | for(int j = 0; j < 96; j++) |
| 491 | cinf_ascii[j] = *(unsigned short *)&cinf_ptr[2 * j]; |
| 492 | if(strncmp(cinf_ascii, player->cinf, 96) != 0) |
| 493 | errorf(ZEN_FW_MISMATCH, "Fail!\n[ERR] Player mismatch: CINF indicates '%s' instead of '%s'\n", |
| 494 | cinf_ascii, player->cinf); |
| 495 | else |
| 496 | printf("Done!\n"); |
| 497 | } |
| 498 | else |
| 499 | printf("Bypass!\n"); |
| 500 | |
| 501 | /** decrypt firmware */ |
| 502 | printf("[INFO] Decrypting encoded block... "); |
| 503 | uint32_t iv[2]; |
| 504 | iv[0] = 0; |
| 505 | iv[1] = swap(le2int(&out_buffer[fw_off + 4])); |
| 506 | if(!bf_cbc_decrypt((unsigned char*)player->tl_key, strlen(player->tl_key) + 1, |
| 507 | &out_buffer[fw_off + 8], le2int(&out_buffer[fw_off + 4]), (const unsigned char*)&iv)) |
| 508 | errorf(ZEN_ERROR, "Fail!\n[ERR] Couldn't decrypt encoded block\n"); |
| 509 | printf("Done!\n"); |
| 510 | |
| 511 | /** sanity checks on firmware */ |
| 512 | uint32_t jrm_size = le2int(&out_buffer[fw_off + 8]); |
| 513 | if(jrm_size > le2int(&out_buffer[fw_off + 4]) * 3) |
| 514 | errorf(ZEN_FW_INVALID, "[ERR] Decrypted length of encoded block is unexpectedly large: 0x%08x\n", jrm_size); |
| 515 | printf("[INFO] Firmware size: %d\n", jrm_size); |
| 516 | uint8_t *jrm = malloc(jrm_size); |
| 517 | if(jrm == NULL) |
| 518 | errorf(ZEN_ERROR, "[ERR] Couldn't allocate memory\n"); |
| 519 | memset(buffer, 0, jrm_size); |
| 520 | |
| 521 | /** decompress firmware */ |
| 522 | printf("[INFO] Decompressing encoded block... "); |
| 523 | if(!cenc_decode(&out_buffer[fw_off + 12], le2int(&out_buffer[fw_off + 4]) - 4, jrm, jrm_size)) |
| 524 | errorf(ZEN_ERROR, "Fail!\n[ERR] Couldn't decompress the encoded block\n"); |
| 525 | printf("Done!\n"); |
| 526 | |
| 527 | /** Copy OF because patching might modify it */ |
| 528 | void *jrm_save = malloc(jrm_size); |
| 529 | uint32_t jrm_save_size = jrm_size; |
| 530 | if(jrm_save == NULL) |
| 531 | errorf(ZEN_ERROR, "[ERR] Couldn't allocate memory"); |
| 532 | memcpy(jrm_save, jrm, jrm_size); |
| 533 | |
| 534 | /** Patch firmware */ |
| 535 | err = patch_firmware(&jrm, &jrm_size, boot, boot_size, opt); |
| 536 | if(err != ZEN_SUCCESS) |
| 537 | errorf(err, "[ERR] Couldn't patch firmware\n"); |
| 538 | |
| 539 | /** Rebuild archive */ |
| 540 | bool keep_old_bits = opt.output == ZEN_DUALBOOT || opt.output == ZEN_MIXEDBOOT; |
| 541 | bool keep_of = opt.output == ZEN_MIXEDBOOT; |
| 542 | /* if we keep old stuff, keep everything up to LT block, otherwise just CIFF header */ |
| 543 | uint32_t off = keep_old_bits ? fw_off : 8; |
| 544 | /* move the rest of the archive if keeping old stuff */ |
| 545 | if(keep_old_bits) |
| 546 | { |
| 547 | uint32_t copy_off = fw_off + 8 + le2int(&out_buffer[fw_off + 4]); |
| 548 | uint32_t copy_size = ciff_size - fw_off - 8 - le2int(&out_buffer[fw_off + 4]) - 28; |
| 549 | memmove(&out_buffer[off], &out_buffer[copy_off], copy_size); |
| 550 | off += copy_size; |
| 551 | } |
| 552 | /* if we keep the OF, put a copy of it after renaming it to Hcreativeos.jrm */ |
| 553 | if(keep_of) |
| 554 | { |
| 555 | out_buffer = realloc(out_buffer, off + jrm_save_size + 40); |
| 556 | if(out_buffer == NULL) |
| 557 | errorf(ZEN_ERROR, "[ERR] Couldn't resize memory block\n"); |
| 558 | printf("[INFO] Renaming encoded block to Hcreativeos.jrm... "); |
| 559 | memcpy(&out_buffer[off], "ATAD", 4); |
| 560 | int2le(jrm_save_size + 32, &out_buffer[off + 4]); |
| 561 | memset(&out_buffer[off + 8], 0, 32); |
| 562 | memcpy(&out_buffer[off + 8], "H\0c\0r\0e\0a\0t\0i\0v\0e\0o\0s\0.\0j\0r\0m", 30); |
| 563 | memcpy(&out_buffer[off + 40], jrm_save, jrm_save_size); |
| 564 | off += jrm_save_size + 40; |
| 565 | printf("Done!\n"); |
| 566 | } |
| 567 | /* put modified firmware */ |
| 568 | out_buffer = realloc(out_buffer, off + jrm_size + 40); |
| 569 | if(out_buffer == NULL) |
| 570 | errorf(ZEN_ERROR, "[ERR] Couldn't resize memory block\n"); |
| 571 | printf("[INFO] Adding Hjukebox2.jrm... "); |
| 572 | memcpy(&out_buffer[off], "ATAD", 4); |
| 573 | int2le(jrm_size + 32, &out_buffer[off + 4]); |
| 574 | memset(&out_buffer[off + 8], 0, 32); |
| 575 | memcpy(&out_buffer[off + 8], "H\0j\0u\0k\0e\0b\0o\0x\0""2\0.\0j\0r\0m", 26); |
| 576 | memcpy(&out_buffer[off + 40], jrm, jrm_size); |
| 577 | off += jrm_size + 40; |
| 578 | printf("Done!\n"); |
| 579 | |
| 580 | /** fix header */ |
| 581 | int2le(off - 8, &out_buffer[4]); |
| 582 | |
| 583 | /** update checksum */ |
| 584 | printf("[INFO] Updating checksum... "); |
| 585 | out_buffer = realloc(out_buffer, off + 28); |
| 586 | if(out_buffer == NULL) |
| 587 | errorf(ZEN_ERROR, "[ERR] Couldn't resize memory block\n"); |
| 588 | memcpy(&out_buffer[off], "LLUN", 4); |
| 589 | int2le(20, &out_buffer[off + 4]); |
| 590 | hmac_sha1((unsigned char*)player->null_key, strlen(player->null_key), out_buffer, |
| 591 | off, &out_buffer[off + 8]); |
| 592 | off += 28; |
| 593 | printf("Done!\n"); |
| 594 | |
| 595 | err = write_file(outfile, out_buffer, off); |
| 596 | |
| 597 | free(jrm); |
| 598 | free(jrm_save); |
| 599 | free(out_buffer); |
| 600 | return err; |
| 601 | } |
| 602 | |
| 603 | /* find an entry into zen_sums which matches the MD5 sum of a file */ |
| 604 | static enum zen_error_t find_model_by_md5sum(uint8_t file_md5sum[16], int *md5_idx) |
| 605 | { |
| 606 | int i = 0; |
| 607 | while(i < NR_ZEN_SUMS) |
| 608 | { |
| 609 | uint8_t md5[20]; |
| 610 | if(strlen(zen_sums[i].md5sum) != 32) |
| 611 | errorf(ZEN_ERROR, "[ERR][INTERNAL] Invalid MD5 sum in zen_sums\n"); |
| 612 | for(int j = 0; j < 16; j++) |
| 613 | { |
| 614 | uint8_t a, b; |
| 615 | if(convxdigit(zen_sums[i].md5sum[2 * j], &a) || convxdigit(zen_sums[i].md5sum[2 * j + 1], &b)) |
| 616 | errorf(ZEN_ERROR, "[ERR][INTERNAL] Bad checksum format: %s\n", zen_sums[i].md5sum); |
| 617 | md5[j] = (a << 4) | b; |
| 618 | } |
| 619 | if(memcmp(file_md5sum, md5, 16) == 0) |
| 620 | break; |
| 621 | i++; |
| 622 | } |
| 623 | if(i == NR_ZEN_SUMS) |
| 624 | errorf(ZEN_NO_MATCH, "[ERR] MD5 sum doesn't match any known file\n"); |
| 625 | *md5_idx = i; |
| 626 | return ZEN_SUCCESS; |
| 627 | } |
| 628 | |
| 629 | enum zen_error_t mkzenboot(const char *infile, const char *bootfile, |
| 630 | const char *outfile, struct zen_option_t opt) |
| 631 | { |
| 632 | /* determine firmware model */ |
| 633 | void *fw; |
| 634 | size_t fw_size; |
| 635 | enum zen_error_t err = read_file(infile, &fw, &fw_size); |
| 636 | uint8_t file_md5sum[16]; |
| 637 | err = compute_md5sum_buf(fw, fw_size, file_md5sum); |
| 638 | if(err != ZEN_SUCCESS) |
| 639 | { |
| 640 | free(fw); |
| 641 | return err; |
| 642 | } |
| 643 | printf("[INFO] MD5 sum of the file: "); |
| 644 | for(int i = 0; i < 16; i++) |
| 645 | printf("%02X ", file_md5sum[i]); |
| 646 | printf("\n"); |
| 647 | if(opt.model == MODEL_UNKNOWN) |
| 648 | { |
| 649 | int idx; |
| 650 | err = find_model_by_md5sum(file_md5sum, &idx); |
| 651 | if(err != ZEN_SUCCESS) |
| 652 | { |
| 653 | free(fw); |
| 654 | errorf(err, "[ERR] Cannot determine model type\n"); |
| 655 | } |
| 656 | opt.model = zen_sums[idx].model; |
| 657 | printf("[INFO] MD5 matches %s, version %s\n", |
| 658 | zen_models[opt.model].model_name, zen_sums[idx].version); |
| 659 | } |
| 660 | printf("[INFO] Model is: %s\n", zen_models[opt.model].model_name); |
| 661 | /* load rockbox file */ |
| 662 | uint8_t *boot; |
| 663 | size_t boot_size; |
| 664 | err = read_file(bootfile, (void **)&boot, &boot_size); |
| 665 | if(err != ZEN_SUCCESS) |
| 666 | { |
| 667 | free(fw); |
| 668 | errorf(err, "[ERR] Cannot read boot file\n"); |
| 669 | } |
| 670 | /* validate checksum */ |
| 671 | if(memcmp(boot + 4, zen_models[opt.model].rb_model_name, 4) != 0) |
| 672 | { |
| 673 | free(fw); |
| 674 | free(boot); |
| 675 | errorf(ZEN_BOOT_MISMATCH, "[ERR] Boot model mismatch\n"); |
| 676 | } |
| 677 | printf("[INFO] Bootloader file matches model\n"); |
| 678 | uint32_t sum = zen_models[opt.model].rb_model_num; |
| 679 | for(int i = 8; i < boot_size; i++) |
| 680 | sum += boot[i]; |
| 681 | if(sum != be2int(boot)) |
| 682 | { |
| 683 | free(fw); |
| 684 | free(boot); |
| 685 | errorf(ZEN_BOOT_CHECKSUM_ERROR, "[ERR] Checksum mismatch\n"); |
| 686 | } |
| 687 | printf("[INFO] Bootloader file checksum is correct\n"); |
| 688 | /* produce file */ |
| 689 | err = build_firmware(fw, fw_size, boot + 8, boot_size - 8, outfile, opt); |
| 690 | free(boot); |
| 691 | free(fw); |
| 692 | return err; |
| 693 | } |