Skip to content

Commit 86dcc42

Browse files
Yi LiuJosephChen2017
authored andcommitted
tools: mkimage: add 356X support
From 356X on,boot block takes new structure. It was defined as RK_HEADER_V2. Each function relative with it ends with 'v2'. Signed-off-by: Yi Liu <[email protected]> Change-Id: I97c832212c931023f314278c817ae816f56ac945
1 parent 7a00f0a commit 86dcc42

File tree

1 file changed

+189
-26
lines changed

1 file changed

+189
-26
lines changed

tools/rkcommon.c

Lines changed: 189 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,67 @@
1111

1212
#include "imagetool.h"
1313
#include <image.h>
14+
#include <u-boot/sha256.h>
1415
#include <rc4.h>
1516
#include "mkimage.h"
1617
#include "rkcommon.h"
1718

1819
enum {
1920
RK_MAGIC = 0x0ff0aa55,
21+
RK_MAGIC_V2 = 0x534E4B52,
2022
};
2123

2224
enum {
2325
RK_HEADER_V1 = 1,
26+
RK_HEADER_V2 = 2,
27+
};
28+
29+
enum hash_type {
30+
HASH_NONE = 0,
31+
HASH_SHA256 = 1,
32+
HASH_SHA512 = 2,
33+
};
34+
35+
/**
36+
* struct image_entry
37+
*
38+
* @size_and_off: [31:16]image size;[15:0]image offset
39+
* @address: default as 0xFFFFFFFF
40+
* @flag: no use
41+
* @counter: no use
42+
* @hash: hash of image
43+
*
44+
*/
45+
struct image_entry {
46+
uint32_t size_and_off;
47+
uint32_t address;
48+
uint32_t flag;
49+
uint32_t counter;
50+
uint8_t reserved[8];
51+
uint8_t hash[64];
52+
};
53+
54+
/**
55+
* struct header0_info_v2 - from rk35 on boot rom using the new header block
56+
*
57+
* This is stored at SD card block 64 (where each block is 512 bytes)
58+
*
59+
* @magic: Magic (must be RK_MAGIC_V2)
60+
* @size_and_nimage: [31:16]number of images;[15:0]
61+
* offset to hash field of header(unit as 4Byte)
62+
* @boot_flag: [3:0]hash type(0:none,1:sha256,2:sha512)
63+
* @signature: hash or signature for header info
64+
*
65+
*/
66+
struct header0_info_v2 {
67+
uint32_t magic;
68+
uint8_t reserved[4];
69+
uint32_t size_and_nimage;
70+
uint32_t boot_flag;
71+
uint8_t reserved1[104];
72+
struct image_entry images[4];
73+
uint8_t reserved2[1064];
74+
uint8_t hash[512];
2475
};
2576

2677
/**
@@ -85,6 +136,7 @@ static struct spl_info spl_infos[] = {
85136
{ "rv1108", "RK11", 0x1800, false, RK_HEADER_V1 },
86137
{ "rv1126", "110B", 0x10000 - 0x1000, false, RK_HEADER_V1 },
87138
{ "rk1808", "RK18", 0x200000 - 0x2000, false, RK_HEADER_V1 },
139+
{ "rk356x", "RK35", 0x10000 - 0x1000, false, RK_HEADER_V2 },
88140
};
89141

90142
/**
@@ -227,6 +279,26 @@ bool rkcommon_need_rc4_spl(struct image_tool_params *params)
227279
return info->spl_rc4;
228280
}
229281

282+
bool rkcommon_is_header_v2(struct image_tool_params *params)
283+
{
284+
struct spl_info *info = rkcommon_get_spl_info(params->imagename);
285+
286+
/*
287+
* info would not be NULL, because of we checked params before.
288+
*/
289+
return (info->header_ver == RK_HEADER_V2);
290+
}
291+
292+
static void do_sha256_hash(uint8_t *buf, uint32_t size, uint8_t *out)
293+
{
294+
sha256_context ctx;
295+
296+
sha256_starts(&ctx);
297+
sha256_update(&ctx, buf, size);
298+
sha256_finish(&ctx, out);
299+
}
300+
301+
230302
static void rkcommon_set_header0(void *buf, struct image_tool_params *params)
231303
{
232304
struct header0_info *hdr = buf;
@@ -255,26 +327,64 @@ static void rkcommon_set_header0(void *buf, struct image_tool_params *params)
255327
rc4_encode(buf, RK_BLK_SIZE, rc4_key);
256328
}
257329

330+
static void rkcommon_set_header0_v2(void *buf, struct image_tool_params *params)
331+
{
332+
struct header0_info_v2 *hdr = buf;
333+
uint32_t sector_offset, image_sector_count;
334+
uint32_t image_size_array[2];
335+
uint8_t *image_ptr = NULL;
336+
int i;
337+
338+
memset(buf, '\0', RK_INIT_OFFSET * RK_BLK_SIZE);
339+
hdr->magic = cpu_to_le32(RK_MAGIC_V2);
340+
hdr->size_and_nimage = cpu_to_le32((2 << 16) + 384);
341+
hdr->boot_flag = cpu_to_le32(HASH_SHA256);
342+
sector_offset = 4;
343+
image_size_array[0] = spl_params.init_size;
344+
image_size_array[1] = spl_params.boot_size;
345+
346+
for (i = 0; i < 2; i++) {
347+
image_sector_count = image_size_array[i] / RK_BLK_SIZE;
348+
hdr->images[i].size_and_off = cpu_to_le32((image_sector_count << 16) + sector_offset);
349+
hdr->images[i].address = 0xFFFFFFFF;
350+
hdr->images[i].counter = cpu_to_le32(i + 1);
351+
image_ptr = buf + sector_offset * RK_BLK_SIZE;
352+
do_sha256_hash(image_ptr, image_size_array[i], hdr->images[i].hash);
353+
sector_offset = sector_offset + image_sector_count;
354+
}
355+
356+
do_sha256_hash(buf, (void *)hdr->hash - buf, hdr->hash);
357+
}
358+
258359
void rkcommon_set_header(void *buf, struct stat *sbuf, int ifd,
259360
struct image_tool_params *params)
260361
{
261362
struct header1_info *hdr = buf + RK_SPL_HDR_START;
262363

263-
rkcommon_set_header0(buf, params);
264-
265-
/* Set up the SPL name (i.e. copy spl_hdr over) */
266-
if (memcmp(&hdr->magic, "RSAK", 4))
364+
if (rkcommon_is_header_v2(params)) {
365+
/* Set up the SPL name (i.e. copy spl_hdr over) */
267366
memcpy(&hdr->magic, rkcommon_get_spl_hdr(params), RK_SPL_HDR_SIZE);
367+
/* because of doing hash in the set_header0_v2
368+
* magic need to be changed first
369+
*/
370+
rkcommon_set_header0_v2(buf, params);
371+
} else {
372+
rkcommon_set_header0(buf, params);
268373

269-
if (rkcommon_need_rc4_spl(params))
270-
rkcommon_rc4_encode_spl(buf, RK_SPL_HDR_START,
271-
spl_params.init_size);
374+
/* Set up the SPL name (i.e. copy spl_hdr over) */
375+
if (memcmp(&hdr->magic, "RSAK", 4))
376+
memcpy(&hdr->magic, rkcommon_get_spl_hdr(params), RK_SPL_HDR_SIZE);
272377

273-
if (spl_params.boot_file) {
274378
if (rkcommon_need_rc4_spl(params))
275-
rkcommon_rc4_encode_spl(buf + RK_SPL_HDR_START,
276-
spl_params.init_size,
277-
spl_params.boot_size);
379+
rkcommon_rc4_encode_spl(buf, RK_SPL_HDR_START,
380+
spl_params.init_size);
381+
382+
if (spl_params.boot_file) {
383+
if (rkcommon_need_rc4_spl(params))
384+
rkcommon_rc4_encode_spl(buf + RK_SPL_HDR_START,
385+
spl_params.init_size,
386+
spl_params.boot_size);
387+
}
278388
}
279389
}
280390

@@ -331,6 +441,43 @@ static int rkcommon_parse_header(const void *buf, struct header0_info *header0,
331441
return -1;
332442
}
333443

444+
static int rkcommon_parse_header_v2(const void *buf,
445+
struct header0_info_v2 *header, struct spl_info **spl_info)
446+
{
447+
unsigned int hdr1_offset;
448+
struct header1_info *hdr1_sdmmc, *hdr1_spi;
449+
int i;
450+
451+
if (spl_info)
452+
*spl_info = NULL;
453+
454+
memcpy((void *)header, buf, sizeof(struct header0_info_v2));
455+
456+
if (le32_to_cpu(header->magic) != RK_MAGIC_V2)
457+
return -EPROTO;
458+
459+
hdr1_offset = ((le32_to_cpu(header->images[0].size_and_off)) & 0xFFFF) * RK_BLK_SIZE;
460+
hdr1_sdmmc = (struct header1_info *)(buf + hdr1_offset);
461+
hdr1_spi = (struct header1_info *)(buf +
462+
rkcommon_offset_to_spi(hdr1_offset));
463+
464+
for (i = 0; i < ARRAY_SIZE(spl_infos); i++) {
465+
if (!memcmp(&hdr1_sdmmc->magic, spl_infos[i].spl_hdr,
466+
RK_SPL_HDR_SIZE)) {
467+
if (spl_info)
468+
*spl_info = &spl_infos[i];
469+
return IH_TYPE_RKSD;
470+
} else if (!memcmp(&hdr1_spi->magic, spl_infos[i].spl_hdr,
471+
RK_SPL_HDR_SIZE)) {
472+
if (spl_info)
473+
*spl_info = &spl_infos[i];
474+
return IH_TYPE_RKSPI;
475+
}
476+
}
477+
478+
return -1;
479+
}
480+
334481
int rkcommon_verify_header(unsigned char *buf, int size,
335482
struct image_tool_params *params)
336483
{
@@ -365,29 +512,45 @@ int rkcommon_verify_header(unsigned char *buf, int size,
365512
void rkcommon_print_header(const void *buf)
366513
{
367514
struct header0_info header0;
515+
struct header0_info_v2 header0_v2;
368516
struct spl_info *spl_info;
369517
uint8_t image_type;
370-
int ret, boot_size;
518+
int ret, boot_size, init_size;
371519

372-
ret = rkcommon_parse_header(buf, &header0, &spl_info);
373-
374-
/* If this is the (unimplemented) RC4 case, then fail silently */
375-
if (ret == -ENOSYS)
376-
return;
520+
if ((*(uint32_t *)buf) == RK_MAGIC_V2) {
521+
ret = rkcommon_parse_header_v2(buf, &header0_v2, &spl_info);
377522

378-
if (ret < 0) {
379-
fprintf(stderr, "Error: image verification failed\n");
380-
return;
381-
}
523+
if (ret < 0) {
524+
fprintf(stderr, "Error: image verification failed\n");
525+
return;
526+
}
382527

383-
image_type = ret;
528+
image_type = ret;
529+
init_size = header0_v2.images[0].size_and_off >> 16;
530+
init_size = init_size * RK_BLK_SIZE;
531+
boot_size = header0_v2.images[1].size_and_off >> 16;
532+
boot_size = boot_size * RK_BLK_SIZE;
533+
} else {
534+
ret = rkcommon_parse_header(buf, &header0, &spl_info);
535+
536+
/* If this is the (unimplemented) RC4 case, then fail silently */
537+
if (ret == -ENOSYS)
538+
return;
539+
540+
if (ret < 0) {
541+
fprintf(stderr, "Error: image verification failed\n");
542+
return;
543+
}
384544

545+
image_type = ret;
546+
init_size = header0.init_size * RK_BLK_SIZE;
547+
boot_size = header0.init_boot_size * RK_BLK_SIZE - init_size;
548+
}
385549
printf("Image Type: Rockchip %s (%s) boot image\n",
386-
spl_info->spl_hdr,
387-
(image_type == IH_TYPE_RKSD) ? "SD/MMC" : "SPI");
388-
printf("Init Data Size: %d bytes\n", header0.init_size * RK_BLK_SIZE);
550+
spl_info->spl_hdr,
551+
(image_type == IH_TYPE_RKSD) ? "SD/MMC" : "SPI");
552+
printf("Init Data Size: %d bytes\n", init_size);
389553

390-
boot_size = (header0.init_boot_size - header0.init_size) * RK_BLK_SIZE;
391554
if (boot_size != RK_MAX_BOOT_SIZE)
392555
printf("Boot Data Size: %d bytes\n", boot_size);
393556
}

0 commit comments

Comments
 (0)