Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | a131c1f | 2015-08-30 16:55:24 -0600 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2015 Google, Inc |
| 4 | * Written by Simon Glass <sjg@chromium.org> |
| 5 | * |
Philipp Tomsich | 2fb371f | 2017-05-30 23:32:08 +0200 | [diff] [blame] | 6 | * (C) 2017 Theobroma Systems Design und Consulting GmbH |
| 7 | * |
Simon Glass | a131c1f | 2015-08-30 16:55:24 -0600 | [diff] [blame] | 8 | * Helper functions for Rockchip images |
| 9 | */ |
| 10 | |
| 11 | #include "imagetool.h" |
| 12 | #include <image.h> |
| 13 | #include <rc4.h> |
| 14 | #include "mkimage.h" |
| 15 | #include "rkcommon.h" |
| 16 | |
| 17 | enum { |
| 18 | RK_SIGNATURE = 0x0ff0aa55, |
| 19 | }; |
| 20 | |
| 21 | /** |
| 22 | * struct header0_info - header block for boot ROM |
| 23 | * |
| 24 | * This is stored at SD card block 64 (where each block is 512 bytes, or at |
| 25 | * the start of SPI flash. It is encoded with RC4. |
| 26 | * |
| 27 | * @signature: Signature (must be RKSD_SIGNATURE) |
| 28 | * @disable_rc4: 0 to use rc4 for boot image, 1 to use plain binary |
Jeffy Chen | 3641339 | 2015-11-17 14:20:30 +0800 | [diff] [blame] | 29 | * @init_offset: Offset in blocks of the SPL code from this header |
Simon Glass | a131c1f | 2015-08-30 16:55:24 -0600 | [diff] [blame] | 30 | * block. E.g. 4 means 2KB after the start of this header. |
| 31 | * Other fields are not used by U-Boot |
| 32 | */ |
| 33 | struct header0_info { |
| 34 | uint32_t signature; |
| 35 | uint8_t reserved[4]; |
| 36 | uint32_t disable_rc4; |
Jeffy Chen | 3641339 | 2015-11-17 14:20:30 +0800 | [diff] [blame] | 37 | uint16_t init_offset; |
| 38 | uint8_t reserved1[492]; |
| 39 | uint16_t init_size; |
| 40 | uint16_t init_boot_size; |
Simon Glass | a131c1f | 2015-08-30 16:55:24 -0600 | [diff] [blame] | 41 | uint8_t reserved2[2]; |
| 42 | }; |
| 43 | |
Jeffy Chen | 7bf274b | 2015-11-27 12:07:17 +0800 | [diff] [blame] | 44 | /** |
Philipp Tomsich | 111bcc4 | 2017-03-15 12:08:43 +0100 | [diff] [blame] | 45 | * struct header1_info |
| 46 | */ |
| 47 | struct header1_info { |
| 48 | uint32_t magic; |
Philipp Tomsich | 111bcc4 | 2017-03-15 12:08:43 +0100 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | /** |
Jeffy Chen | 7bf274b | 2015-11-27 12:07:17 +0800 | [diff] [blame] | 52 | * struct spl_info - spl info for each chip |
| 53 | * |
| 54 | * @imagename: Image name(passed by "mkimage -n") |
| 55 | * @spl_hdr: Boot ROM requires a 4-bytes spl header |
| 56 | * @spl_size: Spl size(include extra 4-bytes spl header) |
Heiko Stübner | cfbcdad | 2017-02-18 19:46:27 +0100 | [diff] [blame] | 57 | * @spl_rc4: RC4 encode the SPL binary (same key as header) |
Jeffy Chen | 7bf274b | 2015-11-27 12:07:17 +0800 | [diff] [blame] | 58 | */ |
Philipp Tomsich | 111bcc4 | 2017-03-15 12:08:43 +0100 | [diff] [blame] | 59 | |
Jeffy Chen | 7bf274b | 2015-11-27 12:07:17 +0800 | [diff] [blame] | 60 | struct spl_info { |
| 61 | const char *imagename; |
| 62 | const char *spl_hdr; |
| 63 | const uint32_t spl_size; |
Heiko Stübner | cfbcdad | 2017-02-18 19:46:27 +0100 | [diff] [blame] | 64 | const bool spl_rc4; |
Jeffy Chen | 7bf274b | 2015-11-27 12:07:17 +0800 | [diff] [blame] | 65 | }; |
| 66 | |
| 67 | static struct spl_info spl_infos[] = { |
Kever Yang | c6e66b1 | 2019-07-12 11:43:34 +0200 | [diff] [blame] | 68 | { "px30", "RK33", 0x2800, false }, |
Philipp Tomsich | 95d363c | 2017-10-10 16:21:18 +0200 | [diff] [blame] | 69 | { "rk3036", "RK30", 0x1000, false }, |
| 70 | { "rk3128", "RK31", 0x1800, false }, |
| 71 | { "rk3188", "RK31", 0x8000 - 0x800, true }, |
| 72 | { "rk322x", "RK32", 0x8000 - 0x1000, false }, |
| 73 | { "rk3288", "RK32", 0x8000, false }, |
Andy Yan | 644c6be | 2019-11-14 11:22:34 +0800 | [diff] [blame] | 74 | { "rk3308", "RK33", 0x40000 - 0x1000, false}, |
Philipp Tomsich | 95d363c | 2017-10-10 16:21:18 +0200 | [diff] [blame] | 75 | { "rk3328", "RK32", 0x8000 - 0x1000, false }, |
| 76 | { "rk3368", "RK33", 0x8000 - 0x1000, false }, |
| 77 | { "rk3399", "RK33", 0x30000 - 0x2000, false }, |
| 78 | { "rv1108", "RK11", 0x1800, false }, |
Jeffy Chen | 7bf274b | 2015-11-27 12:07:17 +0800 | [diff] [blame] | 79 | }; |
| 80 | |
Jeffy Chen | eea6cd8 | 2019-12-27 11:24:41 +0800 | [diff] [blame] | 81 | /** |
| 82 | * struct spl_params - spl params parsed in check_params() |
| 83 | * |
| 84 | * @init_file: Init data file path |
| 85 | * @init_size: Aligned size of init data in bytes |
| 86 | * @boot_file: Boot data file path |
| 87 | * @boot_size: Aligned size of boot data in bytes |
| 88 | */ |
| 89 | |
| 90 | struct spl_params { |
| 91 | char *init_file; |
| 92 | uint32_t init_size; |
| 93 | char *boot_file; |
| 94 | uint32_t boot_size; |
| 95 | }; |
| 96 | |
| 97 | static struct spl_params spl_params = { 0 }; |
| 98 | |
Simon Glass | a131c1f | 2015-08-30 16:55:24 -0600 | [diff] [blame] | 99 | static unsigned char rc4_key[16] = { |
| 100 | 124, 78, 3, 4, 85, 5, 9, 7, |
| 101 | 45, 44, 123, 56, 23, 13, 23, 17 |
| 102 | }; |
| 103 | |
Jeffy Chen | 7bf274b | 2015-11-27 12:07:17 +0800 | [diff] [blame] | 104 | static struct spl_info *rkcommon_get_spl_info(char *imagename) |
| 105 | { |
| 106 | int i; |
| 107 | |
Philipp Tomsich | 24aae93 | 2017-04-17 17:48:05 +0200 | [diff] [blame] | 108 | if (!imagename) |
| 109 | return NULL; |
| 110 | |
Jeffy Chen | 7bf274b | 2015-11-27 12:07:17 +0800 | [diff] [blame] | 111 | for (i = 0; i < ARRAY_SIZE(spl_infos); i++) |
| 112 | if (!strncmp(imagename, spl_infos[i].imagename, 6)) |
| 113 | return spl_infos + i; |
| 114 | |
| 115 | return NULL; |
| 116 | } |
| 117 | |
Jeffy Chen | eea6cd8 | 2019-12-27 11:24:41 +0800 | [diff] [blame] | 118 | static int rkcommon_get_aligned_size(struct image_tool_params *params, |
| 119 | const char *fname) |
| 120 | { |
| 121 | int size; |
| 122 | |
| 123 | size = imagetool_get_filesize(params, fname); |
| 124 | if (size < 0) |
| 125 | return -1; |
| 126 | |
| 127 | /* |
| 128 | * Pad to a 2KB alignment, as required for init/boot size by the ROM |
| 129 | * (see https://lists.denx.de/pipermail/u-boot/2017-May/293268.html) |
| 130 | */ |
| 131 | return ROUND(size, RK_SIZE_ALIGN); |
| 132 | } |
| 133 | |
Jeffy Chen | 7bf274b | 2015-11-27 12:07:17 +0800 | [diff] [blame] | 134 | int rkcommon_check_params(struct image_tool_params *params) |
| 135 | { |
| 136 | int i; |
| 137 | |
Philipp Tomsich | 24aae93 | 2017-04-17 17:48:05 +0200 | [diff] [blame] | 138 | /* |
| 139 | * If this is a operation (list or extract), the don't require |
| 140 | * imagename to be set. |
| 141 | */ |
| 142 | if (params->lflag || params->iflag) |
| 143 | return EXIT_SUCCESS; |
Jeffy Chen | 7bf274b | 2015-11-27 12:07:17 +0800 | [diff] [blame] | 144 | |
Jeffy Chen | eea6cd8 | 2019-12-27 11:24:41 +0800 | [diff] [blame] | 145 | if (!rkcommon_get_spl_info(params->imagename)) |
| 146 | goto err_spl_info; |
| 147 | |
| 148 | spl_params.init_file = params->datafile; |
| 149 | |
| 150 | spl_params.boot_file = strchr(spl_params.init_file, ':'); |
| 151 | if (spl_params.boot_file) { |
| 152 | *spl_params.boot_file = '\0'; |
| 153 | spl_params.boot_file += 1; |
| 154 | } |
| 155 | |
| 156 | spl_params.init_size = |
| 157 | rkcommon_get_aligned_size(params, spl_params.init_file); |
| 158 | if (spl_params.init_size < 0) |
| 159 | return EXIT_FAILURE; |
| 160 | |
| 161 | /* Boot file is optional, and only for back-to-bootrom functionality. */ |
| 162 | if (spl_params.boot_file) { |
| 163 | spl_params.boot_size = |
| 164 | rkcommon_get_aligned_size(params, spl_params.boot_file); |
| 165 | if (spl_params.boot_size < 0) |
| 166 | return EXIT_FAILURE; |
| 167 | } |
| 168 | |
| 169 | if (spl_params.init_size > rkcommon_get_spl_size(params)) { |
| 170 | fprintf(stderr, |
| 171 | "Error: SPL image is too large (size %#x than %#x)\n", |
| 172 | spl_params.init_size, rkcommon_get_spl_size(params)); |
| 173 | return EXIT_FAILURE; |
| 174 | } |
| 175 | |
| 176 | return EXIT_SUCCESS; |
| 177 | |
| 178 | err_spl_info: |
Jeffy Chen | 7bf274b | 2015-11-27 12:07:17 +0800 | [diff] [blame] | 179 | fprintf(stderr, "ERROR: imagename (%s) is not supported!\n", |
Philipp Tomsich | 24aae93 | 2017-04-17 17:48:05 +0200 | [diff] [blame] | 180 | params->imagename ? params->imagename : "NULL"); |
Jeffy Chen | 7bf274b | 2015-11-27 12:07:17 +0800 | [diff] [blame] | 181 | |
| 182 | fprintf(stderr, "Available imagename:"); |
| 183 | for (i = 0; i < ARRAY_SIZE(spl_infos); i++) |
| 184 | fprintf(stderr, "\t%s", spl_infos[i].imagename); |
| 185 | fprintf(stderr, "\n"); |
| 186 | |
Philipp Tomsich | 24aae93 | 2017-04-17 17:48:05 +0200 | [diff] [blame] | 187 | return EXIT_FAILURE; |
Jeffy Chen | 7bf274b | 2015-11-27 12:07:17 +0800 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | const char *rkcommon_get_spl_hdr(struct image_tool_params *params) |
| 191 | { |
| 192 | struct spl_info *info = rkcommon_get_spl_info(params->imagename); |
| 193 | |
| 194 | /* |
| 195 | * info would not be NULL, because of we checked params before. |
| 196 | */ |
| 197 | return info->spl_hdr; |
| 198 | } |
| 199 | |
Philipp Tomsich | 111bcc4 | 2017-03-15 12:08:43 +0100 | [diff] [blame] | 200 | |
Jeffy Chen | 7bf274b | 2015-11-27 12:07:17 +0800 | [diff] [blame] | 201 | int rkcommon_get_spl_size(struct image_tool_params *params) |
| 202 | { |
| 203 | struct spl_info *info = rkcommon_get_spl_info(params->imagename); |
| 204 | |
| 205 | /* |
| 206 | * info would not be NULL, because of we checked params before. |
| 207 | */ |
| 208 | return info->spl_size; |
| 209 | } |
| 210 | |
Heiko Stübner | cfbcdad | 2017-02-18 19:46:27 +0100 | [diff] [blame] | 211 | bool rkcommon_need_rc4_spl(struct image_tool_params *params) |
| 212 | { |
| 213 | struct spl_info *info = rkcommon_get_spl_info(params->imagename); |
| 214 | |
| 215 | /* |
| 216 | * info would not be NULL, because of we checked params before. |
| 217 | */ |
| 218 | return info->spl_rc4; |
| 219 | } |
| 220 | |
Jeffy Chen | eea6cd8 | 2019-12-27 11:24:41 +0800 | [diff] [blame] | 221 | static void rkcommon_set_header0(void *buf, struct image_tool_params *params) |
Simon Glass | a131c1f | 2015-08-30 16:55:24 -0600 | [diff] [blame] | 222 | { |
Philipp Tomsich | 111bcc4 | 2017-03-15 12:08:43 +0100 | [diff] [blame] | 223 | struct header0_info *hdr = buf; |
Simon Glass | a131c1f | 2015-08-30 16:55:24 -0600 | [diff] [blame] | 224 | |
Philipp Tomsich | 111bcc4 | 2017-03-15 12:08:43 +0100 | [diff] [blame] | 225 | memset(buf, '\0', RK_INIT_OFFSET * RK_BLK_SIZE); |
Simon Glass | a131c1f | 2015-08-30 16:55:24 -0600 | [diff] [blame] | 226 | hdr->signature = RK_SIGNATURE; |
Heiko Stübner | cfbcdad | 2017-02-18 19:46:27 +0100 | [diff] [blame] | 227 | hdr->disable_rc4 = !rkcommon_need_rc4_spl(params); |
Jeffy Chen | 3641339 | 2015-11-17 14:20:30 +0800 | [diff] [blame] | 228 | hdr->init_offset = RK_INIT_OFFSET; |
Jeffy Chen | eea6cd8 | 2019-12-27 11:24:41 +0800 | [diff] [blame] | 229 | hdr->init_size = spl_params.init_size / RK_BLK_SIZE; |
Simon Glass | a131c1f | 2015-08-30 16:55:24 -0600 | [diff] [blame] | 230 | |
Philipp Tomsich | a1a2dfb | 2017-04-17 17:48:04 +0200 | [diff] [blame] | 231 | /* |
Philipp Tomsich | a1c29d4 | 2017-05-30 23:32:10 +0200 | [diff] [blame] | 232 | * init_boot_size needs to be set, as it is read by the BootROM |
| 233 | * to determine the size of the next-stage bootloader (e.g. U-Boot |
| 234 | * proper), when used with the back-to-bootrom functionality. |
| 235 | * |
| 236 | * see https://lists.denx.de/pipermail/u-boot/2017-May/293267.html |
| 237 | * for a more detailed explanation by Andy Yan |
Philipp Tomsich | a1a2dfb | 2017-04-17 17:48:04 +0200 | [diff] [blame] | 238 | */ |
Jeffy Chen | eea6cd8 | 2019-12-27 11:24:41 +0800 | [diff] [blame] | 239 | if (spl_params.boot_file) |
| 240 | hdr->init_boot_size = |
| 241 | hdr->init_size + spl_params.boot_size / RK_BLK_SIZE; |
| 242 | else |
| 243 | hdr->init_boot_size = |
| 244 | hdr->init_size + RK_MAX_BOOT_SIZE / RK_BLK_SIZE; |
Simon Glass | a131c1f | 2015-08-30 16:55:24 -0600 | [diff] [blame] | 245 | |
| 246 | rc4_encode(buf, RK_BLK_SIZE, rc4_key); |
Philipp Tomsich | 111bcc4 | 2017-03-15 12:08:43 +0100 | [diff] [blame] | 247 | } |
| 248 | |
Jeffy Chen | eea6cd8 | 2019-12-27 11:24:41 +0800 | [diff] [blame] | 249 | void rkcommon_set_header(void *buf, struct stat *sbuf, int ifd, |
| 250 | struct image_tool_params *params) |
Philipp Tomsich | 111bcc4 | 2017-03-15 12:08:43 +0100 | [diff] [blame] | 251 | { |
| 252 | struct header1_info *hdr = buf + RK_SPL_HDR_START; |
| 253 | |
Jeffy Chen | eea6cd8 | 2019-12-27 11:24:41 +0800 | [diff] [blame] | 254 | rkcommon_set_header0(buf, params); |
Philipp Tomsich | 111bcc4 | 2017-03-15 12:08:43 +0100 | [diff] [blame] | 255 | |
Philipp Tomsich | 2fb371f | 2017-05-30 23:32:08 +0200 | [diff] [blame] | 256 | /* Set up the SPL name (i.e. copy spl_hdr over) */ |
Philipp Tomsich | 111bcc4 | 2017-03-15 12:08:43 +0100 | [diff] [blame] | 257 | memcpy(&hdr->magic, rkcommon_get_spl_hdr(params), RK_SPL_HDR_SIZE); |
| 258 | |
Philipp Tomsich | 111bcc4 | 2017-03-15 12:08:43 +0100 | [diff] [blame] | 259 | if (rkcommon_need_rc4_spl(params)) |
| 260 | rkcommon_rc4_encode_spl(buf, RK_SPL_HDR_START, |
Jeffy Chen | eea6cd8 | 2019-12-27 11:24:41 +0800 | [diff] [blame] | 261 | spl_params.init_size); |
Simon Glass | a131c1f | 2015-08-30 16:55:24 -0600 | [diff] [blame] | 262 | |
Jeffy Chen | eea6cd8 | 2019-12-27 11:24:41 +0800 | [diff] [blame] | 263 | if (spl_params.boot_file) { |
| 264 | if (rkcommon_need_rc4_spl(params)) |
| 265 | rkcommon_rc4_encode_spl(buf + RK_SPL_HDR_START, |
| 266 | spl_params.init_size, |
| 267 | spl_params.boot_size); |
| 268 | } |
Simon Glass | a131c1f | 2015-08-30 16:55:24 -0600 | [diff] [blame] | 269 | } |
Heiko Stübner | cfbcdad | 2017-02-18 19:46:27 +0100 | [diff] [blame] | 270 | |
Philipp Tomsich | 2fb371f | 2017-05-30 23:32:08 +0200 | [diff] [blame] | 271 | static inline unsigned rkcommon_offset_to_spi(unsigned offset) |
| 272 | { |
| 273 | /* |
| 274 | * While SD/MMC images use a flat addressing, SPI images are padded |
| 275 | * to use the first 2K of every 4K sector only. |
| 276 | */ |
| 277 | return ((offset & ~0x7ff) << 1) + (offset & 0x7ff); |
| 278 | } |
| 279 | |
Philipp Tomsich | 2fb371f | 2017-05-30 23:32:08 +0200 | [diff] [blame] | 280 | static int rkcommon_parse_header(const void *buf, struct header0_info *header0, |
| 281 | struct spl_info **spl_info) |
| 282 | { |
| 283 | unsigned hdr1_offset; |
| 284 | struct header1_info *hdr1_sdmmc, *hdr1_spi; |
| 285 | int i; |
| 286 | |
| 287 | if (spl_info) |
| 288 | *spl_info = NULL; |
| 289 | |
| 290 | /* |
| 291 | * The first header (hdr0) is always RC4 encoded, so try to decrypt |
| 292 | * with the well-known key. |
| 293 | */ |
| 294 | memcpy((void *)header0, buf, sizeof(struct header0_info)); |
| 295 | rc4_encode((void *)header0, sizeof(struct header0_info), rc4_key); |
| 296 | |
| 297 | if (header0->signature != RK_SIGNATURE) |
| 298 | return -EPROTO; |
| 299 | |
| 300 | /* We don't support RC4 encoded image payloads here, yet... */ |
| 301 | if (header0->disable_rc4 == 0) |
| 302 | return -ENOSYS; |
| 303 | |
| 304 | hdr1_offset = header0->init_offset * RK_BLK_SIZE; |
| 305 | hdr1_sdmmc = (struct header1_info *)(buf + hdr1_offset); |
| 306 | hdr1_spi = (struct header1_info *)(buf + |
| 307 | rkcommon_offset_to_spi(hdr1_offset)); |
| 308 | |
| 309 | for (i = 0; i < ARRAY_SIZE(spl_infos); i++) { |
| 310 | if (!memcmp(&hdr1_sdmmc->magic, spl_infos[i].spl_hdr, 4)) { |
| 311 | if (spl_info) |
| 312 | *spl_info = &spl_infos[i]; |
| 313 | return IH_TYPE_RKSD; |
| 314 | } else if (!memcmp(&hdr1_spi->magic, spl_infos[i].spl_hdr, 4)) { |
| 315 | if (spl_info) |
| 316 | *spl_info = &spl_infos[i]; |
| 317 | return IH_TYPE_RKSPI; |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | return -1; |
| 322 | } |
| 323 | |
| 324 | int rkcommon_verify_header(unsigned char *buf, int size, |
| 325 | struct image_tool_params *params) |
| 326 | { |
| 327 | struct header0_info header0; |
| 328 | struct spl_info *img_spl_info, *spl_info; |
| 329 | int ret; |
| 330 | |
| 331 | ret = rkcommon_parse_header(buf, &header0, &img_spl_info); |
| 332 | |
| 333 | /* If this is the (unimplemented) RC4 case, then rewrite the result */ |
| 334 | if (ret == -ENOSYS) |
| 335 | return 0; |
| 336 | |
| 337 | if (ret < 0) |
| 338 | return ret; |
| 339 | |
| 340 | /* |
| 341 | * If no 'imagename' is specified via the commandline (e.g. if this is |
| 342 | * 'dumpimage -l' w/o any further constraints), we accept any spl_info. |
| 343 | */ |
| 344 | if (params->imagename == NULL) |
| 345 | return 0; |
| 346 | |
| 347 | /* Match the 'imagename' against the 'spl_hdr' found */ |
| 348 | spl_info = rkcommon_get_spl_info(params->imagename); |
| 349 | if (spl_info && img_spl_info) |
| 350 | return strcmp(spl_info->spl_hdr, img_spl_info->spl_hdr); |
| 351 | |
| 352 | return -ENOENT; |
| 353 | } |
| 354 | |
| 355 | void rkcommon_print_header(const void *buf) |
| 356 | { |
| 357 | struct header0_info header0; |
| 358 | struct spl_info *spl_info; |
| 359 | uint8_t image_type; |
Jeffy Chen | eea6cd8 | 2019-12-27 11:24:41 +0800 | [diff] [blame] | 360 | int ret, boot_size; |
Philipp Tomsich | 2fb371f | 2017-05-30 23:32:08 +0200 | [diff] [blame] | 361 | |
| 362 | ret = rkcommon_parse_header(buf, &header0, &spl_info); |
| 363 | |
| 364 | /* If this is the (unimplemented) RC4 case, then fail silently */ |
| 365 | if (ret == -ENOSYS) |
| 366 | return; |
| 367 | |
| 368 | if (ret < 0) { |
| 369 | fprintf(stderr, "Error: image verification failed\n"); |
| 370 | return; |
| 371 | } |
| 372 | |
| 373 | image_type = ret; |
| 374 | |
| 375 | printf("Image Type: Rockchip %s (%s) boot image\n", |
| 376 | spl_info->spl_hdr, |
| 377 | (image_type == IH_TYPE_RKSD) ? "SD/MMC" : "SPI"); |
Jeffy Chen | eea6cd8 | 2019-12-27 11:24:41 +0800 | [diff] [blame] | 378 | printf("Init Data Size: %d bytes\n", header0.init_size * RK_BLK_SIZE); |
| 379 | |
| 380 | boot_size = (header0.init_boot_size - header0.init_size) * RK_BLK_SIZE; |
| 381 | if (boot_size != RK_MAX_BOOT_SIZE) |
| 382 | printf("Boot Data Size: %d bytes\n", boot_size); |
Philipp Tomsich | 2fb371f | 2017-05-30 23:32:08 +0200 | [diff] [blame] | 383 | } |
| 384 | |
Heiko Stübner | cfbcdad | 2017-02-18 19:46:27 +0100 | [diff] [blame] | 385 | void rkcommon_rc4_encode_spl(void *buf, unsigned int offset, unsigned int size) |
| 386 | { |
| 387 | unsigned int remaining = size; |
| 388 | |
| 389 | while (remaining > 0) { |
| 390 | int step = (remaining > RK_BLK_SIZE) ? RK_BLK_SIZE : remaining; |
| 391 | |
| 392 | rc4_encode(buf + offset, step, rc4_key); |
| 393 | offset += RK_BLK_SIZE; |
| 394 | remaining -= step; |
| 395 | } |
| 396 | } |
Philipp Tomsich | 111bcc4 | 2017-03-15 12:08:43 +0100 | [diff] [blame] | 397 | |
Philipp Tomsich | 366aad4 | 2017-04-17 17:48:01 +0200 | [diff] [blame] | 398 | int rkcommon_vrec_header(struct image_tool_params *params, |
Jeffy Chen | eea6cd8 | 2019-12-27 11:24:41 +0800 | [diff] [blame] | 399 | struct image_type_params *tparams) |
Philipp Tomsich | 111bcc4 | 2017-03-15 12:08:43 +0100 | [diff] [blame] | 400 | { |
| 401 | /* |
| 402 | * The SPL image looks as follows: |
| 403 | * |
| 404 | * 0x0 header0 (see rkcommon.c) |
| 405 | * 0x800 spl_name ('RK30', ..., 'RK33') |
Philipp Tomsich | ea3729e | 2017-04-17 17:48:02 +0200 | [diff] [blame] | 406 | * (start of the payload for AArch64 payloads: we expect the |
| 407 | * first 4 bytes to be available for overwriting with our |
| 408 | * spl_name) |
Philipp Tomsich | 111bcc4 | 2017-03-15 12:08:43 +0100 | [diff] [blame] | 409 | * 0x804 first instruction to be executed |
Philipp Tomsich | ea3729e | 2017-04-17 17:48:02 +0200 | [diff] [blame] | 410 | * (start of the image/payload for 32bit payloads) |
Philipp Tomsich | 111bcc4 | 2017-03-15 12:08:43 +0100 | [diff] [blame] | 411 | * |
Philipp Tomsich | ea3729e | 2017-04-17 17:48:02 +0200 | [diff] [blame] | 412 | * For AArch64 (ARMv8) payloads, natural alignment (8-bytes) is |
| 413 | * required for its sections (so the image we receive needs to |
| 414 | * have the first 4 bytes reserved for the spl_name). Reserving |
| 415 | * these 4 bytes is done using the BOOT0_HOOK infrastructure. |
Philipp Tomsich | 111bcc4 | 2017-03-15 12:08:43 +0100 | [diff] [blame] | 416 | * |
Philipp Tomsich | 95d363c | 2017-10-10 16:21:18 +0200 | [diff] [blame] | 417 | * The header is always at 0x800 (as we now use a payload |
| 418 | * prepadded using the boot0 hook for all targets): the first |
| 419 | * 4 bytes of these images can safely be overwritten using the |
| 420 | * boot magic. |
Philipp Tomsich | 111bcc4 | 2017-03-15 12:08:43 +0100 | [diff] [blame] | 421 | */ |
Philipp Tomsich | 95d363c | 2017-10-10 16:21:18 +0200 | [diff] [blame] | 422 | tparams->header_size = RK_SPL_HDR_START; |
Philipp Tomsich | 111bcc4 | 2017-03-15 12:08:43 +0100 | [diff] [blame] | 423 | |
| 424 | /* Allocate, clear and install the header */ |
| 425 | tparams->hdr = malloc(tparams->header_size); |
Jeffy Chen | eea6cd8 | 2019-12-27 11:24:41 +0800 | [diff] [blame] | 426 | if (!tparams->hdr) { |
| 427 | fprintf(stderr, "%s: Can't alloc header: %s\n", |
| 428 | params->cmdname, strerror(errno)); |
| 429 | exit(EXIT_FAILURE); |
| 430 | } |
Philipp Tomsich | 111bcc4 | 2017-03-15 12:08:43 +0100 | [diff] [blame] | 431 | memset(tparams->hdr, 0, tparams->header_size); |
Philipp Tomsich | 366aad4 | 2017-04-17 17:48:01 +0200 | [diff] [blame] | 432 | |
| 433 | /* |
Jeffy Chen | eea6cd8 | 2019-12-27 11:24:41 +0800 | [diff] [blame] | 434 | * We need to store the original file-size (i.e. before padding), as |
| 435 | * imagetool does not set this during its adjustment of file_size. |
Philipp Tomsich | 366aad4 | 2017-04-17 17:48:01 +0200 | [diff] [blame] | 436 | */ |
Jeffy Chen | eea6cd8 | 2019-12-27 11:24:41 +0800 | [diff] [blame] | 437 | params->orig_file_size = tparams->header_size + |
| 438 | spl_params.init_size + spl_params.boot_size; |
Philipp Tomsich | 366aad4 | 2017-04-17 17:48:01 +0200 | [diff] [blame] | 439 | |
Jeffy Chen | eea6cd8 | 2019-12-27 11:24:41 +0800 | [diff] [blame] | 440 | params->file_size = ROUND(params->orig_file_size, RK_SIZE_ALIGN); |
Philipp Tomsich | 366aad4 | 2017-04-17 17:48:01 +0200 | [diff] [blame] | 441 | |
Jeffy Chen | eea6cd8 | 2019-12-27 11:24:41 +0800 | [diff] [blame] | 442 | /* Ignoring pad len, since we are using our own copy_image() */ |
| 443 | return 0; |
| 444 | } |
| 445 | |
| 446 | static int pad_file(struct image_tool_params *params, int ifd, int pad) |
| 447 | { |
| 448 | uint8_t zeros[4096]; |
| 449 | |
| 450 | memset(zeros, 0, sizeof(zeros)); |
| 451 | |
| 452 | while (pad > 0) { |
| 453 | int todo = sizeof(zeros); |
| 454 | |
| 455 | if (todo > pad) |
| 456 | todo = pad; |
| 457 | if (write(ifd, (char *)&zeros, todo) != todo) { |
| 458 | fprintf(stderr, "%s: Write error on %s: %s\n", |
| 459 | params->cmdname, params->imagefile, |
| 460 | strerror(errno)); |
| 461 | return -1; |
| 462 | } |
| 463 | pad -= todo; |
| 464 | } |
| 465 | |
| 466 | return 0; |
| 467 | } |
| 468 | |
| 469 | static int copy_file(struct image_tool_params *params, int ifd, |
| 470 | const char *file, int padded_size) |
| 471 | { |
| 472 | int dfd; |
| 473 | struct stat sbuf; |
| 474 | unsigned char *ptr; |
| 475 | int size; |
| 476 | |
| 477 | if (params->vflag) |
| 478 | fprintf(stderr, "Adding Image %s\n", file); |
| 479 | |
| 480 | dfd = open(file, O_RDONLY | O_BINARY); |
| 481 | if (dfd < 0) { |
| 482 | fprintf(stderr, "%s: Can't open %s: %s\n", |
| 483 | params->cmdname, file, strerror(errno)); |
| 484 | return -1; |
| 485 | } |
| 486 | |
| 487 | if (fstat(dfd, &sbuf) < 0) { |
| 488 | fprintf(stderr, "%s: Can't stat %s: %s\n", |
| 489 | params->cmdname, file, strerror(errno)); |
| 490 | goto err_close; |
| 491 | } |
| 492 | |
| 493 | if (params->vflag) |
| 494 | fprintf(stderr, "Size %u(pad to %u)\n", |
| 495 | (int)sbuf.st_size, padded_size); |
| 496 | |
| 497 | ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0); |
| 498 | if (ptr == MAP_FAILED) { |
| 499 | fprintf(stderr, "%s: Can't read %s: %s\n", |
| 500 | params->cmdname, file, strerror(errno)); |
| 501 | goto err_munmap; |
| 502 | } |
| 503 | |
| 504 | size = sbuf.st_size; |
| 505 | if (write(ifd, ptr, size) != size) { |
| 506 | fprintf(stderr, "%s: Write error on %s: %s\n", |
| 507 | params->cmdname, params->imagefile, strerror(errno)); |
| 508 | goto err_munmap; |
| 509 | } |
| 510 | |
| 511 | munmap((void *)ptr, sbuf.st_size); |
| 512 | close(dfd); |
| 513 | return pad_file(params, ifd, padded_size - size); |
| 514 | |
| 515 | err_munmap: |
| 516 | munmap((void *)ptr, sbuf.st_size); |
| 517 | err_close: |
| 518 | close(dfd); |
| 519 | return -1; |
| 520 | } |
| 521 | |
| 522 | int rockchip_copy_image(int ifd, struct image_tool_params *params) |
| 523 | { |
| 524 | int ret; |
| 525 | |
| 526 | ret = copy_file(params, ifd, spl_params.init_file, |
| 527 | spl_params.init_size); |
| 528 | if (ret) |
| 529 | return ret; |
| 530 | |
| 531 | if (spl_params.boot_file) { |
| 532 | ret = copy_file(params, ifd, spl_params.boot_file, |
| 533 | spl_params.boot_size); |
| 534 | if (ret) |
| 535 | return ret; |
| 536 | } |
| 537 | |
| 538 | return pad_file(params, ifd, |
| 539 | params->file_size - params->orig_file_size); |
Philipp Tomsich | 111bcc4 | 2017-03-15 12:08:43 +0100 | [diff] [blame] | 540 | } |