2 * i.MX 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 * Reconstucted by Han Xu <han.xu@nxp.com>
11 * SPDX-License-Identifier: GPL-2.0+
17 #include <dm/devres.h>
20 #include <jffs2/jffs2.h>
21 #include <linux/bch.h>
22 #include <linux/mtd/mtd.h>
24 #include <asm/arch/sys_proto.h>
25 #include <asm/mach-imx/imx-nandbcb.h>
26 #include <asm/mach-imx/imximage.cfg>
28 #include <linux/mtd/mtd.h>
32 #include "../../../cmd/legacy-mtd-utils.h"
34 /* FCB related flags */
35 /* FCB layout with leading 12B reserved */
36 #define FCB_LAYOUT_RESV_12B BIT(0)
37 /* FCB layout with leading 32B meta data */
38 #define FCB_LAYOUT_META_32B BIT(1)
39 /* FCB encrypted by Hamming code */
40 #define FCB_ENCODE_HAMMING BIT(2)
41 /* FCB encrypted by 40bit BCH */
42 #define FCB_ENCODE_BCH_40b BIT(3)
43 /* FCB encrypted by 62bit BCH */
44 #define FCB_ENCODE_BCH_62b BIT(4)
45 /* FCB encrypted by BCH */
46 #define FCB_ENCODE_BCH (FCB_ENCODE_BCH_40b | FCB_ENCODE_BCH_62b)
47 /* FCB data was randomized */
48 #define FCB_RANDON_ENABLED BIT(5)
50 /* Firmware related flags */
52 #define FIRMWARE_NEED_PADDING BIT(8)
54 #define FIRMWARE_EXTRA_ONE BIT(9)
55 /* Secondary firmware on fixed address */
56 #define FIRMWARE_SECONDARY_FIXED_ADDR BIT(10)
58 /* Boot search related flags */
59 #define BT_SEARCH_CNT_FROM_FUSE BIT(16)
61 struct platform_config {
65 static struct platform_config plat_config;
68 static struct platform_config imx6qdl_plat_config = {
69 .misc_flags = FCB_LAYOUT_RESV_12B |
71 FIRMWARE_NEED_PADDING,
74 static struct platform_config imx6sx_plat_config = {
75 .misc_flags = FCB_LAYOUT_META_32B |
77 FIRMWARE_NEED_PADDING |
81 static struct platform_config imx7d_plat_config = {
82 .misc_flags = FCB_LAYOUT_META_32B |
84 FIRMWARE_NEED_PADDING |
89 static struct platform_config imx6ul_plat_config = {
90 .misc_flags = FCB_LAYOUT_META_32B |
92 FIRMWARE_NEED_PADDING,
95 static struct platform_config imx8mq_plat_config = {
96 .misc_flags = FCB_LAYOUT_META_32B |
98 FIRMWARE_NEED_PADDING |
103 /* all other imx8mm */
104 static struct platform_config imx8mm_plat_config = {
105 .misc_flags = FCB_LAYOUT_META_32B |
107 FIRMWARE_NEED_PADDING |
112 static struct platform_config imx8mn_plat_config = {
113 .misc_flags = FCB_LAYOUT_META_32B |
116 FIRMWARE_SECONDARY_FIXED_ADDR |
117 BT_SEARCH_CNT_FROM_FUSE,
121 static struct platform_config imx8q_plat_config = {
122 .misc_flags = FCB_LAYOUT_META_32B |
125 FIRMWARE_SECONDARY_FIXED_ADDR |
126 BT_SEARCH_CNT_FROM_FUSE,
129 /* boot search related variables and definitions */
130 static int g_boot_search_count = 4;
131 static int g_boot_search_stride;
132 static int g_pages_per_stride;
134 /* mtd config structure */
137 struct mtd_info *mtd;
141 loff_t boot_stream1_address;
142 loff_t boot_stream2_address;
143 size_t boot_stream1_size;
144 size_t boot_stream2_size;
145 size_t max_boot_stream_size;
146 int stride_size_in_byte;
147 int search_area_size_in_bytes;
148 int search_area_size_in_pages;
149 int secondary_boot_stream_off_in_MB;
152 /* boot_stream config structure */
153 struct boot_stream_config {
165 #define FW_ALL FW1_ONLY | FW2_ONLY
166 #define FW_INX(x) (1 << (x))
168 /* NAND convert macros */
169 #define CONV_TO_PAGES(x) ((u32)(x) / (u32)(mtd->writesize))
170 #define CONV_TO_BLOCKS(x) ((u32)(x) / (u32)(mtd->erasesize))
172 #define GETBIT(v, n) (((v) >> (n)) & 0x1)
173 #define IMX8MQ_SPL_SZ 0x3e000
174 #define IMX8MQ_HDMI_FW_SZ 0x19c00
176 static int nandbcb_get_info(int argc, char * const argv[],
177 struct boot_config *boot_cfg)
180 struct mtd_info *mtd;
182 dev = nand_curr_device;
184 printf("failed to get nand_curr_device, run nand device\n");
185 return CMD_RET_FAILURE;
188 mtd = get_nand_dev_by_index(dev);
190 printf("failed to get mtd info\n");
191 return CMD_RET_FAILURE;
197 return CMD_RET_SUCCESS;
200 static int nandbcb_get_size(int argc, char * const argv[], int num,
201 struct boot_config *boot_cfg)
204 loff_t offset, size, maxsize;
205 struct mtd_info *mtd;
211 if (mtd_arg_off_size(argc - num, argv + num, &dev, &offset, &size,
212 &maxsize, MTD_DEV_TYPE_NAND, mtd->size))
213 return CMD_RET_FAILURE;
215 boot_cfg->maxsize = maxsize;
216 boot_cfg->offset = offset;
218 debug("max: %llx, offset: %llx\n", maxsize, offset);
220 if (size && size != maxsize)
221 boot_cfg->input_size = size;
223 return CMD_RET_SUCCESS;
226 static int nandbcb_set_boot_config(int argc, char * const argv[],
227 struct boot_config *boot_cfg)
229 struct mtd_info *mtd;
231 loff_t boot_stream1_address, boot_stream2_address, max_boot_stream_size;
233 if (!boot_cfg->mtd) {
234 printf("Didn't get the mtd info, quit\n");
235 return CMD_RET_FAILURE;
241 * set the search count as 4
242 * set each FCB/DBBT/Firmware offset at the beginning of blocks
243 * customers may change the value as needed
246 /* if need more compact layout, change these values */
247 /* g_boot_search_count was set as 4 at the definition*/
248 /* g_pages_per_stride was set as block size */
250 g_pages_per_stride = mtd->erasesize / mtd->writesize;
252 g_boot_search_stride = mtd->writesize * g_pages_per_stride;
254 boot_cfg->stride_size_in_byte = g_boot_search_stride * mtd->writesize;
255 boot_cfg->search_area_size_in_bytes =
256 g_boot_search_count * g_boot_search_stride;
257 boot_cfg->search_area_size_in_pages =
258 boot_cfg->search_area_size_in_bytes / mtd->writesize;
260 /* after FCB/DBBT, split the rest of area for two Firmwares */
261 if (!boot_cfg->maxsize) {
262 printf("Didn't get the maxsize, quit\n");
263 return CMD_RET_FAILURE;
265 maxsize = boot_cfg->maxsize;
266 /* align to page boundary */
267 maxsize = ((u32)(maxsize + mtd->writesize - 1)) / (u32)mtd->writesize
270 boot_stream1_address = 2 * boot_cfg->search_area_size_in_bytes;
271 boot_stream2_address = ((maxsize - boot_stream1_address) / 2 +
272 boot_stream1_address);
274 if (boot_cfg->secondary_boot_stream_off_in_MB)
275 boot_stream2_address = boot_cfg->secondary_boot_stream_off_in_MB * 1024 * 1024;
277 max_boot_stream_size = boot_stream2_address - boot_stream1_address;
280 if (max_boot_stream_size <= 0) {
281 debug("st1_addr: %llx, st2_addr: %llx, max: %llx\n",
282 boot_stream1_address, boot_stream2_address,
283 max_boot_stream_size);
284 printf("something wrong with firmware address settings\n");
285 return CMD_RET_FAILURE;
287 boot_cfg->boot_stream1_address = boot_stream1_address;
288 boot_cfg->boot_stream2_address = boot_stream2_address;
289 boot_cfg->max_boot_stream_size = max_boot_stream_size;
291 /* set the boot_stream size as the input size now */
292 if (boot_cfg->input_size) {
293 boot_cfg->boot_stream1_size = boot_cfg->input_size;
294 boot_cfg->boot_stream2_size = boot_cfg->input_size;
297 return CMD_RET_SUCCESS;
300 static int nandbcb_check_space(struct boot_config *boot_cfg)
302 size_t maxsize = boot_cfg->maxsize;
303 size_t max_boot_stream_size = boot_cfg->max_boot_stream_size;
304 loff_t boot_stream2_address = boot_cfg->boot_stream2_address;
306 if (boot_cfg->boot_stream1_size &&
307 boot_cfg->boot_stream1_size > max_boot_stream_size) {
308 printf("boot stream1 doesn't fit, check partition size or settings\n");
309 return CMD_RET_FAILURE;
312 if (boot_cfg->boot_stream2_size &&
313 boot_cfg->boot_stream2_size > maxsize - boot_stream2_address) {
314 printf("boot stream2 doesn't fit, check partition size or settings\n");
315 return CMD_RET_FAILURE;
318 return CMD_RET_SUCCESS;
321 #if defined(CONFIG_MX6UL) || defined(CONFIG_MX6ULL)
322 static uint8_t reverse_bit(uint8_t b)
324 b = (b & 0xf0) >> 4 | (b & 0x0f) << 4;
325 b = (b & 0xcc) >> 2 | (b & 0x33) << 2;
326 b = (b & 0xaa) >> 1 | (b & 0x55) << 1;
331 static void encode_bch_ecc(void *buf, struct fcb_block *fcb, int eccbits)
336 int ecc_buf_size = (m * eccbits + 7) / 8;
337 struct bch_control *bch = init_bch(m, eccbits, 0);
338 u8 *ecc_buf = kzalloc(ecc_buf_size, GFP_KERNEL);
339 u8 *tmp_buf = kzalloc(blocksize * numblocks, GFP_KERNEL);
343 * The blocks here are bit aligned. If eccbits is a multiple of 8,
344 * we just can copy bytes. Otherwiese we must move the blocks to
345 * the next free bit position.
347 WARN_ON(eccbits % 8);
349 memcpy(tmp_buf, fcb, sizeof(*fcb));
351 for (i = 0; i < numblocks; i++) {
352 memset(ecc_buf, 0, ecc_buf_size);
353 psrc = tmp_buf + i * blocksize;
354 pdst = buf + i * (blocksize + ecc_buf_size);
356 /* copy data byte aligned to destination buf */
357 memcpy(pdst, psrc, blocksize);
360 * imx-kobs use a modified encode_bch which reverse the
361 * bit order of the data before calculating bch.
362 * Do this in the buffer and use the bch lib here.
364 for (j = 0; j < blocksize; j++)
365 psrc[j] = reverse_bit(psrc[j]);
367 encode_bch(bch, psrc, blocksize, ecc_buf);
369 /* reverse ecc bit */
370 for (j = 0; j < ecc_buf_size; j++)
371 ecc_buf[j] = reverse_bit(ecc_buf[j]);
373 /* Here eccbuf is byte aligned and we can just copy it */
374 memcpy(pdst + blocksize, ecc_buf, ecc_buf_size);
383 static u8 calculate_parity_13_8(u8 d)
387 p |= (GETBIT(d, 6) ^ GETBIT(d, 5) ^ GETBIT(d, 3) ^ GETBIT(d, 2)) << 0;
388 p |= (GETBIT(d, 7) ^ GETBIT(d, 5) ^ GETBIT(d, 4) ^ GETBIT(d, 2) ^
390 p |= (GETBIT(d, 7) ^ GETBIT(d, 6) ^ GETBIT(d, 5) ^ GETBIT(d, 1) ^
392 p |= (GETBIT(d, 7) ^ GETBIT(d, 4) ^ GETBIT(d, 3) ^ GETBIT(d, 0)) << 3;
393 p |= (GETBIT(d, 6) ^ GETBIT(d, 4) ^ GETBIT(d, 3) ^ GETBIT(d, 2) ^
394 GETBIT(d, 1) ^ GETBIT(d, 0)) << 4;
399 static void encode_hamming_13_8(void *_src, void *_ecc, size_t size)
405 for (i = 0; i < size; i++)
406 ecc[i] = calculate_parity_13_8(src[i]);
410 static u32 calc_chksum(void *buf, size_t size)
416 for (i = 0; i < size; i++)
422 static void fill_fcb(struct fcb_block *fcb, struct boot_config *boot_cfg)
424 struct mtd_info *mtd = boot_cfg->mtd;
425 struct nand_chip *chip = mtd_to_nand(mtd);
426 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
427 struct mxs_nand_layout l;
429 mxs_nand_get_layout(mtd, &l);
431 fcb->fingerprint = FCB_FINGERPRINT;
432 fcb->version = FCB_VERSION_1;
436 fcb->addr_setup = 25;
437 fcb->dsample_time = 6;
439 fcb->pagesize = mtd->writesize;
440 fcb->oob_pagesize = mtd->writesize + mtd->oobsize;
441 fcb->sectors = mtd->erasesize / mtd->writesize;
443 fcb->meta_size = l.meta_size;
444 fcb->nr_blocks = l.nblocks;
445 fcb->ecc_nr = l.data0_size;
446 fcb->ecc_level = l.ecc0;
447 fcb->ecc_size = l.datan_size;
448 fcb->ecc_type = l.eccn;
449 fcb->bchtype = l.gf_len;
451 /* DBBT search area starts from the next block after all FCB */
452 fcb->dbbt_start = boot_cfg->search_area_size_in_pages;
454 fcb->bb_byte = nand_info->bch_geometry.block_mark_byte_offset;
455 fcb->bb_start_bit = nand_info->bch_geometry.block_mark_bit_offset;
457 fcb->phy_offset = mtd->writesize;
461 fcb->fw1_start = CONV_TO_PAGES(boot_cfg->boot_stream1_address);
462 fcb->fw2_start = CONV_TO_PAGES(boot_cfg->boot_stream2_address);
463 fcb->fw1_pages = CONV_TO_PAGES(boot_cfg->boot_stream1_size);
464 fcb->fw2_pages = CONV_TO_PAGES(boot_cfg->boot_stream2_size);
466 fcb->checksum = calc_chksum((void *)fcb + 4, sizeof(*fcb) - 4);
469 static int fill_dbbt_data(struct mtd_info *mtd, void *buf, int num_blocks)
471 int n, n_bad_blocks = 0;
473 u32 *n_bad_blocksp = buf + 0x4;
475 for (n = 0; n < num_blocks; n++) {
476 loff_t offset = n * mtd->erasesize;
477 if (mtd_block_isbad(mtd, offset)) {
484 *n_bad_blocksp = n_bad_blocks;
490 * return 1 - bad block
491 * return 0 - read successfully
492 * return < 0 - read failed
494 static int read_fcb(struct boot_config *boot_cfg, struct fcb_block *fcb,
497 struct mtd_info *mtd;
503 fcb_raw_page = kzalloc(mtd->writesize + mtd->oobsize, GFP_KERNEL);
505 if (mtd_block_isbad(mtd, off)) {
506 printf("Block %d is bad, skipped\n", (int)CONV_TO_BLOCKS(off));
511 * User BCH hardware to decode ECC for FCB
513 if (plat_config.misc_flags & FCB_ENCODE_BCH) {
514 size = sizeof(struct fcb_block);
516 /* switch nand BCH to FCB compatible settings */
517 if (plat_config.misc_flags & FCB_ENCODE_BCH_62b)
518 mxs_nand_mode_fcb_62bit(mtd);
519 else if (plat_config.misc_flags & FCB_ENCODE_BCH_40b)
520 mxs_nand_mode_fcb_40bit(mtd);
522 ret = nand_read(mtd, off, &size, (u_char *)fcb);
524 /* switch BCH back */
525 mxs_nand_mode_normal(mtd);
526 printf("NAND FCB read from 0x%llx offset 0x%zx read: %s\n",
527 off, size, ret ? "ERROR" : "OK");
529 } else if (plat_config.misc_flags & FCB_ENCODE_HAMMING) {
531 mtd_oob_ops_t ops = {
532 .datbuf = (u8 *)fcb_raw_page,
533 .oobbuf = ((u8 *)fcb_raw_page) + mtd->writesize,
534 .len = mtd->writesize,
535 .ooblen = mtd->oobsize,
539 ret = mtd_read_oob(mtd, off, &ops);
540 printf("NAND FCB read from 0x%llx offset 0x%zx read: %s\n",
541 off, ops.len, ret ? "ERROR" : "OK");
545 goto fcb_raw_page_err;
547 if ((plat_config.misc_flags & FCB_ENCODE_HAMMING) &&
548 (plat_config.misc_flags & FCB_LAYOUT_RESV_12B))
549 memcpy(fcb, fcb_raw_page + 12, sizeof(struct fcb_block));
551 /* TODO: check if it can pass Hamming check */
559 static int write_fcb(struct boot_config *boot_cfg, struct fcb_block *fcb)
561 struct mtd_info *mtd;
562 void *fcb_raw_page = NULL;
570 * We prepare raw page only for i.MX6, for i.MX7 we
571 * leverage BCH hw module instead
573 if ((plat_config.misc_flags & FCB_ENCODE_HAMMING) &&
574 (plat_config.misc_flags & FCB_LAYOUT_RESV_12B)) {
575 fcb_raw_page = kzalloc(mtd->writesize + mtd->oobsize,
578 debug("failed to allocate fcb_raw_page\n");
583 #if defined(CONFIG_MX6UL) || defined(CONFIG_MX6ULL)
584 /* 40 bit BCH, for i.MX6UL(L) */
585 encode_bch_ecc(fcb_raw_page + 32, fcb, 40);
587 memcpy(fcb_raw_page + 12, fcb, sizeof(struct fcb_block));
588 encode_hamming_13_8(fcb_raw_page + 12,
589 fcb_raw_page + 12 + 512, 512);
592 * Set the first and second byte of OOB data to 0xFF,
593 * not 0x00. These bytes are used as the Manufacturers Bad
594 * Block Marker (MBBM). Since the FCB is mostly written to
595 * the first page in a block, a scan for
596 * factory bad blocks will detect these blocks as bad, e.g.
597 * when function nand_scan_bbt() is executed to build a new
600 memset(fcb_raw_page + mtd->writesize, 0xFF, 2);
603 /* start writing FCB from the very beginning */
606 for (i = 0; i < g_boot_search_count; i++) {
607 if (mtd_block_isbad(mtd, off)) {
608 printf("Block %d is bad, skipped\n", i);
613 * User BCH hardware module to generate ECC for FCB
615 if (plat_config.misc_flags & FCB_ENCODE_BCH) {
616 size = sizeof(struct fcb_block);
618 /* switch nand BCH to FCB compatible settings */
619 if (plat_config.misc_flags & FCB_ENCODE_BCH_62b)
620 mxs_nand_mode_fcb_62bit(mtd);
621 else if (plat_config.misc_flags & FCB_ENCODE_BCH_40b)
622 mxs_nand_mode_fcb_40bit(mtd);
624 ret = nand_write(mtd, off, &size, (u_char *)fcb);
626 /* switch BCH back */
627 mxs_nand_mode_normal(mtd);
628 printf("NAND FCB write to 0x%zx offset 0x%llx written: %s\n",
629 size, off, ret ? "ERROR" : "OK");
631 } else if (plat_config.misc_flags & FCB_ENCODE_HAMMING) {
633 mtd_oob_ops_t ops = {
634 .datbuf = (u8 *)fcb_raw_page,
635 .oobbuf = ((u8 *)fcb_raw_page) +
637 .len = mtd->writesize,
638 .ooblen = mtd->oobsize,
642 ret = mtd_write_oob(mtd, off, &ops);
643 printf("NAND FCB write to 0x%llxx offset 0x%zx written: %s\n", off, ops.len, ret ? "ERROR" : "OK");
647 goto fcb_raw_page_err;
649 /* next writing location */
650 off += g_boot_search_stride;
662 * return 1 - bad block
663 * return 0 - read successfully
664 * return < 0 - read failed
666 static int read_dbbt(struct boot_config *boot_cfg, struct dbbt_block *dbbt,
667 void *dbbt_data_page, loff_t off)
670 struct mtd_info *mtd;
676 if (mtd_block_isbad(mtd, off)) {
677 printf("Block %d is bad, skipped\n",
678 (int)CONV_TO_BLOCKS(off));
682 size = sizeof(struct dbbt_block);
683 ret = nand_read(mtd, off, &size, (u_char *)dbbt);
684 printf("NAND DBBT read from 0x%llx offset 0x%zx read: %s\n",
685 off, size, ret ? "ERROR" : "OK");
689 /* dbbtpages == 0 if no bad blocks */
690 if (dbbt->dbbtpages > 0) {
691 to = off + 4 * mtd->writesize;
692 size = mtd->writesize;
693 ret = nand_read(mtd, to, &size, dbbt_data_page);
694 printf("DBBT data read from 0x%llx offset 0x%zx read: %s\n",
695 to, size, ret ? "ERROR" : "OK");
704 static int write_dbbt(struct boot_config *boot_cfg, struct dbbt_block *dbbt,
705 void *dbbt_data_page)
710 struct mtd_info *mtd;
715 /* start writing DBBT after all FCBs */
716 off = boot_cfg->search_area_size_in_bytes;
717 size = mtd->writesize;
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)));
726 ret = nand_write(mtd, off, &size, (u_char *)dbbt);
727 printf("NAND DBBT write to 0x%llx offset 0x%zx written: %s\n",
728 off, size, ret ? "ERROR" : "OK");
732 /* dbbtpages == 0 if no bad blocks */
733 if (dbbt->dbbtpages > 0) {
734 to = off + 4 * mtd->writesize;
735 ret = nand_write(mtd, to, &size, dbbt_data_page);
736 printf("DBBT data write to 0x%llx offset 0x%zx written: %s\n",
737 to, size, ret ? "ERROR" : "OK");
743 /* next writing location */
744 off += g_boot_search_stride;
750 /* reuse the check_skip_len from nand_util.c with minor change*/
751 static int check_skip_length(struct boot_config *boot_cfg, loff_t offset,
752 size_t length, size_t *used)
754 struct mtd_info *mtd = boot_cfg->mtd;
755 size_t maxsize = boot_cfg->maxsize;
756 size_t len_excl_bad = 0;
759 while (len_excl_bad < length) {
760 size_t block_len, block_off;
763 if (offset >= maxsize)
766 block_start = offset & ~(loff_t)(mtd->erasesize - 1);
767 block_off = offset & (mtd->erasesize - 1);
768 block_len = mtd->erasesize - block_off;
770 if (!nand_block_isbad(mtd, block_start))
771 len_excl_bad += block_len;
779 /* If the length is not a multiple of block_len, adjust. */
780 if (len_excl_bad > length)
781 *used -= (len_excl_bad - length);
786 static int nandbcb_get_next_good_blk_addr(struct boot_config *boot_cfg,
787 struct boot_stream_config *bs_cfg)
789 struct mtd_info *mtd = boot_cfg->mtd;
790 loff_t offset = bs_cfg->bs_addr;
791 size_t length = bs_cfg->bs_size;
795 ret = check_skip_length(boot_cfg, offset, length, &used);
800 /* get next image address */
801 bs_cfg->next_bs_addr = (u32)(offset + used + mtd->erasesize - 1)
802 / (u32)mtd->erasesize * mtd->erasesize;
807 static int nandbcb_write_bs_skip_bad(struct boot_config *boot_cfg,
808 struct boot_stream_config *bs_cfg)
810 struct mtd_info *mtd;
812 loff_t offset, maxsize;
816 bool padding_flag = false;
819 offset = bs_cfg->bs_addr;
820 maxsize = boot_cfg->maxsize;
821 size = bs_cfg->bs_size;
823 /* some boot images may need leading offset */
824 if (bs_cfg->need_padding &&
825 (plat_config.misc_flags & FIRMWARE_NEED_PADDING))
829 length = ALIGN(size + FLASH_OFFSET_STANDARD, mtd->writesize);
831 length = ALIGN(size, mtd->writesize);
833 buf = kzalloc(length, GFP_KERNEL);
835 printf("failed to allocate buffer for firmware\n");
841 memcpy(buf + FLASH_OFFSET_STANDARD, bs_cfg->bs_buf, size);
843 memcpy(buf, bs_cfg->bs_buf, size);
845 ret = nand_write_skip_bad(mtd, offset, &length, NULL, maxsize,
846 (u_char *)buf, WITH_WR_VERIFY);
847 printf("Write %s @0x%llx offset, 0x%zx bytes written: %s\n",
848 bs_cfg->bs_label, offset, length, ret ? "ERROR" : "OK");
851 /* write image failed, quit */
854 /* get next good blk address if needed */
855 if (bs_cfg->need_padding) {
856 ret = nandbcb_get_next_good_blk_addr(boot_cfg, bs_cfg);
858 printf("Next image cannot fit in NAND partition\n");
863 /* now we know how the exact image size written to NAND */
864 bs_cfg->bs_size = length;
871 static int nandbcb_write_fw(struct boot_config *boot_cfg, u_char *buf,
878 struct boot_stream_config bs_cfg;
881 for (i = 0; i < 2; ++i) {
882 if (!(FW_INX(i) & index))
886 offset = boot_cfg->boot_stream1_address;
887 size = boot_cfg->boot_stream1_size;
889 offset = boot_cfg->boot_stream2_address;
890 size = boot_cfg->boot_stream2_size;
894 if (!(plat_config.misc_flags & FIRMWARE_EXTRA_ONE)) {
895 memset(&bs_cfg, 0, sizeof(struct boot_stream_config));
896 sprintf(bs_cfg.bs_label, "firmware%d", i);
897 bs_cfg.bs_addr = offset;
898 bs_cfg.bs_size = size;
900 bs_cfg.need_padding = 1;
902 ret = nandbcb_write_bs_skip_bad(boot_cfg, &bs_cfg);
906 /* update the boot stream size */
908 boot_cfg->boot_stream1_size = bs_cfg.bs_size;
910 boot_cfg->boot_stream2_size = bs_cfg.bs_size;
913 /* some platforms need extra firmware */
914 memset(&bs_cfg, 0, sizeof(struct boot_stream_config));
915 sprintf(bs_cfg.bs_label, "fw%d_part%d", i, 1);
916 bs_cfg.bs_addr = offset;
917 bs_cfg.bs_size = IMX8MQ_HDMI_FW_SZ;
919 bs_cfg.need_padding = 1;
921 ret = nandbcb_write_bs_skip_bad(boot_cfg, &bs_cfg);
925 /* update the boot stream size */
927 boot_cfg->boot_stream1_size = bs_cfg.bs_size;
929 boot_cfg->boot_stream2_size = bs_cfg.bs_size;
931 /* get next image address */
932 next_bs_addr = bs_cfg.next_bs_addr;
934 memset(&bs_cfg, 0, sizeof(struct boot_stream_config));
935 sprintf(bs_cfg.bs_label, "fw%d_part%d", i, 2);
936 bs_cfg.bs_addr = next_bs_addr;
937 bs_cfg.bs_size = IMX8MQ_SPL_SZ;
938 bs_cfg.bs_buf = (u_char *)(buf + IMX8MQ_HDMI_FW_SZ);
939 bs_cfg.need_padding = 0;
941 ret = nandbcb_write_bs_skip_bad(boot_cfg, &bs_cfg);
950 static int nandbcb_init(struct boot_config *boot_cfg, u_char *buf)
952 struct mtd_info *mtd;
953 nand_erase_options_t opts;
954 struct fcb_block *fcb;
955 struct dbbt_block *dbbt;
956 void *dbbt_page, *dbbt_data_page;
961 maxsize = boot_cfg->maxsize;
962 off = boot_cfg->offset;
965 memset(&opts, 0, sizeof(opts));
967 opts.length = maxsize - 1;
968 ret = nand_erase_opts(mtd, &opts);
970 printf("%s: erase failed (ret = %d)\n", __func__, ret);
975 * Reference documentation from i.MX6DQRM section 8.5.2.2
977 * Nand Boot Control Block(BCB) contains two data structures,
978 * - Firmware Configuration Block(FCB)
979 * - Discovered Bad Block Table(DBBT)
983 * - DBBT search page address,
984 * - start page address of primary firmware
985 * - start page address of secondary firmware
988 * - number of blocks = mtd partition size / mtd erasesize
989 * - two firmware blocks, primary and secondary
990 * - first 4 block for FCB/DBBT
991 * - rest split in half for primary and secondary firmware
992 * - same firmware write twice
996 ret = nandbcb_write_fw(boot_cfg, buf, FW_ALL);
1001 fcb = kzalloc(sizeof(*fcb), GFP_KERNEL);
1003 debug("failed to allocate fcb\n");
1007 fill_fcb(fcb, boot_cfg);
1009 ret = write_fcb(boot_cfg, fcb);
1012 dbbt_page = kzalloc(mtd->writesize, GFP_KERNEL);
1014 debug("failed to allocate dbbt_page\n");
1019 dbbt_data_page = kzalloc(mtd->writesize, GFP_KERNEL);
1020 if (!dbbt_data_page) {
1021 debug("failed to allocate dbbt_data_page\n");
1028 dbbt->fingerprint = DBBT_FINGERPRINT;
1029 dbbt->version = DBBT_VERSION_1;
1030 ret = fill_dbbt_data(mtd, dbbt_data_page, CONV_TO_BLOCKS(maxsize));
1032 goto dbbt_data_page_err;
1034 dbbt->dbbtpages = 1;
1037 ret = write_dbbt(boot_cfg, dbbt, dbbt_data_page);
1039 printf("failed to write FCB/DBBT\n");
1042 kfree(dbbt_data_page);
1051 static int do_nandbcb_bcbonly(int argc, char * const argv[])
1053 struct fcb_block *fcb;
1054 struct dbbt_block *dbbt;
1055 struct mtd_info *mtd;
1056 nand_erase_options_t opts;
1059 void *dbbt_page, *dbbt_data_page;
1061 struct boot_config cfg;
1064 return CMD_RET_USAGE;
1066 memset(&cfg, 0, sizeof(struct boot_config));
1067 if (nandbcb_get_info(argc, argv, &cfg))
1068 return CMD_RET_FAILURE;
1070 /* only get the partition info */
1071 if (nandbcb_get_size(2, argv, 1, &cfg))
1072 return CMD_RET_FAILURE;
1074 if (nandbcb_set_boot_config(argc, argv, &cfg))
1075 return CMD_RET_FAILURE;
1079 cfg.boot_stream1_address = simple_strtoul(argv[2], NULL, 16);
1080 cfg.boot_stream1_size = simple_strtoul(argv[3], NULL, 16);
1081 cfg.boot_stream1_size = ALIGN(cfg.boot_stream1_size, mtd->writesize);
1084 cfg.boot_stream2_address = simple_strtoul(argv[4], NULL, 16);
1085 cfg.boot_stream2_size = simple_strtoul(argv[5], NULL, 16);
1086 cfg.boot_stream2_size = ALIGN(cfg.boot_stream2_size,
1091 nandbcb_check_space(&cfg);
1093 maxsize = cfg.maxsize;
1096 /* erase the previous FCB/DBBT */
1097 memset(&opts, 0, sizeof(opts));
1099 opts.length = g_boot_search_stride * 2;
1100 ret = nand_erase_opts(mtd, &opts);
1102 printf("%s: erase failed (ret = %d)\n", __func__, ret);
1103 return CMD_RET_FAILURE;
1107 fcb = kzalloc(sizeof(*fcb), GFP_KERNEL);
1109 printf("failed to allocate fcb\n");
1111 return CMD_RET_FAILURE;
1114 fill_fcb(fcb, &cfg);
1117 ret = write_fcb(&cfg, fcb);
1120 dbbt_page = kzalloc(mtd->writesize, GFP_KERNEL);
1122 printf("failed to allocate dbbt_page\n");
1127 dbbt_data_page = kzalloc(mtd->writesize, GFP_KERNEL);
1128 if (!dbbt_data_page) {
1129 printf("failed to allocate dbbt_data_page\n");
1136 dbbt->fingerprint = DBBT_FINGERPRINT;
1137 dbbt->version = DBBT_VERSION_1;
1138 ret = fill_dbbt_data(mtd, dbbt_data_page, CONV_TO_BLOCKS(maxsize));
1140 goto dbbt_data_page_err;
1142 dbbt->dbbtpages = 1;
1145 ret = write_dbbt(&cfg, dbbt, dbbt_data_page);
1148 kfree(dbbt_data_page);
1155 printf("failed to write FCB/DBBT\n");
1156 return CMD_RET_FAILURE;
1159 return CMD_RET_SUCCESS;
1162 /* dump data which is read from NAND chip */
1163 void dump_structure(struct boot_config *boot_cfg, struct fcb_block *fcb,
1164 struct dbbt_block *dbbt, void *dbbt_data_page)
1167 struct mtd_info *mtd = boot_cfg->mtd;
1169 #define P1(x) printf(" %s = 0x%08x\n", #x, fcb->x)
1175 #define P1(x) printf(" %s = %d\n", #x, fcb->x)
1220 #if !defined(CONFIG_MX6) || defined(CONFIG_MX6SX) || \
1221 defined(CONFIG_MX6UL) || defined(CONFIG_MX6ULL)
1222 P1(onfi_sync_enable);
1223 P1(onfi_sync_speed);
1224 P1(onfi_sync_nand_data);
1226 P1(disbbm_search_limit);
1227 P1(read_retry_enable);
1230 #define P1(x) printf(" %s = 0x%08x\n", #x, dbbt->x)
1236 #define P1(x) printf(" %s = %d\n", #x, dbbt->x)
1240 for (i = 0; i < dbbt->dbbtpages; ++i)
1241 printf("%d ", *((u32 *)(dbbt_data_page + i)));
1243 if (!(plat_config.misc_flags & FIRMWARE_EXTRA_ONE)) {
1244 printf("Firmware: image #0 @ 0x%x size 0x%x\n",
1245 fcb->fw1_start, fcb->fw1_pages * mtd->writesize);
1246 printf("Firmware: image #1 @ 0x%x size 0x%x\n",
1247 fcb->fw2_start, fcb->fw2_pages * mtd->writesize);
1249 printf("Firmware: image #0 @ 0x%x size 0x%x\n",
1250 fcb->fw1_start, fcb->fw1_pages * mtd->writesize);
1251 printf("Firmware: image #1 @ 0x%x size 0x%x\n",
1252 fcb->fw2_start, fcb->fw2_pages * mtd->writesize);
1253 /* TODO: Add extra image information */
1257 static bool check_fingerprint(void *data, int fingerprint)
1261 return (*(int *)(data + off) == fingerprint);
1264 static int fuse_to_search_count(u32 bank, u32 word, u32 mask, u32 off)
1270 /* by default, the boot search count from fuse should be 2 */
1271 err = fuse_read(bank, word, &val);
1275 val = (val & mask) >> off;
1293 static int nandbcb_dump(struct boot_config *boot_cfg)
1297 struct mtd_info *mtd = boot_cfg->mtd;
1298 struct fcb_block fcb, fcb_copy;
1299 struct dbbt_block dbbt, dbbt_copy;
1300 void *dbbt_data_page, *dbbt_data_page_copy;
1301 bool fcb_not_found, dbbt_not_found;
1304 dbbt_data_page = kzalloc(mtd->writesize, GFP_KERNEL);
1305 if (!dbbt_data_page) {
1306 printf("failed to allocate dbbt_data_page\n");
1311 dbbt_data_page_copy = kzalloc(mtd->writesize, GFP_KERNEL);
1312 if (!dbbt_data_page_copy) {
1313 printf("failed to allocate dbbt_data_page\n");
1321 for (i = 0; i < g_boot_search_count; ++i) {
1322 if (fcb_not_found) {
1323 ret = read_fcb(boot_cfg, &fcb, off);
1326 goto dbbt_page_copy_err;
1330 if (check_fingerprint(&fcb, FCB_FINGERPRINT))
1333 ret = read_fcb(boot_cfg, &fcb_copy, off);
1336 goto dbbt_page_copy_err;
1337 if (memcmp(&fcb, &fcb_copy,
1338 sizeof(struct fcb_block))) {
1339 printf("FCB copies are not identical\n");
1341 goto dbbt_page_copy_err;
1345 /* next read location */
1346 off += g_boot_search_stride;
1351 off = boot_cfg->search_area_size_in_bytes;
1352 for (i = 0; i < g_boot_search_count; ++i) {
1353 if (dbbt_not_found) {
1354 ret = read_dbbt(boot_cfg, &dbbt, dbbt_data_page, off);
1357 goto dbbt_page_copy_err;
1361 if (check_fingerprint(&dbbt, DBBT_FINGERPRINT))
1364 ret = read_dbbt(boot_cfg, &dbbt_copy,
1365 dbbt_data_page_copy, off);
1368 goto dbbt_page_copy_err;
1369 if (memcmp(&dbbt, &dbbt_copy,
1370 sizeof(struct dbbt_block))) {
1371 printf("DBBT copies are not identical\n");
1373 goto dbbt_page_copy_err;
1375 if (dbbt.dbbtpages > 0 &&
1376 memcmp(dbbt_data_page, dbbt_data_page_copy,
1378 printf("DBBT data copies are not identical\n");
1380 goto dbbt_page_copy_err;
1384 /* next read location */
1385 off += g_boot_search_stride;
1388 dump_structure(boot_cfg, &fcb, &dbbt, dbbt_data_page);
1391 kfree(dbbt_data_page_copy);
1393 kfree(dbbt_data_page);
1398 static int do_nandbcb_dump(int argc, char * const argv[])
1400 struct boot_config cfg;
1404 return CMD_RET_USAGE;
1406 memset(&cfg, 0, sizeof(struct boot_config));
1407 if (nandbcb_get_info(argc, argv, &cfg))
1408 return CMD_RET_FAILURE;
1410 if (nandbcb_get_size(argc, argv, 1, &cfg))
1411 return CMD_RET_FAILURE;
1413 if (nandbcb_set_boot_config(argc, argv, &cfg))
1414 return CMD_RET_FAILURE;
1416 ret = nandbcb_dump(&cfg);
1423 static int do_nandbcb_init(int argc, char * const argv[])
1430 struct boot_config cfg;
1433 return CMD_RET_USAGE;
1435 memset(&cfg, 0, sizeof(struct boot_config));
1436 if (nandbcb_get_info(argc, argv, &cfg))
1437 return CMD_RET_FAILURE;
1439 if (nandbcb_get_size(argc, argv, 2, &cfg))
1440 return CMD_RET_FAILURE;
1441 size = cfg.boot_stream1_size;
1443 if (nandbcb_set_boot_config(argc, argv, &cfg))
1444 return CMD_RET_FAILURE;
1446 addr = simple_strtoul(argv[1], &endp, 16);
1447 if (*argv[1] == 0 || *endp != 0)
1448 return CMD_RET_FAILURE;
1450 buf = map_physmem(addr, size, MAP_WRBACK);
1452 puts("failed to map physical memory\n");
1453 return CMD_RET_FAILURE;
1456 ret = nandbcb_init(&cfg, buf);
1458 return ret == 0 ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
1461 static int do_nandbcb(cmd_tbl_t *cmdtp, int flag, int argc,
1462 char * const argv[])
1470 /* check the platform config first */
1472 plat_config = imx6sx_plat_config;
1473 } else if (is_mx7()) {
1474 plat_config = imx7d_plat_config;
1475 } else if (is_mx6ul() || is_mx6ull()) {
1476 plat_config = imx6ul_plat_config;
1477 } else if (is_mx6() && !is_mx6sx() && !is_mx6ul() && !is_mx6ull()) {
1478 plat_config = imx6qdl_plat_config;
1479 } else if (is_imx8mq()) {
1480 plat_config = imx8mq_plat_config;
1481 } else if (is_imx8mm()) {
1482 plat_config = imx8mm_plat_config;
1483 } else if (is_imx8mn()) {
1484 plat_config = imx8mn_plat_config;
1485 } else if (is_imx8qm() || is_imx8qxp()) {
1486 plat_config = imx8q_plat_config;
1488 printf("ERROR: Unknown platform\n");
1489 return CMD_RET_FAILURE;
1492 if (plat_config.misc_flags & BT_SEARCH_CNT_FROM_FUSE) {
1494 g_boot_search_count = fuse_to_search_count(0, 720,
1496 printf("search count set to %d from fuse\n",
1497 g_boot_search_count);
1505 if (strcmp(cmd, "init") == 0) {
1506 ret = do_nandbcb_init(argc, argv);
1510 if (strcmp(cmd, "dump") == 0) {
1511 ret = do_nandbcb_dump(argc, argv);
1515 if (strcmp(cmd, "bcbonly") == 0) {
1516 ret = do_nandbcb_bcbonly(argc, argv);
1524 return CMD_RET_USAGE;
1527 #ifdef CONFIG_SYS_LONGHELP
1528 static char nandbcb_help_text[] =
1529 "init addr off|partition len - update 'len' bytes starting at\n"
1530 " 'off|part' to memory address 'addr', skipping bad blocks\n"
1531 "nandbcb bcbonly off|partition fw1-off fw1-size [fw2-off fw2-size]\n"
1532 " - write BCB only (FCB and DBBT)\n"
1533 " where `fwx-size` is fw sizes in bytes, `fw1-off`\n"
1534 " and `fw2-off` - firmware offsets\n"
1535 " FIY, BCB isn't erased automatically, so mtd erase should\n"
1536 " be called in advance before writing new BCB:\n"
1537 " > mtd erase mx7-bcb\n"
1538 "nandbcb dump off|partition - dump/verify boot structures\n";
1541 U_BOOT_CMD(nandbcb, 7, 1, do_nandbcb,
1542 "i.MX NAND Boot Control Blocks write",