X-Git-Url: https://git.rohieb.name/openwrt.git/blobdiff_plain/dead6fde5190cfcf868e6b46299879c9eb72a886..12304a3ebc0d11b6c518918cf280971bb15e880d:/package/openwrt/mtd.c diff --git a/package/openwrt/mtd.c b/package/openwrt/mtd.c index 296bf4b63..13f4bf195 100644 --- a/package/openwrt/mtd.c +++ b/package/openwrt/mtd.c @@ -1,9 +1,7 @@ /* - * mtd.c + * mtd - simple memory technology device manipulation tool * - * Copyright (C) 2005 Waldemar Brodkorb - * - * $Id$ + * Copyright (C) 2005 Waldemar Brodkorb * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -23,6 +21,8 @@ * it is mainly code from the linux-mtd project, which accepts the same * command line arguments as the broadcom utility * + * $Id$ + * */ #include @@ -62,6 +62,7 @@ struct trx_header { extern int mtd_open(const char *mtd, int flags); extern int mtd_erase(const char *mtd); extern int mtd_write(const char *trxfile, const char *mtd); +extern int mtd_update(const char *trxfile, const char *mtd); int mtd_unlock(const char *mtd) @@ -190,7 +191,7 @@ mtd_write(const char *trxfile, const char *mtd) } if(mtdInfo.size < trxstat.st_size) { - fprintf(stderr, "image to big for mtd partition: %s\n", mtd); + fprintf(stderr, "Image too big for partition: %s\n", mtd); close(trxfd); exit(1); } @@ -201,7 +202,7 @@ mtd_write(const char *trxfile, const char *mtd) /* erase the chunk */ if (ioctl (fd,MEMERASE,&mtdEraseInfo) < 0) { - fprintf(stderr, "erasing mtd failed: %s\n", mtd); + fprintf(stderr, "Erasing mtd failed: %s\n", mtd); exit(1); } @@ -215,10 +216,10 @@ mtd_write(const char *trxfile, const char *mtd) result = write(fd,src,i); if (i != result) { if (result < 0) { - fprintf(stderr,"error while writing image"); + fprintf(stderr,"Error while writing image"); exit(1); } - fprintf(stderr,"error writing image"); + fprintf(stderr,"Error writing image"); exit(1); } written += i; @@ -228,21 +229,52 @@ mtd_write(const char *trxfile, const char *mtd) return 0; } +int +mtd_update(const char *trxfile, const char *mtd) +{ + if (mtd_erase("rootfs") != 0) { + fprintf(stderr, "Could not erase rootfs\n"); + exit(1); + } + if (mtd_write(trxfile, mtd) != 0) { + fprintf(stderr, "Could not update %s with %s\n", mtd, trxfile); + exit(1); + } + return 0; +} + int main(int argc, char **argv) { if(argc == 3 && strcasecmp(argv[1],"unlock")==0) { - printf("Unlocking %s\n",argv[2]); + printf("Unlocking %s ...\n",argv[2]); return mtd_unlock(argv[2]); } if(argc == 3 && strcasecmp(argv[1],"erase")==0) { - printf("Erasing %s\n",argv[2]); + printf("Erasing %s ...\n",argv[2]); return mtd_erase(argv[2]); } if(argc == 4 && strcasecmp(argv[1],"write")==0) { - printf("Writing %s to %s\n",argv[2],argv[3]); + printf("Writing %s to %s ...\n",argv[2],argv[3]); return mtd_write(argv[2],argv[3]); } + if(argc == 4 && strcasecmp(argv[1],"update")==0) { + printf("Updating %s on %s ...\n",argv[2],argv[3]); + return mtd_update(argv[2],argv[3]); + } printf("no valid command given\n"); + printf("\nmtd: modify data within a Memory Technology Device.\n"); + printf("Copyright (C) 2005 Waldemar Brodkorb \n"); + printf("Documented by Mike Strates [dumpedcore] \n"); + printf("mtd has ABSOLUTELY NO WARRANTY and is licensed under the GNU GPL.\n"); + printf("\nUsage: mtd [unlock|erase] device\n"); + printf(" mtd [write|update] imagefile device\n"); + printf("\n.. where device is in the format of mtdX (eg: mtd4) or its label.\n\n"); + printf("unlock enable modification to device\n"); + printf("erase erase all data on device\n"); + printf("write write imagefile to device\n"); + printf("update remove rootfs and update imagefile on device\n"); + printf("\nExample: To write linux.trx to mtd4 labeled as linux\n"); + printf("\n mtd unlock linux && mtd write linux.trx linux\n\n"); return -1; }