Advertisement
Guest User

Untitled

a guest
May 19th, 2017
548
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 33.54 KB | None | 0 0
  1. Index: package/mtd/src/fis.c
  2. ===================================================================
  3. --- package/mtd/src/fis.c   (revision 20847)
  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 20847)
  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,66 @@
  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.  
  175. -   if ((fp = fopen("/proc/mtd", "r"))) {
  176. -       while (fgets(dev, sizeof(dev), fp)) {
  177. -           if (sscanf(dev, "mtd%d:", &i) && strstr(dev, mtd)) {
  178. -               snprintf(dev, sizeof(dev), "/dev/mtd%s/%d", (block ? "block" : ""), i);
  179. -               if ((ret=open(dev, flags))<0) {
  180. -                   snprintf(dev, sizeof(dev), "/dev/mtd%s%d", (block ? "block" : ""), i);
  181. -                   ret=open(dev, flags);
  182. -               }
  183. -               fclose(fp);
  184. -               return ret;
  185. -           }
  186. -       }
  187. -       fclose(fp);
  188. -   }
  189. -
  190. -   return open(mtd, flags);
  191. -}
  192. -
  193. -int mtd_check_open(const char *mtd)
  194. -{
  195. -   struct mtd_info_user mtdInfo;
  196. -   int fd;
  197. -
  198. -   fd = mtd_open(mtd, false);
  199. -   if(fd < 0) {
  200. -       fprintf(stderr, "Could not open mtd device: %s\n", mtd);
  201. -       return -1;
  202. -   }
  203. -
  204. -   if(ioctl(fd, MEMGETINFO, &mtdInfo)) {
  205. -       fprintf(stderr, "Could not get MTD device info from %s\n", mtd);
  206. -       close(fd);
  207. -       return -1;
  208. -   }
  209. -   mtdsize = mtdInfo.size;
  210. -   erasesize = mtdInfo.erasesize;
  211. -
  212. -   return fd;
  213. -}
  214. -
  215. -int mtd_erase_block(int fd, int offset)
  216. -{
  217. -   struct erase_info_user mtdEraseInfo;
  218. -
  219. -   mtdEraseInfo.start = offset;
  220. -   mtdEraseInfo.length = erasesize;
  221. -   ioctl(fd, MEMUNLOCK, &mtdEraseInfo);
  222. -   if (ioctl (fd, MEMERASE, &mtdEraseInfo) < 0)
  223. -       return -1;
  224. -
  225. -   return 0;
  226. -}
  227. -
  228. -int mtd_write_buffer(int fd, const char *buf, int offset, int length)
  229. -{
  230. -   lseek(fd, offset, SEEK_SET);
  231. -   write(fd, buf, length);
  232. -   return 0;
  233. -}
  234. -
  235. -
  236.  static int
  237. -image_check(int imagefd, const char *mtd)
  238. +cmd_erase(struct mtd_device **mtddevs)
  239.  {
  240. -   int ret = 1;
  241. -#ifdef target_brcm
  242. -   ret = trx_check(imagefd, mtd, buf, &buflen);
  243. -#endif
  244. -   return ret;
  245. -}
  246. -
  247. -static int mtd_check(const char *mtd)
  248. -{
  249. -   char *next = NULL;
  250. -   char *str = NULL;
  251. -   int fd;
  252. -
  253. -   if (strchr(mtd, ':')) {
  254. -       str = strdup(mtd);
  255. -       mtd = str;
  256. -   }
  257. -
  258. -   do {
  259. -       next = strchr(mtd, ':');
  260. -       if (next) {
  261. -           *next = 0;
  262. -           next++;
  263. -       }
  264. -
  265. -       fd = mtd_check_open(mtd);
  266. -       if (fd < 0)
  267. -           return 0;
  268. -
  269. -       if (!buf)
  270. -           buf = malloc(erasesize);
  271. -
  272. -       close(fd);
  273. -       mtd = next;
  274. -   } while (next);
  275. -
  276. -   if (str)
  277. -       free(str);
  278. -
  279. -   return 1;
  280. -}
  281. -
  282. -static int
  283. -mtd_unlock(const char *mtd)
  284. -{
  285. -   struct erase_info_user mtdLockInfo;
  286. -   char *next = NULL;
  287. -   char *str = NULL;
  288. -   int fd;
  289. -
  290. -   if (strchr(mtd, ':')) {
  291. -       str = strdup(mtd);
  292. -       mtd = str;
  293. -   }
  294. -
  295. -   do {
  296. -       next = strchr(mtd, ':');
  297. -       if (next) {
  298. -           *next = 0;
  299. -           next++;
  300. -       }
  301. -
  302. -       fd = mtd_check_open(mtd);
  303. -       if(fd < 0) {
  304. -           fprintf(stderr, "Could not open mtd device: %s\n", mtd);
  305. -           exit(1);
  306. -       }
  307. -
  308. +   int i = 0;
  309. +   int ret;
  310. +  
  311. +   while (mtddevs[i]) {
  312.         if (quiet < 2)
  313. -           fprintf(stderr, "Unlocking %s ...\n", mtd);
  314. -
  315. -       mtdLockInfo.start = 0;
  316. -       mtdLockInfo.length = mtdsize;
  317. -       ioctl(fd, MEMUNLOCK, &mtdLockInfo);
  318. -       close(fd);
  319. -       mtd = next;
  320. -   } while (next);
  321. -
  322. -   if (str)
  323. -       free(str);
  324. -
  325. -   return 0;
  326. -}
  327. -
  328. -static int
  329. -mtd_erase(const char *mtd)
  330. -{
  331. -   int fd;
  332. -   struct erase_info_user mtdEraseInfo;
  333. -
  334. -   if (quiet < 2)
  335. -       fprintf(stderr, "Erasing %s ...\n", mtd);
  336. -
  337. -   fd = mtd_check_open(mtd);
  338. -   if(fd < 0) {
  339. -       fprintf(stderr, "Could not open mtd device: %s\n", mtd);
  340. -       exit(1);
  341. +           fprintf(stderr, "Erasing %s ...\n", mtddevs[i]->name);
  342. +      
  343. +       ret = mtd_erase(mtddevs[i++]);
  344. +       if (ret < 0)
  345. +           return ret;
  346.     }
  347. -
  348. -   mtdEraseInfo.length = erasesize;
  349. -
  350. -   for (mtdEraseInfo.start = 0;
  351. -        mtdEraseInfo.start < mtdsize;
  352. -        mtdEraseInfo.start += erasesize) {
  353. -
  354. -       ioctl(fd, MEMUNLOCK, &mtdEraseInfo);
  355. -       if(ioctl(fd, MEMERASE, &mtdEraseInfo))
  356. -           fprintf(stderr, "Failed to erase block on %s at 0x%x\n", mtd, mtdEraseInfo.start);
  357. -   }
  358. -
  359. -   close(fd);
  360. +  
  361.     return 0;
  362. -
  363.  }
  364.  
  365.  static int
  366. -mtd_fixtrx(const char *mtd, size_t offset)
  367. +cmd_refresh(struct mtd_device **mtddevs)
  368.  {
  369. -   int fd;
  370. -   struct trx_header *trx;
  371. -   char *buf;
  372. -   ssize_t res;
  373. -   size_t block_offset;
  374. -
  375. -   if (quiet < 2)
  376. -       fprintf(stderr, "Trying to fix trx header in %s at 0x%x...\n", mtd, offset);
  377. -
  378. -   block_offset = offset & ~(erasesize - 1);
  379. -   offset -= block_offset;
  380. -
  381. -   fd = mtd_check_open(mtd);
  382. -   if(fd < 0) {
  383. -       fprintf(stderr, "Could not open mtd device: %s\n", mtd);
  384. -       exit(1);
  385. -   }
  386. -
  387. -   if (block_offset + erasesize > mtdsize) {
  388. -       fprintf(stderr, "Offset too large, device size 0x%x\n", mtdsize);
  389. -       exit(1);
  390. -   }
  391. -
  392. -   buf = malloc(erasesize);
  393. -   if (!buf) {
  394. -       perror("malloc");
  395. -       exit(1);
  396. -   }
  397. -
  398. -   res = pread(fd, buf, erasesize, block_offset);
  399. -   if (res != erasesize) {
  400. -       perror("pread");
  401. -       exit(1);
  402. -   }
  403. -
  404. -   trx = (struct trx_header *) (buf + offset);
  405. -   if (trx->magic != STORE32_LE(0x30524448)) {
  406. -       fprintf(stderr, "No trx magic found\n");
  407. -       exit(1);
  408. -   }
  409. -
  410. -   if (trx->len == STORE32_LE(erasesize - offset)) {
  411. +   int i = 0;
  412. +   int ret;
  413. +  
  414. +   while (mtddevs[i]) {
  415.         if (quiet < 2)
  416. -           fprintf(stderr, "Header already fixed, exiting\n");
  417. -       close(fd);
  418. -       return 0;
  419. +           fprintf(stderr, "Refreshing mtd partition %s ... \n", mtddevs[i]->name);
  420. +      
  421. +       ret = ioctl(mtddevs[i++]->fd, MTDREFRESH, NULL);
  422. +       if (ret < 0)
  423. +           return ret;
  424.     }
  425. -
  426. -   trx->len = STORE32_LE(erasesize - offset);
  427. -
  428. -   trx->crc32 = STORE32_LE(crc32buf((char*) &trx->flag_version, erasesize - offset - 3*4));
  429. -   if (mtd_erase_block(fd, block_offset)) {
  430. -       fprintf(stderr, "Can't erease block at 0x%x (%s)\n", block_offset, strerror(errno));
  431. -       exit(1);
  432. -   }
  433. -
  434. -   if (quiet < 2)
  435. -       fprintf(stderr, "New crc32: 0x%x, rewriting block\n", trx->crc32);
  436. -
  437. -   if (pwrite(fd, buf, erasesize, block_offset) != erasesize) {
  438. -       fprintf(stderr, "Error writing block (%s)\n", strerror(errno));
  439. -       exit(1);
  440. -   }
  441. -
  442. -   if (quiet < 2)
  443. -       fprintf(stderr, "Done.\n");
  444. -
  445. -   close (fd);
  446. -   sync();
  447. +  
  448.     return 0;
  449. -
  450.  }
  451.  
  452.  static int
  453. -mtd_refresh(const char *mtd)
  454. +cmd_write(int imagefd, struct mtd_device **mtddevs, char *fis_layout)
  455.  {
  456. -   int fd;
  457. -
  458. -   if (quiet < 2)
  459. -       fprintf(stderr, "Refreshing mtd partition %s ... ", mtd);
  460. -
  461. -   fd = mtd_check_open(mtd);
  462. -   if(fd < 0) {
  463. -       fprintf(stderr, "Could not open mtd device: %s\n", mtd);
  464. -       exit(1);
  465. -   }
  466. -
  467. -   if (ioctl(fd, MTDREFRESH, NULL)) {
  468. -       fprintf(stderr, "Failed to refresh the MTD device\n");
  469. -       close(fd);
  470. -       exit(1);
  471. -   }
  472. -   close(fd);
  473. -
  474. -   if (quiet < 2)
  475. -       fprintf(stderr, "\n");
  476. -
  477. -   return 0;
  478. -}
  479. -
  480. -static int
  481. -mtd_write(int imagefd, const char *mtd, char *fis_layout)
  482. -{
  483. -   char *next = NULL;
  484. -   char *str = NULL;
  485. -   int fd, result;
  486. +   int result;
  487.     ssize_t r, w, e;
  488.     uint32_t offset = 0;
  489. +   int i;
  490.  
  491.  #ifdef FIS_SUPPORT
  492.     static struct fis_part new_parts[MAX_ARGS];
  493. @@ -382,24 +98,19 @@
  494.     int n_new = 0, n_old = 0;
  495.  
  496.     if (fis_layout) {
  497. -       const char *tmp = mtd;
  498. +       const char *tmp;
  499.         char *word, *brkt;
  500.         int ret;
  501.  
  502.         memset(&old_parts, 0, sizeof(old_parts));
  503.         memset(&new_parts, 0, sizeof(new_parts));
  504.  
  505. -       do {
  506. -           next = strchr(tmp, ':');
  507. -           if (!next)
  508. -               next = (char *) tmp + strlen(tmp);
  509. +       i = 0;
  510. +       while (mtddevs[i]) {
  511. +           memcpy(old_parts[n_old++].name, mtddevs[i]->name, strlen(mtddevs[i]->name));
  512. +           i++;
  513. +       }
  514.  
  515. -           memcpy(old_parts[n_old].name, tmp, next - tmp);
  516. -
  517. -           n_old++;
  518. -           tmp = next + 1;
  519. -       } while(*next);
  520. -
  521.         for (word = strtok_r(fis_layout, ",", &brkt);
  522.              word;
  523.              word = strtok_r(NULL, ",", &brkt)) {
  524. @@ -431,28 +142,11 @@
  525.     }
  526.  #endif
  527.  
  528. -   if (strchr(mtd, ':')) {
  529. -       str = strdup(mtd);
  530. -       mtd = str;
  531. -   }
  532. -
  533.     r = 0;
  534. -
  535. +   i = 0;
  536.  resume:
  537. -   next = strchr(mtd, ':');
  538. -   if (next) {
  539. -       *next = 0;
  540. -       next++;
  541. -   }
  542. -
  543. -   fd = mtd_check_open(mtd);
  544. -   if(fd < 0) {
  545. -       fprintf(stderr, "Could not open mtd device: %s\n", mtd);
  546. -       exit(1);
  547. -   }
  548. -
  549.     if (quiet < 2)
  550. -       fprintf(stderr, "Writing from %s to %s ... ", imagefile, mtd);
  551. +       fprintf(stderr, "Writing from %s to %s ... ", imagefile, mtddevs[i]->name);
  552.  
  553.     w = e = 0;
  554.     if (!quiet)
  555. @@ -460,8 +154,8 @@
  556.  
  557.     for (;;) {
  558.         /* buffer may contain data already (from trx check or last mtd partition write attempt) */
  559. -       while (buflen < erasesize) {
  560. -           r = read(imagefd, buf + buflen, erasesize - buflen);
  561. +       while (buflen < mtddevs[i]->info.erasesize) {
  562. +           r = read(imagefd, buf + buflen, mtddevs[i]->info.erasesize - buflen);
  563.             if (r < 0) {
  564.                 if ((errno == EINTR) || (errno == EAGAIN))
  565.                     continue;
  566. @@ -485,9 +179,9 @@
  567.                 if (!quiet)
  568.                     fprintf(stderr, "\b\b\b   ");
  569.                 if (quiet < 2)
  570. -                   fprintf(stderr, "\nAppending jffs2 data from %s to %s...", jffs2file, mtd);
  571. +                   fprintf(stderr, "\nAppending jffs2 data from %s to %s...", jffs2file, mtddevs[i]->name);
  572.                 /* got an EOF marker - this is the place to add some jffs2 data */
  573. -               mtd_replace_jffs2(mtd, fd, e, jffs2file);
  574. +               mtd_replace_jffs2(mtddevs[i], e, jffs2file);
  575.                 goto done;
  576.             }
  577.             /* no EOF marker, make sure we figure out the last inode number
  578. @@ -501,16 +195,15 @@
  579.                 fprintf(stderr, "\b\b\b[e]");
  580.  
  581.  
  582. -           if (mtd_erase_block(fd, e) < 0) {
  583. -               if (next) {
  584. +           if (mtd_erase_block(mtddevs[i], e) < 0) {
  585. +               if (mtddevs[i + 1]) {
  586.                     if (w < e) {
  587. -                       write(fd, buf + offset, e - w);
  588. +                       write(mtddevs[i]->fd, buf + offset, e - w);
  589.                         offset = e - w;
  590.                     }
  591.                     w = 0;
  592.                     e = 0;
  593. -                   close(fd);
  594. -                   mtd = next;
  595. +                   i++;
  596.                     fprintf(stderr, "\b\b\b   \n");
  597.                     goto resume;
  598.                 } else {
  599. @@ -520,13 +213,13 @@
  600.             }
  601.  
  602.             /* erase the chunk */
  603. -           e += erasesize;
  604. +           e += mtddevs[i]->info.erasesize;
  605.         }
  606.  
  607.         if (!quiet)
  608.             fprintf(stderr, "\b\b\b[w]");
  609.  
  610. -       if ((result = write(fd, buf + offset, buflen)) < buflen) {
  611. +       if ((result = write(mtddevs[i]->fd, buf + offset, buflen)) < buflen) {
  612.             if (result < 0) {
  613.                 fprintf(stderr, "Error writing image.\n");
  614.                 exit(1);
  615. @@ -555,7 +248,6 @@
  616.     }
  617.  #endif
  618.  
  619. -   close(fd);
  620.     return 0;
  621.  }
  622.  
  623. @@ -569,7 +261,7 @@
  624.     "        erase                   erase all data on device\n"
  625.     "        write <imagefile>|-     write <imagefile> (use - for stdin) to device\n"
  626.     "        jffs2write <file>       append <file> to the jffs2 partition on the device\n"
  627. -   "        fixtrx                  fix the checksum in a trx header on first boot\n"
  628. +   "        fixcrc                  fix the checksum in the image header on first boot\n"
  629.     "Following options are available:\n"
  630.     "        -q                      quiet mode (once: no [w] on writing,\n"
  631.     "                                           twice: no status messages)\n"
  632. @@ -578,7 +270,6 @@
  633.     "        -e <device>             erase <device> before executing the command\n"
  634.     "        -d <name>               directory for jffs2write, defaults to \"tmp\"\n"
  635.     "        -j <name>               integrate <file> into jffs2 data when writing an image\n"
  636. -   "        -o offset               offset of the trx header in the partition (for fixtrx)\n"
  637.  #ifdef FIS_SUPPORT
  638.     "        -F <part>[:<size>[:<entrypoint>]][,<part>...]\n"
  639.     "                                alter the fis partition table to create new partitions replacing\n"
  640. @@ -606,19 +297,19 @@
  641.  
  642.  int main (int argc, char **argv)
  643.  {
  644. -   int ch, i, boot, imagefd = 0, force, unlocked;
  645. +   int ch, i, boot, imagefd = 0, force;
  646.     char *erase[MAX_ARGS], *device = NULL;
  647.     char *fis_layout = NULL;
  648. -   size_t offset = 0;
  649. +   struct mtd_device **mtddevs;
  650.     enum {
  651.         CMD_ERASE,
  652.         CMD_WRITE,
  653.         CMD_UNLOCK,
  654.         CMD_REFRESH,
  655.         CMD_JFFS2WRITE,
  656. -       CMD_FIXTRX,
  657. +       CMD_FIXCRC,
  658.     } cmd = -1;
  659. -
  660. +  
  661.     erase[0] = NULL;
  662.     boot = 0;
  663.     force = 0;
  664. @@ -629,7 +320,7 @@
  665.  #ifdef FIS_SUPPORT
  666.             "F:"
  667.  #endif
  668. -           "frqe:d:j:o:")) != -1)
  669. +           "frqe:d:j:")) != -1)
  670.         switch (ch) {
  671.             case 'f':
  672.                 force = 1;
  673. @@ -654,14 +345,6 @@
  674.             case 'd':
  675.                 jffs2dir = optarg;
  676.                 break;
  677. -           case 'o':
  678. -               errno = 0;
  679. -               offset = strtoul(optarg, 0, 0);
  680. -               if (errno) {
  681. -                   fprintf(stderr, "-o: illegal numeric string\n");
  682. -                   usage();
  683. -               }
  684. -               break;
  685.  #ifdef FIS_SUPPORT
  686.             case 'F':
  687.                 fis_layout = optarg;
  688. @@ -686,8 +369,8 @@
  689.     } else if ((strcmp(argv[0], "erase") == 0) && (argc == 2)) {
  690.         cmd = CMD_ERASE;
  691.         device = argv[1];
  692. -   } else if ((strcmp(argv[0], "fixtrx") == 0) && (argc == 2)) {
  693. -       cmd = CMD_FIXTRX;
  694. +   } else if ((strcmp(argv[0], "fixcrc") == 0) && (argc == 2)) {
  695. +       cmd = CMD_FIXCRC;
  696.         device = argv[1];
  697.     } else if ((strcmp(argv[0], "write") == 0) && (argc == 3)) {
  698.         cmd = CMD_WRITE;
  699. @@ -703,71 +386,120 @@
  700.                 exit(1);
  701.             }
  702.         }
  703. -
  704. -       if (!mtd_check(device)) {
  705. -           fprintf(stderr, "Can't open device for writing!\n");
  706. -           exit(1);
  707. -       }
  708. -       /* check trx file before erasing or writing anything */
  709. -       if (!image_check(imagefd, device) && !force) {
  710. -           fprintf(stderr, "Image check failed.\n");
  711. -           exit(1);
  712. -       }
  713.     } else if ((strcmp(argv[0], "jffs2write") == 0) && (argc == 3)) {
  714.         cmd = CMD_JFFS2WRITE;
  715.         device = argv[2];
  716.  
  717.         imagefile = argv[1];
  718. -       if (!mtd_check(device)) {
  719. -           fprintf(stderr, "Can't open device for writing!\n");
  720. -           exit(1);
  721. -       }
  722.     } else {
  723.         usage();
  724.     }
  725.  
  726. +   mtddevs = mtd_open_multiple(device);
  727. +   if (!mtddevs)
  728. +       exit(1);
  729. +  
  730. +   buf = malloc(mtddevs[0]->info.erasesize);
  731. +   if (!buf) {
  732. +       perror("malloc");
  733. +       exit(1);
  734. +   }
  735. +  
  736. +   if (cmd == CMD_WRITE && (strcmp(mtddevs[0]->name, "firmware") == 0 || strcmp(mtddevs[0]->name, "linux") == 0)) {
  737. +       buflen = read(imagefd, buf, 256);
  738. +       if (buflen < 256) {
  739. +           fprintf(stdout, "Could not get image header, file too small (%d bytes)\n", buflen);
  740. +           exit(1);
  741. +       }
  742. +       /* check image file before erasing or writing anything */
  743. +       if (imageops.checkimage && imageops.checkimage(buf, buflen, mtddevs[0]->info.size) && !force) {
  744. +           fprintf(stderr, "Image check failed.\n");
  745. +           exit(1);
  746. +       }
  747. +   }
  748. +
  749. +  
  750. +
  751.     sync();
  752.  
  753.     i = 0;
  754. -   unlocked = 0;
  755.     while (erase[i] != NULL) {
  756. -       mtd_unlock(erase[i]);
  757. -       mtd_erase(erase[i]);
  758. -       if (strcmp(erase[i], device) == 0)
  759. -           unlocked = 1;
  760. +       struct mtd_device *erasedev;
  761. +       int j = 0;
  762. +      
  763. +       while (mtddevs[j]) {
  764. +           if (strcmp(erase[i], mtddevs[j]->name) == 0)
  765. +               erasedev =  mtddevs[j];
  766. +       }
  767. +       if (!mtddevs[j])
  768. +           erasedev = mtd_open(erase[i], false);
  769. +       if (!erasedev)
  770. +           exit(1);
  771. +       mtd_unlock(erasedev);
  772. +       mtd_erase(erasedev);
  773. +       if (!mtddevs[j])
  774. +           mtd_close(erasedev);
  775. +
  776.         i++;
  777.     }
  778. -
  779. +  
  780. +   i = 0;
  781. +   while (mtddevs[i]) {
  782. +       if (quiet < 2)
  783. +           fprintf(stderr, "Unlocking %s ...\n", mtddevs[i]->name);
  784. +       mtd_unlock(mtddevs[i++]);
  785. +   }
  786. +  
  787.     switch (cmd) {
  788.         case CMD_UNLOCK:
  789. -           if (!unlocked)
  790. -               mtd_unlock(device);
  791. +           /* nothing to do */
  792.             break;
  793.         case CMD_ERASE:
  794. -           if (!unlocked)
  795. -               mtd_unlock(device);
  796. -           mtd_erase(device);
  797. +           cmd_erase(mtddevs);
  798.             break;
  799.         case CMD_WRITE:
  800. -           if (!unlocked)
  801. -               mtd_unlock(device);
  802. -           mtd_write(imagefd, device, fis_layout);
  803. +           cmd_write(imagefd, mtddevs, fis_layout);
  804.             break;
  805.         case CMD_JFFS2WRITE:
  806. -           if (!unlocked)
  807. -               mtd_unlock(device);
  808. -           mtd_write_jffs2(device, imagefile, jffs2dir);
  809. +           cmd_write_jffs2(mtddevs, imagefile, jffs2dir);
  810.             break;
  811.         case CMD_REFRESH:
  812. -           mtd_refresh(device);
  813. +           cmd_refresh(mtddevs);
  814.             break;
  815. -       case CMD_FIXTRX:
  816. -           mtd_fixtrx(device, offset);
  817. +       case CMD_FIXCRC:
  818. +           /* nothing to do */
  819.             break;
  820.     }
  821. +  
  822. +   mtd_close_multiple(mtddevs);
  823.  
  824.     sync();
  825.  
  826. +   if ((cmd == CMD_FIXCRC || cmd == CMD_WRITE || cmd == CMD_JFFS2WRITE) && imageops.fixcrc) {
  827. +       char *colon = strchr(device, ':');
  828. +       struct mtd_device *mtd;
  829. +       int ret;
  830. +      
  831. +       if (colon)
  832. +           *colon = 0;
  833. +      
  834. +       mtd = mtd_open(device, true);
  835. +      
  836. +       if (quiet < 2)
  837. +           fprintf(stderr, "Fixing crc in imageheader on %s ...\n", device);
  838. +      
  839. +       ret = imageops.fixcrc((char *) mtd->mmap, mtd->info.erasesize);
  840. +       if (ret > 0) {
  841. +           if (quiet < 2)
  842. +               fprintf(stderr, "Already fixed\n");
  843. +       } else if (ret < 0)
  844. +           fprintf(stderr, "CRC Fixup failed!\n");
  845. +      
  846. +       mtd_close(mtd);
  847. +          
  848. +       sync();
  849. +   }
  850. +
  851.     if (boot)
  852.         do_reboot();
  853.  
  854. Index: package/mtd/src/mtd.h
  855. ===================================================================
  856. --- package/mtd/src/mtd.h   (revision 20847)
  857. +++ package/mtd/src/mtd.h   (working copy)
  858. @@ -3,6 +3,8 @@
  859.  
  860.  #include <stdbool.h>
  861.  
  862. +#include "mtd-api.h"
  863. +
  864.  #ifdef target_brcm47xx
  865.  #define target_brcm 1
  866.  #endif
  867. @@ -10,19 +12,30 @@
  868.  #define JFFS2_EOF "\xde\xad\xc0\xde"
  869.  
  870.  extern int quiet;
  871. -extern int mtdsize;
  872. -extern int erasesize;
  873.  
  874. -extern int mtd_open(const char *mtd, bool block);
  875. -extern int mtd_check_open(const char *mtd);
  876. -extern int mtd_erase_block(int fd, int offset);
  877. -extern int mtd_write_buffer(int fd, const char *buf, int offset, int length);
  878. -extern int mtd_write_jffs2(const char *mtd, const char *filename, const char *dir);
  879. -extern int mtd_replace_jffs2(const char *mtd, int fd, int ofs, const char *filename);
  880. +struct mtd_device {
  881. +   const char *name;
  882. +   int fd;
  883. +   struct mtd_info_user info;
  884. +   int unlocked    : 1;
  885. +   void *mmap; /* only first eraseblock */
  886. +};
  887. +
  888. +extern struct mtd_device *mtd_open(const char *mtd, bool block);
  889. +extern struct mtd_device **mtd_open_multiple(const char *mtds);
  890. +extern void mtd_close_multiple(struct mtd_device **mtds);
  891. +extern void mtd_close(struct mtd_device *mtd);
  892. +extern int mtd_erase_block(struct mtd_device *mtd, size_t offset);
  893. +extern int mtd_erase(struct mtd_device *mtd);
  894. +extern int mtd_unlock(struct mtd_device *mtd);
  895. +
  896. +extern int cmd_write_jffs2(struct mtd_device **mtds, const char *filename, const char *dir);
  897. +extern int mtd_replace_jffs2(struct mtd_device *mtd, int ofs, const char *filename);
  898.  extern void mtd_parse_jffs2data(const char *buf, const char *dir);
  899.  
  900. -/* target specific */
  901. -extern int trx_fixup(int fd, const char *name);
  902. -extern int trx_check(int imagefd, const char *mtd, char *buf, int *len);
  903. +struct platform_imageops {
  904. +   int (*fixcrc)(char *data, size_t length);
  905. +   int (*checkimage)(const char *imagedata, size_t datalength, size_t mtdlength);
  906. +};
  907.  
  908.  #endif /* __mtd_h */
  909. Index: package/mtd/src/jffs2.c
  910. ===================================================================
  911. --- package/mtd/src/jffs2.c (revision 20847)
  912. +++ package/mtd/src/jffs2.c (working copy)
  913. @@ -42,7 +42,8 @@
  914.  static int last_version = 0;
  915.  static char *buf = NULL;
  916.  static int ofs = 0;
  917. -static int outfd = -1;
  918. +static struct mtd_device *mtddev = NULL;
  919. +static int erasesize = 0;
  920.  static int mtdofs = 0;
  921.  static int target_ino = 0;
  922.  
  923. @@ -59,8 +60,8 @@
  924.     }
  925.     ofs = ofs % erasesize;
  926.     if (ofs == 0) {
  927. -       mtd_erase_block(outfd, mtdofs);
  928. -       write(outfd, buf, erasesize);
  929. +       mtd_erase_block(mtddev, mtdofs);
  930. +       write(mtddev->fd, buf, erasesize);
  931.         mtdofs += erasesize;
  932.     }
  933.  }
  934. @@ -227,9 +228,10 @@
  935.     close(fd);
  936.  }
  937.  
  938. -int mtd_replace_jffs2(const char *mtd, int fd, int ofs, const char *filename)
  939. +int mtd_replace_jffs2(struct mtd_device *mtd, int ofs, const char *filename)
  940.  {
  941. -   outfd = fd;
  942. +   mtddev = mtd;
  943. +   erasesize = mtd->info.erasesize;
  944.     mtdofs = ofs;
  945.  
  946.     buf = malloc(erasesize);
  947. @@ -244,9 +246,6 @@
  948.     pad(erasesize);
  949.     free(buf);
  950.  
  951. -#ifdef target_brcm
  952. -   trx_fixup(outfd, mtd);
  953. -#endif
  954.     return 0;
  955.  }
  956.  
  957. @@ -277,16 +276,15 @@
  958.     }
  959.  }
  960.  
  961. -int mtd_write_jffs2(const char *mtd, const char *filename, const char *dir)
  962. +int cmd_write_jffs2(struct mtd_device **mtd, const char *filename, const char *dir)
  963.  {
  964.     int err = -1, fdeof = 0;
  965.  
  966. -   outfd = mtd_check_open(mtd);
  967. -   if (outfd < 0)
  968. -       return -1;
  969. +   mtddev = mtd[0];
  970. +   erasesize = mtddev->info.erasesize;
  971.  
  972.     if (quiet < 2)
  973. -       fprintf(stderr, "Appending %s to jffs2 partition %s\n", filename, mtd);
  974. +       fprintf(stderr, "Appending %s to jffs2 partition %s\n", filename, mtddev->name);
  975.    
  976.     buf = malloc(erasesize);
  977.     if (!buf) {
  978. @@ -302,7 +300,7 @@
  979.     for(;;) {
  980.         struct jffs2_unknown_node *node = (struct jffs2_unknown_node *) buf;
  981.  
  982. -       if (read(outfd, buf, erasesize) != erasesize) {
  983. +       if (read(mtddev->fd, buf, erasesize) != erasesize) {
  984.             fdeof = 1;
  985.             break;
  986.         }
  987. @@ -328,7 +326,7 @@
  988.  
  989.     /* jump back one eraseblock */
  990.     mtdofs -= erasesize;
  991. -   lseek(outfd, mtdofs, SEEK_SET);
  992. +   lseek(mtddev->fd, mtdofs, SEEK_SET);
  993.  
  994.     ofs = 0;
  995.  
  996. @@ -347,12 +345,7 @@
  997.  
  998.     err = 0;
  999.  
  1000. -#ifdef target_brcm
  1001. -   trx_fixup(outfd, mtd);
  1002. -#endif
  1003. -
  1004.  done:
  1005. -   close(outfd);
  1006.     if (buf)
  1007.         free(buf);
  1008.  
  1009. Index: package/mtd/src/libmtd.c
  1010. ===================================================================
  1011. --- package/mtd/src/libmtd.c    (revision 0)
  1012. +++ package/mtd/src/libmtd.c    (revision 0)
  1013. @@ -0,0 +1,205 @@
  1014. +/*
  1015. + * mtd - simple memory technology device manipulation tool
  1016. + *
  1017. + * Copyright (C) 2005      Waldemar Brodkorb <wbx@dass-it.de>
  1018. + * Copyright (C) 2005-2009 Felix Fietkau <nbd@openwrt.org>
  1019. + * Copyright (C) 2010 Bernhard Loos <bernhardloos@googlemail.com>
  1020. + *
  1021. + * This program is free software; you can redistribute it and/or
  1022. + * modify it under the terms of the GNU General Public License v2
  1023. + * as published by the Free Software Foundation.
  1024. + *
  1025. + * This program is distributed in the hope that it will be useful,
  1026. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  1027. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  1028. + * GNU General Public License for more details.
  1029. + *
  1030. + * You should have received a copy of the GNU General Public License
  1031. + * along with this program; if not, write to the Free Software
  1032. + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  1033. + *
  1034. + *
  1035. + * The code is based on the linux-mtd examples.
  1036. + */
  1037. +
  1038. +#include <limits.h>
  1039. +#include <unistd.h>
  1040. +#include <stdlib.h>
  1041. +#include <stdio.h>
  1042. +#include <sys/ioctl.h>
  1043. +#include <fcntl.h>
  1044. +#include <string.h>
  1045. +#include <sys/mman.h>
  1046. +
  1047. +#include "mtd.h"
  1048. +
  1049. +#define MULTIPLE_MAX 100
  1050. +
  1051. +struct mtd_device *mtd_open(const char *mtd, bool block)
  1052. +{
  1053. +   FILE *fp;
  1054. +   char dev[PATH_MAX];
  1055. +   int i;
  1056. +   int flags = O_RDWR | O_SYNC;
  1057. +   int fd = -1;
  1058. +   struct mtd_device *mtddev;
  1059. +
  1060. +   if ((fp = fopen("/proc/mtd", "r"))) {
  1061. +       while (fgets(dev, sizeof(dev), fp)) {
  1062. +           if (sscanf(dev, "mtd%d:", &i) && strstr(dev, mtd)) {
  1063. +               snprintf(dev, sizeof(dev), "/dev/mtd%s/%d", (block ? "block" : ""), i);
  1064. +               if ((fd = open(dev, flags))<0) {
  1065. +                   snprintf(dev, sizeof(dev), "/dev/mtd%s%d", (block ? "block" : ""), i);
  1066. +                   fd = open(dev, flags);
  1067. +               }
  1068. +           }
  1069. +       }
  1070. +       fclose(fp);
  1071. +   }
  1072. +
  1073. +   if (fd == -1)
  1074. +       open(mtd, flags);
  1075. +  
  1076. +   if(fd < 0) {
  1077. +       fprintf(stderr, "Could not open mtd device: %s\n", mtd);
  1078. +       return NULL;
  1079. +   }
  1080. +
  1081. +   mtddev = malloc(sizeof(*mtddev));
  1082. +   if (!mtddev) {
  1083. +       perror("malloc");
  1084. +       close(fd);
  1085. +       exit(1);
  1086. +   }
  1087. +  
  1088. +   if(ioctl(fd, MEMGETINFO, &mtddev->info) < 0) {
  1089. +       fprintf(stderr, "Could not get MTD device info from %s\n", mtd);
  1090. +       close(fd);
  1091. +       free(mtddev);
  1092. +       return NULL;
  1093. +   }
  1094. +  
  1095. +   mtddev->name = mtd;
  1096. +   mtddev->fd = fd;
  1097. +   mtddev->unlocked = 0;
  1098. +  
  1099. +   if (block) {
  1100. +           mtddev->mmap = mmap(NULL, mtddev->info.erasesize, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
  1101. +           if (mtddev->mmap == MAP_FAILED) {
  1102. +               mtddev->mmap = NULL;
  1103. +               perror("mmap");
  1104. +               mtd_close(mtddev);
  1105. +               return 0;
  1106. +           }
  1107. +   } else
  1108. +       mtddev->mmap = NULL;
  1109. +
  1110. +   return mtddev;
  1111. +}
  1112. +
  1113. +int mtd_unlock(struct mtd_device *mtd)
  1114. +{
  1115. +   struct erase_info_user mtdEraseInfo;
  1116. +  
  1117. +   if (mtd->unlocked)
  1118. +       return 0;
  1119. +
  1120. +   mtdEraseInfo.start = 0;
  1121. +   mtdEraseInfo.length = mtd->info.size;
  1122. +   if (ioctl(mtd->fd, MEMUNLOCK, &mtdEraseInfo) < 0) {
  1123. +       fprintf(stderr, "Failed to unlock %s\n", mtd->name);
  1124. +       return -1;
  1125. +   }
  1126. +
  1127. +   mtd->unlocked = 1;
  1128. +
  1129. +   return 0;
  1130. +}
  1131. +
  1132. +int mtd_erase_block(struct mtd_device *mtd, size_t offset)
  1133. +{
  1134. +   struct erase_info_user mtdEraseInfo;
  1135. +
  1136. +   mtdEraseInfo.start = offset;
  1137. +   mtdEraseInfo.length = mtd->info.erasesize;
  1138. +   ioctl(mtd->fd, MEMUNLOCK, &mtdEraseInfo);
  1139. +   if (ioctl (mtd->fd, MEMERASE, &mtdEraseInfo) < 0) {
  1140. +       fprintf(stderr, "Failed to erase block on %s at 0x%x\n", mtd->name, mtdEraseInfo.start);
  1141. +       return -1;
  1142. +   }
  1143. +
  1144. +   return 0;
  1145. +}
  1146. +
  1147. +int mtd_erase(struct mtd_device *mtd)
  1148. +{
  1149. +   size_t pos = 0;
  1150. +  
  1151. +   while (pos < mtd->info.size) {
  1152. +       if (mtd_erase_block(mtd, pos) < 0)
  1153. +           return -1;
  1154. +      
  1155. +       pos += mtd->info.erasesize;
  1156. +   }
  1157. +   return 0;
  1158. +}
  1159. +
  1160. +void mtd_close(struct mtd_device *mtd)
  1161. +{
  1162. +   if (mtd->mmap)
  1163. +       munmap(mtd->mmap, mtd->info.erasesize);
  1164. +   close(mtd->fd);
  1165. +   free(mtd);
  1166. +}
  1167. +
  1168. +struct mtd_device **mtd_open_multiple(const char *mtds)
  1169. +{
  1170. +   int count = 0;
  1171. +   char *str = strdup(mtds);
  1172. +   struct mtd_device *devs[MULTIPLE_MAX];
  1173. +   char *next;
  1174. +   char *mtd = str;
  1175. +   struct mtd_device **ret;
  1176. +  
  1177. +   do {
  1178. +       next = strchr(mtd, ':');
  1179. +       if (next) {
  1180. +           *next = 0;
  1181. +           next++;
  1182. +       }
  1183. +
  1184. +       devs[count] = mtd_open(mtd, false);
  1185. +       if (!devs[count])
  1186. +           goto error;
  1187. +       count++;
  1188. +      
  1189. +       mtd = next;
  1190. +   } while (next);
  1191. +
  1192. +   devs[count] = NULL;
  1193. +   count++;
  1194. +  
  1195. +   ret = malloc(sizeof(struct mtd_device *) * count);
  1196. +   memcpy(ret, devs, sizeof(struct mtd_device *) * count);
  1197. +  
  1198. +   return ret;
  1199. +error:
  1200. +   while(count > 0) {
  1201. +       --count;
  1202. +       mtd_close(devs[count]);
  1203. +   }
  1204. +   free(str);
  1205. +   return NULL;
  1206. +}
  1207. +
  1208. +void mtd_close_multiple(struct mtd_device **mtds)
  1209. +{
  1210. +   const char *str = mtds[0]->name;
  1211. +   int pos = 0;
  1212. +  
  1213. +   while(mtds[pos])
  1214. +       mtd_close(mtds[pos++]);
  1215. +  
  1216. +   free((char *) str);
  1217. +   free(mtds);
  1218. +}
  1219. Index: package/mtd/src/Makefile
  1220. ===================================================================
  1221. --- package/mtd/src/Makefile    (revision 20847)
  1222. +++ package/mtd/src/Makefile    (working copy)
  1223. @@ -1,9 +1,10 @@
  1224.  CC = gcc
  1225.  CFLAGS += -Wall
  1226.  
  1227. -obj = mtd.o jffs2.o crc32.o
  1228. +obj = mtd.o jffs2.o crc32.o libmtd.o
  1229.  obj.brcm = trx.o
  1230.  obj.brcm47xx = $(obj.brcm)
  1231. +obj.ar71xx = $(obj.brcm)
  1232.  
  1233.  ifdef FIS_SUPPORT
  1234.    obj += fis.o
  1235. Index: package/mtd/src/trx.c
  1236. ===================================================================
  1237. --- package/mtd/src/trx.c   (revision 20847)
  1238. +++ package/mtd/src/trx.c   (working copy)
  1239. @@ -3,6 +3,7 @@
  1240.   *
  1241.   * Copyright (C) 2005 Mike Baker
  1242.   * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
  1243. + * Copyright (C) 2010 Bernhard Loos <bernhardloos@googlemail.com>
  1244.   *
  1245.   * This program is free software; you can redistribute it and/or
  1246.   * modify it under the terms of the GNU General Public License
  1247. @@ -22,17 +23,16 @@
  1248.  #include <stdio.h>
  1249.  #include <stdlib.h>
  1250.  #include <stddef.h>
  1251. -#include <unistd.h>
  1252. -#include <fcntl.h>
  1253. -#include <sys/mman.h>
  1254. -#include <sys/stat.h>
  1255. -#include <string.h>
  1256.  
  1257. -#include <sys/ioctl.h>
  1258. -#include "mtd-api.h"
  1259.  #include "mtd.h"
  1260.  #include "crc32.h"
  1261.  
  1262. +#if __BYTE_ORDER == __BIG_ENDIAN
  1263. +#  define STORE32_LE(val) bswap_32(val)
  1264. +#else
  1265. +#  define STORE32_LE(val) (val)
  1266. +#endif
  1267. +
  1268.  #define TRX_MAGIC       0x30524448      /* "HDR0" */
  1269.  struct trx_header {
  1270.     unsigned magic;     /* "HDR0" */
  1271. @@ -42,69 +42,39 @@
  1272.     unsigned offsets[3];    /* Offsets of partitions from start of header */
  1273.  };
  1274.  
  1275. -int
  1276. -trx_fixup(int fd, const char *name)
  1277. +static int fixcrc_trx(char *data, size_t length)
  1278.  {
  1279. -   struct mtd_info_user mtdInfo;
  1280. -   unsigned long len;
  1281.     struct trx_header *trx;
  1282. -   void *ptr, *scan;
  1283. -   int bfd;
  1284.  
  1285. -   if (ioctl(fd, MEMGETINFO, &mtdInfo) < 0) {
  1286. -       fprintf(stderr, "Failed to get mtd info\n");
  1287. -       goto err;
  1288. -   }
  1289. +#ifdef TARGET_ar71xx
  1290. +   data += 32; /* used on the wrt160nl */
  1291. +   length -= 32;
  1292. +#endif
  1293.  
  1294. -   len = mtdInfo.size;
  1295. -   if (mtdInfo.size <= 0) {
  1296. -       fprintf(stderr, "Invalid MTD device size\n");
  1297. -       goto err;
  1298. +   trx = (struct trx_header *) (data);
  1299. +   if (trx->magic != STORE32_LE(TRX_MAGIC)) {
  1300. +       fprintf(stderr, "No trx magic found\n");
  1301. +       return -1;
  1302.     }
  1303. +  
  1304. +   if (trx->len == STORE32_LE(length))
  1305. +       return 1;
  1306.  
  1307. -   bfd = mtd_open(name, true);
  1308. -   ptr = mmap(NULL, len, PROT_READ|PROT_WRITE, MAP_SHARED, bfd, 0);
  1309. -   if (!ptr || (ptr == (void *) -1)) {
  1310. -       perror("mmap");
  1311. -       goto err1;
  1312. -   }
  1313. +   trx->len = STORE32_LE(length);
  1314. +  
  1315. +   trx->crc32 = STORE32_LE(crc32buf((char*) &trx->flag_version, length - 3*4));
  1316. +   if (quiet < 2)
  1317. +       fprintf(stderr, "New crc32: 0x%x, rewriting block\n", trx->crc32);
  1318.  
  1319. -   trx = ptr;
  1320. -   if (trx->magic != TRX_MAGIC) {
  1321. -       fprintf(stderr, "TRX header not found\n");
  1322. -       goto err;
  1323. -   }
  1324. -
  1325. -   scan = ptr + offsetof(struct trx_header, flag_version);
  1326. -   trx->crc32 = crc32buf(scan, trx->len - (scan - ptr));
  1327. -   msync(ptr, sizeof(struct trx_header), MS_SYNC|MS_INVALIDATE);
  1328. -   munmap(ptr, len);
  1329. -   close(bfd);
  1330.     return 0;
  1331. -
  1332. -err1:
  1333. -   close(bfd);
  1334. -err:
  1335. -   fprintf(stderr, "Error fixing up TRX header\n");
  1336. -   return -1;
  1337.  }
  1338.  
  1339. -int
  1340. -trx_check(int imagefd, const char *mtd, char *buf, int *len)
  1341. +static int
  1342. +trx_check(const char *imagedata, size_t datalength, size_t mtdlength)
  1343.  {
  1344. -   const struct trx_header *trx = (const struct trx_header *) buf;
  1345. -   int fd;
  1346. +   const struct trx_header *trx = (const struct trx_header *) imagedata;
  1347.  
  1348. -   if (strcmp(mtd, "linux") != 0)
  1349. -       return 1;
  1350. -
  1351. -   *len = read(imagefd, buf, 32);
  1352. -   if (*len < 32) {
  1353. -       fprintf(stdout, "Could not get image header, file too small (%d bytes)\n", *len);
  1354. -       return 0;
  1355. -   }
  1356. -
  1357. -   if (trx->magic != TRX_MAGIC || trx->len < sizeof(struct trx_header)) {
  1358. +   if (trx->magic != STORE32_LE(TRX_MAGIC) || STORE32_LE(trx->len) < sizeof(struct trx_header)) {
  1359.         if (quiet < 2) {
  1360.             fprintf(stderr, "Bad trx header\n");
  1361.             fprintf(stderr, "This is not the correct file format; refusing to flash.\n"
  1362. @@ -113,20 +83,15 @@
  1363.         return 0;
  1364.     }
  1365.  
  1366. -   /* check if image fits to mtd device */
  1367. -   fd = mtd_check_open(mtd);
  1368. -   if(fd < 0) {
  1369. -       fprintf(stderr, "Could not open mtd device: %s\n", mtd);
  1370. -       exit(1);
  1371. -   }
  1372. -
  1373. -   if(mtdsize < trx->len) {
  1374. -       fprintf(stderr, "Image too big for partition: %s\n", mtd);
  1375. -       close(fd);
  1376. +   if(mtdlength < STORE32_LE(trx->len)) {
  1377. +       fprintf(stderr, "Image too big\n");
  1378.         return 0;
  1379.     }
  1380.  
  1381. -   close(fd);
  1382.     return 1;
  1383.  }
  1384.  
  1385. +struct platform_imageops imageops = {
  1386. +   .fixcrc = fixcrc_trx,
  1387. +   .checkimage = trx_check,
  1388. +};
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement