f97a442cb13f880365243e89948143e343024c90
[openwrt/svn-archive/archive.git] / target / linux / generic / patches-3.18 / 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 @@ -1209,6 +1209,48 @@
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_DATAFLASH ||
47 + mtd->type == MTD_MLCNANDFLASH) {
48 + mutex_lock(&ubi_devices_mutex);
49 + ubi_msg("auto-attach mtd%d", mtd->index);
50 + err = ubi_attach_mtd_dev(mtd, UBI_DEV_NUM_AUTO, 0, 0);
51 + mutex_unlock(&ubi_devices_mutex);
52 + if (err < 0) {
53 + ubi_err("cannot attach mtd%d", mtd->index);
54 + put_mtd_device(mtd);
55 + }
56 + }
57 + }
58 +}
59 +
60 static int __init ubi_init(void)
61 {
62 int err, i, k;
63 @@ -1298,6 +1340,12 @@
64 }
65 }
66
67 + /* auto-attach mtd devices only if built-in to the kernel and no ubi.mtd
68 + * parameter was given */
69 + if (config_enabled(CONFIG_MTD_ROOTFS_ROOT_DEV) &&
70 + !ubi_is_module() && !mtd_devs)
71 + ubi_auto_attach();
72 +
73 err = ubiblock_init();
74 if (err) {
75 ubi_err("block: cannot initialize, error %d", err);