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+
19 #include <dm/devres.h>
20 #include <linux/bug.h>
23 #include <jffs2/jffs2.h>
24 #include <linux/bch.h>
25 #include <linux/mtd/mtd.h>
27 #include <asm/arch/sys_proto.h>
28 #include <asm/mach-imx/imx-nandbcb.h>
29 #include <asm/mach-imx/imximage.cfg>
31 #include <linux/mtd/mtd.h>
35 #include "../../../cmd/legacy-mtd-utils.h"
37 /* 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)
53 /* Firmware related flags */
55 #define FIRMWARE_NEED_PADDING BIT(8)
57 #define FIRMWARE_EXTRA_ONE BIT(9)
58 /* Secondary firmware on fixed address */
59 #define FIRMWARE_SECONDARY_FIXED_ADDR BIT(10)
61 /* Boot search related flags */
62 #define BT_SEARCH_CNT_FROM_FUSE BIT(16)
64 struct platform_config {
68 static struct platform_config plat_config;
71 static struct platform_config imx6qdl_plat_config = {
72 .misc_flags = FCB_LAYOUT_RESV_12B |
74 FIRMWARE_NEED_PADDING,
77 static struct platform_config imx6sx_plat_config = {
78 .misc_flags = FCB_LAYOUT_META_32B |
80 FIRMWARE_NEED_PADDING |
84 static struct platform_config imx7d_plat_config = {
85 .misc_flags = FCB_LAYOUT_META_32B |
87 FIRMWARE_NEED_PADDING |
92 static struct platform_config imx6ul_plat_config = {
93 .misc_flags = FCB_LAYOUT_META_32B |
95 FIRMWARE_NEED_PADDING,
98 static struct platform_config imx8mq_plat_config = {
99 .misc_flags = FCB_LAYOUT_META_32B |
101 FIRMWARE_NEED_PADDING |
106 /* all other imx8mm */
107 static struct platform_config imx8mm_plat_config = {
108 .misc_flags = FCB_LAYOUT_META_32B |
110 FIRMWARE_NEED_PADDING |
115 static struct platform_config imx8mn_plat_config = {
116 .misc_flags = FCB_LAYOUT_META_32B |
119 FIRMWARE_SECONDARY_FIXED_ADDR |
120 BT_SEARCH_CNT_FROM_FUSE,
124 static struct platform_config imx8q_plat_config = {
125 .misc_flags = FCB_LAYOUT_META_32B |
128 FIRMWARE_SECONDARY_FIXED_ADDR |
129 BT_SEARCH_CNT_FROM_FUSE,
132 /* boot search related variables and definitions */
133 static int g_boot_search_count = 4;
134 static int g_boot_search_stride;
135 static int g_pages_per_stride;
137 /* mtd config structure */
140 struct mtd_info *mtd;
144 loff_t boot_stream1_address;
145 loff_t boot_stream2_address;
146 size_t boot_stream1_size;
147 size_t boot_stream2_size;
148 size_t max_boot_stream_size;
149 int stride_size_in_byte;
150 int search_area_size_in_bytes;
151 int search_area_size_in_pages;
152 int secondary_boot_stream_off_in_MB;
155 /* boot_stream config structure */
156 struct boot_stream_config {
168 #define FW_ALL FW1_ONLY | FW2_ONLY
169 #define FW_INX(x) (1 << (x))
171 /* NAND convert macros */
172 #define CONV_TO_PAGES(x) ((u32)(x) / (u32)(mtd->writesize))
173 #define CONV_TO_BLOCKS(x) ((u32)(x) / (u32)(mtd->erasesize))
175 #define GETBIT(v, n) (((v) >> (n)) & 0x1)
176 #define IMX8MQ_SPL_SZ 0x3e000
177 #define IMX8MQ_HDMI_FW_SZ 0x19c00
179 static int nandbcb_get_info(int argc, char * const argv[],
180 struct boot_config *boot_cfg)
183 struct mtd_info *mtd;
185 dev = nand_curr_device;
187 printf("failed to get nand_curr_device, run nand device\n");
188 return CMD_RET_FAILURE;
191 mtd = get_nand_dev_by_index(dev);
193 printf("failed to get mtd info\n");
194 return CMD_RET_FAILURE;
200 return CMD_RET_SUCCESS;
203 static int nandbcb_get_size(int argc, char * const argv[], int num,
204 struct boot_config *boot_cfg)
207 loff_t offset, size, maxsize;
208 struct mtd_info *mtd;
214 if (mtd_arg_off_size(argc - num, argv + num, &dev, &offset, &size,
215 &maxsize, MTD_DEV_TYPE_NAND, mtd->size))
216 return CMD_RET_FAILURE;
218 boot_cfg->maxsize = maxsize;
219 boot_cfg->offset = offset;
221 debug("max: %llx, offset: %llx\n", maxsize, offset);
223 if (size && size != maxsize)
224 boot_cfg->input_size = size;
226 return CMD_RET_SUCCESS;
229 static int nandbcb_set_boot_config(int argc, char * const argv[],
230 struct boot_config *boot_cfg)
232 struct mtd_info *mtd;
234 loff_t boot_stream1_address, boot_stream2_address, max_boot_stream_size;
236 if (!boot_cfg->mtd) {
237 printf("Didn't get the mtd info, quit\n");
238 return CMD_RET_FAILURE;
244 * set the search count as 4
245 * set each FCB/DBBT/Firmware offset at the beginning of blocks
246 * customers may change the value as needed
249 /* if need more compact layout, change these values */
250 /* g_boot_search_count was set as 4 at the definition*/
251 /* g_pages_per_stride was set as block size */
253 g_pages_per_stride = mtd->erasesize / mtd->writesize;
255 g_boot_search_stride = mtd->writesize * g_pages_per_stride;
257 boot_cfg->stride_size_in_byte = g_boot_search_stride * mtd->writesize;
258 boot_cfg->search_area_size_in_bytes =
259 g_boot_search_count * g_boot_search_stride;
260 boot_cfg->search_area_size_in_pages =
261 boot_cfg->search_area_size_in_bytes / mtd->writesize;
263 /* after FCB/DBBT, split the rest of area for two Firmwares */
264 if (!boot_cfg->maxsize) {
265 printf("Didn't get the maxsize, quit\n");
266 return CMD_RET_FAILURE;
268 maxsize = boot_cfg->maxsize;
269 /* align to page boundary */
270 maxsize = ((u32)(maxsize + mtd->writesize - 1)) / (u32)mtd->writesize
273 boot_stream1_address = 2 * boot_cfg->search_area_size_in_bytes;
274 boot_stream2_address = ((maxsize - boot_stream1_address) / 2 +
275 boot_stream1_address);
277 if (boot_cfg->secondary_boot_stream_off_in_MB)
278 boot_stream2_address = boot_cfg->secondary_boot_stream_off_in_MB * 1024 * 1024;
280 max_boot_stream_size = boot_stream2_address - boot_stream1_address;
283 if (max_boot_stream_size <= 0) {
284 debug("st1_addr: %llx, st2_addr: %llx, max: %llx\n",
285 boot_stream1_address, boot_stream2_address,
286 max_boot_stream_size);
287 printf("something wrong with firmware address settings\n");
288 return CMD_RET_FAILURE;
290 boot_cfg->boot_stream1_address = boot_stream1_address;
291 boot_cfg->boot_stream2_address = boot_stream2_address;
292 boot_cfg->max_boot_stream_size = max_boot_stream_size;
294 /* set the boot_stream size as the input size now */
295 if (boot_cfg->input_size) {
296 boot_cfg->boot_stream1_size = boot_cfg->input_size;
297 boot_cfg->boot_stream2_size = boot_cfg->input_size;
300 return CMD_RET_SUCCESS;
303 static int nandbcb_check_space(struct boot_config *boot_cfg)
305 size_t maxsize = boot_cfg->maxsize;
306 size_t max_boot_stream_size = boot_cfg->max_boot_stream_size;
307 loff_t boot_stream2_address = boot_cfg->boot_stream2_address;
309 if (boot_cfg->boot_stream1_size &&
310 boot_cfg->boot_stream1_size > max_boot_stream_size) {
311 printf("boot stream1 doesn't fit, check partition size or settings\n");
312 return CMD_RET_FAILURE;
315 if (boot_cfg->boot_stream2_size &&
316 boot_cfg->boot_stream2_size > maxsize - boot_stream2_address) {
317 printf("boot stream2 doesn't fit, check partition size or settings\n");
318 return CMD_RET_FAILURE;
321 return CMD_RET_SUCCESS;
324 #if defined(CONFIG_MX6UL) || defined(CONFIG_MX6ULL)
325 static uint8_t reverse_bit(uint8_t b)
327 b = (b & 0xf0) >> 4 | (b & 0x0f) << 4;
328 b = (b & 0xcc) >> 2 | (b & 0x33) << 2;
329 b = (b & 0xaa) >> 1 | (b & 0x55) << 1;
334 static void encode_bch_ecc(void *buf, struct fcb_block *fcb, int eccbits)
339 int ecc_buf_size = (m * eccbits + 7) / 8;
340 struct bch_control *bch = init_bch(m, eccbits, 0);
341 u8 *ecc_buf = kzalloc(ecc_buf_size, GFP_KERNEL);
342 u8 *tmp_buf = kzalloc(blocksize * numblocks, GFP_KERNEL);
346 * The blocks here are bit aligned. If eccbits is a multiple of 8,
347 * we just can copy bytes. Otherwiese we must move the blocks to
348 * the next free bit position.
350 WARN_ON(eccbits % 8);
352 memcpy(tmp_buf, fcb, sizeof(*fcb));
354 for (i = 0; i < numblocks; i++) {
355 memset(ecc_buf, 0, ecc_buf_size);
356 psrc = tmp_buf + i * blocksize;
357 pdst = buf + i * (blocksize + ecc_buf_size);
359 /* copy data byte aligned to destination buf */
360 memcpy(pdst, psrc, blocksize);
363 * imx-kobs use a modified encode_bch which reverse the
364 * bit order of the data before calculating bch.
365 * Do this in the buffer and use the bch lib here.
367 for (j = 0; j < blocksize; j++)
368 psrc[j] = reverse_bit(psrc[j]);
370 encode_bch(bch, psrc, blocksize, ecc_buf);
372 /* reverse ecc bit */
373 for (j = 0; j < ecc_buf_size; j++)
374 ecc_buf[j] = reverse_bit(ecc_buf[j]);
376 /* Here eccbuf is byte aligned and we can just copy it */
377 memcpy(pdst + blocksize, ecc_buf, ecc_buf_size);
386 static u8 calculate_parity_13_8(u8 d)
390 p |= (GETBIT(d, 6) ^ GETBIT(d, 5) ^ GETBIT(d, 3) ^ GETBIT(d, 2)) << 0;
391 p |= (GETBIT(d, 7) ^ GETBIT(d, 5) ^ GETBIT(d, 4) ^ GETBIT(d, 2) ^
393 p |= (GETBIT(d, 7) ^ GETBIT(d, 6) ^ GETBIT(d, 5) ^ GETBIT(d, 1) ^
395 p |= (GETBIT(d, 7) ^ GETBIT(d, 4) ^ GETBIT(d, 3) ^ GETBIT(d, 0)) << 3;
396 p |= (GETBIT(d, 6) ^ GETBIT(d, 4) ^ GETBIT(d, 3) ^ GETBIT(d, 2) ^
397 GETBIT(d, 1) ^ GETBIT(d, 0)) << 4;
402 static void encode_hamming_13_8(void *_src, void *_ecc, size_t size)
408 for (i = 0; i < size; i++)
409 ecc[i] = calculate_parity_13_8(src[i]);
413 static u32 calc_chksum(void *buf, size_t size)
419 for (i = 0; i < size; i++)
425 static void fill_fcb(struct fcb_block *fcb, struct boot_config *boot_cfg)
427 struct mtd_info *mtd = boot_cfg->mtd;
428 struct nand_chip *chip = mtd_to_nand(mtd);
429 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
430 struct mxs_nand_layout l;
432 mxs_nand_get_layout(mtd, &l);
434 fcb->fingerprint = FCB_FINGERPRINT;
435 fcb->version = FCB_VERSION_1;
439 fcb->addr_setup = 25;
440 fcb->dsample_time = 6;
442 fcb->pagesize = mtd->writesize;
443 fcb->oob_pagesize = mtd->writesize + mtd->oobsize;
444 fcb->sectors = mtd->erasesize / mtd->writesize;
446 fcb->meta_size = l.meta_size;
447 fcb->nr_blocks = l.nblocks;
448 fcb->ecc_nr = l.data0_size;
449 fcb->ecc_level = l.ecc0;
450 fcb->ecc_size = l.datan_size;
451 fcb->ecc_type = l.eccn;
452 fcb->bchtype = l.gf_len;
454 /* DBBT search area starts from the next block after all FCB */
455 fcb->dbbt_start = boot_cfg->search_area_size_in_pages;
457 fcb->bb_byte = nand_info->bch_geometry.block_mark_byte_offset;
458 fcb->bb_start_bit = nand_info->bch_geometry.block_mark_bit_offset;
460 fcb->phy_offset = mtd->writesize;
464 fcb->fw1_start = CONV_TO_PAGES(boot_cfg->boot_stream1_address);
465 fcb->fw2_start = CONV_TO_PAGES(boot_cfg->boot_stream2_address);
466 fcb->fw1_pages = CONV_TO_PAGES(boot_cfg->boot_stream1_size);
467 fcb->fw2_pages = CONV_TO_PAGES(boot_cfg->boot_stream2_size);
469 fcb->checksum = calc_chksum((void *)fcb + 4, sizeof(*fcb) - 4);
472 static int fill_dbbt_data(struct mtd_info *mtd, void *buf, int num_blocks)
474 int n, n_bad_blocks = 0;
476 u32 *n_bad_blocksp = buf + 0x4;
478 for (n = 0; n < num_blocks; n++) {
479 loff_t offset = n * mtd->erasesize;
480 if (mtd_block_isbad(mtd, offset)) {
487 *n_bad_blocksp = n_bad_blocks;
493 * return 1 - bad block
494 * return 0 - read successfully
495 * return < 0 - read failed
497 static int read_fcb(struct boot_config *boot_cfg, struct fcb_block *fcb,
500 struct mtd_info *mtd;
506 fcb_raw_page = kzalloc(mtd->writesize + mtd->oobsize, GFP_KERNEL);
508 if (mtd_block_isbad(mtd, off)) {
509 printf("Block %d is bad, skipped\n", (int)CONV_TO_BLOCKS(off));
514 * User BCH hardware to decode ECC for FCB
516 if (plat_config.misc_flags & FCB_ENCODE_BCH) {
517 size = sizeof(struct fcb_block);
519 /* switch nand BCH to FCB compatible settings */
520 if (plat_config.misc_flags & FCB_ENCODE_BCH_62b)
521 mxs_nand_mode_fcb_62bit(mtd);
522 else if (plat_config.misc_flags & FCB_ENCODE_BCH_40b)
523 mxs_nand_mode_fcb_40bit(mtd);
525 ret = nand_read(mtd, off, &size, (u_char *)fcb);
527 /* switch BCH back */
528 mxs_nand_mode_normal(mtd);
529 printf("NAND FCB read from 0x%llx offset 0x%zx read: %s\n",
530 off, size, ret ? "ERROR" : "OK");
532 } else if (plat_config.misc_flags & FCB_ENCODE_HAMMING) {
534 mtd_oob_ops_t ops = {
535 .datbuf = (u8 *)fcb_raw_page,
536 .oobbuf = ((u8 *)fcb_raw_page) + mtd->writesize,
537 .len = mtd->writesize,
538 .ooblen = mtd->oobsize,
542 ret = mtd_read_oob(mtd, off, &ops);
543 printf("NAND FCB read from 0x%llx offset 0x%zx read: %s\n",
544 off, ops.len, ret ? "ERROR" : "OK");
548 goto fcb_raw_page_err;
550 if ((plat_config.misc_flags & FCB_ENCODE_HAMMING) &&
551 (plat_config.misc_flags & FCB_LAYOUT_RESV_12B))
552 memcpy(fcb, fcb_raw_page + 12, sizeof(struct fcb_block));
554 /* TODO: check if it can pass Hamming check */
562 static int write_fcb(struct boot_config *boot_cfg, struct fcb_block *fcb)
564 struct mtd_info *mtd;
565 void *fcb_raw_page = NULL;
573 * We prepare raw page only for i.MX6, for i.MX7 we
574 * leverage BCH hw module instead
576 if ((plat_config.misc_flags & FCB_ENCODE_HAMMING) &&
577 (plat_config.misc_flags & FCB_LAYOUT_RESV_12B)) {
578 fcb_raw_page = kzalloc(mtd->writesize + mtd->oobsize,
581 debug("failed to allocate fcb_raw_page\n");
586 #if defined(CONFIG_MX6UL) || defined(CONFIG_MX6ULL)
587 /* 40 bit BCH, for i.MX6UL(L) */
588 encode_bch_ecc(fcb_raw_page + 32, fcb, 40);
590 memcpy(fcb_raw_page + 12, fcb, sizeof(struct fcb_block));
591 encode_hamming_13_8(fcb_raw_page + 12,
592 fcb_raw_page + 12 + 512, 512);
595 * Set the first and second byte of OOB data to 0xFF,
596 * not 0x00. These bytes are used as the Manufacturers Bad
597 * Block Marker (MBBM). Since the FCB is mostly written to
598 * the first page in a block, a scan for
599 * factory bad blocks will detect these blocks as bad, e.g.
600 * when function nand_scan_bbt() is executed to build a new
603 memset(fcb_raw_page + mtd->writesize, 0xFF, 2);
606 /* start writing FCB from the very beginning */
609 for (i = 0; i < g_boot_search_count; i++) {
610 if (mtd_block_isbad(mtd, off)) {
611 printf("Block %d is bad, skipped\n", i);
616 * User BCH hardware module to generate ECC for FCB
618 if (plat_config.misc_flags & FCB_ENCODE_BCH) {
619 size = sizeof(struct fcb_block);
621 /* switch nand BCH to FCB compatible settings */
622 if (plat_config.misc_flags & FCB_ENCODE_BCH_62b)
623 mxs_nand_mode_fcb_62bit(mtd);
624 else if (plat_config.misc_flags & FCB_ENCODE_BCH_40b)
625 mxs_nand_mode_fcb_40bit(mtd);
627 ret = nand_write(mtd, off, &size, (u_char *)fcb);
629 /* switch BCH back */
630 mxs_nand_mode_normal(mtd);
631 printf("NAND FCB write to 0x%zx offset 0x%llx written: %s\n",
632 size, off, ret ? "ERROR" : "OK");
634 } else if (plat_config.misc_flags & FCB_ENCODE_HAMMING) {
636 mtd_oob_ops_t ops = {
637 .datbuf = (u8 *)fcb_raw_page,
638 .oobbuf = ((u8 *)fcb_raw_page) +
640 .len = mtd->writesize,
641 .ooblen = mtd->oobsize,
645 ret = mtd_write_oob(mtd, off, &ops);
646 printf("NAND FCB write to 0x%llxx offset 0x%zx written: %s\n", off, ops.len, ret ? "ERROR" : "OK");
650 goto fcb_raw_page_err;
652 /* next writing location */
653 off += g_boot_search_stride;
665 * return 1 - bad block
666 * return 0 - read successfully
667 * return < 0 - read failed
669 static int read_dbbt(struct boot_config *boot_cfg, struct dbbt_block *dbbt,
670 void *dbbt_data_page, loff_t off)
673 struct mtd_info *mtd;
679 if (mtd_block_isbad(mtd, off)) {
680 printf("Block %d is bad, skipped\n",
681 (int)CONV_TO_BLOCKS(off));
685 size = sizeof(struct dbbt_block);
686 ret = nand_read(mtd, off, &size, (u_char *)dbbt);
687 printf("NAND DBBT read from 0x%llx offset 0x%zx read: %s\n",
688 off, size, ret ? "ERROR" : "OK");
692 /* dbbtpages == 0 if no bad blocks */
693 if (dbbt->dbbtpages > 0) {
694 to = off + 4 * mtd->writesize;
695 size = mtd->writesize;
696 ret = nand_read(mtd, to, &size, dbbt_data_page);
697 printf("DBBT data read from 0x%llx offset 0x%zx read: %s\n",
698 to, size, ret ? "ERROR" : "OK");
707 static int write_dbbt(struct boot_config *boot_cfg, struct dbbt_block *dbbt,
708 void *dbbt_data_page)
713 struct mtd_info *mtd;
718 /* start writing DBBT after all FCBs */
719 off = boot_cfg->search_area_size_in_bytes;
720 size = mtd->writesize;
722 for (i = 0; i < g_boot_search_count; i++) {
723 if (mtd_block_isbad(mtd, off)) {
724 printf("Block %d is bad, skipped\n",
725 (int)(i + CONV_TO_BLOCKS(off)));
729 ret = nand_write(mtd, off, &size, (u_char *)dbbt);
730 printf("NAND DBBT write to 0x%llx offset 0x%zx written: %s\n",
731 off, size, ret ? "ERROR" : "OK");
735 /* dbbtpages == 0 if no bad blocks */
736 if (dbbt->dbbtpages > 0) {
737 to = off + 4 * mtd->writesize;
738 ret = nand_write(mtd, to, &size, dbbt_data_page);
739 printf("DBBT data write to 0x%llx offset 0x%zx written: %s\n",
740 to, size, ret ? "ERROR" : "OK");
746 /* next writing location */
747 off += g_boot_search_stride;
753 /* reuse the check_skip_len from nand_util.c with minor change*/
754 static int check_skip_length(struct boot_config *boot_cfg, loff_t offset,
755 size_t length, size_t *used)
757 struct mtd_info *mtd = boot_cfg->mtd;
758 size_t maxsize = boot_cfg->maxsize;
759 size_t len_excl_bad = 0;
762 while (len_excl_bad < length) {
763 size_t block_len, block_off;
766 if (offset >= maxsize)
769 block_start = offset & ~(loff_t)(mtd->erasesize - 1);
770 block_off = offset & (mtd->erasesize - 1);
771 block_len = mtd->erasesize - block_off;
773 if (!nand_block_isbad(mtd, block_start))
774 len_excl_bad += block_len;
782 /* If the length is not a multiple of block_len, adjust. */
783 if (len_excl_bad > length)
784 *used -= (len_excl_bad - length);
789 static int nandbcb_get_next_good_blk_addr(struct boot_config *boot_cfg,
790 struct boot_stream_config *bs_cfg)
792 struct mtd_info *mtd = boot_cfg->mtd;
793 loff_t offset = bs_cfg->bs_addr;
794 size_t length = bs_cfg->bs_size;
798 ret = check_skip_length(boot_cfg, offset, length, &used);
803 /* get next image address */
804 bs_cfg->next_bs_addr = (u32)(offset + used + mtd->erasesize - 1)
805 / (u32)mtd->erasesize * mtd->erasesize;
810 static int nandbcb_write_bs_skip_bad(struct boot_config *boot_cfg,
811 struct boot_stream_config *bs_cfg)
813 struct mtd_info *mtd;
815 loff_t offset, maxsize;
819 bool padding_flag = false;
822 offset = bs_cfg->bs_addr;
823 maxsize = boot_cfg->maxsize;
824 size = bs_cfg->bs_size;
826 /* some boot images may need leading offset */
827 if (bs_cfg->need_padding &&
828 (plat_config.misc_flags & FIRMWARE_NEED_PADDING))
832 length = ALIGN(size + FLASH_OFFSET_STANDARD, mtd->writesize);
834 length = ALIGN(size, mtd->writesize);
836 buf = kzalloc(length, GFP_KERNEL);
838 printf("failed to allocate buffer for firmware\n");
844 memcpy(buf + FLASH_OFFSET_STANDARD, bs_cfg->bs_buf, size);
846 memcpy(buf, bs_cfg->bs_buf, size);
848 ret = nand_write_skip_bad(mtd, offset, &length, NULL, maxsize,
849 (u_char *)buf, WITH_WR_VERIFY);
850 printf("Write %s @0x%llx offset, 0x%zx bytes written: %s\n",
851 bs_cfg->bs_label, offset, length, ret ? "ERROR" : "OK");
854 /* write image failed, quit */
857 /* get next good blk address if needed */
858 if (bs_cfg->need_padding) {
859 ret = nandbcb_get_next_good_blk_addr(boot_cfg, bs_cfg);
861 printf("Next image cannot fit in NAND partition\n");
866 /* now we know how the exact image size written to NAND */
867 bs_cfg->bs_size = length;
874 static int nandbcb_write_fw(struct boot_config *boot_cfg, u_char *buf,
881 struct boot_stream_config bs_cfg;
884 for (i = 0; i < 2; ++i) {
885 if (!(FW_INX(i) & index))
889 offset = boot_cfg->boot_stream1_address;
890 size = boot_cfg->boot_stream1_size;
892 offset = boot_cfg->boot_stream2_address;
893 size = boot_cfg->boot_stream2_size;
897 if (!(plat_config.misc_flags & FIRMWARE_EXTRA_ONE)) {
898 memset(&bs_cfg, 0, sizeof(struct boot_stream_config));
899 sprintf(bs_cfg.bs_label, "firmware%d", i);
900 bs_cfg.bs_addr = offset;
901 bs_cfg.bs_size = size;
903 bs_cfg.need_padding = 1;
905 ret = nandbcb_write_bs_skip_bad(boot_cfg, &bs_cfg);
909 /* update the boot stream size */
911 boot_cfg->boot_stream1_size = bs_cfg.bs_size;
913 boot_cfg->boot_stream2_size = bs_cfg.bs_size;
916 /* some platforms need extra firmware */
917 memset(&bs_cfg, 0, sizeof(struct boot_stream_config));
918 sprintf(bs_cfg.bs_label, "fw%d_part%d", i, 1);
919 bs_cfg.bs_addr = offset;
920 bs_cfg.bs_size = IMX8MQ_HDMI_FW_SZ;
922 bs_cfg.need_padding = 1;
924 ret = nandbcb_write_bs_skip_bad(boot_cfg, &bs_cfg);
928 /* update the boot stream size */
930 boot_cfg->boot_stream1_size = bs_cfg.bs_size;
932 boot_cfg->boot_stream2_size = bs_cfg.bs_size;
934 /* get next image address */
935 next_bs_addr = bs_cfg.next_bs_addr;
937 memset(&bs_cfg, 0, sizeof(struct boot_stream_config));
938 sprintf(bs_cfg.bs_label, "fw%d_part%d", i, 2);
939 bs_cfg.bs_addr = next_bs_addr;
940 bs_cfg.bs_size = IMX8MQ_SPL_SZ;
941 bs_cfg.bs_buf = (u_char *)(buf + IMX8MQ_HDMI_FW_SZ);
942 bs_cfg.need_padding = 0;
944 ret = nandbcb_write_bs_skip_bad(boot_cfg, &bs_cfg);
953 static int nandbcb_init(struct boot_config *boot_cfg, u_char *buf)
955 struct mtd_info *mtd;
956 nand_erase_options_t opts;
957 struct fcb_block *fcb;
958 struct dbbt_block *dbbt;
959 void *dbbt_page, *dbbt_data_page;
964 maxsize = boot_cfg->maxsize;
965 off = boot_cfg->offset;
968 memset(&opts, 0, sizeof(opts));
970 opts.length = maxsize - 1;
971 ret = nand_erase_opts(mtd, &opts);
973 printf("%s: erase failed (ret = %d)\n", __func__, ret);
978 * Reference documentation from i.MX6DQRM section 8.5.2.2
980 * Nand Boot Control Block(BCB) contains two data structures,
981 * - Firmware Configuration Block(FCB)
982 * - Discovered Bad Block Table(DBBT)
986 * - DBBT search page address,
987 * - start page address of primary firmware
988 * - start page address of secondary firmware
991 * - number of blocks = mtd partition size / mtd erasesize
992 * - two firmware blocks, primary and secondary
993 * - first 4 block for FCB/DBBT
994 * - rest split in half for primary and secondary firmware
995 * - same firmware write twice
999 ret = nandbcb_write_fw(boot_cfg, buf, FW_ALL);
1004 fcb = kzalloc(sizeof(*fcb), GFP_KERNEL);
1006 debug("failed to allocate fcb\n");
1010 fill_fcb(fcb, boot_cfg);
1012 ret = write_fcb(boot_cfg, fcb);
1015 dbbt_page = kzalloc(mtd->writesize, GFP_KERNEL);
1017 debug("failed to allocate dbbt_page\n");
1022 dbbt_data_page = kzalloc(mtd->writesize, GFP_KERNEL);
1023 if (!dbbt_data_page) {
1024 debug("failed to allocate dbbt_data_page\n");
1031 dbbt->fingerprint = DBBT_FINGERPRINT;
1032 dbbt->version = DBBT_VERSION_1;
1033 ret = fill_dbbt_data(mtd, dbbt_data_page, CONV_TO_BLOCKS(maxsize));
1035 goto dbbt_data_page_err;
1037 dbbt->dbbtpages = 1;
1040 ret = write_dbbt(boot_cfg, dbbt, dbbt_data_page);
1042 printf("failed to write FCB/DBBT\n");
1045 kfree(dbbt_data_page);
1054 static int do_nandbcb_bcbonly(int argc, char *const argv[])
1056 struct fcb_block *fcb;
1057 struct dbbt_block *dbbt;
1058 struct mtd_info *mtd;
1059 nand_erase_options_t opts;
1062 void *dbbt_page, *dbbt_data_page;
1064 struct boot_config cfg;
1067 return CMD_RET_USAGE;
1069 memset(&cfg, 0, sizeof(struct boot_config));
1070 if (nandbcb_get_info(argc, argv, &cfg))
1071 return CMD_RET_FAILURE;
1073 /* only get the partition info */
1074 if (nandbcb_get_size(2, argv, 1, &cfg))
1075 return CMD_RET_FAILURE;
1077 if (nandbcb_set_boot_config(argc, argv, &cfg))
1078 return CMD_RET_FAILURE;
1082 cfg.boot_stream1_address = simple_strtoul(argv[2], NULL, 16);
1083 cfg.boot_stream1_size = simple_strtoul(argv[3], NULL, 16);
1084 cfg.boot_stream1_size = ALIGN(cfg.boot_stream1_size, mtd->writesize);
1087 cfg.boot_stream2_address = simple_strtoul(argv[4], NULL, 16);
1088 cfg.boot_stream2_size = simple_strtoul(argv[5], NULL, 16);
1089 cfg.boot_stream2_size = ALIGN(cfg.boot_stream2_size,
1094 nandbcb_check_space(&cfg);
1096 maxsize = cfg.maxsize;
1099 /* erase the previous FCB/DBBT */
1100 memset(&opts, 0, sizeof(opts));
1102 opts.length = g_boot_search_stride * 2;
1103 ret = nand_erase_opts(mtd, &opts);
1105 printf("%s: erase failed (ret = %d)\n", __func__, ret);
1106 return CMD_RET_FAILURE;
1110 fcb = kzalloc(sizeof(*fcb), GFP_KERNEL);
1112 printf("failed to allocate fcb\n");
1114 return CMD_RET_FAILURE;
1117 fill_fcb(fcb, &cfg);
1120 ret = write_fcb(&cfg, fcb);
1123 dbbt_page = kzalloc(mtd->writesize, GFP_KERNEL);
1125 printf("failed to allocate dbbt_page\n");
1130 dbbt_data_page = kzalloc(mtd->writesize, GFP_KERNEL);
1131 if (!dbbt_data_page) {
1132 printf("failed to allocate dbbt_data_page\n");
1139 dbbt->fingerprint = DBBT_FINGERPRINT;
1140 dbbt->version = DBBT_VERSION_1;
1141 ret = fill_dbbt_data(mtd, dbbt_data_page, CONV_TO_BLOCKS(maxsize));
1143 goto dbbt_data_page_err;
1145 dbbt->dbbtpages = 1;
1148 ret = write_dbbt(&cfg, dbbt, dbbt_data_page);
1151 kfree(dbbt_data_page);
1158 printf("failed to write FCB/DBBT\n");
1159 return CMD_RET_FAILURE;
1162 return CMD_RET_SUCCESS;
1165 /* dump data which is read from NAND chip */
1166 void dump_structure(struct boot_config *boot_cfg, struct fcb_block *fcb,
1167 struct dbbt_block *dbbt, void *dbbt_data_page)
1170 struct mtd_info *mtd = boot_cfg->mtd;
1172 #define P1(x) printf(" %s = 0x%08x\n", #x, fcb->x)
1178 #define P1(x) printf(" %s = %d\n", #x, fcb->x)
1223 #if !defined(CONFIG_MX6) || defined(CONFIG_MX6SX) || \
1224 defined(CONFIG_MX6UL) || defined(CONFIG_MX6ULL)
1225 P1(onfi_sync_enable);
1226 P1(onfi_sync_speed);
1227 P1(onfi_sync_nand_data);
1229 P1(disbbm_search_limit);
1230 P1(read_retry_enable);
1233 #define P1(x) printf(" %s = 0x%08x\n", #x, dbbt->x)
1239 #define P1(x) printf(" %s = %d\n", #x, dbbt->x)
1243 for (i = 0; i < dbbt->dbbtpages; ++i)
1244 printf("%d ", *((u32 *)(dbbt_data_page + i)));
1246 if (!(plat_config.misc_flags & FIRMWARE_EXTRA_ONE)) {
1247 printf("Firmware: image #0 @ 0x%x size 0x%x\n",
1248 fcb->fw1_start, fcb->fw1_pages * mtd->writesize);
1249 printf("Firmware: image #1 @ 0x%x size 0x%x\n",
1250 fcb->fw2_start, fcb->fw2_pages * mtd->writesize);
1252 printf("Firmware: image #0 @ 0x%x size 0x%x\n",
1253 fcb->fw1_start, fcb->fw1_pages * mtd->writesize);
1254 printf("Firmware: image #1 @ 0x%x size 0x%x\n",
1255 fcb->fw2_start, fcb->fw2_pages * mtd->writesize);
1256 /* TODO: Add extra image information */
1260 static bool check_fingerprint(void *data, int fingerprint)
1264 return (*(int *)(data + off) == fingerprint);
1267 static int fuse_to_search_count(u32 bank, u32 word, u32 mask, u32 off)
1273 /* by default, the boot search count from fuse should be 2 */
1274 err = fuse_read(bank, word, &val);
1278 val = (val & mask) >> off;
1296 static int nandbcb_dump(struct boot_config *boot_cfg)
1300 struct mtd_info *mtd = boot_cfg->mtd;
1301 struct fcb_block fcb, fcb_copy;
1302 struct dbbt_block dbbt, dbbt_copy;
1303 void *dbbt_data_page, *dbbt_data_page_copy;
1304 bool fcb_not_found, dbbt_not_found;
1307 dbbt_data_page = kzalloc(mtd->writesize, GFP_KERNEL);
1308 if (!dbbt_data_page) {
1309 printf("failed to allocate dbbt_data_page\n");
1314 dbbt_data_page_copy = kzalloc(mtd->writesize, GFP_KERNEL);
1315 if (!dbbt_data_page_copy) {
1316 printf("failed to allocate dbbt_data_page\n");
1324 for (i = 0; i < g_boot_search_count; ++i) {
1325 if (fcb_not_found) {
1326 ret = read_fcb(boot_cfg, &fcb, off);
1329 goto dbbt_page_copy_err;
1333 if (check_fingerprint(&fcb, FCB_FINGERPRINT))
1336 ret = read_fcb(boot_cfg, &fcb_copy, off);
1339 goto dbbt_page_copy_err;
1340 if (memcmp(&fcb, &fcb_copy,
1341 sizeof(struct fcb_block))) {
1342 printf("FCB copies are not identical\n");
1344 goto dbbt_page_copy_err;
1348 /* next read location */
1349 off += g_boot_search_stride;
1354 off = boot_cfg->search_area_size_in_bytes;
1355 for (i = 0; i < g_boot_search_count; ++i) {
1356 if (dbbt_not_found) {
1357 ret = read_dbbt(boot_cfg, &dbbt, dbbt_data_page, off);
1360 goto dbbt_page_copy_err;
1364 if (check_fingerprint(&dbbt, DBBT_FINGERPRINT))
1367 ret = read_dbbt(boot_cfg, &dbbt_copy,
1368 dbbt_data_page_copy, off);
1371 goto dbbt_page_copy_err;
1372 if (memcmp(&dbbt, &dbbt_copy,
1373 sizeof(struct dbbt_block))) {
1374 printf("DBBT copies are not identical\n");
1376 goto dbbt_page_copy_err;
1378 if (dbbt.dbbtpages > 0 &&
1379 memcmp(dbbt_data_page, dbbt_data_page_copy,
1381 printf("DBBT data copies are not identical\n");
1383 goto dbbt_page_copy_err;
1387 /* next read location */
1388 off += g_boot_search_stride;
1391 dump_structure(boot_cfg, &fcb, &dbbt, dbbt_data_page);
1394 kfree(dbbt_data_page_copy);
1396 kfree(dbbt_data_page);
1401 static int do_nandbcb_dump(int argc, char * const argv[])
1403 struct boot_config cfg;
1407 return CMD_RET_USAGE;
1409 memset(&cfg, 0, sizeof(struct boot_config));
1410 if (nandbcb_get_info(argc, argv, &cfg))
1411 return CMD_RET_FAILURE;
1413 if (nandbcb_get_size(argc, argv, 1, &cfg))
1414 return CMD_RET_FAILURE;
1416 if (nandbcb_set_boot_config(argc, argv, &cfg))
1417 return CMD_RET_FAILURE;
1419 ret = nandbcb_dump(&cfg);
1426 static int do_nandbcb_init(int argc, char * const argv[])
1433 struct boot_config cfg;
1436 return CMD_RET_USAGE;
1438 memset(&cfg, 0, sizeof(struct boot_config));
1439 if (nandbcb_get_info(argc, argv, &cfg))
1440 return CMD_RET_FAILURE;
1442 if (nandbcb_get_size(argc, argv, 2, &cfg))
1443 return CMD_RET_FAILURE;
1444 size = cfg.boot_stream1_size;
1446 if (nandbcb_set_boot_config(argc, argv, &cfg))
1447 return CMD_RET_FAILURE;
1449 addr = simple_strtoul(argv[1], &endp, 16);
1450 if (*argv[1] == 0 || *endp != 0)
1451 return CMD_RET_FAILURE;
1453 buf = map_physmem(addr, size, MAP_WRBACK);
1455 puts("failed to map physical memory\n");
1456 return CMD_RET_FAILURE;
1459 ret = nandbcb_init(&cfg, buf);
1461 return ret == 0 ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
1464 static int do_nandbcb(struct cmd_tbl *cmdtp, int flag, int argc,
1473 /* check the platform config first */
1475 plat_config = imx6sx_plat_config;
1476 } else if (is_mx7()) {
1477 plat_config = imx7d_plat_config;
1478 } else if (is_mx6ul() || is_mx6ull()) {
1479 plat_config = imx6ul_plat_config;
1480 } else if (is_mx6() && !is_mx6sx() && !is_mx6ul() && !is_mx6ull()) {
1481 plat_config = imx6qdl_plat_config;
1482 } else if (is_imx8mq()) {
1483 plat_config = imx8mq_plat_config;
1484 } else if (is_imx8mm()) {
1485 plat_config = imx8mm_plat_config;
1486 } else if (is_imx8mn()) {
1487 plat_config = imx8mn_plat_config;
1488 } else if (is_imx8qm() || is_imx8qxp()) {
1489 plat_config = imx8q_plat_config;
1491 printf("ERROR: Unknown platform\n");
1492 return CMD_RET_FAILURE;
1495 if (plat_config.misc_flags & BT_SEARCH_CNT_FROM_FUSE) {
1497 g_boot_search_count = fuse_to_search_count(0, 720,
1499 printf("search count set to %d from fuse\n",
1500 g_boot_search_count);
1508 if (strcmp(cmd, "init") == 0) {
1509 ret = do_nandbcb_init(argc, argv);
1513 if (strcmp(cmd, "dump") == 0) {
1514 ret = do_nandbcb_dump(argc, argv);
1518 if (strcmp(cmd, "bcbonly") == 0) {
1519 ret = do_nandbcb_bcbonly(argc, argv);
1527 return CMD_RET_USAGE;
1530 #ifdef CONFIG_SYS_LONGHELP
1531 static char nandbcb_help_text[] =
1532 "init addr off|partition len - update 'len' bytes starting at\n"
1533 " 'off|part' to memory address 'addr', skipping bad blocks\n"
1534 "nandbcb bcbonly off|partition fw1-off fw1-size [fw2-off fw2-size]\n"
1535 " - write BCB only (FCB and DBBT)\n"
1536 " where `fwx-size` is fw sizes in bytes, `fw1-off`\n"
1537 " and `fw2-off` - firmware offsets\n"
1538 " FIY, BCB isn't erased automatically, so mtd erase should\n"
1539 " be called in advance before writing new BCB:\n"
1540 " > mtd erase mx7-bcb\n"
1541 "nandbcb dump off|partition - dump/verify boot structures\n";
1544 U_BOOT_CMD(nandbcb, 7, 1, do_nandbcb,
1545 "i.MX NAND Boot Control Blocks write",