SHARE
TWEET

Untitled

a guest May 22nd, 2017 512 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Index: package/mtd/src/fis.c
  2. ===================================================================
  3. --- package/mtd/src/fis.c   (revision 20862)
  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 20862)
  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 (device is not needed)\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. @@ -674,7 +359,7 @@
  691.     argc -= optind;
  692.     argv += optind;
  693.  
  694. -   if (argc < 2)
  695. +   if (argc < 1)
  696.         usage();
  697.  
  698.     if ((strcmp(argv[0], "unlock") == 0) && (argc == 2)) {
  699. @@ -686,9 +371,28 @@
  700.     } else if ((strcmp(argv[0], "erase") == 0) && (argc == 2)) {
  701.         cmd = CMD_ERASE;
  702.         device = argv[1];
  703. -   } else if ((strcmp(argv[0], "fixtrx") == 0) && (argc == 2)) {
  704. -       cmd = CMD_FIXTRX;
  705. -       device = argv[1];
  706. +   } else if ((strcmp(argv[0], "fixcrc") == 0) && (argc == 1)) {
  707. +       struct mtd_device *mtd = 0;
  708. +       int res;
  709. +
  710. +       cmd = CMD_FIXCRC;
  711. +
  712. +       if (imageops.checkimage) {
  713. +           mtd = mtd_open(imageops.firmware_partition, true);
  714. +           if (!mtd)
  715. +               exit(1);
  716. +           if (quiet < 2)
  717. +               fprintf(stderr, "Checking image and fixing crc ...\n");
  718. +           res = imageops.checkimage((char *) mtd->mmap, mtd->info.erasesize, mtd->info.size);
  719. +           if (res < 0) {
  720. +               fprintf(stderr, "Image check failed.\n");
  721. +               exit(1);
  722. +           } else if (res > 0 && quiet < 2)
  723. +               fprintf(stderr, "CRC already fixed\n");
  724. +       }
  725. +
  726. +       mtd_close(mtd);
  727. +       return 0;   /* leave early, there is nothing else to do for us */
  728.     } else if ((strcmp(argv[0], "write") == 0) && (argc == 3)) {
  729.         cmd = CMD_WRITE;
  730.         device = argv[2];
  731. @@ -703,68 +407,102 @@
  732.                 exit(1);
  733.             }
  734.         }
  735. -
  736. -       if (!mtd_check(device)) {
  737. -           fprintf(stderr, "Can't open device for writing!\n");
  738. -           exit(1);
  739. -       }
  740. -       /* check trx file before erasing or writing anything */
  741. -       if (!image_check(imagefd, device) && !force) {
  742. -           fprintf(stderr, "Image check failed.\n");
  743. -           exit(1);
  744. -       }
  745.     } else if ((strcmp(argv[0], "jffs2write") == 0) && (argc == 3)) {
  746.         cmd = CMD_JFFS2WRITE;
  747.         device = argv[2];
  748.  
  749.         imagefile = argv[1];
  750. -       if (!mtd_check(device)) {
  751. -           fprintf(stderr, "Can't open device for writing!\n");
  752. -           exit(1);
  753. -       }
  754.     } else {
  755.         usage();
  756.     }
  757.  
  758.     sync();
  759.  
  760. +   mtddevs = mtd_open_multiple(device);
  761. +   if (!mtddevs)
  762. +       exit(1);
  763. +  
  764. +   buf = malloc(mtddevs[0]->info.erasesize);
  765. +   if (!buf) {
  766. +       perror("malloc");
  767. +       exit(1);
  768. +   }
  769. +  
  770. +   if (strcmp(device, imageops.firmware_partition) == 0 && imageops.checkimage && !force && cmd == CMD_WRITE) {
  771. +       int res;
  772. +       size_t mtd_length = 0;
  773. +       int i = 0;
  774. +
  775. +       while (mtddevs[i])
  776. +           mtd_length += mtddevs[i++]->info.size;
  777. +
  778. +       buflen = read(imagefd, buf, mtddevs[0]->info.erasesize);
  779. +       if (buflen < mtddevs[0]->info.erasesize) {
  780. +           fprintf(stdout, "Could not get image header, file too small (%d bytes)\n", buflen);
  781. +           exit(1);
  782. +       }
  783. +
  784. +       if (quiet < 2)
  785. +           fprintf(stderr, "Checking image and fixing crc ...\n");
  786. +       res = imageops.checkimage(buf, buflen, mtd_length);
  787. +       if (res < 0) {
  788. +           fprintf(stderr, "Image check failed.\n");
  789. +           exit(1);
  790. +       } else if (res > 0 && quiet < 2)
  791. +           fprintf(stderr, "CRC already fixed\n");
  792. +   }
  793. +
  794.     i = 0;
  795. -   unlocked = 0;
  796.     while (erase[i] != NULL) {
  797. -       mtd_unlock(erase[i]);
  798. -       mtd_erase(erase[i]);
  799. -       if (strcmp(erase[i], device) == 0)
  800. -           unlocked = 1;
  801. +       struct mtd_device *erasedev;
  802. +       int j = 0;
  803. +      
  804. +       while (mtddevs[j]) {
  805. +           if (strcmp(erase[i], mtddevs[j]->name) == 0)
  806. +               erasedev =  mtddevs[j];
  807. +           j++;
  808. +       }
  809. +       if (!mtddevs[j])
  810. +           erasedev = mtd_open(erase[i], false);
  811. +       if (!erasedev)
  812. +           exit(1);
  813. +       mtd_unlock(erasedev);
  814. +       mtd_erase(erasedev);
  815. +       if (!mtddevs[j])
  816. +           mtd_close(erasedev);
  817. +
  818.         i++;
  819.     }
  820. -
  821. +  
  822. +   i = 0;
  823. +   while (mtddevs[i]) {
  824. +       if (quiet < 2)
  825. +           fprintf(stderr, "Unlocking %s ...\n", mtddevs[i]->name);
  826. +       mtd_unlock(mtddevs[i++]);
  827. +   }
  828. +  
  829.     switch (cmd) {
  830.         case CMD_UNLOCK:
  831. -           if (!unlocked)
  832. -               mtd_unlock(device);
  833. +           /* nothing to do */
  834.             break;
  835.         case CMD_ERASE:
  836. -           if (!unlocked)
  837. -               mtd_unlock(device);
  838. -           mtd_erase(device);
  839. +           cmd_erase(mtddevs);
  840.             break;
  841.         case CMD_WRITE:
  842. -           if (!unlocked)
  843. -               mtd_unlock(device);
  844. -           mtd_write(imagefd, device, fis_layout);
  845. +           cmd_write(imagefd, mtddevs, fis_layout);
  846.             break;
  847.         case CMD_JFFS2WRITE:
  848. -           if (!unlocked)
  849. -               mtd_unlock(device);
  850. -           mtd_write_jffs2(device, imagefile, jffs2dir);
  851. +           cmd_write_jffs2(mtddevs, imagefile, jffs2dir);
  852.             break;
  853.         case CMD_REFRESH:
  854. -           mtd_refresh(device);
  855. +           cmd_refresh(mtddevs);
  856.             break;
  857. -       case CMD_FIXTRX:
  858. -           mtd_fixtrx(device, offset);
  859. +       case CMD_FIXCRC:
  860. +           /* nothing to do */
  861.             break;
  862.     }
  863. +  
  864. +   mtd_close_multiple(mtddevs);
  865.  
  866.     sync();
  867.  
  868. Index: package/mtd/src/mtd.h
  869. ===================================================================
  870. --- package/mtd/src/mtd.h   (revision 20862)
  871. +++ package/mtd/src/mtd.h   (working copy)
  872. @@ -3,6 +3,8 @@
  873.  
  874.  #include <stdbool.h>
  875.  
  876. +#include "mtd-api.h"
  877. +
  878.  #ifdef target_brcm47xx
  879.  #define target_brcm 1
  880.  #endif
  881. @@ -10,19 +12,30 @@
  882.  #define JFFS2_EOF "\xde\xad\xc0\xde"
  883.  
  884.  extern int quiet;
  885. -extern int mtdsize;
  886. -extern int erasesize;
  887.  
  888. -extern int mtd_open(const char *mtd, bool block);
  889. -extern int mtd_check_open(const char *mtd);
  890. -extern int mtd_erase_block(int fd, int offset);
  891. -extern int mtd_write_buffer(int fd, const char *buf, int offset, int length);
  892. -extern int mtd_write_jffs2(const char *mtd, const char *filename, const char *dir);
  893. -extern int mtd_replace_jffs2(const char *mtd, int fd, int ofs, const char *filename);
  894. +struct mtd_device {
  895. +   const char *name;
  896. +   int fd;
  897. +   struct mtd_info_user info;
  898. +   int unlocked    : 1;
  899. +   void *mmap; /* only first eraseblock */
  900. +};
  901. +
  902. +extern struct mtd_device *mtd_open(const char *mtd, bool block);
  903. +extern struct mtd_device **mtd_open_multiple(const char *mtds);
  904. +extern void mtd_close_multiple(struct mtd_device **mtds);
  905. +extern void mtd_close(struct mtd_device *mtd);
  906. +extern int mtd_erase_block(struct mtd_device *mtd, size_t offset);
  907. +extern int mtd_erase(struct mtd_device *mtd);
  908. +extern int mtd_unlock(struct mtd_device *mtd);
  909. +
  910. +extern int cmd_write_jffs2(struct mtd_device **mtds, const char *filename, const char *dir);
  911. +extern int mtd_replace_jffs2(struct mtd_device *mtd, int ofs, const char *filename);
  912.  extern void mtd_parse_jffs2data(const char *buf, const char *dir);
  913.  
  914. -/* target specific */
  915. -extern int trx_fixup(int fd, const char *name);
  916. -extern int trx_check(int imagefd, const char *mtd, char *buf, int *len);
  917. +struct platform_imageops {
  918. +   int (*checkimage)(char *imagedata, size_t datalength, size_t mtdlength);
  919. +   const char *firmware_partition;
  920. +};
  921.  
  922.  #endif /* __mtd_h */
  923. Index: package/mtd/src/jffs2.c
  924. ===================================================================
  925. --- package/mtd/src/jffs2.c (revision 20862)
  926. +++ package/mtd/src/jffs2.c (working copy)
  927. @@ -42,7 +42,8 @@
  928.  static int last_version = 0;
  929.  static char *buf = NULL;
  930.  static int ofs = 0;
  931. -static int outfd = -1;
  932. +static struct mtd_device *mtddev = NULL;
  933. +static int erasesize = 0;
  934.  static int mtdofs = 0;
  935.  static int target_ino = 0;
  936.  
  937. @@ -59,8 +60,8 @@
  938.     }
  939.     ofs = ofs % erasesize;
  940.     if (ofs == 0) {
  941. -       mtd_erase_block(outfd, mtdofs);
  942. -       write(outfd, buf, erasesize);
  943. +       mtd_erase_block(mtddev, mtdofs);
  944. +       write(mtddev->fd, buf, erasesize);
  945.         mtdofs += erasesize;
  946.     }
  947.  }
  948. @@ -227,9 +228,10 @@
  949.     close(fd);
  950.  }
  951.  
  952. -int mtd_replace_jffs2(const char *mtd, int fd, int ofs, const char *filename)
  953. +int mtd_replace_jffs2(struct mtd_device *mtd, int ofs, const char *filename)
  954.  {
  955. -   outfd = fd;
  956. +   mtddev = mtd;
  957. +   erasesize = mtd->info.erasesize;
  958.     mtdofs = ofs;
  959.  
  960.     buf = malloc(erasesize);
  961. @@ -244,9 +246,6 @@
  962.     pad(erasesize);
  963.     free(buf);
  964.  
  965. -#ifdef target_brcm
  966. -   trx_fixup(outfd, mtd);
  967. -#endif
  968.     return 0;
  969.  }
  970.  
  971. @@ -277,16 +276,15 @@
  972.     }
  973.  }
  974.  
  975. -int mtd_write_jffs2(const char *mtd, const char *filename, const char *dir)
  976. +int cmd_write_jffs2(struct mtd_device **mtd, const char *filename, const char *dir)
  977.  {
  978.     int err = -1, fdeof = 0;
  979.  
  980. -   outfd = mtd_check_open(mtd);
  981. -   if (outfd < 0)
  982. -       return -1;
  983. +   mtddev = mtd[0];
  984. +   erasesize = mtddev->info.erasesize;
  985.  
  986.     if (quiet < 2)
  987. -       fprintf(stderr, "Appending %s to jffs2 partition %s\n", filename, mtd);
  988. +       fprintf(stderr, "Appending %s to jffs2 partition %s\n", filename, mtddev->name);
  989.    
  990.     buf = malloc(erasesize);
  991.     if (!buf) {
  992. @@ -302,7 +300,7 @@
  993.     for(;;) {
  994.         struct jffs2_unknown_node *node = (struct jffs2_unknown_node *) buf;
  995.  
  996. -       if (read(outfd, buf, erasesize) != erasesize) {
  997. +       if (read(mtddev->fd, buf, erasesize) != erasesize) {
  998.             fdeof = 1;
  999.             break;
  1000.         }
  1001. @@ -328,7 +326,7 @@
  1002.  
  1003.     /* jump back one eraseblock */
  1004.     mtdofs -= erasesize;
  1005. -   lseek(outfd, mtdofs, SEEK_SET);
  1006. +   lseek(mtddev->fd, mtdofs, SEEK_SET);
  1007.  
  1008.     ofs = 0;
  1009.  
  1010. @@ -347,12 +345,7 @@
  1011.  
  1012.     err = 0;
  1013.  
  1014. -#ifdef target_brcm
  1015. -   trx_fixup(outfd, mtd);
  1016. -#endif
  1017. -
  1018.  done:
  1019. -   close(outfd);
  1020.     if (buf)
  1021.         free(buf);
  1022.  
  1023. Index: package/mtd/src/libmtd.c
  1024. ===================================================================
  1025. --- package/mtd/src/libmtd.c    (revision 0)
  1026. +++ package/mtd/src/libmtd.c    (revision 0)
  1027. @@ -0,0 +1,219 @@
  1028. +/*
  1029. + * mtd - simple memory technology device manipulation tool
  1030. + *
  1031. + * Copyright (C) 2005      Waldemar Brodkorb <wbx@dass-it.de>
  1032. + * Copyright (C) 2005-2009 Felix Fietkau <nbd@openwrt.org>
  1033. + * Copyright (C) 2010 Bernhard Loos <bernhardloos@googlemail.com>
  1034. + *
  1035. + * This program is free software; you can redistribute it and/or
  1036. + * modify it under the terms of the GNU General Public License v2
  1037. + * as published by the Free Software Foundation.
  1038. + *
  1039. + * This program is distributed in the hope that it will be useful,
  1040. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  1041. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  1042. + * GNU General Public License for more details.
  1043. + *
  1044. + * You should have received a copy of the GNU General Public License
  1045. + * along with this program; if not, write to the Free Software
  1046. + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  1047. + *
  1048. + *
  1049. + * The code is based on the linux-mtd examples.
  1050. + */
  1051. +
  1052. +#include <limits.h>
  1053. +#include <unistd.h>
  1054. +#include <stdlib.h>
  1055. +#include <stdio.h>
  1056. +#include <sys/ioctl.h>
  1057. +#include <fcntl.h>
  1058. +#include <string.h>
  1059. +#include <sys/mman.h>
  1060. +#include <errno.h>
  1061. +
  1062. +#include "mtd.h"
  1063. +
  1064. +#define MULTIPLE_MAX 100
  1065. +
  1066. +struct mtd_device *mtd_open(const char *mtd, bool block)
  1067. +{
  1068. +   FILE *fp;
  1069. +   char number[3] = { 0 };
  1070. +   int flags = O_RDWR | O_SYNC;
  1071. +   struct mtd_device *mtddev;
  1072. +   char dev[PATH_MAX];
  1073. +
  1074. +   mtddev = malloc(sizeof(*mtddev));
  1075. +   if (!mtddev) {
  1076. +       perror("malloc");
  1077. +       exit(1);
  1078. +   }
  1079. +   memset(mtddev, 0, sizeof(struct mtd_device));
  1080. +
  1081. +   fp = fopen("/proc/mtd", "r");
  1082. +   if (!fp) {
  1083. +       fprintf(stderr, "Can't open /proc/mtd\n");
  1084. +       free(mtddev);
  1085. +       return NULL;
  1086. +   }
  1087. +
  1088. +   fscanf(fp, "%*[^\n]\n"); //skip fist line
  1089. +
  1090. +   while (fscanf(fp, "mtd%2[0-9]: %x %x \"%20[^\"]\"\n", number, &mtddev->info.size, &mtddev->info.erasesize, dev) == 4) {
  1091. +       if (strcmp(dev, mtd) == 0)
  1092. +           break;
  1093. +   }
  1094. +
  1095. +   fclose(fp);
  1096. +
  1097. +   mtddev->fd = -1;
  1098. +
  1099. +   if (number[0]) {
  1100. +       snprintf(dev, sizeof(dev), "/dev/mtd%s/%s", (block ? "block" : ""), number);
  1101. +       if ((mtddev->fd = open(dev, flags)) < 0) {
  1102. +           snprintf(dev, sizeof(dev), "/dev/mtd%s%s", (block ? "block" : ""), number);
  1103. +           mtddev->fd = open(dev, flags);
  1104. +       }
  1105. +   } else {
  1106. +       if (block) {
  1107. +           fprintf(stderr, "mtd_open needs a partition name for block mode, not a device\n");
  1108. +           free(mtddev);
  1109. +           return NULL;
  1110. +       }
  1111. +       open(mtd, flags);
  1112. +   }
  1113. +  
  1114. +   if(mtddev->fd < 0) {
  1115. +       fprintf(stderr, "Could not open mtd device: %s\n", mtd);
  1116. +       return NULL;
  1117. +   }
  1118. +
  1119. +   mtddev->name = mtd;
  1120. +
  1121. +   if (block) {
  1122. +           mtddev->mmap = mmap(NULL, mtddev->info.erasesize, PROT_READ|PROT_WRITE, MAP_SHARED, mtddev->fd, 0);
  1123. +           if (mtddev->mmap == MAP_FAILED) {
  1124. +               mtddev->mmap = NULL;
  1125. +               perror("mmap");
  1126. +               mtd_close(mtddev);
  1127. +               return NULL;
  1128. +           }
  1129. +   } else {
  1130. +       if(ioctl(mtddev->fd, MEMGETINFO, &mtddev->info) < 0) {
  1131. +           fprintf(stderr, "Could not get MTD device info for %s\n", mtd);
  1132. +           mtd_close(mtddev);
  1133. +           return NULL;
  1134. +       }
  1135. +   }
  1136. +
  1137. +   return mtddev;
  1138. +}
  1139. +
  1140. +int mtd_unlock(struct mtd_device *mtd)
  1141. +{
  1142. +   struct erase_info_user mtdEraseInfo;
  1143. +  
  1144. +   if (mtd->unlocked)
  1145. +       return 0;
  1146. +
  1147. +   mtdEraseInfo.start = 0;
  1148. +   mtdEraseInfo.length = mtd->info.size;
  1149. +   if (ioctl(mtd->fd, MEMUNLOCK, &mtdEraseInfo) < 0) {
  1150. +       if (errno != EOPNOTSUPP) {
  1151. +           fprintf(stderr, "Failed to unlock %s\n", mtd->name);
  1152. +           return -1;
  1153. +       }
  1154. +   }
  1155. +
  1156. +   mtd->unlocked = 1;
  1157. +
  1158. +   return 0;
  1159. +}
  1160. +
  1161. +int mtd_erase_block(struct mtd_device *mtd, size_t offset)
  1162. +{
  1163. +   struct erase_info_user mtdEraseInfo;
  1164. +
  1165. +   mtdEraseInfo.start = offset;
  1166. +   mtdEraseInfo.length = mtd->info.erasesize;
  1167. +   if (ioctl (mtd->fd, MEMERASE, &mtdEraseInfo) < 0) {
  1168. +       fprintf(stderr, "Failed to erase block on %s at 0x%x\n", mtd->name, mtdEraseInfo.start);
  1169. +       return -1;
  1170. +   }
  1171. +
  1172. +   return 0;
  1173. +}
  1174. +
  1175. +int mtd_erase(struct mtd_device *mtd)
  1176. +{
  1177. +   size_t pos = 0;
  1178. +  
  1179. +   while (pos < mtd->info.size) {
  1180. +       if (mtd_erase_block(mtd, pos) < 0)
  1181. +           return -1;
  1182. +      
  1183. +       pos += mtd->info.erasesize;
  1184. +   }
  1185. +   return 0;
  1186. +}
  1187. +
  1188. +void mtd_close(struct mtd_device *mtd)
  1189. +{
  1190. +   if (mtd->mmap)
  1191. +       munmap(mtd->mmap, mtd->info.erasesize);
  1192. +   close(mtd->fd);
  1193. +   free(mtd);
  1194. +}
  1195. +
  1196. +struct mtd_device **mtd_open_multiple(const char *mtds)
  1197. +{
  1198. +   int count = 0;
  1199. +   char *str = strdup(mtds);
  1200. +   struct mtd_device *devs[MULTIPLE_MAX];
  1201. +   char *next;
  1202. +   char *mtd = str;
  1203. +   struct mtd_device **ret;
  1204. +  
  1205. +   do {
  1206. +       next = strchr(mtd, ':');
  1207. +       if (next) {
  1208. +           *next = 0;
  1209. +           next++;
  1210. +       }
  1211. +
  1212. +       devs[count] = mtd_open(mtd, false);
  1213. +       if (!devs[count])
  1214. +           goto error;
  1215. +       count++;
  1216. +      
  1217. +       mtd = next;
  1218. +   } while (next);
  1219. +
  1220. +   devs[count] = NULL;
  1221. +   count++;
  1222. +  
  1223. +   ret = malloc(sizeof(struct mtd_device *) * count);
  1224. +   memcpy(ret, devs, sizeof(struct mtd_device *) * count);
  1225. +  
  1226. +   return ret;
  1227. +error:
  1228. +   while(count > 0) {
  1229. +       --count;
  1230. +       mtd_close(devs[count]);
  1231. +   }
  1232. +   free(str);
  1233. +   return NULL;
  1234. +}
  1235. +
  1236. +void mtd_close_multiple(struct mtd_device **mtds)
  1237. +{
  1238. +   const char *str = mtds[0]->name;
  1239. +   int pos = 0;
  1240. +  
  1241. +   while(mtds[pos])
  1242. +       mtd_close(mtds[pos++]);
  1243. +  
  1244. +   free((char *) str);
  1245. +   free(mtds);
  1246. +}
  1247. Index: package/mtd/src/Makefile
  1248. ===================================================================
  1249. --- package/mtd/src/Makefile    (revision 20862)
  1250. +++ package/mtd/src/Makefile    (working copy)
  1251. @@ -1,9 +1,10 @@
  1252.  CC = gcc
  1253.  CFLAGS += -Wall
  1254.  
  1255. -obj = mtd.o jffs2.o crc32.o
  1256. +obj = mtd.o jffs2.o crc32.o libmtd.o
  1257.  obj.brcm = trx.o
  1258.  obj.brcm47xx = $(obj.brcm)
  1259. +obj.ar71xx = $(obj.brcm)
  1260.  
  1261.  ifdef FIS_SUPPORT
  1262.    obj += fis.o
  1263. Index: package/mtd/src/trx.c
  1264. ===================================================================
  1265. --- package/mtd/src/trx.c   (revision 20862)
  1266. +++ package/mtd/src/trx.c   (working copy)
  1267. @@ -3,6 +3,7 @@
  1268.   *
  1269.   * Copyright (C) 2005 Mike Baker
  1270.   * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
  1271. + * Copyright (C) 2010 Bernhard Loos <bernhardloos@googlemail.com>
  1272.   *
  1273.   * This program is free software; you can redistribute it and/or
  1274.   * modify it under the terms of the GNU General Public License
  1275. @@ -22,17 +23,16 @@
  1276.  #include <stdio.h>
  1277.  #include <stdlib.h>
  1278.  #include <stddef.h>
  1279. -#include <unistd.h>
  1280. -#include <fcntl.h>
  1281. -#include <sys/mman.h>
  1282. -#include <sys/stat.h>
  1283. -#include <string.h>
  1284.  
  1285. -#include <sys/ioctl.h>
  1286. -#include "mtd-api.h"
  1287.  #include "mtd.h"
  1288.  #include "crc32.h"
  1289.  
  1290. +#if __BYTE_ORDER == __BIG_ENDIAN
  1291. +#  define STORE32_LE(val) bswap_32(val)
  1292. +#else
  1293. +#  define STORE32_LE(val) (val)
  1294. +#endif
  1295. +
  1296.  #define TRX_MAGIC       0x30524448      /* "HDR0" */
  1297.  struct trx_header {
  1298.     unsigned magic;     /* "HDR0" */
  1299. @@ -42,91 +42,44 @@
  1300.     unsigned offsets[3];    /* Offsets of partitions from start of header */
  1301.  };
  1302.  
  1303. -int
  1304. -trx_fixup(int fd, const char *name)
  1305. +static int
  1306. +trx_check(char *imagedata, size_t datalength, size_t mtdlength)
  1307.  {
  1308. -   struct mtd_info_user mtdInfo;
  1309. -   unsigned long len;
  1310.     struct trx_header *trx;
  1311. -   void *ptr, *scan;
  1312. -   int bfd;
  1313.  
  1314. -   if (ioctl(fd, MEMGETINFO, &mtdInfo) < 0) {
  1315. -       fprintf(stderr, "Failed to get mtd info\n");
  1316. -       goto err;
  1317. -   }
  1318. +#ifdef TARGET_ar71xx
  1319. +   imagedata += 32;    /* used on the wrt160nl */
  1320. +   datelength -= 32;
  1321. +#endif
  1322. +   trx = (struct trx_header *) imagedata;
  1323.  
  1324. -   len = mtdInfo.size;
  1325. -   if (mtdInfo.size <= 0) {
  1326. -       fprintf(stderr, "Invalid MTD device size\n");
  1327. -       goto err;
  1328. -   }
  1329. -
  1330. -   bfd = mtd_open(name, true);
  1331. -   ptr = mmap(NULL, len, PROT_READ|PROT_WRITE, MAP_SHARED, bfd, 0);
  1332. -   if (!ptr || (ptr == (void *) -1)) {
  1333. -       perror("mmap");
  1334. -       goto err1;
  1335. -   }
  1336. -
  1337. -   trx = ptr;
  1338. -   if (trx->magic != TRX_MAGIC) {
  1339. -       fprintf(stderr, "TRX header not found\n");
  1340. -       goto err;
  1341. -   }
  1342. -
  1343. -   scan = ptr + offsetof(struct trx_header, flag_version);
  1344. -   trx->crc32 = crc32buf(scan, trx->len - (scan - ptr));
  1345. -   msync(ptr, sizeof(struct trx_header), MS_SYNC|MS_INVALIDATE);
  1346. -   munmap(ptr, len);
  1347. -   close(bfd);
  1348. -   return 0;
  1349. -
  1350. -err1:
  1351. -   close(bfd);
  1352. -err:
  1353. -   fprintf(stderr, "Error fixing up TRX header\n");
  1354. -   return -1;
  1355. -}
  1356. -
  1357. -int
  1358. -trx_check(int imagefd, const char *mtd, char *buf, int *len)
  1359. -{
  1360. -   const struct trx_header *trx = (const struct trx_header *) buf;
  1361. -   int fd;
  1362. -
  1363. -   if (strcmp(mtd, "linux") != 0)
  1364. -       return 1;
  1365. -
  1366. -   *len = read(imagefd, buf, 32);
  1367. -   if (*len < 32) {
  1368. -       fprintf(stdout, "Could not get image header, file too small (%d bytes)\n", *len);
  1369. -       return 0;
  1370. -   }
  1371. -
  1372. -   if (trx->magic != TRX_MAGIC || trx->len < sizeof(struct trx_header)) {
  1373. +   if (trx->magic != STORE32_LE(TRX_MAGIC) || STORE32_LE(trx->len) < sizeof(struct trx_header)) {
  1374.         if (quiet < 2) {
  1375.             fprintf(stderr, "Bad trx header\n");
  1376.             fprintf(stderr, "This is not the correct file format; refusing to flash.\n"
  1377.                     "Please specify the correct file or use -f to force.\n");
  1378.         }
  1379. -       return 0;
  1380. +       return -1;
  1381.     }
  1382.  
  1383. -   /* check if image fits to mtd device */
  1384. -   fd = mtd_check_open(mtd);
  1385. -   if(fd < 0) {
  1386. -       fprintf(stderr, "Could not open mtd device: %s\n", mtd);
  1387. -       exit(1);
  1388. +   if(mtdlength < STORE32_LE(trx->len)) {
  1389. +       fprintf(stderr, "Image too big\n");
  1390. +       return -1;
  1391.     }
  1392. +  
  1393. +   if (trx->len == STORE32_LE(datalength))
  1394. +       return 1;
  1395.  
  1396. -   if(mtdsize < trx->len) {
  1397. -       fprintf(stderr, "Image too big for partition: %s\n", mtd);
  1398. -       close(fd);
  1399. -       return 0;
  1400. -   }
  1401. +   trx->len = STORE32_LE(datalength);
  1402. +  
  1403. +   trx->crc32 = STORE32_LE(crc32buf((char*) &trx->flag_version, datalength - 3*4));
  1404. +   if (quiet < 2)
  1405. +       fprintf(stderr, "New crc32: 0x%x\n", trx->crc32);
  1406.  
  1407. -   close(fd);
  1408. -   return 1;
  1409. +   return 0;
  1410.  }
  1411.  
  1412. +struct platform_imageops imageops = {
  1413. +   .checkimage = trx_check,
  1414. +   .firmware_partition = "linux"
  1415. +};
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top