]> git.sur5r.net Git - u-boot/blob - common/fb_nand.c
fastboot: sparse: resync common/image-sparse.c (part 2)
[u-boot] / common / fb_nand.c
1 /*
2  * Copyright 2014 Broadcom Corporation.
3  * Copyright 2015 Free Electrons.
4  *
5  * SPDX-License-Identifier:     GPL-2.0+
6  */
7
8 #include <config.h>
9 #include <common.h>
10
11 #include <fastboot.h>
12 #include <image-sparse.h>
13
14 #include <linux/mtd/mtd.h>
15 #include <jffs2/jffs2.h>
16 #include <nand.h>
17
18 struct fb_nand_sparse {
19         struct mtd_info         *mtd;
20         struct part_info        *part;
21 };
22
23 __weak int board_fastboot_erase_partition_setup(char *name)
24 {
25         return 0;
26 }
27
28 __weak int board_fastboot_write_partition_setup(char *name)
29 {
30         return 0;
31 }
32
33 static int fb_nand_lookup(const char *partname,
34                           struct mtd_info **mtd,
35                           struct part_info **part)
36 {
37         struct mtd_device *dev;
38         int ret;
39         u8 pnum;
40
41         ret = mtdparts_init();
42         if (ret) {
43                 error("Cannot initialize MTD partitions\n");
44                 fastboot_fail("cannot init mtdparts");
45                 return ret;
46         }
47
48         ret = find_dev_and_part(partname, &dev, &pnum, part);
49         if (ret) {
50                 error("cannot find partition: '%s'", partname);
51                 fastboot_fail("cannot find partition");
52                 return ret;
53         }
54
55         if (dev->id->type != MTD_DEV_TYPE_NAND) {
56                 error("partition '%s' is not stored on a NAND device",
57                       partname);
58                 fastboot_fail("not a NAND device");
59                 return -EINVAL;
60         }
61
62         *mtd = nand_info[dev->id->num];
63
64         return 0;
65 }
66
67 static int _fb_nand_erase(struct mtd_info *mtd, struct part_info *part)
68 {
69         nand_erase_options_t opts;
70         int ret;
71
72         memset(&opts, 0, sizeof(opts));
73         opts.offset = part->offset;
74         opts.length = part->size;
75         opts.quiet = 1;
76
77         printf("Erasing blocks 0x%llx to 0x%llx\n",
78                part->offset, part->offset + part->size);
79
80         ret = nand_erase_opts(mtd, &opts);
81         if (ret)
82                 return ret;
83
84         printf("........ erased 0x%llx bytes from '%s'\n",
85                part->size, part->name);
86
87         return 0;
88 }
89
90 static int _fb_nand_write(struct mtd_info *mtd, struct part_info *part,
91                           void *buffer, unsigned int offset,
92                           unsigned int length, size_t *written)
93 {
94         int flags = WITH_WR_VERIFY;
95
96 #ifdef CONFIG_FASTBOOT_FLASH_NAND_TRIMFFS
97         flags |= WITH_DROP_FFS;
98 #endif
99
100         return nand_write_skip_bad(mtd, offset, &length, written,
101                                    part->size - (offset - part->offset),
102                                    buffer, flags);
103 }
104
105 static lbaint_t fb_nand_sparse_write(struct sparse_storage *info,
106                 lbaint_t blk, lbaint_t blkcnt, const void *buffer)
107 {
108         struct fb_nand_sparse *sparse = info->priv;
109         size_t written;
110         int ret;
111
112         ret = _fb_nand_write(sparse->mtd, sparse->part, (void *)buffer,
113                              blk * info->blksz,
114                              blkcnt * info->blksz, &written);
115         if (ret < 0) {
116                 printf("Failed to write sparse chunk\n");
117                 return ret;
118         }
119
120 /* TODO - verify that the value "written" includes the "bad-blocks" ... */
121
122         /*
123          * the return value must be 'blkcnt' ("good-blocks") plus the
124          * number of "bad-blocks" encountered within this space...
125          */
126         return written / info->blksz;
127 }
128
129 void fb_nand_flash_write(const char *cmd, void *download_buffer,
130                          unsigned int download_bytes)
131 {
132         struct part_info *part;
133         struct mtd_info *mtd = NULL;
134         int ret;
135
136         ret = fb_nand_lookup(cmd, &mtd, &part);
137         if (ret) {
138                 error("invalid NAND device");
139                 fastboot_fail("invalid NAND device");
140                 return;
141         }
142
143         ret = board_fastboot_write_partition_setup(part->name);
144         if (ret)
145                 return;
146
147         if (is_sparse_image(download_buffer)) {
148                 struct fb_nand_sparse sparse_priv;
149                 struct sparse_storage sparse;
150
151                 sparse_priv.mtd = mtd;
152                 sparse_priv.part = part;
153
154                 sparse.blksz = mtd->writesize;
155                 sparse.start = part->offset / sparse.blksz;
156                 sparse.size = part->size / sparse.blksz;
157                 sparse.write = fb_nand_sparse_write;
158
159                 printf("Flashing sparse image at offset " LBAFU "\n",
160                        sparse.start);
161
162                 sparse.priv = &sparse_priv;
163                 write_sparse_image(&sparse, cmd, download_buffer,
164                                    download_bytes);
165         } else {
166                 printf("Flashing raw image at offset 0x%llx\n",
167                        part->offset);
168
169                 ret = _fb_nand_write(mtd, part, download_buffer, part->offset,
170                                      download_bytes, NULL);
171
172                 printf("........ wrote %u bytes to '%s'\n",
173                        download_bytes, part->name);
174         }
175
176         if (ret) {
177                 fastboot_fail("error writing the image");
178                 return;
179         }
180
181         fastboot_okay("");
182 }
183
184 void fb_nand_erase(const char *cmd)
185 {
186         struct part_info *part;
187         struct mtd_info *mtd = NULL;
188         int ret;
189
190         ret = fb_nand_lookup(cmd, &mtd, &part);
191         if (ret) {
192                 error("invalid NAND device");
193                 fastboot_fail("invalid NAND device");
194                 return;
195         }
196
197         ret = board_fastboot_erase_partition_setup(part->name);
198         if (ret)
199                 return;
200
201         ret = _fb_nand_erase(mtd, part);
202         if (ret) {
203                 error("failed erasing from device %s", mtd->name);
204                 fastboot_fail("failed erasing from device");
205                 return;
206         }
207
208         fastboot_okay("");
209 }