blob: 814ee07d1dd5cc2b4608852eaeb8b58a718d871a [file] [log] [blame]
Milton D. Miller IId89d5e02016-01-20 14:57:54 -06001#!/bin/sh
2
Milton Miller54d882e2016-02-05 12:07:53 -06003fslist="proc sys dev run"
Milton D. Miller IId89d5e02016-01-20 14:57:54 -06004rodir=run/initramfs/ro
5rwdir=run/initramfs/rw
6upper=$rwdir/cow
7work=$rwdir/work
8
9cd /
Milton Miller54d882e2016-02-05 12:07:53 -060010mkdir -p $fslist
Milton D. Miller IId89d5e02016-01-20 14:57:54 -060011mount dev dev -tdevtmpfs
12mount sys sys -tsysfs
13mount proc proc -tproc
14if ! grep run proc/mounts
15then
16 mount tmpfs run -t tmpfs -o mode=755,nodev
17fi
18
19mkdir -p $rodir $rwdir
20
21cp -rp init shutdown update whitelist bin sbin usr lib etc var run/initramfs
22
23# To start a interactive shell with job control at this point, run
24# getty 38400 ttyS4
25
26findmtd() {
27 m=$(grep -xl "$1" /sys/class/mtd/*/name)
28 m=${m%/name}
29 m=${m##*/}
30 echo $m
31}
32
Andrew Jefferyacc2c852016-02-23 23:47:23 +103033blkid_fs_type() {
34 # Emulate util-linux's `blkid -s TYPE -o value $1`
35 # Example busybox blkid output:
36 # # blkid /dev/mtdblock5
37 # /dev/mtdblock5: TYPE="squashfs"
38 # Process output to extract TYPE value "squashfs".
39 blkid $1 | sed -e 's/^.*TYPE="//' -e 's/".*$//'
40}
41
42probe_fs_type() {
43 fst=$(blkid_fs_type $1)
44 echo ${fst:=jffs2}
45}
46
Milton D. Miller II04d2aff2016-03-02 15:23:22 -060047# This fw_get_env_var is a simple but slightly broken version of fw_printenv:
48# The u-boot environemnt starts with a crc32, followed by a flag byte
49# when a redundannt environment is configured, followed by var=value\0 sets.
50# The flag byte for nand is a 1 byte counter; for nor it is a 1 or 0 byte.
51# The crc and/or nand flag byte can contain printable characters and be
52# considered part of the first string and parsed as part of the variable
53# name. In addition a variable could have a "\n" embedded in it, this code
54# would split that variable. Ignore for now, the last set var is at the end.
55
56get_fw_env_var() {
57 strings /run/fw_env | sed -ne "s/^$1=//p"
58}
59
Milton Miller06ccb1a2016-02-05 13:04:29 -060060debug_takeover() {
61 echo "$@"
62 test -n "$@" && echo Enter password to try to manually fix.
63 cat << HERE
64After fixing run exit to continue this script, or reboot -f to retry, or
65touch /takeover and exit to become PID 1 allowing editing of this script.
66HERE
67
68 while ! sulogin && ! test -f /takeover
69 do
70 echo getty failed, retrying
71 done
72
73 # Touch /takeover in the above getty to become pid 1
74 if test -e /takeover
75 then
76 cat << HERE
77
78Takeover of init requested. Executing /bin/sh as PID 1.
79When finished exec new init or cleanup and run reboot -f.
80
81Warning: No job control! Shell exit will panic the system!
82HERE
83 export PS1=init#\
84 exec /bin/sh
85 fi
86}
87
Milton D. Miller IIbf7bfd42016-01-27 20:18:16 -060088env=$(findmtd u-boot-env)
89if test -n $env
90then
91 ln -s /dev/$env /run/mtd:u-boot-env
92 cp /run/mtd:u-boot-env /run/fw_env
93fi
94
Milton D. Miller IId89d5e02016-01-20 14:57:54 -060095rofs=$(findmtd rofs)
96rwfs=$(findmtd rwfs)
97
Milton Miller54d882e2016-02-05 12:07:53 -060098rodev=/dev/mtdblock${rofs#mtd}
99rwdev=/dev/mtdblock${rwfs#mtd}
100
Milton Miller0b0d5fe2016-02-23 21:48:48 -0600101# Set to y for yes, anything else for no.
102force_rwfst_jffs2=y
103flash_images_before_init=n
104
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600105rofst=squashfs
Andrew Jefferyacc2c852016-02-23 23:47:23 +1030106rwfst=$(probe_fs_type $rwdev)
Milton Miller54d882e2016-02-05 12:07:53 -0600107roopts=ro
108rwopts=rw
109
Milton Miller0b0d5fe2016-02-23 21:48:48 -0600110image=/run/initramfs/image-
111trigger=${image}rwfs
112
Milton Miller54d882e2016-02-05 12:07:53 -0600113init=/sbin/init
Milton Miller0b0d5fe2016-02-23 21:48:48 -0600114fsckbase=/sbin/fsck.
115fsck=$fsckbase$rwfst
Milton Millerb72114c2016-02-06 16:05:06 -0600116fsckopts=-a
Milton D. Miller II1650db52016-02-27 15:48:52 -0600117optfile=/run/initramfs/init-options
Milton D. Miller IIdbc11372016-02-29 11:35:14 -0600118update=/run/initramfs/update
Milton D. Miller II1650db52016-02-27 15:48:52 -0600119
Milton D. Miller IId2977202016-03-07 07:59:09 -0600120if test -e /${optfile##*/}
121then
122 cp /${optfile##*/} $optfile
123fi
124
Milton D. Miller II1650db52016-02-27 15:48:52 -0600125if test ! -f $optfile
126then
127 cat /proc/cmdline > $optfile
Milton D. Miller II04d2aff2016-03-02 15:23:22 -0600128 get_fw_env_var openbmcinit >> $optfile
129 get_fw_env_var openbmconce >> $optfile
Milton D. Miller II1650db52016-02-27 15:48:52 -0600130fi
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600131
132echo rofs = $rofs $rofst rwfs = $rwfs $rwfst
133
Milton D. Miller II1650db52016-02-27 15:48:52 -0600134if grep -w debug-init-sh $optfile
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600135then
Milton Miller06ccb1a2016-02-05 13:04:29 -0600136 debug_takeover "Debug initial shell requested by command line."
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600137fi
138
Milton D. Miller II1aaffe82016-02-27 17:01:13 -0600139# If there are images in root move them to /run/initramfs/ or /run/ now.
Milton Miller0b0d5fe2016-02-23 21:48:48 -0600140imagebasename=${image##*/}
Milton D. Miller II1aaffe82016-02-27 17:01:13 -0600141if test -n "${imagebasename}" && ls /${imagebasename}* > /dev/null 2>&1
Milton Miller0b0d5fe2016-02-23 21:48:48 -0600142then
Milton D. Miller II1aaffe82016-02-27 17:01:13 -0600143 if test "x$flash_images_before_init" = xy
144 then
145 echo "Flash images found, will update before starting init."
146 mv /${imagebasename}* ${image%$imagebasename}
147 else
148 echo "Flash images found, will use but deferring flash update."
149 mv /${imagebasename}* /run/
150 fi
Milton Miller0b0d5fe2016-02-23 21:48:48 -0600151fi
152
Milton D. Miller II1650db52016-02-27 15:48:52 -0600153if grep -w clean-rwfs-filesystem $optfile
Milton Miller0b0d5fe2016-02-23 21:48:48 -0600154then
155 echo "Cleaning of read-write overlay filesystem requested."
156 touch $trigger
157fi
158
159if test "x$force_rwfst_jffs2" = xy -a $rwfst != jffs2 -a ! -f $trigger
160then
161 echo "Converting read-write overlay filesystem to jffs2 forced."
162 touch $trigger
163fi
164
165if ls $image* > /dev/null 2>&1
166then
Milton D. Miller IIdbc11372016-02-29 11:35:14 -0600167 if ! test -x $update
Milton Miller0b0d5fe2016-02-23 21:48:48 -0600168 then
Milton D. Miller IIdbc11372016-02-29 11:35:14 -0600169 debug_takeover "Flash update requested but $update missing!"
Milton Miller0b0d5fe2016-02-23 21:48:48 -0600170 elif test -f $trigger -a ! -s $trigger
171 then
172 echo "Saving selected files from read-write overlay filesystem."
Milton D. Miller IIdbc11372016-02-29 11:35:14 -0600173 $update --no-restore-files
Milton Miller0b0d5fe2016-02-23 21:48:48 -0600174 echo "Clearing read-write overlay filesystem."
175 flash_eraseall /dev/$rwfs
176 echo "Restoring saved files to read-write overlay filesystem."
177 touch $trigger
Milton D. Miller IIdbc11372016-02-29 11:35:14 -0600178 $update --no-save-files --clean-saved-files
Milton Miller0b0d5fe2016-02-23 21:48:48 -0600179 else
Milton D. Miller IIdbc11372016-02-29 11:35:14 -0600180 $update --clean-saved-files
Milton Miller0b0d5fe2016-02-23 21:48:48 -0600181 fi
182
183 rwfst=$(probe_fs_type $rwdev)
184 fsck=$fsckbase$rwfst
185fi
186
Milton D. Miller IIedfd12c2016-02-27 17:31:39 -0600187if grep -w overlay-filesystem-in-ram $optfile
188then
189 rwfst=none
190fi
191
Milton D. Miller II2728b732016-02-29 12:10:34 -0600192copyfiles=
193if grep -w copy-files-to-ram $optfile
194then
195 rwfst=none
196 copyfiles=y
197fi
198
199# It would be nice to do this after fsck but that mean rofs is mounted
200# which triggers the mtd is mounted check
201if test "$rwfst$copyfiles" = noney
202then
203 touch $trigger
204 $update --copy-files --clean-saved-files --no-restore-files
205fi
206
Milton D. Miller IIc201b042016-02-27 17:31:39 -0600207if grep -w copy-base-filesystem-to-ram $optfile &&
208 test ! -e /run/image-rofs && ! cp $rodev /run/image-rofs
209then
210 # Remove any partial copy to avoid attempted usage later
211 if test -e /run/image-rofs
212 then
213 ls -l /run/image-rofs
214 rm -f /run/image-rofs
215 fi
216 debug_takeover "Copying $rodev to /run/image-rofs failed."
217fi
218
Milton D. Miller IIa36e99c2016-02-27 16:04:32 -0600219if test -s /run/image-rofs
220then
221 rodev=/run/image-rofs
222 roopts=$roopts,loop
223fi
224
Milton Millerb72114c2016-02-06 16:05:06 -0600225mount $rodev $rodir -t $rofst -o $roopts
226
227if test -x $rodir$fsck
228then
229 for fs in $fslist
230 do
231 mount --bind $fs $rodir/$fs
232 done
233 chroot $rodir $fsck $fsckopts $rwdev
234 rc=$?
235 for fs in $fslist
236 do
237 umount $rodir/$fs
238 done
239 if test $rc -gt 1
240 then
241 debug_takeover "fsck of read-write fs on $rwdev failed (rc=$rc)"
242 fi
Milton D. Miller II6dba31f2016-02-27 16:38:16 -0600243elif test "$rwfst" != jffs2 -a "$rwfst" != none
Milton Millerf81c1082016-02-23 21:00:06 -0600244then
Milton Millerb72114c2016-02-06 16:05:06 -0600245 echo "No '$fsck' in read only fs, skipping fsck."
246fi
247
Milton D. Miller II6dba31f2016-02-27 16:38:16 -0600248if test "$rwfst" = none
249then
250 echo "Running with read-write overlay in RAM for this boot."
251 echo "No state will be preserved unless flash update performed."
252elif ! mount $rwdev $rwdir -t $rwfst -o $rwopts
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600253then
Milton Miller06ccb1a2016-02-05 13:04:29 -0600254 msg="$(cat)" << HERE
255
256Mounting read-write $rwdev filesystem failed. Please fix and run
Andrew Jefferyed5fb272016-02-17 17:19:58 +1030257 mount $rwdev $rwdir -t $rwfst -o $rwopts
Milton Miller06ccb1a2016-02-05 13:04:29 -0600258to to continue, or do change nothing to run from RAM for this boot.
259HERE
260 debug_takeover "$msg"
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600261fi
262
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600263rm -rf $work
Milton Millerb72114c2016-02-06 16:05:06 -0600264mkdir -p $upper $work
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600265
266mount -t overlay -o lowerdir=$rodir,upperdir=$upper,workdir=$work cow /root
267
Milton Miller06ccb1a2016-02-05 13:04:29 -0600268while ! chroot /root /bin/sh -c "test -x '$init' -a -s '$init'"
269do
270 msg="$(cat)" << HERE
271
272Unable to confirm /sbin/init is an executable non-empty file
273in merged file system mounted at /root.
274
275Change Root test failed! Invoking emergency shell.
276HERE
277 debug_takeover "$msg"
278done
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600279
Milton Miller54d882e2016-02-05 12:07:53 -0600280for f in $fslist
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600281do
282 mount --move $f root/$f
283done
284
Milton Miller54d882e2016-02-05 12:07:53 -0600285# switch_root /root $init
286exec chroot /root $init
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600287