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