blob: 29c72cf29f21bd0ac673c23cbd34de5fcb147c02 [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
120if test ! -f $optfile
121then
122 cat /proc/cmdline > $optfile
Milton D. Miller II04d2aff2016-03-02 15:23:22 -0600123 get_fw_env_var openbmcinit >> $optfile
124 get_fw_env_var openbmconce >> $optfile
Milton D. Miller II1650db52016-02-27 15:48:52 -0600125fi
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600126
127echo rofs = $rofs $rofst rwfs = $rwfs $rwfst
128
Milton D. Miller II1650db52016-02-27 15:48:52 -0600129if grep -w debug-init-sh $optfile
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600130then
Milton Miller06ccb1a2016-02-05 13:04:29 -0600131 debug_takeover "Debug initial shell requested by command line."
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600132fi
133
Milton D. Miller II1aaffe82016-02-27 17:01:13 -0600134# If there are images in root move them to /run/initramfs/ or /run/ now.
Milton Miller0b0d5fe2016-02-23 21:48:48 -0600135imagebasename=${image##*/}
Milton D. Miller II1aaffe82016-02-27 17:01:13 -0600136if test -n "${imagebasename}" && ls /${imagebasename}* > /dev/null 2>&1
Milton Miller0b0d5fe2016-02-23 21:48:48 -0600137then
Milton D. Miller II1aaffe82016-02-27 17:01:13 -0600138 if test "x$flash_images_before_init" = xy
139 then
140 echo "Flash images found, will update before starting init."
141 mv /${imagebasename}* ${image%$imagebasename}
142 else
143 echo "Flash images found, will use but deferring flash update."
144 mv /${imagebasename}* /run/
145 fi
Milton Miller0b0d5fe2016-02-23 21:48:48 -0600146fi
147
Milton D. Miller II1650db52016-02-27 15:48:52 -0600148if grep -w clean-rwfs-filesystem $optfile
Milton Miller0b0d5fe2016-02-23 21:48:48 -0600149then
150 echo "Cleaning of read-write overlay filesystem requested."
151 touch $trigger
152fi
153
154if test "x$force_rwfst_jffs2" = xy -a $rwfst != jffs2 -a ! -f $trigger
155then
156 echo "Converting read-write overlay filesystem to jffs2 forced."
157 touch $trigger
158fi
159
160if ls $image* > /dev/null 2>&1
161then
Milton D. Miller IIdbc11372016-02-29 11:35:14 -0600162 if ! test -x $update
Milton Miller0b0d5fe2016-02-23 21:48:48 -0600163 then
Milton D. Miller IIdbc11372016-02-29 11:35:14 -0600164 debug_takeover "Flash update requested but $update missing!"
Milton Miller0b0d5fe2016-02-23 21:48:48 -0600165 elif test -f $trigger -a ! -s $trigger
166 then
167 echo "Saving selected files from read-write overlay filesystem."
Milton D. Miller IIdbc11372016-02-29 11:35:14 -0600168 $update --no-restore-files
Milton Miller0b0d5fe2016-02-23 21:48:48 -0600169 echo "Clearing read-write overlay filesystem."
170 flash_eraseall /dev/$rwfs
171 echo "Restoring saved files to read-write overlay filesystem."
172 touch $trigger
Milton D. Miller IIdbc11372016-02-29 11:35:14 -0600173 $update --no-save-files --clean-saved-files
Milton Miller0b0d5fe2016-02-23 21:48:48 -0600174 else
Milton D. Miller IIdbc11372016-02-29 11:35:14 -0600175 $update --clean-saved-files
Milton Miller0b0d5fe2016-02-23 21:48:48 -0600176 fi
177
178 rwfst=$(probe_fs_type $rwdev)
179 fsck=$fsckbase$rwfst
180fi
181
Milton D. Miller IIedfd12c2016-02-27 17:31:39 -0600182if grep -w overlay-filesystem-in-ram $optfile
183then
184 rwfst=none
185fi
186
Milton D. Miller II2728b732016-02-29 12:10:34 -0600187copyfiles=
188if grep -w copy-files-to-ram $optfile
189then
190 rwfst=none
191 copyfiles=y
192fi
193
194# It would be nice to do this after fsck but that mean rofs is mounted
195# which triggers the mtd is mounted check
196if test "$rwfst$copyfiles" = noney
197then
198 touch $trigger
199 $update --copy-files --clean-saved-files --no-restore-files
200fi
201
Milton D. Miller IIc201b042016-02-27 17:31:39 -0600202if grep -w copy-base-filesystem-to-ram $optfile &&
203 test ! -e /run/image-rofs && ! cp $rodev /run/image-rofs
204then
205 # Remove any partial copy to avoid attempted usage later
206 if test -e /run/image-rofs
207 then
208 ls -l /run/image-rofs
209 rm -f /run/image-rofs
210 fi
211 debug_takeover "Copying $rodev to /run/image-rofs failed."
212fi
213
Milton D. Miller IIa36e99c2016-02-27 16:04:32 -0600214if test -s /run/image-rofs
215then
216 rodev=/run/image-rofs
217 roopts=$roopts,loop
218fi
219
Milton Millerb72114c2016-02-06 16:05:06 -0600220mount $rodev $rodir -t $rofst -o $roopts
221
222if test -x $rodir$fsck
223then
224 for fs in $fslist
225 do
226 mount --bind $fs $rodir/$fs
227 done
228 chroot $rodir $fsck $fsckopts $rwdev
229 rc=$?
230 for fs in $fslist
231 do
232 umount $rodir/$fs
233 done
234 if test $rc -gt 1
235 then
236 debug_takeover "fsck of read-write fs on $rwdev failed (rc=$rc)"
237 fi
Milton D. Miller II6dba31f2016-02-27 16:38:16 -0600238elif test "$rwfst" != jffs2 -a "$rwfst" != none
Milton Millerf81c1082016-02-23 21:00:06 -0600239then
Milton Millerb72114c2016-02-06 16:05:06 -0600240 echo "No '$fsck' in read only fs, skipping fsck."
241fi
242
Milton D. Miller II6dba31f2016-02-27 16:38:16 -0600243if test "$rwfst" = none
244then
245 echo "Running with read-write overlay in RAM for this boot."
246 echo "No state will be preserved unless flash update performed."
247elif ! mount $rwdev $rwdir -t $rwfst -o $rwopts
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600248then
Milton Miller06ccb1a2016-02-05 13:04:29 -0600249 msg="$(cat)" << HERE
250
251Mounting read-write $rwdev filesystem failed. Please fix and run
Andrew Jefferyed5fb272016-02-17 17:19:58 +1030252 mount $rwdev $rwdir -t $rwfst -o $rwopts
Milton Miller06ccb1a2016-02-05 13:04:29 -0600253to to continue, or do change nothing to run from RAM for this boot.
254HERE
255 debug_takeover "$msg"
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600256fi
257
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600258rm -rf $work
Milton Millerb72114c2016-02-06 16:05:06 -0600259mkdir -p $upper $work
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600260
261mount -t overlay -o lowerdir=$rodir,upperdir=$upper,workdir=$work cow /root
262
Milton Miller06ccb1a2016-02-05 13:04:29 -0600263while ! chroot /root /bin/sh -c "test -x '$init' -a -s '$init'"
264do
265 msg="$(cat)" << HERE
266
267Unable to confirm /sbin/init is an executable non-empty file
268in merged file system mounted at /root.
269
270Change Root test failed! Invoking emergency shell.
271HERE
272 debug_takeover "$msg"
273done
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600274
Milton Miller54d882e2016-02-05 12:07:53 -0600275for f in $fslist
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600276do
277 mount --move $f root/$f
278done
279
Milton Miller54d882e2016-02-05 12:07:53 -0600280# switch_root /root $init
281exec chroot /root $init
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600282