Advertisement
Guest User

Untitled

a guest
May 20th, 2017
555
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 28.92 KB | None | 0 0
  1. Index: package/mtd/src/fis.c
  2. ===================================================================
  3. --- package/mtd/src/fis.c   (revision 20859)
  4. +++ package/mtd/src/fis.c   (working copy)
  5. @@ -42,51 +42,29 @@
  6.     struct fis_image_crc crc;
  7.  } __attribute__((packed));
  8.  
  9. -static int fis_fd = -1;
  10. +static struct mtd_device *fisdev = NULL;
  11.  static struct fis_image_desc *fis_desc;
  12. -static int fis_erasesize = 0;
  13.  
  14.  static void
  15.  fis_close(void)
  16.  {
  17. -   if (fis_desc)
  18. -       munmap(fis_desc, fis_erasesize);
  19. -
  20. -   if (fis_fd >= 0)
  21. -       close(fis_fd);
  22. -
  23. -   fis_fd = -1;
  24. +   mtd_close(fisdev);
  25. +   fisdev = NULL;
  26.     fis_desc = NULL;
  27.  }
  28.  
  29.  static struct fis_image_desc *
  30.  fis_open(void)
  31.  {
  32. -   struct fis_image_desc *desc;
  33. -
  34. -   if (fis_fd >= 0)
  35. +   if (fisdev)
  36.         fis_close();
  37.  
  38. -   fis_fd = mtd_check_open("FIS directory");
  39. -   if (fis_fd < 0)
  40. -       goto error;
  41. +   fisdev = mtd_open("FIS directory", true);
  42. +   if (!fisdev)
  43. +       return NULL;
  44.  
  45. -   close(fis_fd);
  46. -   fis_fd = mtd_open("FIS directory", true);
  47. -   if (fis_fd < 0)
  48. -       goto error;
  49. -
  50. -   fis_erasesize = erasesize;
  51. -   desc = mmap(NULL, erasesize, PROT_READ|PROT_WRITE, MAP_SHARED, fis_fd, 0);
  52. -   if (desc == MAP_FAILED)
  53. -       goto error;
  54. -
  55. -   fis_desc = desc;
  56. -   return desc;
  57. -
  58. -error:
  59. -   fis_close();
  60. -   return NULL;
  61. +   fis_desc = fisdev->mmap;;
  62. +   return fis_desc;
  63.  }
  64.  
  65.  int
  66. @@ -110,7 +88,7 @@
  67.     }
  68.  
  69.     end = desc;
  70. -   end = (char *) end + fis_erasesize;
  71. +   end = (char *) end + fisdev->info.erasesize;
  72.     while ((void *) desc < end) {
  73.         if (!desc->hdr.name[0] || (desc->hdr.name[0] == 0xff))
  74.             break;
  75. @@ -157,7 +135,7 @@
  76.         fprintf(stderr, "Updating FIS table... \n");
  77.  
  78.     start = (char *) desc;
  79. -   end = (char *) desc + fis_erasesize;
  80. +   end = (char *) desc + fisdev->info.erasesize;
  81.     while ((char *) desc < end) {
  82.         if (!desc->hdr.name[0] || (desc->hdr.name[0] == 0xff))
  83.             break;
  84. @@ -245,7 +223,7 @@
  85.         size -= desc->hdr.size;
  86.     }
  87.  
  88. -   msync(fis_desc, fis_erasesize, MS_SYNC|MS_INVALIDATE);
  89. +   msync(fis_desc, fisdev->info.erasesize, MS_SYNC|MS_INVALIDATE);
  90.     fis_close();
  91.  
  92.     return 0;
  93. Index: package/mtd/src/mtd.c
  94. ===================================================================
  95. --- package/mtd/src/mtd.c   (revision 20859)
  96. +++ package/mtd/src/mtd.c   (working copy)
  97. @@ -1,8 +1,9 @@
  98.  /*
  99.   * mtd - simple memory technology device manipulation tool
  100.   *
  101. - * Copyright (C) 2005      Waldemar Brodkorb <wbx@dass-it.de>,
  102. + * Copyright (C) 2005      Waldemar Brodkorb <wbx@dass-it.de>
  103.   * Copyright (C) 2005-2009 Felix Fietkau <nbd@openwrt.org>
  104. + * Copyright (C) 2010 Bernhard Loos <bernhardloos@googlemail.com>
  105.   *
  106.   * This program is free software; you can redistribute it and/or
  107.   * modify it under the terms of the GNU General Public License v2
  108. @@ -21,7 +22,6 @@
  109.   * The code is based on the linux-mtd examples.
  110.   */
  111.  
  112. -#include <limits.h>
  113.  #include <unistd.h>
  114.  #include <stdlib.h>
  115.  #include <stdio.h>
  116. @@ -31,350 +31,68 @@
  117.  #include <sys/syscall.h>
  118.  #include <fcntl.h>
  119.  #include <errno.h>
  120. -#include <error.h>
  121. -#include <time.h>
  122.  #include <string.h>
  123. -#include <sys/ioctl.h>
  124. -#include <sys/types.h>
  125. -#include <sys/param.h>
  126. -#include <sys/mount.h>
  127. -#include <sys/stat.h>
  128.  #include <sys/reboot.h>
  129.  #include <linux/reboot.h>
  130. -#include "mtd-api.h"
  131.  #include "fis.h"
  132.  #include "mtd.h"
  133. -#include "crc32.h"
  134.  
  135.  #define MAX_ARGS 8
  136.  #define JFFS2_DEFAULT_DIR  "" /* directory name without /, empty means root dir */
  137.  
  138. -#if __BYTE_ORDER == __BIG_ENDIAN
  139. -#define STORE32_LE(X)           ((((X) & 0x000000FF) << 24) | (((X) & 0x0000FF00) << 8) | (((X) & 0x00FF0000) >> 8) | (((X) & 0xFF000000) >> 24))
  140. -#elif __BYTE_ORDER == __LITTLE_ENDIAN
  141. -#define STORE32_LE(X)           (X)
  142. -#else
  143. -#error unkown endianness!
  144. -#endif
  145. -
  146. -ssize_t pread(int fd, void *buf, size_t count, off_t offset);
  147. -ssize_t pwrite(int fd, const void *buf, size_t count, off_t offset);
  148. -
  149. -#define TRX_MAGIC       0x30524448      /* "HDR0" */
  150. -struct trx_header {
  151. -   uint32_t magic;     /* "HDR0" */
  152. -   uint32_t len;       /* Length of file including header */
  153. -   uint32_t crc32;     /* 32-bit CRC from flag_version to end of file */
  154. -   uint32_t flag_version;  /* 0:15 flags, 16:31 version */
  155. -   uint32_t offsets[3];    /* Offsets of partitions from start of header */
  156. -};
  157. -
  158.  static char *buf = NULL;
  159.  static char *imagefile = NULL;
  160.  static char *jffs2file = NULL, *jffs2dir = JFFS2_DEFAULT_DIR;
  161.  static int buflen = 0;
  162.  int quiet;
  163. -int mtdsize = 0;
  164. -int erasesize = 0;
  165.  
  166. -int mtd_open(const char *mtd, bool block)
  167. -{
  168. -   FILE *fp;
  169. -   char dev[PATH_MAX];
  170. -   int i;
  171. -   int ret;
  172. -   int flags = O_RDWR | O_SYNC;
  173. +struct platform_imageops imageops __attribute__((weak)) = {
  174. +   .firmware_partition = "firmware"
  175. +};
  176.  
  177. -   if ((fp = fopen("/proc/mtd", "r"))) {
  178. -       while (fgets(dev, sizeof(dev), fp)) {
  179. -           if (sscanf(dev, "mtd%d:", &i) && strstr(dev, mtd)) {
  180. -               snprintf(dev, sizeof(dev), "/dev/mtd%s/%d", (block ? "block" : ""), i);
  181. -               if ((ret=open(dev, flags))<0) {
  182. -                   snprintf(dev, sizeof(dev), "/dev/mtd%s%d", (block ? "block" : ""), i);
  183. -                   ret=open(dev, flags);
  184. -               }
  185. -               fclose(fp);
  186. -               return ret;
  187. -           }
  188. -       }
  189. -       fclose(fp);
  190. -   }
  191. -
  192. -   return open(mtd, flags);
  193. -}
  194. -
  195. -int mtd_check_open(const char *mtd)
  196. -{
  197. -   struct mtd_info_user mtdInfo;
  198. -   int fd;
  199. -
  200. -   fd = mtd_open(mtd, false);
  201. -   if(fd < 0) {
  202. -       fprintf(stderr, "Could not open mtd device: %s\n", mtd);
  203. -       return -1;
  204. -   }
  205. -
  206. -   if(ioctl(fd, MEMGETINFO, &mtdInfo)) {
  207. -       fprintf(stderr, "Could not get MTD device info from %s\n", mtd);
  208. -       close(fd);
  209. -       return -1;
  210. -   }
  211. -   mtdsize = mtdInfo.size;
  212. -   erasesize = mtdInfo.erasesize;
  213. -
  214. -   return fd;
  215. -}
  216. -
  217. -int mtd_erase_block(int fd, int offset)
  218. -{
  219. -   struct erase_info_user mtdEraseInfo;
  220. -
  221. -   mtdEraseInfo.start = offset;
  222. -   mtdEraseInfo.length = erasesize;
  223. -   ioctl(fd, MEMUNLOCK, &mtdEraseInfo);
  224. -   if (ioctl (fd, MEMERASE, &mtdEraseInfo) < 0)
  225. -       return -1;
  226. -
  227. -   return 0;
  228. -}
  229. -
  230. -int mtd_write_buffer(int fd, const char *buf, int offset, int length)
  231. -{
  232. -   lseek(fd, offset, SEEK_SET);
  233. -   write(fd, buf, length);
  234. -   return 0;
  235. -}
  236. -
  237. -
  238.  static int
  239. -image_check(int imagefd, const char *mtd)
  240. +cmd_erase(struct mtd_device **mtddevs)
  241.  {
  242. -   int ret = 1;
  243. -#ifdef target_brcm
  244. -   ret = trx_check(imagefd, mtd, buf, &buflen);
  245. -#endif
  246. -   return ret;
  247. -}
  248. -
  249. -static int mtd_check(const char *mtd)
  250. -{
  251. -   char *next = NULL;
  252. -   char *str = NULL;
  253. -   int fd;
  254. -
  255. -   if (strchr(mtd, ':')) {
  256. -       str = strdup(mtd);
  257. -       mtd = str;
  258. -   }
  259. -
  260. -   do {
  261. -       next = strchr(mtd, ':');
  262. -       if (next) {
  263. -           *next = 0;
  264. -           next++;
  265. -       }
  266. -
  267. -       fd = mtd_check_open(mtd);
  268. -       if (fd < 0)
  269. -           return 0;
  270. -
  271. -       if (!buf)
  272. -           buf = malloc(erasesize);
  273. -
  274. -       close(fd);
  275. -       mtd = next;
  276. -   } while (next);
  277. -
  278. -   if (str)
  279. -       free(str);
  280. -
  281. -   return 1;
  282. -}
  283. -
  284. -static int
  285. -mtd_unlock(const char *mtd)
  286. -{
  287. -   struct erase_info_user mtdLockInfo;
  288. -   char *next = NULL;
  289. -   char *str = NULL;
  290. -   int fd;
  291. -
  292. -   if (strchr(mtd, ':')) {
  293. -       str = strdup(mtd);
  294. -       mtd = str;
  295. -   }
  296. -
  297. -   do {
  298. -       next = strchr(mtd, ':');
  299. -       if (next) {
  300. -           *next = 0;
  301. -           next++;
  302. -       }
  303. -
  304. -       fd = mtd_check_open(mtd);
  305. -       if(fd < 0) {
  306. -           fprintf(stderr, "Could not open mtd device: %s\n", mtd);
  307. -           exit(1);
  308. -       }
  309. -
  310. +   int i = 0;
  311. +   int ret;
  312. +  
  313. +   while (mtddevs[i]) {
  314.         if (quiet < 2)
  315. -           fprintf(stderr, "Unlocking %s ...\n", mtd);
  316. -
  317. -       mtdLockInfo.start = 0;
  318. -       mtdLockInfo.length = mtdsize;
  319. -       ioctl(fd, MEMUNLOCK, &mtdLockInfo);
  320. -       close(fd);
  321. -       mtd = next;
  322. -   } while (next);
  323. -
  324. -   if (str)
  325. -       free(str);
  326. -
  327. -   return 0;
  328. -}
  329. -
  330. -static int
  331. -mtd_erase(const char *mtd)
  332. -{
  333. -   int fd;
  334. -   struct erase_info_user mtdEraseInfo;
  335. -
  336. -   if (quiet < 2)
  337. -       fprintf(stderr, "Erasing %s ...\n", mtd);
  338. -
  339. -   fd = mtd_check_open(mtd);
  340. -   if(fd < 0) {
  341. -       fprintf(stderr, "Could not open mtd device: %s\n", mtd);
  342. -       exit(1);
  343. +           fprintf(stderr, "Erasing %s ...\n", mtddevs[i]->name);
  344. +      
  345. +       ret = mtd_erase(mtddevs[i++]);
  346. +       if (ret < 0)
  347. +           return ret;
  348.     }
  349. -
  350. -   mtdEraseInfo.length = erasesize;
  351. -
  352. -   for (mtdEraseInfo.start = 0;
  353. -        mtdEraseInfo.start < mtdsize;
  354. -        mtdEraseInfo.start += erasesize) {
  355. -
  356. -       ioctl(fd, MEMUNLOCK, &mtdEraseInfo);
  357. -       if(ioctl(fd, MEMERASE, &mtdEraseInfo))
  358. -           fprintf(stderr, "Failed to erase block on %s at 0x%x\n", mtd, mtdEraseInfo.start);
  359. -   }
  360. -
  361. -   close(fd);
  362. +  
  363.     return 0;
  364. -
  365.  }
  366.  
  367.  static int
  368. -mtd_fixtrx(const char *mtd, size_t offset)
  369. +cmd_refresh(struct mtd_device **mtddevs)
  370.  {
  371. -   int fd;
  372. -   struct trx_header *trx;
  373. -   char *buf;
  374. -   ssize_t res;
  375. -   size_t block_offset;
  376. -
  377. -   if (quiet < 2)
  378. -       fprintf(stderr, "Trying to fix trx header in %s at 0x%x...\n", mtd, offset);
  379. -
  380. -   block_offset = offset & ~(erasesize - 1);
  381. -   offset -= block_offset;
  382. -
  383. -   fd = mtd_check_open(mtd);
  384. -   if(fd < 0) {
  385. -       fprintf(stderr, "Could not open mtd device: %s\n", mtd);
  386. -       exit(1);
  387. -   }
  388. -
  389. -   if (block_offset + erasesize > mtdsize) {
  390. -       fprintf(stderr, "Offset too large, device size 0x%x\n", mtdsize);
  391. -       exit(1);
  392. -   }
  393. -
  394. -   buf = malloc(erasesize);
  395. -   if (!buf) {
  396. -       perror("malloc");
  397. -       exit(1);
  398. -   }
  399. -
  400. -   res = pread(fd, buf, erasesize, block_offset);
  401. -   if (res != erasesize) {
  402. -       perror("pread");
  403. -       exit(1);
  404. -   }
  405. -
  406. -   trx = (struct trx_header *) (buf + offset);
  407. -   if (trx->magic != STORE32_LE(0x30524448)) {
  408. -       fprintf(stderr, "No trx magic found\n");
  409. -       exit(1);
  410. -   }
  411. -
  412. -   if (trx->len == STORE32_LE(erasesize - offset)) {
  413. +   int i = 0;
  414. +   int ret;
  415. +  
  416. +   while (mtddevs[i]) {
  417.         if (quiet < 2)
  418. -           fprintf(stderr, "Header already fixed, exiting\n");
  419. -       close(fd);
  420. -       return 0;
  421. +           fprintf(stderr, "Refreshing mtd partition %s ... \n", mtddevs[i]->name);
  422. +      
  423. +       ret = ioctl(mtddevs[i++]->fd, MTDREFRESH, NULL);
  424. +       if (ret < 0)
  425. +           return ret;
  426.     }
  427. -
  428. -   trx->len = STORE32_LE(erasesize - offset);
  429. -
  430. -   trx->crc32 = STORE32_LE(crc32buf((char*) &trx->flag_version, erasesize - offset - 3*4));
  431. -   if (mtd_erase_block(fd, block_offset)) {
  432. -       fprintf(stderr, "Can't erease block at 0x%x (%s)\n", block_offset, strerror(errno));
  433. -       exit(1);
  434. -   }
  435. -
  436. -   if (quiet < 2)
  437. -       fprintf(stderr, "New crc32: 0x%x, rewriting block\n", trx->crc32);
  438. -
  439. -   if (pwrite(fd, buf, erasesize, block_offset) != erasesize) {
  440. -       fprintf(stderr, "Error writing block (%s)\n", strerror(errno));
  441. -       exit(1);
  442. -   }
  443. -
  444. -   if (quiet < 2)
  445. -       fprintf(stderr, "Done.\n");
  446. -
  447. -   close (fd);
  448. -   sync();
  449. +  
  450.     return 0;
  451. -
  452.  }
  453.  
  454.  static int
  455. -mtd_refresh(const char *mtd)
  456. +cmd_write(int imagefd, struct mtd_device **mtddevs, char *fis_layout)
  457.  {
  458. -   int fd;
  459. -
  460. -   if (quiet < 2)
  461. -       fprintf(stderr, "Refreshing mtd partition %s ... ", mtd);
  462. -
  463. -   fd = mtd_check_open(mtd);
  464. -   if(fd < 0) {
  465. -       fprintf(stderr, "Could not open mtd device: %s\n", mtd);
  466. -       exit(1);
  467. -   }
  468. -
  469. -   if (ioctl(fd, MTDREFRESH, NULL)) {
  470. -       fprintf(stderr, "Failed to refresh the MTD device\n");
  471. -       close(fd);
  472. -       exit(1);
  473. -   }
  474. -   close(fd);
  475. -
  476. -   if (quiet < 2)
  477. -       fprintf(stderr, "\n");
  478. -
  479. -   return 0;
  480. -}
  481. -
  482. -static int
  483. -mtd_write(int imagefd, const char *mtd, char *fis_layout)
  484. -{
  485. -   char *next = NULL;
  486. -   char *str = NULL;
  487. -   int fd, result;
  488. +   int result;
  489.     ssize_t r, w, e;
  490.     uint32_t offset = 0;
  491. +   int i;
  492.  
  493.  #ifdef FIS_SUPPORT
  494.     static struct fis_part new_parts[MAX_ARGS];
  495. @@ -382,24 +100,19 @@
  496.     int n_new = 0, n_old = 0;
  497.  
  498.     if (fis_layout) {
  499. -       const char *tmp = mtd;
  500. +       const char *tmp;
  501.         char *word, *brkt;
  502.         int ret;
  503.  
  504.         memset(&old_parts, 0, sizeof(old_parts));
  505.         memset(&new_parts, 0, sizeof(new_parts));
  506.  
  507. -       do {
  508. -           next = strchr(tmp, ':');
  509. -           if (!next)
  510. -               next = (char *) tmp + strlen(tmp);
  511. +       i = 0;
  512. +       while (mtddevs[i]) {
  513. +           memcpy(old_parts[n_old++].name, mtddevs[i]->name, strlen(mtddevs[i]->name));
  514. +           i++;
  515. +       }
  516.  
  517. -           memcpy(old_parts[n_old].name, tmp, next - tmp);
  518. -
  519. -           n_old++;
  520. -           tmp = next + 1;
  521. -       } while(*next);
  522. -
  523.         for (word = strtok_r(fis_layout, ",", &brkt);
  524.              word;
  525.              word = strtok_r(NULL, ",", &brkt)) {
  526. @@ -431,28 +144,11 @@
  527.     }
  528.  #endif
  529.  
  530. -   if (strchr(mtd, ':')) {
  531. -       str = strdup(mtd);
  532. -       mtd = str;
  533. -   }
  534. -
  535.     r = 0;
  536. -
  537. +   i = 0;
  538.  resume:
  539. -   next = strchr(mtd, ':');
  540. -   if (next) {
  541. -       *next = 0;
  542. -       next++;
  543. -   }
  544. -
  545. -   fd = mtd_check_open(mtd);
  546. -   if(fd < 0) {
  547. -       fprintf(stderr, "Could not open mtd device: %s\n", mtd);
  548. -       exit(1);
  549. -   }
  550. -
  551.     if (quiet < 2)
  552. -       fprintf(stderr, "Writing from %s to %s ... ", imagefile, mtd);
  553. +       fprintf(stderr, "Writing from %s to %s ... ", imagefile, mtddevs[i]->name);
  554.  
  555.     w = e = 0;
  556.     if (!quiet)
  557. @@ -460,8 +156,8 @@
  558.  
  559.     for (;;) {
  560.         /* buffer may contain data already (from trx check or last mtd partition write attempt) */
  561. -       while (buflen < erasesize) {
  562. -           r = read(imagefd, buf + buflen, erasesize - buflen);
  563. +       while (buflen < mtddevs[i]->info.erasesize) {
  564. +           r = read(imagefd, buf + buflen, mtddevs[i]->info.erasesize - buflen);
  565.             if (r < 0) {
  566.                 if ((errno == EINTR) || (errno == EAGAIN))
  567.                     continue;
  568. @@ -485,9 +181,9 @@
  569.                 if (!quiet)
  570.                     fprintf(stderr, "\b\b\b   ");
  571.                 if (quiet < 2)
  572. -                   fprintf(stderr, "\nAppending jffs2 data from %s to %s...", jffs2file, mtd);
  573. +                   fprintf(stderr, "\nAppending jffs2 data from %s to %s...", jffs2file, mtddevs[i]->name);
  574.                 /* got an EOF marker - this is the place to add some jffs2 data */
  575. -               mtd_replace_jffs2(mtd, fd, e, jffs2file);
  576. +               mtd_replace_jffs2(mtddevs[i], e, jffs2file);
  577.                 goto done;
  578.             }
  579.             /* no EOF marker, make sure we figure out the last inode number
  580. @@ -501,16 +197,15 @@
  581.                 fprintf(stderr, "\b\b\b[e]");
  582.  
  583.  
  584. -           if (mtd_erase_block(fd, e) < 0) {
  585. -               if (next) {
  586. +           if (mtd_erase_block(mtddevs[i], e) < 0) {
  587. +               if (mtddevs[i + 1]) {
  588.                     if (w < e) {
  589. -                       write(fd, buf + offset, e - w);
  590. +                       write(mtddevs[i]->fd, buf + offset, e - w);
  591.                         offset = e - w;
  592.                     }
  593.                     w = 0;
  594.                     e = 0;
  595. -                   close(fd);
  596. -                   mtd = next;
  597. +                   i++;
  598.                     fprintf(stderr, "\b\b\b   \n");
  599.                     goto resume;
  600.                 } else {
  601. @@ -520,13 +215,13 @@
  602.             }
  603.  
  604.             /* erase the chunk */
  605. -           e += erasesize;
  606. +           e += mtddevs[i]->info.erasesize;
  607.         }
  608.  
  609.         if (!quiet)
  610.             fprintf(stderr, "\b\b\b[w]");
  611.  
  612. -       if ((result = write(fd, buf + offset, buflen)) < buflen) {
  613. +       if ((result = write(mtddevs[i]->fd, buf + offset, buflen)) < buflen) {
  614.             if (result < 0) {
  615.                 fprintf(stderr, "Error writing image.\n");
  616.                 exit(1);
  617. @@ -555,7 +250,6 @@
  618.     }
  619.  #endif
  620.  
  621. -   close(fd);
  622.     return 0;
  623.  }
  624.  
  625. @@ -569,7 +263,7 @@
  626.     "        erase                   erase all data on device\n"
  627.     "        write <imagefile>|-     write <imagefile> (use - for stdin) to device\n"
  628.     "        jffs2write <file>       append <file> to the jffs2 partition on the device\n"
  629. -   "        fixtrx                  fix the checksum in a trx header on first boot\n"
  630. +   "        fixcrc                  fix the checksum in the image header on first boot\n"
  631.     "Following options are available:\n"
  632.     "        -q                      quiet mode (once: no [w] on writing,\n"
  633.     "                                           twice: no status messages)\n"
  634. @@ -578,7 +272,6 @@
  635.     "        -e <device>             erase <device> before executing the command\n"
  636.     "        -d <name>               directory for jffs2write, defaults to \"tmp\"\n"
  637.     "        -j <name>               integrate <file> into jffs2 data when writing an image\n"
  638. -   "        -o offset               offset of the trx header in the partition (for fixtrx)\n"
  639.  #ifdef FIS_SUPPORT
  640.     "        -F <part>[:<size>[:<entrypoint>]][,<part>...]\n"
  641.     "                                alter the fis partition table to create new partitions replacing\n"
  642. @@ -606,19 +299,19 @@
  643.  
  644.  int main (int argc, char **argv)
  645.  {
  646. -   int ch, i, boot, imagefd = 0, force, unlocked;
  647. +   int ch, i, boot, imagefd = 0, force;
  648.     char *erase[MAX_ARGS], *device = NULL;
  649.     char *fis_layout = NULL;
  650. -   size_t offset = 0;
  651. +   struct mtd_device **mtddevs;
  652.     enum {
  653.         CMD_ERASE,
  654.         CMD_WRITE,
  655.         CMD_UNLOCK,
  656.         CMD_REFRESH,
  657.         CMD_JFFS2WRITE,
  658. -       CMD_FIXTRX,
  659. +       CMD_FIXCRC,
  660.     } cmd = -1;
  661. -
  662. +  
  663.     erase[0] = NULL;
  664.     boot = 0;
  665.     force = 0;
  666. @@ -629,7 +322,7 @@
  667.  #ifdef FIS_SUPPORT
  668.             "F:"
  669.  #endif
  670. -           "frqe:d:j:o:")) != -1)
  671. +           "frqe:d:j:")) != -1)
  672.         switch (ch) {
  673.             case 'f':
  674.                 force = 1;
  675. @@ -654,14 +347,6 @@
  676.             case 'd':
  677.                 jffs2dir = optarg;
  678.                 break;
  679. -           case 'o':
  680. -               errno = 0;
  681. -               offset = strtoul(optarg, 0, 0);
  682. -               if (errno) {
  683. -                   fprintf(stderr, "-o: illegal numeric string\n");
  684. -                   usage();
  685. -               }
  686. -               break;
  687.  #ifdef FIS_SUPPORT
  688.             case 'F':
  689.                 fis_layout = optarg;
  690. @@ -686,8 +371,8 @@
  691.     } else if ((strcmp(argv[0], "erase") == 0) && (argc == 2)) {
  692.         cmd = CMD_ERASE;
  693.         device = argv[1];
  694. -   } else if ((strcmp(argv[0], "fixtrx") == 0) && (argc == 2)) {
  695. -       cmd = CMD_FIXTRX;
  696. +   } else if ((strcmp(argv[0], "fixcrc") == 0) && (argc == 2)) {
  697. +       cmd = CMD_FIXCRC;
  698.         device = argv[1];
  699.     } else if ((strcmp(argv[0], "write") == 0) && (argc == 3)) {
  700.         cmd = CMD_WRITE;
  701. @@ -703,71 +388,122 @@
  702.                 exit(1);
  703.             }
  704.         }
  705. -
  706. -       if (!mtd_check(device)) {
  707. -           fprintf(stderr, "Can't open device for writing!\n");
  708. -           exit(1);
  709. -       }
  710. -       /* check trx file before erasing or writing anything */
  711. -       if (!image_check(imagefd, device) && !force) {
  712. -           fprintf(stderr, "Image check failed.\n");
  713. -           exit(1);
  714. -       }
  715.     } else if ((strcmp(argv[0], "jffs2write") == 0) && (argc == 3)) {
  716.         cmd = CMD_JFFS2WRITE;
  717.         device = argv[2];
  718.  
  719.         imagefile = argv[1];
  720. -       if (!mtd_check(device)) {
  721. -           fprintf(stderr, "Can't open device for writing!\n");
  722. -           exit(1);
  723. -       }
  724.     } else {
  725.         usage();
  726.     }
  727.  
  728. +   mtddevs = mtd_open_multiple(device);
  729. +   if (!mtddevs)
  730. +       exit(1);
  731. +  
  732. +   buf = malloc(mtddevs[0]->info.erasesize);
  733. +   if (!buf) {
  734. +       perror("malloc");
  735. +       exit(1);
  736. +   }
  737. +  
  738. +   if (cmd == CMD_WRITE && strcmp(device, imageops.firmware_partition) == 0) {
  739. +       size_t mtd_length = 0;
  740. +       int i = 0;
  741. +      
  742. +       while (mtddevs[i])
  743. +           mtd_length += mtddevs[i]->info.size;
  744. +      
  745. +       buflen = read(imagefd, buf, 256);
  746. +       if (buflen < 256) {
  747. +           fprintf(stdout, "Could not get image header, file too small (%d bytes)\n", buflen);
  748. +           exit(1);
  749. +       }
  750. +       /* check image file before erasing or writing anything */
  751. +       if (imageops.checkimage && imageops.checkimage(buf, buflen, mtd_length) && !force) {
  752. +           fprintf(stderr, "Image check failed.\n");
  753. +           exit(1);
  754. +       }
  755. +   }
  756. +
  757. +  
  758. +
  759.     sync();
  760.  
  761.     i = 0;
  762. -   unlocked = 0;
  763.     while (erase[i] != NULL) {
  764. -       mtd_unlock(erase[i]);
  765. -       mtd_erase(erase[i]);
  766. -       if (strcmp(erase[i], device) == 0)
  767. -           unlocked = 1;
  768. +       struct mtd_device *erasedev;
  769. +       int j = 0;
  770. +      
  771. +       while (mtddevs[j]) {
  772. +           if (strcmp(erase[i], mtddevs[j]->name) == 0)
  773. +               erasedev =  mtddevs[j];
  774. +       }
  775. +       if (!mtddevs[j])
  776. +           erasedev = mtd_open(erase[i], false);
  777. +       if (!erasedev)
  778. +           exit(1);
  779. +       mtd_unlock(erasedev);
  780. +       mtd_erase(erasedev);
  781. +       if (!mtddevs[j])
  782. +           mtd_close(erasedev);
  783. +
  784.         i++;
  785.     }
  786. -
  787. +  
  788. +   i = 0;
  789. +   while (mtddevs[i]) {
  790. +       if (quiet < 2)
  791. +           fprintf(stderr, "Unlocking %s ...\n", mtddevs[i]->name);
  792. +       mtd_unlock(mtddevs[i++]);
  793. +   }
  794. +  
  795.     switch (cmd) {
  796.         case CMD_UNLOCK:
  797. -           if (!unlocked)
  798. -               mtd_unlock(device);
  799. +           /* nothing to do */
  800.             break;
  801.         case CMD_ERASE:
  802. -           if (!unlocked)
  803. -               mtd_unlock(device);
  804. -           mtd_erase(device);
  805. +           cmd_erase(mtddevs);
  806.             break;
  807.         case CMD_WRITE:
  808. -           if (!unlocked)
  809. -               mtd_unlock(device);
  810. -           mtd_write(imagefd, device, fis_layout);
  811. +           cmd_write(imagefd, mtddevs, fis_layout);
  812.             break;
  813.         case CMD_JFFS2WRITE:
  814. -           if (!unlocked)
  815. -               mtd_unlock(device);
  816. -           mtd_write_jffs2(device, imagefile, jffs2dir);
  817. +           cmd_write_jffs2(mtddevs, imagefile, jffs2dir);
  818.             break;
  819.         case CMD_REFRESH:
  820. -           mtd_refresh(device);
  821. +           cmd_refresh(mtddevs);
  822.             break;
  823. -       case CMD_FIXTRX:
  824. -           mtd_fixtrx(device, offset);
  825. +       case CMD_FIXCRC:
  826. +           /* nothing to do */
  827.             break;
  828.     }
  829. +  
  830. +   mtd_close_multiple(mtddevs);
  831.  
  832.     sync();
  833.  
  834. +   if ((cmd == CMD_FIXCRC || cmd == CMD_WRITE || cmd == CMD_JFFS2WRITE) && imageops.fixcrc) {
  835. +       struct mtd_device *mtd;
  836. +       int ret;
  837. +      
  838. +       mtd = mtd_open(imageops.firmware_partition, true);
  839. +      
  840. +       if (quiet < 2)
  841. +           fprintf(stderr, "Fixing crc in imageheader on %s ...\n", device);
  842. +      
  843. +       ret = imageops.fixcrc((char *) mtd->mmap, mtd->info.erasesize);
  844. +       if (ret > 0) {
  845. +           if (quiet < 2)
  846. +               fprintf(stderr, "Already fixed\n");
  847. +       } else if (ret < 0)
  848. +           fprintf(stderr, "CRC Fixup failed!\n");
  849. +      
  850. +       mtd_close(mtd);
  851. +          
  852. +       sync();
  853. +   }
  854. +
  855.     if (boot)
  856.         do_reboot();
  857.  
  858. Index: package/mtd/src/mtd.h
  859. ===================================================================
  860. --- package/mtd/src/mtd.h   (revision 20859)
  861. +++ package/mtd/src/mtd.h   (working copy)
  862. @@ -3,6 +3,8 @@
  863.  
  864.  #include <stdbool.h>
  865.  
  866. +#include "mtd-api.h"
  867. +
  868.  #ifdef target_brcm47xx
  869.  #define target_brcm 1
  870.  #endif
  871. @@ -10,19 +12,31 @@
  872.  #define JFFS2_EOF "\xde\xad\xc0\xde"
  873.  
  874.  extern int quiet;
  875. -extern int mtdsize;
  876. -extern int erasesize;
  877.  
  878. -extern int mtd_open(const char *mtd, bool block);
  879. -extern int mtd_check_open(const char *mtd);
  880. -extern int mtd_erase_block(int fd, int offset);
  881. -extern int mtd_write_buffer(int fd, const char *buf, int offset, int length);
  882. -extern int mtd_write_jffs2(const char *mtd, const char *filename, const char *dir);
  883. -extern int mtd_replace_jffs2(const char *mtd, int fd, int ofs, const char *filename);
  884. +struct mtd_device {
  885. +   const char *name;
  886. +   int fd;
  887. +   struct mtd_info_user info;
  888. +   int unlocked    : 1;
  889. +   void *mmap; /* only first eraseblock */
  890. +};
  891. +
  892. +extern struct mtd_device *mtd_open(const char *mtd, bool block);
  893. +extern struct mtd_device **mtd_open_multiple(const char *mtds);
  894. +extern void mtd_close_multiple(struct mtd_device **mtds);
  895. +extern void mtd_close(struct mtd_device *mtd);
  896. +extern int mtd_erase_block(struct mtd_device *mtd, size_t offset);
  897. +extern int mtd_erase(struct mtd_device *mtd);
  898. +extern int mtd_unlock(struct mtd_device *mtd);
  899. +
  900. +extern int cmd_write_jffs2(struct mtd_device **mtds, const char *filename, const char *dir);
  901. +extern int mtd_replace_jffs2(struct mtd_device *mtd, int ofs, const char *filename);
  902.  extern void mtd_parse_jffs2data(const char *buf, const char *dir);
  903.  
  904. -/* target specific */
  905. -extern int trx_fixup(int fd, const char *name);
  906. -extern int trx_check(int imagefd, const char *mtd, char *buf, int *len);
  907. +struct platform_imageops {
  908. +   int (*fixcrc)(char *data, size_t length);
  909. +   int (*checkimage)(const char *imagedata, size_t datalength, size_t mtdlength);
  910. +   const char *firmware_partition;
  911. +};
  912.  
  913.  #endif /* __mtd_h */
  914. Index: package/mtd/src/jffs2.c
  915. ===================================================================
  916. --- package/mtd/src/jffs2.c (revision 20859)
  917. +++ package/mtd/src/jffs2.c (working copy)
  918. @@ -42,7 +42,8 @@
  919.  static int last_version = 0;
  920.  static char *buf = NULL;
  921.  static int ofs = 0;
  922. -static int outfd = -1;
  923. +static struct mtd_device *mtddev = NULL;
  924. +static int erasesize = 0;
  925.  static int mtdofs = 0;
  926.  static int target_ino = 0;
  927.  
  928. @@ -59,8 +60,8 @@
  929.     }
  930.     ofs = ofs % erasesize;
  931.     if (ofs == 0) {
  932. -       mtd_erase_block(outfd, mtdofs);
  933. -       write(outfd, buf, erasesize);
  934. +       mtd_erase_block(mtddev, mtdofs);
  935. +       write(mtddev->fd, buf, erasesize);
  936.         mtdofs += erasesize;
  937.     }
  938.  }
  939. @@ -227,9 +228,10 @@
  940.     close(fd);
  941.  }
  942.  
  943. -int mtd_replace_jffs2(const char *mtd, int fd, int ofs, const char *filename)
  944. +int mtd_replace_jffs2(struct mtd_device *mtd, int ofs, const char *filename)
  945.  {
  946. -   outfd = fd;
  947. +   mtddev = mtd;
  948. +   erasesize = mtd->info.erasesize;
  949.     mtdofs = ofs;
  950.  
  951.     buf = malloc(erasesize);
  952. @@ -244,9 +246,6 @@
  953.     pad(erasesize);
  954.     free(buf);
  955.  
  956. -#ifdef target_brcm
  957. -   trx_fixup(outfd, mtd);
  958. -#endif
  959.     return 0;
  960.  }
  961.  
  962. @@ -277,16 +276,15 @@
  963.     }
  964.  }
  965.  
  966. -int mtd_write_jffs2(const char *mtd, const char *filename, const char *dir)
  967. +int cmd_write_jffs2(struct mtd_device **mtd, const char *filename, const char *dir)
  968.  {
  969.     int err = -1, fdeof = 0;
  970.  
  971. -   outfd = mtd_check_open(mtd);
  972. -   if (outfd < 0)
  973. -       return -1;
  974. +   mtddev = mtd[0];
  975. +   erasesize = mtddev->info.erasesize;
  976.  
  977.     if (quiet < 2)
  978. -       fprintf(stderr, "Appending %s to jffs2 partition %s\n", filename, mtd);
  979. +       fprintf(stderr, "Appending %s to jffs2 partition %s\n", filename, mtddev->name);
  980.    
  981.     buf = malloc(erasesize);
  982.     if (!buf) {
  983. @@ -302,7 +300,7 @@
  984.     for(;;) {
  985.         struct jffs2_unknown_node *node = (struct jffs2_unknown_node *) buf;
  986.  
  987. -       if (read(outfd, buf, erasesize) != erasesize) {
  988. +       if (read(mtddev->fd, buf, erasesize) != erasesize) {
  989.             fdeof = 1;
  990.             break;
  991.         }
  992. @@ -328,7 +326,7 @@
  993.  
  994.     /* jump back one eraseblock */
  995.     mtdofs -= erasesize;
  996. -   lseek(outfd, mtdofs, SEEK_SET);
  997. +   lseek(mtddev->fd, mtdofs, SEEK_SET);
  998.  
  999.     ofs = 0;
  1000.  
  1001. @@ -347,12 +345,7 @@
  1002.  
  1003.     err = 0;
  1004.  
  1005. -#ifdef target_brcm
  1006. -   trx_fixup(outfd, mtd);
  1007. -#endif
  1008. -
  1009.  done:
  1010. -   close(outfd);
  1011.     if (buf)
  1012.         free(buf);
  1013.  
  1014. Index: package/mtd/src/Makefile
  1015. ===================================================================
  1016. --- package/mtd/src/Makefile    (revision 20859)
  1017. +++ package/mtd/src/Makefile    (working copy)
  1018. @@ -1,9 +1,10 @@
  1019.  CC = gcc
  1020.  CFLAGS += -Wall
  1021.  
  1022. -obj = mtd.o jffs2.o crc32.o
  1023. +obj = mtd.o jffs2.o crc32.o libmtd.o
  1024.  obj.brcm = trx.o
  1025.  obj.brcm47xx = $(obj.brcm)
  1026. +obj.ar71xx = $(obj.brcm)
  1027.  
  1028.  ifdef FIS_SUPPORT
  1029.    obj += fis.o
  1030. Index: package/mtd/src/trx.c
  1031. ===================================================================
  1032. --- package/mtd/src/trx.c   (revision 20859)
  1033. +++ package/mtd/src/trx.c   (working copy)
  1034. @@ -3,6 +3,7 @@
  1035.   *
  1036.   * Copyright (C) 2005 Mike Baker
  1037.   * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
  1038. + * Copyright (C) 2010 Bernhard Loos <bernhardloos@googlemail.com>
  1039.   *
  1040.   * This program is free software; you can redistribute it and/or
  1041.   * modify it under the terms of the GNU General Public License
  1042. @@ -22,17 +23,16 @@
  1043.  #include <stdio.h>
  1044.  #include <stdlib.h>
  1045.  #include <stddef.h>
  1046. -#include <unistd.h>
  1047. -#include <fcntl.h>
  1048. -#include <sys/mman.h>
  1049. -#include <sys/stat.h>
  1050. -#include <string.h>
  1051.  
  1052. -#include <sys/ioctl.h>
  1053. -#include "mtd-api.h"
  1054.  #include "mtd.h"
  1055.  #include "crc32.h"
  1056.  
  1057. +#if __BYTE_ORDER == __BIG_ENDIAN
  1058. +#  define STORE32_LE(val) bswap_32(val)
  1059. +#else
  1060. +#  define STORE32_LE(val) (val)
  1061. +#endif
  1062. +
  1063.  #define TRX_MAGIC       0x30524448      /* "HDR0" */
  1064.  struct trx_header {
  1065.     unsigned magic;     /* "HDR0" */
  1066. @@ -42,69 +42,45 @@
  1067.     unsigned offsets[3];    /* Offsets of partitions from start of header */
  1068.  };
  1069.  
  1070. -int
  1071. -trx_fixup(int fd, const char *name)
  1072. +static int fixcrc_trx(char *data, size_t length)
  1073.  {
  1074. -   struct mtd_info_user mtdInfo;
  1075. -   unsigned long len;
  1076.     struct trx_header *trx;
  1077. -   void *ptr, *scan;
  1078. -   int bfd;
  1079.  
  1080. -   if (ioctl(fd, MEMGETINFO, &mtdInfo) < 0) {
  1081. -       fprintf(stderr, "Failed to get mtd info\n");
  1082. -       goto err;
  1083. -   }
  1084. +#ifdef TARGET_ar71xx
  1085. +   data += 32; /* used on the wrt160nl */
  1086. +   length -= 32;
  1087. +#endif
  1088.  
  1089. -   len = mtdInfo.size;
  1090. -   if (mtdInfo.size <= 0) {
  1091. -       fprintf(stderr, "Invalid MTD device size\n");
  1092. -       goto err;
  1093. +   trx = (struct trx_header *) (data);
  1094. +   if (trx->magic != STORE32_LE(TRX_MAGIC)) {
  1095. +       fprintf(stderr, "No trx magic found\n");
  1096. +       return -1;
  1097.     }
  1098. +  
  1099. +   if (trx->len == STORE32_LE(length))
  1100. +       return 1;
  1101.  
  1102. -   bfd = mtd_open(name, true);
  1103. -   ptr = mmap(NULL, len, PROT_READ|PROT_WRITE, MAP_SHARED, bfd, 0);
  1104. -   if (!ptr || (ptr == (void *) -1)) {
  1105. -       perror("mmap");
  1106. -       goto err1;
  1107. -   }
  1108. +   trx->len = STORE32_LE(length);
  1109. +  
  1110. +   trx->crc32 = STORE32_LE(crc32buf((char*) &trx->flag_version, length - 3*4));
  1111. +   if (quiet < 2)
  1112. +       fprintf(stderr, "New crc32: 0x%x, rewriting block\n", trx->crc32);
  1113.  
  1114. -   trx = ptr;
  1115. -   if (trx->magic != TRX_MAGIC) {
  1116. -       fprintf(stderr, "TRX header not found\n");
  1117. -       goto err;
  1118. -   }
  1119. -
  1120. -   scan = ptr + offsetof(struct trx_header, flag_version);
  1121. -   trx->crc32 = crc32buf(scan, trx->len - (scan - ptr));
  1122. -   msync(ptr, sizeof(struct trx_header), MS_SYNC|MS_INVALIDATE);
  1123. -   munmap(ptr, len);
  1124. -   close(bfd);
  1125.     return 0;
  1126. -
  1127. -err1:
  1128. -   close(bfd);
  1129. -err:
  1130. -   fprintf(stderr, "Error fixing up TRX header\n");
  1131. -   return -1;
  1132.  }
  1133.  
  1134. -int
  1135. -trx_check(int imagefd, const char *mtd, char *buf, int *len)
  1136. +static int
  1137. +trx_check(const char *imagedata, size_t datalength, size_t mtdlength)
  1138.  {
  1139. -   const struct trx_header *trx = (const struct trx_header *) buf;
  1140. -   int fd;
  1141. +   const struct trx_header *trx;
  1142.  
  1143. -   if (strcmp(mtd, "linux") != 0)
  1144. -       return 1;
  1145. +#ifdef TARGET_ar71xx
  1146. +   imagedata += 32;    /* used on the wrt160nl */
  1147. +   datelength -= 32;
  1148. +#endif
  1149. +   trx = (const struct trx_header *) imagedata;
  1150.  
  1151. -   *len = read(imagefd, buf, 32);
  1152. -   if (*len < 32) {
  1153. -       fprintf(stdout, "Could not get image header, file too small (%d bytes)\n", *len);
  1154. -       return 0;
  1155. -   }
  1156. -
  1157. -   if (trx->magic != TRX_MAGIC || trx->len < sizeof(struct trx_header)) {
  1158. +   if (trx->magic != STORE32_LE(TRX_MAGIC) || STORE32_LE(trx->len) < sizeof(struct trx_header)) {
  1159.         if (quiet < 2) {
  1160.             fprintf(stderr, "Bad trx header\n");
  1161.             fprintf(stderr, "This is not the correct file format; refusing to flash.\n"
  1162. @@ -113,20 +89,16 @@
  1163.         return 0;
  1164.     }
  1165.  
  1166. -   /* check if image fits to mtd device */
  1167. -   fd = mtd_check_open(mtd);
  1168. -   if(fd < 0) {
  1169. -       fprintf(stderr, "Could not open mtd device: %s\n", mtd);
  1170. -       exit(1);
  1171. -   }
  1172. -
  1173. -   if(mtdsize < trx->len) {
  1174. -       fprintf(stderr, "Image too big for partition: %s\n", mtd);
  1175. -       close(fd);
  1176. +   if(mtdlength < STORE32_LE(trx->len)) {
  1177. +       fprintf(stderr, "Image too big\n");
  1178.         return 0;
  1179.     }
  1180.  
  1181. -   close(fd);
  1182.     return 1;
  1183.  }
  1184.  
  1185. +struct platform_imageops imageops = {
  1186. +   .fixcrc = fixcrc_trx,
  1187. +   .checkimage = trx_check,
  1188. +   .firmware_partition = "linux"
  1189. +};
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement