Adriana Kobylak | 9cbfa2e | 2019-04-25 14:02:37 -0500 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | set -eo pipefail |
| 3 | |
| 4 | # Get the root mtd device number (mtdX) from "/dev/ubiblockX_Y on /" |
| 5 | findrootmtd() { |
| 6 | rootmatch=" on / " |
| 7 | m="$(mount | grep "${rootmatch}" | grep "ubiblock")" |
| 8 | m="${m##*ubiblock}" |
| 9 | m="${m%_*}" |
| 10 | if [ -z "${m}" ]; then |
| 11 | # default to bmc mtd (0) |
| 12 | m=0 |
| 13 | fi |
| 14 | echo "mtd${m}" |
| 15 | } |
| 16 | |
| 17 | findrootubi() { |
| 18 | rootmatch=" on / " |
| 19 | m="$(mount | grep "${rootmatch}")" |
| 20 | m="${m##*ubiblock}" |
| 21 | m="${m% on*}" |
| 22 | echo "ubi${m}" |
| 23 | } |
| 24 | |
| 25 | # Get the mtd device number (mtdX) |
| 26 | findmtd() { |
| 27 | m="$(grep -xl "$1" /sys/class/mtd/*/name)" |
| 28 | m="${m%/name}" |
| 29 | m="${m##*/}" |
| 30 | echo "${m}" |
| 31 | } |
| 32 | |
| 33 | # Get the mtd device number only (return X of mtdX) |
| 34 | findmtdnum() { |
| 35 | m="$(findmtd "$1")" |
| 36 | m="${m##mtd}" |
| 37 | echo "${m}" |
| 38 | } |
| 39 | |
| 40 | # Get the ubi device number (ubiX_Y) |
| 41 | findubi() { |
| 42 | u="$(grep -xl "$1" /sys/class/ubi/ubi?/subsystem/ubi*/name)" |
| 43 | u="${u%/name}" |
| 44 | u="${u##*/}" |
| 45 | echo "${u}" |
| 46 | } |
| 47 | |
| 48 | # Get the ubi device number (ubiX_Y) on a specific mtd |
| 49 | findubi_onmtd() { |
| 50 | u="$(grep -xl "$1" /sys/class/ubi/ubi"$2"/subsystem/ubi"$2"*/name)" |
| 51 | u="${u%/name}" |
| 52 | u="${u##*/}" |
| 53 | echo "${u}" |
| 54 | } |
| 55 | |
| 56 | # Get all ubi device names on a specific mtd that match requested string |
| 57 | findubiname_onmtd() { |
| 58 | u="$(grep -h "$1" /sys/class/ubi/ubi"$2"/subsystem/ubi"$2"*/name)" |
| 59 | u="${u%/name}" |
| 60 | u="${u##*/}" |
| 61 | echo "${u}" |
| 62 | } |
| 63 | |
| 64 | # Get the name from the requested ubiX_Y volume |
| 65 | findname() { |
| 66 | n="$(cat /sys/class/ubi/$1/name)" |
| 67 | echo "${n}" |
| 68 | } |
| 69 | |
Adriana Kobylak | a84f06d | 2022-01-18 15:41:57 +0000 | [diff] [blame] | 70 | # Set the version path property to the flash location where the image was |
| 71 | # successfully flashed |
| 72 | set_flashid() { |
| 73 | busctl set-property xyz.openbmc_project.Software.BMC.Updater \ |
| 74 | "/xyz/openbmc_project/software/${version}" \ |
| 75 | xyz.openbmc_project.Common.FilePath \ |
| 76 | Path s "$1" |
| 77 | } |
| 78 | |
Adriana Kobylak | 9cbfa2e | 2019-04-25 14:02:37 -0500 | [diff] [blame] | 79 | # Set the u-boot envs that perform a side switch on failure to boot |
| 80 | set_wdt2bite() { |
| 81 | if ! fw_printenv wdt2bite 2>/dev/null; then |
| 82 | fw_setenv wdt2bite "mw.l 0x1e785024 0xa 1; mw.b 0x1e78502c 0xb3 1" |
| 83 | fw_setenv bootalt "run wdt2bite" |
| 84 | fw_setenv obmc_bootcmd "ubi part obmc-ubi; run do_rwreset; ubi read \ |
| 85 | \${loadaddr} \${kernelname}; bootm \${loadaddr} || run bootalt" |
| 86 | fi |
| 87 | } |
| 88 | |
| 89 | # Make space on flash before creating new volumes. This can be enhanced |
| 90 | # determine current flash usage. For now only keep a "keepmax" number of them |
| 91 | ubi_remove_volumes() |
| 92 | { |
| 93 | rootubi="$(findrootubi)" |
| 94 | rootname="$(findname "${rootubi}")" |
| 95 | rootversion="${rootname##*-}" |
| 96 | rootkernel="kernel-${rootversion}" |
| 97 | |
| 98 | # Just keep max number of volumes before updating, don't delete the version |
| 99 | # the BMC is booted from, and when a version is identified to be deleted, |
| 100 | # delete both the rofs and kernel volumes for that version. |
| 101 | rmnames="$(findubiname_onmtd "${name%-*}-" "${ro}")" |
| 102 | rmnames=(${rmnames}) |
| 103 | ubicount="${#rmnames[@]}" |
| 104 | while [ ${ubicount} -ge ${keepmax} ]; do |
| 105 | # Loop through existing volumes and skip currently active ones |
| 106 | for (( index=0; index<${#rmnames[@]}; index++ )); do |
| 107 | rmname="${rmnames[${index}]}" |
| 108 | rmversion="${rmname##*-}" |
| 109 | [ "${rmversion}" == "${version}" ] && continue |
| 110 | rmubi="$(findubi_onmtd "rofs-${rmversion}" "${ro}")" |
| 111 | if [[ ( "${rmubi}" != "${rootubi}" ) && |
| 112 | ( "${rmname}" != "${rootkernel}" ) ]]; then |
| 113 | ubi_remove "rofs-${rmversion}" "${ro}" |
| 114 | ubi_remove "kernel-${rmversion}" "${ro}" |
| 115 | # Remove priority value |
| 116 | fw_setenv "${rmversion}" |
| 117 | break |
| 118 | fi |
| 119 | done |
| 120 | # Decrease count regardless to avoid an infinite loop |
| 121 | (( ubicount-- )) |
| 122 | done |
| 123 | } |
| 124 | |
| 125 | ubi_rw() { |
| 126 | rwmtd="$(findmtd "${reqmtd}")" |
| 127 | rw="${rwmtd#mtd}" |
| 128 | ubidev="/dev/ubi${rw}" |
| 129 | |
| 130 | # Update rwfs_size, check imgsize was specified, otherwise it'd clear the var |
| 131 | if [ ! -z "$imgsize" ]; then |
| 132 | rwsize="$(fw_printenv -n rwfs_size 2>/dev/null)" || true |
| 133 | if [[ "${imgsize}" != "${rwsize}" ]]; then |
| 134 | fw_setenv rwfs_size "${imgsize}" |
| 135 | fi |
| 136 | fi |
| 137 | |
| 138 | vol="$(findubi "${name}")" |
| 139 | if [ -z "${vol}" ]; then |
| 140 | ubimkvol "${ubidev}" -N "${name}" -s "${imgsize}" |
| 141 | fi |
| 142 | } |
| 143 | |
| 144 | ubi_ro() { |
| 145 | keepmax=2 # Default 2 volumes per mtd |
| 146 | romtd="$(findmtd "${reqmtd}")" |
| 147 | romtd2="$(findmtd "${reqmtd2}")" |
| 148 | |
| 149 | if [ ! "${romtd}" == "${romtd2}" ]; then |
| 150 | # Request to use alternate mtd device, choose the non-root one |
| 151 | keepmax=1 # 1 volume on each of the requested mtds |
| 152 | rootmtd="$(findrootmtd)" |
| 153 | if [ "${rootmtd}" == "${romtd}" ]; then |
| 154 | romtd="${romtd2}" |
| 155 | fi |
| 156 | fi |
| 157 | ro="${romtd#mtd}" |
| 158 | ubidev="/dev/ubi${ro}" |
| 159 | |
| 160 | ubi_remove_volumes |
| 161 | |
| 162 | if [ -z "${imgfile}" ]; then |
| 163 | echo "Unable to create read-only volume. Image file not specified." |
| 164 | return 1 |
| 165 | fi |
| 166 | |
| 167 | # Create a ubi volume, dynamically sized to fit BMC image if size unspecified |
| 168 | img="/tmp/images/${version}/${imgfile}" |
| 169 | imgsize="$(stat -c '%s' ${img})" |
| 170 | |
| 171 | vol="$(findubi "${name}")" |
| 172 | if [ ! -z "${vol}" ]; then |
| 173 | # Allow a duplicate kernel volume on the alt mtd |
| 174 | if [[ "${name}" =~ "kernel" ]]; then |
| 175 | vol="$(findubi_onmtd "${name}" "${ro}")" |
| 176 | fi |
| 177 | fi |
| 178 | if [ -z "${vol}" ]; then |
| 179 | ubimkvol "${ubidev}" -N "${name}" -s "${imgsize}" --type=static |
| 180 | vol="$(findubi "${name}")" |
| 181 | fi |
Adriana Kobylak | a84f06d | 2022-01-18 15:41:57 +0000 | [diff] [blame] | 182 | |
| 183 | set_flashid "${version}" |
Adriana Kobylak | 9cbfa2e | 2019-04-25 14:02:37 -0500 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | # Squashfs images need a ubi block |
| 187 | ubi_block() { |
| 188 | vol="$(findubi "${name}")" |
| 189 | ubidevid="${vol#ubi}" |
| 190 | block="/dev/ubiblock${ubidevid}" |
| 191 | if [ ! -e "$block" ]; then |
| 192 | ubiblock --create "/dev/ubi${ubidevid}" |
| 193 | fi |
| 194 | } |
| 195 | |
| 196 | ubi_updatevol() { |
| 197 | vol="$(findubi "${name}")" |
| 198 | ubidevid="${vol#ubi}" |
| 199 | img="/tmp/images/${version}/${imgfile}" |
| 200 | ubiupdatevol "/dev/ubi${ubidevid}" "${img}" |
| 201 | } |
| 202 | |
| 203 | ubi_remove() { |
| 204 | rmname="$1" |
| 205 | rmmtd="$2" |
| 206 | if [ ! -z "${rmmtd}" ]; then |
| 207 | vol="$(findubi_onmtd "${rmname}" "${rmmtd}")" |
| 208 | else |
| 209 | vol="$(findubi "${rmname}")" |
| 210 | fi |
| 211 | |
| 212 | if [ ! -z "$vol" ]; then |
| 213 | vol="${vol%_*}" |
| 214 | |
| 215 | if grep -q "$rmname" /proc/mounts; then |
| 216 | mountdir=$(grep "$rmname" /proc/mounts | cut -d " " -f 2) |
| 217 | umount "$mountdir" |
| 218 | rm -r "$mountdir" |
| 219 | fi |
| 220 | |
| 221 | ubirmvol "/dev/${vol}" -N "$rmname" |
| 222 | fi |
| 223 | } |
| 224 | |
| 225 | ubi_cleanup() { |
| 226 | # When ubi_cleanup is run, it expects one or no active version. |
| 227 | activeVersion=$(busctl --list --no-pager tree \ |
| 228 | xyz.openbmc_project.Software.BMC.Updater | \ |
| 229 | grep /xyz/openbmc_project/software/ | tail -c 9) |
| 230 | |
| 231 | if [[ -z "$activeVersion" ]]; then |
| 232 | vols=$(ubinfo -a | grep "rofs-" | cut -c 14-) |
| 233 | vols=(${vols}) |
| 234 | else |
Adriana Kobylak | 25773a7 | 2022-01-21 15:24:48 +0000 | [diff] [blame] | 235 | flashid=$(busctl get-property xyz.openbmc_project.Software.BMC.Updater \ |
| 236 | "/xyz/openbmc_project/software/${activeVersion}" \ |
| 237 | xyz.openbmc_project.Common.FilePath Path | awk '{print $NF;}' | tr -d '"') |
Adriana Kobylak | 9cbfa2e | 2019-04-25 14:02:37 -0500 | [diff] [blame] | 238 | vols=$(ubinfo -a | grep "rofs-" | \ |
Adriana Kobylak | 25773a7 | 2022-01-21 15:24:48 +0000 | [diff] [blame] | 239 | grep -v "$flashid" | cut -c 14-) || true |
Adriana Kobylak | 9cbfa2e | 2019-04-25 14:02:37 -0500 | [diff] [blame] | 240 | vols=(${vols}) |
| 241 | fi |
| 242 | |
| 243 | for (( index=0; index<${#vols[@]}; index++ )); do |
| 244 | ubi_remove ${vols[index]} |
| 245 | done |
| 246 | } |
| 247 | |
| 248 | mount_alt_rwfs() { |
| 249 | altNum="$(findmtdnum "alt-bmc")" |
| 250 | if [ ! -z "${altNum}" ]; then |
| 251 | altRwfs=$(ubinfo -a -d ${altNum} | grep -w "rwfs") || true |
| 252 | if [ ! -z "${altRwfs}" ]; then |
| 253 | altVarMount="/media/alt/var" |
| 254 | mkdir -p "${altVarMount}" |
| 255 | if mount ubi"${altNum}":rwfs "${altVarMount}" -t ubifs -o defaults; then |
| 256 | mkdir -p "${altVarMount}"/persist/etc |
| 257 | fi |
| 258 | fi |
| 259 | fi |
| 260 | } |
| 261 | |
| 262 | remount_ubi() { |
| 263 | bmcmtd="$(findmtd "bmc")" |
| 264 | altbmcmtd="$(findmtd "alt-bmc")" |
| 265 | mtds="${bmcmtd: -1}","${altbmcmtd: -1}" |
| 266 | |
Adriana Kobylak | 1e81f23 | 2022-01-18 22:28:47 +0000 | [diff] [blame] | 267 | rootubi="$(findrootubi)" |
| 268 | rootname="$(findname "${rootubi}")" |
| 269 | |
Adriana Kobylak | 9cbfa2e | 2019-04-25 14:02:37 -0500 | [diff] [blame] | 270 | IFS=',' read -r -a mtds <<< "$mtds" |
| 271 | mtds=($(echo "${mtds[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' ')) |
| 272 | for mtd in ${mtds[@]}; do |
| 273 | # Get information on all ubi volumes |
| 274 | ubinfo=$(ubinfo -d ${mtd}) |
| 275 | presentVolumes=${ubinfo##*:} |
| 276 | IFS=', ' read -r -a array <<< "$presentVolumes" |
| 277 | for element in ${array[@]}; do |
| 278 | elementProperties=$(ubinfo -d $mtd -n $element) |
| 279 | # Get ubi volume name by getting rid of additional properties |
| 280 | name=${elementProperties#*Name:} |
| 281 | name="${name%Character*}" |
| 282 | name="$(echo -e "${name}" | tr -d '[:space:]')" |
| 283 | |
| 284 | if [[ ${name} == rofs-* ]]; then |
Adriana Kobylak | 1e81f23 | 2022-01-18 22:28:47 +0000 | [diff] [blame] | 285 | if [[ "${name}" == "${rootname}" ]]; then |
| 286 | mountdir="/media/${name}-functional" |
| 287 | else |
| 288 | mountdir="/media/${name}" |
| 289 | fi |
Adriana Kobylak | 9cbfa2e | 2019-04-25 14:02:37 -0500 | [diff] [blame] | 290 | |
| 291 | if [ ! -d ${mountdir} ]; then |
| 292 | mkdir -p "${mountdir}" |
| 293 | # U-Boot will create the ubiblock for the running version, but not |
| 294 | # for the version on the other chip |
| 295 | if [ ! -e "/dev/ubiblock${mtd}_${element}" ]; then |
| 296 | ubiblock --create /dev/ubi${mtd}_${element} |
| 297 | fi |
| 298 | mount -t squashfs -o ro "/dev/ubiblock${mtd}_${element}" "${mountdir}" |
| 299 | fi |
| 300 | fi |
| 301 | done |
| 302 | done |
| 303 | |
| 304 | set_wdt2bite |
| 305 | } |
| 306 | |
| 307 | # Read the current env variable and set it on the alternate boot env |
| 308 | copy_env_var_to_alt() { |
| 309 | varName=$1 |
| 310 | value="$(fw_printenv -n "${varName}")" |
| 311 | fw_setenv -c /etc/alt_fw_env.config "${varName}" "${value}" |
| 312 | } |
| 313 | |
| 314 | # When the alternate bmc chip boots, u-boot thinks its the primary mtdX. |
| 315 | # Therefore need to swap the chip numbers when copying the ubiblock and root to |
| 316 | # alternate bmc u-boot environment. |
| 317 | copy_ubiblock_to_alt() { |
| 318 | value="$(fw_printenv -n ubiblock)" |
| 319 | bmcNum="$(findmtdnum "bmc")" |
| 320 | altNum="$(findmtdnum "alt-bmc")" |
| 321 | replaceAlt="${value/${altNum},/${bmcNum},}" |
| 322 | |
| 323 | if [[ "${value}" == "${replaceAlt}" ]]; then |
| 324 | replaceBmc="${value/${bmcNum},/${altNum},}" |
| 325 | value=${replaceBmc} |
| 326 | else |
| 327 | value=${replaceAlt} |
| 328 | fi |
| 329 | |
| 330 | fw_setenv -c /etc/alt_fw_env.config ubiblock "${value}" |
| 331 | } |
| 332 | |
| 333 | copy_root_to_alt() { |
| 334 | value="$(fw_printenv -n root)" |
| 335 | bmcNum="$(findmtdnum "bmc")" |
| 336 | altNum="$(findmtdnum "alt-bmc")" |
| 337 | replaceAlt="${value/${altNum}_/${bmcNum}_}" |
| 338 | |
| 339 | if [[ "${value}" == "${replaceAlt}" ]]; then |
| 340 | replaceBmc="${value/${bmcNum}_/${altNum}_}" |
| 341 | value=${replaceBmc} |
| 342 | else |
| 343 | value=${replaceAlt} |
| 344 | fi |
| 345 | |
| 346 | fw_setenv -c /etc/alt_fw_env.config root "${value}" |
| 347 | } |
| 348 | |
| 349 | ubi_setenv() { |
| 350 | # The U-Boot environment maintains two banks of environment variables. |
| 351 | # The banks need to be consistent with each other to ensure that these |
| 352 | # variables can reliably be read from file. In order to guarantee that the |
| 353 | # banks are both correct, we need to run fw_setenv twice. |
| 354 | variable=$1 |
| 355 | if [[ "$variable" == *"="* ]]; then |
| 356 | varName="${variable%=*}" |
| 357 | value="${variable##*=}" |
| 358 | # Write only if var is not set already to the requested value |
| 359 | currentValue="$(fw_printenv -n "${varName}" 2>/dev/null)" || true |
| 360 | if [[ "${currenValue}" != "${value}" ]]; then |
| 361 | fw_setenv "$varName" "$value" |
| 362 | fw_setenv "$varName" "$value" |
| 363 | fi |
| 364 | else |
| 365 | fw_setenv "$variable" |
| 366 | fw_setenv "$variable" |
| 367 | fi |
| 368 | } |
| 369 | |
| 370 | mtd_write() { |
| 371 | flashmtd="$(findmtd "${reqmtd}")" |
| 372 | img="/tmp/images/${version}/${imgfile}" |
| 373 | flashcp -v ${img} /dev/${flashmtd} |
| 374 | } |
| 375 | |
| 376 | backup_env_vars() { |
| 377 | copy_env_var_to_alt kernelname |
| 378 | copy_ubiblock_to_alt |
| 379 | copy_root_to_alt |
| 380 | } |
| 381 | |
| 382 | update_env_vars() { |
Adriana Kobylak | 25773a7 | 2022-01-21 15:24:48 +0000 | [diff] [blame] | 383 | vol="$(findubi rofs-"${flashid}")" |
Adriana Kobylak | 9cbfa2e | 2019-04-25 14:02:37 -0500 | [diff] [blame] | 384 | if [ -z "${vol}" ]; then |
| 385 | return 1 |
| 386 | fi |
| 387 | ubidevid="${vol#ubi}" |
| 388 | block="/dev/ubiblock${ubidevid}" |
| 389 | if [ ! -e "${block}" ]; then |
| 390 | return 1 |
| 391 | fi |
Adriana Kobylak | 25773a7 | 2022-01-21 15:24:48 +0000 | [diff] [blame] | 392 | ubi_setenv "kernelname=kernel-${flashid}" |
Adriana Kobylak | 9cbfa2e | 2019-04-25 14:02:37 -0500 | [diff] [blame] | 393 | ubi_setenv "ubiblock=$(echo "${ubidevid}" | sed 's/_/,/')" |
| 394 | ubi_setenv "root=${block}" |
| 395 | } |
| 396 | |
| 397 | #TODO: Replace the implementation with systemd-inhibitors lock |
| 398 | # once systemd/systemd#949 is resolved |
| 399 | rebootguardenable() { |
| 400 | dir="/run/systemd/system/" |
| 401 | file="reboot-guard.conf" |
| 402 | units=("reboot" "poweroff" "halt") |
| 403 | |
| 404 | for unit in "${units[@]}"; do |
| 405 | mkdir -p ${dir}${unit}.target.d |
| 406 | echo -e "[Unit]\nRefuseManualStart=yes" >> ${dir}${unit}.target.d/${file} |
| 407 | done |
| 408 | } |
| 409 | |
| 410 | #TODO: Replace the implementation with systemd-inhibitors lock |
| 411 | # once systemd/systemd#949 is resolved |
| 412 | rebootguarddisable() { |
| 413 | dir="/run/systemd/system/" |
| 414 | file="reboot-guard.conf" |
| 415 | units=("reboot" "poweroff" "halt") |
| 416 | |
| 417 | for unit in "${units[@]}"; do |
| 418 | rm -rf ${dir}${unit}.target.d/${file} |
| 419 | done |
| 420 | } |
| 421 | |
| 422 | # Create a copy in the alt mtd |
| 423 | create_vol_in_alt() { |
| 424 | alt="alt-bmc" |
| 425 | altmtd="$(findmtd "${alt}")" |
| 426 | if [ ! -z "${altmtd}" ]; then |
| 427 | reqmtd="${alt}" |
| 428 | reqmtd2="${alt}" |
| 429 | ubi_ro |
| 430 | ubi_updatevol |
| 431 | fi |
| 432 | } |
| 433 | |
| 434 | # Copy contents of one MTD device to another |
| 435 | mtd_copy() { |
| 436 | in=$1 |
| 437 | out=$2 |
| 438 | |
| 439 | # Must erase MTD first to prevent corruption |
| 440 | flash_eraseall "${out}" |
| 441 | dd if="${in}" of="${out}" |
| 442 | } |
| 443 | |
| 444 | mirroruboot() { |
| 445 | bmc="$(findmtd "u-boot")" |
| 446 | bmcdev="/dev/${bmc}" |
| 447 | alt="$(findmtd "alt-u-boot")" |
| 448 | altdev="/dev/${alt}" |
| 449 | |
| 450 | checksum_bmc="$(md5sum "${bmcdev}")" |
| 451 | checksum_bmc="${checksum_bmc% *}" |
| 452 | checksum_alt="$(md5sum "${altdev}")" |
| 453 | checksum_alt="${checksum_alt% *}" |
| 454 | |
| 455 | if [[ "${checksum_bmc}" != "${checksum_alt}" ]]; then |
| 456 | bmcenv="$(findmtd "u-boot-env")" |
| 457 | bmcenvdev="/dev/${bmcenv}" |
| 458 | altenv="$(findmtd "alt-u-boot-env")" |
| 459 | altenvdev="/dev/${altenv}" |
| 460 | |
| 461 | echo "Mirroring U-boot to alt chip" |
| 462 | mtd_copy "${bmcdev}" "${altdev}" |
| 463 | mtd_copy "${bmcenvdev}" "${altenvdev}" |
| 464 | |
| 465 | copy_ubiblock_to_alt |
| 466 | copy_root_to_alt |
| 467 | fi |
| 468 | } |
| 469 | |
Adriana Kobylak | 62f3820 | 2020-09-29 13:04:25 -0500 | [diff] [blame] | 470 | # Compare the device where u-boot resides with an image file. Specify the full |
| 471 | # path to the device and image file to use for the compare. Print a value of |
| 472 | # "0" if identical, "1" otherwise. |
| 473 | cmp_uboot() { |
| 474 | device="$1" |
| 475 | image="$2" |
| 476 | |
| 477 | # Since the image file can be smaller than the device, copy the device to a |
| 478 | # tmp file and write the image file on top, then compare the sum of each. |
| 479 | # Use cat / redirection since busybox does not have the conv=notrunc option. |
Adriana Kobylak | 9b8816f | 2020-12-02 11:08:15 -0600 | [diff] [blame] | 480 | tmpFile="$(mktemp /tmp/ubootdev.XXXXXX)" |
Adriana Kobylak | 62f3820 | 2020-09-29 13:04:25 -0500 | [diff] [blame] | 481 | dd if="${device}" of="${tmpFile}" |
| 482 | devSum="$(sha256sum ${tmpFile})" |
| 483 | cat < "${image}" 1<> "${tmpFile}" |
| 484 | imgSum="$(sha256sum ${tmpFile})" |
| 485 | rm -f "${tmpFile}" |
| 486 | |
| 487 | if [ "${imgSum}" == "${devSum}" ]; then |
| 488 | echo "0"; |
| 489 | else |
| 490 | echo "1"; |
| 491 | fi |
| 492 | } |
| 493 | |
Adriana Kobylak | 70f5bc0 | 2020-05-13 14:08:14 -0500 | [diff] [blame] | 494 | # The eMMC partition labels for the kernel and rootfs are boot-a/b and rofs-a/b. |
Adriana Kobylak | 3412435 | 2020-05-22 09:40:40 -0500 | [diff] [blame] | 495 | # Return the label (a or b) for the running partition. |
| 496 | mmc_get_primary_label() { |
Adriana Kobylak | 5312d85 | 2020-07-18 09:49:34 -0500 | [diff] [blame] | 497 | # Get root device /dev/mmcblkpX |
Adriana Kobylak | 3412435 | 2020-05-22 09:40:40 -0500 | [diff] [blame] | 498 | rootmatch=" on / " |
Adriana Kobylak | 5312d85 | 2020-07-18 09:49:34 -0500 | [diff] [blame] | 499 | root="$(mount | grep "${rootmatch}")" |
| 500 | root="${root%${rootmatch}*}" |
| 501 | |
| 502 | # Find the device label |
| 503 | if [[ $(readlink -f /dev/disk/by-partlabel/rofs-a) == "${root}" ]]; then |
Adriana Kobylak | 3412435 | 2020-05-22 09:40:40 -0500 | [diff] [blame] | 504 | echo "a" |
Adriana Kobylak | 5312d85 | 2020-07-18 09:49:34 -0500 | [diff] [blame] | 505 | elif [[ $(readlink -f /dev/disk/by-partlabel/rofs-b) == "${root}" ]]; then |
Adriana Kobylak | 3412435 | 2020-05-22 09:40:40 -0500 | [diff] [blame] | 506 | echo "b" |
Adriana Kobylak | 5312d85 | 2020-07-18 09:49:34 -0500 | [diff] [blame] | 507 | else |
| 508 | echo "" |
Adriana Kobylak | 3412435 | 2020-05-22 09:40:40 -0500 | [diff] [blame] | 509 | fi |
| 510 | } |
| 511 | |
| 512 | # The eMMC partition labels for the kernel and rootfs are boot-a/b and rofs-a/b. |
Adriana Kobylak | 70f5bc0 | 2020-05-13 14:08:14 -0500 | [diff] [blame] | 513 | # Return the label (a or b) for the non-running partition. |
| 514 | mmc_get_secondary_label() { |
Adriana Kobylak | 5312d85 | 2020-07-18 09:49:34 -0500 | [diff] [blame] | 515 | root="$(mmc_get_primary_label)" |
| 516 | if [[ "${root}" == "a" ]]; then |
Adriana Kobylak | 70f5bc0 | 2020-05-13 14:08:14 -0500 | [diff] [blame] | 517 | echo "b" |
Adriana Kobylak | 5312d85 | 2020-07-18 09:49:34 -0500 | [diff] [blame] | 518 | elif [[ "${root}" == "b" ]]; then |
Adriana Kobylak | 70f5bc0 | 2020-05-13 14:08:14 -0500 | [diff] [blame] | 519 | echo "a" |
Adriana Kobylak | 5312d85 | 2020-07-18 09:49:34 -0500 | [diff] [blame] | 520 | else |
| 521 | echo "" |
Adriana Kobylak | 70f5bc0 | 2020-05-13 14:08:14 -0500 | [diff] [blame] | 522 | fi |
| 523 | } |
| 524 | |
Adriana Kobylak | 1e81f23 | 2022-01-18 22:28:47 +0000 | [diff] [blame] | 525 | mmc_mount() { |
| 526 | primaryId="$(mmc_get_primary_label)" |
| 527 | secondaryId="$(mmc_get_secondary_label)" |
| 528 | |
| 529 | primaryDir="${mediaDir}/rofs-${primaryId}-functional" |
| 530 | secondaryDir="${mediaDir}/rofs-${secondaryId}" |
| 531 | |
| 532 | mkdir -p "${primaryDir}" |
| 533 | mkdir -p "${secondaryDir}" |
| 534 | |
| 535 | mount PARTLABEL=rofs-${primaryId} "${primaryDir}" -t ext4 -o ro || rmdir "${primaryDir}" |
| 536 | mount PARTLABEL=rofs-${secondaryId} "${secondaryDir}" -t ext4 -o ro || rmdir "${secondaryDir}" |
| 537 | } |
| 538 | |
Adriana Kobylak | 70f5bc0 | 2020-05-13 14:08:14 -0500 | [diff] [blame] | 539 | mmc_update() { |
Adriana Kobylak | 62f3820 | 2020-09-29 13:04:25 -0500 | [diff] [blame] | 540 | # Update u-boot if needed |
| 541 | bootPartition="mmcblk0boot0" |
| 542 | devUBoot="/dev/${bootPartition}" |
| 543 | imgUBoot="${imgpath}/${version}/image-u-boot" |
| 544 | if [ "$(cmp_uboot "${devUBoot}" "${imgUBoot}")" != "0" ]; then |
| 545 | echo 0 > "/sys/block/${bootPartition}/force_ro" |
| 546 | dd if="${imgUBoot}" of="${devUBoot}" |
| 547 | echo 1 > "/sys/block/${bootPartition}/force_ro" |
| 548 | fi |
| 549 | |
Adriana Kobylak | 70f5bc0 | 2020-05-13 14:08:14 -0500 | [diff] [blame] | 550 | # Update the secondary (non-running) boot and rofs partitions. |
| 551 | label="$(mmc_get_secondary_label)" |
| 552 | |
| 553 | # Update the boot and rootfs partitions, restore their labels after the update |
| 554 | # by getting the partition number mmcblk0pX from their label. |
| 555 | zstd -d -c ${imgpath}/${version}/image-kernel | dd of="/dev/disk/by-partlabel/boot-${label}" |
| 556 | number="$(readlink -f /dev/disk/by-partlabel/boot-${label})" |
| 557 | number="${number##*mmcblk0p}" |
| 558 | sgdisk --change-name=${number}:boot-${label} /dev/mmcblk0 1>/dev/null |
| 559 | |
| 560 | zstd -d -c ${imgpath}/${version}/image-rofs | dd of="/dev/disk/by-partlabel/rofs-${label}" |
| 561 | number="$(readlink -f /dev/disk/by-partlabel/rofs-${label})" |
| 562 | number="${number##*mmcblk0p}" |
| 563 | sgdisk --change-name=${number}:rofs-${label} /dev/mmcblk0 1>/dev/null |
| 564 | |
| 565 | # Run this after sgdisk for labels to take effect. |
| 566 | partprobe |
| 567 | |
Adriana Kobylak | 8c5209d | 2020-07-12 13:35:47 -0500 | [diff] [blame] | 568 | # Update hostfw |
| 569 | if [ -f ${imgpath}/${version}/image-hostfw ]; then |
Adriana Kobylak | d148b4f | 2020-08-27 10:18:49 -0500 | [diff] [blame] | 570 | # Remove patches |
| 571 | patchdir="/usr/local/share/hostfw/alternate" |
| 572 | if [ -d "${patchdir}" ]; then |
| 573 | rm -rf "${patchdir}"/* |
| 574 | fi |
Adriana Kobylak | 8c5209d | 2020-07-12 13:35:47 -0500 | [diff] [blame] | 575 | hostfwdir=$(grep "hostfw " /proc/mounts | cut -d " " -f 2) |
| 576 | cp ${imgpath}/${version}/image-hostfw ${hostfwdir}/hostfw-${label} |
| 577 | mkdir -p ${hostfwdir}/alternate |
| 578 | mount ${hostfwdir}/hostfw-${label} ${hostfwdir}/alternate -o ro |
| 579 | fi |
| 580 | |
Adriana Kobylak | a84f06d | 2022-01-18 15:41:57 +0000 | [diff] [blame] | 581 | set_flashid "${label}" |
Adriana Kobylak | 70f5bc0 | 2020-05-13 14:08:14 -0500 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | mmc_remove() { |
| 585 | # Render the filesystem unbootable by wiping out the first 1MB, this |
| 586 | # invalidates the filesystem header. |
Adriana Kobylak | 25773a7 | 2022-01-21 15:24:48 +0000 | [diff] [blame] | 587 | # Check if the requested id is the one the BMC is running from since dd |
| 588 | # can still write and corrupt the running partition. |
| 589 | primaryid="$(mmc_get_primary_label)" |
| 590 | if [[ "${flashid}" == "${primaryid}" ]]; then |
| 591 | return -1 |
Adriana Kobylak | 70f5bc0 | 2020-05-13 14:08:14 -0500 | [diff] [blame] | 592 | fi |
Adriana Kobylak | 25773a7 | 2022-01-21 15:24:48 +0000 | [diff] [blame] | 593 | dd if=/dev/zero of=/dev/disk/by-partlabel/boot-${flashid} count=2048 |
| 594 | dd if=/dev/zero of=/dev/disk/by-partlabel/rofs-${flashid} count=2048 |
Adriana Kobylak | 8c5209d | 2020-07-12 13:35:47 -0500 | [diff] [blame] | 595 | |
| 596 | hostfw_alt="hostfw/alternate" |
| 597 | if grep -q "${hostfw_alt}" /proc/mounts; then |
| 598 | hostfw_alt=$(grep "${hostfw_alt}" /proc/mounts | cut -d " " -f 2) |
| 599 | umount "${hostfw_alt}" |
| 600 | fi |
| 601 | hostfw_base="hostfw " |
| 602 | if grep -q "${hostfw_base}" /proc/mounts; then |
| 603 | hostfw_base=$(grep "${hostfw_base}" /proc/mounts | cut -d " " -f 2) |
Adriana Kobylak | 25773a7 | 2022-01-21 15:24:48 +0000 | [diff] [blame] | 604 | rm -f ${hostfw_base}/hostfw-${flashid} |
Adriana Kobylak | 8c5209d | 2020-07-12 13:35:47 -0500 | [diff] [blame] | 605 | fi |
Adriana Kobylak | 70f5bc0 | 2020-05-13 14:08:14 -0500 | [diff] [blame] | 606 | } |
| 607 | |
Adriana Kobylak | 3412435 | 2020-05-22 09:40:40 -0500 | [diff] [blame] | 608 | # Set the requested version as primary for the BMC to boot from upon reboot. |
| 609 | mmc_setprimary() { |
Adriana Kobylak | 25773a7 | 2022-01-21 15:24:48 +0000 | [diff] [blame] | 610 | # Point root to the flashid of the requested BMC rootfs. |
| 611 | fw_setenv bootside "${flashid}" |
Adriana Kobylak | 3412435 | 2020-05-22 09:40:40 -0500 | [diff] [blame] | 612 | } |
| 613 | |
Adriana Kobylak | 9cbfa2e | 2019-04-25 14:02:37 -0500 | [diff] [blame] | 614 | case "$1" in |
| 615 | mtduboot) |
| 616 | reqmtd="$2" |
| 617 | version="$3" |
| 618 | imgfile="image-u-boot" |
| 619 | mtd_write |
| 620 | ;; |
| 621 | ubirw) |
| 622 | reqmtd="$2" |
| 623 | name="$3" |
| 624 | imgsize="$4" |
| 625 | ubi_rw |
| 626 | ;; |
| 627 | ubiro) |
| 628 | reqmtd="$(echo "$2" | cut -d "+" -f 1)" |
| 629 | reqmtd2="$(echo "$2" | cut -d "+" -f 2)" |
| 630 | name="$3" |
| 631 | version="$4" |
| 632 | imgfile="image-rofs" |
| 633 | ubi_ro |
| 634 | ubi_updatevol |
| 635 | ubi_block |
| 636 | ;; |
| 637 | ubikernel) |
| 638 | reqmtd="$(echo "$2" | cut -d "+" -f 1)" |
| 639 | reqmtd2="$(echo "$2" | cut -d "+" -f 2)" |
| 640 | name="$3" |
| 641 | version="$4" |
| 642 | imgfile="image-kernel" |
| 643 | ubi_ro |
| 644 | ubi_updatevol |
| 645 | create_vol_in_alt |
| 646 | ;; |
| 647 | ubiremove) |
| 648 | name="$2" |
| 649 | ubi_remove "${name}" |
| 650 | ;; |
| 651 | ubicleanup) |
| 652 | ubi_cleanup |
| 653 | ;; |
| 654 | ubisetenv) |
| 655 | ubi_setenv "$2" |
| 656 | ;; |
| 657 | ubiremount) |
| 658 | remount_ubi |
| 659 | mount_alt_rwfs |
| 660 | ;; |
| 661 | createenvbackup) |
| 662 | backup_env_vars |
| 663 | ;; |
| 664 | updateubootvars) |
Adriana Kobylak | 25773a7 | 2022-01-21 15:24:48 +0000 | [diff] [blame] | 665 | flashid="$2" |
Adriana Kobylak | 9cbfa2e | 2019-04-25 14:02:37 -0500 | [diff] [blame] | 666 | update_env_vars |
| 667 | ;; |
| 668 | rebootguardenable) |
| 669 | rebootguardenable |
| 670 | ;; |
| 671 | rebootguarddisable) |
| 672 | rebootguarddisable |
| 673 | ;; |
| 674 | mirroruboot) |
| 675 | mirroruboot |
| 676 | ;; |
Adriana Kobylak | 70f5bc0 | 2020-05-13 14:08:14 -0500 | [diff] [blame] | 677 | mmc) |
| 678 | version="$2" |
| 679 | imgpath="$3" |
| 680 | mmc_update |
| 681 | ;; |
Adriana Kobylak | 1e81f23 | 2022-01-18 22:28:47 +0000 | [diff] [blame] | 682 | mmc-mount) |
| 683 | mediaDir="$2" |
| 684 | mmc_mount |
| 685 | ;; |
Adriana Kobylak | 70f5bc0 | 2020-05-13 14:08:14 -0500 | [diff] [blame] | 686 | mmc-remove) |
Adriana Kobylak | 25773a7 | 2022-01-21 15:24:48 +0000 | [diff] [blame] | 687 | flashid="$2" |
Adriana Kobylak | 70f5bc0 | 2020-05-13 14:08:14 -0500 | [diff] [blame] | 688 | mmc_remove |
| 689 | ;; |
Adriana Kobylak | 3412435 | 2020-05-22 09:40:40 -0500 | [diff] [blame] | 690 | mmc-setprimary) |
Adriana Kobylak | 25773a7 | 2022-01-21 15:24:48 +0000 | [diff] [blame] | 691 | flashid="$2" |
Adriana Kobylak | 3412435 | 2020-05-22 09:40:40 -0500 | [diff] [blame] | 692 | mmc_setprimary |
| 693 | ;; |
Adriana Kobylak | 9cbfa2e | 2019-04-25 14:02:37 -0500 | [diff] [blame] | 694 | *) |
| 695 | echo "Invalid argument" |
| 696 | exit 1 |
| 697 | ;; |
| 698 | esac |