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