]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - board/karo/tx6/flash.c
karo: tx6: romupdate: remove debug messages
[karo-tx-uboot.git] / board / karo / tx6 / flash.c
1 /*
2  * Copyright (C) 2012,2013 Lothar Waßmann <LW@KARO-electronics.de>
3  *
4  * See file CREDITS for list of people who contributed to this
5  * project.
6  *
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.
10  *
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.
15  *
16  */
17
18 #include <common.h>
19 #include <malloc.h>
20 #include <nand.h>
21 #include <errno.h>
22
23 #include <linux/err.h>
24 #include <jffs2/load_kernel.h>
25
26 #include <asm/io.h>
27 #include <asm/sizes.h>
28 #include <asm/arch/imx-regs.h>
29 #include <asm/imx-common/regs-gpmi.h>
30 #include <asm/imx-common/regs-bch.h>
31
32 #if CONFIG_SYS_NAND_U_BOOT_OFFS < 0x20000
33 #error CONFIG_SYS_NAND_U_BOOT_OFFS must be >= 128kIB
34 #endif
35
36 struct mx6_nand_timing {
37         u8 data_setup;
38         u8 data_hold;
39         u8 address_setup;
40         u8 dsample_time;
41         u8 nand_timing_state;
42         u8 tREA;
43         u8 tRLOH;
44         u8 tRHOH;
45 };
46
47 struct mx6_fcb {
48         u32 checksum;
49         u32 fingerprint;
50         u32 version;
51         struct mx6_nand_timing timing;
52         u32 page_data_size;
53         u32 total_page_size;
54         u32 sectors_per_block;
55         u32 number_of_nands;    /* not used by ROM code */
56         u32 total_internal_die; /* not used by ROM code */
57         u32 cell_type;          /* not used by ROM code */
58         u32 ecc_blockn_type;
59         u32 ecc_block0_size;
60         u32 ecc_blockn_size;
61         u32 ecc_block0_type;
62         u32 metadata_size;
63         u32 ecc_blocks_per_page;
64         u32 rsrvd1[6];          /* not used by ROM code */
65         u32 bch_mode;           /* erase_threshold */
66         u32 rsrvd2[2];
67         u32 fw1_start_page;
68         u32 fw2_start_page;
69         u32 fw1_sectors;
70         u32 fw2_sectors;
71         u32 dbbt_search_area;
72         u32 bb_mark_byte;
73         u32 bb_mark_startbit;
74         u32 bb_mark_phys_offset;
75         u32 bch_type;
76         u32 rsrvd3[8]; /* Toggle NAND timing parameters */
77         u32 disbbm;
78         u32 bb_mark_spare_offset;
79         u32 rsrvd4[9]; /* ONFI NAND parameters */
80         u32 disbb_search;
81 };
82
83 struct mx6_dbbt_header {
84         u32 checksum;
85         u32 fingerprint;
86         u32 version;
87         u32 number_bb;
88         u32 number_pages;
89         u8 spare[492];
90 };
91
92 struct mx6_dbbt {
93         u32 nand_number;
94         u32 number_bb;
95         u32 bb_num[2040 / 4];
96 };
97
98 #define BF_VAL(v, bf)           (((v) & bf##_MASK) >> bf##_OFFSET)
99
100 static nand_info_t *mtd = &nand_info[0];
101
102 extern void *_start;
103
104 #define BIT(v,n)        (((v) >> (n)) & 0x1)
105
106 static inline void memdump(const void *addr, size_t len)
107 {
108         const char *buf = addr;
109         int i;
110
111         for (i = 0; i < len; i++) {
112                 if (i % 16 == 0) {
113                         if (i > 0)
114                                 printf("\n");
115                         printf("%p:", &buf[i]);
116                 }
117                 printf(" %02x", buf[i]);
118         }
119         printf("\n");
120 }
121
122 static u8 calculate_parity_13_8(u8 d)
123 {
124         u8 p = 0;
125
126         p |= (BIT(d, 6) ^ BIT(d, 5) ^ BIT(d, 3) ^ BIT(d, 2))             << 0;
127         p |= (BIT(d, 7) ^ BIT(d, 5) ^ BIT(d, 4) ^ BIT(d, 2) ^ BIT(d, 1)) << 1;
128         p |= (BIT(d, 7) ^ BIT(d, 6) ^ BIT(d, 5) ^ BIT(d, 1) ^ BIT(d, 0)) << 2;
129         p |= (BIT(d, 7) ^ BIT(d, 4) ^ BIT(d, 3) ^ BIT(d, 0))             << 3;
130         p |= (BIT(d, 6) ^ BIT(d, 4) ^ BIT(d, 3) ^ BIT(d, 2) ^ BIT(d, 1) ^ BIT(d, 0)) << 4;
131         return p;
132 }
133
134 static void encode_hamming_13_8(void *_src, void *_ecc, size_t size)
135 {
136         int i;
137         u8 *src = _src;
138         u8 *ecc = _ecc;
139
140         for (i = 0; i < size; i++)
141                 ecc[i] = calculate_parity_13_8(src[i]);
142 }
143
144 static u32 calc_chksum(void *buf, size_t size)
145 {
146         u32 chksum = 0;
147         u8 *bp = buf;
148         size_t i;
149
150         for (i = 0; i < size; i++) {
151                 chksum += bp[i];
152         }
153         return ~chksum;
154 }
155
156 /*
157   Physical organisation of data in NAND flash:
158   metadata
159   payload chunk 0 (may be empty)
160   ecc for metadata + payload chunk 0
161   payload chunk 1
162   ecc for payload chunk 1
163 ...
164   payload chunk n
165   ecc for payload chunk n
166  */
167
168 static inline int calc_bb_offset(nand_info_t *mtd, struct mx6_fcb *fcb)
169 {
170         int bb_mark_offset;
171         int chunk_data_size = fcb->ecc_blockn_size * 8;
172         int chunk_ecc_size = (fcb->ecc_blockn_type << 1) * 13;
173         int chunk_total_size = chunk_data_size + chunk_ecc_size;
174         int bb_mark_chunk, bb_mark_chunk_offs;
175
176         bb_mark_offset = (mtd->writesize - fcb->metadata_size) * 8;
177         if (fcb->ecc_block0_size == 0)
178                 bb_mark_offset -= (fcb->ecc_block0_type << 1) * 13;
179
180         bb_mark_chunk = bb_mark_offset / chunk_total_size;
181         bb_mark_chunk_offs = bb_mark_offset - (bb_mark_chunk * chunk_total_size);
182         if (bb_mark_chunk_offs > chunk_data_size) {
183                 printf("Unsupported ECC layout; BB mark resides in ECC data: %u\n",
184                         bb_mark_chunk_offs);
185                 return -EINVAL;
186         }
187         bb_mark_offset -= bb_mark_chunk * chunk_ecc_size;
188         return bb_mark_offset;
189 }
190
191 static int find_contig_space(int block, int num_blocks, int max_blocks)
192 {
193         int start = block;
194         int found = 0;
195
196         for (; block < block + max_blocks; block++) {
197                 if (nand_block_isbad(mtd, block * mtd->erasesize)) {
198                         found = 0;
199                         printf("Adjusting start block from %u to %u\n",
200                                 start, block + 1);
201                         start = block + 1;
202                 } else {
203                         found++;
204                         if (found >= num_blocks)
205                                 return start;
206                 }
207         }
208         return -ENOSPC;
209 }
210
211 #define pr_fcb_val(p, n)        debug("%s=%08x(%d)\n", #n, (p)->n, (p)->n)
212
213 static struct mx6_fcb *create_fcb(void *buf, int fw1_start_block,
214                                 int fw2_start_block, int fw_num_blocks,
215                                 int max_blocks)
216 {
217         struct gpmi_regs *gpmi_base = (void *)GPMI_BASE_ADDRESS;
218         struct bch_regs *bch_base = (void *)BCH_BASE_ADDRESS;
219         u32 fl0, fl1;
220         u32 t0;
221         int metadata_size;
222         int bb_mark_bit_offs;
223         struct mx6_fcb *fcb;
224         int fcb_offs;
225
226         if (gpmi_base == NULL || bch_base == NULL) {
227                 return ERR_PTR(-ENOMEM);
228         }
229
230         fl0 = readl(&bch_base->hw_bch_flash0layout0);
231         fl1 = readl(&bch_base->hw_bch_flash0layout1);
232         t0 = readl(&gpmi_base->hw_gpmi_timing0);
233
234         metadata_size = BF_VAL(fl0, BCH_FLASHLAYOUT0_META_SIZE);
235
236         fcb = buf + ALIGN(metadata_size, 4);
237         fcb_offs = (void *)fcb - buf;
238
239         memset(buf, 0xff, fcb_offs);
240         memset(fcb, 0x00, sizeof(*fcb));
241         memset(fcb + 1, 0xff, mtd->erasesize - fcb_offs - sizeof(*fcb));
242
243         strncpy((char *)&fcb->fingerprint, "FCB ", 4);
244         fcb->version = cpu_to_be32(1);
245
246         fcb->disbb_search = 0;
247         fcb->disbbm = 1;
248
249         /* ROM code assumes GPMI clock of 25 MHz */
250         fcb->timing.data_setup = BF_VAL(t0, GPMI_TIMING0_DATA_SETUP) * 40;
251         fcb->timing.data_hold = BF_VAL(t0, GPMI_TIMING0_DATA_HOLD) * 40;
252         fcb->timing.address_setup = BF_VAL(t0, GPMI_TIMING0_ADDRESS_SETUP) * 40;
253
254         fcb->page_data_size = mtd->writesize;
255         fcb->total_page_size = mtd->writesize + mtd->oobsize;
256         fcb->sectors_per_block = mtd->erasesize / mtd->writesize;
257
258         fcb->ecc_block0_type = BF_VAL(fl0, BCH_FLASHLAYOUT0_ECC0);
259         fcb->ecc_block0_size = BF_VAL(fl0, BCH_FLASHLAYOUT0_DATA0_SIZE) * 4;
260         fcb->ecc_blockn_type = BF_VAL(fl1, BCH_FLASHLAYOUT1_ECCN);
261         fcb->ecc_blockn_size = BF_VAL(fl1, BCH_FLASHLAYOUT1_DATAN_SIZE) * 4;
262
263         pr_fcb_val(fcb, ecc_block0_type);
264         pr_fcb_val(fcb, ecc_blockn_type);
265         pr_fcb_val(fcb, ecc_block0_size);
266         pr_fcb_val(fcb, ecc_blockn_size);
267
268         fcb->metadata_size = BF_VAL(fl0, BCH_FLASHLAYOUT0_META_SIZE);
269         fcb->ecc_blocks_per_page = BF_VAL(fl0, BCH_FLASHLAYOUT0_NBLOCKS);
270         fcb->bch_mode = readl(&bch_base->hw_bch_mode);
271         fcb->bch_type = 0; /* BCH20 */
272
273         fcb->fw1_start_page = fw1_start_block * fcb->sectors_per_block;
274         fcb->fw1_sectors = fw_num_blocks * fcb->sectors_per_block;
275         pr_fcb_val(fcb, fw1_start_page);
276         pr_fcb_val(fcb, fw1_sectors);
277
278         if (fw2_start_block != 0 && fw2_start_block < mtd->size / mtd->erasesize) {
279                 fcb->fw2_start_page = fw2_start_block * fcb->sectors_per_block;
280                 fcb->fw2_sectors = fw_num_blocks * fcb->sectors_per_block;
281                 pr_fcb_val(fcb, fw2_start_page);
282                 pr_fcb_val(fcb, fw2_sectors);
283         }
284
285         fcb->dbbt_search_area = 0;
286
287         bb_mark_bit_offs = calc_bb_offset(mtd, fcb);
288         if (bb_mark_bit_offs < 0)
289                 return ERR_PTR(bb_mark_bit_offs);
290         fcb->bb_mark_byte = bb_mark_bit_offs / 8;
291         fcb->bb_mark_startbit = bb_mark_bit_offs % 8;
292         fcb->bb_mark_phys_offset = mtd->writesize;
293
294         pr_fcb_val(fcb, bb_mark_byte);
295         pr_fcb_val(fcb, bb_mark_startbit);
296         pr_fcb_val(fcb, bb_mark_phys_offset);
297
298         fcb->checksum = calc_chksum(&fcb->fingerprint, 512 - 4);
299         return fcb;
300 }
301
302 static int find_fcb(void *ref, int page)
303 {
304         int ret = 0;
305         struct nand_chip *chip = mtd->priv;
306         void *buf = malloc(mtd->erasesize);
307
308         if (buf == NULL) {
309                 return -ENOMEM;
310         }
311         chip->select_chip(mtd, 0);
312         chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
313         ret = chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
314         if (ret) {
315                 printf("Failed to read FCB from page %u: %d\n", page, ret);
316                 goto out;
317         }
318         chip->select_chip(mtd, -1);
319         if (memcmp(buf, ref, mtd->writesize) == 0) {
320                 debug("Found FCB in page %u (%08x)\n",
321                         page, page * mtd->writesize);
322                 ret = 1;
323         }
324 out:
325         free(buf);
326         return ret;
327 }
328
329 static int write_fcb(void *buf, int block)
330 {
331         int ret;
332         struct nand_chip *chip = mtd->priv;
333         int page = block * mtd->erasesize / mtd->writesize;
334
335         ret = find_fcb(buf, page);
336         if (ret > 0) {
337                 printf("FCB at block %d is up to date\n", block);
338                 return 0;
339         }
340
341         ret = nand_erase(mtd, block * mtd->erasesize, mtd->erasesize);
342         if (ret) {
343                 printf("Failed to erase FCB block %u\n", block);
344                 return ret;
345         }
346
347         printf("Writing FCB to block %d @ %08x\n", block,
348                 block * mtd->erasesize);
349         chip->select_chip(mtd, 0);
350         ret = chip->write_page(mtd, chip, buf, 1, page, 0, 1);
351         if (ret) {
352                 printf("Failed to write FCB to block %u: %d\n", block, ret);
353         }
354         chip->select_chip(mtd, -1);
355         return ret;
356 }
357
358 struct mx6_ivt {
359         u32 magic;
360         u32 entry;
361         u32 rsrvd1;
362         void *dcd;
363         void *boot_data;
364         void *self;
365         void *csf;
366         u32 rsrvd2;
367 };
368
369 struct mx6_boot_data {
370         u32 start;
371         u32 length;
372         u32 plugin;
373 };
374
375 static int find_ivt(void *buf)
376 {
377         struct mx6_ivt *ivt_hdr = buf + 0x400;
378
379         if ((ivt_hdr->magic & 0xff0000ff) != 0x400000d1)
380                 return 0;
381
382         return 1;
383 }
384
385 static inline void *reloc(void *dst, void *base, void *ptr)
386 {
387         return dst + (ptr - base);
388 }
389
390 static int patch_ivt(void *buf, size_t fsize)
391 {
392         struct mx6_ivt *ivt_hdr = buf + 0x400;
393         struct mx6_boot_data *boot_data;
394
395         if (!find_ivt(buf)) {
396                 printf("No IVT found in image at %p\n", buf);
397                 return -EINVAL;
398         }
399         boot_data = reloc(ivt_hdr, ivt_hdr->self, ivt_hdr->boot_data);
400         boot_data->length = fsize;
401
402         return 0;
403 }
404
405 #define chk_overlap(a,b)                                \
406         ((a##_start_block <= b##_end_block &&           \
407                 a##_end_block >= b##_start_block) ||    \
408         (b##_start_block <= a##_end_block &&            \
409                 b##_end_block >= a##_start_block))
410
411 #define fail_if_overlap(a,b,m1,m2) do {                         \
412         if (chk_overlap(a, b)) {                                \
413                 printf("%s blocks %lu..%lu overlap %s in blocks %lu..%lu!\n", \
414                         m1, a##_start_block, a##_end_block,     \
415                         m2, b##_start_block, b##_end_block);    \
416                 return -EINVAL;                                 \
417         }                                                       \
418 } while (0)
419
420 #ifdef CONFIG_ENV_IS_IN_NAND
421 #ifndef CONFIG_ENV_OFFSET_REDUND
422 #define TOTAL_ENV_SIZE CONFIG_ENV_RANGE
423 #else
424 #define TOTAL_ENV_SIZE (CONFIG_ENV_RANGE * 2)
425 #endif
426 #endif
427
428 #define pr_fcb_offset(n)        printf("%s: %04x (%d)\n", #n, \
429                 offsetof(struct mx6_fcb, n), offsetof(struct mx6_fcb, n))
430
431 int do_update(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
432 {
433         int ret;
434         const unsigned long fcb_start_block = 0, fcb_end_block = 0;
435         int erase_size = mtd->erasesize;
436         int page_size = mtd->writesize;
437         void *buf;
438         char *load_addr;
439         char *file_size;
440         size_t size = 0;
441         void *addr = NULL;
442         struct mx6_fcb *fcb;
443         unsigned long mtd_num_blocks = mtd->size / mtd->erasesize;
444 #ifdef CONFIG_ENV_IS_IN_NAND
445         unsigned long env_start_block = CONFIG_ENV_OFFSET / mtd->erasesize;
446         unsigned long env_end_block = env_start_block +
447                 DIV_ROUND_UP(TOTAL_ENV_SIZE, mtd->erasesize) - 1;
448 #endif
449         int optind;
450         int fw1_set = 0;
451         int fw2_set = 0;
452         unsigned long fw1_start_block = 0, fw1_end_block;
453         unsigned long fw2_start_block = 0, fw2_end_block;
454         unsigned long fw_num_blocks;
455         unsigned long extra_blocks = 2;
456         nand_erase_options_t erase_opts = { 0, };
457         size_t max_len1, max_len2;
458         struct mtd_device *dev;
459         struct part_info *part_info;
460         u8 part_num;
461         size_t actual;
462
463         ret = mtdparts_init();
464         if (ret)
465                 return ret;
466
467         ret = find_dev_and_part("u-boot", &dev, &part_num,
468                                 &part_info);
469         if (ret) {
470                 printf("Failed to find u-boot partition: %d\n", ret);
471                 return ret;
472         }
473
474         for (optind = 1; optind < argc; optind++) {
475                 if (strcmp(argv[optind], "-f") == 0) {
476                         if (optind >= argc - 1) {
477                                 printf("Option %s requires an argument\n",
478                                         argv[optind]);
479                                 return -EINVAL;
480                         }
481                         optind++;
482                         fw1_start_block = simple_strtoul(argv[optind], NULL, 0);
483                         if (fw1_start_block >= mtd_num_blocks) {
484                                 printf("Block number %lu is out of range: 0..%lu\n",
485                                         fw1_start_block, mtd_num_blocks - 1);
486                                 return -EINVAL;
487                         }
488                         fw1_set = 1;
489                 } else if (strcmp(argv[optind], "-r") == 0) {
490                         if (optind < argc - 1 && argv[optind + 1][0] != '-') {
491                                 optind++;
492                                 fw2_start_block = simple_strtoul(argv[optind],
493                                                                 NULL, 0);
494                                 if (fw2_start_block >= mtd_num_blocks) {
495                                         printf("Block number %lu is out of range: 0..%lu\n",
496                                                 fw2_start_block,
497                                                 mtd_num_blocks - 1);
498                                         return -EINVAL;
499                                 }
500                         }
501                         fw2_set = 1;
502                 } else if (strcmp(argv[optind], "-e") == 0) {
503                         if (optind >= argc - 1) {
504                                 printf("Option %s requires an argument\n",
505                                         argv[optind]);
506                                 return -EINVAL;
507                         }
508                         optind++;
509                         extra_blocks = simple_strtoul(argv[optind], NULL, 0);
510                         if (extra_blocks >= mtd_num_blocks) {
511                                 printf("Extra block count %lu is out of range: 0..%lu\n",
512                                         extra_blocks,
513                                         mtd_num_blocks - 1);
514                                 return -EINVAL;
515                         }
516                 } else if (argv[optind][0] == '-') {
517                         printf("Unrecognized option %s\n", argv[optind]);
518                         return -EINVAL;
519                 } else {
520                         break;
521                 }
522         }
523
524         load_addr = getenv("fileaddr");
525         file_size = getenv("filesize");
526
527         if (argc - optind < 1 && load_addr == NULL) {
528                 printf("Load address not specified\n");
529                 return -EINVAL;
530         }
531         if (argc - optind < 2 && file_size == NULL) {
532                 printf("WARNING: Image size not specified; overwriting whole uboot partition\n");
533         }
534         if (argc > optind) {
535                 load_addr = NULL;
536                 addr = (void *)simple_strtoul(argv[optind], NULL, 16);
537                 optind++;
538         }
539         if (argc > optind) {
540                 file_size = NULL;
541                 size = simple_strtoul(argv[optind], NULL, 16);
542                 optind++;
543         }
544         if (load_addr != NULL) {
545                 addr = (void *)simple_strtoul(load_addr, NULL, 16);
546                 printf("Using default load address %p\n", addr);
547         }
548         if (file_size != NULL) {
549                 size = simple_strtoul(file_size, NULL, 16);
550                 printf("Using default file size %08x\n", size);
551         }
552         if (size > 0) {
553                 fw_num_blocks = DIV_ROUND_UP(size, mtd->erasesize);
554         } else {
555                 fw_num_blocks = part_info->size / mtd->erasesize - extra_blocks;
556                 size = fw_num_blocks * mtd->erasesize;
557         }
558         if (!fw1_set)
559                 fw1_start_block = part_info->offset / mtd->erasesize;
560
561         fw1_start_block = find_contig_space(fw1_start_block,
562                                         fw_num_blocks,
563                                         size / mtd->erasesize);
564         if (fw1_start_block < 0) {
565                 printf("Could not find %lu contiguous good blocks for fw image\n",
566                         fw_num_blocks);
567                 return -ENOSPC;
568         }
569         fw1_end_block = fw1_start_block + fw_num_blocks + extra_blocks - 1;
570
571         if (fw2_set && fw2_start_block == 0)
572                 fw2_start_block = fw1_end_block + 1;
573         if (fw2_start_block > 0) {
574                 fw2_start_block = find_contig_space(fw2_start_block,
575                                                 fw_num_blocks,
576                                                 size / mtd->erasesize);
577                 if (fw2_start_block < 0) {
578                         printf("Could not find %lu contiguous good blocks for redundant fw image\n",
579                                 fw_num_blocks);
580                         return -ENOSPC;
581                 }
582         }
583         fw2_end_block = fw2_start_block + fw_num_blocks + extra_blocks - 1;
584
585 #ifdef CONFIG_ENV_IS_IN_NAND
586         fail_if_overlap(fcb, env, "FCB", "Environment");
587         fail_if_overlap(fw1, env, "FW1", "Environment");
588 #endif
589         fail_if_overlap(fcb, fw1, "FCB", "FW1");
590         if (fw2_set) {
591                 fail_if_overlap(fcb, fw2, "FCB", "FW2");
592 #ifdef CONFIG_ENV_IS_IN_NAND
593                 fail_if_overlap(fw2, env, "FW2", "Environment");
594 #endif
595                 fail_if_overlap(fw1, fw2, "FW1", "FW2");
596         }
597
598         buf = malloc(erase_size);
599         if (buf == NULL) {
600                 printf("Failed to allocate buffer\n");
601                 return -ENOMEM;
602         }
603
604         fcb = create_fcb(buf, fw1_start_block, fw2_start_block, fw_num_blocks,
605                         size / mtd->erasesize);
606         if (IS_ERR(fcb)) {
607                 printf("Failed to initialize FCB: %ld\n", PTR_ERR(fcb));
608                 free(buf);
609                 return PTR_ERR(fcb);
610         }
611         encode_hamming_13_8(fcb, (void *)fcb + 512, 512);
612
613         ret = write_fcb(buf, fcb_start_block);
614         free(buf);
615         if (ret) {
616                 printf("Failed to write FCB to block %lu\n", fcb_start_block);
617                 return ret;
618         }
619         ret = patch_ivt(addr, size);
620         if (ret) {
621                 return ret;
622         }
623
624         printf("Programming U-Boot image from %p to block %lu\n",
625                 addr, fw1_start_block);
626         if (size & (page_size - 1)) {
627                 memset(addr + size, 0xff, size & (page_size - 1));
628                 size = ALIGN(size, page_size);
629         }
630
631         erase_opts.offset = fcb->fw1_start_page * page_size;
632         erase_opts.length = (fw1_end_block - fw1_start_block + 1) *
633                 mtd->erasesize;
634         erase_opts.quiet = 1;
635
636         printf("Erasing flash @ %08llx..%08llx\n", erase_opts.offset,
637                 erase_opts.offset + erase_opts.length - 1);
638
639         ret = nand_erase_opts(mtd, &erase_opts);
640         if (ret) {
641                 printf("Failed to erase flash: %d\n", ret);
642                 return ret;
643         }
644
645         max_len1 = size;
646
647         printf("Programming flash @ %08x..%08x from %p\n",
648                 fcb->fw1_start_page * page_size,
649                 fcb->fw1_start_page * page_size + max_len1 - 1, addr);
650         ret = nand_write_skip_bad(mtd, fcb->fw1_start_page * page_size,
651                                 &max_len1, &actual, erase_opts.length, addr,
652                                 WITH_DROP_FFS);
653         if (ret || actual < size) {
654                 printf("Failed to program flash: %d\n", ret);
655                 return ret ?: -EIO;
656         }
657         if (fw2_start_block == 0) {
658                 return ret;
659         }
660
661         printf("Programming redundant U-Boot image to block %lu\n",
662                 fw2_start_block);
663         erase_opts.offset = fcb->fw2_start_page * page_size;
664         erase_opts.length = (fw2_end_block - fw2_start_block + 1) *
665                 mtd->erasesize;
666         printf("Erasing flash @ %08llx..%08llx\n", erase_opts.offset,
667                 erase_opts.offset + erase_opts.length - 1);
668
669         ret = nand_erase_opts(mtd, &erase_opts);
670         if (ret) {
671                 printf("Failed to erase flash: %d\n", ret);
672                 return ret;
673         }
674
675         max_len2 = size;
676
677         printf("Programming flash @ %08x..%08x from %p\n",
678                 fcb->fw2_start_page * page_size,
679                 fcb->fw2_start_page * page_size + max_len2 - 1, addr);
680         ret = nand_write_skip_bad(mtd, fcb->fw2_start_page * page_size,
681                                 &max_len2, &actual, erase_opts.length, addr,
682                                 WITH_DROP_FFS);
683         if (ret || actual < size) {
684                 printf("Failed to program flash: %d\n", ret);
685                 return ret ?: -EIO;
686         }
687         return ret;
688 }
689
690 U_BOOT_CMD(romupdate, 11, 0, do_update,
691         "Creates an FCB data structure and writes an U-Boot image to flash",
692         "[-f #] [-r [#]] [-e #] [<address>] [<length>]\n"
693         "\t-f #\twrite bootloader image at block #\n"
694         "\t-r\twrite redundant bootloader image at next free block after first image\n"
695         "\t-r #\twrite redundant bootloader image at block #\n"
696         "\t-e #\tspecify number of redundant blocks per boot loader image (default 2)\n"
697         "\t<address>\tRAM address of bootloader image (default: ${fileaddr}\n"
698         "\t<length>\tlength of bootloader image in RAM (default: ${filesize}"
699         );