mtd.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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. * $Id$
  22. *
  23. * The code is based on the linux-mtd examples.
  24. */
  25. #include <limits.h>
  26. #include <unistd.h>
  27. #include <stdlib.h>
  28. #include <stdio.h>
  29. #include <stdint.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.h"
  45. #define TRX_MAGIC 0x30524448 /* "HDR0" */
  46. #define BUFSIZE (16 * 1024)
  47. #define MAX_ARGS 8
  48. #define DEBUG
  49. #define SYSTYPE_UNKNOWN 0
  50. #define SYSTYPE_BROADCOM 1
  51. /* to be continued */
  52. struct trx_header {
  53. uint32_t magic; /* "HDR0" */
  54. uint32_t len; /* Length of file including header */
  55. uint32_t crc32; /* 32-bit CRC from flag_version to end of file */
  56. uint32_t flag_version; /* 0:15 flags, 16:31 version */
  57. uint32_t offsets[3]; /* Offsets of partitions from start of header */
  58. };
  59. char buf[BUFSIZE];
  60. int buflen;
  61. int quiet;
  62. #ifdef target_brcm
  63. int
  64. image_check_brcm(int imagefd, const char *mtd)
  65. {
  66. struct trx_header *trx = (struct trx_header *) buf;
  67. struct mtd_info_user mtdInfo;
  68. int fd;
  69. if (strcmp(mtd, "linux") != 0)
  70. return 1;
  71. buflen = read(imagefd, buf, 32);
  72. if (buflen < 32) {
  73. fprintf(stdout, "Could not get image header, file too small (%ld bytes)\n", buflen);
  74. return 0;
  75. }
  76. if (trx->magic != TRX_MAGIC || trx->len < sizeof(struct trx_header)) {
  77. if (quiet < 2) {
  78. fprintf(stderr, "Bad trx header\n");
  79. fprintf(stderr, "This is not the correct file format; refusing to flash.\n"
  80. "Please specify the correct file or use -f to force.\n");
  81. }
  82. return 0;
  83. }
  84. /* check if image fits to mtd device */
  85. fd = mtd_open(mtd, O_RDWR | O_SYNC);
  86. if(fd < 0) {
  87. fprintf(stderr, "Could not open mtd device: %s\n", mtd);
  88. exit(1);
  89. }
  90. if(ioctl(fd, MEMGETINFO, &mtdInfo)) {
  91. fprintf(stderr, "Could not get MTD device info from %s\n", mtd);
  92. exit(1);
  93. }
  94. if(mtdInfo.size < trx->len) {
  95. fprintf(stderr, "Image too big for partition: %s\n", mtd);
  96. close(fd);
  97. return 0;
  98. }
  99. close(fd);
  100. return 1;
  101. }
  102. #endif /* target_brcm */
  103. int
  104. image_check(int imagefd, const char *mtd)
  105. {
  106. int fd, systype;
  107. size_t count;
  108. char *c;
  109. FILE *f;
  110. #ifdef target_brcm
  111. return image_check_brcm(imagefd, mtd);
  112. #endif
  113. }
  114. int mtd_check(char *mtd)
  115. {
  116. struct mtd_info_user mtdInfo;
  117. int fd;
  118. fd = mtd_open(mtd, O_RDWR | O_SYNC);
  119. if(fd < 0) {
  120. fprintf(stderr, "Could not open mtd device: %s\n", mtd);
  121. return 0;
  122. }
  123. if(ioctl(fd, MEMGETINFO, &mtdInfo)) {
  124. fprintf(stderr, "Could not get MTD device info from %s\n", mtd);
  125. close(fd);
  126. return 0;
  127. }
  128. close(fd);
  129. return 1;
  130. }
  131. int
  132. mtd_unlock(const char *mtd)
  133. {
  134. int fd;
  135. struct mtd_info_user mtdInfo;
  136. struct erase_info_user mtdLockInfo;
  137. fd = mtd_open(mtd, O_RDWR | O_SYNC);
  138. if(fd < 0) {
  139. fprintf(stderr, "Could not open mtd device: %s\n", mtd);
  140. exit(1);
  141. }
  142. if(ioctl(fd, MEMGETINFO, &mtdInfo)) {
  143. fprintf(stderr, "Could not get MTD device info from %s\n", mtd);
  144. close(fd);
  145. exit(1);
  146. }
  147. mtdLockInfo.start = 0;
  148. mtdLockInfo.length = mtdInfo.size;
  149. if(ioctl(fd, MEMUNLOCK, &mtdLockInfo)) {
  150. close(fd);
  151. return 0;
  152. }
  153. close(fd);
  154. return 0;
  155. }
  156. int
  157. mtd_open(const char *mtd, int flags)
  158. {
  159. FILE *fp;
  160. char dev[PATH_MAX];
  161. int i;
  162. int ret;
  163. if ((fp = fopen("/proc/mtd", "r"))) {
  164. while (fgets(dev, sizeof(dev), fp)) {
  165. if (sscanf(dev, "mtd%d:", &i) && strstr(dev, mtd)) {
  166. snprintf(dev, sizeof(dev), "/dev/mtd/%d", i);
  167. if ((ret=open(dev, flags))<0) {
  168. snprintf(dev, sizeof(dev), "/dev/mtd%d", i);
  169. ret=open(dev, flags);
  170. }
  171. fclose(fp);
  172. return ret;
  173. }
  174. }
  175. fclose(fp);
  176. }
  177. return open(mtd, flags);
  178. }
  179. int
  180. mtd_erase(const char *mtd)
  181. {
  182. int fd;
  183. struct mtd_info_user mtdInfo;
  184. struct erase_info_user mtdEraseInfo;
  185. fd = mtd_open(mtd, O_RDWR | O_SYNC);
  186. if(fd < 0) {
  187. fprintf(stderr, "Could not open mtd device: %s\n", mtd);
  188. exit(1);
  189. }
  190. if(ioctl(fd, MEMGETINFO, &mtdInfo)) {
  191. fprintf(stderr, "Could not get MTD device info from %s\n", mtd);
  192. close(fd);
  193. exit(1);
  194. }
  195. mtdEraseInfo.length = mtdInfo.erasesize;
  196. for (mtdEraseInfo.start = 0;
  197. mtdEraseInfo.start < mtdInfo.size;
  198. mtdEraseInfo.start += mtdInfo.erasesize) {
  199. ioctl(fd, MEMUNLOCK, &mtdEraseInfo);
  200. if(ioctl(fd, MEMERASE, &mtdEraseInfo))
  201. fprintf(stderr, "Failed to erase block on %s at 0x%x\n", mtd, mtdEraseInfo.start);
  202. }
  203. close(fd);
  204. return 0;
  205. }
  206. int
  207. mtd_write(int imagefd, const char *mtd)
  208. {
  209. int fd, i, result;
  210. size_t r, w, e;
  211. struct mtd_info_user mtdInfo;
  212. struct erase_info_user mtdEraseInfo;
  213. int ret = 0;
  214. fd = mtd_open(mtd, O_RDWR | O_SYNC);
  215. if(fd < 0) {
  216. fprintf(stderr, "Could not open mtd device: %s\n", mtd);
  217. exit(1);
  218. }
  219. if(ioctl(fd, MEMGETINFO, &mtdInfo)) {
  220. fprintf(stderr, "Could not get MTD device info from %s\n", mtd);
  221. close(fd);
  222. exit(1);
  223. }
  224. r = w = e = 0;
  225. if (!quiet)
  226. fprintf(stderr, " [ ]");
  227. for (;;) {
  228. /* buffer may contain data already (from trx check) */
  229. r = buflen;
  230. r += read(imagefd, buf + buflen, BUFSIZE - buflen);
  231. w += r;
  232. /* EOF */
  233. if (r <= 0) break;
  234. /* need to erase the next block before writing data to it */
  235. while (w > e) {
  236. mtdEraseInfo.start = e;
  237. mtdEraseInfo.length = mtdInfo.erasesize;
  238. if (!quiet)
  239. fprintf(stderr, "\b\b\b[e]");
  240. /* erase the chunk */
  241. if (ioctl (fd,MEMERASE,&mtdEraseInfo) < 0) {
  242. fprintf(stderr, "Erasing mtd failed: %s\n", mtd);
  243. exit(1);
  244. }
  245. e += mtdInfo.erasesize;
  246. }
  247. if (!quiet)
  248. fprintf(stderr, "\b\b\b[w]");
  249. if ((result = write(fd, buf, r)) < r) {
  250. if (result < 0) {
  251. fprintf(stderr, "Error writing image.\n");
  252. exit(1);
  253. } else {
  254. fprintf(stderr, "Insufficient space.\n");
  255. exit(1);
  256. }
  257. }
  258. buflen = 0;
  259. }
  260. if (!quiet)
  261. fprintf(stderr, "\b\b\b\b");
  262. close(fd);
  263. return 0;
  264. }
  265. void usage(void)
  266. {
  267. fprintf(stderr, "Usage: mtd [<options> ...] <command> [<arguments> ...] <device>\n\n"
  268. "The device is in the format of mtdX (eg: mtd4) or its label.\n"
  269. "mtd recognizes these commands:\n"
  270. " unlock unlock the device\n"
  271. " erase erase all data on device\n"
  272. " write <imagefile>|- write <imagefile> (use - for stdin) to device\n"
  273. "Following options are available:\n"
  274. " -q quiet mode (once: no [w] on writing,\n"
  275. " twice: no status messages)\n"
  276. " -r reboot after successful command\n"
  277. " -f force write without trx checks\n"
  278. " -e <device> erase <device> before executing the command\n\n"
  279. "Example: To write linux.trx to mtd4 labeled as linux and reboot afterwards\n"
  280. " mtd -r write linux.trx linux\n\n");
  281. exit(1);
  282. }
  283. int main (int argc, char **argv)
  284. {
  285. int ch, i, boot, unlock, imagefd, force, unlocked;
  286. char *erase[MAX_ARGS], *device, *imagefile;
  287. enum {
  288. CMD_ERASE,
  289. CMD_WRITE,
  290. CMD_UNLOCK
  291. } cmd;
  292. erase[0] = NULL;
  293. boot = 0;
  294. force = 0;
  295. buflen = 0;
  296. quiet = 0;
  297. while ((ch = getopt(argc, argv, "frqe:")) != -1)
  298. switch (ch) {
  299. case 'f':
  300. force = 1;
  301. break;
  302. case 'r':
  303. boot = 1;
  304. break;
  305. case 'q':
  306. quiet++;
  307. break;
  308. case 'e':
  309. i = 0;
  310. while ((erase[i] != NULL) && ((i + 1) < MAX_ARGS))
  311. i++;
  312. erase[i++] = optarg;
  313. erase[i] = NULL;
  314. break;
  315. case '?':
  316. default:
  317. usage();
  318. }
  319. argc -= optind;
  320. argv += optind;
  321. if (argc < 2)
  322. usage();
  323. if ((strcmp(argv[0], "unlock") == 0) && (argc == 2)) {
  324. cmd = CMD_UNLOCK;
  325. device = argv[1];
  326. } else if ((strcmp(argv[0], "erase") == 0) && (argc == 2)) {
  327. cmd = CMD_ERASE;
  328. device = argv[1];
  329. } else if ((strcmp(argv[0], "write") == 0) && (argc == 3)) {
  330. cmd = CMD_WRITE;
  331. device = argv[2];
  332. if (strcmp(argv[1], "-") == 0) {
  333. imagefile = "<stdin>";
  334. imagefd = 0;
  335. } else {
  336. imagefile = argv[1];
  337. if ((imagefd = open(argv[1], O_RDONLY)) < 0) {
  338. fprintf(stderr, "Couldn't open image file: %s!\n", imagefile);
  339. exit(1);
  340. }
  341. }
  342. /* check trx file before erasing or writing anything */
  343. if (!image_check(imagefd, device)) {
  344. if (!force) {
  345. fprintf(stderr, "Image check failed.\n");
  346. exit(1);
  347. }
  348. } else {
  349. if (!mtd_check(device)) {
  350. fprintf(stderr, "Can't open device for writing!\n");
  351. exit(1);
  352. }
  353. }
  354. } else {
  355. usage();
  356. }
  357. sync();
  358. i = 0;
  359. unlocked = 0;
  360. while (erase[i] != NULL) {
  361. if (quiet < 2)
  362. fprintf(stderr, "Unlocking %s ...\n", erase[i]);
  363. mtd_unlock(erase[i]);
  364. if (quiet < 2)
  365. fprintf(stderr, "Erasing %s ...\n", erase[i]);
  366. mtd_erase(erase[i]);
  367. if (strcmp(erase[i], device) == 0)
  368. unlocked = 1;
  369. i++;
  370. }
  371. if (!unlocked) {
  372. if (quiet < 2)
  373. fprintf(stderr, "Unlocking %s ...\n", device);
  374. mtd_unlock(device);
  375. }
  376. switch (cmd) {
  377. case CMD_UNLOCK:
  378. break;
  379. case CMD_ERASE:
  380. if (quiet < 2)
  381. fprintf(stderr, "Erasing %s ...\n", device);
  382. mtd_erase(device);
  383. break;
  384. case CMD_WRITE:
  385. if (quiet < 2)
  386. fprintf(stderr, "Writing from %s to %s ... ", imagefile, device);
  387. mtd_write(imagefd, device);
  388. if (quiet < 2)
  389. fprintf(stderr, "\n");
  390. break;
  391. }
  392. sync();
  393. if (boot) {
  394. fprintf(stderr, "Rebooting ...\n");
  395. fflush(stderr);
  396. syscall(SYS_reboot,LINUX_REBOOT_MAGIC1,LINUX_REBOOT_MAGIC2,LINUX_REBOOT_CMD_RESTART,NULL);
  397. }
  398. return 0;
  399. }