kernel: make ubi auto-attach check for a tar file magic
[openwrt.git] / target / linux / generic / patches-3.10 / 490-ubi-auto-attach-mtd-device-named-ubi-or-data-on-boot.patch
1 From 8a52e4100d7c3a4a1dfddfa02b8864a9b0068c13 Mon Sep 17 00:00:00 2001
2 From: Daniel Golle <daniel@makrotopia.org>
3 Date: Sat, 17 May 2014 03:36:18 +0200
4 Subject: [PATCH 1/5] ubi: auto-attach mtd device named "ubi" or "data" on boot
5 To: openwrt-devel@lists.openwrt.org
6
7 Signed-off-by: Daniel Golle <daniel@makrotopia.org>
8 ---
9  drivers/mtd/ubi/build.c | 36 ++++++++++++++++++++++++++++++++++++
10  1 file changed, 36 insertions(+)
11
12 --- a/drivers/mtd/ubi/build.c
13 +++ b/drivers/mtd/ubi/build.c
14 @@ -1207,6 +1207,47 @@
15         return mtd;
16  }
17  
18 +/*
19 + * This function tries attaching mtd partitions named either "ubi" or "data"
20 + * during boot.
21 + */
22 +static void __init ubi_auto_attach(void)
23 +{
24 +       int err;
25 +       struct mtd_info *mtd;
26 +       /* try attaching mtd device named "ubi" or "data" */
27 +       mtd = open_mtd_device("ubi");
28 +       if (IS_ERR(mtd))
29 +               mtd = open_mtd_device("data");
30 +
31 +       if (!IS_ERR(mtd)) {
32 +               size_t len;
33 +               char magic[6];
34 +
35 +               /* check for a tar file magic */
36 +               err = mtd_read(mtd, 261, 6, &len, (void *) magic);
37 +               if (!err && len == 6 && !strncmp(magic, "ustar", 5)) {
38 +                       ubi_err("cannot attach mtd%d as there is a valid tar magic", mtd->index);
39 +                       put_mtd_device(mtd);
40 +                       return;
41 +               }
42 +
43 +               /* auto-add only media types where UBI makes sense */
44 +               if (mtd->type == MTD_NANDFLASH ||
45 +                   mtd->type == MTD_DATAFLASH ||
46 +                   mtd->type == MTD_MLCNANDFLASH) {
47 +                       mutex_lock(&ubi_devices_mutex);
48 +                       ubi_msg("auto-attach mtd%d", mtd->index);
49 +                       err = ubi_attach_mtd_dev(mtd, UBI_DEV_NUM_AUTO, 0, 0);
50 +                       mutex_unlock(&ubi_devices_mutex);
51 +                       if (err < 0) {
52 +                               ubi_err("cannot attach mtd%d", mtd->index);
53 +                               put_mtd_device(mtd);
54 +                       }
55 +               }
56 +       }
57 +}
58 +
59  static int __init ubi_init(void)
60  {
61         int err, i, k;
62 @@ -1290,6 +1331,12 @@
63                 }
64         }
65  
66 +       /* auto-attach mtd devices only if built-in to the kernel and no ubi.mtd
67 +        * parameter was given */
68 +       if (config_enabled(CONFIG_MTD_ROOTFS_ROOT_DEV) &&
69 +           !ubi_is_module() && !mtd_devs)
70 +               ubi_auto_attach();
71 +
72         err = ubiblock_init();
73         if (err) {
74                 ubi_err("block: cannot initialize, error %d", err);