add 'mtd refresh' command
[openwrt/svn-archive/archive.git] / package / mtd / src / mtd.c
index 559b937f971aa5a4e5fa2d647706d5c2d314d1cf..85b069f813949d9c231d77456077670f3d7fe8e1 100644 (file)
 #include <stdlib.h>
 #include <stdio.h>
 #include <stdint.h>
+#include <sys/ioctl.h>
+#include <sys/syscall.h>
 #include <fcntl.h>
 #include <errno.h>
 #include <error.h>
 #include <time.h>
+#include <string.h>
 #include <sys/ioctl.h>
 #include <sys/types.h>
 #include <sys/param.h>
 #include <sys/mount.h>
 #include <sys/stat.h>
 #include <sys/reboot.h>
-#include <string.h>
+#include <linux/reboot.h>
 
-#include <linux/mtd/mtd.h>
+#include "mtd.h"
 
 #define TRX_MAGIC       0x30524448      /* "HDR0" */
 #define BUFSIZE (16 * 1024)
@@ -81,22 +84,11 @@ image_check_brcm(int imagefd, const char *mtd)
                return 0;
        }
 
-       switch(trx->magic) {
-               case 0x47343557: /* W54G */
-               case 0x53343557: /* W54S */
-               case 0x73343557: /* W54s */
-               case 0x46343557: /* W54F */
-               case 0x55343557: /* W54U */
-                       /* ignore the first 32 bytes */
-                       buflen = read(imagefd, buf, sizeof(struct trx_header));
-                       break;
-       }
-       
        if (trx->magic != TRX_MAGIC || trx->len < sizeof(struct trx_header)) {
                if (quiet < 2) {
                        fprintf(stderr, "Bad trx header\n");
-                       fprintf(stderr, "If this is a firmware in bin format, like some of the\n"
-                                       "original firmware files are, you need to convert it to trx.\n");
+                       fprintf(stderr, "This is not the correct file format; refusing to flash.\n"
+                                       "Please specify the correct file or use -f to force.\n");
                }
                return 0;
        }
@@ -194,13 +186,18 @@ mtd_open(const char *mtd, int flags)
        FILE *fp;
        char dev[PATH_MAX];
        int i;
+       int ret;
 
        if ((fp = fopen("/proc/mtd", "r"))) {
                while (fgets(dev, sizeof(dev), fp)) {
                        if (sscanf(dev, "mtd%d:", &i) && strstr(dev, mtd)) {
                                snprintf(dev, sizeof(dev), "/dev/mtd/%d", i);
+                               if ((ret=open(dev, flags))<0) {
+                                       snprintf(dev, sizeof(dev), "/dev/mtd%d", i);
+                                       ret=open(dev, flags);
+                               }
                                fclose(fp);
-                               return open(dev, flags);
+                               return ret;
                        }
                }
                fclose(fp);
@@ -235,11 +232,8 @@ mtd_erase(const char *mtd)
                 mtdEraseInfo.start += mtdInfo.erasesize) {
                
                ioctl(fd, MEMUNLOCK, &mtdEraseInfo);
-               if(ioctl(fd, MEMERASE, &mtdEraseInfo)) {
-                       fprintf(stderr, "Could not erase MTD device: %s\n", mtd);
-                       close(fd);
-                       exit(1);
-               }
+               if(ioctl(fd, MEMERASE, &mtdEraseInfo))
+                       fprintf(stderr, "Failed to erase block on %s at 0x%x\n", mtd, mtdEraseInfo.start);
        }               
 
        close(fd);
@@ -247,6 +241,25 @@ mtd_erase(const char *mtd)
 
 }
 
+int
+mtd_refresh(const char *mtd)
+{
+       int fd;
+
+       fd = mtd_open(mtd, O_RDWR | O_SYNC);
+       if(fd < 0) {
+               fprintf(stderr, "Could not open mtd device: %s\n", mtd);
+               exit(1);
+       }
+       if (ioctl(fd, MTDREFRESH, NULL)) {
+               fprintf(stderr, "Failed to refresh the MTD device\n");
+               close(fd);
+               exit(1);
+       }
+       close(fd);
+       return 0;
+}
+
 int
 mtd_write(int imagefd, const char *mtd)
 {
@@ -324,6 +337,7 @@ void usage(void)
        "The device is in the format of mtdX (eg: mtd4) or its label.\n"
        "mtd recognizes these commands:\n"
        "        unlock                  unlock the device\n"
+       "        refresh                 refresh mtd partition\n"
        "        erase                   erase all data on device\n"
        "        write <imagefile>|-     write <imagefile> (use - for stdin) to device\n"
        "Following options are available:\n"
@@ -344,7 +358,8 @@ int main (int argc, char **argv)
        enum {
                CMD_ERASE,
                CMD_WRITE,
-               CMD_UNLOCK
+               CMD_UNLOCK,
+               CMD_REFRESH
        } cmd;
        
        erase[0] = NULL;
@@ -386,6 +401,9 @@ int main (int argc, char **argv)
        if ((strcmp(argv[0], "unlock") == 0) && (argc == 2)) {
                cmd = CMD_UNLOCK;
                device = argv[1];
+       } else if ((strcmp(argv[0], "refresh") == 0) && (argc == 2)) {
+               cmd = CMD_REFRESH;
+               device = argv[1];
        } else if ((strcmp(argv[0], "erase") == 0) && (argc == 2)) {
                cmd = CMD_ERASE;
                device = argv[1];
@@ -457,12 +475,21 @@ int main (int argc, char **argv)
                        if (quiet < 2)
                                fprintf(stderr, "\n");
                        break;
+               case CMD_REFRESH:
+                       if (quiet < 2)
+                               fprintf(stderr, "Refreshing mtd partition %s ... ");
+                       mtd_refresh(device);
+                       if (quiet < 2)
+                               fprintf(stderr, "\n");
+                       break;
        }
 
        sync();
        
-       if (boot)
-               kill(1, 15); // send SIGTERM to init for reboot
-
+       if (boot) {
+               fprintf(stderr, "Rebooting ...\n");
+               fflush(stderr);
+               syscall(SYS_reboot,LINUX_REBOOT_MAGIC1,LINUX_REBOOT_MAGIC2,LINUX_REBOOT_CMD_RESTART,NULL);
+       }
        return 0;
 }