generic: early (incomplete!) 4.0 support
[openwrt/svn-archive/archive.git] / target / linux / generic / patches-4.0 / 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 @@ -1213,6 +1213,49 @@ static struct mtd_info * __init open_mtd
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 +
27 + /* try attaching mtd device named "ubi" or "data" */
28 + mtd = open_mtd_device("ubi");
29 + if (IS_ERR(mtd))
30 + mtd = open_mtd_device("data");
31 +
32 + if (!IS_ERR(mtd)) {
33 + size_t len;
34 + char magic[4];
35 +
36 + /* check for a valid ubi magic */
37 + err = mtd_read(mtd, 0, 4, &len, (void *) magic);
38 + if (!err && len == 4 && strncmp(magic, "UBI#", 4)) {
39 + ubi_err("no valid UBI magic found inside mtd%d", mtd->index);
40 + put_mtd_device(mtd);
41 + return;
42 + }
43 +
44 + /* auto-add only media types where UBI makes sense */
45 + if (mtd->type == MTD_NANDFLASH ||
46 + mtd->type == MTD_NORFLASH ||
47 + mtd->type == MTD_DATAFLASH ||
48 + mtd->type == MTD_MLCNANDFLASH) {
49 + mutex_lock(&ubi_devices_mutex);
50 + ubi_msg("auto-attach mtd%d", mtd->index);
51 + err = ubi_attach_mtd_dev(mtd, UBI_DEV_NUM_AUTO, 0, 0);
52 + mutex_unlock(&ubi_devices_mutex);
53 + if (err < 0) {
54 + ubi_err("cannot attach mtd%d", mtd->index);
55 + put_mtd_device(mtd);
56 + }
57 + }
58 + }
59 +}
60 +
61 static int __init ubi_init(void)
62 {
63 int err, i, k;
64 @@ -1305,6 +1348,12 @@ static int __init ubi_init(void)
65 }
66 }
67
68 + /* auto-attach mtd devices only if built-in to the kernel and no ubi.mtd
69 + * parameter was given */
70 + if (config_enabled(CONFIG_MTD_ROOTFS_ROOT_DEV) &&
71 + !ubi_is_module() && !mtd_devs)
72 + ubi_auto_attach();
73 +
74 err = ubiblock_init();
75 if (err) {
76 pr_err("UBI error: block: cannot initialize, error %d", err);