2 * Copyright (C) 2014 John Crispin <blogic@openwrt.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License version 2.1
6 * as published by the Free Software Foundation
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
14 #include <sys/mount.h>
15 #include <sys/types.h>
18 #include <asm/byteorder.h>
22 #include <mtd/mtd-user.h>
24 #include "libfstools.h"
37 static struct driver mtd_driver;
39 static int mtd_open(const char *mtd, int block)
43 int i, ret, flags = O_RDWR | O_SYNC;
45 if ((fp = fopen("/proc/mtd", "r"))) {
46 while (fgets(dev, sizeof(dev), fp)) {
47 if (sscanf(dev, "mtd%d:", &i) && strstr(dev, mtd)) {
48 snprintf(dev, sizeof(dev), "/dev/mtd%s/%d", (block ? "block" : ""), i);
49 ret = open(dev, flags);
51 snprintf(dev, sizeof(dev), "/dev/mtd%s%d", (block ? "block" : ""), i);
52 ret = open(dev, flags);
61 return open(mtd, flags);
64 static void mtd_volume_close(struct mtd_volume *p)
73 static int mtd_volume_load(struct mtd_volume *p)
75 struct volume *v = &p->v;
76 struct mtd_info_user mtdInfo;
77 struct erase_info_user mtdLockInfo;
80 lseek(p->fd, 0, SEEK_SET);
87 p->fd = mtd_open(p->chr, 0);
90 ULOG_ERR("Could not open mtd device: %s\n", p->chr);
94 if (ioctl(p->fd, MEMGETINFO, &mtdInfo)) {
96 ULOG_ERR("Could not get MTD device info from %s\n", p->chr);
100 v->size = mtdInfo.size;
101 v->block_size = mtdInfo.erasesize;
102 switch (mtdInfo.type) {
113 v->type = UNKNOWN_TYPE;
117 mtdLockInfo.start = 0;
118 mtdLockInfo.length = v->size;
119 ioctl(p->fd, MEMUNLOCK, &mtdLockInfo);
124 static char* mtd_find_index(char *name)
126 FILE *fp = fopen("/proc/mtd", "r");
127 static char line[256];
133 while (!index && fgets(line, sizeof(line), fp)) {
136 if ((ret = strstr(line, name)) && (ret[strlen(name)] == '"')) {
137 char *eol = strstr(line, ":");
152 static struct volume *mtd_volume_find(char *name)
154 char *idx = mtd_find_index(name);
155 struct mtd_volume *p;
162 p = calloc(1, sizeof(struct mtd_volume));
167 v->name = strdup(name);
168 v->drv = &mtd_driver;
171 snprintf(buffer, sizeof(buffer), "/dev/mtdblock%s", idx);
172 v->blk = strdup(buffer);
174 snprintf(buffer, sizeof(buffer), "/dev/mtd%s", idx);
175 p->chr = strdup(buffer);
177 if (mtd_volume_load(p)) {
178 ULOG_ERR("reading %s failed\n", v->name);
186 static int mtd_volume_identify(struct volume *v)
188 struct mtd_volume *p = container_of(v, struct mtd_volume, v);;
193 if (mtd_volume_load(p)) {
194 ULOG_ERR("reading %s failed\n", v->name);
198 sz = read(p->fd, &deadc0de, sizeof(deadc0de));
200 if (sz != sizeof(deadc0de)) {
201 ULOG_ERR("reading %s failed: %m\n", v->name);
205 if (deadc0de == __be32_to_cpu(0x4f575254))
208 deadc0de = __be32_to_cpu(deadc0de);
209 if (deadc0de == 0xdeadc0de) {
213 jffs2 = __be16_to_cpu(deadc0de >> 16);
214 if (jffs2 == 0x1985) {
218 if (v->type == UBIVOLUME && deadc0de == 0xffffffff) {
225 static int mtd_volume_erase(struct volume *v, int offset, int len)
227 struct mtd_volume *p = container_of(v, struct mtd_volume, v);;
228 struct erase_info_user eiu;
229 int first_block, num_blocks;
231 if (mtd_volume_load(p))
234 if (offset % v->block_size || len % v->block_size) {
235 ULOG_ERR("mtd erase needs to be block aligned\n");
239 first_block = offset / v->block_size;
240 num_blocks = len / v->block_size;
241 eiu.length = v->block_size;
243 for (eiu.start = first_block * v->block_size;
244 eiu.start < v->size && eiu.start < (first_block + num_blocks) * v->block_size;
245 eiu.start += v->block_size) {
246 ULOG_INFO("erasing %x %x\n", eiu.start, v->block_size);
247 ioctl(p->fd, MEMUNLOCK, &eiu);
248 if (ioctl(p->fd, MEMERASE, &eiu))
249 ULOG_ERR("Failed to erase block at 0x%x\n", eiu.start);
257 static int mtd_volume_erase_all(struct volume *v)
259 struct mtd_volume *p = container_of(v, struct mtd_volume, v);;
261 mtd_volume_erase(v, 0, v->size);
267 static int mtd_volume_init(struct volume *v)
269 struct mtd_volume *p = container_of(v, struct mtd_volume, v);;
270 struct mtd_info_user mtdinfo;
273 if (mtd_volume_load(p))
276 ret = ioctl(p->fd, MEMGETINFO, &mtdinfo);
278 ULOG_ERR("ioctl(%d, MEMGETINFO) failed: %m\n", p->fd);
280 struct erase_info_user mtdlock;
283 mtdlock.length = mtdinfo.size;
284 ioctl(p->fd, MEMUNLOCK, &mtdlock);
290 static int mtd_volume_read(struct volume *v, void *buf, int offset, int length)
292 struct mtd_volume *p = container_of(v, struct mtd_volume, v);;
294 if (mtd_volume_load(p))
297 if (lseek(p->fd, offset, SEEK_SET) == (off_t) -1) {
298 ULOG_ERR("lseek/read failed\n");
302 if (read(p->fd, buf, length) == -1) {
303 ULOG_ERR("read failed\n");
310 static int mtd_volume_write(struct volume *v, void *buf, int offset, int length)
312 struct mtd_volume *p = container_of(v, struct mtd_volume, v);;
314 if (mtd_volume_load(p))
317 if (lseek(p->fd, offset, SEEK_SET) == (off_t) -1) {
318 ULOG_ERR("lseek/write failed at offset %d\n", offset);
323 if (write(p->fd, buf, length) == -1) {
324 ULOG_ERR("write failed\n");
331 static struct driver mtd_driver = {
333 .find = mtd_volume_find,
334 .init = mtd_volume_init,
335 .erase = mtd_volume_erase,
336 .erase_all = mtd_volume_erase_all,
337 .read = mtd_volume_read,
338 .write = mtd_volume_write,
339 .identify = mtd_volume_identify,