blob: 09622c13c9808410c987d316a2fa209066a38083 [file] [log] [blame]
Shyam Sainif63ef492019-06-14 13:05:33 +05301/*
Han Xu1f314e52020-05-05 22:04:03 +08002 * i.MX nand boot control block(bcb).
Shyam Sainif63ef492019-06-14 13:05:33 +05303 *
4 * Based on the common/imx-bbu-nand-fcb.c from barebox and imx kobs-ng
5 *
6 * Copyright (C) 2017 Jagan Teki <jagan@amarulasolutions.com>
7 * Copyright (C) 2016 Sergey Kubushyn <ksi@koi8.net>
8 *
Han Xu1f314e52020-05-05 22:04:03 +08009 * Reconstucted by Han Xu <han.xu@nxp.com>
10 *
Shyam Sainif63ef492019-06-14 13:05:33 +053011 * SPDX-License-Identifier: GPL-2.0+
12 */
13
14#include <common.h>
Simon Glassed38aef2020-05-10 11:40:03 -060015#include <command.h>
Simon Glass0f2af882020-05-10 11:40:05 -060016#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070017#include <malloc.h>
Shyam Sainif63ef492019-06-14 13:05:33 +053018#include <nand.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070019#include <dm/devres.h>
Simon Glassc06c1be2020-05-10 11:40:08 -060020#include <linux/bug.h>
Shyam Sainif63ef492019-06-14 13:05:33 +053021
22#include <asm/io.h>
23#include <jffs2/jffs2.h>
Parthiban Nallathambia99188b2019-10-18 11:46:19 +020024#include <linux/bch.h>
Shyam Sainif63ef492019-06-14 13:05:33 +053025#include <linux/mtd/mtd.h>
Tom Rini3bde7e22021-09-22 14:50:35 -040026#include <linux/mtd/rawnand.h>
Shyam Sainif63ef492019-06-14 13:05:33 +053027
Igor Opaniuk344833a2019-11-03 16:49:44 +010028#include <asm/arch/sys_proto.h>
Shyam Sainif63ef492019-06-14 13:05:33 +053029#include <asm/mach-imx/imx-nandbcb.h>
30#include <asm/mach-imx/imximage.cfg>
31#include <mxs_nand.h>
32#include <linux/mtd/mtd.h>
33#include <nand.h>
Han Xu65781602020-05-05 22:04:04 +080034#include <fuse.h>
Shyam Sainif63ef492019-06-14 13:05:33 +053035
Miquel Raynal617fd792019-10-25 19:39:29 +020036#include "../../../cmd/legacy-mtd-utils.h"
37
Han Xu1f314e52020-05-05 22:04:03 +080038/* 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)
53
54/* Firmware related flags */
55/* No 1K padding */
56#define FIRMWARE_NEED_PADDING BIT(8)
57/* Extra firmware*/
58#define FIRMWARE_EXTRA_ONE BIT(9)
59/* Secondary firmware on fixed address */
60#define FIRMWARE_SECONDARY_FIXED_ADDR BIT(10)
61
62/* Boot search related flags */
63#define BT_SEARCH_CNT_FROM_FUSE BIT(16)
64
65struct platform_config {
66 int misc_flags;
67};
68
69static struct platform_config plat_config;
70
71/* imx6q/dl/solo */
72static struct platform_config imx6qdl_plat_config = {
73 .misc_flags = FCB_LAYOUT_RESV_12B |
74 FCB_ENCODE_HAMMING |
75 FIRMWARE_NEED_PADDING,
76};
77
78static struct platform_config imx6sx_plat_config = {
79 .misc_flags = FCB_LAYOUT_META_32B |
80 FCB_ENCODE_BCH_62b |
81 FIRMWARE_NEED_PADDING |
82 FCB_RANDON_ENABLED,
83};
84
85static struct platform_config imx7d_plat_config = {
86 .misc_flags = FCB_LAYOUT_META_32B |
87 FCB_ENCODE_BCH_62b |
88 FIRMWARE_NEED_PADDING |
89 FCB_RANDON_ENABLED,
90};
91
92/* imx6ul/ull/ulz */
93static struct platform_config imx6ul_plat_config = {
94 .misc_flags = FCB_LAYOUT_META_32B |
95 FCB_ENCODE_BCH_40b |
96 FIRMWARE_NEED_PADDING,
97};
98
99static struct platform_config imx8mq_plat_config = {
100 .misc_flags = FCB_LAYOUT_META_32B |
101 FCB_ENCODE_BCH_62b |
102 FIRMWARE_NEED_PADDING |
103 FCB_RANDON_ENABLED |
104 FIRMWARE_EXTRA_ONE,
105};
106
107/* all other imx8mm */
108static struct platform_config imx8mm_plat_config = {
109 .misc_flags = FCB_LAYOUT_META_32B |
110 FCB_ENCODE_BCH_62b |
111 FIRMWARE_NEED_PADDING |
112 FCB_RANDON_ENABLED,
113};
114
115/* imx8mn */
116static struct platform_config imx8mn_plat_config = {
117 .misc_flags = FCB_LAYOUT_META_32B |
118 FCB_ENCODE_BCH_62b |
119 FCB_RANDON_ENABLED |
120 FIRMWARE_SECONDARY_FIXED_ADDR |
121 BT_SEARCH_CNT_FROM_FUSE,
122};
123
124/* imx8qx/qm */
125static struct platform_config imx8q_plat_config = {
126 .misc_flags = FCB_LAYOUT_META_32B |
127 FCB_ENCODE_BCH_62b |
128 FCB_RANDON_ENABLED |
129 FIRMWARE_SECONDARY_FIXED_ADDR |
130 BT_SEARCH_CNT_FROM_FUSE,
131};
132
133/* boot search related variables and definitions */
134static int g_boot_search_count = 4;
135static int g_boot_search_stride;
136static int g_pages_per_stride;
137
138/* mtd config structure */
139struct boot_config {
140 int dev;
141 struct mtd_info *mtd;
142 loff_t maxsize;
143 loff_t input_size;
144 loff_t offset;
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;
154};
155
156/* boot_stream config structure */
157struct boot_stream_config {
158 char bs_label[32];
159 loff_t bs_addr;
160 size_t bs_size;
161 void *bs_buf;
162 loff_t next_bs_addr;
163 bool need_padding;
164};
165
166/* FW index */
167#define FW1_ONLY 1
168#define FW2_ONLY 2
169#define FW_ALL FW1_ONLY | FW2_ONLY
170#define FW_INX(x) (1 << (x))
171
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))
175
Shyam Sainif63ef492019-06-14 13:05:33 +0530176#define GETBIT(v, n) (((v) >> (n)) & 0x1)
Alice Guoa3f815b2020-05-05 22:04:00 +0800177#define IMX8MQ_SPL_SZ 0x3e000
178#define IMX8MQ_HDMI_FW_SZ 0x19c00
Han Xu1f314e52020-05-05 22:04:03 +0800179
180static int nandbcb_get_info(int argc, char * const argv[],
181 struct boot_config *boot_cfg)
182{
183 int dev;
184 struct mtd_info *mtd;
185
186 dev = nand_curr_device;
187 if (dev < 0) {
188 printf("failed to get nand_curr_device, run nand device\n");
189 return CMD_RET_FAILURE;
190 }
191
192 mtd = get_nand_dev_by_index(dev);
193 if (!mtd) {
194 printf("failed to get mtd info\n");
195 return CMD_RET_FAILURE;
196 }
197
198 boot_cfg->dev = dev;
199 boot_cfg->mtd = mtd;
200
201 return CMD_RET_SUCCESS;
202}
203
204static int nandbcb_get_size(int argc, char * const argv[], int num,
205 struct boot_config *boot_cfg)
206{
207 int dev;
208 loff_t offset, size, maxsize;
209 struct mtd_info *mtd;
210
211 dev = boot_cfg->dev;
212 mtd = boot_cfg->mtd;
213 size = 0;
214
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;
218
219 boot_cfg->maxsize = maxsize;
220 boot_cfg->offset = offset;
Alice Guoeaab9052020-05-05 22:04:01 +0800221
Han Xu1f314e52020-05-05 22:04:03 +0800222 debug("max: %llx, offset: %llx\n", maxsize, offset);
223
224 if (size && size != maxsize)
225 boot_cfg->input_size = size;
226
227 return CMD_RET_SUCCESS;
228}
229
230static int nandbcb_set_boot_config(int argc, char * const argv[],
231 struct boot_config *boot_cfg)
232{
233 struct mtd_info *mtd;
234 loff_t maxsize;
235 loff_t boot_stream1_address, boot_stream2_address, max_boot_stream_size;
236
237 if (!boot_cfg->mtd) {
238 printf("Didn't get the mtd info, quit\n");
239 return CMD_RET_FAILURE;
240 }
241 mtd = boot_cfg->mtd;
242
243 /*
244 * By default
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
248 */
249
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 */
253
254 g_pages_per_stride = mtd->erasesize / mtd->writesize;
255
256 g_boot_search_stride = mtd->writesize * g_pages_per_stride;
257
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;
263
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;
268 }
269 maxsize = boot_cfg->maxsize;
270 /* align to page boundary */
271 maxsize = ((u32)(maxsize + mtd->writesize - 1)) / (u32)mtd->writesize
272 * mtd->writesize;
273
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);
277
278 if (boot_cfg->secondary_boot_stream_off_in_MB)
Ye Li50710d22020-08-02 23:07:55 -0700279 boot_stream2_address =
280 (loff_t)boot_cfg->secondary_boot_stream_off_in_MB * 1024 * 1024;
Han Xu1f314e52020-05-05 22:04:03 +0800281
282 max_boot_stream_size = boot_stream2_address - boot_stream1_address;
283
284 /* sanity check */
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;
291 }
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;
295
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;
300 }
301
302 return CMD_RET_SUCCESS;
303}
304
305static int nandbcb_check_space(struct boot_config *boot_cfg)
306{
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;
310
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;
315 }
316
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;
321 }
322
323 return CMD_RET_SUCCESS;
324}
Shyam Sainif63ef492019-06-14 13:05:33 +0530325
Parthiban Nallathambia99188b2019-10-18 11:46:19 +0200326#if defined(CONFIG_MX6UL) || defined(CONFIG_MX6ULL)
327static uint8_t reverse_bit(uint8_t b)
328{
329 b = (b & 0xf0) >> 4 | (b & 0x0f) << 4;
330 b = (b & 0xcc) >> 2 | (b & 0x33) << 2;
331 b = (b & 0xaa) >> 1 | (b & 0x55) << 1;
332
333 return b;
334}
335
336static void encode_bch_ecc(void *buf, struct fcb_block *fcb, int eccbits)
337{
338 int i, j, m = 13;
339 int blocksize = 128;
340 int numblocks = 8;
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);
345 u8 *psrc, *pdst;
346
347 /*
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.
351 */
352 WARN_ON(eccbits % 8);
353
354 memcpy(tmp_buf, fcb, sizeof(*fcb));
355
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);
360
361 /* copy data byte aligned to destination buf */
362 memcpy(pdst, psrc, blocksize);
363
364 /*
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.
368 */
369 for (j = 0; j < blocksize; j++)
370 psrc[j] = reverse_bit(psrc[j]);
371
372 encode_bch(bch, psrc, blocksize, ecc_buf);
373
374 /* reverse ecc bit */
375 for (j = 0; j < ecc_buf_size; j++)
376 ecc_buf[j] = reverse_bit(ecc_buf[j]);
377
378 /* Here eccbuf is byte aligned and we can just copy it */
379 memcpy(pdst + blocksize, ecc_buf, ecc_buf_size);
380 }
381
382 kfree(ecc_buf);
383 kfree(tmp_buf);
384 free_bch(bch);
385}
386#else
387
Shyam Sainif63ef492019-06-14 13:05:33 +0530388static u8 calculate_parity_13_8(u8 d)
389{
390 u8 p = 0;
391
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) ^
394 GETBIT(d, 1)) << 1;
395 p |= (GETBIT(d, 7) ^ GETBIT(d, 6) ^ GETBIT(d, 5) ^ GETBIT(d, 1) ^
396 GETBIT(d, 0)) << 2;
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;
400
401 return p;
402}
403
404static void encode_hamming_13_8(void *_src, void *_ecc, size_t size)
405{
406 int i;
407 u8 *src = _src;
408 u8 *ecc = _ecc;
409
410 for (i = 0; i < size; i++)
411 ecc[i] = calculate_parity_13_8(src[i]);
412}
Parthiban Nallathambia99188b2019-10-18 11:46:19 +0200413#endif
Shyam Sainif63ef492019-06-14 13:05:33 +0530414
415static u32 calc_chksum(void *buf, size_t size)
416{
417 u32 chksum = 0;
418 u8 *bp = buf;
419 size_t i;
420
421 for (i = 0; i < size; i++)
422 chksum += bp[i];
423
424 return ~chksum;
425}
426
Han Xu1f314e52020-05-05 22:04:03 +0800427static void fill_fcb(struct fcb_block *fcb, struct boot_config *boot_cfg)
Shyam Sainif63ef492019-06-14 13:05:33 +0530428{
Han Xu1f314e52020-05-05 22:04:03 +0800429 struct mtd_info *mtd = boot_cfg->mtd;
Shyam Sainif63ef492019-06-14 13:05:33 +0530430 struct nand_chip *chip = mtd_to_nand(mtd);
431 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
Igor Opaniuk344833a2019-11-03 16:49:44 +0100432 struct mxs_nand_layout l;
433
434 mxs_nand_get_layout(mtd, &l);
Shyam Sainif63ef492019-06-14 13:05:33 +0530435
436 fcb->fingerprint = FCB_FINGERPRINT;
437 fcb->version = FCB_VERSION_1;
Igor Opaniuk344833a2019-11-03 16:49:44 +0100438
Han Xu1f314e52020-05-05 22:04:03 +0800439 fcb->datasetup = 80;
440 fcb->datahold = 60;
441 fcb->addr_setup = 25;
442 fcb->dsample_time = 6;
443
Shyam Sainif63ef492019-06-14 13:05:33 +0530444 fcb->pagesize = mtd->writesize;
445 fcb->oob_pagesize = mtd->writesize + mtd->oobsize;
446 fcb->sectors = mtd->erasesize / mtd->writesize;
447
Igor Opaniuk344833a2019-11-03 16:49:44 +0100448 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;
Han Xuae0bdd72020-05-05 22:03:59 +0800454 fcb->bchtype = l.gf_len;
Shyam Sainif63ef492019-06-14 13:05:33 +0530455
Han Xu1f314e52020-05-05 22:04:03 +0800456 /* DBBT search area starts from the next block after all FCB */
457 fcb->dbbt_start = boot_cfg->search_area_size_in_pages;
Shyam Sainif63ef492019-06-14 13:05:33 +0530458
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;
461
462 fcb->phy_offset = mtd->writesize;
463
Igor Opaniuk344833a2019-11-03 16:49:44 +0100464 fcb->disbbm = 0;
Igor Opaniuk344833a2019-11-03 16:49:44 +0100465
Han Xu1f314e52020-05-05 22:04:03 +0800466 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);
Igor Opaniuk14a489b2019-11-03 16:49:45 +0100470
Shyam Sainif63ef492019-06-14 13:05:33 +0530471 fcb->checksum = calc_chksum((void *)fcb + 4, sizeof(*fcb) - 4);
472}
473
Han Xu1f314e52020-05-05 22:04:03 +0800474static int fill_dbbt_data(struct mtd_info *mtd, void *buf, int num_blocks)
Shyam Sainif63ef492019-06-14 13:05:33 +0530475{
476 int n, n_bad_blocks = 0;
477 u32 *bb = buf + 0x8;
478 u32 *n_bad_blocksp = buf + 0x4;
479
480 for (n = 0; n < num_blocks; n++) {
Ye Lid701ed52020-08-02 22:59:43 -0700481 loff_t offset = (loff_t)n * mtd->erasesize;
Shyam Sainif63ef492019-06-14 13:05:33 +0530482 if (mtd_block_isbad(mtd, offset)) {
483 n_bad_blocks++;
484 *bb = n;
485 bb++;
486 }
487 }
488
489 *n_bad_blocksp = n_bad_blocks;
490
491 return n_bad_blocks;
492}
493
Han Xu1f314e52020-05-05 22:04:03 +0800494/*
495 * return 1 - bad block
496 * return 0 - read successfully
497 * return < 0 - read failed
498 */
499static int read_fcb(struct boot_config *boot_cfg, struct fcb_block *fcb,
500 loff_t off)
Igor Opaniuk14a489b2019-11-03 16:49:45 +0100501{
Han Xu1f314e52020-05-05 22:04:03 +0800502 struct mtd_info *mtd;
503 void *fcb_raw_page;
504 size_t size;
505 int ret = 0;
506
507 mtd = boot_cfg->mtd;
Han Xu1f314e52020-05-05 22:04:03 +0800508 if (mtd_block_isbad(mtd, off)) {
509 printf("Block %d is bad, skipped\n", (int)CONV_TO_BLOCKS(off));
510 return 1;
511 }
512
Ye Lic61dedb2020-08-02 22:43:45 -0700513 fcb_raw_page = kzalloc(mtd->writesize + mtd->oobsize, GFP_KERNEL);
514 if (!fcb_raw_page) {
515 debug("failed to allocate fcb_raw_page\n");
516 ret = -ENOMEM;
517 return ret;
518 }
519
Han Xu1f314e52020-05-05 22:04:03 +0800520 /*
521 * User BCH hardware to decode ECC for FCB
522 */
523 if (plat_config.misc_flags & FCB_ENCODE_BCH) {
524 size = sizeof(struct fcb_block);
525
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);
531
532 ret = nand_read(mtd, off, &size, (u_char *)fcb);
533
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");
538
539 } else if (plat_config.misc_flags & FCB_ENCODE_HAMMING) {
540 /* raw read*/
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,
546 .mode = MTD_OPS_RAW
547 };
548
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");
552 }
553
554 if (ret)
555 goto fcb_raw_page_err;
556
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));
560
561/* TODO: check if it can pass Hamming check */
562
563fcb_raw_page_err:
564 kfree(fcb_raw_page);
565
566 return ret;
567}
568
569static int write_fcb(struct boot_config *boot_cfg, struct fcb_block *fcb)
570{
571 struct mtd_info *mtd;
572 void *fcb_raw_page = NULL;
Ye Li0c55e332020-08-02 20:55:17 -0700573 int i, ret = 0;
Han Xu1f314e52020-05-05 22:04:03 +0800574 loff_t off;
575 size_t size;
576
577 mtd = boot_cfg->mtd;
Igor Opaniuk14a489b2019-11-03 16:49:45 +0100578
579 /*
580 * We prepare raw page only for i.MX6, for i.MX7 we
581 * leverage BCH hw module instead
582 */
Han Xu1f314e52020-05-05 22:04:03 +0800583 if ((plat_config.misc_flags & FCB_ENCODE_HAMMING) &&
584 (plat_config.misc_flags & FCB_LAYOUT_RESV_12B)) {
Igor Opaniuk14a489b2019-11-03 16:49:45 +0100585 fcb_raw_page = kzalloc(mtd->writesize + mtd->oobsize,
586 GFP_KERNEL);
587 if (!fcb_raw_page) {
588 debug("failed to allocate fcb_raw_page\n");
589 ret = -ENOMEM;
590 return ret;
591 }
592
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);
596#else
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);
600#endif
601 /*
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
608 * bad block table.
609 */
610 memset(fcb_raw_page + mtd->writesize, 0xFF, 2);
611 }
Han Xu1f314e52020-05-05 22:04:03 +0800612
613 /* start writing FCB from the very beginning */
614 off = 0;
615
616 for (i = 0; i < g_boot_search_count; i++) {
Igor Opaniuk14a489b2019-11-03 16:49:45 +0100617 if (mtd_block_isbad(mtd, off)) {
618 printf("Block %d is bad, skipped\n", i);
619 continue;
620 }
621
622 /*
Han Xu1f314e52020-05-05 22:04:03 +0800623 * User BCH hardware module to generate ECC for FCB
Igor Opaniuk14a489b2019-11-03 16:49:45 +0100624 */
Han Xu1f314e52020-05-05 22:04:03 +0800625 if (plat_config.misc_flags & FCB_ENCODE_BCH) {
626 size = sizeof(struct fcb_block);
Igor Opaniuk14a489b2019-11-03 16:49:45 +0100627
628 /* switch nand BCH to FCB compatible settings */
Han Xu1f314e52020-05-05 22:04:03 +0800629 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);
Alice Guoeaab9052020-05-05 22:04:01 +0800633
Han Xu1f314e52020-05-05 22:04:03 +0800634 ret = nand_write(mtd, off, &size, (u_char *)fcb);
Alice Guoeaab9052020-05-05 22:04:01 +0800635
Han Xu1f314e52020-05-05 22:04:03 +0800636 /* switch BCH back */
Igor Opaniuk14a489b2019-11-03 16:49:45 +0100637 mxs_nand_mode_normal(mtd);
Han Xu1f314e52020-05-05 22:04:03 +0800638 printf("NAND FCB write to 0x%zx offset 0x%llx written: %s\n",
639 size, off, ret ? "ERROR" : "OK");
Igor Opaniuk14a489b2019-11-03 16:49:45 +0100640
Han Xu1f314e52020-05-05 22:04:03 +0800641 } else if (plat_config.misc_flags & FCB_ENCODE_HAMMING) {
Igor Opaniuk14a489b2019-11-03 16:49:45 +0100642 /* raw write */
643 mtd_oob_ops_t ops = {
644 .datbuf = (u8 *)fcb_raw_page,
645 .oobbuf = ((u8 *)fcb_raw_page) +
646 mtd->writesize,
647 .len = mtd->writesize,
648 .ooblen = mtd->oobsize,
649 .mode = MTD_OPS_RAW
650 };
651
Han Xu1f314e52020-05-05 22:04:03 +0800652 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");
Igor Opaniuk14a489b2019-11-03 16:49:45 +0100654 }
655
Igor Opaniuk14a489b2019-11-03 16:49:45 +0100656 if (ret)
657 goto fcb_raw_page_err;
Han Xu1f314e52020-05-05 22:04:03 +0800658
659 /* next writing location */
660 off += g_boot_search_stride;
661 }
662
Han Xu1f314e52020-05-05 22:04:03 +0800663fcb_raw_page_err:
664 kfree(fcb_raw_page);
665
666 return ret;
667}
668
669/*
670 * return 1 - bad block
671 * return 0 - read successfully
672 * return < 0 - read failed
673 */
674static int read_dbbt(struct boot_config *boot_cfg, struct dbbt_block *dbbt,
675 void *dbbt_data_page, loff_t off)
676{
677 size_t size;
678 struct mtd_info *mtd;
679 loff_t to;
680 int ret;
Igor Opaniuk14a489b2019-11-03 16:49:45 +0100681
Han Xu1f314e52020-05-05 22:04:03 +0800682 mtd = boot_cfg->mtd;
Alice Guoeaab9052020-05-05 22:04:01 +0800683
Han Xu1f314e52020-05-05 22:04:03 +0800684 if (mtd_block_isbad(mtd, off)) {
685 printf("Block %d is bad, skipped\n",
686 (int)CONV_TO_BLOCKS(off));
687 return 1;
688 }
689
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");
694 if (ret)
695 return ret;
696
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");
704
705 if (ret)
706 return ret;
707 }
708
709 return 0;
710}
711
712static int write_dbbt(struct boot_config *boot_cfg, struct dbbt_block *dbbt,
713 void *dbbt_data_page)
714{
715 int i;
716 loff_t off, to;
717 size_t size;
718 struct mtd_info *mtd;
719 int ret;
720
721 mtd = boot_cfg->mtd;
722
723 /* start writing DBBT after all FCBs */
724 off = boot_cfg->search_area_size_in_bytes;
725 size = mtd->writesize;
726
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)));
731 continue;
732 }
733
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");
737 if (ret)
738 return ret;
Alice Guoeaab9052020-05-05 22:04:01 +0800739
Igor Opaniuk14a489b2019-11-03 16:49:45 +0100740 /* dbbtpages == 0 if no bad blocks */
741 if (dbbt->dbbtpages > 0) {
Han Xu1f314e52020-05-05 22:04:03 +0800742 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");
Igor Opaniuk14a489b2019-11-03 16:49:45 +0100746
Han Xu1f314e52020-05-05 22:04:03 +0800747 if (ret)
748 return ret;
Igor Opaniuk14a489b2019-11-03 16:49:45 +0100749 }
Han Xu1f314e52020-05-05 22:04:03 +0800750
751 /* next writing location */
752 off += g_boot_search_stride;
Igor Opaniuk14a489b2019-11-03 16:49:45 +0100753 }
754
Han Xu1f314e52020-05-05 22:04:03 +0800755 return 0;
756}
757
758/* reuse the check_skip_len from nand_util.c with minor change*/
759static int check_skip_length(struct boot_config *boot_cfg, loff_t offset,
760 size_t length, size_t *used)
761{
762 struct mtd_info *mtd = boot_cfg->mtd;
763 size_t maxsize = boot_cfg->maxsize;
764 size_t len_excl_bad = 0;
765 int ret = 0;
766
767 while (len_excl_bad < length) {
768 size_t block_len, block_off;
769 loff_t block_start;
770
771 if (offset >= maxsize)
772 return -1;
773
774 block_start = offset & ~(loff_t)(mtd->erasesize - 1);
775 block_off = offset & (mtd->erasesize - 1);
776 block_len = mtd->erasesize - block_off;
777
778 if (!nand_block_isbad(mtd, block_start))
779 len_excl_bad += block_len;
780 else
781 ret = 1;
782
783 offset += block_len;
784 *used += block_len;
785 }
786
787 /* If the length is not a multiple of block_len, adjust. */
788 if (len_excl_bad > length)
789 *used -= (len_excl_bad - length);
790
791 return ret;
792}
793
794static int nandbcb_get_next_good_blk_addr(struct boot_config *boot_cfg,
795 struct boot_stream_config *bs_cfg)
796{
797 struct mtd_info *mtd = boot_cfg->mtd;
798 loff_t offset = bs_cfg->bs_addr;
799 size_t length = bs_cfg->bs_size;
800 size_t used = 0;
801 int ret;
802
803 ret = check_skip_length(boot_cfg, offset, length, &used);
804
805 if (ret < 0)
806 return ret;
807
808 /* get next image address */
809 bs_cfg->next_bs_addr = (u32)(offset + used + mtd->erasesize - 1)
810 / (u32)mtd->erasesize * mtd->erasesize;
811
812 return ret;
813}
814
815static int nandbcb_write_bs_skip_bad(struct boot_config *boot_cfg,
816 struct boot_stream_config *bs_cfg)
817{
818 struct mtd_info *mtd;
819 void *buf;
820 loff_t offset, maxsize;
821 size_t size;
822 size_t length;
823 int ret;
824 bool padding_flag = false;
825
826 mtd = boot_cfg->mtd;
827 offset = bs_cfg->bs_addr;
828 maxsize = boot_cfg->maxsize;
829 size = bs_cfg->bs_size;
830
831 /* some boot images may need leading offset */
832 if (bs_cfg->need_padding &&
833 (plat_config.misc_flags & FIRMWARE_NEED_PADDING))
834 padding_flag = 1;
835
836 if (padding_flag)
837 length = ALIGN(size + FLASH_OFFSET_STANDARD, mtd->writesize);
838 else
839 length = ALIGN(size, mtd->writesize);
840
841 buf = kzalloc(length, GFP_KERNEL);
842 if (!buf) {
843 printf("failed to allocate buffer for firmware\n");
844 ret = -ENOMEM;
845 return ret;
846 }
847
848 if (padding_flag)
849 memcpy(buf + FLASH_OFFSET_STANDARD, bs_cfg->bs_buf, size);
850 else
851 memcpy(buf, bs_cfg->bs_buf, size);
Igor Opaniuk14a489b2019-11-03 16:49:45 +0100852
Han Xu1f314e52020-05-05 22:04:03 +0800853 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");
857
858 if (ret)
859 /* write image failed, quit */
860 goto err;
861
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);
865 if (ret < 0) {
866 printf("Next image cannot fit in NAND partition\n");
867 goto err;
868 }
869 }
870
871 /* now we know how the exact image size written to NAND */
872 bs_cfg->bs_size = length;
873 return 0;
874err:
875 kfree(buf);
Igor Opaniuk14a489b2019-11-03 16:49:45 +0100876 return ret;
877}
878
Han Xu1f314e52020-05-05 22:04:03 +0800879static int nandbcb_write_fw(struct boot_config *boot_cfg, u_char *buf,
880 int index)
Shyam Sainif63ef492019-06-14 13:05:33 +0530881{
Han Xu1f314e52020-05-05 22:04:03 +0800882 int i;
883 loff_t offset;
884 size_t size;
885 loff_t next_bs_addr;
886 struct boot_stream_config bs_cfg;
887 int ret;
888
889 for (i = 0; i < 2; ++i) {
890 if (!(FW_INX(i) & index))
891 continue;
892
893 if (i == 0) {
894 offset = boot_cfg->boot_stream1_address;
895 size = boot_cfg->boot_stream1_size;
896 } else {
897 offset = boot_cfg->boot_stream2_address;
898 size = boot_cfg->boot_stream2_size;
899 }
900
901 /* write Firmware*/
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;
907 bs_cfg.bs_buf = buf;
908 bs_cfg.need_padding = 1;
909
910 ret = nandbcb_write_bs_skip_bad(boot_cfg, &bs_cfg);
911 if (ret)
912 return ret;
913
914 /* update the boot stream size */
915 if (i == 0)
916 boot_cfg->boot_stream1_size = bs_cfg.bs_size;
917 else
918 boot_cfg->boot_stream2_size = bs_cfg.bs_size;
919
920 } else {
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;
926 bs_cfg.bs_buf = buf;
927 bs_cfg.need_padding = 1;
928
929 ret = nandbcb_write_bs_skip_bad(boot_cfg, &bs_cfg);
930 if (ret)
931 return ret;
932
933 /* update the boot stream size */
934 if (i == 0)
935 boot_cfg->boot_stream1_size = bs_cfg.bs_size;
936 else
937 boot_cfg->boot_stream2_size = bs_cfg.bs_size;
938
939 /* get next image address */
940 next_bs_addr = bs_cfg.next_bs_addr;
941
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;
948
949 ret = nandbcb_write_bs_skip_bad(boot_cfg, &bs_cfg);
950 if (ret)
951 return ret;
952 }
953 }
954
955 return 0;
956}
957
958static int nandbcb_init(struct boot_config *boot_cfg, u_char *buf)
959{
960 struct mtd_info *mtd;
Shyam Sainif63ef492019-06-14 13:05:33 +0530961 nand_erase_options_t opts;
962 struct fcb_block *fcb;
963 struct dbbt_block *dbbt;
Han Xu1f314e52020-05-05 22:04:03 +0800964 void *dbbt_page, *dbbt_data_page;
Igor Opaniuk14a489b2019-11-03 16:49:45 +0100965 int ret;
Han Xu1f314e52020-05-05 22:04:03 +0800966 loff_t maxsize, off;
967
968 mtd = boot_cfg->mtd;
969 maxsize = boot_cfg->maxsize;
970 off = boot_cfg->offset;
Shyam Sainif63ef492019-06-14 13:05:33 +0530971
972 /* erase */
973 memset(&opts, 0, sizeof(opts));
974 opts.offset = off;
975 opts.length = maxsize - 1;
976 ret = nand_erase_opts(mtd, &opts);
977 if (ret) {
978 printf("%s: erase failed (ret = %d)\n", __func__, ret);
979 return ret;
980 }
981
982 /*
983 * Reference documentation from i.MX6DQRM section 8.5.2.2
984 *
985 * Nand Boot Control Block(BCB) contains two data structures,
986 * - Firmware Configuration Block(FCB)
987 * - Discovered Bad Block Table(DBBT)
988 *
989 * FCB contains,
990 * - nand timings
991 * - DBBT search page address,
992 * - start page address of primary firmware
993 * - start page address of secondary firmware
994 *
995 * setup fcb:
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
Han Xu1f314e52020-05-05 22:04:03 +08001000 * - same firmware write twice
Shyam Sainif63ef492019-06-14 13:05:33 +05301001 */
Shyam Sainif63ef492019-06-14 13:05:33 +05301002
Han Xu1f314e52020-05-05 22:04:03 +08001003 /* write Firmware*/
1004 ret = nandbcb_write_fw(boot_cfg, buf, FW_ALL);
1005 if (ret)
1006 goto err;
Shyam Sainif63ef492019-06-14 13:05:33 +05301007
1008 /* fill fcb */
1009 fcb = kzalloc(sizeof(*fcb), GFP_KERNEL);
1010 if (!fcb) {
1011 debug("failed to allocate fcb\n");
1012 ret = -ENOMEM;
Han Xu1f314e52020-05-05 22:04:03 +08001013 return ret;
Shyam Sainif63ef492019-06-14 13:05:33 +05301014 }
Han Xu1f314e52020-05-05 22:04:03 +08001015 fill_fcb(fcb, boot_cfg);
Shyam Sainif63ef492019-06-14 13:05:33 +05301016
Han Xu1f314e52020-05-05 22:04:03 +08001017 ret = write_fcb(boot_cfg, fcb);
Alice Guoeaab9052020-05-05 22:04:01 +08001018
Shyam Sainif63ef492019-06-14 13:05:33 +05301019 /* fill dbbt */
1020 dbbt_page = kzalloc(mtd->writesize, GFP_KERNEL);
1021 if (!dbbt_page) {
1022 debug("failed to allocate dbbt_page\n");
1023 ret = -ENOMEM;
1024 goto fcb_err;
1025 }
1026
1027 dbbt_data_page = kzalloc(mtd->writesize, GFP_KERNEL);
1028 if (!dbbt_data_page) {
1029 debug("failed to allocate dbbt_data_page\n");
1030 ret = -ENOMEM;
1031 goto dbbt_page_err;
1032 }
1033
1034 dbbt = dbbt_page;
1035 dbbt->checksum = 0;
Han Xu1f314e52020-05-05 22:04:03 +08001036 dbbt->fingerprint = DBBT_FINGERPRINT;
Shyam Sainif63ef492019-06-14 13:05:33 +05301037 dbbt->version = DBBT_VERSION_1;
Han Xu1f314e52020-05-05 22:04:03 +08001038 ret = fill_dbbt_data(mtd, dbbt_data_page, CONV_TO_BLOCKS(maxsize));
Shyam Sainif63ef492019-06-14 13:05:33 +05301039 if (ret < 0)
1040 goto dbbt_data_page_err;
1041 else if (ret > 0)
1042 dbbt->dbbtpages = 1;
1043
Han Xu1f314e52020-05-05 22:04:03 +08001044 /* write dbbt */
1045 ret = write_dbbt(boot_cfg, dbbt, dbbt_data_page);
Igor Opaniuk14a489b2019-11-03 16:49:45 +01001046 if (ret < 0)
1047 printf("failed to write FCB/DBBT\n");
Shyam Sainif63ef492019-06-14 13:05:33 +05301048
Shyam Sainif63ef492019-06-14 13:05:33 +05301049dbbt_data_page_err:
1050 kfree(dbbt_data_page);
1051dbbt_page_err:
1052 kfree(dbbt_page);
1053fcb_err:
1054 kfree(fcb);
Shyam Sainif63ef492019-06-14 13:05:33 +05301055err:
1056 return ret;
1057}
1058
Simon Glassed38aef2020-05-10 11:40:03 -06001059static int do_nandbcb_bcbonly(int argc, char *const argv[])
Igor Opaniukf3caefe2019-11-03 16:49:46 +01001060{
1061 struct fcb_block *fcb;
1062 struct dbbt_block *dbbt;
Igor Opaniukf3caefe2019-11-03 16:49:46 +01001063 struct mtd_info *mtd;
Han Xu1f314e52020-05-05 22:04:03 +08001064 nand_erase_options_t opts;
1065 size_t maxsize;
1066 loff_t off;
Igor Opaniukf3caefe2019-11-03 16:49:46 +01001067 void *dbbt_page, *dbbt_data_page;
Han Xu1f314e52020-05-05 22:04:03 +08001068 int ret;
1069 struct boot_config cfg;
Igor Opaniukf3caefe2019-11-03 16:49:46 +01001070
Han Xu1f314e52020-05-05 22:04:03 +08001071 if (argc < 4)
1072 return CMD_RET_USAGE;
1073
1074 memset(&cfg, 0, sizeof(struct boot_config));
1075 if (nandbcb_get_info(argc, argv, &cfg))
Igor Opaniukf3caefe2019-11-03 16:49:46 +01001076 return CMD_RET_FAILURE;
Igor Opaniukf3caefe2019-11-03 16:49:46 +01001077
Han Xu1f314e52020-05-05 22:04:03 +08001078 /* only get the partition info */
1079 if (nandbcb_get_size(2, argv, 1, &cfg))
1080 return CMD_RET_FAILURE;
Igor Opaniukf3caefe2019-11-03 16:49:46 +01001081
Han Xu1f314e52020-05-05 22:04:03 +08001082 if (nandbcb_set_boot_config(argc, argv, &cfg))
Igor Opaniukf3caefe2019-11-03 16:49:46 +01001083 return CMD_RET_FAILURE;
1084
Han Xu1f314e52020-05-05 22:04:03 +08001085 mtd = cfg.mtd;
Igor Opaniukf3caefe2019-11-03 16:49:46 +01001086
Simon Glass3ff49ec2021-07-24 09:03:29 -06001087 cfg.boot_stream1_address = hextoul(argv[2], NULL);
1088 cfg.boot_stream1_size = hextoul(argv[3], NULL);
Han Xu1f314e52020-05-05 22:04:03 +08001089 cfg.boot_stream1_size = ALIGN(cfg.boot_stream1_size, mtd->writesize);
1090
1091 if (argc > 5) {
Simon Glass3ff49ec2021-07-24 09:03:29 -06001092 cfg.boot_stream2_address = hextoul(argv[4], NULL);
1093 cfg.boot_stream2_size = hextoul(argv[5], NULL);
Han Xu1f314e52020-05-05 22:04:03 +08001094 cfg.boot_stream2_size = ALIGN(cfg.boot_stream2_size,
1095 mtd->writesize);
1096 }
1097
1098 /* sanity check */
1099 nandbcb_check_space(&cfg);
1100
1101 maxsize = cfg.maxsize;
1102 off = cfg.offset;
1103
1104 /* erase the previous FCB/DBBT */
1105 memset(&opts, 0, sizeof(opts));
1106 opts.offset = off;
1107 opts.length = g_boot_search_stride * 2;
1108 ret = nand_erase_opts(mtd, &opts);
1109 if (ret) {
1110 printf("%s: erase failed (ret = %d)\n", __func__, ret);
1111 return CMD_RET_FAILURE;
1112 }
Igor Opaniukf3caefe2019-11-03 16:49:46 +01001113
1114 /* fill fcb */
1115 fcb = kzalloc(sizeof(*fcb), GFP_KERNEL);
1116 if (!fcb) {
Han Xu1f314e52020-05-05 22:04:03 +08001117 printf("failed to allocate fcb\n");
Igor Opaniukf3caefe2019-11-03 16:49:46 +01001118 ret = -ENOMEM;
1119 return CMD_RET_FAILURE;
1120 }
1121
Han Xu1f314e52020-05-05 22:04:03 +08001122 fill_fcb(fcb, &cfg);
1123
1124 /* write fcb */
1125 ret = write_fcb(&cfg, fcb);
Igor Opaniukf3caefe2019-11-03 16:49:46 +01001126
1127 /* fill dbbt */
1128 dbbt_page = kzalloc(mtd->writesize, GFP_KERNEL);
1129 if (!dbbt_page) {
Han Xu1f314e52020-05-05 22:04:03 +08001130 printf("failed to allocate dbbt_page\n");
Igor Opaniukf3caefe2019-11-03 16:49:46 +01001131 ret = -ENOMEM;
1132 goto fcb_err;
1133 }
1134
1135 dbbt_data_page = kzalloc(mtd->writesize, GFP_KERNEL);
1136 if (!dbbt_data_page) {
Han Xu1f314e52020-05-05 22:04:03 +08001137 printf("failed to allocate dbbt_data_page\n");
Igor Opaniukf3caefe2019-11-03 16:49:46 +01001138 ret = -ENOMEM;
1139 goto dbbt_page_err;
1140 }
1141
1142 dbbt = dbbt_page;
1143 dbbt->checksum = 0;
Han Xu1f314e52020-05-05 22:04:03 +08001144 dbbt->fingerprint = DBBT_FINGERPRINT;
Igor Opaniukf3caefe2019-11-03 16:49:46 +01001145 dbbt->version = DBBT_VERSION_1;
Han Xu1f314e52020-05-05 22:04:03 +08001146 ret = fill_dbbt_data(mtd, dbbt_data_page, CONV_TO_BLOCKS(maxsize));
Igor Opaniukf3caefe2019-11-03 16:49:46 +01001147 if (ret < 0)
1148 goto dbbt_data_page_err;
1149 else if (ret > 0)
1150 dbbt->dbbtpages = 1;
1151
Han Xu1f314e52020-05-05 22:04:03 +08001152 /* write dbbt */
1153 ret = write_dbbt(&cfg, dbbt, dbbt_data_page);
1154
Igor Opaniukf3caefe2019-11-03 16:49:46 +01001155dbbt_data_page_err:
1156 kfree(dbbt_data_page);
1157dbbt_page_err:
1158 kfree(dbbt_page);
1159fcb_err:
1160 kfree(fcb);
1161
1162 if (ret < 0) {
1163 printf("failed to write FCB/DBBT\n");
1164 return CMD_RET_FAILURE;
1165 }
1166
1167 return CMD_RET_SUCCESS;
1168}
1169
Alice Guoeaab9052020-05-05 22:04:01 +08001170/* dump data which is read from NAND chip */
Han Xu1f314e52020-05-05 22:04:03 +08001171void dump_structure(struct boot_config *boot_cfg, struct fcb_block *fcb,
1172 struct dbbt_block *dbbt, void *dbbt_data_page)
Alice Guoeaab9052020-05-05 22:04:01 +08001173{
Han Xu1f314e52020-05-05 22:04:03 +08001174 int i;
1175 struct mtd_info *mtd = boot_cfg->mtd;
1176
1177 #define P1(x) printf(" %s = 0x%08x\n", #x, fcb->x)
1178 printf("FCB\n");
Alice Guoeaab9052020-05-05 22:04:01 +08001179 P1(checksum);
1180 P1(fingerprint);
1181 P1(version);
1182 #undef P1
Han Xu1f314e52020-05-05 22:04:03 +08001183 #define P1(x) printf(" %s = %d\n", #x, fcb->x)
Alice Guoeaab9052020-05-05 22:04:01 +08001184 P1(datasetup);
1185 P1(datahold);
1186 P1(addr_setup);
1187 P1(dsample_time);
1188 P1(pagesize);
1189 P1(oob_pagesize);
1190 P1(sectors);
1191 P1(nr_nand);
1192 P1(nr_die);
1193 P1(celltype);
1194 P1(ecc_type);
1195 P1(ecc_nr);
1196 P1(ecc_size);
1197 P1(ecc_level);
1198 P1(meta_size);
1199 P1(nr_blocks);
1200 P1(ecc_type_sdk);
1201 P1(ecc_nr_sdk);
1202 P1(ecc_size_sdk);
1203 P1(ecc_level_sdk);
1204 P1(nr_blocks_sdk);
1205 P1(meta_size_sdk);
1206 P1(erase_th);
1207 P1(bootpatch);
1208 P1(patch_size);
1209 P1(fw1_start);
1210 P1(fw2_start);
1211 P1(fw1_pages);
1212 P1(fw2_pages);
1213 P1(dbbt_start);
1214 P1(bb_byte);
1215 P1(bb_start_bit);
1216 P1(phy_offset);
1217 P1(bchtype);
1218 P1(readlatency);
1219 P1(predelay);
1220 P1(cedelay);
1221 P1(postdelay);
1222 P1(cmdaddpause);
1223 P1(datapause);
1224 P1(tmspeed);
1225 P1(busytimeout);
1226 P1(disbbm);
1227 P1(spare_offset);
Han Xu1f314e52020-05-05 22:04:03 +08001228#if !defined(CONFIG_MX6) || defined(CONFIG_MX6SX) || \
1229 defined(CONFIG_MX6UL) || defined(CONFIG_MX6ULL)
Alice Guoeaab9052020-05-05 22:04:01 +08001230 P1(onfi_sync_enable);
1231 P1(onfi_sync_speed);
1232 P1(onfi_sync_nand_data);
1233 P1(disbbm_search);
1234 P1(disbbm_search_limit);
1235 P1(read_retry_enable);
Han Xu1f314e52020-05-05 22:04:03 +08001236#endif
Alice Guoeaab9052020-05-05 22:04:01 +08001237 #undef P1
Han Xu1f314e52020-05-05 22:04:03 +08001238 #define P1(x) printf(" %s = 0x%08x\n", #x, dbbt->x)
1239 printf("DBBT :\n");
Alice Guoeaab9052020-05-05 22:04:01 +08001240 P1(checksum);
1241 P1(fingerprint);
1242 P1(version);
1243 #undef P1
Han Xu1f314e52020-05-05 22:04:03 +08001244 #define P1(x) printf(" %s = %d\n", #x, dbbt->x)
1245 P1(dbbtpages);
Alice Guoeaab9052020-05-05 22:04:01 +08001246 #undef P1
1247
Han Xu1f314e52020-05-05 22:04:03 +08001248 for (i = 0; i < dbbt->dbbtpages; ++i)
1249 printf("%d ", *((u32 *)(dbbt_data_page + i)));
1250
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);
1256 } else {
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 */
Alice Guoeaab9052020-05-05 22:04:01 +08001262 }
1263}
1264
Han Xu1f314e52020-05-05 22:04:03 +08001265static bool check_fingerprint(void *data, int fingerprint)
Alice Guoeaab9052020-05-05 22:04:01 +08001266{
Han Xu1f314e52020-05-05 22:04:03 +08001267 int off = 4;
Alice Guoeaab9052020-05-05 22:04:01 +08001268
Han Xu1f314e52020-05-05 22:04:03 +08001269 return (*(int *)(data + off) == fingerprint);
1270}
Alice Guoeaab9052020-05-05 22:04:01 +08001271
Han Xu65781602020-05-05 22:04:04 +08001272static int fuse_to_search_count(u32 bank, u32 word, u32 mask, u32 off)
1273{
1274 int err;
1275 u32 val;
1276 int ret;
1277
1278 /* by default, the boot search count from fuse should be 2 */
1279 err = fuse_read(bank, word, &val);
1280 if (err)
1281 return 2;
1282
1283 val = (val & mask) >> off;
1284
1285 switch (val) {
1286 case 0:
1287 ret = 2;
1288 break;
1289 case 1:
1290 case 2:
1291 case 3:
1292 ret = 1 << val;
1293 break;
1294 default:
1295 ret = 2;
1296 }
1297
1298 return ret;
1299}
1300
Han Xu1f314e52020-05-05 22:04:03 +08001301static int nandbcb_dump(struct boot_config *boot_cfg)
1302{
1303 int i;
1304 loff_t off;
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;
1310 int ret = 0;
1311
1312 dbbt_data_page = kzalloc(mtd->writesize, GFP_KERNEL);
1313 if (!dbbt_data_page) {
1314 printf("failed to allocate dbbt_data_page\n");
1315 ret = -ENOMEM;
1316 return ret;
Alice Guoeaab9052020-05-05 22:04:01 +08001317 }
1318
Han Xu1f314e52020-05-05 22:04:03 +08001319 dbbt_data_page_copy = kzalloc(mtd->writesize, GFP_KERNEL);
1320 if (!dbbt_data_page_copy) {
1321 printf("failed to allocate dbbt_data_page\n");
1322 ret = -ENOMEM;
1323 goto dbbt_page_err;
1324 }
Alice Guoeaab9052020-05-05 22:04:01 +08001325
Han Xu1f314e52020-05-05 22:04:03 +08001326 /* read fcb */
1327 fcb_not_found = 1;
1328 off = 0;
1329 for (i = 0; i < g_boot_search_count; ++i) {
1330 if (fcb_not_found) {
1331 ret = read_fcb(boot_cfg, &fcb, off);
Alice Guoeaab9052020-05-05 22:04:01 +08001332
Han Xu1f314e52020-05-05 22:04:03 +08001333 if (ret < 0)
1334 goto dbbt_page_copy_err;
1335 else if (ret == 1)
1336 continue;
1337 else if (ret == 0)
1338 if (check_fingerprint(&fcb, FCB_FINGERPRINT))
1339 fcb_not_found = 0;
Alice Guoeaab9052020-05-05 22:04:01 +08001340 } else {
Han Xu1f314e52020-05-05 22:04:03 +08001341 ret = read_fcb(boot_cfg, &fcb_copy, off);
1342
1343 if (ret < 0)
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");
1348 ret = -EINVAL;
1349 goto dbbt_page_copy_err;
1350 }
Alice Guoeaab9052020-05-05 22:04:01 +08001351 }
Alice Guoeaab9052020-05-05 22:04:01 +08001352
Han Xu1f314e52020-05-05 22:04:03 +08001353 /* next read location */
1354 off += g_boot_search_stride;
1355 }
Alice Guoeaab9052020-05-05 22:04:01 +08001356
Han Xu1f314e52020-05-05 22:04:03 +08001357 /* read dbbt*/
1358 dbbt_not_found = 1;
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);
Alice Guoeaab9052020-05-05 22:04:01 +08001363
Han Xu1f314e52020-05-05 22:04:03 +08001364 if (ret < 0)
1365 goto dbbt_page_copy_err;
1366 else if (ret == 1)
1367 continue;
1368 else if (ret == 0)
1369 if (check_fingerprint(&dbbt, DBBT_FINGERPRINT))
1370 dbbt_not_found = 0;
Alice Guoeaab9052020-05-05 22:04:01 +08001371 } else {
Han Xu1f314e52020-05-05 22:04:03 +08001372 ret = read_dbbt(boot_cfg, &dbbt_copy,
1373 dbbt_data_page_copy, off);
1374
1375 if (ret < 0)
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");
1380 ret = -EINVAL;
1381 goto dbbt_page_copy_err;
1382 }
1383 if (dbbt.dbbtpages > 0 &&
1384 memcmp(dbbt_data_page, dbbt_data_page_copy,
1385 mtd->writesize)) {
1386 printf("DBBT data copies are not identical\n");
1387 ret = -EINVAL;
1388 goto dbbt_page_copy_err;
1389 }
Alice Guoeaab9052020-05-05 22:04:01 +08001390 }
Han Xu1f314e52020-05-05 22:04:03 +08001391
1392 /* next read location */
1393 off += g_boot_search_stride;
Alice Guoeaab9052020-05-05 22:04:01 +08001394 }
1395
Han Xu1f314e52020-05-05 22:04:03 +08001396 dump_structure(boot_cfg, &fcb, &dbbt, dbbt_data_page);
1397
1398dbbt_page_copy_err:
1399 kfree(dbbt_data_page_copy);
1400dbbt_page_err:
1401 kfree(dbbt_data_page);
1402
1403 return ret;
1404}
1405
1406static int do_nandbcb_dump(int argc, char * const argv[])
1407{
1408 struct boot_config cfg;
1409 int ret;
Alice Guoeaab9052020-05-05 22:04:01 +08001410
Han Xu1f314e52020-05-05 22:04:03 +08001411 if (argc != 2)
Alice Guoeaab9052020-05-05 22:04:01 +08001412 return CMD_RET_USAGE;
Han Xu1f314e52020-05-05 22:04:03 +08001413
1414 memset(&cfg, 0, sizeof(struct boot_config));
1415 if (nandbcb_get_info(argc, argv, &cfg))
1416 return CMD_RET_FAILURE;
Alice Guoeaab9052020-05-05 22:04:01 +08001417
Han Xu1f314e52020-05-05 22:04:03 +08001418 if (nandbcb_get_size(argc, argv, 1, &cfg))
1419 return CMD_RET_FAILURE;
Alice Guoeaab9052020-05-05 22:04:01 +08001420
Han Xu1f314e52020-05-05 22:04:03 +08001421 if (nandbcb_set_boot_config(argc, argv, &cfg))
1422 return CMD_RET_FAILURE;
1423
1424 ret = nandbcb_dump(&cfg);
1425 if (ret)
1426 return ret;
1427
1428 return ret;
Alice Guoeaab9052020-05-05 22:04:01 +08001429}
1430
Han Xu1f314e52020-05-05 22:04:03 +08001431static int do_nandbcb_init(int argc, char * const argv[])
Shyam Sainif63ef492019-06-14 13:05:33 +05301432{
Shyam Sainif63ef492019-06-14 13:05:33 +05301433 u_char *buf;
Han Xu1f314e52020-05-05 22:04:03 +08001434 size_t size;
1435 loff_t addr;
1436 char *endp;
Shyam Sainif63ef492019-06-14 13:05:33 +05301437 int ret;
Han Xu1f314e52020-05-05 22:04:03 +08001438 struct boot_config cfg;
Shyam Sainif63ef492019-06-14 13:05:33 +05301439
1440 if (argc != 4)
1441 return CMD_RET_USAGE;
1442
Han Xu1f314e52020-05-05 22:04:03 +08001443 memset(&cfg, 0, sizeof(struct boot_config));
1444 if (nandbcb_get_info(argc, argv, &cfg))
Shyam Sainif63ef492019-06-14 13:05:33 +05301445 return CMD_RET_FAILURE;
Shyam Sainif63ef492019-06-14 13:05:33 +05301446
Han Xu1f314e52020-05-05 22:04:03 +08001447 if (nandbcb_get_size(argc, argv, 2, &cfg))
Shyam Sainif63ef492019-06-14 13:05:33 +05301448 return CMD_RET_FAILURE;
Han Xu1f314e52020-05-05 22:04:03 +08001449 size = cfg.boot_stream1_size;
Shyam Sainif63ef492019-06-14 13:05:33 +05301450
Han Xu1f314e52020-05-05 22:04:03 +08001451 if (nandbcb_set_boot_config(argc, argv, &cfg))
Shyam Sainif63ef492019-06-14 13:05:33 +05301452 return CMD_RET_FAILURE;
1453
Simon Glass3ff49ec2021-07-24 09:03:29 -06001454 addr = hextoul(argv[1], &endp);
Han Xu1f314e52020-05-05 22:04:03 +08001455 if (*argv[1] == 0 || *endp != 0)
1456 return CMD_RET_FAILURE;
Alice Guoeaab9052020-05-05 22:04:01 +08001457
Shyam Sainif63ef492019-06-14 13:05:33 +05301458 buf = map_physmem(addr, size, MAP_WRBACK);
1459 if (!buf) {
1460 puts("failed to map physical memory\n");
1461 return CMD_RET_FAILURE;
1462 }
1463
Han Xu1f314e52020-05-05 22:04:03 +08001464 ret = nandbcb_init(&cfg, buf);
Shyam Sainif63ef492019-06-14 13:05:33 +05301465
1466 return ret == 0 ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
1467}
1468
Simon Glassed38aef2020-05-10 11:40:03 -06001469static int do_nandbcb(struct cmd_tbl *cmdtp, int flag, int argc,
1470 char *const argv[])
Shyam Sainif63ef492019-06-14 13:05:33 +05301471{
1472 const char *cmd;
1473 int ret = 0;
1474
Alice Guoeaab9052020-05-05 22:04:01 +08001475 if (argc < 3)
Shyam Sainif63ef492019-06-14 13:05:33 +05301476 goto usage;
1477
Han Xu1f314e52020-05-05 22:04:03 +08001478 /* check the platform config first */
1479 if (is_mx6sx()) {
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;
Han Xuab6d50d2020-10-10 08:48:49 -05001491 } else if (is_imx8mn() || is_imx8mp()) {
Han Xu1f314e52020-05-05 22:04:03 +08001492 plat_config = imx8mn_plat_config;
1493 } else if (is_imx8qm() || is_imx8qxp()) {
1494 plat_config = imx8q_plat_config;
1495 } else {
1496 printf("ERROR: Unknown platform\n");
1497 return CMD_RET_FAILURE;
1498 }
1499
Han Xuab6d50d2020-10-10 08:48:49 -05001500 if ((plat_config.misc_flags) & BT_SEARCH_CNT_FROM_FUSE) {
1501 if (is_imx8qxp())
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);
Han Xu65781602020-05-05 22:04:04 +08001507 }
Han Xu1f314e52020-05-05 22:04:03 +08001508
Shyam Sainif63ef492019-06-14 13:05:33 +05301509 cmd = argv[1];
1510 --argc;
1511 ++argv;
1512
Han Xu1f314e52020-05-05 22:04:03 +08001513 if (strcmp(cmd, "init") == 0) {
1514 ret = do_nandbcb_init(argc, argv);
Shyam Sainif63ef492019-06-14 13:05:33 +05301515 goto done;
1516 }
1517
Alice Guoeaab9052020-05-05 22:04:01 +08001518 if (strcmp(cmd, "dump") == 0) {
1519 ret = do_nandbcb_dump(argc, argv);
1520 goto done;
1521 }
1522
Igor Opaniukf3caefe2019-11-03 16:49:46 +01001523 if (strcmp(cmd, "bcbonly") == 0) {
1524 ret = do_nandbcb_bcbonly(argc, argv);
1525 goto done;
1526 }
1527
Shyam Sainif63ef492019-06-14 13:05:33 +05301528done:
1529 if (ret != -1)
1530 return ret;
1531usage:
1532 return CMD_RET_USAGE;
1533}
1534
Parthiban Nallathambib3a40682019-08-23 18:35:10 +02001535#ifdef CONFIG_SYS_LONGHELP
Shyam Sainif63ef492019-06-14 13:05:33 +05301536static char nandbcb_help_text[] =
Han Xu1f314e52020-05-05 22:04:03 +08001537 "init addr off|partition len - update 'len' bytes starting at\n"
Igor Opaniukf3caefe2019-11-03 16:49:46 +01001538 " 'off|part' to memory address 'addr', skipping bad blocks\n"
Han Xu1f314e52020-05-05 22:04:03 +08001539 "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"
Igor Opaniukbe604312019-12-16 14:06:44 +02001542 " 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"
Alice Guoeaab9052020-05-05 22:04:01 +08001545 " > mtd erase mx7-bcb\n"
Han Xu1f314e52020-05-05 22:04:03 +08001546 "nandbcb dump off|partition - dump/verify boot structures\n";
Parthiban Nallathambib3a40682019-08-23 18:35:10 +02001547#endif
Shyam Sainif63ef492019-06-14 13:05:33 +05301548
Han Xu1f314e52020-05-05 22:04:03 +08001549U_BOOT_CMD(nandbcb, 7, 1, do_nandbcb,
1550 "i.MX NAND Boot Control Blocks write",
Shyam Sainif63ef492019-06-14 13:05:33 +05301551 nandbcb_help_text
1552);