2 * i.MX6 nand boot control block(bcb).
4 * Based on the common/imx-bbu-nand-fcb.c from barebox and imx kobs-ng
6 * Copyright (C) 2017 Jagan Teki <jagan@amarulasolutions.com>
7 * Copyright (C) 2016 Sergey Kubushyn <ksi@koi8.net>
9 * SPDX-License-Identifier: GPL-2.0+
15 #include <dm/devres.h>
18 #include <jffs2/jffs2.h>
19 #include <linux/bch.h>
20 #include <linux/mtd/mtd.h>
22 #include <asm/arch/sys_proto.h>
23 #include <asm/mach-imx/imx-nandbcb.h>
24 #include <asm/mach-imx/imximage.cfg>
26 #include <linux/mtd/mtd.h>
30 #include "../../../cmd/legacy-mtd-utils.h"
32 #define BF_VAL(v, bf) (((v) & bf##_MASK) >> bf##_OFFSET)
33 #define GETBIT(v, n) (((v) >> (n)) & 0x1)
34 #define IMX8MQ_SPL_SZ 0x3e000
35 #define IMX8MQ_HDMI_FW_SZ 0x19c00
36 #define BOOT_SEARCH_COUNT 2
38 struct mtd_info *dump_mtd;
39 static loff_t dump_nandboot_size;
40 static struct fcb_block dump_fill_fcb;
41 static struct dbbt_block dump_fill_dbbt;
42 static struct fcb_block dump_nand_fcb[BOOT_SEARCH_COUNT];
43 static struct dbbt_block dump_nand_dbbt[BOOT_SEARCH_COUNT];
44 static u32 dump_fcb_off[BOOT_SEARCH_COUNT];
45 static u32 dump_dbbt_off[BOOT_SEARCH_COUNT];
47 #if defined(CONFIG_MX6UL) || defined(CONFIG_MX6ULL)
48 static uint8_t reverse_bit(uint8_t b)
50 b = (b & 0xf0) >> 4 | (b & 0x0f) << 4;
51 b = (b & 0xcc) >> 2 | (b & 0x33) << 2;
52 b = (b & 0xaa) >> 1 | (b & 0x55) << 1;
57 static void encode_bch_ecc(void *buf, struct fcb_block *fcb, int eccbits)
62 int ecc_buf_size = (m * eccbits + 7) / 8;
63 struct bch_control *bch = init_bch(m, eccbits, 0);
64 u8 *ecc_buf = kzalloc(ecc_buf_size, GFP_KERNEL);
65 u8 *tmp_buf = kzalloc(blocksize * numblocks, GFP_KERNEL);
69 * The blocks here are bit aligned. If eccbits is a multiple of 8,
70 * we just can copy bytes. Otherwiese we must move the blocks to
71 * the next free bit position.
75 memcpy(tmp_buf, fcb, sizeof(*fcb));
77 for (i = 0; i < numblocks; i++) {
78 memset(ecc_buf, 0, ecc_buf_size);
79 psrc = tmp_buf + i * blocksize;
80 pdst = buf + i * (blocksize + ecc_buf_size);
82 /* copy data byte aligned to destination buf */
83 memcpy(pdst, psrc, blocksize);
86 * imx-kobs use a modified encode_bch which reverse the
87 * bit order of the data before calculating bch.
88 * Do this in the buffer and use the bch lib here.
90 for (j = 0; j < blocksize; j++)
91 psrc[j] = reverse_bit(psrc[j]);
93 encode_bch(bch, psrc, blocksize, ecc_buf);
96 for (j = 0; j < ecc_buf_size; j++)
97 ecc_buf[j] = reverse_bit(ecc_buf[j]);
99 /* Here eccbuf is byte aligned and we can just copy it */
100 memcpy(pdst + blocksize, ecc_buf, ecc_buf_size);
109 static u8 calculate_parity_13_8(u8 d)
113 p |= (GETBIT(d, 6) ^ GETBIT(d, 5) ^ GETBIT(d, 3) ^ GETBIT(d, 2)) << 0;
114 p |= (GETBIT(d, 7) ^ GETBIT(d, 5) ^ GETBIT(d, 4) ^ GETBIT(d, 2) ^
116 p |= (GETBIT(d, 7) ^ GETBIT(d, 6) ^ GETBIT(d, 5) ^ GETBIT(d, 1) ^
118 p |= (GETBIT(d, 7) ^ GETBIT(d, 4) ^ GETBIT(d, 3) ^ GETBIT(d, 0)) << 3;
119 p |= (GETBIT(d, 6) ^ GETBIT(d, 4) ^ GETBIT(d, 3) ^ GETBIT(d, 2) ^
120 GETBIT(d, 1) ^ GETBIT(d, 0)) << 4;
125 static void encode_hamming_13_8(void *_src, void *_ecc, size_t size)
131 for (i = 0; i < size; i++)
132 ecc[i] = calculate_parity_13_8(src[i]);
136 static u32 calc_chksum(void *buf, size_t size)
142 for (i = 0; i < size; i++)
148 static void fill_fcb(struct fcb_block *fcb, struct mtd_info *mtd,
149 u32 fw1_start, u32 fw2_start, u32 fw_pages)
151 struct nand_chip *chip = mtd_to_nand(mtd);
152 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
153 struct mxs_nand_layout l;
155 mxs_nand_get_layout(mtd, &l);
157 fcb->fingerprint = FCB_FINGERPRINT;
158 fcb->version = FCB_VERSION_1;
160 fcb->pagesize = mtd->writesize;
161 fcb->oob_pagesize = mtd->writesize + mtd->oobsize;
162 fcb->sectors = mtd->erasesize / mtd->writesize;
164 fcb->meta_size = l.meta_size;
165 fcb->nr_blocks = l.nblocks;
166 fcb->ecc_nr = l.data0_size;
167 fcb->ecc_level = l.ecc0;
168 fcb->ecc_size = l.datan_size;
169 fcb->ecc_type = l.eccn;
170 fcb->bchtype = l.gf_len;
172 /* Also hardcoded in kobs-ng */
173 if (is_mx6() || is_imx8m()) {
176 fcb->addr_setup = 25;
177 fcb->dsample_time = 6;
178 } else if (is_mx7()) {
181 fcb->addr_setup = 15;
182 fcb->dsample_time = 6;
185 /* DBBT search area starts at second page on first block */
188 fcb->bb_byte = nand_info->bch_geometry.block_mark_byte_offset;
189 fcb->bb_start_bit = nand_info->bch_geometry.block_mark_bit_offset;
191 fcb->phy_offset = mtd->writesize;
193 fcb->nr_blocks = mtd->writesize / fcb->ecc_nr - 1;
196 fcb->disbbm_search = 0;
198 fcb->fw1_start = fw1_start; /* Firmware image starts on this sector */
199 fcb->fw2_start = fw2_start; /* Secondary FW Image starting Sector */
200 fcb->fw1_pages = fw_pages; /* Number of sectors in firmware image */
201 fcb->fw2_pages = fw_pages; /* Number of sector in secondary FW image */
203 fcb->checksum = calc_chksum((void *)fcb + 4, sizeof(*fcb) - 4);
206 static int dbbt_fill_data(struct mtd_info *mtd, void *buf, int num_blocks)
208 int n, n_bad_blocks = 0;
210 u32 *n_bad_blocksp = buf + 0x4;
212 for (n = 0; n < num_blocks; n++) {
213 loff_t offset = n * mtd->erasesize;
214 if (mtd_block_isbad(mtd, offset)) {
221 *n_bad_blocksp = n_bad_blocks;
226 static int write_fcb_dbbt_and_readback(struct mtd_info *mtd,
227 struct fcb_block *fcb,
228 struct dbbt_block *dbbt,
229 void *dbbt_data_page, loff_t off)
231 void *fcb_raw_page = 0;
236 * We prepare raw page only for i.MX6, for i.MX7 we
237 * leverage BCH hw module instead
241 fcb_raw_page = kzalloc(mtd->writesize + mtd->oobsize,
244 debug("failed to allocate fcb_raw_page\n");
249 #if defined(CONFIG_MX6UL) || defined(CONFIG_MX6ULL)
250 /* 40 bit BCH, for i.MX6UL(L) */
251 encode_bch_ecc(fcb_raw_page + 32, fcb, 40);
253 memcpy(fcb_raw_page + 12, fcb, sizeof(struct fcb_block));
254 encode_hamming_13_8(fcb_raw_page + 12,
255 fcb_raw_page + 12 + 512, 512);
258 * Set the first and second byte of OOB data to 0xFF,
259 * not 0x00. These bytes are used as the Manufacturers Bad
260 * Block Marker (MBBM). Since the FCB is mostly written to
261 * the first page in a block, a scan for
262 * factory bad blocks will detect these blocks as bad, e.g.
263 * when function nand_scan_bbt() is executed to build a new
266 memset(fcb_raw_page + mtd->writesize, 0xFF, 2);
268 for (i = 0; i < 2; i++) {
269 if (mtd_block_isbad(mtd, off)) {
270 printf("Block %d is bad, skipped\n", i);
275 * User BCH ECC hardware module for i.MX7
277 if (is_mx7() || is_imx8m()) {
278 u32 off = i * mtd->erasesize;
279 size_t rwsize = sizeof(*fcb);
281 printf("Writing %zd bytes to 0x%x: ", rwsize, off);
283 /* switch nand BCH to FCB compatible settings */
284 mxs_nand_mode_fcb(mtd);
285 ret = nand_write(mtd, off, &rwsize,
286 (unsigned char *)fcb);
288 dump_fcb_off[i] = off;
289 nand_read(mtd, off, &rwsize,
290 (unsigned char *)(dump_nand_fcb + i));
292 mxs_nand_mode_normal(mtd);
294 printf("%s\n", ret ? "ERROR" : "OK");
295 } else if (is_mx6()) {
297 mtd_oob_ops_t ops = {
298 .datbuf = (u8 *)fcb_raw_page,
299 .oobbuf = ((u8 *)fcb_raw_page) +
301 .len = mtd->writesize,
302 .ooblen = mtd->oobsize,
306 ret = mtd_write_oob(mtd, mtd->erasesize * i, &ops);
308 goto fcb_raw_page_err;
309 debug("NAND fcb write: 0x%x offset 0x%zx written: %s\n",
310 mtd->erasesize * i, ops.len, ret ?
313 ops.datbuf = (u8 *)(dump_nand_fcb + i);
314 ops.oobbuf = ((u8 *)(dump_nand_fcb + i)) + mtd->writesize;
315 mtd_read_oob(mtd, mtd->erasesize * i, &ops);
318 ret = mtd_write(mtd, mtd->erasesize * i + mtd->writesize,
319 mtd->writesize, &dummy, (void *)dbbt);
321 goto fcb_raw_page_err;
322 debug("NAND dbbt write: 0x%x offset, 0x%zx bytes written: %s\n",
323 mtd->erasesize * i + mtd->writesize, dummy,
324 ret ? "ERROR" : "OK");
326 dump_dbbt_off[i] = mtd->erasesize * i + mtd->writesize;
327 size_t rwsize = sizeof(*dbbt);
329 nand_read(mtd, dump_dbbt_off[i], &rwsize,
330 (unsigned char *)(dump_nand_dbbt + i));
332 /* dbbtpages == 0 if no bad blocks */
333 if (dbbt->dbbtpages > 0) {
334 loff_t to = (mtd->erasesize * i + mtd->writesize * 5);
336 ret = mtd_write(mtd, to, mtd->writesize, &dummy,
339 goto fcb_raw_page_err;
350 static int nandbcb_update(struct mtd_info *mtd, loff_t off, size_t size,
351 size_t maxsize, const u_char *buf)
353 nand_erase_options_t opts;
354 struct fcb_block *fcb;
355 struct dbbt_block *dbbt;
357 void *fwbuf, *dbbt_page, *dbbt_data_page;
358 u32 fw1_start, fw1_pages;
359 int nr_blks, nr_blks_fcb, fw1_blk;
364 loff_t extra_fw1_off;
367 memset(&opts, 0, sizeof(opts));
369 opts.length = maxsize - 1;
370 ret = nand_erase_opts(mtd, &opts);
372 printf("%s: erase failed (ret = %d)\n", __func__, ret);
377 * Reference documentation from i.MX6DQRM section 8.5.2.2
379 * Nand Boot Control Block(BCB) contains two data structures,
380 * - Firmware Configuration Block(FCB)
381 * - Discovered Bad Block Table(DBBT)
385 * - DBBT search page address,
386 * - start page address of primary firmware
387 * - start page address of secondary firmware
390 * - number of blocks = mtd partition size / mtd erasesize
391 * - two firmware blocks, primary and secondary
392 * - first 4 block for FCB/DBBT
393 * - rest split in half for primary and secondary firmware
394 * - same firmware will write two times
396 nr_blks_fcb = BOOT_SEARCH_COUNT;
397 nr_blks = maxsize / mtd->erasesize;
398 fw1_blk = nr_blks_fcb;
402 if (is_mx6() || is_mx7()) {
403 fwsize = ALIGN(size + FLASH_OFFSET_STANDARD + mtd->writesize,
405 fwbuf = kzalloc(fwsize, GFP_KERNEL);
407 debug("failed to allocate fwbuf\n");
412 memcpy(fwbuf + FLASH_OFFSET_STANDARD, buf, size);
413 fw1_off = fw1_blk * mtd->erasesize;
414 ret = nand_write_skip_bad(mtd, fw1_off, &fwsize, NULL, maxsize,
415 (u_char *)fwbuf, WITH_WR_VERIFY);
416 printf("NAND fw write: 0x%llx offset, 0x%zx bytes written: %s\n",
417 fw1_off, fwsize, ret ? "ERROR" : "OK");
420 } else if (is_imx8m()) {
421 fwsize = ALIGN(IMX8MQ_SPL_SZ + FLASH_OFFSET_STANDARD + mtd->writesize, mtd->writesize);
422 fwbuf = kzalloc(fwsize, GFP_KERNEL);
424 printf("failed to allocate fwbuf\n");
429 memcpy(fwbuf + FLASH_OFFSET_STANDARD, buf, IMX8MQ_SPL_SZ);
430 fw1_off = fw1_blk * mtd->erasesize;
431 ret = nand_write_skip_bad(mtd, fw1_off, &fwsize, NULL, maxsize,
432 (u_char *)fwbuf, WITH_WR_VERIFY);
433 printf("NAND fw write: 0x%llx offset, 0x%zx bytes written: %s\n",
434 fw1_off, fwsize, ret ? "ERROR" : "OK");
438 extra_fwsize = ALIGN(IMX8MQ_SPL_SZ + mtd->writesize, mtd->writesize);
439 extra_fwbuf = kzalloc(extra_fwsize, GFP_KERNEL);
440 extra_fw1_off = fw1_off + mtd->erasesize * ((IMX8MQ_SPL_SZ + mtd->erasesize - 1) / mtd->erasesize);
442 printf("failed to allocate fwbuf\n");
447 memcpy(extra_fwbuf, buf + IMX8MQ_HDMI_FW_SZ, IMX8MQ_SPL_SZ);
448 ret = nand_write_skip_bad(mtd, extra_fw1_off, &extra_fwsize, NULL, maxsize,
449 (u_char *)extra_fwbuf, WITH_WR_VERIFY);
450 printf("NAND extra_fw write: 0x%llx offset, 0x%zx bytes written: %s\n",
451 extra_fw1_off, extra_fwsize, ret ? "ERROR" : "OK");
459 fcb = kzalloc(sizeof(*fcb), GFP_KERNEL);
461 debug("failed to allocate fcb\n");
466 fw1_start = (fw1_blk * mtd->erasesize) / mtd->writesize;
467 fw1_pages = size / mtd->writesize + 1;
469 fw1_pages = (IMX8MQ_SPL_SZ + (mtd->writesize - 1)) / mtd->writesize;
470 fill_fcb(fcb, mtd, fw1_start, 0, fw1_pages);
472 dump_fill_fcb = *fcb;
475 dbbt_page = kzalloc(mtd->writesize, GFP_KERNEL);
477 debug("failed to allocate dbbt_page\n");
482 dbbt_data_page = kzalloc(mtd->writesize, GFP_KERNEL);
483 if (!dbbt_data_page) {
484 debug("failed to allocate dbbt_data_page\n");
491 dbbt->fingerprint = DBBT_FINGERPRINT2;
492 dbbt->version = DBBT_VERSION_1;
493 ret = dbbt_fill_data(mtd, dbbt_data_page, nr_blks);
495 goto dbbt_data_page_err;
499 dump_fill_dbbt = *dbbt;
501 /* write fcb and dbbt to nand */
502 ret = write_fcb_dbbt_and_readback(mtd, fcb, dbbt, dbbt_data_page, off);
504 printf("failed to write FCB/DBBT\n");
507 kfree(dbbt_data_page);
518 static int do_nandbcb_bcbonly(int argc, char * const argv[])
520 struct fcb_block *fcb;
521 struct dbbt_block *dbbt;
522 u32 fw_len, fw1_off, fw2_off;
523 struct mtd_info *mtd;
524 void *dbbt_page, *dbbt_data_page;
527 dev = nand_curr_device;
528 if ((dev < 0) || (dev >= CONFIG_SYS_MAX_NAND_DEVICE) ||
529 (!get_nand_dev_by_index(dev))) {
530 puts("No devices available\n");
531 return CMD_RET_FAILURE;
534 mtd = get_nand_dev_by_index(dev);
537 return CMD_RET_FAILURE;
539 fw_len = simple_strtoul(argv[1], NULL, 16);
540 fw1_off = simple_strtoul(argv[2], NULL, 16);
543 fw2_off = simple_strtoul(argv[3], NULL, 16);
548 fcb = kzalloc(sizeof(*fcb), GFP_KERNEL);
550 debug("failed to allocate fcb\n");
552 return CMD_RET_FAILURE;
555 fill_fcb(fcb, mtd, fw1_off / mtd->writesize,
556 fw2_off / mtd->writesize, fw_len / mtd->writesize);
559 dbbt_page = kzalloc(mtd->writesize, GFP_KERNEL);
561 debug("failed to allocate dbbt_page\n");
566 dbbt_data_page = kzalloc(mtd->writesize, GFP_KERNEL);
567 if (!dbbt_data_page) {
568 debug("failed to allocate dbbt_data_page\n");
575 dbbt->fingerprint = DBBT_FINGERPRINT2;
576 dbbt->version = DBBT_VERSION_1;
577 ret = dbbt_fill_data(mtd, dbbt_data_page, 0);
579 goto dbbt_data_page_err;
583 /* write fcb and dbbt to nand */
584 ret = write_fcb_dbbt_and_readback(mtd, fcb, dbbt, dbbt_data_page, 0);
586 kfree(dbbt_data_page);
593 printf("failed to write FCB/DBBT\n");
594 return CMD_RET_FAILURE;
597 return CMD_RET_SUCCESS;
600 /* dump data which is planned to be encoded and written to NAND chip */
601 void mtd_cfg_dump(void)
605 printf("MTD CONFIG:\n");
606 printf(" %s = %d\n", "data_setup_time", dump_fill_fcb.datasetup);
607 printf(" %s = %d\n", "data_hold_time", dump_fill_fcb.datahold);
608 printf(" %s = %d\n", "address_setup_time", dump_fill_fcb.addr_setup);
609 printf(" %s = %d\n", "data_sample_time", dump_fill_fcb.dsample_time);
611 printf("NFC geometry :\n");
612 printf("\tECC Strength : %d\n", dump_mtd->ecc_strength);
613 printf("\tPage Size in Bytes : %d\n", dump_fill_fcb.oob_pagesize);
614 printf("\tMetadata size : %d\n", dump_fill_fcb.meta_size);
615 printf("\tECC Chunk Size in byte : %d\n", dump_fill_fcb.ecc_size);
616 printf("\tECC Chunk count : %d\n", dump_fill_fcb.nr_blocks + 1);
617 printf("\tBlock Mark Byte Offset : %d\n", dump_fill_fcb.bb_byte);
618 printf("\tBlock Mark Bit Offset : %d\n", dump_fill_fcb.bb_start_bit);
619 printf("====================================================\n");
621 printf("mtd: partition #0\n");
622 printf(" %s = %d\n", "type", dump_mtd->type);
623 printf(" %s = %d\n", "flags", dump_mtd->flags);
624 printf(" %s = %llu\n", "size", dump_nandboot_size);
625 printf(" %s = %d\n", "erasesize", dump_mtd->erasesize);
626 printf(" %s = %d\n", "writesize", dump_mtd->writesize);
627 printf(" %s = %d\n", "oobsize", dump_mtd->oobsize);
628 blocks = dump_nandboot_size;
629 do_div(blocks, dump_mtd->erasesize);
630 printf(" %s = %llu\n", "blocks", blocks);
633 /* dump data which is read from NAND chip */
634 void mtd_dump_structure(int i)
636 #define P1(x) printf(" %s = 0x%08x\n", #x, dump_nand_fcb[i].x)
637 printf("FCB %d:\n", i);
642 #define P1(x) printf(" %s = %d\n", #x, dump_nand_fcb[i].x)
687 P1(onfi_sync_enable);
689 P1(onfi_sync_nand_data);
691 P1(disbbm_search_limit);
692 P1(read_retry_enable);
694 #define P1(x) printf(" %s = 0x%08x\n", #x, dump_nand_dbbt[i].x)
695 printf("DBBT %d:\n", i);
700 #define P1(x) printf(" %s = %d\n", #x, dump_nand_dbbt[i].x)
704 printf("Firmware: image #0 @ 0x%x size 0x%x - available 0x%llx\n",
705 dump_nand_fcb[i].fw1_start * dump_nand_fcb[i].pagesize,
706 dump_nand_fcb[i].fw1_pages * dump_nand_fcb[i].pagesize,
707 dump_nandboot_size - dump_nand_fcb[i].fw1_start *
708 dump_nand_fcb[i].pagesize);
710 printf("Extra Firmware: image #0 @ 0x%x size 0x%x - available 0x%llx\n",
711 dump_nand_fcb[i].fw1_start *
712 dump_nand_fcb[i].pagesize + dump_mtd->erasesize *
713 ((IMX8MQ_SPL_SZ + dump_mtd->erasesize - 1) /
714 dump_mtd->erasesize),
715 dump_nand_fcb[i].fw1_pages * dump_nand_fcb[i].pagesize,
717 (dump_nand_fcb[i].fw1_start *
718 dump_nand_fcb[i].pagesize + dump_mtd->erasesize *
719 ((IMX8MQ_SPL_SZ + dump_mtd->erasesize - 1) /
720 dump_mtd->erasesize)));
724 static int do_nandbcb_dump(int argc, char * const argv[])
729 bool bab_block_table[BOOT_SEARCH_COUNT];
733 return CMD_RET_USAGE;
735 switch (argv[1][0]) {
743 return CMD_RET_USAGE;
746 /* dump data which is planned to be encoded and written to NAND chip */
749 stride = dump_mtd->erasesize;
750 search_area_sz = BOOT_SEARCH_COUNT * stride;
751 printf("stride: %x, search_area_sz: %x\n", stride, search_area_sz);
754 for (int i = 0; i < BOOT_SEARCH_COUNT; i++) {
755 if (mtd_block_isbad(dump_mtd,
756 (loff_t)(dump_mtd->erasesize * i))) {
757 bab_block_table[i] = 1;
761 bab_block_table[i] = 0;
762 if (!memcmp(dump_nand_fcb + i, &dump_fill_fcb,
763 sizeof(dump_fill_fcb))) {
764 printf("mtd: found FCB%d candidate version %08x @%d:0x%x\n",
765 i, dump_nand_fcb[i].version, i, dump_fcb_off[i]);
767 printf("mtd: FCB%d not found\n", i);
771 for (int i = 0; i < BOOT_SEARCH_COUNT; i++) {
772 if (mtd_block_isbad(dump_mtd,
773 (loff_t)(dump_mtd->erasesize * i)))
776 if (!memcmp(dump_nand_dbbt + i, &dump_fill_dbbt,
777 sizeof(dump_fill_dbbt))) {
778 printf("mtd: DBBT%d found\n", i);
779 printf("mtd: Valid DBBT%d found @%d:0x%x\n",
780 i, i, dump_dbbt_off[i]);
783 printf("mtd: DBBT%d not found\n", i);
786 if (bab_block_flag == 0) {
787 printf("no bad block found, dbbt: %08x\n",
788 dump_fill_dbbt.fingerprint);
790 for (int i = 0; i < BOOT_SEARCH_COUNT; i++) {
791 if (bab_block_table[i] == 1)
792 printf("mtd: bad block @ 0x%llx\n",
793 (loff_t)(dump_mtd->erasesize * i));
797 /* dump data which is read from NAND chip */
798 if (num > (BOOT_SEARCH_COUNT - 1))
799 return CMD_RET_USAGE;
801 if (bab_block_table[num] == 1) {
802 printf("mtd: bad block @ 0x%llx (FCB - DBBT)\n",
803 (loff_t)(dump_mtd->erasesize * num));
804 return CMD_RET_USAGE;
807 mtd_dump_structure(num);
812 static int do_nandbcb_update(int argc, char * const argv[])
814 struct mtd_info *mtd;
815 loff_t addr, offset, size, maxsize;
822 return CMD_RET_USAGE;
824 dev = nand_curr_device;
826 printf("failed to get nand_curr_device, run nand device\n");
827 return CMD_RET_FAILURE;
830 addr = simple_strtoul(argv[1], &endp, 16);
831 if (*argv[1] == 0 || *endp != 0)
832 return CMD_RET_FAILURE;
834 mtd = get_nand_dev_by_index(dev);
835 if (mtd_arg_off_size(argc - 2, argv + 2, &dev, &offset, &size,
836 &maxsize, MTD_DEV_TYPE_NAND, mtd->size))
837 return CMD_RET_FAILURE;
839 /* dump_mtd and dump_nandboot_size are used for "nandbcb dump [-v]" */
841 dump_nandboot_size = maxsize;
843 buf = map_physmem(addr, size, MAP_WRBACK);
845 puts("failed to map physical memory\n");
846 return CMD_RET_FAILURE;
849 ret = nandbcb_update(mtd, offset, size, maxsize, buf);
851 return ret == 0 ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
854 static int do_nandbcb(cmd_tbl_t *cmdtp, int flag, int argc,
867 if (strcmp(cmd, "update") == 0) {
868 ret = do_nandbcb_update(argc, argv);
872 if (strcmp(cmd, "dump") == 0) {
873 ret = do_nandbcb_dump(argc, argv);
877 if (strcmp(cmd, "bcbonly") == 0) {
878 ret = do_nandbcb_bcbonly(argc, argv);
886 return CMD_RET_USAGE;
889 #ifdef CONFIG_SYS_LONGHELP
890 static char nandbcb_help_text[] =
891 "update addr off|partition len - update 'len' bytes starting at\n"
892 " 'off|part' to memory address 'addr', skipping bad blocks\n"
893 "bcbonly fw-size fw1-off [fw2-off] - write only BCB (FCB and DBBT)\n"
894 " where `fw-size` is fw sizes in bytes, `fw1-off`\n"
895 " and `fw2-off` - firmware offsets\n"
896 " FIY, BCB isn't erased automatically, so mtd erase should\n"
897 " be called in advance before writing new BCB:\n"
898 " > mtd erase mx7-bcb\n"
899 "nandbcb dump num - verify/dump boot structures\n"
900 " 'num' can be set to 0 and 1";
903 U_BOOT_CMD(nandbcb, 5, 1, do_nandbcb,
904 "i.MX6/i.MX7 NAND Boot Control Blocks write",