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_device(const char *dev)
43 ret = open(dev, O_RDWR | O_SYNC);
45 ret = open(dev, O_RDONLY);
50 static int mtd_open(const char *mtd, int block)
56 if ((fp = fopen("/proc/mtd", "r"))) {
57 while (fgets(dev, sizeof(dev), fp)) {
58 if (sscanf(dev, "mtd%d:", &i) && strstr(dev, mtd)) {
59 snprintf(dev, sizeof(dev), "/dev/mtd%s/%d", (block ? "block" : ""), i);
60 ret = mtd_open_device(dev);
62 snprintf(dev, sizeof(dev), "/dev/mtd%s%d", (block ? "block" : ""), i);
63 ret = mtd_open_device(dev);
72 return mtd_open_device(mtd);
75 static void mtd_volume_close(struct mtd_volume *p)
84 static int mtd_volume_load(struct mtd_volume *p)
86 struct volume *v = &p->v;
87 struct mtd_info_user mtdInfo;
88 struct erase_info_user mtdLockInfo;
91 lseek(p->fd, 0, SEEK_SET);
98 p->fd = mtd_open(p->chr, 0);
101 ULOG_ERR("Could not open mtd device: %s\n", p->chr);
105 if (ioctl(p->fd, MEMGETINFO, &mtdInfo)) {
107 ULOG_ERR("Could not get MTD device info from %s\n", p->chr);
111 v->size = mtdInfo.size;
112 v->block_size = mtdInfo.erasesize;
113 switch (mtdInfo.type) {
124 v->type = UNKNOWN_TYPE;
128 mtdLockInfo.start = 0;
129 mtdLockInfo.length = v->size;
130 ioctl(p->fd, MEMUNLOCK, &mtdLockInfo);
135 static char* mtd_find_index(char *name)
137 FILE *fp = fopen("/proc/mtd", "r");
138 static char line[256];
144 while (!index && fgets(line, sizeof(line), fp)) {
147 if ((ret = strstr(line, name)) && (ret[strlen(name)] == '"')) {
148 char *eol = strstr(line, ":");
163 static struct volume *mtd_volume_find(char *name)
165 char *idx = mtd_find_index(name);
166 struct mtd_volume *p;
173 p = calloc(1, sizeof(struct mtd_volume));
178 v->name = strdup(name);
179 v->drv = &mtd_driver;
182 snprintf(buffer, sizeof(buffer), "/dev/mtdblock%s", idx);
183 v->blk = strdup(buffer);
185 snprintf(buffer, sizeof(buffer), "/dev/mtd%s", idx);
186 p->chr = strdup(buffer);
188 if (mtd_volume_load(p)) {
189 ULOG_ERR("reading %s failed\n", v->name);
197 static int mtd_volume_identify(struct volume *v)
199 struct mtd_volume *p = container_of(v, struct mtd_volume, v);;
204 if (mtd_volume_load(p)) {
205 ULOG_ERR("reading %s failed\n", v->name);
209 sz = read(p->fd, &deadc0de, sizeof(deadc0de));
211 if (sz != sizeof(deadc0de)) {
212 ULOG_ERR("reading %s failed: %m\n", v->name);
216 if (deadc0de == __be32_to_cpu(0x4f575254))
219 deadc0de = __be32_to_cpu(deadc0de);
220 if (deadc0de == 0xdeadc0de) {
224 jffs2 = __be16_to_cpu(deadc0de >> 16);
225 if (jffs2 == 0x1985) {
229 if (v->type == UBIVOLUME && deadc0de == 0xffffffff) {
236 static int mtd_volume_erase(struct volume *v, int offset, int len)
238 struct mtd_volume *p = container_of(v, struct mtd_volume, v);;
239 struct erase_info_user eiu;
240 int first_block, num_blocks;
242 if (mtd_volume_load(p))
245 if (offset % v->block_size || len % v->block_size) {
246 ULOG_ERR("mtd erase needs to be block aligned\n");
250 first_block = offset / v->block_size;
251 num_blocks = len / v->block_size;
252 eiu.length = v->block_size;
254 for (eiu.start = first_block * v->block_size;
255 eiu.start < v->size && eiu.start < (first_block + num_blocks) * v->block_size;
256 eiu.start += v->block_size) {
257 ULOG_INFO("erasing %x %x\n", eiu.start, v->block_size);
258 ioctl(p->fd, MEMUNLOCK, &eiu);
259 if (ioctl(p->fd, MEMERASE, &eiu))
260 ULOG_ERR("Failed to erase block at 0x%x\n", eiu.start);
268 static int mtd_volume_erase_all(struct volume *v)
270 struct mtd_volume *p = container_of(v, struct mtd_volume, v);;
272 mtd_volume_erase(v, 0, v->size);
278 static int mtd_volume_init(struct volume *v)
280 struct mtd_volume *p = container_of(v, struct mtd_volume, v);;
281 struct mtd_info_user mtdinfo;
284 if (mtd_volume_load(p))
287 ret = ioctl(p->fd, MEMGETINFO, &mtdinfo);
289 ULOG_ERR("ioctl(%d, MEMGETINFO) failed: %m\n", p->fd);
291 struct erase_info_user mtdlock;
294 mtdlock.length = mtdinfo.size;
295 ioctl(p->fd, MEMUNLOCK, &mtdlock);
301 static int mtd_volume_read(struct volume *v, void *buf, int offset, int length)
303 struct mtd_volume *p = container_of(v, struct mtd_volume, v);;
305 if (mtd_volume_load(p))
308 if (lseek(p->fd, offset, SEEK_SET) == (off_t) -1) {
309 ULOG_ERR("lseek/read failed\n");
313 if (read(p->fd, buf, length) == -1) {
314 ULOG_ERR("read failed\n");
321 static int mtd_volume_write(struct volume *v, void *buf, int offset, int length)
323 struct mtd_volume *p = container_of(v, struct mtd_volume, v);;
325 if (mtd_volume_load(p))
328 if (lseek(p->fd, offset, SEEK_SET) == (off_t) -1) {
329 ULOG_ERR("lseek/write failed at offset %d\n", offset);
334 if (write(p->fd, buf, length) == -1) {
335 ULOG_ERR("write failed\n");
342 static struct driver mtd_driver = {
344 .find = mtd_volume_find,
345 .init = mtd_volume_init,
346 .erase = mtd_volume_erase,
347 .erase_all = mtd_volume_erase_all,
348 .read = mtd_volume_read,
349 .write = mtd_volume_write,
350 .identify = mtd_volume_identify,