mtd.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*
  2. * mtd - simple memory technology device manipulation tool
  3. *
  4. * Copyright (c) 2006, 2007 Thorsten Glaser <tg@freewrt.org>
  5. * Copyright (C) 2005 Waldemar Brodkorb <wbx@freewrt.org>,
  6. * Felix Fietkau <nbd@openwrt.org>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. *
  22. * The code is based on the linux-mtd examples.
  23. */
  24. #include <sys/param.h>
  25. #include <sys/types.h>
  26. #include <sys/ioctl.h>
  27. #include <sys/mount.h>
  28. #include <sys/stat.h>
  29. #include <sys/ioctl.h>
  30. #include <sys/syscall.h>
  31. #include <limits.h>
  32. #include <unistd.h>
  33. #include <stdbool.h>
  34. #include <stdlib.h>
  35. #include <stdio.h>
  36. #include <stdint.h>
  37. #include <fcntl.h>
  38. #include <errno.h>
  39. #include <err.h>
  40. #include <time.h>
  41. #include <string.h>
  42. #include <mtd/mtd-user.h>
  43. #include <linux/reboot.h>
  44. #define BUFSIZE (16 * 1024)
  45. #define MAX_ARGS 8
  46. #define DEBUG
  47. int mtd_check(char *);
  48. int mtd_unlock(const char *);
  49. int mtd_open(const char *, int);
  50. int mtd_erase(const char *);
  51. int mtd_write(int, const char *, int, bool);
  52. void usage(void) __attribute__((noreturn));
  53. char buf[BUFSIZE];
  54. int buflen;
  55. int
  56. mtd_check(char *mtd)
  57. {
  58. struct mtd_info_user mtdInfo;
  59. int fd;
  60. fd = mtd_open(mtd, O_RDWR | O_SYNC);
  61. if(fd < 0) {
  62. fprintf(stderr, "Could not open mtd device: %s\n", mtd);
  63. return 0;
  64. }
  65. if(ioctl(fd, MEMGETINFO, &mtdInfo)) {
  66. fprintf(stderr, "Could not get MTD device info from %s\n", mtd);
  67. close(fd);
  68. return 0;
  69. }
  70. close(fd);
  71. return 1;
  72. }
  73. int
  74. mtd_unlock(const char *mtd)
  75. {
  76. int fd;
  77. struct mtd_info_user mtdInfo;
  78. struct erase_info_user mtdLockInfo;
  79. fd = mtd_open(mtd, O_RDWR | O_SYNC);
  80. if(fd < 0) {
  81. fprintf(stderr, "Could not open mtd device: %s\n", mtd);
  82. exit(1);
  83. }
  84. if(ioctl(fd, MEMGETINFO, &mtdInfo)) {
  85. fprintf(stderr, "Could not get MTD device info from %s\n", mtd);
  86. close(fd);
  87. exit(1);
  88. }
  89. mtdLockInfo.start = 0;
  90. mtdLockInfo.length = mtdInfo.size;
  91. if(ioctl(fd, MEMUNLOCK, &mtdLockInfo)) {
  92. close(fd);
  93. return 0;
  94. }
  95. close(fd);
  96. return 0;
  97. }
  98. int
  99. mtd_open(const char *mtd, int flags)
  100. {
  101. FILE *fp;
  102. char dev[PATH_MAX];
  103. int i;
  104. if ((fp = fopen("/proc/mtd", "r"))) {
  105. while (fgets(dev, sizeof(dev), fp)) {
  106. if (sscanf(dev, "mtd%d:", &i) && strstr(dev, mtd)) {
  107. snprintf(dev, sizeof(dev), "/dev/mtd%d", i);
  108. fclose(fp);
  109. return open(dev, flags);
  110. }
  111. }
  112. fclose(fp);
  113. }
  114. return open(mtd, flags);
  115. }
  116. int
  117. mtd_erase(const char *mtd)
  118. {
  119. int fd;
  120. struct mtd_info_user mtdInfo;
  121. struct erase_info_user mtdEraseInfo;
  122. fd = mtd_open(mtd, O_RDWR | O_SYNC);
  123. if(fd < 0) {
  124. fprintf(stderr, "Could not open mtd device: %s\n", mtd);
  125. exit(1);
  126. }
  127. if(ioctl(fd, MEMGETINFO, &mtdInfo)) {
  128. fprintf(stderr, "Could not get MTD device info from %s\n", mtd);
  129. close(fd);
  130. exit(1);
  131. }
  132. mtdEraseInfo.length = mtdInfo.erasesize;
  133. for (mtdEraseInfo.start = 0;
  134. mtdEraseInfo.start < mtdInfo.size;
  135. mtdEraseInfo.start += mtdInfo.erasesize) {
  136. ioctl(fd, MEMUNLOCK, &mtdEraseInfo);
  137. if(ioctl(fd, MEMERASE, &mtdEraseInfo)) {
  138. fprintf(stderr, "Could not erase MTD device: %s\n", mtd);
  139. close(fd);
  140. exit(1);
  141. }
  142. }
  143. close(fd);
  144. return 0;
  145. }
  146. int
  147. mtd_write(int imagefd, const char *mtd, int quiet, bool do_erase)
  148. {
  149. int fd, result;
  150. size_t r, w, e;
  151. struct mtd_info_user mtdInfo;
  152. struct erase_info_user mtdEraseInfo;
  153. fd = mtd_open(mtd, O_RDWR | O_SYNC);
  154. if(fd < 0) {
  155. fprintf(stderr, "Could not open mtd device: %s\n", mtd);
  156. exit(1);
  157. }
  158. if(ioctl(fd, MEMGETINFO, &mtdInfo)) {
  159. fprintf(stderr, "Could not get MTD device info from %s\n", mtd);
  160. close(fd);
  161. exit(1);
  162. }
  163. r = w = e = 0;
  164. if (!quiet)
  165. fprintf(stderr, " [ ]");
  166. for (;;) {
  167. /* buffer may contain data already (from trx check) */
  168. r = buflen;
  169. r += read(imagefd, buf + buflen, BUFSIZE - buflen);
  170. w += r;
  171. /* EOF */
  172. if (r <= 0) break;
  173. /* need to erase the next block before writing data to it */
  174. while (do_erase && w > e) {
  175. mtdEraseInfo.start = e;
  176. mtdEraseInfo.length = mtdInfo.erasesize;
  177. if (!quiet)
  178. fprintf(stderr, "\b\b\b[e]");
  179. /* erase the chunk */
  180. if (ioctl (fd,MEMERASE,&mtdEraseInfo) < 0) {
  181. fprintf(stderr, "Erasing mtd failed: %s\n", mtd);
  182. exit(1);
  183. }
  184. e += mtdInfo.erasesize;
  185. }
  186. if (!quiet)
  187. fprintf(stderr, "\b\b\b[w]");
  188. if ((result = write(fd, buf, r)) < (ssize_t)r) {
  189. if (result < 0) {
  190. fprintf(stderr, "Error writing image.\n");
  191. exit(1);
  192. } else {
  193. fprintf(stderr, "Insufficient space.\n");
  194. exit(1);
  195. }
  196. }
  197. buflen = 0;
  198. }
  199. if (!quiet)
  200. fprintf(stderr, "\b\b\b\b");
  201. close(fd);
  202. return 0;
  203. }
  204. void
  205. usage(void)
  206. {
  207. fprintf(stderr, "Usage: mtd [<options> ...] <command> [<arguments> ...] <device>\n\n"
  208. "The device is in the format of mtdX (eg: mtd4) or its label.\n"
  209. "mtd recognises these commands:\n"
  210. " unlock unlock the device\n"
  211. " erase erase all data on device\n"
  212. " write <imagefile>|- write <imagefile> (use - for stdin) to device\n"
  213. "Following options are available:\n"
  214. " -q quiet mode (once: no [w] on writing,\n"
  215. " twice: no status messages)\n"
  216. " -e <device> erase <device> before executing the command\n\n"
  217. " -r reboot after successful command\n"
  218. "Example: To write linux.img to mtd partition labeled as linux\n"
  219. " mtd write linux.img linux\n\n");
  220. exit(1);
  221. }
  222. int
  223. main(int argc, char **argv)
  224. {
  225. int ch, i, imagefd = -1, quiet, unlocked, boot;
  226. char *erase[MAX_ARGS], *device;
  227. const char *imagefile = NULL;
  228. enum {
  229. CMD_ERASE,
  230. CMD_WRITE,
  231. CMD_UNLOCK
  232. } cmd;
  233. erase[0] = NULL;
  234. boot = 0;
  235. buflen = 0;
  236. quiet = 0;
  237. while ((ch = getopt(argc, argv, "Fqre:")) != -1)
  238. switch (ch) {
  239. case 'F':
  240. quiet = 1;
  241. /* FALLTHROUGH */
  242. case 'q':
  243. quiet++;
  244. break;
  245. case 'r':
  246. boot = 1;
  247. break;
  248. case 'e':
  249. i = 0;
  250. while ((erase[i] != NULL) && ((i + 1) < MAX_ARGS))
  251. i++;
  252. erase[i++] = optarg;
  253. erase[i] = NULL;
  254. break;
  255. case '?':
  256. default:
  257. usage();
  258. }
  259. argc -= optind;
  260. argv += optind;
  261. if (argc < 2)
  262. usage();
  263. if ((strcmp(argv[0], "unlock") == 0) && (argc == 2)) {
  264. cmd = CMD_UNLOCK;
  265. device = argv[1];
  266. } else if ((strcmp(argv[0], "erase") == 0) && (argc == 2)) {
  267. cmd = CMD_ERASE;
  268. device = argv[1];
  269. } else if ((strcmp(argv[0], "write") == 0) && (argc == 3)) {
  270. cmd = CMD_WRITE;
  271. device = argv[2];
  272. if (strcmp(argv[1], "-") == 0) {
  273. imagefile = "<stdin>";
  274. imagefd = 0;
  275. } else {
  276. imagefile = argv[1];
  277. if ((imagefd = open(argv[1], O_RDONLY)) < 0) {
  278. fprintf(stderr, "Couldn't open image file: %s!\n", imagefile);
  279. exit(1);
  280. }
  281. }
  282. if (!mtd_check(device)) {
  283. fprintf(stderr, "Can't open device for writing!\n");
  284. exit(1);
  285. }
  286. } else {
  287. usage();
  288. }
  289. sync();
  290. i = 0;
  291. unlocked = 0;
  292. while (erase[i] != NULL) {
  293. if (quiet < 2)
  294. fprintf(stderr, "Unlocking %s ...\n", erase[i]);
  295. mtd_unlock(erase[i]);
  296. if (quiet < 2)
  297. fprintf(stderr, "Erasing %s ...\n", erase[i]);
  298. mtd_erase(erase[i]);
  299. if (strcmp(erase[i], device) == 0)
  300. /* this means that <device> is unlocked and erased */
  301. unlocked = 1;
  302. i++;
  303. }
  304. if (!unlocked) {
  305. if (quiet < 2)
  306. fprintf(stderr, "Unlocking %s ...\n", device);
  307. mtd_unlock(device);
  308. }
  309. switch (cmd) {
  310. case CMD_UNLOCK:
  311. break;
  312. case CMD_ERASE:
  313. if (unlocked) {
  314. fprintf(stderr, "Already erased: %s\n", device);
  315. break;
  316. }
  317. if (quiet < 2)
  318. fprintf(stderr, "Erasing %s ...\n", device);
  319. mtd_erase(device);
  320. break;
  321. case CMD_WRITE:
  322. if (quiet < 2)
  323. fprintf(stderr, "Writing from %s to %s ... ", imagefile, device);
  324. mtd_write(imagefd, device, quiet, (unlocked == 0));
  325. if (quiet < 2)
  326. fprintf(stderr, "\n");
  327. break;
  328. }
  329. sync();
  330. if (boot) {
  331. fprintf(stderr, "\nRebooting ... ");
  332. fflush(stdout);
  333. fflush(stderr);
  334. syscall(SYS_reboot,LINUX_REBOOT_MAGIC1,LINUX_REBOOT_MAGIC2,LINUX_REBOOT_CMD_RESTART,NULL);
  335. }
  336. return 0;
  337. }