Mykola Kostenok | 4d17df6 | 2017-09-01 09:56:45 +0300 | [diff] [blame^] | 1 | #!/bin/sh |
| 2 | |
| 3 | fslist="proc sys dev run" |
| 4 | rodir=run/initramfs/ro |
| 5 | rwdir=run/initramfs/rw |
| 6 | upper=$rwdir/cow |
| 7 | work=$rwdir/work |
| 8 | |
| 9 | cd / |
| 10 | mkdir -p $fslist |
| 11 | mount dev dev -tdevtmpfs |
| 12 | mount sys sys -tsysfs |
| 13 | mount proc proc -tproc |
| 14 | if ! grep run proc/mounts |
| 15 | then |
| 16 | mount tmpfs run -t tmpfs -o mode=755,nodev |
| 17 | fi |
| 18 | |
| 19 | mkdir -p $rodir $rwdir |
| 20 | |
| 21 | cp -rp init failsafe recovery shutdown update update_all 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 | |
| 26 | findmtd() { |
| 27 | m=$(grep -xl "$1" /sys/class/mtd/*/name) |
| 28 | m=${m%/name} |
| 29 | m=${m##*/} |
| 30 | echo $m |
| 31 | } |
| 32 | |
| 33 | blkid_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 | |
| 42 | probe_fs_type() { |
| 43 | fst=$(blkid_fs_type $1) |
| 44 | echo ${fst:=jffs2} |
| 45 | } |
| 46 | |
| 47 | # This fw_get_env_var is a possibly broken version of fw_printenv that |
| 48 | # does not check the crc or flag byte. |
| 49 | # 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. |
| 52 | |
| 53 | get_fw_env_var() { |
| 54 | # 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=2 |
| 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 | envdev=$(findmtd u-boot-env) |
| 68 | if test -n $envdev |
| 69 | then |
| 70 | cat /dev/$envdev | |
| 71 | tr '\n\000' '\r\n' | |
| 72 | tail -c +5 | tail -c +${copies-1} | |
| 73 | sed -ne '/^$/,$d' -e "s/^$1=//p" |
| 74 | fi |
| 75 | } |
| 76 | |
| 77 | setup_resolv() { |
| 78 | runresolv=/run/systemd/resolve/resolv.conf |
| 79 | etcresolv=/etc/resolv.conf |
| 80 | |
| 81 | if test ! -e $etcresolv -a ! -L $etcresolv |
| 82 | then |
| 83 | mkdir -p ${runresolv%/*} |
| 84 | ln -s $runresolv $etcresolv |
| 85 | fi |
| 86 | if test ! -f $runresolv |
| 87 | then |
| 88 | cat /proc/net/pnp > $runresolv |
| 89 | fi |
| 90 | |
| 91 | return 0 |
| 92 | } |
| 93 | |
| 94 | try_tftp() { |
| 95 | # split into tftp:// host:port/ path/on/remote |
| 96 | # then spilt off / and then :port from the end of host:port/ |
| 97 | # and : from the beginning of port |
| 98 | |
| 99 | rest="${1#tftp://}" |
| 100 | path=${rest#*/} |
| 101 | host=${rest%$path} |
| 102 | host="${host%/}" |
| 103 | port="${host#${host%:*}}" |
| 104 | host="${host%$port}" |
| 105 | port="${port#:}" |
| 106 | |
| 107 | setup_resolv |
| 108 | |
| 109 | if test -z "$host" -o -z "$path" |
| 110 | then |
| 111 | debug_takeover "Invalid tftp download url '$url'." |
| 112 | elif echo "Downloading '$url' from $host ..." && |
| 113 | ! tftp -g -r "$path" -l /run/image-rofs "$host" ${port+"$port"} |
| 114 | then |
| 115 | debug_takeover "Download of '$url' failed." |
| 116 | fi |
| 117 | } |
| 118 | |
| 119 | try_wget() { |
| 120 | setup_resolv |
| 121 | |
| 122 | echo "Downloading '$1' ..." |
| 123 | if ! wget -O /run/image-rofs "$1" |
| 124 | then |
| 125 | debug_takeover "Download of '$url' failed." |
| 126 | fi |
| 127 | } |
| 128 | |
| 129 | debug_takeover() { |
| 130 | echo "$@" |
| 131 | test -n "$@" && echo Enter password to try to manually fix. |
| 132 | cat << HERE |
| 133 | After fixing run exit to continue this script, or reboot -f to retry, or |
| 134 | touch /takeover and exit to become PID 1 allowing editing of this script. |
| 135 | HERE |
| 136 | |
| 137 | while ! sulogin && ! test -f /takeover |
| 138 | do |
| 139 | echo getty failed, retrying |
| 140 | done |
| 141 | |
| 142 | # Touch /takeover in the above getty to become pid 1 |
| 143 | if test -e /takeover |
| 144 | then |
| 145 | cat << HERE |
| 146 | |
| 147 | Takeover of init requested. Executing /bin/sh as PID 1. |
| 148 | When finished exec new init or cleanup and run reboot -f. |
| 149 | |
| 150 | Warning: No job control! Shell exit will panic the system! |
| 151 | HERE |
| 152 | export PS1=init#\ |
| 153 | exec /bin/sh |
| 154 | fi |
| 155 | } |
| 156 | |
| 157 | env=$(findmtd u-boot-env) |
| 158 | if test -n $env |
| 159 | then |
| 160 | ln -s /dev/$env /run/mtd:u-boot-env |
| 161 | cp /run/mtd:u-boot-env /run/fw_env |
| 162 | fi |
| 163 | |
| 164 | rofs=$(findmtd rofs) |
| 165 | rwfs=$(findmtd rwfs) |
| 166 | |
| 167 | rodev=/dev/mtdblock${rofs#mtd} |
| 168 | rwdev=/dev/mtdblock${rwfs#mtd} |
| 169 | |
| 170 | # Set to y for yes, anything else for no. |
| 171 | force_rwfst_jffs2=y |
| 172 | flash_images_before_init=n |
| 173 | consider_download_files=y |
| 174 | consider_download_tftp=y |
| 175 | consider_download_http=y |
| 176 | consider_download_ftp=y |
| 177 | |
| 178 | rofst=squashfs |
| 179 | rwfst=$(probe_fs_type $rwdev) |
| 180 | roopts=ro |
| 181 | rwopts=rw |
| 182 | |
| 183 | image=/run/initramfs/image- |
| 184 | trigger=${image}rwfs |
| 185 | |
| 186 | init=/sbin/init |
| 187 | failsafe=/run/initramfs/failsafe |
| 188 | fsckbase=/sbin/fsck. |
| 189 | fsck=$fsckbase$rwfst |
| 190 | fsckopts=-a |
| 191 | optfile=/run/initramfs/init-options |
| 192 | optbase=/run/initramfs/init-options-base |
| 193 | urlfile=/run/initramfs/init-download-url |
| 194 | update=/run/initramfs/update |
| 195 | |
| 196 | if test -e /${optfile##*/} |
| 197 | then |
| 198 | cp /${optfile##*/} $optfile |
| 199 | fi |
| 200 | |
| 201 | if test -e /${optbase##*/} |
| 202 | then |
| 203 | cp /${optbase##*/} $optbase |
| 204 | else |
| 205 | touch $optbase |
| 206 | fi |
| 207 | |
| 208 | if test ! -f $optfile |
| 209 | then |
| 210 | cat /proc/cmdline $optbase > $optfile |
| 211 | get_fw_env_var openbmcinit >> $optfile |
| 212 | get_fw_env_var openbmconce >> $optfile |
| 213 | fi |
| 214 | |
| 215 | echo rofs = $rofs $rofst rwfs = $rwfs $rwfst |
| 216 | |
| 217 | if grep -w debug-init-sh $optfile |
| 218 | then |
| 219 | debug_takeover "Debug initial shell requested by command line." |
| 220 | fi |
| 221 | |
| 222 | if test "x$consider_download_files" = xy && |
| 223 | grep -w openbmc-init-download-files $optfile |
| 224 | then |
| 225 | if test -f ${urlfile##*/} |
| 226 | then |
| 227 | cp ${urlfile##*/} $urlfile |
| 228 | fi |
| 229 | if test ! -f $urlfile |
| 230 | then |
| 231 | get_fw_env_var openbmcinitdownloadurl > $urlfile |
| 232 | fi |
| 233 | url="$(cat $urlfile)" |
| 234 | rest="${url#*://}" |
| 235 | proto="${url%$rest}" |
| 236 | |
| 237 | if test -z "$url" |
| 238 | then |
| 239 | echo "Download url empty. Ignoring download request." |
| 240 | elif test -z "$proto" |
| 241 | then |
| 242 | echo "Download failed." |
| 243 | elif test "$proto" = tftp:// |
| 244 | then |
| 245 | if test "x$consider_download_tftp" = xy |
| 246 | then |
| 247 | try_tftp "$url" |
| 248 | else |
| 249 | echo "Download failed." |
| 250 | fi |
| 251 | elif test "$proto" = http:// |
| 252 | then |
| 253 | if test "x$consider_download_http" = xy |
| 254 | then |
| 255 | try_wget "$url" |
| 256 | else |
| 257 | echo "Download failed." |
| 258 | fi |
| 259 | elif test "$proto" = ftp:// |
| 260 | then |
| 261 | if test "x$consider_download_ftp" = xy |
| 262 | then |
| 263 | try_wget "$url" |
| 264 | else |
| 265 | echo "Download failed." |
| 266 | fi |
| 267 | else |
| 268 | echo "Download failed." |
| 269 | fi |
| 270 | fi |
| 271 | |
| 272 | # If there are images in root move them to /run/initramfs/ or /run/ now. |
| 273 | imagebasename=${image##*/} |
| 274 | if test -n "${imagebasename}" && ls /${imagebasename}* > /dev/null 2>&1 |
| 275 | then |
| 276 | if test "x$flash_images_before_init" = xy |
| 277 | then |
| 278 | echo "Flash images found, will update before starting init." |
| 279 | mv /${imagebasename}* ${image%$imagebasename} |
| 280 | else |
| 281 | echo "Flash images found, will use but deferring flash update." |
| 282 | mv /${imagebasename}* /run/ |
| 283 | fi |
| 284 | fi |
| 285 | |
| 286 | if grep -w clean-rwfs-filesystem $optfile |
| 287 | then |
| 288 | echo "Cleaning of read-write overlay filesystem requested." |
| 289 | touch $trigger |
| 290 | fi |
| 291 | |
| 292 | if test "x$force_rwfst_jffs2" = xy -a $rwfst != jffs2 -a ! -f $trigger |
| 293 | then |
| 294 | echo "Converting read-write overlay filesystem to jffs2 forced." |
| 295 | touch $trigger |
| 296 | fi |
| 297 | |
| 298 | if ls $image* > /dev/null 2>&1 |
| 299 | then |
| 300 | if ! test -x $update |
| 301 | then |
| 302 | debug_takeover "Flash update requested but $update missing!" |
| 303 | elif test -f $trigger -a ! -s $trigger |
| 304 | then |
| 305 | echo "Saving selected files from read-write overlay filesystem." |
| 306 | $update --no-restore-files |
| 307 | echo "Clearing read-write overlay filesystem." |
| 308 | flash_eraseall /dev/$rwfs |
| 309 | echo "Restoring saved files to read-write overlay filesystem." |
| 310 | touch $trigger |
| 311 | $update --no-save-files --clean-saved-files |
| 312 | else |
| 313 | $update --clean-saved-files |
| 314 | fi |
| 315 | |
| 316 | rwfst=$(probe_fs_type $rwdev) |
| 317 | fsck=$fsckbase$rwfst |
| 318 | fi |
| 319 | |
| 320 | if grep -w overlay-filesystem-in-ram $optfile |
| 321 | then |
| 322 | rwfst=none |
| 323 | fi |
| 324 | |
| 325 | copyfiles= |
| 326 | if grep -w copy-files-to-ram $optfile |
| 327 | then |
| 328 | rwfst=none |
| 329 | copyfiles=y |
| 330 | fi |
| 331 | |
| 332 | # It would be nice to do this after fsck but that mean rofs is mounted |
| 333 | # which triggers the mtd is mounted check |
| 334 | if test "$rwfst$copyfiles" = noney |
| 335 | then |
| 336 | touch $trigger |
| 337 | $update --copy-files --clean-saved-files --no-restore-files |
| 338 | fi |
| 339 | |
| 340 | if grep -w copy-base-filesystem-to-ram $optfile && |
| 341 | test ! -e /run/image-rofs && ! cp $rodev /run/image-rofs |
| 342 | then |
| 343 | # Remove any partial copy to avoid attempted usage later |
| 344 | if test -e /run/image-rofs |
| 345 | then |
| 346 | ls -l /run/image-rofs |
| 347 | rm -f /run/image-rofs |
| 348 | fi |
| 349 | debug_takeover "Copying $rodev to /run/image-rofs failed." |
| 350 | fi |
| 351 | |
| 352 | if test -s /run/image-rofs |
| 353 | then |
| 354 | rodev=/run/image-rofs |
| 355 | roopts=$roopts,loop |
| 356 | fi |
| 357 | |
| 358 | mount $rodev $rodir -t $rofst -o $roopts |
| 359 | |
| 360 | if test -x $rodir$fsck |
| 361 | then |
| 362 | for fs in $fslist |
| 363 | do |
| 364 | mount --bind $fs $rodir/$fs |
| 365 | done |
| 366 | chroot $rodir $fsck $fsckopts $rwdev |
| 367 | rc=$? |
| 368 | for fs in $fslist |
| 369 | do |
| 370 | umount $rodir/$fs |
| 371 | done |
| 372 | if test $rc -gt 1 |
| 373 | then |
| 374 | debug_takeover "fsck of read-write fs on $rwdev failed (rc=$rc)" |
| 375 | fi |
| 376 | elif test "$rwfst" != jffs2 -a "$rwfst" != none |
| 377 | then |
| 378 | echo "No '$fsck' in read only fs, skipping fsck." |
| 379 | fi |
| 380 | |
| 381 | if test "$rwfst" = none |
| 382 | then |
| 383 | echo "Running with read-write overlay in RAM for this boot." |
| 384 | echo "No state will be preserved unless flash update performed." |
| 385 | elif ! mount $rwdev $rwdir -t $rwfst -o $rwopts |
| 386 | then |
| 387 | msg="$(cat)" << HERE |
| 388 | |
| 389 | Mounting read-write $rwdev filesystem failed. Please fix and run |
| 390 | mount $rwdev $rwdir -t $rwfst -o $rwopts |
| 391 | to to continue, or do change nothing to run from RAM for this boot. |
| 392 | HERE |
| 393 | debug_takeover "$msg" |
| 394 | fi |
| 395 | |
| 396 | rm -rf $work |
| 397 | mkdir -p $upper $work |
| 398 | |
| 399 | mount -t overlay -o lowerdir=$rodir,upperdir=$upper,workdir=$work cow /root |
| 400 | |
| 401 | while ! chroot /root /bin/sh -c "test -x '$init' -a -s '$init'" |
| 402 | do |
| 403 | msg="$(cat)" << HERE |
| 404 | |
| 405 | Unable to confirm /sbin/init is an executable non-empty file |
| 406 | in merged file system mounted at /root. |
| 407 | |
| 408 | Change Root test failed! Invoking emergency shell. |
| 409 | HERE |
| 410 | debug_takeover "$msg" |
| 411 | done |
| 412 | |
| 413 | for f in $fslist |
| 414 | do |
| 415 | mount --move $f root/$f |
| 416 | done |
| 417 | |
| 418 | # switch_root /root $init |
| 419 | ln -s /root/dev/mem /dev/mem |
| 420 | FLASH_CP=`/root/sbin/devmem 0x1e785030` |
| 421 | FLASH_CP=$(($FLASH_CP&0x02)) |
| 422 | rm /dev/mem |
| 423 | |
| 424 | if [ $FLASH_CP == 0 ]; then |
| 425 | # echo "Flash 1 used. Normal boot."; |
| 426 | exec chroot /root $init; |
| 427 | else |
| 428 | exec chroot /root $failsafe; |
| 429 | fi |
| 430 | |