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+
18 #include <dm/devres.h>
21 #include <jffs2/jffs2.h>
22 #include <linux/bch.h>
23 #include <linux/mtd/mtd.h>
25 #include <asm/arch/sys_proto.h>
26 #include <asm/mach-imx/imx-nandbcb.h>
27 #include <asm/mach-imx/imximage.cfg>
29 #include <linux/mtd/mtd.h>
33 #include "../../../cmd/legacy-mtd-utils.h"
35 /* FCB related flags */
36 /* FCB layout with leading 12B reserved */
37 #define FCB_LAYOUT_RESV_12B BIT(0)
38 /* FCB layout with leading 32B meta data */
39 #define FCB_LAYOUT_META_32B BIT(1)
40 /* FCB encrypted by Hamming code */
41 #define FCB_ENCODE_HAMMING BIT(2)
42 /* FCB encrypted by 40bit BCH */
43 #define FCB_ENCODE_BCH_40b BIT(3)
44 /* FCB encrypted by 62bit BCH */
45 #define FCB_ENCODE_BCH_62b BIT(4)
46 /* FCB encrypted by BCH */
47 #define FCB_ENCODE_BCH (FCB_ENCODE_BCH_40b | FCB_ENCODE_BCH_62b)
48 /* FCB data was randomized */
49 #define FCB_RANDON_ENABLED BIT(5)
51 /* Firmware related flags */
53 #define FIRMWARE_NEED_PADDING BIT(8)
55 #define FIRMWARE_EXTRA_ONE BIT(9)
56 /* Secondary firmware on fixed address */
57 #define FIRMWARE_SECONDARY_FIXED_ADDR BIT(10)
59 /* Boot search related flags */
60 #define BT_SEARCH_CNT_FROM_FUSE BIT(16)
62 struct platform_config {
66 static struct platform_config plat_config;
69 static struct platform_config imx6qdl_plat_config = {
70 .misc_flags = FCB_LAYOUT_RESV_12B |
72 FIRMWARE_NEED_PADDING,
75 static struct platform_config imx6sx_plat_config = {
76 .misc_flags = FCB_LAYOUT_META_32B |
78 FIRMWARE_NEED_PADDING |
82 static struct platform_config imx7d_plat_config = {
83 .misc_flags = FCB_LAYOUT_META_32B |
85 FIRMWARE_NEED_PADDING |
90 static struct platform_config imx6ul_plat_config = {
91 .misc_flags = FCB_LAYOUT_META_32B |
93 FIRMWARE_NEED_PADDING,
96 static struct platform_config imx8mq_plat_config = {
97 .misc_flags = FCB_LAYOUT_META_32B |
99 FIRMWARE_NEED_PADDING |
104 /* all other imx8mm */
105 static struct platform_config imx8mm_plat_config = {
106 .misc_flags = FCB_LAYOUT_META_32B |
108 FIRMWARE_NEED_PADDING |
113 static struct platform_config imx8mn_plat_config = {
114 .misc_flags = FCB_LAYOUT_META_32B |
117 FIRMWARE_SECONDARY_FIXED_ADDR |
118 BT_SEARCH_CNT_FROM_FUSE,
122 static struct platform_config imx8q_plat_config = {
123 .misc_flags = FCB_LAYOUT_META_32B |
126 FIRMWARE_SECONDARY_FIXED_ADDR |
127 BT_SEARCH_CNT_FROM_FUSE,
130 /* boot search related variables and definitions */
131 static int g_boot_search_count = 4;
132 static int g_boot_search_stride;
133 static int g_pages_per_stride;
135 /* mtd config structure */
138 struct mtd_info *mtd;
142 loff_t boot_stream1_address;
143 loff_t boot_stream2_address;
144 size_t boot_stream1_size;
145 size_t boot_stream2_size;
146 size_t max_boot_stream_size;
147 int stride_size_in_byte;
148 int search_area_size_in_bytes;
149 int search_area_size_in_pages;
150 int secondary_boot_stream_off_in_MB;
153 /* boot_stream config structure */
154 struct boot_stream_config {
166 #define FW_ALL FW1_ONLY | FW2_ONLY
167 #define FW_INX(x) (1 << (x))
169 /* NAND convert macros */
170 #define CONV_TO_PAGES(x) ((u32)(x) / (u32)(mtd->writesize))
171 #define CONV_TO_BLOCKS(x) ((u32)(x) / (u32)(mtd->erasesize))
173 #define GETBIT(v, n) (((v) >> (n)) & 0x1)
174 #define IMX8MQ_SPL_SZ 0x3e000
175 #define IMX8MQ_HDMI_FW_SZ 0x19c00
177 static int nandbcb_get_info(int argc, char * const argv[],
178 struct boot_config *boot_cfg)
181 struct mtd_info *mtd;
183 dev = nand_curr_device;
185 printf("failed to get nand_curr_device, run nand device\n");
186 return CMD_RET_FAILURE;
189 mtd = get_nand_dev_by_index(dev);
191 printf("failed to get mtd info\n");
192 return CMD_RET_FAILURE;
198 return CMD_RET_SUCCESS;
201 static int nandbcb_get_size(int argc, char * const argv[], int num,
202 struct boot_config *boot_cfg)
205 loff_t offset, size, maxsize;
206 struct mtd_info *mtd;
212 if (mtd_arg_off_size(argc - num, argv + num, &dev, &offset, &size,
213 &maxsize, MTD_DEV_TYPE_NAND, mtd->size))
214 return CMD_RET_FAILURE;
216 boot_cfg->maxsize = maxsize;
217 boot_cfg->offset = offset;
219 debug("max: %llx, offset: %llx\n", maxsize, offset);
221 if (size && size != maxsize)
222 boot_cfg->input_size = size;
224 return CMD_RET_SUCCESS;
227 static int nandbcb_set_boot_config(int argc, char * const argv[],
228 struct boot_config *boot_cfg)
230 struct mtd_info *mtd;
232 loff_t boot_stream1_address, boot_stream2_address, max_boot_stream_size;
234 if (!boot_cfg->mtd) {
235 printf("Didn't get the mtd info, quit\n");
236 return CMD_RET_FAILURE;
242 * set the search count as 4
243 * set each FCB/DBBT/Firmware offset at the beginning of blocks
244 * customers may change the value as needed
247 /* if need more compact layout, change these values */
248 /* g_boot_search_count was set as 4 at the definition*/
249 /* g_pages_per_stride was set as block size */
251 g_pages_per_stride = mtd->erasesize / mtd->writesize;
253 g_boot_search_stride = mtd->writesize * g_pages_per_stride;
255 boot_cfg->stride_size_in_byte = g_boot_search_stride * mtd->writesize;
256 boot_cfg->search_area_size_in_bytes =
257 g_boot_search_count * g_boot_search_stride;
258 boot_cfg->search_area_size_in_pages =
259 boot_cfg->search_area_size_in_bytes / mtd->writesize;
261 /* after FCB/DBBT, split the rest of area for two Firmwares */
262 if (!boot_cfg->maxsize) {
263 printf("Didn't get the maxsize, quit\n");
264 return CMD_RET_FAILURE;
266 maxsize = boot_cfg->maxsize;
267 /* align to page boundary */
268 maxsize = ((u32)(maxsize + mtd->writesize - 1)) / (u32)mtd->writesize
271 boot_stream1_address = 2 * boot_cfg->search_area_size_in_bytes;
272 boot_stream2_address = ((maxsize - boot_stream1_address) / 2 +
273 boot_stream1_address);
275 if (boot_cfg->secondary_boot_stream_off_in_MB)
276 boot_stream2_address = boot_cfg->secondary_boot_stream_off_in_MB * 1024 * 1024;
278 max_boot_stream_size = boot_stream2_address - boot_stream1_address;
281 if (max_boot_stream_size <= 0) {
282 debug("st1_addr: %llx, st2_addr: %llx, max: %llx\n",
283 boot_stream1_address, boot_stream2_address,
284 max_boot_stream_size);
285 printf("something wrong with firmware address settings\n");
286 return CMD_RET_FAILURE;
288 boot_cfg->boot_stream1_address = boot_stream1_address;
289 boot_cfg->boot_stream2_address = boot_stream2_address;
290 boot_cfg->max_boot_stream_size = max_boot_stream_size;
292 /* set the boot_stream size as the input size now */
293 if (boot_cfg->input_size) {
294 boot_cfg->boot_stream1_size = boot_cfg->input_size;
295 boot_cfg->boot_stream2_size = boot_cfg->input_size;
298 return CMD_RET_SUCCESS;
301 static int nandbcb_check_space(struct boot_config *boot_cfg)
303 size_t maxsize = boot_cfg->maxsize;
304 size_t max_boot_stream_size = boot_cfg->max_boot_stream_size;
305 loff_t boot_stream2_address = boot_cfg->boot_stream2_address;
307 if (boot_cfg->boot_stream1_size &&
308 boot_cfg->boot_stream1_size > max_boot_stream_size) {
309 printf("boot stream1 doesn't fit, check partition size or settings\n");
310 return CMD_RET_FAILURE;
313 if (boot_cfg->boot_stream2_size &&
314 boot_cfg->boot_stream2_size > maxsize - boot_stream2_address) {
315 printf("boot stream2 doesn't fit, check partition size or settings\n");
316 return CMD_RET_FAILURE;
319 return CMD_RET_SUCCESS;
322 #if defined(CONFIG_MX6UL) || defined(CONFIG_MX6ULL)
323 static uint8_t reverse_bit(uint8_t b)
325 b = (b & 0xf0) >> 4 | (b & 0x0f) << 4;
326 b = (b & 0xcc) >> 2 | (b & 0x33) << 2;
327 b = (b & 0xaa) >> 1 | (b & 0x55) << 1;
332 static void encode_bch_ecc(void *buf, struct fcb_block *fcb, int eccbits)
337 int ecc_buf_size = (m * eccbits + 7) / 8;
338 struct bch_control *bch = init_bch(m, eccbits, 0);
339 u8 *ecc_buf = kzalloc(ecc_buf_size, GFP_KERNEL);
340 u8 *tmp_buf = kzalloc(blocksize * numblocks, GFP_KERNEL);
344 * The blocks here are bit aligned. If eccbits is a multiple of 8,
345 * we just can copy bytes. Otherwiese we must move the blocks to
346 * the next free bit position.
348 WARN_ON(eccbits % 8);
350 memcpy(tmp_buf, fcb, sizeof(*fcb));
352 for (i = 0; i < numblocks; i++) {
353 memset(ecc_buf, 0, ecc_buf_size);
354 psrc = tmp_buf + i * blocksize;
355 pdst = buf + i * (blocksize + ecc_buf_size);
357 /* copy data byte aligned to destination buf */
358 memcpy(pdst, psrc, blocksize);
361 * imx-kobs use a modified encode_bch which reverse the
362 * bit order of the data before calculating bch.
363 * Do this in the buffer and use the bch lib here.
365 for (j = 0; j < blocksize; j++)
366 psrc[j] = reverse_bit(psrc[j]);
368 encode_bch(bch, psrc, blocksize, ecc_buf);
370 /* reverse ecc bit */
371 for (j = 0; j < ecc_buf_size; j++)
372 ecc_buf[j] = reverse_bit(ecc_buf[j]);
374 /* Here eccbuf is byte aligned and we can just copy it */
375 memcpy(pdst + blocksize, ecc_buf, ecc_buf_size);
384 static u8 calculate_parity_13_8(u8 d)
388 p |= (GETBIT(d, 6) ^ GETBIT(d, 5) ^ GETBIT(d, 3) ^ GETBIT(d, 2)) << 0;
389 p |= (GETBIT(d, 7) ^ GETBIT(d, 5) ^ GETBIT(d, 4) ^ GETBIT(d, 2) ^
391 p |= (GETBIT(d, 7) ^ GETBIT(d, 6) ^ GETBIT(d, 5) ^ GETBIT(d, 1) ^
393 p |= (GETBIT(d, 7) ^ GETBIT(d, 4) ^ GETBIT(d, 3) ^ GETBIT(d, 0)) << 3;
394 p |= (GETBIT(d, 6) ^ GETBIT(d, 4) ^ GETBIT(d, 3) ^ GETBIT(d, 2) ^
395 GETBIT(d, 1) ^ GETBIT(d, 0)) << 4;
400 static void encode_hamming_13_8(void *_src, void *_ecc, size_t size)
406 for (i = 0; i < size; i++)
407 ecc[i] = calculate_parity_13_8(src[i]);
411 static u32 calc_chksum(void *buf, size_t size)
417 for (i = 0; i < size; i++)
423 static void fill_fcb(struct fcb_block *fcb, struct boot_config *boot_cfg)
425 struct mtd_info *mtd = boot_cfg->mtd;
426 struct nand_chip *chip = mtd_to_nand(mtd);
427 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
428 struct mxs_nand_layout l;
430 mxs_nand_get_layout(mtd, &l);
432 fcb->fingerprint = FCB_FINGERPRINT;
433 fcb->version = FCB_VERSION_1;
437 fcb->addr_setup = 25;
438 fcb->dsample_time = 6;
440 fcb->pagesize = mtd->writesize;
441 fcb->oob_pagesize = mtd->writesize + mtd->oobsize;
442 fcb->sectors = mtd->erasesize / mtd->writesize;
444 fcb->meta_size = l.meta_size;
445 fcb->nr_blocks = l.nblocks;
446 fcb->ecc_nr = l.data0_size;
447 fcb->ecc_level = l.ecc0;
448 fcb->ecc_size = l.datan_size;
449 fcb->ecc_type = l.eccn;
450 fcb->bchtype = l.gf_len;
452 /* DBBT search area starts from the next block after all FCB */
453 fcb->dbbt_start = boot_cfg->search_area_size_in_pages;
455 fcb->bb_byte = nand_info->bch_geometry.block_mark_byte_offset;
456 fcb->bb_start_bit = nand_info->bch_geometry.block_mark_bit_offset;
458 fcb->phy_offset = mtd->writesize;
462 fcb->fw1_start = CONV_TO_PAGES(boot_cfg->boot_stream1_address);
463 fcb->fw2_start = CONV_TO_PAGES(boot_cfg->boot_stream2_address);
464 fcb->fw1_pages = CONV_TO_PAGES(boot_cfg->boot_stream1_size);
465 fcb->fw2_pages = CONV_TO_PAGES(boot_cfg->boot_stream2_size);
467 fcb->checksum = calc_chksum((void *)fcb + 4, sizeof(*fcb) - 4);
470 static int fill_dbbt_data(struct mtd_info *mtd, void *buf, int num_blocks)
472 int n, n_bad_blocks = 0;
474 u32 *n_bad_blocksp = buf + 0x4;
476 for (n = 0; n < num_blocks; n++) {
477 loff_t offset = n * mtd->erasesize;
478 if (mtd_block_isbad(mtd, offset)) {
485 *n_bad_blocksp = n_bad_blocks;
491 * return 1 - bad block
492 * return 0 - read successfully
493 * return < 0 - read failed
495 static int read_fcb(struct boot_config *boot_cfg, struct fcb_block *fcb,
498 struct mtd_info *mtd;
504 fcb_raw_page = kzalloc(mtd->writesize + mtd->oobsize, GFP_KERNEL);
506 if (mtd_block_isbad(mtd, off)) {
507 printf("Block %d is bad, skipped\n", (int)CONV_TO_BLOCKS(off));
512 * User BCH hardware to decode ECC for FCB
514 if (plat_config.misc_flags & FCB_ENCODE_BCH) {
515 size = sizeof(struct fcb_block);
517 /* switch nand BCH to FCB compatible settings */
518 if (plat_config.misc_flags & FCB_ENCODE_BCH_62b)
519 mxs_nand_mode_fcb_62bit(mtd);
520 else if (plat_config.misc_flags & FCB_ENCODE_BCH_40b)
521 mxs_nand_mode_fcb_40bit(mtd);
523 ret = nand_read(mtd, off, &size, (u_char *)fcb);
525 /* switch BCH back */
526 mxs_nand_mode_normal(mtd);
527 printf("NAND FCB read from 0x%llx offset 0x%zx read: %s\n",
528 off, size, ret ? "ERROR" : "OK");
530 } else if (plat_config.misc_flags & FCB_ENCODE_HAMMING) {
532 mtd_oob_ops_t ops = {
533 .datbuf = (u8 *)fcb_raw_page,
534 .oobbuf = ((u8 *)fcb_raw_page) + mtd->writesize,
535 .len = mtd->writesize,
536 .ooblen = mtd->oobsize,
540 ret = mtd_read_oob(mtd, off, &ops);
541 printf("NAND FCB read from 0x%llx offset 0x%zx read: %s\n",
542 off, ops.len, ret ? "ERROR" : "OK");
546 goto fcb_raw_page_err;
548 if ((plat_config.misc_flags & FCB_ENCODE_HAMMING) &&
549 (plat_config.misc_flags & FCB_LAYOUT_RESV_12B))
550 memcpy(fcb, fcb_raw_page + 12, sizeof(struct fcb_block));
552 /* TODO: check if it can pass Hamming check */
560 static int write_fcb(struct boot_config *boot_cfg, struct fcb_block *fcb)
562 struct mtd_info *mtd;
563 void *fcb_raw_page = NULL;
571 * We prepare raw page only for i.MX6, for i.MX7 we
572 * leverage BCH hw module instead
574 if ((plat_config.misc_flags & FCB_ENCODE_HAMMING) &&
575 (plat_config.misc_flags & FCB_LAYOUT_RESV_12B)) {
576 fcb_raw_page = kzalloc(mtd->writesize + mtd->oobsize,
579 debug("failed to allocate fcb_raw_page\n");
584 #if defined(CONFIG_MX6UL) || defined(CONFIG_MX6ULL)
585 /* 40 bit BCH, for i.MX6UL(L) */
586 encode_bch_ecc(fcb_raw_page + 32, fcb, 40);
588 memcpy(fcb_raw_page + 12, fcb, sizeof(struct fcb_block));
589 encode_hamming_13_8(fcb_raw_page + 12,
590 fcb_raw_page + 12 + 512, 512);
593 * Set the first and second byte of OOB data to 0xFF,
594 * not 0x00. These bytes are used as the Manufacturers Bad
595 * Block Marker (MBBM). Since the FCB is mostly written to
596 * the first page in a block, a scan for
597 * factory bad blocks will detect these blocks as bad, e.g.
598 * when function nand_scan_bbt() is executed to build a new
601 memset(fcb_raw_page + mtd->writesize, 0xFF, 2);
604 /* start writing FCB from the very beginning */
607 for (i = 0; i < g_boot_search_count; i++) {
608 if (mtd_block_isbad(mtd, off)) {
609 printf("Block %d is bad, skipped\n", i);
614 * User BCH hardware module to generate ECC for FCB
616 if (plat_config.misc_flags & FCB_ENCODE_BCH) {
617 size = sizeof(struct fcb_block);
619 /* switch nand BCH to FCB compatible settings */
620 if (plat_config.misc_flags & FCB_ENCODE_BCH_62b)
621 mxs_nand_mode_fcb_62bit(mtd);
622 else if (plat_config.misc_flags & FCB_ENCODE_BCH_40b)
623 mxs_nand_mode_fcb_40bit(mtd);
625 ret = nand_write(mtd, off, &size, (u_char *)fcb);
627 /* switch BCH back */
628 mxs_nand_mode_normal(mtd);
629 printf("NAND FCB write to 0x%zx offset 0x%llx written: %s\n",
630 size, off, ret ? "ERROR" : "OK");
632 } else if (plat_config.misc_flags & FCB_ENCODE_HAMMING) {
634 mtd_oob_ops_t ops = {
635 .datbuf = (u8 *)fcb_raw_page,
636 .oobbuf = ((u8 *)fcb_raw_page) +
638 .len = mtd->writesize,
639 .ooblen = mtd->oobsize,
643 ret = mtd_write_oob(mtd, off, &ops);
644 printf("NAND FCB write to 0x%llxx offset 0x%zx written: %s\n", off, ops.len, ret ? "ERROR" : "OK");
648 goto fcb_raw_page_err;
650 /* next writing location */
651 off += g_boot_search_stride;
663 * return 1 - bad block
664 * return 0 - read successfully
665 * return < 0 - read failed
667 static int read_dbbt(struct boot_config *boot_cfg, struct dbbt_block *dbbt,
668 void *dbbt_data_page, loff_t off)
671 struct mtd_info *mtd;
677 if (mtd_block_isbad(mtd, off)) {
678 printf("Block %d is bad, skipped\n",
679 (int)CONV_TO_BLOCKS(off));
683 size = sizeof(struct dbbt_block);
684 ret = nand_read(mtd, off, &size, (u_char *)dbbt);
685 printf("NAND DBBT read from 0x%llx offset 0x%zx read: %s\n",
686 off, size, ret ? "ERROR" : "OK");
690 /* dbbtpages == 0 if no bad blocks */
691 if (dbbt->dbbtpages > 0) {
692 to = off + 4 * mtd->writesize;
693 size = mtd->writesize;
694 ret = nand_read(mtd, to, &size, dbbt_data_page);
695 printf("DBBT data read from 0x%llx offset 0x%zx read: %s\n",
696 to, size, ret ? "ERROR" : "OK");
705 static int write_dbbt(struct boot_config *boot_cfg, struct dbbt_block *dbbt,
706 void *dbbt_data_page)
711 struct mtd_info *mtd;
716 /* start writing DBBT after all FCBs */
717 off = boot_cfg->search_area_size_in_bytes;
718 size = mtd->writesize;
720 for (i = 0; i < g_boot_search_count; i++) {
721 if (mtd_block_isbad(mtd, off)) {
722 printf("Block %d is bad, skipped\n",
723 (int)(i + CONV_TO_BLOCKS(off)));
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");
733 /* dbbtpages == 0 if no bad blocks */
734 if (dbbt->dbbtpages > 0) {
735 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");
744 /* next writing location */
745 off += g_boot_search_stride;
751 /* reuse the check_skip_len from nand_util.c with minor change*/
752 static int check_skip_length(struct boot_config *boot_cfg, loff_t offset,
753 size_t length, size_t *used)
755 struct mtd_info *mtd = boot_cfg->mtd;
756 size_t maxsize = boot_cfg->maxsize;
757 size_t len_excl_bad = 0;
760 while (len_excl_bad < length) {
761 size_t block_len, block_off;
764 if (offset >= maxsize)
767 block_start = offset & ~(loff_t)(mtd->erasesize - 1);
768 block_off = offset & (mtd->erasesize - 1);
769 block_len = mtd->erasesize - block_off;
771 if (!nand_block_isbad(mtd, block_start))
772 len_excl_bad += block_len;
780 /* If the length is not a multiple of block_len, adjust. */
781 if (len_excl_bad > length)
782 *used -= (len_excl_bad - length);
787 static int nandbcb_get_next_good_blk_addr(struct boot_config *boot_cfg,
788 struct boot_stream_config *bs_cfg)
790 struct mtd_info *mtd = boot_cfg->mtd;
791 loff_t offset = bs_cfg->bs_addr;
792 size_t length = bs_cfg->bs_size;
796 ret = check_skip_length(boot_cfg, offset, length, &used);
801 /* get next image address */
802 bs_cfg->next_bs_addr = (u32)(offset + used + mtd->erasesize - 1)
803 / (u32)mtd->erasesize * mtd->erasesize;
808 static int nandbcb_write_bs_skip_bad(struct boot_config *boot_cfg,
809 struct boot_stream_config *bs_cfg)
811 struct mtd_info *mtd;
813 loff_t offset, maxsize;
817 bool padding_flag = false;
820 offset = bs_cfg->bs_addr;
821 maxsize = boot_cfg->maxsize;
822 size = bs_cfg->bs_size;
824 /* some boot images may need leading offset */
825 if (bs_cfg->need_padding &&
826 (plat_config.misc_flags & FIRMWARE_NEED_PADDING))
830 length = ALIGN(size + FLASH_OFFSET_STANDARD, mtd->writesize);
832 length = ALIGN(size, mtd->writesize);
834 buf = kzalloc(length, GFP_KERNEL);
836 printf("failed to allocate buffer for firmware\n");
842 memcpy(buf + FLASH_OFFSET_STANDARD, bs_cfg->bs_buf, size);
844 memcpy(buf, bs_cfg->bs_buf, size);
846 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");
852 /* write image failed, quit */
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);
859 printf("Next image cannot fit in NAND partition\n");
864 /* now we know how the exact image size written to NAND */
865 bs_cfg->bs_size = length;
872 static int nandbcb_write_fw(struct boot_config *boot_cfg, u_char *buf,
879 struct boot_stream_config bs_cfg;
882 for (i = 0; i < 2; ++i) {
883 if (!(FW_INX(i) & index))
887 offset = boot_cfg->boot_stream1_address;
888 size = boot_cfg->boot_stream1_size;
890 offset = boot_cfg->boot_stream2_address;
891 size = boot_cfg->boot_stream2_size;
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;
901 bs_cfg.need_padding = 1;
903 ret = nandbcb_write_bs_skip_bad(boot_cfg, &bs_cfg);
907 /* update the boot stream size */
909 boot_cfg->boot_stream1_size = bs_cfg.bs_size;
911 boot_cfg->boot_stream2_size = bs_cfg.bs_size;
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;
920 bs_cfg.need_padding = 1;
922 ret = nandbcb_write_bs_skip_bad(boot_cfg, &bs_cfg);
926 /* update the boot stream size */
928 boot_cfg->boot_stream1_size = bs_cfg.bs_size;
930 boot_cfg->boot_stream2_size = bs_cfg.bs_size;
932 /* get next image address */
933 next_bs_addr = bs_cfg.next_bs_addr;
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;
942 ret = nandbcb_write_bs_skip_bad(boot_cfg, &bs_cfg);
951 static int nandbcb_init(struct boot_config *boot_cfg, u_char *buf)
953 struct mtd_info *mtd;
954 nand_erase_options_t opts;
955 struct fcb_block *fcb;
956 struct dbbt_block *dbbt;
957 void *dbbt_page, *dbbt_data_page;
962 maxsize = boot_cfg->maxsize;
963 off = boot_cfg->offset;
966 memset(&opts, 0, sizeof(opts));
968 opts.length = maxsize - 1;
969 ret = nand_erase_opts(mtd, &opts);
971 printf("%s: erase failed (ret = %d)\n", __func__, ret);
976 * Reference documentation from i.MX6DQRM section 8.5.2.2
978 * Nand Boot Control Block(BCB) contains two data structures,
979 * - Firmware Configuration Block(FCB)
980 * - Discovered Bad Block Table(DBBT)
984 * - DBBT search page address,
985 * - start page address of primary firmware
986 * - start page address of secondary firmware
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
993 * - same firmware write twice
997 ret = nandbcb_write_fw(boot_cfg, buf, FW_ALL);
1002 fcb = kzalloc(sizeof(*fcb), GFP_KERNEL);
1004 debug("failed to allocate fcb\n");
1008 fill_fcb(fcb, boot_cfg);
1010 ret = write_fcb(boot_cfg, fcb);
1013 dbbt_page = kzalloc(mtd->writesize, GFP_KERNEL);
1015 debug("failed to allocate dbbt_page\n");
1020 dbbt_data_page = kzalloc(mtd->writesize, GFP_KERNEL);
1021 if (!dbbt_data_page) {
1022 debug("failed to allocate dbbt_data_page\n");
1029 dbbt->fingerprint = DBBT_FINGERPRINT;
1030 dbbt->version = DBBT_VERSION_1;
1031 ret = fill_dbbt_data(mtd, dbbt_data_page, CONV_TO_BLOCKS(maxsize));
1033 goto dbbt_data_page_err;
1035 dbbt->dbbtpages = 1;
1038 ret = write_dbbt(boot_cfg, dbbt, dbbt_data_page);
1040 printf("failed to write FCB/DBBT\n");
1043 kfree(dbbt_data_page);
1052 static int do_nandbcb_bcbonly(int argc, char *const argv[])
1054 struct fcb_block *fcb;
1055 struct dbbt_block *dbbt;
1056 struct mtd_info *mtd;
1057 nand_erase_options_t opts;
1060 void *dbbt_page, *dbbt_data_page;
1062 struct boot_config cfg;
1065 return CMD_RET_USAGE;
1067 memset(&cfg, 0, sizeof(struct boot_config));
1068 if (nandbcb_get_info(argc, argv, &cfg))
1069 return CMD_RET_FAILURE;
1071 /* only get the partition info */
1072 if (nandbcb_get_size(2, argv, 1, &cfg))
1073 return CMD_RET_FAILURE;
1075 if (nandbcb_set_boot_config(argc, argv, &cfg))
1076 return CMD_RET_FAILURE;
1080 cfg.boot_stream1_address = simple_strtoul(argv[2], NULL, 16);
1081 cfg.boot_stream1_size = simple_strtoul(argv[3], NULL, 16);
1082 cfg.boot_stream1_size = ALIGN(cfg.boot_stream1_size, mtd->writesize);
1085 cfg.boot_stream2_address = simple_strtoul(argv[4], NULL, 16);
1086 cfg.boot_stream2_size = simple_strtoul(argv[5], NULL, 16);
1087 cfg.boot_stream2_size = ALIGN(cfg.boot_stream2_size,
1092 nandbcb_check_space(&cfg);
1094 maxsize = cfg.maxsize;
1097 /* erase the previous FCB/DBBT */
1098 memset(&opts, 0, sizeof(opts));
1100 opts.length = g_boot_search_stride * 2;
1101 ret = nand_erase_opts(mtd, &opts);
1103 printf("%s: erase failed (ret = %d)\n", __func__, ret);
1104 return CMD_RET_FAILURE;
1108 fcb = kzalloc(sizeof(*fcb), GFP_KERNEL);
1110 printf("failed to allocate fcb\n");
1112 return CMD_RET_FAILURE;
1115 fill_fcb(fcb, &cfg);
1118 ret = write_fcb(&cfg, fcb);
1121 dbbt_page = kzalloc(mtd->writesize, GFP_KERNEL);
1123 printf("failed to allocate dbbt_page\n");
1128 dbbt_data_page = kzalloc(mtd->writesize, GFP_KERNEL);
1129 if (!dbbt_data_page) {
1130 printf("failed to allocate dbbt_data_page\n");
1137 dbbt->fingerprint = DBBT_FINGERPRINT;
1138 dbbt->version = DBBT_VERSION_1;
1139 ret = fill_dbbt_data(mtd, dbbt_data_page, CONV_TO_BLOCKS(maxsize));
1141 goto dbbt_data_page_err;
1143 dbbt->dbbtpages = 1;
1146 ret = write_dbbt(&cfg, dbbt, dbbt_data_page);
1149 kfree(dbbt_data_page);
1156 printf("failed to write FCB/DBBT\n");
1157 return CMD_RET_FAILURE;
1160 return CMD_RET_SUCCESS;
1163 /* dump data which is read from NAND chip */
1164 void dump_structure(struct boot_config *boot_cfg, struct fcb_block *fcb,
1165 struct dbbt_block *dbbt, void *dbbt_data_page)
1168 struct mtd_info *mtd = boot_cfg->mtd;
1170 #define P1(x) printf(" %s = 0x%08x\n", #x, fcb->x)
1176 #define P1(x) printf(" %s = %d\n", #x, fcb->x)
1221 #if !defined(CONFIG_MX6) || defined(CONFIG_MX6SX) || \
1222 defined(CONFIG_MX6UL) || defined(CONFIG_MX6ULL)
1223 P1(onfi_sync_enable);
1224 P1(onfi_sync_speed);
1225 P1(onfi_sync_nand_data);
1227 P1(disbbm_search_limit);
1228 P1(read_retry_enable);
1231 #define P1(x) printf(" %s = 0x%08x\n", #x, dbbt->x)
1237 #define P1(x) printf(" %s = %d\n", #x, dbbt->x)
1241 for (i = 0; i < dbbt->dbbtpages; ++i)
1242 printf("%d ", *((u32 *)(dbbt_data_page + i)));
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);
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 */
1258 static bool check_fingerprint(void *data, int fingerprint)
1262 return (*(int *)(data + off) == fingerprint);
1265 static int fuse_to_search_count(u32 bank, u32 word, u32 mask, u32 off)
1271 /* by default, the boot search count from fuse should be 2 */
1272 err = fuse_read(bank, word, &val);
1276 val = (val & mask) >> off;
1294 static int nandbcb_dump(struct boot_config *boot_cfg)
1298 struct mtd_info *mtd = boot_cfg->mtd;
1299 struct fcb_block fcb, fcb_copy;
1300 struct dbbt_block dbbt, dbbt_copy;
1301 void *dbbt_data_page, *dbbt_data_page_copy;
1302 bool fcb_not_found, dbbt_not_found;
1305 dbbt_data_page = kzalloc(mtd->writesize, GFP_KERNEL);
1306 if (!dbbt_data_page) {
1307 printf("failed to allocate dbbt_data_page\n");
1312 dbbt_data_page_copy = kzalloc(mtd->writesize, GFP_KERNEL);
1313 if (!dbbt_data_page_copy) {
1314 printf("failed to allocate dbbt_data_page\n");
1322 for (i = 0; i < g_boot_search_count; ++i) {
1323 if (fcb_not_found) {
1324 ret = read_fcb(boot_cfg, &fcb, off);
1327 goto dbbt_page_copy_err;
1331 if (check_fingerprint(&fcb, FCB_FINGERPRINT))
1334 ret = read_fcb(boot_cfg, &fcb_copy, off);
1337 goto dbbt_page_copy_err;
1338 if (memcmp(&fcb, &fcb_copy,
1339 sizeof(struct fcb_block))) {
1340 printf("FCB copies are not identical\n");
1342 goto dbbt_page_copy_err;
1346 /* next read location */
1347 off += g_boot_search_stride;
1352 off = boot_cfg->search_area_size_in_bytes;
1353 for (i = 0; i < g_boot_search_count; ++i) {
1354 if (dbbt_not_found) {
1355 ret = read_dbbt(boot_cfg, &dbbt, dbbt_data_page, off);
1358 goto dbbt_page_copy_err;
1362 if (check_fingerprint(&dbbt, DBBT_FINGERPRINT))
1365 ret = read_dbbt(boot_cfg, &dbbt_copy,
1366 dbbt_data_page_copy, off);
1369 goto dbbt_page_copy_err;
1370 if (memcmp(&dbbt, &dbbt_copy,
1371 sizeof(struct dbbt_block))) {
1372 printf("DBBT copies are not identical\n");
1374 goto dbbt_page_copy_err;
1376 if (dbbt.dbbtpages > 0 &&
1377 memcmp(dbbt_data_page, dbbt_data_page_copy,
1379 printf("DBBT data copies are not identical\n");
1381 goto dbbt_page_copy_err;
1385 /* next read location */
1386 off += g_boot_search_stride;
1389 dump_structure(boot_cfg, &fcb, &dbbt, dbbt_data_page);
1392 kfree(dbbt_data_page_copy);
1394 kfree(dbbt_data_page);
1399 static int do_nandbcb_dump(int argc, char * const argv[])
1401 struct boot_config cfg;
1405 return CMD_RET_USAGE;
1407 memset(&cfg, 0, sizeof(struct boot_config));
1408 if (nandbcb_get_info(argc, argv, &cfg))
1409 return CMD_RET_FAILURE;
1411 if (nandbcb_get_size(argc, argv, 1, &cfg))
1412 return CMD_RET_FAILURE;
1414 if (nandbcb_set_boot_config(argc, argv, &cfg))
1415 return CMD_RET_FAILURE;
1417 ret = nandbcb_dump(&cfg);
1424 static int do_nandbcb_init(int argc, char * const argv[])
1431 struct boot_config cfg;
1434 return CMD_RET_USAGE;
1436 memset(&cfg, 0, sizeof(struct boot_config));
1437 if (nandbcb_get_info(argc, argv, &cfg))
1438 return CMD_RET_FAILURE;
1440 if (nandbcb_get_size(argc, argv, 2, &cfg))
1441 return CMD_RET_FAILURE;
1442 size = cfg.boot_stream1_size;
1444 if (nandbcb_set_boot_config(argc, argv, &cfg))
1445 return CMD_RET_FAILURE;
1447 addr = simple_strtoul(argv[1], &endp, 16);
1448 if (*argv[1] == 0 || *endp != 0)
1449 return CMD_RET_FAILURE;
1451 buf = map_physmem(addr, size, MAP_WRBACK);
1453 puts("failed to map physical memory\n");
1454 return CMD_RET_FAILURE;
1457 ret = nandbcb_init(&cfg, buf);
1459 return ret == 0 ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
1462 static int do_nandbcb(struct cmd_tbl *cmdtp, int flag, int argc,
1471 /* check the platform config first */
1473 plat_config = imx6sx_plat_config;
1474 } else if (is_mx7()) {
1475 plat_config = imx7d_plat_config;
1476 } else if (is_mx6ul() || is_mx6ull()) {
1477 plat_config = imx6ul_plat_config;
1478 } else if (is_mx6() && !is_mx6sx() && !is_mx6ul() && !is_mx6ull()) {
1479 plat_config = imx6qdl_plat_config;
1480 } else if (is_imx8mq()) {
1481 plat_config = imx8mq_plat_config;
1482 } else if (is_imx8mm()) {
1483 plat_config = imx8mm_plat_config;
1484 } else if (is_imx8mn()) {
1485 plat_config = imx8mn_plat_config;
1486 } else if (is_imx8qm() || is_imx8qxp()) {
1487 plat_config = imx8q_plat_config;
1489 printf("ERROR: Unknown platform\n");
1490 return CMD_RET_FAILURE;
1493 if (plat_config.misc_flags & BT_SEARCH_CNT_FROM_FUSE) {
1495 g_boot_search_count = fuse_to_search_count(0, 720,
1497 printf("search count set to %d from fuse\n",
1498 g_boot_search_count);
1506 if (strcmp(cmd, "init") == 0) {
1507 ret = do_nandbcb_init(argc, argv);
1511 if (strcmp(cmd, "dump") == 0) {
1512 ret = do_nandbcb_dump(argc, argv);
1516 if (strcmp(cmd, "bcbonly") == 0) {
1517 ret = do_nandbcb_bcbonly(argc, argv);
1525 return CMD_RET_USAGE;
1528 #ifdef CONFIG_SYS_LONGHELP
1529 static char nandbcb_help_text[] =
1530 "init addr off|partition len - update 'len' bytes starting at\n"
1531 " 'off|part' to memory address 'addr', skipping bad blocks\n"
1532 "nandbcb bcbonly off|partition fw1-off fw1-size [fw2-off fw2-size]\n"
1533 " - write BCB only (FCB and DBBT)\n"
1534 " where `fwx-size` is fw sizes in bytes, `fw1-off`\n"
1535 " and `fw2-off` - firmware offsets\n"
1536 " FIY, BCB isn't erased automatically, so mtd erase should\n"
1537 " be called in advance before writing new BCB:\n"
1538 " > mtd erase mx7-bcb\n"
1539 "nandbcb dump off|partition - dump/verify boot structures\n";
1542 U_BOOT_CMD(nandbcb, 7, 1, do_nandbcb,
1543 "i.MX NAND Boot Control Blocks write",