Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
Milton Miller | ba65b7b | 2016-02-05 12:07:53 -0600 | [diff] [blame] | 3 | fslist="proc sys dev run" |
Milton Miller | 9820316 | 2023-08-25 00:00:24 -0500 | [diff] [blame] | 4 | api=run/initramfs |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 5 | |
| 6 | cd / |
Patrick Williams | 51d6af4 | 2023-04-14 14:19:55 -0500 | [diff] [blame] | 7 | # shellcheck disable=SC2086 |
Milton Miller | ba65b7b | 2016-02-05 12:07:53 -0600 | [diff] [blame] | 8 | mkdir -p $fslist |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 9 | mount dev dev -tdevtmpfs |
| 10 | mount sys sys -tsysfs |
| 11 | mount proc proc -tproc |
Milton Miller | 9820316 | 2023-08-25 00:00:24 -0500 | [diff] [blame] | 12 | if grep $api proc/mounts |
| 13 | then |
| 14 | umount $api |
| 15 | fi |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 16 | if ! grep run proc/mounts |
| 17 | then |
| 18 | mount tmpfs run -t tmpfs -o mode=755,nodev |
| 19 | fi |
| 20 | |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 21 | # To start a interactive shell with job control at this point, run |
| 22 | # getty 38400 ttyS4 |
| 23 | |
| 24 | findmtd() { |
| 25 | m=$(grep -xl "$1" /sys/class/mtd/*/name) |
| 26 | m=${m%/name} |
| 27 | m=${m##*/} |
Patrick Williams | 51d6af4 | 2023-04-14 14:19:55 -0500 | [diff] [blame] | 28 | echo "$m" |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 29 | } |
| 30 | |
Andrew Jeffery | 25a5022 | 2016-02-23 23:47:23 +1030 | [diff] [blame] | 31 | blkid_fs_type() { |
| 32 | # Emulate util-linux's `blkid -s TYPE -o value $1` |
| 33 | # Example busybox blkid output: |
| 34 | # # blkid /dev/mtdblock5 |
| 35 | # /dev/mtdblock5: TYPE="squashfs" |
| 36 | # Process output to extract TYPE value "squashfs". |
Patrick Williams | 51d6af4 | 2023-04-14 14:19:55 -0500 | [diff] [blame] | 37 | blkid "$1" | sed -e 's/^.*TYPE="//' -e 's/".*$//' |
Andrew Jeffery | 25a5022 | 2016-02-23 23:47:23 +1030 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | probe_fs_type() { |
Patrick Williams | 51d6af4 | 2023-04-14 14:19:55 -0500 | [diff] [blame] | 41 | fst=$(blkid_fs_type "$1") |
| 42 | echo "${fst:=jffs2}" |
Andrew Jeffery | 25a5022 | 2016-02-23 23:47:23 +1030 | [diff] [blame] | 43 | } |
| 44 | |
Milton D. Miller II | a81cb93 | 2016-03-07 17:46:28 -0600 | [diff] [blame] | 45 | # This fw_get_env_var is a possibly broken version of fw_printenv that |
| 46 | # does not check the crc or flag byte. |
Gunnar Mills | e5b3860 | 2018-08-31 12:39:15 -0500 | [diff] [blame] | 47 | # The u-boot environment starts with a crc32, followed by a flag byte |
Milton D. Miller II | ab61836 | 2016-03-02 15:23:22 -0600 | [diff] [blame] | 48 | # when a redundannt environment is configured, followed by var=value\0 sets. |
| 49 | # The flag byte for nand is a 1 byte counter; for nor it is a 1 or 0 byte. |
Milton D. Miller II | ab61836 | 2016-03-02 15:23:22 -0600 | [diff] [blame] | 50 | |
| 51 | get_fw_env_var() { |
Milton D. Miller II | a81cb93 | 2016-03-07 17:46:28 -0600 | [diff] [blame] | 52 | # do we have 1 or 2 copies of the environment? |
| 53 | # count non-blank non-comment lines |
| 54 | # copies=$(grep -v ^# /etc/fw_env.config | grep -c [::alnum::]) |
| 55 | # ... we could if we had the fw_env.config in the initramfs |
Edward A. James | fd6f07c | 2017-08-15 14:18:12 -0500 | [diff] [blame] | 56 | copies=2 |
Milton D. Miller II | a81cb93 | 2016-03-07 17:46:28 -0600 | [diff] [blame] | 57 | |
| 58 | # * Change \n to \r and \0 to \n |
| 59 | # * Skip to the 5th byte to skip over crc |
| 60 | # * then skip to the first or 2nd byte to skip over flag if it exists |
| 61 | # * stop parsing at first empty line corresponding to the |
| 62 | # double \0 at the end of the environment. |
| 63 | # * print the value of the variable name passed as argument |
| 64 | |
Edward A. James | eb77a8f | 2017-07-14 12:32:44 -0500 | [diff] [blame] | 65 | envdev=$(findmtd u-boot-env) |
Patrick Williams | 51d6af4 | 2023-04-14 14:19:55 -0500 | [diff] [blame] | 66 | if test -n "$envdev" |
Edward A. James | eb77a8f | 2017-07-14 12:32:44 -0500 | [diff] [blame] | 67 | then |
Patrick Williams | 51d6af4 | 2023-04-14 14:19:55 -0500 | [diff] [blame] | 68 | tr '\n\000' '\r\n' < "/dev/$envdev" | |
Edward A. James | eb77a8f | 2017-07-14 12:32:44 -0500 | [diff] [blame] | 69 | tail -c +5 | tail -c +${copies-1} | |
| 70 | sed -ne '/^$/,$d' -e "s/^$1=//p" |
| 71 | fi |
Milton D. Miller II | ab61836 | 2016-03-02 15:23:22 -0600 | [diff] [blame] | 72 | } |
| 73 | |
Milton D. Miller II | 71e7ec1 | 2016-03-02 18:28:54 -0600 | [diff] [blame] | 74 | setup_resolv() { |
| 75 | runresolv=/run/systemd/resolve/resolv.conf |
| 76 | etcresolv=/etc/resolv.conf |
| 77 | |
| 78 | if test ! -e $etcresolv -a ! -L $etcresolv |
| 79 | then |
| 80 | mkdir -p ${runresolv%/*} |
| 81 | ln -s $runresolv $etcresolv |
| 82 | fi |
| 83 | if test ! -f $runresolv |
| 84 | then |
| 85 | cat /proc/net/pnp > $runresolv |
| 86 | fi |
| 87 | |
| 88 | return 0 |
| 89 | } |
| 90 | |
| 91 | try_tftp() { |
| 92 | # split into tftp:// host:port/ path/on/remote |
| 93 | # then spilt off / and then :port from the end of host:port/ |
| 94 | # and : from the beginning of port |
| 95 | |
| 96 | rest="${1#tftp://}" |
| 97 | path=${rest#*/} |
Patrick Williams | 51d6af4 | 2023-04-14 14:19:55 -0500 | [diff] [blame] | 98 | host=${rest%"$path"} |
Milton D. Miller II | 71e7ec1 | 2016-03-02 18:28:54 -0600 | [diff] [blame] | 99 | host="${host%/}" |
Patrick Williams | 51d6af4 | 2023-04-14 14:19:55 -0500 | [diff] [blame] | 100 | port="${host#"${host%:*}"}" |
| 101 | host="${host%"$port"}" |
Milton D. Miller II | 71e7ec1 | 2016-03-02 18:28:54 -0600 | [diff] [blame] | 102 | port="${port#:}" |
| 103 | |
| 104 | setup_resolv |
| 105 | |
| 106 | if test -z "$host" -o -z "$path" |
| 107 | then |
| 108 | debug_takeover "Invalid tftp download url '$url'." |
| 109 | elif echo "Downloading '$url' from $host ..." && |
| 110 | ! tftp -g -r "$path" -l /run/image-rofs "$host" ${port+"$port"} |
| 111 | then |
| 112 | debug_takeover "Download of '$url' failed." |
| 113 | fi |
| 114 | } |
| 115 | |
| 116 | try_wget() { |
| 117 | setup_resolv |
| 118 | |
| 119 | echo "Downloading '$1' ..." |
| 120 | if ! wget -O /run/image-rofs "$1" |
| 121 | then |
| 122 | debug_takeover "Download of '$url' failed." |
| 123 | fi |
| 124 | } |
| 125 | |
Milton Miller | 2250982 | 2016-02-05 13:04:29 -0600 | [diff] [blame] | 126 | debug_takeover() { |
| 127 | echo "$@" |
Adriana Kobylak | 7fd0173 | 2020-03-12 11:12:59 -0500 | [diff] [blame] | 128 | |
| 129 | if ! grep -w enable-initrd-debug-sh "$optfile" |
| 130 | then |
| 131 | echo "Fatal error, triggering kernel panic!" |
| 132 | exit 1 |
| 133 | fi |
| 134 | |
| 135 | test -n "$@" && echo Try to manually fix. |
Milton Miller | 2250982 | 2016-02-05 13:04:29 -0600 | [diff] [blame] | 136 | cat << HERE |
| 137 | After fixing run exit to continue this script, or reboot -f to retry, or |
| 138 | touch /takeover and exit to become PID 1 allowing editing of this script. |
| 139 | HERE |
| 140 | |
Adriana Kobylak | 7fd0173 | 2020-03-12 11:12:59 -0500 | [diff] [blame] | 141 | while ! /bin/sh && ! test -f /takeover |
Milton Miller | 2250982 | 2016-02-05 13:04:29 -0600 | [diff] [blame] | 142 | do |
Adriana Kobylak | 7fd0173 | 2020-03-12 11:12:59 -0500 | [diff] [blame] | 143 | echo /bin/sh failed, retrying |
Milton Miller | 2250982 | 2016-02-05 13:04:29 -0600 | [diff] [blame] | 144 | done |
| 145 | |
Adriana Kobylak | 7fd0173 | 2020-03-12 11:12:59 -0500 | [diff] [blame] | 146 | # Touch /takeover in the above shell to become pid 1 |
Milton Miller | 2250982 | 2016-02-05 13:04:29 -0600 | [diff] [blame] | 147 | if test -e /takeover |
| 148 | then |
| 149 | cat << HERE |
| 150 | |
| 151 | Takeover of init requested. Executing /bin/sh as PID 1. |
| 152 | When finished exec new init or cleanup and run reboot -f. |
| 153 | |
| 154 | Warning: No job control! Shell exit will panic the system! |
| 155 | HERE |
Patrick Williams | 51d6af4 | 2023-04-14 14:19:55 -0500 | [diff] [blame] | 156 | export PS1=init#\ |
Milton Miller | 2250982 | 2016-02-05 13:04:29 -0600 | [diff] [blame] | 157 | exec /bin/sh |
| 158 | fi |
| 159 | } |
| 160 | |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 161 | rofs=$(findmtd rofs) |
| 162 | rwfs=$(findmtd rwfs) |
| 163 | |
Milton Miller | ba65b7b | 2016-02-05 12:07:53 -0600 | [diff] [blame] | 164 | rodev=/dev/mtdblock${rofs#mtd} |
| 165 | rwdev=/dev/mtdblock${rwfs#mtd} |
| 166 | |
Milton Miller | ec0ba3c | 2016-02-23 21:48:48 -0600 | [diff] [blame] | 167 | # Set to y for yes, anything else for no. |
| 168 | force_rwfst_jffs2=y |
| 169 | flash_images_before_init=n |
Milton D. Miller II | 71e7ec1 | 2016-03-02 18:28:54 -0600 | [diff] [blame] | 170 | consider_download_files=y |
| 171 | consider_download_tftp=y |
| 172 | consider_download_http=y |
| 173 | consider_download_ftp=y |
Milton Miller | ec0ba3c | 2016-02-23 21:48:48 -0600 | [diff] [blame] | 174 | |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 175 | rofst=squashfs |
Patrick Williams | 51d6af4 | 2023-04-14 14:19:55 -0500 | [diff] [blame] | 176 | rwfst=$(probe_fs_type "$rwdev") |
Milton Miller | ba65b7b | 2016-02-05 12:07:53 -0600 | [diff] [blame] | 177 | roopts=ro |
| 178 | rwopts=rw |
| 179 | |
Milton Miller | 9820316 | 2023-08-25 00:00:24 -0500 | [diff] [blame] | 180 | image=$api/image- |
| 181 | update=$api/update |
Milton Miller | ec0ba3c | 2016-02-23 21:48:48 -0600 | [diff] [blame] | 182 | trigger=${image}rwfs |
| 183 | |
Milton Miller | ba65b7b | 2016-02-05 12:07:53 -0600 | [diff] [blame] | 184 | init=/sbin/init |
Milton Miller | ec0ba3c | 2016-02-23 21:48:48 -0600 | [diff] [blame] | 185 | fsckbase=/sbin/fsck. |
| 186 | fsck=$fsckbase$rwfst |
Milton Miller | 5c212e4 | 2016-02-06 16:05:06 -0600 | [diff] [blame] | 187 | fsckopts=-a |
Milton Miller | 9820316 | 2023-08-25 00:00:24 -0500 | [diff] [blame] | 188 | |
Milton D. Miller II | 90b6847 | 2016-02-27 15:48:52 -0600 | [diff] [blame] | 189 | optfile=/run/initramfs/init-options |
Milton Miller | 49ce6a1 | 2016-05-25 18:52:04 -0500 | [diff] [blame] | 190 | optbase=/run/initramfs/init-options-base |
Milton D. Miller II | 71e7ec1 | 2016-03-02 18:28:54 -0600 | [diff] [blame] | 191 | urlfile=/run/initramfs/init-download-url |
Milton D. Miller II | 90b6847 | 2016-02-27 15:48:52 -0600 | [diff] [blame] | 192 | |
Milton Miller | e9b9b4c | 2023-08-25 01:02:48 -0500 | [diff] [blame^] | 193 | mkdir -p ${optfile%/*} |
| 194 | |
Milton D. Miller II | 6d85431 | 2016-03-07 07:59:09 -0600 | [diff] [blame] | 195 | if test -e /${optfile##*/} |
| 196 | then |
| 197 | cp /${optfile##*/} $optfile |
| 198 | fi |
| 199 | |
Milton Miller | 49ce6a1 | 2016-05-25 18:52:04 -0500 | [diff] [blame] | 200 | if test -e /${optbase##*/} |
| 201 | then |
| 202 | cp /${optbase##*/} $optbase |
| 203 | else |
| 204 | touch $optbase |
| 205 | fi |
| 206 | |
Milton D. Miller II | 90b6847 | 2016-02-27 15:48:52 -0600 | [diff] [blame] | 207 | if test ! -f $optfile |
| 208 | then |
Milton Miller | 49ce6a1 | 2016-05-25 18:52:04 -0500 | [diff] [blame] | 209 | cat /proc/cmdline $optbase > $optfile |
Milton D. Miller II | ab61836 | 2016-03-02 15:23:22 -0600 | [diff] [blame] | 210 | get_fw_env_var openbmcinit >> $optfile |
| 211 | get_fw_env_var openbmconce >> $optfile |
Milton D. Miller II | 90b6847 | 2016-02-27 15:48:52 -0600 | [diff] [blame] | 212 | fi |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 213 | |
Patrick Williams | 51d6af4 | 2023-04-14 14:19:55 -0500 | [diff] [blame] | 214 | echo "rofs = $rofs $rofst rwfs = $rwfs $rwfst" |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 215 | |
Milton D. Miller II | 90b6847 | 2016-02-27 15:48:52 -0600 | [diff] [blame] | 216 | if grep -w debug-init-sh $optfile |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 217 | then |
Adriana Kobylak | 7fd0173 | 2020-03-12 11:12:59 -0500 | [diff] [blame] | 218 | if grep -w enable-initrd-debug-sh "$optfile" |
| 219 | then |
| 220 | debug_takeover "Debug initial shell requested by command line." |
| 221 | else |
| 222 | echo "Need to also add enable-initrd-debug-sh for debug shell." |
| 223 | fi |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 224 | fi |
| 225 | |
Milton Miller | e9b9b4c | 2023-08-25 01:02:48 -0500 | [diff] [blame^] | 226 | mkdir -p $api |
| 227 | if grep -w mount-bind-run-initramfs $optfile |
| 228 | then |
| 229 | mount --bind $api $api |
| 230 | elif grep -w mount-tmpfs-run-initramfs $optfile |
| 231 | then |
| 232 | optstmpd=run/initoptstmp |
| 233 | rm -rf $optstmpd |
| 234 | mkdir -p $optstmpd |
| 235 | cp $optfile $optbase $optstmpd |
| 236 | |
| 237 | mount initrd $api -t tmpfs |
| 238 | |
| 239 | mv $optstmpd/* $api |
| 240 | rmdir $optstmpd |
| 241 | fi |
| 242 | cp -rp init shutdown update whitelist bin sbin usr lib etc var $api |
| 243 | |
| 244 | if test -f ${urlfile##*/} |
| 245 | then |
| 246 | cp ${urlfile##*/} $urlfile |
| 247 | if test ${urlfile%/*} != $api |
| 248 | then |
| 249 | cp -p $urlfile $api |
| 250 | fi |
| 251 | fi |
| 252 | |
| 253 | if grep -w mount-under-run-mnt $optfile |
| 254 | then |
| 255 | mnt=run/mnt |
| 256 | else |
| 257 | mnt=$api |
| 258 | fi |
| 259 | |
| 260 | rodir=$mnt/ro |
| 261 | rwdir=$mnt/rw |
| 262 | upper=$rwdir/cow |
| 263 | work=$rwdir/work |
| 264 | |
| 265 | mkdir -p $rodir $rwdir |
| 266 | |
| 267 | |
Patrick Williams | 51d6af4 | 2023-04-14 14:19:55 -0500 | [diff] [blame] | 268 | if test "$consider_download_files" = "y" && |
Milton D. Miller II | 71e7ec1 | 2016-03-02 18:28:54 -0600 | [diff] [blame] | 269 | grep -w openbmc-init-download-files $optfile |
| 270 | then |
Milton D. Miller II | 71e7ec1 | 2016-03-02 18:28:54 -0600 | [diff] [blame] | 271 | if test ! -f $urlfile |
| 272 | then |
| 273 | get_fw_env_var openbmcinitdownloadurl > $urlfile |
| 274 | fi |
| 275 | url="$(cat $urlfile)" |
| 276 | rest="${url#*://}" |
Patrick Williams | 51d6af4 | 2023-04-14 14:19:55 -0500 | [diff] [blame] | 277 | proto="${url%"$rest"}" |
Milton D. Miller II | 71e7ec1 | 2016-03-02 18:28:54 -0600 | [diff] [blame] | 278 | |
| 279 | if test -z "$url" |
| 280 | then |
| 281 | echo "Download url empty. Ignoring download request." |
| 282 | elif test -z "$proto" |
| 283 | then |
| 284 | echo "Download failed." |
| 285 | elif test "$proto" = tftp:// |
| 286 | then |
Patrick Williams | 51d6af4 | 2023-04-14 14:19:55 -0500 | [diff] [blame] | 287 | if test "$consider_download_tftp" = "y" |
Milton D. Miller II | 71e7ec1 | 2016-03-02 18:28:54 -0600 | [diff] [blame] | 288 | then |
| 289 | try_tftp "$url" |
| 290 | else |
| 291 | echo "Download failed." |
| 292 | fi |
| 293 | elif test "$proto" = http:// |
| 294 | then |
Patrick Williams | 51d6af4 | 2023-04-14 14:19:55 -0500 | [diff] [blame] | 295 | if test "$consider_download_http" = "y" |
Milton D. Miller II | 71e7ec1 | 2016-03-02 18:28:54 -0600 | [diff] [blame] | 296 | then |
| 297 | try_wget "$url" |
| 298 | else |
| 299 | echo "Download failed." |
| 300 | fi |
| 301 | elif test "$proto" = ftp:// |
| 302 | then |
Patrick Williams | 51d6af4 | 2023-04-14 14:19:55 -0500 | [diff] [blame] | 303 | if test "$consider_download_ftp" = "y" |
Milton D. Miller II | 71e7ec1 | 2016-03-02 18:28:54 -0600 | [diff] [blame] | 304 | then |
| 305 | try_wget "$url" |
| 306 | else |
| 307 | echo "Download failed." |
| 308 | fi |
| 309 | else |
| 310 | echo "Download failed." |
| 311 | fi |
| 312 | fi |
| 313 | |
Milton D. Miller II | 21786ff | 2016-02-27 17:01:13 -0600 | [diff] [blame] | 314 | # If there are images in root move them to /run/initramfs/ or /run/ now. |
Milton Miller | ec0ba3c | 2016-02-23 21:48:48 -0600 | [diff] [blame] | 315 | imagebasename=${image##*/} |
Patrick Williams | 51d6af4 | 2023-04-14 14:19:55 -0500 | [diff] [blame] | 316 | if test -n "${imagebasename}" && ls /"${imagebasename}"* > /dev/null 2>&1 |
Milton Miller | ec0ba3c | 2016-02-23 21:48:48 -0600 | [diff] [blame] | 317 | then |
Patrick Williams | 51d6af4 | 2023-04-14 14:19:55 -0500 | [diff] [blame] | 318 | if test "$flash_images_before_init" = "y" |
Milton D. Miller II | 21786ff | 2016-02-27 17:01:13 -0600 | [diff] [blame] | 319 | then |
| 320 | echo "Flash images found, will update before starting init." |
Patrick Williams | 51d6af4 | 2023-04-14 14:19:55 -0500 | [diff] [blame] | 321 | mv /"${imagebasename}"* ${image%"$imagebasename"} |
Milton D. Miller II | 21786ff | 2016-02-27 17:01:13 -0600 | [diff] [blame] | 322 | else |
| 323 | echo "Flash images found, will use but deferring flash update." |
Patrick Williams | 51d6af4 | 2023-04-14 14:19:55 -0500 | [diff] [blame] | 324 | mv /"${imagebasename}"* /run/ |
Milton D. Miller II | 21786ff | 2016-02-27 17:01:13 -0600 | [diff] [blame] | 325 | fi |
Milton Miller | ec0ba3c | 2016-02-23 21:48:48 -0600 | [diff] [blame] | 326 | fi |
| 327 | |
Milton D. Miller II | 90b6847 | 2016-02-27 15:48:52 -0600 | [diff] [blame] | 328 | if grep -w clean-rwfs-filesystem $optfile |
Milton Miller | ec0ba3c | 2016-02-23 21:48:48 -0600 | [diff] [blame] | 329 | then |
| 330 | echo "Cleaning of read-write overlay filesystem requested." |
| 331 | touch $trigger |
| 332 | fi |
| 333 | |
Lei YU | a08c996 | 2018-07-20 15:11:27 +0800 | [diff] [blame] | 334 | if grep -w factory-reset $optfile |
| 335 | then |
| 336 | echo "Factory reset requested." |
| 337 | touch $trigger |
| 338 | do_save=--no-save-files |
| 339 | else |
| 340 | do_save=--save-files |
| 341 | fi |
| 342 | |
Patrick Williams | 51d6af4 | 2023-04-14 14:19:55 -0500 | [diff] [blame] | 343 | if test "$force_rwfst_jffs2" = "y" -a "$rwfst" != jffs2 -a ! -f $trigger |
Milton Miller | ec0ba3c | 2016-02-23 21:48:48 -0600 | [diff] [blame] | 344 | then |
| 345 | echo "Converting read-write overlay filesystem to jffs2 forced." |
| 346 | touch $trigger |
| 347 | fi |
| 348 | |
| 349 | if ls $image* > /dev/null 2>&1 |
| 350 | then |
Milton D. Miller II | 383bfc7 | 2016-02-29 11:35:14 -0600 | [diff] [blame] | 351 | if ! test -x $update |
Milton Miller | ec0ba3c | 2016-02-23 21:48:48 -0600 | [diff] [blame] | 352 | then |
Milton D. Miller II | 383bfc7 | 2016-02-29 11:35:14 -0600 | [diff] [blame] | 353 | debug_takeover "Flash update requested but $update missing!" |
Milton Miller | ec0ba3c | 2016-02-23 21:48:48 -0600 | [diff] [blame] | 354 | elif test -f $trigger -a ! -s $trigger |
| 355 | then |
Lei YU | a08c996 | 2018-07-20 15:11:27 +0800 | [diff] [blame] | 356 | if [ $do_save = "--save-files" ] |
| 357 | then |
| 358 | echo "Saving selected files from read-write overlay filesystem." |
| 359 | else |
| 360 | echo "No files will be selected for save." |
| 361 | fi |
| 362 | $update --no-restore-files $do_save |
Milton Miller | ec0ba3c | 2016-02-23 21:48:48 -0600 | [diff] [blame] | 363 | echo "Clearing read-write overlay filesystem." |
Patrick Williams | 51d6af4 | 2023-04-14 14:19:55 -0500 | [diff] [blame] | 364 | flash_eraseall "/dev/$rwfs" |
Milton Miller | ec0ba3c | 2016-02-23 21:48:48 -0600 | [diff] [blame] | 365 | echo "Restoring saved files to read-write overlay filesystem." |
| 366 | touch $trigger |
Milton D. Miller II | 383bfc7 | 2016-02-29 11:35:14 -0600 | [diff] [blame] | 367 | $update --no-save-files --clean-saved-files |
Milton Miller | ec0ba3c | 2016-02-23 21:48:48 -0600 | [diff] [blame] | 368 | else |
Lei YU | a08c996 | 2018-07-20 15:11:27 +0800 | [diff] [blame] | 369 | $update --clean-saved-files $do_save |
Milton Miller | ec0ba3c | 2016-02-23 21:48:48 -0600 | [diff] [blame] | 370 | fi |
| 371 | |
Patrick Williams | 51d6af4 | 2023-04-14 14:19:55 -0500 | [diff] [blame] | 372 | rwfst=$(probe_fs_type "$rwdev") |
Milton Miller | ec0ba3c | 2016-02-23 21:48:48 -0600 | [diff] [blame] | 373 | fsck=$fsckbase$rwfst |
| 374 | fi |
| 375 | |
Milton D. Miller II | 5d772b0 | 2016-02-27 17:31:39 -0600 | [diff] [blame] | 376 | if grep -w overlay-filesystem-in-ram $optfile |
| 377 | then |
| 378 | rwfst=none |
| 379 | fi |
| 380 | |
Milton D. Miller II | 77be704 | 2016-02-29 12:10:34 -0600 | [diff] [blame] | 381 | copyfiles= |
| 382 | if grep -w copy-files-to-ram $optfile |
| 383 | then |
| 384 | rwfst=none |
| 385 | copyfiles=y |
| 386 | fi |
| 387 | |
| 388 | # It would be nice to do this after fsck but that mean rofs is mounted |
| 389 | # which triggers the mtd is mounted check |
| 390 | if test "$rwfst$copyfiles" = noney |
| 391 | then |
| 392 | touch $trigger |
| 393 | $update --copy-files --clean-saved-files --no-restore-files |
| 394 | fi |
| 395 | |
Milton D. Miller II | 9ecec17 | 2016-02-27 17:31:39 -0600 | [diff] [blame] | 396 | if grep -w copy-base-filesystem-to-ram $optfile && |
Patrick Williams | 51d6af4 | 2023-04-14 14:19:55 -0500 | [diff] [blame] | 397 | test ! -e /run/image-rofs && ! cp "$rodev" /run/image-rofs |
Milton D. Miller II | 9ecec17 | 2016-02-27 17:31:39 -0600 | [diff] [blame] | 398 | then |
| 399 | # Remove any partial copy to avoid attempted usage later |
| 400 | if test -e /run/image-rofs |
| 401 | then |
| 402 | ls -l /run/image-rofs |
| 403 | rm -f /run/image-rofs |
| 404 | fi |
| 405 | debug_takeover "Copying $rodev to /run/image-rofs failed." |
| 406 | fi |
| 407 | |
Milton D. Miller II | a8f26b6 | 2016-02-27 16:04:32 -0600 | [diff] [blame] | 408 | if test -s /run/image-rofs |
| 409 | then |
| 410 | rodev=/run/image-rofs |
| 411 | roopts=$roopts,loop |
| 412 | fi |
| 413 | |
Patrick Williams | 51d6af4 | 2023-04-14 14:19:55 -0500 | [diff] [blame] | 414 | mount "$rodev" $rodir -t $rofst -o $roopts |
Milton Miller | 5c212e4 | 2016-02-06 16:05:06 -0600 | [diff] [blame] | 415 | |
Patrick Williams | 51d6af4 | 2023-04-14 14:19:55 -0500 | [diff] [blame] | 416 | if test -x "$rodir$fsck" |
Milton Miller | 5c212e4 | 2016-02-06 16:05:06 -0600 | [diff] [blame] | 417 | then |
| 418 | for fs in $fslist |
| 419 | do |
Patrick Williams | 51d6af4 | 2023-04-14 14:19:55 -0500 | [diff] [blame] | 420 | mount --bind "$fs" "$rodir/$fs" |
Milton Miller | 5c212e4 | 2016-02-06 16:05:06 -0600 | [diff] [blame] | 421 | done |
Patrick Williams | 51d6af4 | 2023-04-14 14:19:55 -0500 | [diff] [blame] | 422 | chroot $rodir "$fsck" $fsckopts "$rwdev" |
Milton Miller | 5c212e4 | 2016-02-06 16:05:06 -0600 | [diff] [blame] | 423 | rc=$? |
| 424 | for fs in $fslist |
| 425 | do |
Patrick Williams | 51d6af4 | 2023-04-14 14:19:55 -0500 | [diff] [blame] | 426 | umount "$rodir/$fs" |
Milton Miller | 5c212e4 | 2016-02-06 16:05:06 -0600 | [diff] [blame] | 427 | done |
| 428 | if test $rc -gt 1 |
| 429 | then |
| 430 | debug_takeover "fsck of read-write fs on $rwdev failed (rc=$rc)" |
| 431 | fi |
Milton D. Miller II | ed3680d | 2016-02-27 16:38:16 -0600 | [diff] [blame] | 432 | elif test "$rwfst" != jffs2 -a "$rwfst" != none |
Milton Miller | e442584 | 2016-02-23 21:00:06 -0600 | [diff] [blame] | 433 | then |
Milton Miller | 5c212e4 | 2016-02-06 16:05:06 -0600 | [diff] [blame] | 434 | echo "No '$fsck' in read only fs, skipping fsck." |
| 435 | fi |
| 436 | |
Milton D. Miller II | ed3680d | 2016-02-27 16:38:16 -0600 | [diff] [blame] | 437 | if test "$rwfst" = none |
| 438 | then |
| 439 | echo "Running with read-write overlay in RAM for this boot." |
| 440 | echo "No state will be preserved unless flash update performed." |
Patrick Williams | 51d6af4 | 2023-04-14 14:19:55 -0500 | [diff] [blame] | 441 | elif ! mount "$rwdev" $rwdir -t "$rwfst" -o $rwopts |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 442 | then |
Milton Miller | 2250982 | 2016-02-05 13:04:29 -0600 | [diff] [blame] | 443 | msg="$(cat)" << HERE |
| 444 | |
| 445 | Mounting read-write $rwdev filesystem failed. Please fix and run |
Andrew Jeffery | e4f6d37 | 2016-02-17 17:19:58 +1030 | [diff] [blame] | 446 | mount $rwdev $rwdir -t $rwfst -o $rwopts |
Adriana Kobylak | 7fd0173 | 2020-03-12 11:12:59 -0500 | [diff] [blame] | 447 | or perform a factory reset with the clean-rwfs-filesystem option. |
Milton Miller | 2250982 | 2016-02-05 13:04:29 -0600 | [diff] [blame] | 448 | HERE |
| 449 | debug_takeover "$msg" |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 450 | fi |
| 451 | |
Heyi Guo | 2168adb | 2022-01-26 14:10:33 +0800 | [diff] [blame] | 452 | # Empty workdir; do not remove workdir itself for it will fail to recreate it if |
| 453 | # RWFS is full |
| 454 | if [ -d $work ] |
| 455 | then |
| 456 | find $work -maxdepth 1 -mindepth 1 -exec rm -rf '{}' + |
| 457 | fi |
| 458 | |
Milton Miller | 5c212e4 | 2016-02-06 16:05:06 -0600 | [diff] [blame] | 459 | mkdir -p $upper $work |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 460 | |
William A. Kennington III | 5f87e53 | 2022-05-02 17:44:21 -0700 | [diff] [blame] | 461 | # Opportunisticly set a sane BMC date based on a file that gets |
| 462 | # written right before rebooting or powercycling. If none exists, |
| 463 | # use the image build date. |
| 464 | files="$upper/var/lib/systemd/random-seed $rodir/etc/os-release" |
Zev Weiss | 1bcd53d | 2023-04-18 20:12:07 -0700 | [diff] [blame] | 465 | # shellcheck disable=SC2086 |
| 466 | time=$(find $files -exec stat -c %Y {} \; | sort -n | tail -n 1) |
William A. Kennington III | 5f87e53 | 2022-05-02 17:44:21 -0700 | [diff] [blame] | 467 | # Allow RTC coordinated time to supersede this setting |
| 468 | if [ "$(date +%s)" -lt "$time" ]; then |
| 469 | date -s @$((time + 5)) || true |
| 470 | fi |
| 471 | |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 472 | mount -t overlay -o lowerdir=$rodir,upperdir=$upper,workdir=$work cow /root |
| 473 | |
Milton Miller | 2250982 | 2016-02-05 13:04:29 -0600 | [diff] [blame] | 474 | while ! chroot /root /bin/sh -c "test -x '$init' -a -s '$init'" |
| 475 | do |
| 476 | msg="$(cat)" << HERE |
| 477 | |
| 478 | Unable to confirm /sbin/init is an executable non-empty file |
| 479 | in merged file system mounted at /root. |
| 480 | |
Adriana Kobylak | 7fd0173 | 2020-03-12 11:12:59 -0500 | [diff] [blame] | 481 | Change Root test failed! |
Milton Miller | 2250982 | 2016-02-05 13:04:29 -0600 | [diff] [blame] | 482 | HERE |
| 483 | debug_takeover "$msg" |
| 484 | done |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 485 | |
Milton Miller | ba65b7b | 2016-02-05 12:07:53 -0600 | [diff] [blame] | 486 | for f in $fslist |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 487 | do |
Patrick Williams | 51d6af4 | 2023-04-14 14:19:55 -0500 | [diff] [blame] | 488 | mount --move "$f" "root/$f" |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 489 | done |
| 490 | |
Zev Weiss | bab3387 | 2022-06-29 14:54:10 -0700 | [diff] [blame] | 491 | exec switch_root /root $init |