2 * Copyright (C) 2011-2015 Lothar Waßmann <LW@KARO-electronics.de>
4 * See file CREDITS for list of people who contributed to this
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * version 2 as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
23 #include <linux/err.h>
24 #include <jffs2/load_kernel.h>
35 u32 bb_mark_phys_offset;
42 static nand_info_t *mtd = &nand_info[0];
45 static inline int calc_bb_offset(struct mx53_fcb *fcb)
47 int ecc_block_size = 512;
48 int ecc_size = mtd->oobsize / (mtd->writesize / ecc_block_size);
49 int bb_mark_chunk, bb_mark_chunk_offs;
51 bb_mark_chunk = mtd->writesize / (ecc_block_size + ecc_size);
52 bb_mark_chunk_offs = mtd->writesize % (ecc_block_size + ecc_size);
53 if (bb_mark_chunk_offs > ecc_block_size) {
54 printf("Unsupported ECC layout; BB mark resides in ECC data: %u\n",
58 printf("BB mark is in block %d offset %d\n",
59 bb_mark_chunk, bb_mark_chunk_offs);
61 return bb_mark_chunk * ecc_block_size + bb_mark_chunk_offs;
65 * return number of blocks to skip for a contiguous partition
68 static int find_contig_space(int block, int num_blocks, int max_blocks)
72 int last = block + max_blocks;
74 debug("Searching %u contiguous blocks from %d..%d\n",
75 num_blocks, block, block + max_blocks - 1);
76 for (; block < last; block++) {
77 if (nand_block_isbad(mtd, block * mtd->erasesize)) {
80 debug("Skipping %u blocks to %u\n",
84 if (found >= num_blocks) {
85 debug("Found %u good blocks from %d..%d\n",
86 found, block - found + 1, block);
94 #define offset_of(p, m) ((void *)&(p)->m - (void *)(p))
95 #define pr_fcb_val(p, n) debug("%-24s[%02x]=%08x(%d)\n", #n, offset_of(p, n), (p)->n, (p)->n)
97 static struct mx53_fcb *create_fcb(void *buf, int fw1_start_block,
98 int fw2_start_block, int fw_num_blocks)
100 struct mx53_fcb *fcb;
101 u32 sectors_per_block = mtd->erasesize / mtd->writesize;
105 memset(fcb, 0x00, sizeof(*fcb));
106 memset(fcb + 1, 0xff, SZ_1K - sizeof(*fcb));
108 strncpy((char *)&fcb->fingerprint, "FCB ", 4);
111 fcb->fw1_start_page = fw1_start_block * sectors_per_block;
112 pr_fcb_val(fcb, fw1_start_page);
114 if (fw2_start_block != 0 &&
115 fw2_start_block < lldiv(mtd->size, mtd->erasesize)) {
116 fcb->fw2_start_page = fw2_start_block * sectors_per_block;
117 pr_fcb_val(fcb, fw2_start_page);
123 static int find_fcb(void *ref, int page)
126 struct nand_chip *chip = mtd->priv;
127 void *buf = malloc(mtd->erasesize);
132 chip->select_chip(mtd, 0);
133 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
134 ret = chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
136 printf("Failed to read FCB from page %u: %d\n", page, ret);
139 if (memcmp(buf, ref, mtd->writesize) == 0) {
140 debug("Found FCB in page %u (%08x)\n",
141 page, page * mtd->writesize);
145 chip->select_chip(mtd, -1);
150 static int write_fcb(void *buf, int block)
153 struct nand_chip *chip = mtd->priv;
154 int page = block * mtd->erasesize / mtd->writesize;
156 ret = find_fcb(buf, page);
158 printf("FCB at block %d is up to date\n", block);
163 ret = nand_erase(mtd, block * mtd->erasesize, mtd->erasesize);
165 printf("Failed to erase FCB block %u\n", block);
170 printf("Writing FCB to block %d @ %08llx\n", block,
171 (u64)block * mtd->erasesize);
173 chip->select_chip(mtd, 0);
174 ret = chip->write_page(mtd, chip, 0, mtd->writesize,
177 printf("Failed to write FCB to block %u: %d\n", block, ret);
179 chip->select_chip(mtd, -1);
184 #define chk_overlap(a,b) \
185 ((a##_start_block <= b##_end_block && \
186 a##_end_block >= b##_start_block) || \
187 (b##_start_block <= a##_end_block && \
188 b##_end_block >= a##_start_block))
190 #define fail_if_overlap(a,b,m1,m2) do { \
192 printf("check: %s[%lu..%lu] <> %s[%lu..%lu]\n", \
193 m1, a##_start_block, a##_end_block, \
194 m2, b##_start_block, b##_end_block); \
195 if (a##_end_block < a##_start_block) \
196 printf("Invalid start/end block # for %s\n", m1); \
197 if (b##_end_block < b##_start_block) \
198 printf("Invalid start/end block # for %s\n", m2); \
199 if (chk_overlap(a, b)) { \
200 printf("%s blocks %lu..%lu overlap %s in blocks %lu..%lu!\n", \
201 m1, a##_start_block, a##_end_block, \
202 m2, b##_start_block, b##_end_block); \
207 static int tx53_prog_uboot(void *addr, int start_block, int skip,
208 size_t size, size_t max_len)
211 nand_erase_options_t erase_opts = { 0, };
213 size_t prg_length = max_len - skip * mtd->erasesize;
214 int prg_start = start_block * mtd->erasesize;
216 erase_opts.offset = (start_block - skip) * mtd->erasesize;
217 erase_opts.length = max_len;
218 erase_opts.quiet = 1;
220 printf("Erasing flash @ %08llx..%08llx\n", erase_opts.offset,
221 erase_opts.offset + erase_opts.length - 1);
223 ret = nand_erase_opts(mtd, &erase_opts);
225 printf("Failed to erase flash: %d\n", ret);
230 printf("Programming flash @ %08x..%08x from %p\n",
231 prg_start, prg_start + size - 1, addr);
234 ret = nand_write_skip_bad(mtd, prg_start, &actual, NULL,
235 prg_length, addr, 0);
237 printf("Failed to program flash: %d\n", ret);
241 printf("Could only write %u of %u bytes\n", actual, size);
248 #ifdef CONFIG_ENV_IS_IN_NAND
249 #ifndef CONFIG_ENV_OFFSET_REDUND
250 #define TOTAL_ENV_SIZE CONFIG_ENV_RANGE
252 #define TOTAL_ENV_SIZE (CONFIG_ENV_RANGE * 2)
256 int do_update(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
259 const unsigned long fcb_start_block = 0, fcb_end_block = 0;
260 int erase_size = mtd->erasesize;
267 struct mx53_fcb *fcb;
268 unsigned long mtd_num_blocks = lldiv(mtd->size, mtd->erasesize);
269 #ifdef CONFIG_ENV_IS_IN_NAND
270 unsigned long env_start_block = CONFIG_ENV_OFFSET / mtd->erasesize;
271 unsigned long env_end_block = env_start_block +
272 DIV_ROUND_UP(TOTAL_ENV_SIZE, mtd->erasesize) - 1;
276 unsigned long fw1_start_block = 0, fw1_end_block;
277 unsigned long fw2_start_block = 0, fw2_end_block;
278 unsigned long fw_num_blocks;
279 int fw1_skip, fw2_skip;
280 unsigned long extra_blocks = 0;
281 u64 max_len1, max_len2;
282 struct mtd_device *dev;
283 struct part_info *part_info;
284 struct part_info *redund_part_info;
285 const char *uboot_part = "u-boot";
286 const char *redund_part = NULL;
290 ret = mtdparts_init();
295 for (optind = 1; optind < argc; optind++) {
298 if (strcmp(argv[optind], "-f") == 0) {
299 if (optind >= argc - 1) {
300 printf("Option %s requires an argument\n",
305 fw1_start_block = simple_strtoul(argv[optind], &endp, 0);
307 uboot_part = argv[optind];
311 if (fw1_start_block >= mtd_num_blocks) {
312 printf("Block number %lu is out of range: 0..%lu\n",
313 fw1_start_block, mtd_num_blocks - 1);
316 } else if (strcmp(argv[optind], "-r") == 0) {
318 if (optind < argc - 1 && argv[optind + 1][0] != '-') {
320 fw2_start_block = simple_strtoul(argv[optind],
323 redund_part = argv[optind];
326 if (fw2_start_block >= mtd_num_blocks) {
327 printf("Block number %lu is out of range: 0..%lu\n",
333 } else if (strcmp(argv[optind], "-e") == 0) {
334 if (optind >= argc - 1) {
335 printf("Option %s requires an argument\n",
340 extra_blocks = simple_strtoul(argv[optind], NULL, 0);
341 if (extra_blocks >= mtd_num_blocks) {
342 printf("Extra block count %lu is out of range: 0..%lu\n",
347 } else if (strcmp(argv[optind], "-n") == 0) {
349 } else if (argv[optind][0] == '-') {
350 printf("Unrecognized option %s\n", argv[optind]);
357 load_addr = getenv("fileaddr");
358 file_size = getenv("filesize");
360 if (argc - optind < 1 && load_addr == NULL) {
361 printf("Load address not specified\n");
364 if (argc - optind < 2 && file_size == NULL) {
366 printf("WARNING: Image size not specified; overwriting whole '%s' partition\n",
368 printf("This will only work, if there are no bad blocks inside this partition!\n");
370 printf("ERROR: Image size must be specified\n");
376 addr = (void *)simple_strtoul(argv[optind], NULL, 16);
381 size = simple_strtoul(argv[optind], NULL, 16);
384 if (load_addr != NULL) {
385 addr = (void *)simple_strtoul(load_addr, NULL, 16);
386 printf("Using default load address %p\n", addr);
388 if (file_size != NULL) {
389 size = simple_strtoul(file_size, NULL, 16);
390 printf("Using default file size %08x\n", size);
393 fw_num_blocks = DIV_ROUND_UP(size, mtd->erasesize);
398 ret = find_dev_and_part(uboot_part, &dev, &part_num,
401 printf("Failed to find '%s' partition: %d\n",
405 fw1_start_block = lldiv(part_info->offset, mtd->erasesize);
406 max_len1 = part_info->size;
408 fw_num_blocks = lldiv(max_len1, mtd->erasesize);
410 max_len1 = (u64)(fw_num_blocks + extra_blocks) * mtd->erasesize;
414 ret = find_dev_and_part(redund_part, &dev, &redund_part_num,
417 printf("Failed to find '%s' partition: %d\n",
421 fw2_start_block = lldiv(redund_part_info->offset, mtd->erasesize);
422 max_len2 = redund_part_info->size;
423 if (fw2_start_block == fcb_start_block) {
425 max_len2 -= mtd->erasesize;
428 fw_num_blocks = lldiv(max_len2, mtd->erasesize);
429 } else if (fw2_set) {
430 max_len2 = (u64)(fw_num_blocks + extra_blocks) * mtd->erasesize;
435 fw1_skip = find_contig_space(fw1_start_block, fw_num_blocks,
436 lldiv(max_len1, mtd->erasesize));
438 printf("Could not find %lu contiguous good blocks for fw image in blocks %lu..%llu\n",
439 fw_num_blocks, fw1_start_block,
440 fw1_start_block + lldiv(max_len1, mtd->erasesize) - 1);
442 #ifdef CONFIG_ENV_IS_IN_NAND
443 if (part_info->offset <= CONFIG_ENV_OFFSET + TOTAL_ENV_SIZE) {
444 printf("Use a different partition\n");
446 printf("Increase the size of the '%s' partition\n",
450 printf("Increase the size of the '%s' partition\n",
454 printf("Increase the number of spare blocks to use with the '-e' option\n");
459 fw1_end_block = fw1_start_block + fw_num_blocks + extra_blocks - 1;
461 fw1_end_block = fw1_start_block + fw_num_blocks + fw1_skip - 1;
463 if (fw2_set && fw2_start_block == 0)
464 fw2_start_block = fw1_end_block + 1;
465 if (fw2_start_block > 0) {
466 fw2_skip = find_contig_space(fw2_start_block, fw_num_blocks,
467 lldiv(max_len2, mtd->erasesize));
469 printf("Could not find %lu contiguous good blocks for redundant fw image in blocks %lu..%llu\n",
470 fw_num_blocks, fw2_start_block,
471 fw2_start_block + lldiv(max_len2, mtd->erasesize) - 1);
473 printf("Increase the size of the '%s' partition or use a different partition\n",
476 printf("Increase the number of spare blocks to use with the '-e' option\n");
484 fw2_end_block = fw2_start_block + fw_num_blocks + extra_blocks - 1;
486 fw2_end_block = fw2_start_block + fw_num_blocks + fw2_skip - 1;
488 #ifdef CONFIG_ENV_IS_IN_NAND
489 fail_if_overlap(fcb, env, "FCB", "Environment");
490 fail_if_overlap(fw1, env, "FW1", "Environment");
493 fail_if_overlap(fcb, fw2, "FCB", "FW2");
494 #ifdef CONFIG_ENV_IS_IN_NAND
495 fail_if_overlap(fw2, env, "FW2", "Environment");
497 fail_if_overlap(fw1, fw2, "FW1", "FW2");
499 fw1_start_block += fw1_skip;
500 fw2_start_block += fw2_skip;
502 buf_size = fw_num_blocks * erase_size;
503 buf = memalign(erase_size, buf_size);
505 printf("Failed to allocate buffer\n");
509 /* copy U-Boot image to buffer */
510 memcpy(buf, addr, size);
511 memset(buf + size, 0xff, buf_size - size);
513 fcb = create_fcb(buf, fw1_start_block,
514 fw2_start_block, fw_num_blocks);
516 printf("Failed to initialize FCB: %ld\n", PTR_ERR(fcb));
521 if (fw1_start_block == fcb_start_block) {
522 printf("Programming FCB + U-Boot image from %p to block %lu @ %08llx\n",
523 buf, fw1_start_block, (u64)fw1_start_block * mtd->erasesize);
524 ret = tx53_prog_uboot(buf, fw1_start_block, fw1_skip, size,
527 printf("Programming U-Boot image from %p to block %lu @ %08llx\n",
528 buf, fw1_start_block, (u64)fw1_start_block * mtd->erasesize);
529 ret = tx53_prog_uboot(buf, fw1_start_block, fw1_skip, size,
534 memset(buf + sizeof(*fcb), 0xff,
535 mtd->writesize - sizeof(*fcb));
537 ret = write_fcb(buf, fcb_start_block);
539 printf("Failed to write FCB to block %lu\n", fcb_start_block);
542 memset(buf, 0xff, SZ_1K);
544 if (ret || fw2_start_block == 0)
547 printf("Programming redundant U-Boot image to block %lu @ %08llx\n",
548 fw2_start_block, (u64)fw2_start_block * mtd->erasesize);
549 ret = tx53_prog_uboot(buf, fw2_start_block, fw2_skip, size,
556 U_BOOT_CMD(romupdate, 11, 0, do_update,
557 "Creates an FCB data structure and writes an U-Boot image to flash",
558 "[-f {<part>|block#}] [-r [{<part>|block#}]] [-e #] [<address>] [<length>]\n"
559 "\t-f <part>\twrite bootloader image to partition <part>\n"
560 "\t-f #\t\twrite bootloader image at block # (decimal)\n"
561 "\t-r\t\twrite redundant bootloader image at next free block after first image\n"
562 "\t-r <part>\twrite redundant bootloader image to partition <part>\n"
563 "\t-r #\t\twrite redundant bootloader image at block # (decimal)\n"
564 "\t-e #\t\tspecify number of redundant blocks per boot loader image\n"
565 "\t\t\t(only valid if -f or -r specify a flash address rather than a partition name)\n"
566 "\t-n\t\tshow what would be done without actually updating the flash\n"
567 "\t<address>\tRAM address of bootloader image (default: ${fileaddr})\n"
568 "\t<length>\tlength of bootloader image in RAM (default: ${filesize})"