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>
22 #include <jffs2/jffs2.h>
23 #include <linux/bch.h>
24 #include <linux/mtd/mtd.h>
26 #include <asm/arch/sys_proto.h>
27 #include <asm/mach-imx/imx-nandbcb.h>
28 #include <asm/mach-imx/imximage.cfg>
30 #include <linux/mtd/mtd.h>
34 #include "../../../cmd/legacy-mtd-utils.h"
36 /* FCB related flags */
37 /* FCB layout with leading 12B reserved */
38 #define FCB_LAYOUT_RESV_12B BIT(0)
39 /* FCB layout with leading 32B meta data */
40 #define FCB_LAYOUT_META_32B BIT(1)
41 /* FCB encrypted by Hamming code */
42 #define FCB_ENCODE_HAMMING BIT(2)
43 /* FCB encrypted by 40bit BCH */
44 #define FCB_ENCODE_BCH_40b BIT(3)
45 /* FCB encrypted by 62bit BCH */
46 #define FCB_ENCODE_BCH_62b BIT(4)
47 /* FCB encrypted by BCH */
48 #define FCB_ENCODE_BCH (FCB_ENCODE_BCH_40b | FCB_ENCODE_BCH_62b)
49 /* FCB data was randomized */
50 #define FCB_RANDON_ENABLED BIT(5)
52 /* Firmware related flags */
54 #define FIRMWARE_NEED_PADDING BIT(8)
56 #define FIRMWARE_EXTRA_ONE BIT(9)
57 /* Secondary firmware on fixed address */
58 #define FIRMWARE_SECONDARY_FIXED_ADDR BIT(10)
60 /* Boot search related flags */
61 #define BT_SEARCH_CNT_FROM_FUSE BIT(16)
63 struct platform_config {
67 static struct platform_config plat_config;
70 static struct platform_config imx6qdl_plat_config = {
71 .misc_flags = FCB_LAYOUT_RESV_12B |
73 FIRMWARE_NEED_PADDING,
76 static struct platform_config imx6sx_plat_config = {
77 .misc_flags = FCB_LAYOUT_META_32B |
79 FIRMWARE_NEED_PADDING |
83 static struct platform_config imx7d_plat_config = {
84 .misc_flags = FCB_LAYOUT_META_32B |
86 FIRMWARE_NEED_PADDING |
91 static struct platform_config imx6ul_plat_config = {
92 .misc_flags = FCB_LAYOUT_META_32B |
94 FIRMWARE_NEED_PADDING,
97 static struct platform_config imx8mq_plat_config = {
98 .misc_flags = FCB_LAYOUT_META_32B |
100 FIRMWARE_NEED_PADDING |
105 /* all other imx8mm */
106 static struct platform_config imx8mm_plat_config = {
107 .misc_flags = FCB_LAYOUT_META_32B |
109 FIRMWARE_NEED_PADDING |
114 static struct platform_config imx8mn_plat_config = {
115 .misc_flags = FCB_LAYOUT_META_32B |
118 FIRMWARE_SECONDARY_FIXED_ADDR |
119 BT_SEARCH_CNT_FROM_FUSE,
123 static struct platform_config imx8q_plat_config = {
124 .misc_flags = FCB_LAYOUT_META_32B |
127 FIRMWARE_SECONDARY_FIXED_ADDR |
128 BT_SEARCH_CNT_FROM_FUSE,
131 /* boot search related variables and definitions */
132 static int g_boot_search_count = 4;
133 static int g_boot_search_stride;
134 static int g_pages_per_stride;
136 /* mtd config structure */
139 struct mtd_info *mtd;
143 loff_t boot_stream1_address;
144 loff_t boot_stream2_address;
145 size_t boot_stream1_size;
146 size_t boot_stream2_size;
147 size_t max_boot_stream_size;
148 int stride_size_in_byte;
149 int search_area_size_in_bytes;
150 int search_area_size_in_pages;
151 int secondary_boot_stream_off_in_MB;
154 /* boot_stream config structure */
155 struct boot_stream_config {
167 #define FW_ALL FW1_ONLY | FW2_ONLY
168 #define FW_INX(x) (1 << (x))
170 /* NAND convert macros */
171 #define CONV_TO_PAGES(x) ((u32)(x) / (u32)(mtd->writesize))
172 #define CONV_TO_BLOCKS(x) ((u32)(x) / (u32)(mtd->erasesize))
174 #define GETBIT(v, n) (((v) >> (n)) & 0x1)
175 #define IMX8MQ_SPL_SZ 0x3e000
176 #define IMX8MQ_HDMI_FW_SZ 0x19c00
178 static int nandbcb_get_info(int argc, char * const argv[],
179 struct boot_config *boot_cfg)
182 struct mtd_info *mtd;
184 dev = nand_curr_device;
186 printf("failed to get nand_curr_device, run nand device\n");
187 return CMD_RET_FAILURE;
190 mtd = get_nand_dev_by_index(dev);
192 printf("failed to get mtd info\n");
193 return CMD_RET_FAILURE;
199 return CMD_RET_SUCCESS;
202 static int nandbcb_get_size(int argc, char * const argv[], int num,
203 struct boot_config *boot_cfg)
206 loff_t offset, size, maxsize;
207 struct mtd_info *mtd;
213 if (mtd_arg_off_size(argc - num, argv + num, &dev, &offset, &size,
214 &maxsize, MTD_DEV_TYPE_NAND, mtd->size))
215 return CMD_RET_FAILURE;
217 boot_cfg->maxsize = maxsize;
218 boot_cfg->offset = offset;
220 debug("max: %llx, offset: %llx\n", maxsize, offset);
222 if (size && size != maxsize)
223 boot_cfg->input_size = size;
225 return CMD_RET_SUCCESS;
228 static int nandbcb_set_boot_config(int argc, char * const argv[],
229 struct boot_config *boot_cfg)
231 struct mtd_info *mtd;
233 loff_t boot_stream1_address, boot_stream2_address, max_boot_stream_size;
235 if (!boot_cfg->mtd) {
236 printf("Didn't get the mtd info, quit\n");
237 return CMD_RET_FAILURE;
243 * set the search count as 4
244 * set each FCB/DBBT/Firmware offset at the beginning of blocks
245 * customers may change the value as needed
248 /* if need more compact layout, change these values */
249 /* g_boot_search_count was set as 4 at the definition*/
250 /* g_pages_per_stride was set as block size */
252 g_pages_per_stride = mtd->erasesize / mtd->writesize;
254 g_boot_search_stride = mtd->writesize * g_pages_per_stride;
256 boot_cfg->stride_size_in_byte = g_boot_search_stride * mtd->writesize;
257 boot_cfg->search_area_size_in_bytes =
258 g_boot_search_count * g_boot_search_stride;
259 boot_cfg->search_area_size_in_pages =
260 boot_cfg->search_area_size_in_bytes / mtd->writesize;
262 /* after FCB/DBBT, split the rest of area for two Firmwares */
263 if (!boot_cfg->maxsize) {
264 printf("Didn't get the maxsize, quit\n");
265 return CMD_RET_FAILURE;
267 maxsize = boot_cfg->maxsize;
268 /* align to page boundary */
269 maxsize = ((u32)(maxsize + mtd->writesize - 1)) / (u32)mtd->writesize
272 boot_stream1_address = 2 * boot_cfg->search_area_size_in_bytes;
273 boot_stream2_address = ((maxsize - boot_stream1_address) / 2 +
274 boot_stream1_address);
276 if (boot_cfg->secondary_boot_stream_off_in_MB)
277 boot_stream2_address = boot_cfg->secondary_boot_stream_off_in_MB * 1024 * 1024;
279 max_boot_stream_size = boot_stream2_address - boot_stream1_address;
282 if (max_boot_stream_size <= 0) {
283 debug("st1_addr: %llx, st2_addr: %llx, max: %llx\n",
284 boot_stream1_address, boot_stream2_address,
285 max_boot_stream_size);
286 printf("something wrong with firmware address settings\n");
287 return CMD_RET_FAILURE;
289 boot_cfg->boot_stream1_address = boot_stream1_address;
290 boot_cfg->boot_stream2_address = boot_stream2_address;
291 boot_cfg->max_boot_stream_size = max_boot_stream_size;
293 /* set the boot_stream size as the input size now */
294 if (boot_cfg->input_size) {
295 boot_cfg->boot_stream1_size = boot_cfg->input_size;
296 boot_cfg->boot_stream2_size = boot_cfg->input_size;
299 return CMD_RET_SUCCESS;
302 static int nandbcb_check_space(struct boot_config *boot_cfg)
304 size_t maxsize = boot_cfg->maxsize;
305 size_t max_boot_stream_size = boot_cfg->max_boot_stream_size;
306 loff_t boot_stream2_address = boot_cfg->boot_stream2_address;
308 if (boot_cfg->boot_stream1_size &&
309 boot_cfg->boot_stream1_size > max_boot_stream_size) {
310 printf("boot stream1 doesn't fit, check partition size or settings\n");
311 return CMD_RET_FAILURE;
314 if (boot_cfg->boot_stream2_size &&
315 boot_cfg->boot_stream2_size > maxsize - boot_stream2_address) {
316 printf("boot stream2 doesn't fit, check partition size or settings\n");
317 return CMD_RET_FAILURE;
320 return CMD_RET_SUCCESS;
323 #if defined(CONFIG_MX6UL) || defined(CONFIG_MX6ULL)
324 static uint8_t reverse_bit(uint8_t b)
326 b = (b & 0xf0) >> 4 | (b & 0x0f) << 4;
327 b = (b & 0xcc) >> 2 | (b & 0x33) << 2;
328 b = (b & 0xaa) >> 1 | (b & 0x55) << 1;
333 static void encode_bch_ecc(void *buf, struct fcb_block *fcb, int eccbits)
338 int ecc_buf_size = (m * eccbits + 7) / 8;
339 struct bch_control *bch = init_bch(m, eccbits, 0);
340 u8 *ecc_buf = kzalloc(ecc_buf_size, GFP_KERNEL);
341 u8 *tmp_buf = kzalloc(blocksize * numblocks, GFP_KERNEL);
345 * The blocks here are bit aligned. If eccbits is a multiple of 8,
346 * we just can copy bytes. Otherwiese we must move the blocks to
347 * the next free bit position.
349 WARN_ON(eccbits % 8);
351 memcpy(tmp_buf, fcb, sizeof(*fcb));
353 for (i = 0; i < numblocks; i++) {
354 memset(ecc_buf, 0, ecc_buf_size);
355 psrc = tmp_buf + i * blocksize;
356 pdst = buf + i * (blocksize + ecc_buf_size);
358 /* copy data byte aligned to destination buf */
359 memcpy(pdst, psrc, blocksize);
362 * imx-kobs use a modified encode_bch which reverse the
363 * bit order of the data before calculating bch.
364 * Do this in the buffer and use the bch lib here.
366 for (j = 0; j < blocksize; j++)
367 psrc[j] = reverse_bit(psrc[j]);
369 encode_bch(bch, psrc, blocksize, ecc_buf);
371 /* reverse ecc bit */
372 for (j = 0; j < ecc_buf_size; j++)
373 ecc_buf[j] = reverse_bit(ecc_buf[j]);
375 /* Here eccbuf is byte aligned and we can just copy it */
376 memcpy(pdst + blocksize, ecc_buf, ecc_buf_size);
385 static u8 calculate_parity_13_8(u8 d)
389 p |= (GETBIT(d, 6) ^ GETBIT(d, 5) ^ GETBIT(d, 3) ^ GETBIT(d, 2)) << 0;
390 p |= (GETBIT(d, 7) ^ GETBIT(d, 5) ^ GETBIT(d, 4) ^ GETBIT(d, 2) ^
392 p |= (GETBIT(d, 7) ^ GETBIT(d, 6) ^ GETBIT(d, 5) ^ GETBIT(d, 1) ^
394 p |= (GETBIT(d, 7) ^ GETBIT(d, 4) ^ GETBIT(d, 3) ^ GETBIT(d, 0)) << 3;
395 p |= (GETBIT(d, 6) ^ GETBIT(d, 4) ^ GETBIT(d, 3) ^ GETBIT(d, 2) ^
396 GETBIT(d, 1) ^ GETBIT(d, 0)) << 4;
401 static void encode_hamming_13_8(void *_src, void *_ecc, size_t size)
407 for (i = 0; i < size; i++)
408 ecc[i] = calculate_parity_13_8(src[i]);
412 static u32 calc_chksum(void *buf, size_t size)
418 for (i = 0; i < size; i++)
424 static void fill_fcb(struct fcb_block *fcb, struct boot_config *boot_cfg)
426 struct mtd_info *mtd = boot_cfg->mtd;
427 struct nand_chip *chip = mtd_to_nand(mtd);
428 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
429 struct mxs_nand_layout l;
431 mxs_nand_get_layout(mtd, &l);
433 fcb->fingerprint = FCB_FINGERPRINT;
434 fcb->version = FCB_VERSION_1;
438 fcb->addr_setup = 25;
439 fcb->dsample_time = 6;
441 fcb->pagesize = mtd->writesize;
442 fcb->oob_pagesize = mtd->writesize + mtd->oobsize;
443 fcb->sectors = mtd->erasesize / mtd->writesize;
445 fcb->meta_size = l.meta_size;
446 fcb->nr_blocks = l.nblocks;
447 fcb->ecc_nr = l.data0_size;
448 fcb->ecc_level = l.ecc0;
449 fcb->ecc_size = l.datan_size;
450 fcb->ecc_type = l.eccn;
451 fcb->bchtype = l.gf_len;
453 /* DBBT search area starts from the next block after all FCB */
454 fcb->dbbt_start = boot_cfg->search_area_size_in_pages;
456 fcb->bb_byte = nand_info->bch_geometry.block_mark_byte_offset;
457 fcb->bb_start_bit = nand_info->bch_geometry.block_mark_bit_offset;
459 fcb->phy_offset = mtd->writesize;
463 fcb->fw1_start = CONV_TO_PAGES(boot_cfg->boot_stream1_address);
464 fcb->fw2_start = CONV_TO_PAGES(boot_cfg->boot_stream2_address);
465 fcb->fw1_pages = CONV_TO_PAGES(boot_cfg->boot_stream1_size);
466 fcb->fw2_pages = CONV_TO_PAGES(boot_cfg->boot_stream2_size);
468 fcb->checksum = calc_chksum((void *)fcb + 4, sizeof(*fcb) - 4);
471 static int fill_dbbt_data(struct mtd_info *mtd, void *buf, int num_blocks)
473 int n, n_bad_blocks = 0;
475 u32 *n_bad_blocksp = buf + 0x4;
477 for (n = 0; n < num_blocks; n++) {
478 loff_t offset = n * mtd->erasesize;
479 if (mtd_block_isbad(mtd, offset)) {
486 *n_bad_blocksp = n_bad_blocks;
492 * return 1 - bad block
493 * return 0 - read successfully
494 * return < 0 - read failed
496 static int read_fcb(struct boot_config *boot_cfg, struct fcb_block *fcb,
499 struct mtd_info *mtd;
505 fcb_raw_page = kzalloc(mtd->writesize + mtd->oobsize, GFP_KERNEL);
507 if (mtd_block_isbad(mtd, off)) {
508 printf("Block %d is bad, skipped\n", (int)CONV_TO_BLOCKS(off));
513 * User BCH hardware to decode ECC for FCB
515 if (plat_config.misc_flags & FCB_ENCODE_BCH) {
516 size = sizeof(struct fcb_block);
518 /* switch nand BCH to FCB compatible settings */
519 if (plat_config.misc_flags & FCB_ENCODE_BCH_62b)
520 mxs_nand_mode_fcb_62bit(mtd);
521 else if (plat_config.misc_flags & FCB_ENCODE_BCH_40b)
522 mxs_nand_mode_fcb_40bit(mtd);
524 ret = nand_read(mtd, off, &size, (u_char *)fcb);
526 /* switch BCH back */
527 mxs_nand_mode_normal(mtd);
528 printf("NAND FCB read from 0x%llx offset 0x%zx read: %s\n",
529 off, size, ret ? "ERROR" : "OK");
531 } else if (plat_config.misc_flags & FCB_ENCODE_HAMMING) {
533 mtd_oob_ops_t ops = {
534 .datbuf = (u8 *)fcb_raw_page,
535 .oobbuf = ((u8 *)fcb_raw_page) + mtd->writesize,
536 .len = mtd->writesize,
537 .ooblen = mtd->oobsize,
541 ret = mtd_read_oob(mtd, off, &ops);
542 printf("NAND FCB read from 0x%llx offset 0x%zx read: %s\n",
543 off, ops.len, ret ? "ERROR" : "OK");
547 goto fcb_raw_page_err;
549 if ((plat_config.misc_flags & FCB_ENCODE_HAMMING) &&
550 (plat_config.misc_flags & FCB_LAYOUT_RESV_12B))
551 memcpy(fcb, fcb_raw_page + 12, sizeof(struct fcb_block));
553 /* TODO: check if it can pass Hamming check */
561 static int write_fcb(struct boot_config *boot_cfg, struct fcb_block *fcb)
563 struct mtd_info *mtd;
564 void *fcb_raw_page = NULL;
572 * We prepare raw page only for i.MX6, for i.MX7 we
573 * leverage BCH hw module instead
575 if ((plat_config.misc_flags & FCB_ENCODE_HAMMING) &&
576 (plat_config.misc_flags & FCB_LAYOUT_RESV_12B)) {
577 fcb_raw_page = kzalloc(mtd->writesize + mtd->oobsize,
580 debug("failed to allocate fcb_raw_page\n");
585 #if defined(CONFIG_MX6UL) || defined(CONFIG_MX6ULL)
586 /* 40 bit BCH, for i.MX6UL(L) */
587 encode_bch_ecc(fcb_raw_page + 32, fcb, 40);
589 memcpy(fcb_raw_page + 12, fcb, sizeof(struct fcb_block));
590 encode_hamming_13_8(fcb_raw_page + 12,
591 fcb_raw_page + 12 + 512, 512);
594 * Set the first and second byte of OOB data to 0xFF,
595 * not 0x00. These bytes are used as the Manufacturers Bad
596 * Block Marker (MBBM). Since the FCB is mostly written to
597 * the first page in a block, a scan for
598 * factory bad blocks will detect these blocks as bad, e.g.
599 * when function nand_scan_bbt() is executed to build a new
602 memset(fcb_raw_page + mtd->writesize, 0xFF, 2);
605 /* start writing FCB from the very beginning */
608 for (i = 0; i < g_boot_search_count; i++) {
609 if (mtd_block_isbad(mtd, off)) {
610 printf("Block %d is bad, skipped\n", i);
615 * User BCH hardware module to generate ECC for FCB
617 if (plat_config.misc_flags & FCB_ENCODE_BCH) {
618 size = sizeof(struct fcb_block);
620 /* switch nand BCH to FCB compatible settings */
621 if (plat_config.misc_flags & FCB_ENCODE_BCH_62b)
622 mxs_nand_mode_fcb_62bit(mtd);
623 else if (plat_config.misc_flags & FCB_ENCODE_BCH_40b)
624 mxs_nand_mode_fcb_40bit(mtd);
626 ret = nand_write(mtd, off, &size, (u_char *)fcb);
628 /* switch BCH back */
629 mxs_nand_mode_normal(mtd);
630 printf("NAND FCB write to 0x%zx offset 0x%llx written: %s\n",
631 size, off, ret ? "ERROR" : "OK");
633 } else if (plat_config.misc_flags & FCB_ENCODE_HAMMING) {
635 mtd_oob_ops_t ops = {
636 .datbuf = (u8 *)fcb_raw_page,
637 .oobbuf = ((u8 *)fcb_raw_page) +
639 .len = mtd->writesize,
640 .ooblen = mtd->oobsize,
644 ret = mtd_write_oob(mtd, off, &ops);
645 printf("NAND FCB write to 0x%llxx offset 0x%zx written: %s\n", off, ops.len, ret ? "ERROR" : "OK");
649 goto fcb_raw_page_err;
651 /* next writing location */
652 off += g_boot_search_stride;
664 * return 1 - bad block
665 * return 0 - read successfully
666 * return < 0 - read failed
668 static int read_dbbt(struct boot_config *boot_cfg, struct dbbt_block *dbbt,
669 void *dbbt_data_page, loff_t off)
672 struct mtd_info *mtd;
678 if (mtd_block_isbad(mtd, off)) {
679 printf("Block %d is bad, skipped\n",
680 (int)CONV_TO_BLOCKS(off));
684 size = sizeof(struct dbbt_block);
685 ret = nand_read(mtd, off, &size, (u_char *)dbbt);
686 printf("NAND DBBT read from 0x%llx offset 0x%zx read: %s\n",
687 off, size, ret ? "ERROR" : "OK");
691 /* dbbtpages == 0 if no bad blocks */
692 if (dbbt->dbbtpages > 0) {
693 to = off + 4 * mtd->writesize;
694 size = mtd->writesize;
695 ret = nand_read(mtd, to, &size, dbbt_data_page);
696 printf("DBBT data read from 0x%llx offset 0x%zx read: %s\n",
697 to, size, ret ? "ERROR" : "OK");
706 static int write_dbbt(struct boot_config *boot_cfg, struct dbbt_block *dbbt,
707 void *dbbt_data_page)
712 struct mtd_info *mtd;
717 /* start writing DBBT after all FCBs */
718 off = boot_cfg->search_area_size_in_bytes;
719 size = mtd->writesize;
721 for (i = 0; i < g_boot_search_count; i++) {
722 if (mtd_block_isbad(mtd, off)) {
723 printf("Block %d is bad, skipped\n",
724 (int)(i + CONV_TO_BLOCKS(off)));
728 ret = nand_write(mtd, off, &size, (u_char *)dbbt);
729 printf("NAND DBBT write to 0x%llx offset 0x%zx written: %s\n",
730 off, size, ret ? "ERROR" : "OK");
734 /* dbbtpages == 0 if no bad blocks */
735 if (dbbt->dbbtpages > 0) {
736 to = off + 4 * mtd->writesize;
737 ret = nand_write(mtd, to, &size, dbbt_data_page);
738 printf("DBBT data write to 0x%llx offset 0x%zx written: %s\n",
739 to, size, ret ? "ERROR" : "OK");
745 /* next writing location */
746 off += g_boot_search_stride;
752 /* reuse the check_skip_len from nand_util.c with minor change*/
753 static int check_skip_length(struct boot_config *boot_cfg, loff_t offset,
754 size_t length, size_t *used)
756 struct mtd_info *mtd = boot_cfg->mtd;
757 size_t maxsize = boot_cfg->maxsize;
758 size_t len_excl_bad = 0;
761 while (len_excl_bad < length) {
762 size_t block_len, block_off;
765 if (offset >= maxsize)
768 block_start = offset & ~(loff_t)(mtd->erasesize - 1);
769 block_off = offset & (mtd->erasesize - 1);
770 block_len = mtd->erasesize - block_off;
772 if (!nand_block_isbad(mtd, block_start))
773 len_excl_bad += block_len;
781 /* If the length is not a multiple of block_len, adjust. */
782 if (len_excl_bad > length)
783 *used -= (len_excl_bad - length);
788 static int nandbcb_get_next_good_blk_addr(struct boot_config *boot_cfg,
789 struct boot_stream_config *bs_cfg)
791 struct mtd_info *mtd = boot_cfg->mtd;
792 loff_t offset = bs_cfg->bs_addr;
793 size_t length = bs_cfg->bs_size;
797 ret = check_skip_length(boot_cfg, offset, length, &used);
802 /* get next image address */
803 bs_cfg->next_bs_addr = (u32)(offset + used + mtd->erasesize - 1)
804 / (u32)mtd->erasesize * mtd->erasesize;
809 static int nandbcb_write_bs_skip_bad(struct boot_config *boot_cfg,
810 struct boot_stream_config *bs_cfg)
812 struct mtd_info *mtd;
814 loff_t offset, maxsize;
818 bool padding_flag = false;
821 offset = bs_cfg->bs_addr;
822 maxsize = boot_cfg->maxsize;
823 size = bs_cfg->bs_size;
825 /* some boot images may need leading offset */
826 if (bs_cfg->need_padding &&
827 (plat_config.misc_flags & FIRMWARE_NEED_PADDING))
831 length = ALIGN(size + FLASH_OFFSET_STANDARD, mtd->writesize);
833 length = ALIGN(size, mtd->writesize);
835 buf = kzalloc(length, GFP_KERNEL);
837 printf("failed to allocate buffer for firmware\n");
843 memcpy(buf + FLASH_OFFSET_STANDARD, bs_cfg->bs_buf, size);
845 memcpy(buf, bs_cfg->bs_buf, size);
847 ret = nand_write_skip_bad(mtd, offset, &length, NULL, maxsize,
848 (u_char *)buf, WITH_WR_VERIFY);
849 printf("Write %s @0x%llx offset, 0x%zx bytes written: %s\n",
850 bs_cfg->bs_label, offset, length, ret ? "ERROR" : "OK");
853 /* write image failed, quit */
856 /* get next good blk address if needed */
857 if (bs_cfg->need_padding) {
858 ret = nandbcb_get_next_good_blk_addr(boot_cfg, bs_cfg);
860 printf("Next image cannot fit in NAND partition\n");
865 /* now we know how the exact image size written to NAND */
866 bs_cfg->bs_size = length;
873 static int nandbcb_write_fw(struct boot_config *boot_cfg, u_char *buf,
880 struct boot_stream_config bs_cfg;
883 for (i = 0; i < 2; ++i) {
884 if (!(FW_INX(i) & index))
888 offset = boot_cfg->boot_stream1_address;
889 size = boot_cfg->boot_stream1_size;
891 offset = boot_cfg->boot_stream2_address;
892 size = boot_cfg->boot_stream2_size;
896 if (!(plat_config.misc_flags & FIRMWARE_EXTRA_ONE)) {
897 memset(&bs_cfg, 0, sizeof(struct boot_stream_config));
898 sprintf(bs_cfg.bs_label, "firmware%d", i);
899 bs_cfg.bs_addr = offset;
900 bs_cfg.bs_size = size;
902 bs_cfg.need_padding = 1;
904 ret = nandbcb_write_bs_skip_bad(boot_cfg, &bs_cfg);
908 /* update the boot stream size */
910 boot_cfg->boot_stream1_size = bs_cfg.bs_size;
912 boot_cfg->boot_stream2_size = bs_cfg.bs_size;
915 /* some platforms need extra firmware */
916 memset(&bs_cfg, 0, sizeof(struct boot_stream_config));
917 sprintf(bs_cfg.bs_label, "fw%d_part%d", i, 1);
918 bs_cfg.bs_addr = offset;
919 bs_cfg.bs_size = IMX8MQ_HDMI_FW_SZ;
921 bs_cfg.need_padding = 1;
923 ret = nandbcb_write_bs_skip_bad(boot_cfg, &bs_cfg);
927 /* update the boot stream size */
929 boot_cfg->boot_stream1_size = bs_cfg.bs_size;
931 boot_cfg->boot_stream2_size = bs_cfg.bs_size;
933 /* get next image address */
934 next_bs_addr = bs_cfg.next_bs_addr;
936 memset(&bs_cfg, 0, sizeof(struct boot_stream_config));
937 sprintf(bs_cfg.bs_label, "fw%d_part%d", i, 2);
938 bs_cfg.bs_addr = next_bs_addr;
939 bs_cfg.bs_size = IMX8MQ_SPL_SZ;
940 bs_cfg.bs_buf = (u_char *)(buf + IMX8MQ_HDMI_FW_SZ);
941 bs_cfg.need_padding = 0;
943 ret = nandbcb_write_bs_skip_bad(boot_cfg, &bs_cfg);
952 static int nandbcb_init(struct boot_config *boot_cfg, u_char *buf)
954 struct mtd_info *mtd;
955 nand_erase_options_t opts;
956 struct fcb_block *fcb;
957 struct dbbt_block *dbbt;
958 void *dbbt_page, *dbbt_data_page;
963 maxsize = boot_cfg->maxsize;
964 off = boot_cfg->offset;
967 memset(&opts, 0, sizeof(opts));
969 opts.length = maxsize - 1;
970 ret = nand_erase_opts(mtd, &opts);
972 printf("%s: erase failed (ret = %d)\n", __func__, ret);
977 * Reference documentation from i.MX6DQRM section 8.5.2.2
979 * Nand Boot Control Block(BCB) contains two data structures,
980 * - Firmware Configuration Block(FCB)
981 * - Discovered Bad Block Table(DBBT)
985 * - DBBT search page address,
986 * - start page address of primary firmware
987 * - start page address of secondary firmware
990 * - number of blocks = mtd partition size / mtd erasesize
991 * - two firmware blocks, primary and secondary
992 * - first 4 block for FCB/DBBT
993 * - rest split in half for primary and secondary firmware
994 * - same firmware write twice
998 ret = nandbcb_write_fw(boot_cfg, buf, FW_ALL);
1003 fcb = kzalloc(sizeof(*fcb), GFP_KERNEL);
1005 debug("failed to allocate fcb\n");
1009 fill_fcb(fcb, boot_cfg);
1011 ret = write_fcb(boot_cfg, fcb);
1014 dbbt_page = kzalloc(mtd->writesize, GFP_KERNEL);
1016 debug("failed to allocate dbbt_page\n");
1021 dbbt_data_page = kzalloc(mtd->writesize, GFP_KERNEL);
1022 if (!dbbt_data_page) {
1023 debug("failed to allocate dbbt_data_page\n");
1030 dbbt->fingerprint = DBBT_FINGERPRINT;
1031 dbbt->version = DBBT_VERSION_1;
1032 ret = fill_dbbt_data(mtd, dbbt_data_page, CONV_TO_BLOCKS(maxsize));
1034 goto dbbt_data_page_err;
1036 dbbt->dbbtpages = 1;
1039 ret = write_dbbt(boot_cfg, dbbt, dbbt_data_page);
1041 printf("failed to write FCB/DBBT\n");
1044 kfree(dbbt_data_page);
1053 static int do_nandbcb_bcbonly(int argc, char *const argv[])
1055 struct fcb_block *fcb;
1056 struct dbbt_block *dbbt;
1057 struct mtd_info *mtd;
1058 nand_erase_options_t opts;
1061 void *dbbt_page, *dbbt_data_page;
1063 struct boot_config cfg;
1066 return CMD_RET_USAGE;
1068 memset(&cfg, 0, sizeof(struct boot_config));
1069 if (nandbcb_get_info(argc, argv, &cfg))
1070 return CMD_RET_FAILURE;
1072 /* only get the partition info */
1073 if (nandbcb_get_size(2, argv, 1, &cfg))
1074 return CMD_RET_FAILURE;
1076 if (nandbcb_set_boot_config(argc, argv, &cfg))
1077 return CMD_RET_FAILURE;
1081 cfg.boot_stream1_address = simple_strtoul(argv[2], NULL, 16);
1082 cfg.boot_stream1_size = simple_strtoul(argv[3], NULL, 16);
1083 cfg.boot_stream1_size = ALIGN(cfg.boot_stream1_size, mtd->writesize);
1086 cfg.boot_stream2_address = simple_strtoul(argv[4], NULL, 16);
1087 cfg.boot_stream2_size = simple_strtoul(argv[5], NULL, 16);
1088 cfg.boot_stream2_size = ALIGN(cfg.boot_stream2_size,
1093 nandbcb_check_space(&cfg);
1095 maxsize = cfg.maxsize;
1098 /* erase the previous FCB/DBBT */
1099 memset(&opts, 0, sizeof(opts));
1101 opts.length = g_boot_search_stride * 2;
1102 ret = nand_erase_opts(mtd, &opts);
1104 printf("%s: erase failed (ret = %d)\n", __func__, ret);
1105 return CMD_RET_FAILURE;
1109 fcb = kzalloc(sizeof(*fcb), GFP_KERNEL);
1111 printf("failed to allocate fcb\n");
1113 return CMD_RET_FAILURE;
1116 fill_fcb(fcb, &cfg);
1119 ret = write_fcb(&cfg, fcb);
1122 dbbt_page = kzalloc(mtd->writesize, GFP_KERNEL);
1124 printf("failed to allocate dbbt_page\n");
1129 dbbt_data_page = kzalloc(mtd->writesize, GFP_KERNEL);
1130 if (!dbbt_data_page) {
1131 printf("failed to allocate dbbt_data_page\n");
1138 dbbt->fingerprint = DBBT_FINGERPRINT;
1139 dbbt->version = DBBT_VERSION_1;
1140 ret = fill_dbbt_data(mtd, dbbt_data_page, CONV_TO_BLOCKS(maxsize));
1142 goto dbbt_data_page_err;
1144 dbbt->dbbtpages = 1;
1147 ret = write_dbbt(&cfg, dbbt, dbbt_data_page);
1150 kfree(dbbt_data_page);
1157 printf("failed to write FCB/DBBT\n");
1158 return CMD_RET_FAILURE;
1161 return CMD_RET_SUCCESS;
1164 /* dump data which is read from NAND chip */
1165 void dump_structure(struct boot_config *boot_cfg, struct fcb_block *fcb,
1166 struct dbbt_block *dbbt, void *dbbt_data_page)
1169 struct mtd_info *mtd = boot_cfg->mtd;
1171 #define P1(x) printf(" %s = 0x%08x\n", #x, fcb->x)
1177 #define P1(x) printf(" %s = %d\n", #x, fcb->x)
1222 #if !defined(CONFIG_MX6) || defined(CONFIG_MX6SX) || \
1223 defined(CONFIG_MX6UL) || defined(CONFIG_MX6ULL)
1224 P1(onfi_sync_enable);
1225 P1(onfi_sync_speed);
1226 P1(onfi_sync_nand_data);
1228 P1(disbbm_search_limit);
1229 P1(read_retry_enable);
1232 #define P1(x) printf(" %s = 0x%08x\n", #x, dbbt->x)
1238 #define P1(x) printf(" %s = %d\n", #x, dbbt->x)
1242 for (i = 0; i < dbbt->dbbtpages; ++i)
1243 printf("%d ", *((u32 *)(dbbt_data_page + i)));
1245 if (!(plat_config.misc_flags & FIRMWARE_EXTRA_ONE)) {
1246 printf("Firmware: image #0 @ 0x%x size 0x%x\n",
1247 fcb->fw1_start, fcb->fw1_pages * mtd->writesize);
1248 printf("Firmware: image #1 @ 0x%x size 0x%x\n",
1249 fcb->fw2_start, fcb->fw2_pages * mtd->writesize);
1251 printf("Firmware: image #0 @ 0x%x size 0x%x\n",
1252 fcb->fw1_start, fcb->fw1_pages * mtd->writesize);
1253 printf("Firmware: image #1 @ 0x%x size 0x%x\n",
1254 fcb->fw2_start, fcb->fw2_pages * mtd->writesize);
1255 /* TODO: Add extra image information */
1259 static bool check_fingerprint(void *data, int fingerprint)
1263 return (*(int *)(data + off) == fingerprint);
1266 static int fuse_to_search_count(u32 bank, u32 word, u32 mask, u32 off)
1272 /* by default, the boot search count from fuse should be 2 */
1273 err = fuse_read(bank, word, &val);
1277 val = (val & mask) >> off;
1295 static int nandbcb_dump(struct boot_config *boot_cfg)
1299 struct mtd_info *mtd = boot_cfg->mtd;
1300 struct fcb_block fcb, fcb_copy;
1301 struct dbbt_block dbbt, dbbt_copy;
1302 void *dbbt_data_page, *dbbt_data_page_copy;
1303 bool fcb_not_found, dbbt_not_found;
1306 dbbt_data_page = kzalloc(mtd->writesize, GFP_KERNEL);
1307 if (!dbbt_data_page) {
1308 printf("failed to allocate dbbt_data_page\n");
1313 dbbt_data_page_copy = kzalloc(mtd->writesize, GFP_KERNEL);
1314 if (!dbbt_data_page_copy) {
1315 printf("failed to allocate dbbt_data_page\n");
1323 for (i = 0; i < g_boot_search_count; ++i) {
1324 if (fcb_not_found) {
1325 ret = read_fcb(boot_cfg, &fcb, off);
1328 goto dbbt_page_copy_err;
1332 if (check_fingerprint(&fcb, FCB_FINGERPRINT))
1335 ret = read_fcb(boot_cfg, &fcb_copy, off);
1338 goto dbbt_page_copy_err;
1339 if (memcmp(&fcb, &fcb_copy,
1340 sizeof(struct fcb_block))) {
1341 printf("FCB copies are not identical\n");
1343 goto dbbt_page_copy_err;
1347 /* next read location */
1348 off += g_boot_search_stride;
1353 off = boot_cfg->search_area_size_in_bytes;
1354 for (i = 0; i < g_boot_search_count; ++i) {
1355 if (dbbt_not_found) {
1356 ret = read_dbbt(boot_cfg, &dbbt, dbbt_data_page, off);
1359 goto dbbt_page_copy_err;
1363 if (check_fingerprint(&dbbt, DBBT_FINGERPRINT))
1366 ret = read_dbbt(boot_cfg, &dbbt_copy,
1367 dbbt_data_page_copy, off);
1370 goto dbbt_page_copy_err;
1371 if (memcmp(&dbbt, &dbbt_copy,
1372 sizeof(struct dbbt_block))) {
1373 printf("DBBT copies are not identical\n");
1375 goto dbbt_page_copy_err;
1377 if (dbbt.dbbtpages > 0 &&
1378 memcmp(dbbt_data_page, dbbt_data_page_copy,
1380 printf("DBBT data copies are not identical\n");
1382 goto dbbt_page_copy_err;
1386 /* next read location */
1387 off += g_boot_search_stride;
1390 dump_structure(boot_cfg, &fcb, &dbbt, dbbt_data_page);
1393 kfree(dbbt_data_page_copy);
1395 kfree(dbbt_data_page);
1400 static int do_nandbcb_dump(int argc, char * const argv[])
1402 struct boot_config cfg;
1406 return CMD_RET_USAGE;
1408 memset(&cfg, 0, sizeof(struct boot_config));
1409 if (nandbcb_get_info(argc, argv, &cfg))
1410 return CMD_RET_FAILURE;
1412 if (nandbcb_get_size(argc, argv, 1, &cfg))
1413 return CMD_RET_FAILURE;
1415 if (nandbcb_set_boot_config(argc, argv, &cfg))
1416 return CMD_RET_FAILURE;
1418 ret = nandbcb_dump(&cfg);
1425 static int do_nandbcb_init(int argc, char * const argv[])
1432 struct boot_config cfg;
1435 return CMD_RET_USAGE;
1437 memset(&cfg, 0, sizeof(struct boot_config));
1438 if (nandbcb_get_info(argc, argv, &cfg))
1439 return CMD_RET_FAILURE;
1441 if (nandbcb_get_size(argc, argv, 2, &cfg))
1442 return CMD_RET_FAILURE;
1443 size = cfg.boot_stream1_size;
1445 if (nandbcb_set_boot_config(argc, argv, &cfg))
1446 return CMD_RET_FAILURE;
1448 addr = simple_strtoul(argv[1], &endp, 16);
1449 if (*argv[1] == 0 || *endp != 0)
1450 return CMD_RET_FAILURE;
1452 buf = map_physmem(addr, size, MAP_WRBACK);
1454 puts("failed to map physical memory\n");
1455 return CMD_RET_FAILURE;
1458 ret = nandbcb_init(&cfg, buf);
1460 return ret == 0 ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
1463 static int do_nandbcb(struct cmd_tbl *cmdtp, int flag, int argc,
1472 /* check the platform config first */
1474 plat_config = imx6sx_plat_config;
1475 } else if (is_mx7()) {
1476 plat_config = imx7d_plat_config;
1477 } else if (is_mx6ul() || is_mx6ull()) {
1478 plat_config = imx6ul_plat_config;
1479 } else if (is_mx6() && !is_mx6sx() && !is_mx6ul() && !is_mx6ull()) {
1480 plat_config = imx6qdl_plat_config;
1481 } else if (is_imx8mq()) {
1482 plat_config = imx8mq_plat_config;
1483 } else if (is_imx8mm()) {
1484 plat_config = imx8mm_plat_config;
1485 } else if (is_imx8mn()) {
1486 plat_config = imx8mn_plat_config;
1487 } else if (is_imx8qm() || is_imx8qxp()) {
1488 plat_config = imx8q_plat_config;
1490 printf("ERROR: Unknown platform\n");
1491 return CMD_RET_FAILURE;
1494 if (plat_config.misc_flags & BT_SEARCH_CNT_FROM_FUSE) {
1496 g_boot_search_count = fuse_to_search_count(0, 720,
1498 printf("search count set to %d from fuse\n",
1499 g_boot_search_count);
1507 if (strcmp(cmd, "init") == 0) {
1508 ret = do_nandbcb_init(argc, argv);
1512 if (strcmp(cmd, "dump") == 0) {
1513 ret = do_nandbcb_dump(argc, argv);
1517 if (strcmp(cmd, "bcbonly") == 0) {
1518 ret = do_nandbcb_bcbonly(argc, argv);
1526 return CMD_RET_USAGE;
1529 #ifdef CONFIG_SYS_LONGHELP
1530 static char nandbcb_help_text[] =
1531 "init addr off|partition len - update 'len' bytes starting at\n"
1532 " 'off|part' to memory address 'addr', skipping bad blocks\n"
1533 "nandbcb bcbonly off|partition fw1-off fw1-size [fw2-off fw2-size]\n"
1534 " - write BCB only (FCB and DBBT)\n"
1535 " where `fwx-size` is fw sizes in bytes, `fw1-off`\n"
1536 " and `fw2-off` - firmware offsets\n"
1537 " FIY, BCB isn't erased automatically, so mtd erase should\n"
1538 " be called in advance before writing new BCB:\n"
1539 " > mtd erase mx7-bcb\n"
1540 "nandbcb dump off|partition - dump/verify boot structures\n";
1543 U_BOOT_CMD(nandbcb, 7, 1, do_nandbcb,
1544 "i.MX NAND Boot Control Blocks write",