Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 1 | /* |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 2 | * i.MX nand boot control block(bcb). |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 3 | * |
| 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 Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 9 | * Reconstucted by Han Xu <han.xu@nxp.com> |
| 10 | * |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 11 | * SPDX-License-Identifier: GPL-2.0+ |
| 12 | */ |
| 13 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 14 | #include <command.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 15 | #include <log.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 16 | #include <malloc.h> |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 17 | #include <nand.h> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 18 | #include <dm/devres.h> |
Simon Glass | c06c1be | 2020-05-10 11:40:08 -0600 | [diff] [blame] | 19 | #include <linux/bug.h> |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 20 | |
| 21 | #include <asm/io.h> |
| 22 | #include <jffs2/jffs2.h> |
Parthiban Nallathambi | a99188b | 2019-10-18 11:46:19 +0200 | [diff] [blame] | 23 | #include <linux/bch.h> |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 24 | #include <linux/mtd/mtd.h> |
Tom Rini | 3bde7e2 | 2021-09-22 14:50:35 -0400 | [diff] [blame] | 25 | #include <linux/mtd/rawnand.h> |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 26 | |
Igor Opaniuk | 344833a | 2019-11-03 16:49:44 +0100 | [diff] [blame] | 27 | #include <asm/arch/sys_proto.h> |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 28 | #include <asm/mach-imx/imx-nandbcb.h> |
| 29 | #include <asm/mach-imx/imximage.cfg> |
| 30 | #include <mxs_nand.h> |
| 31 | #include <linux/mtd/mtd.h> |
| 32 | #include <nand.h> |
Han Xu | 6578160 | 2020-05-05 22:04:04 +0800 | [diff] [blame] | 33 | #include <fuse.h> |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 34 | |
Miquel Raynal | 617fd79 | 2019-10-25 19:39:29 +0200 | [diff] [blame] | 35 | #include "../../../cmd/legacy-mtd-utils.h" |
| 36 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 37 | /* FCB related flags */ |
| 38 | /* FCB layout with leading 12B reserved */ |
| 39 | #define FCB_LAYOUT_RESV_12B BIT(0) |
| 40 | /* FCB layout with leading 32B meta data */ |
| 41 | #define FCB_LAYOUT_META_32B BIT(1) |
| 42 | /* FCB encrypted by Hamming code */ |
| 43 | #define FCB_ENCODE_HAMMING BIT(2) |
| 44 | /* FCB encrypted by 40bit BCH */ |
| 45 | #define FCB_ENCODE_BCH_40b BIT(3) |
| 46 | /* FCB encrypted by 62bit BCH */ |
| 47 | #define FCB_ENCODE_BCH_62b BIT(4) |
| 48 | /* FCB encrypted by BCH */ |
| 49 | #define FCB_ENCODE_BCH (FCB_ENCODE_BCH_40b | FCB_ENCODE_BCH_62b) |
| 50 | /* FCB data was randomized */ |
| 51 | #define FCB_RANDON_ENABLED BIT(5) |
| 52 | |
| 53 | /* Firmware related flags */ |
| 54 | /* No 1K padding */ |
| 55 | #define FIRMWARE_NEED_PADDING BIT(8) |
| 56 | /* Extra firmware*/ |
| 57 | #define FIRMWARE_EXTRA_ONE BIT(9) |
| 58 | /* Secondary firmware on fixed address */ |
| 59 | #define FIRMWARE_SECONDARY_FIXED_ADDR BIT(10) |
| 60 | |
| 61 | /* Boot search related flags */ |
| 62 | #define BT_SEARCH_CNT_FROM_FUSE BIT(16) |
| 63 | |
| 64 | struct platform_config { |
| 65 | int misc_flags; |
| 66 | }; |
| 67 | |
| 68 | static struct platform_config plat_config; |
| 69 | |
| 70 | /* imx6q/dl/solo */ |
| 71 | static struct platform_config imx6qdl_plat_config = { |
| 72 | .misc_flags = FCB_LAYOUT_RESV_12B | |
| 73 | FCB_ENCODE_HAMMING | |
| 74 | FIRMWARE_NEED_PADDING, |
| 75 | }; |
| 76 | |
| 77 | static struct platform_config imx6sx_plat_config = { |
| 78 | .misc_flags = FCB_LAYOUT_META_32B | |
| 79 | FCB_ENCODE_BCH_62b | |
| 80 | FIRMWARE_NEED_PADDING | |
| 81 | FCB_RANDON_ENABLED, |
| 82 | }; |
| 83 | |
| 84 | static struct platform_config imx7d_plat_config = { |
| 85 | .misc_flags = FCB_LAYOUT_META_32B | |
| 86 | FCB_ENCODE_BCH_62b | |
| 87 | FIRMWARE_NEED_PADDING | |
| 88 | FCB_RANDON_ENABLED, |
| 89 | }; |
| 90 | |
| 91 | /* imx6ul/ull/ulz */ |
| 92 | static struct platform_config imx6ul_plat_config = { |
| 93 | .misc_flags = FCB_LAYOUT_META_32B | |
| 94 | FCB_ENCODE_BCH_40b | |
| 95 | FIRMWARE_NEED_PADDING, |
| 96 | }; |
| 97 | |
| 98 | static struct platform_config imx8mq_plat_config = { |
| 99 | .misc_flags = FCB_LAYOUT_META_32B | |
| 100 | FCB_ENCODE_BCH_62b | |
| 101 | FIRMWARE_NEED_PADDING | |
| 102 | FCB_RANDON_ENABLED | |
| 103 | FIRMWARE_EXTRA_ONE, |
| 104 | }; |
| 105 | |
| 106 | /* all other imx8mm */ |
| 107 | static struct platform_config imx8mm_plat_config = { |
| 108 | .misc_flags = FCB_LAYOUT_META_32B | |
| 109 | FCB_ENCODE_BCH_62b | |
| 110 | FIRMWARE_NEED_PADDING | |
| 111 | FCB_RANDON_ENABLED, |
| 112 | }; |
| 113 | |
| 114 | /* imx8mn */ |
| 115 | static struct platform_config imx8mn_plat_config = { |
| 116 | .misc_flags = FCB_LAYOUT_META_32B | |
| 117 | FCB_ENCODE_BCH_62b | |
| 118 | FCB_RANDON_ENABLED | |
| 119 | FIRMWARE_SECONDARY_FIXED_ADDR | |
| 120 | BT_SEARCH_CNT_FROM_FUSE, |
| 121 | }; |
| 122 | |
| 123 | /* imx8qx/qm */ |
| 124 | static struct platform_config imx8q_plat_config = { |
| 125 | .misc_flags = FCB_LAYOUT_META_32B | |
| 126 | FCB_ENCODE_BCH_62b | |
| 127 | FCB_RANDON_ENABLED | |
| 128 | FIRMWARE_SECONDARY_FIXED_ADDR | |
| 129 | BT_SEARCH_CNT_FROM_FUSE, |
| 130 | }; |
| 131 | |
| 132 | /* boot search related variables and definitions */ |
| 133 | static int g_boot_search_count = 4; |
Michael Trimarchi | ed83611 | 2022-01-07 18:27:17 +0100 | [diff] [blame] | 134 | static int g_boot_secondary_offset; |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 135 | static int g_boot_search_stride; |
| 136 | static int g_pages_per_stride; |
| 137 | |
| 138 | /* mtd config structure */ |
| 139 | struct 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 */ |
| 157 | struct 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 Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 176 | #define GETBIT(v, n) (((v) >> (n)) & 0x1) |
Alice Guo | a3f815b | 2020-05-05 22:04:00 +0800 | [diff] [blame] | 177 | #define IMX8MQ_SPL_SZ 0x3e000 |
| 178 | #define IMX8MQ_HDMI_FW_SZ 0x19c00 |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 179 | |
| 180 | static 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 | |
| 204 | static 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 Guo | eaab905 | 2020-05-05 22:04:01 +0800 | [diff] [blame] | 221 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 222 | 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 | |
| 230 | static 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 | |
Michael Trimarchi | ed83611 | 2022-01-07 18:27:17 +0100 | [diff] [blame] | 278 | if (g_boot_secondary_offset) |
Ye Li | 50710d2 | 2020-08-02 23:07:55 -0700 | [diff] [blame] | 279 | boot_stream2_address = |
Michael Trimarchi | ed83611 | 2022-01-07 18:27:17 +0100 | [diff] [blame] | 280 | (loff_t)g_boot_secondary_offset * 1024 * 1024; |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 281 | |
| 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 | |
| 305 | static 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 Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 325 | |
Parthiban Nallathambi | a99188b | 2019-10-18 11:46:19 +0200 | [diff] [blame] | 326 | #if defined(CONFIG_MX6UL) || defined(CONFIG_MX6ULL) |
| 327 | static 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 | |
| 336 | static 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 Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 388 | static 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 | |
| 404 | static 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 Nallathambi | a99188b | 2019-10-18 11:46:19 +0200 | [diff] [blame] | 413 | #endif |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 414 | |
| 415 | static 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 Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 427 | static void fill_fcb(struct fcb_block *fcb, struct boot_config *boot_cfg) |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 428 | { |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 429 | struct mtd_info *mtd = boot_cfg->mtd; |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 430 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 431 | struct mxs_nand_info *nand_info = nand_get_controller_data(chip); |
Igor Opaniuk | 344833a | 2019-11-03 16:49:44 +0100 | [diff] [blame] | 432 | struct mxs_nand_layout l; |
| 433 | |
| 434 | mxs_nand_get_layout(mtd, &l); |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 435 | |
| 436 | fcb->fingerprint = FCB_FINGERPRINT; |
| 437 | fcb->version = FCB_VERSION_1; |
Igor Opaniuk | 344833a | 2019-11-03 16:49:44 +0100 | [diff] [blame] | 438 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 439 | fcb->datasetup = 80; |
| 440 | fcb->datahold = 60; |
| 441 | fcb->addr_setup = 25; |
| 442 | fcb->dsample_time = 6; |
| 443 | |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 444 | fcb->pagesize = mtd->writesize; |
| 445 | fcb->oob_pagesize = mtd->writesize + mtd->oobsize; |
| 446 | fcb->sectors = mtd->erasesize / mtd->writesize; |
| 447 | |
Igor Opaniuk | 344833a | 2019-11-03 16:49:44 +0100 | [diff] [blame] | 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; |
Han Xu | ae0bdd7 | 2020-05-05 22:03:59 +0800 | [diff] [blame] | 454 | fcb->bchtype = l.gf_len; |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 455 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 456 | /* DBBT search area starts from the next block after all FCB */ |
| 457 | fcb->dbbt_start = boot_cfg->search_area_size_in_pages; |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 458 | |
| 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 Opaniuk | 344833a | 2019-11-03 16:49:44 +0100 | [diff] [blame] | 464 | fcb->disbbm = 0; |
Igor Opaniuk | 344833a | 2019-11-03 16:49:44 +0100 | [diff] [blame] | 465 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 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); |
Igor Opaniuk | 14a489b | 2019-11-03 16:49:45 +0100 | [diff] [blame] | 470 | |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 471 | fcb->checksum = calc_chksum((void *)fcb + 4, sizeof(*fcb) - 4); |
| 472 | } |
| 473 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 474 | static int fill_dbbt_data(struct mtd_info *mtd, void *buf, int num_blocks) |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 475 | { |
| 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 Li | d701ed5 | 2020-08-02 22:59:43 -0700 | [diff] [blame] | 481 | loff_t offset = (loff_t)n * mtd->erasesize; |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 482 | 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 Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 494 | /* |
| 495 | * return 1 - bad block |
| 496 | * return 0 - read successfully |
| 497 | * return < 0 - read failed |
| 498 | */ |
| 499 | static int read_fcb(struct boot_config *boot_cfg, struct fcb_block *fcb, |
| 500 | loff_t off) |
Igor Opaniuk | 14a489b | 2019-11-03 16:49:45 +0100 | [diff] [blame] | 501 | { |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 502 | struct mtd_info *mtd; |
| 503 | void *fcb_raw_page; |
| 504 | size_t size; |
| 505 | int ret = 0; |
| 506 | |
| 507 | mtd = boot_cfg->mtd; |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 508 | |
Ye Li | c61dedb | 2020-08-02 22:43:45 -0700 | [diff] [blame] | 509 | fcb_raw_page = kzalloc(mtd->writesize + mtd->oobsize, GFP_KERNEL); |
| 510 | if (!fcb_raw_page) { |
| 511 | debug("failed to allocate fcb_raw_page\n"); |
| 512 | ret = -ENOMEM; |
| 513 | return ret; |
| 514 | } |
| 515 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 516 | /* |
| 517 | * User BCH hardware to decode ECC for FCB |
| 518 | */ |
| 519 | if (plat_config.misc_flags & FCB_ENCODE_BCH) { |
| 520 | size = sizeof(struct fcb_block); |
| 521 | |
| 522 | /* switch nand BCH to FCB compatible settings */ |
| 523 | if (plat_config.misc_flags & FCB_ENCODE_BCH_62b) |
| 524 | mxs_nand_mode_fcb_62bit(mtd); |
| 525 | else if (plat_config.misc_flags & FCB_ENCODE_BCH_40b) |
| 526 | mxs_nand_mode_fcb_40bit(mtd); |
| 527 | |
Michael Trimarchi | 97cd4d8 | 2022-05-15 11:35:32 +0200 | [diff] [blame] | 528 | ret = nand_read_skip_bad(mtd, off, &size, NULL, mtd->size, (u_char *)fcb); |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 529 | |
| 530 | /* switch BCH back */ |
| 531 | mxs_nand_mode_normal(mtd); |
| 532 | printf("NAND FCB read from 0x%llx offset 0x%zx read: %s\n", |
| 533 | off, size, ret ? "ERROR" : "OK"); |
| 534 | |
| 535 | } else if (plat_config.misc_flags & FCB_ENCODE_HAMMING) { |
| 536 | /* raw read*/ |
| 537 | mtd_oob_ops_t ops = { |
| 538 | .datbuf = (u8 *)fcb_raw_page, |
| 539 | .oobbuf = ((u8 *)fcb_raw_page) + mtd->writesize, |
| 540 | .len = mtd->writesize, |
| 541 | .ooblen = mtd->oobsize, |
| 542 | .mode = MTD_OPS_RAW |
| 543 | }; |
| 544 | |
| 545 | ret = mtd_read_oob(mtd, off, &ops); |
| 546 | printf("NAND FCB read from 0x%llx offset 0x%zx read: %s\n", |
| 547 | off, ops.len, ret ? "ERROR" : "OK"); |
| 548 | } |
| 549 | |
| 550 | if (ret) |
| 551 | goto fcb_raw_page_err; |
| 552 | |
| 553 | if ((plat_config.misc_flags & FCB_ENCODE_HAMMING) && |
| 554 | (plat_config.misc_flags & FCB_LAYOUT_RESV_12B)) |
| 555 | memcpy(fcb, fcb_raw_page + 12, sizeof(struct fcb_block)); |
| 556 | |
| 557 | /* TODO: check if it can pass Hamming check */ |
| 558 | |
| 559 | fcb_raw_page_err: |
| 560 | kfree(fcb_raw_page); |
| 561 | |
| 562 | return ret; |
| 563 | } |
| 564 | |
| 565 | static int write_fcb(struct boot_config *boot_cfg, struct fcb_block *fcb) |
| 566 | { |
| 567 | struct mtd_info *mtd; |
| 568 | void *fcb_raw_page = NULL; |
Ye Li | 0c55e33 | 2020-08-02 20:55:17 -0700 | [diff] [blame] | 569 | int i, ret = 0; |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 570 | loff_t off; |
| 571 | size_t size; |
| 572 | |
| 573 | mtd = boot_cfg->mtd; |
Igor Opaniuk | 14a489b | 2019-11-03 16:49:45 +0100 | [diff] [blame] | 574 | |
| 575 | /* |
| 576 | * We prepare raw page only for i.MX6, for i.MX7 we |
| 577 | * leverage BCH hw module instead |
| 578 | */ |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 579 | if ((plat_config.misc_flags & FCB_ENCODE_HAMMING) && |
| 580 | (plat_config.misc_flags & FCB_LAYOUT_RESV_12B)) { |
Igor Opaniuk | 14a489b | 2019-11-03 16:49:45 +0100 | [diff] [blame] | 581 | fcb_raw_page = kzalloc(mtd->writesize + mtd->oobsize, |
| 582 | GFP_KERNEL); |
| 583 | if (!fcb_raw_page) { |
| 584 | debug("failed to allocate fcb_raw_page\n"); |
| 585 | ret = -ENOMEM; |
| 586 | return ret; |
| 587 | } |
| 588 | |
| 589 | #if defined(CONFIG_MX6UL) || defined(CONFIG_MX6ULL) |
| 590 | /* 40 bit BCH, for i.MX6UL(L) */ |
| 591 | encode_bch_ecc(fcb_raw_page + 32, fcb, 40); |
| 592 | #else |
| 593 | memcpy(fcb_raw_page + 12, fcb, sizeof(struct fcb_block)); |
| 594 | encode_hamming_13_8(fcb_raw_page + 12, |
| 595 | fcb_raw_page + 12 + 512, 512); |
| 596 | #endif |
| 597 | /* |
| 598 | * Set the first and second byte of OOB data to 0xFF, |
| 599 | * not 0x00. These bytes are used as the Manufacturers Bad |
| 600 | * Block Marker (MBBM). Since the FCB is mostly written to |
| 601 | * the first page in a block, a scan for |
| 602 | * factory bad blocks will detect these blocks as bad, e.g. |
| 603 | * when function nand_scan_bbt() is executed to build a new |
| 604 | * bad block table. |
| 605 | */ |
| 606 | memset(fcb_raw_page + mtd->writesize, 0xFF, 2); |
| 607 | } |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 608 | |
| 609 | /* start writing FCB from the very beginning */ |
| 610 | off = 0; |
| 611 | |
| 612 | for (i = 0; i < g_boot_search_count; i++) { |
Igor Opaniuk | 14a489b | 2019-11-03 16:49:45 +0100 | [diff] [blame] | 613 | if (mtd_block_isbad(mtd, off)) { |
| 614 | printf("Block %d is bad, skipped\n", i); |
Michael Trimarchi | 97cd4d8 | 2022-05-15 11:35:32 +0200 | [diff] [blame] | 615 | off += mtd->erasesize; |
Igor Opaniuk | 14a489b | 2019-11-03 16:49:45 +0100 | [diff] [blame] | 616 | continue; |
| 617 | } |
| 618 | |
| 619 | /* |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 620 | * User BCH hardware module to generate ECC for FCB |
Igor Opaniuk | 14a489b | 2019-11-03 16:49:45 +0100 | [diff] [blame] | 621 | */ |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 622 | if (plat_config.misc_flags & FCB_ENCODE_BCH) { |
| 623 | size = sizeof(struct fcb_block); |
Igor Opaniuk | 14a489b | 2019-11-03 16:49:45 +0100 | [diff] [blame] | 624 | |
| 625 | /* switch nand BCH to FCB compatible settings */ |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 626 | if (plat_config.misc_flags & FCB_ENCODE_BCH_62b) |
| 627 | mxs_nand_mode_fcb_62bit(mtd); |
| 628 | else if (plat_config.misc_flags & FCB_ENCODE_BCH_40b) |
| 629 | mxs_nand_mode_fcb_40bit(mtd); |
Alice Guo | eaab905 | 2020-05-05 22:04:01 +0800 | [diff] [blame] | 630 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 631 | ret = nand_write(mtd, off, &size, (u_char *)fcb); |
Alice Guo | eaab905 | 2020-05-05 22:04:01 +0800 | [diff] [blame] | 632 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 633 | /* switch BCH back */ |
Igor Opaniuk | 14a489b | 2019-11-03 16:49:45 +0100 | [diff] [blame] | 634 | mxs_nand_mode_normal(mtd); |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 635 | printf("NAND FCB write to 0x%zx offset 0x%llx written: %s\n", |
| 636 | size, off, ret ? "ERROR" : "OK"); |
Igor Opaniuk | 14a489b | 2019-11-03 16:49:45 +0100 | [diff] [blame] | 637 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 638 | } else if (plat_config.misc_flags & FCB_ENCODE_HAMMING) { |
Igor Opaniuk | 14a489b | 2019-11-03 16:49:45 +0100 | [diff] [blame] | 639 | /* raw write */ |
| 640 | mtd_oob_ops_t ops = { |
| 641 | .datbuf = (u8 *)fcb_raw_page, |
| 642 | .oobbuf = ((u8 *)fcb_raw_page) + |
| 643 | mtd->writesize, |
| 644 | .len = mtd->writesize, |
| 645 | .ooblen = mtd->oobsize, |
| 646 | .mode = MTD_OPS_RAW |
| 647 | }; |
| 648 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 649 | ret = mtd_write_oob(mtd, off, &ops); |
Pali Rohár | dc5c6bf | 2021-10-20 11:13:15 +0200 | [diff] [blame] | 650 | printf("NAND FCB write to 0x%llx offset 0x%zx written: %s\n", off, ops.len, ret ? "ERROR" : "OK"); |
Igor Opaniuk | 14a489b | 2019-11-03 16:49:45 +0100 | [diff] [blame] | 651 | } |
| 652 | |
Igor Opaniuk | 14a489b | 2019-11-03 16:49:45 +0100 | [diff] [blame] | 653 | if (ret) |
| 654 | goto fcb_raw_page_err; |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 655 | |
| 656 | /* next writing location */ |
| 657 | off += g_boot_search_stride; |
| 658 | } |
| 659 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 660 | fcb_raw_page_err: |
| 661 | kfree(fcb_raw_page); |
| 662 | |
| 663 | return ret; |
| 664 | } |
| 665 | |
| 666 | /* |
| 667 | * return 1 - bad block |
| 668 | * return 0 - read successfully |
| 669 | * return < 0 - read failed |
| 670 | */ |
| 671 | static int read_dbbt(struct boot_config *boot_cfg, struct dbbt_block *dbbt, |
| 672 | void *dbbt_data_page, loff_t off) |
| 673 | { |
| 674 | size_t size; |
Michael Trimarchi | 97cd4d8 | 2022-05-15 11:35:32 +0200 | [diff] [blame] | 675 | size_t actual_size; |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 676 | struct mtd_info *mtd; |
| 677 | loff_t to; |
| 678 | int ret; |
Igor Opaniuk | 14a489b | 2019-11-03 16:49:45 +0100 | [diff] [blame] | 679 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 680 | mtd = boot_cfg->mtd; |
Alice Guo | eaab905 | 2020-05-05 22:04:01 +0800 | [diff] [blame] | 681 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 682 | size = sizeof(struct dbbt_block); |
Michael Trimarchi | 97cd4d8 | 2022-05-15 11:35:32 +0200 | [diff] [blame] | 683 | ret = nand_read_skip_bad(mtd, off, &size, &actual_size, mtd->size, (u_char *)dbbt); |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 684 | printf("NAND DBBT read from 0x%llx offset 0x%zx read: %s\n", |
| 685 | off, size, ret ? "ERROR" : "OK"); |
| 686 | if (ret) |
| 687 | return ret; |
| 688 | |
| 689 | /* dbbtpages == 0 if no bad blocks */ |
| 690 | if (dbbt->dbbtpages > 0) { |
Michael Trimarchi | 97cd4d8 | 2022-05-15 11:35:32 +0200 | [diff] [blame] | 691 | to = off + 4 * mtd->writesize + actual_size - size; |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 692 | size = mtd->writesize; |
Michael Trimarchi | 97cd4d8 | 2022-05-15 11:35:32 +0200 | [diff] [blame] | 693 | ret = nand_read_skip_bad(mtd, to, &size, NULL, mtd->size, dbbt_data_page); |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 694 | printf("DBBT data read from 0x%llx offset 0x%zx read: %s\n", |
| 695 | to, size, ret ? "ERROR" : "OK"); |
| 696 | |
| 697 | if (ret) |
| 698 | return ret; |
| 699 | } |
| 700 | |
| 701 | return 0; |
| 702 | } |
| 703 | |
| 704 | static int write_dbbt(struct boot_config *boot_cfg, struct dbbt_block *dbbt, |
| 705 | void *dbbt_data_page) |
| 706 | { |
| 707 | int i; |
| 708 | loff_t off, to; |
| 709 | size_t size; |
| 710 | struct mtd_info *mtd; |
| 711 | int ret; |
| 712 | |
| 713 | mtd = boot_cfg->mtd; |
| 714 | |
| 715 | /* start writing DBBT after all FCBs */ |
| 716 | off = boot_cfg->search_area_size_in_bytes; |
| 717 | size = mtd->writesize; |
| 718 | |
| 719 | for (i = 0; i < g_boot_search_count; i++) { |
| 720 | if (mtd_block_isbad(mtd, off)) { |
| 721 | printf("Block %d is bad, skipped\n", |
| 722 | (int)(i + CONV_TO_BLOCKS(off))); |
Michael Trimarchi | 97cd4d8 | 2022-05-15 11:35:32 +0200 | [diff] [blame] | 723 | off += mtd->erasesize; |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 724 | continue; |
| 725 | } |
| 726 | |
| 727 | ret = nand_write(mtd, off, &size, (u_char *)dbbt); |
| 728 | printf("NAND DBBT write to 0x%llx offset 0x%zx written: %s\n", |
| 729 | off, size, ret ? "ERROR" : "OK"); |
| 730 | if (ret) |
| 731 | return ret; |
Alice Guo | eaab905 | 2020-05-05 22:04:01 +0800 | [diff] [blame] | 732 | |
Igor Opaniuk | 14a489b | 2019-11-03 16:49:45 +0100 | [diff] [blame] | 733 | /* dbbtpages == 0 if no bad blocks */ |
| 734 | if (dbbt->dbbtpages > 0) { |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 735 | to = off + 4 * mtd->writesize; |
| 736 | ret = nand_write(mtd, to, &size, dbbt_data_page); |
| 737 | printf("DBBT data write to 0x%llx offset 0x%zx written: %s\n", |
| 738 | to, size, ret ? "ERROR" : "OK"); |
Igor Opaniuk | 14a489b | 2019-11-03 16:49:45 +0100 | [diff] [blame] | 739 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 740 | if (ret) |
| 741 | return ret; |
Igor Opaniuk | 14a489b | 2019-11-03 16:49:45 +0100 | [diff] [blame] | 742 | } |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 743 | |
| 744 | /* next writing location */ |
| 745 | off += g_boot_search_stride; |
Igor Opaniuk | 14a489b | 2019-11-03 16:49:45 +0100 | [diff] [blame] | 746 | } |
| 747 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 748 | return 0; |
| 749 | } |
| 750 | |
| 751 | /* reuse the check_skip_len from nand_util.c with minor change*/ |
| 752 | static int check_skip_length(struct boot_config *boot_cfg, loff_t offset, |
| 753 | size_t length, size_t *used) |
| 754 | { |
| 755 | struct mtd_info *mtd = boot_cfg->mtd; |
| 756 | size_t maxsize = boot_cfg->maxsize; |
| 757 | size_t len_excl_bad = 0; |
| 758 | int ret = 0; |
| 759 | |
| 760 | while (len_excl_bad < length) { |
| 761 | size_t block_len, block_off; |
| 762 | loff_t block_start; |
| 763 | |
| 764 | if (offset >= maxsize) |
| 765 | return -1; |
| 766 | |
| 767 | block_start = offset & ~(loff_t)(mtd->erasesize - 1); |
| 768 | block_off = offset & (mtd->erasesize - 1); |
| 769 | block_len = mtd->erasesize - block_off; |
| 770 | |
| 771 | if (!nand_block_isbad(mtd, block_start)) |
| 772 | len_excl_bad += block_len; |
| 773 | else |
| 774 | ret = 1; |
| 775 | |
| 776 | offset += block_len; |
| 777 | *used += block_len; |
| 778 | } |
| 779 | |
| 780 | /* If the length is not a multiple of block_len, adjust. */ |
| 781 | if (len_excl_bad > length) |
| 782 | *used -= (len_excl_bad - length); |
| 783 | |
| 784 | return ret; |
| 785 | } |
| 786 | |
| 787 | static int nandbcb_get_next_good_blk_addr(struct boot_config *boot_cfg, |
| 788 | struct boot_stream_config *bs_cfg) |
| 789 | { |
| 790 | struct mtd_info *mtd = boot_cfg->mtd; |
| 791 | loff_t offset = bs_cfg->bs_addr; |
| 792 | size_t length = bs_cfg->bs_size; |
| 793 | size_t used = 0; |
| 794 | int ret; |
| 795 | |
| 796 | ret = check_skip_length(boot_cfg, offset, length, &used); |
| 797 | |
| 798 | if (ret < 0) |
| 799 | return ret; |
| 800 | |
| 801 | /* get next image address */ |
| 802 | bs_cfg->next_bs_addr = (u32)(offset + used + mtd->erasesize - 1) |
| 803 | / (u32)mtd->erasesize * mtd->erasesize; |
| 804 | |
| 805 | return ret; |
| 806 | } |
| 807 | |
| 808 | static int nandbcb_write_bs_skip_bad(struct boot_config *boot_cfg, |
| 809 | struct boot_stream_config *bs_cfg) |
| 810 | { |
| 811 | struct mtd_info *mtd; |
| 812 | void *buf; |
| 813 | loff_t offset, maxsize; |
| 814 | size_t size; |
| 815 | size_t length; |
| 816 | int ret; |
| 817 | bool padding_flag = false; |
| 818 | |
| 819 | mtd = boot_cfg->mtd; |
| 820 | offset = bs_cfg->bs_addr; |
| 821 | maxsize = boot_cfg->maxsize; |
| 822 | size = bs_cfg->bs_size; |
| 823 | |
| 824 | /* some boot images may need leading offset */ |
| 825 | if (bs_cfg->need_padding && |
| 826 | (plat_config.misc_flags & FIRMWARE_NEED_PADDING)) |
| 827 | padding_flag = 1; |
| 828 | |
| 829 | if (padding_flag) |
| 830 | length = ALIGN(size + FLASH_OFFSET_STANDARD, mtd->writesize); |
| 831 | else |
| 832 | length = ALIGN(size, mtd->writesize); |
| 833 | |
| 834 | buf = kzalloc(length, GFP_KERNEL); |
| 835 | if (!buf) { |
| 836 | printf("failed to allocate buffer for firmware\n"); |
| 837 | ret = -ENOMEM; |
| 838 | return ret; |
| 839 | } |
| 840 | |
| 841 | if (padding_flag) |
| 842 | memcpy(buf + FLASH_OFFSET_STANDARD, bs_cfg->bs_buf, size); |
| 843 | else |
| 844 | memcpy(buf, bs_cfg->bs_buf, size); |
Igor Opaniuk | 14a489b | 2019-11-03 16:49:45 +0100 | [diff] [blame] | 845 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 846 | ret = nand_write_skip_bad(mtd, offset, &length, NULL, maxsize, |
| 847 | (u_char *)buf, WITH_WR_VERIFY); |
| 848 | printf("Write %s @0x%llx offset, 0x%zx bytes written: %s\n", |
| 849 | bs_cfg->bs_label, offset, length, ret ? "ERROR" : "OK"); |
| 850 | |
| 851 | if (ret) |
| 852 | /* write image failed, quit */ |
| 853 | goto err; |
| 854 | |
| 855 | /* get next good blk address if needed */ |
| 856 | if (bs_cfg->need_padding) { |
| 857 | ret = nandbcb_get_next_good_blk_addr(boot_cfg, bs_cfg); |
| 858 | if (ret < 0) { |
| 859 | printf("Next image cannot fit in NAND partition\n"); |
| 860 | goto err; |
| 861 | } |
| 862 | } |
| 863 | |
| 864 | /* now we know how the exact image size written to NAND */ |
| 865 | bs_cfg->bs_size = length; |
| 866 | return 0; |
| 867 | err: |
| 868 | kfree(buf); |
Igor Opaniuk | 14a489b | 2019-11-03 16:49:45 +0100 | [diff] [blame] | 869 | return ret; |
| 870 | } |
| 871 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 872 | static int nandbcb_write_fw(struct boot_config *boot_cfg, u_char *buf, |
| 873 | int index) |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 874 | { |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 875 | int i; |
| 876 | loff_t offset; |
| 877 | size_t size; |
| 878 | loff_t next_bs_addr; |
| 879 | struct boot_stream_config bs_cfg; |
| 880 | int ret; |
| 881 | |
| 882 | for (i = 0; i < 2; ++i) { |
| 883 | if (!(FW_INX(i) & index)) |
| 884 | continue; |
| 885 | |
| 886 | if (i == 0) { |
| 887 | offset = boot_cfg->boot_stream1_address; |
| 888 | size = boot_cfg->boot_stream1_size; |
| 889 | } else { |
| 890 | offset = boot_cfg->boot_stream2_address; |
| 891 | size = boot_cfg->boot_stream2_size; |
| 892 | } |
| 893 | |
| 894 | /* write Firmware*/ |
| 895 | if (!(plat_config.misc_flags & FIRMWARE_EXTRA_ONE)) { |
| 896 | memset(&bs_cfg, 0, sizeof(struct boot_stream_config)); |
| 897 | sprintf(bs_cfg.bs_label, "firmware%d", i); |
| 898 | bs_cfg.bs_addr = offset; |
| 899 | bs_cfg.bs_size = size; |
| 900 | bs_cfg.bs_buf = buf; |
| 901 | bs_cfg.need_padding = 1; |
| 902 | |
| 903 | ret = nandbcb_write_bs_skip_bad(boot_cfg, &bs_cfg); |
| 904 | if (ret) |
| 905 | return ret; |
| 906 | |
| 907 | /* update the boot stream size */ |
| 908 | if (i == 0) |
| 909 | boot_cfg->boot_stream1_size = bs_cfg.bs_size; |
| 910 | else |
| 911 | boot_cfg->boot_stream2_size = bs_cfg.bs_size; |
| 912 | |
| 913 | } else { |
| 914 | /* some platforms need extra firmware */ |
| 915 | memset(&bs_cfg, 0, sizeof(struct boot_stream_config)); |
| 916 | sprintf(bs_cfg.bs_label, "fw%d_part%d", i, 1); |
| 917 | bs_cfg.bs_addr = offset; |
| 918 | bs_cfg.bs_size = IMX8MQ_HDMI_FW_SZ; |
| 919 | bs_cfg.bs_buf = buf; |
| 920 | bs_cfg.need_padding = 1; |
| 921 | |
| 922 | ret = nandbcb_write_bs_skip_bad(boot_cfg, &bs_cfg); |
| 923 | if (ret) |
| 924 | return ret; |
| 925 | |
| 926 | /* update the boot stream size */ |
| 927 | if (i == 0) |
| 928 | boot_cfg->boot_stream1_size = bs_cfg.bs_size; |
| 929 | else |
| 930 | boot_cfg->boot_stream2_size = bs_cfg.bs_size; |
| 931 | |
| 932 | /* get next image address */ |
| 933 | next_bs_addr = bs_cfg.next_bs_addr; |
| 934 | |
| 935 | memset(&bs_cfg, 0, sizeof(struct boot_stream_config)); |
| 936 | sprintf(bs_cfg.bs_label, "fw%d_part%d", i, 2); |
| 937 | bs_cfg.bs_addr = next_bs_addr; |
| 938 | bs_cfg.bs_size = IMX8MQ_SPL_SZ; |
| 939 | bs_cfg.bs_buf = (u_char *)(buf + IMX8MQ_HDMI_FW_SZ); |
| 940 | bs_cfg.need_padding = 0; |
| 941 | |
| 942 | ret = nandbcb_write_bs_skip_bad(boot_cfg, &bs_cfg); |
| 943 | if (ret) |
| 944 | return ret; |
| 945 | } |
| 946 | } |
| 947 | |
| 948 | return 0; |
| 949 | } |
| 950 | |
| 951 | static int nandbcb_init(struct boot_config *boot_cfg, u_char *buf) |
| 952 | { |
| 953 | struct mtd_info *mtd; |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 954 | nand_erase_options_t opts; |
| 955 | struct fcb_block *fcb; |
| 956 | struct dbbt_block *dbbt; |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 957 | void *dbbt_page, *dbbt_data_page; |
Igor Opaniuk | 14a489b | 2019-11-03 16:49:45 +0100 | [diff] [blame] | 958 | int ret; |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 959 | loff_t maxsize, off; |
| 960 | |
| 961 | mtd = boot_cfg->mtd; |
| 962 | maxsize = boot_cfg->maxsize; |
| 963 | off = boot_cfg->offset; |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 964 | |
| 965 | /* erase */ |
| 966 | memset(&opts, 0, sizeof(opts)); |
| 967 | opts.offset = off; |
| 968 | opts.length = maxsize - 1; |
| 969 | ret = nand_erase_opts(mtd, &opts); |
| 970 | if (ret) { |
| 971 | printf("%s: erase failed (ret = %d)\n", __func__, ret); |
| 972 | return ret; |
| 973 | } |
| 974 | |
| 975 | /* |
| 976 | * Reference documentation from i.MX6DQRM section 8.5.2.2 |
| 977 | * |
| 978 | * Nand Boot Control Block(BCB) contains two data structures, |
| 979 | * - Firmware Configuration Block(FCB) |
| 980 | * - Discovered Bad Block Table(DBBT) |
| 981 | * |
| 982 | * FCB contains, |
| 983 | * - nand timings |
| 984 | * - DBBT search page address, |
| 985 | * - start page address of primary firmware |
| 986 | * - start page address of secondary firmware |
| 987 | * |
| 988 | * setup fcb: |
| 989 | * - number of blocks = mtd partition size / mtd erasesize |
| 990 | * - two firmware blocks, primary and secondary |
| 991 | * - first 4 block for FCB/DBBT |
| 992 | * - rest split in half for primary and secondary firmware |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 993 | * - same firmware write twice |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 994 | */ |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 995 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 996 | /* write Firmware*/ |
| 997 | ret = nandbcb_write_fw(boot_cfg, buf, FW_ALL); |
| 998 | if (ret) |
| 999 | goto err; |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 1000 | |
| 1001 | /* fill fcb */ |
| 1002 | fcb = kzalloc(sizeof(*fcb), GFP_KERNEL); |
| 1003 | if (!fcb) { |
| 1004 | debug("failed to allocate fcb\n"); |
| 1005 | ret = -ENOMEM; |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 1006 | return ret; |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 1007 | } |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 1008 | fill_fcb(fcb, boot_cfg); |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 1009 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 1010 | ret = write_fcb(boot_cfg, fcb); |
Alice Guo | eaab905 | 2020-05-05 22:04:01 +0800 | [diff] [blame] | 1011 | |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 1012 | /* fill dbbt */ |
| 1013 | dbbt_page = kzalloc(mtd->writesize, GFP_KERNEL); |
| 1014 | if (!dbbt_page) { |
| 1015 | debug("failed to allocate dbbt_page\n"); |
| 1016 | ret = -ENOMEM; |
| 1017 | goto fcb_err; |
| 1018 | } |
| 1019 | |
| 1020 | dbbt_data_page = kzalloc(mtd->writesize, GFP_KERNEL); |
| 1021 | if (!dbbt_data_page) { |
| 1022 | debug("failed to allocate dbbt_data_page\n"); |
| 1023 | ret = -ENOMEM; |
| 1024 | goto dbbt_page_err; |
| 1025 | } |
| 1026 | |
| 1027 | dbbt = dbbt_page; |
| 1028 | dbbt->checksum = 0; |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 1029 | dbbt->fingerprint = DBBT_FINGERPRINT; |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 1030 | dbbt->version = DBBT_VERSION_1; |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 1031 | ret = fill_dbbt_data(mtd, dbbt_data_page, CONV_TO_BLOCKS(maxsize)); |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 1032 | if (ret < 0) |
| 1033 | goto dbbt_data_page_err; |
| 1034 | else if (ret > 0) |
| 1035 | dbbt->dbbtpages = 1; |
| 1036 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 1037 | /* write dbbt */ |
| 1038 | ret = write_dbbt(boot_cfg, dbbt, dbbt_data_page); |
Igor Opaniuk | 14a489b | 2019-11-03 16:49:45 +0100 | [diff] [blame] | 1039 | if (ret < 0) |
| 1040 | printf("failed to write FCB/DBBT\n"); |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 1041 | |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 1042 | dbbt_data_page_err: |
| 1043 | kfree(dbbt_data_page); |
| 1044 | dbbt_page_err: |
| 1045 | kfree(dbbt_page); |
| 1046 | fcb_err: |
| 1047 | kfree(fcb); |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 1048 | err: |
| 1049 | return ret; |
| 1050 | } |
| 1051 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 1052 | static int do_nandbcb_bcbonly(int argc, char *const argv[]) |
Igor Opaniuk | f3caefe | 2019-11-03 16:49:46 +0100 | [diff] [blame] | 1053 | { |
| 1054 | struct fcb_block *fcb; |
| 1055 | struct dbbt_block *dbbt; |
Igor Opaniuk | f3caefe | 2019-11-03 16:49:46 +0100 | [diff] [blame] | 1056 | struct mtd_info *mtd; |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 1057 | nand_erase_options_t opts; |
| 1058 | size_t maxsize; |
| 1059 | loff_t off; |
Igor Opaniuk | f3caefe | 2019-11-03 16:49:46 +0100 | [diff] [blame] | 1060 | void *dbbt_page, *dbbt_data_page; |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 1061 | int ret; |
| 1062 | struct boot_config cfg; |
Igor Opaniuk | f3caefe | 2019-11-03 16:49:46 +0100 | [diff] [blame] | 1063 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 1064 | if (argc < 4) |
| 1065 | return CMD_RET_USAGE; |
| 1066 | |
| 1067 | memset(&cfg, 0, sizeof(struct boot_config)); |
| 1068 | if (nandbcb_get_info(argc, argv, &cfg)) |
Igor Opaniuk | f3caefe | 2019-11-03 16:49:46 +0100 | [diff] [blame] | 1069 | return CMD_RET_FAILURE; |
Igor Opaniuk | f3caefe | 2019-11-03 16:49:46 +0100 | [diff] [blame] | 1070 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 1071 | /* only get the partition info */ |
| 1072 | if (nandbcb_get_size(2, argv, 1, &cfg)) |
| 1073 | return CMD_RET_FAILURE; |
Igor Opaniuk | f3caefe | 2019-11-03 16:49:46 +0100 | [diff] [blame] | 1074 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 1075 | if (nandbcb_set_boot_config(argc, argv, &cfg)) |
Igor Opaniuk | f3caefe | 2019-11-03 16:49:46 +0100 | [diff] [blame] | 1076 | return CMD_RET_FAILURE; |
| 1077 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 1078 | mtd = cfg.mtd; |
Igor Opaniuk | f3caefe | 2019-11-03 16:49:46 +0100 | [diff] [blame] | 1079 | |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 1080 | cfg.boot_stream1_address = hextoul(argv[2], NULL); |
| 1081 | cfg.boot_stream1_size = hextoul(argv[3], NULL); |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 1082 | cfg.boot_stream1_size = ALIGN(cfg.boot_stream1_size, mtd->writesize); |
| 1083 | |
| 1084 | if (argc > 5) { |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 1085 | cfg.boot_stream2_address = hextoul(argv[4], NULL); |
| 1086 | cfg.boot_stream2_size = hextoul(argv[5], NULL); |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 1087 | cfg.boot_stream2_size = ALIGN(cfg.boot_stream2_size, |
| 1088 | mtd->writesize); |
| 1089 | } |
| 1090 | |
| 1091 | /* sanity check */ |
| 1092 | nandbcb_check_space(&cfg); |
| 1093 | |
| 1094 | maxsize = cfg.maxsize; |
| 1095 | off = cfg.offset; |
| 1096 | |
| 1097 | /* erase the previous FCB/DBBT */ |
| 1098 | memset(&opts, 0, sizeof(opts)); |
| 1099 | opts.offset = off; |
| 1100 | opts.length = g_boot_search_stride * 2; |
| 1101 | ret = nand_erase_opts(mtd, &opts); |
| 1102 | if (ret) { |
| 1103 | printf("%s: erase failed (ret = %d)\n", __func__, ret); |
| 1104 | return CMD_RET_FAILURE; |
| 1105 | } |
Igor Opaniuk | f3caefe | 2019-11-03 16:49:46 +0100 | [diff] [blame] | 1106 | |
| 1107 | /* fill fcb */ |
| 1108 | fcb = kzalloc(sizeof(*fcb), GFP_KERNEL); |
| 1109 | if (!fcb) { |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 1110 | printf("failed to allocate fcb\n"); |
Igor Opaniuk | f3caefe | 2019-11-03 16:49:46 +0100 | [diff] [blame] | 1111 | ret = -ENOMEM; |
| 1112 | return CMD_RET_FAILURE; |
| 1113 | } |
| 1114 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 1115 | fill_fcb(fcb, &cfg); |
| 1116 | |
| 1117 | /* write fcb */ |
| 1118 | ret = write_fcb(&cfg, fcb); |
Igor Opaniuk | f3caefe | 2019-11-03 16:49:46 +0100 | [diff] [blame] | 1119 | |
| 1120 | /* fill dbbt */ |
| 1121 | dbbt_page = kzalloc(mtd->writesize, GFP_KERNEL); |
| 1122 | if (!dbbt_page) { |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 1123 | printf("failed to allocate dbbt_page\n"); |
Igor Opaniuk | f3caefe | 2019-11-03 16:49:46 +0100 | [diff] [blame] | 1124 | ret = -ENOMEM; |
| 1125 | goto fcb_err; |
| 1126 | } |
| 1127 | |
| 1128 | dbbt_data_page = kzalloc(mtd->writesize, GFP_KERNEL); |
| 1129 | if (!dbbt_data_page) { |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 1130 | printf("failed to allocate dbbt_data_page\n"); |
Igor Opaniuk | f3caefe | 2019-11-03 16:49:46 +0100 | [diff] [blame] | 1131 | ret = -ENOMEM; |
| 1132 | goto dbbt_page_err; |
| 1133 | } |
| 1134 | |
| 1135 | dbbt = dbbt_page; |
| 1136 | dbbt->checksum = 0; |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 1137 | dbbt->fingerprint = DBBT_FINGERPRINT; |
Igor Opaniuk | f3caefe | 2019-11-03 16:49:46 +0100 | [diff] [blame] | 1138 | dbbt->version = DBBT_VERSION_1; |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 1139 | ret = fill_dbbt_data(mtd, dbbt_data_page, CONV_TO_BLOCKS(maxsize)); |
Igor Opaniuk | f3caefe | 2019-11-03 16:49:46 +0100 | [diff] [blame] | 1140 | if (ret < 0) |
| 1141 | goto dbbt_data_page_err; |
| 1142 | else if (ret > 0) |
| 1143 | dbbt->dbbtpages = 1; |
| 1144 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 1145 | /* write dbbt */ |
| 1146 | ret = write_dbbt(&cfg, dbbt, dbbt_data_page); |
| 1147 | |
Igor Opaniuk | f3caefe | 2019-11-03 16:49:46 +0100 | [diff] [blame] | 1148 | dbbt_data_page_err: |
| 1149 | kfree(dbbt_data_page); |
| 1150 | dbbt_page_err: |
| 1151 | kfree(dbbt_page); |
| 1152 | fcb_err: |
| 1153 | kfree(fcb); |
| 1154 | |
| 1155 | if (ret < 0) { |
| 1156 | printf("failed to write FCB/DBBT\n"); |
| 1157 | return CMD_RET_FAILURE; |
| 1158 | } |
| 1159 | |
| 1160 | return CMD_RET_SUCCESS; |
| 1161 | } |
| 1162 | |
Alice Guo | eaab905 | 2020-05-05 22:04:01 +0800 | [diff] [blame] | 1163 | /* dump data which is read from NAND chip */ |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 1164 | void dump_structure(struct boot_config *boot_cfg, struct fcb_block *fcb, |
| 1165 | struct dbbt_block *dbbt, void *dbbt_data_page) |
Alice Guo | eaab905 | 2020-05-05 22:04:01 +0800 | [diff] [blame] | 1166 | { |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 1167 | int i; |
| 1168 | struct mtd_info *mtd = boot_cfg->mtd; |
| 1169 | |
| 1170 | #define P1(x) printf(" %s = 0x%08x\n", #x, fcb->x) |
| 1171 | printf("FCB\n"); |
Alice Guo | eaab905 | 2020-05-05 22:04:01 +0800 | [diff] [blame] | 1172 | P1(checksum); |
| 1173 | P1(fingerprint); |
| 1174 | P1(version); |
| 1175 | #undef P1 |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 1176 | #define P1(x) printf(" %s = %d\n", #x, fcb->x) |
Alice Guo | eaab905 | 2020-05-05 22:04:01 +0800 | [diff] [blame] | 1177 | P1(datasetup); |
| 1178 | P1(datahold); |
| 1179 | P1(addr_setup); |
| 1180 | P1(dsample_time); |
| 1181 | P1(pagesize); |
| 1182 | P1(oob_pagesize); |
| 1183 | P1(sectors); |
| 1184 | P1(nr_nand); |
| 1185 | P1(nr_die); |
| 1186 | P1(celltype); |
| 1187 | P1(ecc_type); |
| 1188 | P1(ecc_nr); |
| 1189 | P1(ecc_size); |
| 1190 | P1(ecc_level); |
| 1191 | P1(meta_size); |
| 1192 | P1(nr_blocks); |
| 1193 | P1(ecc_type_sdk); |
| 1194 | P1(ecc_nr_sdk); |
| 1195 | P1(ecc_size_sdk); |
| 1196 | P1(ecc_level_sdk); |
| 1197 | P1(nr_blocks_sdk); |
| 1198 | P1(meta_size_sdk); |
| 1199 | P1(erase_th); |
| 1200 | P1(bootpatch); |
| 1201 | P1(patch_size); |
| 1202 | P1(fw1_start); |
| 1203 | P1(fw2_start); |
| 1204 | P1(fw1_pages); |
| 1205 | P1(fw2_pages); |
| 1206 | P1(dbbt_start); |
| 1207 | P1(bb_byte); |
| 1208 | P1(bb_start_bit); |
| 1209 | P1(phy_offset); |
| 1210 | P1(bchtype); |
| 1211 | P1(readlatency); |
| 1212 | P1(predelay); |
| 1213 | P1(cedelay); |
| 1214 | P1(postdelay); |
| 1215 | P1(cmdaddpause); |
| 1216 | P1(datapause); |
| 1217 | P1(tmspeed); |
| 1218 | P1(busytimeout); |
| 1219 | P1(disbbm); |
| 1220 | P1(spare_offset); |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 1221 | #if !defined(CONFIG_MX6) || defined(CONFIG_MX6SX) || \ |
| 1222 | defined(CONFIG_MX6UL) || defined(CONFIG_MX6ULL) |
Alice Guo | eaab905 | 2020-05-05 22:04:01 +0800 | [diff] [blame] | 1223 | P1(onfi_sync_enable); |
| 1224 | P1(onfi_sync_speed); |
| 1225 | P1(onfi_sync_nand_data); |
| 1226 | P1(disbbm_search); |
| 1227 | P1(disbbm_search_limit); |
| 1228 | P1(read_retry_enable); |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 1229 | #endif |
Alice Guo | eaab905 | 2020-05-05 22:04:01 +0800 | [diff] [blame] | 1230 | #undef P1 |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 1231 | #define P1(x) printf(" %s = 0x%08x\n", #x, dbbt->x) |
| 1232 | printf("DBBT :\n"); |
Alice Guo | eaab905 | 2020-05-05 22:04:01 +0800 | [diff] [blame] | 1233 | P1(checksum); |
| 1234 | P1(fingerprint); |
| 1235 | P1(version); |
| 1236 | #undef P1 |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 1237 | #define P1(x) printf(" %s = %d\n", #x, dbbt->x) |
| 1238 | P1(dbbtpages); |
Alice Guo | eaab905 | 2020-05-05 22:04:01 +0800 | [diff] [blame] | 1239 | #undef P1 |
| 1240 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 1241 | for (i = 0; i < dbbt->dbbtpages; ++i) |
| 1242 | printf("%d ", *((u32 *)(dbbt_data_page + i))); |
| 1243 | |
| 1244 | if (!(plat_config.misc_flags & FIRMWARE_EXTRA_ONE)) { |
| 1245 | printf("Firmware: image #0 @ 0x%x size 0x%x\n", |
| 1246 | fcb->fw1_start, fcb->fw1_pages * mtd->writesize); |
| 1247 | printf("Firmware: image #1 @ 0x%x size 0x%x\n", |
| 1248 | fcb->fw2_start, fcb->fw2_pages * mtd->writesize); |
| 1249 | } else { |
| 1250 | printf("Firmware: image #0 @ 0x%x size 0x%x\n", |
| 1251 | fcb->fw1_start, fcb->fw1_pages * mtd->writesize); |
| 1252 | printf("Firmware: image #1 @ 0x%x size 0x%x\n", |
| 1253 | fcb->fw2_start, fcb->fw2_pages * mtd->writesize); |
| 1254 | /* TODO: Add extra image information */ |
Alice Guo | eaab905 | 2020-05-05 22:04:01 +0800 | [diff] [blame] | 1255 | } |
| 1256 | } |
| 1257 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 1258 | static bool check_fingerprint(void *data, int fingerprint) |
Alice Guo | eaab905 | 2020-05-05 22:04:01 +0800 | [diff] [blame] | 1259 | { |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 1260 | int off = 4; |
Alice Guo | eaab905 | 2020-05-05 22:04:01 +0800 | [diff] [blame] | 1261 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 1262 | return (*(int *)(data + off) == fingerprint); |
| 1263 | } |
Alice Guo | eaab905 | 2020-05-05 22:04:01 +0800 | [diff] [blame] | 1264 | |
Michael Trimarchi | ed83611 | 2022-01-07 18:27:17 +0100 | [diff] [blame] | 1265 | static int fuse_secondary_boot(u32 bank, u32 word, u32 mask, u32 off) |
| 1266 | { |
| 1267 | int err; |
| 1268 | u32 val; |
| 1269 | int ret; |
| 1270 | |
| 1271 | err = fuse_read(bank, word, &val); |
| 1272 | if (err) |
| 1273 | return 0; |
| 1274 | |
| 1275 | val = (val & mask) >> off; |
| 1276 | |
| 1277 | if (val > 10) |
| 1278 | return 0; |
| 1279 | |
| 1280 | switch (val) { |
| 1281 | case 0: |
| 1282 | ret = 4; |
| 1283 | break; |
| 1284 | case 1: |
| 1285 | ret = 1; |
| 1286 | break; |
| 1287 | default: |
| 1288 | ret = 2 << val; |
| 1289 | break; |
| 1290 | } |
| 1291 | |
| 1292 | return ret; |
| 1293 | }; |
| 1294 | |
Han Xu | 6578160 | 2020-05-05 22:04:04 +0800 | [diff] [blame] | 1295 | static int fuse_to_search_count(u32 bank, u32 word, u32 mask, u32 off) |
| 1296 | { |
| 1297 | int err; |
| 1298 | u32 val; |
| 1299 | int ret; |
| 1300 | |
| 1301 | /* by default, the boot search count from fuse should be 2 */ |
| 1302 | err = fuse_read(bank, word, &val); |
| 1303 | if (err) |
| 1304 | return 2; |
| 1305 | |
| 1306 | val = (val & mask) >> off; |
| 1307 | |
| 1308 | switch (val) { |
| 1309 | case 0: |
| 1310 | ret = 2; |
| 1311 | break; |
| 1312 | case 1: |
| 1313 | case 2: |
| 1314 | case 3: |
| 1315 | ret = 1 << val; |
| 1316 | break; |
| 1317 | default: |
| 1318 | ret = 2; |
| 1319 | } |
| 1320 | |
| 1321 | return ret; |
| 1322 | } |
| 1323 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 1324 | static int nandbcb_dump(struct boot_config *boot_cfg) |
| 1325 | { |
| 1326 | int i; |
| 1327 | loff_t off; |
| 1328 | struct mtd_info *mtd = boot_cfg->mtd; |
| 1329 | struct fcb_block fcb, fcb_copy; |
| 1330 | struct dbbt_block dbbt, dbbt_copy; |
| 1331 | void *dbbt_data_page, *dbbt_data_page_copy; |
| 1332 | bool fcb_not_found, dbbt_not_found; |
| 1333 | int ret = 0; |
| 1334 | |
| 1335 | dbbt_data_page = kzalloc(mtd->writesize, GFP_KERNEL); |
| 1336 | if (!dbbt_data_page) { |
| 1337 | printf("failed to allocate dbbt_data_page\n"); |
| 1338 | ret = -ENOMEM; |
| 1339 | return ret; |
Alice Guo | eaab905 | 2020-05-05 22:04:01 +0800 | [diff] [blame] | 1340 | } |
| 1341 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 1342 | dbbt_data_page_copy = kzalloc(mtd->writesize, GFP_KERNEL); |
| 1343 | if (!dbbt_data_page_copy) { |
| 1344 | printf("failed to allocate dbbt_data_page\n"); |
| 1345 | ret = -ENOMEM; |
| 1346 | goto dbbt_page_err; |
| 1347 | } |
Alice Guo | eaab905 | 2020-05-05 22:04:01 +0800 | [diff] [blame] | 1348 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 1349 | /* read fcb */ |
| 1350 | fcb_not_found = 1; |
| 1351 | off = 0; |
| 1352 | for (i = 0; i < g_boot_search_count; ++i) { |
| 1353 | if (fcb_not_found) { |
| 1354 | ret = read_fcb(boot_cfg, &fcb, off); |
Alice Guo | eaab905 | 2020-05-05 22:04:01 +0800 | [diff] [blame] | 1355 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 1356 | if (ret < 0) |
| 1357 | goto dbbt_page_copy_err; |
| 1358 | else if (ret == 1) |
| 1359 | continue; |
| 1360 | else if (ret == 0) |
| 1361 | if (check_fingerprint(&fcb, FCB_FINGERPRINT)) |
| 1362 | fcb_not_found = 0; |
Alice Guo | eaab905 | 2020-05-05 22:04:01 +0800 | [diff] [blame] | 1363 | } else { |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 1364 | ret = read_fcb(boot_cfg, &fcb_copy, off); |
| 1365 | |
| 1366 | if (ret < 0) |
| 1367 | goto dbbt_page_copy_err; |
| 1368 | if (memcmp(&fcb, &fcb_copy, |
| 1369 | sizeof(struct fcb_block))) { |
| 1370 | printf("FCB copies are not identical\n"); |
| 1371 | ret = -EINVAL; |
| 1372 | goto dbbt_page_copy_err; |
| 1373 | } |
Alice Guo | eaab905 | 2020-05-05 22:04:01 +0800 | [diff] [blame] | 1374 | } |
Alice Guo | eaab905 | 2020-05-05 22:04:01 +0800 | [diff] [blame] | 1375 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 1376 | /* next read location */ |
| 1377 | off += g_boot_search_stride; |
| 1378 | } |
Alice Guo | eaab905 | 2020-05-05 22:04:01 +0800 | [diff] [blame] | 1379 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 1380 | /* read dbbt*/ |
| 1381 | dbbt_not_found = 1; |
| 1382 | off = boot_cfg->search_area_size_in_bytes; |
| 1383 | for (i = 0; i < g_boot_search_count; ++i) { |
| 1384 | if (dbbt_not_found) { |
| 1385 | ret = read_dbbt(boot_cfg, &dbbt, dbbt_data_page, off); |
Alice Guo | eaab905 | 2020-05-05 22:04:01 +0800 | [diff] [blame] | 1386 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 1387 | if (ret < 0) |
| 1388 | goto dbbt_page_copy_err; |
| 1389 | else if (ret == 1) |
| 1390 | continue; |
| 1391 | else if (ret == 0) |
| 1392 | if (check_fingerprint(&dbbt, DBBT_FINGERPRINT)) |
| 1393 | dbbt_not_found = 0; |
Alice Guo | eaab905 | 2020-05-05 22:04:01 +0800 | [diff] [blame] | 1394 | } else { |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 1395 | ret = read_dbbt(boot_cfg, &dbbt_copy, |
| 1396 | dbbt_data_page_copy, off); |
| 1397 | |
| 1398 | if (ret < 0) |
| 1399 | goto dbbt_page_copy_err; |
| 1400 | if (memcmp(&dbbt, &dbbt_copy, |
| 1401 | sizeof(struct dbbt_block))) { |
| 1402 | printf("DBBT copies are not identical\n"); |
| 1403 | ret = -EINVAL; |
| 1404 | goto dbbt_page_copy_err; |
| 1405 | } |
| 1406 | if (dbbt.dbbtpages > 0 && |
| 1407 | memcmp(dbbt_data_page, dbbt_data_page_copy, |
| 1408 | mtd->writesize)) { |
| 1409 | printf("DBBT data copies are not identical\n"); |
| 1410 | ret = -EINVAL; |
| 1411 | goto dbbt_page_copy_err; |
| 1412 | } |
Alice Guo | eaab905 | 2020-05-05 22:04:01 +0800 | [diff] [blame] | 1413 | } |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 1414 | |
| 1415 | /* next read location */ |
| 1416 | off += g_boot_search_stride; |
Alice Guo | eaab905 | 2020-05-05 22:04:01 +0800 | [diff] [blame] | 1417 | } |
| 1418 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 1419 | dump_structure(boot_cfg, &fcb, &dbbt, dbbt_data_page); |
| 1420 | |
| 1421 | dbbt_page_copy_err: |
| 1422 | kfree(dbbt_data_page_copy); |
| 1423 | dbbt_page_err: |
| 1424 | kfree(dbbt_data_page); |
| 1425 | |
| 1426 | return ret; |
| 1427 | } |
| 1428 | |
| 1429 | static int do_nandbcb_dump(int argc, char * const argv[]) |
| 1430 | { |
| 1431 | struct boot_config cfg; |
| 1432 | int ret; |
Alice Guo | eaab905 | 2020-05-05 22:04:01 +0800 | [diff] [blame] | 1433 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 1434 | if (argc != 2) |
Alice Guo | eaab905 | 2020-05-05 22:04:01 +0800 | [diff] [blame] | 1435 | return CMD_RET_USAGE; |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 1436 | |
| 1437 | memset(&cfg, 0, sizeof(struct boot_config)); |
| 1438 | if (nandbcb_get_info(argc, argv, &cfg)) |
| 1439 | return CMD_RET_FAILURE; |
Alice Guo | eaab905 | 2020-05-05 22:04:01 +0800 | [diff] [blame] | 1440 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 1441 | if (nandbcb_get_size(argc, argv, 1, &cfg)) |
| 1442 | return CMD_RET_FAILURE; |
Alice Guo | eaab905 | 2020-05-05 22:04:01 +0800 | [diff] [blame] | 1443 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 1444 | if (nandbcb_set_boot_config(argc, argv, &cfg)) |
| 1445 | return CMD_RET_FAILURE; |
| 1446 | |
| 1447 | ret = nandbcb_dump(&cfg); |
| 1448 | if (ret) |
| 1449 | return ret; |
| 1450 | |
| 1451 | return ret; |
Alice Guo | eaab905 | 2020-05-05 22:04:01 +0800 | [diff] [blame] | 1452 | } |
| 1453 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 1454 | static int do_nandbcb_init(int argc, char * const argv[]) |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 1455 | { |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 1456 | u_char *buf; |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 1457 | size_t size; |
| 1458 | loff_t addr; |
| 1459 | char *endp; |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 1460 | int ret; |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 1461 | struct boot_config cfg; |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 1462 | |
| 1463 | if (argc != 4) |
| 1464 | return CMD_RET_USAGE; |
| 1465 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 1466 | memset(&cfg, 0, sizeof(struct boot_config)); |
| 1467 | if (nandbcb_get_info(argc, argv, &cfg)) |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 1468 | return CMD_RET_FAILURE; |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 1469 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 1470 | if (nandbcb_get_size(argc, argv, 2, &cfg)) |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 1471 | return CMD_RET_FAILURE; |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 1472 | size = cfg.boot_stream1_size; |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 1473 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 1474 | if (nandbcb_set_boot_config(argc, argv, &cfg)) |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 1475 | return CMD_RET_FAILURE; |
| 1476 | |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 1477 | addr = hextoul(argv[1], &endp); |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 1478 | if (*argv[1] == 0 || *endp != 0) |
| 1479 | return CMD_RET_FAILURE; |
Alice Guo | eaab905 | 2020-05-05 22:04:01 +0800 | [diff] [blame] | 1480 | |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 1481 | buf = map_physmem(addr, size, MAP_WRBACK); |
| 1482 | if (!buf) { |
| 1483 | puts("failed to map physical memory\n"); |
| 1484 | return CMD_RET_FAILURE; |
| 1485 | } |
| 1486 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 1487 | ret = nandbcb_init(&cfg, buf); |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 1488 | |
| 1489 | return ret == 0 ? CMD_RET_SUCCESS : CMD_RET_FAILURE; |
| 1490 | } |
| 1491 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 1492 | static int do_nandbcb(struct cmd_tbl *cmdtp, int flag, int argc, |
| 1493 | char *const argv[]) |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 1494 | { |
| 1495 | const char *cmd; |
| 1496 | int ret = 0; |
| 1497 | |
Alice Guo | eaab905 | 2020-05-05 22:04:01 +0800 | [diff] [blame] | 1498 | if (argc < 3) |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 1499 | goto usage; |
| 1500 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 1501 | /* check the platform config first */ |
| 1502 | if (is_mx6sx()) { |
| 1503 | plat_config = imx6sx_plat_config; |
| 1504 | } else if (is_mx7()) { |
| 1505 | plat_config = imx7d_plat_config; |
| 1506 | } else if (is_mx6ul() || is_mx6ull()) { |
| 1507 | plat_config = imx6ul_plat_config; |
| 1508 | } else if (is_mx6() && !is_mx6sx() && !is_mx6ul() && !is_mx6ull()) { |
| 1509 | plat_config = imx6qdl_plat_config; |
| 1510 | } else if (is_imx8mq()) { |
| 1511 | plat_config = imx8mq_plat_config; |
| 1512 | } else if (is_imx8mm()) { |
| 1513 | plat_config = imx8mm_plat_config; |
Han Xu | ab6d50d | 2020-10-10 08:48:49 -0500 | [diff] [blame] | 1514 | } else if (is_imx8mn() || is_imx8mp()) { |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 1515 | plat_config = imx8mn_plat_config; |
| 1516 | } else if (is_imx8qm() || is_imx8qxp()) { |
| 1517 | plat_config = imx8q_plat_config; |
| 1518 | } else { |
| 1519 | printf("ERROR: Unknown platform\n"); |
| 1520 | return CMD_RET_FAILURE; |
| 1521 | } |
| 1522 | |
Han Xu | ab6d50d | 2020-10-10 08:48:49 -0500 | [diff] [blame] | 1523 | if ((plat_config.misc_flags) & BT_SEARCH_CNT_FROM_FUSE) { |
| 1524 | if (is_imx8qxp()) |
| 1525 | g_boot_search_count = fuse_to_search_count(0, 720, 0xc0, 6); |
| 1526 | if (is_imx8mn() || is_imx8mp()) |
| 1527 | g_boot_search_count = fuse_to_search_count(2, 2, 0x6000, 13); |
| 1528 | printf("search count set to %d from fuse\n", |
| 1529 | g_boot_search_count); |
Han Xu | 6578160 | 2020-05-05 22:04:04 +0800 | [diff] [blame] | 1530 | } |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 1531 | |
Michael Trimarchi | ed83611 | 2022-01-07 18:27:17 +0100 | [diff] [blame] | 1532 | if (plat_config.misc_flags & FIRMWARE_SECONDARY_FIXED_ADDR) { |
| 1533 | if (is_imx8mn()) |
| 1534 | g_boot_secondary_offset = fuse_secondary_boot(2, 1, 0xff0000, 16); |
| 1535 | } |
| 1536 | |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 1537 | cmd = argv[1]; |
| 1538 | --argc; |
| 1539 | ++argv; |
| 1540 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 1541 | if (strcmp(cmd, "init") == 0) { |
| 1542 | ret = do_nandbcb_init(argc, argv); |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 1543 | goto done; |
| 1544 | } |
| 1545 | |
Alice Guo | eaab905 | 2020-05-05 22:04:01 +0800 | [diff] [blame] | 1546 | if (strcmp(cmd, "dump") == 0) { |
| 1547 | ret = do_nandbcb_dump(argc, argv); |
| 1548 | goto done; |
| 1549 | } |
| 1550 | |
Igor Opaniuk | f3caefe | 2019-11-03 16:49:46 +0100 | [diff] [blame] | 1551 | if (strcmp(cmd, "bcbonly") == 0) { |
| 1552 | ret = do_nandbcb_bcbonly(argc, argv); |
| 1553 | goto done; |
| 1554 | } |
| 1555 | |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 1556 | done: |
| 1557 | if (ret != -1) |
| 1558 | return ret; |
| 1559 | usage: |
| 1560 | return CMD_RET_USAGE; |
| 1561 | } |
| 1562 | |
Tom Rini | 03f146c | 2023-10-07 15:13:08 -0400 | [diff] [blame] | 1563 | U_BOOT_LONGHELP(nandbcb, |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 1564 | "init addr off|partition len - update 'len' bytes starting at\n" |
Igor Opaniuk | f3caefe | 2019-11-03 16:49:46 +0100 | [diff] [blame] | 1565 | " 'off|part' to memory address 'addr', skipping bad blocks\n" |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 1566 | "nandbcb bcbonly off|partition fw1-off fw1-size [fw2-off fw2-size]\n" |
| 1567 | " - write BCB only (FCB and DBBT)\n" |
| 1568 | " where `fwx-size` is fw sizes in bytes, `fw1-off`\n" |
Igor Opaniuk | be60431 | 2019-12-16 14:06:44 +0200 | [diff] [blame] | 1569 | " and `fw2-off` - firmware offsets\n" |
| 1570 | " FIY, BCB isn't erased automatically, so mtd erase should\n" |
| 1571 | " be called in advance before writing new BCB:\n" |
Alice Guo | eaab905 | 2020-05-05 22:04:01 +0800 | [diff] [blame] | 1572 | " > mtd erase mx7-bcb\n" |
Tom Rini | 03f146c | 2023-10-07 15:13:08 -0400 | [diff] [blame] | 1573 | "nandbcb dump off|partition - dump/verify boot structures\n"); |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 1574 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame] | 1575 | U_BOOT_CMD(nandbcb, 7, 1, do_nandbcb, |
| 1576 | "i.MX NAND Boot Control Blocks write", |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 1577 | nandbcb_help_text |
| 1578 | ); |