1 From 20ebf03eab571b25e9f62b2764ab84932111dcd6 Mon Sep 17 00:00:00 2001
2 From: Weijie Gao <weijie.gao@mediatek.com>
3 Date: Tue, 2 Aug 2022 17:23:57 +0800
4 Subject: [PATCH 29/31] tools: mtk_image: split the code of generating NAND
7 The predefined NAND headers take too much spaces in the mtk_image.c.
8 Moving them into a new file can significantly improve the readability of
9 both mtk_image.c and the new mtk_nand_headers.c.
11 This is a preparation for adding more NAND headers.
13 Reviewed-by: Simon Glass <sjg@chromium.org>
14 Signed-off-by: Weijie Gao <weijie.gao@mediatek.com>
17 tools/mtk_image.c | 305 ++++++---------------------------------
18 tools/mtk_image.h | 25 ----
19 tools/mtk_nand_headers.c | 286 ++++++++++++++++++++++++++++++++++++
20 tools/mtk_nand_headers.h | 61 ++++++++
21 5 files changed, 389 insertions(+), 289 deletions(-)
22 create mode 100644 tools/mtk_nand_headers.c
23 create mode 100644 tools/mtk_nand_headers.h
27 @@ -148,6 +148,7 @@ dumpimage-mkimage-objs := aisimage.o \
31 + mtk_nand_headers.o \
35 --- a/tools/mtk_image.c
36 +++ b/tools/mtk_image.c
38 #include <u-boot/sha256.h>
39 #include "imagetool.h"
40 #include "mtk_image.h"
42 -/* NAND header for SPI-NAND with 2KB page + 64B spare */
43 -static const union nand_boot_header snand_hdr_2k_64_data = {
45 - 0x42, 0x4F, 0x4F, 0x54, 0x4C, 0x4F, 0x41, 0x44,
46 - 0x45, 0x52, 0x21, 0x00, 0x56, 0x30, 0x30, 0x36,
47 - 0x4E, 0x46, 0x49, 0x49, 0x4E, 0x46, 0x4F, 0x00,
48 - 0x00, 0x00, 0x00, 0x08, 0x03, 0x00, 0x40, 0x00,
49 - 0x40, 0x00, 0x00, 0x08, 0x10, 0x00, 0x16, 0x00,
50 - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
51 - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
52 - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
53 - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
54 - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
55 - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
56 - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
57 - 0x00, 0x00, 0x00, 0x00, 0x7B, 0xC4, 0x17, 0x9D,
58 - 0xCA, 0x42, 0x90, 0xD0, 0x98, 0xD0, 0xE0, 0xF7,
59 - 0xDB, 0xCD, 0x16, 0xF6, 0x03, 0x73, 0xD2, 0xB8,
60 - 0x93, 0xB2, 0x56, 0x5A, 0x84, 0x6E, 0x00, 0x00
64 -/* NAND header for SPI-NAND with 2KB page + 120B/128B spare */
65 -static const union nand_boot_header snand_hdr_2k_128_data = {
67 - 0x42, 0x4F, 0x4F, 0x54, 0x4C, 0x4F, 0x41, 0x44,
68 - 0x45, 0x52, 0x21, 0x00, 0x56, 0x30, 0x30, 0x36,
69 - 0x4E, 0x46, 0x49, 0x49, 0x4E, 0x46, 0x4F, 0x00,
70 - 0x00, 0x00, 0x00, 0x08, 0x05, 0x00, 0x70, 0x00,
71 - 0x40, 0x00, 0x00, 0x08, 0x10, 0x00, 0x16, 0x00,
72 - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
73 - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
74 - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
75 - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
76 - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
77 - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
78 - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
79 - 0x00, 0x00, 0x00, 0x00, 0x90, 0x28, 0xED, 0x13,
80 - 0x7F, 0x12, 0x22, 0xCD, 0x3D, 0x06, 0xF1, 0xB3,
81 - 0x6F, 0x2E, 0xD9, 0xA0, 0x9D, 0x7A, 0xBD, 0xD7,
82 - 0xB3, 0x28, 0x3C, 0x13, 0xDB, 0x4E, 0x00, 0x00
86 -/* NAND header for SPI-NAND with 4KB page + 256B spare */
87 -static const union nand_boot_header snand_hdr_4k_256_data = {
89 - 0x42, 0x4F, 0x4F, 0x54, 0x4C, 0x4F, 0x41, 0x44,
90 - 0x45, 0x52, 0x21, 0x00, 0x56, 0x30, 0x30, 0x36,
91 - 0x4E, 0x46, 0x49, 0x49, 0x4E, 0x46, 0x4F, 0x00,
92 - 0x00, 0x00, 0x00, 0x10, 0x05, 0x00, 0xE0, 0x00,
93 - 0x40, 0x00, 0x00, 0x08, 0x10, 0x00, 0x16, 0x00,
94 - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
95 - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
96 - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
97 - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
98 - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
99 - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
100 - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
101 - 0x00, 0x00, 0x00, 0x00, 0x47, 0xED, 0x0E, 0xC3,
102 - 0x83, 0xBF, 0x41, 0xD2, 0x85, 0x21, 0x97, 0x57,
103 - 0xC4, 0x2E, 0x6B, 0x7A, 0x40, 0xE0, 0xCF, 0x8F,
104 - 0x37, 0xBD, 0x17, 0xB6, 0xC7, 0xFE, 0x00, 0x00
108 -/* NAND header for Parallel NAND 1Gb with 2KB page + 64B spare */
109 -static const union nand_boot_header nand_hdr_1gb_2k_64_data = {
111 - 0x42, 0x4F, 0x4F, 0x54, 0x4C, 0x4F, 0x41, 0x44,
112 - 0x45, 0x52, 0x21, 0x00, 0x56, 0x30, 0x30, 0x36,
113 - 0x4E, 0x46, 0x49, 0x49, 0x4E, 0x46, 0x4F, 0x00,
114 - 0x00, 0x00, 0x00, 0x08, 0x05, 0x00, 0x40, 0x00,
115 - 0x40, 0x00, 0x00, 0x04, 0x0B, 0x00, 0x11, 0x00,
116 - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
117 - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
118 - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
119 - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
120 - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
121 - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
122 - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
123 - 0x00, 0x00, 0x00, 0x00, 0x12, 0x28, 0x1C, 0x12,
124 - 0x8F, 0xFD, 0xF8, 0x32, 0x6F, 0x6D, 0xCF, 0x6C,
125 - 0xDA, 0x21, 0x70, 0x8C, 0xDA, 0x0A, 0x22, 0x82,
126 - 0xAA, 0x59, 0xFA, 0x7C, 0x42, 0x2D, 0x00, 0x00
130 -/* NAND header for Parallel NAND 2Gb with 2KB page + 64B spare */
131 -static const union nand_boot_header nand_hdr_2gb_2k_64_data = {
133 - 0x42, 0x4F, 0x4F, 0x54, 0x4C, 0x4F, 0x41, 0x44,
134 - 0x45, 0x52, 0x21, 0x00, 0x56, 0x30, 0x30, 0x36,
135 - 0x4E, 0x46, 0x49, 0x49, 0x4E, 0x46, 0x4F, 0x00,
136 - 0x00, 0x00, 0x00, 0x08, 0x05, 0x00, 0x40, 0x00,
137 - 0x40, 0x00, 0x00, 0x08, 0x0B, 0x00, 0x11, 0x00,
138 - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
139 - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
140 - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
141 - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
142 - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
143 - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
144 - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
145 - 0x00, 0x00, 0x00, 0x00, 0x20, 0x9C, 0x3D, 0x2D,
146 - 0x7B, 0x68, 0x63, 0x52, 0x2E, 0x04, 0x63, 0xF1,
147 - 0x35, 0x4E, 0x44, 0x3E, 0xF8, 0xAC, 0x9B, 0x95,
148 - 0xAB, 0xFE, 0xE4, 0xE1, 0xD5, 0xF9, 0x00, 0x00
152 -/* NAND header for Parallel NAND 4Gb with 2KB page + 64B spare */
153 -static const union nand_boot_header nand_hdr_4gb_2k_64_data = {
155 - 0x42, 0x4F, 0x4F, 0x54, 0x4C, 0x4F, 0x41, 0x44,
156 - 0x45, 0x52, 0x21, 0x00, 0x56, 0x30, 0x30, 0x36,
157 - 0x4E, 0x46, 0x49, 0x49, 0x4E, 0x46, 0x4F, 0x00,
158 - 0x00, 0x00, 0x00, 0x08, 0x05, 0x00, 0x40, 0x00,
159 - 0x40, 0x00, 0x00, 0x10, 0x0B, 0x00, 0x11, 0x00,
160 - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
161 - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
162 - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
163 - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
164 - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
165 - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
166 - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
167 - 0x00, 0x00, 0x00, 0x00, 0xE3, 0x0F, 0x86, 0x32,
168 - 0x68, 0x05, 0xD9, 0xC8, 0x13, 0xDF, 0xC5, 0x0B,
169 - 0x35, 0x3A, 0x68, 0xA5, 0x3C, 0x0C, 0x73, 0x87,
170 - 0x63, 0xB0, 0xBE, 0xCC, 0x84, 0x47, 0x00, 0x00
174 -/* NAND header for Parallel NAND 2Gb with 2KB page + 128B spare */
175 -static const union nand_boot_header nand_hdr_2gb_2k_128_data = {
177 - 0x42, 0x4F, 0x4F, 0x54, 0x4C, 0x4F, 0x41, 0x44,
178 - 0x45, 0x52, 0x21, 0x00, 0x56, 0x30, 0x30, 0x36,
179 - 0x4E, 0x46, 0x49, 0x49, 0x4E, 0x46, 0x4F, 0x00,
180 - 0x00, 0x00, 0x00, 0x08, 0x05, 0x00, 0x70, 0x00,
181 - 0x40, 0x00, 0x00, 0x08, 0x0B, 0x00, 0x11, 0x00,
182 - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
183 - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
184 - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
185 - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
186 - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
187 - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
188 - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
189 - 0x00, 0x00, 0x00, 0x00, 0x01, 0xA5, 0xE9, 0x5A,
190 - 0xDF, 0x58, 0x62, 0x41, 0xD6, 0x26, 0x77, 0xBC,
191 - 0x76, 0x1F, 0x27, 0x4E, 0x4F, 0x6C, 0xC3, 0xF0,
192 - 0x36, 0xDE, 0xD9, 0xB3, 0xFF, 0x93, 0x00, 0x00
196 -/* NAND header for Parallel NAND 4Gb with 2KB page + 128B spare */
197 -static const union nand_boot_header nand_hdr_4gb_2k_128_data = {
199 - 0x42, 0x4F, 0x4F, 0x54, 0x4C, 0x4F, 0x41, 0x44,
200 - 0x45, 0x52, 0x21, 0x00, 0x56, 0x30, 0x30, 0x36,
201 - 0x4E, 0x46, 0x49, 0x49, 0x4E, 0x46, 0x4F, 0x00,
202 - 0x00, 0x00, 0x00, 0x08, 0x05, 0x00, 0x70, 0x00,
203 - 0x40, 0x00, 0x00, 0x10, 0x0B, 0x00, 0x11, 0x00,
204 - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
205 - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
206 - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
207 - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
208 - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
209 - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
210 - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
211 - 0x00, 0x00, 0x00, 0x00, 0xC2, 0x36, 0x52, 0x45,
212 - 0xCC, 0x35, 0xD8, 0xDB, 0xEB, 0xFD, 0xD1, 0x46,
213 - 0x76, 0x6B, 0x0B, 0xD5, 0x8B, 0xCC, 0x2B, 0xE2,
214 - 0xFE, 0x90, 0x83, 0x9E, 0xAE, 0x2D, 0x00, 0x00
218 -static const struct nand_header_type {
220 - const union nand_boot_header *data;
221 -} nand_headers[] = {
224 - .data = &snand_hdr_2k_64_data
227 - .data = &snand_hdr_2k_128_data
230 - .data = &snand_hdr_2k_128_data
233 - .data = &snand_hdr_4k_256_data
235 - .name = "1g:2k+64",
236 - .data = &nand_hdr_1gb_2k_64_data
238 - .name = "2g:2k+64",
239 - .data = &nand_hdr_2gb_2k_64_data
241 - .name = "4g:2k+64",
242 - .data = &nand_hdr_4gb_2k_64_data
244 - .name = "2g:2k+128",
245 - .data = &nand_hdr_2gb_2k_128_data
247 - .name = "4g:2k+128",
248 - .data = &nand_hdr_4gb_2k_128_data
251 +#include "mtk_nand_headers.h"
253 static const struct brom_img_type {
255 @@ -264,6 +55,7 @@ static uint32_t crc32tbl[256];
257 /* NAND header selected by user */
258 static const union nand_boot_header *hdr_nand;
259 +static uint32_t hdr_nand_size;
261 /* GFH header + 2 * 4KB pages of NAND */
262 static char hdr_tmp[sizeof(struct gfh_header) + 0x2000];
263 @@ -402,12 +194,7 @@ static int mtk_brom_parse_imagename(cons
266 /* parse nand header type */
267 - for (i = 0; i < ARRAY_SIZE(nand_headers); i++) {
268 - if (!strcmp(nand_headers[i].name, nandinfo)) {
269 - hdr_nand = nand_headers[i].data;
273 + hdr_nand = mtk_nand_header_find(nandinfo);
275 /* parse device header offset */
276 if (hdr_offs && hdr_offs[0])
277 @@ -432,6 +219,9 @@ static int mtk_brom_parse_imagename(cons
281 + if (hdr_media == BRLYT_TYPE_NAND || hdr_media == BRLYT_TYPE_SNAND)
282 + hdr_nand_size = mtk_nand_header_size(hdr_nand);
287 @@ -468,7 +258,7 @@ static int mtk_image_vrec_header(struct
290 if (hdr_media == BRLYT_TYPE_NAND || hdr_media == BRLYT_TYPE_SNAND)
291 - tparams->header_size = 2 * le16_to_cpu(hdr_nand->pagesize);
292 + tparams->header_size = hdr_nand_size;
294 tparams->header_size = sizeof(struct gen_device_header);
296 @@ -566,16 +356,17 @@ static int mtk_image_verify_gen_header(c
298 static int mtk_image_verify_nand_header(const uint8_t *ptr, int print)
300 - union nand_boot_header *nh = (union nand_boot_header *)ptr;
301 struct brom_layout_header *bh;
302 + struct nand_header_info info;
303 struct gfh_header *gfh;
304 const char *bootmedia;
307 - if (strncmp(nh->version, NAND_BOOT_VERSION, sizeof(nh->version)) ||
308 - strcmp(nh->id, NAND_BOOT_ID))
310 + ret = mtk_nand_header_info(ptr, &info);
314 - bh = (struct brom_layout_header *)(ptr + le16_to_cpu(nh->pagesize));
315 + bh = (struct brom_layout_header *)(ptr + info.page_size);
317 if (strcmp(bh->name, BRLYT_NAME))
319 @@ -586,34 +377,23 @@ static int mtk_image_verify_nand_header(
320 if (le32_to_cpu(bh->type) == BRLYT_TYPE_NAND)
321 bootmedia = "Parallel NAND";
322 else if (le32_to_cpu(bh->type) == BRLYT_TYPE_SNAND)
323 - bootmedia = "Serial NAND";
324 + bootmedia = "Serial NAND (SNFI/AP)";
330 - printf("Boot Media: %s\n", bootmedia);
332 - if (le32_to_cpu(bh->type) == BRLYT_TYPE_NAND) {
333 - uint64_t capacity =
334 - (uint64_t)le16_to_cpu(nh->numblocks) *
335 - (uint64_t)le16_to_cpu(nh->pages_of_block) *
336 - (uint64_t)le16_to_cpu(nh->pagesize) * 8;
337 - printf("Capacity: %dGb\n",
338 - (uint32_t)(capacity >> 30));
340 + printf("Boot Media: %s\n", bootmedia);
342 - if (le16_to_cpu(nh->pagesize) >= 1024)
343 - printf("Page Size: %dKB\n",
344 - le16_to_cpu(nh->pagesize) >> 10);
345 + if (info.page_size >= 1024)
346 + printf("Page Size: %dKB\n", info.page_size >> 10);
348 - printf("Page Size: %dB\n",
349 - le16_to_cpu(nh->pagesize));
350 + printf("Page Size: %dB\n", info.page_size);
352 - printf("Spare Size: %dB\n", le16_to_cpu(nh->oobsize));
353 + printf("Spare Size: %dB\n", info.spare_size);
356 - gfh = (struct gfh_header *)(ptr + 2 * le16_to_cpu(nh->pagesize));
357 + gfh = (struct gfh_header *)(ptr + info.gfh_offset);
359 return mtk_image_verify_gfh(gfh, GFH_FLASH_TYPE_NAND, print);
361 @@ -713,7 +493,7 @@ static int mtk_image_verify_header(unsig
362 if (image_get_magic(hdr) == IH_MAGIC)
363 return mtk_image_verify_mt7621_header(ptr, 0);
365 - if (!strcmp((char *)ptr, NAND_BOOT_NAME))
366 + if (is_mtk_nand_header(ptr))
367 return mtk_image_verify_nand_header(ptr, 0);
369 return mtk_image_verify_gen_header(ptr, 0);
370 @@ -739,7 +519,7 @@ static void mtk_image_print_header(const
374 - if (!strcmp((char *)ptr, NAND_BOOT_NAME))
375 + if (is_mtk_nand_header(ptr))
376 mtk_image_verify_nand_header(ptr, 1);
378 mtk_image_verify_gen_header(ptr, 1);
379 @@ -870,36 +650,33 @@ static void mtk_image_set_gen_header(voi
380 static void mtk_image_set_nand_header(void *ptr, off_t filesize,
383 - union nand_boot_header *nh = (union nand_boot_header *)ptr;
384 struct brom_layout_header *brlyt;
385 struct gfh_header *gfh;
386 - uint32_t payload_pages;
388 + uint32_t payload_pages, nand_page_size;
390 - /* NAND device header, repeat 4 times */
391 - for (i = 0; i < 4; i++)
392 - memcpy(nh + i, hdr_nand, sizeof(union nand_boot_header));
394 + nand_page_size = mtk_nand_header_put(hdr_nand, ptr);
397 - payload_pages = (filesize + le16_to_cpu(hdr_nand->pagesize) - 1) /
398 - le16_to_cpu(hdr_nand->pagesize);
399 - brlyt = (struct brom_layout_header *)
400 - (ptr + le16_to_cpu(hdr_nand->pagesize));
401 - put_brom_layout_header(brlyt, hdr_media);
402 - brlyt->header_size = cpu_to_le32(2);
403 - brlyt->total_size = cpu_to_le32(payload_pages);
404 - brlyt->header_size_2 = brlyt->header_size;
405 - brlyt->total_size_2 = brlyt->total_size;
406 - brlyt->unused = cpu_to_le32(1);
407 + if (nand_page_size) {
409 + payload_pages = (filesize + nand_page_size - 1) /
411 + brlyt = (struct brom_layout_header *)(ptr + nand_page_size);
412 + put_brom_layout_header(brlyt, hdr_media);
413 + brlyt->header_size = cpu_to_le32(2);
414 + brlyt->total_size = cpu_to_le32(payload_pages);
415 + brlyt->header_size_2 = brlyt->header_size;
416 + brlyt->total_size_2 = brlyt->total_size;
417 + brlyt->unused = cpu_to_le32(1);
421 - gfh = (struct gfh_header *)(ptr + 2 * le16_to_cpu(hdr_nand->pagesize));
422 - put_ghf_header(gfh, filesize, 2 * le16_to_cpu(hdr_nand->pagesize),
423 - loadaddr, GFH_FLASH_TYPE_NAND);
424 + gfh = (struct gfh_header *)(ptr + hdr_nand_size);
425 + put_ghf_header(gfh, filesize, hdr_nand_size, loadaddr,
426 + GFH_FLASH_TYPE_NAND);
428 /* Generate SHA256 hash */
429 - put_hash((uint8_t *)gfh,
430 - filesize - 2 * le16_to_cpu(hdr_nand->pagesize) - SHA256_SUM_LEN);
431 + put_hash((uint8_t *)gfh, filesize - hdr_nand_size - SHA256_SUM_LEN);
434 static void mtk_image_set_mt7621_header(void *ptr, off_t filesize,
435 --- a/tools/mtk_image.h
436 +++ b/tools/mtk_image.h
437 @@ -26,31 +26,6 @@ union gen_boot_header {
438 #define SF_BOOT_NAME "SF_BOOT"
439 #define SDMMC_BOOT_NAME "SDMMC_BOOT"
441 -/* Header for NAND */
442 -union nand_boot_header {
449 - uint16_t addrcycles;
451 - uint16_t pages_of_block;
452 - uint16_t numblocks;
453 - uint16_t writesize_shift;
454 - uint16_t erasesize_shift;
456 - uint8_t ecc_parity[28];
459 - uint8_t data[0x80];
462 -#define NAND_BOOT_NAME "BOOTLOADER!"
463 -#define NAND_BOOT_VERSION "V006"
464 -#define NAND_BOOT_ID "NFIINFO"
466 /* BootROM layout header */
467 struct brom_layout_header {
470 +++ b/tools/mtk_nand_headers.c
472 +// SPDX-License-Identifier: GPL-2.0+
474 + * MediaTek BootROM NAND header definitions
476 + * Copyright (C) 2022 MediaTek Inc.
477 + * Author: Weijie Gao <weijie.gao@mediatek.com>
482 +#include "imagetool.h"
483 +#include "mtk_image.h"
484 +#include "mtk_nand_headers.h"
486 +/* NAND header for SPI-NAND with 2KB page + 64B spare */
487 +static const union nand_boot_header snand_hdr_2k_64_data = {
489 + 0x42, 0x4F, 0x4F, 0x54, 0x4C, 0x4F, 0x41, 0x44,
490 + 0x45, 0x52, 0x21, 0x00, 0x56, 0x30, 0x30, 0x36,
491 + 0x4E, 0x46, 0x49, 0x49, 0x4E, 0x46, 0x4F, 0x00,
492 + 0x00, 0x00, 0x00, 0x08, 0x03, 0x00, 0x40, 0x00,
493 + 0x40, 0x00, 0x00, 0x08, 0x10, 0x00, 0x16, 0x00,
494 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
495 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
496 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
497 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
498 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
499 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
500 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
501 + 0x00, 0x00, 0x00, 0x00, 0x7B, 0xC4, 0x17, 0x9D,
502 + 0xCA, 0x42, 0x90, 0xD0, 0x98, 0xD0, 0xE0, 0xF7,
503 + 0xDB, 0xCD, 0x16, 0xF6, 0x03, 0x73, 0xD2, 0xB8,
504 + 0x93, 0xB2, 0x56, 0x5A, 0x84, 0x6E, 0x00, 0x00
508 +/* NAND header for SPI-NAND with 2KB page + 120B/128B spare */
509 +static const union nand_boot_header snand_hdr_2k_128_data = {
511 + 0x42, 0x4F, 0x4F, 0x54, 0x4C, 0x4F, 0x41, 0x44,
512 + 0x45, 0x52, 0x21, 0x00, 0x56, 0x30, 0x30, 0x36,
513 + 0x4E, 0x46, 0x49, 0x49, 0x4E, 0x46, 0x4F, 0x00,
514 + 0x00, 0x00, 0x00, 0x08, 0x05, 0x00, 0x70, 0x00,
515 + 0x40, 0x00, 0x00, 0x08, 0x10, 0x00, 0x16, 0x00,
516 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
517 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
518 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
519 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
520 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
521 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
522 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
523 + 0x00, 0x00, 0x00, 0x00, 0x90, 0x28, 0xED, 0x13,
524 + 0x7F, 0x12, 0x22, 0xCD, 0x3D, 0x06, 0xF1, 0xB3,
525 + 0x6F, 0x2E, 0xD9, 0xA0, 0x9D, 0x7A, 0xBD, 0xD7,
526 + 0xB3, 0x28, 0x3C, 0x13, 0xDB, 0x4E, 0x00, 0x00
530 +/* NAND header for SPI-NAND with 4KB page + 256B spare */
531 +static const union nand_boot_header snand_hdr_4k_256_data = {
533 + 0x42, 0x4F, 0x4F, 0x54, 0x4C, 0x4F, 0x41, 0x44,
534 + 0x45, 0x52, 0x21, 0x00, 0x56, 0x30, 0x30, 0x36,
535 + 0x4E, 0x46, 0x49, 0x49, 0x4E, 0x46, 0x4F, 0x00,
536 + 0x00, 0x00, 0x00, 0x10, 0x05, 0x00, 0xE0, 0x00,
537 + 0x40, 0x00, 0x00, 0x08, 0x10, 0x00, 0x16, 0x00,
538 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
539 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
540 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
541 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
542 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
543 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
544 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
545 + 0x00, 0x00, 0x00, 0x00, 0x47, 0xED, 0x0E, 0xC3,
546 + 0x83, 0xBF, 0x41, 0xD2, 0x85, 0x21, 0x97, 0x57,
547 + 0xC4, 0x2E, 0x6B, 0x7A, 0x40, 0xE0, 0xCF, 0x8F,
548 + 0x37, 0xBD, 0x17, 0xB6, 0xC7, 0xFE, 0x00, 0x00
552 +/* NAND header for Parallel NAND 1Gb with 2KB page + 64B spare */
553 +static const union nand_boot_header nand_hdr_1gb_2k_64_data = {
555 + 0x42, 0x4F, 0x4F, 0x54, 0x4C, 0x4F, 0x41, 0x44,
556 + 0x45, 0x52, 0x21, 0x00, 0x56, 0x30, 0x30, 0x36,
557 + 0x4E, 0x46, 0x49, 0x49, 0x4E, 0x46, 0x4F, 0x00,
558 + 0x00, 0x00, 0x00, 0x08, 0x05, 0x00, 0x40, 0x00,
559 + 0x40, 0x00, 0x00, 0x04, 0x0B, 0x00, 0x11, 0x00,
560 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
561 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
562 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
563 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
564 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
565 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
566 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
567 + 0x00, 0x00, 0x00, 0x00, 0x12, 0x28, 0x1C, 0x12,
568 + 0x8F, 0xFD, 0xF8, 0x32, 0x6F, 0x6D, 0xCF, 0x6C,
569 + 0xDA, 0x21, 0x70, 0x8C, 0xDA, 0x0A, 0x22, 0x82,
570 + 0xAA, 0x59, 0xFA, 0x7C, 0x42, 0x2D, 0x00, 0x00
574 +/* NAND header for Parallel NAND 2Gb with 2KB page + 64B spare */
575 +static const union nand_boot_header nand_hdr_2gb_2k_64_data = {
577 + 0x42, 0x4F, 0x4F, 0x54, 0x4C, 0x4F, 0x41, 0x44,
578 + 0x45, 0x52, 0x21, 0x00, 0x56, 0x30, 0x30, 0x36,
579 + 0x4E, 0x46, 0x49, 0x49, 0x4E, 0x46, 0x4F, 0x00,
580 + 0x00, 0x00, 0x00, 0x08, 0x05, 0x00, 0x40, 0x00,
581 + 0x40, 0x00, 0x00, 0x08, 0x0B, 0x00, 0x11, 0x00,
582 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
583 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
584 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
585 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
586 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
587 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
588 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
589 + 0x00, 0x00, 0x00, 0x00, 0x20, 0x9C, 0x3D, 0x2D,
590 + 0x7B, 0x68, 0x63, 0x52, 0x2E, 0x04, 0x63, 0xF1,
591 + 0x35, 0x4E, 0x44, 0x3E, 0xF8, 0xAC, 0x9B, 0x95,
592 + 0xAB, 0xFE, 0xE4, 0xE1, 0xD5, 0xF9, 0x00, 0x00
596 +/* NAND header for Parallel NAND 4Gb with 2KB page + 64B spare */
597 +static const union nand_boot_header nand_hdr_4gb_2k_64_data = {
599 + 0x42, 0x4F, 0x4F, 0x54, 0x4C, 0x4F, 0x41, 0x44,
600 + 0x45, 0x52, 0x21, 0x00, 0x56, 0x30, 0x30, 0x36,
601 + 0x4E, 0x46, 0x49, 0x49, 0x4E, 0x46, 0x4F, 0x00,
602 + 0x00, 0x00, 0x00, 0x08, 0x05, 0x00, 0x40, 0x00,
603 + 0x40, 0x00, 0x00, 0x10, 0x0B, 0x00, 0x11, 0x00,
604 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
605 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
606 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
607 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
608 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
609 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
610 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
611 + 0x00, 0x00, 0x00, 0x00, 0xE3, 0x0F, 0x86, 0x32,
612 + 0x68, 0x05, 0xD9, 0xC8, 0x13, 0xDF, 0xC5, 0x0B,
613 + 0x35, 0x3A, 0x68, 0xA5, 0x3C, 0x0C, 0x73, 0x87,
614 + 0x63, 0xB0, 0xBE, 0xCC, 0x84, 0x47, 0x00, 0x00
618 +/* NAND header for Parallel NAND 2Gb with 2KB page + 128B spare */
619 +static const union nand_boot_header nand_hdr_2gb_2k_128_data = {
621 + 0x42, 0x4F, 0x4F, 0x54, 0x4C, 0x4F, 0x41, 0x44,
622 + 0x45, 0x52, 0x21, 0x00, 0x56, 0x30, 0x30, 0x36,
623 + 0x4E, 0x46, 0x49, 0x49, 0x4E, 0x46, 0x4F, 0x00,
624 + 0x00, 0x00, 0x00, 0x08, 0x05, 0x00, 0x70, 0x00,
625 + 0x40, 0x00, 0x00, 0x08, 0x0B, 0x00, 0x11, 0x00,
626 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
627 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
628 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
629 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
630 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
631 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
632 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
633 + 0x00, 0x00, 0x00, 0x00, 0x01, 0xA5, 0xE9, 0x5A,
634 + 0xDF, 0x58, 0x62, 0x41, 0xD6, 0x26, 0x77, 0xBC,
635 + 0x76, 0x1F, 0x27, 0x4E, 0x4F, 0x6C, 0xC3, 0xF0,
636 + 0x36, 0xDE, 0xD9, 0xB3, 0xFF, 0x93, 0x00, 0x00
640 +/* NAND header for Parallel NAND 4Gb with 2KB page + 128B spare */
641 +static const union nand_boot_header nand_hdr_4gb_2k_128_data = {
643 + 0x42, 0x4F, 0x4F, 0x54, 0x4C, 0x4F, 0x41, 0x44,
644 + 0x45, 0x52, 0x21, 0x00, 0x56, 0x30, 0x30, 0x36,
645 + 0x4E, 0x46, 0x49, 0x49, 0x4E, 0x46, 0x4F, 0x00,
646 + 0x00, 0x00, 0x00, 0x08, 0x05, 0x00, 0x70, 0x00,
647 + 0x40, 0x00, 0x00, 0x10, 0x0B, 0x00, 0x11, 0x00,
648 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
649 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
650 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
651 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
652 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
653 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
654 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
655 + 0x00, 0x00, 0x00, 0x00, 0xC2, 0x36, 0x52, 0x45,
656 + 0xCC, 0x35, 0xD8, 0xDB, 0xEB, 0xFD, 0xD1, 0x46,
657 + 0x76, 0x6B, 0x0B, 0xD5, 0x8B, 0xCC, 0x2B, 0xE2,
658 + 0xFE, 0x90, 0x83, 0x9E, 0xAE, 0x2D, 0x00, 0x00
662 +static const struct nand_header_type {
664 + const union nand_boot_header *data;
665 +} nand_headers[] = {
668 + .data = &snand_hdr_2k_64_data
671 + .data = &snand_hdr_2k_128_data
674 + .data = &snand_hdr_2k_128_data
677 + .data = &snand_hdr_4k_256_data
679 + .name = "1g:2k+64",
680 + .data = &nand_hdr_1gb_2k_64_data
682 + .name = "2g:2k+64",
683 + .data = &nand_hdr_2gb_2k_64_data
685 + .name = "4g:2k+64",
686 + .data = &nand_hdr_4gb_2k_64_data
688 + .name = "2g:2k+128",
689 + .data = &nand_hdr_2gb_2k_128_data
691 + .name = "4g:2k+128",
692 + .data = &nand_hdr_4gb_2k_128_data
696 +const union nand_boot_header *mtk_nand_header_find(const char *name)
700 + for (i = 0; i < ARRAY_SIZE(nand_headers); i++) {
701 + if (!strcmp(nand_headers[i].name, name))
702 + return nand_headers[i].data;
708 +uint32_t mtk_nand_header_size(const union nand_boot_header *hdr_nand)
710 + return 2 * le16_to_cpu(hdr_nand->pagesize);
713 +static int mtk_nand_header_ap_info(const void *ptr,
714 + struct nand_header_info *info)
716 + union nand_boot_header *nh = (union nand_boot_header *)ptr;
718 + if (strncmp(nh->version, NAND_BOOT_VERSION, sizeof(nh->version)) ||
719 + strcmp(nh->id, NAND_BOOT_ID))
722 + info->page_size = le16_to_cpu(nh->pagesize);
723 + info->spare_size = le16_to_cpu(nh->oobsize);
724 + info->gfh_offset = 2 * info->page_size;
729 +int mtk_nand_header_info(const void *ptr, struct nand_header_info *info)
731 + if (!strcmp((char *)ptr, NAND_BOOT_NAME))
732 + return mtk_nand_header_ap_info(ptr, info);
737 +bool is_mtk_nand_header(const void *ptr)
739 + struct nand_header_info info;
741 + if (mtk_nand_header_info(ptr, &info) >= 0)
747 +uint32_t mtk_nand_header_put(const union nand_boot_header *hdr_nand, void *ptr)
749 + union nand_boot_header *nh = (union nand_boot_header *)ptr;
752 + /* NAND device header, repeat 4 times */
753 + for (i = 0; i < 4; i++)
754 + memcpy(nh + i, hdr_nand, sizeof(union nand_boot_header));
756 + return le16_to_cpu(hdr_nand->pagesize);
759 +++ b/tools/mtk_nand_headers.h
761 +/* SPDX-License-Identifier: GPL-2.0+ */
763 + * MediaTek BootROM NAND header definitions
765 + * Copyright (C) 2022 MediaTek Inc.
766 + * Author: Weijie Gao <weijie.gao@mediatek.com>
769 +#ifndef _MTK_NAND_HEADERS_H
770 +#define _MTK_NAND_HEADERS_H
773 +#include <stdbool.h>
775 +struct nand_header_info {
776 + uint32_t page_size;
777 + uint32_t spare_size;
778 + uint32_t gfh_offset;
781 +/* AP BROM Header for NAND */
782 +union nand_boot_header {
787 + uint16_t ioif; /* I/O interface */
788 + uint16_t pagesize; /* NAND page size */
789 + uint16_t addrcycles; /* Address cycles */
790 + uint16_t oobsize; /* NAND page spare size */
791 + uint16_t pages_of_block; /* Pages of one block */
792 + uint16_t numblocks; /* Total blocks of NAND chip */
793 + uint16_t writesize_shift;
794 + uint16_t erasesize_shift;
796 + uint8_t ecc_parity[28]; /* ECC parity of this header */
799 + uint8_t data[0x80];
802 +#define NAND_BOOT_NAME "BOOTLOADER!"
803 +#define NAND_BOOT_VERSION "V006"
804 +#define NAND_BOOT_ID "NFIINFO"
806 +/* Find nand header data by name */
807 +const union nand_boot_header *mtk_nand_header_find(const char *name);
809 +/* Device header size using this nand header */
810 +uint32_t mtk_nand_header_size(const union nand_boot_header *hdr_nand);
812 +/* Get nand info from nand header (page size, spare size, ...) */
813 +int mtk_nand_header_info(const void *ptr, struct nand_header_info *info);
815 +/* Whether given header data is valid */
816 +bool is_mtk_nand_header(const void *ptr);
818 +/* Generate Device header using give nand header */
819 +uint32_t mtk_nand_header_put(const union nand_boot_header *hdr_nand, void *ptr);
821 +#endif /* _MTK_NAND_HEADERS_H */