Bläddra i källkod

add preinit modularization work by Daniel Dickinson (cshore)

SVN-Revision: 19331
Felix Fietkau 15 år sedan
förälder
incheckning
56bc02a305
79 ändrade filer med 1449 tillägg och 438 borttagningar
  1. 18 0
      package/base-files/Makefile
  2. 29 82
      package/base-files/files/etc/preinit
  3. 10 0
      package/base-files/files/lib/firstboot/05_firstboot_skip
  4. 46 0
      package/base-files/files/lib/firstboot/10_determine_parts
  5. 17 0
      package/base-files/files/lib/firstboot/10_no_fo_clear_overlay
  6. 12 0
      package/base-files/files/lib/firstboot/10_reset_has_mini_fo
  7. 13 0
      package/base-files/files/lib/firstboot/20_has_mini_fo
  8. 15 0
      package/base-files/files/lib/firstboot/20_no_fo_mount_jffs
  9. 14 0
      package/base-files/files/lib/firstboot/20_reset_clear_jffs
  10. 10 0
      package/base-files/files/lib/firstboot/30_is_rootfs_mounted
  11. 11 0
      package/base-files/files/lib/firstboot/30_no_fo_pivot
  12. 13 0
      package/base-files/files/lib/firstboot/30_reset_copy_rom
  13. 15 0
      package/base-files/files/lib/firstboot/40_copy_ramoverlay
  14. 14 0
      package/base-files/files/lib/firstboot/40_no_fo_copy_ramoverlay
  15. 17 0
      package/base-files/files/lib/firstboot/50_pivot
  16. 13 0
      package/base-files/files/lib/firstboot/99_10_no_fo_cleanup
  17. 13 0
      package/base-files/files/lib/firstboot/99_10_with_fo_cleanup
  18. 95 0
      package/base-files/files/lib/functions/boot.sh
  19. 13 0
      package/base-files/files/lib/preinit/05_mount_skip
  20. 20 0
      package/base-files/files/lib/preinit/10_check_for_mtd
  21. 25 0
      package/base-files/files/lib/preinit/10_essential_fs
  22. 18 0
      package/base-files/files/lib/preinit/10_indicate_failsafe
  23. 52 0
      package/base-files/files/lib/preinit/10_indicate_preinit
  24. 19 0
      package/base-files/files/lib/preinit/20_check_jffs2_ready
  25. 28 0
      package/base-files/files/lib/preinit/20_device_fs_mount
  26. 36 0
      package/base-files/files/lib/preinit/30_device_fs_daemons
  27. 86 0
      package/base-files/files/lib/preinit/30_failsafe_wait
  28. 10 0
      package/base-files/files/lib/preinit/40_init_shm
  29. 15 0
      package/base-files/files/lib/preinit/40_mount_devpts
  30. 24 0
      package/base-files/files/lib/preinit/40_mount_jffs2
  31. 13 0
      package/base-files/files/lib/preinit/40_run_failsafe_hook
  32. 31 0
      package/base-files/files/lib/preinit/50_choose_console
  33. 12 0
      package/base-files/files/lib/preinit/50_indicate_regular_preinit
  34. 10 0
      package/base-files/files/lib/preinit/60_init_hotplug
  35. 13 0
      package/base-files/files/lib/preinit/70_initramfs_test
  36. 13 0
      package/base-files/files/lib/preinit/70_pivot_jffs2_root
  37. 10 0
      package/base-files/files/lib/preinit/80_mount_root
  38. 14 0
      package/base-files/files/lib/preinit/90_init_console
  39. 12 0
      package/base-files/files/lib/preinit/90_mount_no_jffs2
  40. 17 0
      package/base-files/files/lib/preinit/90_restore_config
  41. 18 0
      package/base-files/files/lib/preinit/99_10_failsafe_login
  42. 12 0
      package/base-files/files/lib/preinit/99_10_mount_no_mtd
  43. 16 0
      package/base-files/files/lib/preinit/99_10_run_init
  44. 24 152
      package/base-files/files/sbin/firstboot
  45. 22 21
      package/base-files/files/sbin/mount_root
  46. 132 0
      package/base-files/image-config.in
  47. 6 2
      scripts/metadata.pl
  48. 0 16
      target/linux/adm5120/base-files/etc/preinit.arch
  49. 12 0
      target/linux/adm5120/base-files/lib/preinit/05_enable_leds_adm5120
  50. 9 0
      target/linux/adm5120/base-files/lib/preinit/05_set_preinit_face_adm5120
  51. 16 0
      target/linux/ar71xx/base-files/lib/preinit/05_enable_leds_ar71xx
  52. 1 18
      target/linux/ar71xx/base-files/lib/preinit/05_set_iface_mac_ar71xx
  53. 16 0
      target/linux/ar71xx/base-files/lib/preinit/05_set_preinit_iface_ar71xx
  54. 0 21
      target/linux/atheros/base-files/etc/preinit.arch
  55. 32 0
      target/linux/atheros/base-files/lib/preinit/15_preinit_iface_atheros
  56. 0 71
      target/linux/brcm-2.4/base-files/etc/preinit.arch
  57. 9 0
      target/linux/brcm-2.4/base-files/lib/preinit/03_init_hotplug_failsafe_brcm-2.4
  58. 38 0
      target/linux/brcm-2.4/base-files/lib/preinit/05_init_interfaces_brcm-2.4
  59. 15 0
      target/linux/brcm-2.4/base-files/lib/preinit/05_set_failsafe_switch_brcm-2.4
  60. 6 0
      target/linux/brcm-2.4/base-files/lib/preinit/15_mount_proc_brcm-2.4
  61. 38 0
      target/linux/brcm-2.4/base-files/lib/preinit/15_set_preinit_interface_brcm-2.4
  62. 14 0
      target/linux/brcm-2.4/base-files/lib/preinit/20_failsafe_set_boot_wait_brcm-2.4
  63. 0 6
      target/linux/gemini/base-files/etc/preinit.arch
  64. 13 0
      target/linux/gemini/base-files/lib/preinit/05_set_ether_mac_gemini
  65. 0 5
      target/linux/ifxmips/nfs/base-files/etc/preinit.arch
  66. 12 0
      target/linux/ifxmips/nfs/base-files/lib/preinit/01_init_nfs_ifxmips
  67. 0 15
      target/linux/ixp4xx/base-files/etc/preinit.arch
  68. 23 0
      target/linux/ixp4xx/base-files/lib/preinit/05_set_ether_mac_ixp4xx
  69. 0 2
      target/linux/pxcab/base-files/etc/preinit.arch
  70. 8 0
      target/linux/pxcab/base-files/lib/preinit/15_essential_fs_pxcab
  71. 14 0
      target/linux/pxcab/base-files/lib/preinit/45_failsafe_pxcab
  72. 0 23
      target/linux/rdc/base-files/etc/preinit.arch
  73. 32 0
      target/linux/rdc/base-files/lib/preinit/05_set_ether_mac_rdc
  74. 0 2
      target/linux/x86/base-files/etc/preinit.arch
  75. 8 0
      target/linux/x86/base-files/lib/preinit/15_essential_fs_x86
  76. 14 0
      target/linux/x86/base-files/lib/preinit/45_failsafe_x86
  77. 0 2
      target/linux/x86/olpc/base-files/etc/preinit.arch
  78. 9 0
      target/linux/x86/olpc/base-files/lib/preinit/15_essential_fs_x86
  79. 14 0
      target/linux/x86/olpc/base-files/lib/preinit/45_failsafe_x86

+ 18 - 0
package/base-files/Makefile

@@ -1,5 +1,6 @@
 #
 # Copyright (C) 2007-2010 OpenWrt.org
+# Copyright (C) 2010 Vertical Communications
 #
 # This is free software, licensed under the GNU General Public License v2.
 # See /LICENSE for more information.
@@ -298,6 +299,22 @@ define Package/ldconfig/config
 endef
 
 
+define ImageConfigOptions
+	mkdir -p $(1)/lib/preinit
+	echo 'pi_suppress_stderr="$(CONFIG_TARGET_PREINIT_SUPPRESS_STDERR)"' >$(1)/lib/preinit/00_preinit.conf
+	echo 'fs_failsafe_wait_timeout=$(if $(CONFIG_TARGET_PREINIT_TIMEOUT),$(CONFIG_TARGET_PREINIT_TIMEOUT),2)' >>$(1)/lib/preinit/00_preinit.conf
+	echo 'pi_init_path=$(if $(CONFIG_TARGET_INIT_PATH),$(CONFIG_TARGET_INIT_PATH),"/bin:/sbin:/usr/bin:/usr/sbin")' >>$(1)/lib/preinit/00_preinit.conf
+	echo 'pi_init_env=$(if $(CONFIG_TARGET_INIT_ENV),$(CONFIG_TARGET_INIT_ENV),"")' >>$(1)/lib/preinit/00_preinit.conf
+	echo 'pi_init_cmd=$(if $(CONFIG_TARGET_INIT_CMD),$(CONFIG_TARGET_INIT_CMD),"/sbin/init")' >>$(1)/lib/preinit/00_preinit.conf
+	echo 'pi_init_suppress_stderr="$(CONFIG_TARGET_INIT_SUPPRESS_STDERR)"' >>$(1)/lib/preinit/00_preinit.conf
+	echo 'pi_ifname=$(if $(CONFIG_TARGET_PREINIT_IFNAME),$(CONFIG_TARGET_PREINIT_IFNAME),"")' >>$(1)/lib/preinit/00_preinit.conf
+	echo 'pi_ip=$(if $(CONFIG_TARGET_PREINIT_IP),$(CONFIG_TARGET_PREINIT_IP),"192.168.1.1")' >>$(1)/lib/preinit/00_preinit.conf
+	echo 'pi_netmask=$(if $(CONFIG_TARGET_PREINIT_NETMASK),$(CONFIG_TARGET_PREINIT_NETMASK),"255.255.255.0")' >>$(1)/lib/preinit/00_preinit.conf
+	echo 'pi_broadcast=$(if $(CONFIG_TARGET_PREINIT_BROADCAST),$(CONFIG_TARGET_PREINIT_BROADCAST),"")' >>$(1)/lib/preinit/00_preinit.conf
+	echo 'pi_preinit_net_messages="$(CONFIG_TARGET_PREINIT_SHOW_NETMSG)"' >>$(1)/lib/preinit/00_preinit.conf
+	echo 'pi_preinit_no_failsafe_netmsg="$(CONFIG_TARGET_SUPPRESS_FAILSAFE_NETMSG)"' >>$(1)/lib/preinit/00_preinit.conf
+endef
+
 define Build/Prepare
 	mkdir -p $(PKG_BUILD_DIR)
 endef
@@ -362,6 +379,7 @@ define Package/base-files/install
 	ln -sf /tmp $(1)/var
 	mkdir -p $(1)/etc
 	ln -sf /tmp/resolv.conf /tmp/fstab /tmp/TZ $(1)/etc/
+	$(call ImageConfigOptions,$(1))
 	$(call Package/base-files/install-target,$(1))
 	for conffile in $(1)/etc/config/*; do \
 		if [ -f "$$$$conffile" ]; then \

+ 29 - 82
package/base-files/files/etc/preinit

@@ -1,98 +1,45 @@
 #!/bin/sh
 # Copyright (C) 2006 OpenWrt.org
+# Copyright (C) 2010 Vertical Communications
+
 export PATH=/bin:/sbin:/usr/bin:/usr/sbin
 . /etc/diag.sh
 
-failsafe_ip() {
-	ifconfig $ifname 192.168.1.1 netmask 255.255.255.0 broadcast 192.168.1.255 up
-}
-
-failsafe() {
-	set_state failsafe
-	[ -n "$ifname" ] && grep "$ifname" /proc/net/dev >/dev/null && {
-		failsafe_ip
-		netmsg 192.168.1.255 "Entering Failsafe!"
-		telnetd -l /bin/login.sh <> /dev/null 2>&1
-	}
-	lock /tmp/.failsafe
-	ash --login
-}
-
-mount proc /proc -t proc
-mount sysfs /sys -t sysfs
+preinit_essential_hook=
+preinit_main_hook=
+failsafe_hook=
+initramfs_hook=
+preinit_mount_root=
 
-size=$(awk '/MemTotal:/ {l=5242880;mt=($2*1024);print((s=mt/2)<l)&&(mt>l)?mt-l:s}' /proc/meminfo)
-mount tmpfs /tmp -t tmpfs -o size=$size,nosuid,nodev,mode=1777
+pi_ifname=
+pi_ip=192.168.1.1
+pi_broadcast=192.168.1.255
+pi_netmask=255.255.255.0
 
-if grep devfs /proc/filesystems > /dev/null; then
-	mount devfs /dev -t devfs
-	M0=/dev/pty/m0
-	M1=/dev/pty/m1
-	HOTPLUG=/sbin/hotplug-call
+fs_failsafe_ifname=
+fs_failsafe_ip=192.168.1.1
+fs_failsafe_broadcast=192.168.1.255
+fs_failsafe_netmask=255.255.255.0
 
-elif [ -x /sbin/hotplug2 ]; then
-	mount -t tmpfs tmpfs /dev -o size=512K
-	mknod /dev/console c 5 1
-	/sbin/hotplug2 --set-worker /lib/hotplug2/worker_fork.so --set-rules-file /etc/hotplug2-init.rules --no-persistent --set-coldplug-cmd /sbin/udevtrigger
-	/sbin/hotplug2 --set-worker /lib/hotplug2/worker_fork.so --set-rules-file /etc/hotplug2-init.rules --persistent &
-	M0=/dev/ptmx
-	M1=/dev/ptmx
-	HOTPLUG=
+fs_failsafe_wait_timeout=2
 
-elif [ -x /sbin/udevd ]; then
-	mount -n -t tmpfs -o mode=0755 udev /dev
-	[ -d /lib/udev/devices ] && cp -af /lib/udev/devices/* /dev/
-	[ -c /dev/console ] || mknod -m 0600 /dev/console c 5 1
-	[ -c /dev/null ] || mknod -m 0666 /dev/null c 1 3
-	/sbin/udevd --daemon --resolve-names=never
-	/sbin/udevadm trigger
-	/sbin/udevadm settle
-	M0=/dev/pty/ptmx
-	M1=/dev/pty/ptmx
-	HOTPLUG=
-fi
+pi_suppress_stderr="y"
+pi_init_suppress_stderr="y"
+pi_init_path="/bin:/sbin:/usr/bin:/usr/sbin"
+pi_init_cmd="/sbin/init"
 
-[ -d /dev/pts ] || mkdir -p /dev/pts
-[ -d /dev/shm ] || mkdir -p /dev/shm
-mount devpts /dev/pts -t devpts
+. /lib/functions/boot.sh
 
-# the shell really doesn't like having stdin/out closed
-# that's why we use /dev/pty/m0 and m1 as replacement
-# for /dev/console if there's no serial console available
-dd if=/dev/console of=/dev/null bs=1 count=0 >/dev/null 2>/dev/null && {
-	M0=/dev/console
-	M1=/dev/console
-}
 
-exec <$M0 >$M1 2>&0
+for pi_source_file in /lib/preinit/*; do
+    . $pi_source_file
+done
 
-echo "- preinit -"
-set_state preinit
-echo "Press CTRL-C for failsafe"
-trap 'FAILSAFE=true' INT
-trap 'FAILSAFE=true' USR1
-if [ -e /etc/preinit.arch ]; then
-	. /etc/preinit.arch
-else
-	sleep 2
-fi
-echo "$HOTPLUG" > /proc/sys/kernel/hotplug
-export FAILSAFE
-eval ${FAILSAFE:+failsafe}
-lock -w /tmp/.failsafe
+boot_run_hook preinit_essential
 
-if [ -z "$INITRAMFS" ]; then
-	mount_root
-	[ -f /sysupgrade.tgz ] && {
-		echo "- config restore -"
-		cd /
-		mv sysupgrade.tgz /tmp
-		tar xzf /tmp/sysupgrade.tgz
-		rm -f /tmp/sysupgrade.tgz
-		sync
-	}
+pi_mount_skip_next=false
+pi_jffs2_mount_success=false
+pi_failsafe_net_message=false
 
-	echo "- init -"
+boot_run_hook preinit_main
 
-	exec /sbin/init
-fi

+ 10 - 0
package/base-files/files/lib/firstboot/05_firstboot_skip

@@ -0,0 +1,10 @@
+#!/bin/sh
+
+check_skip() {
+    if [ "$firsboot_skip_next" = "true" ]; then
+	return 0
+    else 
+	return 1
+    fi
+}
+

+ 46 - 0
package/base-files/files/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

+ 17 - 0
package/base-files/files/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

+ 12 - 0
package/base-files/files/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_mini_fo() {
+    if grep -q mini_fo /proc/filesystems; then
+	reset_has_fo=true
+    fi
+}
+
+boot_hook_add jffs2reset reset_check_for_mini_fo

+ 13 - 0
package/base-files/files/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_mini_fo() {
+    if ! grep -q mini_fo /proc/filesystems; then
+	boot_run_hook no_fo
+	exit 1
+    fi
+}
+
+boot_hook_add switch2jffs check_for_mini_fo

+ 15 - 0
package/base-files/files/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 "$mtdpart" /jffs -t jffs2 || exit
+    
+    # workaround to ensure that union can attach properly
+    sync
+    ls /jffs >/dev/null
+}
+
+boot_hook_add no_fo no_fo_mount_jffs

+ 14 - 0
package/base-files/files/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

+ 10 - 0
package/base-files/files/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 "$mtdpart" /rom/jffs -t jffs2 || exit
+}
+
+boot_hook_add switch2jffs skip_if_rootfs_mounted

+ 11 - 0
package/base-files/files/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 /jffs /rom 1
+}
+
+boot_hook_add no_fo no_fo_pivot

+ 13 - 0
package/base-files/files/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

+ 15 - 0
package/base-files/files/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/jffs 2>&-
+    echo "done"
+}
+
+boot_hook_add switch2jffs copy_ramoverlay

+ 14 - 0
package/base-files/files/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

+ 17 - 0
package/base-files/files/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
+
+    # /jffs is the overlay
+    # /rom is the readonly
+    fopivot /jffs /rom
+}
+
+boot_hook_add switch2jffs with_fo_pivot

+ 13 - 0
package/base-files/files/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 /jffs
+    umount -l /tmp/root
+    exit 0
+}
+
+boot_hook_add no_fo no_fo_cleanup

+ 13 - 0
package/base-files/files/lib/firstboot/99_10_with_fo_cleanup

@@ -0,0 +1,13 @@
+#!/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
+    mount /tmp/root 2>&-
+    exit 0
+}
+
+boot_hook_add switch2jffs with_fo_cleanup

+ 95 - 0
package/base-files/files/lib/functions/boot.sh

@@ -0,0 +1,95 @@
+#!/bin/sh
+# Copyright (C) 2006-2010 OpenWrt.org
+# Copyright (C) 2010 Vertical Communications
+
+boot_hook_add() {
+    	local hook="${1}_hook"
+	local value="$2"
+	local sep=" "
+	
+	eval "$hook=\"\${$hook:+\${$hook}\${value:+\$sep}}\$value\""
+}
+
+boot_run_hook() {
+    local boot_func
+    for boot_func in $(eval "echo \"\$${1}_hook\""); do
+	$boot_func "$1" "$2"
+    done
+}
+
+find_mtd_part() {
+	local PART="$(grep "\"$1\"" /proc/mtd | awk -F: '{print $1}')"
+	local PREFIX=/dev/mtdblock
+	
+	PART="${PART##mtd}"
+	[ -d /dev/mtdblock ] && PREFIX=/dev/mtdblock/
+	echo "${PART:+$PREFIX$PART}"
+}
+
+jffs2_ready () {
+    mtdpart="$(find_mtd_part rootfs_data)"
+    magic=$(hexdump $mtdpart -n 4 -e '4/1 "%02x"')
+    [ "$magic" != "deadc0de" ]
+}
+
+dupe() { # <new_root> <old_root>
+	cd $1
+	echo -n "creating directories... "
+	{
+		cd $2 
+		find . -xdev -type d
+		echo "./dev ./jffs ./mnt ./proc ./tmp"
+		# xdev skips mounted directories
+		cd $1 
+	} | xargs mkdir -p
+	echo "done"
+
+	echo -n "setting up symlinks... "
+	for file in $(cd $2; find . -xdev -type f;); do
+		case "$file" in
+		./rom/note) ;; #nothing
+		./etc/config*|\
+		./usr/lib/opkg/info/*) cp -af $2/$file $file;;
+		*) ln -sf /rom/${file#./*} $file;;
+		esac
+	done
+	for file in $(cd $2; find . -xdev -type l;); do
+		cp -af $2/${file#./*} $file
+	done
+	echo "done"
+}
+
+pivot() { # <new_root> <old_root>
+	mount -o move /proc $1/proc && \
+	pivot_root $1 $1$2 && {
+		mount -o move $2/dev /dev
+		mount -o move $2/tmp /tmp
+		mount -o move $2/sys /sys 2>&-
+		mount -o move $2/jffs /jffs 2>&-
+		return 0
+	}
+}
+
+fopivot() { # <rw_root> <ro_root> <dupe?>
+	root=$1
+	{
+		if grep -q mini_fo /proc/filesystems; then
+			mount -t mini_fo -o base=/,sto=$1 "mini_fo:$1" /mnt 2>&- && root=/mnt
+		else
+			mount --bind / /mnt
+			mount --bind -o union "$1" /mnt && root=/mnt 
+		fi
+	} || {
+		[ "$3" = "1" ] && {
+		mount | grep "on $1 type" 2>&- 1>&- || mount -o bind $1 $1
+		dupe $1 $rom
+		}
+	}
+	pivot $root $2
+}
+
+ramoverlay() {
+	mkdir -p /tmp/root
+	mount -t tmpfs root /tmp/root
+	fopivot /tmp/root /rom 1
+}

+ 13 - 0
package/base-files/files/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
+}
+

+ 20 - 0
package/base-files/files/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 remount,rw /dev/root /
+}
+
+check_for_mtd() {
+    check_skip || {
+	grep rootfs_data /proc/mtd >/dev/null 2>/dev/null || {
+	    mount_no_mtd && pi_mount_skip_next=true
+	}
+    }
+}
+
+boot_hook_add preinit_mount_root check_for_mtd
+

+ 25 - 0
package/base-files/files/lib/preinit/10_essential_fs

@@ -0,0 +1,25 @@
+#!/bin/sh 
+# Copyright (C) 2006 OpenWrt.org
+# Copyright (C) 2010 Vertical Communications
+
+do_mount_procfs() {
+   mount proc /proc -t proc
+}
+
+do_mount_sysfs() {
+    mount sysfs /sys -t sysfs
+}
+
+calc_tmpfs_size() {
+    pi_size=$(awk '/MemTotal:/ {l=5242880;mt=($2*1024);print((s=mt/2)<l)&&(mt>l)?mt-l:s}' /proc/meminfo)
+}
+
+do_mount_tmpfs() {
+    calc_tmpfs_size
+    mount tmpfs /tmp -t tmpfs -o size=$pi_size,nosuid,nodev,mode=1777
+}
+
+boot_hook_add preinit_essential do_mount_procfs
+boot_hook_add preinit_essential do_mount_sysfs
+boot_hook_add preinit_essential do_mount_tmpfs
+

+ 18 - 0
package/base-files/files/lib/preinit/10_indicate_failsafe

@@ -0,0 +1,18 @@
+#!/bin/sh
+# Copyright (C) 2006 OpenWrt.org
+# Copyright (C) 2010 Vertical Communications
+
+# commands for emitting messages to network in failsafe mode
+
+indicate_failsafe_led () {
+    set_state failsafe
+}
+
+indicate_failsafe() {
+    echo "- failsafe -"
+    preinit_net_echo "Entering Failsafe!\n"
+    indicate_failsafe_led
+}
+
+boot_hook_add failsafe indicate_failsafe
+

+ 52 - 0
package/base-files/files/lib/preinit/10_indicate_preinit

@@ -0,0 +1,52 @@
+#!/bin/sh
+# Copyright (C) 2006 OpenWrt.org
+# Copyright (C) 2010 Vertical Communications
+
+preinit_ip() {
+    # if the preinit interface isn't specified and ifname is set in 
+    # preinit.arch use that interface
+    if [ -z "$pi_ifname" ]; then
+	pi_ifname=$ifname    
+    fi
+
+    [ -n "$pi_ifname" ] && grep "$pi_ifname" /proc/net/dev >/dev/null && {
+	ifconfig $pi_ifname $pi_ip netmask $pi_netmask broadcast $pi_broadcast up
+    }
+}
+
+preinit_ip_deconfig() {
+    [ -n "$pi_ifname" ] && grep "$pi_ifname" /proc/net/dev >/dev/null && {
+	ifconfig $pi_ifname 0.0.0.0 down
+    }
+}
+
+preinit_net_echo() {
+	[ -n "$pi_ifname" ] && grep "$pi_ifname" /proc/net/dev >/dev/null && {
+		{ 
+			[ "$pi_preinit_net_messages" = "y" ] || {
+    		     		[ "$pi_failsafe_net_message"  = "true" ] && 
+    		         	    [ "$pi_preinit_no_failsafe_netmsg" != "y" ] 
+    		         }
+	    			
+		} && netmsg $pi_broadcast "$1"
+    	}
+}
+
+preinit_echo() {
+    preinit_net_echo $1
+    echo $1
+}
+
+pi_indicate_led() {
+    set_state preinit
+}
+
+pi_indicate_preinit() {
+    echo "- preinit -"
+    preinit_net_echo "Doing OpenWRT Preinit\n"
+    pi_indicate_led
+}
+
+boot_hook_add preinit_main preinit_ip
+boot_hook_add preinit_main pi_indicate_preinit
+

+ 19 - 0
package/base-files/files/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
+

+ 28 - 0
package/base-files/files/lib/preinit/20_device_fs_mount

@@ -0,0 +1,28 @@
+#!/bin/sh
+# Copyright (C) 2006 OpenWrt.org
+# Copyright (C) 2010 Vertical Communications
+
+do_mount_devfs() {
+    mount devfs /dev -t devfs
+}
+
+do_mount_hotplug() {
+    mount -t tmpfs tmpfs /dev -o size=512K
+}
+
+do_mount_udev() {
+    mount -n -t tmpfs -o mode=0755 udev /dev
+}
+
+choose_device_fs() {
+    if grep devfs /proc/filesystems > /dev/null; then
+        do_mount_devfs
+    elif [ -x /sbin/hotplug2 ]; then
+        do_mount_hotplug
+    elif [ -x /sbin/udevd ]; then
+        do_mount_udev
+    fi
+}
+
+boot_hook_add preinit_essential choose_device_fs
+

+ 36 - 0
package/base-files/files/lib/preinit/30_device_fs_daemons

@@ -0,0 +1,36 @@
+#!/bin/sh
+# Copyright (C) 2006 OpenWrt.org
+# Copyright (C) 2010 Vertical Communications
+
+init_devfs() {
+    HOTPLUG=/sbin/hotplug-call
+}
+
+init_hotplug2() {
+    [ -c /dev/console ] || mknod /dev/console c 5 1
+    /sbin/hotplug2 --set-worker /lib/hotplug2/worker_fork.so --set-rules-file /etc/hotplug2-init.rules --no-persistent --set-coldplug-cmd /sbin/udevtrigger
+    /sbin/hotplug2 --set-worker /lib/hotplug2/worker_fork.so --set-rules-file /etc/hotplug2-init.rules --persistent &
+}
+
+init_udev() {
+    [ -d /lib/udev/devices ] && cp -af /lib/udev/devices/* /dev/
+    [ -c /dev/console ] || mknod -m 0600 /dev/console c 5 1
+    [ -c /dev/null ] || mknod -m 0666 /dev/null c 1 3
+    /sbin/udevd --daemon --resolve-names=never
+    /sbin/udevadm trigger
+    /sbin/udevadm settle    
+}
+
+init_device_fs() {
+    HOTPLUG=
+    if grep devfs /proc/filesystems > /dev/null; then
+	init_devfs
+    elif [ -x /sbin/hotplug2 ]; then
+	init_hotplug2
+    elif [ -x /sbin/udevd ]; then
+	init_udev
+    fi
+}
+
+boot_hook_add preinit_essential init_device_fs
+

+ 86 - 0
package/base-files/files/lib/preinit/30_failsafe_wait

@@ -0,0 +1,86 @@
+#!/bin/sh
+# Copyright (C) 2006-2010 OpenWrt.org
+# Copyright (C) 2010 Vertical Communications
+
+fs_wait_for_key () {
+    local timeout=$3
+    local timer
+    local do_failsafe
+    local keypress_true="$(mktemp)"
+    local keypress_wait="$(mktemp)"
+    local keypress_sec="$(mktemp)"
+    if [ -z "$keypress_wait" ]; then
+	keypress_wait=/tmp/.keypress_wait
+	touch $keypress_wait
+    fi
+    if [ -z "$keypress_true" ]; then
+	keypress_true=/tmp/.keypress_true
+	touch $keypress_true
+    fi
+    if [ -z "$keypress_sec" ]; then
+	keypress_sec=/tmp/.keypress_sec
+	touch $keypress_sec
+    fi
+
+    trap "echo 'true' >$keypress_true; lock -u $keypress_wait ; rm -f $keypress_wait" INT
+    trap "echo 'true' >$keypress_true; lock -u $keypress_wait ; rm -f $keypress_wait" USR1
+
+    [ -n "$timeout" ] || timeout=1
+    [ $timeout -ge 1 ] || timeout=1
+    timer=$timeout
+    lock $keypress_wait
+    {
+	while [ $timer -gt 0 ]; do
+            echo "$timer" >$keypress_sec
+	    timer=$(($timer - 1))
+	    sleep 1
+	done
+	lock -u $keypress_wait
+	rm -f $keypress_wait
+    } &
+
+    echo "Press $1<ENTER> $2"
+    # if we're on the console we wait for input
+    { 
+	while [ -r $keypress_wait ]; do
+	    timer="$(cat $keypress_sec)"
+
+	    [ -n "$timer" ] || timer=1
+	    timer="${timer%%\ *}"
+	    [ $timer -ge 1 ] || timer=1
+	    do_failsafe=""
+	    {
+		read -t "$timer" do_failsafe
+		if [ "$do_failsafe" = "$1" ]; then
+		    echo "true" >$keypress_true
+		    lock -u $keypress_wait
+		    rm -f $keypress_wait
+ 		fi
+	    }
+	done
+    }
+    lock -w $keypress_wait
+
+    trap - INT
+    trap - USR1
+
+    keypressed=1
+    [ "$(cat $keypress_true)" = "true" ] && keypressed=0
+    rm -f $keypress_true
+    rm -f $keypress_wait
+    rm -f $keypress_sec
+    
+    return $keypressed
+}
+
+failsafe_wait() {
+    FAILSAFE=
+    pi_failsafe_net_message=true
+    preinit_net_echo "Please press button now to enter failsafe"
+    pi_failsafe_net_message=false
+    echo -n "Press CTRL-C or "
+    fs_wait_for_key f 'to enter failsafe mode' $fs_failsafe_wait_timeout && FAILSAFE=true && export FAILSAFE
+}
+
+boot_hook_add preinit_main failsafe_wait
+

+ 10 - 0
package/base-files/files/lib/preinit/40_init_shm

@@ -0,0 +1,10 @@
+#!/bin/sh
+# Copyright (C) 2006 OpenWrt.org
+# Copyright (C) 2010 Vertical Communications
+
+init_shm() {
+    [ -d /dev/shm ] || mkdir -p /dev/shm
+}
+
+boot_hook_add preinit_essential init_shm
+

+ 15 - 0
package/base-files/files/lib/preinit/40_mount_devpts

@@ -0,0 +1,15 @@
+#!/bin/sh
+# Copyright (C) 2006 OpenWrt.org
+# Copyright (C) 2010 Vertical Communications
+
+init_devpts() {
+    [ -d /dev/pts ] || mkdir -p /dev/pts
+}
+
+do_mount_devpts() {
+    mount devpts /dev/pts -t devpts
+}
+
+boot_hook_add preinit_essential init_devpts
+boot_hook_add preinit_essential do_mount_devpts
+

+ 24 - 0
package/base-files/files/lib/preinit/40_mount_jffs2

@@ -0,0 +1,24 @@
+#!/bin/sh
+# Copyright (C) 2006-2010 OpenWrt.org
+# Copyright (C) 2010 Vertical Communications
+
+find_mount_jffs2() {
+    mount "$(find_mtd_part rootfs_data)" /jffs -t jffs2
+}
+
+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
+

+ 13 - 0
package/base-files/files/lib/preinit/40_run_failsafe_hook

@@ -0,0 +1,13 @@
+#!/bin/sh
+# Copyright (C) 2006-2010 OpenWrt.org
+# Copyright (C) 2010 Vertical Communications
+
+run_failsafe_hook() {
+    if [ "$FAILSAFE" = "true" ]; then
+	boot_run_hook failsafe
+	lock -w /tmp/.failsafe
+    fi
+}
+
+boot_hook_add preinit_main run_failsafe_hook
+

+ 31 - 0
package/base-files/files/lib/preinit/50_choose_console

@@ -0,0 +1,31 @@
+#!/bin/sh
+# Copyright (C) 2006-2010 OpenWrt.org
+# Copyright (C) 2010 Vertical Communications
+
+choose_console() {
+    # the shell really doesn't like having stdin/out closed
+    # that's why we use /dev/pty/m0 and m1 (or equivalent) as replacement
+    # for /dev/console if there's no serial console available
+
+    if grep devfs /proc/filesystems > /dev/null; then
+	M0=/dev/pty/m0
+	M1=/dev/pty/m1
+	M2=/dev/pty/m1
+    elif [ -x /sbin/hotplug2 ]; then
+	M0=/dev/ptmx
+	M1=/dev/ptmx
+	M2=/dev/ptmx
+    elif [ -x /sbin/udevd ]; then
+	M0=/dev/pty/ptmx
+	M1=/dev/pty/ptmx
+	M2=/dev/pty/ptmx
+    fi
+    dd if=/dev/console of=/dev/null bs=1 count=0 >/dev/null 2>/dev/null && {
+	M0=/dev/console
+	M1=/dev/console
+	M2=/dev/console    
+    }
+}
+
+boot_hook_add preinit_essential choose_console
+

+ 12 - 0
package/base-files/files/lib/preinit/50_indicate_regular_preinit

@@ -0,0 +1,12 @@
+#!/bin/sh
+# Copyright (C) 2006 OpenWrt.org
+# Copyright (C) 2010 Vertical Communications
+
+indicate_regular_preinit() {
+    echo "- regular preinit -"
+    preinit_net_echo "Continuing with Regular Preinit\n"
+    pi_indicate_led
+}
+
+boot_hook_add preinit_main indicate_regular_preinit
+

+ 10 - 0
package/base-files/files/lib/preinit/60_init_hotplug

@@ -0,0 +1,10 @@
+#!/bin/sh
+# Copyright (C) 2006 OpenWrt.org
+# Copyright (C) 2010 Vertical Communications
+
+init_hotplug() {
+   echo "$HOTPLUG" > /proc/sys/kernel/hotplug
+}
+
+boot_hook_add preinit_main init_hotplug
+

+ 13 - 0
package/base-files/files/lib/preinit/70_initramfs_test

@@ -0,0 +1,13 @@
+#!/bin/sh
+# Copyright (C) 2006 OpenWrt.org
+# Copyright (C) 2010 Vertical Communications
+
+initramfs_test() {
+    if [ -n "$INITRAMFS" ]; then
+	boot_run_hook initramfs
+	break
+    fi
+}
+
+boot_hook_add preinit_main initramfs_test
+

+ 13 - 0
package/base-files/files/lib/preinit/70_pivot_jffs2_root

@@ -0,0 +1,13 @@
+#!/bin/sh
+# Copyright (C) 2006-2010 OpenWrt.org
+# Copyright (C) 2010 Vertical Communications
+
+rootfs_pivot() {
+    check_skip || jffs2_not_mounted || {
+	echo "switching to jffs2"
+	fopivot /jffs /rom && pi_mount_skip_next=true
+    }
+}
+
+boot_hook_add preinit_mount_root rootfs_pivot
+

+ 10 - 0
package/base-files/files/lib/preinit/80_mount_root

@@ -0,0 +1,10 @@
+#!/bin/sh
+# Copyright (C) 2006 OpenWrt.org
+# Copyright (C) 2010 Vertical Communications
+
+do_mount_root() {
+    boot_run_hook preinit_mount_root
+}   
+
+boot_hook_add preinit_main do_mount_root
+

+ 14 - 0
package/base-files/files/lib/preinit/90_init_console

@@ -0,0 +1,14 @@
+#!/bin/sh
+# Copyright (C) 2006-2010 OpenWrt.org
+# Copyright (C) 2010 Vertical Communications
+
+init_console() {
+    if [ "$pi_suppress_stderr" = "y" ]; then
+	exec <$M0 >$M1 2>&0
+    else 
+	exec <$M0 >$M1 2>$M2
+    fi
+}
+
+boot_hook_add preinit_essential init_console
+

+ 12 - 0
package/base-files/files/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
+

+ 17 - 0
package/base-files/files/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
+

+ 18 - 0
package/base-files/files/lib/preinit/99_10_failsafe_login

@@ -0,0 +1,18 @@
+#!/bin/sh
+# Copyright (C) 2006 OpenWrt.org
+# Copyright (C) 2010 Vertical Communications
+
+failsafe_netlogin () {
+    telnetd -l /bin/login.sh <> /dev/null 2>&1    
+}
+
+failsafe_shell() {
+    lock /tmp/.failsafe
+    ash --login
+    echo "Please reboot system when done with failsafe network logins"
+}
+
+
+boot_hook_add failsafe failsafe_netlogin
+boot_hook_add failsafe failsafe_shell
+

+ 12 - 0
package/base-files/files/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
+

+ 16 - 0
package/base-files/files/lib/preinit/99_10_run_init

@@ -0,0 +1,16 @@
+#!/bin/sh
+# Copyright (C) 2006 OpenWrt.org
+# Copyright (C) 2010 Vertical Communications
+
+run_init() {
+    preinit_echo "- init -"
+    preinit_ip_deconfig
+    if [ "$pi_init_stderr_suppress" = "y" ]; then
+	exec env - PATH=$pi_init_path $pi_init_env $pi_init_cmd 2>&0
+    else
+	exec env - PATH=$pi_init_path $pi_init_env $pi_init_cmd
+    fi
+}
+
+boot_hook_add preinit_main run_init
+

+ 24 - 152
package/base-files/files/sbin/firstboot

@@ -1,162 +1,34 @@
 #!/bin/sh
-. /etc/functions.sh
 
-partname="rootfs_data"
-mtdpart="$(find_mtd_part $partname)"
+switch2jffs_hook=
+jffs2reset_hook=
+no_fo_hook=
 
-rom=$(awk '/squashfs/ {print $2}' /proc/mounts)
-jffs=$(awk '/jffs2/ {print $2}' /proc/mounts)
+. /lib/functions/boot.sh
 
-dupe() { # <new_root> <old_root>
-	cd $1
-	echo -n "creating directories... "
-	{
-		cd $2 
-		find . -xdev -type d
-		echo "./dev ./jffs ./mnt ./proc ./tmp"
-		# xdev skips mounted directories
-		cd $1 
-	} | xargs mkdir -p
-	echo "done"
+firstboot_skip_next=false
 
-	echo -n "setting up symlinks... "
-	for file in $(cd $2; find . -xdev -type f;); do
-		case "$file" in
-		./rom/note) ;; #nothing
-		./etc/config*|\
-		./usr/lib/opkg/info/*) cp -af $2/$file $file;;
-		*) ln -sf /rom/${file#./*} $file;;
-		esac
-	done
-	for file in $(cd $2; find . -xdev -type l;); do
-		cp -af $2/${file#./*} $file
-	done
-	echo "done"
-}
-
-pivot() { # <new_root> <old_root>
-	mount -o move /proc $1/proc && \
-	pivot_root $1 $1$2 && {
-		mount -o move $2/dev /dev
-		mount -o move $2/tmp /tmp
-		mount -o move $2/sys /sys 2>&-
-		mount -o move $2/jffs /jffs 2>&-
-		return 0
-	}
-}
-
-fopivot() { # <rw_root> <ro_root> <dupe?>
-	root=$1
-	{
-		if grep -q mini_fo /proc/filesystems; then
-			mount -t mini_fo -o base=/,sto=$1 "mini_fo:$1" /mnt 2>&- && root=/mnt
-		else
-			mount --bind / /mnt
-			mount --bind -o union "$1" /mnt && root=/mnt 
-		fi
-	} || {
-		[ "$3" = "1" ] && {
-		mount | grep "on $1 type" 2>&- 1>&- || mount -o bind $1 $1
-		dupe $1 $rom
-		}
-	}
-	pivot $root $2
-}
-
-ramoverlay() {
-	mkdir -p /tmp/root
-	mount -t tmpfs root /tmp/root
-	fopivot /tmp/root /rom 1
-}
+for fb_source_file in /lib/firstboot/*; do
+    . $fb_source_file
+done
 
 # invoked as an executable
-[ "${0##*/}" = "firstboot" ] && {
-
-	[ -z "$mtdpart" ] && {
-		echo "MTD partition not found."
-		exit 1
-	}
-
-	[ -z "$rom" ] && {
-		echo "You do not have a squashfs partition; aborting"
-		echo "(firstboot cannot be run on jffs2 based firmwares)"
-		exit 1
-	}
-
-	[ "$1" = "switch2jffs" ] && {
-		if grep -q mini_fo /proc/filesystems; then
-			mount "$mtdpart" /rom/jffs -t jffs2 || exit
-
-			# 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/jffs 2>&-
-			echo "done"
-
-			# switch back to squashfs (temporarily)
-			# and park the ramdisk ontop of /tmp/root
-			pivot /rom /mnt
-			mount -o move /mnt /tmp/root
-
-			# /jffs is the overlay
-			# /rom is the readonly
-			fopivot /jffs /rom
-
-			# try to get rid of /tmp/root
-			# this will almost always fail
-			umount /tmp/root 2>&-
-		else
-			# 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
-
-			# initialize jffs2
-			mount "$mtdpart" /jffs -t jffs2 || exit
-
-			# workaround to ensure that union can attach properly
-			sync
-			ls /jffs >/dev/null
-
-			# switch to the new (empty) jffs2
-			fopivot /jffs /rom 1
-
-			# 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
-			echo "done"
-
-			umount -l /jffs
-			umount -l /tmp/root
-		fi
-
-		exit 0
-	}
-
-	# script run manually
-	[ \! -z "$jffs" ] && {
-		echo "firstboot has already been run"
-		echo "jffs2 partition is mounted, only resetting files"
-		grep mini_fo /proc/filesystems >&-
-		[ $? != 0 ] && {
-			dupe $jffs $rom
-			exit 0
-		} || { 
-			rm -rf $jffs/* 2>&-
-			mount -o remount $jffs / 2>&-
-			exit 0
-		}
-	}
-
+if [ "${0##*/}" = "firstboot" ]; then
+    if [ "$1" = "switch2jffs" ]; then
+	boot_run_hook switch2jffs
+    elif [ -z "$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 "$mtdpart" /jffs -t jffs2
 	fopivot /jffs /rom 1
-}
+    fi
+else 
+    set_mtd_part
+    set_rom_part
+    set_jffs_part
+fi
+

+ 22 - 21
package/base-files/files/sbin/mount_root

@@ -1,25 +1,26 @@
 #!/bin/sh
-# Copyright (C) 2006 OpenWrt.org
-. /etc/functions.sh
+# Copyright (C) 2006-2010 OpenWrt.org
+# Copyright (C) 2010 Vertical Communications
 
-jffs2_ready () {
-	mtdpart="$(find_mtd_part rootfs_data)"
-	magic=$(hexdump $mtdpart -n 4 -e '4/1 "%02x"')
-	[ "$magic" != "deadc0de" ]
-}
+# Initialize hook variables et by pulling in from preinit
+
+preinit_essential_hook=
+preinit_main_hook=
+failsafe_hook=
+initramfs_hook=
+preinit_mount_root=
+
+. /lib/functions/boot.sh
 
-grep rootfs_data /proc/mtd >/dev/null 2>/dev/null && {
-	. /sbin/firstboot
-	mtd unlock rootfs_data
-	jffs2_ready && {
-		echo "switching to jffs2"
-		mount "$(find_mtd_part rootfs_data)" /jffs -t jffs2 && \
-			fopivot /jffs /rom
-	} || {
-		echo "jffs2 not ready yet; using ramdisk"
-		ramoverlay
-	}
-} || {
-	mtd unlock rootfs
-	mount -o remount,rw /dev/root /
+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 mount_root

+ 132 - 0
package/base-files/image-config.in

@@ -0,0 +1,132 @@
+# Copyright (C) 2006 OpenWrt.org
+# Copyright (C) 2010 Vertical Communications
+#
+# This is free software, licensed under the GNU General Public License v2.
+# See /LICENSE for more information.
+#
+
+menuconfig PREINITOPT
+     bool "Preinit configuration options" if IMAGEOPT
+     default n
+     help
+	These options are used to control the environment used to initialize
+	the system before running init (which typically mean /sbin/init which
+	switches to multiuser mode).
+
+config TARGET_PREINIT_SUPPRESS_STDERR
+      bool "Suppress stderr messages during preinit" if PREINITOPT
+      default y
+      help
+	Sends stderr to null during preinit.  This is the default behaviour
+	in previous versions of OpenWRT.  This also prevents init process
+	itself from displaying stderr, however processes launched by init
+	in multiuser through inittab will use the current terminal (e.g.
+	the ash shell launched by inittab will display stderr).  That's
+	the same behaviour as seen in previous version of OpenWRT.
+
+config TARGET_PREINIT_TIMEOUT
+       int
+       prompt "Failsafe wait timeout" if PREINITOPT
+       default 2
+       help
+		How long to wait for failsafe mode to be entered before
+		continuing with a regular boot if failsafe not selected.
+		
+config TARGET_PREINIT_SHOW_NETMSG
+	bool
+	prompt "Show all preinit network messages" if PREINITOPT
+	default n
+	help
+		Show preinit all network messages (via netmsg broadcast), not only
+		the message indicating to press reset to enter failsafe.  Note that
+		if the architecture doesn't define an interface, and there is no
+		'Preinit network interface' defined, then no messages will be
+		emitted, even if this is set.
+		
+config TARGET_PREINIT_SUPPRESS_FAILSAFE_NETMSG
+	bool
+	prompt "Suppress network message indicating failsafe" if PREINITOPT
+	default n
+	help
+		If "Show all preinit network messages" above is not set, then
+		setting this option suppresses the only message that would be
+		emitted otherwise, name the network message to enter failsafe
+		(via netmsg).
+
+config TARGET_PREINIT_IFNAME
+       string
+       prompt "Preinit network interface" if PREINITOPT
+       default ""
+       help
+		Interface for sending preinit messages to network, and any other
+		default networking in failsafe or preinit.  If empty
+		uses $ifname (if defined in /etc/preinit.arch).
+
+config TARGET_PREINIT_IP
+       string
+       prompt "IP address for preinit network messages" if PREINITOPT
+       default "192.168.1.1"
+       help    
+       	       IP address used to configure interface for preinit network
+	       messages, including failsafe messages
+
+config TARGET_PREINIT_NETMASK
+       string
+       prompt "Netmask for preinit network messages" if PREINITOPT
+       default "255.255.255.0"
+       help
+		Netmask used to configure interface for preinit network	
+		messages, including failsafes messages
+
+config TARGET_PREINIT_BROADCAST
+       string
+       prompt "Broadcast address for preinit network messages" if PREINITOPT
+       default "192.168.1.255
+       help
+		Broadcast address to which to send preinit network messages, as
+		as failsafe messages
+
+
+menuconfig INITOPT
+    bool "Init configuration options" if IMAGEOPT
+    default n
+    help
+	These option choose the command that will run as the 'init' command
+	(that is which is responsible for controlling the system once preinit
+	transfers control to it) as well as some options controlling its
+	behaviour.  Normally init is /sbin/init.
+
+    config TARGET_INIT_PATH
+        string
+    	prompt "PATH for regular boot" if INITOPT
+	default "/bin:/sbin:/usr/bin:/usr/sbin"
+	help
+		Default PATH used during normal operation
+
+    config TARGET_INIT_ENV
+        string
+	    prompt "Environment variables to set when starting init (start with none)" if INITOPT
+	    default ""
+	    help
+		Should be a space seperated list of variable assignments.  These
+		variables will be present in the environment.  Spaces may not be
+		present (including through expansion) even in a quoted string
+        	(env doesn't understanding quoting).
+
+    config TARGET_INIT_CMD
+        string
+	    prompt "Init command" if INITOPT
+            default "/sbin/init"
+	    help
+		The executable to run as the init process.  Is 'exec'd by
+		preinit (which is the init that the kernel launches on boot).
+
+    config TARGET_INIT_SUPPRESS_STDERR
+        bool
+        prompt "Suppress stderr messages of init" if INITOPT
+        default y
+	help
+		Prevents showing stderr messages for init command if not already
+		suppressed during preinit.  This is the default behaviour in 
+		previous versions of OpenWRT.  Removing this does nothing if 
+		stderr is suppressed during preinit (which is the default).

+ 6 - 2
scripts/metadata.pl

@@ -562,20 +562,24 @@ EOF
 
 sub gen_package_config() {
 	parse_package_metadata($ARGV[0]) or exit 1;
-	print "menuconfig UCI_PRECONFIG\n\tbool \"Image configuration\"\n" if %preconfig;
+	print "menuconfig IMAGEOPT\n\tbool \"Image configuration\"\n\tdefault n\n";
 	foreach my $preconfig (keys %preconfig) {
 		foreach my $cfg (keys %{$preconfig{$preconfig}}) {
 			my $conf = $preconfig{$preconfig}->{$cfg}->{id};
 			$conf =~ tr/\.-/__/;
 			print <<EOF
 	config UCI_PRECONFIG_$conf
-		string "$preconfig{$preconfig}->{$cfg}->{label}" if UCI_PRECONFIG
+		string "$preconfig{$preconfig}->{$cfg}->{label}" if IMAGEOPT
 		depends PACKAGE_$preconfig
 		default "$preconfig{$preconfig}->{$cfg}->{default}"
 
 EOF
 		}
 	}
+	print "source \"package/*/image-config.in\"\n";
+	if (scalar glob "package/feeds/*/*/image-config.in") {
+	    print "source \"package/feeds/*/*/image-config.in\"\n";
+	}
 	print_package_features();
 	print_package_config_category 'Base system';
 	foreach my $cat (keys %category) {

+ 0 - 16
target/linux/adm5120/base-files/etc/preinit.arch

@@ -1,16 +0,0 @@
-. /etc/functions.sh
-
-insmod input-core
-insmod input-polldev
-insmod gpio_buttons
-insmod button-hotplug
-
-ifname=eth0
-
-failsafe_ip
-
-netmsg 192.168.1.255 "Press reset now, to enter Failsafe!"
-
-ifconfig "$ifname" 0.0.0.0 down
-
-sleep 2

+ 12 - 0
target/linux/adm5120/base-files/lib/preinit/05_enable_leds_adm5120

@@ -0,0 +1,12 @@
+#!/bin/sh
+
+enable_leds() {
+	insmod input-core
+	insmod input-polldev
+	insmod gpio_buttons
+	insmod button-hotplug
+}
+
+boot_hook_add preinit_main enable_leds
+
+

+ 9 - 0
target/linux/adm5120/base-files/lib/preinit/05_set_preinit_face_adm5120

@@ -0,0 +1,9 @@
+#!/bin/sh
+
+set_preinit_ifname() {
+	ifname=eth0
+}
+
+boot_hook_add preinit_main set_preinit_ifname
+
+

+ 16 - 0
target/linux/ar71xx/base-files/lib/preinit/05_enable_leds_ar71xx

@@ -0,0 +1,16 @@
+#
+# Copyright (C) 2009 OpenWrt.org
+#
+
+. /lib/ar71xx.sh
+
+
+preinit_enable_leds() {
+	insmod input-core
+	insmod input-polldev
+	insmod gpio_buttons
+	insmod button-hotplug
+}
+
+boot_hook_add preinit_main preinit_enable_leds
+

+ 1 - 18
target/linux/ar71xx/base-files/etc/preinit.arch → target/linux/ar71xx/base-files/lib/preinit/05_set_iface_mac_ar71xx

@@ -20,13 +20,6 @@ fetch_mac_from_mtd() {
 	[ ! -z $mac ] && ifconfig eth1 hw ether $mac 2>/dev/null
 }
 
-preinit_load_modules() {
-	insmod input-core
-	insmod input-polldev
-	insmod gpio_buttons
-	insmod button-hotplug
-}
-
 preinit_set_mac_address() {
 	case $(ar71xx_board_name) in
 		tew-632brp)
@@ -42,15 +35,5 @@ preinit_set_mac_address() {
 	esac
 }
 
-preinit_set_mac_address
-preinit_load_modules
-
-ifname=eth0
-
-failsafe_ip
-
-netmsg 192.168.1.255 "Press any button now, to enter Failsafe!"
-
-ifconfig "$ifname" 0.0.0.0 down
+boot_hook_add preinit_main preinit_set_mac_address
 
-sleep 2

+ 16 - 0
target/linux/ar71xx/base-files/lib/preinit/05_set_preinit_iface_ar71xx

@@ -0,0 +1,16 @@
+#!/bin/sh
+
+#
+# Copyright (C) 2009 OpenWrt.org
+#
+
+. /lib/ar71xx.sh
+
+set_preinit_iface() {
+	ifname=eth0
+}
+
+boot_hook_add preinit_main set_preinit_iface
+
+
+

+ 0 - 21
target/linux/atheros/base-files/etc/preinit.arch

@@ -1,21 +0,0 @@
-# reset button only supported on ar5315+ at the moment
-grep 'Atheros AR231[567]' /proc/cpuinfo > /dev/null && {
-	if [ -e "/sys/bus/mdio_bus/drivers/Infineon ADM6996/0:00" -o \
-	     -e "/sys/bus/mdio_bus/drivers/Marvell 88E6060/0:10" ]; then
-		vconfig set_name_type DEV_PLUS_VID_NO_PAD
-		ifconfig eth0 up
-		vconfig add eth0 1
-		ifname=eth0.1
-	else
-		ifname=eth0
-	fi
-	failsafe_ip
-	netmsg 192.168.1.255 "Press reset now, to enter Failsafe!"
-	sleep 2
-	if [ -z "$FAILSAFE" -a \
-		 \( -e "/sys/bus/mdio_bus/drivers/Infineon ADM6996/0:00" -o \
-	        -e "/sys/bus/mdio_bus/drivers/Marvell 88E6060/0:10" \) ]; then
-		vconfig rem eth0.1
-		ifconfig eth0 down
-	fi
-}

+ 32 - 0
target/linux/atheros/base-files/lib/preinit/15_preinit_iface_atheros

@@ -0,0 +1,32 @@
+#!/bin/sh
+
+# reset button only supported on ar5315+ at the moment
+preinit_ip() {
+	if [ -z "$pi_ifname" ]; then
+		grep 'Atheros AR231[567]' /proc/cpuinfo > /dev/null && {
+			if [ -e "/sys/bus/mdio_bus/drivers/Infineon ADM6996/0:00" -o \
+			     -e "/sys/bus/mdio_bus/drivers/Marvell 88E6060/0:10" ]; then
+				vconfig set_name_type DEV_PLUS_VID_NO_PAD
+				ifconfig eth0 up		
+				vconfig add eth0 1
+				ifname=eth0.1
+			else
+				ifname=eth0
+			fi
+		pi_ifname=$ifname
+	}
+	[ -n "$pi_ifname" ] && grep "$pi_ifname" /proc/net/dev >/dev/null && {
+		ifconfig $pi_ifname $pi_ip netmask $pi_netmask broadcast $pi_broadcast up
+    	}
+}
+
+
+# reset button only supported on ar5315+ at the moment
+
+preinit_ip_deconfig() {
+	if [ -e "/sys/bus/mdio_bus/drivers/Infineon ADM6996/0:00" -o \
+	        -e "/sys/bus/mdio_bus/drivers/Marvell 88E6060/0:10" ]; then
+		vconfig rem eth0.1 2>/dev/null
+		ifconfig $pi_ifname down
+	fi
+}

+ 0 - 71
target/linux/brcm-2.4/base-files/etc/preinit.arch

@@ -1,71 +0,0 @@
-. /etc/functions.sh
-
-failsafe() {
-	lock /tmp/.failsafe
-	failsafe_ip
-
-	echo "0 1 2 3 4 ${cpu_port:-5u*}" > /proc/switch/eth0/vlan/0/ports
-	
-	set_state failsafe
-	[ -x "/usr/sbin/nvram" ] && {
-		[ "$(nvram get boot_wait)" != "on" ] && {
-			nvram set boot_wait=on
-			nvram commit
-		}
-	}
-
-	netmsg 192.168.1.255 "Entering Failsafe!"
-	telnetd -l /bin/login.sh <> /dev/null 2>&1
-
-	ash --login
-}
-
-mount none /proc -t proc
-
-insmod diag
-set_state preinit
-echo '/sbin/hotplug.failsafe' > /proc/sys/kernel/hotplug
-
-ifname=eth0
-
-# hardware specific overrides
-case "$(cat /proc/diag/model)" in
-	"Linksys WAP54G V1") ifname=eth1;;
-	"ASUS WL-HDD") ifname=eth1;;
-	"ASUS WL-300g") ifname=eth1;;
-	"ASUS (unknown, BCM4702)") ifname=eth1;;
-	"Sitecom WL-105b") ifname=eth1;;
-esac
-
-check_module () {
-  module="$1"; shift; params="$*"
-
-  insmod "$module" "$params"
-  sleep 1
-  grep "^$module" /proc/modules
-  return $?
-}
-
-check_module tg3
-failsafe_ip
-
-insmod switch-core
-check_module switch-robo || check_module switch-adm  || { 
-  check_module bcm57xx activate_gpio=0x4 && cpu_port="8u*"
-} || rmmod switch-core
-
-[ -d /proc/switch/eth0 ] && {
-	echo 1 > /proc/switch/eth0/reset
-
-	# this would be easier if we blasted the message across all ports
-	# but we don't want packets leaking across interfaces
-	for port in $(seq 0 4); do {
-		echo "$port ${cpu_port:-5u*}" > /proc/switch/eth0/vlan/0/ports
-		netmsg 192.168.1.255 "Press reset now, to enter Failsafe!"
-	}; done
-} || netmsg 192.168.1.255 "Press reset now, to enter Failsafe!"
-
-ifconfig $ifname 0.0.0.0 down
-
-sleep 2
-

+ 9 - 0
target/linux/brcm-2.4/base-files/lib/preinit/03_init_hotplug_failsafe_brcm-2.4

@@ -0,0 +1,9 @@
+#!/bin/sh
+
+init_hotplug_failsafe() {
+	echo '/sbin/hotplug.failsafe' > /proc/sys/kernel/hotplug
+}
+
+boot_hook_add preinit_main init_hotplug_failsafe
+
+

+ 38 - 0
target/linux/brcm-2.4/base-files/lib/preinit/05_init_interfaces_brcm-2.4

@@ -0,0 +1,38 @@
+#!/bin/sh
+
+set_preinit_iface() {
+	ifname=eth0
+
+	insmod diag
+
+	# hardware specific overrides
+	case "$(cat /proc/diag/model)" in
+		"Linksys WAP54G V1") ifname=eth1;;
+		"ASUS WL-HDD") ifname=eth1;;
+		"ASUS WL-300g") ifname=eth1;;
+		"ASUS (unknown, BCM4702)") ifname=eth1;;
+		"Sitecom WL-105b") ifname=eth1;;
+	esac
+}
+
+check_module () {
+  module="$1"; shift; params="$*"
+
+  insmod "$module" "$params"
+  sleep 1
+  grep "^$module" /proc/modules
+  return $?
+}
+
+init_iface() {
+	check_module tg3
+	insmod switch-core
+	check_module switch-robo || check_module switch-adm  || { 
+	  check_module bcm57xx activate_gpio=0x4 && cpu_port="8u*"
+	} || rmmod switch-core
+}
+
+boot_hook_add preinit_main init_iface
+boot_hook_add preinit_main set_preinit_iface
+
+

+ 15 - 0
target/linux/brcm-2.4/base-files/lib/preinit/05_set_failsafe_switch_brcm-2.4

@@ -0,0 +1,15 @@
+#!/bin/sh
+
+failsafe_ip() {
+	[ -d /proc/switch/eth0 ] && [ "$ifname" = "eth0" ] && {
+		ifconfig eth0 0.0.0.0 down
+		echo "0 1 2 3 4 ${cpu_port:-5u*}" > /proc/switch/eth0/vlan/0/ports
+	}
+	
+	[ -n "$pi_ifname" ] && grep "$pi_ifname" /proc/net/dev >/dev/null && {
+        	ifconfig $pi_ifname $pi_ip netmask $pi_netmask broadcast $pi_broadcast up
+        }
+}
+
+boot_hook_add failsafe failsafe_ip
+

+ 6 - 0
target/linux/brcm-2.4/base-files/lib/preinit/15_mount_proc_brcm-2.4

@@ -0,0 +1,6 @@
+#!/bin/sh
+
+do_mount_procfs() {
+	mount none /proc -t proc
+}
+

+ 38 - 0
target/linux/brcm-2.4/base-files/lib/preinit/15_set_preinit_interface_brcm-2.4

@@ -0,0 +1,38 @@
+. /etc/functions.sh
+
+port_net_echo() {
+    [ -n "$pi_ifname" ] && grep "$pi_ifname" /proc/net/dev >/dev/null ] {
+        if [ "$pi_preinit_net_messages" = "y" ] || [ "$pi_failsafe_net_message"  = "true"] && [ "$pi_preinit_no_failsafe_netmsg" != "y" ]; then
+                netmsg $pi_broadcast "$1"
+        }
+    }
+}
+
+preinit_ip_deconfig() {
+	if [ -z "$pi_ifname" ]; then
+		ifconfig $ifname 0.0.0.0 down
+	else
+		grep "$pi_ifname" /proc/net/dev >/dev/null && {
+	        ifconfig $pi_ifname 0.0.0.0 down
+	    }
+	fi
+}
+
+preinit_net_echo() {
+	preinit_ip
+	
+	[ -d /proc/switch/eth0 ] && [ "$pi_ifname" = "eth0" ] && {
+		echo 1 > /proc/switch/eth0/reset
+
+		# this would be easier if we blasted the message across all ports
+		# but we don't want packets leaking across interfaces
+		for port in $(seq 0 4); do {
+			echo "$port ${cpu_port:-5u*}" > /proc/switch/eth0/vlan/0/ports
+			port_net_echo $1
+		}; done
+	} || port_net_echo $1
+
+	preinit_ip_deconfig
+}
+
+

+ 14 - 0
target/linux/brcm-2.4/base-files/lib/preinit/20_failsafe_set_boot_wait_brcm-2.4

@@ -0,0 +1,14 @@
+#!/bin/sh
+
+. /etc/functions.sh
+
+set_boot_wait() {
+	[ -x "/usr/sbin/nvram" ] && {
+		[ "$(nvram get boot_wait)" != "on" ] && {
+			nvram set boot_wait=on
+			nvram commit
+		}
+	}
+}
+
+boot_hook_add failsafe set_boot_wait

+ 0 - 6
target/linux/gemini/base-files/etc/preinit.arch

@@ -1,6 +0,0 @@
-CONFIG_PARTITION="$(grep "VCTL" /proc/mtd | cut -d: -f1)"
-MAC1="$(strings /dev/$CONFIG_PARTITION |grep MAC|cut -d: -f2|cut -c3-14|sed -e 's,\(..\),:\1,g' -e 's,^:,,')"
-MAC2="$(strings /dev/$CONFIG_PARTITION |grep MAC|cut -d: -f8|cut -c3-14|sed -e 's,\(..\),:\1,g' -e 's,^:,,')"
-
-ifconfig eth0 hw ether $MAC1 2>/dev/null
-ifconfig eth1 hw ether $MAC2 2>/dev/null

+ 13 - 0
target/linux/gemini/base-files/lib/preinit/05_set_ether_mac_gemini

@@ -0,0 +1,13 @@
+#!/bin/sh
+
+set_ether_mac() {
+	CONFIG_PARTITION="$(grep "VCTL" /proc/mtd | cut -d: -f1)"
+	MAC1="$(strings /dev/$CONFIG_PARTITION |grep MAC|cut -d: -f2|cut -c3-14|sed -e 's,\(..\),:\1,g' -e 's,^:,,')"
+	MAC2="$(strings /dev/$CONFIG_PARTITION |grep MAC|cut -d: -f8|cut -c3-14|sed -e 's,\(..\),:\1,g' -e 's,^:,,')"
+
+	ifconfig eth0 hw ether $MAC1 2>/dev/null
+	ifconfig eth1 hw ether $MAC2 2>/dev/null
+}
+
+boot_hook_add preinit_main set_ether_mac
+

+ 0 - 5
target/linux/ifxmips/nfs/base-files/etc/preinit.arch

@@ -1,5 +0,0 @@
-
-grep "/dev/root" /proc/mounts |grep -q nfs && {
-	echo "- init nfs -"
-	exec /sbin/init
-}

+ 12 - 0
target/linux/ifxmips/nfs/base-files/lib/preinit/01_init_nfs_ifxmips

@@ -0,0 +1,12 @@
+#!/bin/sh
+
+
+init_nfs() {
+	grep "/dev/root" /proc/mounts |grep -q nfs && {
+		echo "- init nfs -"
+		exec /sbin/init
+	}
+}
+
+boot_hook_add preinit_main init_nfs
+

+ 0 - 15
target/linux/ixp4xx/base-files/etc/preinit.arch

@@ -1,15 +0,0 @@
-RB_CONFIG="$(grep "RedBoot config" /proc/mtd | cut -d: -f1)"
-
-for npe in eth0 eth1 eth2
-do
-	if [ "$(ifconfig $npe 2>/dev/null | grep -c 00:00:00:00:00:00)" == "1" ]; then
-		ifconfig $npe hw ether $(fconfig -s -r -d /dev/$RB_CONFIG -n npe_"$npe"_esa)
-	fi
-done
-
-# Some developers should be shot on sight at Zcom/Netgear
-# -- Fixup for the WG302v1, need someone with a WAG302v1 to fix that, too
-
-if [ "$(ifconfig eth0 2>/dev/null | grep -c 00:00:00:00:00:00)" == "1" ]; then
-		ifconfig eth0 hw ether $(fconfig -s -r -d /dev/$RB_CONFIG -n zcom_npe_esa)
-fi

+ 23 - 0
target/linux/ixp4xx/base-files/lib/preinit/05_set_ether_mac_ixp4xx

@@ -0,0 +1,23 @@
+#!/bin/sh
+
+set_ether_mac() {
+
+	RB_CONFIG="$(grep "RedBoot config" /proc/mtd | cut -d: -f1)"
+
+	for npe in eth0 eth1 eth2
+	do
+		if [ "$(ifconfig $npe 2>/dev/null | grep -c 00:00:00:00:00:00)" == "1" ]; then
+			ifconfig $npe hw ether $(fconfig -s -r -d /dev/$RB_CONFIG -n npe_"$npe"_esa)
+		fi
+	done
+
+	# Some developers should be shot on sight at Zcom/Netgear
+	# -- Fixup for the WG302v1, need someone with a WAG302v1 to fix that, too
+
+	if [ "$(ifconfig eth0 2>/dev/null | grep -c 00:00:00:00:00:00)" == "1" ]; then
+			ifconfig eth0 hw ether $(fconfig -s -r -d /dev/$RB_CONFIG -n zcom_npe_esa)
+	fi
+}
+
+boot_hook_add preinit_main set_ether_mac
+

+ 0 - 2
target/linux/pxcab/base-files/etc/preinit.arch

@@ -1,2 +0,0 @@
-mount -t proc none /proc
-grep 'failsafe=' /proc/cmdline > /dev/null && export FAILSAFE=true

+ 8 - 0
target/linux/pxcab/base-files/lib/preinit/15_essential_fs_pxcab

@@ -0,0 +1,8 @@
+#!/bin/sh
+
+#!/bin/sh
+
+do_mount_procfs() {
+	mount -t proc none /proc
+}
+

+ 14 - 0
target/linux/pxcab/base-files/lib/preinit/45_failsafe_pxcab

@@ -0,0 +1,14 @@
+#!/bin/sh
+# Copyright (C) 2006-2010 OpenWrt.org
+# Copyright (C) 2010 Vertical Communications
+
+failsafe_wait() {
+    FAILSAFE=
+    grep 'failsafe=' /proc/cmdline > /dev/null && FAILSAFE=true && export FAILSAFE
+    if [ "$FAILSAFE" != "true" ]; then
+        preinit_net_echo "Please press button now to enter failsafe"
+        echo -n "Press CTRL-C or "
+        fs_wait_for_key f 'to enter failsafe mode' $fs_failsafe_wait_timeout && FAILSAFE=true && export FAILSAFE
+    fi
+}
+

+ 0 - 23
target/linux/rdc/base-files/etc/preinit.arch

@@ -1,23 +0,0 @@
-# Copyright (C) 2009 OpenWrt.org
-
-. /etc/functions.sh
-
-insmod r6040
-
-kernel=$(grep magic /proc/mtd | awk -F: '{print $1}')
-[ -n $kernel ] && hdr=$(dd if=/dev/$kernel count=4 bs=1 2> /dev/null)
-[ "$hdr" = "CSYS" ] && config=$(find_mtd_part config)
-if [ -n "$config" ]; then
-	mac0=$(hexdump -n 6 -e '6/1 ":%X"' -s 24583 $config | cut -c2-)
-	[ "$mac0" = "FF:FF:FF:FF:FF:FF" -o "$mac0" = "0:0:0:0:0:0" ] && unset mac0
-	mac1=$(hexdump -n 6 -e '6/1 ":%X"' -s 24589 $config | cut -c2-)
-	[ "$mac1" = "FF:FF:FF:FF:FF:FF" -o "$mac0" = "0:0:0:0:0:0" ] && unset mac1
-fi
-if [ -n $mac0 ]; then
-	logger -t kernel -p user.info "r6040: Setting MAC for eth0 to $mac0"
-	ifconfig eth0 hw ether $mac0
-fi
-if [ -n $mac1 ]; then
-	logger -t kernel -p user.info "r6040: Setting MAC for eth1 to $mac1"
-	ifconfig eth1 hw ether $mac1
-fi

+ 32 - 0
target/linux/rdc/base-files/lib/preinit/05_set_ether_mac_rdc

@@ -0,0 +1,32 @@
+#!/bin/sh
+# Copyright (C) 2009 OpenWrt.org
+
+. /etc/boot.sh
+
+r6040_module() {
+	insmod r6040
+}
+
+set_ether_mac() {
+	kernel=$(grep magic /proc/mtd | awk -F: '{print $1}')
+	[ -n $kernel ] && hdr=$(dd if=/dev/$kernel count=4 bs=1 2> /dev/null)
+	[ "$hdr" = "CSYS" ] && config=$(find_mtd_part config)
+	if [ -n "$config" ]; then
+		mac0=$(hexdump -n 6 -e '6/1 ":%X"' -s 24583 $config | cut -c2-)
+		[ "$mac0" = "FF:FF:FF:FF:FF:FF" -o "$mac0" = "0:0:0:0:0:0" ] && unset mac0
+		mac1=$(hexdump -n 6 -e '6/1 ":%X"' -s 24589 $config | cut -c2-)
+		[ "$mac1" = "FF:FF:FF:FF:FF:FF" -o "$mac0" = "0:0:0:0:0:0" ] && unset mac1
+	fi
+	if [ -n $mac0 ]; then
+		logger -t kernel -p user.info "r6040: Setting MAC for eth0 to $mac0"
+		ifconfig eth0 hw ether $mac0
+	fi
+	if [ -n $mac1 ]; then
+		logger -t kernel -p user.info "r6040: Setting MAC for eth1 to $mac1"
+		ifconfig eth1 hw ether $mac1
+	fi
+}
+
+boot_hook_add preinit_main r6040_module
+boot_hook_add preinit_main set_ether_mac
+

+ 0 - 2
target/linux/x86/base-files/etc/preinit.arch

@@ -1,2 +0,0 @@
-mount -t proc none /proc
-grep 'failsafe=' /proc/cmdline > /dev/null && export FAILSAFE=true

+ 8 - 0
target/linux/x86/base-files/lib/preinit/15_essential_fs_x86

@@ -0,0 +1,8 @@
+#!/bin/sh
+# Copyright (C) 2006-2010 OpenWrt.org
+# Copyright (C) 2010 Vertical Communications
+
+do_mount_procfs() {
+	mount -t proc none /proc
+}
+

+ 14 - 0
target/linux/x86/base-files/lib/preinit/45_failsafe_x86

@@ -0,0 +1,14 @@
+#!/bin/sh
+# Copyright (C) 2006-2010 OpenWrt.org
+# Copyright (C) 2010 Vertical Communications
+
+failsafe_wait() {
+    FAILSAFE=
+    grep 'failsafe=' /proc/cmdline > /dev/null && FAILSAFE=true && export FAILSAFE
+    if [ "$FAILSAFE" != "true" ]; then
+        preinit_net_echo "Please press button now to enter failsafe"
+        echo -n "Press CTRL-C or "
+        fs_wait_for_key f 'to enter failsafe mode' $fs_failsafe_wait_timeout && FAILSAFE=true && export FAILSAFE
+    fi
+}
+

+ 0 - 2
target/linux/x86/olpc/base-files/etc/preinit.arch

@@ -1,2 +0,0 @@
-mount -t proc none /proc
-grep 'failsafe=' /proc/cmdline && export FAILSAFE=true

+ 9 - 0
target/linux/x86/olpc/base-files/lib/preinit/15_essential_fs_x86

@@ -0,0 +1,9 @@
+#!/bin/sh
+# Copyright (C) 2006-2010 OpenWrt.org
+# Copyright (C) 2010 Vertical Communications
+
+do_mount_procfs() {
+	mount -t proc none /proc
+}
+
+

+ 14 - 0
target/linux/x86/olpc/base-files/lib/preinit/45_failsafe_x86

@@ -0,0 +1,14 @@
+#!/bin/sh
+# Copyright (C) 2006-2010 OpenWrt.org
+# Copyright (C) 2010 Vertical Communications
+
+failsafe_wait() {
+    FAILSAFE=
+    grep 'failsafe=' /proc/cmdline > /dev/null && FAILSAFE=true && export FAILSAFE
+    if [ "$FAILSAFE" != "true" ]; then
+        preinit_net_echo "Please press button now to enter failsafe"
+        echo -n "Press CTRL-C or "
+        fs_wait_for_key f 'to enter failsafe mode' $fs_failsafe_wait_timeout && FAILSAFE=true && export FAILSAFE
+    fi
+}
+