Dave Chapman | 3c65d2b | 2008-10-11 13:11:47 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
Dave Chapman | 4f80274 | 2008-10-11 13:13:44 +0000 | [diff] [blame] | 8 | * $Id$ |
Dave Chapman | 3c65d2b | 2008-10-11 13:11:47 +0000 | [diff] [blame] | 9 | * |
| 10 | * mkamsboot.c - a tool for merging bootloader code into an Sansa V2 |
| 11 | * (AMS) firmware file |
| 12 | * |
| 13 | * Copyright (C) 2008 Dave Chapman |
| 14 | * |
| 15 | * This program is free software; you can redistribute it and/or |
| 16 | * modify it under the terms of the GNU General Public License |
| 17 | * as published by the Free Software Foundation; either version 2 |
| 18 | * of the License, or (at your option) any later version. |
| 19 | * |
| 20 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 21 | * KIND, either express or implied. |
| 22 | * |
| 23 | ****************************************************************************/ |
Dave Chapman | 1aa6cde | 2008-05-11 18:29:53 +0000 | [diff] [blame] | 24 | |
| 25 | /* |
| 26 | |
Rafaël Carré | 96165ab | 2009-05-28 18:27:08 +0000 | [diff] [blame] | 27 | Insert a Rockbox bootloader into a Sansa AMS original firmware file. |
| 28 | |
| 29 | Layout of a Sansa AMS original firmware file: |
| 30 | |
| 31 | ---------------------- 0x0 |
| 32 | | HEADER | |
| 33 | |----------------------| 0x400 |
| 34 | | FIRMWARE BLOCK | |
| 35 | |----------------------| 0x400 + firmware block size |
| 36 | | LIBRARIES/DATA | |
| 37 | ---------------------- END |
Dave Chapman | 1aa6cde | 2008-05-11 18:29:53 +0000 | [diff] [blame] | 38 | |
Dave Chapman | c2933cd | 2008-10-02 12:37:47 +0000 | [diff] [blame] | 39 | We replace the main firmware block (bytes 0x400..0x400+firmware_size) |
| 40 | as follows: |
Dave Chapman | 1aa6cde | 2008-05-11 18:29:53 +0000 | [diff] [blame] | 41 | |
Dave Chapman | 1aa6cde | 2008-05-11 18:29:53 +0000 | [diff] [blame] | 42 | |
Dave Chapman | 1c4bcfa | 2008-10-11 11:35:59 +0000 | [diff] [blame] | 43 | ---------------------- 0x0 |
| 44 | | | |
| 45 | | Dual-boot code | |
| 46 | | | |
| 47 | |----------------------| |
| 48 | | EMPTY SPACE | |
| 49 | |----------------------| |
| 50 | | | |
| 51 | | compressed RB image | |
| 52 | | | |
| 53 | |----------------------| |
| 54 | | | |
| 55 | | compressed OF image | |
| 56 | | | |
| 57 | |----------------------| |
| 58 | | UCL unpack function | |
| 59 | ---------------------- |
Dave Chapman | 1aa6cde | 2008-05-11 18:29:53 +0000 | [diff] [blame] | 60 | |
Dave Chapman | c2933cd | 2008-10-02 12:37:47 +0000 | [diff] [blame] | 61 | This entire block fits into the space previously occupied by the main |
Dave Chapman | 1c4bcfa | 2008-10-11 11:35:59 +0000 | [diff] [blame] | 62 | firmware block - the space saved by compressing the OF image is used |
Rafaël Carré | 96165ab | 2009-05-28 18:27:08 +0000 | [diff] [blame] | 63 | to store the compressed version of the Rockbox bootloader. |
| 64 | |
| 65 | On version 1 firmwares, the OF image is typically about 120KB, which allows |
| 66 | us to store a Rockbox bootloader with an uncompressed size of about 60KB-70KB. |
| 67 | Version 2 firmwares are bigger and are stored in SDRAM (instead of IRAM). |
| 68 | In both cases, the RAM we are using is mapped at offset 0x0. |
Dave Chapman | 1aa6cde | 2008-05-11 18:29:53 +0000 | [diff] [blame] | 69 | |
Dave Chapman | c2933cd | 2008-10-02 12:37:47 +0000 | [diff] [blame] | 70 | mkamsboot then corrects the checksums and writes a new legal firmware |
| 71 | file which can be installed on the device. |
Dave Chapman | 1aa6cde | 2008-05-11 18:29:53 +0000 | [diff] [blame] | 72 | |
Dave Chapman | 1c4bcfa | 2008-10-11 11:35:59 +0000 | [diff] [blame] | 73 | When the Sansa device boots, this firmware block is loaded to RAM at |
| 74 | address 0x0 and executed. |
Dave Chapman | 1aa6cde | 2008-05-11 18:29:53 +0000 | [diff] [blame] | 75 | |
Dave Chapman | 1c4bcfa | 2008-10-11 11:35:59 +0000 | [diff] [blame] | 76 | Firstly, the dual-boot code will copy the UCL unpack function to the |
| 77 | end of RAM. |
Dave Chapman | 757f511 | 2008-10-01 09:15:44 +0000 | [diff] [blame] | 78 | |
Dave Chapman | 1c4bcfa | 2008-10-11 11:35:59 +0000 | [diff] [blame] | 79 | Then, depending on the detection of the dual-boot keypress, either the |
| 80 | OF image or the Rockbox image is copied to the end of RAM (just before |
Rafaël Carré | 96165ab | 2009-05-28 18:27:08 +0000 | [diff] [blame] | 81 | the ucl unpack function) and uncompressed to the start of RAM. |
Dave Chapman | 1c4bcfa | 2008-10-11 11:35:59 +0000 | [diff] [blame] | 82 | |
| 83 | Finally, the ucl unpack function branches to address 0x0, passing |
| 84 | execution to the uncompressed firmware. |
| 85 | |
Dave Chapman | 757f511 | 2008-10-01 09:15:44 +0000 | [diff] [blame] | 86 | |
Dave Chapman | 1aa6cde | 2008-05-11 18:29:53 +0000 | [diff] [blame] | 87 | */ |
| 88 | |
Dave Chapman | 1aa6cde | 2008-05-11 18:29:53 +0000 | [diff] [blame] | 89 | #include <stdio.h> |
| 90 | #include <stdlib.h> |
| 91 | #include <stdint.h> |
| 92 | #include <sys/types.h> |
| 93 | #include <sys/stat.h> |
| 94 | #include <fcntl.h> |
| 95 | #include <unistd.h> |
| 96 | #include <string.h> |
| 97 | |
Dave Chapman | 1c4bcfa | 2008-10-11 11:35:59 +0000 | [diff] [blame] | 98 | #include <ucl/ucl.h> |
| 99 | |
Rafaël Carré | 96165ab | 2009-05-28 18:27:08 +0000 | [diff] [blame] | 100 | #include "mkamsboot.h" |
| 101 | |
Dave Chapman | c91d787 | 2008-10-30 00:13:29 +0000 | [diff] [blame] | 102 | #include "md5.h" |
| 103 | |
Rafaël Carré | 96165ab | 2009-05-28 18:27:08 +0000 | [diff] [blame] | 104 | /* Header for ARM code binaries */ |
| 105 | #include "dualboot.h" |
Dave Chapman | 1aa6cde | 2008-05-11 18:29:53 +0000 | [diff] [blame] | 106 | |
| 107 | /* Win32 compatibility */ |
| 108 | #ifndef O_BINARY |
| 109 | #define O_BINARY 0 |
| 110 | #endif |
| 111 | |
Rafaël Carré | 8b926e9 | 2010-01-13 03:05:29 +0000 | [diff] [blame] | 112 | /* 4 for m200, 2 for e200/c200, 1 or 2 for fuze/clip, 1 for clip+ */ |
Dominik Riebeling | ea61780 | 2009-11-04 20:58:40 +0000 | [diff] [blame] | 113 | const unsigned short hw_revisions[] = { |
Thomas Martitz | f9eeab2 | 2009-07-05 02:30:33 +0000 | [diff] [blame] | 114 | [MODEL_FUZE] = 1, |
| 115 | [MODEL_CLIP] = 1, |
| 116 | [MODEL_CLIPV2] = 2, |
| 117 | [MODEL_E200V2] = 2, |
| 118 | [MODEL_M200V4] = 4, |
| 119 | [MODEL_C200V2] = 2, |
Rafaël Carré | 8b926e9 | 2010-01-13 03:05:29 +0000 | [diff] [blame] | 120 | [MODEL_CLIPPLUS]= 1, |
Thomas Martitz | f6540e8 | 2010-02-26 13:55:17 +0000 | [diff] [blame] | 121 | [MODEL_FUZEV2] = 2, |
Thomas Martitz | f9eeab2 | 2009-07-05 02:30:33 +0000 | [diff] [blame] | 122 | }; |
| 123 | |
Rafaël Carré | 8b926e9 | 2010-01-13 03:05:29 +0000 | [diff] [blame] | 124 | /* version 2 is used in Clipv2, Clip+ and Fuzev2 firmwares */ |
Dominik Riebeling | ea61780 | 2009-11-04 20:58:40 +0000 | [diff] [blame] | 125 | const unsigned short fw_revisions[] = { |
Thomas Martitz | f9eeab2 | 2009-07-05 02:30:33 +0000 | [diff] [blame] | 126 | [MODEL_FUZE] = 1, |
| 127 | [MODEL_CLIP] = 1, |
| 128 | [MODEL_CLIPV2] = 2, |
| 129 | [MODEL_E200V2] = 1, |
| 130 | [MODEL_M200V4] = 1, |
| 131 | [MODEL_C200V2] = 1, |
Rafaël Carré | 8b926e9 | 2010-01-13 03:05:29 +0000 | [diff] [blame] | 132 | [MODEL_CLIPPLUS]= 2, |
Thomas Martitz | f6540e8 | 2010-02-26 13:55:17 +0000 | [diff] [blame] | 133 | [MODEL_FUZEV2] = 2, |
Thomas Martitz | f9eeab2 | 2009-07-05 02:30:33 +0000 | [diff] [blame] | 134 | }; |
| 135 | |
Rafaël Carré | 96165ab | 2009-05-28 18:27:08 +0000 | [diff] [blame] | 136 | /* Descriptive name of these models */ |
Dominik Riebeling | ea61780 | 2009-11-04 20:58:40 +0000 | [diff] [blame] | 137 | const char* model_names[] = { |
Thomas Martitz | f9eeab2 | 2009-07-05 02:30:33 +0000 | [diff] [blame] | 138 | [MODEL_FUZE] = "Fuze", |
| 139 | [MODEL_CLIP] = "Clip", |
| 140 | [MODEL_CLIPV2] = "Clip", |
Rafaël Carré | 8b926e9 | 2010-01-13 03:05:29 +0000 | [diff] [blame] | 141 | [MODEL_CLIPPLUS]= "Clip+", |
Thomas Martitz | f9eeab2 | 2009-07-05 02:30:33 +0000 | [diff] [blame] | 142 | [MODEL_E200V2] = "e200", |
| 143 | [MODEL_M200V4] = "m200", |
| 144 | [MODEL_C200V2] = "c200", |
Thomas Martitz | f6540e8 | 2010-02-26 13:55:17 +0000 | [diff] [blame] | 145 | [MODEL_FUZEV2] = "Fuze", |
Dave Chapman | 1c4bcfa | 2008-10-11 11:35:59 +0000 | [diff] [blame] | 146 | }; |
| 147 | |
Rafaël Carré | 96165ab | 2009-05-28 18:27:08 +0000 | [diff] [blame] | 148 | /* Dualboot functions for these models */ |
| 149 | static const unsigned char* bootloaders[] = { |
Thomas Martitz | f9eeab2 | 2009-07-05 02:30:33 +0000 | [diff] [blame] | 150 | [MODEL_FUZE] = dualboot_fuze, |
| 151 | [MODEL_CLIP] = dualboot_clip, |
| 152 | [MODEL_CLIPV2] = dualboot_clipv2, |
| 153 | [MODEL_E200V2] = dualboot_e200v2, |
| 154 | [MODEL_M200V4] = dualboot_m200v4, |
| 155 | [MODEL_C200V2] = dualboot_c200v2, |
Rafaël Carré | 8b926e9 | 2010-01-13 03:05:29 +0000 | [diff] [blame] | 156 | [MODEL_CLIPPLUS]= dualboot_clipplus, |
Thomas Martitz | f6540e8 | 2010-02-26 13:55:17 +0000 | [diff] [blame] | 157 | [MODEL_FUZEV2] = dualboot_fuzev2, |
Dave Chapman | 1c4bcfa | 2008-10-11 11:35:59 +0000 | [diff] [blame] | 158 | }; |
| 159 | |
Rafaël Carré | 96165ab | 2009-05-28 18:27:08 +0000 | [diff] [blame] | 160 | /* Size of dualboot functions for these models */ |
Dominik Riebeling | ea61780 | 2009-11-04 20:58:40 +0000 | [diff] [blame] | 161 | const int bootloader_sizes[] = { |
Thomas Martitz | f9eeab2 | 2009-07-05 02:30:33 +0000 | [diff] [blame] | 162 | [MODEL_FUZE] = sizeof(dualboot_fuze), |
| 163 | [MODEL_CLIP] = sizeof(dualboot_clip), |
| 164 | [MODEL_CLIPV2] = sizeof(dualboot_clipv2), |
| 165 | [MODEL_E200V2] = sizeof(dualboot_e200v2), |
| 166 | [MODEL_M200V4] = sizeof(dualboot_m200v4), |
| 167 | [MODEL_C200V2] = sizeof(dualboot_c200v2), |
Rafaël Carré | 8b926e9 | 2010-01-13 03:05:29 +0000 | [diff] [blame] | 168 | [MODEL_CLIPPLUS]= sizeof(dualboot_clipplus), |
Thomas Martitz | f6540e8 | 2010-02-26 13:55:17 +0000 | [diff] [blame] | 169 | [MODEL_FUZEV2] = sizeof(dualboot_fuzev2), |
Dave Chapman | 1c4bcfa | 2008-10-11 11:35:59 +0000 | [diff] [blame] | 170 | }; |
Dave Chapman | 1aa6cde | 2008-05-11 18:29:53 +0000 | [diff] [blame] | 171 | |
Dave Chapman | 6bbe66a | 2008-10-12 19:34:47 +0000 | [diff] [blame] | 172 | /* Model names used in the Rockbox header in ".sansa" files - these match the |
| 173 | -add parameter to the "scramble" tool */ |
Rafaël Carré | 96165ab | 2009-05-28 18:27:08 +0000 | [diff] [blame] | 174 | static const char* rb_model_names[] = { |
Thomas Martitz | f9eeab2 | 2009-07-05 02:30:33 +0000 | [diff] [blame] | 175 | [MODEL_FUZE] = "fuze", |
| 176 | [MODEL_CLIP] = "clip", |
| 177 | [MODEL_CLIPV2] = "clv2", |
| 178 | [MODEL_E200V2] = "e2v2", |
| 179 | [MODEL_M200V4] = "m2v4", |
| 180 | [MODEL_C200V2] = "c2v2", |
Rafaël Carré | 8b926e9 | 2010-01-13 03:05:29 +0000 | [diff] [blame] | 181 | [MODEL_CLIPPLUS]= "cli+", |
Thomas Martitz | f6540e8 | 2010-02-26 13:55:17 +0000 | [diff] [blame] | 182 | [MODEL_FUZEV2] = "fuz2", |
Dave Chapman | 6bbe66a | 2008-10-12 19:34:47 +0000 | [diff] [blame] | 183 | }; |
| 184 | |
| 185 | /* Model numbers used to initialise the checksum in the Rockbox header in |
| 186 | ".sansa" files - these are the same as MODEL_NUMBER in config-target.h */ |
Rafaël Carré | 96165ab | 2009-05-28 18:27:08 +0000 | [diff] [blame] | 187 | static const int rb_model_num[] = { |
Thomas Martitz | f9eeab2 | 2009-07-05 02:30:33 +0000 | [diff] [blame] | 188 | [MODEL_FUZE] = 43, |
| 189 | [MODEL_CLIP] = 40, |
| 190 | [MODEL_CLIPV2] = 60, |
| 191 | [MODEL_E200V2] = 41, |
| 192 | [MODEL_M200V4] = 42, |
Rafaël Carré | 8b926e9 | 2010-01-13 03:05:29 +0000 | [diff] [blame] | 193 | [MODEL_C200V2] = 44, |
| 194 | [MODEL_CLIPPLUS]= 66, |
Thomas Martitz | f6540e8 | 2010-02-26 13:55:17 +0000 | [diff] [blame] | 195 | [MODEL_FUZEV2] = 68, |
Dave Chapman | c91d787 | 2008-10-30 00:13:29 +0000 | [diff] [blame] | 196 | }; |
| 197 | |
| 198 | /* Checksums of unmodified original firmwares - for safety, and device |
| 199 | detection */ |
| 200 | static struct md5sums sansasums[] = { |
| 201 | /* NOTE: Different regional versions of the firmware normally only |
| 202 | differ in the filename - the md5sums are identical */ |
Dave Chapman | c91d787 | 2008-10-30 00:13:29 +0000 | [diff] [blame] | 203 | |
Thomas Martitz | f9eeab2 | 2009-07-05 02:30:33 +0000 | [diff] [blame] | 204 | /* model version md5 */ |
| 205 | { MODEL_E200V2, "3.01.11", "e622ca8cb6df423f54b8b39628a1f0a3" }, |
| 206 | { MODEL_E200V2, "3.01.14", "2c1d0383fc3584b2cc83ba8cc2243af6" }, |
| 207 | { MODEL_E200V2, "3.01.16", "12563ad71b25a1034cf2092d1e0218c4" }, |
Dave Chapman | c91d787 | 2008-10-30 00:13:29 +0000 | [diff] [blame] | 208 | |
Thomas Martitz | f9eeab2 | 2009-07-05 02:30:33 +0000 | [diff] [blame] | 209 | { MODEL_FUZE, "1.01.11", "cac8ffa03c599330ac02c4d41de66166" }, |
| 210 | { MODEL_FUZE, "1.01.15", "df0e2c1612727f722c19a3c764cff7f2" }, |
| 211 | { MODEL_FUZE, "1.01.22", "5aff5486fe8dd64239cc71eac470af98" }, |
| 212 | { MODEL_FUZE, "1.02.26", "7c632c479461c48c8833baed74eb5e4f" }, |
Rafaël Carré | e51dbc0 | 2009-10-10 11:21:15 +0000 | [diff] [blame] | 213 | { MODEL_FUZE, "1.02.28", "5b34260f6470e75f702a9c6825471752" }, |
Michael Chicoine | b1dfdd4 | 2010-01-21 14:34:04 +0000 | [diff] [blame] | 214 | { MODEL_FUZE, "1.02.31", "66d01b37462a5ef7ccc6ad37188b4235" }, |
Dave Chapman | c91d787 | 2008-10-30 00:13:29 +0000 | [diff] [blame] | 215 | |
Thomas Martitz | f9eeab2 | 2009-07-05 02:30:33 +0000 | [diff] [blame] | 216 | { MODEL_C200V2, "3.02.05", "b6378ebd720b0ade3fad4dc7ab61c1a5" }, |
Dave Chapman | c91d787 | 2008-10-30 00:13:29 +0000 | [diff] [blame] | 217 | |
Thomas Martitz | f9eeab2 | 2009-07-05 02:30:33 +0000 | [diff] [blame] | 218 | { MODEL_M200V4, "4.00.45", "82e3194310d1514e3bbcd06e84c4add3" }, |
| 219 | { MODEL_M200V4, "4.01.08-A", "fc9dd6116001b3e6a150b898f1b091f0" }, |
| 220 | { MODEL_M200V4, "4.01.08-E", "d3fb7d8ec8624ee65bc99f8dab0e2369" }, |
Rafaël Carré | 96165ab | 2009-05-28 18:27:08 +0000 | [diff] [blame] | 221 | |
Thomas Martitz | f9eeab2 | 2009-07-05 02:30:33 +0000 | [diff] [blame] | 222 | { MODEL_CLIP, "1.01.17", "12caad785d506219d73f538772afd99e" }, |
| 223 | { MODEL_CLIP, "1.01.18", "d720b266bd5afa38a198986ef0508a45" }, |
| 224 | { MODEL_CLIP, "1.01.20", "236d8f75189f468462c03f6d292cf2ac" }, |
| 225 | { MODEL_CLIP, "1.01.29", "c12711342169c66e209540cd1f27cd26" }, |
| 226 | { MODEL_CLIP, "1.01.30", "f2974d47c536549c9d8259170f1dbe4d" }, |
| 227 | { MODEL_CLIP, "1.01.32", "d835d12342500732ffb9c4ee54abec15" }, |
Rafaël Carré | a384cdf | 2010-03-16 02:11:04 +0000 | [diff] [blame] | 228 | { MODEL_CLIP, "1.01.35", "b4d0edb3b8f2a4e8eee0a344f7f8e480" }, |
Rafaël Carré | 96165ab | 2009-05-28 18:27:08 +0000 | [diff] [blame] | 229 | |
Thomas Martitz | f9eeab2 | 2009-07-05 02:30:33 +0000 | [diff] [blame] | 230 | { MODEL_CLIPV2, "2.01.16", "c57fb3fcbe07c2c9b360f060938f80cb" }, |
Rafaël Carré | 8b926e9 | 2010-01-13 03:05:29 +0000 | [diff] [blame] | 231 | { MODEL_CLIPV2, "2.01.32", "0ad3723e52022509089d938d0fbbf8c5" }, |
Rafaël Carré | 20fccd8 | 2010-03-22 08:32:04 +0000 | [diff] [blame] | 232 | { MODEL_CLIPV2, "2.01.35", "a3cbbd22b9508d7f8a9a1a39acc342c2" }, |
Rafaël Carré | 8b926e9 | 2010-01-13 03:05:29 +0000 | [diff] [blame] | 233 | |
Rafaël Carré | 8b926e9 | 2010-01-13 03:05:29 +0000 | [diff] [blame] | 234 | { MODEL_CLIPPLUS, "01.02.09", "656d38114774c2001dc18e6726df3c5d" }, |
Rafaël Carré | fdf8e2d | 2010-04-17 12:17:01 +0000 | [diff] [blame] | 235 | { MODEL_CLIPPLUS, "01.02.13", "5f89872b79ef440b0e5ee3a7a44328b2" }, |
Rafaël Carré | d9e508b | 2010-05-20 18:51:49 +0000 | [diff] [blame] | 236 | { MODEL_CLIPPLUS, "01.02.15", "680a4f521e790ad25b93b1b16f3a207d" }, |
Thomas Martitz | f6540e8 | 2010-02-26 13:55:17 +0000 | [diff] [blame] | 237 | |
| 238 | { MODEL_FUZEV2, "2.01.17", "8b85fb05bf645d08a4c8c3e344ec9ebe" }, |
| 239 | { MODEL_FUZEV2, "2.02.26", "d4f6f85c3e4a8ea8f2e5acc421641801" }, |
Rafaël Carré | be920c4 | 2010-03-28 02:58:00 +0000 | [diff] [blame] | 240 | { MODEL_FUZEV2, "2.03.31", "74fb197ccd51707388f3b233402186a6" }, |
Rafaël Carré | e2d1eb6 | 2010-05-21 13:13:53 +0000 | [diff] [blame] | 241 | { MODEL_FUZEV2, "2.03.33", "1599cc73d02ea7fe53fe2d4379c24b66" }, |
Dave Chapman | c91d787 | 2008-10-30 00:13:29 +0000 | [diff] [blame] | 242 | }; |
| 243 | |
| 244 | #define NUM_MD5S (sizeof(sansasums)/sizeof(sansasums[0])) |
Dave Chapman | 1aa6cde | 2008-05-11 18:29:53 +0000 | [diff] [blame] | 245 | |
Rafaël Carré | b6c20c1 | 2010-02-19 14:10:26 +0000 | [diff] [blame] | 246 | static unsigned int model_memory_size(int model) |
| 247 | { |
| 248 | if(model == MODEL_CLIPV2) |
| 249 | { |
| 250 | /* The decompressed Clipv2 OF is around 380kB. |
| 251 | * Since it doesn't fit in the 0x50000 bytes IRAM, the OF starts |
| 252 | * with DRAM mapped at 0x0 |
| 253 | * |
| 254 | * We could use all the available memory (supposedly 8MB) |
| 255 | * but 1MB ought to be enough for our use |
| 256 | */ |
| 257 | return 1 << 20; |
| 258 | } |
| 259 | else |
| 260 | { /* The OF boots with IRAM (320kB) mapped at 0x0 */ |
| 261 | return 320 << 10; |
| 262 | } |
| 263 | } |
| 264 | |
Dominik Wenger | a66dfa4 | 2009-08-15 13:04:21 +0000 | [diff] [blame] | 265 | int firmware_revision(int model) |
| 266 | { |
| 267 | return fw_revisions[model]; |
| 268 | } |
| 269 | |
Rafaël Carré | a96f237 | 2009-05-28 21:19:21 +0000 | [diff] [blame] | 270 | static off_t filesize(int fd) |
| 271 | { |
Dave Chapman | 1aa6cde | 2008-05-11 18:29:53 +0000 | [diff] [blame] | 272 | struct stat buf; |
| 273 | |
Rafaël Carré | 96165ab | 2009-05-28 18:27:08 +0000 | [diff] [blame] | 274 | if (fstat(fd, &buf) < 0) { |
Dave Chapman | 1aa6cde | 2008-05-11 18:29:53 +0000 | [diff] [blame] | 275 | perror("[ERR] Checking filesize of input file"); |
| 276 | return -1; |
| 277 | } else { |
| 278 | return(buf.st_size); |
| 279 | } |
| 280 | } |
| 281 | |
Rafaël Carré | a96f237 | 2009-05-28 21:19:21 +0000 | [diff] [blame] | 282 | static uint32_t get_uint32le(unsigned char* p) |
| 283 | { |
Dave Chapman | 1aa6cde | 2008-05-11 18:29:53 +0000 | [diff] [blame] | 284 | return p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24); |
| 285 | } |
| 286 | |
Rafaël Carré | a96f237 | 2009-05-28 21:19:21 +0000 | [diff] [blame] | 287 | static uint32_t get_uint32be(unsigned char* p) |
| 288 | { |
Dave Chapman | 6bbe66a | 2008-10-12 19:34:47 +0000 | [diff] [blame] | 289 | return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]; |
| 290 | } |
| 291 | |
Rafaël Carré | a96f237 | 2009-05-28 21:19:21 +0000 | [diff] [blame] | 292 | static void put_uint32le(unsigned char* p, uint32_t x) |
| 293 | { |
Dave Chapman | 1aa6cde | 2008-05-11 18:29:53 +0000 | [diff] [blame] | 294 | p[0] = x & 0xff; |
| 295 | p[1] = (x >> 8) & 0xff; |
| 296 | p[2] = (x >> 16) & 0xff; |
| 297 | p[3] = (x >> 24) & 0xff; |
| 298 | } |
| 299 | |
Rafaël Carré | a96f237 | 2009-05-28 21:19:21 +0000 | [diff] [blame] | 300 | void calc_MD5(unsigned char* buf, int len, char *md5str) |
| 301 | { |
Dave Chapman | c91d787 | 2008-10-30 00:13:29 +0000 | [diff] [blame] | 302 | int i; |
| 303 | md5_context ctx; |
| 304 | unsigned char md5sum[16]; |
Rafaël Carré | 96165ab | 2009-05-28 18:27:08 +0000 | [diff] [blame] | 305 | |
Dave Chapman | c91d787 | 2008-10-30 00:13:29 +0000 | [diff] [blame] | 306 | md5_starts(&ctx); |
| 307 | md5_update(&ctx, buf, len); |
| 308 | md5_finish(&ctx, md5sum); |
| 309 | |
| 310 | for (i = 0; i < 16; ++i) |
| 311 | sprintf(md5str + 2*i, "%02x", md5sum[i]); |
| 312 | } |
| 313 | |
Rafaël Carré | 96165ab | 2009-05-28 18:27:08 +0000 | [diff] [blame] | 314 | /* Calculate a simple checksum used in Sansa Original Firmwares */ |
Rafaël Carré | a96f237 | 2009-05-28 21:19:21 +0000 | [diff] [blame] | 315 | static uint32_t calc_checksum(unsigned char* buf, uint32_t n) |
| 316 | { |
Dave Chapman | 1aa6cde | 2008-05-11 18:29:53 +0000 | [diff] [blame] | 317 | uint32_t sum = 0; |
| 318 | uint32_t i; |
| 319 | |
| 320 | for (i=0;i<n;i+=4) |
| 321 | sum += get_uint32le(buf + i); |
| 322 | |
| 323 | return sum; |
| 324 | } |
| 325 | |
Rafaël Carré | 96165ab | 2009-05-28 18:27:08 +0000 | [diff] [blame] | 326 | /* Compress using nrv2e algorithm : Thumb decompressor fits in 168 bytes ! */ |
Rafaël Carré | a96f237 | 2009-05-28 21:19:21 +0000 | [diff] [blame] | 327 | static unsigned char* uclpack(unsigned char* inbuf, int insize, int* outsize) |
| 328 | { |
Dave Chapman | 1c4bcfa | 2008-10-11 11:35:59 +0000 | [diff] [blame] | 329 | int maxsize; |
| 330 | unsigned char* outbuf; |
| 331 | int r; |
| 332 | |
| 333 | /* The following formula comes from the UCL documentation */ |
| 334 | maxsize = insize + (insize / 8) + 256; |
| 335 | |
| 336 | /* Allocate some memory for the output buffer */ |
| 337 | outbuf = malloc(maxsize); |
| 338 | |
Rafaël Carré | 96165ab | 2009-05-28 18:27:08 +0000 | [diff] [blame] | 339 | if (outbuf == NULL) |
Dave Chapman | 1c4bcfa | 2008-10-11 11:35:59 +0000 | [diff] [blame] | 340 | return NULL; |
Rafaël Carré | 96165ab | 2009-05-28 18:27:08 +0000 | [diff] [blame] | 341 | |
Dave Chapman | 1c4bcfa | 2008-10-11 11:35:59 +0000 | [diff] [blame] | 342 | r = ucl_nrv2e_99_compress( |
| 343 | (const ucl_bytep) inbuf, |
| 344 | (ucl_uint) insize, |
| 345 | (ucl_bytep) outbuf, |
| 346 | (ucl_uintp) outsize, |
| 347 | 0, 10, NULL, NULL); |
| 348 | |
Rafaël Carré | 96165ab | 2009-05-28 18:27:08 +0000 | [diff] [blame] | 349 | if (r != UCL_E_OK || *outsize > maxsize) { |
Dave Chapman | 1c4bcfa | 2008-10-11 11:35:59 +0000 | [diff] [blame] | 350 | /* this should NEVER happen, and implies memory corruption */ |
| 351 | fprintf(stderr, "internal error - compression failed: %d\n", r); |
| 352 | free(outbuf); |
| 353 | return NULL; |
| 354 | } |
| 355 | |
| 356 | return outbuf; |
| 357 | } |
| 358 | |
Rafaël Carré | 96165ab | 2009-05-28 18:27:08 +0000 | [diff] [blame] | 359 | #define ERROR(format, ...) \ |
| 360 | do { \ |
| 361 | snprintf(errstr, errstrsize, format, __VA_ARGS__); \ |
| 362 | goto error; \ |
| 363 | } while(0) |
| 364 | |
| 365 | /* Loads a Sansa AMS Original Firmware file into memory */ |
| 366 | unsigned char* load_of_file( |
Rafaël Carré | ff6b042 | 2010-05-24 10:06:52 +0000 | [diff] [blame] | 367 | char* filename, int model, off_t* bufsize, struct md5sums *sum, |
Thomas Martitz | f9eeab2 | 2009-07-05 02:30:33 +0000 | [diff] [blame] | 368 | int* firmware_size, unsigned char** of_packed, |
Rafaël Carré | a96f237 | 2009-05-28 21:19:21 +0000 | [diff] [blame] | 369 | int* of_packedsize, char* errstr, int errstrsize) |
| 370 | { |
Dave Chapman | 1c4bcfa | 2008-10-11 11:35:59 +0000 | [diff] [blame] | 371 | int fd; |
Rafaël Carré | 96165ab | 2009-05-28 18:27:08 +0000 | [diff] [blame] | 372 | unsigned char* buf =NULL; |
Dave Chapman | 1c4bcfa | 2008-10-11 11:35:59 +0000 | [diff] [blame] | 373 | off_t n; |
Rafaël Carré | 96165ab | 2009-05-28 18:27:08 +0000 | [diff] [blame] | 374 | unsigned int i=0; |
| 375 | uint32_t checksum; |
Rafaël Carré | 96165ab | 2009-05-28 18:27:08 +0000 | [diff] [blame] | 376 | unsigned int last_word; |
Dave Chapman | 1c4bcfa | 2008-10-11 11:35:59 +0000 | [diff] [blame] | 377 | |
| 378 | fd = open(filename, O_RDONLY|O_BINARY); |
| 379 | if (fd < 0) |
Rafaël Carré | 96165ab | 2009-05-28 18:27:08 +0000 | [diff] [blame] | 380 | ERROR("[ERR] Could not open %s for reading\n", filename); |
Dave Chapman | 1c4bcfa | 2008-10-11 11:35:59 +0000 | [diff] [blame] | 381 | |
| 382 | *bufsize = filesize(fd); |
| 383 | |
| 384 | buf = malloc(*bufsize); |
Rafaël Carré | 96165ab | 2009-05-28 18:27:08 +0000 | [diff] [blame] | 385 | if (buf == NULL) |
| 386 | ERROR("[ERR] Could not allocate memory for %s\n", filename); |
Dave Chapman | 1c4bcfa | 2008-10-11 11:35:59 +0000 | [diff] [blame] | 387 | |
| 388 | n = read(fd, buf, *bufsize); |
| 389 | |
Rafaël Carré | 96165ab | 2009-05-28 18:27:08 +0000 | [diff] [blame] | 390 | if (n != *bufsize) |
| 391 | ERROR("[ERR] Could not read file %s\n", filename); |
| 392 | |
| 393 | /* check the file */ |
| 394 | |
| 395 | /* Calculate MD5 checksum of OF */ |
Thomas Martitz | f9eeab2 | 2009-07-05 02:30:33 +0000 | [diff] [blame] | 396 | calc_MD5(buf, *bufsize, sum->md5); |
Rafaël Carré | 96165ab | 2009-05-28 18:27:08 +0000 | [diff] [blame] | 397 | |
Thomas Martitz | f9eeab2 | 2009-07-05 02:30:33 +0000 | [diff] [blame] | 398 | while ((i < NUM_MD5S) && (strcmp(sansasums[i].md5, sum->md5) != 0)) |
Rafaël Carré | 96165ab | 2009-05-28 18:27:08 +0000 | [diff] [blame] | 399 | i++; |
| 400 | |
| 401 | if (i < NUM_MD5S) { |
Thomas Martitz | f9eeab2 | 2009-07-05 02:30:33 +0000 | [diff] [blame] | 402 | *sum = sansasums[i]; |
Rafaël Carré | ff6b042 | 2010-05-24 10:06:52 +0000 | [diff] [blame] | 403 | if(sum->model != model) { |
| 404 | ERROR("[ERR] OF File provided is %sv%d version %s, not for %sv%d\n", |
| 405 | model_names[sum->model], hw_revisions[sum->model], |
| 406 | sum->version, model_names[model], hw_revisions[model] |
| 407 | ); |
| 408 | } |
Rafaël Carré | 96165ab | 2009-05-28 18:27:08 +0000 | [diff] [blame] | 409 | } else { |
Rafaël Carré | ff6b042 | 2010-05-24 10:06:52 +0000 | [diff] [blame] | 410 | /* OF unknown, give a list of tested versions for the requested model */ |
Rafaël Carré | 96165ab | 2009-05-28 18:27:08 +0000 | [diff] [blame] | 411 | |
Rafaël Carré | 8bb5e43 | 2009-06-04 16:31:11 +0000 | [diff] [blame] | 412 | char tested_versions[100]; |
| 413 | tested_versions[0] = '\0'; |
| 414 | |
| 415 | for (i = 0; i < NUM_MD5S ; i++) |
Rafaël Carré | ff6b042 | 2010-05-24 10:06:52 +0000 | [diff] [blame] | 416 | if (sansasums[i].model == model) { |
Rafaël Carré | 8bb5e43 | 2009-06-04 16:31:11 +0000 | [diff] [blame] | 417 | if (tested_versions[0] != '\0') { |
| 418 | strncat(tested_versions, ", ", |
| 419 | sizeof(tested_versions) - strlen(tested_versions) - 1); |
| 420 | } |
| 421 | strncat(tested_versions, sansasums[i].version, |
| 422 | sizeof(tested_versions) - strlen(tested_versions) - 1); |
| 423 | } |
| 424 | |
| 425 | ERROR("[ERR] Original firmware unknown, please try an other version." \ |
Rafaël Carré | ff6b042 | 2010-05-24 10:06:52 +0000 | [diff] [blame] | 426 | " Tested %sv%d versions are : %s\n", |
| 427 | model_names[model], hw_revisions[model], tested_versions); |
Dave Chapman | 1c4bcfa | 2008-10-11 11:35:59 +0000 | [diff] [blame] | 428 | } |
| 429 | |
Rafaël Carré | 96165ab | 2009-05-28 18:27:08 +0000 | [diff] [blame] | 430 | /* TODO: Do some more sanity checks on the OF image. Some images (like |
| 431 | m200v4) dont have a checksum at the end, only padding (0xdeadbeef). */ |
| 432 | last_word = *bufsize - 4; |
| 433 | checksum = get_uint32le(buf + last_word); |
| 434 | if (checksum != 0xefbeadde && checksum != calc_checksum(buf, last_word)) |
| 435 | ERROR("%s", "[ERR] Whole file checksum failed\n"); |
| 436 | |
Thomas Martitz | f9eeab2 | 2009-07-05 02:30:33 +0000 | [diff] [blame] | 437 | if (bootloaders[sum->model] == NULL) |
| 438 | ERROR("[ERR] Unsupported model - \"%s\"\n", model_names[sum->model]); |
Rafaël Carré | 96165ab | 2009-05-28 18:27:08 +0000 | [diff] [blame] | 439 | |
| 440 | /* Get the firmware size */ |
Thomas Martitz | f9eeab2 | 2009-07-05 02:30:33 +0000 | [diff] [blame] | 441 | if (fw_revisions[sum->model] == 1) |
Rafaël Carré | 96165ab | 2009-05-28 18:27:08 +0000 | [diff] [blame] | 442 | *firmware_size = get_uint32le(&buf[0x0c]); |
Thomas Martitz | f9eeab2 | 2009-07-05 02:30:33 +0000 | [diff] [blame] | 443 | else if (fw_revisions[sum->model] == 2) |
Rafaël Carré | 96165ab | 2009-05-28 18:27:08 +0000 | [diff] [blame] | 444 | *firmware_size = get_uint32le(&buf[0x10]); |
| 445 | |
| 446 | /* Compress the original firmware image */ |
| 447 | *of_packed = uclpack(buf + 0x400, *firmware_size, of_packedsize); |
| 448 | if (*of_packed == NULL) |
| 449 | ERROR("[ERR] Could not compress %s\n", filename); |
| 450 | |
Dave Chapman | 1c4bcfa | 2008-10-11 11:35:59 +0000 | [diff] [blame] | 451 | return buf; |
Rafaël Carré | 96165ab | 2009-05-28 18:27:08 +0000 | [diff] [blame] | 452 | |
| 453 | error: |
| 454 | free(buf); |
| 455 | return NULL; |
Dave Chapman | 1c4bcfa | 2008-10-11 11:35:59 +0000 | [diff] [blame] | 456 | } |
| 457 | |
Rafaël Carré | 96165ab | 2009-05-28 18:27:08 +0000 | [diff] [blame] | 458 | /* Loads a rockbox bootloader file into memory */ |
| 459 | unsigned char* load_rockbox_file( |
Rafaël Carré | ff6b042 | 2010-05-24 10:06:52 +0000 | [diff] [blame] | 460 | char* filename, int *model, int* bufsize, int* rb_packedsize, |
Rafaël Carré | a96f237 | 2009-05-28 21:19:21 +0000 | [diff] [blame] | 461 | char* errstr, int errstrsize) |
| 462 | { |
Dave Chapman | 6bbe66a | 2008-10-12 19:34:47 +0000 | [diff] [blame] | 463 | int fd; |
Rafaël Carré | 96165ab | 2009-05-28 18:27:08 +0000 | [diff] [blame] | 464 | unsigned char* buf = NULL; |
| 465 | unsigned char* packed = NULL; |
Dave Chapman | 6bbe66a | 2008-10-12 19:34:47 +0000 | [diff] [blame] | 466 | unsigned char header[8]; |
| 467 | uint32_t sum; |
| 468 | off_t n; |
| 469 | int i; |
| 470 | |
| 471 | fd = open(filename, O_RDONLY|O_BINARY); |
| 472 | if (fd < 0) |
Rafaël Carré | 96165ab | 2009-05-28 18:27:08 +0000 | [diff] [blame] | 473 | ERROR("[ERR] Could not open %s for reading\n", filename); |
Dave Chapman | 6bbe66a | 2008-10-12 19:34:47 +0000 | [diff] [blame] | 474 | |
| 475 | /* Read Rockbox header */ |
| 476 | n = read(fd, header, sizeof(header)); |
Rafaël Carré | 96165ab | 2009-05-28 18:27:08 +0000 | [diff] [blame] | 477 | if (n != sizeof(header)) |
| 478 | ERROR("[ERR] Could not read file %s\n", filename); |
Dave Chapman | 6bbe66a | 2008-10-12 19:34:47 +0000 | [diff] [blame] | 479 | |
Rafaël Carré | ff6b042 | 2010-05-24 10:06:52 +0000 | [diff] [blame] | 480 | for(*model = 0; *model < NUM_MODELS; (*model)++) |
| 481 | if (memcmp(rb_model_names[*model], header + 4, 4) == 0) |
| 482 | break; |
| 483 | |
| 484 | if(*model == NUM_MODELS) |
| 485 | ERROR("[ERR] Model name \"%4.4s\" unknown. Is this really a rockbox bootloader?\n", header + 4); |
Dave Chapman | 6bbe66a | 2008-10-12 19:34:47 +0000 | [diff] [blame] | 486 | |
| 487 | *bufsize = filesize(fd) - sizeof(header); |
| 488 | |
| 489 | buf = malloc(*bufsize); |
Rafaël Carré | 96165ab | 2009-05-28 18:27:08 +0000 | [diff] [blame] | 490 | if (buf == NULL) |
| 491 | ERROR("[ERR] Could not allocate memory for %s\n", filename); |
Dave Chapman | 6bbe66a | 2008-10-12 19:34:47 +0000 | [diff] [blame] | 492 | |
| 493 | n = read(fd, buf, *bufsize); |
| 494 | |
Rafaël Carré | 96165ab | 2009-05-28 18:27:08 +0000 | [diff] [blame] | 495 | if (n != *bufsize) |
| 496 | ERROR("[ERR] Could not read file %s\n", filename); |
Dave Chapman | 6bbe66a | 2008-10-12 19:34:47 +0000 | [diff] [blame] | 497 | |
| 498 | /* Check checksum */ |
Rafaël Carré | ff6b042 | 2010-05-24 10:06:52 +0000 | [diff] [blame] | 499 | sum = rb_model_num[*model]; |
Dave Chapman | 6bbe66a | 2008-10-12 19:34:47 +0000 | [diff] [blame] | 500 | for (i = 0; i < *bufsize; i++) { |
| 501 | /* add 8 unsigned bits but keep a 32 bit sum */ |
| 502 | sum += buf[i]; |
| 503 | } |
| 504 | |
Rafaël Carré | 96165ab | 2009-05-28 18:27:08 +0000 | [diff] [blame] | 505 | if (sum != get_uint32be(header)) |
| 506 | ERROR("[ERR] Checksum mismatch in %s\n", filename); |
| 507 | |
| 508 | packed = uclpack(buf, *bufsize, rb_packedsize); |
| 509 | if(packed == NULL) |
| 510 | ERROR("[ERR] Could not compress %s\n", filename); |
| 511 | |
| 512 | free(buf); |
| 513 | return packed; |
| 514 | |
| 515 | error: |
| 516 | free(buf); |
| 517 | return NULL; |
Dave Chapman | 6bbe66a | 2008-10-12 19:34:47 +0000 | [diff] [blame] | 518 | } |
| 519 | |
Rafaël Carré | 96165ab | 2009-05-28 18:27:08 +0000 | [diff] [blame] | 520 | #undef ERROR |
Dave Chapman | 6bbe66a | 2008-10-12 19:34:47 +0000 | [diff] [blame] | 521 | |
Rafaël Carré | 96165ab | 2009-05-28 18:27:08 +0000 | [diff] [blame] | 522 | /* Patches a Sansa AMS Original Firmware file */ |
| 523 | void patch_firmware( |
Thomas Martitz | f9eeab2 | 2009-07-05 02:30:33 +0000 | [diff] [blame] | 524 | int model, int fw_revision, int firmware_size, unsigned char* buf, |
Rafaël Carré | 96165ab | 2009-05-28 18:27:08 +0000 | [diff] [blame] | 525 | int len, unsigned char* of_packed, int of_packedsize, |
Rafaël Carré | a96f237 | 2009-05-28 21:19:21 +0000 | [diff] [blame] | 526 | unsigned char* rb_packed, int rb_packedsize) |
| 527 | { |
Rafaël Carré | 96165ab | 2009-05-28 18:27:08 +0000 | [diff] [blame] | 528 | unsigned char *p; |
| 529 | uint32_t sum, filesum; |
Rafaël Carré | b6c20c1 | 2010-02-19 14:10:26 +0000 | [diff] [blame] | 530 | uint32_t ucl_dest; |
Rafaël Carré | 96165ab | 2009-05-28 18:27:08 +0000 | [diff] [blame] | 531 | unsigned int i; |
Dave Chapman | 1aa6cde | 2008-05-11 18:29:53 +0000 | [diff] [blame] | 532 | |
Dave Chapman | 757f511 | 2008-10-01 09:15:44 +0000 | [diff] [blame] | 533 | /* Zero the original firmware area - not needed, but helps debugging */ |
| 534 | memset(buf + 0x400, 0, firmware_size); |
Dave Chapman | 1aa6cde | 2008-05-11 18:29:53 +0000 | [diff] [blame] | 535 | |
Dave Chapman | 1c4bcfa | 2008-10-11 11:35:59 +0000 | [diff] [blame] | 536 | /* Insert dual-boot bootloader at offset 0 */ |
| 537 | memcpy(buf + 0x400, bootloaders[model], bootloader_sizes[model]); |
Dave Chapman | 1aa6cde | 2008-05-11 18:29:53 +0000 | [diff] [blame] | 538 | |
Dave Chapman | 1c4bcfa | 2008-10-11 11:35:59 +0000 | [diff] [blame] | 539 | /* We are filling the firmware buffer backwards from the end */ |
| 540 | p = buf + 0x400 + firmware_size; |
Dave Chapman | 1aa6cde | 2008-05-11 18:29:53 +0000 | [diff] [blame] | 541 | |
Dave Chapman | 1c4bcfa | 2008-10-11 11:35:59 +0000 | [diff] [blame] | 542 | /* 1 - UCL unpack function */ |
Dave Chapman | dcb0e43 | 2008-11-14 22:16:22 +0000 | [diff] [blame] | 543 | p -= sizeof(nrv2e_d8); |
| 544 | memcpy(p, nrv2e_d8, sizeof(nrv2e_d8)); |
Dave Chapman | 1c4bcfa | 2008-10-11 11:35:59 +0000 | [diff] [blame] | 545 | |
| 546 | /* 2 - Compressed copy of original firmware */ |
| 547 | p -= of_packedsize; |
| 548 | memcpy(p, of_packed, of_packedsize); |
| 549 | |
| 550 | /* 3 - Compressed copy of Rockbox bootloader */ |
| 551 | p -= rb_packedsize; |
| 552 | memcpy(p, rb_packed, rb_packedsize); |
| 553 | |
Rafaël Carré | 96165ab | 2009-05-28 18:27:08 +0000 | [diff] [blame] | 554 | /* Write the locations of the various images to the variables at the |
Dave Chapman | 1c4bcfa | 2008-10-11 11:35:59 +0000 | [diff] [blame] | 555 | start of the dualboot image - we save the location of the last byte |
| 556 | in each image, along with the size in bytes */ |
| 557 | |
| 558 | /* UCL unpack function */ |
Dave Chapman | 1a549d4 | 2008-10-11 12:02:23 +0000 | [diff] [blame] | 559 | put_uint32le(&buf[0x420], firmware_size - 1); |
Dave Chapman | dcb0e43 | 2008-11-14 22:16:22 +0000 | [diff] [blame] | 560 | put_uint32le(&buf[0x424], sizeof(nrv2e_d8)); |
Dave Chapman | 1c4bcfa | 2008-10-11 11:35:59 +0000 | [diff] [blame] | 561 | |
| 562 | /* Compressed original firmware image */ |
Dave Chapman | dcb0e43 | 2008-11-14 22:16:22 +0000 | [diff] [blame] | 563 | put_uint32le(&buf[0x428], firmware_size - sizeof(nrv2e_d8) - 1); |
Dave Chapman | 1c4bcfa | 2008-10-11 11:35:59 +0000 | [diff] [blame] | 564 | put_uint32le(&buf[0x42c], of_packedsize); |
| 565 | |
| 566 | /* Compressed Rockbox image */ |
Rafaël Carré | 96165ab | 2009-05-28 18:27:08 +0000 | [diff] [blame] | 567 | put_uint32le(&buf[0x430], firmware_size - sizeof(nrv2e_d8) - of_packedsize |
| 568 | - 1); |
Dave Chapman | 1c4bcfa | 2008-10-11 11:35:59 +0000 | [diff] [blame] | 569 | put_uint32le(&buf[0x434], rb_packedsize); |
Dave Chapman | 757f511 | 2008-10-01 09:15:44 +0000 | [diff] [blame] | 570 | |
Rafaël Carré | b6c20c1 | 2010-02-19 14:10:26 +0000 | [diff] [blame] | 571 | ucl_dest = model_memory_size(model) - 1; /* last byte of memory */ |
| 572 | put_uint32le(&buf[0x438], ucl_dest); |
Dave Chapman | 757f511 | 2008-10-01 09:15:44 +0000 | [diff] [blame] | 573 | |
Dave Chapman | 1c4bcfa | 2008-10-11 11:35:59 +0000 | [diff] [blame] | 574 | /* Update the firmware block checksum */ |
Rafaël Carré | 96165ab | 2009-05-28 18:27:08 +0000 | [diff] [blame] | 575 | sum = calc_checksum(buf + 0x400, firmware_size); |
Dave Chapman | 1aa6cde | 2008-05-11 18:29:53 +0000 | [diff] [blame] | 576 | |
Thomas Martitz | f9eeab2 | 2009-07-05 02:30:33 +0000 | [diff] [blame] | 577 | if (fw_revision == 1) { |
Dave Chapman | 1c4bcfa | 2008-10-11 11:35:59 +0000 | [diff] [blame] | 578 | put_uint32le(&buf[0x04], sum); |
| 579 | put_uint32le(&buf[0x204], sum); |
Thomas Martitz | f9eeab2 | 2009-07-05 02:30:33 +0000 | [diff] [blame] | 580 | } else if (fw_revision == 2) { |
Dave Chapman | 1c4bcfa | 2008-10-11 11:35:59 +0000 | [diff] [blame] | 581 | put_uint32le(&buf[0x08], sum); |
| 582 | put_uint32le(&buf[0x208], sum); |
| 583 | |
| 584 | /* Update the header checksums */ |
| 585 | put_uint32le(&buf[0x1fc], calc_checksum(buf, 0x1fc)); |
| 586 | put_uint32le(&buf[0x3fc], calc_checksum(buf + 0x200, 0x1fc)); |
| 587 | } |
Dave Chapman | 1aa6cde | 2008-05-11 18:29:53 +0000 | [diff] [blame] | 588 | |
Dave Chapman | 1aa6cde | 2008-05-11 18:29:53 +0000 | [diff] [blame] | 589 | /* Update the whole-file checksum */ |
| 590 | filesum = 0; |
Dave Chapman | 757f511 | 2008-10-01 09:15:44 +0000 | [diff] [blame] | 591 | for (i=0;i < (unsigned)len - 4; i+=4) |
Dave Chapman | 1aa6cde | 2008-05-11 18:29:53 +0000 | [diff] [blame] | 592 | filesum += get_uint32le(&buf[i]); |
| 593 | |
Dave Chapman | 757f511 | 2008-10-01 09:15:44 +0000 | [diff] [blame] | 594 | put_uint32le(buf + len - 4, filesum); |
Rafaël Carré | 96165ab | 2009-05-28 18:27:08 +0000 | [diff] [blame] | 595 | } |
Dave Chapman | 1aa6cde | 2008-05-11 18:29:53 +0000 | [diff] [blame] | 596 | |
Rafaël Carré | b6c20c1 | 2010-02-19 14:10:26 +0000 | [diff] [blame] | 597 | /* returns != 0 if the firmware can be safely patched */ |
| 598 | int check_sizes(int model, int rb_packed_size, int rb_unpacked_size, |
| 599 | int of_packed_size, int of_unpacked_size, int *total_size, |
| 600 | char *errstr, int errstrsize) |
Rafaël Carré | a96f237 | 2009-05-28 21:19:21 +0000 | [diff] [blame] | 601 | { |
Rafaël Carré | b6c20c1 | 2010-02-19 14:10:26 +0000 | [diff] [blame] | 602 | unsigned int packed_size = bootloader_sizes[model] + sizeof(nrv2e_d8) + |
| 603 | of_packed_size + rb_packed_size; |
Rafaël Carré | 96165ab | 2009-05-28 18:27:08 +0000 | [diff] [blame] | 604 | |
Rafaël Carré | b6c20c1 | 2010-02-19 14:10:26 +0000 | [diff] [blame] | 605 | /* how much memory is available */ |
| 606 | unsigned int memory_size = model_memory_size(model); |
| 607 | |
| 608 | /* the memory used when unpacking the OF */ |
| 609 | unsigned int ram_of = sizeof(nrv2e_d8) + of_packed_size + of_unpacked_size; |
| 610 | |
| 611 | /* the memory used when unpacking the bootloader */ |
| 612 | unsigned int ram_rb = sizeof(nrv2e_d8) + rb_packed_size + rb_unpacked_size; |
| 613 | |
| 614 | *total_size = packed_size; |
| 615 | |
| 616 | #define ERROR(format, ...) \ |
| 617 | do { \ |
| 618 | snprintf(errstr, errstrsize, format, __VA_ARGS__); \ |
| 619 | return 0; \ |
| 620 | } while(0) |
| 621 | |
| 622 | /* will packed data fit in the OF file ? */ |
| 623 | if(packed_size > of_unpacked_size) |
| 624 | ERROR( |
| 625 | "[ERR] Packed data (%d bytes) doesn't fit in the firmware " |
| 626 | "(%d bytes)\n", packed_size, of_unpacked_size |
| 627 | ); |
| 628 | |
| 629 | else if(ram_rb > memory_size) |
| 630 | ERROR("[ERR] Rockbox can't be unpacked at runtime, needs %d bytes " |
| 631 | "of memory and only %d available\n", ram_rb, memory_size |
| 632 | ); |
| 633 | |
| 634 | else if(ram_of > memory_size) |
| 635 | ERROR("[ERR] OF can't be unpacked at runtime, needs %d bytes " |
| 636 | "of memory and only %d available\n", ram_of, memory_size |
| 637 | ); |
| 638 | |
| 639 | return 1; |
| 640 | |
| 641 | #undef ERROR |
| 642 | } |