blob: 9528a7b4eecb8a099e6b1d0325cc974780febafe [file] [log] [blame]
Patrick Delaunay6015af22019-10-14 09:28:04 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * dfu_mtd.c -- DFU for MTD device.
4 *
5 * Copyright (C) 2019,STMicroelectronics - All Rights Reserved
6 *
7 * Based on dfu_nand.c
8 */
9
10#include <common.h>
11#include <dfu.h>
12#include <mtd.h>
Patrick Delaunayd5640f72019-10-14 09:28:05 +020013#include <jffs2/load_kernel.h>
Patrick Delaunay6015af22019-10-14 09:28:04 +020014
15static bool mtd_is_aligned_with_block_size(struct mtd_info *mtd, u64 size)
16{
17 return !do_div(size, mtd->erasesize);
18}
19
20static int mtd_block_op(enum dfu_op op, struct dfu_entity *dfu,
21 u64 offset, void *buf, long *len)
22{
23 u64 off, lim, remaining;
24 struct mtd_info *mtd = dfu->data.mtd.info;
25 struct mtd_oob_ops io_op = {};
26 int ret = 0;
27 bool has_pages = mtd->type == MTD_NANDFLASH ||
28 mtd->type == MTD_MLCNANDFLASH;
29
30 /* if buf == NULL return total size of the area */
31 if (!buf) {
32 *len = dfu->data.mtd.size;
33 return 0;
34 }
35
36 off = dfu->data.mtd.start + offset + dfu->bad_skip;
37 lim = dfu->data.mtd.start + dfu->data.mtd.size;
38
39 if (off >= lim) {
40 printf("Limit reached 0x%llx\n", lim);
41 *len = 0;
42 return op == DFU_OP_READ ? 0 : -EIO;
43 }
44 /* limit request with the available size */
45 if (off + *len >= lim)
46 *len = lim - off;
47
48 if (!mtd_is_aligned_with_block_size(mtd, off)) {
49 printf("Offset not aligned with a block (0x%x)\n",
50 mtd->erasesize);
51 return 0;
52 }
53
54 /* first erase */
55 if (op == DFU_OP_WRITE) {
56 struct erase_info erase_op = {};
57
58 remaining = round_up(*len, mtd->erasesize);
59 erase_op.mtd = mtd;
60 erase_op.addr = off;
61 erase_op.len = mtd->erasesize;
62 erase_op.scrub = 0;
63
64 while (remaining) {
65 if (erase_op.addr + remaining > lim) {
66 printf("Limit reached 0x%llx while erasing at offset 0x%llx\n",
67 lim, off);
68 return -EIO;
69 }
70
71 ret = mtd_erase(mtd, &erase_op);
72
73 if (ret) {
74 /* Abort if its not a bad block error */
75 if (ret != -EIO) {
76 printf("Failure while erasing at offset 0x%llx\n",
77 erase_op.fail_addr);
78 return 0;
79 }
80 printf("Skipping bad block at 0x%08llx\n",
81 erase_op.addr);
82 } else {
83 remaining -= mtd->erasesize;
84 }
85
86 /* Continue erase behind bad block */
87 erase_op.addr += mtd->erasesize;
88 }
89 }
90
91 io_op.mode = MTD_OPS_AUTO_OOB;
92 io_op.len = *len;
93 if (has_pages && io_op.len > mtd->writesize)
94 io_op.len = mtd->writesize;
95 io_op.ooblen = 0;
96 io_op.datbuf = buf;
97 io_op.oobbuf = NULL;
98
99 /* Loop over to do the actual read/write */
100 remaining = *len;
101 while (remaining) {
102 if (off + remaining > lim) {
103 printf("Limit reached 0x%llx while %s at offset 0x%llx\n",
104 lim, op == DFU_OP_READ ? "reading" : "writing",
105 off);
106 if (op == DFU_OP_READ) {
107 *len -= remaining;
108 return 0;
109 } else {
110 return -EIO;
111 }
112 }
113
114 /* Skip the block if it is bad */
115 if (mtd_is_aligned_with_block_size(mtd, off) &&
116 mtd_block_isbad(mtd, off)) {
117 off += mtd->erasesize;
118 dfu->bad_skip += mtd->erasesize;
119 continue;
120 }
121
122 if (op == DFU_OP_READ)
123 ret = mtd_read_oob(mtd, off, &io_op);
124 else
125 ret = mtd_write_oob(mtd, off, &io_op);
126
127 if (ret) {
128 printf("Failure while %s at offset 0x%llx\n",
129 op == DFU_OP_READ ? "reading" : "writing", off);
130 return -EIO;
131 }
132
133 off += io_op.retlen;
134 remaining -= io_op.retlen;
135 io_op.datbuf += io_op.retlen;
136 io_op.len = remaining;
137 if (has_pages && io_op.len > mtd->writesize)
138 io_op.len = mtd->writesize;
139 }
140
141 return ret;
142}
143
144static int dfu_get_medium_size_mtd(struct dfu_entity *dfu, u64 *size)
145{
146 *size = dfu->data.mtd.info->size;
147
148 return 0;
149}
150
151static int dfu_read_medium_mtd(struct dfu_entity *dfu, u64 offset, void *buf,
152 long *len)
153{
154 int ret = -1;
155
156 switch (dfu->layout) {
157 case DFU_RAW_ADDR:
158 ret = mtd_block_op(DFU_OP_READ, dfu, offset, buf, len);
159 break;
160 default:
161 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
162 dfu_get_layout(dfu->layout));
163 }
164
165 return ret;
166}
167
168static int dfu_write_medium_mtd(struct dfu_entity *dfu,
169 u64 offset, void *buf, long *len)
170{
171 int ret = -1;
172
173 switch (dfu->layout) {
174 case DFU_RAW_ADDR:
175 ret = mtd_block_op(DFU_OP_WRITE, dfu, offset, buf, len);
176 break;
177 default:
178 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
179 dfu_get_layout(dfu->layout));
180 }
181
182 return ret;
183}
184
185static int dfu_flush_medium_mtd(struct dfu_entity *dfu)
186{
Patrick Delaunayd5640f72019-10-14 09:28:05 +0200187 struct mtd_info *mtd = dfu->data.mtd.info;
188 u64 remaining;
189 int ret;
190
191 /* in case of ubi partition, erase rest of the partition */
192 if (dfu->data.nand.ubi) {
193 struct erase_info erase_op = {};
194
195 erase_op.mtd = dfu->data.mtd.info;
196 erase_op.addr = round_up(dfu->data.mtd.start + dfu->offset +
197 dfu->bad_skip, mtd->erasesize);
198 erase_op.len = mtd->erasesize;
199 erase_op.scrub = 0;
200
201 remaining = dfu->data.mtd.start + dfu->data.mtd.size -
202 erase_op.addr;
203
204 while (remaining) {
205 ret = mtd_erase(mtd, &erase_op);
206
207 if (ret) {
208 /* Abort if its not a bad block error */
209 if (ret != -EIO)
210 break;
211 printf("Skipping bad block at 0x%08llx\n",
212 erase_op.addr);
213 }
214
215 /* Skip bad block and continue behind it */
216 erase_op.addr += mtd->erasesize;
217 remaining -= mtd->erasesize;
218 }
219 }
Patrick Delaunay6015af22019-10-14 09:28:04 +0200220 return 0;
221}
222
223static unsigned int dfu_polltimeout_mtd(struct dfu_entity *dfu)
224{
Patrick Delaunayd5640f72019-10-14 09:28:05 +0200225 /*
226 * Currently, Poll Timeout != 0 is only needed on nand
227 * ubi partition, as sectors which are not used need
228 * to be erased
229 */
230 if (dfu->data.nand.ubi)
231 return DFU_MANIFEST_POLL_TIMEOUT;
232
Patrick Delaunay6015af22019-10-14 09:28:04 +0200233 return DFU_DEFAULT_POLL_TIMEOUT;
234}
235
236int dfu_fill_entity_mtd(struct dfu_entity *dfu, char *devstr, char *s)
237{
238 char *st;
239 struct mtd_info *mtd;
240 bool has_pages;
Patrick Delaunayd5640f72019-10-14 09:28:05 +0200241 int ret, part;
Patrick Delaunay6015af22019-10-14 09:28:04 +0200242
243 mtd = get_mtd_device_nm(devstr);
244 if (IS_ERR_OR_NULL(mtd))
245 return -ENODEV;
246 put_mtd_device(mtd);
247
248 dfu->dev_type = DFU_DEV_MTD;
249 dfu->data.mtd.info = mtd;
250
251 has_pages = mtd->type == MTD_NANDFLASH || mtd->type == MTD_MLCNANDFLASH;
252 dfu->max_buf_size = has_pages ? mtd->erasesize : 0;
253
254 st = strsep(&s, " ");
255 if (!strcmp(st, "raw")) {
256 dfu->layout = DFU_RAW_ADDR;
257 dfu->data.mtd.start = simple_strtoul(s, &s, 16);
258 s++;
259 dfu->data.mtd.size = simple_strtoul(s, &s, 16);
Patrick Delaunayd5640f72019-10-14 09:28:05 +0200260 } else if ((!strcmp(st, "part")) || (!strcmp(st, "partubi"))) {
261 char mtd_id[32];
262 struct mtd_device *mtd_dev;
263 u8 part_num;
264 struct part_info *pi;
265
266 dfu->layout = DFU_RAW_ADDR;
267
268 part = simple_strtoul(s, &s, 10);
269
270 sprintf(mtd_id, "%s,%d", devstr, part - 1);
271 printf("using id '%s'\n", mtd_id);
272
273 mtdparts_init();
274
275 ret = find_dev_and_part(mtd_id, &mtd_dev, &part_num, &pi);
276 if (ret != 0) {
277 printf("Could not locate '%s'\n", mtd_id);
278 return -1;
279 }
280
281 dfu->data.mtd.start = pi->offset;
282 dfu->data.mtd.size = pi->size;
283 if (!strcmp(st, "partubi"))
284 dfu->data.mtd.ubi = 1;
Patrick Delaunay6015af22019-10-14 09:28:04 +0200285 } else {
Patrick Delaunayd5640f72019-10-14 09:28:05 +0200286 printf("%s: Memory layout (%s) not supported!\n", __func__, st);
Patrick Delaunay6015af22019-10-14 09:28:04 +0200287 return -1;
288 }
289
Patrick Delaunayd5640f72019-10-14 09:28:05 +0200290 if (!mtd_is_aligned_with_block_size(mtd, dfu->data.mtd.start)) {
291 printf("Offset not aligned with a block (0x%x)\n",
292 mtd->erasesize);
293 return -EINVAL;
294 }
295 if (!mtd_is_aligned_with_block_size(mtd, dfu->data.mtd.size)) {
296 printf("Size not aligned with a block (0x%x)\n",
297 mtd->erasesize);
298 return -EINVAL;
299 }
300
Patrick Delaunay6015af22019-10-14 09:28:04 +0200301 dfu->get_medium_size = dfu_get_medium_size_mtd;
302 dfu->read_medium = dfu_read_medium_mtd;
303 dfu->write_medium = dfu_write_medium_mtd;
304 dfu->flush_medium = dfu_flush_medium_mtd;
305 dfu->poll_timeout = dfu_polltimeout_mtd;
306
307 /* initial state */
308 dfu->inited = 0;
309
310 return 0;
311}