1 /* 2 * (C) Copyright 2015 Google, Inc 3 * Written by Simon Glass <sjg@chromium.org> 4 * 5 * (C) 2017 Theobroma Systems Design und Consulting GmbH 6 * 7 * SPDX-License-Identifier: GPL-2.0+ 8 * 9 * Helper functions for Rockchip images 10 */ 11 12 #include "imagetool.h" 13 #include <image.h> 14 #include <rc4.h> 15 #include "mkimage.h" 16 #include "rkcommon.h" 17 18 #define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d)) 19 20 enum { 21 RK_SIGNATURE = 0x0ff0aa55, 22 }; 23 24 /** 25 * struct header0_info - header block for boot ROM 26 * 27 * This is stored at SD card block 64 (where each block is 512 bytes, or at 28 * the start of SPI flash. It is encoded with RC4. 29 * 30 * @signature: Signature (must be RKSD_SIGNATURE) 31 * @disable_rc4: 0 to use rc4 for boot image, 1 to use plain binary 32 * @init_offset: Offset in blocks of the SPL code from this header 33 * block. E.g. 4 means 2KB after the start of this header. 34 * Other fields are not used by U-Boot 35 */ 36 struct header0_info { 37 uint32_t signature; 38 uint8_t reserved[4]; 39 uint32_t disable_rc4; 40 uint16_t init_offset; 41 uint8_t reserved1[492]; 42 uint16_t init_size; 43 uint16_t init_boot_size; 44 uint8_t reserved2[2]; 45 }; 46 47 /** 48 * struct header1 info 49 */ 50 struct header1_info { 51 uint32_t magic; 52 }; 53 54 /** 55 * struct spl_info - spl info for each chip 56 * 57 * @imagename: Image name(passed by "mkimage -n") 58 * @spl_hdr: Boot ROM requires a 4-bytes spl header 59 * @spl_size: Spl size(include extra 4-bytes spl header) 60 * @spl_rc4: RC4 encode the SPL binary (same key as header) 61 */ 62 63 struct spl_info { 64 const char *imagename; 65 const char *spl_hdr; 66 const uint32_t spl_size; 67 const bool spl_rc4; 68 }; 69 70 static struct spl_info spl_infos[] = { 71 { "rk3036", "RK30", 0x1000, false }, 72 { "rk3066", "RK30", 0x8000, true, }, 73 { "rk3128", "RK31", 0x1800, false }, 74 { "rk3188", "RK31", 0x8000 - 0x800, true }, 75 { "rk322x", "RK32", 0x8000 - 0x1000, false }, 76 { "rk3288", "RK32", 0x8000, false }, 77 { "rk3308", "RK33", 0x40000 - 0x1000, false}, 78 { "rk3328", "RK32", 0x8000 - 0x1000, false }, 79 { "rk3368", "RK33", 0x8000 - 0x1000, false }, 80 { "rk3399", "RK33", 0x30000 - 0x2000, false }, 81 { "px30", "RK33", 0x2800, false }, 82 { "rv1108", "RK11", 0x1800, false }, 83 { "rk1808", "RK18", 0x200000 - 0x2000, false}, 84 }; 85 86 static unsigned char rc4_key[16] = { 87 124, 78, 3, 4, 85, 5, 9, 7, 88 45, 44, 123, 56, 23, 13, 23, 17 89 }; 90 91 static struct spl_info *rkcommon_get_spl_info(char *imagename) 92 { 93 int i; 94 95 if (!imagename) 96 return NULL; 97 98 for (i = 0; i < ARRAY_SIZE(spl_infos); i++) 99 if (!strncmp(imagename, spl_infos[i].imagename, 6)) 100 return spl_infos + i; 101 102 return NULL; 103 } 104 105 int rkcommon_check_params(struct image_tool_params *params) 106 { 107 int i; 108 109 if (rkcommon_get_spl_info(params->imagename) != NULL) 110 return EXIT_SUCCESS; 111 112 /* 113 * If this is a operation (list or extract), the don't require 114 * imagename to be set. 115 */ 116 if (params->lflag || params->iflag) 117 return EXIT_SUCCESS; 118 119 fprintf(stderr, "ERROR: imagename (%s) is not supported!\n", 120 params->imagename ? params->imagename : "NULL"); 121 122 fprintf(stderr, "Available imagename:"); 123 for (i = 0; i < ARRAY_SIZE(spl_infos); i++) 124 fprintf(stderr, "\t%s", spl_infos[i].imagename); 125 fprintf(stderr, "\n"); 126 127 return EXIT_FAILURE; 128 } 129 130 const char *rkcommon_get_spl_hdr(struct image_tool_params *params) 131 { 132 struct spl_info *info = rkcommon_get_spl_info(params->imagename); 133 134 /* 135 * info would not be NULL, because of we checked params before. 136 */ 137 return info->spl_hdr; 138 } 139 140 141 int rkcommon_get_spl_size(struct image_tool_params *params) 142 { 143 struct spl_info *info = rkcommon_get_spl_info(params->imagename); 144 145 /* 146 * info would not be NULL, because of we checked params before. 147 */ 148 return info->spl_size; 149 } 150 151 bool rkcommon_need_rc4_spl(struct image_tool_params *params) 152 { 153 struct spl_info *info = rkcommon_get_spl_info(params->imagename); 154 155 /* 156 * info would not be NULL, because of we checked params before. 157 */ 158 return info->spl_rc4; 159 } 160 161 static void rkcommon_set_header0(void *buf, uint file_size, uint max_size, 162 struct image_tool_params *params) 163 { 164 struct header0_info *hdr = buf; 165 166 memset(buf, '\0', RK_INIT_OFFSET * RK_BLK_SIZE); 167 hdr->signature = RK_SIGNATURE; 168 hdr->disable_rc4 = !rkcommon_need_rc4_spl(params); 169 hdr->init_offset = RK_INIT_OFFSET; 170 171 hdr->init_size = DIV_ROUND_UP(file_size, RK_BLK_SIZE); 172 /* 173 * The init_size has to be a multiple of 4 blocks (i.e. of 2K) 174 * or the BootROM will not boot the image. 175 * 176 * Note: To verify that this is not a legacy constraint, we 177 * rechecked this against the RK3399 BootROM. 178 */ 179 hdr->init_size = ROUND(hdr->init_size, 4); 180 /* 181 * init_boot_size needs to be set, as it is read by the BootROM 182 * to determine the size of the next-stage bootloader (e.g. U-Boot 183 * proper), when used with the back-to-bootrom functionality. 184 * 185 * see https://lists.denx.de/pipermail/u-boot/2017-May/293267.html 186 * for a more detailed explanation by Andy Yan 187 */ 188 hdr->init_boot_size = hdr->init_size + DIV_ROUND_UP(max_size, RK_BLK_SIZE); 189 hdr->init_boot_size = ROUND(hdr->init_boot_size, 4); 190 191 rc4_encode(buf, RK_BLK_SIZE, rc4_key); 192 } 193 194 int rkcommon_set_header(void *buf, uint file_size, uint max_size, 195 struct image_tool_params *params) 196 { 197 struct header1_info *hdr = buf + RK_SPL_HDR_START; 198 199 if (file_size > rkcommon_get_spl_size(params)) 200 return -ENOSPC; 201 202 rkcommon_set_header0(buf, file_size, max_size, params); 203 204 /* Set up the SPL name (i.e. copy spl_hdr over) */ 205 memcpy(&hdr->magic, rkcommon_get_spl_hdr(params), RK_SPL_HDR_SIZE); 206 207 if (rkcommon_need_rc4_spl(params)) 208 rkcommon_rc4_encode_spl(buf, RK_SPL_HDR_START, 209 params->file_size - RK_SPL_HDR_START); 210 211 return 0; 212 } 213 214 static inline unsigned rkcommon_offset_to_spi(unsigned offset) 215 { 216 /* 217 * While SD/MMC images use a flat addressing, SPI images are padded 218 * to use the first 2K of every 4K sector only. 219 */ 220 return ((offset & ~0x7ff) << 1) + (offset & 0x7ff); 221 } 222 223 static int rkcommon_parse_header(const void *buf, struct header0_info *header0, 224 struct spl_info **spl_info) 225 { 226 unsigned hdr1_offset; 227 struct header1_info *hdr1_sdmmc, *hdr1_spi; 228 int i; 229 230 if (spl_info) 231 *spl_info = NULL; 232 233 /* 234 * The first header (hdr0) is always RC4 encoded, so try to decrypt 235 * with the well-known key. 236 */ 237 memcpy((void *)header0, buf, sizeof(struct header0_info)); 238 rc4_encode((void *)header0, sizeof(struct header0_info), rc4_key); 239 240 if (header0->signature != RK_SIGNATURE) 241 return -EPROTO; 242 243 /* We don't support RC4 encoded image payloads here, yet... */ 244 if (header0->disable_rc4 == 0) 245 return -ENOSYS; 246 247 hdr1_offset = header0->init_offset * RK_BLK_SIZE; 248 hdr1_sdmmc = (struct header1_info *)(buf + hdr1_offset); 249 hdr1_spi = (struct header1_info *)(buf + 250 rkcommon_offset_to_spi(hdr1_offset)); 251 252 for (i = 0; i < ARRAY_SIZE(spl_infos); i++) { 253 if (!memcmp(&hdr1_sdmmc->magic, spl_infos[i].spl_hdr, 4)) { 254 if (spl_info) 255 *spl_info = &spl_infos[i]; 256 return IH_TYPE_RKSD; 257 } else if (!memcmp(&hdr1_spi->magic, spl_infos[i].spl_hdr, 4)) { 258 if (spl_info) 259 *spl_info = &spl_infos[i]; 260 return IH_TYPE_RKSPI; 261 } 262 } 263 264 return -1; 265 } 266 267 int rkcommon_verify_header(unsigned char *buf, int size, 268 struct image_tool_params *params) 269 { 270 struct header0_info header0; 271 struct spl_info *img_spl_info, *spl_info; 272 int ret; 273 274 ret = rkcommon_parse_header(buf, &header0, &img_spl_info); 275 276 /* If this is the (unimplemented) RC4 case, then rewrite the result */ 277 if (ret == -ENOSYS) 278 return 0; 279 280 if (ret < 0) 281 return ret; 282 283 /* 284 * If no 'imagename' is specified via the commandline (e.g. if this is 285 * 'dumpimage -l' w/o any further constraints), we accept any spl_info. 286 */ 287 if (params->imagename == NULL) 288 return 0; 289 290 /* Match the 'imagename' against the 'spl_hdr' found */ 291 spl_info = rkcommon_get_spl_info(params->imagename); 292 if (spl_info && img_spl_info) 293 return strcmp(spl_info->spl_hdr, img_spl_info->spl_hdr); 294 295 return -ENOENT; 296 } 297 298 void rkcommon_print_header(const void *buf) 299 { 300 struct header0_info header0; 301 struct spl_info *spl_info; 302 uint8_t image_type; 303 int ret; 304 305 ret = rkcommon_parse_header(buf, &header0, &spl_info); 306 307 /* If this is the (unimplemented) RC4 case, then fail silently */ 308 if (ret == -ENOSYS) 309 return; 310 311 if (ret < 0) { 312 fprintf(stderr, "Error: image verification failed\n"); 313 return; 314 } 315 316 image_type = ret; 317 318 printf("Image Type: Rockchip %s (%s) boot image\n", 319 spl_info->spl_hdr, 320 (image_type == IH_TYPE_RKSD) ? "SD/MMC" : "SPI"); 321 printf("Data Size: %d bytes\n", header0.init_size * RK_BLK_SIZE); 322 } 323 324 void rkcommon_rc4_encode_spl(void *buf, unsigned int offset, unsigned int size) 325 { 326 unsigned int remaining = size; 327 328 while (remaining > 0) { 329 int step = (remaining > RK_BLK_SIZE) ? RK_BLK_SIZE : remaining; 330 331 rc4_encode(buf + offset, step, rc4_key); 332 offset += RK_BLK_SIZE; 333 remaining -= step; 334 } 335 } 336 337 int rkcommon_vrec_header(struct image_tool_params *params, 338 struct image_type_params *tparams, 339 unsigned int alignment) 340 { 341 unsigned int unpadded_size; 342 unsigned int padded_size; 343 344 /* 345 * The SPL image looks as follows: 346 * 347 * 0x0 header0 (see rkcommon.c) 348 * 0x800 spl_name ('RK30', ..., 'RK33') 349 * (start of the payload for AArch64 payloads: we expect the 350 * first 4 bytes to be available for overwriting with our 351 * spl_name) 352 * 0x804 first instruction to be executed 353 * (start of the image/payload for 32bit payloads) 354 * 355 * For AArch64 (ARMv8) payloads, natural alignment (8-bytes) is 356 * required for its sections (so the image we receive needs to 357 * have the first 4 bytes reserved for the spl_name). Reserving 358 * these 4 bytes is done using the BOOT0_HOOK infrastructure. 359 * 360 * The header is always at 0x800 (as we now use a payload 361 * prepadded using the boot0 hook for all targets): the first 362 * 4 bytes of these images can safely be overwritten using the 363 * boot magic. 364 */ 365 tparams->header_size = RK_SPL_HDR_START; 366 367 /* Allocate, clear and install the header */ 368 tparams->hdr = malloc(tparams->header_size); 369 if (!tparams->hdr) 370 return -ENOMEM; 371 memset(tparams->hdr, 0, tparams->header_size); 372 373 /* 374 * If someone passed in 0 for the alignment, we'd better handle 375 * it correctly... 376 */ 377 if (!alignment) 378 alignment = 1; 379 380 unpadded_size = tparams->header_size + params->file_size; 381 padded_size = ROUND(unpadded_size, alignment); 382 383 return padded_size - unpadded_size; 384 } 385