blob: c2e452b69270cf83011bfc1f876f7f363b7f7cfc [file] [log] [blame]
Shyam Sainif63ef492019-06-14 13:05:33 +05301/*
Han Xu1f314e52020-05-05 22:04:03 +08002 * i.MX nand boot control block(bcb).
Shyam Sainif63ef492019-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 Xu1f314e52020-05-05 22:04:03 +08009 * Reconstucted by Han Xu <han.xu@nxp.com>
10 *
Shyam Sainif63ef492019-06-14 13:05:33 +053011 * SPDX-License-Identifier: GPL-2.0+
12 */
13
Simon Glassed38aef2020-05-10 11:40:03 -060014#include <command.h>
Simon Glass0f2af882020-05-10 11:40:05 -060015#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070016#include <malloc.h>
Shyam Sainif63ef492019-06-14 13:05:33 +053017#include <nand.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070018#include <dm/devres.h>
Simon Glassc06c1be2020-05-10 11:40:08 -060019#include <linux/bug.h>
Shyam Sainif63ef492019-06-14 13:05:33 +053020
21#include <asm/io.h>
22#include <jffs2/jffs2.h>
Parthiban Nallathambia99188b2019-10-18 11:46:19 +020023#include <linux/bch.h>
Shyam Sainif63ef492019-06-14 13:05:33 +053024#include <linux/mtd/mtd.h>
Tom Rini3bde7e22021-09-22 14:50:35 -040025#include <linux/mtd/rawnand.h>
Shyam Sainif63ef492019-06-14 13:05:33 +053026
Igor Opaniuk344833a2019-11-03 16:49:44 +010027#include <asm/arch/sys_proto.h>
Shyam Sainif63ef492019-06-14 13:05:33 +053028#include <asm/mach-imx/imx-nandbcb.h>
29#include <asm/mach-imx/imximage.cfg>
30#include <mxs_nand.h>
31#include <linux/mtd/mtd.h>
32#include <nand.h>
Han Xu65781602020-05-05 22:04:04 +080033#include <fuse.h>
Shyam Sainif63ef492019-06-14 13:05:33 +053034
Miquel Raynal617fd792019-10-25 19:39:29 +020035#include "../../../cmd/legacy-mtd-utils.h"
36
Han Xu1f314e52020-05-05 22:04:03 +080037/* FCB related flags */
38/* FCB layout with leading 12B reserved */
39#define FCB_LAYOUT_RESV_12B BIT(0)
40/* FCB layout with leading 32B meta data */
41#define FCB_LAYOUT_META_32B BIT(1)
42/* FCB encrypted by Hamming code */
43#define FCB_ENCODE_HAMMING BIT(2)
44/* FCB encrypted by 40bit BCH */
45#define FCB_ENCODE_BCH_40b BIT(3)
46/* FCB encrypted by 62bit BCH */
47#define FCB_ENCODE_BCH_62b BIT(4)
48/* FCB encrypted by BCH */
49#define FCB_ENCODE_BCH (FCB_ENCODE_BCH_40b | FCB_ENCODE_BCH_62b)
50/* FCB data was randomized */
51#define FCB_RANDON_ENABLED BIT(5)
52
53/* Firmware related flags */
54/* No 1K padding */
55#define FIRMWARE_NEED_PADDING BIT(8)
56/* Extra firmware*/
57#define FIRMWARE_EXTRA_ONE BIT(9)
58/* Secondary firmware on fixed address */
59#define FIRMWARE_SECONDARY_FIXED_ADDR BIT(10)
60
61/* Boot search related flags */
62#define BT_SEARCH_CNT_FROM_FUSE BIT(16)
63
64struct platform_config {
65 int misc_flags;
66};
67
68static struct platform_config plat_config;
69
70/* imx6q/dl/solo */
71static struct platform_config imx6qdl_plat_config = {
72 .misc_flags = FCB_LAYOUT_RESV_12B |
73 FCB_ENCODE_HAMMING |
74 FIRMWARE_NEED_PADDING,
75};
76
77static struct platform_config imx6sx_plat_config = {
78 .misc_flags = FCB_LAYOUT_META_32B |
79 FCB_ENCODE_BCH_62b |
80 FIRMWARE_NEED_PADDING |
81 FCB_RANDON_ENABLED,
82};
83
84static struct platform_config imx7d_plat_config = {
85 .misc_flags = FCB_LAYOUT_META_32B |
86 FCB_ENCODE_BCH_62b |
87 FIRMWARE_NEED_PADDING |
88 FCB_RANDON_ENABLED,
89};
90
91/* imx6ul/ull/ulz */
92static struct platform_config imx6ul_plat_config = {
93 .misc_flags = FCB_LAYOUT_META_32B |
94 FCB_ENCODE_BCH_40b |
95 FIRMWARE_NEED_PADDING,
96};
97
98static struct platform_config imx8mq_plat_config = {
99 .misc_flags = FCB_LAYOUT_META_32B |
100 FCB_ENCODE_BCH_62b |
101 FIRMWARE_NEED_PADDING |
102 FCB_RANDON_ENABLED |
103 FIRMWARE_EXTRA_ONE,
104};
105
106/* all other imx8mm */
107static struct platform_config imx8mm_plat_config = {
108 .misc_flags = FCB_LAYOUT_META_32B |
109 FCB_ENCODE_BCH_62b |
110 FIRMWARE_NEED_PADDING |
111 FCB_RANDON_ENABLED,
112};
113
114/* imx8mn */
115static struct platform_config imx8mn_plat_config = {
116 .misc_flags = FCB_LAYOUT_META_32B |
117 FCB_ENCODE_BCH_62b |
118 FCB_RANDON_ENABLED |
119 FIRMWARE_SECONDARY_FIXED_ADDR |
120 BT_SEARCH_CNT_FROM_FUSE,
121};
122
123/* imx8qx/qm */
124static struct platform_config imx8q_plat_config = {
125 .misc_flags = FCB_LAYOUT_META_32B |
126 FCB_ENCODE_BCH_62b |
127 FCB_RANDON_ENABLED |
128 FIRMWARE_SECONDARY_FIXED_ADDR |
129 BT_SEARCH_CNT_FROM_FUSE,
130};
131
132/* boot search related variables and definitions */
133static int g_boot_search_count = 4;
Michael Trimarchied836112022-01-07 18:27:17 +0100134static int g_boot_secondary_offset;
Han Xu1f314e52020-05-05 22:04:03 +0800135static int g_boot_search_stride;
136static int g_pages_per_stride;
137
138/* mtd config structure */
139struct boot_config {
140 int dev;
141 struct mtd_info *mtd;
142 loff_t maxsize;
143 loff_t input_size;
144 loff_t offset;
145 loff_t boot_stream1_address;
146 loff_t boot_stream2_address;
147 size_t boot_stream1_size;
148 size_t boot_stream2_size;
149 size_t max_boot_stream_size;
150 int stride_size_in_byte;
151 int search_area_size_in_bytes;
152 int search_area_size_in_pages;
153 int secondary_boot_stream_off_in_MB;
154};
155
156/* boot_stream config structure */
157struct boot_stream_config {
158 char bs_label[32];
159 loff_t bs_addr;
160 size_t bs_size;
161 void *bs_buf;
162 loff_t next_bs_addr;
163 bool need_padding;
164};
165
166/* FW index */
167#define FW1_ONLY 1
168#define FW2_ONLY 2
169#define FW_ALL FW1_ONLY | FW2_ONLY
170#define FW_INX(x) (1 << (x))
171
172/* NAND convert macros */
173#define CONV_TO_PAGES(x) ((u32)(x) / (u32)(mtd->writesize))
174#define CONV_TO_BLOCKS(x) ((u32)(x) / (u32)(mtd->erasesize))
175
Shyam Sainif63ef492019-06-14 13:05:33 +0530176#define GETBIT(v, n) (((v) >> (n)) & 0x1)
Alice Guoa3f815b2020-05-05 22:04:00 +0800177#define IMX8MQ_SPL_SZ 0x3e000
178#define IMX8MQ_HDMI_FW_SZ 0x19c00
Han Xu1f314e52020-05-05 22:04:03 +0800179
180static int nandbcb_get_info(int argc, char * const argv[],
181 struct boot_config *boot_cfg)
182{
183 int dev;
184 struct mtd_info *mtd;
185
186 dev = nand_curr_device;
187 if (dev < 0) {
188 printf("failed to get nand_curr_device, run nand device\n");
189 return CMD_RET_FAILURE;
190 }
191
192 mtd = get_nand_dev_by_index(dev);
193 if (!mtd) {
194 printf("failed to get mtd info\n");
195 return CMD_RET_FAILURE;
196 }
197
198 boot_cfg->dev = dev;
199 boot_cfg->mtd = mtd;
200
201 return CMD_RET_SUCCESS;
202}
203
204static int nandbcb_get_size(int argc, char * const argv[], int num,
205 struct boot_config *boot_cfg)
206{
207 int dev;
208 loff_t offset, size, maxsize;
209 struct mtd_info *mtd;
210
211 dev = boot_cfg->dev;
212 mtd = boot_cfg->mtd;
213 size = 0;
214
215 if (mtd_arg_off_size(argc - num, argv + num, &dev, &offset, &size,
216 &maxsize, MTD_DEV_TYPE_NAND, mtd->size))
217 return CMD_RET_FAILURE;
218
219 boot_cfg->maxsize = maxsize;
220 boot_cfg->offset = offset;
Alice Guoeaab9052020-05-05 22:04:01 +0800221
Han Xu1f314e52020-05-05 22:04:03 +0800222 debug("max: %llx, offset: %llx\n", maxsize, offset);
223
224 if (size && size != maxsize)
225 boot_cfg->input_size = size;
226
227 return CMD_RET_SUCCESS;
228}
229
230static int nandbcb_set_boot_config(int argc, char * const argv[],
231 struct boot_config *boot_cfg)
232{
233 struct mtd_info *mtd;
234 loff_t maxsize;
235 loff_t boot_stream1_address, boot_stream2_address, max_boot_stream_size;
236
237 if (!boot_cfg->mtd) {
238 printf("Didn't get the mtd info, quit\n");
239 return CMD_RET_FAILURE;
240 }
241 mtd = boot_cfg->mtd;
242
243 /*
244 * By default
245 * set the search count as 4
246 * set each FCB/DBBT/Firmware offset at the beginning of blocks
247 * customers may change the value as needed
248 */
249
250 /* if need more compact layout, change these values */
251 /* g_boot_search_count was set as 4 at the definition*/
252 /* g_pages_per_stride was set as block size */
253
254 g_pages_per_stride = mtd->erasesize / mtd->writesize;
255
256 g_boot_search_stride = mtd->writesize * g_pages_per_stride;
257
258 boot_cfg->stride_size_in_byte = g_boot_search_stride * mtd->writesize;
259 boot_cfg->search_area_size_in_bytes =
260 g_boot_search_count * g_boot_search_stride;
261 boot_cfg->search_area_size_in_pages =
262 boot_cfg->search_area_size_in_bytes / mtd->writesize;
263
264 /* after FCB/DBBT, split the rest of area for two Firmwares */
265 if (!boot_cfg->maxsize) {
266 printf("Didn't get the maxsize, quit\n");
267 return CMD_RET_FAILURE;
268 }
269 maxsize = boot_cfg->maxsize;
270 /* align to page boundary */
271 maxsize = ((u32)(maxsize + mtd->writesize - 1)) / (u32)mtd->writesize
272 * mtd->writesize;
273
274 boot_stream1_address = 2 * boot_cfg->search_area_size_in_bytes;
275 boot_stream2_address = ((maxsize - boot_stream1_address) / 2 +
276 boot_stream1_address);
277
Michael Trimarchied836112022-01-07 18:27:17 +0100278 if (g_boot_secondary_offset)
Ye Li50710d22020-08-02 23:07:55 -0700279 boot_stream2_address =
Michael Trimarchied836112022-01-07 18:27:17 +0100280 (loff_t)g_boot_secondary_offset * 1024 * 1024;
Han Xu1f314e52020-05-05 22:04:03 +0800281
282 max_boot_stream_size = boot_stream2_address - boot_stream1_address;
283
284 /* sanity check */
285 if (max_boot_stream_size <= 0) {
286 debug("st1_addr: %llx, st2_addr: %llx, max: %llx\n",
287 boot_stream1_address, boot_stream2_address,
288 max_boot_stream_size);
289 printf("something wrong with firmware address settings\n");
290 return CMD_RET_FAILURE;
291 }
292 boot_cfg->boot_stream1_address = boot_stream1_address;
293 boot_cfg->boot_stream2_address = boot_stream2_address;
294 boot_cfg->max_boot_stream_size = max_boot_stream_size;
295
296 /* set the boot_stream size as the input size now */
297 if (boot_cfg->input_size) {
298 boot_cfg->boot_stream1_size = boot_cfg->input_size;
299 boot_cfg->boot_stream2_size = boot_cfg->input_size;
300 }
301
302 return CMD_RET_SUCCESS;
303}
304
305static int nandbcb_check_space(struct boot_config *boot_cfg)
306{
307 size_t maxsize = boot_cfg->maxsize;
308 size_t max_boot_stream_size = boot_cfg->max_boot_stream_size;
309 loff_t boot_stream2_address = boot_cfg->boot_stream2_address;
310
311 if (boot_cfg->boot_stream1_size &&
312 boot_cfg->boot_stream1_size > max_boot_stream_size) {
313 printf("boot stream1 doesn't fit, check partition size or settings\n");
314 return CMD_RET_FAILURE;
315 }
316
317 if (boot_cfg->boot_stream2_size &&
318 boot_cfg->boot_stream2_size > maxsize - boot_stream2_address) {
319 printf("boot stream2 doesn't fit, check partition size or settings\n");
320 return CMD_RET_FAILURE;
321 }
322
323 return CMD_RET_SUCCESS;
324}
Shyam Sainif63ef492019-06-14 13:05:33 +0530325
Parthiban Nallathambia99188b2019-10-18 11:46:19 +0200326#if defined(CONFIG_MX6UL) || defined(CONFIG_MX6ULL)
327static uint8_t reverse_bit(uint8_t b)
328{
329 b = (b & 0xf0) >> 4 | (b & 0x0f) << 4;
330 b = (b & 0xcc) >> 2 | (b & 0x33) << 2;
331 b = (b & 0xaa) >> 1 | (b & 0x55) << 1;
332
333 return b;
334}
335
336static void encode_bch_ecc(void *buf, struct fcb_block *fcb, int eccbits)
337{
338 int i, j, m = 13;
339 int blocksize = 128;
340 int numblocks = 8;
341 int ecc_buf_size = (m * eccbits + 7) / 8;
342 struct bch_control *bch = init_bch(m, eccbits, 0);
343 u8 *ecc_buf = kzalloc(ecc_buf_size, GFP_KERNEL);
344 u8 *tmp_buf = kzalloc(blocksize * numblocks, GFP_KERNEL);
345 u8 *psrc, *pdst;
346
347 /*
348 * The blocks here are bit aligned. If eccbits is a multiple of 8,
349 * we just can copy bytes. Otherwiese we must move the blocks to
350 * the next free bit position.
351 */
352 WARN_ON(eccbits % 8);
353
354 memcpy(tmp_buf, fcb, sizeof(*fcb));
355
356 for (i = 0; i < numblocks; i++) {
357 memset(ecc_buf, 0, ecc_buf_size);
358 psrc = tmp_buf + i * blocksize;
359 pdst = buf + i * (blocksize + ecc_buf_size);
360
361 /* copy data byte aligned to destination buf */
362 memcpy(pdst, psrc, blocksize);
363
364 /*
365 * imx-kobs use a modified encode_bch which reverse the
366 * bit order of the data before calculating bch.
367 * Do this in the buffer and use the bch lib here.
368 */
369 for (j = 0; j < blocksize; j++)
370 psrc[j] = reverse_bit(psrc[j]);
371
372 encode_bch(bch, psrc, blocksize, ecc_buf);
373
374 /* reverse ecc bit */
375 for (j = 0; j < ecc_buf_size; j++)
376 ecc_buf[j] = reverse_bit(ecc_buf[j]);
377
378 /* Here eccbuf is byte aligned and we can just copy it */
379 memcpy(pdst + blocksize, ecc_buf, ecc_buf_size);
380 }
381
382 kfree(ecc_buf);
383 kfree(tmp_buf);
384 free_bch(bch);
385}
386#else
387
Shyam Sainif63ef492019-06-14 13:05:33 +0530388static u8 calculate_parity_13_8(u8 d)
389{
390 u8 p = 0;
391
392 p |= (GETBIT(d, 6) ^ GETBIT(d, 5) ^ GETBIT(d, 3) ^ GETBIT(d, 2)) << 0;
393 p |= (GETBIT(d, 7) ^ GETBIT(d, 5) ^ GETBIT(d, 4) ^ GETBIT(d, 2) ^
394 GETBIT(d, 1)) << 1;
395 p |= (GETBIT(d, 7) ^ GETBIT(d, 6) ^ GETBIT(d, 5) ^ GETBIT(d, 1) ^
396 GETBIT(d, 0)) << 2;
397 p |= (GETBIT(d, 7) ^ GETBIT(d, 4) ^ GETBIT(d, 3) ^ GETBIT(d, 0)) << 3;
398 p |= (GETBIT(d, 6) ^ GETBIT(d, 4) ^ GETBIT(d, 3) ^ GETBIT(d, 2) ^
399 GETBIT(d, 1) ^ GETBIT(d, 0)) << 4;
400
401 return p;
402}
403
404static void encode_hamming_13_8(void *_src, void *_ecc, size_t size)
405{
406 int i;
407 u8 *src = _src;
408 u8 *ecc = _ecc;
409
410 for (i = 0; i < size; i++)
411 ecc[i] = calculate_parity_13_8(src[i]);
412}
Parthiban Nallathambia99188b2019-10-18 11:46:19 +0200413#endif
Shyam Sainif63ef492019-06-14 13:05:33 +0530414
415static u32 calc_chksum(void *buf, size_t size)
416{
417 u32 chksum = 0;
418 u8 *bp = buf;
419 size_t i;
420
421 for (i = 0; i < size; i++)
422 chksum += bp[i];
423
424 return ~chksum;
425}
426
Han Xu1f314e52020-05-05 22:04:03 +0800427static void fill_fcb(struct fcb_block *fcb, struct boot_config *boot_cfg)
Shyam Sainif63ef492019-06-14 13:05:33 +0530428{
Han Xu1f314e52020-05-05 22:04:03 +0800429 struct mtd_info *mtd = boot_cfg->mtd;
Shyam Sainif63ef492019-06-14 13:05:33 +0530430 struct nand_chip *chip = mtd_to_nand(mtd);
431 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
Igor Opaniuk344833a2019-11-03 16:49:44 +0100432 struct mxs_nand_layout l;
433
434 mxs_nand_get_layout(mtd, &l);
Shyam Sainif63ef492019-06-14 13:05:33 +0530435
436 fcb->fingerprint = FCB_FINGERPRINT;
437 fcb->version = FCB_VERSION_1;
Igor Opaniuk344833a2019-11-03 16:49:44 +0100438
Han Xu1f314e52020-05-05 22:04:03 +0800439 fcb->datasetup = 80;
440 fcb->datahold = 60;
441 fcb->addr_setup = 25;
442 fcb->dsample_time = 6;
443
Shyam Sainif63ef492019-06-14 13:05:33 +0530444 fcb->pagesize = mtd->writesize;
445 fcb->oob_pagesize = mtd->writesize + mtd->oobsize;
446 fcb->sectors = mtd->erasesize / mtd->writesize;
447
Igor Opaniuk344833a2019-11-03 16:49:44 +0100448 fcb->meta_size = l.meta_size;
449 fcb->nr_blocks = l.nblocks;
450 fcb->ecc_nr = l.data0_size;
451 fcb->ecc_level = l.ecc0;
452 fcb->ecc_size = l.datan_size;
453 fcb->ecc_type = l.eccn;
Han Xuae0bdd72020-05-05 22:03:59 +0800454 fcb->bchtype = l.gf_len;
Shyam Sainif63ef492019-06-14 13:05:33 +0530455
Han Xu1f314e52020-05-05 22:04:03 +0800456 /* DBBT search area starts from the next block after all FCB */
457 fcb->dbbt_start = boot_cfg->search_area_size_in_pages;
Shyam Sainif63ef492019-06-14 13:05:33 +0530458
459 fcb->bb_byte = nand_info->bch_geometry.block_mark_byte_offset;
460 fcb->bb_start_bit = nand_info->bch_geometry.block_mark_bit_offset;
461
462 fcb->phy_offset = mtd->writesize;
463
Igor Opaniuk344833a2019-11-03 16:49:44 +0100464 fcb->disbbm = 0;
Igor Opaniuk344833a2019-11-03 16:49:44 +0100465
Han Xu1f314e52020-05-05 22:04:03 +0800466 fcb->fw1_start = CONV_TO_PAGES(boot_cfg->boot_stream1_address);
467 fcb->fw2_start = CONV_TO_PAGES(boot_cfg->boot_stream2_address);
468 fcb->fw1_pages = CONV_TO_PAGES(boot_cfg->boot_stream1_size);
469 fcb->fw2_pages = CONV_TO_PAGES(boot_cfg->boot_stream2_size);
Igor Opaniuk14a489b2019-11-03 16:49:45 +0100470
Shyam Sainif63ef492019-06-14 13:05:33 +0530471 fcb->checksum = calc_chksum((void *)fcb + 4, sizeof(*fcb) - 4);
472}
473
Han Xu1f314e52020-05-05 22:04:03 +0800474static int fill_dbbt_data(struct mtd_info *mtd, void *buf, int num_blocks)
Shyam Sainif63ef492019-06-14 13:05:33 +0530475{
476 int n, n_bad_blocks = 0;
477 u32 *bb = buf + 0x8;
478 u32 *n_bad_blocksp = buf + 0x4;
479
480 for (n = 0; n < num_blocks; n++) {
Ye Lid701ed52020-08-02 22:59:43 -0700481 loff_t offset = (loff_t)n * mtd->erasesize;
Shyam Sainif63ef492019-06-14 13:05:33 +0530482 if (mtd_block_isbad(mtd, offset)) {
483 n_bad_blocks++;
484 *bb = n;
485 bb++;
486 }
487 }
488
489 *n_bad_blocksp = n_bad_blocks;
490
491 return n_bad_blocks;
492}
493
Han Xu1f314e52020-05-05 22:04:03 +0800494/*
495 * return 1 - bad block
496 * return 0 - read successfully
497 * return < 0 - read failed
498 */
499static int read_fcb(struct boot_config *boot_cfg, struct fcb_block *fcb,
500 loff_t off)
Igor Opaniuk14a489b2019-11-03 16:49:45 +0100501{
Han Xu1f314e52020-05-05 22:04:03 +0800502 struct mtd_info *mtd;
503 void *fcb_raw_page;
504 size_t size;
505 int ret = 0;
506
507 mtd = boot_cfg->mtd;
Han Xu1f314e52020-05-05 22:04:03 +0800508
Ye Lic61dedb2020-08-02 22:43:45 -0700509 fcb_raw_page = kzalloc(mtd->writesize + mtd->oobsize, GFP_KERNEL);
510 if (!fcb_raw_page) {
511 debug("failed to allocate fcb_raw_page\n");
512 ret = -ENOMEM;
513 return ret;
514 }
515
Han Xu1f314e52020-05-05 22:04:03 +0800516 /*
517 * User BCH hardware to decode ECC for FCB
518 */
519 if (plat_config.misc_flags & FCB_ENCODE_BCH) {
520 size = sizeof(struct fcb_block);
521
522 /* switch nand BCH to FCB compatible settings */
523 if (plat_config.misc_flags & FCB_ENCODE_BCH_62b)
524 mxs_nand_mode_fcb_62bit(mtd);
525 else if (plat_config.misc_flags & FCB_ENCODE_BCH_40b)
526 mxs_nand_mode_fcb_40bit(mtd);
527
Michael Trimarchi97cd4d82022-05-15 11:35:32 +0200528 ret = nand_read_skip_bad(mtd, off, &size, NULL, mtd->size, (u_char *)fcb);
Han Xu1f314e52020-05-05 22:04:03 +0800529
530 /* switch BCH back */
531 mxs_nand_mode_normal(mtd);
532 printf("NAND FCB read from 0x%llx offset 0x%zx read: %s\n",
533 off, size, ret ? "ERROR" : "OK");
534
535 } else if (plat_config.misc_flags & FCB_ENCODE_HAMMING) {
536 /* raw read*/
537 mtd_oob_ops_t ops = {
538 .datbuf = (u8 *)fcb_raw_page,
539 .oobbuf = ((u8 *)fcb_raw_page) + mtd->writesize,
540 .len = mtd->writesize,
541 .ooblen = mtd->oobsize,
542 .mode = MTD_OPS_RAW
543 };
544
545 ret = mtd_read_oob(mtd, off, &ops);
546 printf("NAND FCB read from 0x%llx offset 0x%zx read: %s\n",
547 off, ops.len, ret ? "ERROR" : "OK");
548 }
549
550 if (ret)
551 goto fcb_raw_page_err;
552
553 if ((plat_config.misc_flags & FCB_ENCODE_HAMMING) &&
554 (plat_config.misc_flags & FCB_LAYOUT_RESV_12B))
555 memcpy(fcb, fcb_raw_page + 12, sizeof(struct fcb_block));
556
557/* TODO: check if it can pass Hamming check */
558
559fcb_raw_page_err:
560 kfree(fcb_raw_page);
561
562 return ret;
563}
564
565static int write_fcb(struct boot_config *boot_cfg, struct fcb_block *fcb)
566{
567 struct mtd_info *mtd;
568 void *fcb_raw_page = NULL;
Ye Li0c55e332020-08-02 20:55:17 -0700569 int i, ret = 0;
Han Xu1f314e52020-05-05 22:04:03 +0800570 loff_t off;
571 size_t size;
572
573 mtd = boot_cfg->mtd;
Igor Opaniuk14a489b2019-11-03 16:49:45 +0100574
575 /*
576 * We prepare raw page only for i.MX6, for i.MX7 we
577 * leverage BCH hw module instead
578 */
Han Xu1f314e52020-05-05 22:04:03 +0800579 if ((plat_config.misc_flags & FCB_ENCODE_HAMMING) &&
580 (plat_config.misc_flags & FCB_LAYOUT_RESV_12B)) {
Igor Opaniuk14a489b2019-11-03 16:49:45 +0100581 fcb_raw_page = kzalloc(mtd->writesize + mtd->oobsize,
582 GFP_KERNEL);
583 if (!fcb_raw_page) {
584 debug("failed to allocate fcb_raw_page\n");
585 ret = -ENOMEM;
586 return ret;
587 }
588
589#if defined(CONFIG_MX6UL) || defined(CONFIG_MX6ULL)
590 /* 40 bit BCH, for i.MX6UL(L) */
591 encode_bch_ecc(fcb_raw_page + 32, fcb, 40);
592#else
593 memcpy(fcb_raw_page + 12, fcb, sizeof(struct fcb_block));
594 encode_hamming_13_8(fcb_raw_page + 12,
595 fcb_raw_page + 12 + 512, 512);
596#endif
597 /*
598 * Set the first and second byte of OOB data to 0xFF,
599 * not 0x00. These bytes are used as the Manufacturers Bad
600 * Block Marker (MBBM). Since the FCB is mostly written to
601 * the first page in a block, a scan for
602 * factory bad blocks will detect these blocks as bad, e.g.
603 * when function nand_scan_bbt() is executed to build a new
604 * bad block table.
605 */
606 memset(fcb_raw_page + mtd->writesize, 0xFF, 2);
607 }
Han Xu1f314e52020-05-05 22:04:03 +0800608
609 /* start writing FCB from the very beginning */
610 off = 0;
611
612 for (i = 0; i < g_boot_search_count; i++) {
Igor Opaniuk14a489b2019-11-03 16:49:45 +0100613 if (mtd_block_isbad(mtd, off)) {
614 printf("Block %d is bad, skipped\n", i);
Michael Trimarchi97cd4d82022-05-15 11:35:32 +0200615 off += mtd->erasesize;
Igor Opaniuk14a489b2019-11-03 16:49:45 +0100616 continue;
617 }
618
619 /*
Han Xu1f314e52020-05-05 22:04:03 +0800620 * User BCH hardware module to generate ECC for FCB
Igor Opaniuk14a489b2019-11-03 16:49:45 +0100621 */
Han Xu1f314e52020-05-05 22:04:03 +0800622 if (plat_config.misc_flags & FCB_ENCODE_BCH) {
623 size = sizeof(struct fcb_block);
Igor Opaniuk14a489b2019-11-03 16:49:45 +0100624
625 /* switch nand BCH to FCB compatible settings */
Han Xu1f314e52020-05-05 22:04:03 +0800626 if (plat_config.misc_flags & FCB_ENCODE_BCH_62b)
627 mxs_nand_mode_fcb_62bit(mtd);
628 else if (plat_config.misc_flags & FCB_ENCODE_BCH_40b)
629 mxs_nand_mode_fcb_40bit(mtd);
Alice Guoeaab9052020-05-05 22:04:01 +0800630
Han Xu1f314e52020-05-05 22:04:03 +0800631 ret = nand_write(mtd, off, &size, (u_char *)fcb);
Alice Guoeaab9052020-05-05 22:04:01 +0800632
Han Xu1f314e52020-05-05 22:04:03 +0800633 /* switch BCH back */
Igor Opaniuk14a489b2019-11-03 16:49:45 +0100634 mxs_nand_mode_normal(mtd);
Han Xu1f314e52020-05-05 22:04:03 +0800635 printf("NAND FCB write to 0x%zx offset 0x%llx written: %s\n",
636 size, off, ret ? "ERROR" : "OK");
Igor Opaniuk14a489b2019-11-03 16:49:45 +0100637
Han Xu1f314e52020-05-05 22:04:03 +0800638 } else if (plat_config.misc_flags & FCB_ENCODE_HAMMING) {
Igor Opaniuk14a489b2019-11-03 16:49:45 +0100639 /* raw write */
640 mtd_oob_ops_t ops = {
641 .datbuf = (u8 *)fcb_raw_page,
642 .oobbuf = ((u8 *)fcb_raw_page) +
643 mtd->writesize,
644 .len = mtd->writesize,
645 .ooblen = mtd->oobsize,
646 .mode = MTD_OPS_RAW
647 };
648
Han Xu1f314e52020-05-05 22:04:03 +0800649 ret = mtd_write_oob(mtd, off, &ops);
Pali Rohárdc5c6bf2021-10-20 11:13:15 +0200650 printf("NAND FCB write to 0x%llx offset 0x%zx written: %s\n", off, ops.len, ret ? "ERROR" : "OK");
Igor Opaniuk14a489b2019-11-03 16:49:45 +0100651 }
652
Igor Opaniuk14a489b2019-11-03 16:49:45 +0100653 if (ret)
654 goto fcb_raw_page_err;
Han Xu1f314e52020-05-05 22:04:03 +0800655
656 /* next writing location */
657 off += g_boot_search_stride;
658 }
659
Han Xu1f314e52020-05-05 22:04:03 +0800660fcb_raw_page_err:
661 kfree(fcb_raw_page);
662
663 return ret;
664}
665
666/*
667 * return 1 - bad block
668 * return 0 - read successfully
669 * return < 0 - read failed
670 */
671static int read_dbbt(struct boot_config *boot_cfg, struct dbbt_block *dbbt,
672 void *dbbt_data_page, loff_t off)
673{
674 size_t size;
Michael Trimarchi97cd4d82022-05-15 11:35:32 +0200675 size_t actual_size;
Han Xu1f314e52020-05-05 22:04:03 +0800676 struct mtd_info *mtd;
677 loff_t to;
678 int ret;
Igor Opaniuk14a489b2019-11-03 16:49:45 +0100679
Han Xu1f314e52020-05-05 22:04:03 +0800680 mtd = boot_cfg->mtd;
Alice Guoeaab9052020-05-05 22:04:01 +0800681
Han Xu1f314e52020-05-05 22:04:03 +0800682 size = sizeof(struct dbbt_block);
Michael Trimarchi97cd4d82022-05-15 11:35:32 +0200683 ret = nand_read_skip_bad(mtd, off, &size, &actual_size, mtd->size, (u_char *)dbbt);
Han Xu1f314e52020-05-05 22:04:03 +0800684 printf("NAND DBBT read from 0x%llx offset 0x%zx read: %s\n",
685 off, size, ret ? "ERROR" : "OK");
686 if (ret)
687 return ret;
688
689 /* dbbtpages == 0 if no bad blocks */
690 if (dbbt->dbbtpages > 0) {
Michael Trimarchi97cd4d82022-05-15 11:35:32 +0200691 to = off + 4 * mtd->writesize + actual_size - size;
Han Xu1f314e52020-05-05 22:04:03 +0800692 size = mtd->writesize;
Michael Trimarchi97cd4d82022-05-15 11:35:32 +0200693 ret = nand_read_skip_bad(mtd, to, &size, NULL, mtd->size, dbbt_data_page);
Han Xu1f314e52020-05-05 22:04:03 +0800694 printf("DBBT data read from 0x%llx offset 0x%zx read: %s\n",
695 to, size, ret ? "ERROR" : "OK");
696
697 if (ret)
698 return ret;
699 }
700
701 return 0;
702}
703
704static int write_dbbt(struct boot_config *boot_cfg, struct dbbt_block *dbbt,
705 void *dbbt_data_page)
706{
707 int i;
708 loff_t off, to;
709 size_t size;
710 struct mtd_info *mtd;
711 int ret;
712
713 mtd = boot_cfg->mtd;
714
715 /* start writing DBBT after all FCBs */
716 off = boot_cfg->search_area_size_in_bytes;
717 size = mtd->writesize;
718
719 for (i = 0; i < g_boot_search_count; i++) {
720 if (mtd_block_isbad(mtd, off)) {
721 printf("Block %d is bad, skipped\n",
722 (int)(i + CONV_TO_BLOCKS(off)));
Michael Trimarchi97cd4d82022-05-15 11:35:32 +0200723 off += mtd->erasesize;
Han Xu1f314e52020-05-05 22:04:03 +0800724 continue;
725 }
726
727 ret = nand_write(mtd, off, &size, (u_char *)dbbt);
728 printf("NAND DBBT write to 0x%llx offset 0x%zx written: %s\n",
729 off, size, ret ? "ERROR" : "OK");
730 if (ret)
731 return ret;
Alice Guoeaab9052020-05-05 22:04:01 +0800732
Igor Opaniuk14a489b2019-11-03 16:49:45 +0100733 /* dbbtpages == 0 if no bad blocks */
734 if (dbbt->dbbtpages > 0) {
Han Xu1f314e52020-05-05 22:04:03 +0800735 to = off + 4 * mtd->writesize;
736 ret = nand_write(mtd, to, &size, dbbt_data_page);
737 printf("DBBT data write to 0x%llx offset 0x%zx written: %s\n",
738 to, size, ret ? "ERROR" : "OK");
Igor Opaniuk14a489b2019-11-03 16:49:45 +0100739
Han Xu1f314e52020-05-05 22:04:03 +0800740 if (ret)
741 return ret;
Igor Opaniuk14a489b2019-11-03 16:49:45 +0100742 }
Han Xu1f314e52020-05-05 22:04:03 +0800743
744 /* next writing location */
745 off += g_boot_search_stride;
Igor Opaniuk14a489b2019-11-03 16:49:45 +0100746 }
747
Han Xu1f314e52020-05-05 22:04:03 +0800748 return 0;
749}
750
751/* reuse the check_skip_len from nand_util.c with minor change*/
752static int check_skip_length(struct boot_config *boot_cfg, loff_t offset,
753 size_t length, size_t *used)
754{
755 struct mtd_info *mtd = boot_cfg->mtd;
756 size_t maxsize = boot_cfg->maxsize;
757 size_t len_excl_bad = 0;
758 int ret = 0;
759
760 while (len_excl_bad < length) {
761 size_t block_len, block_off;
762 loff_t block_start;
763
764 if (offset >= maxsize)
765 return -1;
766
767 block_start = offset & ~(loff_t)(mtd->erasesize - 1);
768 block_off = offset & (mtd->erasesize - 1);
769 block_len = mtd->erasesize - block_off;
770
771 if (!nand_block_isbad(mtd, block_start))
772 len_excl_bad += block_len;
773 else
774 ret = 1;
775
776 offset += block_len;
777 *used += block_len;
778 }
779
780 /* If the length is not a multiple of block_len, adjust. */
781 if (len_excl_bad > length)
782 *used -= (len_excl_bad - length);
783
784 return ret;
785}
786
787static int nandbcb_get_next_good_blk_addr(struct boot_config *boot_cfg,
788 struct boot_stream_config *bs_cfg)
789{
790 struct mtd_info *mtd = boot_cfg->mtd;
791 loff_t offset = bs_cfg->bs_addr;
792 size_t length = bs_cfg->bs_size;
793 size_t used = 0;
794 int ret;
795
796 ret = check_skip_length(boot_cfg, offset, length, &used);
797
798 if (ret < 0)
799 return ret;
800
801 /* get next image address */
802 bs_cfg->next_bs_addr = (u32)(offset + used + mtd->erasesize - 1)
803 / (u32)mtd->erasesize * mtd->erasesize;
804
805 return ret;
806}
807
808static int nandbcb_write_bs_skip_bad(struct boot_config *boot_cfg,
809 struct boot_stream_config *bs_cfg)
810{
811 struct mtd_info *mtd;
812 void *buf;
813 loff_t offset, maxsize;
814 size_t size;
815 size_t length;
816 int ret;
817 bool padding_flag = false;
818
819 mtd = boot_cfg->mtd;
820 offset = bs_cfg->bs_addr;
821 maxsize = boot_cfg->maxsize;
822 size = bs_cfg->bs_size;
823
824 /* some boot images may need leading offset */
825 if (bs_cfg->need_padding &&
826 (plat_config.misc_flags & FIRMWARE_NEED_PADDING))
827 padding_flag = 1;
828
829 if (padding_flag)
830 length = ALIGN(size + FLASH_OFFSET_STANDARD, mtd->writesize);
831 else
832 length = ALIGN(size, mtd->writesize);
833
834 buf = kzalloc(length, GFP_KERNEL);
835 if (!buf) {
836 printf("failed to allocate buffer for firmware\n");
837 ret = -ENOMEM;
838 return ret;
839 }
840
841 if (padding_flag)
842 memcpy(buf + FLASH_OFFSET_STANDARD, bs_cfg->bs_buf, size);
843 else
844 memcpy(buf, bs_cfg->bs_buf, size);
Igor Opaniuk14a489b2019-11-03 16:49:45 +0100845
Han Xu1f314e52020-05-05 22:04:03 +0800846 ret = nand_write_skip_bad(mtd, offset, &length, NULL, maxsize,
847 (u_char *)buf, WITH_WR_VERIFY);
848 printf("Write %s @0x%llx offset, 0x%zx bytes written: %s\n",
849 bs_cfg->bs_label, offset, length, ret ? "ERROR" : "OK");
850
851 if (ret)
852 /* write image failed, quit */
853 goto err;
854
855 /* get next good blk address if needed */
856 if (bs_cfg->need_padding) {
857 ret = nandbcb_get_next_good_blk_addr(boot_cfg, bs_cfg);
858 if (ret < 0) {
859 printf("Next image cannot fit in NAND partition\n");
860 goto err;
861 }
862 }
863
864 /* now we know how the exact image size written to NAND */
865 bs_cfg->bs_size = length;
866 return 0;
867err:
868 kfree(buf);
Igor Opaniuk14a489b2019-11-03 16:49:45 +0100869 return ret;
870}
871
Han Xu1f314e52020-05-05 22:04:03 +0800872static int nandbcb_write_fw(struct boot_config *boot_cfg, u_char *buf,
873 int index)
Shyam Sainif63ef492019-06-14 13:05:33 +0530874{
Han Xu1f314e52020-05-05 22:04:03 +0800875 int i;
876 loff_t offset;
877 size_t size;
878 loff_t next_bs_addr;
879 struct boot_stream_config bs_cfg;
880 int ret;
881
882 for (i = 0; i < 2; ++i) {
883 if (!(FW_INX(i) & index))
884 continue;
885
886 if (i == 0) {
887 offset = boot_cfg->boot_stream1_address;
888 size = boot_cfg->boot_stream1_size;
889 } else {
890 offset = boot_cfg->boot_stream2_address;
891 size = boot_cfg->boot_stream2_size;
892 }
893
894 /* write Firmware*/
895 if (!(plat_config.misc_flags & FIRMWARE_EXTRA_ONE)) {
896 memset(&bs_cfg, 0, sizeof(struct boot_stream_config));
897 sprintf(bs_cfg.bs_label, "firmware%d", i);
898 bs_cfg.bs_addr = offset;
899 bs_cfg.bs_size = size;
900 bs_cfg.bs_buf = buf;
901 bs_cfg.need_padding = 1;
902
903 ret = nandbcb_write_bs_skip_bad(boot_cfg, &bs_cfg);
904 if (ret)
905 return ret;
906
907 /* update the boot stream size */
908 if (i == 0)
909 boot_cfg->boot_stream1_size = bs_cfg.bs_size;
910 else
911 boot_cfg->boot_stream2_size = bs_cfg.bs_size;
912
913 } else {
914 /* some platforms need extra firmware */
915 memset(&bs_cfg, 0, sizeof(struct boot_stream_config));
916 sprintf(bs_cfg.bs_label, "fw%d_part%d", i, 1);
917 bs_cfg.bs_addr = offset;
918 bs_cfg.bs_size = IMX8MQ_HDMI_FW_SZ;
919 bs_cfg.bs_buf = buf;
920 bs_cfg.need_padding = 1;
921
922 ret = nandbcb_write_bs_skip_bad(boot_cfg, &bs_cfg);
923 if (ret)
924 return ret;
925
926 /* update the boot stream size */
927 if (i == 0)
928 boot_cfg->boot_stream1_size = bs_cfg.bs_size;
929 else
930 boot_cfg->boot_stream2_size = bs_cfg.bs_size;
931
932 /* get next image address */
933 next_bs_addr = bs_cfg.next_bs_addr;
934
935 memset(&bs_cfg, 0, sizeof(struct boot_stream_config));
936 sprintf(bs_cfg.bs_label, "fw%d_part%d", i, 2);
937 bs_cfg.bs_addr = next_bs_addr;
938 bs_cfg.bs_size = IMX8MQ_SPL_SZ;
939 bs_cfg.bs_buf = (u_char *)(buf + IMX8MQ_HDMI_FW_SZ);
940 bs_cfg.need_padding = 0;
941
942 ret = nandbcb_write_bs_skip_bad(boot_cfg, &bs_cfg);
943 if (ret)
944 return ret;
945 }
946 }
947
948 return 0;
949}
950
951static int nandbcb_init(struct boot_config *boot_cfg, u_char *buf)
952{
953 struct mtd_info *mtd;
Shyam Sainif63ef492019-06-14 13:05:33 +0530954 nand_erase_options_t opts;
955 struct fcb_block *fcb;
956 struct dbbt_block *dbbt;
Han Xu1f314e52020-05-05 22:04:03 +0800957 void *dbbt_page, *dbbt_data_page;
Igor Opaniuk14a489b2019-11-03 16:49:45 +0100958 int ret;
Han Xu1f314e52020-05-05 22:04:03 +0800959 loff_t maxsize, off;
960
961 mtd = boot_cfg->mtd;
962 maxsize = boot_cfg->maxsize;
963 off = boot_cfg->offset;
Shyam Sainif63ef492019-06-14 13:05:33 +0530964
965 /* erase */
966 memset(&opts, 0, sizeof(opts));
967 opts.offset = off;
968 opts.length = maxsize - 1;
969 ret = nand_erase_opts(mtd, &opts);
970 if (ret) {
971 printf("%s: erase failed (ret = %d)\n", __func__, ret);
972 return ret;
973 }
974
975 /*
976 * Reference documentation from i.MX6DQRM section 8.5.2.2
977 *
978 * Nand Boot Control Block(BCB) contains two data structures,
979 * - Firmware Configuration Block(FCB)
980 * - Discovered Bad Block Table(DBBT)
981 *
982 * FCB contains,
983 * - nand timings
984 * - DBBT search page address,
985 * - start page address of primary firmware
986 * - start page address of secondary firmware
987 *
988 * setup fcb:
989 * - number of blocks = mtd partition size / mtd erasesize
990 * - two firmware blocks, primary and secondary
991 * - first 4 block for FCB/DBBT
992 * - rest split in half for primary and secondary firmware
Han Xu1f314e52020-05-05 22:04:03 +0800993 * - same firmware write twice
Shyam Sainif63ef492019-06-14 13:05:33 +0530994 */
Shyam Sainif63ef492019-06-14 13:05:33 +0530995
Han Xu1f314e52020-05-05 22:04:03 +0800996 /* write Firmware*/
997 ret = nandbcb_write_fw(boot_cfg, buf, FW_ALL);
998 if (ret)
999 goto err;
Shyam Sainif63ef492019-06-14 13:05:33 +05301000
1001 /* fill fcb */
1002 fcb = kzalloc(sizeof(*fcb), GFP_KERNEL);
1003 if (!fcb) {
1004 debug("failed to allocate fcb\n");
1005 ret = -ENOMEM;
Han Xu1f314e52020-05-05 22:04:03 +08001006 return ret;
Shyam Sainif63ef492019-06-14 13:05:33 +05301007 }
Han Xu1f314e52020-05-05 22:04:03 +08001008 fill_fcb(fcb, boot_cfg);
Shyam Sainif63ef492019-06-14 13:05:33 +05301009
Han Xu1f314e52020-05-05 22:04:03 +08001010 ret = write_fcb(boot_cfg, fcb);
Alice Guoeaab9052020-05-05 22:04:01 +08001011
Shyam Sainif63ef492019-06-14 13:05:33 +05301012 /* fill dbbt */
1013 dbbt_page = kzalloc(mtd->writesize, GFP_KERNEL);
1014 if (!dbbt_page) {
1015 debug("failed to allocate dbbt_page\n");
1016 ret = -ENOMEM;
1017 goto fcb_err;
1018 }
1019
1020 dbbt_data_page = kzalloc(mtd->writesize, GFP_KERNEL);
1021 if (!dbbt_data_page) {
1022 debug("failed to allocate dbbt_data_page\n");
1023 ret = -ENOMEM;
1024 goto dbbt_page_err;
1025 }
1026
1027 dbbt = dbbt_page;
1028 dbbt->checksum = 0;
Han Xu1f314e52020-05-05 22:04:03 +08001029 dbbt->fingerprint = DBBT_FINGERPRINT;
Shyam Sainif63ef492019-06-14 13:05:33 +05301030 dbbt->version = DBBT_VERSION_1;
Han Xu1f314e52020-05-05 22:04:03 +08001031 ret = fill_dbbt_data(mtd, dbbt_data_page, CONV_TO_BLOCKS(maxsize));
Shyam Sainif63ef492019-06-14 13:05:33 +05301032 if (ret < 0)
1033 goto dbbt_data_page_err;
1034 else if (ret > 0)
1035 dbbt->dbbtpages = 1;
1036
Han Xu1f314e52020-05-05 22:04:03 +08001037 /* write dbbt */
1038 ret = write_dbbt(boot_cfg, dbbt, dbbt_data_page);
Igor Opaniuk14a489b2019-11-03 16:49:45 +01001039 if (ret < 0)
1040 printf("failed to write FCB/DBBT\n");
Shyam Sainif63ef492019-06-14 13:05:33 +05301041
Shyam Sainif63ef492019-06-14 13:05:33 +05301042dbbt_data_page_err:
1043 kfree(dbbt_data_page);
1044dbbt_page_err:
1045 kfree(dbbt_page);
1046fcb_err:
1047 kfree(fcb);
Shyam Sainif63ef492019-06-14 13:05:33 +05301048err:
1049 return ret;
1050}
1051
Simon Glassed38aef2020-05-10 11:40:03 -06001052static int do_nandbcb_bcbonly(int argc, char *const argv[])
Igor Opaniukf3caefe2019-11-03 16:49:46 +01001053{
1054 struct fcb_block *fcb;
1055 struct dbbt_block *dbbt;
Igor Opaniukf3caefe2019-11-03 16:49:46 +01001056 struct mtd_info *mtd;
Han Xu1f314e52020-05-05 22:04:03 +08001057 nand_erase_options_t opts;
1058 size_t maxsize;
1059 loff_t off;
Igor Opaniukf3caefe2019-11-03 16:49:46 +01001060 void *dbbt_page, *dbbt_data_page;
Han Xu1f314e52020-05-05 22:04:03 +08001061 int ret;
1062 struct boot_config cfg;
Igor Opaniukf3caefe2019-11-03 16:49:46 +01001063
Han Xu1f314e52020-05-05 22:04:03 +08001064 if (argc < 4)
1065 return CMD_RET_USAGE;
1066
1067 memset(&cfg, 0, sizeof(struct boot_config));
1068 if (nandbcb_get_info(argc, argv, &cfg))
Igor Opaniukf3caefe2019-11-03 16:49:46 +01001069 return CMD_RET_FAILURE;
Igor Opaniukf3caefe2019-11-03 16:49:46 +01001070
Han Xu1f314e52020-05-05 22:04:03 +08001071 /* only get the partition info */
1072 if (nandbcb_get_size(2, argv, 1, &cfg))
1073 return CMD_RET_FAILURE;
Igor Opaniukf3caefe2019-11-03 16:49:46 +01001074
Han Xu1f314e52020-05-05 22:04:03 +08001075 if (nandbcb_set_boot_config(argc, argv, &cfg))
Igor Opaniukf3caefe2019-11-03 16:49:46 +01001076 return CMD_RET_FAILURE;
1077
Han Xu1f314e52020-05-05 22:04:03 +08001078 mtd = cfg.mtd;
Igor Opaniukf3caefe2019-11-03 16:49:46 +01001079
Simon Glass3ff49ec2021-07-24 09:03:29 -06001080 cfg.boot_stream1_address = hextoul(argv[2], NULL);
1081 cfg.boot_stream1_size = hextoul(argv[3], NULL);
Han Xu1f314e52020-05-05 22:04:03 +08001082 cfg.boot_stream1_size = ALIGN(cfg.boot_stream1_size, mtd->writesize);
1083
1084 if (argc > 5) {
Simon Glass3ff49ec2021-07-24 09:03:29 -06001085 cfg.boot_stream2_address = hextoul(argv[4], NULL);
1086 cfg.boot_stream2_size = hextoul(argv[5], NULL);
Han Xu1f314e52020-05-05 22:04:03 +08001087 cfg.boot_stream2_size = ALIGN(cfg.boot_stream2_size,
1088 mtd->writesize);
1089 }
1090
1091 /* sanity check */
1092 nandbcb_check_space(&cfg);
1093
1094 maxsize = cfg.maxsize;
1095 off = cfg.offset;
1096
1097 /* erase the previous FCB/DBBT */
1098 memset(&opts, 0, sizeof(opts));
1099 opts.offset = off;
1100 opts.length = g_boot_search_stride * 2;
1101 ret = nand_erase_opts(mtd, &opts);
1102 if (ret) {
1103 printf("%s: erase failed (ret = %d)\n", __func__, ret);
1104 return CMD_RET_FAILURE;
1105 }
Igor Opaniukf3caefe2019-11-03 16:49:46 +01001106
1107 /* fill fcb */
1108 fcb = kzalloc(sizeof(*fcb), GFP_KERNEL);
1109 if (!fcb) {
Han Xu1f314e52020-05-05 22:04:03 +08001110 printf("failed to allocate fcb\n");
Igor Opaniukf3caefe2019-11-03 16:49:46 +01001111 ret = -ENOMEM;
1112 return CMD_RET_FAILURE;
1113 }
1114
Han Xu1f314e52020-05-05 22:04:03 +08001115 fill_fcb(fcb, &cfg);
1116
1117 /* write fcb */
1118 ret = write_fcb(&cfg, fcb);
Igor Opaniukf3caefe2019-11-03 16:49:46 +01001119
1120 /* fill dbbt */
1121 dbbt_page = kzalloc(mtd->writesize, GFP_KERNEL);
1122 if (!dbbt_page) {
Han Xu1f314e52020-05-05 22:04:03 +08001123 printf("failed to allocate dbbt_page\n");
Igor Opaniukf3caefe2019-11-03 16:49:46 +01001124 ret = -ENOMEM;
1125 goto fcb_err;
1126 }
1127
1128 dbbt_data_page = kzalloc(mtd->writesize, GFP_KERNEL);
1129 if (!dbbt_data_page) {
Han Xu1f314e52020-05-05 22:04:03 +08001130 printf("failed to allocate dbbt_data_page\n");
Igor Opaniukf3caefe2019-11-03 16:49:46 +01001131 ret = -ENOMEM;
1132 goto dbbt_page_err;
1133 }
1134
1135 dbbt = dbbt_page;
1136 dbbt->checksum = 0;
Han Xu1f314e52020-05-05 22:04:03 +08001137 dbbt->fingerprint = DBBT_FINGERPRINT;
Igor Opaniukf3caefe2019-11-03 16:49:46 +01001138 dbbt->version = DBBT_VERSION_1;
Han Xu1f314e52020-05-05 22:04:03 +08001139 ret = fill_dbbt_data(mtd, dbbt_data_page, CONV_TO_BLOCKS(maxsize));
Igor Opaniukf3caefe2019-11-03 16:49:46 +01001140 if (ret < 0)
1141 goto dbbt_data_page_err;
1142 else if (ret > 0)
1143 dbbt->dbbtpages = 1;
1144
Han Xu1f314e52020-05-05 22:04:03 +08001145 /* write dbbt */
1146 ret = write_dbbt(&cfg, dbbt, dbbt_data_page);
1147
Igor Opaniukf3caefe2019-11-03 16:49:46 +01001148dbbt_data_page_err:
1149 kfree(dbbt_data_page);
1150dbbt_page_err:
1151 kfree(dbbt_page);
1152fcb_err:
1153 kfree(fcb);
1154
1155 if (ret < 0) {
1156 printf("failed to write FCB/DBBT\n");
1157 return CMD_RET_FAILURE;
1158 }
1159
1160 return CMD_RET_SUCCESS;
1161}
1162
Alice Guoeaab9052020-05-05 22:04:01 +08001163/* dump data which is read from NAND chip */
Han Xu1f314e52020-05-05 22:04:03 +08001164void dump_structure(struct boot_config *boot_cfg, struct fcb_block *fcb,
1165 struct dbbt_block *dbbt, void *dbbt_data_page)
Alice Guoeaab9052020-05-05 22:04:01 +08001166{
Han Xu1f314e52020-05-05 22:04:03 +08001167 int i;
1168 struct mtd_info *mtd = boot_cfg->mtd;
1169
1170 #define P1(x) printf(" %s = 0x%08x\n", #x, fcb->x)
1171 printf("FCB\n");
Alice Guoeaab9052020-05-05 22:04:01 +08001172 P1(checksum);
1173 P1(fingerprint);
1174 P1(version);
1175 #undef P1
Han Xu1f314e52020-05-05 22:04:03 +08001176 #define P1(x) printf(" %s = %d\n", #x, fcb->x)
Alice Guoeaab9052020-05-05 22:04:01 +08001177 P1(datasetup);
1178 P1(datahold);
1179 P1(addr_setup);
1180 P1(dsample_time);
1181 P1(pagesize);
1182 P1(oob_pagesize);
1183 P1(sectors);
1184 P1(nr_nand);
1185 P1(nr_die);
1186 P1(celltype);
1187 P1(ecc_type);
1188 P1(ecc_nr);
1189 P1(ecc_size);
1190 P1(ecc_level);
1191 P1(meta_size);
1192 P1(nr_blocks);
1193 P1(ecc_type_sdk);
1194 P1(ecc_nr_sdk);
1195 P1(ecc_size_sdk);
1196 P1(ecc_level_sdk);
1197 P1(nr_blocks_sdk);
1198 P1(meta_size_sdk);
1199 P1(erase_th);
1200 P1(bootpatch);
1201 P1(patch_size);
1202 P1(fw1_start);
1203 P1(fw2_start);
1204 P1(fw1_pages);
1205 P1(fw2_pages);
1206 P1(dbbt_start);
1207 P1(bb_byte);
1208 P1(bb_start_bit);
1209 P1(phy_offset);
1210 P1(bchtype);
1211 P1(readlatency);
1212 P1(predelay);
1213 P1(cedelay);
1214 P1(postdelay);
1215 P1(cmdaddpause);
1216 P1(datapause);
1217 P1(tmspeed);
1218 P1(busytimeout);
1219 P1(disbbm);
1220 P1(spare_offset);
Han Xu1f314e52020-05-05 22:04:03 +08001221#if !defined(CONFIG_MX6) || defined(CONFIG_MX6SX) || \
1222 defined(CONFIG_MX6UL) || defined(CONFIG_MX6ULL)
Alice Guoeaab9052020-05-05 22:04:01 +08001223 P1(onfi_sync_enable);
1224 P1(onfi_sync_speed);
1225 P1(onfi_sync_nand_data);
1226 P1(disbbm_search);
1227 P1(disbbm_search_limit);
1228 P1(read_retry_enable);
Han Xu1f314e52020-05-05 22:04:03 +08001229#endif
Alice Guoeaab9052020-05-05 22:04:01 +08001230 #undef P1
Han Xu1f314e52020-05-05 22:04:03 +08001231 #define P1(x) printf(" %s = 0x%08x\n", #x, dbbt->x)
1232 printf("DBBT :\n");
Alice Guoeaab9052020-05-05 22:04:01 +08001233 P1(checksum);
1234 P1(fingerprint);
1235 P1(version);
1236 #undef P1
Han Xu1f314e52020-05-05 22:04:03 +08001237 #define P1(x) printf(" %s = %d\n", #x, dbbt->x)
1238 P1(dbbtpages);
Alice Guoeaab9052020-05-05 22:04:01 +08001239 #undef P1
1240
Han Xu1f314e52020-05-05 22:04:03 +08001241 for (i = 0; i < dbbt->dbbtpages; ++i)
1242 printf("%d ", *((u32 *)(dbbt_data_page + i)));
1243
1244 if (!(plat_config.misc_flags & FIRMWARE_EXTRA_ONE)) {
1245 printf("Firmware: image #0 @ 0x%x size 0x%x\n",
1246 fcb->fw1_start, fcb->fw1_pages * mtd->writesize);
1247 printf("Firmware: image #1 @ 0x%x size 0x%x\n",
1248 fcb->fw2_start, fcb->fw2_pages * mtd->writesize);
1249 } else {
1250 printf("Firmware: image #0 @ 0x%x size 0x%x\n",
1251 fcb->fw1_start, fcb->fw1_pages * mtd->writesize);
1252 printf("Firmware: image #1 @ 0x%x size 0x%x\n",
1253 fcb->fw2_start, fcb->fw2_pages * mtd->writesize);
1254 /* TODO: Add extra image information */
Alice Guoeaab9052020-05-05 22:04:01 +08001255 }
1256}
1257
Han Xu1f314e52020-05-05 22:04:03 +08001258static bool check_fingerprint(void *data, int fingerprint)
Alice Guoeaab9052020-05-05 22:04:01 +08001259{
Han Xu1f314e52020-05-05 22:04:03 +08001260 int off = 4;
Alice Guoeaab9052020-05-05 22:04:01 +08001261
Han Xu1f314e52020-05-05 22:04:03 +08001262 return (*(int *)(data + off) == fingerprint);
1263}
Alice Guoeaab9052020-05-05 22:04:01 +08001264
Michael Trimarchied836112022-01-07 18:27:17 +01001265static int fuse_secondary_boot(u32 bank, u32 word, u32 mask, u32 off)
1266{
1267 int err;
1268 u32 val;
1269 int ret;
1270
1271 err = fuse_read(bank, word, &val);
1272 if (err)
1273 return 0;
1274
1275 val = (val & mask) >> off;
1276
1277 if (val > 10)
1278 return 0;
1279
1280 switch (val) {
1281 case 0:
1282 ret = 4;
1283 break;
1284 case 1:
1285 ret = 1;
1286 break;
1287 default:
1288 ret = 2 << val;
1289 break;
1290 }
1291
1292 return ret;
1293};
1294
Han Xu65781602020-05-05 22:04:04 +08001295static int fuse_to_search_count(u32 bank, u32 word, u32 mask, u32 off)
1296{
1297 int err;
1298 u32 val;
1299 int ret;
1300
1301 /* by default, the boot search count from fuse should be 2 */
1302 err = fuse_read(bank, word, &val);
1303 if (err)
1304 return 2;
1305
1306 val = (val & mask) >> off;
1307
1308 switch (val) {
1309 case 0:
1310 ret = 2;
1311 break;
1312 case 1:
1313 case 2:
1314 case 3:
1315 ret = 1 << val;
1316 break;
1317 default:
1318 ret = 2;
1319 }
1320
1321 return ret;
1322}
1323
Han Xu1f314e52020-05-05 22:04:03 +08001324static int nandbcb_dump(struct boot_config *boot_cfg)
1325{
1326 int i;
1327 loff_t off;
1328 struct mtd_info *mtd = boot_cfg->mtd;
1329 struct fcb_block fcb, fcb_copy;
1330 struct dbbt_block dbbt, dbbt_copy;
1331 void *dbbt_data_page, *dbbt_data_page_copy;
1332 bool fcb_not_found, dbbt_not_found;
1333 int ret = 0;
1334
1335 dbbt_data_page = kzalloc(mtd->writesize, GFP_KERNEL);
1336 if (!dbbt_data_page) {
1337 printf("failed to allocate dbbt_data_page\n");
1338 ret = -ENOMEM;
1339 return ret;
Alice Guoeaab9052020-05-05 22:04:01 +08001340 }
1341
Han Xu1f314e52020-05-05 22:04:03 +08001342 dbbt_data_page_copy = kzalloc(mtd->writesize, GFP_KERNEL);
1343 if (!dbbt_data_page_copy) {
1344 printf("failed to allocate dbbt_data_page\n");
1345 ret = -ENOMEM;
1346 goto dbbt_page_err;
1347 }
Alice Guoeaab9052020-05-05 22:04:01 +08001348
Han Xu1f314e52020-05-05 22:04:03 +08001349 /* read fcb */
1350 fcb_not_found = 1;
1351 off = 0;
1352 for (i = 0; i < g_boot_search_count; ++i) {
1353 if (fcb_not_found) {
1354 ret = read_fcb(boot_cfg, &fcb, off);
Alice Guoeaab9052020-05-05 22:04:01 +08001355
Han Xu1f314e52020-05-05 22:04:03 +08001356 if (ret < 0)
1357 goto dbbt_page_copy_err;
1358 else if (ret == 1)
1359 continue;
1360 else if (ret == 0)
1361 if (check_fingerprint(&fcb, FCB_FINGERPRINT))
1362 fcb_not_found = 0;
Alice Guoeaab9052020-05-05 22:04:01 +08001363 } else {
Han Xu1f314e52020-05-05 22:04:03 +08001364 ret = read_fcb(boot_cfg, &fcb_copy, off);
1365
1366 if (ret < 0)
1367 goto dbbt_page_copy_err;
1368 if (memcmp(&fcb, &fcb_copy,
1369 sizeof(struct fcb_block))) {
1370 printf("FCB copies are not identical\n");
1371 ret = -EINVAL;
1372 goto dbbt_page_copy_err;
1373 }
Alice Guoeaab9052020-05-05 22:04:01 +08001374 }
Alice Guoeaab9052020-05-05 22:04:01 +08001375
Han Xu1f314e52020-05-05 22:04:03 +08001376 /* next read location */
1377 off += g_boot_search_stride;
1378 }
Alice Guoeaab9052020-05-05 22:04:01 +08001379
Han Xu1f314e52020-05-05 22:04:03 +08001380 /* read dbbt*/
1381 dbbt_not_found = 1;
1382 off = boot_cfg->search_area_size_in_bytes;
1383 for (i = 0; i < g_boot_search_count; ++i) {
1384 if (dbbt_not_found) {
1385 ret = read_dbbt(boot_cfg, &dbbt, dbbt_data_page, off);
Alice Guoeaab9052020-05-05 22:04:01 +08001386
Han Xu1f314e52020-05-05 22:04:03 +08001387 if (ret < 0)
1388 goto dbbt_page_copy_err;
1389 else if (ret == 1)
1390 continue;
1391 else if (ret == 0)
1392 if (check_fingerprint(&dbbt, DBBT_FINGERPRINT))
1393 dbbt_not_found = 0;
Alice Guoeaab9052020-05-05 22:04:01 +08001394 } else {
Han Xu1f314e52020-05-05 22:04:03 +08001395 ret = read_dbbt(boot_cfg, &dbbt_copy,
1396 dbbt_data_page_copy, off);
1397
1398 if (ret < 0)
1399 goto dbbt_page_copy_err;
1400 if (memcmp(&dbbt, &dbbt_copy,
1401 sizeof(struct dbbt_block))) {
1402 printf("DBBT copies are not identical\n");
1403 ret = -EINVAL;
1404 goto dbbt_page_copy_err;
1405 }
1406 if (dbbt.dbbtpages > 0 &&
1407 memcmp(dbbt_data_page, dbbt_data_page_copy,
1408 mtd->writesize)) {
1409 printf("DBBT data copies are not identical\n");
1410 ret = -EINVAL;
1411 goto dbbt_page_copy_err;
1412 }
Alice Guoeaab9052020-05-05 22:04:01 +08001413 }
Han Xu1f314e52020-05-05 22:04:03 +08001414
1415 /* next read location */
1416 off += g_boot_search_stride;
Alice Guoeaab9052020-05-05 22:04:01 +08001417 }
1418
Han Xu1f314e52020-05-05 22:04:03 +08001419 dump_structure(boot_cfg, &fcb, &dbbt, dbbt_data_page);
1420
1421dbbt_page_copy_err:
1422 kfree(dbbt_data_page_copy);
1423dbbt_page_err:
1424 kfree(dbbt_data_page);
1425
1426 return ret;
1427}
1428
1429static int do_nandbcb_dump(int argc, char * const argv[])
1430{
1431 struct boot_config cfg;
1432 int ret;
Alice Guoeaab9052020-05-05 22:04:01 +08001433
Han Xu1f314e52020-05-05 22:04:03 +08001434 if (argc != 2)
Alice Guoeaab9052020-05-05 22:04:01 +08001435 return CMD_RET_USAGE;
Han Xu1f314e52020-05-05 22:04:03 +08001436
1437 memset(&cfg, 0, sizeof(struct boot_config));
1438 if (nandbcb_get_info(argc, argv, &cfg))
1439 return CMD_RET_FAILURE;
Alice Guoeaab9052020-05-05 22:04:01 +08001440
Han Xu1f314e52020-05-05 22:04:03 +08001441 if (nandbcb_get_size(argc, argv, 1, &cfg))
1442 return CMD_RET_FAILURE;
Alice Guoeaab9052020-05-05 22:04:01 +08001443
Han Xu1f314e52020-05-05 22:04:03 +08001444 if (nandbcb_set_boot_config(argc, argv, &cfg))
1445 return CMD_RET_FAILURE;
1446
1447 ret = nandbcb_dump(&cfg);
1448 if (ret)
1449 return ret;
1450
1451 return ret;
Alice Guoeaab9052020-05-05 22:04:01 +08001452}
1453
Han Xu1f314e52020-05-05 22:04:03 +08001454static int do_nandbcb_init(int argc, char * const argv[])
Shyam Sainif63ef492019-06-14 13:05:33 +05301455{
Shyam Sainif63ef492019-06-14 13:05:33 +05301456 u_char *buf;
Han Xu1f314e52020-05-05 22:04:03 +08001457 size_t size;
1458 loff_t addr;
1459 char *endp;
Shyam Sainif63ef492019-06-14 13:05:33 +05301460 int ret;
Han Xu1f314e52020-05-05 22:04:03 +08001461 struct boot_config cfg;
Shyam Sainif63ef492019-06-14 13:05:33 +05301462
1463 if (argc != 4)
1464 return CMD_RET_USAGE;
1465
Han Xu1f314e52020-05-05 22:04:03 +08001466 memset(&cfg, 0, sizeof(struct boot_config));
1467 if (nandbcb_get_info(argc, argv, &cfg))
Shyam Sainif63ef492019-06-14 13:05:33 +05301468 return CMD_RET_FAILURE;
Shyam Sainif63ef492019-06-14 13:05:33 +05301469
Han Xu1f314e52020-05-05 22:04:03 +08001470 if (nandbcb_get_size(argc, argv, 2, &cfg))
Shyam Sainif63ef492019-06-14 13:05:33 +05301471 return CMD_RET_FAILURE;
Han Xu1f314e52020-05-05 22:04:03 +08001472 size = cfg.boot_stream1_size;
Shyam Sainif63ef492019-06-14 13:05:33 +05301473
Han Xu1f314e52020-05-05 22:04:03 +08001474 if (nandbcb_set_boot_config(argc, argv, &cfg))
Shyam Sainif63ef492019-06-14 13:05:33 +05301475 return CMD_RET_FAILURE;
1476
Simon Glass3ff49ec2021-07-24 09:03:29 -06001477 addr = hextoul(argv[1], &endp);
Han Xu1f314e52020-05-05 22:04:03 +08001478 if (*argv[1] == 0 || *endp != 0)
1479 return CMD_RET_FAILURE;
Alice Guoeaab9052020-05-05 22:04:01 +08001480
Shyam Sainif63ef492019-06-14 13:05:33 +05301481 buf = map_physmem(addr, size, MAP_WRBACK);
1482 if (!buf) {
1483 puts("failed to map physical memory\n");
1484 return CMD_RET_FAILURE;
1485 }
1486
Han Xu1f314e52020-05-05 22:04:03 +08001487 ret = nandbcb_init(&cfg, buf);
Shyam Sainif63ef492019-06-14 13:05:33 +05301488
1489 return ret == 0 ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
1490}
1491
Simon Glassed38aef2020-05-10 11:40:03 -06001492static int do_nandbcb(struct cmd_tbl *cmdtp, int flag, int argc,
1493 char *const argv[])
Shyam Sainif63ef492019-06-14 13:05:33 +05301494{
1495 const char *cmd;
1496 int ret = 0;
1497
Alice Guoeaab9052020-05-05 22:04:01 +08001498 if (argc < 3)
Shyam Sainif63ef492019-06-14 13:05:33 +05301499 goto usage;
1500
Han Xu1f314e52020-05-05 22:04:03 +08001501 /* check the platform config first */
1502 if (is_mx6sx()) {
1503 plat_config = imx6sx_plat_config;
1504 } else if (is_mx7()) {
1505 plat_config = imx7d_plat_config;
1506 } else if (is_mx6ul() || is_mx6ull()) {
1507 plat_config = imx6ul_plat_config;
1508 } else if (is_mx6() && !is_mx6sx() && !is_mx6ul() && !is_mx6ull()) {
1509 plat_config = imx6qdl_plat_config;
1510 } else if (is_imx8mq()) {
1511 plat_config = imx8mq_plat_config;
1512 } else if (is_imx8mm()) {
1513 plat_config = imx8mm_plat_config;
Han Xuab6d50d2020-10-10 08:48:49 -05001514 } else if (is_imx8mn() || is_imx8mp()) {
Han Xu1f314e52020-05-05 22:04:03 +08001515 plat_config = imx8mn_plat_config;
1516 } else if (is_imx8qm() || is_imx8qxp()) {
1517 plat_config = imx8q_plat_config;
1518 } else {
1519 printf("ERROR: Unknown platform\n");
1520 return CMD_RET_FAILURE;
1521 }
1522
Han Xuab6d50d2020-10-10 08:48:49 -05001523 if ((plat_config.misc_flags) & BT_SEARCH_CNT_FROM_FUSE) {
1524 if (is_imx8qxp())
1525 g_boot_search_count = fuse_to_search_count(0, 720, 0xc0, 6);
1526 if (is_imx8mn() || is_imx8mp())
1527 g_boot_search_count = fuse_to_search_count(2, 2, 0x6000, 13);
1528 printf("search count set to %d from fuse\n",
1529 g_boot_search_count);
Han Xu65781602020-05-05 22:04:04 +08001530 }
Han Xu1f314e52020-05-05 22:04:03 +08001531
Michael Trimarchied836112022-01-07 18:27:17 +01001532 if (plat_config.misc_flags & FIRMWARE_SECONDARY_FIXED_ADDR) {
1533 if (is_imx8mn())
1534 g_boot_secondary_offset = fuse_secondary_boot(2, 1, 0xff0000, 16);
1535 }
1536
Shyam Sainif63ef492019-06-14 13:05:33 +05301537 cmd = argv[1];
1538 --argc;
1539 ++argv;
1540
Han Xu1f314e52020-05-05 22:04:03 +08001541 if (strcmp(cmd, "init") == 0) {
1542 ret = do_nandbcb_init(argc, argv);
Shyam Sainif63ef492019-06-14 13:05:33 +05301543 goto done;
1544 }
1545
Alice Guoeaab9052020-05-05 22:04:01 +08001546 if (strcmp(cmd, "dump") == 0) {
1547 ret = do_nandbcb_dump(argc, argv);
1548 goto done;
1549 }
1550
Igor Opaniukf3caefe2019-11-03 16:49:46 +01001551 if (strcmp(cmd, "bcbonly") == 0) {
1552 ret = do_nandbcb_bcbonly(argc, argv);
1553 goto done;
1554 }
1555
Shyam Sainif63ef492019-06-14 13:05:33 +05301556done:
1557 if (ret != -1)
1558 return ret;
1559usage:
1560 return CMD_RET_USAGE;
1561}
1562
Tom Rini03f146c2023-10-07 15:13:08 -04001563U_BOOT_LONGHELP(nandbcb,
Han Xu1f314e52020-05-05 22:04:03 +08001564 "init addr off|partition len - update 'len' bytes starting at\n"
Igor Opaniukf3caefe2019-11-03 16:49:46 +01001565 " 'off|part' to memory address 'addr', skipping bad blocks\n"
Han Xu1f314e52020-05-05 22:04:03 +08001566 "nandbcb bcbonly off|partition fw1-off fw1-size [fw2-off fw2-size]\n"
1567 " - write BCB only (FCB and DBBT)\n"
1568 " where `fwx-size` is fw sizes in bytes, `fw1-off`\n"
Igor Opaniukbe604312019-12-16 14:06:44 +02001569 " and `fw2-off` - firmware offsets\n"
1570 " FIY, BCB isn't erased automatically, so mtd erase should\n"
1571 " be called in advance before writing new BCB:\n"
Alice Guoeaab9052020-05-05 22:04:01 +08001572 " > mtd erase mx7-bcb\n"
Tom Rini03f146c2023-10-07 15:13:08 -04001573 "nandbcb dump off|partition - dump/verify boot structures\n");
Shyam Sainif63ef492019-06-14 13:05:33 +05301574
Han Xu1f314e52020-05-05 22:04:03 +08001575U_BOOT_CMD(nandbcb, 7, 1, do_nandbcb,
1576 "i.MX NAND Boot Control Blocks write",
Shyam Sainif63ef492019-06-14 13:05:33 +05301577 nandbcb_help_text
1578);