1 From ece50ef6d4b5191636c971ee3896ca22fa538625 Mon Sep 17 00:00:00 2001
2 From: Gabor Juhos <juhosg@openwrt.org>
3 Date: Tue, 5 Nov 2013 16:16:03 +0100
4 Subject: [PATCH] mount_root: fix jffs2 handling on MTD devices emulated by
7 The jffs2_ready() function in mount_root.c checks
8 the presence of various JFFS2 markers at the start
9 of a given MTD device. The function works on NOR
10 flashes because JFFS2 puts 'cleanmarker' nodes at
11 the start of freshly erased blocks.
13 However if jffs2 is used on a MTD device emulated
14 by the gluebi layer, the 'cleanmarker' nodes are
15 not present and the jffs2_ready() function fails.
17 Update the code to handle jffs2 correctly even on
18 MTD devices emulated by the gluebi layer.
20 Signed-off-by: Gabor Juhos <juhosg@openwrt.org>
22 mount_root.c | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++++-----
23 1 file changed, 83 insertions(+), 7 deletions(-)
27 @@ -54,6 +54,14 @@ enum {
39 static const char *argv0;
41 /* this is a raw syscall - man 2 pivot_root */
42 @@ -223,6 +231,64 @@ static int find_mtd_char(char *name, cha
46 +static enum mtd_type mtd_get_type(char *index)
49 + static char buf[32];
53 + snprintf(p, sizeof(p), "/sys/class/mtd/mtd%s/type", index);
57 + ERROR("unable to open %s\n", p);
58 + return MTD_TYPE_NOT_FOUND;
61 + memset(buf, 0, sizeof(buf));
62 + sz = fread(buf, 1, sizeof(buf) - 1, fp);
66 + ERROR("unable to read from %s\n", p);
67 + return MTD_TYPE_UNKNOWN;
73 + if (buf[sz] != '\n')
79 + if (strcmp(buf, "nor") == 0)
80 + return MTD_TYPE_NOR;
82 + if (strcmp(buf, "nand") == 0)
83 + return MTD_TYPE_NAND;
85 + if (strcmp(buf, "ubi") == 0)
86 + return MTD_TYPE_UBI;
88 + ERROR("mtd%s has unknow type '%s'\n", index, buf);
89 + return MTD_TYPE_UNKNOWN;
92 +static enum mtd_type mtd_get_type_by_name(char *name)
96 + index = find_mtd_index(name);
98 + return MTD_TYPE_NOT_FOUND;
100 + return mtd_get_type(index);
104 static int mtd_unlock(char *mtd)
106 struct erase_info_user mtdlock;
107 @@ -277,7 +343,7 @@ static int mtd_mount_jffs2(void)
108 return mtd_unlock(rootfs_data);
111 -static int jffs2_ready(char *mtd)
112 +static int jffs2_ready(char *mtd, enum mtd_type type)
114 FILE *fp = fopen(mtd, "r");
116 @@ -298,16 +364,21 @@ static int jffs2_ready(char *mtd)
119 deadc0de = __be32_to_cpu(deadc0de);
120 - jffs2 = __be16_to_cpu(deadc0de >> 16);
121 + if (deadc0de == 0xdeadc0de) {
122 + LOG("jffs2 is not ready - EOF marker found\n");
123 + return FS_DEADCODE;
126 + jffs2 = __be16_to_cpu(deadc0de >> 16);
127 if (jffs2 == 0x1985) {
128 LOG("jffs2 is ready\n");
132 - if (deadc0de == 0xdeadc0de) {
133 - LOG("jffs2 is not ready - marker found\n");
134 - return FS_DEADCODE;
135 + if (type == MTD_TYPE_UBI &&
136 + deadc0de == 0xffffffff) {
137 + LOG("jffs2 is ready\n");
141 ERROR("No jffs2 marker was found\n");
142 @@ -638,6 +709,7 @@ static int main_switch2jffs(int argc, ch
146 + enum mtd_type type;
148 if (find_overlay_mount("overlayfs:/tmp/root"))
150 @@ -659,7 +731,8 @@ static int main_switch2jffs(int argc, ch
154 - switch (jffs2_ready(mtd)) {
155 + type = mtd_get_type_by_name("rootfs_data");
156 + switch (jffs2_ready(mtd, type)) {
158 ERROR("no jffs2 marker found\n");
160 @@ -781,12 +854,15 @@ int main(int argc, char **argv)
161 LOG("mounting /dev/root\n");
162 mount("/dev/root", "/", NULL, MS_NOATIME | MS_REMOUNT, 0);
164 + enum mtd_type type;
167 fprintf(stderr, "mount_root: switched to extroot\n");
171 - switch (jffs2_ready(mtd)) {
172 + type = mtd_get_type_by_name("rootfs_data");
173 + switch (jffs2_ready(mtd, type)) {