mtd.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. /*
  2. * mtd - simple memory technology device manipulation tool
  3. *
  4. * Copyright (C) 2005 Waldemar Brodkorb <[email protected]>,
  5. * Felix Fietkau <[email protected]>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. *
  21. *
  22. * The code is based on the linux-mtd examples.
  23. */
  24. #include <limits.h>
  25. #include <unistd.h>
  26. #include <stdlib.h>
  27. #include <stdio.h>
  28. #include <stdint.h>
  29. #include <signal.h>
  30. #include <sys/ioctl.h>
  31. #include <sys/syscall.h>
  32. #include <fcntl.h>
  33. #include <errno.h>
  34. #include <error.h>
  35. #include <time.h>
  36. #include <string.h>
  37. #include <sys/ioctl.h>
  38. #include <sys/types.h>
  39. #include <sys/param.h>
  40. #include <sys/mount.h>
  41. #include <sys/stat.h>
  42. #include <sys/reboot.h>
  43. #include <linux/reboot.h>
  44. #include "mtd-api.h"
  45. #include "mtd.h"
  46. #define MAX_ARGS 8
  47. #define JFFS2_DEFAULT_DIR "" /* directory name without /, empty means root dir */
  48. struct trx_header {
  49. uint32_t magic; /* "HDR0" */
  50. uint32_t len; /* Length of file including header */
  51. uint32_t crc32; /* 32-bit CRC from flag_version to end of file */
  52. uint32_t flag_version; /* 0:15 flags, 16:31 version */
  53. uint32_t offsets[3]; /* Offsets of partitions from start of header */
  54. };
  55. static char *buf = NULL;
  56. static char *imagefile = NULL;
  57. static char *jffs2file = NULL, *jffs2dir = JFFS2_DEFAULT_DIR;
  58. static int buflen = 0;
  59. int quiet;
  60. int mtdsize = 0;
  61. int erasesize = 0;
  62. int mtd_open(const char *mtd, bool block)
  63. {
  64. FILE *fp;
  65. char dev[PATH_MAX];
  66. int i;
  67. int ret;
  68. int flags = O_RDWR | O_SYNC;
  69. if ((fp = fopen("/proc/mtd", "r"))) {
  70. while (fgets(dev, sizeof(dev), fp)) {
  71. if (sscanf(dev, "mtd%d:", &i) && strstr(dev, mtd)) {
  72. snprintf(dev, sizeof(dev), "/dev/mtd%s/%d", (block ? "block" : ""), i);
  73. if ((ret=open(dev, flags))<0) {
  74. snprintf(dev, sizeof(dev), "/dev/mtd%s%d", (block ? "block" : ""), i);
  75. ret=open(dev, flags);
  76. }
  77. fclose(fp);
  78. return ret;
  79. }
  80. }
  81. fclose(fp);
  82. }
  83. return open(mtd, flags);
  84. }
  85. int mtd_check_open(const char *mtd)
  86. {
  87. struct mtd_info_user mtdInfo;
  88. int fd;
  89. fd = mtd_open(mtd, false);
  90. if(fd < 0) {
  91. fprintf(stderr, "Could not open mtd device: %s\n", mtd);
  92. return 0;
  93. }
  94. if(ioctl(fd, MEMGETINFO, &mtdInfo)) {
  95. fprintf(stderr, "Could not get MTD device info from %s\n", mtd);
  96. close(fd);
  97. return 0;
  98. }
  99. mtdsize = mtdInfo.size;
  100. erasesize = mtdInfo.erasesize;
  101. return fd;
  102. }
  103. int mtd_erase_block(int fd, int offset)
  104. {
  105. struct erase_info_user mtdEraseInfo;
  106. mtdEraseInfo.start = offset;
  107. mtdEraseInfo.length = erasesize;
  108. ioctl(fd, MEMUNLOCK, &mtdEraseInfo);
  109. if (ioctl (fd, MEMERASE, &mtdEraseInfo) < 0) {
  110. fprintf(stderr, "Erasing mtd failed.\n");
  111. exit(1);
  112. }
  113. return 0;
  114. }
  115. int mtd_write_buffer(int fd, const char *buf, int offset, int length)
  116. {
  117. lseek(fd, offset, SEEK_SET);
  118. write(fd, buf, length);
  119. return 0;
  120. }
  121. static int
  122. image_check(int imagefd, const char *mtd)
  123. {
  124. int ret = 1;
  125. #ifdef target_brcm
  126. ret = trx_check(imagefd, mtd, buf, &buflen);
  127. #endif
  128. return ret;
  129. }
  130. static int mtd_check(const char *mtd)
  131. {
  132. int fd;
  133. fd = mtd_check_open(mtd);
  134. if (!fd)
  135. return 0;
  136. if (!buf)
  137. buf = malloc(erasesize);
  138. close(fd);
  139. return 1;
  140. }
  141. static int
  142. mtd_unlock(const char *mtd)
  143. {
  144. int fd;
  145. struct erase_info_user mtdLockInfo;
  146. fd = mtd_check_open(mtd);
  147. if(fd <= 0) {
  148. fprintf(stderr, "Could not open mtd device: %s\n", mtd);
  149. exit(1);
  150. }
  151. if (quiet < 2)
  152. fprintf(stderr, "Unlocking %s ...\n", mtd);
  153. mtdLockInfo.start = 0;
  154. mtdLockInfo.length = mtdsize;
  155. if(ioctl(fd, MEMUNLOCK, &mtdLockInfo)) {
  156. close(fd);
  157. return 0;
  158. }
  159. close(fd);
  160. return 0;
  161. }
  162. static int
  163. mtd_erase(const char *mtd)
  164. {
  165. int fd;
  166. struct erase_info_user mtdEraseInfo;
  167. if (quiet < 2)
  168. fprintf(stderr, "Erasing %s ...\n", mtd);
  169. fd = mtd_check_open(mtd);
  170. if(fd <= 0) {
  171. fprintf(stderr, "Could not open mtd device: %s\n", mtd);
  172. exit(1);
  173. }
  174. mtdEraseInfo.length = erasesize;
  175. for (mtdEraseInfo.start = 0;
  176. mtdEraseInfo.start < mtdsize;
  177. mtdEraseInfo.start += erasesize) {
  178. ioctl(fd, MEMUNLOCK, &mtdEraseInfo);
  179. if(ioctl(fd, MEMERASE, &mtdEraseInfo))
  180. fprintf(stderr, "Failed to erase block on %s at 0x%x\n", mtd, mtdEraseInfo.start);
  181. }
  182. close(fd);
  183. return 0;
  184. }
  185. static int
  186. mtd_refresh(const char *mtd)
  187. {
  188. int fd;
  189. if (quiet < 2)
  190. fprintf(stderr, "Refreshing mtd partition %s ... ", mtd);
  191. fd = mtd_check_open(mtd);
  192. if(fd <= 0) {
  193. fprintf(stderr, "Could not open mtd device: %s\n", mtd);
  194. exit(1);
  195. }
  196. if (ioctl(fd, MTDREFRESH, NULL)) {
  197. fprintf(stderr, "Failed to refresh the MTD device\n");
  198. close(fd);
  199. exit(1);
  200. }
  201. close(fd);
  202. if (quiet < 2)
  203. fprintf(stderr, "\n");
  204. return 0;
  205. }
  206. static int
  207. mtd_write(int imagefd, const char *mtd)
  208. {
  209. int fd, result;
  210. ssize_t r, w, e;
  211. fd = mtd_check_open(mtd);
  212. if(fd < 0) {
  213. fprintf(stderr, "Could not open mtd device: %s\n", mtd);
  214. exit(1);
  215. }
  216. if (quiet < 2)
  217. fprintf(stderr, "Writing from %s to %s ... ", imagefile, mtd);
  218. r = w = e = 0;
  219. if (!quiet)
  220. fprintf(stderr, " [ ]");
  221. for (;;) {
  222. /* buffer may contain data already (from trx check) */
  223. do {
  224. r = read(imagefd, buf + buflen, erasesize - buflen);
  225. if (r < 0) {
  226. if ((errno == EINTR) || (errno == EAGAIN))
  227. continue;
  228. else {
  229. perror("read");
  230. break;
  231. }
  232. }
  233. if (r == 0)
  234. break;
  235. buflen += r;
  236. } while (buflen < erasesize);
  237. if (buflen == 0)
  238. break;
  239. if (jffs2file) {
  240. if (memcmp(buf, JFFS2_EOF, sizeof(JFFS2_EOF) - 1) == 0) {
  241. if (!quiet)
  242. fprintf(stderr, "\b\b\b ");
  243. if (quiet < 2)
  244. fprintf(stderr, "\nAppending jffs2 data to from %s to %s...", jffs2file, mtd);
  245. /* got an EOF marker - this is the place to add some jffs2 data */
  246. mtd_replace_jffs2(mtd, fd, e, jffs2file);
  247. goto done;
  248. }
  249. /* no EOF marker, make sure we figure out the last inode number
  250. * before appending some data */
  251. mtd_parse_jffs2data(buf, jffs2dir);
  252. }
  253. /* need to erase the next block before writing data to it */
  254. while (w + buflen > e) {
  255. if (!quiet)
  256. fprintf(stderr, "\b\b\b[e]");
  257. mtd_erase_block(fd, e);
  258. /* erase the chunk */
  259. e += erasesize;
  260. }
  261. if (!quiet)
  262. fprintf(stderr, "\b\b\b[w]");
  263. if ((result = write(fd, buf, buflen)) < buflen) {
  264. if (result < 0) {
  265. fprintf(stderr, "Error writing image.\n");
  266. exit(1);
  267. } else {
  268. fprintf(stderr, "Insufficient space.\n");
  269. exit(1);
  270. }
  271. }
  272. w += buflen;
  273. buflen = 0;
  274. }
  275. if (!quiet)
  276. fprintf(stderr, "\b\b\b\b");
  277. done:
  278. if (quiet < 2)
  279. fprintf(stderr, "\n");
  280. close(fd);
  281. return 0;
  282. }
  283. static void usage(void)
  284. {
  285. fprintf(stderr, "Usage: mtd [<options> ...] <command> [<arguments> ...] <device>\n\n"
  286. "The device is in the format of mtdX (eg: mtd4) or its label.\n"
  287. "mtd recognizes these commands:\n"
  288. " unlock unlock the device\n"
  289. " refresh refresh mtd partition\n"
  290. " erase erase all data on device\n"
  291. " write <imagefile>|- write <imagefile> (use - for stdin) to device\n"
  292. " jffs2write <file> append <file> to the jffs2 partition on the device\n"
  293. "Following options are available:\n"
  294. " -q quiet mode (once: no [w] on writing,\n"
  295. " twice: no status messages)\n"
  296. " -r reboot after successful command\n"
  297. " -f force write without trx checks\n"
  298. " -e <device> erase <device> before executing the command\n"
  299. " -d <name> directory for jffs2write, defaults to \"tmp\"\n"
  300. " -j <name> integrate <file> into jffs2 data when writing an image\n"
  301. "\n"
  302. "Example: To write linux.trx to mtd4 labeled as linux and reboot afterwards\n"
  303. " mtd -r write linux.trx linux\n\n");
  304. exit(1);
  305. }
  306. static void do_reboot(void)
  307. {
  308. fprintf(stderr, "Rebooting ...\n");
  309. fflush(stderr);
  310. /* try regular reboot method first */
  311. system("/sbin/reboot");
  312. sleep(2);
  313. /* if we're still alive at this point, force the kernel to reboot */
  314. syscall(SYS_reboot,LINUX_REBOOT_MAGIC1,LINUX_REBOOT_MAGIC2,LINUX_REBOOT_CMD_RESTART,NULL);
  315. }
  316. int main (int argc, char **argv)
  317. {
  318. int ch, i, boot, imagefd = 0, force, unlocked;
  319. char *erase[MAX_ARGS], *device = NULL;
  320. enum {
  321. CMD_ERASE,
  322. CMD_WRITE,
  323. CMD_UNLOCK,
  324. CMD_REFRESH,
  325. CMD_JFFS2WRITE
  326. } cmd = -1;
  327. erase[0] = NULL;
  328. boot = 0;
  329. force = 0;
  330. buflen = 0;
  331. quiet = 0;
  332. while ((ch = getopt(argc, argv, "frqe:d:j:")) != -1)
  333. switch (ch) {
  334. case 'f':
  335. force = 1;
  336. break;
  337. case 'r':
  338. boot = 1;
  339. break;
  340. case 'j':
  341. jffs2file = optarg;
  342. break;
  343. case 'q':
  344. quiet++;
  345. break;
  346. case 'e':
  347. i = 0;
  348. while ((erase[i] != NULL) && ((i + 1) < MAX_ARGS))
  349. i++;
  350. erase[i++] = optarg;
  351. erase[i] = NULL;
  352. break;
  353. case 'd':
  354. jffs2dir = optarg;
  355. break;
  356. case '?':
  357. default:
  358. usage();
  359. }
  360. argc -= optind;
  361. argv += optind;
  362. if (argc < 2)
  363. usage();
  364. if ((strcmp(argv[0], "unlock") == 0) && (argc == 2)) {
  365. cmd = CMD_UNLOCK;
  366. device = argv[1];
  367. } else if ((strcmp(argv[0], "refresh") == 0) && (argc == 2)) {
  368. cmd = CMD_REFRESH;
  369. device = argv[1];
  370. } else if ((strcmp(argv[0], "erase") == 0) && (argc == 2)) {
  371. cmd = CMD_ERASE;
  372. device = argv[1];
  373. } else if ((strcmp(argv[0], "write") == 0) && (argc == 3)) {
  374. cmd = CMD_WRITE;
  375. device = argv[2];
  376. if (strcmp(argv[1], "-") == 0) {
  377. imagefile = "<stdin>";
  378. imagefd = 0;
  379. } else {
  380. imagefile = argv[1];
  381. if ((imagefd = open(argv[1], O_RDONLY)) < 0) {
  382. fprintf(stderr, "Couldn't open image file: %s!\n", imagefile);
  383. exit(1);
  384. }
  385. }
  386. if (!mtd_check(device)) {
  387. fprintf(stderr, "Can't open device for writing!\n");
  388. exit(1);
  389. }
  390. /* check trx file before erasing or writing anything */
  391. if (!image_check(imagefd, device) && !force) {
  392. fprintf(stderr, "Image check failed.\n");
  393. exit(1);
  394. }
  395. } else if ((strcmp(argv[0], "jffs2write") == 0) && (argc == 3)) {
  396. cmd = CMD_JFFS2WRITE;
  397. device = argv[2];
  398. imagefile = argv[1];
  399. if (!mtd_check(device)) {
  400. fprintf(stderr, "Can't open device for writing!\n");
  401. exit(1);
  402. }
  403. } else {
  404. usage();
  405. }
  406. sync();
  407. i = 0;
  408. unlocked = 0;
  409. while (erase[i] != NULL) {
  410. mtd_unlock(erase[i]);
  411. mtd_erase(erase[i]);
  412. if (strcmp(erase[i], device) == 0)
  413. unlocked = 1;
  414. i++;
  415. }
  416. switch (cmd) {
  417. case CMD_UNLOCK:
  418. if (!unlocked)
  419. mtd_unlock(device);
  420. break;
  421. case CMD_ERASE:
  422. if (!unlocked)
  423. mtd_unlock(device);
  424. mtd_erase(device);
  425. break;
  426. case CMD_WRITE:
  427. if (!unlocked)
  428. mtd_unlock(device);
  429. mtd_write(imagefd, device);
  430. break;
  431. case CMD_JFFS2WRITE:
  432. if (!unlocked)
  433. mtd_unlock(device);
  434. mtd_write_jffs2(device, imagefile, jffs2dir);
  435. break;
  436. case CMD_REFRESH:
  437. mtd_refresh(device);
  438. break;
  439. }
  440. sync();
  441. if (boot)
  442. do_reboot();
  443. return 0;
  444. }