mtd.c 6.7 KB

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