blob: 70af93d991f251d937c69fa085021c7dc4c9990a [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 IIee30a912016-03-07 17:46:28 -060047# This fw_get_env_var is a possibly broken version of fw_printenv that
48# does not check the crc or flag byte.
Milton D. Miller II04d2aff2016-03-02 15:23:22 -060049# The u-boot environemnt starts with a crc32, followed by a flag byte
50# when a redundannt environment is configured, followed by var=value\0 sets.
51# The flag byte for nand is a 1 byte counter; for nor it is a 1 or 0 byte.
Milton D. Miller II04d2aff2016-03-02 15:23:22 -060052
53get_fw_env_var() {
Milton D. Miller IIee30a912016-03-07 17:46:28 -060054 # do we have 1 or 2 copies of the environment?
55 # count non-blank non-comment lines
56 # copies=$(grep -v ^# /etc/fw_env.config | grep -c [::alnum::])
57 # ... we could if we had the fw_env.config in the initramfs
58 copies=1
59
60 # * Change \n to \r and \0 to \n
61 # * Skip to the 5th byte to skip over crc
62 # * then skip to the first or 2nd byte to skip over flag if it exists
63 # * stop parsing at first empty line corresponding to the
64 # double \0 at the end of the environment.
65 # * print the value of the variable name passed as argument
66
67 cat /run/fw_env |
68 tr '\n\000' '\r\n' |
69 tail -c +5 | tail -c +${copies-1} |
70 sed -ne '/^$/,$d' -e "s/^$1=//p"
Milton D. Miller II04d2aff2016-03-02 15:23:22 -060071}
72
Milton D. Miller II9be688c2016-03-02 18:28:54 -060073setup_resolv() {
74 runresolv=/run/systemd/resolve/resolv.conf
75 etcresolv=/etc/resolv.conf
76
77 if test ! -e $etcresolv -a ! -L $etcresolv
78 then
79 mkdir -p ${runresolv%/*}
80 ln -s $runresolv $etcresolv
81 fi
82 if test ! -f $runresolv
83 then
84 cat /proc/net/pnp > $runresolv
85 fi
86
87 return 0
88}
89
90try_tftp() {
91 # split into tftp:// host:port/ path/on/remote
92 # then spilt off / and then :port from the end of host:port/
93 # and : from the beginning of port
94
95 rest="${1#tftp://}"
96 path=${rest#*/}
97 host=${rest%$path}
98 host="${host%/}"
99 port="${host#${host%:*}}"
100 host="${host%$port}"
101 port="${port#:}"
102
103 setup_resolv
104
105 if test -z "$host" -o -z "$path"
106 then
107 debug_takeover "Invalid tftp download url '$url'."
108 elif echo "Downloading '$url' from $host ..." &&
109 ! tftp -g -r "$path" -l /run/image-rofs "$host" ${port+"$port"}
110 then
111 debug_takeover "Download of '$url' failed."
112 fi
113}
114
115try_wget() {
116 setup_resolv
117
118 echo "Downloading '$1' ..."
119 if ! wget -O /run/image-rofs "$1"
120 then
121 debug_takeover "Download of '$url' failed."
122 fi
123}
124
Milton Miller06ccb1a2016-02-05 13:04:29 -0600125debug_takeover() {
126 echo "$@"
127 test -n "$@" && echo Enter password to try to manually fix.
128 cat << HERE
129After fixing run exit to continue this script, or reboot -f to retry, or
130touch /takeover and exit to become PID 1 allowing editing of this script.
131HERE
132
133 while ! sulogin && ! test -f /takeover
134 do
135 echo getty failed, retrying
136 done
137
138 # Touch /takeover in the above getty to become pid 1
139 if test -e /takeover
140 then
141 cat << HERE
142
143Takeover of init requested. Executing /bin/sh as PID 1.
144When finished exec new init or cleanup and run reboot -f.
145
146Warning: No job control! Shell exit will panic the system!
147HERE
148 export PS1=init#\
149 exec /bin/sh
150 fi
151}
152
Milton D. Miller IIbf7bfd42016-01-27 20:18:16 -0600153env=$(findmtd u-boot-env)
154if test -n $env
155then
156 ln -s /dev/$env /run/mtd:u-boot-env
157 cp /run/mtd:u-boot-env /run/fw_env
158fi
159
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600160rofs=$(findmtd rofs)
161rwfs=$(findmtd rwfs)
162
Milton Miller54d882e2016-02-05 12:07:53 -0600163rodev=/dev/mtdblock${rofs#mtd}
164rwdev=/dev/mtdblock${rwfs#mtd}
165
Milton Miller0b0d5fe2016-02-23 21:48:48 -0600166# Set to y for yes, anything else for no.
167force_rwfst_jffs2=y
168flash_images_before_init=n
Milton D. Miller II9be688c2016-03-02 18:28:54 -0600169consider_download_files=y
170consider_download_tftp=y
171consider_download_http=y
172consider_download_ftp=y
Milton Miller0b0d5fe2016-02-23 21:48:48 -0600173
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600174rofst=squashfs
Andrew Jefferyacc2c852016-02-23 23:47:23 +1030175rwfst=$(probe_fs_type $rwdev)
Milton Miller54d882e2016-02-05 12:07:53 -0600176roopts=ro
177rwopts=rw
178
Milton Miller0b0d5fe2016-02-23 21:48:48 -0600179image=/run/initramfs/image-
180trigger=${image}rwfs
181
Milton Miller54d882e2016-02-05 12:07:53 -0600182init=/sbin/init
Milton Miller0b0d5fe2016-02-23 21:48:48 -0600183fsckbase=/sbin/fsck.
184fsck=$fsckbase$rwfst
Milton Millerb72114c2016-02-06 16:05:06 -0600185fsckopts=-a
Milton D. Miller II1650db52016-02-27 15:48:52 -0600186optfile=/run/initramfs/init-options
Milton Millerad025a82016-05-25 18:52:04 -0500187optbase=/run/initramfs/init-options-base
Milton D. Miller II9be688c2016-03-02 18:28:54 -0600188urlfile=/run/initramfs/init-download-url
Milton D. Miller IIdbc11372016-02-29 11:35:14 -0600189update=/run/initramfs/update
Milton D. Miller II1650db52016-02-27 15:48:52 -0600190
Milton D. Miller IId2977202016-03-07 07:59:09 -0600191if test -e /${optfile##*/}
192then
193 cp /${optfile##*/} $optfile
194fi
195
Milton Millerad025a82016-05-25 18:52:04 -0500196if test -e /${optbase##*/}
197then
198 cp /${optbase##*/} $optbase
199else
200 touch $optbase
201fi
202
Milton D. Miller II1650db52016-02-27 15:48:52 -0600203if test ! -f $optfile
204then
Milton Millerad025a82016-05-25 18:52:04 -0500205 cat /proc/cmdline $optbase > $optfile
Milton D. Miller II04d2aff2016-03-02 15:23:22 -0600206 get_fw_env_var openbmcinit >> $optfile
207 get_fw_env_var openbmconce >> $optfile
Milton D. Miller II1650db52016-02-27 15:48:52 -0600208fi
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600209
210echo rofs = $rofs $rofst rwfs = $rwfs $rwfst
211
Milton D. Miller II1650db52016-02-27 15:48:52 -0600212if grep -w debug-init-sh $optfile
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600213then
Milton Miller06ccb1a2016-02-05 13:04:29 -0600214 debug_takeover "Debug initial shell requested by command line."
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600215fi
216
Milton D. Miller II9be688c2016-03-02 18:28:54 -0600217if test "x$consider_download_files" = xy &&
218 grep -w openbmc-init-download-files $optfile
219then
220 if test -f ${urlfile##*/}
221 then
222 cp ${urlfile##*/} $urlfile
223 fi
224 if test ! -f $urlfile
225 then
226 get_fw_env_var openbmcinitdownloadurl > $urlfile
227 fi
228 url="$(cat $urlfile)"
229 rest="${url#*://}"
230 proto="${url%$rest}"
231
232 if test -z "$url"
233 then
234 echo "Download url empty. Ignoring download request."
235 elif test -z "$proto"
236 then
237 echo "Download failed."
238 elif test "$proto" = tftp://
239 then
240 if test "x$consider_download_tftp" = xy
241 then
242 try_tftp "$url"
243 else
244 echo "Download failed."
245 fi
246 elif test "$proto" = http://
247 then
248 if test "x$consider_download_http" = xy
249 then
250 try_wget "$url"
251 else
252 echo "Download failed."
253 fi
254 elif test "$proto" = ftp://
255 then
256 if test "x$consider_download_ftp" = xy
257 then
258 try_wget "$url"
259 else
260 echo "Download failed."
261 fi
262 else
263 echo "Download failed."
264 fi
265fi
266
Milton D. Miller II1aaffe82016-02-27 17:01:13 -0600267# If there are images in root move them to /run/initramfs/ or /run/ now.
Milton Miller0b0d5fe2016-02-23 21:48:48 -0600268imagebasename=${image##*/}
Milton D. Miller II1aaffe82016-02-27 17:01:13 -0600269if test -n "${imagebasename}" && ls /${imagebasename}* > /dev/null 2>&1
Milton Miller0b0d5fe2016-02-23 21:48:48 -0600270then
Milton D. Miller II1aaffe82016-02-27 17:01:13 -0600271 if test "x$flash_images_before_init" = xy
272 then
273 echo "Flash images found, will update before starting init."
274 mv /${imagebasename}* ${image%$imagebasename}
275 else
276 echo "Flash images found, will use but deferring flash update."
277 mv /${imagebasename}* /run/
278 fi
Milton Miller0b0d5fe2016-02-23 21:48:48 -0600279fi
280
Milton D. Miller II1650db52016-02-27 15:48:52 -0600281if grep -w clean-rwfs-filesystem $optfile
Milton Miller0b0d5fe2016-02-23 21:48:48 -0600282then
283 echo "Cleaning of read-write overlay filesystem requested."
284 touch $trigger
285fi
286
287if test "x$force_rwfst_jffs2" = xy -a $rwfst != jffs2 -a ! -f $trigger
288then
289 echo "Converting read-write overlay filesystem to jffs2 forced."
290 touch $trigger
291fi
292
293if ls $image* > /dev/null 2>&1
294then
Milton D. Miller IIdbc11372016-02-29 11:35:14 -0600295 if ! test -x $update
Milton Miller0b0d5fe2016-02-23 21:48:48 -0600296 then
Milton D. Miller IIdbc11372016-02-29 11:35:14 -0600297 debug_takeover "Flash update requested but $update missing!"
Milton Miller0b0d5fe2016-02-23 21:48:48 -0600298 elif test -f $trigger -a ! -s $trigger
299 then
300 echo "Saving selected files from read-write overlay filesystem."
Milton D. Miller IIdbc11372016-02-29 11:35:14 -0600301 $update --no-restore-files
Milton Miller0b0d5fe2016-02-23 21:48:48 -0600302 echo "Clearing read-write overlay filesystem."
303 flash_eraseall /dev/$rwfs
304 echo "Restoring saved files to read-write overlay filesystem."
305 touch $trigger
Milton D. Miller IIdbc11372016-02-29 11:35:14 -0600306 $update --no-save-files --clean-saved-files
Milton Miller0b0d5fe2016-02-23 21:48:48 -0600307 else
Milton D. Miller IIdbc11372016-02-29 11:35:14 -0600308 $update --clean-saved-files
Milton Miller0b0d5fe2016-02-23 21:48:48 -0600309 fi
310
311 rwfst=$(probe_fs_type $rwdev)
312 fsck=$fsckbase$rwfst
313fi
314
Milton D. Miller IIedfd12c2016-02-27 17:31:39 -0600315if grep -w overlay-filesystem-in-ram $optfile
316then
317 rwfst=none
318fi
319
Milton D. Miller II2728b732016-02-29 12:10:34 -0600320copyfiles=
321if grep -w copy-files-to-ram $optfile
322then
323 rwfst=none
324 copyfiles=y
325fi
326
327# It would be nice to do this after fsck but that mean rofs is mounted
328# which triggers the mtd is mounted check
329if test "$rwfst$copyfiles" = noney
330then
331 touch $trigger
332 $update --copy-files --clean-saved-files --no-restore-files
333fi
334
Milton D. Miller IIc201b042016-02-27 17:31:39 -0600335if grep -w copy-base-filesystem-to-ram $optfile &&
336 test ! -e /run/image-rofs && ! cp $rodev /run/image-rofs
337then
338 # Remove any partial copy to avoid attempted usage later
339 if test -e /run/image-rofs
340 then
341 ls -l /run/image-rofs
342 rm -f /run/image-rofs
343 fi
344 debug_takeover "Copying $rodev to /run/image-rofs failed."
345fi
346
Milton D. Miller IIa36e99c2016-02-27 16:04:32 -0600347if test -s /run/image-rofs
348then
349 rodev=/run/image-rofs
350 roopts=$roopts,loop
351fi
352
Milton Millerb72114c2016-02-06 16:05:06 -0600353mount $rodev $rodir -t $rofst -o $roopts
354
355if test -x $rodir$fsck
356then
357 for fs in $fslist
358 do
359 mount --bind $fs $rodir/$fs
360 done
361 chroot $rodir $fsck $fsckopts $rwdev
362 rc=$?
363 for fs in $fslist
364 do
365 umount $rodir/$fs
366 done
367 if test $rc -gt 1
368 then
369 debug_takeover "fsck of read-write fs on $rwdev failed (rc=$rc)"
370 fi
Milton D. Miller II6dba31f2016-02-27 16:38:16 -0600371elif test "$rwfst" != jffs2 -a "$rwfst" != none
Milton Millerf81c1082016-02-23 21:00:06 -0600372then
Milton Millerb72114c2016-02-06 16:05:06 -0600373 echo "No '$fsck' in read only fs, skipping fsck."
374fi
375
Milton D. Miller II6dba31f2016-02-27 16:38:16 -0600376if test "$rwfst" = none
377then
378 echo "Running with read-write overlay in RAM for this boot."
379 echo "No state will be preserved unless flash update performed."
380elif ! mount $rwdev $rwdir -t $rwfst -o $rwopts
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600381then
Milton Miller06ccb1a2016-02-05 13:04:29 -0600382 msg="$(cat)" << HERE
383
384Mounting read-write $rwdev filesystem failed. Please fix and run
Andrew Jefferyed5fb272016-02-17 17:19:58 +1030385 mount $rwdev $rwdir -t $rwfst -o $rwopts
Milton Miller06ccb1a2016-02-05 13:04:29 -0600386to to continue, or do change nothing to run from RAM for this boot.
387HERE
388 debug_takeover "$msg"
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600389fi
390
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600391rm -rf $work
Milton Millerb72114c2016-02-06 16:05:06 -0600392mkdir -p $upper $work
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600393
394mount -t overlay -o lowerdir=$rodir,upperdir=$upper,workdir=$work cow /root
395
Milton Miller06ccb1a2016-02-05 13:04:29 -0600396while ! chroot /root /bin/sh -c "test -x '$init' -a -s '$init'"
397do
398 msg="$(cat)" << HERE
399
400Unable to confirm /sbin/init is an executable non-empty file
401in merged file system mounted at /root.
402
403Change Root test failed! Invoking emergency shell.
404HERE
405 debug_takeover "$msg"
406done
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600407
Milton Miller54d882e2016-02-05 12:07:53 -0600408for f in $fslist
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600409do
410 mount --move $f root/$f
411done
412
Milton Miller54d882e2016-02-05 12:07:53 -0600413# switch_root /root $init
414exec chroot /root $init
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600415