Skip to content

Commit 145e317

Browse files
committed
uboot-rockchip: workaround for buggy rk3576
1 parent 972418b commit 145e317

8 files changed

Lines changed: 870 additions & 0 deletions
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
From 6b152999908b526c73b2ce91ecf444bae892796e Mon Sep 17 00:00:00 2001
2+
From: Jonas Karlman <jonas@kwiboo.se>
3+
Date: Wed, 29 Jan 2025 22:36:27 +0000
4+
Subject: [PATCH 1/7] rockchip: mkimage: Split size_and_off and size_and_nimage
5+
6+
Split 32-bit size_and_off and size_and_nimage fields of the v2 image
7+
format header into their own 16-bit size, offset and num_images fields.
8+
9+
Set num_images based on number of images passed by the datafile
10+
parameter and size based on the offset to the hash field to fix using a
11+
single init data file and no boot data file for the v2 image format.
12+
13+
Signed-off-by: Jonas Karlman <jonas@kwiboo.se>
14+
---
15+
tools/rkcommon.c | 44 ++++++++++++++++++++++++--------------------
16+
1 file changed, 24 insertions(+), 20 deletions(-)
17+
18+
--- a/tools/rkcommon.c
19+
+++ b/tools/rkcommon.c
20+
@@ -34,15 +34,16 @@ enum hash_type {
21+
/**
22+
* struct image_entry
23+
*
24+
- * @size_and_off: [31:16]image size;[15:0]image offset
25+
- * @address: default as 0xFFFFFFFF
26+
+ * @offset: image offset (unit as 512 byte blocks)
27+
+ * @size: image size (unit as 512 byte blocks)
28+
+ * @address: load address (default as 0xFFFFFFFF)
29+
* @flag: no use
30+
* @counter: no use
31+
* @hash: hash of image
32+
- *
33+
*/
34+
struct image_entry {
35+
- uint32_t size_and_off;
36+
+ uint16_t offset;
37+
+ uint16_t size;
38+
uint32_t address;
39+
uint32_t flag;
40+
uint32_t counter;
41+
@@ -56,16 +57,17 @@ struct image_entry {
42+
* This is stored at SD card block 64 (where each block is 512 bytes)
43+
*
44+
* @magic: Magic (must be RK_MAGIC_V2)
45+
- * @size_and_nimage: [31:16]number of images;[15:0]
46+
- * offset to hash field of header(unit as 4Byte)
47+
- * @boot_flag: [3:0]hash type(0:none,1:sha256,2:sha512)
48+
- * @signature: hash or signature for header info
49+
- *
50+
+ * @size: offset to hash field of header (unit as 4 bytes)
51+
+ * @num_images: number of images
52+
+ * @boot_flag: [3:0] hash type (0:none, 1:sha256, 2:sha512)
53+
+ * @images: images
54+
+ * @hash: hash or signature for header info
55+
*/
56+
struct header0_info_v2 {
57+
uint32_t magic;
58+
uint8_t reserved[4];
59+
- uint32_t size_and_nimage;
60+
+ uint16_t size;
61+
+ uint16_t num_images;
62+
uint32_t boot_flag;
63+
uint8_t reserved1[104];
64+
struct image_entry images[4];
65+
@@ -350,17 +352,18 @@ static void rkcommon_set_header0_v2(void
66+
printf("Image Type: Rockchip %s boot image\n",
67+
rkcommon_get_spl_hdr(params));
68+
memset(buf, '\0', RK_INIT_OFFSET * RK_BLK_SIZE);
69+
- hdr->magic = cpu_to_le32(RK_MAGIC_V2);
70+
- hdr->size_and_nimage = cpu_to_le32((2 << 16) + 384);
71+
+ hdr->magic = cpu_to_le32(RK_MAGIC_V2);
72+
hdr->boot_flag = cpu_to_le32(HASH_SHA256);
73+
sector_offset = 4;
74+
image_size_array[0] = spl_params.init_size;
75+
image_size_array[1] = spl_params.boot_size;
76+
77+
for (i = 0; i < 2; i++) {
78+
+ if (!image_size_array[i])
79+
+ break;
80+
image_sector_count = image_size_array[i] / RK_BLK_SIZE;
81+
- hdr->images[i].size_and_off = cpu_to_le32((image_sector_count
82+
- << 16) + sector_offset);
83+
+ hdr->images[i].offset = cpu_to_le16(sector_offset);
84+
+ hdr->images[i].size = cpu_to_le16(image_sector_count);
85+
hdr->images[i].address = 0xFFFFFFFF;
86+
hdr->images[i].counter = cpu_to_le32(i + 1);
87+
image_ptr = buf + sector_offset * RK_BLK_SIZE;
88+
@@ -369,6 +372,8 @@ static void rkcommon_set_header0_v2(void
89+
sector_offset = sector_offset + image_sector_count;
90+
}
91+
92+
+ hdr->num_images = cpu_to_le16(i);
93+
+ hdr->size = cpu_to_le16(offsetof(typeof(*hdr), hash) / sizeof(uint32_t));
94+
do_sha256_hash(buf, (void *)hdr->hash - buf, hdr->hash);
95+
}
96+
97+
@@ -515,10 +520,8 @@ void rkcommon_print_header(const void *b
98+
return;
99+
}
100+
101+
- init_size = header0_v2.images[0].size_and_off >> 16;
102+
- init_size = init_size * RK_BLK_SIZE;
103+
- boot_size = header0_v2.images[1].size_and_off >> 16;
104+
- boot_size = boot_size * RK_BLK_SIZE;
105+
+ init_size = le16_to_cpu(header0_v2.images[0].size) * RK_BLK_SIZE;
106+
+ boot_size = le16_to_cpu(header0_v2.images[1].size) * RK_BLK_SIZE;
107+
} else {
108+
ret = rkcommon_parse_header(buf, &header0, &spl_info);
109+
110+
@@ -532,8 +535,9 @@ void rkcommon_print_header(const void *b
111+
}
112+
113+
image_type = ret;
114+
- init_size = header0.init_size * RK_BLK_SIZE;
115+
- boot_size = header0.init_boot_size * RK_BLK_SIZE - init_size;
116+
+ init_size = le16_to_cpu(header0.init_size) * RK_BLK_SIZE;
117+
+ boot_size = le16_to_cpu(header0.init_boot_size) * RK_BLK_SIZE -
118+
+ init_size;
119+
120+
printf("Image Type: Rockchip %s (%s) boot image\n",
121+
spl_info->spl_hdr,
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
From beea345ad405bd236d15d53da1256eaa95014c7b Mon Sep 17 00:00:00 2001
2+
From: Jonas Karlman <jonas@kwiboo.se>
3+
Date: Wed, 29 Jan 2025 22:36:28 +0000
4+
Subject: [PATCH 2/7] rockchip: mkimage: Print image information for all
5+
embedded images
6+
7+
The v2 image format can embed up to 4 data files compared to the two
8+
init and boot data files using the older image format.
9+
10+
Add support for displaying more of the image header information that
11+
exists in the v2 image format, e.g. image load address and flag.
12+
13+
Example for v2 image format:
14+
15+
> tools/mkimage -l rk3576_idblock_v1.09.107.img
16+
Rockchip Boot Image (v2)
17+
Image 1: 4096 @ 0x1000
18+
- Load address: 0x3ffc0000
19+
Image 2: 77824 @ 0x2000
20+
- Load address: 0x3ff81000
21+
Image 3: 262144 @ 0x15000
22+
23+
Example for older image format:
24+
25+
> tools/mkimage -l u-boot-rockchip.bin
26+
Rockchip RK32 (SD/MMC) Boot Image
27+
Init Data: 20480 @ 0x800
28+
Boot Data: 112640 @ 0x5800
29+
30+
Signed-off-by: Jonas Karlman <jonas@kwiboo.se>
31+
---
32+
tools/rkcommon.c | 41 +++++++++++++++++++++++++++++++----------
33+
1 file changed, 31 insertions(+), 10 deletions(-)
34+
35+
--- a/tools/rkcommon.c
36+
+++ b/tools/rkcommon.c
37+
@@ -349,8 +349,6 @@ static void rkcommon_set_header0_v2(void
38+
uint8_t *image_ptr = NULL;
39+
int i;
40+
41+
- printf("Image Type: Rockchip %s boot image\n",
42+
- rkcommon_get_spl_hdr(params));
43+
memset(buf, '\0', RK_INIT_OFFSET * RK_BLK_SIZE);
44+
hdr->magic = cpu_to_le32(RK_MAGIC_V2);
45+
hdr->boot_flag = cpu_to_le32(HASH_SHA256);
46+
@@ -504,6 +502,29 @@ int rkcommon_verify_header(unsigned char
47+
return -ENOENT;
48+
}
49+
50+
+static void rkcommon_print_header_v2(const struct header0_info_v2 *hdr)
51+
+{
52+
+ uint32_t val;
53+
+ int i;
54+
+
55+
+ printf("Rockchip Boot Image (v2)\n");
56+
+
57+
+ for (i = 0; i < le16_to_cpu(hdr->num_images); i++) {
58+
+ printf("Image %u: %u @ 0x%x\n",
59+
+ le32_to_cpu(hdr->images[i].counter),
60+
+ le16_to_cpu(hdr->images[i].size) * RK_BLK_SIZE,
61+
+ le16_to_cpu(hdr->images[i].offset) * RK_BLK_SIZE);
62+
+
63+
+ val = le32_to_cpu(hdr->images[i].address);
64+
+ if (val != 0xFFFFFFFF)
65+
+ printf("- Load address: 0x%x\n", val);
66+
+
67+
+ val = le32_to_cpu(hdr->images[i].flag);
68+
+ if (val)
69+
+ printf("- Flag: 0x%x\n", val);
70+
+ }
71+
+}
72+
+
73+
void rkcommon_print_header(const void *buf, struct image_tool_params *params)
74+
{
75+
struct header0_info header0;
76+
@@ -520,8 +541,7 @@ void rkcommon_print_header(const void *b
77+
return;
78+
}
79+
80+
- init_size = le16_to_cpu(header0_v2.images[0].size) * RK_BLK_SIZE;
81+
- boot_size = le16_to_cpu(header0_v2.images[1].size) * RK_BLK_SIZE;
82+
+ rkcommon_print_header_v2(&header0_v2);
83+
} else {
84+
ret = rkcommon_parse_header(buf, &header0, &spl_info);
85+
86+
@@ -539,15 +559,16 @@ void rkcommon_print_header(const void *b
87+
boot_size = le16_to_cpu(header0.init_boot_size) * RK_BLK_SIZE -
88+
init_size;
89+
90+
- printf("Image Type: Rockchip %s (%s) boot image\n",
91+
- spl_info->spl_hdr,
92+
+ printf("Rockchip %s (%s) Boot Image\n", spl_info->spl_hdr,
93+
(image_type == IH_TYPE_RKSD) ? "SD/MMC" : "SPI");
94+
- }
95+
96+
- printf("Init Data Size: %d bytes\n", init_size);
97+
+ printf("Init Data: %d @ 0x%x\n", init_size,
98+
+ le16_to_cpu(header0.init_offset) * RK_BLK_SIZE);
99+
100+
- if (boot_size != RK_MAX_BOOT_SIZE)
101+
- printf("Boot Data Size: %d bytes\n", boot_size);
102+
+ if (boot_size != RK_MAX_BOOT_SIZE)
103+
+ printf("Boot Data: %d @ 0x%x\n", boot_size, init_size +
104+
+ le16_to_cpu(header0.init_offset) * RK_BLK_SIZE);
105+
+ }
106+
}
107+
108+
void rkcommon_rc4_encode_spl(void *buf, unsigned int offset, unsigned int size)
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
From cad4d0d7d04f71e9b8cc7704254abe74e7b17b50 Mon Sep 17 00:00:00 2001
2+
From: Jonas Karlman <jonas@kwiboo.se>
3+
Date: Wed, 29 Jan 2025 22:36:29 +0000
4+
Subject: [PATCH 3/7] rockchip: mkimage: Print boot0 and boot1 parameters
5+
6+
The v2 image format embeds boot0 and boot1 parameters, the vendor tool
7+
boot_merger may write these parameters based on the rkboot miniall.ini
8+
files.
9+
10+
E.g. a RK3576 boot image may contain a boot1 parameter that signals
11+
BootROM or vendor blobs to use 1 GHz instead of the regular 24 MHz rate
12+
for the high precision timer.
13+
14+
Add support for printing boot0 and boot1 parameters, e.g.:
15+
16+
> tools/mkimage -l rk3576_idblock_v1.09.107.img
17+
Rockchip Boot Image (v2)
18+
Boot1 2: 0x100
19+
Image 1: 4096 @ 0x1000
20+
- Load address: 0x3ffc0000
21+
Image 2: 77824 @ 0x2000
22+
- Load address: 0x3ff81000
23+
Image 3: 262144 @ 0x15000
24+
25+
Signed-off-by: Jonas Karlman <jonas@kwiboo.se>
26+
---
27+
tools/rkcommon.c | 18 +++++++++++++++++-
28+
1 file changed, 17 insertions(+), 1 deletion(-)
29+
30+
--- a/tools/rkcommon.c
31+
+++ b/tools/rkcommon.c
32+
@@ -62,6 +62,8 @@ struct image_entry {
33+
* @boot_flag: [3:0] hash type (0:none, 1:sha256, 2:sha512)
34+
* @images: images
35+
* @hash: hash or signature for header info
36+
+ *
37+
+ * Other fields are not used by U-Boot
38+
*/
39+
struct header0_info_v2 {
40+
uint32_t magic;
41+
@@ -69,7 +71,9 @@ struct header0_info_v2 {
42+
uint16_t size;
43+
uint16_t num_images;
44+
uint32_t boot_flag;
45+
- uint8_t reserved1[104];
46+
+ uint8_t reserved1[32];
47+
+ uint32_t boot0_param[10];
48+
+ uint32_t boot1_param[8];
49+
struct image_entry images[4];
50+
uint8_t reserved2[1064];
51+
uint8_t hash[512];
52+
@@ -509,6 +513,18 @@ static void rkcommon_print_header_v2(con
53+
54+
printf("Rockchip Boot Image (v2)\n");
55+
56+
+ for (i = 0; i < ARRAY_SIZE(hdr->boot0_param); i++) {
57+
+ val = le32_to_cpu(hdr->boot0_param[i]);
58+
+ if (val)
59+
+ printf("Boot0 %d: 0x%x\n", i, val);
60+
+ }
61+
+
62+
+ for (i = 0; i < ARRAY_SIZE(hdr->boot1_param); i++) {
63+
+ val = le32_to_cpu(hdr->boot1_param[i]);
64+
+ if (val)
65+
+ printf("Boot1 %d: 0x%x\n", i, val);
66+
+ }
67+
+
68+
for (i = 0; i < le16_to_cpu(hdr->num_images); i++) {
69+
printf("Image %u: %u @ 0x%x\n",
70+
le32_to_cpu(hdr->images[i].counter),

0 commit comments

Comments
 (0)