2 * Copyright (C) 2011-2014 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, mtd->erasesize - 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 && fw2_start_block < mtd->size / mtd->erasesize) {
115 fcb->fw2_start_page = fw2_start_block * sectors_per_block;
116 pr_fcb_val(fcb, fw2_start_page);
122 static int find_fcb(void *ref, int page)
125 struct nand_chip *chip = mtd->priv;
126 void *buf = malloc(mtd->erasesize);
131 chip->select_chip(mtd, 0);
132 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
133 ret = chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
135 printf("Failed to read FCB from page %u: %d\n", page, ret);
138 if (memcmp(buf, ref, mtd->writesize) == 0) {
139 debug("Found FCB in page %u (%08x)\n",
140 page, page * mtd->writesize);
144 chip->select_chip(mtd, -1);
149 static int write_fcb(void *buf, int block)
152 struct nand_chip *chip = mtd->priv;
153 int page = block * mtd->erasesize / mtd->writesize;
155 ret = find_fcb(buf, page);
157 printf("FCB at block %d is up to date\n", block);
161 printf("Erasing FCB block %08x..%08x\n", block * mtd->erasesize,
162 block * mtd->erasesize + mtd->erasesize - 1);
164 ret = nand_erase(mtd, block * mtd->erasesize, mtd->erasesize);
166 printf("Failed to erase FCB block %u\n", block);
171 printf("Writing FCB to block %d @ %08llx\n", block,
172 (u64)block * mtd->erasesize);
174 chip->select_chip(mtd, 0);
175 ret = chip->write_page(mtd, chip, 0, mtd->writesize,
178 printf("Failed to write FCB to block %u: %d\n", block, ret);
180 chip->select_chip(mtd, -1);
185 #define chk_overlap(a,b) \
186 ((a##_start_block <= b##_end_block && \
187 a##_end_block >= b##_start_block) || \
188 (b##_start_block <= a##_end_block && \
189 b##_end_block >= a##_start_block))
191 #define fail_if_overlap(a,b,m1,m2) do { \
193 printf("check: %s[%lu..%lu] <> %s[%lu..%lu]\n", \
194 m1, a##_start_block, a##_end_block, \
195 m2, b##_start_block, b##_end_block); \
196 if (a##_end_block < a##_start_block) \
197 printf("Invalid start/end block # for %s\n", m1); \
198 if (b##_end_block < b##_start_block) \
199 printf("Invalid start/end block # for %s\n", m2); \
200 if (chk_overlap(a, b)) { \
201 printf("%s blocks %lu..%lu overlap %s in blocks %lu..%lu!\n", \
202 m1, a##_start_block, a##_end_block, \
203 m2, b##_start_block, b##_end_block); \
208 static int tx53_prog_uboot(void *addr, int start_block, int skip,
209 size_t size, size_t max_len)
212 nand_erase_options_t erase_opts = { 0, };
214 size_t prg_length = max_len - skip * mtd->erasesize;
215 int prg_start = start_block * mtd->erasesize;
217 erase_opts.offset = (start_block - skip) * mtd->erasesize;
218 erase_opts.length = max_len;
219 erase_opts.quiet = 1;
221 printf("Erasing flash @ %08llx..%08llx\n", erase_opts.offset,
222 erase_opts.offset + erase_opts.length - 1);
224 ret = nand_erase_opts(mtd, &erase_opts);
226 printf("Failed to erase flash: %d\n", ret);
231 printf("Programming flash @ %08x..%08x from %p\n",
232 prg_start, prg_start + size - 1, addr);
235 ret = nand_write_skip_bad(mtd, prg_start, &actual, NULL,
236 prg_length, addr, WITH_DROP_FFS);
238 printf("Failed to program flash: %d\n", ret);
242 printf("Could only write %u of %u bytes\n", actual, size);
249 #ifdef CONFIG_ENV_IS_IN_NAND
250 #ifndef CONFIG_ENV_OFFSET_REDUND
251 #define TOTAL_ENV_SIZE CONFIG_ENV_RANGE
253 #define TOTAL_ENV_SIZE (CONFIG_ENV_RANGE * 2)
257 int do_update(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
260 const unsigned long fcb_start_block = 0, fcb_end_block = 0;
261 int erase_size = mtd->erasesize;
262 int page_size = mtd->writesize;
268 struct mx53_fcb *fcb;
269 unsigned long mtd_num_blocks = mtd->size / mtd->erasesize;
270 #ifdef CONFIG_ENV_IS_IN_NAND
271 unsigned long env_start_block = CONFIG_ENV_OFFSET / mtd->erasesize;
272 unsigned long env_end_block = env_start_block +
273 DIV_ROUND_UP(TOTAL_ENV_SIZE, mtd->erasesize) - 1;
277 unsigned long fw1_start_block = 0, fw1_end_block;
278 unsigned long fw2_start_block = 0, fw2_end_block;
279 unsigned long fw_num_blocks;
280 int fw1_skip, fw2_skip;
281 unsigned long extra_blocks = 0;
282 size_t max_len1, max_len2;
283 struct mtd_device *dev;
284 struct part_info *part_info;
285 struct part_info *redund_part_info;
286 const char *uboot_part = "u-boot";
287 const char *redund_part = NULL;
291 ret = mtdparts_init();
296 for (optind = 1; optind < argc; optind++) {
299 if (strcmp(argv[optind], "-f") == 0) {
300 if (optind >= argc - 1) {
301 printf("Option %s requires an argument\n",
306 fw1_start_block = simple_strtoul(argv[optind], &endp, 0);
308 uboot_part = argv[optind];
312 if (fw1_start_block >= mtd_num_blocks) {
313 printf("Block number %lu is out of range: 0..%lu\n",
314 fw1_start_block, mtd_num_blocks - 1);
317 } else if (strcmp(argv[optind], "-r") == 0) {
319 if (optind < argc - 1 && argv[optind + 1][0] != '-') {
321 fw2_start_block = simple_strtoul(argv[optind],
324 redund_part = argv[optind];
327 if (fw2_start_block >= mtd_num_blocks) {
328 printf("Block number %lu is out of range: 0..%lu\n",
334 } else if (strcmp(argv[optind], "-e") == 0) {
335 if (optind >= argc - 1) {
336 printf("Option %s requires an argument\n",
341 extra_blocks = simple_strtoul(argv[optind], NULL, 0);
342 if (extra_blocks >= mtd_num_blocks) {
343 printf("Extra block count %lu is out of range: 0..%lu\n",
348 } else if (strcmp(argv[optind], "-n") == 0) {
350 } else if (argv[optind][0] == '-') {
351 printf("Unrecognized option %s\n", argv[optind]);
358 load_addr = getenv("fileaddr");
359 file_size = getenv("filesize");
361 if (argc - optind < 1 && load_addr == NULL) {
362 printf("Load address not specified\n");
365 if (argc - optind < 2 && file_size == NULL) {
367 printf("WARNING: Image size not specified; overwriting whole '%s' partition\n",
369 printf("This will only work, if there are no bad blocks inside this partition!\n");
371 printf("ERROR: Image size must be specified\n");
377 addr = (void *)simple_strtoul(argv[optind], NULL, 16);
382 size = simple_strtoul(argv[optind], NULL, 16);
385 if (load_addr != NULL) {
386 addr = (void *)simple_strtoul(load_addr, NULL, 16);
387 printf("Using default load address %p\n", addr);
389 if (file_size != NULL) {
390 size = simple_strtoul(file_size, NULL, 16);
391 printf("Using default file size %08x\n", size);
394 fw_num_blocks = DIV_ROUND_UP(size, mtd->erasesize);
399 ret = find_dev_and_part(uboot_part, &dev, &part_num,
402 printf("Failed to find '%s' partition: %d\n",
406 fw1_start_block = part_info->offset / mtd->erasesize;
407 max_len1 = part_info->size;
409 * Skip one block, if the U-Boot image resides in the
410 * same partition as the FCB
412 if (fw1_start_block == fcb_start_block) {
414 max_len1 -= mtd->erasesize;
417 fw_num_blocks = max_len1 / mtd->erasesize;
419 max_len1 = (fw_num_blocks + extra_blocks) * mtd->erasesize;
423 ret = find_dev_and_part(redund_part, &dev, &redund_part_num,
426 printf("Failed to find '%s' partition: %d\n",
430 fw2_start_block = redund_part_info->offset / mtd->erasesize;
431 max_len2 = redund_part_info->size;
432 if (fw2_start_block == fcb_start_block) {
434 max_len2 -= mtd->erasesize;
437 fw_num_blocks = max_len2 / mtd->erasesize;
438 } else if (fw2_set) {
439 max_len2 = (fw_num_blocks + extra_blocks) * mtd->erasesize;
444 fw1_skip = find_contig_space(fw1_start_block, fw_num_blocks,
445 max_len1 / mtd->erasesize);
447 printf("Could not find %lu contiguous good blocks for fw image in blocks %lu..%lu\n",
448 fw_num_blocks, fw1_start_block,
449 fw1_start_block + max_len1 / mtd->erasesize - 1);
451 #ifdef CONFIG_ENV_IS_IN_NAND
452 if (part_info->offset <= CONFIG_ENV_OFFSET + TOTAL_ENV_SIZE) {
453 printf("Use a different partition\n");
455 printf("Increase the size of the '%s' partition\n",
459 printf("Increase the size of the '%s' partition\n",
463 printf("Increase the number of spare blocks to use with the '-e' option\n");
468 fw1_end_block = fw1_start_block + fw_num_blocks + extra_blocks - 1;
470 fw1_end_block = fw1_start_block + fw_num_blocks + fw1_skip - 1;
472 if (fw2_set && fw2_start_block == 0)
473 fw2_start_block = fw1_end_block + 1;
474 if (fw2_start_block > 0) {
475 fw2_skip = find_contig_space(fw2_start_block, fw_num_blocks,
476 max_len2 / mtd->erasesize);
478 printf("Could not find %lu contiguous good blocks for redundant fw image in blocks %lu..%lu\n",
479 fw_num_blocks, fw2_start_block,
480 fw2_start_block + max_len2 / mtd->erasesize - 1);
482 printf("Increase the size of the '%s' partition or use a different partition\n",
485 printf("Increase the number of spare blocks to use with the '-e' option\n");
493 fw2_end_block = fw2_start_block + fw_num_blocks + extra_blocks - 1;
495 fw2_end_block = fw2_start_block + fw_num_blocks + fw2_skip - 1;
497 #ifdef CONFIG_ENV_IS_IN_NAND
498 fail_if_overlap(fcb, env, "FCB", "Environment");
499 fail_if_overlap(fw1, env, "FW1", "Environment");
501 fail_if_overlap(fw1, fcb, "FW1", "FCB");
503 fail_if_overlap(fw2, fcb, "FW2", "FCB");
504 #ifdef CONFIG_ENV_IS_IN_NAND
505 fail_if_overlap(fw2, env, "FW2", "Environment");
507 fail_if_overlap(fw1, fw2, "FW1", "FW2");
509 fw1_start_block += fw1_skip;
510 fw2_start_block += fw2_skip;
512 buf = malloc(erase_size);
514 printf("Failed to allocate buffer\n");
518 fcb = create_fcb(buf, fw1_start_block,
519 fw2_start_block, fw_num_blocks);
521 printf("Failed to initialize FCB: %ld\n", PTR_ERR(fcb));
526 ret = write_fcb(buf, fcb_start_block);
529 printf("Failed to write FCB to block %lu\n", fcb_start_block);
533 if (size & (page_size - 1)) {
534 memset(addr + size, 0xff, size & (page_size - 1));
535 size = ALIGN(size, page_size);
538 printf("Programming U-Boot image from %p to block %lu @ %08llx\n",
539 addr, fw1_start_block, (u64)fw1_start_block * mtd->erasesize);
540 ret = tx53_prog_uboot(addr, fw1_start_block, fw1_skip, size,
542 if (ret || fw2_start_block == 0)
545 printf("Programming redundant U-Boot image to block %lu @ %08llx\n",
546 fw2_start_block, (u64)fw2_start_block * mtd->erasesize);
547 ret = tx53_prog_uboot(addr, fw2_start_block, fw2_skip, size,
552 U_BOOT_CMD(romupdate, 11, 0, do_update,
553 "Creates an FCB data structure and writes an U-Boot image to flash",
554 "[-f {<part>|block#}] [-r [{<part>|block#}]] [-e #] [<address>] [<length>]\n"
555 "\t-f <part>\twrite bootloader image to partition <part>\n"
556 "\t-f #\t\twrite bootloader image at block # (decimal)\n"
557 "\t-r\t\twrite redundant bootloader image at next free block after first image\n"
558 "\t-r <part>\twrite redundant bootloader image to partition <part>\n"
559 "\t-r #\t\twrite redundant bootloader image at block # (decimal)\n"
560 "\t-e #\t\tspecify number of redundant blocks per boot loader image\n"
561 "\t\t\t(only valid if -f or -r specify a flash address rather than a partition name)\n"
562 "\t-n\t\tshow what would be done without actually updating the flash\n"
563 "\t<address>\tRAM address of bootloader image (default: ${fileaddr})\n"
564 "\t<length>\tlength of bootloader image in RAM (default: ${filesize})"