summaryrefslogtreecommitdiff
path: root/package/system/fstools/patches/004-emmc.patch
blob: 86b8ca7019888d50fde5be5ebebd00ae0992e23a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
Index: fstools-2015-02-25.1/mount_root.c
===================================================================
--- fstools-2015-02-25.1.orig/mount_root.c
+++ fstools-2015-02-25.1/mount_root.c
@@ -62,6 +62,7 @@ start(int argc, char *argv[1])
 
 	case FS_JFFS2:
 	case FS_UBIFS:
+	case FS_EXT4:
 		mount_overlay(data);
 		break;
 
@@ -89,6 +90,7 @@ static int
 done(int argc, char *argv[1])
 {
 	struct volume *v = volume_find("rootfs_data");
+	FILE * fp = NULL;
 
 	if (!v)
 		return -1;
@@ -96,6 +98,19 @@ done(int argc, char *argv[1])
 	switch (volume_identify(v)) {
 	case FS_NONE:
 	case FS_DEADCODE:
+		/* if the mtd is created by block2mtd */
+		fp = fopen("/proc/cmdline", "rb");
+		if (fp) {
+			char buf[2048] = {0};
+			fread(buf, 1, sizeof(buf), fp);
+			fclose(fp);
+			buf[sizeof(buf)-1] = 0;
+			if (strstr(buf, "block2mtd")) {
+				fprintf(stderr, "%s is created by block2mtd, ext4_switch.\n", v->name);
+				return ext4_switch(v);
+			}
+		}
+		fprintf(stderr, "%s, normally, jffs2_switch.\n", v->name);
 		return jffs2_switch(v);
 	}
 
@@ -104,8 +119,11 @@ done(int argc, char *argv[1])
 
 int main(int argc, char **argv)
 {
-	if (argc < 2)
+	if (argc < 2) {
+        fprintf(stderr, "%s.\n", argv[0]);
 		return start(argc, argv);
+	}
+    fprintf(stderr, "%s %s.\n", argv[0], argv[1]);
 	if (!strcmp(argv[1], "stop"))
 		return stop(argc, argv);
 	if (!strcmp(argv[1], "done"))
Index: fstools-2015-02-25.1/libfstools/emmc.c
===================================================================
--- /dev/null
+++ fstools-2015-02-25.1/libfstools/emmc.c
@@ -0,0 +1,272 @@
+/*
+ * Copyright (C) 2015 Hua Shao <nossiac@163.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1
+ * as published by the Free Software Foundation
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#include <sys/mount.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <asm/byteorder.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <mtd/mtd-user.h>
+
+#include "libfstools.h"
+
+#include "volume.h"
+
+#define PATH_MAX		256
+
+struct emmc_priv {
+	int	fd;
+	int	idx;
+	char *chr;
+};
+
+static struct driver emmc_driver;
+
+
+static int emmc_partition_load(struct volume *v)
+{
+	struct emmc_priv *p = (struct emmc_priv*) v->priv;
+	struct mtd_info_user mtd_info;
+
+    fprintf(stderr, "%s(%s)\n", __FUNCTION__, v->name);
+	if (p->fd)
+		return 0;
+
+	p->fd = open(p->chr, O_RDWR | O_SYNC);
+	if (p->fd < 0) {
+		p->fd = 0;
+		fprintf(stderr, "Could not open block2mtd device: %s\n", p->chr);
+		return -1;
+	}
+
+    v->type = EMMC;
+
+	if (ioctl(p->fd, MEMGETINFO, &mtd_info)) {
+		close(p->fd);
+		fprintf(stderr, "Could not get MTD device info from %s(block2mtd)\n", p->chr);
+        v->size = 0; // TODO
+        v->block_size = -1; // TODO
+	}
+
+	v->size = mtd_info.size;
+	v->block_size = mtd_info.erasesize;
+
+	return 0;
+}
+
+
+static int block2mtd_find_index(char *name)
+{
+	FILE *fp = fopen("/proc/mtd", "r");
+	static char line[256];
+	char *index = NULL;
+
+	if(!fp)
+		return -1;
+
+	while (!index && fgets(line, sizeof(line), fp)) {
+		char *ret;
+
+		if ((ret = strstr(line, name)) && (ret[strlen(name)] == '"')) {
+			char *eol = strstr(line, ":");
+
+			if (!eol)
+				continue;
+
+			*eol = '\0';
+			index = &line[3];
+		}
+	}
+
+	fclose(fp);
+    if (index)
+    	return atoi(index);
+    else
+        return -1;
+}
+
+
+static int emmc_partition_find(struct volume *v, char *name)
+{
+	struct emmc_priv *p;
+	static char buffer[32];
+	FILE * fp = NULL;
+
+    fprintf(stderr, "%s(%p, %s)\n", __FUNCTION__, v, name);
+
+    fp = fopen("/proc/cmdline", "rb");
+    if (fp) {
+        char buf[2048] = {0};
+        fread(buf, 1, sizeof(buf), fp);
+        fclose(fp);
+        buf[sizeof(buf)-1] = 0;
+        if (!strstr(buf, "block2mtd")) {
+            fprintf(stderr, "%s is not created by block2mtd, skip.\n", v->name);
+            return -1;
+        }
+    }
+
+	p = calloc(1, sizeof(struct emmc_priv));
+	if (!p)
+		return -1;
+
+	p->idx = block2mtd_find_index(name);
+
+	v->priv = p;
+	v->name = strdup(name);
+	v->drv = &emmc_driver;
+
+	snprintf(buffer, sizeof(buffer), "/dev/mtdblock%d", p->idx);
+	v->blk = strdup(buffer);
+
+	snprintf(buffer, sizeof(buffer), "/dev/mtd%d", p->idx);
+	p->chr = strdup(buffer);
+
+	if (emmc_partition_load(v)) {
+		fprintf(stderr, "%s emmc_partition_load %s failed\n", __FUNCTION__, v->name);
+		return -1;
+	}
+
+    fprintf(stderr, "v->name = %s\n", v->name);
+    fprintf(stderr, "v->blk = %s\n", v->blk);
+    fprintf(stderr, "v->drv->name = %s\n", v->drv->name);
+    fprintf(stderr, "v->priv->fd = %d\n", p->fd);
+    fprintf(stderr, "v->priv->idx = %d\n", p->idx);
+    fprintf(stderr, "v->priv->chr = %s\n", p->chr);
+
+	return 0;
+}
+
+static int emmc_partition_identify(struct volume *v)
+{
+	struct emmc_priv *p = (struct emmc_priv*) v->priv;
+	__u32 deadc0de;
+	__u16 ext4;
+	size_t sz;
+
+    fprintf(stderr, "%s(%s)\n", __FUNCTION__, v->name);
+	if (emmc_partition_load(v)) {
+		fprintf(stderr, "%s, emmc_partition_load %s failed\n", __FUNCTION__, v->name);
+		return -1;
+	}
+
+	sz = read(p->fd, &deadc0de, sizeof(deadc0de));
+	if (sz != sizeof(deadc0de)) {
+		fprintf(stderr, "%s, reading %s failed: %s\n", __FUNCTION__, v->name, strerror(errno));
+		return -1;
+	}
+
+	deadc0de = __be32_to_cpu(deadc0de);
+	if (deadc0de == 0xdeadc0de) {
+		fprintf(stderr, "ext4 is not ready - marker found\n");
+		return FS_DEADCODE;
+	}
+
+    /* 0x400 -> super block offset in partition
+       0x38  -> magic offset in superblock
+     */
+    lseek(p->fd, 0x438, SEEK_SET);
+	sz = read(p->fd, &ext4, sizeof(ext4));
+	if (sz != sizeof(ext4)) {
+		fprintf(stderr, "reading %s failed: %s\n", v->name, strerror(errno));
+		return -1;
+	}
+
+	if (ext4 == 0xEF53) {
+		fprintf(stderr, "ext4 is ready\n");
+		return FS_EXT4;
+	}
+
+	fprintf(stderr, "No ext4 magic was found, expected %x, got %x\n", 0xEF53, ext4);
+
+	return FS_NONE;
+}
+
+static int emmc_partition_erase(struct volume *v, int offset, int len)
+{
+    fprintf(stderr, "%s(%s, %d, %d)\n", __FUNCTION__, v->name, offset, len);
+	return 0;
+}
+
+static int emmc_partition_erase_all(struct volume *v)
+{
+    fprintf(stderr, "%s, remove files instead of erase partitions.\n", __FUNCTION__);
+    system("rm -rf /overlay/*");
+	return 0;
+}
+
+static int emmc_partition_init(struct volume *v)
+{
+    fprintf(stderr, "%s(%s)\n", __FUNCTION__, v->name);
+	return 0;
+}
+
+static int emmc_partition_read(struct volume *v, void *buf, int offset, int length)
+{
+	struct emmc_priv *p = (struct emmc_priv*) v->priv;
+
+    fprintf(stderr, "%s(%s, %p, %d, %d)\n", __FUNCTION__, v->name, buf, offset, length);
+	if (emmc_partition_load(v))
+		return -1;
+
+	if (lseek(p->fd, offset, SEEK_SET) == (off_t) -1) {
+		fprintf(stderr, "lseek/read failed\n");
+		return -1;
+	}
+
+	if (read(p->fd, buf, length) == -1) {
+		fprintf(stderr, "read failed\n");
+		return -1;
+	}
+
+	return 0;
+}
+
+static int emmc_partition_write(struct volume *v, void *buf, int offset, int length)
+{
+
+    fprintf(stderr, "%s(%s, %p, %d, %d)\n", __FUNCTION__, v->name, buf, offset, length);
+
+#if 0
+	struct emmc_priv *p = (struct emmc_priv*) v->priv;
+	if (emmc_partition_load(v))
+		return -1;
+
+	if (lseek(p->fd, offset, SEEK_SET) == (off_t) -1) {
+		fprintf(stderr, "lseek/write failed at offset %d\n", offset);
+		perror("lseek");
+		return -1;
+	}
+
+	if (write(p->fd, buf, length) == -1) {
+		fprintf(stderr, "write failed\n");
+		return -1;
+	}
+#endif
+	return 0;
+}
+
+static struct driver emmc_driver = {
+	.name = "emmc",
+	.find = emmc_partition_find,
+	.init = emmc_partition_init,
+	.erase = emmc_partition_erase,
+	.erase_all = emmc_partition_erase_all,
+	.read = emmc_partition_read,
+	.write = emmc_partition_write,
+	.identify = emmc_partition_identify,
+};
+DRIVER(emmc_driver);
Index: fstools-2015-02-25.1/libfstools/libfstools.h
===================================================================
--- fstools-2015-02-25.1.orig/libfstools/libfstools.h
+++ fstools-2015-02-25.1/libfstools/libfstools.h
@@ -25,6 +25,7 @@ enum {
 	FS_JFFS2,
 	FS_DEADCODE,
 	FS_UBIFS,
+	FS_EXT4,
 };
 
 extern char const *extroot_prefix;
@@ -43,6 +44,7 @@ extern char* find_mount_point(char *bloc
 extern int find_filesystem(char *fs);
 
 extern int jffs2_switch(struct volume *v);
+extern int ext4_switch(struct volume *v);
 
 extern int handle_whiteout(const char *dir);
 extern void foreachdir(const char *dir, int (*cb)(const char*));
Index: fstools-2015-02-25.1/libfstools/volume.h
===================================================================
--- fstools-2015-02-25.1.orig/libfstools/volume.h
+++ fstools-2015-02-25.1/libfstools/volume.h
@@ -47,6 +47,7 @@ enum {
 	NANDFLASH,
 	NORFLASH,
 	UBIVOLUME,
+	EMMC,
 };
 
 struct volume {
Index: fstools-2015-02-25.1/libfstools/overlay.c
===================================================================
--- fstools-2015-02-25.1.orig/libfstools/overlay.c
+++ fstools-2015-02-25.1/libfstools/overlay.c
@@ -31,6 +31,7 @@
 #include "volume.h"
 
 #define SWITCH_JFFS2 "/tmp/.switch_jffs2"
+#define SWITCH_EXT4 "/tmp/.switch_ext4"
 
 void
 foreachdir(const char *dir, int (*cb)(const char*))
@@ -106,6 +107,59 @@ switch2jffs(struct volume *v)
 	return fopivot("/overlay", "/rom");
 }
 
+
+static int
+switch2ext4(struct volume *v)
+{
+	struct stat s;
+	int ret;
+
+	if (!stat(SWITCH_EXT4, &s)) {
+		fprintf(stderr, "ext4 switch already running\n");
+		return -1;
+	}
+
+	creat(SWITCH_EXT4, 0600);
+	ret = mount(v->blk, "/rom/overlay", "ext4", MS_NOATIME, NULL);
+	unlink(SWITCH_EXT4);
+	if (ret) {
+		fprintf(stderr, "failed - mount -t ext4 %s /rom/overlay: %s\n", v->blk, strerror(errno));
+
+        char buf[128] = {0};
+        fprintf(stderr, "maybe ext4 is not created yet.\n");
+        snprintf(buf, sizeof(buf), "mkfs.ext4 %s", v->blk);
+        system(buf);
+        fprintf(stderr, "%s.\n", buf);
+
+        creat(SWITCH_EXT4, 0600);
+        ret = mount(v->blk, "/rom/overlay", "ext4", MS_NOATIME, NULL);
+        unlink(SWITCH_EXT4);
+        if (ret) {
+            fprintf(stderr, "failed again, give up - mount -t ext4 %s /rom/overlay: %s\n", v->blk, strerror(errno));
+		    return -1;
+        }
+	}
+
+	if (mount("none", "/", NULL, MS_NOATIME | MS_REMOUNT, 0)) {
+		fprintf(stderr, "failed - mount -o remount,ro none: %s\n", strerror(errno));
+		return -1;
+	}
+
+	system("cp -a /tmp/root/* /rom/overlay");
+
+	if (pivot("/rom", "/mnt")) {
+		fprintf(stderr, "failed - pivot /rom /mnt: %s\n", strerror(errno));
+		return -1;
+	}
+
+	if (mount_move("/mnt", "/tmp/root", "")) {
+		fprintf(stderr, "failed - mount -o move /mnt /tmp/root %s\n", strerror(errno));
+		return -1;
+	}
+
+	return fopivot("/overlay", "/rom");
+}
+
 int
 handle_whiteout(const char *dir)
 {
@@ -199,6 +253,53 @@ jffs2_switch(struct volume *v)
 	return ret;
 }
 
+int
+ext4_switch(struct volume *v)
+{
+	char *mp;
+	int ret = -1;
+
+	if (find_overlay_mount("overlayfs:/tmp/root"))
+		return -1;
+
+	if (find_filesystem("overlay")) {
+		fprintf(stderr, "overlayfs not found\n");
+		return ret;
+	}
+
+	mp = find_mount_point(v->blk, 0);
+	if (mp) {
+		fprintf(stderr, "rootfs_data:%s is already mounted as %s\n", v->blk, mp);
+		return -1;
+	}
+
+	switch (volume_identify(v)) {
+	case FS_NONE:
+		fprintf(stderr, "no ext4 marker found\n");
+		/* fall through */
+
+	case FS_DEADCODE:
+		ret = switch2ext4(v);
+		if (!ret) {
+			fprintf(stderr, "doing fo cleanup\n");
+			umount2("/tmp/root", MNT_DETACH);
+			foreachdir("/overlay/", handle_whiteout);
+		}
+		break;
+
+	case FS_EXT4:
+		ret = overlay_mount(v, "ext4");
+		if (ret)
+			break;
+		if (mount_move("/tmp", "", "/overlay") || fopivot("/overlay", "/rom")) {
+			fprintf(stderr, "switching to ext4 failed\n");
+			ret = -1;
+		}
+		break;
+	}
+	return ret;
+}
+
 static int overlay_mount_fs(struct volume *v)
 {
 	char *fstype;
@@ -214,12 +315,29 @@ static int overlay_mount_fs(struct volum
 	case FS_UBIFS:
 		fstype = "ubifs";
 		break;
+    case FS_EXT4:
+        fstype = "ext4";
+        break;
 	}
 
 	if (mount(v->blk, "/tmp/overlay", fstype, MS_NOATIME, NULL)) {
 		fprintf(stderr, "failed to mount -t %s %s /tmp/overlay: %s\n",
 				fstype, v->blk, strerror(errno));
-		return -1;
+
+        if (0 == strcmp(fstype, "ext4")) {
+            /* maybe ext4 is not created yet! */
+            char buf[128] = {0};
+            fprintf(stderr, "maybe ext4 is not created yet.\n");
+            snprintf(buf, sizeof(buf), "mkfs.ext4 %s", v->blk);
+            system(buf);
+            fprintf(stderr, "%s.\n", buf);
+
+            if (mount(v->blk, "/tmp/overlay", fstype, MS_NOATIME, NULL)) {
+                fprintf(stderr, "again! failed to mount -t %s %s /tmp/overlay: %s\n",
+                        fstype, v->blk, strerror(errno));
+                return -1;
+            }
+        }
 	}
 
 	volume_init(v);
@@ -250,7 +368,7 @@ int mount_overlay(struct volume *v)
 
 	fprintf(stderr, "switching to overlay\n");
 	if (mount_move("/tmp", "", "/overlay") || fopivot("/overlay", "/rom")) {
-		fprintf(stderr, "switching to jffs2 failed - fallback to ramoverlay\n");
+		fprintf(stderr, "switching to overlay failed - fallback to ramoverlay\n");
 		return ramoverlay();
 	}
 
Index: fstools-2015-02-25.1/CMakeLists.txt
===================================================================
--- fstools-2015-02-25.1.orig/CMakeLists.txt
+++ fstools-2015-02-25.1/CMakeLists.txt
@@ -10,6 +10,7 @@ ADD_LIBRARY(fstools SHARED
 		libfstools/extroot.c
 		libfstools/overlay.c
 		libfstools/volume.c
+		libfstools/emmc.c
 		libfstools/mtd.c
 		libfstools/mount.c
 		libfstools/ubi.c
Index: fstools-2015-02-25.1/libfstools/mtd.c
===================================================================
--- fstools-2015-02-25.1.orig/libfstools/mtd.c
+++ fstools-2015-02-25.1/libfstools/mtd.c
@@ -153,10 +153,25 @@ static int mtd_volume_find(struct volume
 	char *idx = mtd_find_index(name);
 	struct mtd_priv *p;
 	char buffer[32];
+	size_t sz;
+    FILE * fp = NULL;
 
 	if (!idx)
 		return -1;
 
+    /* if the mtd is created by block2mtd */
+    fp = fopen("/proc/cmdline", "rb");
+    if (fp) {
+        char buf[2048] = {0};
+        sz = fread(buf, 1, sizeof(buf), fp);
+		fclose(fp);
+        buf[sizeof(buf)-1] = 0;
+        if (strstr(buf, "block2mtd")) {
+            fprintf(stderr, "%s, %s is created by block2mtd.\n", __FUNCTION__, name);
+            return -1;
+        }
+    }
+
 	p = calloc(1, sizeof(struct mtd_priv));
 	if (!p)
 		return -1;
@@ -186,12 +201,26 @@ static int mtd_volume_identify(struct vo
 	__u32 deadc0de;
 	__u16 jffs2;
 	size_t sz;
+    FILE * fp = NULL;
 
 	if (mtd_volume_load(v)) {
 		fprintf(stderr, "reading %s failed\n", v->name);
 		return -1;
 	}
 
+    /* if the mtd is created by block2mtd */
+    fp = fopen("/proc/cmdline", "rb");
+    if (fp) {
+        char buf[2048] = {0};
+        sz = fread(buf, 1, sizeof(buf), fp);
+		fclose(fp);
+        buf[sizeof(buf)-1] = 0;
+        if (strstr(buf, "block2mtd")) {
+            fprintf(stderr, "%s, %s is created by block2mtd, default use FS_EXT4.\n", __FUNCTION__, v->name);
+            return FS_EXT4;
+        }
+    }
+
 	sz = read(p->fd, &deadc0de, sizeof(deadc0de));
 
 	if (sz != sizeof(deadc0de)) {