Commit 2fb371ff6483227f9be74cbce17c088bec96b411

Authored by Philipp Tomsich
Committed by Simon Glass
1 parent 37ffae7c5b

rockchip: mkimage: add support for verify_header/print_header

The rockchip image generation was previously missing the ability to
verify the generated header (and dump the image-type) without having
to resort to hexdump or od. Experience in our testing has showed it
to be very easy to get the rkspi and rksd images mixed up and the
lab... so we add the necessary support to have dumpimage tell us
what image type we're dealing with.

This change set adds the verify_header and print_header capability
to the rksd/rkspi image drivers (through shared code in rkcommon).

As of now, we only support images fully that are not RC4-encoded for
the SPL payload (i.e. header1 and payload). For RC4-encoded payloads,
the outer header (header0) is checked, but no detection of whether
this is a SD/MMC or SPI formatted payload takes place.

The output of dumpsys now prints the image type (spl_hdr), whether it
is a SD/MMC or SPI image, and the (padded) size of the image:
  $ ./tools/dumpimage -l ./spl.img
  Image Type:   Rockchip RK33 (SD/MMC) boot image
                               ^^^^^^ SD/MMC vs. SPI indication
                         ^^^^ spl_hdr indicated by the image
  Data Size:    79872 bytes

Signed-off-by: Philipp Tomsich <philipp.tomsich@theobroma-systems.com>
Acked-by: Simon Glass <sjg@chromium.org>

Showing 4 changed files with 146 additions and 42 deletions Side-by-side Diff

... ... @@ -2,6 +2,8 @@
2 2 * (C) Copyright 2015 Google, Inc
3 3 * Written by Simon Glass <sjg@chromium.org>
4 4 *
  5 + * (C) 2017 Theobroma Systems Design und Consulting GmbH
  6 + *
5 7 * SPDX-License-Identifier: GPL-2.0+
6 8 *
7 9 * Helper functions for Rockchip images
... ... @@ -201,7 +203,7 @@
201 203  
202 204 rkcommon_set_header0(buf, file_size, params);
203 205  
204   - /* Set up the SPL name */
  206 + /* Set up the SPL name (i.e. copy spl_hdr over) */
205 207 memcpy(&hdr->magic, rkcommon_get_spl_hdr(params), RK_SPL_HDR_SIZE);
206 208  
207 209 if (rkcommon_need_rc4_spl(params))
... ... @@ -209,6 +211,121 @@
209 211 params->file_size - RK_SPL_HDR_START);
210 212  
211 213 return 0;
  214 +}
  215 +
  216 +static inline unsigned rkcommon_offset_to_spi(unsigned offset)
  217 +{
  218 + /*
  219 + * While SD/MMC images use a flat addressing, SPI images are padded
  220 + * to use the first 2K of every 4K sector only.
  221 + */
  222 + return ((offset & ~0x7ff) << 1) + (offset & 0x7ff);
  223 +}
  224 +
  225 +static inline unsigned rkcommon_spi_to_offset(unsigned offset)
  226 +{
  227 + return ((offset & ~0x7ff) >> 1) + (offset & 0x7ff);
  228 +}
  229 +
  230 +static int rkcommon_parse_header(const void *buf, struct header0_info *header0,
  231 + struct spl_info **spl_info)
  232 +{
  233 + unsigned hdr1_offset;
  234 + struct header1_info *hdr1_sdmmc, *hdr1_spi;
  235 + int i;
  236 +
  237 + if (spl_info)
  238 + *spl_info = NULL;
  239 +
  240 + /*
  241 + * The first header (hdr0) is always RC4 encoded, so try to decrypt
  242 + * with the well-known key.
  243 + */
  244 + memcpy((void *)header0, buf, sizeof(struct header0_info));
  245 + rc4_encode((void *)header0, sizeof(struct header0_info), rc4_key);
  246 +
  247 + if (header0->signature != RK_SIGNATURE)
  248 + return -EPROTO;
  249 +
  250 + /* We don't support RC4 encoded image payloads here, yet... */
  251 + if (header0->disable_rc4 == 0)
  252 + return -ENOSYS;
  253 +
  254 + hdr1_offset = header0->init_offset * RK_BLK_SIZE;
  255 + hdr1_sdmmc = (struct header1_info *)(buf + hdr1_offset);
  256 + hdr1_spi = (struct header1_info *)(buf +
  257 + rkcommon_offset_to_spi(hdr1_offset));
  258 +
  259 + for (i = 0; i < ARRAY_SIZE(spl_infos); i++) {
  260 + if (!memcmp(&hdr1_sdmmc->magic, spl_infos[i].spl_hdr, 4)) {
  261 + if (spl_info)
  262 + *spl_info = &spl_infos[i];
  263 + return IH_TYPE_RKSD;
  264 + } else if (!memcmp(&hdr1_spi->magic, spl_infos[i].spl_hdr, 4)) {
  265 + if (spl_info)
  266 + *spl_info = &spl_infos[i];
  267 + return IH_TYPE_RKSPI;
  268 + }
  269 + }
  270 +
  271 + return -1;
  272 +}
  273 +
  274 +int rkcommon_verify_header(unsigned char *buf, int size,
  275 + struct image_tool_params *params)
  276 +{
  277 + struct header0_info header0;
  278 + struct spl_info *img_spl_info, *spl_info;
  279 + int ret;
  280 +
  281 + ret = rkcommon_parse_header(buf, &header0, &img_spl_info);
  282 +
  283 + /* If this is the (unimplemented) RC4 case, then rewrite the result */
  284 + if (ret == -ENOSYS)
  285 + return 0;
  286 +
  287 + if (ret < 0)
  288 + return ret;
  289 +
  290 + /*
  291 + * If no 'imagename' is specified via the commandline (e.g. if this is
  292 + * 'dumpimage -l' w/o any further constraints), we accept any spl_info.
  293 + */
  294 + if (params->imagename == NULL)
  295 + return 0;
  296 +
  297 + /* Match the 'imagename' against the 'spl_hdr' found */
  298 + spl_info = rkcommon_get_spl_info(params->imagename);
  299 + if (spl_info && img_spl_info)
  300 + return strcmp(spl_info->spl_hdr, img_spl_info->spl_hdr);
  301 +
  302 + return -ENOENT;
  303 +}
  304 +
  305 +void rkcommon_print_header(const void *buf)
  306 +{
  307 + struct header0_info header0;
  308 + struct spl_info *spl_info;
  309 + uint8_t image_type;
  310 + int ret;
  311 +
  312 + ret = rkcommon_parse_header(buf, &header0, &spl_info);
  313 +
  314 + /* If this is the (unimplemented) RC4 case, then fail silently */
  315 + if (ret == -ENOSYS)
  316 + return;
  317 +
  318 + if (ret < 0) {
  319 + fprintf(stderr, "Error: image verification failed\n");
  320 + return;
  321 + }
  322 +
  323 + image_type = ret;
  324 +
  325 + printf("Image Type: Rockchip %s (%s) boot image\n",
  326 + spl_info->spl_hdr,
  327 + (image_type == IH_TYPE_RKSD) ? "SD/MMC" : "SPI");
  328 + printf("Data Size: %d bytes\n", header0.init_size * RK_BLK_SIZE);
212 329 }
213 330  
214 331 void rkcommon_rc4_encode_spl(void *buf, unsigned int offset, unsigned int size)
... ... @@ -56,6 +56,25 @@
56 56 struct image_tool_params *params);
57 57  
58 58 /**
  59 + * rkcommon_verify_header() - verify the header for a Rockchip boot image
  60 + *
  61 + * @buf: Pointer to the image file
  62 + * @file_size: Size of entire bootable image file (incl. all padding)
  63 + * @return 0 if OK
  64 + */
  65 +int rkcommon_verify_header(unsigned char *buf, int size,
  66 + struct image_tool_params *params);
  67 +
  68 +/**
  69 + * rkcommon_print_header() - print the header for a Rockchip boot image
  70 + *
  71 + * This prints the header, spl_name and whether this is a SD/MMC or SPI image.
  72 + *
  73 + * @buf: Pointer to the image (can be a read-only file-mapping)
  74 + */
  75 +void rkcommon_print_header(const void *buf);
  76 +
  77 +/**
59 78 * rkcommon_need_rc4_spl() - check if rc4 encoded spl is required
60 79 *
61 80 * Some socs cannot disable the rc4-encryption of the spl binary.
... ... @@ -13,29 +13,17 @@
13 13 #include "mkimage.h"
14 14 #include "rkcommon.h"
15 15  
16   -static int rksd_verify_header(unsigned char *buf, int size,
17   - struct image_tool_params *params)
18   -{
19   - return 0;
20   -}
21   -
22   -static void rksd_print_header(const void *buf)
23   -{
24   -}
25   -
26 16 static void rksd_set_header(void *buf, struct stat *sbuf, int ifd,
27   - struct image_tool_params *params)
  17 + struct image_tool_params *params)
28 18 {
29 19 unsigned int size;
30 20 int ret;
31 21  
32   - printf("params->file_size %d\n", params->file_size);
33   - printf("params->orig_file_size %d\n", params->orig_file_size);
34   -
35 22 /*
36 23 * We need to calculate this using 'RK_SPL_HDR_START' and not using
37 24 * 'tparams->header_size', as the additional byte inserted when
38   - * 'is_boot0' is true counts towards the payload.
  25 + * 'is_boot0' is true counts towards the payload (and not towards the
  26 + * header).
39 27 */
40 28 size = params->file_size - RK_SPL_HDR_START;
41 29 ret = rkcommon_set_header(buf, size, params);
... ... @@ -46,11 +34,6 @@
46 34 }
47 35 }
48 36  
49   -static int rksd_extract_subimage(void *buf, struct image_tool_params *params)
50   -{
51   - return 0;
52   -}
53   -
54 37 static int rksd_check_image_type(uint8_t type)
55 38 {
56 39 if (type == IH_TYPE_RKSD)
57 40  
... ... @@ -78,10 +61,10 @@
78 61 0,
79 62 NULL,
80 63 rkcommon_check_params,
81   - rksd_verify_header,
82   - rksd_print_header,
  64 + rkcommon_verify_header,
  65 + rkcommon_print_header,
83 66 rksd_set_header,
84   - rksd_extract_subimage,
  67 + NULL,
85 68 rksd_check_image_type,
86 69 NULL,
87 70 rksd_vrec_header
... ... @@ -17,16 +17,6 @@
17 17 RKSPI_SECT_LEN = RK_BLK_SIZE * 4,
18 18 };
19 19  
20   -static int rkspi_verify_header(unsigned char *buf, int size,
21   - struct image_tool_params *params)
22   -{
23   - return 0;
24   -}
25   -
26   -static void rkspi_print_header(const void *buf)
27   -{
28   -}
29   -
30 20 static void rkspi_set_header(void *buf, struct stat *sbuf, int ifd,
31 21 struct image_tool_params *params)
32 22 {
... ... @@ -58,11 +48,6 @@
58 48 }
59 49 }
60 50  
61   -static int rkspi_extract_subimage(void *buf, struct image_tool_params *params)
62   -{
63   - return 0;
64   -}
65   -
66 51 static int rkspi_check_image_type(uint8_t type)
67 52 {
68 53 if (type == IH_TYPE_RKSPI)
69 54  
... ... @@ -112,10 +97,10 @@
112 97 0,
113 98 NULL,
114 99 rkcommon_check_params,
115   - rkspi_verify_header,
116   - rkspi_print_header,
  100 + rkcommon_verify_header,
  101 + rkcommon_print_header,
117 102 rkspi_set_header,
118   - rkspi_extract_subimage,
  103 + NULL,
119 104 rkspi_check_image_type,
120 105 NULL,
121 106 rkspi_vrec_header