mtd.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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 = -1;
  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 >= 0)
  67. return (lseek(p->fd, 0, SEEK_SET) == -1);
  68. if (!p->chr)
  69. return -1;
  70. p->fd = mtd_open(p->chr, 0);
  71. if (p->fd < 0) {
  72. ULOG_ERR("Could not open mtd device: %s\n", p->chr);
  73. return -1;
  74. }
  75. if (ioctl(p->fd, MEMGETINFO, &mtdInfo)) {
  76. mtd_volume_close(p);
  77. ULOG_ERR("Could not get MTD device info from %s\n", p->chr);
  78. return -1;
  79. }
  80. v->size = mtdInfo.size;
  81. v->block_size = mtdInfo.erasesize;
  82. switch (mtdInfo.type) {
  83. case MTD_NORFLASH:
  84. v->type = NORFLASH;
  85. break;
  86. case MTD_NANDFLASH:
  87. v->type = NANDFLASH;
  88. break;
  89. case MTD_UBIVOLUME:
  90. v->type = UBIVOLUME;
  91. break;
  92. default:
  93. v->type = UNKNOWN_TYPE;
  94. break;
  95. }
  96. mtdLockInfo.start = 0;
  97. mtdLockInfo.length = v->size;
  98. ioctl(p->fd, MEMUNLOCK, &mtdLockInfo);
  99. return 0;
  100. }
  101. static char* mtd_find_index(char *name)
  102. {
  103. FILE *fp = fopen("/proc/mtd", "r");
  104. static char line[256];
  105. char *index = NULL;
  106. if(!fp)
  107. return index;
  108. while (!index && fgets(line, sizeof(line), fp)) {
  109. char *ret;
  110. if ((ret = strstr(line, name)) && (ret[strlen(name)] == '"')) {
  111. char *eol = strstr(line, ":");
  112. if (!eol)
  113. continue;
  114. *eol = '\0';
  115. index = &line[3];
  116. }
  117. }
  118. fclose(fp);
  119. return index;
  120. }
  121. static struct volume *mtd_volume_find(char *name)
  122. {
  123. char *idx = mtd_find_index(name);
  124. struct mtd_volume *p;
  125. struct volume *v;
  126. char buffer[32];
  127. if (!idx)
  128. return NULL;
  129. p = calloc(1, sizeof(struct mtd_volume));
  130. if (!p)
  131. return NULL;
  132. v = &p->v;
  133. v->name = strdup(name);
  134. v->drv = &mtd_driver;
  135. p->idx = atoi(idx);
  136. p->fd = -1;
  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. size_t sz;
  153. if (mtd_volume_load(p)) {
  154. ULOG_ERR("reading %s failed\n", v->name);
  155. return -1;
  156. }
  157. sz = read(p->fd, &deadc0de, sizeof(deadc0de));
  158. if (sz != sizeof(deadc0de)) {
  159. ULOG_ERR("reading %s failed: %m\n", v->name);
  160. return -1;
  161. }
  162. if (deadc0de == ~0) {
  163. struct mtd_oob_buf oob = {
  164. .start = 0,
  165. .length = sizeof(deadc0de),
  166. .ptr = (void *)&deadc0de,
  167. };
  168. ioctl(p->fd, MEMREADOOB, &oob);
  169. }
  170. if (deadc0de == __be32_to_cpu(0x4f575254))
  171. return FS_SNAPSHOT;
  172. deadc0de = __be32_to_cpu(deadc0de);
  173. if (deadc0de == 0xdeadc0de) {
  174. return FS_DEADCODE;
  175. }
  176. if (__be16_to_cpu(deadc0de) == 0x1985 ||
  177. __be16_to_cpu(deadc0de >> 16) == 0x1985)
  178. return FS_JFFS2;
  179. if (v->type == UBIVOLUME && deadc0de == 0xffffffff) {
  180. return FS_JFFS2;
  181. }
  182. return FS_NONE;
  183. }
  184. static int mtd_volume_erase(struct volume *v, int offset, int len)
  185. {
  186. struct mtd_volume *p = container_of(v, struct mtd_volume, v);;
  187. struct erase_info_user eiu;
  188. int first_block, num_blocks;
  189. if (mtd_volume_load(p))
  190. return -1;
  191. if (offset % v->block_size || len % v->block_size) {
  192. ULOG_ERR("mtd erase needs to be block aligned\n");
  193. return -1;
  194. }
  195. first_block = offset / v->block_size;
  196. num_blocks = len / v->block_size;
  197. eiu.length = v->block_size;
  198. for (eiu.start = first_block * v->block_size;
  199. eiu.start < v->size && eiu.start < (first_block + num_blocks) * v->block_size;
  200. eiu.start += v->block_size) {
  201. ULOG_INFO("erasing %x %x\n", eiu.start, v->block_size);
  202. ioctl(p->fd, MEMUNLOCK, &eiu);
  203. if (ioctl(p->fd, MEMERASE, &eiu))
  204. ULOG_ERR("Failed to erase block at 0x%x\n", eiu.start);
  205. }
  206. mtd_volume_close(p);
  207. return 0;
  208. }
  209. static int mtd_volume_erase_all(struct volume *v)
  210. {
  211. struct mtd_volume *p = container_of(v, struct mtd_volume, v);;
  212. mtd_volume_erase(v, 0, v->size);
  213. mtd_volume_close(p);
  214. return 0;
  215. }
  216. static int mtd_volume_init(struct volume *v)
  217. {
  218. struct mtd_volume *p = container_of(v, struct mtd_volume, v);;
  219. struct mtd_info_user mtdinfo;
  220. int ret;
  221. if (mtd_volume_load(p))
  222. return -1;
  223. ret = ioctl(p->fd, MEMGETINFO, &mtdinfo);
  224. if (ret) {
  225. ULOG_ERR("ioctl(%d, MEMGETINFO) failed: %m\n", p->fd);
  226. } else {
  227. struct erase_info_user mtdlock;
  228. mtdlock.start = 0;
  229. mtdlock.length = mtdinfo.size;
  230. ioctl(p->fd, MEMUNLOCK, &mtdlock);
  231. }
  232. return ret;
  233. }
  234. static int mtd_volume_read(struct volume *v, void *buf, int offset, int length)
  235. {
  236. struct mtd_volume *p = container_of(v, struct mtd_volume, v);;
  237. if (mtd_volume_load(p))
  238. return -1;
  239. if (lseek(p->fd, offset, SEEK_SET) == (off_t) -1) {
  240. ULOG_ERR("lseek/read failed\n");
  241. return -1;
  242. }
  243. if (read(p->fd, buf, length) == -1) {
  244. ULOG_ERR("read failed\n");
  245. return -1;
  246. }
  247. return 0;
  248. }
  249. static int mtd_volume_write(struct volume *v, void *buf, int offset, int length)
  250. {
  251. struct mtd_volume *p = container_of(v, struct mtd_volume, v);;
  252. if (mtd_volume_load(p))
  253. return -1;
  254. if (lseek(p->fd, offset, SEEK_SET) == (off_t) -1) {
  255. ULOG_ERR("lseek/write failed at offset %d\n", offset);
  256. perror("lseek");
  257. return -1;
  258. }
  259. if (write(p->fd, buf, length) == -1) {
  260. ULOG_ERR("write failed\n");
  261. return -1;
  262. }
  263. return 0;
  264. }
  265. static struct driver mtd_driver = {
  266. .name = "mtd",
  267. .priority = 10,
  268. .find = mtd_volume_find,
  269. .init = mtd_volume_init,
  270. .erase = mtd_volume_erase,
  271. .erase_all = mtd_volume_erase_all,
  272. .read = mtd_volume_read,
  273. .write = mtd_volume_write,
  274. .identify = mtd_volume_identify,
  275. };
  276. DRIVER(mtd_driver);