Commit eea6cd8d7c41df5f7118ac4fb1e9e375eb04c4eb

Authored by Jeffy Chen
Committed by Kever Yang
1 parent dcdea292d9

rockchip: mkimage: support packing optional second level boot-loader

Support packing optional second level boot-loader:

$ ./tools/mkimage -n rk3399 -T rksd -d \
  rk3399_ddr_800MHz_v1.24.bin:rk3399_miniloader_v1.19.bin out -v
Adding Image rk3399_ddr_800MHz_v1.24.bin
Size 116492(pad to 116736)
Adding Image rk3399_miniloader_v1.19.bin
Size 88060(pad to 88064)
Image Type:   Rockchip RK33 (SD/MMC) boot image
Init Data Size: 116736 bytes
Boot Data Size: 88064 bytes

Mainly parse init file and boot file from datafile option, copy them to
the image with 2KB alignment.

Signed-off-by: Jeffy Chen <jeffy.chen@rock-chips.com>
Reviewed-by: Kever Yang <kever.yang@rock-chips.com>

Showing 6 changed files with 231 additions and 115 deletions Side-by-side Diff

... ... @@ -253,6 +253,7 @@
253 253 int zynqmpbif_copy_image(int fd, struct image_tool_params *mparams);
254 254 int imx8image_copy_image(int fd, struct image_tool_params *mparams);
255 255 int imx8mimage_copy_image(int fd, struct image_tool_params *mparams);
  256 +int rockchip_copy_image(int fd, struct image_tool_params *mparams);
256 257  
257 258 #define ___cat(a, b) a ## b
258 259 #define __cat(a, b) ___cat(a, b)
... ... @@ -544,6 +544,14 @@
544 544 ret = imx8mimage_copy_image(ifd, &params);
545 545 if (ret)
546 546 return ret;
  547 + } else if ((params.type == IH_TYPE_RKSD) ||
  548 + (params.type == IH_TYPE_RKSPI)) {
  549 + /* Rockchip has special Image format */
  550 + int ret;
  551 +
  552 + ret = rockchip_copy_image(ifd, &params);
  553 + if (ret)
  554 + return ret;
547 555 } else {
548 556 copy_file(ifd, params.datafile, pad_len);
549 557 }
... ... @@ -14,8 +14,6 @@
14 14 #include "mkimage.h"
15 15 #include "rkcommon.h"
16 16  
17   -#define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
18   -
19 17 enum {
20 18 RK_SIGNATURE = 0x0ff0aa55,
21 19 };
... ... @@ -80,6 +78,24 @@
80 78 { "rv1108", "RK11", 0x1800, false },
81 79 };
82 80  
  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 +
83 99 static unsigned char rc4_key[16] = {
84 100 124, 78, 3, 4, 85, 5, 9, 7,
85 101 45, 44, 123, 56, 23, 13, 23, 17
86 102  
... ... @@ -99,13 +115,26 @@
99 115 return NULL;
100 116 }
101 117  
  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 +
102 134 int rkcommon_check_params(struct image_tool_params *params)
103 135 {
104 136 int i;
105 137  
106   - if (rkcommon_get_spl_info(params->imagename) != NULL)
107   - return EXIT_SUCCESS;
108   -
109 138 /*
110 139 * If this is a operation (list or extract), the don't require
111 140 * imagename to be set.
... ... @@ -113,6 +142,40 @@
113 142 if (params->lflag || params->iflag)
114 143 return EXIT_SUCCESS;
115 144  
  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:
116 179 fprintf(stderr, "ERROR: imagename (%s) is not supported!\n",
117 180 params->imagename ? params->imagename : "NULL");
118 181  
... ... @@ -155,8 +218,7 @@
155 218 return info->spl_rc4;
156 219 }
157 220  
158   -static void rkcommon_set_header0(void *buf, uint file_size,
159   - struct image_tool_params *params)
  221 +static void rkcommon_set_header0(void *buf, struct image_tool_params *params)
160 222 {
161 223 struct header0_info *hdr = buf;
162 224  
163 225  
164 226  
... ... @@ -164,17 +226,9 @@
164 226 hdr->signature = RK_SIGNATURE;
165 227 hdr->disable_rc4 = !rkcommon_need_rc4_spl(params);
166 228 hdr->init_offset = RK_INIT_OFFSET;
  229 + hdr->init_size = spl_params.init_size / RK_BLK_SIZE;
167 230  
168   - hdr->init_size = DIV_ROUND_UP(file_size, RK_BLK_SIZE);
169 231 /*
170   - * The init_size has to be a multiple of 4 blocks (i.e. of 2K)
171   - * or the BootROM will not boot the image.
172   - *
173   - * Note: To verify that this is not a legacy constraint, we
174   - * rechecked this against the RK3399 BootROM.
175   - */
176   - hdr->init_size = ROUND(hdr->init_size, 4);
177   - /*
178 232 * init_boot_size needs to be set, as it is read by the BootROM
179 233 * to determine the size of the next-stage bootloader (e.g. U-Boot
180 234 * proper), when used with the back-to-bootrom functionality.
181 235  
182 236  
183 237  
184 238  
185 239  
... ... @@ -182,29 +236,36 @@
182 236 * see https://lists.denx.de/pipermail/u-boot/2017-May/293267.html
183 237 * for a more detailed explanation by Andy Yan
184 238 */
185   - hdr->init_boot_size = hdr->init_size + RK_MAX_BOOT_SIZE / RK_BLK_SIZE;
  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;
186 245  
187 246 rc4_encode(buf, RK_BLK_SIZE, rc4_key);
188 247 }
189 248  
190   -int rkcommon_set_header(void *buf, uint file_size,
191   - struct image_tool_params *params)
  249 +void rkcommon_set_header(void *buf, struct stat *sbuf, int ifd,
  250 + struct image_tool_params *params)
192 251 {
193 252 struct header1_info *hdr = buf + RK_SPL_HDR_START;
194 253  
195   - if (file_size > rkcommon_get_spl_size(params))
196   - return -ENOSPC;
  254 + rkcommon_set_header0(buf, params);
197 255  
198   - rkcommon_set_header0(buf, file_size, params);
199   -
200 256 /* Set up the SPL name (i.e. copy spl_hdr over) */
201 257 memcpy(&hdr->magic, rkcommon_get_spl_hdr(params), RK_SPL_HDR_SIZE);
202 258  
203 259 if (rkcommon_need_rc4_spl(params))
204 260 rkcommon_rc4_encode_spl(buf, RK_SPL_HDR_START,
205   - params->file_size - RK_SPL_HDR_START);
  261 + spl_params.init_size);
206 262  
207   - return 0;
  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 + }
208 269 }
209 270  
210 271 static inline unsigned rkcommon_offset_to_spi(unsigned offset)
... ... @@ -296,7 +357,7 @@
296 357 struct header0_info header0;
297 358 struct spl_info *spl_info;
298 359 uint8_t image_type;
299   - int ret;
  360 + int ret, boot_size;
300 361  
301 362 ret = rkcommon_parse_header(buf, &header0, &spl_info);
302 363  
... ... @@ -314,7 +375,11 @@
314 375 printf("Image Type: Rockchip %s (%s) boot image\n",
315 376 spl_info->spl_hdr,
316 377 (image_type == IH_TYPE_RKSD) ? "SD/MMC" : "SPI");
317   - printf("Data Size: %d bytes\n", header0.init_size * RK_BLK_SIZE);
  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);
318 383 }
319 384  
320 385 void rkcommon_rc4_encode_spl(void *buf, unsigned int offset, unsigned int size)
321 386  
... ... @@ -331,12 +396,8 @@
331 396 }
332 397  
333 398 int rkcommon_vrec_header(struct image_tool_params *params,
334   - struct image_type_params *tparams,
335   - unsigned int alignment)
  399 + struct image_type_params *tparams)
336 400 {
337   - unsigned int unpadded_size;
338   - unsigned int padded_size;
339   -
340 401 /*
341 402 * The SPL image looks as follows:
342 403 *
343 404  
344 405  
345 406  
346 407  
... ... @@ -362,20 +423,119 @@
362 423  
363 424 /* Allocate, clear and install the header */
364 425 tparams->hdr = malloc(tparams->header_size);
365   - if (!tparams->hdr)
366   - return -ENOMEM;
  426 + if (!tparams->hdr) {
  427 + fprintf(stderr, "%s: Can't alloc header: %s\n",
  428 + params->cmdname, strerror(errno));
  429 + exit(EXIT_FAILURE);
  430 + }
367 431 memset(tparams->hdr, 0, tparams->header_size);
368 432  
369 433 /*
370   - * If someone passed in 0 for the alignment, we'd better handle
371   - * it correctly...
  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.
372 436 */
373   - if (!alignment)
374   - alignment = 1;
  437 + params->orig_file_size = tparams->header_size +
  438 + spl_params.init_size + spl_params.boot_size;
375 439  
376   - unpadded_size = tparams->header_size + params->file_size;
377   - padded_size = ROUND(unpadded_size, alignment);
  440 + params->file_size = ROUND(params->orig_file_size, RK_SIZE_ALIGN);
378 441  
379   - return padded_size - unpadded_size;
  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);
380 540 }
... ... @@ -9,13 +9,11 @@
9 9  
10 10 enum {
11 11 RK_BLK_SIZE = 512,
12   - RK_INIT_SIZE_ALIGN = 2048,
  12 + RK_SIZE_ALIGN = 2048,
13 13 RK_INIT_OFFSET = 4,
14 14 RK_MAX_BOOT_SIZE = 512 << 10,
15 15 RK_SPL_HDR_START = RK_INIT_OFFSET * RK_BLK_SIZE,
16 16 RK_SPL_HDR_SIZE = 4,
17   - RK_SPL_START = RK_SPL_HDR_START + RK_SPL_HDR_SIZE,
18   - RK_IMAGE_HEADER_LEN = RK_SPL_START,
19 17 };
20 18  
21 19 /**
22 20  
... ... @@ -49,11 +47,9 @@
49 47 * This sets up a 2KB header which can be interpreted by the Rockchip boot ROM.
50 48 *
51 49 * @buf: Pointer to header place (must be at least 2KB in size)
52   - * @file_size: Size of the file we want the boot ROM to load, in bytes
53   - * @return 0 if OK, -ENOSPC if too large
54 50 */
55   -int rkcommon_set_header(void *buf, uint file_size,
56   - struct image_tool_params *params);
  51 +void rkcommon_set_header(void *buf, struct stat *sbuf, int ifd,
  52 + struct image_tool_params *params);
57 53  
58 54 /**
59 55 * rkcommon_verify_header() - verify the header for a Rockchip boot image
60 56  
61 57  
... ... @@ -102,15 +98,11 @@
102 98 * @params: Pointer to the tool params structure
103 99 * @tparams: Pointer tot the image type structure (for setting
104 100 * the header and header_size)
105   - * @alignment: Alignment (a power of two) that the image should be
106   - * padded to (e.g. 512 if we want to align with SD/MMC
107   - * blocksizes or 2048 for the SPI format)
108 101 *
109   - * @return bytes of padding required/added (does not include the header_size)
  102 + * @return 0 (always)
110 103 */
111 104 int rkcommon_vrec_header(struct image_tool_params *params,
112   - struct image_type_params *tparams,
113   - unsigned int alignment);
  105 + struct image_type_params *tparams);
114 106  
115 107 #endif
... ... @@ -12,27 +12,6 @@
12 12 #include "mkimage.h"
13 13 #include "rkcommon.h"
14 14  
15   -static void rksd_set_header(void *buf, struct stat *sbuf, int ifd,
16   - struct image_tool_params *params)
17   -{
18   - unsigned int size;
19   - int ret;
20   -
21   - /*
22   - * We need to calculate this using 'RK_SPL_HDR_START' and not using
23   - * 'tparams->header_size', as the additional byte inserted when
24   - * 'is_boot0' is true counts towards the payload (and not towards the
25   - * header).
26   - */
27   - size = params->file_size - RK_SPL_HDR_START;
28   - ret = rkcommon_set_header(buf, size, params);
29   - if (ret) {
30   - /* TODO(sjg@chromium.org): This method should return an error */
31   - printf("Warning: SPL image is too large (size %#x) and will "
32   - "not boot\n", size);
33   - }
34   -}
35   -
36 15 static int rksd_check_image_type(uint8_t type)
37 16 {
38 17 if (type == IH_TYPE_RKSD)
... ... @@ -41,16 +20,6 @@
41 20 return EXIT_FAILURE;
42 21 }
43 22  
44   -static int rksd_vrec_header(struct image_tool_params *params,
45   - struct image_type_params *tparams)
46   -{
47   - /*
48   - * Pad to a 2KB alignment, as required for init_size by the ROM
49   - * (see https://lists.denx.de/pipermail/u-boot/2017-May/293268.html)
50   - */
51   - return rkcommon_vrec_header(params, tparams, RK_INIT_SIZE_ALIGN);
52   -}
53   -
54 23 /*
55 24 * rk_sd parameters
56 25 */
57 26  
... ... @@ -62,10 +31,10 @@
62 31 rkcommon_check_params,
63 32 rkcommon_verify_header,
64 33 rkcommon_print_header,
65   - rksd_set_header,
  34 + rkcommon_set_header,
66 35 NULL,
67 36 rksd_check_image_type,
68 37 NULL,
69   - rksd_vrec_header
  38 + rkcommon_vrec_header
70 39 );
... ... @@ -21,22 +21,20 @@
21 21 {
22 22 int sector;
23 23 unsigned int size;
24   - int ret;
25 24  
26 25 size = params->orig_file_size;
27   - ret = rkcommon_set_header(buf, size, params);
28   - debug("size %x\n", size);
29   - if (ret) {
30   - /* TODO(sjg@chromium.org): This method should return an error */
31   - printf("Warning: SPL image is too large (size %#x) and will "
32   - "not boot\n", size);
33   - }
34 26  
  27 + rkcommon_set_header(buf, sbuf, ifd, params);
  28 +
35 29 /*
36 30 * Spread the image out so we only use the first 2KB of each 4KB
37 31 * region. This is a feature of the SPI format required by the Rockchip
38 32 * boot ROM. Its rationale is unknown.
39 33 */
  34 + if (params->vflag)
  35 + fprintf(stderr, "Spreading spi image from %u to %u\n",
  36 + size, params->file_size);
  37 +
40 38 for (sector = size / RKSPI_SECT_LEN - 1; sector >= 0; sector--) {
41 39 debug("sector %u\n", sector);
42 40 memmove(buf + sector * RKSPI_SECT_LEN * 2,
43 41  
44 42  
45 43  
46 44  
... ... @@ -56,35 +54,23 @@
56 54 }
57 55  
58 56 /*
59   - * The SPI payload needs to be padded out to make space for odd half-sector
60   - * layout used in flash (i.e. only the first 2K of each 4K sector is used).
  57 + * The SPI payload needs to make space for odd half-sector layout used in flash
  58 + * (i.e. only the first 2K of each 4K sector is used).
61 59 */
62 60 static int rkspi_vrec_header(struct image_tool_params *params,
63 61 struct image_type_params *tparams)
64 62 {
65   - int padding = rkcommon_vrec_header(params, tparams, RK_INIT_SIZE_ALIGN);
66   - /*
67   - * The file size has not been adjusted at this point (our caller will
68   - * eventually add the header/padding to the file_size), so we need to
69   - * add up the header_size, file_size and padding ourselves.
70   - */
71   - int padded_size = tparams->header_size + params->file_size + padding;
  63 + rkcommon_vrec_header(params, tparams);
72 64  
73 65 /*
74   - * We need to store the original file-size (i.e. before padding), as
75   - * imagetool does not set this during its adjustment of file_size.
76   - */
77   - params->orig_file_size = padded_size;
78   -
79   - /*
80 66 * Converting to the SPI format (i.e. splitting each 4K page into two
81 67 * 2K subpages and then padding these 2K pages up to take a complete
82   - * 4K sector again) will will double the image size.
83   - *
84   - * Thus we return the padded_size as an additional padding requirement
85   - * (be sure to add this to the padding returned from the common code).
  68 + * 4K sector again) which will double the image size.
86 69 */
87   - return padded_size + padding;
  70 + params->file_size = ROUND(params->file_size, RKSPI_SECT_LEN) << 1;
  71 +
  72 + /* Ignoring pad len, since we are using our own copy_image() */
  73 + return 0;
88 74 }
89 75  
90 76 /*