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 | |
Lei YU | 6376964 | 2021-12-10 16:15:04 +0800 | [diff] [blame] | 248 | mount_ubi_alt_rwfs() { |
Adriana Kobylak | 9cbfa2e | 2019-04-25 14:02:37 -0500 | [diff] [blame] | 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 | |
Lei YU | 6376964 | 2021-12-10 16:15:04 +0800 | [diff] [blame] | 307 | mount_static_alt() { |
| 308 | typ=$1 |
| 309 | altFs=$2 |
| 310 | mountName=$3 |
| 311 | altNum="$(findmtdnum ${altFs})" |
| 312 | if [ ! -z "${altNum}" ]; then |
| 313 | altFsMount="/run/media/${mountName}" |
| 314 | mkdir -p "${altFsMount}" |
| 315 | altFsBlock="/dev/mtdblock${altNum}" |
| 316 | mount -t "${typ}" "${altFsBlock}" "${altFsMount}" |
| 317 | fi |
| 318 | } |
| 319 | |
Adriana Kobylak | 9cbfa2e | 2019-04-25 14:02:37 -0500 | [diff] [blame] | 320 | # Read the current env variable and set it on the alternate boot env |
| 321 | copy_env_var_to_alt() { |
| 322 | varName=$1 |
| 323 | value="$(fw_printenv -n "${varName}")" |
| 324 | fw_setenv -c /etc/alt_fw_env.config "${varName}" "${value}" |
| 325 | } |
| 326 | |
| 327 | # When the alternate bmc chip boots, u-boot thinks its the primary mtdX. |
| 328 | # Therefore need to swap the chip numbers when copying the ubiblock and root to |
| 329 | # alternate bmc u-boot environment. |
| 330 | copy_ubiblock_to_alt() { |
| 331 | value="$(fw_printenv -n ubiblock)" |
| 332 | bmcNum="$(findmtdnum "bmc")" |
| 333 | altNum="$(findmtdnum "alt-bmc")" |
| 334 | replaceAlt="${value/${altNum},/${bmcNum},}" |
| 335 | |
| 336 | if [[ "${value}" == "${replaceAlt}" ]]; then |
| 337 | replaceBmc="${value/${bmcNum},/${altNum},}" |
| 338 | value=${replaceBmc} |
| 339 | else |
| 340 | value=${replaceAlt} |
| 341 | fi |
| 342 | |
| 343 | fw_setenv -c /etc/alt_fw_env.config ubiblock "${value}" |
| 344 | } |
| 345 | |
| 346 | copy_root_to_alt() { |
| 347 | value="$(fw_printenv -n root)" |
| 348 | bmcNum="$(findmtdnum "bmc")" |
| 349 | altNum="$(findmtdnum "alt-bmc")" |
| 350 | replaceAlt="${value/${altNum}_/${bmcNum}_}" |
| 351 | |
| 352 | if [[ "${value}" == "${replaceAlt}" ]]; then |
| 353 | replaceBmc="${value/${bmcNum}_/${altNum}_}" |
| 354 | value=${replaceBmc} |
| 355 | else |
| 356 | value=${replaceAlt} |
| 357 | fi |
| 358 | |
| 359 | fw_setenv -c /etc/alt_fw_env.config root "${value}" |
| 360 | } |
| 361 | |
| 362 | ubi_setenv() { |
| 363 | # The U-Boot environment maintains two banks of environment variables. |
| 364 | # The banks need to be consistent with each other to ensure that these |
| 365 | # variables can reliably be read from file. In order to guarantee that the |
| 366 | # banks are both correct, we need to run fw_setenv twice. |
| 367 | variable=$1 |
| 368 | if [[ "$variable" == *"="* ]]; then |
| 369 | varName="${variable%=*}" |
| 370 | value="${variable##*=}" |
| 371 | # Write only if var is not set already to the requested value |
| 372 | currentValue="$(fw_printenv -n "${varName}" 2>/dev/null)" || true |
| 373 | if [[ "${currenValue}" != "${value}" ]]; then |
| 374 | fw_setenv "$varName" "$value" |
| 375 | fw_setenv "$varName" "$value" |
| 376 | fi |
| 377 | else |
| 378 | fw_setenv "$variable" |
| 379 | fw_setenv "$variable" |
| 380 | fi |
| 381 | } |
| 382 | |
| 383 | mtd_write() { |
| 384 | flashmtd="$(findmtd "${reqmtd}")" |
| 385 | img="/tmp/images/${version}/${imgfile}" |
| 386 | flashcp -v ${img} /dev/${flashmtd} |
| 387 | } |
| 388 | |
| 389 | backup_env_vars() { |
| 390 | copy_env_var_to_alt kernelname |
| 391 | copy_ubiblock_to_alt |
| 392 | copy_root_to_alt |
| 393 | } |
| 394 | |
| 395 | update_env_vars() { |
Adriana Kobylak | 25773a7 | 2022-01-21 15:24:48 +0000 | [diff] [blame] | 396 | vol="$(findubi rofs-"${flashid}")" |
Adriana Kobylak | 9cbfa2e | 2019-04-25 14:02:37 -0500 | [diff] [blame] | 397 | if [ -z "${vol}" ]; then |
| 398 | return 1 |
| 399 | fi |
| 400 | ubidevid="${vol#ubi}" |
| 401 | block="/dev/ubiblock${ubidevid}" |
| 402 | if [ ! -e "${block}" ]; then |
| 403 | return 1 |
| 404 | fi |
Adriana Kobylak | 25773a7 | 2022-01-21 15:24:48 +0000 | [diff] [blame] | 405 | ubi_setenv "kernelname=kernel-${flashid}" |
Adriana Kobylak | 9cbfa2e | 2019-04-25 14:02:37 -0500 | [diff] [blame] | 406 | ubi_setenv "ubiblock=$(echo "${ubidevid}" | sed 's/_/,/')" |
| 407 | ubi_setenv "root=${block}" |
| 408 | } |
| 409 | |
| 410 | #TODO: Replace the implementation with systemd-inhibitors lock |
| 411 | # once systemd/systemd#949 is resolved |
| 412 | rebootguardenable() { |
| 413 | dir="/run/systemd/system/" |
| 414 | file="reboot-guard.conf" |
| 415 | units=("reboot" "poweroff" "halt") |
| 416 | |
| 417 | for unit in "${units[@]}"; do |
| 418 | mkdir -p ${dir}${unit}.target.d |
| 419 | echo -e "[Unit]\nRefuseManualStart=yes" >> ${dir}${unit}.target.d/${file} |
| 420 | done |
| 421 | } |
| 422 | |
| 423 | #TODO: Replace the implementation with systemd-inhibitors lock |
| 424 | # once systemd/systemd#949 is resolved |
| 425 | rebootguarddisable() { |
| 426 | dir="/run/systemd/system/" |
| 427 | file="reboot-guard.conf" |
| 428 | units=("reboot" "poweroff" "halt") |
| 429 | |
| 430 | for unit in "${units[@]}"; do |
| 431 | rm -rf ${dir}${unit}.target.d/${file} |
| 432 | done |
| 433 | } |
| 434 | |
| 435 | # Create a copy in the alt mtd |
| 436 | create_vol_in_alt() { |
| 437 | alt="alt-bmc" |
| 438 | altmtd="$(findmtd "${alt}")" |
| 439 | if [ ! -z "${altmtd}" ]; then |
| 440 | reqmtd="${alt}" |
| 441 | reqmtd2="${alt}" |
| 442 | ubi_ro |
| 443 | ubi_updatevol |
| 444 | fi |
| 445 | } |
| 446 | |
| 447 | # Copy contents of one MTD device to another |
| 448 | mtd_copy() { |
| 449 | in=$1 |
| 450 | out=$2 |
| 451 | |
| 452 | # Must erase MTD first to prevent corruption |
| 453 | flash_eraseall "${out}" |
| 454 | dd if="${in}" of="${out}" |
| 455 | } |
| 456 | |
| 457 | mirroruboot() { |
| 458 | bmc="$(findmtd "u-boot")" |
| 459 | bmcdev="/dev/${bmc}" |
| 460 | alt="$(findmtd "alt-u-boot")" |
| 461 | altdev="/dev/${alt}" |
| 462 | |
| 463 | checksum_bmc="$(md5sum "${bmcdev}")" |
| 464 | checksum_bmc="${checksum_bmc% *}" |
| 465 | checksum_alt="$(md5sum "${altdev}")" |
| 466 | checksum_alt="${checksum_alt% *}" |
| 467 | |
| 468 | if [[ "${checksum_bmc}" != "${checksum_alt}" ]]; then |
| 469 | bmcenv="$(findmtd "u-boot-env")" |
| 470 | bmcenvdev="/dev/${bmcenv}" |
| 471 | altenv="$(findmtd "alt-u-boot-env")" |
| 472 | altenvdev="/dev/${altenv}" |
| 473 | |
| 474 | echo "Mirroring U-boot to alt chip" |
| 475 | mtd_copy "${bmcdev}" "${altdev}" |
| 476 | mtd_copy "${bmcenvdev}" "${altenvdev}" |
| 477 | |
| 478 | copy_ubiblock_to_alt |
| 479 | copy_root_to_alt |
| 480 | fi |
| 481 | } |
| 482 | |
Adriana Kobylak | 62f3820 | 2020-09-29 13:04:25 -0500 | [diff] [blame] | 483 | # Compare the device where u-boot resides with an image file. Specify the full |
| 484 | # path to the device and image file to use for the compare. Print a value of |
| 485 | # "0" if identical, "1" otherwise. |
| 486 | cmp_uboot() { |
| 487 | device="$1" |
| 488 | image="$2" |
| 489 | |
| 490 | # Since the image file can be smaller than the device, copy the device to a |
| 491 | # tmp file and write the image file on top, then compare the sum of each. |
| 492 | # Use cat / redirection since busybox does not have the conv=notrunc option. |
Adriana Kobylak | 9b8816f | 2020-12-02 11:08:15 -0600 | [diff] [blame] | 493 | tmpFile="$(mktemp /tmp/ubootdev.XXXXXX)" |
Adriana Kobylak | 62f3820 | 2020-09-29 13:04:25 -0500 | [diff] [blame] | 494 | dd if="${device}" of="${tmpFile}" |
| 495 | devSum="$(sha256sum ${tmpFile})" |
| 496 | cat < "${image}" 1<> "${tmpFile}" |
| 497 | imgSum="$(sha256sum ${tmpFile})" |
| 498 | rm -f "${tmpFile}" |
| 499 | |
| 500 | if [ "${imgSum}" == "${devSum}" ]; then |
| 501 | echo "0"; |
| 502 | else |
| 503 | echo "1"; |
| 504 | fi |
| 505 | } |
| 506 | |
Adriana Kobylak | 70f5bc0 | 2020-05-13 14:08:14 -0500 | [diff] [blame] | 507 | # 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] | 508 | # Return the label (a or b) for the running partition. |
| 509 | mmc_get_primary_label() { |
Adriana Kobylak | 5312d85 | 2020-07-18 09:49:34 -0500 | [diff] [blame] | 510 | # Get root device /dev/mmcblkpX |
Adriana Kobylak | 3412435 | 2020-05-22 09:40:40 -0500 | [diff] [blame] | 511 | rootmatch=" on / " |
Adriana Kobylak | 5312d85 | 2020-07-18 09:49:34 -0500 | [diff] [blame] | 512 | root="$(mount | grep "${rootmatch}")" |
| 513 | root="${root%${rootmatch}*}" |
| 514 | |
| 515 | # Find the device label |
| 516 | if [[ $(readlink -f /dev/disk/by-partlabel/rofs-a) == "${root}" ]]; then |
Adriana Kobylak | 3412435 | 2020-05-22 09:40:40 -0500 | [diff] [blame] | 517 | echo "a" |
Adriana Kobylak | 5312d85 | 2020-07-18 09:49:34 -0500 | [diff] [blame] | 518 | elif [[ $(readlink -f /dev/disk/by-partlabel/rofs-b) == "${root}" ]]; then |
Adriana Kobylak | 3412435 | 2020-05-22 09:40:40 -0500 | [diff] [blame] | 519 | echo "b" |
Adriana Kobylak | 5312d85 | 2020-07-18 09:49:34 -0500 | [diff] [blame] | 520 | else |
| 521 | echo "" |
Adriana Kobylak | 3412435 | 2020-05-22 09:40:40 -0500 | [diff] [blame] | 522 | fi |
| 523 | } |
| 524 | |
| 525 | # 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] | 526 | # Return the label (a or b) for the non-running partition. |
| 527 | mmc_get_secondary_label() { |
Adriana Kobylak | 5312d85 | 2020-07-18 09:49:34 -0500 | [diff] [blame] | 528 | root="$(mmc_get_primary_label)" |
| 529 | if [[ "${root}" == "a" ]]; then |
Adriana Kobylak | 70f5bc0 | 2020-05-13 14:08:14 -0500 | [diff] [blame] | 530 | echo "b" |
Adriana Kobylak | 5312d85 | 2020-07-18 09:49:34 -0500 | [diff] [blame] | 531 | elif [[ "${root}" == "b" ]]; then |
Adriana Kobylak | 70f5bc0 | 2020-05-13 14:08:14 -0500 | [diff] [blame] | 532 | echo "a" |
Adriana Kobylak | 5312d85 | 2020-07-18 09:49:34 -0500 | [diff] [blame] | 533 | else |
| 534 | echo "" |
Adriana Kobylak | 70f5bc0 | 2020-05-13 14:08:14 -0500 | [diff] [blame] | 535 | fi |
| 536 | } |
| 537 | |
Adriana Kobylak | 1e81f23 | 2022-01-18 22:28:47 +0000 | [diff] [blame] | 538 | mmc_mount() { |
| 539 | primaryId="$(mmc_get_primary_label)" |
| 540 | secondaryId="$(mmc_get_secondary_label)" |
| 541 | |
| 542 | primaryDir="${mediaDir}/rofs-${primaryId}-functional" |
| 543 | secondaryDir="${mediaDir}/rofs-${secondaryId}" |
| 544 | |
| 545 | mkdir -p "${primaryDir}" |
| 546 | mkdir -p "${secondaryDir}" |
| 547 | |
| 548 | mount PARTLABEL=rofs-${primaryId} "${primaryDir}" -t ext4 -o ro || rmdir "${primaryDir}" |
| 549 | mount PARTLABEL=rofs-${secondaryId} "${secondaryDir}" -t ext4 -o ro || rmdir "${secondaryDir}" |
| 550 | } |
| 551 | |
Adriana Kobylak | 70f5bc0 | 2020-05-13 14:08:14 -0500 | [diff] [blame] | 552 | mmc_update() { |
Adriana Kobylak | 62f3820 | 2020-09-29 13:04:25 -0500 | [diff] [blame] | 553 | # Update u-boot if needed |
| 554 | bootPartition="mmcblk0boot0" |
| 555 | devUBoot="/dev/${bootPartition}" |
| 556 | imgUBoot="${imgpath}/${version}/image-u-boot" |
| 557 | if [ "$(cmp_uboot "${devUBoot}" "${imgUBoot}")" != "0" ]; then |
| 558 | echo 0 > "/sys/block/${bootPartition}/force_ro" |
| 559 | dd if="${imgUBoot}" of="${devUBoot}" |
| 560 | echo 1 > "/sys/block/${bootPartition}/force_ro" |
| 561 | fi |
| 562 | |
Adriana Kobylak | 70f5bc0 | 2020-05-13 14:08:14 -0500 | [diff] [blame] | 563 | # Update the secondary (non-running) boot and rofs partitions. |
| 564 | label="$(mmc_get_secondary_label)" |
| 565 | |
| 566 | # Update the boot and rootfs partitions, restore their labels after the update |
| 567 | # by getting the partition number mmcblk0pX from their label. |
| 568 | zstd -d -c ${imgpath}/${version}/image-kernel | dd of="/dev/disk/by-partlabel/boot-${label}" |
| 569 | number="$(readlink -f /dev/disk/by-partlabel/boot-${label})" |
| 570 | number="${number##*mmcblk0p}" |
| 571 | sgdisk --change-name=${number}:boot-${label} /dev/mmcblk0 1>/dev/null |
| 572 | |
| 573 | zstd -d -c ${imgpath}/${version}/image-rofs | dd of="/dev/disk/by-partlabel/rofs-${label}" |
| 574 | number="$(readlink -f /dev/disk/by-partlabel/rofs-${label})" |
| 575 | number="${number##*mmcblk0p}" |
| 576 | sgdisk --change-name=${number}:rofs-${label} /dev/mmcblk0 1>/dev/null |
| 577 | |
| 578 | # Run this after sgdisk for labels to take effect. |
| 579 | partprobe |
| 580 | |
Adriana Kobylak | 8c5209d | 2020-07-12 13:35:47 -0500 | [diff] [blame] | 581 | # Update hostfw |
| 582 | if [ -f ${imgpath}/${version}/image-hostfw ]; then |
Adriana Kobylak | d148b4f | 2020-08-27 10:18:49 -0500 | [diff] [blame] | 583 | # Remove patches |
| 584 | patchdir="/usr/local/share/hostfw/alternate" |
| 585 | if [ -d "${patchdir}" ]; then |
| 586 | rm -rf "${patchdir}"/* |
| 587 | fi |
Adriana Kobylak | 8c5209d | 2020-07-12 13:35:47 -0500 | [diff] [blame] | 588 | hostfwdir=$(grep "hostfw " /proc/mounts | cut -d " " -f 2) |
| 589 | cp ${imgpath}/${version}/image-hostfw ${hostfwdir}/hostfw-${label} |
| 590 | mkdir -p ${hostfwdir}/alternate |
| 591 | mount ${hostfwdir}/hostfw-${label} ${hostfwdir}/alternate -o ro |
| 592 | fi |
| 593 | |
Adriana Kobylak | a84f06d | 2022-01-18 15:41:57 +0000 | [diff] [blame] | 594 | set_flashid "${label}" |
Adriana Kobylak | 70f5bc0 | 2020-05-13 14:08:14 -0500 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | mmc_remove() { |
| 598 | # Render the filesystem unbootable by wiping out the first 1MB, this |
| 599 | # invalidates the filesystem header. |
Adriana Kobylak | 25773a7 | 2022-01-21 15:24:48 +0000 | [diff] [blame] | 600 | # Check if the requested id is the one the BMC is running from since dd |
| 601 | # can still write and corrupt the running partition. |
| 602 | primaryid="$(mmc_get_primary_label)" |
| 603 | if [[ "${flashid}" == "${primaryid}" ]]; then |
| 604 | return -1 |
Adriana Kobylak | 70f5bc0 | 2020-05-13 14:08:14 -0500 | [diff] [blame] | 605 | fi |
Adriana Kobylak | 25773a7 | 2022-01-21 15:24:48 +0000 | [diff] [blame] | 606 | dd if=/dev/zero of=/dev/disk/by-partlabel/boot-${flashid} count=2048 |
| 607 | 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] | 608 | |
| 609 | hostfw_alt="hostfw/alternate" |
| 610 | if grep -q "${hostfw_alt}" /proc/mounts; then |
| 611 | hostfw_alt=$(grep "${hostfw_alt}" /proc/mounts | cut -d " " -f 2) |
| 612 | umount "${hostfw_alt}" |
| 613 | fi |
| 614 | hostfw_base="hostfw " |
| 615 | if grep -q "${hostfw_base}" /proc/mounts; then |
| 616 | hostfw_base=$(grep "${hostfw_base}" /proc/mounts | cut -d " " -f 2) |
Adriana Kobylak | 25773a7 | 2022-01-21 15:24:48 +0000 | [diff] [blame] | 617 | rm -f ${hostfw_base}/hostfw-${flashid} |
Adriana Kobylak | 8c5209d | 2020-07-12 13:35:47 -0500 | [diff] [blame] | 618 | fi |
Adriana Kobylak | 70f5bc0 | 2020-05-13 14:08:14 -0500 | [diff] [blame] | 619 | } |
| 620 | |
Adriana Kobylak | 3412435 | 2020-05-22 09:40:40 -0500 | [diff] [blame] | 621 | # Set the requested version as primary for the BMC to boot from upon reboot. |
| 622 | mmc_setprimary() { |
Adriana Kobylak | 25773a7 | 2022-01-21 15:24:48 +0000 | [diff] [blame] | 623 | # Point root to the flashid of the requested BMC rootfs. |
| 624 | fw_setenv bootside "${flashid}" |
Adriana Kobylak | 3412435 | 2020-05-22 09:40:40 -0500 | [diff] [blame] | 625 | } |
| 626 | |
Adriana Kobylak | 9cbfa2e | 2019-04-25 14:02:37 -0500 | [diff] [blame] | 627 | case "$1" in |
| 628 | mtduboot) |
| 629 | reqmtd="$2" |
| 630 | version="$3" |
| 631 | imgfile="image-u-boot" |
| 632 | mtd_write |
| 633 | ;; |
| 634 | ubirw) |
| 635 | reqmtd="$2" |
| 636 | name="$3" |
| 637 | imgsize="$4" |
| 638 | ubi_rw |
| 639 | ;; |
| 640 | ubiro) |
| 641 | reqmtd="$(echo "$2" | cut -d "+" -f 1)" |
| 642 | reqmtd2="$(echo "$2" | cut -d "+" -f 2)" |
| 643 | name="$3" |
| 644 | version="$4" |
| 645 | imgfile="image-rofs" |
| 646 | ubi_ro |
| 647 | ubi_updatevol |
| 648 | ubi_block |
| 649 | ;; |
| 650 | ubikernel) |
| 651 | reqmtd="$(echo "$2" | cut -d "+" -f 1)" |
| 652 | reqmtd2="$(echo "$2" | cut -d "+" -f 2)" |
| 653 | name="$3" |
| 654 | version="$4" |
| 655 | imgfile="image-kernel" |
| 656 | ubi_ro |
| 657 | ubi_updatevol |
| 658 | create_vol_in_alt |
| 659 | ;; |
| 660 | ubiremove) |
| 661 | name="$2" |
| 662 | ubi_remove "${name}" |
| 663 | ;; |
| 664 | ubicleanup) |
| 665 | ubi_cleanup |
| 666 | ;; |
| 667 | ubisetenv) |
| 668 | ubi_setenv "$2" |
| 669 | ;; |
| 670 | ubiremount) |
| 671 | remount_ubi |
Lei YU | 6376964 | 2021-12-10 16:15:04 +0800 | [diff] [blame] | 672 | mount_ubi_alt_rwfs |
Adriana Kobylak | 9cbfa2e | 2019-04-25 14:02:37 -0500 | [diff] [blame] | 673 | ;; |
| 674 | createenvbackup) |
| 675 | backup_env_vars |
| 676 | ;; |
| 677 | updateubootvars) |
Adriana Kobylak | 25773a7 | 2022-01-21 15:24:48 +0000 | [diff] [blame] | 678 | flashid="$2" |
Adriana Kobylak | 9cbfa2e | 2019-04-25 14:02:37 -0500 | [diff] [blame] | 679 | update_env_vars |
| 680 | ;; |
| 681 | rebootguardenable) |
| 682 | rebootguardenable |
| 683 | ;; |
| 684 | rebootguarddisable) |
| 685 | rebootguarddisable |
| 686 | ;; |
| 687 | mirroruboot) |
| 688 | mirroruboot |
| 689 | ;; |
Adriana Kobylak | 70f5bc0 | 2020-05-13 14:08:14 -0500 | [diff] [blame] | 690 | mmc) |
| 691 | version="$2" |
| 692 | imgpath="$3" |
| 693 | mmc_update |
| 694 | ;; |
Adriana Kobylak | 1e81f23 | 2022-01-18 22:28:47 +0000 | [diff] [blame] | 695 | mmc-mount) |
| 696 | mediaDir="$2" |
| 697 | mmc_mount |
| 698 | ;; |
Adriana Kobylak | 70f5bc0 | 2020-05-13 14:08:14 -0500 | [diff] [blame] | 699 | mmc-remove) |
Adriana Kobylak | 25773a7 | 2022-01-21 15:24:48 +0000 | [diff] [blame] | 700 | flashid="$2" |
Adriana Kobylak | 70f5bc0 | 2020-05-13 14:08:14 -0500 | [diff] [blame] | 701 | mmc_remove |
| 702 | ;; |
Adriana Kobylak | 3412435 | 2020-05-22 09:40:40 -0500 | [diff] [blame] | 703 | mmc-setprimary) |
Adriana Kobylak | 25773a7 | 2022-01-21 15:24:48 +0000 | [diff] [blame] | 704 | flashid="$2" |
Adriana Kobylak | 3412435 | 2020-05-22 09:40:40 -0500 | [diff] [blame] | 705 | mmc_setprimary |
| 706 | ;; |
Lei YU | 6376964 | 2021-12-10 16:15:04 +0800 | [diff] [blame] | 707 | static-altfs) |
| 708 | mount_static_alt "$2" "$3" "$4" |
| 709 | ;; |
Adriana Kobylak | 9cbfa2e | 2019-04-25 14:02:37 -0500 | [diff] [blame] | 710 | *) |
| 711 | echo "Invalid argument" |
| 712 | exit 1 |
| 713 | ;; |
| 714 | esac |