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 | |
| 14 | #include <common.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 15 | #include <malloc.h> |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 16 | #include <nand.h> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 17 | #include <dm/devres.h> |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 18 | |
| 19 | #include <asm/io.h> |
| 20 | #include <jffs2/jffs2.h> |
Parthiban Nallathambi | a99188b | 2019-10-18 11:46:19 +0200 | [diff] [blame] | 21 | #include <linux/bch.h> |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 22 | #include <linux/mtd/mtd.h> |
| 23 | |
Igor Opaniuk | 344833a | 2019-11-03 16:49:44 +0100 | [diff] [blame] | 24 | #include <asm/arch/sys_proto.h> |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 25 | #include <asm/mach-imx/imx-nandbcb.h> |
| 26 | #include <asm/mach-imx/imximage.cfg> |
| 27 | #include <mxs_nand.h> |
| 28 | #include <linux/mtd/mtd.h> |
| 29 | #include <nand.h> |
| 30 | |
Miquel Raynal | 617fd79 | 2019-10-25 19:39:29 +0200 | [diff] [blame] | 31 | #include "../../../cmd/legacy-mtd-utils.h" |
| 32 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 33 | /* FCB related flags */ |
| 34 | /* FCB layout with leading 12B reserved */ |
| 35 | #define FCB_LAYOUT_RESV_12B BIT(0) |
| 36 | /* FCB layout with leading 32B meta data */ |
| 37 | #define FCB_LAYOUT_META_32B BIT(1) |
| 38 | /* FCB encrypted by Hamming code */ |
| 39 | #define FCB_ENCODE_HAMMING BIT(2) |
| 40 | /* FCB encrypted by 40bit BCH */ |
| 41 | #define FCB_ENCODE_BCH_40b BIT(3) |
| 42 | /* FCB encrypted by 62bit BCH */ |
| 43 | #define FCB_ENCODE_BCH_62b BIT(4) |
| 44 | /* FCB encrypted by BCH */ |
| 45 | #define FCB_ENCODE_BCH (FCB_ENCODE_BCH_40b | FCB_ENCODE_BCH_62b) |
| 46 | /* FCB data was randomized */ |
| 47 | #define FCB_RANDON_ENABLED BIT(5) |
| 48 | |
| 49 | /* Firmware related flags */ |
| 50 | /* No 1K padding */ |
| 51 | #define FIRMWARE_NEED_PADDING BIT(8) |
| 52 | /* Extra firmware*/ |
| 53 | #define FIRMWARE_EXTRA_ONE BIT(9) |
| 54 | /* Secondary firmware on fixed address */ |
| 55 | #define FIRMWARE_SECONDARY_FIXED_ADDR BIT(10) |
| 56 | |
| 57 | /* Boot search related flags */ |
| 58 | #define BT_SEARCH_CNT_FROM_FUSE BIT(16) |
| 59 | |
| 60 | struct platform_config { |
| 61 | int misc_flags; |
| 62 | }; |
| 63 | |
| 64 | static struct platform_config plat_config; |
| 65 | |
| 66 | /* imx6q/dl/solo */ |
| 67 | static struct platform_config imx6qdl_plat_config = { |
| 68 | .misc_flags = FCB_LAYOUT_RESV_12B | |
| 69 | FCB_ENCODE_HAMMING | |
| 70 | FIRMWARE_NEED_PADDING, |
| 71 | }; |
| 72 | |
| 73 | static struct platform_config imx6sx_plat_config = { |
| 74 | .misc_flags = FCB_LAYOUT_META_32B | |
| 75 | FCB_ENCODE_BCH_62b | |
| 76 | FIRMWARE_NEED_PADDING | |
| 77 | FCB_RANDON_ENABLED, |
| 78 | }; |
| 79 | |
| 80 | static struct platform_config imx7d_plat_config = { |
| 81 | .misc_flags = FCB_LAYOUT_META_32B | |
| 82 | FCB_ENCODE_BCH_62b | |
| 83 | FIRMWARE_NEED_PADDING | |
| 84 | FCB_RANDON_ENABLED, |
| 85 | }; |
| 86 | |
| 87 | /* imx6ul/ull/ulz */ |
| 88 | static struct platform_config imx6ul_plat_config = { |
| 89 | .misc_flags = FCB_LAYOUT_META_32B | |
| 90 | FCB_ENCODE_BCH_40b | |
| 91 | FIRMWARE_NEED_PADDING, |
| 92 | }; |
| 93 | |
| 94 | static struct platform_config imx8mq_plat_config = { |
| 95 | .misc_flags = FCB_LAYOUT_META_32B | |
| 96 | FCB_ENCODE_BCH_62b | |
| 97 | FIRMWARE_NEED_PADDING | |
| 98 | FCB_RANDON_ENABLED | |
| 99 | FIRMWARE_EXTRA_ONE, |
| 100 | }; |
| 101 | |
| 102 | /* all other imx8mm */ |
| 103 | static struct platform_config imx8mm_plat_config = { |
| 104 | .misc_flags = FCB_LAYOUT_META_32B | |
| 105 | FCB_ENCODE_BCH_62b | |
| 106 | FIRMWARE_NEED_PADDING | |
| 107 | FCB_RANDON_ENABLED, |
| 108 | }; |
| 109 | |
| 110 | /* imx8mn */ |
| 111 | static struct platform_config imx8mn_plat_config = { |
| 112 | .misc_flags = FCB_LAYOUT_META_32B | |
| 113 | FCB_ENCODE_BCH_62b | |
| 114 | FCB_RANDON_ENABLED | |
| 115 | FIRMWARE_SECONDARY_FIXED_ADDR | |
| 116 | BT_SEARCH_CNT_FROM_FUSE, |
| 117 | }; |
| 118 | |
| 119 | /* imx8qx/qm */ |
| 120 | static struct platform_config imx8q_plat_config = { |
| 121 | .misc_flags = FCB_LAYOUT_META_32B | |
| 122 | FCB_ENCODE_BCH_62b | |
| 123 | FCB_RANDON_ENABLED | |
| 124 | FIRMWARE_SECONDARY_FIXED_ADDR | |
| 125 | BT_SEARCH_CNT_FROM_FUSE, |
| 126 | }; |
| 127 | |
| 128 | /* boot search related variables and definitions */ |
| 129 | static int g_boot_search_count = 4; |
| 130 | static int g_boot_search_stride; |
| 131 | static int g_pages_per_stride; |
| 132 | |
| 133 | /* mtd config structure */ |
| 134 | struct boot_config { |
| 135 | int dev; |
| 136 | struct mtd_info *mtd; |
| 137 | loff_t maxsize; |
| 138 | loff_t input_size; |
| 139 | loff_t offset; |
| 140 | loff_t boot_stream1_address; |
| 141 | loff_t boot_stream2_address; |
| 142 | size_t boot_stream1_size; |
| 143 | size_t boot_stream2_size; |
| 144 | size_t max_boot_stream_size; |
| 145 | int stride_size_in_byte; |
| 146 | int search_area_size_in_bytes; |
| 147 | int search_area_size_in_pages; |
| 148 | int secondary_boot_stream_off_in_MB; |
| 149 | }; |
| 150 | |
| 151 | /* boot_stream config structure */ |
| 152 | struct boot_stream_config { |
| 153 | char bs_label[32]; |
| 154 | loff_t bs_addr; |
| 155 | size_t bs_size; |
| 156 | void *bs_buf; |
| 157 | loff_t next_bs_addr; |
| 158 | bool need_padding; |
| 159 | }; |
| 160 | |
| 161 | /* FW index */ |
| 162 | #define FW1_ONLY 1 |
| 163 | #define FW2_ONLY 2 |
| 164 | #define FW_ALL FW1_ONLY | FW2_ONLY |
| 165 | #define FW_INX(x) (1 << (x)) |
| 166 | |
| 167 | /* NAND convert macros */ |
| 168 | #define CONV_TO_PAGES(x) ((u32)(x) / (u32)(mtd->writesize)) |
| 169 | #define CONV_TO_BLOCKS(x) ((u32)(x) / (u32)(mtd->erasesize)) |
| 170 | |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 171 | #define GETBIT(v, n) (((v) >> (n)) & 0x1) |
Alice Guo | a3f815b | 2020-05-05 22:04:00 +0800 | [diff] [blame] | 172 | #define IMX8MQ_SPL_SZ 0x3e000 |
| 173 | #define IMX8MQ_HDMI_FW_SZ 0x19c00 |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 174 | |
| 175 | static int nandbcb_get_info(int argc, char * const argv[], |
| 176 | struct boot_config *boot_cfg) |
| 177 | { |
| 178 | int dev; |
| 179 | struct mtd_info *mtd; |
| 180 | |
| 181 | dev = nand_curr_device; |
| 182 | if (dev < 0) { |
| 183 | printf("failed to get nand_curr_device, run nand device\n"); |
| 184 | return CMD_RET_FAILURE; |
| 185 | } |
| 186 | |
| 187 | mtd = get_nand_dev_by_index(dev); |
| 188 | if (!mtd) { |
| 189 | printf("failed to get mtd info\n"); |
| 190 | return CMD_RET_FAILURE; |
| 191 | } |
| 192 | |
| 193 | boot_cfg->dev = dev; |
| 194 | boot_cfg->mtd = mtd; |
| 195 | |
| 196 | return CMD_RET_SUCCESS; |
| 197 | } |
| 198 | |
| 199 | static int nandbcb_get_size(int argc, char * const argv[], int num, |
| 200 | struct boot_config *boot_cfg) |
| 201 | { |
| 202 | int dev; |
| 203 | loff_t offset, size, maxsize; |
| 204 | struct mtd_info *mtd; |
| 205 | |
| 206 | dev = boot_cfg->dev; |
| 207 | mtd = boot_cfg->mtd; |
| 208 | size = 0; |
| 209 | |
| 210 | if (mtd_arg_off_size(argc - num, argv + num, &dev, &offset, &size, |
| 211 | &maxsize, MTD_DEV_TYPE_NAND, mtd->size)) |
| 212 | return CMD_RET_FAILURE; |
| 213 | |
| 214 | boot_cfg->maxsize = maxsize; |
| 215 | boot_cfg->offset = offset; |
Alice Guo | eaab905 | 2020-05-05 22:04:01 +0800 | [diff] [blame] | 216 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 217 | debug("max: %llx, offset: %llx\n", maxsize, offset); |
| 218 | |
| 219 | if (size && size != maxsize) |
| 220 | boot_cfg->input_size = size; |
| 221 | |
| 222 | return CMD_RET_SUCCESS; |
| 223 | } |
| 224 | |
| 225 | static int nandbcb_set_boot_config(int argc, char * const argv[], |
| 226 | struct boot_config *boot_cfg) |
| 227 | { |
| 228 | struct mtd_info *mtd; |
| 229 | loff_t maxsize; |
| 230 | loff_t boot_stream1_address, boot_stream2_address, max_boot_stream_size; |
| 231 | |
| 232 | if (!boot_cfg->mtd) { |
| 233 | printf("Didn't get the mtd info, quit\n"); |
| 234 | return CMD_RET_FAILURE; |
| 235 | } |
| 236 | mtd = boot_cfg->mtd; |
| 237 | |
| 238 | /* |
| 239 | * By default |
| 240 | * set the search count as 4 |
| 241 | * set each FCB/DBBT/Firmware offset at the beginning of blocks |
| 242 | * customers may change the value as needed |
| 243 | */ |
| 244 | |
| 245 | /* if need more compact layout, change these values */ |
| 246 | /* g_boot_search_count was set as 4 at the definition*/ |
| 247 | /* g_pages_per_stride was set as block size */ |
| 248 | |
| 249 | g_pages_per_stride = mtd->erasesize / mtd->writesize; |
| 250 | |
| 251 | g_boot_search_stride = mtd->writesize * g_pages_per_stride; |
| 252 | |
| 253 | boot_cfg->stride_size_in_byte = g_boot_search_stride * mtd->writesize; |
| 254 | boot_cfg->search_area_size_in_bytes = |
| 255 | g_boot_search_count * g_boot_search_stride; |
| 256 | boot_cfg->search_area_size_in_pages = |
| 257 | boot_cfg->search_area_size_in_bytes / mtd->writesize; |
| 258 | |
| 259 | /* after FCB/DBBT, split the rest of area for two Firmwares */ |
| 260 | if (!boot_cfg->maxsize) { |
| 261 | printf("Didn't get the maxsize, quit\n"); |
| 262 | return CMD_RET_FAILURE; |
| 263 | } |
| 264 | maxsize = boot_cfg->maxsize; |
| 265 | /* align to page boundary */ |
| 266 | maxsize = ((u32)(maxsize + mtd->writesize - 1)) / (u32)mtd->writesize |
| 267 | * mtd->writesize; |
| 268 | |
| 269 | boot_stream1_address = 2 * boot_cfg->search_area_size_in_bytes; |
| 270 | boot_stream2_address = ((maxsize - boot_stream1_address) / 2 + |
| 271 | boot_stream1_address); |
| 272 | |
| 273 | if (boot_cfg->secondary_boot_stream_off_in_MB) |
| 274 | boot_stream2_address = boot_cfg->secondary_boot_stream_off_in_MB * 1024 * 1024; |
| 275 | |
| 276 | max_boot_stream_size = boot_stream2_address - boot_stream1_address; |
| 277 | |
| 278 | /* sanity check */ |
| 279 | if (max_boot_stream_size <= 0) { |
| 280 | debug("st1_addr: %llx, st2_addr: %llx, max: %llx\n", |
| 281 | boot_stream1_address, boot_stream2_address, |
| 282 | max_boot_stream_size); |
| 283 | printf("something wrong with firmware address settings\n"); |
| 284 | return CMD_RET_FAILURE; |
| 285 | } |
| 286 | boot_cfg->boot_stream1_address = boot_stream1_address; |
| 287 | boot_cfg->boot_stream2_address = boot_stream2_address; |
| 288 | boot_cfg->max_boot_stream_size = max_boot_stream_size; |
| 289 | |
| 290 | /* set the boot_stream size as the input size now */ |
| 291 | if (boot_cfg->input_size) { |
| 292 | boot_cfg->boot_stream1_size = boot_cfg->input_size; |
| 293 | boot_cfg->boot_stream2_size = boot_cfg->input_size; |
| 294 | } |
| 295 | |
| 296 | return CMD_RET_SUCCESS; |
| 297 | } |
| 298 | |
| 299 | static int nandbcb_check_space(struct boot_config *boot_cfg) |
| 300 | { |
| 301 | size_t maxsize = boot_cfg->maxsize; |
| 302 | size_t max_boot_stream_size = boot_cfg->max_boot_stream_size; |
| 303 | loff_t boot_stream2_address = boot_cfg->boot_stream2_address; |
| 304 | |
| 305 | if (boot_cfg->boot_stream1_size && |
| 306 | boot_cfg->boot_stream1_size > max_boot_stream_size) { |
| 307 | printf("boot stream1 doesn't fit, check partition size or settings\n"); |
| 308 | return CMD_RET_FAILURE; |
| 309 | } |
| 310 | |
| 311 | if (boot_cfg->boot_stream2_size && |
| 312 | boot_cfg->boot_stream2_size > maxsize - boot_stream2_address) { |
| 313 | printf("boot stream2 doesn't fit, check partition size or settings\n"); |
| 314 | return CMD_RET_FAILURE; |
| 315 | } |
| 316 | |
| 317 | return CMD_RET_SUCCESS; |
| 318 | } |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 319 | |
Parthiban Nallathambi | a99188b | 2019-10-18 11:46:19 +0200 | [diff] [blame] | 320 | #if defined(CONFIG_MX6UL) || defined(CONFIG_MX6ULL) |
| 321 | static uint8_t reverse_bit(uint8_t b) |
| 322 | { |
| 323 | b = (b & 0xf0) >> 4 | (b & 0x0f) << 4; |
| 324 | b = (b & 0xcc) >> 2 | (b & 0x33) << 2; |
| 325 | b = (b & 0xaa) >> 1 | (b & 0x55) << 1; |
| 326 | |
| 327 | return b; |
| 328 | } |
| 329 | |
| 330 | static void encode_bch_ecc(void *buf, struct fcb_block *fcb, int eccbits) |
| 331 | { |
| 332 | int i, j, m = 13; |
| 333 | int blocksize = 128; |
| 334 | int numblocks = 8; |
| 335 | int ecc_buf_size = (m * eccbits + 7) / 8; |
| 336 | struct bch_control *bch = init_bch(m, eccbits, 0); |
| 337 | u8 *ecc_buf = kzalloc(ecc_buf_size, GFP_KERNEL); |
| 338 | u8 *tmp_buf = kzalloc(blocksize * numblocks, GFP_KERNEL); |
| 339 | u8 *psrc, *pdst; |
| 340 | |
| 341 | /* |
| 342 | * The blocks here are bit aligned. If eccbits is a multiple of 8, |
| 343 | * we just can copy bytes. Otherwiese we must move the blocks to |
| 344 | * the next free bit position. |
| 345 | */ |
| 346 | WARN_ON(eccbits % 8); |
| 347 | |
| 348 | memcpy(tmp_buf, fcb, sizeof(*fcb)); |
| 349 | |
| 350 | for (i = 0; i < numblocks; i++) { |
| 351 | memset(ecc_buf, 0, ecc_buf_size); |
| 352 | psrc = tmp_buf + i * blocksize; |
| 353 | pdst = buf + i * (blocksize + ecc_buf_size); |
| 354 | |
| 355 | /* copy data byte aligned to destination buf */ |
| 356 | memcpy(pdst, psrc, blocksize); |
| 357 | |
| 358 | /* |
| 359 | * imx-kobs use a modified encode_bch which reverse the |
| 360 | * bit order of the data before calculating bch. |
| 361 | * Do this in the buffer and use the bch lib here. |
| 362 | */ |
| 363 | for (j = 0; j < blocksize; j++) |
| 364 | psrc[j] = reverse_bit(psrc[j]); |
| 365 | |
| 366 | encode_bch(bch, psrc, blocksize, ecc_buf); |
| 367 | |
| 368 | /* reverse ecc bit */ |
| 369 | for (j = 0; j < ecc_buf_size; j++) |
| 370 | ecc_buf[j] = reverse_bit(ecc_buf[j]); |
| 371 | |
| 372 | /* Here eccbuf is byte aligned and we can just copy it */ |
| 373 | memcpy(pdst + blocksize, ecc_buf, ecc_buf_size); |
| 374 | } |
| 375 | |
| 376 | kfree(ecc_buf); |
| 377 | kfree(tmp_buf); |
| 378 | free_bch(bch); |
| 379 | } |
| 380 | #else |
| 381 | |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 382 | static u8 calculate_parity_13_8(u8 d) |
| 383 | { |
| 384 | u8 p = 0; |
| 385 | |
| 386 | p |= (GETBIT(d, 6) ^ GETBIT(d, 5) ^ GETBIT(d, 3) ^ GETBIT(d, 2)) << 0; |
| 387 | p |= (GETBIT(d, 7) ^ GETBIT(d, 5) ^ GETBIT(d, 4) ^ GETBIT(d, 2) ^ |
| 388 | GETBIT(d, 1)) << 1; |
| 389 | p |= (GETBIT(d, 7) ^ GETBIT(d, 6) ^ GETBIT(d, 5) ^ GETBIT(d, 1) ^ |
| 390 | GETBIT(d, 0)) << 2; |
| 391 | p |= (GETBIT(d, 7) ^ GETBIT(d, 4) ^ GETBIT(d, 3) ^ GETBIT(d, 0)) << 3; |
| 392 | p |= (GETBIT(d, 6) ^ GETBIT(d, 4) ^ GETBIT(d, 3) ^ GETBIT(d, 2) ^ |
| 393 | GETBIT(d, 1) ^ GETBIT(d, 0)) << 4; |
| 394 | |
| 395 | return p; |
| 396 | } |
| 397 | |
| 398 | static void encode_hamming_13_8(void *_src, void *_ecc, size_t size) |
| 399 | { |
| 400 | int i; |
| 401 | u8 *src = _src; |
| 402 | u8 *ecc = _ecc; |
| 403 | |
| 404 | for (i = 0; i < size; i++) |
| 405 | ecc[i] = calculate_parity_13_8(src[i]); |
| 406 | } |
Parthiban Nallathambi | a99188b | 2019-10-18 11:46:19 +0200 | [diff] [blame] | 407 | #endif |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 408 | |
| 409 | static u32 calc_chksum(void *buf, size_t size) |
| 410 | { |
| 411 | u32 chksum = 0; |
| 412 | u8 *bp = buf; |
| 413 | size_t i; |
| 414 | |
| 415 | for (i = 0; i < size; i++) |
| 416 | chksum += bp[i]; |
| 417 | |
| 418 | return ~chksum; |
| 419 | } |
| 420 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 421 | 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] | 422 | { |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 423 | struct mtd_info *mtd = boot_cfg->mtd; |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 424 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 425 | struct mxs_nand_info *nand_info = nand_get_controller_data(chip); |
Igor Opaniuk | 344833a | 2019-11-03 16:49:44 +0100 | [diff] [blame] | 426 | struct mxs_nand_layout l; |
| 427 | |
| 428 | mxs_nand_get_layout(mtd, &l); |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 429 | |
| 430 | fcb->fingerprint = FCB_FINGERPRINT; |
| 431 | fcb->version = FCB_VERSION_1; |
Igor Opaniuk | 344833a | 2019-11-03 16:49:44 +0100 | [diff] [blame] | 432 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 433 | fcb->datasetup = 80; |
| 434 | fcb->datahold = 60; |
| 435 | fcb->addr_setup = 25; |
| 436 | fcb->dsample_time = 6; |
| 437 | |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 438 | fcb->pagesize = mtd->writesize; |
| 439 | fcb->oob_pagesize = mtd->writesize + mtd->oobsize; |
| 440 | fcb->sectors = mtd->erasesize / mtd->writesize; |
| 441 | |
Igor Opaniuk | 344833a | 2019-11-03 16:49:44 +0100 | [diff] [blame] | 442 | fcb->meta_size = l.meta_size; |
| 443 | fcb->nr_blocks = l.nblocks; |
| 444 | fcb->ecc_nr = l.data0_size; |
| 445 | fcb->ecc_level = l.ecc0; |
| 446 | fcb->ecc_size = l.datan_size; |
| 447 | fcb->ecc_type = l.eccn; |
Han Xu | ae0bdd7 | 2020-05-05 22:03:59 +0800 | [diff] [blame] | 448 | fcb->bchtype = l.gf_len; |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 449 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 450 | /* DBBT search area starts from the next block after all FCB */ |
| 451 | fcb->dbbt_start = boot_cfg->search_area_size_in_pages; |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 452 | |
| 453 | fcb->bb_byte = nand_info->bch_geometry.block_mark_byte_offset; |
| 454 | fcb->bb_start_bit = nand_info->bch_geometry.block_mark_bit_offset; |
| 455 | |
| 456 | fcb->phy_offset = mtd->writesize; |
| 457 | |
Igor Opaniuk | 344833a | 2019-11-03 16:49:44 +0100 | [diff] [blame] | 458 | fcb->disbbm = 0; |
Igor Opaniuk | 344833a | 2019-11-03 16:49:44 +0100 | [diff] [blame] | 459 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 460 | fcb->fw1_start = CONV_TO_PAGES(boot_cfg->boot_stream1_address); |
| 461 | fcb->fw2_start = CONV_TO_PAGES(boot_cfg->boot_stream2_address); |
| 462 | fcb->fw1_pages = CONV_TO_PAGES(boot_cfg->boot_stream1_size); |
| 463 | fcb->fw2_pages = CONV_TO_PAGES(boot_cfg->boot_stream2_size); |
Igor Opaniuk | 14a489b | 2019-11-03 16:49:45 +0100 | [diff] [blame] | 464 | |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 465 | fcb->checksum = calc_chksum((void *)fcb + 4, sizeof(*fcb) - 4); |
| 466 | } |
| 467 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 468 | 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] | 469 | { |
| 470 | int n, n_bad_blocks = 0; |
| 471 | u32 *bb = buf + 0x8; |
| 472 | u32 *n_bad_blocksp = buf + 0x4; |
| 473 | |
| 474 | for (n = 0; n < num_blocks; n++) { |
| 475 | loff_t offset = n * mtd->erasesize; |
| 476 | if (mtd_block_isbad(mtd, offset)) { |
| 477 | n_bad_blocks++; |
| 478 | *bb = n; |
| 479 | bb++; |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | *n_bad_blocksp = n_bad_blocks; |
| 484 | |
| 485 | return n_bad_blocks; |
| 486 | } |
| 487 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 488 | /* |
| 489 | * return 1 - bad block |
| 490 | * return 0 - read successfully |
| 491 | * return < 0 - read failed |
| 492 | */ |
| 493 | static int read_fcb(struct boot_config *boot_cfg, struct fcb_block *fcb, |
| 494 | loff_t off) |
Igor Opaniuk | 14a489b | 2019-11-03 16:49:45 +0100 | [diff] [blame] | 495 | { |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 496 | struct mtd_info *mtd; |
| 497 | void *fcb_raw_page; |
| 498 | size_t size; |
| 499 | int ret = 0; |
| 500 | |
| 501 | mtd = boot_cfg->mtd; |
| 502 | fcb_raw_page = kzalloc(mtd->writesize + mtd->oobsize, GFP_KERNEL); |
| 503 | |
| 504 | if (mtd_block_isbad(mtd, off)) { |
| 505 | printf("Block %d is bad, skipped\n", (int)CONV_TO_BLOCKS(off)); |
| 506 | return 1; |
| 507 | } |
| 508 | |
| 509 | /* |
| 510 | * User BCH hardware to decode ECC for FCB |
| 511 | */ |
| 512 | if (plat_config.misc_flags & FCB_ENCODE_BCH) { |
| 513 | size = sizeof(struct fcb_block); |
| 514 | |
| 515 | /* switch nand BCH to FCB compatible settings */ |
| 516 | if (plat_config.misc_flags & FCB_ENCODE_BCH_62b) |
| 517 | mxs_nand_mode_fcb_62bit(mtd); |
| 518 | else if (plat_config.misc_flags & FCB_ENCODE_BCH_40b) |
| 519 | mxs_nand_mode_fcb_40bit(mtd); |
| 520 | |
| 521 | ret = nand_read(mtd, off, &size, (u_char *)fcb); |
| 522 | |
| 523 | /* switch BCH back */ |
| 524 | mxs_nand_mode_normal(mtd); |
| 525 | printf("NAND FCB read from 0x%llx offset 0x%zx read: %s\n", |
| 526 | off, size, ret ? "ERROR" : "OK"); |
| 527 | |
| 528 | } else if (plat_config.misc_flags & FCB_ENCODE_HAMMING) { |
| 529 | /* raw read*/ |
| 530 | mtd_oob_ops_t ops = { |
| 531 | .datbuf = (u8 *)fcb_raw_page, |
| 532 | .oobbuf = ((u8 *)fcb_raw_page) + mtd->writesize, |
| 533 | .len = mtd->writesize, |
| 534 | .ooblen = mtd->oobsize, |
| 535 | .mode = MTD_OPS_RAW |
| 536 | }; |
| 537 | |
| 538 | ret = mtd_read_oob(mtd, off, &ops); |
| 539 | printf("NAND FCB read from 0x%llx offset 0x%zx read: %s\n", |
| 540 | off, ops.len, ret ? "ERROR" : "OK"); |
| 541 | } |
| 542 | |
| 543 | if (ret) |
| 544 | goto fcb_raw_page_err; |
| 545 | |
| 546 | if ((plat_config.misc_flags & FCB_ENCODE_HAMMING) && |
| 547 | (plat_config.misc_flags & FCB_LAYOUT_RESV_12B)) |
| 548 | memcpy(fcb, fcb_raw_page + 12, sizeof(struct fcb_block)); |
| 549 | |
| 550 | /* TODO: check if it can pass Hamming check */ |
| 551 | |
| 552 | fcb_raw_page_err: |
| 553 | kfree(fcb_raw_page); |
| 554 | |
| 555 | return ret; |
| 556 | } |
| 557 | |
| 558 | static int write_fcb(struct boot_config *boot_cfg, struct fcb_block *fcb) |
| 559 | { |
| 560 | struct mtd_info *mtd; |
| 561 | void *fcb_raw_page = NULL; |
Igor Opaniuk | 14a489b | 2019-11-03 16:49:45 +0100 | [diff] [blame] | 562 | int i, ret; |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 563 | loff_t off; |
| 564 | size_t size; |
| 565 | |
| 566 | mtd = boot_cfg->mtd; |
Igor Opaniuk | 14a489b | 2019-11-03 16:49:45 +0100 | [diff] [blame] | 567 | |
| 568 | /* |
| 569 | * We prepare raw page only for i.MX6, for i.MX7 we |
| 570 | * leverage BCH hw module instead |
| 571 | */ |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 572 | if ((plat_config.misc_flags & FCB_ENCODE_HAMMING) && |
| 573 | (plat_config.misc_flags & FCB_LAYOUT_RESV_12B)) { |
Igor Opaniuk | 14a489b | 2019-11-03 16:49:45 +0100 | [diff] [blame] | 574 | fcb_raw_page = kzalloc(mtd->writesize + mtd->oobsize, |
| 575 | GFP_KERNEL); |
| 576 | if (!fcb_raw_page) { |
| 577 | debug("failed to allocate fcb_raw_page\n"); |
| 578 | ret = -ENOMEM; |
| 579 | return ret; |
| 580 | } |
| 581 | |
| 582 | #if defined(CONFIG_MX6UL) || defined(CONFIG_MX6ULL) |
| 583 | /* 40 bit BCH, for i.MX6UL(L) */ |
| 584 | encode_bch_ecc(fcb_raw_page + 32, fcb, 40); |
| 585 | #else |
| 586 | memcpy(fcb_raw_page + 12, fcb, sizeof(struct fcb_block)); |
| 587 | encode_hamming_13_8(fcb_raw_page + 12, |
| 588 | fcb_raw_page + 12 + 512, 512); |
| 589 | #endif |
| 590 | /* |
| 591 | * Set the first and second byte of OOB data to 0xFF, |
| 592 | * not 0x00. These bytes are used as the Manufacturers Bad |
| 593 | * Block Marker (MBBM). Since the FCB is mostly written to |
| 594 | * the first page in a block, a scan for |
| 595 | * factory bad blocks will detect these blocks as bad, e.g. |
| 596 | * when function nand_scan_bbt() is executed to build a new |
| 597 | * bad block table. |
| 598 | */ |
| 599 | memset(fcb_raw_page + mtd->writesize, 0xFF, 2); |
| 600 | } |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 601 | |
| 602 | /* start writing FCB from the very beginning */ |
| 603 | off = 0; |
| 604 | |
| 605 | for (i = 0; i < g_boot_search_count; i++) { |
Igor Opaniuk | 14a489b | 2019-11-03 16:49:45 +0100 | [diff] [blame] | 606 | if (mtd_block_isbad(mtd, off)) { |
| 607 | printf("Block %d is bad, skipped\n", i); |
| 608 | continue; |
| 609 | } |
| 610 | |
| 611 | /* |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 612 | * User BCH hardware module to generate ECC for FCB |
Igor Opaniuk | 14a489b | 2019-11-03 16:49:45 +0100 | [diff] [blame] | 613 | */ |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 614 | if (plat_config.misc_flags & FCB_ENCODE_BCH) { |
| 615 | size = sizeof(struct fcb_block); |
Igor Opaniuk | 14a489b | 2019-11-03 16:49:45 +0100 | [diff] [blame] | 616 | |
| 617 | /* switch nand BCH to FCB compatible settings */ |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 618 | if (plat_config.misc_flags & FCB_ENCODE_BCH_62b) |
| 619 | mxs_nand_mode_fcb_62bit(mtd); |
| 620 | else if (plat_config.misc_flags & FCB_ENCODE_BCH_40b) |
| 621 | mxs_nand_mode_fcb_40bit(mtd); |
Alice Guo | eaab905 | 2020-05-05 22:04:01 +0800 | [diff] [blame] | 622 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 623 | ret = nand_write(mtd, off, &size, (u_char *)fcb); |
Alice Guo | eaab905 | 2020-05-05 22:04:01 +0800 | [diff] [blame] | 624 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 625 | /* switch BCH back */ |
Igor Opaniuk | 14a489b | 2019-11-03 16:49:45 +0100 | [diff] [blame] | 626 | mxs_nand_mode_normal(mtd); |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 627 | printf("NAND FCB write to 0x%zx offset 0x%llx written: %s\n", |
| 628 | size, off, ret ? "ERROR" : "OK"); |
Igor Opaniuk | 14a489b | 2019-11-03 16:49:45 +0100 | [diff] [blame] | 629 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 630 | } else if (plat_config.misc_flags & FCB_ENCODE_HAMMING) { |
Igor Opaniuk | 14a489b | 2019-11-03 16:49:45 +0100 | [diff] [blame] | 631 | /* raw write */ |
| 632 | mtd_oob_ops_t ops = { |
| 633 | .datbuf = (u8 *)fcb_raw_page, |
| 634 | .oobbuf = ((u8 *)fcb_raw_page) + |
| 635 | mtd->writesize, |
| 636 | .len = mtd->writesize, |
| 637 | .ooblen = mtd->oobsize, |
| 638 | .mode = MTD_OPS_RAW |
| 639 | }; |
| 640 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 641 | ret = mtd_write_oob(mtd, off, &ops); |
| 642 | printf("NAND FCB write to 0x%llxx offset 0x%zx written: %s\n", off, ops.len, ret ? "ERROR" : "OK"); |
Igor Opaniuk | 14a489b | 2019-11-03 16:49:45 +0100 | [diff] [blame] | 643 | } |
| 644 | |
Igor Opaniuk | 14a489b | 2019-11-03 16:49:45 +0100 | [diff] [blame] | 645 | if (ret) |
| 646 | goto fcb_raw_page_err; |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 647 | |
| 648 | /* next writing location */ |
| 649 | off += g_boot_search_stride; |
| 650 | } |
| 651 | |
| 652 | return 0; |
| 653 | |
| 654 | fcb_raw_page_err: |
| 655 | kfree(fcb_raw_page); |
| 656 | |
| 657 | return ret; |
| 658 | } |
| 659 | |
| 660 | /* |
| 661 | * return 1 - bad block |
| 662 | * return 0 - read successfully |
| 663 | * return < 0 - read failed |
| 664 | */ |
| 665 | static int read_dbbt(struct boot_config *boot_cfg, struct dbbt_block *dbbt, |
| 666 | void *dbbt_data_page, loff_t off) |
| 667 | { |
| 668 | size_t size; |
| 669 | struct mtd_info *mtd; |
| 670 | loff_t to; |
| 671 | int ret; |
Igor Opaniuk | 14a489b | 2019-11-03 16:49:45 +0100 | [diff] [blame] | 672 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 673 | mtd = boot_cfg->mtd; |
Alice Guo | eaab905 | 2020-05-05 22:04:01 +0800 | [diff] [blame] | 674 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 675 | if (mtd_block_isbad(mtd, off)) { |
| 676 | printf("Block %d is bad, skipped\n", |
| 677 | (int)CONV_TO_BLOCKS(off)); |
| 678 | return 1; |
| 679 | } |
| 680 | |
| 681 | size = sizeof(struct dbbt_block); |
| 682 | ret = nand_read(mtd, off, &size, (u_char *)dbbt); |
| 683 | printf("NAND DBBT read from 0x%llx offset 0x%zx read: %s\n", |
| 684 | off, size, ret ? "ERROR" : "OK"); |
| 685 | if (ret) |
| 686 | return ret; |
| 687 | |
| 688 | /* dbbtpages == 0 if no bad blocks */ |
| 689 | if (dbbt->dbbtpages > 0) { |
| 690 | to = off + 4 * mtd->writesize; |
| 691 | size = mtd->writesize; |
| 692 | ret = nand_read(mtd, to, &size, dbbt_data_page); |
| 693 | printf("DBBT data read from 0x%llx offset 0x%zx read: %s\n", |
| 694 | to, size, ret ? "ERROR" : "OK"); |
| 695 | |
| 696 | if (ret) |
| 697 | return ret; |
| 698 | } |
| 699 | |
| 700 | return 0; |
| 701 | } |
| 702 | |
| 703 | static int write_dbbt(struct boot_config *boot_cfg, struct dbbt_block *dbbt, |
| 704 | void *dbbt_data_page) |
| 705 | { |
| 706 | int i; |
| 707 | loff_t off, to; |
| 708 | size_t size; |
| 709 | struct mtd_info *mtd; |
| 710 | int ret; |
| 711 | |
| 712 | mtd = boot_cfg->mtd; |
| 713 | |
| 714 | /* start writing DBBT after all FCBs */ |
| 715 | off = boot_cfg->search_area_size_in_bytes; |
| 716 | size = mtd->writesize; |
| 717 | |
| 718 | for (i = 0; i < g_boot_search_count; i++) { |
| 719 | if (mtd_block_isbad(mtd, off)) { |
| 720 | printf("Block %d is bad, skipped\n", |
| 721 | (int)(i + CONV_TO_BLOCKS(off))); |
| 722 | continue; |
| 723 | } |
| 724 | |
| 725 | ret = nand_write(mtd, off, &size, (u_char *)dbbt); |
| 726 | printf("NAND DBBT write to 0x%llx offset 0x%zx written: %s\n", |
| 727 | off, size, ret ? "ERROR" : "OK"); |
| 728 | if (ret) |
| 729 | return ret; |
Alice Guo | eaab905 | 2020-05-05 22:04:01 +0800 | [diff] [blame] | 730 | |
Igor Opaniuk | 14a489b | 2019-11-03 16:49:45 +0100 | [diff] [blame] | 731 | /* dbbtpages == 0 if no bad blocks */ |
| 732 | if (dbbt->dbbtpages > 0) { |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 733 | to = off + 4 * mtd->writesize; |
| 734 | ret = nand_write(mtd, to, &size, dbbt_data_page); |
| 735 | printf("DBBT data write to 0x%llx offset 0x%zx written: %s\n", |
| 736 | to, size, ret ? "ERROR" : "OK"); |
Igor Opaniuk | 14a489b | 2019-11-03 16:49:45 +0100 | [diff] [blame] | 737 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 738 | if (ret) |
| 739 | return ret; |
Igor Opaniuk | 14a489b | 2019-11-03 16:49:45 +0100 | [diff] [blame] | 740 | } |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 741 | |
| 742 | /* next writing location */ |
| 743 | off += g_boot_search_stride; |
Igor Opaniuk | 14a489b | 2019-11-03 16:49:45 +0100 | [diff] [blame] | 744 | } |
| 745 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 746 | return 0; |
| 747 | } |
| 748 | |
| 749 | /* reuse the check_skip_len from nand_util.c with minor change*/ |
| 750 | static int check_skip_length(struct boot_config *boot_cfg, loff_t offset, |
| 751 | size_t length, size_t *used) |
| 752 | { |
| 753 | struct mtd_info *mtd = boot_cfg->mtd; |
| 754 | size_t maxsize = boot_cfg->maxsize; |
| 755 | size_t len_excl_bad = 0; |
| 756 | int ret = 0; |
| 757 | |
| 758 | while (len_excl_bad < length) { |
| 759 | size_t block_len, block_off; |
| 760 | loff_t block_start; |
| 761 | |
| 762 | if (offset >= maxsize) |
| 763 | return -1; |
| 764 | |
| 765 | block_start = offset & ~(loff_t)(mtd->erasesize - 1); |
| 766 | block_off = offset & (mtd->erasesize - 1); |
| 767 | block_len = mtd->erasesize - block_off; |
| 768 | |
| 769 | if (!nand_block_isbad(mtd, block_start)) |
| 770 | len_excl_bad += block_len; |
| 771 | else |
| 772 | ret = 1; |
| 773 | |
| 774 | offset += block_len; |
| 775 | *used += block_len; |
| 776 | } |
| 777 | |
| 778 | /* If the length is not a multiple of block_len, adjust. */ |
| 779 | if (len_excl_bad > length) |
| 780 | *used -= (len_excl_bad - length); |
| 781 | |
| 782 | return ret; |
| 783 | } |
| 784 | |
| 785 | static int nandbcb_get_next_good_blk_addr(struct boot_config *boot_cfg, |
| 786 | struct boot_stream_config *bs_cfg) |
| 787 | { |
| 788 | struct mtd_info *mtd = boot_cfg->mtd; |
| 789 | loff_t offset = bs_cfg->bs_addr; |
| 790 | size_t length = bs_cfg->bs_size; |
| 791 | size_t used = 0; |
| 792 | int ret; |
| 793 | |
| 794 | ret = check_skip_length(boot_cfg, offset, length, &used); |
| 795 | |
| 796 | if (ret < 0) |
| 797 | return ret; |
| 798 | |
| 799 | /* get next image address */ |
| 800 | bs_cfg->next_bs_addr = (u32)(offset + used + mtd->erasesize - 1) |
| 801 | / (u32)mtd->erasesize * mtd->erasesize; |
| 802 | |
| 803 | return ret; |
| 804 | } |
| 805 | |
| 806 | static int nandbcb_write_bs_skip_bad(struct boot_config *boot_cfg, |
| 807 | struct boot_stream_config *bs_cfg) |
| 808 | { |
| 809 | struct mtd_info *mtd; |
| 810 | void *buf; |
| 811 | loff_t offset, maxsize; |
| 812 | size_t size; |
| 813 | size_t length; |
| 814 | int ret; |
| 815 | bool padding_flag = false; |
| 816 | |
| 817 | mtd = boot_cfg->mtd; |
| 818 | offset = bs_cfg->bs_addr; |
| 819 | maxsize = boot_cfg->maxsize; |
| 820 | size = bs_cfg->bs_size; |
| 821 | |
| 822 | /* some boot images may need leading offset */ |
| 823 | if (bs_cfg->need_padding && |
| 824 | (plat_config.misc_flags & FIRMWARE_NEED_PADDING)) |
| 825 | padding_flag = 1; |
| 826 | |
| 827 | if (padding_flag) |
| 828 | length = ALIGN(size + FLASH_OFFSET_STANDARD, mtd->writesize); |
| 829 | else |
| 830 | length = ALIGN(size, mtd->writesize); |
| 831 | |
| 832 | buf = kzalloc(length, GFP_KERNEL); |
| 833 | if (!buf) { |
| 834 | printf("failed to allocate buffer for firmware\n"); |
| 835 | ret = -ENOMEM; |
| 836 | return ret; |
| 837 | } |
| 838 | |
| 839 | if (padding_flag) |
| 840 | memcpy(buf + FLASH_OFFSET_STANDARD, bs_cfg->bs_buf, size); |
| 841 | else |
| 842 | memcpy(buf, bs_cfg->bs_buf, size); |
Igor Opaniuk | 14a489b | 2019-11-03 16:49:45 +0100 | [diff] [blame] | 843 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 844 | ret = nand_write_skip_bad(mtd, offset, &length, NULL, maxsize, |
| 845 | (u_char *)buf, WITH_WR_VERIFY); |
| 846 | printf("Write %s @0x%llx offset, 0x%zx bytes written: %s\n", |
| 847 | bs_cfg->bs_label, offset, length, ret ? "ERROR" : "OK"); |
| 848 | |
| 849 | if (ret) |
| 850 | /* write image failed, quit */ |
| 851 | goto err; |
| 852 | |
| 853 | /* get next good blk address if needed */ |
| 854 | if (bs_cfg->need_padding) { |
| 855 | ret = nandbcb_get_next_good_blk_addr(boot_cfg, bs_cfg); |
| 856 | if (ret < 0) { |
| 857 | printf("Next image cannot fit in NAND partition\n"); |
| 858 | goto err; |
| 859 | } |
| 860 | } |
| 861 | |
| 862 | /* now we know how the exact image size written to NAND */ |
| 863 | bs_cfg->bs_size = length; |
| 864 | return 0; |
| 865 | err: |
| 866 | kfree(buf); |
Igor Opaniuk | 14a489b | 2019-11-03 16:49:45 +0100 | [diff] [blame] | 867 | return ret; |
| 868 | } |
| 869 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 870 | static int nandbcb_write_fw(struct boot_config *boot_cfg, u_char *buf, |
| 871 | int index) |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 872 | { |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 873 | int i; |
| 874 | loff_t offset; |
| 875 | size_t size; |
| 876 | loff_t next_bs_addr; |
| 877 | struct boot_stream_config bs_cfg; |
| 878 | int ret; |
| 879 | |
| 880 | for (i = 0; i < 2; ++i) { |
| 881 | if (!(FW_INX(i) & index)) |
| 882 | continue; |
| 883 | |
| 884 | if (i == 0) { |
| 885 | offset = boot_cfg->boot_stream1_address; |
| 886 | size = boot_cfg->boot_stream1_size; |
| 887 | } else { |
| 888 | offset = boot_cfg->boot_stream2_address; |
| 889 | size = boot_cfg->boot_stream2_size; |
| 890 | } |
| 891 | |
| 892 | /* write Firmware*/ |
| 893 | if (!(plat_config.misc_flags & FIRMWARE_EXTRA_ONE)) { |
| 894 | memset(&bs_cfg, 0, sizeof(struct boot_stream_config)); |
| 895 | sprintf(bs_cfg.bs_label, "firmware%d", i); |
| 896 | bs_cfg.bs_addr = offset; |
| 897 | bs_cfg.bs_size = size; |
| 898 | bs_cfg.bs_buf = buf; |
| 899 | bs_cfg.need_padding = 1; |
| 900 | |
| 901 | ret = nandbcb_write_bs_skip_bad(boot_cfg, &bs_cfg); |
| 902 | if (ret) |
| 903 | return ret; |
| 904 | |
| 905 | /* update the boot stream size */ |
| 906 | if (i == 0) |
| 907 | boot_cfg->boot_stream1_size = bs_cfg.bs_size; |
| 908 | else |
| 909 | boot_cfg->boot_stream2_size = bs_cfg.bs_size; |
| 910 | |
| 911 | } else { |
| 912 | /* some platforms need extra firmware */ |
| 913 | memset(&bs_cfg, 0, sizeof(struct boot_stream_config)); |
| 914 | sprintf(bs_cfg.bs_label, "fw%d_part%d", i, 1); |
| 915 | bs_cfg.bs_addr = offset; |
| 916 | bs_cfg.bs_size = IMX8MQ_HDMI_FW_SZ; |
| 917 | bs_cfg.bs_buf = buf; |
| 918 | bs_cfg.need_padding = 1; |
| 919 | |
| 920 | ret = nandbcb_write_bs_skip_bad(boot_cfg, &bs_cfg); |
| 921 | if (ret) |
| 922 | return ret; |
| 923 | |
| 924 | /* update the boot stream size */ |
| 925 | if (i == 0) |
| 926 | boot_cfg->boot_stream1_size = bs_cfg.bs_size; |
| 927 | else |
| 928 | boot_cfg->boot_stream2_size = bs_cfg.bs_size; |
| 929 | |
| 930 | /* get next image address */ |
| 931 | next_bs_addr = bs_cfg.next_bs_addr; |
| 932 | |
| 933 | memset(&bs_cfg, 0, sizeof(struct boot_stream_config)); |
| 934 | sprintf(bs_cfg.bs_label, "fw%d_part%d", i, 2); |
| 935 | bs_cfg.bs_addr = next_bs_addr; |
| 936 | bs_cfg.bs_size = IMX8MQ_SPL_SZ; |
| 937 | bs_cfg.bs_buf = (u_char *)(buf + IMX8MQ_HDMI_FW_SZ); |
| 938 | bs_cfg.need_padding = 0; |
| 939 | |
| 940 | ret = nandbcb_write_bs_skip_bad(boot_cfg, &bs_cfg); |
| 941 | if (ret) |
| 942 | return ret; |
| 943 | } |
| 944 | } |
| 945 | |
| 946 | return 0; |
| 947 | } |
| 948 | |
| 949 | static int nandbcb_init(struct boot_config *boot_cfg, u_char *buf) |
| 950 | { |
| 951 | struct mtd_info *mtd; |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 952 | nand_erase_options_t opts; |
| 953 | struct fcb_block *fcb; |
| 954 | struct dbbt_block *dbbt; |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 955 | void *dbbt_page, *dbbt_data_page; |
Igor Opaniuk | 14a489b | 2019-11-03 16:49:45 +0100 | [diff] [blame] | 956 | int ret; |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 957 | loff_t maxsize, off; |
| 958 | |
| 959 | mtd = boot_cfg->mtd; |
| 960 | maxsize = boot_cfg->maxsize; |
| 961 | off = boot_cfg->offset; |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 962 | |
| 963 | /* erase */ |
| 964 | memset(&opts, 0, sizeof(opts)); |
| 965 | opts.offset = off; |
| 966 | opts.length = maxsize - 1; |
| 967 | ret = nand_erase_opts(mtd, &opts); |
| 968 | if (ret) { |
| 969 | printf("%s: erase failed (ret = %d)\n", __func__, ret); |
| 970 | return ret; |
| 971 | } |
| 972 | |
| 973 | /* |
| 974 | * Reference documentation from i.MX6DQRM section 8.5.2.2 |
| 975 | * |
| 976 | * Nand Boot Control Block(BCB) contains two data structures, |
| 977 | * - Firmware Configuration Block(FCB) |
| 978 | * - Discovered Bad Block Table(DBBT) |
| 979 | * |
| 980 | * FCB contains, |
| 981 | * - nand timings |
| 982 | * - DBBT search page address, |
| 983 | * - start page address of primary firmware |
| 984 | * - start page address of secondary firmware |
| 985 | * |
| 986 | * setup fcb: |
| 987 | * - number of blocks = mtd partition size / mtd erasesize |
| 988 | * - two firmware blocks, primary and secondary |
| 989 | * - first 4 block for FCB/DBBT |
| 990 | * - rest split in half for primary and secondary firmware |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 991 | * - same firmware write twice |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 992 | */ |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 993 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 994 | /* write Firmware*/ |
| 995 | ret = nandbcb_write_fw(boot_cfg, buf, FW_ALL); |
| 996 | if (ret) |
| 997 | goto err; |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 998 | |
| 999 | /* fill fcb */ |
| 1000 | fcb = kzalloc(sizeof(*fcb), GFP_KERNEL); |
| 1001 | if (!fcb) { |
| 1002 | debug("failed to allocate fcb\n"); |
| 1003 | ret = -ENOMEM; |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 1004 | return ret; |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 1005 | } |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 1006 | fill_fcb(fcb, boot_cfg); |
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 | ret = write_fcb(boot_cfg, fcb); |
Alice Guo | eaab905 | 2020-05-05 22:04:01 +0800 | [diff] [blame] | 1009 | |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 1010 | /* fill dbbt */ |
| 1011 | dbbt_page = kzalloc(mtd->writesize, GFP_KERNEL); |
| 1012 | if (!dbbt_page) { |
| 1013 | debug("failed to allocate dbbt_page\n"); |
| 1014 | ret = -ENOMEM; |
| 1015 | goto fcb_err; |
| 1016 | } |
| 1017 | |
| 1018 | dbbt_data_page = kzalloc(mtd->writesize, GFP_KERNEL); |
| 1019 | if (!dbbt_data_page) { |
| 1020 | debug("failed to allocate dbbt_data_page\n"); |
| 1021 | ret = -ENOMEM; |
| 1022 | goto dbbt_page_err; |
| 1023 | } |
| 1024 | |
| 1025 | dbbt = dbbt_page; |
| 1026 | dbbt->checksum = 0; |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 1027 | dbbt->fingerprint = DBBT_FINGERPRINT; |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 1028 | dbbt->version = DBBT_VERSION_1; |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 1029 | ret = fill_dbbt_data(mtd, dbbt_data_page, CONV_TO_BLOCKS(maxsize)); |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 1030 | if (ret < 0) |
| 1031 | goto dbbt_data_page_err; |
| 1032 | else if (ret > 0) |
| 1033 | dbbt->dbbtpages = 1; |
| 1034 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 1035 | /* write dbbt */ |
| 1036 | ret = write_dbbt(boot_cfg, dbbt, dbbt_data_page); |
Igor Opaniuk | 14a489b | 2019-11-03 16:49:45 +0100 | [diff] [blame] | 1037 | if (ret < 0) |
| 1038 | printf("failed to write FCB/DBBT\n"); |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 1039 | |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 1040 | dbbt_data_page_err: |
| 1041 | kfree(dbbt_data_page); |
| 1042 | dbbt_page_err: |
| 1043 | kfree(dbbt_page); |
| 1044 | fcb_err: |
| 1045 | kfree(fcb); |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 1046 | err: |
| 1047 | return ret; |
| 1048 | } |
| 1049 | |
Igor Opaniuk | f3caefe | 2019-11-03 16:49:46 +0100 | [diff] [blame] | 1050 | static int do_nandbcb_bcbonly(int argc, char * const argv[]) |
| 1051 | { |
| 1052 | struct fcb_block *fcb; |
| 1053 | struct dbbt_block *dbbt; |
Igor Opaniuk | f3caefe | 2019-11-03 16:49:46 +0100 | [diff] [blame] | 1054 | struct mtd_info *mtd; |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 1055 | nand_erase_options_t opts; |
| 1056 | size_t maxsize; |
| 1057 | loff_t off; |
Igor Opaniuk | f3caefe | 2019-11-03 16:49:46 +0100 | [diff] [blame] | 1058 | void *dbbt_page, *dbbt_data_page; |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 1059 | int ret; |
| 1060 | struct boot_config cfg; |
Igor Opaniuk | f3caefe | 2019-11-03 16:49:46 +0100 | [diff] [blame] | 1061 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 1062 | if (argc < 4) |
| 1063 | return CMD_RET_USAGE; |
| 1064 | |
| 1065 | memset(&cfg, 0, sizeof(struct boot_config)); |
| 1066 | if (nandbcb_get_info(argc, argv, &cfg)) |
Igor Opaniuk | f3caefe | 2019-11-03 16:49:46 +0100 | [diff] [blame] | 1067 | return CMD_RET_FAILURE; |
Igor Opaniuk | f3caefe | 2019-11-03 16:49:46 +0100 | [diff] [blame] | 1068 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 1069 | /* only get the partition info */ |
| 1070 | if (nandbcb_get_size(2, argv, 1, &cfg)) |
| 1071 | return CMD_RET_FAILURE; |
Igor Opaniuk | f3caefe | 2019-11-03 16:49:46 +0100 | [diff] [blame] | 1072 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 1073 | if (nandbcb_set_boot_config(argc, argv, &cfg)) |
Igor Opaniuk | f3caefe | 2019-11-03 16:49:46 +0100 | [diff] [blame] | 1074 | return CMD_RET_FAILURE; |
| 1075 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 1076 | mtd = cfg.mtd; |
Igor Opaniuk | f3caefe | 2019-11-03 16:49:46 +0100 | [diff] [blame] | 1077 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 1078 | cfg.boot_stream1_address = simple_strtoul(argv[2], NULL, 16); |
| 1079 | cfg.boot_stream1_size = simple_strtoul(argv[3], NULL, 16); |
| 1080 | cfg.boot_stream1_size = ALIGN(cfg.boot_stream1_size, mtd->writesize); |
| 1081 | |
| 1082 | if (argc > 5) { |
| 1083 | cfg.boot_stream2_address = simple_strtoul(argv[4], NULL, 16); |
| 1084 | cfg.boot_stream2_size = simple_strtoul(argv[5], NULL, 16); |
| 1085 | cfg.boot_stream2_size = ALIGN(cfg.boot_stream2_size, |
| 1086 | mtd->writesize); |
| 1087 | } |
| 1088 | |
| 1089 | /* sanity check */ |
| 1090 | nandbcb_check_space(&cfg); |
| 1091 | |
| 1092 | maxsize = cfg.maxsize; |
| 1093 | off = cfg.offset; |
| 1094 | |
| 1095 | /* erase the previous FCB/DBBT */ |
| 1096 | memset(&opts, 0, sizeof(opts)); |
| 1097 | opts.offset = off; |
| 1098 | opts.length = g_boot_search_stride * 2; |
| 1099 | ret = nand_erase_opts(mtd, &opts); |
| 1100 | if (ret) { |
| 1101 | printf("%s: erase failed (ret = %d)\n", __func__, ret); |
| 1102 | return CMD_RET_FAILURE; |
| 1103 | } |
Igor Opaniuk | f3caefe | 2019-11-03 16:49:46 +0100 | [diff] [blame] | 1104 | |
| 1105 | /* fill fcb */ |
| 1106 | fcb = kzalloc(sizeof(*fcb), GFP_KERNEL); |
| 1107 | if (!fcb) { |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 1108 | printf("failed to allocate fcb\n"); |
Igor Opaniuk | f3caefe | 2019-11-03 16:49:46 +0100 | [diff] [blame] | 1109 | ret = -ENOMEM; |
| 1110 | return CMD_RET_FAILURE; |
| 1111 | } |
| 1112 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 1113 | fill_fcb(fcb, &cfg); |
| 1114 | |
| 1115 | /* write fcb */ |
| 1116 | ret = write_fcb(&cfg, fcb); |
Igor Opaniuk | f3caefe | 2019-11-03 16:49:46 +0100 | [diff] [blame] | 1117 | |
| 1118 | /* fill dbbt */ |
| 1119 | dbbt_page = kzalloc(mtd->writesize, GFP_KERNEL); |
| 1120 | if (!dbbt_page) { |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 1121 | printf("failed to allocate dbbt_page\n"); |
Igor Opaniuk | f3caefe | 2019-11-03 16:49:46 +0100 | [diff] [blame] | 1122 | ret = -ENOMEM; |
| 1123 | goto fcb_err; |
| 1124 | } |
| 1125 | |
| 1126 | dbbt_data_page = kzalloc(mtd->writesize, GFP_KERNEL); |
| 1127 | if (!dbbt_data_page) { |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 1128 | printf("failed to allocate dbbt_data_page\n"); |
Igor Opaniuk | f3caefe | 2019-11-03 16:49:46 +0100 | [diff] [blame] | 1129 | ret = -ENOMEM; |
| 1130 | goto dbbt_page_err; |
| 1131 | } |
| 1132 | |
| 1133 | dbbt = dbbt_page; |
| 1134 | dbbt->checksum = 0; |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 1135 | dbbt->fingerprint = DBBT_FINGERPRINT; |
Igor Opaniuk | f3caefe | 2019-11-03 16:49:46 +0100 | [diff] [blame] | 1136 | dbbt->version = DBBT_VERSION_1; |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 1137 | ret = fill_dbbt_data(mtd, dbbt_data_page, CONV_TO_BLOCKS(maxsize)); |
Igor Opaniuk | f3caefe | 2019-11-03 16:49:46 +0100 | [diff] [blame] | 1138 | if (ret < 0) |
| 1139 | goto dbbt_data_page_err; |
| 1140 | else if (ret > 0) |
| 1141 | dbbt->dbbtpages = 1; |
| 1142 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 1143 | /* write dbbt */ |
| 1144 | ret = write_dbbt(&cfg, dbbt, dbbt_data_page); |
| 1145 | |
Igor Opaniuk | f3caefe | 2019-11-03 16:49:46 +0100 | [diff] [blame] | 1146 | dbbt_data_page_err: |
| 1147 | kfree(dbbt_data_page); |
| 1148 | dbbt_page_err: |
| 1149 | kfree(dbbt_page); |
| 1150 | fcb_err: |
| 1151 | kfree(fcb); |
| 1152 | |
| 1153 | if (ret < 0) { |
| 1154 | printf("failed to write FCB/DBBT\n"); |
| 1155 | return CMD_RET_FAILURE; |
| 1156 | } |
| 1157 | |
| 1158 | return CMD_RET_SUCCESS; |
| 1159 | } |
| 1160 | |
Alice Guo | eaab905 | 2020-05-05 22:04:01 +0800 | [diff] [blame] | 1161 | /* dump data which is read from NAND chip */ |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 1162 | void dump_structure(struct boot_config *boot_cfg, struct fcb_block *fcb, |
| 1163 | struct dbbt_block *dbbt, void *dbbt_data_page) |
Alice Guo | eaab905 | 2020-05-05 22:04:01 +0800 | [diff] [blame] | 1164 | { |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 1165 | int i; |
| 1166 | struct mtd_info *mtd = boot_cfg->mtd; |
| 1167 | |
| 1168 | #define P1(x) printf(" %s = 0x%08x\n", #x, fcb->x) |
| 1169 | printf("FCB\n"); |
Alice Guo | eaab905 | 2020-05-05 22:04:01 +0800 | [diff] [blame] | 1170 | P1(checksum); |
| 1171 | P1(fingerprint); |
| 1172 | P1(version); |
| 1173 | #undef P1 |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 1174 | #define P1(x) printf(" %s = %d\n", #x, fcb->x) |
Alice Guo | eaab905 | 2020-05-05 22:04:01 +0800 | [diff] [blame] | 1175 | P1(datasetup); |
| 1176 | P1(datahold); |
| 1177 | P1(addr_setup); |
| 1178 | P1(dsample_time); |
| 1179 | P1(pagesize); |
| 1180 | P1(oob_pagesize); |
| 1181 | P1(sectors); |
| 1182 | P1(nr_nand); |
| 1183 | P1(nr_die); |
| 1184 | P1(celltype); |
| 1185 | P1(ecc_type); |
| 1186 | P1(ecc_nr); |
| 1187 | P1(ecc_size); |
| 1188 | P1(ecc_level); |
| 1189 | P1(meta_size); |
| 1190 | P1(nr_blocks); |
| 1191 | P1(ecc_type_sdk); |
| 1192 | P1(ecc_nr_sdk); |
| 1193 | P1(ecc_size_sdk); |
| 1194 | P1(ecc_level_sdk); |
| 1195 | P1(nr_blocks_sdk); |
| 1196 | P1(meta_size_sdk); |
| 1197 | P1(erase_th); |
| 1198 | P1(bootpatch); |
| 1199 | P1(patch_size); |
| 1200 | P1(fw1_start); |
| 1201 | P1(fw2_start); |
| 1202 | P1(fw1_pages); |
| 1203 | P1(fw2_pages); |
| 1204 | P1(dbbt_start); |
| 1205 | P1(bb_byte); |
| 1206 | P1(bb_start_bit); |
| 1207 | P1(phy_offset); |
| 1208 | P1(bchtype); |
| 1209 | P1(readlatency); |
| 1210 | P1(predelay); |
| 1211 | P1(cedelay); |
| 1212 | P1(postdelay); |
| 1213 | P1(cmdaddpause); |
| 1214 | P1(datapause); |
| 1215 | P1(tmspeed); |
| 1216 | P1(busytimeout); |
| 1217 | P1(disbbm); |
| 1218 | P1(spare_offset); |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 1219 | #if !defined(CONFIG_MX6) || defined(CONFIG_MX6SX) || \ |
| 1220 | defined(CONFIG_MX6UL) || defined(CONFIG_MX6ULL) |
Alice Guo | eaab905 | 2020-05-05 22:04:01 +0800 | [diff] [blame] | 1221 | P1(onfi_sync_enable); |
| 1222 | P1(onfi_sync_speed); |
| 1223 | P1(onfi_sync_nand_data); |
| 1224 | P1(disbbm_search); |
| 1225 | P1(disbbm_search_limit); |
| 1226 | P1(read_retry_enable); |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 1227 | #endif |
Alice Guo | eaab905 | 2020-05-05 22:04:01 +0800 | [diff] [blame] | 1228 | #undef P1 |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 1229 | #define P1(x) printf(" %s = 0x%08x\n", #x, dbbt->x) |
| 1230 | printf("DBBT :\n"); |
Alice Guo | eaab905 | 2020-05-05 22:04:01 +0800 | [diff] [blame] | 1231 | P1(checksum); |
| 1232 | P1(fingerprint); |
| 1233 | P1(version); |
| 1234 | #undef P1 |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 1235 | #define P1(x) printf(" %s = %d\n", #x, dbbt->x) |
| 1236 | P1(dbbtpages); |
Alice Guo | eaab905 | 2020-05-05 22:04:01 +0800 | [diff] [blame] | 1237 | #undef P1 |
| 1238 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 1239 | for (i = 0; i < dbbt->dbbtpages; ++i) |
| 1240 | printf("%d ", *((u32 *)(dbbt_data_page + i))); |
| 1241 | |
| 1242 | if (!(plat_config.misc_flags & FIRMWARE_EXTRA_ONE)) { |
| 1243 | printf("Firmware: image #0 @ 0x%x size 0x%x\n", |
| 1244 | fcb->fw1_start, fcb->fw1_pages * mtd->writesize); |
| 1245 | printf("Firmware: image #1 @ 0x%x size 0x%x\n", |
| 1246 | fcb->fw2_start, fcb->fw2_pages * mtd->writesize); |
| 1247 | } else { |
| 1248 | printf("Firmware: image #0 @ 0x%x size 0x%x\n", |
| 1249 | fcb->fw1_start, fcb->fw1_pages * mtd->writesize); |
| 1250 | printf("Firmware: image #1 @ 0x%x size 0x%x\n", |
| 1251 | fcb->fw2_start, fcb->fw2_pages * mtd->writesize); |
| 1252 | /* TODO: Add extra image information */ |
Alice Guo | eaab905 | 2020-05-05 22:04:01 +0800 | [diff] [blame] | 1253 | } |
| 1254 | } |
| 1255 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 1256 | static bool check_fingerprint(void *data, int fingerprint) |
Alice Guo | eaab905 | 2020-05-05 22:04:01 +0800 | [diff] [blame] | 1257 | { |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 1258 | int off = 4; |
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 | return (*(int *)(data + off) == fingerprint); |
| 1261 | } |
Alice Guo | eaab905 | 2020-05-05 22:04:01 +0800 | [diff] [blame] | 1262 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 1263 | static int nandbcb_dump(struct boot_config *boot_cfg) |
| 1264 | { |
| 1265 | int i; |
| 1266 | loff_t off; |
| 1267 | struct mtd_info *mtd = boot_cfg->mtd; |
| 1268 | struct fcb_block fcb, fcb_copy; |
| 1269 | struct dbbt_block dbbt, dbbt_copy; |
| 1270 | void *dbbt_data_page, *dbbt_data_page_copy; |
| 1271 | bool fcb_not_found, dbbt_not_found; |
| 1272 | int ret = 0; |
| 1273 | |
| 1274 | dbbt_data_page = kzalloc(mtd->writesize, GFP_KERNEL); |
| 1275 | if (!dbbt_data_page) { |
| 1276 | printf("failed to allocate dbbt_data_page\n"); |
| 1277 | ret = -ENOMEM; |
| 1278 | return ret; |
Alice Guo | eaab905 | 2020-05-05 22:04:01 +0800 | [diff] [blame] | 1279 | } |
| 1280 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 1281 | dbbt_data_page_copy = kzalloc(mtd->writesize, GFP_KERNEL); |
| 1282 | if (!dbbt_data_page_copy) { |
| 1283 | printf("failed to allocate dbbt_data_page\n"); |
| 1284 | ret = -ENOMEM; |
| 1285 | goto dbbt_page_err; |
| 1286 | } |
Alice Guo | eaab905 | 2020-05-05 22:04:01 +0800 | [diff] [blame] | 1287 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 1288 | /* read fcb */ |
| 1289 | fcb_not_found = 1; |
| 1290 | off = 0; |
| 1291 | for (i = 0; i < g_boot_search_count; ++i) { |
| 1292 | if (fcb_not_found) { |
| 1293 | ret = read_fcb(boot_cfg, &fcb, off); |
Alice Guo | eaab905 | 2020-05-05 22:04:01 +0800 | [diff] [blame] | 1294 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 1295 | if (ret < 0) |
| 1296 | goto dbbt_page_copy_err; |
| 1297 | else if (ret == 1) |
| 1298 | continue; |
| 1299 | else if (ret == 0) |
| 1300 | if (check_fingerprint(&fcb, FCB_FINGERPRINT)) |
| 1301 | fcb_not_found = 0; |
Alice Guo | eaab905 | 2020-05-05 22:04:01 +0800 | [diff] [blame] | 1302 | } else { |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 1303 | ret = read_fcb(boot_cfg, &fcb_copy, off); |
| 1304 | |
| 1305 | if (ret < 0) |
| 1306 | goto dbbt_page_copy_err; |
| 1307 | if (memcmp(&fcb, &fcb_copy, |
| 1308 | sizeof(struct fcb_block))) { |
| 1309 | printf("FCB copies are not identical\n"); |
| 1310 | ret = -EINVAL; |
| 1311 | goto dbbt_page_copy_err; |
| 1312 | } |
Alice Guo | eaab905 | 2020-05-05 22:04:01 +0800 | [diff] [blame] | 1313 | } |
Alice Guo | eaab905 | 2020-05-05 22:04:01 +0800 | [diff] [blame] | 1314 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 1315 | /* next read location */ |
| 1316 | off += g_boot_search_stride; |
| 1317 | } |
Alice Guo | eaab905 | 2020-05-05 22:04:01 +0800 | [diff] [blame] | 1318 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 1319 | /* read dbbt*/ |
| 1320 | dbbt_not_found = 1; |
| 1321 | off = boot_cfg->search_area_size_in_bytes; |
| 1322 | for (i = 0; i < g_boot_search_count; ++i) { |
| 1323 | if (dbbt_not_found) { |
| 1324 | ret = read_dbbt(boot_cfg, &dbbt, dbbt_data_page, off); |
Alice Guo | eaab905 | 2020-05-05 22:04:01 +0800 | [diff] [blame] | 1325 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 1326 | if (ret < 0) |
| 1327 | goto dbbt_page_copy_err; |
| 1328 | else if (ret == 1) |
| 1329 | continue; |
| 1330 | else if (ret == 0) |
| 1331 | if (check_fingerprint(&dbbt, DBBT_FINGERPRINT)) |
| 1332 | dbbt_not_found = 0; |
Alice Guo | eaab905 | 2020-05-05 22:04:01 +0800 | [diff] [blame] | 1333 | } else { |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 1334 | ret = read_dbbt(boot_cfg, &dbbt_copy, |
| 1335 | dbbt_data_page_copy, off); |
| 1336 | |
| 1337 | if (ret < 0) |
| 1338 | goto dbbt_page_copy_err; |
| 1339 | if (memcmp(&dbbt, &dbbt_copy, |
| 1340 | sizeof(struct dbbt_block))) { |
| 1341 | printf("DBBT copies are not identical\n"); |
| 1342 | ret = -EINVAL; |
| 1343 | goto dbbt_page_copy_err; |
| 1344 | } |
| 1345 | if (dbbt.dbbtpages > 0 && |
| 1346 | memcmp(dbbt_data_page, dbbt_data_page_copy, |
| 1347 | mtd->writesize)) { |
| 1348 | printf("DBBT data copies are not identical\n"); |
| 1349 | ret = -EINVAL; |
| 1350 | goto dbbt_page_copy_err; |
| 1351 | } |
Alice Guo | eaab905 | 2020-05-05 22:04:01 +0800 | [diff] [blame] | 1352 | } |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 1353 | |
| 1354 | /* next read location */ |
| 1355 | off += g_boot_search_stride; |
Alice Guo | eaab905 | 2020-05-05 22:04:01 +0800 | [diff] [blame] | 1356 | } |
| 1357 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 1358 | dump_structure(boot_cfg, &fcb, &dbbt, dbbt_data_page); |
| 1359 | |
| 1360 | dbbt_page_copy_err: |
| 1361 | kfree(dbbt_data_page_copy); |
| 1362 | dbbt_page_err: |
| 1363 | kfree(dbbt_data_page); |
| 1364 | |
| 1365 | return ret; |
| 1366 | } |
| 1367 | |
| 1368 | static int do_nandbcb_dump(int argc, char * const argv[]) |
| 1369 | { |
| 1370 | struct boot_config cfg; |
| 1371 | int ret; |
Alice Guo | eaab905 | 2020-05-05 22:04:01 +0800 | [diff] [blame] | 1372 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 1373 | if (argc != 2) |
Alice Guo | eaab905 | 2020-05-05 22:04:01 +0800 | [diff] [blame] | 1374 | return CMD_RET_USAGE; |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 1375 | |
| 1376 | memset(&cfg, 0, sizeof(struct boot_config)); |
| 1377 | if (nandbcb_get_info(argc, argv, &cfg)) |
| 1378 | return CMD_RET_FAILURE; |
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 | if (nandbcb_get_size(argc, argv, 1, &cfg)) |
| 1381 | return CMD_RET_FAILURE; |
Alice Guo | eaab905 | 2020-05-05 22:04:01 +0800 | [diff] [blame] | 1382 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 1383 | if (nandbcb_set_boot_config(argc, argv, &cfg)) |
| 1384 | return CMD_RET_FAILURE; |
| 1385 | |
| 1386 | ret = nandbcb_dump(&cfg); |
| 1387 | if (ret) |
| 1388 | return ret; |
| 1389 | |
| 1390 | return ret; |
Alice Guo | eaab905 | 2020-05-05 22:04:01 +0800 | [diff] [blame] | 1391 | } |
| 1392 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 1393 | static int do_nandbcb_init(int argc, char * const argv[]) |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 1394 | { |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 1395 | u_char *buf; |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 1396 | size_t size; |
| 1397 | loff_t addr; |
| 1398 | char *endp; |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 1399 | int ret; |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 1400 | struct boot_config cfg; |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 1401 | |
| 1402 | if (argc != 4) |
| 1403 | return CMD_RET_USAGE; |
| 1404 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 1405 | memset(&cfg, 0, sizeof(struct boot_config)); |
| 1406 | if (nandbcb_get_info(argc, argv, &cfg)) |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 1407 | return CMD_RET_FAILURE; |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 1408 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 1409 | if (nandbcb_get_size(argc, argv, 2, &cfg)) |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 1410 | return CMD_RET_FAILURE; |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 1411 | size = cfg.boot_stream1_size; |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 1412 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 1413 | if (nandbcb_set_boot_config(argc, argv, &cfg)) |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 1414 | return CMD_RET_FAILURE; |
| 1415 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 1416 | addr = simple_strtoul(argv[1], &endp, 16); |
| 1417 | if (*argv[1] == 0 || *endp != 0) |
| 1418 | return CMD_RET_FAILURE; |
Alice Guo | eaab905 | 2020-05-05 22:04:01 +0800 | [diff] [blame] | 1419 | |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 1420 | buf = map_physmem(addr, size, MAP_WRBACK); |
| 1421 | if (!buf) { |
| 1422 | puts("failed to map physical memory\n"); |
| 1423 | return CMD_RET_FAILURE; |
| 1424 | } |
| 1425 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 1426 | ret = nandbcb_init(&cfg, buf); |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 1427 | |
| 1428 | return ret == 0 ? CMD_RET_SUCCESS : CMD_RET_FAILURE; |
| 1429 | } |
| 1430 | |
| 1431 | static int do_nandbcb(cmd_tbl_t *cmdtp, int flag, int argc, |
| 1432 | char * const argv[]) |
| 1433 | { |
| 1434 | const char *cmd; |
| 1435 | int ret = 0; |
| 1436 | |
Alice Guo | eaab905 | 2020-05-05 22:04:01 +0800 | [diff] [blame] | 1437 | if (argc < 3) |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 1438 | goto usage; |
| 1439 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 1440 | /* check the platform config first */ |
| 1441 | if (is_mx6sx()) { |
| 1442 | plat_config = imx6sx_plat_config; |
| 1443 | } else if (is_mx7()) { |
| 1444 | plat_config = imx7d_plat_config; |
| 1445 | } else if (is_mx6ul() || is_mx6ull()) { |
| 1446 | plat_config = imx6ul_plat_config; |
| 1447 | } else if (is_mx6() && !is_mx6sx() && !is_mx6ul() && !is_mx6ull()) { |
| 1448 | plat_config = imx6qdl_plat_config; |
| 1449 | } else if (is_imx8mq()) { |
| 1450 | plat_config = imx8mq_plat_config; |
| 1451 | } else if (is_imx8mm()) { |
| 1452 | plat_config = imx8mm_plat_config; |
| 1453 | } else if (is_imx8mn()) { |
| 1454 | plat_config = imx8mn_plat_config; |
| 1455 | } else if (is_imx8qm() || is_imx8qxp()) { |
| 1456 | plat_config = imx8q_plat_config; |
| 1457 | } else { |
| 1458 | printf("ERROR: Unknown platform\n"); |
| 1459 | return CMD_RET_FAILURE; |
| 1460 | } |
| 1461 | |
| 1462 | /* TODO: set the boot search count if need to read from fuse */ |
| 1463 | |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 1464 | cmd = argv[1]; |
| 1465 | --argc; |
| 1466 | ++argv; |
| 1467 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 1468 | if (strcmp(cmd, "init") == 0) { |
| 1469 | ret = do_nandbcb_init(argc, argv); |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 1470 | goto done; |
| 1471 | } |
| 1472 | |
Alice Guo | eaab905 | 2020-05-05 22:04:01 +0800 | [diff] [blame] | 1473 | if (strcmp(cmd, "dump") == 0) { |
| 1474 | ret = do_nandbcb_dump(argc, argv); |
| 1475 | goto done; |
| 1476 | } |
| 1477 | |
Igor Opaniuk | f3caefe | 2019-11-03 16:49:46 +0100 | [diff] [blame] | 1478 | if (strcmp(cmd, "bcbonly") == 0) { |
| 1479 | ret = do_nandbcb_bcbonly(argc, argv); |
| 1480 | goto done; |
| 1481 | } |
| 1482 | |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 1483 | done: |
| 1484 | if (ret != -1) |
| 1485 | return ret; |
| 1486 | usage: |
| 1487 | return CMD_RET_USAGE; |
| 1488 | } |
| 1489 | |
Parthiban Nallathambi | b3a4068 | 2019-08-23 18:35:10 +0200 | [diff] [blame] | 1490 | #ifdef CONFIG_SYS_LONGHELP |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 1491 | static char nandbcb_help_text[] = |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 1492 | "init addr off|partition len - update 'len' bytes starting at\n" |
Igor Opaniuk | f3caefe | 2019-11-03 16:49:46 +0100 | [diff] [blame] | 1493 | " 'off|part' to memory address 'addr', skipping bad blocks\n" |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 1494 | "nandbcb bcbonly off|partition fw1-off fw1-size [fw2-off fw2-size]\n" |
| 1495 | " - write BCB only (FCB and DBBT)\n" |
| 1496 | " where `fwx-size` is fw sizes in bytes, `fw1-off`\n" |
Igor Opaniuk | be60431 | 2019-12-16 14:06:44 +0200 | [diff] [blame] | 1497 | " and `fw2-off` - firmware offsets\n" |
| 1498 | " FIY, BCB isn't erased automatically, so mtd erase should\n" |
| 1499 | " be called in advance before writing new BCB:\n" |
Alice Guo | eaab905 | 2020-05-05 22:04:01 +0800 | [diff] [blame] | 1500 | " > mtd erase mx7-bcb\n" |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 1501 | "nandbcb dump off|partition - dump/verify boot structures\n"; |
Parthiban Nallathambi | b3a4068 | 2019-08-23 18:35:10 +0200 | [diff] [blame] | 1502 | #endif |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 1503 | |
Han Xu | 1f314e5 | 2020-05-05 22:04:03 +0800 | [diff] [blame^] | 1504 | U_BOOT_CMD(nandbcb, 7, 1, do_nandbcb, |
| 1505 | "i.MX NAND Boot Control Blocks write", |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 1506 | nandbcb_help_text |
| 1507 | ); |