X-Git-Url: https://git.rohieb.name/openwrt.git/blobdiff_plain/91c63e98223fcc68fd6c4c3b4406e853f311f2d9..11543233b23052270bdda2cb21cc41096e579904:/package/mtd/src/mtd.c?ds=sidebyside

diff --git a/package/mtd/src/mtd.c b/package/mtd/src/mtd.c
index 23e99160a..0a968409a 100644
--- a/package/mtd/src/mtd.c
+++ b/package/mtd/src/mtd.c
@@ -28,19 +28,22 @@
 #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)
@@ -62,14 +65,19 @@ struct trx_header {
 
 char buf[BUFSIZE];
 int buflen;
+int quiet;
 
+#ifdef target_brcm
 int
-image_check_bcom(int imagefd, const char *mtd)
+image_check_brcm(int imagefd, const char *mtd)
 {
 	struct trx_header *trx = (struct trx_header *) buf;
 	struct mtd_info_user mtdInfo;
 	int fd;
 
+	if (strcmp(mtd, "linux") != 0)
+		return 1;
+	
 	buflen = read(imagefd, buf, 32);
 	if (buflen < 32) {
 		fprintf(stdout, "Could not get image header, file too small (%ld bytes)\n", buflen);
@@ -88,10 +96,11 @@ image_check_bcom(int imagefd, const char *mtd)
 	}
 	
 	if (trx->magic != TRX_MAGIC || trx->len < sizeof(struct trx_header)) {
-		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, use following command to convert to trx:\n"
-				"dd if=firmware.bin of=firmware.trx bs=32 skip=1\n");
+		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");
+		}
 		return 0;
 	}
 
@@ -116,6 +125,7 @@ image_check_bcom(int imagefd, const char *mtd)
 	close(fd);
 	return 1;
 }
+#endif /* target_brcm */
 
 int
 image_check(int imagefd, const char *mtd)
@@ -125,23 +135,9 @@ image_check(int imagefd, const char *mtd)
 	char *c;
 	FILE *f;
 
-	systype = SYSTYPE_UNKNOWN;
-	f = fopen("/proc/cpuinfo", "r");
-	while (!feof(f) && (fgets(buf, BUFSIZE - 1, f) != NULL)) {
-		if ((strncmp(buf, "system type", 11) == 0) && (c = strchr(buf, ':'))) {
-			c += 2;
-			if (strncmp(c, "Broadcom BCM947XX", 17) == 0)
-				systype = SYSTYPE_BROADCOM;
-		}
-	}
-	fclose(f);
-	
-	switch(systype) {
-		case SYSTYPE_BROADCOM:
-			return image_check_bcom(imagefd, mtd);
-		default:
-			return 1;
-	}
+#ifdef target_brcm
+	return image_check_brcm(imagefd, mtd);
+#endif
 }
 
 int mtd_check(char *mtd)
@@ -201,13 +197,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);
@@ -242,11 +243,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);
@@ -255,7 +253,7 @@ mtd_erase(const char *mtd)
 }
 
 int
-mtd_write(int imagefd, const char *mtd, int quiet)
+mtd_write(int imagefd, const char *mtd)
 {
 	int fd, i, result;
 	size_t r, w, e;
@@ -346,7 +344,7 @@ void usage(void)
 
 int main (int argc, char **argv)
 {
-	int ch, i, boot, unlock, imagefd, force, quiet, unlocked;
+	int ch, i, boot, unlock, imagefd, force, unlocked;
 	char *erase[MAX_ARGS], *device, *imagefile;
 	enum {
 		CMD_ERASE,
@@ -413,10 +411,10 @@ int main (int argc, char **argv)
 	
 		/* check trx file before erasing or writing anything */
 		if (!image_check(imagefd, device)) {
-			if ((quiet < 2) || !force)
-				fprintf(stderr, "TRX check failed!\n");
-			if (!force)
+			if (!force) {
+				fprintf(stderr, "Image check failed.\n");
 				exit(1);
+			}
 		} else {
 			if (!mtd_check(device)) {
 				fprintf(stderr, "Can't open device for writing!\n");
@@ -460,7 +458,7 @@ int main (int argc, char **argv)
 		case CMD_WRITE:
 			if (quiet < 2)
 				fprintf(stderr, "Writing from %s to %s ... ", imagefile, device);
-			mtd_write(imagefd, device, quiet);
+			mtd_write(imagefd, device);
 			if (quiet < 2)
 				fprintf(stderr, "\n");
 			break;
@@ -468,8 +466,9 @@ int main (int argc, char **argv)
 
 	sync();
 	
-	if (boot)
-		kill(1, 15); // send SIGTERM to init for reboot
-
+	if (boot) {
+		fflush(stdout);
+		syscall(SYS_reboot,LINUX_REBOOT_MAGIC1,LINUX_REBOOT_MAGIC2,LINUX_REBOOT_CMD_RESTART,NULL);
+	}
 	return 0;
 }