e1992c9a29d7614b5e133aef56afc6b204a3cb2b
[openwrt/openwrt.git] / target / linux / generic / patches-3.14 / 491-ubi-auto-create-ubiblock-device-for-rootfs.patch
1 From 0f3966579815f889bb2fcb4846152c35f65e79c4 Mon Sep 17 00:00:00 2001
2 From: Daniel Golle <daniel@makrotopia.org>
3 Date: Thu, 15 May 2014 21:06:33 +0200
4 Subject: [PATCH 2/5] ubi: auto-create ubiblock device for rootfs
5 To: openwrt-devel@lists.openwrt.org
6
7 Signed-off-by: Daniel Golle <daniel@makrotopia.org>
8 ---
9 drivers/mtd/ubi/block.c | 42 ++++++++++++++++++++++++++++++++++++++++++
10 1 file changed, 42 insertions(+)
11
12 diff --git a/drivers/mtd/ubi/block.c b/drivers/mtd/ubi/block.c
13 index 8d659e6..2dbe2f4 100644
14 --- a/drivers/mtd/ubi/block.c
15 +++ b/drivers/mtd/ubi/block.c
16 @@ -593,6 +593,44 @@ static int __init ubiblock_create_from_param(void)
17 return ret;
18 }
19
20 +#define UBIFS_NODE_MAGIC 0x06101831
21 +static inline int ubi_vol_is_ubifs(struct ubi_volume_desc *desc)
22 +{
23 + int ret;
24 + uint32_t magic_of, magic;
25 + ret = ubi_read(desc, 0, (char *)&magic_of, 0, 4);
26 + if (ret)
27 + return 0;
28 + magic = le32_to_cpu(magic_of);
29 + return magic == UBIFS_NODE_MAGIC;
30 +}
31 +
32 +static void __init ubiblock_create_auto_rootfs(void)
33 +{
34 + int ubi_num, ret, is_ubifs;
35 + struct ubi_volume_desc *desc;
36 + struct ubi_volume_info vi;
37 +
38 + for (ubi_num = 0; ubi_num < UBI_MAX_DEVICES; ubi_num++) {
39 + desc = ubi_open_volume_nm(ubi_num, "rootfs", UBI_READONLY);
40 + if (IS_ERR(desc))
41 + continue;
42 +
43 + ubi_get_volume_info(desc, &vi);
44 + is_ubifs = ubi_vol_is_ubifs(desc);
45 + ubi_close_volume(desc);
46 + if (is_ubifs)
47 + break;
48 +
49 + ret = ubiblock_create(&vi);
50 + if (ret)
51 + ubi_err("block: can't add '%s' volume, err=%d\n",
52 + vi.name, ret);
53 + /* always break if we get here */
54 + break;
55 + }
56 +}
57 +
58 static void ubiblock_remove_all(void)
59 {
60 struct ubiblock *next;
61 @@ -623,6 +661,10 @@ int __init ubiblock_init(void)
62 if (ret)
63 goto err_remove;
64
65 + /* auto-attach "rootfs" volume if existing and non-ubifs */
66 + if (config_enabled(CONFIG_MTD_ROOTFS_ROOT_DEV))
67 + ubiblock_create_auto_rootfs();
68 +
69 /*
70 * Block devices are only created upon user requests, so we ignore
71 * existing volumes.
72 --
73 1.9.2
74