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>
26 #include <linux/mtd/rawnand.h>
28 #include <asm/arch/sys_proto.h>
29 #include <asm/mach-imx/imx-nandbcb.h>
30 #include <asm/mach-imx/imximage.cfg>
32 #include <linux/mtd/mtd.h>
36 #include "../../../cmd/legacy-mtd-utils.h"
38 /* FCB related flags */
39 /* FCB layout with leading 12B reserved */
40 #define FCB_LAYOUT_RESV_12B BIT(0)
41 /* FCB layout with leading 32B meta data */
42 #define FCB_LAYOUT_META_32B BIT(1)
43 /* FCB encrypted by Hamming code */
44 #define FCB_ENCODE_HAMMING BIT(2)
45 /* FCB encrypted by 40bit BCH */
46 #define FCB_ENCODE_BCH_40b BIT(3)
47 /* FCB encrypted by 62bit BCH */
48 #define FCB_ENCODE_BCH_62b BIT(4)
49 /* FCB encrypted by BCH */
50 #define FCB_ENCODE_BCH (FCB_ENCODE_BCH_40b | FCB_ENCODE_BCH_62b)
51 /* FCB data was randomized */
52 #define FCB_RANDON_ENABLED BIT(5)
54 /* Firmware related flags */
56 #define FIRMWARE_NEED_PADDING BIT(8)
58 #define FIRMWARE_EXTRA_ONE BIT(9)
59 /* Secondary firmware on fixed address */
60 #define FIRMWARE_SECONDARY_FIXED_ADDR BIT(10)
62 /* Boot search related flags */
63 #define BT_SEARCH_CNT_FROM_FUSE BIT(16)
65 struct platform_config {
69 static struct platform_config plat_config;
72 static struct platform_config imx6qdl_plat_config = {
73 .misc_flags = FCB_LAYOUT_RESV_12B |
75 FIRMWARE_NEED_PADDING,
78 static struct platform_config imx6sx_plat_config = {
79 .misc_flags = FCB_LAYOUT_META_32B |
81 FIRMWARE_NEED_PADDING |
85 static struct platform_config imx7d_plat_config = {
86 .misc_flags = FCB_LAYOUT_META_32B |
88 FIRMWARE_NEED_PADDING |
93 static struct platform_config imx6ul_plat_config = {
94 .misc_flags = FCB_LAYOUT_META_32B |
96 FIRMWARE_NEED_PADDING,
99 static struct platform_config imx8mq_plat_config = {
100 .misc_flags = FCB_LAYOUT_META_32B |
102 FIRMWARE_NEED_PADDING |
107 /* all other imx8mm */
108 static struct platform_config imx8mm_plat_config = {
109 .misc_flags = FCB_LAYOUT_META_32B |
111 FIRMWARE_NEED_PADDING |
116 static struct platform_config imx8mn_plat_config = {
117 .misc_flags = FCB_LAYOUT_META_32B |
120 FIRMWARE_SECONDARY_FIXED_ADDR |
121 BT_SEARCH_CNT_FROM_FUSE,
125 static struct platform_config imx8q_plat_config = {
126 .misc_flags = FCB_LAYOUT_META_32B |
129 FIRMWARE_SECONDARY_FIXED_ADDR |
130 BT_SEARCH_CNT_FROM_FUSE,
133 /* boot search related variables and definitions */
134 static int g_boot_search_count = 4;
135 static int g_boot_search_stride;
136 static int g_pages_per_stride;
138 /* mtd config structure */
141 struct mtd_info *mtd;
145 loff_t boot_stream1_address;
146 loff_t boot_stream2_address;
147 size_t boot_stream1_size;
148 size_t boot_stream2_size;
149 size_t max_boot_stream_size;
150 int stride_size_in_byte;
151 int search_area_size_in_bytes;
152 int search_area_size_in_pages;
153 int secondary_boot_stream_off_in_MB;
156 /* boot_stream config structure */
157 struct boot_stream_config {
169 #define FW_ALL FW1_ONLY | FW2_ONLY
170 #define FW_INX(x) (1 << (x))
172 /* NAND convert macros */
173 #define CONV_TO_PAGES(x) ((u32)(x) / (u32)(mtd->writesize))
174 #define CONV_TO_BLOCKS(x) ((u32)(x) / (u32)(mtd->erasesize))
176 #define GETBIT(v, n) (((v) >> (n)) & 0x1)
177 #define IMX8MQ_SPL_SZ 0x3e000
178 #define IMX8MQ_HDMI_FW_SZ 0x19c00
180 static int nandbcb_get_info(int argc, char * const argv[],
181 struct boot_config *boot_cfg)
184 struct mtd_info *mtd;
186 dev = nand_curr_device;
188 printf("failed to get nand_curr_device, run nand device\n");
189 return CMD_RET_FAILURE;
192 mtd = get_nand_dev_by_index(dev);
194 printf("failed to get mtd info\n");
195 return CMD_RET_FAILURE;
201 return CMD_RET_SUCCESS;
204 static int nandbcb_get_size(int argc, char * const argv[], int num,
205 struct boot_config *boot_cfg)
208 loff_t offset, size, maxsize;
209 struct mtd_info *mtd;
215 if (mtd_arg_off_size(argc - num, argv + num, &dev, &offset, &size,
216 &maxsize, MTD_DEV_TYPE_NAND, mtd->size))
217 return CMD_RET_FAILURE;
219 boot_cfg->maxsize = maxsize;
220 boot_cfg->offset = offset;
222 debug("max: %llx, offset: %llx\n", maxsize, offset);
224 if (size && size != maxsize)
225 boot_cfg->input_size = size;
227 return CMD_RET_SUCCESS;
230 static int nandbcb_set_boot_config(int argc, char * const argv[],
231 struct boot_config *boot_cfg)
233 struct mtd_info *mtd;
235 loff_t boot_stream1_address, boot_stream2_address, max_boot_stream_size;
237 if (!boot_cfg->mtd) {
238 printf("Didn't get the mtd info, quit\n");
239 return CMD_RET_FAILURE;
245 * set the search count as 4
246 * set each FCB/DBBT/Firmware offset at the beginning of blocks
247 * customers may change the value as needed
250 /* if need more compact layout, change these values */
251 /* g_boot_search_count was set as 4 at the definition*/
252 /* g_pages_per_stride was set as block size */
254 g_pages_per_stride = mtd->erasesize / mtd->writesize;
256 g_boot_search_stride = mtd->writesize * g_pages_per_stride;
258 boot_cfg->stride_size_in_byte = g_boot_search_stride * mtd->writesize;
259 boot_cfg->search_area_size_in_bytes =
260 g_boot_search_count * g_boot_search_stride;
261 boot_cfg->search_area_size_in_pages =
262 boot_cfg->search_area_size_in_bytes / mtd->writesize;
264 /* after FCB/DBBT, split the rest of area for two Firmwares */
265 if (!boot_cfg->maxsize) {
266 printf("Didn't get the maxsize, quit\n");
267 return CMD_RET_FAILURE;
269 maxsize = boot_cfg->maxsize;
270 /* align to page boundary */
271 maxsize = ((u32)(maxsize + mtd->writesize - 1)) / (u32)mtd->writesize
274 boot_stream1_address = 2 * boot_cfg->search_area_size_in_bytes;
275 boot_stream2_address = ((maxsize - boot_stream1_address) / 2 +
276 boot_stream1_address);
278 if (boot_cfg->secondary_boot_stream_off_in_MB)
279 boot_stream2_address =
280 (loff_t)boot_cfg->secondary_boot_stream_off_in_MB * 1024 * 1024;
282 max_boot_stream_size = boot_stream2_address - boot_stream1_address;
285 if (max_boot_stream_size <= 0) {
286 debug("st1_addr: %llx, st2_addr: %llx, max: %llx\n",
287 boot_stream1_address, boot_stream2_address,
288 max_boot_stream_size);
289 printf("something wrong with firmware address settings\n");
290 return CMD_RET_FAILURE;
292 boot_cfg->boot_stream1_address = boot_stream1_address;
293 boot_cfg->boot_stream2_address = boot_stream2_address;
294 boot_cfg->max_boot_stream_size = max_boot_stream_size;
296 /* set the boot_stream size as the input size now */
297 if (boot_cfg->input_size) {
298 boot_cfg->boot_stream1_size = boot_cfg->input_size;
299 boot_cfg->boot_stream2_size = boot_cfg->input_size;
302 return CMD_RET_SUCCESS;
305 static int nandbcb_check_space(struct boot_config *boot_cfg)
307 size_t maxsize = boot_cfg->maxsize;
308 size_t max_boot_stream_size = boot_cfg->max_boot_stream_size;
309 loff_t boot_stream2_address = boot_cfg->boot_stream2_address;
311 if (boot_cfg->boot_stream1_size &&
312 boot_cfg->boot_stream1_size > max_boot_stream_size) {
313 printf("boot stream1 doesn't fit, check partition size or settings\n");
314 return CMD_RET_FAILURE;
317 if (boot_cfg->boot_stream2_size &&
318 boot_cfg->boot_stream2_size > maxsize - boot_stream2_address) {
319 printf("boot stream2 doesn't fit, check partition size or settings\n");
320 return CMD_RET_FAILURE;
323 return CMD_RET_SUCCESS;
326 #if defined(CONFIG_MX6UL) || defined(CONFIG_MX6ULL)
327 static uint8_t reverse_bit(uint8_t b)
329 b = (b & 0xf0) >> 4 | (b & 0x0f) << 4;
330 b = (b & 0xcc) >> 2 | (b & 0x33) << 2;
331 b = (b & 0xaa) >> 1 | (b & 0x55) << 1;
336 static void encode_bch_ecc(void *buf, struct fcb_block *fcb, int eccbits)
341 int ecc_buf_size = (m * eccbits + 7) / 8;
342 struct bch_control *bch = init_bch(m, eccbits, 0);
343 u8 *ecc_buf = kzalloc(ecc_buf_size, GFP_KERNEL);
344 u8 *tmp_buf = kzalloc(blocksize * numblocks, GFP_KERNEL);
348 * The blocks here are bit aligned. If eccbits is a multiple of 8,
349 * we just can copy bytes. Otherwiese we must move the blocks to
350 * the next free bit position.
352 WARN_ON(eccbits % 8);
354 memcpy(tmp_buf, fcb, sizeof(*fcb));
356 for (i = 0; i < numblocks; i++) {
357 memset(ecc_buf, 0, ecc_buf_size);
358 psrc = tmp_buf + i * blocksize;
359 pdst = buf + i * (blocksize + ecc_buf_size);
361 /* copy data byte aligned to destination buf */
362 memcpy(pdst, psrc, blocksize);
365 * imx-kobs use a modified encode_bch which reverse the
366 * bit order of the data before calculating bch.
367 * Do this in the buffer and use the bch lib here.
369 for (j = 0; j < blocksize; j++)
370 psrc[j] = reverse_bit(psrc[j]);
372 encode_bch(bch, psrc, blocksize, ecc_buf);
374 /* reverse ecc bit */
375 for (j = 0; j < ecc_buf_size; j++)
376 ecc_buf[j] = reverse_bit(ecc_buf[j]);
378 /* Here eccbuf is byte aligned and we can just copy it */
379 memcpy(pdst + blocksize, ecc_buf, ecc_buf_size);
388 static u8 calculate_parity_13_8(u8 d)
392 p |= (GETBIT(d, 6) ^ GETBIT(d, 5) ^ GETBIT(d, 3) ^ GETBIT(d, 2)) << 0;
393 p |= (GETBIT(d, 7) ^ GETBIT(d, 5) ^ GETBIT(d, 4) ^ GETBIT(d, 2) ^
395 p |= (GETBIT(d, 7) ^ GETBIT(d, 6) ^ GETBIT(d, 5) ^ GETBIT(d, 1) ^
397 p |= (GETBIT(d, 7) ^ GETBIT(d, 4) ^ GETBIT(d, 3) ^ GETBIT(d, 0)) << 3;
398 p |= (GETBIT(d, 6) ^ GETBIT(d, 4) ^ GETBIT(d, 3) ^ GETBIT(d, 2) ^
399 GETBIT(d, 1) ^ GETBIT(d, 0)) << 4;
404 static void encode_hamming_13_8(void *_src, void *_ecc, size_t size)
410 for (i = 0; i < size; i++)
411 ecc[i] = calculate_parity_13_8(src[i]);
415 static u32 calc_chksum(void *buf, size_t size)
421 for (i = 0; i < size; i++)
427 static void fill_fcb(struct fcb_block *fcb, struct boot_config *boot_cfg)
429 struct mtd_info *mtd = boot_cfg->mtd;
430 struct nand_chip *chip = mtd_to_nand(mtd);
431 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
432 struct mxs_nand_layout l;
434 mxs_nand_get_layout(mtd, &l);
436 fcb->fingerprint = FCB_FINGERPRINT;
437 fcb->version = FCB_VERSION_1;
441 fcb->addr_setup = 25;
442 fcb->dsample_time = 6;
444 fcb->pagesize = mtd->writesize;
445 fcb->oob_pagesize = mtd->writesize + mtd->oobsize;
446 fcb->sectors = mtd->erasesize / mtd->writesize;
448 fcb->meta_size = l.meta_size;
449 fcb->nr_blocks = l.nblocks;
450 fcb->ecc_nr = l.data0_size;
451 fcb->ecc_level = l.ecc0;
452 fcb->ecc_size = l.datan_size;
453 fcb->ecc_type = l.eccn;
454 fcb->bchtype = l.gf_len;
456 /* DBBT search area starts from the next block after all FCB */
457 fcb->dbbt_start = boot_cfg->search_area_size_in_pages;
459 fcb->bb_byte = nand_info->bch_geometry.block_mark_byte_offset;
460 fcb->bb_start_bit = nand_info->bch_geometry.block_mark_bit_offset;
462 fcb->phy_offset = mtd->writesize;
466 fcb->fw1_start = CONV_TO_PAGES(boot_cfg->boot_stream1_address);
467 fcb->fw2_start = CONV_TO_PAGES(boot_cfg->boot_stream2_address);
468 fcb->fw1_pages = CONV_TO_PAGES(boot_cfg->boot_stream1_size);
469 fcb->fw2_pages = CONV_TO_PAGES(boot_cfg->boot_stream2_size);
471 fcb->checksum = calc_chksum((void *)fcb + 4, sizeof(*fcb) - 4);
474 static int fill_dbbt_data(struct mtd_info *mtd, void *buf, int num_blocks)
476 int n, n_bad_blocks = 0;
478 u32 *n_bad_blocksp = buf + 0x4;
480 for (n = 0; n < num_blocks; n++) {
481 loff_t offset = (loff_t)n * mtd->erasesize;
482 if (mtd_block_isbad(mtd, offset)) {
489 *n_bad_blocksp = n_bad_blocks;
495 * return 1 - bad block
496 * return 0 - read successfully
497 * return < 0 - read failed
499 static int read_fcb(struct boot_config *boot_cfg, struct fcb_block *fcb,
502 struct mtd_info *mtd;
508 if (mtd_block_isbad(mtd, off)) {
509 printf("Block %d is bad, skipped\n", (int)CONV_TO_BLOCKS(off));
513 fcb_raw_page = kzalloc(mtd->writesize + mtd->oobsize, GFP_KERNEL);
515 debug("failed to allocate fcb_raw_page\n");
521 * User BCH hardware to decode ECC for FCB
523 if (plat_config.misc_flags & FCB_ENCODE_BCH) {
524 size = sizeof(struct fcb_block);
526 /* switch nand BCH to FCB compatible settings */
527 if (plat_config.misc_flags & FCB_ENCODE_BCH_62b)
528 mxs_nand_mode_fcb_62bit(mtd);
529 else if (plat_config.misc_flags & FCB_ENCODE_BCH_40b)
530 mxs_nand_mode_fcb_40bit(mtd);
532 ret = nand_read(mtd, off, &size, (u_char *)fcb);
534 /* switch BCH back */
535 mxs_nand_mode_normal(mtd);
536 printf("NAND FCB read from 0x%llx offset 0x%zx read: %s\n",
537 off, size, ret ? "ERROR" : "OK");
539 } else if (plat_config.misc_flags & FCB_ENCODE_HAMMING) {
541 mtd_oob_ops_t ops = {
542 .datbuf = (u8 *)fcb_raw_page,
543 .oobbuf = ((u8 *)fcb_raw_page) + mtd->writesize,
544 .len = mtd->writesize,
545 .ooblen = mtd->oobsize,
549 ret = mtd_read_oob(mtd, off, &ops);
550 printf("NAND FCB read from 0x%llx offset 0x%zx read: %s\n",
551 off, ops.len, ret ? "ERROR" : "OK");
555 goto fcb_raw_page_err;
557 if ((plat_config.misc_flags & FCB_ENCODE_HAMMING) &&
558 (plat_config.misc_flags & FCB_LAYOUT_RESV_12B))
559 memcpy(fcb, fcb_raw_page + 12, sizeof(struct fcb_block));
561 /* TODO: check if it can pass Hamming check */
569 static int write_fcb(struct boot_config *boot_cfg, struct fcb_block *fcb)
571 struct mtd_info *mtd;
572 void *fcb_raw_page = NULL;
580 * We prepare raw page only for i.MX6, for i.MX7 we
581 * leverage BCH hw module instead
583 if ((plat_config.misc_flags & FCB_ENCODE_HAMMING) &&
584 (plat_config.misc_flags & FCB_LAYOUT_RESV_12B)) {
585 fcb_raw_page = kzalloc(mtd->writesize + mtd->oobsize,
588 debug("failed to allocate fcb_raw_page\n");
593 #if defined(CONFIG_MX6UL) || defined(CONFIG_MX6ULL)
594 /* 40 bit BCH, for i.MX6UL(L) */
595 encode_bch_ecc(fcb_raw_page + 32, fcb, 40);
597 memcpy(fcb_raw_page + 12, fcb, sizeof(struct fcb_block));
598 encode_hamming_13_8(fcb_raw_page + 12,
599 fcb_raw_page + 12 + 512, 512);
602 * Set the first and second byte of OOB data to 0xFF,
603 * not 0x00. These bytes are used as the Manufacturers Bad
604 * Block Marker (MBBM). Since the FCB is mostly written to
605 * the first page in a block, a scan for
606 * factory bad blocks will detect these blocks as bad, e.g.
607 * when function nand_scan_bbt() is executed to build a new
610 memset(fcb_raw_page + mtd->writesize, 0xFF, 2);
613 /* start writing FCB from the very beginning */
616 for (i = 0; i < g_boot_search_count; i++) {
617 if (mtd_block_isbad(mtd, off)) {
618 printf("Block %d is bad, skipped\n", i);
623 * User BCH hardware module to generate ECC for FCB
625 if (plat_config.misc_flags & FCB_ENCODE_BCH) {
626 size = sizeof(struct fcb_block);
628 /* switch nand BCH to FCB compatible settings */
629 if (plat_config.misc_flags & FCB_ENCODE_BCH_62b)
630 mxs_nand_mode_fcb_62bit(mtd);
631 else if (plat_config.misc_flags & FCB_ENCODE_BCH_40b)
632 mxs_nand_mode_fcb_40bit(mtd);
634 ret = nand_write(mtd, off, &size, (u_char *)fcb);
636 /* switch BCH back */
637 mxs_nand_mode_normal(mtd);
638 printf("NAND FCB write to 0x%zx offset 0x%llx written: %s\n",
639 size, off, ret ? "ERROR" : "OK");
641 } else if (plat_config.misc_flags & FCB_ENCODE_HAMMING) {
643 mtd_oob_ops_t ops = {
644 .datbuf = (u8 *)fcb_raw_page,
645 .oobbuf = ((u8 *)fcb_raw_page) +
647 .len = mtd->writesize,
648 .ooblen = mtd->oobsize,
652 ret = mtd_write_oob(mtd, off, &ops);
653 printf("NAND FCB write to 0x%llxx offset 0x%zx written: %s\n", off, ops.len, ret ? "ERROR" : "OK");
657 goto fcb_raw_page_err;
659 /* next writing location */
660 off += g_boot_search_stride;
670 * return 1 - bad block
671 * return 0 - read successfully
672 * return < 0 - read failed
674 static int read_dbbt(struct boot_config *boot_cfg, struct dbbt_block *dbbt,
675 void *dbbt_data_page, loff_t off)
678 struct mtd_info *mtd;
684 if (mtd_block_isbad(mtd, off)) {
685 printf("Block %d is bad, skipped\n",
686 (int)CONV_TO_BLOCKS(off));
690 size = sizeof(struct dbbt_block);
691 ret = nand_read(mtd, off, &size, (u_char *)dbbt);
692 printf("NAND DBBT read from 0x%llx offset 0x%zx read: %s\n",
693 off, size, ret ? "ERROR" : "OK");
697 /* dbbtpages == 0 if no bad blocks */
698 if (dbbt->dbbtpages > 0) {
699 to = off + 4 * mtd->writesize;
700 size = mtd->writesize;
701 ret = nand_read(mtd, to, &size, dbbt_data_page);
702 printf("DBBT data read from 0x%llx offset 0x%zx read: %s\n",
703 to, size, ret ? "ERROR" : "OK");
712 static int write_dbbt(struct boot_config *boot_cfg, struct dbbt_block *dbbt,
713 void *dbbt_data_page)
718 struct mtd_info *mtd;
723 /* start writing DBBT after all FCBs */
724 off = boot_cfg->search_area_size_in_bytes;
725 size = mtd->writesize;
727 for (i = 0; i < g_boot_search_count; i++) {
728 if (mtd_block_isbad(mtd, off)) {
729 printf("Block %d is bad, skipped\n",
730 (int)(i + CONV_TO_BLOCKS(off)));
734 ret = nand_write(mtd, off, &size, (u_char *)dbbt);
735 printf("NAND DBBT write to 0x%llx offset 0x%zx written: %s\n",
736 off, size, ret ? "ERROR" : "OK");
740 /* dbbtpages == 0 if no bad blocks */
741 if (dbbt->dbbtpages > 0) {
742 to = off + 4 * mtd->writesize;
743 ret = nand_write(mtd, to, &size, dbbt_data_page);
744 printf("DBBT data write to 0x%llx offset 0x%zx written: %s\n",
745 to, size, ret ? "ERROR" : "OK");
751 /* next writing location */
752 off += g_boot_search_stride;
758 /* reuse the check_skip_len from nand_util.c with minor change*/
759 static int check_skip_length(struct boot_config *boot_cfg, loff_t offset,
760 size_t length, size_t *used)
762 struct mtd_info *mtd = boot_cfg->mtd;
763 size_t maxsize = boot_cfg->maxsize;
764 size_t len_excl_bad = 0;
767 while (len_excl_bad < length) {
768 size_t block_len, block_off;
771 if (offset >= maxsize)
774 block_start = offset & ~(loff_t)(mtd->erasesize - 1);
775 block_off = offset & (mtd->erasesize - 1);
776 block_len = mtd->erasesize - block_off;
778 if (!nand_block_isbad(mtd, block_start))
779 len_excl_bad += block_len;
787 /* If the length is not a multiple of block_len, adjust. */
788 if (len_excl_bad > length)
789 *used -= (len_excl_bad - length);
794 static int nandbcb_get_next_good_blk_addr(struct boot_config *boot_cfg,
795 struct boot_stream_config *bs_cfg)
797 struct mtd_info *mtd = boot_cfg->mtd;
798 loff_t offset = bs_cfg->bs_addr;
799 size_t length = bs_cfg->bs_size;
803 ret = check_skip_length(boot_cfg, offset, length, &used);
808 /* get next image address */
809 bs_cfg->next_bs_addr = (u32)(offset + used + mtd->erasesize - 1)
810 / (u32)mtd->erasesize * mtd->erasesize;
815 static int nandbcb_write_bs_skip_bad(struct boot_config *boot_cfg,
816 struct boot_stream_config *bs_cfg)
818 struct mtd_info *mtd;
820 loff_t offset, maxsize;
824 bool padding_flag = false;
827 offset = bs_cfg->bs_addr;
828 maxsize = boot_cfg->maxsize;
829 size = bs_cfg->bs_size;
831 /* some boot images may need leading offset */
832 if (bs_cfg->need_padding &&
833 (plat_config.misc_flags & FIRMWARE_NEED_PADDING))
837 length = ALIGN(size + FLASH_OFFSET_STANDARD, mtd->writesize);
839 length = ALIGN(size, mtd->writesize);
841 buf = kzalloc(length, GFP_KERNEL);
843 printf("failed to allocate buffer for firmware\n");
849 memcpy(buf + FLASH_OFFSET_STANDARD, bs_cfg->bs_buf, size);
851 memcpy(buf, bs_cfg->bs_buf, size);
853 ret = nand_write_skip_bad(mtd, offset, &length, NULL, maxsize,
854 (u_char *)buf, WITH_WR_VERIFY);
855 printf("Write %s @0x%llx offset, 0x%zx bytes written: %s\n",
856 bs_cfg->bs_label, offset, length, ret ? "ERROR" : "OK");
859 /* write image failed, quit */
862 /* get next good blk address if needed */
863 if (bs_cfg->need_padding) {
864 ret = nandbcb_get_next_good_blk_addr(boot_cfg, bs_cfg);
866 printf("Next image cannot fit in NAND partition\n");
871 /* now we know how the exact image size written to NAND */
872 bs_cfg->bs_size = length;
879 static int nandbcb_write_fw(struct boot_config *boot_cfg, u_char *buf,
886 struct boot_stream_config bs_cfg;
889 for (i = 0; i < 2; ++i) {
890 if (!(FW_INX(i) & index))
894 offset = boot_cfg->boot_stream1_address;
895 size = boot_cfg->boot_stream1_size;
897 offset = boot_cfg->boot_stream2_address;
898 size = boot_cfg->boot_stream2_size;
902 if (!(plat_config.misc_flags & FIRMWARE_EXTRA_ONE)) {
903 memset(&bs_cfg, 0, sizeof(struct boot_stream_config));
904 sprintf(bs_cfg.bs_label, "firmware%d", i);
905 bs_cfg.bs_addr = offset;
906 bs_cfg.bs_size = size;
908 bs_cfg.need_padding = 1;
910 ret = nandbcb_write_bs_skip_bad(boot_cfg, &bs_cfg);
914 /* update the boot stream size */
916 boot_cfg->boot_stream1_size = bs_cfg.bs_size;
918 boot_cfg->boot_stream2_size = bs_cfg.bs_size;
921 /* some platforms need extra firmware */
922 memset(&bs_cfg, 0, sizeof(struct boot_stream_config));
923 sprintf(bs_cfg.bs_label, "fw%d_part%d", i, 1);
924 bs_cfg.bs_addr = offset;
925 bs_cfg.bs_size = IMX8MQ_HDMI_FW_SZ;
927 bs_cfg.need_padding = 1;
929 ret = nandbcb_write_bs_skip_bad(boot_cfg, &bs_cfg);
933 /* update the boot stream size */
935 boot_cfg->boot_stream1_size = bs_cfg.bs_size;
937 boot_cfg->boot_stream2_size = bs_cfg.bs_size;
939 /* get next image address */
940 next_bs_addr = bs_cfg.next_bs_addr;
942 memset(&bs_cfg, 0, sizeof(struct boot_stream_config));
943 sprintf(bs_cfg.bs_label, "fw%d_part%d", i, 2);
944 bs_cfg.bs_addr = next_bs_addr;
945 bs_cfg.bs_size = IMX8MQ_SPL_SZ;
946 bs_cfg.bs_buf = (u_char *)(buf + IMX8MQ_HDMI_FW_SZ);
947 bs_cfg.need_padding = 0;
949 ret = nandbcb_write_bs_skip_bad(boot_cfg, &bs_cfg);
958 static int nandbcb_init(struct boot_config *boot_cfg, u_char *buf)
960 struct mtd_info *mtd;
961 nand_erase_options_t opts;
962 struct fcb_block *fcb;
963 struct dbbt_block *dbbt;
964 void *dbbt_page, *dbbt_data_page;
969 maxsize = boot_cfg->maxsize;
970 off = boot_cfg->offset;
973 memset(&opts, 0, sizeof(opts));
975 opts.length = maxsize - 1;
976 ret = nand_erase_opts(mtd, &opts);
978 printf("%s: erase failed (ret = %d)\n", __func__, ret);
983 * Reference documentation from i.MX6DQRM section 8.5.2.2
985 * Nand Boot Control Block(BCB) contains two data structures,
986 * - Firmware Configuration Block(FCB)
987 * - Discovered Bad Block Table(DBBT)
991 * - DBBT search page address,
992 * - start page address of primary firmware
993 * - start page address of secondary firmware
996 * - number of blocks = mtd partition size / mtd erasesize
997 * - two firmware blocks, primary and secondary
998 * - first 4 block for FCB/DBBT
999 * - rest split in half for primary and secondary firmware
1000 * - same firmware write twice
1004 ret = nandbcb_write_fw(boot_cfg, buf, FW_ALL);
1009 fcb = kzalloc(sizeof(*fcb), GFP_KERNEL);
1011 debug("failed to allocate fcb\n");
1015 fill_fcb(fcb, boot_cfg);
1017 ret = write_fcb(boot_cfg, fcb);
1020 dbbt_page = kzalloc(mtd->writesize, GFP_KERNEL);
1022 debug("failed to allocate dbbt_page\n");
1027 dbbt_data_page = kzalloc(mtd->writesize, GFP_KERNEL);
1028 if (!dbbt_data_page) {
1029 debug("failed to allocate dbbt_data_page\n");
1036 dbbt->fingerprint = DBBT_FINGERPRINT;
1037 dbbt->version = DBBT_VERSION_1;
1038 ret = fill_dbbt_data(mtd, dbbt_data_page, CONV_TO_BLOCKS(maxsize));
1040 goto dbbt_data_page_err;
1042 dbbt->dbbtpages = 1;
1045 ret = write_dbbt(boot_cfg, dbbt, dbbt_data_page);
1047 printf("failed to write FCB/DBBT\n");
1050 kfree(dbbt_data_page);
1059 static int do_nandbcb_bcbonly(int argc, char *const argv[])
1061 struct fcb_block *fcb;
1062 struct dbbt_block *dbbt;
1063 struct mtd_info *mtd;
1064 nand_erase_options_t opts;
1067 void *dbbt_page, *dbbt_data_page;
1069 struct boot_config cfg;
1072 return CMD_RET_USAGE;
1074 memset(&cfg, 0, sizeof(struct boot_config));
1075 if (nandbcb_get_info(argc, argv, &cfg))
1076 return CMD_RET_FAILURE;
1078 /* only get the partition info */
1079 if (nandbcb_get_size(2, argv, 1, &cfg))
1080 return CMD_RET_FAILURE;
1082 if (nandbcb_set_boot_config(argc, argv, &cfg))
1083 return CMD_RET_FAILURE;
1087 cfg.boot_stream1_address = hextoul(argv[2], NULL);
1088 cfg.boot_stream1_size = hextoul(argv[3], NULL);
1089 cfg.boot_stream1_size = ALIGN(cfg.boot_stream1_size, mtd->writesize);
1092 cfg.boot_stream2_address = hextoul(argv[4], NULL);
1093 cfg.boot_stream2_size = hextoul(argv[5], NULL);
1094 cfg.boot_stream2_size = ALIGN(cfg.boot_stream2_size,
1099 nandbcb_check_space(&cfg);
1101 maxsize = cfg.maxsize;
1104 /* erase the previous FCB/DBBT */
1105 memset(&opts, 0, sizeof(opts));
1107 opts.length = g_boot_search_stride * 2;
1108 ret = nand_erase_opts(mtd, &opts);
1110 printf("%s: erase failed (ret = %d)\n", __func__, ret);
1111 return CMD_RET_FAILURE;
1115 fcb = kzalloc(sizeof(*fcb), GFP_KERNEL);
1117 printf("failed to allocate fcb\n");
1119 return CMD_RET_FAILURE;
1122 fill_fcb(fcb, &cfg);
1125 ret = write_fcb(&cfg, fcb);
1128 dbbt_page = kzalloc(mtd->writesize, GFP_KERNEL);
1130 printf("failed to allocate dbbt_page\n");
1135 dbbt_data_page = kzalloc(mtd->writesize, GFP_KERNEL);
1136 if (!dbbt_data_page) {
1137 printf("failed to allocate dbbt_data_page\n");
1144 dbbt->fingerprint = DBBT_FINGERPRINT;
1145 dbbt->version = DBBT_VERSION_1;
1146 ret = fill_dbbt_data(mtd, dbbt_data_page, CONV_TO_BLOCKS(maxsize));
1148 goto dbbt_data_page_err;
1150 dbbt->dbbtpages = 1;
1153 ret = write_dbbt(&cfg, dbbt, dbbt_data_page);
1156 kfree(dbbt_data_page);
1163 printf("failed to write FCB/DBBT\n");
1164 return CMD_RET_FAILURE;
1167 return CMD_RET_SUCCESS;
1170 /* dump data which is read from NAND chip */
1171 void dump_structure(struct boot_config *boot_cfg, struct fcb_block *fcb,
1172 struct dbbt_block *dbbt, void *dbbt_data_page)
1175 struct mtd_info *mtd = boot_cfg->mtd;
1177 #define P1(x) printf(" %s = 0x%08x\n", #x, fcb->x)
1183 #define P1(x) printf(" %s = %d\n", #x, fcb->x)
1228 #if !defined(CONFIG_MX6) || defined(CONFIG_MX6SX) || \
1229 defined(CONFIG_MX6UL) || defined(CONFIG_MX6ULL)
1230 P1(onfi_sync_enable);
1231 P1(onfi_sync_speed);
1232 P1(onfi_sync_nand_data);
1234 P1(disbbm_search_limit);
1235 P1(read_retry_enable);
1238 #define P1(x) printf(" %s = 0x%08x\n", #x, dbbt->x)
1244 #define P1(x) printf(" %s = %d\n", #x, dbbt->x)
1248 for (i = 0; i < dbbt->dbbtpages; ++i)
1249 printf("%d ", *((u32 *)(dbbt_data_page + i)));
1251 if (!(plat_config.misc_flags & FIRMWARE_EXTRA_ONE)) {
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);
1257 printf("Firmware: image #0 @ 0x%x size 0x%x\n",
1258 fcb->fw1_start, fcb->fw1_pages * mtd->writesize);
1259 printf("Firmware: image #1 @ 0x%x size 0x%x\n",
1260 fcb->fw2_start, fcb->fw2_pages * mtd->writesize);
1261 /* TODO: Add extra image information */
1265 static bool check_fingerprint(void *data, int fingerprint)
1269 return (*(int *)(data + off) == fingerprint);
1272 static int fuse_to_search_count(u32 bank, u32 word, u32 mask, u32 off)
1278 /* by default, the boot search count from fuse should be 2 */
1279 err = fuse_read(bank, word, &val);
1283 val = (val & mask) >> off;
1301 static int nandbcb_dump(struct boot_config *boot_cfg)
1305 struct mtd_info *mtd = boot_cfg->mtd;
1306 struct fcb_block fcb, fcb_copy;
1307 struct dbbt_block dbbt, dbbt_copy;
1308 void *dbbt_data_page, *dbbt_data_page_copy;
1309 bool fcb_not_found, dbbt_not_found;
1312 dbbt_data_page = kzalloc(mtd->writesize, GFP_KERNEL);
1313 if (!dbbt_data_page) {
1314 printf("failed to allocate dbbt_data_page\n");
1319 dbbt_data_page_copy = kzalloc(mtd->writesize, GFP_KERNEL);
1320 if (!dbbt_data_page_copy) {
1321 printf("failed to allocate dbbt_data_page\n");
1329 for (i = 0; i < g_boot_search_count; ++i) {
1330 if (fcb_not_found) {
1331 ret = read_fcb(boot_cfg, &fcb, off);
1334 goto dbbt_page_copy_err;
1338 if (check_fingerprint(&fcb, FCB_FINGERPRINT))
1341 ret = read_fcb(boot_cfg, &fcb_copy, off);
1344 goto dbbt_page_copy_err;
1345 if (memcmp(&fcb, &fcb_copy,
1346 sizeof(struct fcb_block))) {
1347 printf("FCB copies are not identical\n");
1349 goto dbbt_page_copy_err;
1353 /* next read location */
1354 off += g_boot_search_stride;
1359 off = boot_cfg->search_area_size_in_bytes;
1360 for (i = 0; i < g_boot_search_count; ++i) {
1361 if (dbbt_not_found) {
1362 ret = read_dbbt(boot_cfg, &dbbt, dbbt_data_page, off);
1365 goto dbbt_page_copy_err;
1369 if (check_fingerprint(&dbbt, DBBT_FINGERPRINT))
1372 ret = read_dbbt(boot_cfg, &dbbt_copy,
1373 dbbt_data_page_copy, off);
1376 goto dbbt_page_copy_err;
1377 if (memcmp(&dbbt, &dbbt_copy,
1378 sizeof(struct dbbt_block))) {
1379 printf("DBBT copies are not identical\n");
1381 goto dbbt_page_copy_err;
1383 if (dbbt.dbbtpages > 0 &&
1384 memcmp(dbbt_data_page, dbbt_data_page_copy,
1386 printf("DBBT data copies are not identical\n");
1388 goto dbbt_page_copy_err;
1392 /* next read location */
1393 off += g_boot_search_stride;
1396 dump_structure(boot_cfg, &fcb, &dbbt, dbbt_data_page);
1399 kfree(dbbt_data_page_copy);
1401 kfree(dbbt_data_page);
1406 static int do_nandbcb_dump(int argc, char * const argv[])
1408 struct boot_config cfg;
1412 return CMD_RET_USAGE;
1414 memset(&cfg, 0, sizeof(struct boot_config));
1415 if (nandbcb_get_info(argc, argv, &cfg))
1416 return CMD_RET_FAILURE;
1418 if (nandbcb_get_size(argc, argv, 1, &cfg))
1419 return CMD_RET_FAILURE;
1421 if (nandbcb_set_boot_config(argc, argv, &cfg))
1422 return CMD_RET_FAILURE;
1424 ret = nandbcb_dump(&cfg);
1431 static int do_nandbcb_init(int argc, char * const argv[])
1438 struct boot_config cfg;
1441 return CMD_RET_USAGE;
1443 memset(&cfg, 0, sizeof(struct boot_config));
1444 if (nandbcb_get_info(argc, argv, &cfg))
1445 return CMD_RET_FAILURE;
1447 if (nandbcb_get_size(argc, argv, 2, &cfg))
1448 return CMD_RET_FAILURE;
1449 size = cfg.boot_stream1_size;
1451 if (nandbcb_set_boot_config(argc, argv, &cfg))
1452 return CMD_RET_FAILURE;
1454 addr = hextoul(argv[1], &endp);
1455 if (*argv[1] == 0 || *endp != 0)
1456 return CMD_RET_FAILURE;
1458 buf = map_physmem(addr, size, MAP_WRBACK);
1460 puts("failed to map physical memory\n");
1461 return CMD_RET_FAILURE;
1464 ret = nandbcb_init(&cfg, buf);
1466 return ret == 0 ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
1469 static int do_nandbcb(struct cmd_tbl *cmdtp, int flag, int argc,
1478 /* check the platform config first */
1480 plat_config = imx6sx_plat_config;
1481 } else if (is_mx7()) {
1482 plat_config = imx7d_plat_config;
1483 } else if (is_mx6ul() || is_mx6ull()) {
1484 plat_config = imx6ul_plat_config;
1485 } else if (is_mx6() && !is_mx6sx() && !is_mx6ul() && !is_mx6ull()) {
1486 plat_config = imx6qdl_plat_config;
1487 } else if (is_imx8mq()) {
1488 plat_config = imx8mq_plat_config;
1489 } else if (is_imx8mm()) {
1490 plat_config = imx8mm_plat_config;
1491 } else if (is_imx8mn() || is_imx8mp()) {
1492 plat_config = imx8mn_plat_config;
1493 } else if (is_imx8qm() || is_imx8qxp()) {
1494 plat_config = imx8q_plat_config;
1496 printf("ERROR: Unknown platform\n");
1497 return CMD_RET_FAILURE;
1500 if ((plat_config.misc_flags) & BT_SEARCH_CNT_FROM_FUSE) {
1502 g_boot_search_count = fuse_to_search_count(0, 720, 0xc0, 6);
1503 if (is_imx8mn() || is_imx8mp())
1504 g_boot_search_count = fuse_to_search_count(2, 2, 0x6000, 13);
1505 printf("search count set to %d from fuse\n",
1506 g_boot_search_count);
1513 if (strcmp(cmd, "init") == 0) {
1514 ret = do_nandbcb_init(argc, argv);
1518 if (strcmp(cmd, "dump") == 0) {
1519 ret = do_nandbcb_dump(argc, argv);
1523 if (strcmp(cmd, "bcbonly") == 0) {
1524 ret = do_nandbcb_bcbonly(argc, argv);
1532 return CMD_RET_USAGE;
1535 #ifdef CONFIG_SYS_LONGHELP
1536 static char nandbcb_help_text[] =
1537 "init addr off|partition len - update 'len' bytes starting at\n"
1538 " 'off|part' to memory address 'addr', skipping bad blocks\n"
1539 "nandbcb bcbonly off|partition fw1-off fw1-size [fw2-off fw2-size]\n"
1540 " - write BCB only (FCB and DBBT)\n"
1541 " where `fwx-size` is fw sizes in bytes, `fw1-off`\n"
1542 " and `fw2-off` - firmware offsets\n"
1543 " FIY, BCB isn't erased automatically, so mtd erase should\n"
1544 " be called in advance before writing new BCB:\n"
1545 " > mtd erase mx7-bcb\n"
1546 "nandbcb dump off|partition - dump/verify boot structures\n";
1549 U_BOOT_CMD(nandbcb, 7, 1, do_nandbcb,
1550 "i.MX NAND Boot Control Blocks write",