summaryrefslogtreecommitdiff
path: root/mount_root.c
blob: 1927efa13f45911196e5260bc513ddc933bfe611 (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
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
/*
 * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
 * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
 *
 * 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 <stdio.h>
#include <string.h>
#include <getopt.h>
#include <syslog.h>
#include <errno.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <libgen.h>
#include <glob.h>
#include <dirent.h>

#include <sys/stat.h>
#include <sys/types.h>
#include <sys/mount.h>
#include <sys/wait.h>

#include <asm/byteorder.h>

#include <mtd/mtd-user.h>

#define DEBUG(level, fmt, ...) do { \
	if (debug >= level) \
		fprintf(stderr, "%s %s(%d): " fmt, argv0, __func__, __LINE__, ## __VA_ARGS__); \
	} while (0)

#define LOG(fmt, ...) do { \
		syslog(LOG_INFO, fmt, ## __VA_ARGS__); \
		fprintf(stderr, "%s: "fmt, argv0, ## __VA_ARGS__); \
	} while (0)

#define ERROR(fmt, ...) do { \
		syslog(LOG_ERR, fmt, ## __VA_ARGS__); \
		fprintf(stderr, "%s: "fmt, argv0, ## __VA_ARGS__); \
	} while (0)

enum {
	FS_NONE,
	FS_JFFS2,
	FS_DEADCODE,
};

static const char *argv0;

/* this is a raw syscall - man 2 pivot_root */
extern int pivot_root(const char *new_root, const char *put_old);

static int debug = 0;

static void foreachdir(const char *dir, int (*cb)(const char*))
{
	char globdir[256];
	glob_t gl;
	int j;

	if (dir[strlen(dir) - 1] == '/')
		snprintf(globdir, 256, "%s*", dir);
	else
		snprintf(globdir, 256, "%s/*", dir);

	if (!glob(globdir, GLOB_NOESCAPE | GLOB_MARK | GLOB_ONLYDIR, NULL, &gl))
		for (j = 0; j < gl.gl_pathc; j++)
			foreachdir(gl.gl_pathv[j], cb);

	cb(dir);
}

static int find_overlay_mount(char *overlay)
{
	FILE *fp = fopen("/proc/mounts", "r");
	static char line[256];
	int ret = -1;

	if(!fp)
		return ret;

	while (ret && fgets(line, sizeof(line), fp))
		if (!strncmp(line, overlay, strlen(overlay)))
			ret = 0;

	fclose(fp);

	return ret;
}

static char* find_mount(char *mp)
{
	FILE *fp = fopen("/proc/mounts", "r");
	static char line[256];
	char *point = NULL;

	if(!fp)
		return NULL;

	while (fgets(line, sizeof(line), fp)) {
		char *s, *t = strstr(line, " ");

		if (!t) {
			fclose(fp);
			return NULL;
		}
		t++;
		s = strstr(t, " ");
		if (!s) {
			fclose(fp);
			return NULL;
		}
		*s = '\0';

		if (!strcmp(t, mp)) {
			fclose(fp);
			return t;
		}
	}

	fclose(fp);

	return point;
}

static char* find_mount_point(char *block, char *fs)
{
	FILE *fp = fopen("/proc/mounts", "r");
	static char line[256];
	int len = strlen(block);
	char *point = NULL;

	if(!fp)
		return NULL;

	while (fgets(line, sizeof(line), fp)) {
		if (!strncmp(line, block, len)) {
			char *p = &line[len + 1];
			char *t = strstr(p, " ");

			if (!t) {
				fclose(fp);
				return NULL;
			}

			*t = '\0';
			t++;

			if (fs && strncmp(t, fs, strlen(fs))) {
				fclose(fp);
				ERROR("block is mounted with wrong fs\n");
				return NULL;
			}
			point = p;
			break;
		}
	}

	fclose(fp);

	return point;
}

static char* find_mtd_index(char *name)
{
	FILE *fp = fopen("/proc/mtd", "r");
	static char line[256];
	char *index = NULL;

	if(!fp)
		return index;

	while (!index && fgets(line, sizeof(line), fp)) {
		if (strstr(line, name)) {
			char *eol = strstr(line, ":");

			if (!eol)
				continue;

			*eol = '\0';
			index = &line[3];
			DEBUG(1, "found %s -> index:%s\n", name, index);
		}
	}

	fclose(fp);

	return index;
}

static int find_mtd_block(char *name, char *part, int plen)
{
	char *index = find_mtd_index(name);

	if (!index)
		return -1;

	snprintf(part, plen, "/dev/mtdblock%s", index);
	DEBUG(1, "found %s -> %s\n", name, part);

	return 0;
}

static int find_mtd_char(char *name, char *part, int plen)
{
	char *index = find_mtd_index(name);

	if (!index)
		return -1;

	snprintf(part, plen, "/dev/mtd%s", index);
	DEBUG(1, "found %s -> %s\n", name, part);

	return 0;
}

static int mtd_unlock(char *mtd)
{
	struct erase_info_user mtdlock;
	struct mtd_info_user mtdinfo;
	int fd = open(mtd, O_RDWR | O_SYNC);
	int ret = -1;

	DEBUG(1, "%s\n", mtd);

	if (!fd) {
		ERROR("failed to open %s: %s\n", mtd, strerror(errno));
		return -1;
	}

	ret = ioctl(fd, MEMGETINFO, &mtdinfo);
	if (ret) {
		ERROR("ioctl(%s, MEMGETINFO) failed: %s\n", mtd, strerror(errno));
		goto err_out;
	}

	mtdlock.start = 0;
	mtdlock.length = mtdinfo.size;
	ioctl(fd, MEMUNLOCK, &mtdlock);

err_out:
	close(fd);

	return ret;
}

static int mtd_mount_jffs2(void)
{
	char rootfs_data[32];

	if (mkdir("/tmp/overlay", 0755)) {
		ERROR("failed to mkdir /tmp/overlay: %s\n", strerror(errno));
		return -1;
	}

	if (find_mtd_block("rootfs_data", rootfs_data, sizeof(rootfs_data))) {
		ERROR("rootfs_data does not exist\n");
		return -1;
	}

	if (mount(rootfs_data, "/tmp/overlay", "jffs2", MS_NOATIME, NULL)) {
		ERROR("failed to mount -t jffs2 %s /tmp/overlay: %s\n", rootfs_data, strerror(errno));
		return -1;
	}

	find_mtd_char("rootfs_data", rootfs_data, sizeof(rootfs_data));

	return mtd_unlock(rootfs_data);
}

static int jffs2_ready(char *mtd)
{
	FILE *fp = fopen(mtd, "r");
	__u32 deadc0de;
	__u16 jffs2;
	size_t sz;

	if (!fp) {
		ERROR("reading %s failed\n", mtd);
		exit(-1);
	}

	sz = fread(&deadc0de, sizeof(deadc0de), 1, fp);
	fclose(fp);

	if (sz != 1) {
		ERROR("reading %s failed: %s\n", mtd, strerror(errno));
		exit(-1);
	}

	deadc0de = __be32_to_cpu(deadc0de);
	jffs2 = __be16_to_cpu(deadc0de >> 16);

	if (jffs2 == 0x1985) {
		LOG("jffs2 is ready\n");
		return FS_JFFS2;
	}

	if (deadc0de == 0xdeadc0de) {
		LOG("jffs2 is not ready - marker found\n");
		return FS_DEADCODE;
	}

	ERROR("No jffs2 marker was found\n");

	return FS_NONE;
}

static int check_fs_exists(char *fs)
{
	FILE *fp = fopen("/proc/filesystems", "r");
	static char line[256];
	int ret = -1;

	DEBUG(2, "%s\n", fs);

	if (!fp) {
		ERROR("opening /proc/filesystems failed: %s\n", strerror(errno));
		goto out;
	}

	while (ret && fgets(line, sizeof(line), fp))
		if (strstr(line, fs))
			ret = 0;

	fclose(fp);

out:
	return ret;
}

static int mount_move(char *oldroot, char *newroot, char *dir)
{
#ifndef MS_MOVE
#define MS_MOVE	(1 << 13)
#endif
	struct stat s;
	char olddir[64];
	char newdir[64];
	int ret;

	DEBUG(2, "%s %s %s\n", oldroot, newroot, dir);

	snprintf(olddir, sizeof(olddir), "%s%s", oldroot, dir);
	snprintf(newdir, sizeof(newdir), "%s%s", newroot, dir);

	if (stat(olddir, &s) || !S_ISDIR(s.st_mode))
		return -1;

	if (stat(newdir, &s) || !S_ISDIR(s.st_mode))
		return -1;

	ret = mount(olddir, newdir, NULL, MS_NOATIME | MS_MOVE, NULL);

	if (ret)
		DEBUG(1, "failed %s %s: %s\n", olddir, newdir, strerror(errno));

	return ret;
}

static int pivot(char *new, char *old)
{
	char pivotdir[64];
	int ret;

	DEBUG(2, "%s %s\n", new, old);

	if (mount_move("", new, "/proc"))
		return -1;

	snprintf(pivotdir, sizeof(pivotdir), "%s%s", new, old);

	ret = pivot_root(new, pivotdir);

	if (ret < 0) {
		ERROR("pivot_root failed %s %s: %s\n", new, pivotdir, strerror(errno));
		return -1;
	}

	mount_move(old, "", "/dev");
	mount_move(old, "", "/tmp");
	mount_move(old, "", "/sys");
	mount_move(old, "", "/overlay");

	return 0;
}

static int fopivot(char *rw_root, char *ro_root)
{
	char overlay[64], lowerdir[64];

	DEBUG(2, "%s %s\n", rw_root, ro_root);

	if (check_fs_exists("overlay")) {
		ERROR("BUG: no suitable fs found\n");
		return -1;
	}

	snprintf(overlay, sizeof(overlay), "overlayfs:%s", rw_root);
	snprintf(lowerdir, sizeof(lowerdir), "lowerdir=/,upperdir=%s", rw_root);

	if (mount(overlay, "/mnt", "overlayfs", MS_NOATIME, lowerdir)) {
		ERROR("mount failed: %s\n", strerror(errno));
		return -1;
	}

	return pivot("/mnt", ro_root);
}

static int ramoverlay(void)
{
	DEBUG(2, "\n");

	mkdir("/tmp/root", 0755);
	mount("tmpfs", "/tmp/root", "tmpfs", MS_NOATIME, "mode=0755");

	return fopivot("/tmp/root", "/rom");
}

static int switch2jffs(void)
{
	char mtd[32];

	if (!find_mtd_block("rootfs_patches", mtd, sizeof(mtd)))
		return 0;

	if (find_mtd_block("rootfs_data", mtd, sizeof(mtd))) {
		ERROR("no rootfs_data was found\n");
		return -1;
	}

	if (mount(mtd, "/rom/overlay", "jffs2", MS_NOATIME, NULL)) {
		ERROR("failed - mount -t jffs2 %s /rom/overlay: %s\n", mtd, strerror(errno));
		return -1;
	}

	if (mount("none", "/", NULL, MS_NOATIME | MS_REMOUNT, 0)) {
		ERROR("failed - mount -o remount,ro none: %s\n", strerror(errno));
		return -1;
	}

	system("cp -a /tmp/root/* /rom/overlay");

	if (pivot("/rom", "/mnt")) {
		ERROR("failed - pivot /rom /mnt: %s\n", strerror(errno));
		return -1;
	}

	if (mount_move("/mnt", "/tmp/root", "")) {
		ERROR("failed - mount -o move /mnt /tmp/root %s\n", strerror(errno));
		return -1;
	}

	return fopivot("/overlay", "/rom");
}

static int handle_whiteout(const char *dir)
{
	struct stat s;
	char link[256];
	ssize_t sz;
	struct dirent **namelist;
	int n;

	n = scandir(dir, &namelist, NULL, NULL);

	if (n < 1)
		return -1;

	while (n--) {
		char file[256];

		snprintf(file, sizeof(file), "%s%s", dir, namelist[n]->d_name);
		if (!lstat(file, &s) && S_ISLNK(s.st_mode)) {
			sz = readlink(file, link, sizeof(link) - 1);
			if (sz > 0) {
				char *orig;

				link[sz] = '\0';
				orig = strstr(&file[1], "/");
				if (orig && !strcmp(link, "(overlay-whiteout)")) {
					DEBUG(1, "unlinking %s\n", orig);
					unlink(orig);
				}
			}
		}
		free(namelist[n]);
	}
	free(namelist);

	return 0;
}

static int mtd_erase(const char *mtd)
{
	int fd = open(mtd, O_RDWR | O_SYNC);
	struct mtd_info_user i;
	struct erase_info_user e;
	int ret;

	if (!fd) {
		ERROR("failed to open %s: %s\n", mtd, strerror(errno));
		return -1;
	}

	ret = ioctl(fd, MEMGETINFO, &i);
	if (ret) {
		ERROR("ioctl(%s, MEMGETINFO) failed: %s\n", mtd, strerror(errno));
		return -1;
	}

	e.length = i.erasesize;
	for (e.start = 0; e.start < i.size; e.start += i.erasesize) {
		ioctl(fd, MEMUNLOCK, &e);
		if(ioctl(fd, MEMERASE, &e))
			ERROR("Failed to erase block on %s at 0x%x\n", mtd, e.start);
	}

	close(fd);
	return 0;
}

static int ask_user(int argc, char **argv)
{
	if ((argc < 2) || strcmp(argv[1], "-y")) {
		LOG("This will erase all settings and remove any installed packages. Are you sure? [N/y]\n");
		if (getchar() != 'y')
			return -1;
	}
	return 0;

}

static int handle_rmdir(const char *dir)
{
	struct stat s;
	struct dirent **namelist;
	int n;

	n = scandir(dir, &namelist, NULL, NULL);

	if (n < 1)
		return -1;

	while (n--) {
		char file[256];

		snprintf(file, sizeof(file), "%s%s", dir, namelist[n]->d_name);
		if (!lstat(file, &s) && !S_ISDIR(s.st_mode)) {
			DEBUG(1, "unlinking %s\n", file);
			unlink(file);
		}
		free(namelist[n]);
	}
	free(namelist);

	DEBUG(1, "rmdir %s\n", dir);
	rmdir(dir);

	return 0;
}

static int main_jffs2reset(int argc, char **argv)
{
	char mtd[32];
	char *mp;

	if (ask_user(argc, argv))
		return -1;

	if (check_fs_exists("overlay")) {
		ERROR("overlayfs not found\n");
		return -1;
	}

	if (find_mtd_block("rootfs_data", mtd, sizeof(mtd))) {
		ERROR("no rootfs_data was found\n");
		return -1;
	}

	mp = find_mount_point(mtd, "jffs2");
	if (mp) {
		LOG("%s is mounted as %s, only erasing files\n", mtd, mp);
		foreachdir(mp, handle_rmdir);
		mount(mp, "/", NULL, MS_REMOUNT, 0);
	} else {
		LOG("%s is not mounted, erasing it\n", mtd);
		find_mtd_char("rootfs_data", mtd, sizeof(mtd));
		mtd_erase(mtd);
	}

	return 0;
}

static int main_jffs2mark(int argc, char **argv)
{
	FILE *fp;
	__u32 deadc0de = __cpu_to_be32(0xdeadc0de);
	char mtd[32];
	size_t sz;

	if (ask_user(argc, argv))
		return -1;

	if (find_mtd_block("rootfs_data", mtd, sizeof(mtd))) {
		ERROR("no rootfs_data was found\n");
		return -1;
	}

	fp = fopen(mtd, "w");
	LOG("%s - marking with deadc0de\n", mtd);
	if (!fp) {
		ERROR("opening %s failed\n", mtd);
		return -1;
	}

	sz = fwrite(&deadc0de, sizeof(deadc0de), 1, fp);
	fclose(fp);

	if (sz != 1) {
		ERROR("writing %s failed: %s\n", mtd, strerror(errno));
		return -1;
	}

	return 0;
 }
static int main_switch2jffs(int argc, char **argv)
{
	char mtd[32];
	char *mp;
	int ret = -1;

	if (find_overlay_mount("overlayfs:/tmp/root"))
		return -1;

	if (check_fs_exists("overlay")) {
		ERROR("overlayfs not found\n");
		return ret;
	}

	find_mtd_block("rootfs_data", mtd, sizeof(mtd));
	mp = find_mount_point(mtd, NULL);
	if (mp) {
		LOG("rootfs_data:%s is already mounted as %s\n", mtd, mp);
		return -1;
	}

	if (find_mtd_char("rootfs_data", mtd, sizeof(mtd))) {
		ERROR("no rootfs_data was found\n");
		return ret;
	}

	switch (jffs2_ready(mtd)) {
	case FS_NONE:
		ERROR("no jffs2 marker found\n");
		/* fall through */

	case FS_DEADCODE:
		ret = switch2jffs();
		if (!ret) {
			DEBUG(1, "doing fo cleanup\n");
			umount2("/tmp/root", MNT_DETACH);
			foreachdir("/overlay/", handle_whiteout);
		}
		break;

	case FS_JFFS2:
		ret = mtd_mount_jffs2();
		if (ret)
			break;
		if (mount_move("/tmp", "", "/overlay") || fopivot("/overlay", "/rom")) {
			ERROR("switching to jffs2 failed\n");
			ret = -1;
		}
		break;
	}

	return ret;
}

static int extroot(const char *prefix)
{
	char block_path[32];
	char kmod_loader[64];
	struct stat s;
	pid_t pid;

	sprintf(block_path, "%s/sbin/block", prefix);

	if (stat(block_path, &s))
	{
		sprintf(block_path, "/sbin/block");

		if (stat(block_path, &s))
			return -1;
	}

	sprintf(kmod_loader, "/sbin/kmodloader %s/etc/modules-boot.d/ %s", prefix, prefix);
	system(kmod_loader);

	LOG("starting block executable %s\n", block_path);

	pid = fork();
	if (!pid) {
		mkdir("/tmp/extroot", 0755);
		execl(block_path, block_path, "extroot", NULL);
		exit(-1);
	} else if (pid > 0) {
		int status;

		waitpid(pid, &status, 0);
		if (!WEXITSTATUS(status)) {
			if (find_mount("/tmp/extroot/mnt")) {
				mount("/dev/root", "/", NULL, MS_NOATIME | MS_REMOUNT | MS_RDONLY, 0);

				mkdir("/tmp/extroot/mnt/proc", 0755);
				mkdir("/tmp/extroot/mnt/dev", 0755);
				mkdir("/tmp/extroot/mnt/sys", 0755);
				mkdir("/tmp/extroot/mnt/tmp", 0755);
				mkdir("/tmp/extroot/mnt/rom", 0755);

				if (mount_move("/tmp/extroot", "", "/mnt")) {
					ERROR("moving pivotroot failed - continue normal boot\n");
					umount("/tmp/extroot/mnt");
				} else if (pivot("/mnt", "/rom")) {
					ERROR("switching to pivotroot failed - continue normal boot\n");
					umount("/mnt");
				} else {
					umount("/tmp/overlay");
					rmdir("/tmp/overlay");
					rmdir("/tmp/extroot/mnt");
					rmdir("/tmp/extroot");
					return 0;
				}
			} else if (find_mount("/tmp/extroot/overlay")) {
				if (mount_move("/tmp/extroot", "", "/overlay")) {
					ERROR("moving extroot failed - continue normal boot\n");
					umount("/tmp/extroot/overlay");
				} else if (fopivot("/overlay", "/rom")) {
					ERROR("switching to extroot failed - continue normal boot\n");
					umount("/overlay");
				} else {
					umount("/tmp/overlay");
					rmdir("/tmp/overlay");
					rmdir("/tmp/extroot/overlay");
					rmdir("/tmp/extroot");
					return 0;
				}
			} else {
				ERROR("block executable did not set up an overlay\n");
			}
		} else {
			ERROR("block executable failed with code %d\n", WEXITSTATUS(status));
		}
	}
	return -1;
}

int main(int argc, char **argv)
{
	char *mp;
	char mtd[32];

	argv0 = basename(*argv);

	if (!strcmp(basename(*argv), "jffs2mark"))
		return main_jffs2mark(argc, argv);

	if (!strcmp(basename(*argv), "jffs2reset"))
		return main_jffs2reset(argc, argv);

	if (!strcmp(basename(*argv), "switch2jffs"))
		return main_switch2jffs(argc, argv);

	if (!getenv("PREINIT"))
		return -1;

	if (!find_mtd_block("rootfs_patches", mtd, sizeof(mtd))) {
		ramoverlay();
	} else if (find_mtd_char("rootfs_data", mtd, sizeof(mtd))) {
		if (!find_mtd_char("rootfs", mtd, sizeof(mtd)))
			mtd_unlock(mtd);
		LOG("mounting /dev/root\n");
		mount("/dev/root", "/", NULL, MS_NOATIME | MS_REMOUNT, 0);
	} else {
		if (!extroot("")) {
			fprintf(stderr, "mount_root: switched to extroot\n");
			return 0;
		}

		switch (jffs2_ready(mtd)) {
		case FS_NONE:
		case FS_DEADCODE:
			return ramoverlay();

		case FS_JFFS2:
			find_mtd_block("rootfs_data", mtd, sizeof(mtd));
			mp = find_mount_point(mtd, NULL);
			if (mp) {
				LOG("rootfs_data:%s is already mounted as %s\n", mtd, mp);
				return -1;
			}

			mtd_mount_jffs2();

			if (!extroot("/tmp/overlay")) {
				fprintf(stderr, "mount_root: switched to extroot\n");
				return 0;
			}

			DEBUG(1, "switching to jffs2\n");
			if (mount_move("/tmp", "", "/overlay") || fopivot("/overlay", "/rom")) {
				ERROR("switching to jffs2 failed - fallback to ramoverlay\n");
				return ramoverlay();
			}
		}
	}

	return 0;
}