kernel: fix ubi auto attach logging
[openwrt/openwrt.git] / target / linux / generic / patches-4.9 / 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 Index: linux-4.9.17/drivers/mtd/ubi/build.c
13 ===================================================================
14 --- linux-4.9.17.orig/drivers/mtd/ubi/build.c
15 +++ linux-4.9.17/drivers/mtd/ubi/build.c
16 @@ -1212,6 +1212,49 @@ static struct mtd_info * __init open_mtd
17 return mtd;
18 }
19
20 +/*
21 + * This function tries attaching mtd partitions named either "ubi" or "data"
22 + * during boot.
23 + */
24 +static void __init ubi_auto_attach(void)
25 +{
26 + int err;
27 + struct mtd_info *mtd;
28 +
29 + /* try attaching mtd device named "ubi" or "data" */
30 + mtd = open_mtd_device("ubi");
31 + if (IS_ERR(mtd))
32 + mtd = open_mtd_device("data");
33 +
34 + if (!IS_ERR(mtd)) {
35 + size_t len;
36 + char magic[4];
37 +
38 + /* check for a valid ubi magic */
39 + err = mtd_read(mtd, 0, 4, &len, (void *) magic);
40 + if (!err && len == 4 && strncmp(magic, "UBI#", 4)) {
41 + pr_err("UBI error: no valid UBI magic found inside mtd%d", mtd->index);
42 + put_mtd_device(mtd);
43 + return;
44 + }
45 +
46 + /* auto-add only media types where UBI makes sense */
47 + if (mtd->type == MTD_NANDFLASH ||
48 + mtd->type == MTD_NORFLASH ||
49 + mtd->type == MTD_DATAFLASH ||
50 + mtd->type == MTD_MLCNANDFLASH) {
51 + mutex_lock(&ubi_devices_mutex);
52 + pr_notice("UBI: auto-attach mtd%d\n", mtd->index);
53 + err = ubi_attach_mtd_dev(mtd, UBI_DEV_NUM_AUTO, 0, 0);
54 + mutex_unlock(&ubi_devices_mutex);
55 + if (err < 0) {
56 + pr_err("UBI error: cannot attach mtd%d", mtd->index);
57 + put_mtd_device(mtd);
58 + }
59 + }
60 + }
61 +}
62 +
63 static int __init ubi_init(void)
64 {
65 int err, i, k;
66 @@ -1295,6 +1338,12 @@ static int __init ubi_init(void)
67 }
68 }
69
70 + /* auto-attach mtd devices only if built-in to the kernel and no ubi.mtd
71 + * parameter was given */
72 + if (IS_ENABLED(CONFIG_MTD_ROOTFS_ROOT_DEV) &&
73 + !ubi_is_module() && !mtd_devs)
74 + ubi_auto_attach();
75 +
76 err = ubiblock_init();
77 if (err) {
78 pr_err("UBI error: block: cannot initialize, error %d", err);