From a10a204aab26ccec266cd09fa94110a61293fdd9 Mon Sep 17 00:00:00 2001
From: John Crispin <john@openwrt.org>
Date: Wed, 3 Dec 2014 14:32:38 +0000
Subject: kernel: make ubi auto-attach check for a tar file magic

Signed-off-by: John Crispin <blogic@openwrt.org>

SVN-Revision: 43506
---
 ...uto-attach-mtd-device-named-ubi-or-data-on-boot.patch | 15 +++++++++++++--
 ...uto-attach-mtd-device-named-ubi-or-data-on-boot.patch | 16 ++++++++++++++--
 ...uto-attach-mtd-device-named-ubi-or-data-on-boot.patch | 15 +++++++++++++--
 3 files changed, 40 insertions(+), 6 deletions(-)

diff --git a/target/linux/generic/patches-3.10/490-ubi-auto-attach-mtd-device-named-ubi-or-data-on-boot.patch b/target/linux/generic/patches-3.10/490-ubi-auto-attach-mtd-device-named-ubi-or-data-on-boot.patch
index b0b367e..68e98ca 100644
--- a/target/linux/generic/patches-3.10/490-ubi-auto-attach-mtd-device-named-ubi-or-data-on-boot.patch
+++ b/target/linux/generic/patches-3.10/490-ubi-auto-attach-mtd-device-named-ubi-or-data-on-boot.patch
@@ -11,7 +11,7 @@ Signed-off-by: Daniel Golle <daniel@makrotopia.org>
 
 --- a/drivers/mtd/ubi/build.c
 +++ b/drivers/mtd/ubi/build.c
-@@ -1207,6 +1207,36 @@ static struct mtd_info * __init open_mtd
+@@ -1207,6 +1207,47 @@
  	return mtd;
  }
  
@@ -29,6 +29,17 @@ Signed-off-by: Daniel Golle <daniel@makrotopia.org>
 +		mtd = open_mtd_device("data");
 +
 +	if (!IS_ERR(mtd)) {
++		size_t len;
++		char magic[6];
++
++		/* check for a tar file magic */
++		err = mtd_read(mtd, 261, 6, &len, (void *) magic);
++		if (!err && len == 6 && !strncmp(magic, "ustar", 5)) {
++			ubi_err("cannot attach mtd%d as there is a valid tar magic", mtd->index);
++			put_mtd_device(mtd);
++			return;
++		}
++
 +		/* auto-add only media types where UBI makes sense */
 +		if (mtd->type == MTD_NANDFLASH ||
 +		    mtd->type == MTD_DATAFLASH ||
@@ -48,7 +59,7 @@ Signed-off-by: Daniel Golle <daniel@makrotopia.org>
  static int __init ubi_init(void)
  {
  	int err, i, k;
-@@ -1290,6 +1320,12 @@ static int __init ubi_init(void)
+@@ -1290,6 +1331,12 @@
  		}
  	}
  
diff --git a/target/linux/generic/patches-3.14/490-ubi-auto-attach-mtd-device-named-ubi-or-data-on-boot.patch b/target/linux/generic/patches-3.14/490-ubi-auto-attach-mtd-device-named-ubi-or-data-on-boot.patch
index ed83666..ca785bf 100644
--- a/target/linux/generic/patches-3.14/490-ubi-auto-attach-mtd-device-named-ubi-or-data-on-boot.patch
+++ b/target/linux/generic/patches-3.14/490-ubi-auto-attach-mtd-device-named-ubi-or-data-on-boot.patch
@@ -11,7 +11,7 @@ Signed-off-by: Daniel Golle <daniel@makrotopia.org>
 
 --- a/drivers/mtd/ubi/build.c
 +++ b/drivers/mtd/ubi/build.c
-@@ -1209,6 +1209,36 @@ static struct mtd_info * __init open_mtd
+@@ -1209,6 +1209,48 @@
  	return mtd;
  }
  
@@ -23,12 +23,24 @@ Signed-off-by: Daniel Golle <daniel@makrotopia.org>
 +{
 +	int err;
 +	struct mtd_info *mtd;
++
 +	/* try attaching mtd device named "ubi" or "data" */
 +	mtd = open_mtd_device("ubi");
 +	if (IS_ERR(mtd))
 +		mtd = open_mtd_device("data");
 +
 +	if (!IS_ERR(mtd)) {
++		size_t len;
++		char magic[6];
++
++		/* check for a tar file magic */
++		err = mtd_read(mtd, 261, 6, &len, (void *) magic);
++		if (!err && len == 6 && !strncmp(magic, "ustar", 5)) {
++			ubi_err("cannot attach mtd%d as there is a valid tar magic", mtd->index);
++			put_mtd_device(mtd);
++			return;
++		}
++
 +		/* auto-add only media types where UBI makes sense */
 +		if (mtd->type == MTD_NANDFLASH ||
 +		    mtd->type == MTD_DATAFLASH ||
@@ -48,7 +60,7 @@ Signed-off-by: Daniel Golle <daniel@makrotopia.org>
  static int __init ubi_init(void)
  {
  	int err, i, k;
-@@ -1298,6 +1328,12 @@ static int __init ubi_init(void)
+@@ -1298,6 +1340,12 @@
  		}
  	}
  
diff --git a/target/linux/generic/patches-3.18/490-ubi-auto-attach-mtd-device-named-ubi-or-data-on-boot.patch b/target/linux/generic/patches-3.18/490-ubi-auto-attach-mtd-device-named-ubi-or-data-on-boot.patch
index ed83666..2b72665 100644
--- a/target/linux/generic/patches-3.18/490-ubi-auto-attach-mtd-device-named-ubi-or-data-on-boot.patch
+++ b/target/linux/generic/patches-3.18/490-ubi-auto-attach-mtd-device-named-ubi-or-data-on-boot.patch
@@ -11,7 +11,7 @@ Signed-off-by: Daniel Golle <daniel@makrotopia.org>
 
 --- a/drivers/mtd/ubi/build.c
 +++ b/drivers/mtd/ubi/build.c
-@@ -1209,6 +1209,36 @@ static struct mtd_info * __init open_mtd
+@@ -1209,6 +1209,47 @@
  	return mtd;
  }
  
@@ -29,6 +29,17 @@ Signed-off-by: Daniel Golle <daniel@makrotopia.org>
 +		mtd = open_mtd_device("data");
 +
 +	if (!IS_ERR(mtd)) {
++		size_t len;
++		char magic[6];
++
++		/* check for a tar file magic */
++		err = mtd_read(mtd, 261, 6, &len, (void *) magic);
++		if (!err && len == 6 && !strncmp(magic, "ustar", 5)) {
++			ubi_err("cannot attach mtd%d as there is a valid tar magic", mtd->index);
++			put_mtd_device(mtd);
++			return;
++		}
++
 +		/* auto-add only media types where UBI makes sense */
 +		if (mtd->type == MTD_NANDFLASH ||
 +		    mtd->type == MTD_DATAFLASH ||
@@ -48,7 +59,7 @@ Signed-off-by: Daniel Golle <daniel@makrotopia.org>
  static int __init ubi_init(void)
  {
  	int err, i, k;
-@@ -1298,6 +1328,12 @@ static int __init ubi_init(void)
+@@ -1298,6 +1339,12 @@
  		}
  	}
  
-- 
cgit v1.1