mtd.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047
  1. /*
  2. * mtd - simple memory technology device manipulation tool
  3. *
  4. * Copyright (C) 2005 Waldemar Brodkorb <wbx@dass-it.de>,
  5. * Copyright (C) 2005-2009 Felix Fietkau <nbd@nbd.name>
  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 v2
  9. * as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. *
  20. *
  21. * The code is based on the linux-mtd examples.
  22. */
  23. #define _GNU_SOURCE
  24. #include <byteswap.h>
  25. #include <endian.h>
  26. #include <limits.h>
  27. #include <unistd.h>
  28. #include <stdlib.h>
  29. #include <stdio.h>
  30. #include <stdint.h>
  31. #include <signal.h>
  32. #include <sys/ioctl.h>
  33. #include <sys/syscall.h>
  34. #include <fcntl.h>
  35. #include <errno.h>
  36. #include <time.h>
  37. #include <string.h>
  38. #include <sys/ioctl.h>
  39. #include <sys/types.h>
  40. #include <sys/param.h>
  41. #include <sys/mount.h>
  42. #include <sys/stat.h>
  43. #include <sys/reboot.h>
  44. #include <linux/reboot.h>
  45. #include <mtd/mtd-user.h>
  46. #include "fis.h"
  47. #include "mtd.h"
  48. #include <libubox/md5.h>
  49. #define MAX_ARGS 8
  50. #define JFFS2_DEFAULT_DIR "" /* directory name without /, empty means root dir */
  51. #define TRX_MAGIC 0x48445230 /* "HDR0" */
  52. #define SEAMA_MAGIC 0x5ea3a417
  53. #define WRG_MAGIC 0x20040220
  54. #define WRGG03_MAGIC 0x20080321
  55. #if !defined(__BYTE_ORDER)
  56. #error "Unknown byte order"
  57. #endif
  58. #if __BYTE_ORDER == __BIG_ENDIAN
  59. #define cpu_to_be32(x) (x)
  60. #define be32_to_cpu(x) (x)
  61. #define le32_to_cpu(x) bswap_32(x)
  62. #elif __BYTE_ORDER == __LITTLE_ENDIAN
  63. #define cpu_to_be32(x) bswap_32(x)
  64. #define be32_to_cpu(x) bswap_32(x)
  65. #define le32_to_cpu(x) (x)
  66. #else
  67. #error "Unsupported endianness"
  68. #endif
  69. enum mtd_image_format {
  70. MTD_IMAGE_FORMAT_UNKNOWN,
  71. MTD_IMAGE_FORMAT_TRX,
  72. MTD_IMAGE_FORMAT_SEAMA,
  73. MTD_IMAGE_FORMAT_WRG,
  74. MTD_IMAGE_FORMAT_WRGG03,
  75. };
  76. static char *buf = NULL;
  77. static char *imagefile = NULL;
  78. static enum mtd_image_format imageformat = MTD_IMAGE_FORMAT_UNKNOWN;
  79. static char *jffs2file = NULL, *jffs2dir = JFFS2_DEFAULT_DIR;
  80. static int buflen = 0;
  81. int quiet;
  82. int no_erase;
  83. int mtdsize = 0;
  84. int erasesize = 0;
  85. int jffs2_skip_bytes=0;
  86. int mtdtype = 0;
  87. int mtd_open(const char *mtd, bool block)
  88. {
  89. FILE *fp;
  90. char dev[PATH_MAX];
  91. int i;
  92. int ret;
  93. int flags = O_RDWR | O_SYNC;
  94. char name[PATH_MAX];
  95. snprintf(name, sizeof(name), "\"%s\"", mtd);
  96. if ((fp = fopen("/proc/mtd", "r"))) {
  97. while (fgets(dev, sizeof(dev), fp)) {
  98. if (sscanf(dev, "mtd%d:", &i) && strstr(dev, name)) {
  99. snprintf(dev, sizeof(dev), "/dev/mtd%s/%d", (block ? "block" : ""), i);
  100. if ((ret=open(dev, flags))<0) {
  101. snprintf(dev, sizeof(dev), "/dev/mtd%s%d", (block ? "block" : ""), i);
  102. ret=open(dev, flags);
  103. }
  104. fclose(fp);
  105. return ret;
  106. }
  107. }
  108. fclose(fp);
  109. }
  110. return open(mtd, flags);
  111. }
  112. int mtd_check_open(const char *mtd)
  113. {
  114. struct mtd_info_user mtdInfo;
  115. int fd;
  116. fd = mtd_open(mtd, false);
  117. if(fd < 0) {
  118. fprintf(stderr, "Could not open mtd device: %s\n", mtd);
  119. return -1;
  120. }
  121. if(ioctl(fd, MEMGETINFO, &mtdInfo)) {
  122. fprintf(stderr, "Could not get MTD device info from %s\n", mtd);
  123. close(fd);
  124. return -1;
  125. }
  126. mtdsize = mtdInfo.size;
  127. erasesize = mtdInfo.erasesize;
  128. mtdtype = mtdInfo.type;
  129. return fd;
  130. }
  131. int mtd_block_is_bad(int fd, int offset)
  132. {
  133. int r = 0;
  134. loff_t o = offset;
  135. if (mtdtype == MTD_NANDFLASH)
  136. {
  137. r = ioctl(fd, MEMGETBADBLOCK, &o);
  138. if (r < 0)
  139. {
  140. fprintf(stderr, "Failed to get erase block status\n");
  141. exit(1);
  142. }
  143. }
  144. return r;
  145. }
  146. int mtd_erase_block(int fd, int offset)
  147. {
  148. struct erase_info_user mtdEraseInfo;
  149. mtdEraseInfo.start = offset;
  150. mtdEraseInfo.length = erasesize;
  151. ioctl(fd, MEMUNLOCK, &mtdEraseInfo);
  152. if (ioctl (fd, MEMERASE, &mtdEraseInfo) < 0)
  153. return -1;
  154. return 0;
  155. }
  156. int mtd_write_buffer(int fd, const char *buf, int offset, int length)
  157. {
  158. lseek(fd, offset, SEEK_SET);
  159. write(fd, buf, length);
  160. return 0;
  161. }
  162. static int
  163. image_check(int imagefd, const char *mtd)
  164. {
  165. uint32_t magic;
  166. int ret = 1;
  167. int bufread;
  168. while (buflen < sizeof(magic)) {
  169. bufread = read(imagefd, buf + buflen, sizeof(magic) - buflen);
  170. if (bufread < 1)
  171. break;
  172. buflen += bufread;
  173. }
  174. if (buflen < sizeof(magic)) {
  175. fprintf(stdout, "Could not get image magic\n");
  176. return 0;
  177. }
  178. magic = ((uint32_t *)buf)[0];
  179. if (be32_to_cpu(magic) == TRX_MAGIC)
  180. imageformat = MTD_IMAGE_FORMAT_TRX;
  181. else if (be32_to_cpu(magic) == SEAMA_MAGIC)
  182. imageformat = MTD_IMAGE_FORMAT_SEAMA;
  183. else if (le32_to_cpu(magic) == WRG_MAGIC)
  184. imageformat = MTD_IMAGE_FORMAT_WRG;
  185. else if (le32_to_cpu(magic) == WRGG03_MAGIC)
  186. imageformat = MTD_IMAGE_FORMAT_WRGG03;
  187. switch (imageformat) {
  188. case MTD_IMAGE_FORMAT_TRX:
  189. if (trx_check)
  190. ret = trx_check(imagefd, mtd, buf, &buflen);
  191. break;
  192. case MTD_IMAGE_FORMAT_SEAMA:
  193. case MTD_IMAGE_FORMAT_WRG:
  194. case MTD_IMAGE_FORMAT_WRGG03:
  195. break;
  196. default:
  197. #ifdef target_brcm
  198. if (!strcmp(mtd, "firmware"))
  199. ret = 0;
  200. #endif
  201. break;
  202. }
  203. return ret;
  204. }
  205. static int mtd_check(const char *mtd)
  206. {
  207. char *next = NULL;
  208. char *str = NULL;
  209. int fd;
  210. if (strchr(mtd, ':')) {
  211. str = strdup(mtd);
  212. mtd = str;
  213. }
  214. do {
  215. next = strchr(mtd, ':');
  216. if (next) {
  217. *next = 0;
  218. next++;
  219. }
  220. fd = mtd_check_open(mtd);
  221. if (fd < 0)
  222. return 0;
  223. if (!buf)
  224. buf = malloc(erasesize);
  225. close(fd);
  226. mtd = next;
  227. } while (next);
  228. if (str)
  229. free(str);
  230. return 1;
  231. }
  232. static int
  233. mtd_unlock(const char *mtd)
  234. {
  235. struct erase_info_user mtdLockInfo;
  236. char *next = NULL;
  237. char *str = NULL;
  238. int fd;
  239. if (strchr(mtd, ':')) {
  240. str = strdup(mtd);
  241. mtd = str;
  242. }
  243. do {
  244. next = strchr(mtd, ':');
  245. if (next) {
  246. *next = 0;
  247. next++;
  248. }
  249. fd = mtd_check_open(mtd);
  250. if(fd < 0) {
  251. fprintf(stderr, "Could not open mtd device: %s\n", mtd);
  252. exit(1);
  253. }
  254. if (quiet < 2)
  255. fprintf(stderr, "Unlocking %s ...\n", mtd);
  256. mtdLockInfo.start = 0;
  257. mtdLockInfo.length = mtdsize;
  258. ioctl(fd, MEMUNLOCK, &mtdLockInfo);
  259. close(fd);
  260. mtd = next;
  261. } while (next);
  262. if (str)
  263. free(str);
  264. return 0;
  265. }
  266. static int
  267. mtd_erase(const char *mtd)
  268. {
  269. int fd;
  270. struct erase_info_user mtdEraseInfo;
  271. if (quiet < 2)
  272. fprintf(stderr, "Erasing %s ...\n", mtd);
  273. fd = mtd_check_open(mtd);
  274. if(fd < 0) {
  275. fprintf(stderr, "Could not open mtd device: %s\n", mtd);
  276. exit(1);
  277. }
  278. mtdEraseInfo.length = erasesize;
  279. for (mtdEraseInfo.start = 0;
  280. mtdEraseInfo.start < mtdsize;
  281. mtdEraseInfo.start += erasesize) {
  282. if (mtd_block_is_bad(fd, mtdEraseInfo.start)) {
  283. if (!quiet)
  284. fprintf(stderr, "\nSkipping bad block at 0x%x ", mtdEraseInfo.start);
  285. } else {
  286. ioctl(fd, MEMUNLOCK, &mtdEraseInfo);
  287. if(ioctl(fd, MEMERASE, &mtdEraseInfo))
  288. fprintf(stderr, "Failed to erase block on %s at 0x%x\n", mtd, mtdEraseInfo.start);
  289. }
  290. }
  291. close(fd);
  292. return 0;
  293. }
  294. static int
  295. mtd_dump(const char *mtd, int part_offset, int size)
  296. {
  297. int ret = 0, offset = 0;
  298. int fd;
  299. char *buf;
  300. if (quiet < 2)
  301. fprintf(stderr, "Dumping %s ...\n", mtd);
  302. fd = mtd_check_open(mtd);
  303. if(fd < 0) {
  304. fprintf(stderr, "Could not open mtd device: %s\n", mtd);
  305. return -1;
  306. }
  307. if (!size)
  308. size = mtdsize;
  309. if (part_offset)
  310. lseek(fd, part_offset, SEEK_SET);
  311. buf = malloc(erasesize);
  312. if (!buf)
  313. return -1;
  314. do {
  315. int len = (size > erasesize) ? (erasesize) : (size);
  316. int rlen = read(fd, buf, len);
  317. if (rlen < 0) {
  318. if (errno == EINTR)
  319. continue;
  320. ret = -1;
  321. goto out;
  322. }
  323. if (!rlen || rlen != len)
  324. break;
  325. if (mtd_block_is_bad(fd, offset)) {
  326. fprintf(stderr, "skipping bad block at 0x%08x\n", offset);
  327. } else {
  328. size -= rlen;
  329. write(1, buf, rlen);
  330. }
  331. offset += rlen;
  332. } while (size > 0);
  333. out:
  334. close(fd);
  335. return ret;
  336. }
  337. static int
  338. mtd_verify(const char *mtd, char *file)
  339. {
  340. uint32_t f_md5[4], m_md5[4];
  341. struct stat s;
  342. md5_ctx_t ctx;
  343. int ret = 0;
  344. int fd;
  345. if (quiet < 2)
  346. fprintf(stderr, "Verifying %s against %s ...\n", mtd, file);
  347. if (stat(file, &s) || md5sum(file, f_md5) < 0) {
  348. fprintf(stderr, "Failed to hash %s\n", file);
  349. return -1;
  350. }
  351. fd = mtd_check_open(mtd);
  352. if(fd < 0) {
  353. fprintf(stderr, "Could not open mtd device: %s\n", mtd);
  354. return -1;
  355. }
  356. md5_begin(&ctx);
  357. do {
  358. char buf[256];
  359. int len = (s.st_size > sizeof(buf)) ? (sizeof(buf)) : (s.st_size);
  360. int rlen = read(fd, buf, len);
  361. if (rlen < 0) {
  362. if (errno == EINTR)
  363. continue;
  364. ret = -1;
  365. goto out;
  366. }
  367. if (!rlen)
  368. break;
  369. md5_hash(buf, rlen, &ctx);
  370. s.st_size -= rlen;
  371. } while (s.st_size > 0);
  372. md5_end(m_md5, &ctx);
  373. fprintf(stderr, "%08x%08x%08x%08x - %s\n", m_md5[0], m_md5[1], m_md5[2], m_md5[3], mtd);
  374. fprintf(stderr, "%08x%08x%08x%08x - %s\n", f_md5[0], f_md5[1], f_md5[2], f_md5[3], file);
  375. ret = memcmp(f_md5, m_md5, sizeof(m_md5));
  376. if (!ret)
  377. fprintf(stderr, "Success\n");
  378. else
  379. fprintf(stderr, "Failed\n");
  380. out:
  381. close(fd);
  382. return ret;
  383. }
  384. static void
  385. indicate_writing(const char *mtd)
  386. {
  387. if (quiet < 2)
  388. fprintf(stderr, "\nWriting from %s to %s ... ", imagefile, mtd);
  389. if (!quiet)
  390. fprintf(stderr, " [ ]");
  391. }
  392. static int
  393. mtd_write(int imagefd, const char *mtd, char *fis_layout, size_t part_offset)
  394. {
  395. char *next = NULL;
  396. char *str = NULL;
  397. int fd, result;
  398. ssize_t r, w, e;
  399. ssize_t skip = 0;
  400. uint32_t offset = 0;
  401. int jffs2_replaced = 0;
  402. int skip_bad_blocks = 0;
  403. #ifdef FIS_SUPPORT
  404. static struct fis_part new_parts[MAX_ARGS];
  405. static struct fis_part old_parts[MAX_ARGS];
  406. int n_new = 0, n_old = 0;
  407. if (fis_layout) {
  408. const char *tmp = mtd;
  409. char *word, *brkt;
  410. int ret;
  411. memset(&old_parts, 0, sizeof(old_parts));
  412. memset(&new_parts, 0, sizeof(new_parts));
  413. do {
  414. next = strchr(tmp, ':');
  415. if (!next)
  416. next = (char *) tmp + strlen(tmp);
  417. memcpy(old_parts[n_old].name, tmp, next - tmp);
  418. n_old++;
  419. tmp = next + 1;
  420. } while(*next);
  421. for (word = strtok_r(fis_layout, ",", &brkt);
  422. word;
  423. word = strtok_r(NULL, ",", &brkt)) {
  424. tmp = strtok(word, ":");
  425. strncpy((char *) new_parts[n_new].name, tmp, sizeof(new_parts[n_new].name) - 1);
  426. tmp = strtok(NULL, ":");
  427. if (!tmp)
  428. goto next;
  429. new_parts[n_new].size = strtoul(tmp, NULL, 0);
  430. tmp = strtok(NULL, ":");
  431. if (!tmp)
  432. goto next;
  433. new_parts[n_new].loadaddr = strtoul(tmp, NULL, 16);
  434. next:
  435. n_new++;
  436. }
  437. ret = fis_validate(old_parts, n_old, new_parts, n_new);
  438. if (ret < 0) {
  439. fprintf(stderr, "Failed to validate the new FIS partition table\n");
  440. exit(1);
  441. }
  442. if (ret == 0)
  443. fis_layout = NULL;
  444. }
  445. #endif
  446. if (strchr(mtd, ':')) {
  447. str = strdup(mtd);
  448. mtd = str;
  449. }
  450. r = 0;
  451. resume:
  452. next = strchr(mtd, ':');
  453. if (next) {
  454. *next = 0;
  455. next++;
  456. }
  457. fd = mtd_check_open(mtd);
  458. if(fd < 0) {
  459. fprintf(stderr, "Could not open mtd device: %s\n", mtd);
  460. exit(1);
  461. }
  462. if (part_offset > 0) {
  463. fprintf(stderr, "Seeking on mtd device '%s' to: %zu\n", mtd, part_offset);
  464. lseek(fd, part_offset, SEEK_SET);
  465. }
  466. indicate_writing(mtd);
  467. w = e = 0;
  468. for (;;) {
  469. /* buffer may contain data already (from trx check or last mtd partition write attempt) */
  470. while (buflen < erasesize) {
  471. r = read(imagefd, buf + buflen, erasesize - buflen);
  472. if (r < 0) {
  473. if ((errno == EINTR) || (errno == EAGAIN))
  474. continue;
  475. else {
  476. perror("read");
  477. break;
  478. }
  479. }
  480. if (r == 0)
  481. break;
  482. buflen += r;
  483. }
  484. if (buflen == 0)
  485. break;
  486. if (buflen < erasesize) {
  487. /* Pad block to eraseblock size */
  488. memset(&buf[buflen], 0xff, erasesize - buflen);
  489. buflen = erasesize;
  490. }
  491. if (skip > 0) {
  492. skip -= buflen;
  493. buflen = 0;
  494. if (skip <= 0)
  495. indicate_writing(mtd);
  496. continue;
  497. }
  498. if (jffs2file && w >= jffs2_skip_bytes) {
  499. if (memcmp(buf, JFFS2_EOF, sizeof(JFFS2_EOF) - 1) == 0) {
  500. if (!quiet)
  501. fprintf(stderr, "\b\b\b ");
  502. if (quiet < 2)
  503. fprintf(stderr, "\nAppending jffs2 data from %s to %s..\n.", jffs2file, mtd);
  504. /* got an EOF marker - this is the place to add some jffs2 data */
  505. skip = mtd_replace_jffs2(mtd, fd, e, jffs2file);
  506. jffs2_replaced = 1;
  507. /* don't add it again */
  508. jffs2file = NULL;
  509. w += skip;
  510. e += skip;
  511. skip -= buflen;
  512. buflen = 0;
  513. offset = 0;
  514. continue;
  515. }
  516. /* no EOF marker, make sure we figure out the last inode number
  517. * before appending some data */
  518. mtd_parse_jffs2data(buf, jffs2dir);
  519. }
  520. /* need to erase the next block before writing data to it */
  521. if(!no_erase)
  522. {
  523. while (w + buflen > e - skip_bad_blocks) {
  524. if (!quiet)
  525. fprintf(stderr, "\b\b\b[e]");
  526. if (mtd_block_is_bad(fd, e)) {
  527. if (!quiet)
  528. fprintf(stderr, "\nSkipping bad block at 0x%08zx ", e);
  529. skip_bad_blocks += erasesize;
  530. e += erasesize;
  531. // Move the file pointer along over the bad block.
  532. lseek(fd, erasesize, SEEK_CUR);
  533. continue;
  534. }
  535. if (mtd_erase_block(fd, e) < 0) {
  536. if (next) {
  537. if (w < e) {
  538. write(fd, buf + offset, e - w);
  539. offset = e - w;
  540. }
  541. w = 0;
  542. e = 0;
  543. close(fd);
  544. mtd = next;
  545. fprintf(stderr, "\b\b\b \n");
  546. goto resume;
  547. } else {
  548. fprintf(stderr, "Failed to erase block\n");
  549. exit(1);
  550. }
  551. }
  552. /* erase the chunk */
  553. e += erasesize;
  554. }
  555. }
  556. if (!quiet)
  557. fprintf(stderr, "\b\b\b[w]");
  558. if ((result = write(fd, buf + offset, buflen)) < buflen) {
  559. if (result < 0) {
  560. fprintf(stderr, "Error writing image.\n");
  561. exit(1);
  562. } else {
  563. fprintf(stderr, "Insufficient space.\n");
  564. exit(1);
  565. }
  566. }
  567. w += buflen;
  568. buflen = 0;
  569. offset = 0;
  570. }
  571. if (jffs2_replaced) {
  572. switch (imageformat) {
  573. case MTD_IMAGE_FORMAT_TRX:
  574. if (trx_fixup)
  575. trx_fixup(fd, mtd);
  576. break;
  577. case MTD_IMAGE_FORMAT_SEAMA:
  578. if (mtd_fixseama)
  579. mtd_fixseama(mtd, 0, 0);
  580. break;
  581. case MTD_IMAGE_FORMAT_WRG:
  582. if (mtd_fixwrg)
  583. mtd_fixwrg(mtd, 0, 0);
  584. break;
  585. case MTD_IMAGE_FORMAT_WRGG03:
  586. if (mtd_fixwrgg)
  587. mtd_fixwrgg(mtd, 0, 0);
  588. break;
  589. default:
  590. break;
  591. }
  592. }
  593. if (!quiet)
  594. fprintf(stderr, "\b\b\b\b ");
  595. if (quiet < 2)
  596. fprintf(stderr, "\n");
  597. #ifdef FIS_SUPPORT
  598. if (fis_layout) {
  599. if (fis_remap(old_parts, n_old, new_parts, n_new) < 0)
  600. fprintf(stderr, "Failed to update the FIS partition table\n");
  601. }
  602. #endif
  603. close(fd);
  604. return 0;
  605. }
  606. static void usage(void)
  607. {
  608. fprintf(stderr, "Usage: mtd [<options> ...] <command> [<arguments> ...] <device>[:<device>...]\n\n"
  609. "The device is in the format of mtdX (eg: mtd4) or its label.\n"
  610. "mtd recognizes these commands:\n"
  611. " unlock unlock the device\n"
  612. " refresh refresh mtd partition\n"
  613. " erase erase all data on device\n"
  614. " verify <imagefile>|- verify <imagefile> (use - for stdin) to device\n"
  615. " write <imagefile>|- write <imagefile> (use - for stdin) to device\n"
  616. " jffs2write <file> append <file> to the jffs2 partition on the device\n");
  617. if (mtd_resetbc) {
  618. fprintf(stderr,
  619. " resetbc <device> reset the uboot boot counter\n");
  620. }
  621. if (mtd_fixtrx) {
  622. fprintf(stderr,
  623. " fixtrx fix the checksum in a trx header on first boot\n");
  624. }
  625. if (mtd_fixseama) {
  626. fprintf(stderr,
  627. " fixseama fix the checksum in a seama header on first boot\n");
  628. }
  629. if (mtd_fixwrg) {
  630. fprintf(stderr,
  631. " fixwrg fix the checksum in a wrg header on first boot\n");
  632. }
  633. if (mtd_fixwrgg) {
  634. fprintf(stderr,
  635. " fixwrgg fix the checksum in a wrgg header on first boot\n");
  636. }
  637. fprintf(stderr,
  638. "Following options are available:\n"
  639. " -q quiet mode (once: no [w] on writing,\n"
  640. " twice: no status messages)\n"
  641. " -n write without first erasing the blocks\n"
  642. " -r reboot after successful command\n"
  643. " -f force write without trx checks\n"
  644. " -e <device> erase <device> before executing the command\n"
  645. " -d <name> directory for jffs2write, defaults to \"tmp\"\n"
  646. " -j <name> integrate <file> into jffs2 data when writing an image\n"
  647. " -s <number> skip the first n bytes when appending data to the jffs2 partiton, defaults to \"0\"\n"
  648. " -p <number> write beginning at partition offset\n"
  649. " -l <length> the length of data that we want to dump\n");
  650. if (mtd_fixtrx) {
  651. fprintf(stderr,
  652. " -o offset offset of the image header in the partition(for fixtrx)\n");
  653. }
  654. if (mtd_fixtrx || mtd_fixseama || mtd_fixwrg || mtd_fixwrgg) {
  655. fprintf(stderr,
  656. " -c datasize amount of data to be used for checksum calculation (for fixtrx / fixseama / fixwrg / fixwrgg)\n");
  657. }
  658. fprintf(stderr,
  659. #ifdef FIS_SUPPORT
  660. " -F <part>[:<size>[:<entrypoint>]][,<part>...]\n"
  661. " alter the fis partition table to create new partitions replacing\n"
  662. " the partitions provided as argument to the write command\n"
  663. " (only valid together with the write command)\n"
  664. #endif
  665. "\n"
  666. "Example: To write linux.trx to mtd4 labeled as linux and reboot afterwards\n"
  667. " mtd -r write linux.trx linux\n\n");
  668. exit(1);
  669. }
  670. static void do_reboot(void)
  671. {
  672. fprintf(stderr, "Rebooting ...\n");
  673. fflush(stderr);
  674. /* try regular reboot method first */
  675. system("/sbin/reboot");
  676. sleep(2);
  677. /* if we're still alive at this point, force the kernel to reboot */
  678. syscall(SYS_reboot,LINUX_REBOOT_MAGIC1,LINUX_REBOOT_MAGIC2,LINUX_REBOOT_CMD_RESTART,NULL);
  679. }
  680. int main (int argc, char **argv)
  681. {
  682. int ch, i, boot, imagefd = 0, force, unlocked;
  683. char *erase[MAX_ARGS], *device = NULL;
  684. char *fis_layout = NULL;
  685. size_t offset = 0, data_size = 0, part_offset = 0, dump_len = 0;
  686. enum {
  687. CMD_ERASE,
  688. CMD_WRITE,
  689. CMD_UNLOCK,
  690. CMD_JFFS2WRITE,
  691. CMD_FIXTRX,
  692. CMD_FIXSEAMA,
  693. CMD_FIXWRG,
  694. CMD_FIXWRGG,
  695. CMD_VERIFY,
  696. CMD_DUMP,
  697. CMD_RESETBC,
  698. } cmd = -1;
  699. erase[0] = NULL;
  700. boot = 0;
  701. force = 0;
  702. buflen = 0;
  703. quiet = 0;
  704. no_erase = 0;
  705. while ((ch = getopt(argc, argv,
  706. #ifdef FIS_SUPPORT
  707. "F:"
  708. #endif
  709. "frnqe:d:s:j:p:o:c:l:")) != -1)
  710. switch (ch) {
  711. case 'f':
  712. force = 1;
  713. break;
  714. case 'r':
  715. boot = 1;
  716. break;
  717. case 'n':
  718. no_erase = 1;
  719. break;
  720. case 'j':
  721. jffs2file = optarg;
  722. break;
  723. case 's':
  724. errno = 0;
  725. jffs2_skip_bytes = strtoul(optarg, 0, 0);
  726. if (errno) {
  727. fprintf(stderr, "-s: illegal numeric string\n");
  728. usage();
  729. }
  730. break;
  731. case 'q':
  732. quiet++;
  733. break;
  734. case 'e':
  735. i = 0;
  736. while ((erase[i] != NULL) && ((i + 1) < MAX_ARGS))
  737. i++;
  738. erase[i++] = optarg;
  739. erase[i] = NULL;
  740. break;
  741. case 'd':
  742. jffs2dir = optarg;
  743. break;
  744. case 'p':
  745. errno = 0;
  746. part_offset = strtoul(optarg, 0, 0);
  747. if (errno) {
  748. fprintf(stderr, "-p: illegal numeric string\n");
  749. usage();
  750. }
  751. break;
  752. case 'l':
  753. errno = 0;
  754. dump_len = strtoul(optarg, 0, 0);
  755. if (errno) {
  756. fprintf(stderr, "-l: illegal numeric string\n");
  757. usage();
  758. }
  759. break;
  760. case 'o':
  761. errno = 0;
  762. offset = strtoul(optarg, 0, 0);
  763. if (errno) {
  764. fprintf(stderr, "-o: illegal numeric string\n");
  765. usage();
  766. }
  767. break;
  768. case 'c':
  769. errno = 0;
  770. data_size = strtoul(optarg, 0, 0);
  771. if (errno) {
  772. fprintf(stderr, "-c: illegal numeric string\n");
  773. usage();
  774. }
  775. break;
  776. #ifdef FIS_SUPPORT
  777. case 'F':
  778. fis_layout = optarg;
  779. break;
  780. #endif
  781. case '?':
  782. default:
  783. usage();
  784. }
  785. argc -= optind;
  786. argv += optind;
  787. if (argc < 2)
  788. usage();
  789. if ((strcmp(argv[0], "unlock") == 0) && (argc == 2)) {
  790. cmd = CMD_UNLOCK;
  791. device = argv[1];
  792. } else if ((strcmp(argv[0], "erase") == 0) && (argc == 2)) {
  793. cmd = CMD_ERASE;
  794. device = argv[1];
  795. } else if (((strcmp(argv[0], "resetbc") == 0) && (argc == 2)) && mtd_resetbc) {
  796. cmd = CMD_RESETBC;
  797. device = argv[1];
  798. } else if (((strcmp(argv[0], "fixtrx") == 0) && (argc == 2)) && mtd_fixtrx) {
  799. cmd = CMD_FIXTRX;
  800. device = argv[1];
  801. } else if (((strcmp(argv[0], "fixseama") == 0) && (argc == 2)) && mtd_fixseama) {
  802. cmd = CMD_FIXSEAMA;
  803. device = argv[1];
  804. } else if (((strcmp(argv[0], "fixwrg") == 0) && (argc == 2)) && mtd_fixwrg) {
  805. cmd = CMD_FIXWRG;
  806. device = argv[1];
  807. } else if (((strcmp(argv[0], "fixwrgg") == 0) && (argc == 2)) && mtd_fixwrgg) {
  808. cmd = CMD_FIXWRGG;
  809. device = argv[1];
  810. } else if ((strcmp(argv[0], "verify") == 0) && (argc == 3)) {
  811. cmd = CMD_VERIFY;
  812. imagefile = argv[1];
  813. device = argv[2];
  814. } else if ((strcmp(argv[0], "dump") == 0) && (argc == 2)) {
  815. cmd = CMD_DUMP;
  816. device = argv[1];
  817. } else if ((strcmp(argv[0], "write") == 0) && (argc == 3)) {
  818. cmd = CMD_WRITE;
  819. device = argv[2];
  820. if (strcmp(argv[1], "-") == 0) {
  821. imagefile = "<stdin>";
  822. imagefd = 0;
  823. } else {
  824. imagefile = argv[1];
  825. if ((imagefd = open(argv[1], O_RDONLY)) < 0) {
  826. fprintf(stderr, "Couldn't open image file: %s!\n", imagefile);
  827. exit(1);
  828. }
  829. }
  830. if (!mtd_check(device)) {
  831. fprintf(stderr, "Can't open device for writing!\n");
  832. exit(1);
  833. }
  834. /* check trx file before erasing or writing anything */
  835. if (!image_check(imagefd, device) && !force) {
  836. fprintf(stderr, "Image check failed.\n");
  837. exit(1);
  838. }
  839. } else if ((strcmp(argv[0], "jffs2write") == 0) && (argc == 3)) {
  840. cmd = CMD_JFFS2WRITE;
  841. device = argv[2];
  842. imagefile = argv[1];
  843. if (!mtd_check(device)) {
  844. fprintf(stderr, "Can't open device for writing!\n");
  845. exit(1);
  846. }
  847. } else {
  848. usage();
  849. }
  850. sync();
  851. i = 0;
  852. unlocked = 0;
  853. while (erase[i] != NULL) {
  854. mtd_unlock(erase[i]);
  855. mtd_erase(erase[i]);
  856. if (strcmp(erase[i], device) == 0)
  857. unlocked = 1;
  858. i++;
  859. }
  860. switch (cmd) {
  861. case CMD_UNLOCK:
  862. if (!unlocked)
  863. mtd_unlock(device);
  864. break;
  865. case CMD_VERIFY:
  866. mtd_verify(device, imagefile);
  867. break;
  868. case CMD_DUMP:
  869. mtd_dump(device, offset, dump_len);
  870. break;
  871. case CMD_ERASE:
  872. if (!unlocked)
  873. mtd_unlock(device);
  874. mtd_erase(device);
  875. break;
  876. case CMD_WRITE:
  877. if (!unlocked)
  878. mtd_unlock(device);
  879. mtd_write(imagefd, device, fis_layout, part_offset);
  880. break;
  881. case CMD_JFFS2WRITE:
  882. if (!unlocked)
  883. mtd_unlock(device);
  884. mtd_write_jffs2(device, imagefile, jffs2dir);
  885. break;
  886. case CMD_FIXTRX:
  887. if (mtd_fixtrx) {
  888. mtd_fixtrx(device, offset, data_size);
  889. }
  890. break;
  891. case CMD_RESETBC:
  892. if (mtd_resetbc) {
  893. mtd_resetbc(device);
  894. }
  895. break;
  896. case CMD_FIXSEAMA:
  897. if (mtd_fixseama)
  898. mtd_fixseama(device, 0, data_size);
  899. break;
  900. case CMD_FIXWRG:
  901. if (mtd_fixwrg)
  902. mtd_fixwrg(device, 0, data_size);
  903. break;
  904. case CMD_FIXWRGG:
  905. if (mtd_fixwrgg)
  906. mtd_fixwrgg(device, 0, data_size);
  907. break;
  908. }
  909. sync();
  910. if (boot)
  911. do_reboot();
  912. return 0;
  913. }