blob: f119e9f88d5c0bc9fded88f78a72f1086493b2ca [file] [log] [blame]
Shyam Saini1d43e242019-06-14 13:05:33 +05301/*
Han Xu214b7d52020-05-05 22:04:03 +08002 * i.MX nand boot control block(bcb).
Shyam Saini1d43e242019-06-14 13:05:33 +05303 *
4 * Based on the common/imx-bbu-nand-fcb.c from barebox and imx kobs-ng
5 *
6 * Copyright (C) 2017 Jagan Teki <jagan@amarulasolutions.com>
7 * Copyright (C) 2016 Sergey Kubushyn <ksi@koi8.net>
8 *
Han Xu214b7d52020-05-05 22:04:03 +08009 * Reconstucted by Han Xu <han.xu@nxp.com>
10 *
Shyam Saini1d43e242019-06-14 13:05:33 +053011 * SPDX-License-Identifier: GPL-2.0+
12 */
13
14#include <common.h>
Simon Glass09140112020-05-10 11:40:03 -060015#include <command.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060016#include <log.h>
Simon Glass336d4612020-02-03 07:36:16 -070017#include <malloc.h>
Shyam Saini1d43e242019-06-14 13:05:33 +053018#include <nand.h>
Simon Glass61b29b82020-02-03 07:36:15 -070019#include <dm/devres.h>
Simon Glasseb41d8a2020-05-10 11:40:08 -060020#include <linux/bug.h>
Shyam Saini1d43e242019-06-14 13:05:33 +053021
22#include <asm/io.h>
23#include <jffs2/jffs2.h>
Parthiban Nallathambi6aa87492019-10-18 11:46:19 +020024#include <linux/bch.h>
Shyam Saini1d43e242019-06-14 13:05:33 +053025#include <linux/mtd/mtd.h>
Tom Rini1cefed12021-09-22 14:50:35 -040026#include <linux/mtd/rawnand.h>
Shyam Saini1d43e242019-06-14 13:05:33 +053027
Igor Opaniukdad30dd2019-11-03 16:49:44 +010028#include <asm/arch/sys_proto.h>
Shyam Saini1d43e242019-06-14 13:05:33 +053029#include <asm/mach-imx/imx-nandbcb.h>
30#include <asm/mach-imx/imximage.cfg>
31#include <mxs_nand.h>
32#include <linux/mtd/mtd.h>
33#include <nand.h>
Han Xuf797fe82020-05-05 22:04:04 +080034#include <fuse.h>
Shyam Saini1d43e242019-06-14 13:05:33 +053035
Miquel Raynaleb446ef2019-10-25 19:39:29 +020036#include "../../../cmd/legacy-mtd-utils.h"
37
Han Xu214b7d52020-05-05 22:04:03 +080038/* FCB related flags */
39/* FCB layout with leading 12B reserved */
40#define FCB_LAYOUT_RESV_12B BIT(0)
41/* FCB layout with leading 32B meta data */
42#define FCB_LAYOUT_META_32B BIT(1)
43/* FCB encrypted by Hamming code */
44#define FCB_ENCODE_HAMMING BIT(2)
45/* FCB encrypted by 40bit BCH */
46#define FCB_ENCODE_BCH_40b BIT(3)
47/* FCB encrypted by 62bit BCH */
48#define FCB_ENCODE_BCH_62b BIT(4)
49/* FCB encrypted by BCH */
50#define FCB_ENCODE_BCH (FCB_ENCODE_BCH_40b | FCB_ENCODE_BCH_62b)
51/* FCB data was randomized */
52#define FCB_RANDON_ENABLED BIT(5)
53
54/* Firmware related flags */
55/* No 1K padding */
56#define FIRMWARE_NEED_PADDING BIT(8)
57/* Extra firmware*/
58#define FIRMWARE_EXTRA_ONE BIT(9)
59/* Secondary firmware on fixed address */
60#define FIRMWARE_SECONDARY_FIXED_ADDR BIT(10)
61
62/* Boot search related flags */
63#define BT_SEARCH_CNT_FROM_FUSE BIT(16)
64
65struct platform_config {
66 int misc_flags;
67};
68
69static struct platform_config plat_config;
70
71/* imx6q/dl/solo */
72static struct platform_config imx6qdl_plat_config = {
73 .misc_flags = FCB_LAYOUT_RESV_12B |
74 FCB_ENCODE_HAMMING |
75 FIRMWARE_NEED_PADDING,
76};
77
78static struct platform_config imx6sx_plat_config = {
79 .misc_flags = FCB_LAYOUT_META_32B |
80 FCB_ENCODE_BCH_62b |
81 FIRMWARE_NEED_PADDING |
82 FCB_RANDON_ENABLED,
83};
84
85static struct platform_config imx7d_plat_config = {
86 .misc_flags = FCB_LAYOUT_META_32B |
87 FCB_ENCODE_BCH_62b |
88 FIRMWARE_NEED_PADDING |
89 FCB_RANDON_ENABLED,
90};
91
92/* imx6ul/ull/ulz */
93static struct platform_config imx6ul_plat_config = {
94 .misc_flags = FCB_LAYOUT_META_32B |
95 FCB_ENCODE_BCH_40b |
96 FIRMWARE_NEED_PADDING,
97};
98
99static struct platform_config imx8mq_plat_config = {
100 .misc_flags = FCB_LAYOUT_META_32B |
101 FCB_ENCODE_BCH_62b |
102 FIRMWARE_NEED_PADDING |
103 FCB_RANDON_ENABLED |
104 FIRMWARE_EXTRA_ONE,
105};
106
107/* all other imx8mm */
108static struct platform_config imx8mm_plat_config = {
109 .misc_flags = FCB_LAYOUT_META_32B |
110 FCB_ENCODE_BCH_62b |
111 FIRMWARE_NEED_PADDING |
112 FCB_RANDON_ENABLED,
113};
114
115/* imx8mn */
116static struct platform_config imx8mn_plat_config = {
117 .misc_flags = FCB_LAYOUT_META_32B |
118 FCB_ENCODE_BCH_62b |
119 FCB_RANDON_ENABLED |
120 FIRMWARE_SECONDARY_FIXED_ADDR |
121 BT_SEARCH_CNT_FROM_FUSE,
122};
123
124/* imx8qx/qm */
125static struct platform_config imx8q_plat_config = {
126 .misc_flags = FCB_LAYOUT_META_32B |
127 FCB_ENCODE_BCH_62b |
128 FCB_RANDON_ENABLED |
129 FIRMWARE_SECONDARY_FIXED_ADDR |
130 BT_SEARCH_CNT_FROM_FUSE,
131};
132
133/* boot search related variables and definitions */
134static int g_boot_search_count = 4;
Michael Trimarchi02737ac2022-01-07 18:27:17 +0100135static int g_boot_secondary_offset;
Han Xu214b7d52020-05-05 22:04:03 +0800136static int g_boot_search_stride;
137static int g_pages_per_stride;
138
139/* mtd config structure */
140struct boot_config {
141 int dev;
142 struct mtd_info *mtd;
143 loff_t maxsize;
144 loff_t input_size;
145 loff_t offset;
146 loff_t boot_stream1_address;
147 loff_t boot_stream2_address;
148 size_t boot_stream1_size;
149 size_t boot_stream2_size;
150 size_t max_boot_stream_size;
151 int stride_size_in_byte;
152 int search_area_size_in_bytes;
153 int search_area_size_in_pages;
154 int secondary_boot_stream_off_in_MB;
155};
156
157/* boot_stream config structure */
158struct boot_stream_config {
159 char bs_label[32];
160 loff_t bs_addr;
161 size_t bs_size;
162 void *bs_buf;
163 loff_t next_bs_addr;
164 bool need_padding;
165};
166
167/* FW index */
168#define FW1_ONLY 1
169#define FW2_ONLY 2
170#define FW_ALL FW1_ONLY | FW2_ONLY
171#define FW_INX(x) (1 << (x))
172
173/* NAND convert macros */
174#define CONV_TO_PAGES(x) ((u32)(x) / (u32)(mtd->writesize))
175#define CONV_TO_BLOCKS(x) ((u32)(x) / (u32)(mtd->erasesize))
176
Shyam Saini1d43e242019-06-14 13:05:33 +0530177#define GETBIT(v, n) (((v) >> (n)) & 0x1)
Alice Guo66dbd9c2020-05-05 22:04:00 +0800178#define IMX8MQ_SPL_SZ 0x3e000
179#define IMX8MQ_HDMI_FW_SZ 0x19c00
Alice Guo0b103372020-05-05 22:04:01 +0800180
Han Xu214b7d52020-05-05 22:04:03 +0800181static int nandbcb_get_info(int argc, char * const argv[],
182 struct boot_config *boot_cfg)
183{
184 int dev;
185 struct mtd_info *mtd;
186
187 dev = nand_curr_device;
188 if (dev < 0) {
189 printf("failed to get nand_curr_device, run nand device\n");
190 return CMD_RET_FAILURE;
191 }
192
193 mtd = get_nand_dev_by_index(dev);
194 if (!mtd) {
195 printf("failed to get mtd info\n");
196 return CMD_RET_FAILURE;
197 }
198
199 boot_cfg->dev = dev;
200 boot_cfg->mtd = mtd;
201
202 return CMD_RET_SUCCESS;
203}
204
205static int nandbcb_get_size(int argc, char * const argv[], int num,
206 struct boot_config *boot_cfg)
207{
208 int dev;
209 loff_t offset, size, maxsize;
210 struct mtd_info *mtd;
211
212 dev = boot_cfg->dev;
213 mtd = boot_cfg->mtd;
214 size = 0;
215
216 if (mtd_arg_off_size(argc - num, argv + num, &dev, &offset, &size,
217 &maxsize, MTD_DEV_TYPE_NAND, mtd->size))
218 return CMD_RET_FAILURE;
219
220 boot_cfg->maxsize = maxsize;
221 boot_cfg->offset = offset;
222
223 debug("max: %llx, offset: %llx\n", maxsize, offset);
224
225 if (size && size != maxsize)
226 boot_cfg->input_size = size;
227
228 return CMD_RET_SUCCESS;
229}
230
231static int nandbcb_set_boot_config(int argc, char * const argv[],
232 struct boot_config *boot_cfg)
233{
234 struct mtd_info *mtd;
235 loff_t maxsize;
236 loff_t boot_stream1_address, boot_stream2_address, max_boot_stream_size;
237
238 if (!boot_cfg->mtd) {
239 printf("Didn't get the mtd info, quit\n");
240 return CMD_RET_FAILURE;
241 }
242 mtd = boot_cfg->mtd;
243
244 /*
245 * By default
246 * set the search count as 4
247 * set each FCB/DBBT/Firmware offset at the beginning of blocks
248 * customers may change the value as needed
249 */
250
251 /* if need more compact layout, change these values */
252 /* g_boot_search_count was set as 4 at the definition*/
253 /* g_pages_per_stride was set as block size */
254
255 g_pages_per_stride = mtd->erasesize / mtd->writesize;
256
257 g_boot_search_stride = mtd->writesize * g_pages_per_stride;
258
259 boot_cfg->stride_size_in_byte = g_boot_search_stride * mtd->writesize;
260 boot_cfg->search_area_size_in_bytes =
261 g_boot_search_count * g_boot_search_stride;
262 boot_cfg->search_area_size_in_pages =
263 boot_cfg->search_area_size_in_bytes / mtd->writesize;
264
265 /* after FCB/DBBT, split the rest of area for two Firmwares */
266 if (!boot_cfg->maxsize) {
267 printf("Didn't get the maxsize, quit\n");
268 return CMD_RET_FAILURE;
269 }
270 maxsize = boot_cfg->maxsize;
271 /* align to page boundary */
272 maxsize = ((u32)(maxsize + mtd->writesize - 1)) / (u32)mtd->writesize
273 * mtd->writesize;
274
275 boot_stream1_address = 2 * boot_cfg->search_area_size_in_bytes;
276 boot_stream2_address = ((maxsize - boot_stream1_address) / 2 +
277 boot_stream1_address);
278
Michael Trimarchi02737ac2022-01-07 18:27:17 +0100279 if (g_boot_secondary_offset)
Ye Li233b0b02020-08-02 23:07:55 -0700280 boot_stream2_address =
Michael Trimarchi02737ac2022-01-07 18:27:17 +0100281 (loff_t)g_boot_secondary_offset * 1024 * 1024;
Han Xu214b7d52020-05-05 22:04:03 +0800282
283 max_boot_stream_size = boot_stream2_address - boot_stream1_address;
284
285 /* sanity check */
286 if (max_boot_stream_size <= 0) {
287 debug("st1_addr: %llx, st2_addr: %llx, max: %llx\n",
288 boot_stream1_address, boot_stream2_address,
289 max_boot_stream_size);
290 printf("something wrong with firmware address settings\n");
291 return CMD_RET_FAILURE;
292 }
293 boot_cfg->boot_stream1_address = boot_stream1_address;
294 boot_cfg->boot_stream2_address = boot_stream2_address;
295 boot_cfg->max_boot_stream_size = max_boot_stream_size;
296
297 /* set the boot_stream size as the input size now */
298 if (boot_cfg->input_size) {
299 boot_cfg->boot_stream1_size = boot_cfg->input_size;
300 boot_cfg->boot_stream2_size = boot_cfg->input_size;
301 }
302
303 return CMD_RET_SUCCESS;
304}
305
306static int nandbcb_check_space(struct boot_config *boot_cfg)
307{
308 size_t maxsize = boot_cfg->maxsize;
309 size_t max_boot_stream_size = boot_cfg->max_boot_stream_size;
310 loff_t boot_stream2_address = boot_cfg->boot_stream2_address;
311
312 if (boot_cfg->boot_stream1_size &&
313 boot_cfg->boot_stream1_size > max_boot_stream_size) {
314 printf("boot stream1 doesn't fit, check partition size or settings\n");
315 return CMD_RET_FAILURE;
316 }
317
318 if (boot_cfg->boot_stream2_size &&
319 boot_cfg->boot_stream2_size > maxsize - boot_stream2_address) {
320 printf("boot stream2 doesn't fit, check partition size or settings\n");
321 return CMD_RET_FAILURE;
322 }
323
324 return CMD_RET_SUCCESS;
325}
Shyam Saini1d43e242019-06-14 13:05:33 +0530326
Parthiban Nallathambi6aa87492019-10-18 11:46:19 +0200327#if defined(CONFIG_MX6UL) || defined(CONFIG_MX6ULL)
328static uint8_t reverse_bit(uint8_t b)
329{
330 b = (b & 0xf0) >> 4 | (b & 0x0f) << 4;
331 b = (b & 0xcc) >> 2 | (b & 0x33) << 2;
332 b = (b & 0xaa) >> 1 | (b & 0x55) << 1;
333
334 return b;
335}
336
337static void encode_bch_ecc(void *buf, struct fcb_block *fcb, int eccbits)
338{
339 int i, j, m = 13;
340 int blocksize = 128;
341 int numblocks = 8;
342 int ecc_buf_size = (m * eccbits + 7) / 8;
343 struct bch_control *bch = init_bch(m, eccbits, 0);
344 u8 *ecc_buf = kzalloc(ecc_buf_size, GFP_KERNEL);
345 u8 *tmp_buf = kzalloc(blocksize * numblocks, GFP_KERNEL);
346 u8 *psrc, *pdst;
347
348 /*
349 * The blocks here are bit aligned. If eccbits is a multiple of 8,
350 * we just can copy bytes. Otherwiese we must move the blocks to
351 * the next free bit position.
352 */
353 WARN_ON(eccbits % 8);
354
355 memcpy(tmp_buf, fcb, sizeof(*fcb));
356
357 for (i = 0; i < numblocks; i++) {
358 memset(ecc_buf, 0, ecc_buf_size);
359 psrc = tmp_buf + i * blocksize;
360 pdst = buf + i * (blocksize + ecc_buf_size);
361
362 /* copy data byte aligned to destination buf */
363 memcpy(pdst, psrc, blocksize);
364
365 /*
366 * imx-kobs use a modified encode_bch which reverse the
367 * bit order of the data before calculating bch.
368 * Do this in the buffer and use the bch lib here.
369 */
370 for (j = 0; j < blocksize; j++)
371 psrc[j] = reverse_bit(psrc[j]);
372
373 encode_bch(bch, psrc, blocksize, ecc_buf);
374
375 /* reverse ecc bit */
376 for (j = 0; j < ecc_buf_size; j++)
377 ecc_buf[j] = reverse_bit(ecc_buf[j]);
378
379 /* Here eccbuf is byte aligned and we can just copy it */
380 memcpy(pdst + blocksize, ecc_buf, ecc_buf_size);
381 }
382
383 kfree(ecc_buf);
384 kfree(tmp_buf);
385 free_bch(bch);
386}
387#else
388
Shyam Saini1d43e242019-06-14 13:05:33 +0530389static u8 calculate_parity_13_8(u8 d)
390{
391 u8 p = 0;
392
393 p |= (GETBIT(d, 6) ^ GETBIT(d, 5) ^ GETBIT(d, 3) ^ GETBIT(d, 2)) << 0;
394 p |= (GETBIT(d, 7) ^ GETBIT(d, 5) ^ GETBIT(d, 4) ^ GETBIT(d, 2) ^
395 GETBIT(d, 1)) << 1;
396 p |= (GETBIT(d, 7) ^ GETBIT(d, 6) ^ GETBIT(d, 5) ^ GETBIT(d, 1) ^
397 GETBIT(d, 0)) << 2;
398 p |= (GETBIT(d, 7) ^ GETBIT(d, 4) ^ GETBIT(d, 3) ^ GETBIT(d, 0)) << 3;
399 p |= (GETBIT(d, 6) ^ GETBIT(d, 4) ^ GETBIT(d, 3) ^ GETBIT(d, 2) ^
400 GETBIT(d, 1) ^ GETBIT(d, 0)) << 4;
401
402 return p;
403}
404
405static void encode_hamming_13_8(void *_src, void *_ecc, size_t size)
406{
407 int i;
408 u8 *src = _src;
409 u8 *ecc = _ecc;
410
411 for (i = 0; i < size; i++)
412 ecc[i] = calculate_parity_13_8(src[i]);
413}
Parthiban Nallathambi6aa87492019-10-18 11:46:19 +0200414#endif
Shyam Saini1d43e242019-06-14 13:05:33 +0530415
416static u32 calc_chksum(void *buf, size_t size)
417{
418 u32 chksum = 0;
419 u8 *bp = buf;
420 size_t i;
421
422 for (i = 0; i < size; i++)
423 chksum += bp[i];
424
425 return ~chksum;
426}
427
Han Xu214b7d52020-05-05 22:04:03 +0800428static void fill_fcb(struct fcb_block *fcb, struct boot_config *boot_cfg)
Shyam Saini1d43e242019-06-14 13:05:33 +0530429{
Han Xu214b7d52020-05-05 22:04:03 +0800430 struct mtd_info *mtd = boot_cfg->mtd;
Shyam Saini1d43e242019-06-14 13:05:33 +0530431 struct nand_chip *chip = mtd_to_nand(mtd);
432 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
Igor Opaniukdad30dd2019-11-03 16:49:44 +0100433 struct mxs_nand_layout l;
434
435 mxs_nand_get_layout(mtd, &l);
Shyam Saini1d43e242019-06-14 13:05:33 +0530436
437 fcb->fingerprint = FCB_FINGERPRINT;
438 fcb->version = FCB_VERSION_1;
Igor Opaniukdad30dd2019-11-03 16:49:44 +0100439
Han Xu214b7d52020-05-05 22:04:03 +0800440 fcb->datasetup = 80;
441 fcb->datahold = 60;
442 fcb->addr_setup = 25;
443 fcb->dsample_time = 6;
444
Shyam Saini1d43e242019-06-14 13:05:33 +0530445 fcb->pagesize = mtd->writesize;
446 fcb->oob_pagesize = mtd->writesize + mtd->oobsize;
447 fcb->sectors = mtd->erasesize / mtd->writesize;
448
Igor Opaniukdad30dd2019-11-03 16:49:44 +0100449 fcb->meta_size = l.meta_size;
450 fcb->nr_blocks = l.nblocks;
451 fcb->ecc_nr = l.data0_size;
452 fcb->ecc_level = l.ecc0;
453 fcb->ecc_size = l.datan_size;
454 fcb->ecc_type = l.eccn;
Han Xuc6ed3502020-05-05 22:03:59 +0800455 fcb->bchtype = l.gf_len;
Shyam Saini1d43e242019-06-14 13:05:33 +0530456
Han Xu214b7d52020-05-05 22:04:03 +0800457 /* DBBT search area starts from the next block after all FCB */
458 fcb->dbbt_start = boot_cfg->search_area_size_in_pages;
Shyam Saini1d43e242019-06-14 13:05:33 +0530459
460 fcb->bb_byte = nand_info->bch_geometry.block_mark_byte_offset;
461 fcb->bb_start_bit = nand_info->bch_geometry.block_mark_bit_offset;
462
463 fcb->phy_offset = mtd->writesize;
464
Igor Opaniukdad30dd2019-11-03 16:49:44 +0100465 fcb->disbbm = 0;
Igor Opaniukdad30dd2019-11-03 16:49:44 +0100466
Han Xu214b7d52020-05-05 22:04:03 +0800467 fcb->fw1_start = CONV_TO_PAGES(boot_cfg->boot_stream1_address);
468 fcb->fw2_start = CONV_TO_PAGES(boot_cfg->boot_stream2_address);
469 fcb->fw1_pages = CONV_TO_PAGES(boot_cfg->boot_stream1_size);
470 fcb->fw2_pages = CONV_TO_PAGES(boot_cfg->boot_stream2_size);
Igor Opaniuk1b899a82019-11-03 16:49:45 +0100471
Shyam Saini1d43e242019-06-14 13:05:33 +0530472 fcb->checksum = calc_chksum((void *)fcb + 4, sizeof(*fcb) - 4);
473}
474
Han Xu214b7d52020-05-05 22:04:03 +0800475static int fill_dbbt_data(struct mtd_info *mtd, void *buf, int num_blocks)
Shyam Saini1d43e242019-06-14 13:05:33 +0530476{
477 int n, n_bad_blocks = 0;
478 u32 *bb = buf + 0x8;
479 u32 *n_bad_blocksp = buf + 0x4;
480
481 for (n = 0; n < num_blocks; n++) {
Ye Li983f5e02020-08-02 22:59:43 -0700482 loff_t offset = (loff_t)n * mtd->erasesize;
Shyam Saini1d43e242019-06-14 13:05:33 +0530483 if (mtd_block_isbad(mtd, offset)) {
484 n_bad_blocks++;
485 *bb = n;
486 bb++;
487 }
488 }
489
490 *n_bad_blocksp = n_bad_blocks;
491
492 return n_bad_blocks;
493}
494
Han Xu214b7d52020-05-05 22:04:03 +0800495/*
496 * return 1 - bad block
497 * return 0 - read successfully
498 * return < 0 - read failed
499 */
500static int read_fcb(struct boot_config *boot_cfg, struct fcb_block *fcb,
501 loff_t off)
Igor Opaniuk1b899a82019-11-03 16:49:45 +0100502{
Han Xu214b7d52020-05-05 22:04:03 +0800503 struct mtd_info *mtd;
504 void *fcb_raw_page;
505 size_t size;
506 int ret = 0;
507
508 mtd = boot_cfg->mtd;
Han Xu214b7d52020-05-05 22:04:03 +0800509 if (mtd_block_isbad(mtd, off)) {
510 printf("Block %d is bad, skipped\n", (int)CONV_TO_BLOCKS(off));
511 return 1;
512 }
513
Ye Lif637c402020-08-02 22:43:45 -0700514 fcb_raw_page = kzalloc(mtd->writesize + mtd->oobsize, GFP_KERNEL);
515 if (!fcb_raw_page) {
516 debug("failed to allocate fcb_raw_page\n");
517 ret = -ENOMEM;
518 return ret;
519 }
520
Han Xu214b7d52020-05-05 22:04:03 +0800521 /*
522 * User BCH hardware to decode ECC for FCB
523 */
524 if (plat_config.misc_flags & FCB_ENCODE_BCH) {
525 size = sizeof(struct fcb_block);
526
527 /* switch nand BCH to FCB compatible settings */
528 if (plat_config.misc_flags & FCB_ENCODE_BCH_62b)
529 mxs_nand_mode_fcb_62bit(mtd);
530 else if (plat_config.misc_flags & FCB_ENCODE_BCH_40b)
531 mxs_nand_mode_fcb_40bit(mtd);
532
533 ret = nand_read(mtd, off, &size, (u_char *)fcb);
534
535 /* switch BCH back */
536 mxs_nand_mode_normal(mtd);
537 printf("NAND FCB read from 0x%llx offset 0x%zx read: %s\n",
538 off, size, ret ? "ERROR" : "OK");
539
540 } else if (plat_config.misc_flags & FCB_ENCODE_HAMMING) {
541 /* raw read*/
542 mtd_oob_ops_t ops = {
543 .datbuf = (u8 *)fcb_raw_page,
544 .oobbuf = ((u8 *)fcb_raw_page) + mtd->writesize,
545 .len = mtd->writesize,
546 .ooblen = mtd->oobsize,
547 .mode = MTD_OPS_RAW
548 };
549
550 ret = mtd_read_oob(mtd, off, &ops);
551 printf("NAND FCB read from 0x%llx offset 0x%zx read: %s\n",
552 off, ops.len, ret ? "ERROR" : "OK");
553 }
554
555 if (ret)
556 goto fcb_raw_page_err;
557
558 if ((plat_config.misc_flags & FCB_ENCODE_HAMMING) &&
559 (plat_config.misc_flags & FCB_LAYOUT_RESV_12B))
560 memcpy(fcb, fcb_raw_page + 12, sizeof(struct fcb_block));
561
562/* TODO: check if it can pass Hamming check */
563
564fcb_raw_page_err:
565 kfree(fcb_raw_page);
566
567 return ret;
568}
569
570static int write_fcb(struct boot_config *boot_cfg, struct fcb_block *fcb)
571{
572 struct mtd_info *mtd;
573 void *fcb_raw_page = NULL;
Ye Li9dd599a2020-08-02 20:55:17 -0700574 int i, ret = 0;
Han Xu214b7d52020-05-05 22:04:03 +0800575 loff_t off;
576 size_t size;
577
578 mtd = boot_cfg->mtd;
Igor Opaniuk1b899a82019-11-03 16:49:45 +0100579
580 /*
581 * We prepare raw page only for i.MX6, for i.MX7 we
582 * leverage BCH hw module instead
583 */
Han Xu214b7d52020-05-05 22:04:03 +0800584 if ((plat_config.misc_flags & FCB_ENCODE_HAMMING) &&
585 (plat_config.misc_flags & FCB_LAYOUT_RESV_12B)) {
Igor Opaniuk1b899a82019-11-03 16:49:45 +0100586 fcb_raw_page = kzalloc(mtd->writesize + mtd->oobsize,
587 GFP_KERNEL);
588 if (!fcb_raw_page) {
589 debug("failed to allocate fcb_raw_page\n");
590 ret = -ENOMEM;
591 return ret;
592 }
593
594#if defined(CONFIG_MX6UL) || defined(CONFIG_MX6ULL)
595 /* 40 bit BCH, for i.MX6UL(L) */
596 encode_bch_ecc(fcb_raw_page + 32, fcb, 40);
597#else
598 memcpy(fcb_raw_page + 12, fcb, sizeof(struct fcb_block));
599 encode_hamming_13_8(fcb_raw_page + 12,
600 fcb_raw_page + 12 + 512, 512);
601#endif
602 /*
603 * Set the first and second byte of OOB data to 0xFF,
604 * not 0x00. These bytes are used as the Manufacturers Bad
605 * Block Marker (MBBM). Since the FCB is mostly written to
606 * the first page in a block, a scan for
607 * factory bad blocks will detect these blocks as bad, e.g.
608 * when function nand_scan_bbt() is executed to build a new
609 * bad block table.
610 */
611 memset(fcb_raw_page + mtd->writesize, 0xFF, 2);
612 }
Han Xu214b7d52020-05-05 22:04:03 +0800613
614 /* start writing FCB from the very beginning */
615 off = 0;
616
617 for (i = 0; i < g_boot_search_count; i++) {
Igor Opaniuk1b899a82019-11-03 16:49:45 +0100618 if (mtd_block_isbad(mtd, off)) {
619 printf("Block %d is bad, skipped\n", i);
620 continue;
621 }
622
623 /*
Han Xu214b7d52020-05-05 22:04:03 +0800624 * User BCH hardware module to generate ECC for FCB
Igor Opaniuk1b899a82019-11-03 16:49:45 +0100625 */
Han Xu214b7d52020-05-05 22:04:03 +0800626 if (plat_config.misc_flags & FCB_ENCODE_BCH) {
627 size = sizeof(struct fcb_block);
Igor Opaniuk1b899a82019-11-03 16:49:45 +0100628
629 /* switch nand BCH to FCB compatible settings */
Han Xu214b7d52020-05-05 22:04:03 +0800630 if (plat_config.misc_flags & FCB_ENCODE_BCH_62b)
631 mxs_nand_mode_fcb_62bit(mtd);
632 else if (plat_config.misc_flags & FCB_ENCODE_BCH_40b)
633 mxs_nand_mode_fcb_40bit(mtd);
Alice Guo0b103372020-05-05 22:04:01 +0800634
Han Xu214b7d52020-05-05 22:04:03 +0800635 ret = nand_write(mtd, off, &size, (u_char *)fcb);
Alice Guo0b103372020-05-05 22:04:01 +0800636
Han Xu214b7d52020-05-05 22:04:03 +0800637 /* switch BCH back */
Igor Opaniuk1b899a82019-11-03 16:49:45 +0100638 mxs_nand_mode_normal(mtd);
Han Xu214b7d52020-05-05 22:04:03 +0800639 printf("NAND FCB write to 0x%zx offset 0x%llx written: %s\n",
640 size, off, ret ? "ERROR" : "OK");
Igor Opaniuk1b899a82019-11-03 16:49:45 +0100641
Han Xu214b7d52020-05-05 22:04:03 +0800642 } else if (plat_config.misc_flags & FCB_ENCODE_HAMMING) {
Igor Opaniuk1b899a82019-11-03 16:49:45 +0100643 /* raw write */
644 mtd_oob_ops_t ops = {
645 .datbuf = (u8 *)fcb_raw_page,
646 .oobbuf = ((u8 *)fcb_raw_page) +
647 mtd->writesize,
648 .len = mtd->writesize,
649 .ooblen = mtd->oobsize,
650 .mode = MTD_OPS_RAW
651 };
652
Han Xu214b7d52020-05-05 22:04:03 +0800653 ret = mtd_write_oob(mtd, off, &ops);
Pali Roháraf602632021-10-20 11:13:15 +0200654 printf("NAND FCB write to 0x%llx offset 0x%zx written: %s\n", off, ops.len, ret ? "ERROR" : "OK");
Igor Opaniuk1b899a82019-11-03 16:49:45 +0100655 }
656
Igor Opaniuk1b899a82019-11-03 16:49:45 +0100657 if (ret)
658 goto fcb_raw_page_err;
Igor Opaniuk1b899a82019-11-03 16:49:45 +0100659
Han Xu214b7d52020-05-05 22:04:03 +0800660 /* next writing location */
661 off += g_boot_search_stride;
Igor Opaniuk1b899a82019-11-03 16:49:45 +0100662 }
663
664fcb_raw_page_err:
Han Xu214b7d52020-05-05 22:04:03 +0800665 kfree(fcb_raw_page);
Igor Opaniuk1b899a82019-11-03 16:49:45 +0100666
667 return ret;
668}
669
Han Xu214b7d52020-05-05 22:04:03 +0800670/*
671 * return 1 - bad block
672 * return 0 - read successfully
673 * return < 0 - read failed
674 */
675static int read_dbbt(struct boot_config *boot_cfg, struct dbbt_block *dbbt,
676 void *dbbt_data_page, loff_t off)
Shyam Saini1d43e242019-06-14 13:05:33 +0530677{
Han Xu214b7d52020-05-05 22:04:03 +0800678 size_t size;
679 struct mtd_info *mtd;
680 loff_t to;
681 int ret;
682
683 mtd = boot_cfg->mtd;
684
685 if (mtd_block_isbad(mtd, off)) {
686 printf("Block %d is bad, skipped\n",
687 (int)CONV_TO_BLOCKS(off));
688 return 1;
689 }
690
691 size = sizeof(struct dbbt_block);
692 ret = nand_read(mtd, off, &size, (u_char *)dbbt);
693 printf("NAND DBBT read from 0x%llx offset 0x%zx read: %s\n",
694 off, size, ret ? "ERROR" : "OK");
695 if (ret)
696 return ret;
697
698 /* dbbtpages == 0 if no bad blocks */
699 if (dbbt->dbbtpages > 0) {
700 to = off + 4 * mtd->writesize;
701 size = mtd->writesize;
702 ret = nand_read(mtd, to, &size, dbbt_data_page);
703 printf("DBBT data read from 0x%llx offset 0x%zx read: %s\n",
704 to, size, ret ? "ERROR" : "OK");
705
706 if (ret)
707 return ret;
708 }
709
710 return 0;
711}
712
713static int write_dbbt(struct boot_config *boot_cfg, struct dbbt_block *dbbt,
714 void *dbbt_data_page)
715{
716 int i;
717 loff_t off, to;
718 size_t size;
719 struct mtd_info *mtd;
720 int ret;
721
722 mtd = boot_cfg->mtd;
723
724 /* start writing DBBT after all FCBs */
725 off = boot_cfg->search_area_size_in_bytes;
726 size = mtd->writesize;
727
728 for (i = 0; i < g_boot_search_count; i++) {
729 if (mtd_block_isbad(mtd, off)) {
730 printf("Block %d is bad, skipped\n",
731 (int)(i + CONV_TO_BLOCKS(off)));
732 continue;
733 }
734
735 ret = nand_write(mtd, off, &size, (u_char *)dbbt);
736 printf("NAND DBBT write to 0x%llx offset 0x%zx written: %s\n",
737 off, size, ret ? "ERROR" : "OK");
738 if (ret)
739 return ret;
740
741 /* dbbtpages == 0 if no bad blocks */
742 if (dbbt->dbbtpages > 0) {
743 to = off + 4 * mtd->writesize;
744 ret = nand_write(mtd, to, &size, dbbt_data_page);
745 printf("DBBT data write to 0x%llx offset 0x%zx written: %s\n",
746 to, size, ret ? "ERROR" : "OK");
747
748 if (ret)
749 return ret;
750 }
751
752 /* next writing location */
753 off += g_boot_search_stride;
754 }
755
756 return 0;
757}
758
759/* reuse the check_skip_len from nand_util.c with minor change*/
760static int check_skip_length(struct boot_config *boot_cfg, loff_t offset,
761 size_t length, size_t *used)
762{
763 struct mtd_info *mtd = boot_cfg->mtd;
764 size_t maxsize = boot_cfg->maxsize;
765 size_t len_excl_bad = 0;
766 int ret = 0;
767
768 while (len_excl_bad < length) {
769 size_t block_len, block_off;
770 loff_t block_start;
771
772 if (offset >= maxsize)
773 return -1;
774
775 block_start = offset & ~(loff_t)(mtd->erasesize - 1);
776 block_off = offset & (mtd->erasesize - 1);
777 block_len = mtd->erasesize - block_off;
778
779 if (!nand_block_isbad(mtd, block_start))
780 len_excl_bad += block_len;
781 else
782 ret = 1;
783
784 offset += block_len;
785 *used += block_len;
786 }
787
788 /* If the length is not a multiple of block_len, adjust. */
789 if (len_excl_bad > length)
790 *used -= (len_excl_bad - length);
791
792 return ret;
793}
794
795static int nandbcb_get_next_good_blk_addr(struct boot_config *boot_cfg,
796 struct boot_stream_config *bs_cfg)
797{
798 struct mtd_info *mtd = boot_cfg->mtd;
799 loff_t offset = bs_cfg->bs_addr;
800 size_t length = bs_cfg->bs_size;
801 size_t used = 0;
802 int ret;
803
804 ret = check_skip_length(boot_cfg, offset, length, &used);
805
806 if (ret < 0)
807 return ret;
808
809 /* get next image address */
810 bs_cfg->next_bs_addr = (u32)(offset + used + mtd->erasesize - 1)
811 / (u32)mtd->erasesize * mtd->erasesize;
812
813 return ret;
814}
815
816static int nandbcb_write_bs_skip_bad(struct boot_config *boot_cfg,
817 struct boot_stream_config *bs_cfg)
818{
819 struct mtd_info *mtd;
820 void *buf;
821 loff_t offset, maxsize;
822 size_t size;
823 size_t length;
824 int ret;
825 bool padding_flag = false;
826
827 mtd = boot_cfg->mtd;
828 offset = bs_cfg->bs_addr;
829 maxsize = boot_cfg->maxsize;
830 size = bs_cfg->bs_size;
831
832 /* some boot images may need leading offset */
833 if (bs_cfg->need_padding &&
834 (plat_config.misc_flags & FIRMWARE_NEED_PADDING))
835 padding_flag = 1;
836
837 if (padding_flag)
838 length = ALIGN(size + FLASH_OFFSET_STANDARD, mtd->writesize);
839 else
840 length = ALIGN(size, mtd->writesize);
841
842 buf = kzalloc(length, GFP_KERNEL);
843 if (!buf) {
844 printf("failed to allocate buffer for firmware\n");
845 ret = -ENOMEM;
846 return ret;
847 }
848
849 if (padding_flag)
850 memcpy(buf + FLASH_OFFSET_STANDARD, bs_cfg->bs_buf, size);
851 else
852 memcpy(buf, bs_cfg->bs_buf, size);
853
854 ret = nand_write_skip_bad(mtd, offset, &length, NULL, maxsize,
855 (u_char *)buf, WITH_WR_VERIFY);
856 printf("Write %s @0x%llx offset, 0x%zx bytes written: %s\n",
857 bs_cfg->bs_label, offset, length, ret ? "ERROR" : "OK");
858
859 if (ret)
860 /* write image failed, quit */
861 goto err;
862
863 /* get next good blk address if needed */
864 if (bs_cfg->need_padding) {
865 ret = nandbcb_get_next_good_blk_addr(boot_cfg, bs_cfg);
866 if (ret < 0) {
867 printf("Next image cannot fit in NAND partition\n");
868 goto err;
869 }
870 }
871
872 /* now we know how the exact image size written to NAND */
873 bs_cfg->bs_size = length;
874 return 0;
875err:
876 kfree(buf);
877 return ret;
878}
879
880static int nandbcb_write_fw(struct boot_config *boot_cfg, u_char *buf,
881 int index)
882{
883 int i;
884 loff_t offset;
885 size_t size;
886 loff_t next_bs_addr;
887 struct boot_stream_config bs_cfg;
888 int ret;
889
890 for (i = 0; i < 2; ++i) {
891 if (!(FW_INX(i) & index))
892 continue;
893
894 if (i == 0) {
895 offset = boot_cfg->boot_stream1_address;
896 size = boot_cfg->boot_stream1_size;
897 } else {
898 offset = boot_cfg->boot_stream2_address;
899 size = boot_cfg->boot_stream2_size;
900 }
901
902 /* write Firmware*/
903 if (!(plat_config.misc_flags & FIRMWARE_EXTRA_ONE)) {
904 memset(&bs_cfg, 0, sizeof(struct boot_stream_config));
905 sprintf(bs_cfg.bs_label, "firmware%d", i);
906 bs_cfg.bs_addr = offset;
907 bs_cfg.bs_size = size;
908 bs_cfg.bs_buf = buf;
909 bs_cfg.need_padding = 1;
910
911 ret = nandbcb_write_bs_skip_bad(boot_cfg, &bs_cfg);
912 if (ret)
913 return ret;
914
915 /* update the boot stream size */
916 if (i == 0)
917 boot_cfg->boot_stream1_size = bs_cfg.bs_size;
918 else
919 boot_cfg->boot_stream2_size = bs_cfg.bs_size;
920
921 } else {
922 /* some platforms need extra firmware */
923 memset(&bs_cfg, 0, sizeof(struct boot_stream_config));
924 sprintf(bs_cfg.bs_label, "fw%d_part%d", i, 1);
925 bs_cfg.bs_addr = offset;
926 bs_cfg.bs_size = IMX8MQ_HDMI_FW_SZ;
927 bs_cfg.bs_buf = buf;
928 bs_cfg.need_padding = 1;
929
930 ret = nandbcb_write_bs_skip_bad(boot_cfg, &bs_cfg);
931 if (ret)
932 return ret;
933
934 /* update the boot stream size */
935 if (i == 0)
936 boot_cfg->boot_stream1_size = bs_cfg.bs_size;
937 else
938 boot_cfg->boot_stream2_size = bs_cfg.bs_size;
939
940 /* get next image address */
941 next_bs_addr = bs_cfg.next_bs_addr;
942
943 memset(&bs_cfg, 0, sizeof(struct boot_stream_config));
944 sprintf(bs_cfg.bs_label, "fw%d_part%d", i, 2);
945 bs_cfg.bs_addr = next_bs_addr;
946 bs_cfg.bs_size = IMX8MQ_SPL_SZ;
947 bs_cfg.bs_buf = (u_char *)(buf + IMX8MQ_HDMI_FW_SZ);
948 bs_cfg.need_padding = 0;
949
950 ret = nandbcb_write_bs_skip_bad(boot_cfg, &bs_cfg);
951 if (ret)
952 return ret;
953 }
954 }
955
956 return 0;
957}
958
959static int nandbcb_init(struct boot_config *boot_cfg, u_char *buf)
960{
961 struct mtd_info *mtd;
Shyam Saini1d43e242019-06-14 13:05:33 +0530962 nand_erase_options_t opts;
963 struct fcb_block *fcb;
964 struct dbbt_block *dbbt;
Han Xu214b7d52020-05-05 22:04:03 +0800965 void *dbbt_page, *dbbt_data_page;
Igor Opaniuk1b899a82019-11-03 16:49:45 +0100966 int ret;
Han Xu214b7d52020-05-05 22:04:03 +0800967 loff_t maxsize, off;
968
969 mtd = boot_cfg->mtd;
970 maxsize = boot_cfg->maxsize;
971 off = boot_cfg->offset;
Shyam Saini1d43e242019-06-14 13:05:33 +0530972
973 /* erase */
974 memset(&opts, 0, sizeof(opts));
975 opts.offset = off;
976 opts.length = maxsize - 1;
977 ret = nand_erase_opts(mtd, &opts);
978 if (ret) {
979 printf("%s: erase failed (ret = %d)\n", __func__, ret);
980 return ret;
981 }
982
983 /*
984 * Reference documentation from i.MX6DQRM section 8.5.2.2
985 *
986 * Nand Boot Control Block(BCB) contains two data structures,
987 * - Firmware Configuration Block(FCB)
988 * - Discovered Bad Block Table(DBBT)
989 *
990 * FCB contains,
991 * - nand timings
992 * - DBBT search page address,
993 * - start page address of primary firmware
994 * - start page address of secondary firmware
995 *
996 * setup fcb:
997 * - number of blocks = mtd partition size / mtd erasesize
998 * - two firmware blocks, primary and secondary
999 * - first 4 block for FCB/DBBT
1000 * - rest split in half for primary and secondary firmware
Han Xu214b7d52020-05-05 22:04:03 +08001001 * - same firmware write twice
Shyam Saini1d43e242019-06-14 13:05:33 +05301002 */
Shyam Saini1d43e242019-06-14 13:05:33 +05301003
Han Xu214b7d52020-05-05 22:04:03 +08001004 /* write Firmware*/
1005 ret = nandbcb_write_fw(boot_cfg, buf, FW_ALL);
1006 if (ret)
1007 goto err;
Shyam Saini1d43e242019-06-14 13:05:33 +05301008
1009 /* fill fcb */
1010 fcb = kzalloc(sizeof(*fcb), GFP_KERNEL);
1011 if (!fcb) {
1012 debug("failed to allocate fcb\n");
1013 ret = -ENOMEM;
Han Xu214b7d52020-05-05 22:04:03 +08001014 return ret;
Shyam Saini1d43e242019-06-14 13:05:33 +05301015 }
Han Xu214b7d52020-05-05 22:04:03 +08001016 fill_fcb(fcb, boot_cfg);
Shyam Saini1d43e242019-06-14 13:05:33 +05301017
Han Xu214b7d52020-05-05 22:04:03 +08001018 ret = write_fcb(boot_cfg, fcb);
Alice Guo0b103372020-05-05 22:04:01 +08001019
Shyam Saini1d43e242019-06-14 13:05:33 +05301020 /* fill dbbt */
1021 dbbt_page = kzalloc(mtd->writesize, GFP_KERNEL);
1022 if (!dbbt_page) {
1023 debug("failed to allocate dbbt_page\n");
1024 ret = -ENOMEM;
1025 goto fcb_err;
1026 }
1027
1028 dbbt_data_page = kzalloc(mtd->writesize, GFP_KERNEL);
1029 if (!dbbt_data_page) {
1030 debug("failed to allocate dbbt_data_page\n");
1031 ret = -ENOMEM;
1032 goto dbbt_page_err;
1033 }
1034
1035 dbbt = dbbt_page;
1036 dbbt->checksum = 0;
Han Xu214b7d52020-05-05 22:04:03 +08001037 dbbt->fingerprint = DBBT_FINGERPRINT;
Shyam Saini1d43e242019-06-14 13:05:33 +05301038 dbbt->version = DBBT_VERSION_1;
Han Xu214b7d52020-05-05 22:04:03 +08001039 ret = fill_dbbt_data(mtd, dbbt_data_page, CONV_TO_BLOCKS(maxsize));
Shyam Saini1d43e242019-06-14 13:05:33 +05301040 if (ret < 0)
1041 goto dbbt_data_page_err;
1042 else if (ret > 0)
1043 dbbt->dbbtpages = 1;
1044
Han Xu214b7d52020-05-05 22:04:03 +08001045 /* write dbbt */
1046 ret = write_dbbt(boot_cfg, dbbt, dbbt_data_page);
Igor Opaniuk1b899a82019-11-03 16:49:45 +01001047 if (ret < 0)
1048 printf("failed to write FCB/DBBT\n");
Shyam Saini1d43e242019-06-14 13:05:33 +05301049
Shyam Saini1d43e242019-06-14 13:05:33 +05301050dbbt_data_page_err:
1051 kfree(dbbt_data_page);
1052dbbt_page_err:
1053 kfree(dbbt_page);
1054fcb_err:
1055 kfree(fcb);
Shyam Saini1d43e242019-06-14 13:05:33 +05301056err:
1057 return ret;
1058}
1059
Simon Glass09140112020-05-10 11:40:03 -06001060static int do_nandbcb_bcbonly(int argc, char *const argv[])
Igor Opaniukae8a53e2019-11-03 16:49:46 +01001061{
1062 struct fcb_block *fcb;
1063 struct dbbt_block *dbbt;
Igor Opaniukae8a53e2019-11-03 16:49:46 +01001064 struct mtd_info *mtd;
Han Xu214b7d52020-05-05 22:04:03 +08001065 nand_erase_options_t opts;
1066 size_t maxsize;
1067 loff_t off;
Igor Opaniukae8a53e2019-11-03 16:49:46 +01001068 void *dbbt_page, *dbbt_data_page;
Han Xu214b7d52020-05-05 22:04:03 +08001069 int ret;
1070 struct boot_config cfg;
Igor Opaniukae8a53e2019-11-03 16:49:46 +01001071
Han Xu214b7d52020-05-05 22:04:03 +08001072 if (argc < 4)
1073 return CMD_RET_USAGE;
1074
1075 memset(&cfg, 0, sizeof(struct boot_config));
1076 if (nandbcb_get_info(argc, argv, &cfg))
Igor Opaniukae8a53e2019-11-03 16:49:46 +01001077 return CMD_RET_FAILURE;
Han Xu214b7d52020-05-05 22:04:03 +08001078
1079 /* only get the partition info */
1080 if (nandbcb_get_size(2, argv, 1, &cfg))
1081 return CMD_RET_FAILURE;
1082
1083 if (nandbcb_set_boot_config(argc, argv, &cfg))
1084 return CMD_RET_FAILURE;
1085
1086 mtd = cfg.mtd;
1087
Simon Glass7e5f4602021-07-24 09:03:29 -06001088 cfg.boot_stream1_address = hextoul(argv[2], NULL);
1089 cfg.boot_stream1_size = hextoul(argv[3], NULL);
Han Xu214b7d52020-05-05 22:04:03 +08001090 cfg.boot_stream1_size = ALIGN(cfg.boot_stream1_size, mtd->writesize);
1091
1092 if (argc > 5) {
Simon Glass7e5f4602021-07-24 09:03:29 -06001093 cfg.boot_stream2_address = hextoul(argv[4], NULL);
1094 cfg.boot_stream2_size = hextoul(argv[5], NULL);
Han Xu214b7d52020-05-05 22:04:03 +08001095 cfg.boot_stream2_size = ALIGN(cfg.boot_stream2_size,
1096 mtd->writesize);
Igor Opaniukae8a53e2019-11-03 16:49:46 +01001097 }
1098
Han Xu214b7d52020-05-05 22:04:03 +08001099 /* sanity check */
1100 nandbcb_check_space(&cfg);
Igor Opaniukae8a53e2019-11-03 16:49:46 +01001101
Han Xu214b7d52020-05-05 22:04:03 +08001102 maxsize = cfg.maxsize;
1103 off = cfg.offset;
1104
1105 /* erase the previous FCB/DBBT */
1106 memset(&opts, 0, sizeof(opts));
1107 opts.offset = off;
1108 opts.length = g_boot_search_stride * 2;
1109 ret = nand_erase_opts(mtd, &opts);
1110 if (ret) {
1111 printf("%s: erase failed (ret = %d)\n", __func__, ret);
Igor Opaniukae8a53e2019-11-03 16:49:46 +01001112 return CMD_RET_FAILURE;
Han Xu214b7d52020-05-05 22:04:03 +08001113 }
Igor Opaniukae8a53e2019-11-03 16:49:46 +01001114
1115 /* fill fcb */
1116 fcb = kzalloc(sizeof(*fcb), GFP_KERNEL);
1117 if (!fcb) {
Han Xu214b7d52020-05-05 22:04:03 +08001118 printf("failed to allocate fcb\n");
Igor Opaniukae8a53e2019-11-03 16:49:46 +01001119 ret = -ENOMEM;
1120 return CMD_RET_FAILURE;
1121 }
1122
Han Xu214b7d52020-05-05 22:04:03 +08001123 fill_fcb(fcb, &cfg);
1124
1125 /* write fcb */
1126 ret = write_fcb(&cfg, fcb);
Igor Opaniukae8a53e2019-11-03 16:49:46 +01001127
1128 /* fill dbbt */
1129 dbbt_page = kzalloc(mtd->writesize, GFP_KERNEL);
1130 if (!dbbt_page) {
Han Xu214b7d52020-05-05 22:04:03 +08001131 printf("failed to allocate dbbt_page\n");
Igor Opaniukae8a53e2019-11-03 16:49:46 +01001132 ret = -ENOMEM;
1133 goto fcb_err;
1134 }
1135
1136 dbbt_data_page = kzalloc(mtd->writesize, GFP_KERNEL);
1137 if (!dbbt_data_page) {
Han Xu214b7d52020-05-05 22:04:03 +08001138 printf("failed to allocate dbbt_data_page\n");
Igor Opaniukae8a53e2019-11-03 16:49:46 +01001139 ret = -ENOMEM;
1140 goto dbbt_page_err;
1141 }
1142
1143 dbbt = dbbt_page;
1144 dbbt->checksum = 0;
Han Xu214b7d52020-05-05 22:04:03 +08001145 dbbt->fingerprint = DBBT_FINGERPRINT;
Igor Opaniukae8a53e2019-11-03 16:49:46 +01001146 dbbt->version = DBBT_VERSION_1;
Han Xu214b7d52020-05-05 22:04:03 +08001147 ret = fill_dbbt_data(mtd, dbbt_data_page, CONV_TO_BLOCKS(maxsize));
Igor Opaniukae8a53e2019-11-03 16:49:46 +01001148 if (ret < 0)
1149 goto dbbt_data_page_err;
1150 else if (ret > 0)
1151 dbbt->dbbtpages = 1;
1152
Han Xu214b7d52020-05-05 22:04:03 +08001153 /* write dbbt */
1154 ret = write_dbbt(&cfg, dbbt, dbbt_data_page);
1155
Igor Opaniukae8a53e2019-11-03 16:49:46 +01001156dbbt_data_page_err:
1157 kfree(dbbt_data_page);
1158dbbt_page_err:
1159 kfree(dbbt_page);
1160fcb_err:
1161 kfree(fcb);
1162
1163 if (ret < 0) {
1164 printf("failed to write FCB/DBBT\n");
1165 return CMD_RET_FAILURE;
1166 }
1167
1168 return CMD_RET_SUCCESS;
1169}
1170
Alice Guo0b103372020-05-05 22:04:01 +08001171/* dump data which is read from NAND chip */
Han Xu214b7d52020-05-05 22:04:03 +08001172void dump_structure(struct boot_config *boot_cfg, struct fcb_block *fcb,
1173 struct dbbt_block *dbbt, void *dbbt_data_page)
Alice Guo0b103372020-05-05 22:04:01 +08001174{
Han Xu214b7d52020-05-05 22:04:03 +08001175 int i;
1176 struct mtd_info *mtd = boot_cfg->mtd;
1177
1178 #define P1(x) printf(" %s = 0x%08x\n", #x, fcb->x)
1179 printf("FCB\n");
Alice Guo0b103372020-05-05 22:04:01 +08001180 P1(checksum);
1181 P1(fingerprint);
1182 P1(version);
1183 #undef P1
Han Xu214b7d52020-05-05 22:04:03 +08001184 #define P1(x) printf(" %s = %d\n", #x, fcb->x)
Alice Guo0b103372020-05-05 22:04:01 +08001185 P1(datasetup);
1186 P1(datahold);
1187 P1(addr_setup);
1188 P1(dsample_time);
1189 P1(pagesize);
1190 P1(oob_pagesize);
1191 P1(sectors);
1192 P1(nr_nand);
1193 P1(nr_die);
1194 P1(celltype);
1195 P1(ecc_type);
1196 P1(ecc_nr);
1197 P1(ecc_size);
1198 P1(ecc_level);
1199 P1(meta_size);
1200 P1(nr_blocks);
1201 P1(ecc_type_sdk);
1202 P1(ecc_nr_sdk);
1203 P1(ecc_size_sdk);
1204 P1(ecc_level_sdk);
1205 P1(nr_blocks_sdk);
1206 P1(meta_size_sdk);
1207 P1(erase_th);
1208 P1(bootpatch);
1209 P1(patch_size);
1210 P1(fw1_start);
1211 P1(fw2_start);
1212 P1(fw1_pages);
1213 P1(fw2_pages);
1214 P1(dbbt_start);
1215 P1(bb_byte);
1216 P1(bb_start_bit);
1217 P1(phy_offset);
1218 P1(bchtype);
1219 P1(readlatency);
1220 P1(predelay);
1221 P1(cedelay);
1222 P1(postdelay);
1223 P1(cmdaddpause);
1224 P1(datapause);
1225 P1(tmspeed);
1226 P1(busytimeout);
1227 P1(disbbm);
1228 P1(spare_offset);
Han Xu214b7d52020-05-05 22:04:03 +08001229#if !defined(CONFIG_MX6) || defined(CONFIG_MX6SX) || \
1230 defined(CONFIG_MX6UL) || defined(CONFIG_MX6ULL)
Alice Guo0b103372020-05-05 22:04:01 +08001231 P1(onfi_sync_enable);
1232 P1(onfi_sync_speed);
1233 P1(onfi_sync_nand_data);
1234 P1(disbbm_search);
1235 P1(disbbm_search_limit);
1236 P1(read_retry_enable);
Han Xu214b7d52020-05-05 22:04:03 +08001237#endif
Alice Guo0b103372020-05-05 22:04:01 +08001238 #undef P1
Han Xu214b7d52020-05-05 22:04:03 +08001239 #define P1(x) printf(" %s = 0x%08x\n", #x, dbbt->x)
1240 printf("DBBT :\n");
Alice Guo0b103372020-05-05 22:04:01 +08001241 P1(checksum);
1242 P1(fingerprint);
1243 P1(version);
1244 #undef P1
Han Xu214b7d52020-05-05 22:04:03 +08001245 #define P1(x) printf(" %s = %d\n", #x, dbbt->x)
1246 P1(dbbtpages);
Alice Guo0b103372020-05-05 22:04:01 +08001247 #undef P1
1248
Han Xu214b7d52020-05-05 22:04:03 +08001249 for (i = 0; i < dbbt->dbbtpages; ++i)
1250 printf("%d ", *((u32 *)(dbbt_data_page + i)));
1251
1252 if (!(plat_config.misc_flags & FIRMWARE_EXTRA_ONE)) {
1253 printf("Firmware: image #0 @ 0x%x size 0x%x\n",
1254 fcb->fw1_start, fcb->fw1_pages * mtd->writesize);
1255 printf("Firmware: image #1 @ 0x%x size 0x%x\n",
1256 fcb->fw2_start, fcb->fw2_pages * mtd->writesize);
1257 } else {
1258 printf("Firmware: image #0 @ 0x%x size 0x%x\n",
1259 fcb->fw1_start, fcb->fw1_pages * mtd->writesize);
1260 printf("Firmware: image #1 @ 0x%x size 0x%x\n",
1261 fcb->fw2_start, fcb->fw2_pages * mtd->writesize);
1262 /* TODO: Add extra image information */
Alice Guo0b103372020-05-05 22:04:01 +08001263 }
1264}
1265
Han Xu214b7d52020-05-05 22:04:03 +08001266static bool check_fingerprint(void *data, int fingerprint)
1267{
1268 int off = 4;
1269
1270 return (*(int *)(data + off) == fingerprint);
1271}
1272
Michael Trimarchi02737ac2022-01-07 18:27:17 +01001273static int fuse_secondary_boot(u32 bank, u32 word, u32 mask, u32 off)
1274{
1275 int err;
1276 u32 val;
1277 int ret;
1278
1279 err = fuse_read(bank, word, &val);
1280 if (err)
1281 return 0;
1282
1283 val = (val & mask) >> off;
1284
1285 if (val > 10)
1286 return 0;
1287
1288 switch (val) {
1289 case 0:
1290 ret = 4;
1291 break;
1292 case 1:
1293 ret = 1;
1294 break;
1295 default:
1296 ret = 2 << val;
1297 break;
1298 }
1299
1300 return ret;
1301};
1302
Han Xuf797fe82020-05-05 22:04:04 +08001303static int fuse_to_search_count(u32 bank, u32 word, u32 mask, u32 off)
1304{
1305 int err;
1306 u32 val;
1307 int ret;
1308
1309 /* by default, the boot search count from fuse should be 2 */
1310 err = fuse_read(bank, word, &val);
1311 if (err)
1312 return 2;
1313
1314 val = (val & mask) >> off;
1315
1316 switch (val) {
1317 case 0:
1318 ret = 2;
1319 break;
1320 case 1:
1321 case 2:
1322 case 3:
1323 ret = 1 << val;
1324 break;
1325 default:
1326 ret = 2;
1327 }
1328
1329 return ret;
1330}
1331
Han Xu214b7d52020-05-05 22:04:03 +08001332static int nandbcb_dump(struct boot_config *boot_cfg)
1333{
1334 int i;
1335 loff_t off;
1336 struct mtd_info *mtd = boot_cfg->mtd;
1337 struct fcb_block fcb, fcb_copy;
1338 struct dbbt_block dbbt, dbbt_copy;
1339 void *dbbt_data_page, *dbbt_data_page_copy;
1340 bool fcb_not_found, dbbt_not_found;
1341 int ret = 0;
1342
1343 dbbt_data_page = kzalloc(mtd->writesize, GFP_KERNEL);
1344 if (!dbbt_data_page) {
1345 printf("failed to allocate dbbt_data_page\n");
1346 ret = -ENOMEM;
1347 return ret;
1348 }
1349
1350 dbbt_data_page_copy = kzalloc(mtd->writesize, GFP_KERNEL);
1351 if (!dbbt_data_page_copy) {
1352 printf("failed to allocate dbbt_data_page\n");
1353 ret = -ENOMEM;
1354 goto dbbt_page_err;
1355 }
1356
1357 /* read fcb */
1358 fcb_not_found = 1;
1359 off = 0;
1360 for (i = 0; i < g_boot_search_count; ++i) {
1361 if (fcb_not_found) {
1362 ret = read_fcb(boot_cfg, &fcb, off);
1363
1364 if (ret < 0)
1365 goto dbbt_page_copy_err;
1366 else if (ret == 1)
1367 continue;
1368 else if (ret == 0)
1369 if (check_fingerprint(&fcb, FCB_FINGERPRINT))
1370 fcb_not_found = 0;
1371 } else {
1372 ret = read_fcb(boot_cfg, &fcb_copy, off);
1373
1374 if (ret < 0)
1375 goto dbbt_page_copy_err;
1376 if (memcmp(&fcb, &fcb_copy,
1377 sizeof(struct fcb_block))) {
1378 printf("FCB copies are not identical\n");
1379 ret = -EINVAL;
1380 goto dbbt_page_copy_err;
1381 }
1382 }
1383
1384 /* next read location */
1385 off += g_boot_search_stride;
1386 }
1387
1388 /* read dbbt*/
1389 dbbt_not_found = 1;
1390 off = boot_cfg->search_area_size_in_bytes;
1391 for (i = 0; i < g_boot_search_count; ++i) {
1392 if (dbbt_not_found) {
1393 ret = read_dbbt(boot_cfg, &dbbt, dbbt_data_page, off);
1394
1395 if (ret < 0)
1396 goto dbbt_page_copy_err;
1397 else if (ret == 1)
1398 continue;
1399 else if (ret == 0)
1400 if (check_fingerprint(&dbbt, DBBT_FINGERPRINT))
1401 dbbt_not_found = 0;
1402 } else {
1403 ret = read_dbbt(boot_cfg, &dbbt_copy,
1404 dbbt_data_page_copy, off);
1405
1406 if (ret < 0)
1407 goto dbbt_page_copy_err;
1408 if (memcmp(&dbbt, &dbbt_copy,
1409 sizeof(struct dbbt_block))) {
1410 printf("DBBT copies are not identical\n");
1411 ret = -EINVAL;
1412 goto dbbt_page_copy_err;
1413 }
1414 if (dbbt.dbbtpages > 0 &&
1415 memcmp(dbbt_data_page, dbbt_data_page_copy,
1416 mtd->writesize)) {
1417 printf("DBBT data copies are not identical\n");
1418 ret = -EINVAL;
1419 goto dbbt_page_copy_err;
1420 }
1421 }
1422
1423 /* next read location */
1424 off += g_boot_search_stride;
1425 }
1426
1427 dump_structure(boot_cfg, &fcb, &dbbt, dbbt_data_page);
1428
1429dbbt_page_copy_err:
1430 kfree(dbbt_data_page_copy);
1431dbbt_page_err:
1432 kfree(dbbt_data_page);
1433
1434 return ret;
1435}
1436
Alice Guo0b103372020-05-05 22:04:01 +08001437static int do_nandbcb_dump(int argc, char * const argv[])
1438{
Han Xu214b7d52020-05-05 22:04:03 +08001439 struct boot_config cfg;
1440 int ret;
Alice Guo0b103372020-05-05 22:04:01 +08001441
1442 if (argc != 2)
1443 return CMD_RET_USAGE;
1444
Han Xu214b7d52020-05-05 22:04:03 +08001445 memset(&cfg, 0, sizeof(struct boot_config));
1446 if (nandbcb_get_info(argc, argv, &cfg))
1447 return CMD_RET_FAILURE;
Alice Guo0b103372020-05-05 22:04:01 +08001448
Han Xu214b7d52020-05-05 22:04:03 +08001449 if (nandbcb_get_size(argc, argv, 1, &cfg))
1450 return CMD_RET_FAILURE;
Alice Guo0b103372020-05-05 22:04:01 +08001451
Han Xu214b7d52020-05-05 22:04:03 +08001452 if (nandbcb_set_boot_config(argc, argv, &cfg))
1453 return CMD_RET_FAILURE;
Alice Guo0b103372020-05-05 22:04:01 +08001454
Han Xu214b7d52020-05-05 22:04:03 +08001455 ret = nandbcb_dump(&cfg);
1456 if (ret)
1457 return ret;
Alice Guo0b103372020-05-05 22:04:01 +08001458
Han Xu214b7d52020-05-05 22:04:03 +08001459 return ret;
Alice Guo0b103372020-05-05 22:04:01 +08001460}
1461
Han Xu214b7d52020-05-05 22:04:03 +08001462static int do_nandbcb_init(int argc, char * const argv[])
Shyam Saini1d43e242019-06-14 13:05:33 +05301463{
Shyam Saini1d43e242019-06-14 13:05:33 +05301464 u_char *buf;
Han Xu214b7d52020-05-05 22:04:03 +08001465 size_t size;
1466 loff_t addr;
1467 char *endp;
Shyam Saini1d43e242019-06-14 13:05:33 +05301468 int ret;
Han Xu214b7d52020-05-05 22:04:03 +08001469 struct boot_config cfg;
Shyam Saini1d43e242019-06-14 13:05:33 +05301470
1471 if (argc != 4)
1472 return CMD_RET_USAGE;
1473
Han Xu214b7d52020-05-05 22:04:03 +08001474 memset(&cfg, 0, sizeof(struct boot_config));
1475 if (nandbcb_get_info(argc, argv, &cfg))
Shyam Saini1d43e242019-06-14 13:05:33 +05301476 return CMD_RET_FAILURE;
Han Xu214b7d52020-05-05 22:04:03 +08001477
1478 if (nandbcb_get_size(argc, argv, 2, &cfg))
1479 return CMD_RET_FAILURE;
1480 size = cfg.boot_stream1_size;
1481
1482 if (nandbcb_set_boot_config(argc, argv, &cfg))
1483 return CMD_RET_FAILURE;
Shyam Saini1d43e242019-06-14 13:05:33 +05301484
Simon Glass7e5f4602021-07-24 09:03:29 -06001485 addr = hextoul(argv[1], &endp);
Shyam Saini1d43e242019-06-14 13:05:33 +05301486 if (*argv[1] == 0 || *endp != 0)
1487 return CMD_RET_FAILURE;
1488
Shyam Saini1d43e242019-06-14 13:05:33 +05301489 buf = map_physmem(addr, size, MAP_WRBACK);
1490 if (!buf) {
1491 puts("failed to map physical memory\n");
1492 return CMD_RET_FAILURE;
1493 }
1494
Han Xu214b7d52020-05-05 22:04:03 +08001495 ret = nandbcb_init(&cfg, buf);
Shyam Saini1d43e242019-06-14 13:05:33 +05301496
1497 return ret == 0 ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
1498}
1499
Simon Glass09140112020-05-10 11:40:03 -06001500static int do_nandbcb(struct cmd_tbl *cmdtp, int flag, int argc,
1501 char *const argv[])
Shyam Saini1d43e242019-06-14 13:05:33 +05301502{
1503 const char *cmd;
1504 int ret = 0;
1505
Alice Guo0b103372020-05-05 22:04:01 +08001506 if (argc < 3)
Shyam Saini1d43e242019-06-14 13:05:33 +05301507 goto usage;
1508
Han Xu214b7d52020-05-05 22:04:03 +08001509 /* check the platform config first */
1510 if (is_mx6sx()) {
1511 plat_config = imx6sx_plat_config;
1512 } else if (is_mx7()) {
1513 plat_config = imx7d_plat_config;
1514 } else if (is_mx6ul() || is_mx6ull()) {
1515 plat_config = imx6ul_plat_config;
1516 } else if (is_mx6() && !is_mx6sx() && !is_mx6ul() && !is_mx6ull()) {
1517 plat_config = imx6qdl_plat_config;
1518 } else if (is_imx8mq()) {
1519 plat_config = imx8mq_plat_config;
1520 } else if (is_imx8mm()) {
1521 plat_config = imx8mm_plat_config;
Han Xu42a49752020-10-10 08:48:49 -05001522 } else if (is_imx8mn() || is_imx8mp()) {
Han Xu214b7d52020-05-05 22:04:03 +08001523 plat_config = imx8mn_plat_config;
1524 } else if (is_imx8qm() || is_imx8qxp()) {
1525 plat_config = imx8q_plat_config;
1526 } else {
1527 printf("ERROR: Unknown platform\n");
1528 return CMD_RET_FAILURE;
1529 }
1530
Han Xu42a49752020-10-10 08:48:49 -05001531 if ((plat_config.misc_flags) & BT_SEARCH_CNT_FROM_FUSE) {
1532 if (is_imx8qxp())
1533 g_boot_search_count = fuse_to_search_count(0, 720, 0xc0, 6);
1534 if (is_imx8mn() || is_imx8mp())
1535 g_boot_search_count = fuse_to_search_count(2, 2, 0x6000, 13);
1536 printf("search count set to %d from fuse\n",
1537 g_boot_search_count);
Han Xuf797fe82020-05-05 22:04:04 +08001538 }
Han Xu214b7d52020-05-05 22:04:03 +08001539
Michael Trimarchi02737ac2022-01-07 18:27:17 +01001540 if (plat_config.misc_flags & FIRMWARE_SECONDARY_FIXED_ADDR) {
1541 if (is_imx8mn())
1542 g_boot_secondary_offset = fuse_secondary_boot(2, 1, 0xff0000, 16);
1543 }
1544
Shyam Saini1d43e242019-06-14 13:05:33 +05301545 cmd = argv[1];
1546 --argc;
1547 ++argv;
1548
Han Xu214b7d52020-05-05 22:04:03 +08001549 if (strcmp(cmd, "init") == 0) {
1550 ret = do_nandbcb_init(argc, argv);
Shyam Saini1d43e242019-06-14 13:05:33 +05301551 goto done;
1552 }
1553
Alice Guo0b103372020-05-05 22:04:01 +08001554 if (strcmp(cmd, "dump") == 0) {
1555 ret = do_nandbcb_dump(argc, argv);
1556 goto done;
1557 }
1558
Igor Opaniukae8a53e2019-11-03 16:49:46 +01001559 if (strcmp(cmd, "bcbonly") == 0) {
1560 ret = do_nandbcb_bcbonly(argc, argv);
1561 goto done;
1562 }
1563
Shyam Saini1d43e242019-06-14 13:05:33 +05301564done:
1565 if (ret != -1)
1566 return ret;
1567usage:
1568 return CMD_RET_USAGE;
1569}
1570
Parthiban Nallathambi4ee0ff12019-08-23 18:35:10 +02001571#ifdef CONFIG_SYS_LONGHELP
Shyam Saini1d43e242019-06-14 13:05:33 +05301572static char nandbcb_help_text[] =
Han Xu214b7d52020-05-05 22:04:03 +08001573 "init addr off|partition len - update 'len' bytes starting at\n"
Igor Opaniukae8a53e2019-11-03 16:49:46 +01001574 " 'off|part' to memory address 'addr', skipping bad blocks\n"
Han Xu214b7d52020-05-05 22:04:03 +08001575 "nandbcb bcbonly off|partition fw1-off fw1-size [fw2-off fw2-size]\n"
1576 " - write BCB only (FCB and DBBT)\n"
1577 " where `fwx-size` is fw sizes in bytes, `fw1-off`\n"
Igor Opaniuk061b63b2019-12-16 14:06:44 +02001578 " and `fw2-off` - firmware offsets\n"
1579 " FIY, BCB isn't erased automatically, so mtd erase should\n"
1580 " be called in advance before writing new BCB:\n"
Alice Guo0b103372020-05-05 22:04:01 +08001581 " > mtd erase mx7-bcb\n"
Han Xu214b7d52020-05-05 22:04:03 +08001582 "nandbcb dump off|partition - dump/verify boot structures\n";
Parthiban Nallathambi4ee0ff12019-08-23 18:35:10 +02001583#endif
Shyam Saini1d43e242019-06-14 13:05:33 +05301584
Han Xu214b7d52020-05-05 22:04:03 +08001585U_BOOT_CMD(nandbcb, 7, 1, do_nandbcb,
1586 "i.MX NAND Boot Control Blocks write",
Shyam Saini1d43e242019-06-14 13:05:33 +05301587 nandbcb_help_text
1588);