1 // SPDX-License-Identifier: GPL-2.0+
3 * dfu_mtd.c -- DFU for MTD device.
5 * Copyright (C) 2019,STMicroelectronics - All Rights Reserved
13 #include <jffs2/load_kernel.h>
14 #include <linux/err.h>
16 static bool mtd_is_aligned_with_block_size(struct mtd_info *mtd, u64 size)
18 return !do_div(size, mtd->erasesize);
21 static int mtd_block_op(enum dfu_op op, struct dfu_entity *dfu,
22 u64 offset, void *buf, long *len)
24 u64 off, lim, remaining;
25 struct mtd_info *mtd = dfu->data.mtd.info;
26 struct mtd_oob_ops io_op = {};
28 bool has_pages = mtd->type == MTD_NANDFLASH ||
29 mtd->type == MTD_MLCNANDFLASH;
31 /* if buf == NULL return total size of the area */
33 *len = dfu->data.mtd.size;
37 off = dfu->data.mtd.start + offset + dfu->bad_skip;
38 lim = dfu->data.mtd.start + dfu->data.mtd.size;
41 printf("Limit reached 0x%llx\n", lim);
43 return op == DFU_OP_READ ? 0 : -EIO;
45 /* limit request with the available size */
46 if (off + *len >= lim)
49 if (!mtd_is_aligned_with_block_size(mtd, off)) {
50 printf("Offset not aligned with a block (0x%x)\n",
56 if (op == DFU_OP_WRITE) {
57 struct erase_info erase_op = {};
59 remaining = round_up(*len, mtd->erasesize);
62 erase_op.len = mtd->erasesize;
66 if (erase_op.addr + remaining > lim) {
67 printf("Limit reached 0x%llx while erasing at offset 0x%llx\n",
72 ret = mtd_erase(mtd, &erase_op);
75 /* Abort if its not a bad block error */
77 printf("Failure while erasing at offset 0x%llx\n",
81 printf("Skipping bad block at 0x%08llx\n",
84 remaining -= mtd->erasesize;
87 /* Continue erase behind bad block */
88 erase_op.addr += mtd->erasesize;
92 io_op.mode = MTD_OPS_AUTO_OOB;
94 if (has_pages && io_op.len > mtd->writesize)
95 io_op.len = mtd->writesize;
100 /* Loop over to do the actual read/write */
103 if (off + remaining > lim) {
104 printf("Limit reached 0x%llx while %s at offset 0x%llx\n",
105 lim, op == DFU_OP_READ ? "reading" : "writing",
107 if (op == DFU_OP_READ) {
115 /* Skip the block if it is bad */
116 if (mtd_is_aligned_with_block_size(mtd, off) &&
117 mtd_block_isbad(mtd, off)) {
118 off += mtd->erasesize;
119 dfu->bad_skip += mtd->erasesize;
123 if (op == DFU_OP_READ)
124 ret = mtd_read_oob(mtd, off, &io_op);
126 ret = mtd_write_oob(mtd, off, &io_op);
129 printf("Failure while %s at offset 0x%llx\n",
130 op == DFU_OP_READ ? "reading" : "writing", off);
135 remaining -= io_op.retlen;
136 io_op.datbuf += io_op.retlen;
137 io_op.len = remaining;
138 if (has_pages && io_op.len > mtd->writesize)
139 io_op.len = mtd->writesize;
145 static int dfu_get_medium_size_mtd(struct dfu_entity *dfu, u64 *size)
147 *size = dfu->data.mtd.info->size;
152 static int dfu_read_medium_mtd(struct dfu_entity *dfu, u64 offset, void *buf,
157 switch (dfu->layout) {
159 ret = mtd_block_op(DFU_OP_READ, dfu, offset, buf, len);
162 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
163 dfu_get_layout(dfu->layout));
169 static int dfu_write_medium_mtd(struct dfu_entity *dfu,
170 u64 offset, void *buf, long *len)
174 switch (dfu->layout) {
176 ret = mtd_block_op(DFU_OP_WRITE, dfu, offset, buf, len);
179 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
180 dfu_get_layout(dfu->layout));
186 static int dfu_flush_medium_mtd(struct dfu_entity *dfu)
188 struct mtd_info *mtd = dfu->data.mtd.info;
192 /* in case of ubi partition, erase rest of the partition */
193 if (dfu->data.nand.ubi) {
194 struct erase_info erase_op = {};
196 erase_op.mtd = dfu->data.mtd.info;
197 erase_op.addr = round_up(dfu->data.mtd.start + dfu->offset +
198 dfu->bad_skip, mtd->erasesize);
199 erase_op.len = mtd->erasesize;
202 remaining = dfu->data.mtd.start + dfu->data.mtd.size -
206 ret = mtd_erase(mtd, &erase_op);
209 /* Abort if its not a bad block error */
212 printf("Skipping bad block at 0x%08llx\n",
216 /* Skip bad block and continue behind it */
217 erase_op.addr += mtd->erasesize;
218 remaining -= mtd->erasesize;
224 static unsigned int dfu_polltimeout_mtd(struct dfu_entity *dfu)
227 * Currently, Poll Timeout != 0 is only needed on nand
228 * ubi partition, as sectors which are not used need
231 if (dfu->data.nand.ubi)
232 return DFU_MANIFEST_POLL_TIMEOUT;
234 return DFU_DEFAULT_POLL_TIMEOUT;
237 int dfu_fill_entity_mtd(struct dfu_entity *dfu, char *devstr, char *s)
240 struct mtd_info *mtd;
244 mtd = get_mtd_device_nm(devstr);
245 if (IS_ERR_OR_NULL(mtd))
249 dfu->dev_type = DFU_DEV_MTD;
250 dfu->data.mtd.info = mtd;
252 has_pages = mtd->type == MTD_NANDFLASH || mtd->type == MTD_MLCNANDFLASH;
253 dfu->max_buf_size = has_pages ? mtd->erasesize : 0;
255 st = strsep(&s, " ");
256 if (!strcmp(st, "raw")) {
257 dfu->layout = DFU_RAW_ADDR;
258 dfu->data.mtd.start = simple_strtoul(s, &s, 16);
260 dfu->data.mtd.size = simple_strtoul(s, &s, 16);
261 } else if ((!strcmp(st, "part")) || (!strcmp(st, "partubi"))) {
263 struct mtd_device *mtd_dev;
265 struct part_info *pi;
267 dfu->layout = DFU_RAW_ADDR;
269 part = simple_strtoul(s, &s, 10);
271 sprintf(mtd_id, "%s,%d", devstr, part - 1);
272 printf("using id '%s'\n", mtd_id);
276 ret = find_dev_and_part(mtd_id, &mtd_dev, &part_num, &pi);
278 printf("Could not locate '%s'\n", mtd_id);
282 dfu->data.mtd.start = pi->offset;
283 dfu->data.mtd.size = pi->size;
284 if (!strcmp(st, "partubi"))
285 dfu->data.mtd.ubi = 1;
287 printf("%s: Memory layout (%s) not supported!\n", __func__, st);
291 if (!mtd_is_aligned_with_block_size(mtd, dfu->data.mtd.start)) {
292 printf("Offset not aligned with a block (0x%x)\n",
296 if (!mtd_is_aligned_with_block_size(mtd, dfu->data.mtd.size)) {
297 printf("Size not aligned with a block (0x%x)\n",
302 dfu->get_medium_size = dfu_get_medium_size_mtd;
303 dfu->read_medium = dfu_read_medium_mtd;
304 dfu->write_medium = dfu_write_medium_mtd;
305 dfu->flush_medium = dfu_flush_medium_mtd;
306 dfu->poll_timeout = dfu_polltimeout_mtd;