mtd.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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 <error.h>
  40. #include <err.h>
  41. #include <time.h>
  42. #include <string.h>
  43. #include <mtd/mtd-user.h>
  44. #include <linux/reboot.h>
  45. #define BUFSIZE (16 * 1024)
  46. #define MAX_ARGS 8
  47. #define DEBUG
  48. int mtd_check(char *);
  49. int mtd_unlock(const char *);
  50. int mtd_open(const char *, int);
  51. int mtd_erase(const char *);
  52. int mtd_write(int, const char *, int, bool);
  53. void usage(void) __attribute__((noreturn));
  54. char buf[BUFSIZE];
  55. int buflen;
  56. int
  57. mtd_check(char *mtd)
  58. {
  59. struct mtd_info_user mtdInfo;
  60. int fd;
  61. fd = mtd_open(mtd, O_RDWR | O_SYNC);
  62. if(fd < 0) {
  63. fprintf(stderr, "Could not open mtd device: %s\n", mtd);
  64. return 0;
  65. }
  66. if(ioctl(fd, MEMGETINFO, &mtdInfo)) {
  67. fprintf(stderr, "Could not get MTD device info from %s\n", mtd);
  68. close(fd);
  69. return 0;
  70. }
  71. close(fd);
  72. return 1;
  73. }
  74. int
  75. mtd_unlock(const char *mtd)
  76. {
  77. int fd;
  78. struct mtd_info_user mtdInfo;
  79. struct erase_info_user mtdLockInfo;
  80. fd = mtd_open(mtd, O_RDWR | O_SYNC);
  81. if(fd < 0) {
  82. fprintf(stderr, "Could not open mtd device: %s\n", mtd);
  83. exit(1);
  84. }
  85. if(ioctl(fd, MEMGETINFO, &mtdInfo)) {
  86. fprintf(stderr, "Could not get MTD device info from %s\n", mtd);
  87. close(fd);
  88. exit(1);
  89. }
  90. mtdLockInfo.start = 0;
  91. mtdLockInfo.length = mtdInfo.size;
  92. if(ioctl(fd, MEMUNLOCK, &mtdLockInfo)) {
  93. close(fd);
  94. return 0;
  95. }
  96. close(fd);
  97. return 0;
  98. }
  99. int
  100. mtd_open(const char *mtd, int flags)
  101. {
  102. FILE *fp;
  103. char dev[PATH_MAX];
  104. int i;
  105. if ((fp = fopen("/proc/mtd", "r"))) {
  106. while (fgets(dev, sizeof(dev), fp)) {
  107. if (sscanf(dev, "mtd%d:", &i) && strstr(dev, mtd)) {
  108. snprintf(dev, sizeof(dev), "/dev/mtd%d", i);
  109. fclose(fp);
  110. return open(dev, flags);
  111. }
  112. }
  113. fclose(fp);
  114. }
  115. return open(mtd, flags);
  116. }
  117. int
  118. mtd_erase(const char *mtd)
  119. {
  120. int fd;
  121. struct mtd_info_user mtdInfo;
  122. struct erase_info_user mtdEraseInfo;
  123. fd = mtd_open(mtd, O_RDWR | O_SYNC);
  124. if(fd < 0) {
  125. fprintf(stderr, "Could not open mtd device: %s\n", mtd);
  126. exit(1);
  127. }
  128. if(ioctl(fd, MEMGETINFO, &mtdInfo)) {
  129. fprintf(stderr, "Could not get MTD device info from %s\n", mtd);
  130. close(fd);
  131. exit(1);
  132. }
  133. mtdEraseInfo.length = mtdInfo.erasesize;
  134. for (mtdEraseInfo.start = 0;
  135. mtdEraseInfo.start < mtdInfo.size;
  136. mtdEraseInfo.start += mtdInfo.erasesize) {
  137. ioctl(fd, MEMUNLOCK, &mtdEraseInfo);
  138. if(ioctl(fd, MEMERASE, &mtdEraseInfo)) {
  139. fprintf(stderr, "Could not erase MTD device: %s\n", mtd);
  140. close(fd);
  141. exit(1);
  142. }
  143. }
  144. close(fd);
  145. return 0;
  146. }
  147. int
  148. mtd_write(int imagefd, const char *mtd, int quiet, bool do_erase)
  149. {
  150. int fd, result;
  151. size_t r, w, e;
  152. struct mtd_info_user mtdInfo;
  153. struct erase_info_user mtdEraseInfo;
  154. fd = mtd_open(mtd, O_RDWR | O_SYNC);
  155. if(fd < 0) {
  156. fprintf(stderr, "Could not open mtd device: %s\n", mtd);
  157. exit(1);
  158. }
  159. if(ioctl(fd, MEMGETINFO, &mtdInfo)) {
  160. fprintf(stderr, "Could not get MTD device info from %s\n", mtd);
  161. close(fd);
  162. exit(1);
  163. }
  164. r = w = e = 0;
  165. if (!quiet)
  166. fprintf(stderr, " [ ]");
  167. for (;;) {
  168. /* buffer may contain data already (from trx check) */
  169. r = buflen;
  170. r += read(imagefd, buf + buflen, BUFSIZE - buflen);
  171. w += r;
  172. /* EOF */
  173. if (r <= 0) break;
  174. /* need to erase the next block before writing data to it */
  175. while (do_erase && w > e) {
  176. mtdEraseInfo.start = e;
  177. mtdEraseInfo.length = mtdInfo.erasesize;
  178. if (!quiet)
  179. fprintf(stderr, "\b\b\b[e]");
  180. /* erase the chunk */
  181. if (ioctl (fd,MEMERASE,&mtdEraseInfo) < 0) {
  182. fprintf(stderr, "Erasing mtd failed: %s\n", mtd);
  183. exit(1);
  184. }
  185. e += mtdInfo.erasesize;
  186. }
  187. if (!quiet)
  188. fprintf(stderr, "\b\b\b[w]");
  189. if ((result = write(fd, buf, r)) < (ssize_t)r) {
  190. if (result < 0) {
  191. fprintf(stderr, "Error writing image.\n");
  192. exit(1);
  193. } else {
  194. fprintf(stderr, "Insufficient space.\n");
  195. exit(1);
  196. }
  197. }
  198. buflen = 0;
  199. }
  200. if (!quiet)
  201. fprintf(stderr, "\b\b\b\b");
  202. close(fd);
  203. return 0;
  204. }
  205. void
  206. usage(void)
  207. {
  208. fprintf(stderr, "Usage: mtd [<options> ...] <command> [<arguments> ...] <device>\n\n"
  209. "The device is in the format of mtdX (eg: mtd4) or its label.\n"
  210. "mtd recognises these commands:\n"
  211. " unlock unlock the device\n"
  212. " erase erase all data on device\n"
  213. " write <imagefile>|- write <imagefile> (use - for stdin) to device\n"
  214. "Following options are available:\n"
  215. " -q quiet mode (once: no [w] on writing,\n"
  216. " twice: no status messages)\n"
  217. " -e <device> erase <device> before executing the command\n\n"
  218. " -r reboot after successful command\n"
  219. "Example: To write linux.img to mtd partition labeled as linux\n"
  220. " mtd write linux.img linux\n\n");
  221. exit(1);
  222. }
  223. int
  224. main(int argc, char **argv)
  225. {
  226. int ch, i, imagefd = -1, quiet, unlocked, boot;
  227. char *erase[MAX_ARGS], *device;
  228. const char *imagefile = NULL;
  229. enum {
  230. CMD_ERASE,
  231. CMD_WRITE,
  232. CMD_UNLOCK
  233. } cmd;
  234. erase[0] = NULL;
  235. boot = 0;
  236. buflen = 0;
  237. quiet = 0;
  238. while ((ch = getopt(argc, argv, "Fqre:")) != -1)
  239. switch (ch) {
  240. case 'F':
  241. quiet = 1;
  242. /* FALLTHROUGH */
  243. case 'q':
  244. quiet++;
  245. break;
  246. case 'r':
  247. boot = 1;
  248. break;
  249. case 'e':
  250. i = 0;
  251. while ((erase[i] != NULL) && ((i + 1) < MAX_ARGS))
  252. i++;
  253. erase[i++] = optarg;
  254. erase[i] = NULL;
  255. break;
  256. case '?':
  257. default:
  258. usage();
  259. }
  260. argc -= optind;
  261. argv += optind;
  262. if (argc < 2)
  263. usage();
  264. if ((strcmp(argv[0], "unlock") == 0) && (argc == 2)) {
  265. cmd = CMD_UNLOCK;
  266. device = argv[1];
  267. } else if ((strcmp(argv[0], "erase") == 0) && (argc == 2)) {
  268. cmd = CMD_ERASE;
  269. device = argv[1];
  270. } else if ((strcmp(argv[0], "write") == 0) && (argc == 3)) {
  271. cmd = CMD_WRITE;
  272. device = argv[2];
  273. if (strcmp(argv[1], "-") == 0) {
  274. imagefile = "<stdin>";
  275. imagefd = 0;
  276. } else {
  277. imagefile = argv[1];
  278. if ((imagefd = open(argv[1], O_RDONLY)) < 0) {
  279. fprintf(stderr, "Couldn't open image file: %s!\n", imagefile);
  280. exit(1);
  281. }
  282. }
  283. if (!mtd_check(device)) {
  284. fprintf(stderr, "Can't open device for writing!\n");
  285. exit(1);
  286. }
  287. } else {
  288. usage();
  289. }
  290. sync();
  291. i = 0;
  292. unlocked = 0;
  293. while (erase[i] != NULL) {
  294. if (quiet < 2)
  295. fprintf(stderr, "Unlocking %s ...\n", erase[i]);
  296. mtd_unlock(erase[i]);
  297. if (quiet < 2)
  298. fprintf(stderr, "Erasing %s ...\n", erase[i]);
  299. mtd_erase(erase[i]);
  300. if (strcmp(erase[i], device) == 0)
  301. /* this means that <device> is unlocked and erased */
  302. unlocked = 1;
  303. i++;
  304. }
  305. if (!unlocked) {
  306. if (quiet < 2)
  307. fprintf(stderr, "Unlocking %s ...\n", device);
  308. mtd_unlock(device);
  309. }
  310. switch (cmd) {
  311. case CMD_UNLOCK:
  312. break;
  313. case CMD_ERASE:
  314. if (unlocked) {
  315. fprintf(stderr, "Already erased: %s\n", device);
  316. break;
  317. }
  318. if (quiet < 2)
  319. fprintf(stderr, "Erasing %s ...\n", device);
  320. mtd_erase(device);
  321. break;
  322. case CMD_WRITE:
  323. if (quiet < 2)
  324. fprintf(stderr, "Writing from %s to %s ... ", imagefile, device);
  325. mtd_write(imagefd, device, quiet, (unlocked == 0));
  326. if (quiet < 2)
  327. fprintf(stderr, "\n");
  328. break;
  329. }
  330. sync();
  331. if (boot) {
  332. fprintf(stderr, "\nRebooting ... ");
  333. fflush(stdout);
  334. fflush(stderr);
  335. syscall(SYS_reboot,LINUX_REBOOT_MAGIC1,LINUX_REBOOT_MAGIC2,LINUX_REBOOT_CMD_RESTART,NULL);
  336. }
  337. return 0;
  338. }