d8637aa108a906d6209903f756d6776e61f6d18c
[project/fstools.git] / libfstools / mtd.c
1 /*
2  * Copyright (C) 2014 John Crispin <blogic@openwrt.org>
3  *
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
7  *
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.
12  */
13
14 #include <sys/mount.h>
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <fcntl.h>
18 #include <asm/byteorder.h>
19 #include <unistd.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <mtd/mtd-user.h>
23
24 #include "libfstools.h"
25
26 #include "volume.h"
27
28 #define PATH_MAX                256
29
30 struct mtd_priv {
31         int     fd;
32         int     idx;
33         char    *chr;
34 };
35
36 static struct driver mtd_driver;
37
38 static int mtd_open(const char *mtd, int block)
39 {
40         FILE *fp;
41         char dev[PATH_MAX];
42         int i, ret, flags = O_RDWR | O_SYNC;
43
44         if ((fp = fopen("/proc/mtd", "r"))) {
45                 while (fgets(dev, sizeof(dev), fp)) {
46                         if (sscanf(dev, "mtd%d:", &i) && strstr(dev, mtd)) {
47                                 snprintf(dev, sizeof(dev), "/dev/mtd%s/%d", (block ? "block" : ""), i);
48                                 ret = open(dev, flags);
49                                 if (ret < 0) {
50                                         snprintf(dev, sizeof(dev), "/dev/mtd%s%d", (block ? "block" : ""), i);
51                                         ret = open(dev, flags);
52                                 }
53                                 fclose(fp);
54                                 return ret;
55                         }
56                 }
57                 fclose(fp);
58         }
59
60         return open(mtd, flags);
61 }
62
63 static void mtd_volume_close(struct volume *v)
64 {
65         struct mtd_priv *p = (struct mtd_priv*) v->priv;
66
67         if (!p->fd)
68                 return;
69
70         close(p->fd);
71         p->fd = 0;
72 }
73
74 static int mtd_volume_load(struct volume *v)
75 {
76         struct mtd_priv *p = (struct mtd_priv*) v->priv;
77         struct mtd_info_user mtdInfo;
78         struct erase_info_user mtdLockInfo;
79
80         if (p->fd)
81                 return 0;
82
83         if (!p->chr)
84                 return -1;
85
86         p->fd = mtd_open(p->chr, 0);
87         if (p->fd < 0) {
88                 p->fd = 0;
89                 ULOG_ERR("Could not open mtd device: %s\n", p->chr);
90                 return -1;
91         }
92
93         if (ioctl(p->fd, MEMGETINFO, &mtdInfo)) {
94                 mtd_volume_close(v);
95                 ULOG_ERR("Could not get MTD device info from %s\n", p->chr);
96                 return -1;
97         }
98
99         v->size = mtdInfo.size;
100         v->block_size = mtdInfo.erasesize;
101         switch (mtdInfo.type) {
102         case MTD_NORFLASH:
103                 v->type = NORFLASH;
104                 break;
105         case MTD_NANDFLASH:
106                 v->type = NANDFLASH;
107                 break;
108         case MTD_UBIVOLUME:
109                 v->type = UBIVOLUME;
110                 break;
111         default:
112                 v->type = UNKNOWN_TYPE;
113                 break;
114         }
115
116         mtdLockInfo.start = 0;
117         mtdLockInfo.length = v->size;
118         ioctl(p->fd, MEMUNLOCK, &mtdLockInfo);
119
120         return 0;
121 }
122
123 static char* mtd_find_index(char *name)
124 {
125         FILE *fp = fopen("/proc/mtd", "r");
126         static char line[256];
127         char *index = NULL;
128
129         if(!fp)
130                 return index;
131
132         while (!index && fgets(line, sizeof(line), fp)) {
133                 char *ret;
134
135                 if ((ret = strstr(line, name)) && (ret[strlen(name)] == '"')) {
136                         char *eol = strstr(line, ":");
137
138                         if (!eol)
139                                 continue;
140
141                         *eol = '\0';
142                         index = &line[3];
143                 }
144         }
145
146         fclose(fp);
147
148         return index;
149 }
150
151 static int mtd_volume_find(struct volume *v, char *name)
152 {
153         char *idx = mtd_find_index(name);
154         struct mtd_priv *p;
155         char buffer[32];
156
157         if (!idx)
158                 return -1;
159
160         p = calloc(1, sizeof(struct mtd_priv));
161         if (!p)
162                 return -1;
163
164         v->priv = p;
165         v->name = strdup(name);
166         v->drv = &mtd_driver;
167         p->idx = atoi(idx);
168
169         snprintf(buffer, sizeof(buffer), "/dev/mtdblock%s", idx);
170         v->blk = strdup(buffer);
171
172         snprintf(buffer, sizeof(buffer), "/dev/mtd%s", idx);
173         p->chr = strdup(buffer);
174
175         if (mtd_volume_load(v)) {
176                 ULOG_ERR("reading %s failed\n", v->name);
177                 return -1;
178         }
179
180         return 0;
181 }
182
183 static int mtd_volume_identify(struct volume *v)
184 {
185         struct mtd_priv *p = (struct mtd_priv*) v->priv;
186         __u32 deadc0de;
187         __u16 jffs2;
188         size_t sz;
189
190         if (mtd_volume_load(v)) {
191                 ULOG_ERR("reading %s failed\n", v->name);
192                 return -1;
193         }
194
195         sz = read(p->fd, &deadc0de, sizeof(deadc0de));
196
197         if (sz != sizeof(deadc0de)) {
198                 ULOG_ERR("reading %s failed: %s\n", v->name, strerror(errno));
199                 return -1;
200         }
201
202         if (deadc0de == __be32_to_cpu(0x4f575254))
203                 return FS_SNAPSHOT;
204
205         deadc0de = __be32_to_cpu(deadc0de);
206         if (deadc0de == 0xdeadc0de) {
207                 ULOG_INFO("jffs2 is not ready - marker found\n");
208                 return FS_DEADCODE;
209         }
210
211         jffs2 = __be16_to_cpu(deadc0de >> 16);
212         if (jffs2 == 0x1985) {
213                 ULOG_INFO("jffs2 is ready\n");
214                 return FS_JFFS2;
215         }
216
217         if (v->type == UBIVOLUME && deadc0de == 0xffffffff) {
218                 ULOG_INFO("jffs2 is ready\n");
219                 return FS_JFFS2;
220         }
221
222         ULOG_INFO("No jffs2 marker was found\n");
223
224         return FS_NONE;
225 }
226
227 static int mtd_volume_erase(struct volume *v, int offset, int len)
228 {
229         struct mtd_priv *p = (struct mtd_priv*) v->priv;
230         struct erase_info_user eiu;
231         int first_block, num_blocks;
232
233         if (mtd_volume_load(v))
234                 return -1;
235
236         if (offset % v->block_size || len % v->block_size) {
237                 ULOG_ERR("mtd erase needs to be block aligned\n");
238                 return -1;
239         }
240
241         first_block = offset / v->block_size;
242         num_blocks = len / v->block_size;
243         eiu.length = v->block_size;
244
245         for (eiu.start = first_block * v->block_size;
246                         eiu.start < v->size && eiu.start < (first_block + num_blocks) * v->block_size;
247                         eiu.start += v->block_size) {
248                 ULOG_INFO("erasing %x %x\n", eiu.start, v->block_size);
249                 ioctl(p->fd, MEMUNLOCK, &eiu);
250                 if (ioctl(p->fd, MEMERASE, &eiu))
251                         ULOG_ERR("Failed to erase block at 0x%x\n", eiu.start);
252         }
253
254         mtd_volume_close(v);
255
256         return 0;
257 }
258
259 static int mtd_volume_erase_all(struct volume *v)
260 {
261         mtd_volume_erase(v, 0, v->size);
262         mtd_volume_close(v);
263
264         return 0;
265 }
266
267 static int mtd_volume_init(struct volume *v)
268 {
269         struct mtd_priv *p = (struct mtd_priv*) v->priv;
270         struct mtd_info_user mtdinfo;
271         int ret;
272
273         if (mtd_volume_load(v))
274                 return -1;
275
276         ret = ioctl(p->fd, MEMGETINFO, &mtdinfo);
277         if (ret) {
278                 ULOG_ERR("ioctl(%d, MEMGETINFO) failed: %s\n", p->fd, strerror(errno));
279         } else {
280                 struct erase_info_user mtdlock;
281
282                 mtdlock.start = 0;
283                 mtdlock.length = mtdinfo.size;
284                 ioctl(p->fd, MEMUNLOCK, &mtdlock);
285         }
286
287         return ret;
288 }
289
290 static int mtd_volume_read(struct volume *v, void *buf, int offset, int length)
291 {
292         struct mtd_priv *p = (struct mtd_priv*) v->priv;
293
294         if (mtd_volume_load(v))
295                 return -1;
296
297         if (lseek(p->fd, offset, SEEK_SET) == (off_t) -1) {
298                 ULOG_ERR("lseek/read failed\n");
299                 return -1;
300         }
301
302         if (read(p->fd, buf, length) == -1) {
303                 ULOG_ERR("read failed\n");
304                 return -1;
305         }
306
307         return 0;
308 }
309
310 static int mtd_volume_write(struct volume *v, void *buf, int offset, int length)
311 {
312         struct mtd_priv *p = (struct mtd_priv*) v->priv;
313
314         if (mtd_volume_load(v))
315                 return -1;
316
317         if (lseek(p->fd, offset, SEEK_SET) == (off_t) -1) {
318                 ULOG_ERR("lseek/write failed at offset %d\n", offset);
319                 perror("lseek");
320                 return -1;
321         }
322
323         if (write(p->fd, buf, length) == -1) {
324                 ULOG_ERR("write failed\n");
325                 return -1;
326         }
327
328         return 0;
329 }
330
331 static struct driver mtd_driver = {
332         .name = "mtd",
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,
340 };
341 DRIVER(mtd_driver);