From b5367474cba180d7b2555e4e30421a7340f63264 Mon Sep 17 00:00:00 2001
From: blogic <blogic@3c298f89-4303-0410-b956-a3cf2f4a3e73>
Date: Thu, 25 Apr 2013 19:02:32 +0000
Subject: mount_root: prepare base-files

Signed-off-by: John Crispin <blogic@openwrt.org>

git-svn-id: svn://svn.openwrt.org/openwrt/trunk@36429 3c298f89-4303-0410-b956-a3cf2f4a3e73
---
 package/base-files/files.old/etc/init.d/done       | 20 ++++++++++
 .../files.old/lib/firstboot/05_firstboot_skip      | 10 +++++
 .../files.old/lib/firstboot/10_determine_parts     | 46 ++++++++++++++++++++++
 .../files.old/lib/firstboot/10_no_fo_clear_overlay | 17 ++++++++
 .../files.old/lib/firstboot/10_reset_has_mini_fo   | 12 ++++++
 .../files.old/lib/firstboot/20_has_mini_fo         | 13 ++++++
 .../files.old/lib/firstboot/20_no_fo_mount_jffs    | 15 +++++++
 .../files.old/lib/firstboot/20_reset_clear_jffs    | 14 +++++++
 .../files.old/lib/firstboot/30_is_rootfs_mounted   | 10 +++++
 .../files.old/lib/firstboot/30_no_fo_pivot         | 11 ++++++
 .../files.old/lib/firstboot/30_reset_copy_rom      | 13 ++++++
 .../files.old/lib/firstboot/40_copy_ramoverlay     | 15 +++++++
 .../lib/firstboot/40_no_fo_copy_ramoverlay         | 14 +++++++
 .../base-files/files.old/lib/firstboot/50_pivot    | 17 ++++++++
 .../files.old/lib/firstboot/99_10_no_fo_cleanup    | 13 ++++++
 .../files.old/lib/firstboot/99_10_with_fo_cleanup  |  2 +-
 .../base-files/files.old/lib/preinit/05_mount_skip | 13 ++++++
 .../files.old/lib/preinit/10_check_for_mtd         | 20 ++++++++++
 .../files.old/lib/preinit/20_check_jffs2_ready     | 19 +++++++++
 .../files.old/lib/preinit/40_mount_jffs2           | 26 ++++++++++++
 .../files.old/lib/preinit/41_merge_overlay_hooks   | 24 +++++++++++
 .../files.old/lib/preinit/70_pivot_jffs2_root      | 14 +++++++
 .../files.old/lib/preinit/90_mount_no_jffs2        | 12 ++++++
 .../files.old/lib/preinit/90_restore_config        | 17 ++++++++
 .../files.old/lib/preinit/99_10_mount_no_mtd       | 12 ++++++
 package/base-files/files.old/sbin/firstboot        | 44 +++++++++++++++++++++
 package/base-files/files.old/sbin/mount_root       | 26 ++++++++++++
 package/base-files/files/etc/init.d/done           |  2 +-
 package/base-files/files/etc/rc.button/reset       |  2 +-
 .../files/lib/firstboot/05_firstboot_skip          | 10 -----
 .../files/lib/firstboot/10_determine_parts         | 46 ----------------------
 .../files/lib/firstboot/10_no_fo_clear_overlay     | 17 --------
 .../files/lib/firstboot/10_reset_has_mini_fo       | 12 ------
 .../base-files/files/lib/firstboot/20_has_mini_fo  | 13 ------
 .../files/lib/firstboot/20_no_fo_mount_jffs        | 15 -------
 .../files/lib/firstboot/20_reset_clear_jffs        | 14 -------
 .../files/lib/firstboot/30_is_rootfs_mounted       | 10 -----
 .../base-files/files/lib/firstboot/30_no_fo_pivot  | 11 ------
 .../files/lib/firstboot/30_reset_copy_rom          | 13 ------
 .../files/lib/firstboot/40_copy_ramoverlay         | 15 -------
 .../files/lib/firstboot/40_no_fo_copy_ramoverlay   | 14 -------
 package/base-files/files/lib/firstboot/50_pivot    | 17 --------
 .../files/lib/firstboot/99_10_no_fo_cleanup        | 13 ------
 .../files/lib/firstboot/99_10_with_fo_cleanup      | 25 ------------
 package/base-files/files/lib/preinit/05_mount_skip | 13 ------
 .../base-files/files/lib/preinit/10_check_for_mtd  | 20 ----------
 .../files/lib/preinit/20_check_jffs2_ready         | 19 ---------
 .../base-files/files/lib/preinit/40_mount_jffs2    | 26 ------------
 .../files/lib/preinit/41_merge_overlay_hooks       | 24 -----------
 .../files/lib/preinit/70_pivot_jffs2_root          | 14 -------
 package/base-files/files/lib/preinit/80_mount_root | 11 +++++-
 .../base-files/files/lib/preinit/90_mount_no_jffs2 | 12 ------
 .../base-files/files/lib/preinit/90_restore_config | 17 --------
 .../files/lib/preinit/99_10_mount_no_mtd           | 12 ------
 package/base-files/files/sbin/firstboot            | 43 +-------------------
 package/base-files/files/sbin/mount_root           | 26 ------------
 package/base-files/files/sbin/sysupgrade           |  2 +
 57 files changed, 483 insertions(+), 474 deletions(-)
 create mode 100755 package/base-files/files.old/etc/init.d/done
 create mode 100644 package/base-files/files.old/lib/firstboot/05_firstboot_skip
 create mode 100644 package/base-files/files.old/lib/firstboot/10_determine_parts
 create mode 100644 package/base-files/files.old/lib/firstboot/10_no_fo_clear_overlay
 create mode 100644 package/base-files/files.old/lib/firstboot/10_reset_has_mini_fo
 create mode 100644 package/base-files/files.old/lib/firstboot/20_has_mini_fo
 create mode 100644 package/base-files/files.old/lib/firstboot/20_no_fo_mount_jffs
 create mode 100644 package/base-files/files.old/lib/firstboot/20_reset_clear_jffs
 create mode 100644 package/base-files/files.old/lib/firstboot/30_is_rootfs_mounted
 create mode 100644 package/base-files/files.old/lib/firstboot/30_no_fo_pivot
 create mode 100644 package/base-files/files.old/lib/firstboot/30_reset_copy_rom
 create mode 100644 package/base-files/files.old/lib/firstboot/40_copy_ramoverlay
 create mode 100644 package/base-files/files.old/lib/firstboot/40_no_fo_copy_ramoverlay
 create mode 100644 package/base-files/files.old/lib/firstboot/50_pivot
 create mode 100644 package/base-files/files.old/lib/firstboot/99_10_no_fo_cleanup
 create mode 100644 package/base-files/files.old/lib/preinit/05_mount_skip
 create mode 100644 package/base-files/files.old/lib/preinit/10_check_for_mtd
 create mode 100644 package/base-files/files.old/lib/preinit/20_check_jffs2_ready
 create mode 100644 package/base-files/files.old/lib/preinit/40_mount_jffs2
 create mode 100644 package/base-files/files.old/lib/preinit/41_merge_overlay_hooks
 create mode 100644 package/base-files/files.old/lib/preinit/70_pivot_jffs2_root
 create mode 100644 package/base-files/files.old/lib/preinit/90_mount_no_jffs2
 create mode 100644 package/base-files/files.old/lib/preinit/90_restore_config
 create mode 100644 package/base-files/files.old/lib/preinit/99_10_mount_no_mtd
 create mode 100755 package/base-files/files.old/sbin/firstboot
 create mode 100755 package/base-files/files.old/sbin/mount_root
 delete mode 100644 package/base-files/files/lib/firstboot/05_firstboot_skip
 delete mode 100644 package/base-files/files/lib/firstboot/10_determine_parts
 delete mode 100644 package/base-files/files/lib/firstboot/10_no_fo_clear_overlay
 delete mode 100644 package/base-files/files/lib/firstboot/10_reset_has_mini_fo
 delete mode 100644 package/base-files/files/lib/firstboot/20_has_mini_fo
 delete mode 100644 package/base-files/files/lib/firstboot/20_no_fo_mount_jffs
 delete mode 100644 package/base-files/files/lib/firstboot/20_reset_clear_jffs
 delete mode 100644 package/base-files/files/lib/firstboot/30_is_rootfs_mounted
 delete mode 100644 package/base-files/files/lib/firstboot/30_no_fo_pivot
 delete mode 100644 package/base-files/files/lib/firstboot/30_reset_copy_rom
 delete mode 100644 package/base-files/files/lib/firstboot/40_copy_ramoverlay
 delete mode 100644 package/base-files/files/lib/firstboot/40_no_fo_copy_ramoverlay
 delete mode 100644 package/base-files/files/lib/firstboot/50_pivot
 delete mode 100644 package/base-files/files/lib/firstboot/99_10_no_fo_cleanup
 delete mode 100644 package/base-files/files/lib/firstboot/99_10_with_fo_cleanup
 delete mode 100644 package/base-files/files/lib/preinit/05_mount_skip
 delete mode 100644 package/base-files/files/lib/preinit/10_check_for_mtd
 delete mode 100644 package/base-files/files/lib/preinit/20_check_jffs2_ready
 delete mode 100644 package/base-files/files/lib/preinit/40_mount_jffs2
 delete mode 100644 package/base-files/files/lib/preinit/41_merge_overlay_hooks
 delete mode 100644 package/base-files/files/lib/preinit/70_pivot_jffs2_root
 delete mode 100644 package/base-files/files/lib/preinit/90_mount_no_jffs2
 delete mode 100644 package/base-files/files/lib/preinit/90_restore_config
 delete mode 100644 package/base-files/files/lib/preinit/99_10_mount_no_mtd
 delete mode 100755 package/base-files/files/sbin/mount_root

(limited to 'package/base-files')

diff --git a/package/base-files/files.old/etc/init.d/done b/package/base-files/files.old/etc/init.d/done
new file mode 100755
index 000000000..827866912
--- /dev/null
+++ b/package/base-files/files.old/etc/init.d/done
@@ -0,0 +1,20 @@
+#!/bin/sh /etc/rc.common
+# Copyright (C) 2006 OpenWrt.org
+
+START=95
+boot() {
+	[ -d /tmp/root ] && {
+		lock /tmp/.switch2jffs
+		firstboot switch2jffs
+		lock -u /tmp/.switch2jffs
+	}
+
+	# process user commands
+	[ -f /etc/rc.local ] && {
+		sh /etc/rc.local
+	}
+
+	# set leds to normal state
+	. /etc/diag.sh
+	set_state done
+}
diff --git a/package/base-files/files.old/lib/firstboot/05_firstboot_skip b/package/base-files/files.old/lib/firstboot/05_firstboot_skip
new file mode 100644
index 000000000..5f44df621
--- /dev/null
+++ b/package/base-files/files.old/lib/firstboot/05_firstboot_skip
@@ -0,0 +1,10 @@
+#!/bin/sh
+
+check_skip() {
+    if [ "$firstboot_skip_next" = "true" ]; then
+	return 0
+    else 
+	return 1
+    fi
+}
+
diff --git a/package/base-files/files.old/lib/firstboot/10_determine_parts b/package/base-files/files.old/lib/firstboot/10_determine_parts
new file mode 100644
index 000000000..3f56e32a8
--- /dev/null
+++ b/package/base-files/files.old/lib/firstboot/10_determine_parts
@@ -0,0 +1,46 @@
+#!/bin/sh
+
+set_mtd_part() {
+    partname="rootfs_data"
+    mtdpart="$(find_mtd_part $partname)"
+}
+
+set_rom_part() {
+    rom=$(awk '/squashfs/ {print $2}' /proc/mounts)
+}
+
+set_jffs_part() {
+    jffs=$(awk '/jffs2/ {print $2}' /proc/mounts)
+}
+
+determine_mtd_part() {
+    set_mtd_part
+    if [ -z "$mtdpart" ]; then
+	echo "MTD partition not found."
+	exit 1
+    fi
+}
+
+determine_rom_part() {
+    check_skip || {
+	set_rom_part
+	if [ -z "$rom" ]; then
+	    echo "You do not have a squashfs partition; aborting"
+	    echo "(firstboot cannot be run on jffs2 based firmwares)"
+	    exit 1
+	fi
+    }
+}
+
+determine_jffs2_part() {
+    check_skip || {
+	set_jffs_part
+    }
+}
+
+boot_hook_add switch2jffs determine_mtd_part
+boot_hook_add jffs2reset determine_mtd_part
+boot_hook_add switch2jffs determine_rom_part
+boot_hook_add jffs2reset determine_rom_part
+boot_hook_add switch2jffs determine_jffs2_part
+boot_hook_add jffs2reset determine_jffs2_part
diff --git a/package/base-files/files.old/lib/firstboot/10_no_fo_clear_overlay b/package/base-files/files.old/lib/firstboot/10_no_fo_clear_overlay
new file mode 100644
index 000000000..8a7d9b066
--- /dev/null
+++ b/package/base-files/files.old/lib/firstboot/10_no_fo_clear_overlay
@@ -0,0 +1,17 @@
+#!/bin/sh
+
+# Copyright (C) 2006-2010 OpenWrt.org
+# Copyright (C) 2010 Vertical Communications
+
+no_fo_clear_overlay() {
+    # switch back to squashfs temporarily
+    pivot /rom /mnt
+
+    # get rid of the old overlay
+    umount -l /mnt
+
+    # another umount to get rid of the bind from /tmp/root
+    umount -l /mnt
+}
+
+boot_hook_add no_fo no_fo_clear_overlay
diff --git a/package/base-files/files.old/lib/firstboot/10_reset_has_mini_fo b/package/base-files/files.old/lib/firstboot/10_reset_has_mini_fo
new file mode 100644
index 000000000..4e285840a
--- /dev/null
+++ b/package/base-files/files.old/lib/firstboot/10_reset_has_mini_fo
@@ -0,0 +1,12 @@
+#!/bin/sh
+
+# Copyright (C) 2006-2010 OpenWrt.org
+# Copyright (C) 2010 Vertical Communications
+
+reset_check_for_overlay() {
+    if grep -qE '(mini_fo|overlay)' /proc/filesystems; then
+	reset_has_fo=true
+    fi
+}
+
+boot_hook_add jffs2reset reset_check_for_overlay
diff --git a/package/base-files/files.old/lib/firstboot/20_has_mini_fo b/package/base-files/files.old/lib/firstboot/20_has_mini_fo
new file mode 100644
index 000000000..8ca6a4fe5
--- /dev/null
+++ b/package/base-files/files.old/lib/firstboot/20_has_mini_fo
@@ -0,0 +1,13 @@
+#!/bin/sh
+
+# Copyright (C) 2006-2010 OpenWrt.org
+# Copyright (C) 2010 Vertical Communications
+
+check_for_overlay() {
+    if ! grep -qE '(mini_fo|overlay)' /proc/filesystems; then
+	boot_run_hook no_fo
+	exit 1
+    fi
+}
+
+boot_hook_add switch2jffs check_for_overlay
diff --git a/package/base-files/files.old/lib/firstboot/20_no_fo_mount_jffs b/package/base-files/files.old/lib/firstboot/20_no_fo_mount_jffs
new file mode 100644
index 000000000..c03714fa6
--- /dev/null
+++ b/package/base-files/files.old/lib/firstboot/20_no_fo_mount_jffs
@@ -0,0 +1,15 @@
+#!/bin/sh
+
+# Copyright (C) 2006-2010 OpenWrt.org
+# Copyright (C) 2010 Vertical Communications
+
+no_fo_mount_jffs() {
+    # initialize jffs2
+    mount -o noatime "$mtdpart" /overlay -t jffs2 || exit
+
+    # workaround to ensure that union can attach properly
+    sync
+    ls /overlay >/dev/null
+}
+
+boot_hook_add no_fo no_fo_mount_jffs
diff --git a/package/base-files/files.old/lib/firstboot/20_reset_clear_jffs b/package/base-files/files.old/lib/firstboot/20_reset_clear_jffs
new file mode 100644
index 000000000..a3cd24fe9
--- /dev/null
+++ b/package/base-files/files.old/lib/firstboot/20_reset_clear_jffs
@@ -0,0 +1,14 @@
+#!/bin/sh
+
+# Copyright (C) 2006-2010 OpenWrt.org
+# Copyright (C) 2010 Vertical Communications
+
+reset_clear_jffs() {
+    [ "$reset_has_fo" = "true" ] && {
+	rm -rf $jffs/* 2>&-
+	mount -o remount $jffs / 2>&-
+	exit 0
+    } || reset_has_fo=false
+}
+
+boot_hook_add jffs2reset reset_clear_jffs
diff --git a/package/base-files/files.old/lib/firstboot/30_is_rootfs_mounted b/package/base-files/files.old/lib/firstboot/30_is_rootfs_mounted
new file mode 100644
index 000000000..7233fd903
--- /dev/null
+++ b/package/base-files/files.old/lib/firstboot/30_is_rootfs_mounted
@@ -0,0 +1,10 @@
+#!/bin/sh
+
+# Copyright (C) 2006-2010 OpenWrt.org
+# Copyright (C) 2010 Vertical Communications
+
+skip_if_rootfs_mounted() {
+    mount -o noatime -t jffs2 "$mtdpart" /rom/overlay || exit
+}
+
+boot_hook_add switch2jffs skip_if_rootfs_mounted
diff --git a/package/base-files/files.old/lib/firstboot/30_no_fo_pivot b/package/base-files/files.old/lib/firstboot/30_no_fo_pivot
new file mode 100644
index 000000000..b5c2601ee
--- /dev/null
+++ b/package/base-files/files.old/lib/firstboot/30_no_fo_pivot
@@ -0,0 +1,11 @@
+#!/bin/sh
+
+# Copyright (C) 2006-2010 OpenWrt.org
+# Copyright (C) 2010 Vertical Communications
+
+no_fo_pivot() {
+    # switch to the new (empty) jffs2
+    fopivot /overlay /rom 1
+}
+
+boot_hook_add no_fo no_fo_pivot
diff --git a/package/base-files/files.old/lib/firstboot/30_reset_copy_rom b/package/base-files/files.old/lib/firstboot/30_reset_copy_rom
new file mode 100644
index 000000000..d91c68947
--- /dev/null
+++ b/package/base-files/files.old/lib/firstboot/30_reset_copy_rom
@@ -0,0 +1,13 @@
+#!/bin/sh
+
+# Copyright (C) 2006-2010 OpenWrt.org
+# Copyright (C) 2010 Vertical Communications
+
+reset_copy_rom() {
+    [ "$reset_has_fo" != "true" ] && {
+	dupe $jffs $rom
+	exit 0
+    } 
+}
+
+boot_hook_add jffs2reset reset_copy_rom
diff --git a/package/base-files/files.old/lib/firstboot/40_copy_ramoverlay b/package/base-files/files.old/lib/firstboot/40_copy_ramoverlay
new file mode 100644
index 000000000..39c2edacb
--- /dev/null
+++ b/package/base-files/files.old/lib/firstboot/40_copy_ramoverlay
@@ -0,0 +1,15 @@
+#!/bin/sh
+
+# Copyright (C) 2006-2010 OpenWrt.org
+# Copyright (C) 2010 Vertical Communications
+
+copy_ramoverlay() {
+    # try to avoid fs changing while copying
+    mount -o remount,ro none / 2>&-
+    # copy ramoverlay to jffs2
+    echo -n "copying files ... "
+    cp -a /tmp/root/* /rom/overlay 2>&-
+    echo "done"
+}
+
+boot_hook_add switch2jffs copy_ramoverlay
diff --git a/package/base-files/files.old/lib/firstboot/40_no_fo_copy_ramoverlay b/package/base-files/files.old/lib/firstboot/40_no_fo_copy_ramoverlay
new file mode 100644
index 000000000..ced7c1b66
--- /dev/null
+++ b/package/base-files/files.old/lib/firstboot/40_no_fo_copy_ramoverlay
@@ -0,0 +1,14 @@
+#!/bin/sh
+
+# Copyright (C) 2006-2010 OpenWrt.org
+# Copyright (C) 2010 Vertical Communications
+
+no_fo_copy_ramoverlay() {
+    # copy ramoverlay to jffs2, must be done after switching
+    # to the new rootfs to avoid creating opaque directories
+    echo -n "copying files ... "
+    cp -a /tmp/root/* / >/dev/null 2>&1
+    sync
+}
+
+boot_hook_add no_fo no_fo_ramoverlay
diff --git a/package/base-files/files.old/lib/firstboot/50_pivot b/package/base-files/files.old/lib/firstboot/50_pivot
new file mode 100644
index 000000000..53801d7a8
--- /dev/null
+++ b/package/base-files/files.old/lib/firstboot/50_pivot
@@ -0,0 +1,17 @@
+#!/bin/sh
+
+# Copyright (C) 2006-2010 OpenWrt.org
+# Copyright (C) 2010 Vertical Communications
+
+with_fo_pivot() {
+    # switch back to squashfs (temporarily)
+    # and park the ramdisk ontop of /tmp/root
+    pivot /rom /mnt
+    mount -o move /mnt /tmp/root
+
+    # /overlay is the overlay
+    # /rom is the readonly
+    fopivot /overlay /rom
+}
+
+boot_hook_add switch2jffs with_fo_pivot
diff --git a/package/base-files/files.old/lib/firstboot/99_10_no_fo_cleanup b/package/base-files/files.old/lib/firstboot/99_10_no_fo_cleanup
new file mode 100644
index 000000000..6dedcb1e8
--- /dev/null
+++ b/package/base-files/files.old/lib/firstboot/99_10_no_fo_cleanup
@@ -0,0 +1,13 @@
+#!/bin/sh
+
+# Copyright (C) 2006-2010 OpenWrt.org
+# Copyright (C) 2010 Vertical Communications
+
+no_fo_cleanup() {
+    echo "done"
+    umount -l /overlay
+    umount -l /tmp/root
+    exit 0
+}
+
+boot_hook_add no_fo no_fo_cleanup
diff --git a/package/base-files/files.old/lib/firstboot/99_10_with_fo_cleanup b/package/base-files/files.old/lib/firstboot/99_10_with_fo_cleanup
index 0181b39d4..560169901 100644
--- a/package/base-files/files.old/lib/firstboot/99_10_with_fo_cleanup
+++ b/package/base-files/files.old/lib/firstboot/99_10_with_fo_cleanup
@@ -6,7 +6,7 @@
 with_fo_cleanup() {
     # try to get rid of /tmp/root
     # this will almost always fail
-    umount /tmp/root 2>&-
+    umount -l /tmp/root 2>&-
 	grep -q overlay /proc/filesystems && {
 		cd /
 		(
diff --git a/package/base-files/files.old/lib/preinit/05_mount_skip b/package/base-files/files.old/lib/preinit/05_mount_skip
new file mode 100644
index 000000000..c2b7ee79d
--- /dev/null
+++ b/package/base-files/files.old/lib/preinit/05_mount_skip
@@ -0,0 +1,13 @@
+#!/bin/sh
+
+# Copyright (C) 2006 OpenWrt.org
+# Copyright (C) 2010 Vertical Communications
+
+check_skip() {
+    if [ "$pi_mount_skip_next" = "true" ]; then
+	return 0
+    else 
+	return 1
+    fi
+}
+
diff --git a/package/base-files/files.old/lib/preinit/10_check_for_mtd b/package/base-files/files.old/lib/preinit/10_check_for_mtd
new file mode 100644
index 000000000..6a7bd307f
--- /dev/null
+++ b/package/base-files/files.old/lib/preinit/10_check_for_mtd
@@ -0,0 +1,20 @@
+#!/bin/sh
+
+# Copyright (C) 2006-2010 OpenWrt.org
+# Copyright (C) 2010 Vertical Communications
+
+mount_no_mtd() {
+    mtd unlock rootfs
+    mount -o noatime,remount,rw /dev/root /
+}
+
+check_for_mtd() {
+    check_skip || {
+	grep -qs rootfs_data /proc/mtd || {
+	    mount_no_mtd && pi_mount_skip_next=true
+	}
+    }
+}
+
+boot_hook_add preinit_mount_root check_for_mtd
+
diff --git a/package/base-files/files.old/lib/preinit/20_check_jffs2_ready b/package/base-files/files.old/lib/preinit/20_check_jffs2_ready
new file mode 100644
index 000000000..3c5cf67e8
--- /dev/null
+++ b/package/base-files/files.old/lib/preinit/20_check_jffs2_ready
@@ -0,0 +1,19 @@
+#!/bin/sh
+# Copyright (C) 2006-2010 OpenWrt.org
+# Copyright (C) 2010 Vertical Communications
+
+mount_no_jffs2() {
+    echo "jffs2 not ready yet; using ramdisk"
+    ramoverlay
+}
+
+check_for_jffs2() {
+    check_skip || {
+	jffs2_ready || {
+	    mount_no_jffs2 && pi_mount_skip_next=true
+	}
+    }
+}
+
+boot_hook_add preinit_mount_root check_for_jffs2
+
diff --git a/package/base-files/files.old/lib/preinit/40_mount_jffs2 b/package/base-files/files.old/lib/preinit/40_mount_jffs2
new file mode 100644
index 000000000..ded785ed5
--- /dev/null
+++ b/package/base-files/files.old/lib/preinit/40_mount_jffs2
@@ -0,0 +1,26 @@
+#!/bin/sh
+# Copyright (C) 2006-2010 OpenWrt.org
+# Copyright (C) 2010 Vertical Communications
+
+find_mount_jffs2() {
+    mkdir -p /tmp/overlay
+    mount -o noatime -t jffs2 "$(find_mtd_part rootfs_data)" /tmp/overlay
+    mtd -qq unlock rootfs_data
+}
+
+jffs2_not_mounted() {
+    if [ "$pi_jffs2_mount_success" != "true" ]; then
+	return 0
+    else
+	return 1
+    fi
+}
+
+do_mount_jffs2() {
+    check_skip || {
+	find_mount_jffs2 && pi_jffs2_mount_success=true
+    }
+}
+
+boot_hook_add preinit_mount_root do_mount_jffs2
+
diff --git a/package/base-files/files.old/lib/preinit/41_merge_overlay_hooks b/package/base-files/files.old/lib/preinit/41_merge_overlay_hooks
new file mode 100644
index 000000000..4ca6877be
--- /dev/null
+++ b/package/base-files/files.old/lib/preinit/41_merge_overlay_hooks
@@ -0,0 +1,24 @@
+#!/bin/sh
+# Copyright (C) 2010 OpenWrt.org
+
+merge_overlay_hooks() {
+	jffs2_not_mounted || [ ! -d /tmp/overlay/lib/preinit ] || {
+		echo "- merge overlay components -"
+
+		mkdir -p /tmp/preinit-hook-merge
+		ln -sf /lib/preinit/* /tmp/overlay/lib/preinit/[0-9][0-9]_* /tmp/preinit-hook-merge/
+
+		boot_hook_splice_start
+
+		local pipart
+		for pipart in /tmp/preinit-hook-merge/*; do
+			. $pipart
+		done
+
+		boot_hook_splice_finish
+
+		rm -rf /tmp/preinit-hook-merge
+	}
+}
+
+boot_hook_add preinit_mount_root merge_overlay_hooks
diff --git a/package/base-files/files.old/lib/preinit/70_pivot_jffs2_root b/package/base-files/files.old/lib/preinit/70_pivot_jffs2_root
new file mode 100644
index 000000000..b76f111ca
--- /dev/null
+++ b/package/base-files/files.old/lib/preinit/70_pivot_jffs2_root
@@ -0,0 +1,14 @@
+#!/bin/sh
+# Copyright (C) 2006-2010 OpenWrt.org
+# Copyright (C) 2010 Vertical Communications
+
+rootfs_pivot() {
+    check_skip || jffs2_not_mounted || {
+	echo "switching to jffs2"
+	mount -o noatime,move /tmp/overlay /overlay 2>&-
+	fopivot /overlay /rom && pi_mount_skip_next=true
+    }
+}
+
+boot_hook_add preinit_mount_root rootfs_pivot
+
diff --git a/package/base-files/files.old/lib/preinit/90_mount_no_jffs2 b/package/base-files/files.old/lib/preinit/90_mount_no_jffs2
new file mode 100644
index 000000000..d8ad4ae11
--- /dev/null
+++ b/package/base-files/files.old/lib/preinit/90_mount_no_jffs2
@@ -0,0 +1,12 @@
+#!/bin/sh
+# Copyright (C) 2006-2010 OpenWrt.org
+# Copyright (C) 2010 Vertical Communications
+
+do_mount_no_jffs2() {
+    check_skip || {
+	mount_no_jffs2 && pi_mount_skip_next=true
+    }
+}
+
+boot_hook_add preinit_mount_root do_mount_no_jffs2
+
diff --git a/package/base-files/files.old/lib/preinit/90_restore_config b/package/base-files/files.old/lib/preinit/90_restore_config
new file mode 100644
index 000000000..210bf6184
--- /dev/null
+++ b/package/base-files/files.old/lib/preinit/90_restore_config
@@ -0,0 +1,17 @@
+#!/bin/sh
+# Copyright (C) 2006 OpenWrt.org
+# Copyright (C) 2010 Vertical Communications
+
+restore_config() {
+    [ -f /sysupgrade.tgz ] && {
+	echo "- config restore -"
+	cd /
+	mv sysupgrade.tgz /tmp
+	tar xzf /tmp/sysupgrade.tgz
+	rm -f /tmp/sysupgrade.tgz
+	sync
+    }
+}
+
+boot_hook_add preinit_main restore_config
+
diff --git a/package/base-files/files.old/lib/preinit/99_10_mount_no_mtd b/package/base-files/files.old/lib/preinit/99_10_mount_no_mtd
new file mode 100644
index 000000000..c4f38e415
--- /dev/null
+++ b/package/base-files/files.old/lib/preinit/99_10_mount_no_mtd
@@ -0,0 +1,12 @@
+#!/bin/sh
+# Copyright (C) 2006-2010 OpenWrt.org
+# Copyright (C) 2010 Vertical Communications
+
+do_mount_no_mtd() {
+    check_skip || {
+	mount_no_mtd
+    }
+}
+
+boot_hook_add preinit_mount_root do_mount_no_mtd
+
diff --git a/package/base-files/files.old/sbin/firstboot b/package/base-files/files.old/sbin/firstboot
new file mode 100755
index 000000000..06b64c313
--- /dev/null
+++ b/package/base-files/files.old/sbin/firstboot
@@ -0,0 +1,44 @@
+#!/bin/sh
+
+switch2jffs_hook=
+jffs2reset_hook=
+no_fo_hook=
+
+. /lib/functions.sh
+
+firstboot_skip_next=false
+
+for fb_source_file in /lib/firstboot/*; do
+    . $fb_source_file
+done
+
+set_mtd_part
+set_rom_part
+set_jffs_part
+
+# invoked as an executable
+if [ "${0##*/}" = "firstboot" ]; then
+    if [ "$1" = "switch2jffs" ]; then
+	boot_run_hook switch2jffs
+    else
+	if [ -t 0 ] && [ "$1" != "-y" ]; then
+		local input
+
+		echo -n "firstboot will erase all settings and remove any installed packages. Are you sure? [N/y]"
+		read input
+		[ "$input" = "y" ] || [ "$input" = "Y" ] || return 0
+	fi
+
+	if [ -n "$jffs" ]; then
+	    reset_has_fo=true
+	    echo "firstboot has already been run"
+	    echo "jffs2 partition is mounted, only resetting files"
+	    boot_run_hook jffs2reset
+	else
+	    mtd erase "$partname"
+	    mount -o noatime "$mtdpart" /overlay -t jffs2
+	    fopivot /overlay /rom 1
+	fi
+    fi
+fi
+
diff --git a/package/base-files/files.old/sbin/mount_root b/package/base-files/files.old/sbin/mount_root
new file mode 100755
index 000000000..1c48ebab1
--- /dev/null
+++ b/package/base-files/files.old/sbin/mount_root
@@ -0,0 +1,26 @@
+#!/bin/sh
+# Copyright (C) 2006-2010 OpenWrt.org
+# Copyright (C) 2010 Vertical Communications
+
+# Initialize hook variables et by pulling in from preinit
+
+preinit_essential_hook=
+preinit_main_hook=
+failsafe_hook=
+initramfs_hook=
+preinit_mount_root=
+
+. /lib/functions.sh
+
+for pi_source_file in /lib/preinit/*; do
+    . $pi_source_file
+done
+
+# Override function that doesn't apply to use of mount_root as a command rather
+# than as part of preinit
+
+preinit_echo() {
+    echo "$@"
+}
+
+boot_run_hook preinit_mount_root
diff --git a/package/base-files/files/etc/init.d/done b/package/base-files/files/etc/init.d/done
index 827866912..8040766dc 100755
--- a/package/base-files/files/etc/init.d/done
+++ b/package/base-files/files/etc/init.d/done
@@ -5,7 +5,7 @@ START=95
 boot() {
 	[ -d /tmp/root ] && {
 		lock /tmp/.switch2jffs
-		firstboot switch2jffs
+		switch2jffs
 		lock -u /tmp/.switch2jffs
 	}
 
diff --git a/package/base-files/files/etc/rc.button/reset b/package/base-files/files/etc/rc.button/reset
index 763994d07..229b503a6 100755
--- a/package/base-files/files/etc/rc.button/reset
+++ b/package/base-files/files/etc/rc.button/reset
@@ -14,5 +14,5 @@ then
 elif [ "$SEEN" -gt 5 ]
 then
 	echo "FACTORY RESET" > /dev/console
-	firstboot && reboot &
+	jffs2reset -y && reboot &
 fi
diff --git a/package/base-files/files/lib/firstboot/05_firstboot_skip b/package/base-files/files/lib/firstboot/05_firstboot_skip
deleted file mode 100644
index 5f44df621..000000000
--- a/package/base-files/files/lib/firstboot/05_firstboot_skip
+++ /dev/null
@@ -1,10 +0,0 @@
-#!/bin/sh
-
-check_skip() {
-    if [ "$firstboot_skip_next" = "true" ]; then
-	return 0
-    else 
-	return 1
-    fi
-}
-
diff --git a/package/base-files/files/lib/firstboot/10_determine_parts b/package/base-files/files/lib/firstboot/10_determine_parts
deleted file mode 100644
index 3f56e32a8..000000000
--- a/package/base-files/files/lib/firstboot/10_determine_parts
+++ /dev/null
@@ -1,46 +0,0 @@
-#!/bin/sh
-
-set_mtd_part() {
-    partname="rootfs_data"
-    mtdpart="$(find_mtd_part $partname)"
-}
-
-set_rom_part() {
-    rom=$(awk '/squashfs/ {print $2}' /proc/mounts)
-}
-
-set_jffs_part() {
-    jffs=$(awk '/jffs2/ {print $2}' /proc/mounts)
-}
-
-determine_mtd_part() {
-    set_mtd_part
-    if [ -z "$mtdpart" ]; then
-	echo "MTD partition not found."
-	exit 1
-    fi
-}
-
-determine_rom_part() {
-    check_skip || {
-	set_rom_part
-	if [ -z "$rom" ]; then
-	    echo "You do not have a squashfs partition; aborting"
-	    echo "(firstboot cannot be run on jffs2 based firmwares)"
-	    exit 1
-	fi
-    }
-}
-
-determine_jffs2_part() {
-    check_skip || {
-	set_jffs_part
-    }
-}
-
-boot_hook_add switch2jffs determine_mtd_part
-boot_hook_add jffs2reset determine_mtd_part
-boot_hook_add switch2jffs determine_rom_part
-boot_hook_add jffs2reset determine_rom_part
-boot_hook_add switch2jffs determine_jffs2_part
-boot_hook_add jffs2reset determine_jffs2_part
diff --git a/package/base-files/files/lib/firstboot/10_no_fo_clear_overlay b/package/base-files/files/lib/firstboot/10_no_fo_clear_overlay
deleted file mode 100644
index 8a7d9b066..000000000
--- a/package/base-files/files/lib/firstboot/10_no_fo_clear_overlay
+++ /dev/null
@@ -1,17 +0,0 @@
-#!/bin/sh
-
-# Copyright (C) 2006-2010 OpenWrt.org
-# Copyright (C) 2010 Vertical Communications
-
-no_fo_clear_overlay() {
-    # switch back to squashfs temporarily
-    pivot /rom /mnt
-
-    # get rid of the old overlay
-    umount -l /mnt
-
-    # another umount to get rid of the bind from /tmp/root
-    umount -l /mnt
-}
-
-boot_hook_add no_fo no_fo_clear_overlay
diff --git a/package/base-files/files/lib/firstboot/10_reset_has_mini_fo b/package/base-files/files/lib/firstboot/10_reset_has_mini_fo
deleted file mode 100644
index 4e285840a..000000000
--- a/package/base-files/files/lib/firstboot/10_reset_has_mini_fo
+++ /dev/null
@@ -1,12 +0,0 @@
-#!/bin/sh
-
-# Copyright (C) 2006-2010 OpenWrt.org
-# Copyright (C) 2010 Vertical Communications
-
-reset_check_for_overlay() {
-    if grep -qE '(mini_fo|overlay)' /proc/filesystems; then
-	reset_has_fo=true
-    fi
-}
-
-boot_hook_add jffs2reset reset_check_for_overlay
diff --git a/package/base-files/files/lib/firstboot/20_has_mini_fo b/package/base-files/files/lib/firstboot/20_has_mini_fo
deleted file mode 100644
index 8ca6a4fe5..000000000
--- a/package/base-files/files/lib/firstboot/20_has_mini_fo
+++ /dev/null
@@ -1,13 +0,0 @@
-#!/bin/sh
-
-# Copyright (C) 2006-2010 OpenWrt.org
-# Copyright (C) 2010 Vertical Communications
-
-check_for_overlay() {
-    if ! grep -qE '(mini_fo|overlay)' /proc/filesystems; then
-	boot_run_hook no_fo
-	exit 1
-    fi
-}
-
-boot_hook_add switch2jffs check_for_overlay
diff --git a/package/base-files/files/lib/firstboot/20_no_fo_mount_jffs b/package/base-files/files/lib/firstboot/20_no_fo_mount_jffs
deleted file mode 100644
index c03714fa6..000000000
--- a/package/base-files/files/lib/firstboot/20_no_fo_mount_jffs
+++ /dev/null
@@ -1,15 +0,0 @@
-#!/bin/sh
-
-# Copyright (C) 2006-2010 OpenWrt.org
-# Copyright (C) 2010 Vertical Communications
-
-no_fo_mount_jffs() {
-    # initialize jffs2
-    mount -o noatime "$mtdpart" /overlay -t jffs2 || exit
-
-    # workaround to ensure that union can attach properly
-    sync
-    ls /overlay >/dev/null
-}
-
-boot_hook_add no_fo no_fo_mount_jffs
diff --git a/package/base-files/files/lib/firstboot/20_reset_clear_jffs b/package/base-files/files/lib/firstboot/20_reset_clear_jffs
deleted file mode 100644
index a3cd24fe9..000000000
--- a/package/base-files/files/lib/firstboot/20_reset_clear_jffs
+++ /dev/null
@@ -1,14 +0,0 @@
-#!/bin/sh
-
-# Copyright (C) 2006-2010 OpenWrt.org
-# Copyright (C) 2010 Vertical Communications
-
-reset_clear_jffs() {
-    [ "$reset_has_fo" = "true" ] && {
-	rm -rf $jffs/* 2>&-
-	mount -o remount $jffs / 2>&-
-	exit 0
-    } || reset_has_fo=false
-}
-
-boot_hook_add jffs2reset reset_clear_jffs
diff --git a/package/base-files/files/lib/firstboot/30_is_rootfs_mounted b/package/base-files/files/lib/firstboot/30_is_rootfs_mounted
deleted file mode 100644
index 7233fd903..000000000
--- a/package/base-files/files/lib/firstboot/30_is_rootfs_mounted
+++ /dev/null
@@ -1,10 +0,0 @@
-#!/bin/sh
-
-# Copyright (C) 2006-2010 OpenWrt.org
-# Copyright (C) 2010 Vertical Communications
-
-skip_if_rootfs_mounted() {
-    mount -o noatime -t jffs2 "$mtdpart" /rom/overlay || exit
-}
-
-boot_hook_add switch2jffs skip_if_rootfs_mounted
diff --git a/package/base-files/files/lib/firstboot/30_no_fo_pivot b/package/base-files/files/lib/firstboot/30_no_fo_pivot
deleted file mode 100644
index b5c2601ee..000000000
--- a/package/base-files/files/lib/firstboot/30_no_fo_pivot
+++ /dev/null
@@ -1,11 +0,0 @@
-#!/bin/sh
-
-# Copyright (C) 2006-2010 OpenWrt.org
-# Copyright (C) 2010 Vertical Communications
-
-no_fo_pivot() {
-    # switch to the new (empty) jffs2
-    fopivot /overlay /rom 1
-}
-
-boot_hook_add no_fo no_fo_pivot
diff --git a/package/base-files/files/lib/firstboot/30_reset_copy_rom b/package/base-files/files/lib/firstboot/30_reset_copy_rom
deleted file mode 100644
index d91c68947..000000000
--- a/package/base-files/files/lib/firstboot/30_reset_copy_rom
+++ /dev/null
@@ -1,13 +0,0 @@
-#!/bin/sh
-
-# Copyright (C) 2006-2010 OpenWrt.org
-# Copyright (C) 2010 Vertical Communications
-
-reset_copy_rom() {
-    [ "$reset_has_fo" != "true" ] && {
-	dupe $jffs $rom
-	exit 0
-    } 
-}
-
-boot_hook_add jffs2reset reset_copy_rom
diff --git a/package/base-files/files/lib/firstboot/40_copy_ramoverlay b/package/base-files/files/lib/firstboot/40_copy_ramoverlay
deleted file mode 100644
index 39c2edacb..000000000
--- a/package/base-files/files/lib/firstboot/40_copy_ramoverlay
+++ /dev/null
@@ -1,15 +0,0 @@
-#!/bin/sh
-
-# Copyright (C) 2006-2010 OpenWrt.org
-# Copyright (C) 2010 Vertical Communications
-
-copy_ramoverlay() {
-    # try to avoid fs changing while copying
-    mount -o remount,ro none / 2>&-
-    # copy ramoverlay to jffs2
-    echo -n "copying files ... "
-    cp -a /tmp/root/* /rom/overlay 2>&-
-    echo "done"
-}
-
-boot_hook_add switch2jffs copy_ramoverlay
diff --git a/package/base-files/files/lib/firstboot/40_no_fo_copy_ramoverlay b/package/base-files/files/lib/firstboot/40_no_fo_copy_ramoverlay
deleted file mode 100644
index ced7c1b66..000000000
--- a/package/base-files/files/lib/firstboot/40_no_fo_copy_ramoverlay
+++ /dev/null
@@ -1,14 +0,0 @@
-#!/bin/sh
-
-# Copyright (C) 2006-2010 OpenWrt.org
-# Copyright (C) 2010 Vertical Communications
-
-no_fo_copy_ramoverlay() {
-    # copy ramoverlay to jffs2, must be done after switching
-    # to the new rootfs to avoid creating opaque directories
-    echo -n "copying files ... "
-    cp -a /tmp/root/* / >/dev/null 2>&1
-    sync
-}
-
-boot_hook_add no_fo no_fo_ramoverlay
diff --git a/package/base-files/files/lib/firstboot/50_pivot b/package/base-files/files/lib/firstboot/50_pivot
deleted file mode 100644
index 53801d7a8..000000000
--- a/package/base-files/files/lib/firstboot/50_pivot
+++ /dev/null
@@ -1,17 +0,0 @@
-#!/bin/sh
-
-# Copyright (C) 2006-2010 OpenWrt.org
-# Copyright (C) 2010 Vertical Communications
-
-with_fo_pivot() {
-    # switch back to squashfs (temporarily)
-    # and park the ramdisk ontop of /tmp/root
-    pivot /rom /mnt
-    mount -o move /mnt /tmp/root
-
-    # /overlay is the overlay
-    # /rom is the readonly
-    fopivot /overlay /rom
-}
-
-boot_hook_add switch2jffs with_fo_pivot
diff --git a/package/base-files/files/lib/firstboot/99_10_no_fo_cleanup b/package/base-files/files/lib/firstboot/99_10_no_fo_cleanup
deleted file mode 100644
index 6dedcb1e8..000000000
--- a/package/base-files/files/lib/firstboot/99_10_no_fo_cleanup
+++ /dev/null
@@ -1,13 +0,0 @@
-#!/bin/sh
-
-# Copyright (C) 2006-2010 OpenWrt.org
-# Copyright (C) 2010 Vertical Communications
-
-no_fo_cleanup() {
-    echo "done"
-    umount -l /overlay
-    umount -l /tmp/root
-    exit 0
-}
-
-boot_hook_add no_fo no_fo_cleanup
diff --git a/package/base-files/files/lib/firstboot/99_10_with_fo_cleanup b/package/base-files/files/lib/firstboot/99_10_with_fo_cleanup
deleted file mode 100644
index 560169901..000000000
--- a/package/base-files/files/lib/firstboot/99_10_with_fo_cleanup
+++ /dev/null
@@ -1,25 +0,0 @@
-#!/bin/sh
-
-# Copyright (C) 2006-2010 OpenWrt.org
-# Copyright (C) 2010 Vertical Communications
-
-with_fo_cleanup() {
-    # try to get rid of /tmp/root
-    # this will almost always fail
-    umount -l /tmp/root 2>&-
-	grep -q overlay /proc/filesystems && {
-		cd /
-		(
-			cd /overlay
-			find -type l
-		) | while read FILE; do
-			[ -z "$FILE" ] && break
-			if ls -la "$FILE" 2>&- | grep -q '(overlay-whiteout)'; then
-				rm -f "$FILE"
-			fi
-		done
-	}
-    exit 0
-}
-
-boot_hook_add switch2jffs with_fo_cleanup
diff --git a/package/base-files/files/lib/preinit/05_mount_skip b/package/base-files/files/lib/preinit/05_mount_skip
deleted file mode 100644
index c2b7ee79d..000000000
--- a/package/base-files/files/lib/preinit/05_mount_skip
+++ /dev/null
@@ -1,13 +0,0 @@
-#!/bin/sh
-
-# Copyright (C) 2006 OpenWrt.org
-# Copyright (C) 2010 Vertical Communications
-
-check_skip() {
-    if [ "$pi_mount_skip_next" = "true" ]; then
-	return 0
-    else 
-	return 1
-    fi
-}
-
diff --git a/package/base-files/files/lib/preinit/10_check_for_mtd b/package/base-files/files/lib/preinit/10_check_for_mtd
deleted file mode 100644
index 6a7bd307f..000000000
--- a/package/base-files/files/lib/preinit/10_check_for_mtd
+++ /dev/null
@@ -1,20 +0,0 @@
-#!/bin/sh
-
-# Copyright (C) 2006-2010 OpenWrt.org
-# Copyright (C) 2010 Vertical Communications
-
-mount_no_mtd() {
-    mtd unlock rootfs
-    mount -o noatime,remount,rw /dev/root /
-}
-
-check_for_mtd() {
-    check_skip || {
-	grep -qs rootfs_data /proc/mtd || {
-	    mount_no_mtd && pi_mount_skip_next=true
-	}
-    }
-}
-
-boot_hook_add preinit_mount_root check_for_mtd
-
diff --git a/package/base-files/files/lib/preinit/20_check_jffs2_ready b/package/base-files/files/lib/preinit/20_check_jffs2_ready
deleted file mode 100644
index 3c5cf67e8..000000000
--- a/package/base-files/files/lib/preinit/20_check_jffs2_ready
+++ /dev/null
@@ -1,19 +0,0 @@
-#!/bin/sh
-# Copyright (C) 2006-2010 OpenWrt.org
-# Copyright (C) 2010 Vertical Communications
-
-mount_no_jffs2() {
-    echo "jffs2 not ready yet; using ramdisk"
-    ramoverlay
-}
-
-check_for_jffs2() {
-    check_skip || {
-	jffs2_ready || {
-	    mount_no_jffs2 && pi_mount_skip_next=true
-	}
-    }
-}
-
-boot_hook_add preinit_mount_root check_for_jffs2
-
diff --git a/package/base-files/files/lib/preinit/40_mount_jffs2 b/package/base-files/files/lib/preinit/40_mount_jffs2
deleted file mode 100644
index ded785ed5..000000000
--- a/package/base-files/files/lib/preinit/40_mount_jffs2
+++ /dev/null
@@ -1,26 +0,0 @@
-#!/bin/sh
-# Copyright (C) 2006-2010 OpenWrt.org
-# Copyright (C) 2010 Vertical Communications
-
-find_mount_jffs2() {
-    mkdir -p /tmp/overlay
-    mount -o noatime -t jffs2 "$(find_mtd_part rootfs_data)" /tmp/overlay
-    mtd -qq unlock rootfs_data
-}
-
-jffs2_not_mounted() {
-    if [ "$pi_jffs2_mount_success" != "true" ]; then
-	return 0
-    else
-	return 1
-    fi
-}
-
-do_mount_jffs2() {
-    check_skip || {
-	find_mount_jffs2 && pi_jffs2_mount_success=true
-    }
-}
-
-boot_hook_add preinit_mount_root do_mount_jffs2
-
diff --git a/package/base-files/files/lib/preinit/41_merge_overlay_hooks b/package/base-files/files/lib/preinit/41_merge_overlay_hooks
deleted file mode 100644
index 4ca6877be..000000000
--- a/package/base-files/files/lib/preinit/41_merge_overlay_hooks
+++ /dev/null
@@ -1,24 +0,0 @@
-#!/bin/sh
-# Copyright (C) 2010 OpenWrt.org
-
-merge_overlay_hooks() {
-	jffs2_not_mounted || [ ! -d /tmp/overlay/lib/preinit ] || {
-		echo "- merge overlay components -"
-
-		mkdir -p /tmp/preinit-hook-merge
-		ln -sf /lib/preinit/* /tmp/overlay/lib/preinit/[0-9][0-9]_* /tmp/preinit-hook-merge/
-
-		boot_hook_splice_start
-
-		local pipart
-		for pipart in /tmp/preinit-hook-merge/*; do
-			. $pipart
-		done
-
-		boot_hook_splice_finish
-
-		rm -rf /tmp/preinit-hook-merge
-	}
-}
-
-boot_hook_add preinit_mount_root merge_overlay_hooks
diff --git a/package/base-files/files/lib/preinit/70_pivot_jffs2_root b/package/base-files/files/lib/preinit/70_pivot_jffs2_root
deleted file mode 100644
index b76f111ca..000000000
--- a/package/base-files/files/lib/preinit/70_pivot_jffs2_root
+++ /dev/null
@@ -1,14 +0,0 @@
-#!/bin/sh
-# Copyright (C) 2006-2010 OpenWrt.org
-# Copyright (C) 2010 Vertical Communications
-
-rootfs_pivot() {
-    check_skip || jffs2_not_mounted || {
-	echo "switching to jffs2"
-	mount -o noatime,move /tmp/overlay /overlay 2>&-
-	fopivot /overlay /rom && pi_mount_skip_next=true
-    }
-}
-
-boot_hook_add preinit_mount_root rootfs_pivot
-
diff --git a/package/base-files/files/lib/preinit/80_mount_root b/package/base-files/files/lib/preinit/80_mount_root
index 9a99ee910..0292d82d4 100644
--- a/package/base-files/files/lib/preinit/80_mount_root
+++ b/package/base-files/files/lib/preinit/80_mount_root
@@ -3,7 +3,16 @@
 # Copyright (C) 2010 Vertical Communications
 
 do_mount_root() {
-    boot_run_hook preinit_mount_root
+	mount_root
+	boot_run_hook preinit_mount_root
+	[ -f /sysupgrade.tgz ] && {
+		echo "- config restore -"
+		cd /
+		mv sysupgrade.tgz /tmp
+		tar xzf /tmp/sysupgrade.tgz
+		rm -f /tmp/sysupgrade.tgz
+		sync
+	}
 }
 
 [ "$INITRAMFS" = "1" ] || boot_hook_add preinit_main do_mount_root
diff --git a/package/base-files/files/lib/preinit/90_mount_no_jffs2 b/package/base-files/files/lib/preinit/90_mount_no_jffs2
deleted file mode 100644
index d8ad4ae11..000000000
--- a/package/base-files/files/lib/preinit/90_mount_no_jffs2
+++ /dev/null
@@ -1,12 +0,0 @@
-#!/bin/sh
-# Copyright (C) 2006-2010 OpenWrt.org
-# Copyright (C) 2010 Vertical Communications
-
-do_mount_no_jffs2() {
-    check_skip || {
-	mount_no_jffs2 && pi_mount_skip_next=true
-    }
-}
-
-boot_hook_add preinit_mount_root do_mount_no_jffs2
-
diff --git a/package/base-files/files/lib/preinit/90_restore_config b/package/base-files/files/lib/preinit/90_restore_config
deleted file mode 100644
index 210bf6184..000000000
--- a/package/base-files/files/lib/preinit/90_restore_config
+++ /dev/null
@@ -1,17 +0,0 @@
-#!/bin/sh
-# Copyright (C) 2006 OpenWrt.org
-# Copyright (C) 2010 Vertical Communications
-
-restore_config() {
-    [ -f /sysupgrade.tgz ] && {
-	echo "- config restore -"
-	cd /
-	mv sysupgrade.tgz /tmp
-	tar xzf /tmp/sysupgrade.tgz
-	rm -f /tmp/sysupgrade.tgz
-	sync
-    }
-}
-
-boot_hook_add preinit_main restore_config
-
diff --git a/package/base-files/files/lib/preinit/99_10_mount_no_mtd b/package/base-files/files/lib/preinit/99_10_mount_no_mtd
deleted file mode 100644
index c4f38e415..000000000
--- a/package/base-files/files/lib/preinit/99_10_mount_no_mtd
+++ /dev/null
@@ -1,12 +0,0 @@
-#!/bin/sh
-# Copyright (C) 2006-2010 OpenWrt.org
-# Copyright (C) 2010 Vertical Communications
-
-do_mount_no_mtd() {
-    check_skip || {
-	mount_no_mtd
-    }
-}
-
-boot_hook_add preinit_mount_root do_mount_no_mtd
-
diff --git a/package/base-files/files/sbin/firstboot b/package/base-files/files/sbin/firstboot
index 06b64c313..081febb7b 100755
--- a/package/base-files/files/sbin/firstboot
+++ b/package/base-files/files/sbin/firstboot
@@ -1,44 +1,3 @@
 #!/bin/sh
 
-switch2jffs_hook=
-jffs2reset_hook=
-no_fo_hook=
-
-. /lib/functions.sh
-
-firstboot_skip_next=false
-
-for fb_source_file in /lib/firstboot/*; do
-    . $fb_source_file
-done
-
-set_mtd_part
-set_rom_part
-set_jffs_part
-
-# invoked as an executable
-if [ "${0##*/}" = "firstboot" ]; then
-    if [ "$1" = "switch2jffs" ]; then
-	boot_run_hook switch2jffs
-    else
-	if [ -t 0 ] && [ "$1" != "-y" ]; then
-		local input
-
-		echo -n "firstboot will erase all settings and remove any installed packages. Are you sure? [N/y]"
-		read input
-		[ "$input" = "y" ] || [ "$input" = "Y" ] || return 0
-	fi
-
-	if [ -n "$jffs" ]; then
-	    reset_has_fo=true
-	    echo "firstboot has already been run"
-	    echo "jffs2 partition is mounted, only resetting files"
-	    boot_run_hook jffs2reset
-	else
-	    mtd erase "$partname"
-	    mount -o noatime "$mtdpart" /overlay -t jffs2
-	    fopivot /overlay /rom 1
-	fi
-    fi
-fi
-
+/sbin/jffs2reset
diff --git a/package/base-files/files/sbin/mount_root b/package/base-files/files/sbin/mount_root
deleted file mode 100755
index 1c48ebab1..000000000
--- a/package/base-files/files/sbin/mount_root
+++ /dev/null
@@ -1,26 +0,0 @@
-#!/bin/sh
-# Copyright (C) 2006-2010 OpenWrt.org
-# Copyright (C) 2010 Vertical Communications
-
-# Initialize hook variables et by pulling in from preinit
-
-preinit_essential_hook=
-preinit_main_hook=
-failsafe_hook=
-initramfs_hook=
-preinit_mount_root=
-
-. /lib/functions.sh
-
-for pi_source_file in /lib/preinit/*; do
-    . $pi_source_file
-done
-
-# Override function that doesn't apply to use of mount_root as a command rather
-# than as part of preinit
-
-preinit_echo() {
-    echo "$@"
-}
-
-boot_run_hook preinit_mount_root
diff --git a/package/base-files/files/sbin/sysupgrade b/package/base-files/files/sbin/sysupgrade
index 67019307d..1ad6ea44a 100755
--- a/package/base-files/files/sbin/sysupgrade
+++ b/package/base-files/files/sbin/sysupgrade
@@ -179,6 +179,8 @@ fi
 
 run_hooks "" $sysupgrade_pre_upgrade
 
+ubus call system upgrade
+
 kill_remaining TERM
 sleep 3
 kill_remaining KILL
-- 
cgit v1.2.3