19cbfa2efSAdriana Kobylak#!/bin/bash
29cbfa2efSAdriana Kobylakset -eo pipefail
39cbfa2efSAdriana Kobylak
49cbfa2efSAdriana Kobylak# Get the root mtd device number (mtdX) from "/dev/ubiblockX_Y on /"
5780c930eSPatrick Williamsfunction findrootmtd() {
69cbfa2efSAdriana Kobylak    rootmatch=" on / "
79cbfa2efSAdriana Kobylak    m="$(mount | grep "${rootmatch}" | grep "ubiblock")"
89cbfa2efSAdriana Kobylak    m="${m##*ubiblock}"
99cbfa2efSAdriana Kobylak    m="${m%_*}"
109cbfa2efSAdriana Kobylak    if [ -z "${m}" ]; then
119cbfa2efSAdriana Kobylak        # default to bmc mtd (0)
129cbfa2efSAdriana Kobylak        m=0
139cbfa2efSAdriana Kobylak    fi
149cbfa2efSAdriana Kobylak    echo "mtd${m}"
159cbfa2efSAdriana Kobylak}
169cbfa2efSAdriana Kobylak
17780c930eSPatrick Williamsfunction findrootubi() {
189cbfa2efSAdriana Kobylak    rootmatch=" on / "
199cbfa2efSAdriana Kobylak    m="$(mount | grep "${rootmatch}")"
209cbfa2efSAdriana Kobylak    m="${m##*ubiblock}"
219cbfa2efSAdriana Kobylak    m="${m% on*}"
229cbfa2efSAdriana Kobylak    echo "ubi${m}"
239cbfa2efSAdriana Kobylak}
249cbfa2efSAdriana Kobylak
259cbfa2efSAdriana Kobylak# Get the mtd device number (mtdX)
26780c930eSPatrick Williamsfunction findmtd() {
279cbfa2efSAdriana Kobylak    m="$(grep -xl "$1" /sys/class/mtd/*/name)"
289cbfa2efSAdriana Kobylak    m="${m%/name}"
299cbfa2efSAdriana Kobylak    m="${m##*/}"
309cbfa2efSAdriana Kobylak    echo "${m}"
319cbfa2efSAdriana Kobylak}
329cbfa2efSAdriana Kobylak
339cbfa2efSAdriana Kobylak# Get the mtd device number only (return X of mtdX)
34780c930eSPatrick Williamsfunction findmtdnum() {
359cbfa2efSAdriana Kobylak    m="$(findmtd "$1")"
369cbfa2efSAdriana Kobylak    m="${m##mtd}"
379cbfa2efSAdriana Kobylak    echo "${m}"
389cbfa2efSAdriana Kobylak}
399cbfa2efSAdriana Kobylak
409cbfa2efSAdriana Kobylak# Get the ubi device number (ubiX_Y)
41780c930eSPatrick Williamsfunction findubi() {
429cbfa2efSAdriana Kobylak    u="$(grep -xl "$1" /sys/class/ubi/ubi?/subsystem/ubi*/name)"
439cbfa2efSAdriana Kobylak    u="${u%/name}"
449cbfa2efSAdriana Kobylak    u="${u##*/}"
459cbfa2efSAdriana Kobylak    echo "${u}"
469cbfa2efSAdriana Kobylak}
479cbfa2efSAdriana Kobylak
489cbfa2efSAdriana Kobylak# Get the ubi device number (ubiX_Y) on a specific mtd
49780c930eSPatrick Williamsfunction findubi_onmtd() {
509cbfa2efSAdriana Kobylak    u="$(grep -xl "$1" /sys/class/ubi/ubi"$2"/subsystem/ubi"$2"*/name)"
519cbfa2efSAdriana Kobylak    u="${u%/name}"
529cbfa2efSAdriana Kobylak    u="${u##*/}"
539cbfa2efSAdriana Kobylak    echo "${u}"
549cbfa2efSAdriana Kobylak}
559cbfa2efSAdriana Kobylak
569cbfa2efSAdriana Kobylak# Get all ubi device names on a specific mtd that match requested string
57780c930eSPatrick Williamsfunction findubiname_onmtd() {
589cbfa2efSAdriana Kobylak    u="$(grep -h "$1" /sys/class/ubi/ubi"$2"/subsystem/ubi"$2"*/name)"
599cbfa2efSAdriana Kobylak    u="${u%/name}"
609cbfa2efSAdriana Kobylak    u="${u##*/}"
619cbfa2efSAdriana Kobylak    echo "${u}"
629cbfa2efSAdriana Kobylak}
639cbfa2efSAdriana Kobylak
649cbfa2efSAdriana Kobylak# Get the name from the requested ubiX_Y volume
65780c930eSPatrick Williamsfunction findname() {
66c53fccacSIsaac Kurth    n="$(cat /sys/class/ubi/"$1"/name)"
679cbfa2efSAdriana Kobylak    echo "${n}"
689cbfa2efSAdriana Kobylak}
699cbfa2efSAdriana Kobylak
70a84f06d2SAdriana Kobylak# Set the version path property to the flash location where the image was
71a84f06d2SAdriana Kobylak# successfully flashed
72780c930eSPatrick Williamsfunction set_flashid() {
73a84f06d2SAdriana Kobylak    busctl set-property xyz.openbmc_project.Software.BMC.Updater \
74a84f06d2SAdriana Kobylak        "/xyz/openbmc_project/software/${version}" \
75a84f06d2SAdriana Kobylak        xyz.openbmc_project.Common.FilePath \
76a84f06d2SAdriana Kobylak        Path s "$1"
77a84f06d2SAdriana Kobylak}
78a84f06d2SAdriana Kobylak
799cbfa2efSAdriana Kobylak# Set the u-boot envs that perform a side switch on failure to boot
80780c930eSPatrick Williamsfunction set_wdt2bite() {
819cbfa2efSAdriana Kobylak    if ! fw_printenv wdt2bite 2>/dev/null; then
829cbfa2efSAdriana Kobylak        fw_setenv wdt2bite "mw.l 0x1e785024 0xa 1; mw.b 0x1e78502c 0xb3 1"
839cbfa2efSAdriana Kobylak        fw_setenv bootalt "run wdt2bite"
849cbfa2efSAdriana Kobylak        fw_setenv obmc_bootcmd "ubi part obmc-ubi; run do_rwreset; ubi read \
859cbfa2efSAdriana Kobylak\${loadaddr} \${kernelname}; bootm \${loadaddr} || run bootalt"
869cbfa2efSAdriana Kobylak    fi
879cbfa2efSAdriana Kobylak}
889cbfa2efSAdriana Kobylak
899cbfa2efSAdriana Kobylak# Make space on flash before creating new volumes. This can be enhanced
909cbfa2efSAdriana Kobylak# determine current flash usage. For now only keep a "keepmax" number of them
91780c930eSPatrick Williamsfunction ubi_remove_volumes()
929cbfa2efSAdriana Kobylak{
939cbfa2efSAdriana Kobylak    rootubi="$(findrootubi)"
949cbfa2efSAdriana Kobylak    rootname="$(findname "${rootubi}")"
959cbfa2efSAdriana Kobylak    rootversion="${rootname##*-}"
969cbfa2efSAdriana Kobylak    rootkernel="kernel-${rootversion}"
979cbfa2efSAdriana Kobylak
989cbfa2efSAdriana Kobylak    # Just keep max number of volumes before updating, don't delete the version
999cbfa2efSAdriana Kobylak    # the BMC is booted from, and when a version is identified to be deleted,
1009cbfa2efSAdriana Kobylak    # delete both the rofs and kernel volumes for that version.
1019cbfa2efSAdriana Kobylak    rmnames="$(findubiname_onmtd "${name%-*}-" "${ro}")"
102c53fccacSIsaac Kurth    mapfile -t array <<< "${rmnames}"
103c53fccacSIsaac Kurth    ubicount="${#array[@]}"
104c53fccacSIsaac Kurth    while [ "${ubicount}" -ge "${keepmax}" ]; do
1059cbfa2efSAdriana Kobylak        # Loop through existing volumes and skip currently active ones
106c53fccacSIsaac Kurth        for (( index=0; index<${#array[@]}; index++ )); do
107c53fccacSIsaac Kurth            rmname="${array[${index}]}"
1089cbfa2efSAdriana Kobylak            rmversion="${rmname##*-}"
1099cbfa2efSAdriana Kobylak            [ "${rmversion}" == "${version}" ] && continue
1109cbfa2efSAdriana Kobylak            rmubi="$(findubi_onmtd "rofs-${rmversion}" "${ro}")"
111780c930eSPatrick Williams            if [[ "${rmubi}" != "${rootubi}" ]] && \
112780c930eSPatrick Williams                [[ "${rmname}" != "${rootkernel}" ]]; then
1139cbfa2efSAdriana Kobylak                ubi_remove "rofs-${rmversion}" "${ro}"
1149cbfa2efSAdriana Kobylak                ubi_remove "kernel-${rmversion}" "${ro}"
1159cbfa2efSAdriana Kobylak                # Remove priority value
1169cbfa2efSAdriana Kobylak                fw_setenv "${rmversion}"
1179cbfa2efSAdriana Kobylak                break
1189cbfa2efSAdriana Kobylak            fi
1199cbfa2efSAdriana Kobylak        done
1209cbfa2efSAdriana Kobylak        # Decrease count regardless to avoid an infinite loop
1219cbfa2efSAdriana Kobylak        (( ubicount-- ))
1229cbfa2efSAdriana Kobylak    done
1239cbfa2efSAdriana Kobylak}
1249cbfa2efSAdriana Kobylak
125780c930eSPatrick Williamsfunction ubi_rw() {
1269cbfa2efSAdriana Kobylak    rwmtd="$(findmtd "${reqmtd}")"
1279cbfa2efSAdriana Kobylak    rw="${rwmtd#mtd}"
1289cbfa2efSAdriana Kobylak    ubidev="/dev/ubi${rw}"
1299cbfa2efSAdriana Kobylak
1309cbfa2efSAdriana Kobylak    # Update rwfs_size, check imgsize was specified, otherwise it'd clear the var
131c53fccacSIsaac Kurth    if [ -n "$imgsize" ]; then
1329cbfa2efSAdriana Kobylak        rwsize="$(fw_printenv -n rwfs_size 2>/dev/null)" || true
1339cbfa2efSAdriana Kobylak        if [[ "${imgsize}" != "${rwsize}" ]]; then
1349cbfa2efSAdriana Kobylak            fw_setenv rwfs_size "${imgsize}"
1359cbfa2efSAdriana Kobylak        fi
1369cbfa2efSAdriana Kobylak    fi
1379cbfa2efSAdriana Kobylak
1389cbfa2efSAdriana Kobylak    vol="$(findubi "${name}")"
1399cbfa2efSAdriana Kobylak    if [ -z "${vol}" ]; then
1409cbfa2efSAdriana Kobylak        ubimkvol "${ubidev}" -N "${name}" -s "${imgsize}"
1419cbfa2efSAdriana Kobylak    fi
1429cbfa2efSAdriana Kobylak}
1439cbfa2efSAdriana Kobylak
144780c930eSPatrick Williamsfunction ubi_ro() {
1459cbfa2efSAdriana Kobylak    keepmax=2 # Default 2 volumes per mtd
1469cbfa2efSAdriana Kobylak    romtd="$(findmtd "${reqmtd}")"
1479cbfa2efSAdriana Kobylak    romtd2="$(findmtd "${reqmtd2}")"
1489cbfa2efSAdriana Kobylak
1499cbfa2efSAdriana Kobylak    if [ ! "${romtd}" == "${romtd2}" ]; then
1509cbfa2efSAdriana Kobylak        # Request to use alternate mtd device, choose the non-root one
1519cbfa2efSAdriana Kobylak        keepmax=1 # 1 volume on each of the requested mtds
1529cbfa2efSAdriana Kobylak        rootmtd="$(findrootmtd)"
1539cbfa2efSAdriana Kobylak        if [ "${rootmtd}" == "${romtd}" ]; then
1549cbfa2efSAdriana Kobylak            romtd="${romtd2}"
1559cbfa2efSAdriana Kobylak        fi
1569cbfa2efSAdriana Kobylak    fi
1579cbfa2efSAdriana Kobylak    ro="${romtd#mtd}"
1589cbfa2efSAdriana Kobylak    ubidev="/dev/ubi${ro}"
1599cbfa2efSAdriana Kobylak
1609cbfa2efSAdriana Kobylak    ubi_remove_volumes
1619cbfa2efSAdriana Kobylak
1629cbfa2efSAdriana Kobylak    if [ -z "${imgfile}" ]; then
1639cbfa2efSAdriana Kobylak        echo "Unable to create read-only volume. Image file not specified."
1649cbfa2efSAdriana Kobylak        return 1
1659cbfa2efSAdriana Kobylak    fi
1669cbfa2efSAdriana Kobylak
1679cbfa2efSAdriana Kobylak    # Create a ubi volume, dynamically sized to fit BMC image if size unspecified
1689cbfa2efSAdriana Kobylak    img="/tmp/images/${version}/${imgfile}"
169c53fccacSIsaac Kurth    imgsize="$(stat -c '%s' "${img}")"
1709cbfa2efSAdriana Kobylak
1719cbfa2efSAdriana Kobylak    vol="$(findubi "${name}")"
172c53fccacSIsaac Kurth    if [ -n "${vol}" ]; then
1739cbfa2efSAdriana Kobylak        # Allow a duplicate kernel volume on the alt mtd
1749cbfa2efSAdriana Kobylak        if [[ "${name}" =~ "kernel" ]]; then
1759cbfa2efSAdriana Kobylak            vol="$(findubi_onmtd "${name}" "${ro}")"
1769cbfa2efSAdriana Kobylak        fi
1779cbfa2efSAdriana Kobylak    fi
1789cbfa2efSAdriana Kobylak    if [ -z "${vol}" ]; then
1799cbfa2efSAdriana Kobylak        ubimkvol "${ubidev}" -N "${name}" -s "${imgsize}" --type=static
1809cbfa2efSAdriana Kobylak        vol="$(findubi "${name}")"
1819cbfa2efSAdriana Kobylak    fi
182a84f06d2SAdriana Kobylak
183a84f06d2SAdriana Kobylak    set_flashid "${version}"
1849cbfa2efSAdriana Kobylak}
1859cbfa2efSAdriana Kobylak
1869cbfa2efSAdriana Kobylak# Squashfs images need a ubi block
187780c930eSPatrick Williamsfunction ubi_block() {
1889cbfa2efSAdriana Kobylak    vol="$(findubi "${name}")"
1899cbfa2efSAdriana Kobylak    ubidevid="${vol#ubi}"
1909cbfa2efSAdriana Kobylak    block="/dev/ubiblock${ubidevid}"
1919cbfa2efSAdriana Kobylak    if [ ! -e "$block" ]; then
1929cbfa2efSAdriana Kobylak        ubiblock --create "/dev/ubi${ubidevid}"
1939cbfa2efSAdriana Kobylak    fi
1949cbfa2efSAdriana Kobylak}
1959cbfa2efSAdriana Kobylak
196780c930eSPatrick Williamsfunction ubi_updatevol() {
1979cbfa2efSAdriana Kobylak    vol="$(findubi "${name}")"
1989cbfa2efSAdriana Kobylak    ubidevid="${vol#ubi}"
1999cbfa2efSAdriana Kobylak    img="/tmp/images/${version}/${imgfile}"
2009cbfa2efSAdriana Kobylak    ubiupdatevol "/dev/ubi${ubidevid}" "${img}"
2019cbfa2efSAdriana Kobylak}
2029cbfa2efSAdriana Kobylak
203780c930eSPatrick Williamsfunction ubi_remove() {
2049cbfa2efSAdriana Kobylak    rmname="$1"
2059cbfa2efSAdriana Kobylak    rmmtd="$2"
206c53fccacSIsaac Kurth    if [ -n "${rmmtd}" ]; then
2079cbfa2efSAdriana Kobylak        vol="$(findubi_onmtd "${rmname}" "${rmmtd}")"
2089cbfa2efSAdriana Kobylak    else
2099cbfa2efSAdriana Kobylak        vol="$(findubi "${rmname}")"
2109cbfa2efSAdriana Kobylak    fi
2119cbfa2efSAdriana Kobylak
212c53fccacSIsaac Kurth    if [ -n "$vol" ]; then
2139cbfa2efSAdriana Kobylak        vol="${vol%_*}"
2149cbfa2efSAdriana Kobylak
2159cbfa2efSAdriana Kobylak        if grep -q "$rmname" /proc/mounts; then
2169cbfa2efSAdriana Kobylak            mountdir=$(grep "$rmname" /proc/mounts | cut -d " " -f 2)
2179cbfa2efSAdriana Kobylak            umount "$mountdir"
2189cbfa2efSAdriana Kobylak            rm -r "$mountdir"
2199cbfa2efSAdriana Kobylak        fi
2209cbfa2efSAdriana Kobylak
2219cbfa2efSAdriana Kobylak        ubirmvol "/dev/${vol}" -N "$rmname"
2229cbfa2efSAdriana Kobylak    fi
2239cbfa2efSAdriana Kobylak}
2249cbfa2efSAdriana Kobylak
225780c930eSPatrick Williamsfunction ubi_cleanup() {
2269cbfa2efSAdriana Kobylak    # When ubi_cleanup is run, it expects one or no active version.
2279cbfa2efSAdriana Kobylak    activeVersion=$(busctl --list --no-pager tree \
2289cbfa2efSAdriana Kobylak            xyz.openbmc_project.Software.BMC.Updater | \
2299cbfa2efSAdriana Kobylak        grep /xyz/openbmc_project/software/ | tail -c 9)
2309cbfa2efSAdriana Kobylak
2319cbfa2efSAdriana Kobylak    if [[ -z "$activeVersion" ]]; then
2329cbfa2efSAdriana Kobylak        vols=$(ubinfo -a | grep "rofs-" | cut -c 14-)
2339cbfa2efSAdriana Kobylak    else
23425773a7eSAdriana Kobylak        flashid=$(busctl get-property xyz.openbmc_project.Software.BMC.Updater \
23525773a7eSAdriana Kobylak                "/xyz/openbmc_project/software/${activeVersion}" \
23625773a7eSAdriana Kobylak            xyz.openbmc_project.Common.FilePath Path |  awk '{print $NF;}' | tr -d '"')
2379cbfa2efSAdriana Kobylak        vols=$(ubinfo -a | grep "rofs-" | \
23825773a7eSAdriana Kobylak            grep -v "$flashid" | cut -c 14-) || true
2399cbfa2efSAdriana Kobylak    fi
2409cbfa2efSAdriana Kobylak
241c53fccacSIsaac Kurth    mapfile -t array <<< "${vols}"
242c53fccacSIsaac Kurth    for (( index=0; index<${#array[@]}; index++ )); do
243c53fccacSIsaac Kurth        ubi_remove "${array[index]}"
2449cbfa2efSAdriana Kobylak    done
2459cbfa2efSAdriana Kobylak}
2469cbfa2efSAdriana Kobylak
247780c930eSPatrick Williamsfunction mount_ubi_alt_rwfs() {
2489cbfa2efSAdriana Kobylak    altNum="$(findmtdnum "alt-bmc")"
249c53fccacSIsaac Kurth    if [ -n "${altNum}" ]; then
250c53fccacSIsaac Kurth        altRwfs=$(ubinfo -a -d "${altNum}" | grep -w "rwfs") || true
251c53fccacSIsaac Kurth        if [ -n "${altRwfs}" ]; then
2529cbfa2efSAdriana Kobylak            altVarMount="/media/alt/var"
2539cbfa2efSAdriana Kobylak            mkdir -p "${altVarMount}"
2549cbfa2efSAdriana Kobylak            if mount ubi"${altNum}":rwfs "${altVarMount}" -t ubifs -o defaults; then
2559cbfa2efSAdriana Kobylak                mkdir -p "${altVarMount}"/persist/etc
2569cbfa2efSAdriana Kobylak            fi
2579cbfa2efSAdriana Kobylak        fi
2589cbfa2efSAdriana Kobylak    fi
2599cbfa2efSAdriana Kobylak}
2609cbfa2efSAdriana Kobylak
261780c930eSPatrick Williamsfunction remount_ubi() {
2629cbfa2efSAdriana Kobylak    bmcmtd="$(findmtd "bmc")"
2639cbfa2efSAdriana Kobylak    altbmcmtd="$(findmtd "alt-bmc")"
2649cbfa2efSAdriana Kobylak    mtds="${bmcmtd: -1}","${altbmcmtd: -1}"
2659cbfa2efSAdriana Kobylak
2661e81f23cSAdriana Kobylak    rootubi="$(findrootubi)"
2671e81f23cSAdriana Kobylak    rootname="$(findname "${rootubi}")"
2681e81f23cSAdriana Kobylak
269c53fccacSIsaac Kurth    IFS=',' read -r -a arrayMtds <<< "$mtds"
270c53fccacSIsaac Kurth    for mtd in "${arrayMtds[@]}"; do
2719cbfa2efSAdriana Kobylak        # Get information on all ubi volumes
272c53fccacSIsaac Kurth        ubinfo=$(ubinfo -d "${mtd}")
2739cbfa2efSAdriana Kobylak        presentVolumes=${ubinfo##*:}
2749cbfa2efSAdriana Kobylak        IFS=', ' read -r -a array <<< "$presentVolumes"
275c53fccacSIsaac Kurth        for element in "${array[@]}"; do
276c53fccacSIsaac Kurth            elementProperties=$(ubinfo -d "$mtd" -n "$element")
2779cbfa2efSAdriana Kobylak            # Get ubi volume name by getting rid of additional properties
2789cbfa2efSAdriana Kobylak            name=${elementProperties#*Name:}
2799cbfa2efSAdriana Kobylak            name="${name%Character*}"
2809cbfa2efSAdriana Kobylak            name="$(echo -e "${name}" | tr -d '[:space:]')"
2819cbfa2efSAdriana Kobylak
2829cbfa2efSAdriana Kobylak            if [[ ${name} == rofs-* ]]; then
2831e81f23cSAdriana Kobylak                if [[ "${name}" == "${rootname}" ]]; then
2841e81f23cSAdriana Kobylak                    mountdir="/media/${name}-functional"
2851e81f23cSAdriana Kobylak                else
2869cbfa2efSAdriana Kobylak                    mountdir="/media/${name}"
2871e81f23cSAdriana Kobylak                fi
2889cbfa2efSAdriana Kobylak
289c53fccacSIsaac Kurth                if [ ! -d "${mountdir}" ]; then
2909cbfa2efSAdriana Kobylak                    mkdir -p "${mountdir}"
2919cbfa2efSAdriana Kobylak                    # U-Boot will create the ubiblock for the running version, but not
2929cbfa2efSAdriana Kobylak                    # for the version on the other chip
2939cbfa2efSAdriana Kobylak                    if [ ! -e "/dev/ubiblock${mtd}_${element}" ]; then
294c53fccacSIsaac Kurth                        ubiblock --create "/dev/ubi${mtd}_${element}"
2959cbfa2efSAdriana Kobylak                    fi
2969cbfa2efSAdriana Kobylak                    mount -t squashfs -o ro "/dev/ubiblock${mtd}_${element}" "${mountdir}"
2979cbfa2efSAdriana Kobylak                fi
2989cbfa2efSAdriana Kobylak            fi
2999cbfa2efSAdriana Kobylak        done
3009cbfa2efSAdriana Kobylak    done
3019cbfa2efSAdriana Kobylak
3029cbfa2efSAdriana Kobylak    set_wdt2bite
3039cbfa2efSAdriana Kobylak}
3049cbfa2efSAdriana Kobylak
305780c930eSPatrick Williamsfunction mount_static_alt() {
3066376964eSLei YU    typ=$1
3076376964eSLei YU    altFs=$2
3086376964eSLei YU    mountName=$3
309c53fccacSIsaac Kurth    altNum="$(findmtdnum "${altFs}")"
310c53fccacSIsaac Kurth    if [ -n "${altNum}" ]; then
3116376964eSLei YU        altFsMount="/run/media/${mountName}"
3126376964eSLei YU        mkdir -p "${altFsMount}"
3136376964eSLei YU        altFsBlock="/dev/mtdblock${altNum}"
3146376964eSLei YU        mount -t "${typ}" "${altFsBlock}" "${altFsMount}"
3156376964eSLei YU    fi
3166376964eSLei YU}
3176376964eSLei YU
318780c930eSPatrick Williamsfunction umount_static_alt() {
319b5171acdSLei YU    altFs=$1
320b5171acdSLei YU    altFsMount="/run/media/${altFs}"
321b5171acdSLei YU    umount "${altFsMount}"
322b5171acdSLei YU}
323b5171acdSLei YU
3249cbfa2efSAdriana Kobylak# Read the current env variable and set it on the alternate boot env
325780c930eSPatrick Williamsfunction copy_env_var_to_alt() {
3269cbfa2efSAdriana Kobylak    varName=$1
3279cbfa2efSAdriana Kobylak    value="$(fw_printenv -n "${varName}")"
3289cbfa2efSAdriana Kobylak    fw_setenv -c /etc/alt_fw_env.config "${varName}" "${value}"
3299cbfa2efSAdriana Kobylak}
3309cbfa2efSAdriana Kobylak
3319cbfa2efSAdriana Kobylak# When the alternate bmc chip boots, u-boot thinks its the primary mtdX.
3329cbfa2efSAdriana Kobylak# Therefore need to swap the chip numbers when copying the ubiblock and root to
3339cbfa2efSAdriana Kobylak# alternate bmc u-boot environment.
334780c930eSPatrick Williamsfunction copy_ubiblock_to_alt() {
3359cbfa2efSAdriana Kobylak    value="$(fw_printenv -n ubiblock)"
3369cbfa2efSAdriana Kobylak    bmcNum="$(findmtdnum "bmc")"
3379cbfa2efSAdriana Kobylak    altNum="$(findmtdnum "alt-bmc")"
3389cbfa2efSAdriana Kobylak    replaceAlt="${value/${altNum},/${bmcNum},}"
3399cbfa2efSAdriana Kobylak
3409cbfa2efSAdriana Kobylak    if [[ "${value}" == "${replaceAlt}" ]]; then
3419cbfa2efSAdriana Kobylak        replaceBmc="${value/${bmcNum},/${altNum},}"
3429cbfa2efSAdriana Kobylak        value=${replaceBmc}
3439cbfa2efSAdriana Kobylak    else
3449cbfa2efSAdriana Kobylak        value=${replaceAlt}
3459cbfa2efSAdriana Kobylak    fi
3469cbfa2efSAdriana Kobylak
3479cbfa2efSAdriana Kobylak    fw_setenv -c /etc/alt_fw_env.config ubiblock "${value}"
3489cbfa2efSAdriana Kobylak}
3499cbfa2efSAdriana Kobylak
350780c930eSPatrick Williamsfunction copy_root_to_alt() {
3519cbfa2efSAdriana Kobylak    value="$(fw_printenv -n root)"
3529cbfa2efSAdriana Kobylak    bmcNum="$(findmtdnum "bmc")"
3539cbfa2efSAdriana Kobylak    altNum="$(findmtdnum "alt-bmc")"
3549cbfa2efSAdriana Kobylak    replaceAlt="${value/${altNum}_/${bmcNum}_}"
3559cbfa2efSAdriana Kobylak
3569cbfa2efSAdriana Kobylak    if [[ "${value}" == "${replaceAlt}" ]]; then
3579cbfa2efSAdriana Kobylak        replaceBmc="${value/${bmcNum}_/${altNum}_}"
3589cbfa2efSAdriana Kobylak        value=${replaceBmc}
3599cbfa2efSAdriana Kobylak    else
3609cbfa2efSAdriana Kobylak        value=${replaceAlt}
3619cbfa2efSAdriana Kobylak    fi
3629cbfa2efSAdriana Kobylak
3639cbfa2efSAdriana Kobylak    fw_setenv -c /etc/alt_fw_env.config root "${value}"
3649cbfa2efSAdriana Kobylak}
3659cbfa2efSAdriana Kobylak
366780c930eSPatrick Williamsfunction ubi_setenv() {
3679cbfa2efSAdriana Kobylak    # The U-Boot environment maintains two banks of environment variables.
3689cbfa2efSAdriana Kobylak    # The banks need to be consistent with each other to ensure that these
3699cbfa2efSAdriana Kobylak    # variables can reliably be read from file. In order to guarantee that the
3709cbfa2efSAdriana Kobylak    # banks are both correct, we need to run fw_setenv twice.
3719cbfa2efSAdriana Kobylak    variable=$1
3729cbfa2efSAdriana Kobylak    if [[ "$variable" == *"="* ]]; then
3739cbfa2efSAdriana Kobylak        varName="${variable%=*}"
3749cbfa2efSAdriana Kobylak        value="${variable##*=}"
3759cbfa2efSAdriana Kobylak        # Write only if var is not set already to the requested value
3769cbfa2efSAdriana Kobylak        currentValue="$(fw_printenv -n "${varName}" 2>/dev/null)" || true
377c53fccacSIsaac Kurth        if [[ "${currentValue}" != "${value}" ]]; then
3789cbfa2efSAdriana Kobylak            fw_setenv "$varName" "$value"
3799cbfa2efSAdriana Kobylak            fw_setenv "$varName" "$value"
3809cbfa2efSAdriana Kobylak        fi
3819cbfa2efSAdriana Kobylak    else
3829cbfa2efSAdriana Kobylak        fw_setenv "$variable"
3839cbfa2efSAdriana Kobylak        fw_setenv "$variable"
3849cbfa2efSAdriana Kobylak    fi
3859cbfa2efSAdriana Kobylak}
3869cbfa2efSAdriana Kobylak
387780c930eSPatrick Williamsfunction mtd_write() {
3889cbfa2efSAdriana Kobylak    flashmtd="$(findmtd "${reqmtd}")"
3899cbfa2efSAdriana Kobylak    img="/tmp/images/${version}/${imgfile}"
390c53fccacSIsaac Kurth    flashcp -v "${img}" /dev/"${flashmtd}"
3919cbfa2efSAdriana Kobylak}
3929cbfa2efSAdriana Kobylak
393780c930eSPatrick Williamsfunction backup_env_vars() {
3949cbfa2efSAdriana Kobylak    copy_env_var_to_alt kernelname
3959cbfa2efSAdriana Kobylak    copy_ubiblock_to_alt
3969cbfa2efSAdriana Kobylak    copy_root_to_alt
3979cbfa2efSAdriana Kobylak}
3989cbfa2efSAdriana Kobylak
399780c930eSPatrick Williamsfunction update_env_vars() {
40025773a7eSAdriana Kobylak    vol="$(findubi rofs-"${flashid}")"
4019cbfa2efSAdriana Kobylak    if [ -z "${vol}" ]; then
4029cbfa2efSAdriana Kobylak        return 1
4039cbfa2efSAdriana Kobylak    fi
4049cbfa2efSAdriana Kobylak    ubidevid="${vol#ubi}"
4059cbfa2efSAdriana Kobylak    block="/dev/ubiblock${ubidevid}"
4069cbfa2efSAdriana Kobylak    if [ ! -e "${block}" ]; then
4079cbfa2efSAdriana Kobylak        return 1
4089cbfa2efSAdriana Kobylak    fi
40925773a7eSAdriana Kobylak    ubi_setenv "kernelname=kernel-${flashid}"
410c53fccacSIsaac Kurth    ubi_setenv "ubiblock=${ubidevid//_/,}"
4119cbfa2efSAdriana Kobylak    ubi_setenv "root=${block}"
4129cbfa2efSAdriana Kobylak}
4139cbfa2efSAdriana Kobylak
4149cbfa2efSAdriana Kobylak#TODO: Replace the implementation with systemd-inhibitors lock
4159cbfa2efSAdriana Kobylak#      once systemd/systemd#949 is resolved
416780c930eSPatrick Williamsfunction rebootguardenable() {
4179cbfa2efSAdriana Kobylak    dir="/run/systemd/system/"
4189cbfa2efSAdriana Kobylak    file="reboot-guard.conf"
4199cbfa2efSAdriana Kobylak    units=("reboot" "poweroff" "halt")
4209cbfa2efSAdriana Kobylak
4219cbfa2efSAdriana Kobylak    for unit in "${units[@]}"; do
422c53fccacSIsaac Kurth        mkdir -p ${dir}"${unit}".target.d
423c53fccacSIsaac Kurth        echo -e "[Unit]\nRefuseManualStart=yes" >> ${dir}"${unit}".target.d/${file}
4249cbfa2efSAdriana Kobylak    done
4259cbfa2efSAdriana Kobylak}
4269cbfa2efSAdriana Kobylak
4279cbfa2efSAdriana Kobylak#TODO: Replace the implementation with systemd-inhibitors lock
4289cbfa2efSAdriana Kobylak#      once systemd/systemd#949 is resolved
429780c930eSPatrick Williamsfunction rebootguarddisable() {
4309cbfa2efSAdriana Kobylak    dir="/run/systemd/system/"
4319cbfa2efSAdriana Kobylak    file="reboot-guard.conf"
4329cbfa2efSAdriana Kobylak    units=("reboot" "poweroff" "halt")
4339cbfa2efSAdriana Kobylak
4349cbfa2efSAdriana Kobylak    for unit in "${units[@]}"; do
435c53fccacSIsaac Kurth        rm -rf ${dir}"${unit}".target.d/${file}
4369cbfa2efSAdriana Kobylak    done
4379cbfa2efSAdriana Kobylak}
4389cbfa2efSAdriana Kobylak
4399cbfa2efSAdriana Kobylak# Create a copy in the alt mtd
440780c930eSPatrick Williamsfunction create_vol_in_alt() {
4419cbfa2efSAdriana Kobylak    alt="alt-bmc"
4429cbfa2efSAdriana Kobylak    altmtd="$(findmtd "${alt}")"
443c53fccacSIsaac Kurth    if [ -n "${altmtd}" ]; then
4449cbfa2efSAdriana Kobylak        reqmtd="${alt}"
4459cbfa2efSAdriana Kobylak        reqmtd2="${alt}"
4469cbfa2efSAdriana Kobylak        ubi_ro
4479cbfa2efSAdriana Kobylak        ubi_updatevol
4489cbfa2efSAdriana Kobylak    fi
4499cbfa2efSAdriana Kobylak}
4509cbfa2efSAdriana Kobylak
4519cbfa2efSAdriana Kobylak# Copy contents of one MTD device to another
452780c930eSPatrick Williamsfunction mtd_copy() {
4539cbfa2efSAdriana Kobylak    in=$1
4549cbfa2efSAdriana Kobylak    out=$2
4559cbfa2efSAdriana Kobylak
4569cbfa2efSAdriana Kobylak    # Must erase MTD first to prevent corruption
4579cbfa2efSAdriana Kobylak    flash_eraseall "${out}"
4589cbfa2efSAdriana Kobylak    dd if="${in}" of="${out}"
4599cbfa2efSAdriana Kobylak}
4609cbfa2efSAdriana Kobylak
461780c930eSPatrick Williamsfunction mirroruboot() {
4629cbfa2efSAdriana Kobylak    bmc="$(findmtd "u-boot")"
4639cbfa2efSAdriana Kobylak    bmcdev="/dev/${bmc}"
4649cbfa2efSAdriana Kobylak    alt="$(findmtd "alt-u-boot")"
4659cbfa2efSAdriana Kobylak    altdev="/dev/${alt}"
4669cbfa2efSAdriana Kobylak
4679cbfa2efSAdriana Kobylak    checksum_bmc="$(md5sum "${bmcdev}")"
4689cbfa2efSAdriana Kobylak    checksum_bmc="${checksum_bmc% *}"
4699cbfa2efSAdriana Kobylak    checksum_alt="$(md5sum "${altdev}")"
4709cbfa2efSAdriana Kobylak    checksum_alt="${checksum_alt% *}"
4719cbfa2efSAdriana Kobylak
4729cbfa2efSAdriana Kobylak    if [[ "${checksum_bmc}" != "${checksum_alt}" ]]; then
4739cbfa2efSAdriana Kobylak        bmcenv="$(findmtd "u-boot-env")"
4749cbfa2efSAdriana Kobylak        bmcenvdev="/dev/${bmcenv}"
4759cbfa2efSAdriana Kobylak        altenv="$(findmtd "alt-u-boot-env")"
4769cbfa2efSAdriana Kobylak        altenvdev="/dev/${altenv}"
4779cbfa2efSAdriana Kobylak
4789cbfa2efSAdriana Kobylak        echo "Mirroring U-boot to alt chip"
4799cbfa2efSAdriana Kobylak        mtd_copy "${bmcdev}" "${altdev}"
4809cbfa2efSAdriana Kobylak        mtd_copy "${bmcenvdev}" "${altenvdev}"
4819cbfa2efSAdriana Kobylak
4829cbfa2efSAdriana Kobylak        copy_ubiblock_to_alt
4839cbfa2efSAdriana Kobylak        copy_root_to_alt
4849cbfa2efSAdriana Kobylak    fi
4859cbfa2efSAdriana Kobylak}
4869cbfa2efSAdriana Kobylak
48762f3820bSAdriana Kobylak# Compare the device where u-boot resides with an image file. Specify the full
48862f3820bSAdriana Kobylak# path to the device and image file to use for the compare. Print a value of
48962f3820bSAdriana Kobylak# "0" if identical, "1" otherwise.
490780c930eSPatrick Williamsfunction cmp_uboot() {
49162f3820bSAdriana Kobylak    device="$1"
49262f3820bSAdriana Kobylak    image="$2"
49362f3820bSAdriana Kobylak
49462f3820bSAdriana Kobylak    # Since the image file can be smaller than the device, copy the device to a
49562f3820bSAdriana Kobylak    # tmp file and write the image file on top, then compare the sum of each.
49662f3820bSAdriana Kobylak    # Use cat / redirection since busybox does not have the conv=notrunc option.
4979b8816f5SAdriana Kobylak    tmpFile="$(mktemp /tmp/ubootdev.XXXXXX)"
49862f3820bSAdriana Kobylak    dd if="${device}" of="${tmpFile}"
499c53fccacSIsaac Kurth    devSum="$(sha256sum "${tmpFile}")"
50062f3820bSAdriana Kobylak    cat < "${image}" 1<> "${tmpFile}"
501c53fccacSIsaac Kurth    imgSum="$(sha256sum "${tmpFile}")"
50262f3820bSAdriana Kobylak    rm -f "${tmpFile}"
50362f3820bSAdriana Kobylak
50462f3820bSAdriana Kobylak    if [ "${imgSum}" == "${devSum}" ]; then
50562f3820bSAdriana Kobylak        echo "0";
50662f3820bSAdriana Kobylak    else
50762f3820bSAdriana Kobylak        echo "1";
50862f3820bSAdriana Kobylak    fi
50962f3820bSAdriana Kobylak}
51062f3820bSAdriana Kobylak
51170f5bc04SAdriana Kobylak# The eMMC partition labels for the kernel and rootfs are boot-a/b and rofs-a/b.
51234124351SAdriana Kobylak# Return the label (a or b) for the running partition.
513780c930eSPatrick Williamsfunction mmc_get_primary_label() {
5145312d854SAdriana Kobylak    # Get root device /dev/mmcblkpX
51534124351SAdriana Kobylak    rootmatch=" on / "
5165312d854SAdriana Kobylak    root="$(mount | grep "${rootmatch}")"
5177b1b4cb8SLei YU    # shellcheck disable=SC2295
5185312d854SAdriana Kobylak    root="${root%${rootmatch}*}"
5195312d854SAdriana Kobylak
5205312d854SAdriana Kobylak    # Find the device label
5215312d854SAdriana Kobylak    if [[ $(readlink -f /dev/disk/by-partlabel/rofs-a) == "${root}" ]]; then
52234124351SAdriana Kobylak        echo "a"
5235312d854SAdriana Kobylak    elif [[ $(readlink -f /dev/disk/by-partlabel/rofs-b) == "${root}" ]]; then
52434124351SAdriana Kobylak        echo "b"
5255312d854SAdriana Kobylak    else
5265312d854SAdriana Kobylak        echo ""
52734124351SAdriana Kobylak    fi
52834124351SAdriana Kobylak}
52934124351SAdriana Kobylak
53034124351SAdriana Kobylak# The eMMC partition labels for the kernel and rootfs are boot-a/b and rofs-a/b.
53170f5bc04SAdriana Kobylak# Return the label (a or b) for the non-running partition.
532780c930eSPatrick Williamsfunction mmc_get_secondary_label() {
5335312d854SAdriana Kobylak    root="$(mmc_get_primary_label)"
5345312d854SAdriana Kobylak    if [[ "${root}" == "a" ]]; then
53570f5bc04SAdriana Kobylak        echo "b"
5365312d854SAdriana Kobylak    elif [[ "${root}" == "b" ]]; then
53770f5bc04SAdriana Kobylak        echo "a"
5385312d854SAdriana Kobylak    else
5395312d854SAdriana Kobylak        echo ""
54070f5bc04SAdriana Kobylak    fi
54170f5bc04SAdriana Kobylak}
54270f5bc04SAdriana Kobylak
543780c930eSPatrick Williamsfunction mmc_mount() {
5441e81f23cSAdriana Kobylak    primaryId="$(mmc_get_primary_label)"
5451e81f23cSAdriana Kobylak    secondaryId="$(mmc_get_secondary_label)"
5461e81f23cSAdriana Kobylak
5471e81f23cSAdriana Kobylak    primaryDir="${mediaDir}/rofs-${primaryId}-functional"
5481e81f23cSAdriana Kobylak    secondaryDir="${mediaDir}/rofs-${secondaryId}"
5491e81f23cSAdriana Kobylak
550dcfadad5SAdriana Kobylak    # Check the alternate version, remove it if it's corrupted. This can occur
551dcfadad5SAdriana Kobylak    # when the BMC is rebooted in the middle of a code update for example.
552dcfadad5SAdriana Kobylak    if ! fsck.ext4 -p "/dev/disk/by-partlabel/rofs-${secondaryId}"; then
553dcfadad5SAdriana Kobylak        flashid="${secondaryId}"
554dcfadad5SAdriana Kobylak        mmc_remove
555dcfadad5SAdriana Kobylak    fi
556dcfadad5SAdriana Kobylak
5571e81f23cSAdriana Kobylak    mkdir -p "${primaryDir}"
5581e81f23cSAdriana Kobylak    mkdir -p "${secondaryDir}"
5591e81f23cSAdriana Kobylak
560c53fccacSIsaac Kurth    mount PARTLABEL=rofs-"${primaryId}" "${primaryDir}" -t ext4 -o ro || rmdir "${primaryDir}"
561c53fccacSIsaac Kurth    mount PARTLABEL=rofs-"${secondaryId}" "${secondaryDir}" -t ext4 -o ro || rmdir "${secondaryDir}"
5621e81f23cSAdriana Kobylak}
5631e81f23cSAdriana Kobylak
564780c930eSPatrick Williamsfunction mmc_update() {
56562f3820bSAdriana Kobylak    # Update u-boot if needed
56662f3820bSAdriana Kobylak    bootPartition="mmcblk0boot0"
56762f3820bSAdriana Kobylak    devUBoot="/dev/${bootPartition}"
56862f3820bSAdriana Kobylak    imgUBoot="${imgpath}/${version}/image-u-boot"
56962f3820bSAdriana Kobylak    if [ "$(cmp_uboot "${devUBoot}" "${imgUBoot}")" != "0" ]; then
57062f3820bSAdriana Kobylak        echo 0 > "/sys/block/${bootPartition}/force_ro"
57162f3820bSAdriana Kobylak        dd if="${imgUBoot}" of="${devUBoot}"
57262f3820bSAdriana Kobylak        echo 1 > "/sys/block/${bootPartition}/force_ro"
57362f3820bSAdriana Kobylak    fi
57462f3820bSAdriana Kobylak
57570f5bc04SAdriana Kobylak    # Update the secondary (non-running) boot and rofs partitions.
57670f5bc04SAdriana Kobylak    label="$(mmc_get_secondary_label)"
57770f5bc04SAdriana Kobylak
57870f5bc04SAdriana Kobylak    # Update the boot and rootfs partitions, restore their labels after the update
57970f5bc04SAdriana Kobylak    # by getting the partition number mmcblk0pX from their label.
580c53fccacSIsaac Kurth    zstd -d -c "${imgpath}"/"${version}"/image-kernel | dd of="/dev/disk/by-partlabel/boot-${label}"
581c53fccacSIsaac Kurth    number="$(readlink -f /dev/disk/by-partlabel/boot-"${label}")"
58270f5bc04SAdriana Kobylak    number="${number##*mmcblk0p}"
583c53fccacSIsaac Kurth    sgdisk --change-name="${number}":boot-"${label}" /dev/mmcblk0 1>/dev/null
58470f5bc04SAdriana Kobylak
585c53fccacSIsaac Kurth    zstd -d -c "${imgpath}"/"${version}"/image-rofs | dd of="/dev/disk/by-partlabel/rofs-${label}"
586c53fccacSIsaac Kurth    number="$(readlink -f /dev/disk/by-partlabel/rofs-"${label}")"
58770f5bc04SAdriana Kobylak    number="${number##*mmcblk0p}"
588c53fccacSIsaac Kurth    sgdisk --change-name="${number}":rofs-"${label}" /dev/mmcblk0 1>/dev/null
58970f5bc04SAdriana Kobylak
59070f5bc04SAdriana Kobylak    # Run this after sgdisk for labels to take effect.
59170f5bc04SAdriana Kobylak    partprobe
59270f5bc04SAdriana Kobylak
593*dedcb9a6SAdriana Kobylak    # Update hostfw. The remove function doesn't touch the hostfw image, so
594*dedcb9a6SAdriana Kobylak    # need to unmount and delete it prior to updating it.
595c53fccacSIsaac Kurth    if [ -f "${imgpath}"/"${version}"/image-hostfw ]; then
596d148b4f0SAdriana Kobylak        # Remove patches
597*dedcb9a6SAdriana Kobylak        hostfw_alt="hostfw/alternate"
598*dedcb9a6SAdriana Kobylak        patchdir="/usr/local/share/${hostfw_alt}"
599d148b4f0SAdriana Kobylak        if [ -d "${patchdir}" ]; then
600c53fccacSIsaac Kurth            rm -rf "${patchdir:?}"/*
601d148b4f0SAdriana Kobylak        fi
602*dedcb9a6SAdriana Kobylak        if grep -q "${hostfw_alt}" /proc/mounts; then
603*dedcb9a6SAdriana Kobylak            hostfw_alt=$(grep "${hostfw_alt}" /proc/mounts | cut -d " " -f 2)
604*dedcb9a6SAdriana Kobylak            umount "${hostfw_alt}"
605*dedcb9a6SAdriana Kobylak        fi
6068c5209d3SAdriana Kobylak        hostfwdir=$(grep "hostfw " /proc/mounts | cut -d " " -f 2)
607*dedcb9a6SAdriana Kobylak        rm -f "${hostfwdir}/hostfw-${flashid}"
608c53fccacSIsaac Kurth        cp "${imgpath}"/"${version}"/image-hostfw "${hostfwdir}"/hostfw-"${label}"
609c53fccacSIsaac Kurth        mkdir -p "${hostfwdir}"/alternate
610c53fccacSIsaac Kurth        mount "${hostfwdir}"/hostfw-"${label}" "${hostfwdir}"/alternate -o ro
6118c5209d3SAdriana Kobylak    fi
6128c5209d3SAdriana Kobylak
613a84f06d2SAdriana Kobylak    set_flashid "${label}"
61470f5bc04SAdriana Kobylak}
61570f5bc04SAdriana Kobylak
616780c930eSPatrick Williamsfunction mmc_remove() {
61770f5bc04SAdriana Kobylak    # Render the filesystem unbootable by wiping out the first 1MB, this
61870f5bc04SAdriana Kobylak    # invalidates the filesystem header.
61925773a7eSAdriana Kobylak    # Check if the requested id is the one the BMC is running from since dd
62025773a7eSAdriana Kobylak    # can still write and corrupt the running partition.
62125773a7eSAdriana Kobylak    primaryid="$(mmc_get_primary_label)"
62225773a7eSAdriana Kobylak    if [[ "${flashid}" == "${primaryid}" ]]; then
623c53fccacSIsaac Kurth        return 1
62470f5bc04SAdriana Kobylak    fi
625c53fccacSIsaac Kurth    dd if=/dev/zero of=/dev/disk/by-partlabel/boot-"${flashid}" count=2048
626c53fccacSIsaac Kurth    dd if=/dev/zero of=/dev/disk/by-partlabel/rofs-"${flashid}" count=2048
62770f5bc04SAdriana Kobylak}
62870f5bc04SAdriana Kobylak
62934124351SAdriana Kobylak# Set the requested version as primary for the BMC to boot from upon reboot.
630780c930eSPatrick Williamsfunction mmc_setprimary() {
63125773a7eSAdriana Kobylak    # Point root to the flashid of the requested BMC rootfs.
63225773a7eSAdriana Kobylak    fw_setenv bootside "${flashid}"
63334124351SAdriana Kobylak}
63434124351SAdriana Kobylak
63587c5b5bdSLakshmi Yadlapatifunction mmc_mirroruboot() {
63687c5b5bdSLakshmi Yadlapati    # Get current boot device; 0-primary_bootdev device; 1 - alt_bootdev
63787c5b5bdSLakshmi Yadlapati    bootdev=$(cat /sys/kernel/debug/aspeed/sbc/abr_image)
63887c5b5bdSLakshmi Yadlapati    if [[ "${bootdev}" == "0" ]]; then
63987c5b5bdSLakshmi Yadlapati        bootPartition="mmcblk0boot0"
64087c5b5bdSLakshmi Yadlapati        alt_bootPartition="mmcblk0boot1"
64187c5b5bdSLakshmi Yadlapati    else
64287c5b5bdSLakshmi Yadlapati        bootPartition="mmcblk0boot1"
64387c5b5bdSLakshmi Yadlapati        alt_bootPartition="mmcblk0boot0"
64487c5b5bdSLakshmi Yadlapati    fi
64587c5b5bdSLakshmi Yadlapati
64687c5b5bdSLakshmi Yadlapati    devUBoot="/dev/${bootPartition}"
64787c5b5bdSLakshmi Yadlapati    alt_devUBoot="/dev/${alt_bootPartition}"
64887c5b5bdSLakshmi Yadlapati
64987c5b5bdSLakshmi Yadlapati    checksum_UBoot="$(md5sum "${devUBoot}")"
65087c5b5bdSLakshmi Yadlapati    checksum_UBoot="${checksum_UBoot% *}"
65187c5b5bdSLakshmi Yadlapati    checksum_alt_UBoot="$(md5sum "${alt_devUBoot}")"
65287c5b5bdSLakshmi Yadlapati    checksum_alt_UBoot="${checksum_alt% *}"
65387c5b5bdSLakshmi Yadlapati
65487c5b5bdSLakshmi Yadlapati    if [[ "${checksum_UBoot}" != "${checksum_alt_UBoot}" ]]; then
65587c5b5bdSLakshmi Yadlapati        echo "Mirroring U-boot to alt chip"
65687c5b5bdSLakshmi Yadlapati        echo 0 > "/sys/block/${alt_bootPartition}/force_ro"
65787c5b5bdSLakshmi Yadlapati        dd if="${devUBoot}" of="${alt_devUBoot}"
65887c5b5bdSLakshmi Yadlapati        echo 1 > "/sys/block/${alt_bootPartition}/force_ro"
65987c5b5bdSLakshmi Yadlapati    fi
66087c5b5bdSLakshmi Yadlapati}
66187c5b5bdSLakshmi Yadlapati
6629cbfa2efSAdriana Kobylakcase "$1" in
6639cbfa2efSAdriana Kobylak    mtduboot)
6649cbfa2efSAdriana Kobylak        reqmtd="$2"
6659cbfa2efSAdriana Kobylak        version="$3"
6669cbfa2efSAdriana Kobylak        imgfile="image-u-boot"
6679cbfa2efSAdriana Kobylak        mtd_write
6689cbfa2efSAdriana Kobylak        ;;
6699cbfa2efSAdriana Kobylak    ubirw)
6709cbfa2efSAdriana Kobylak        reqmtd="$2"
6719cbfa2efSAdriana Kobylak        name="$3"
6729cbfa2efSAdriana Kobylak        imgsize="$4"
6739cbfa2efSAdriana Kobylak        ubi_rw
6749cbfa2efSAdriana Kobylak        ;;
6759cbfa2efSAdriana Kobylak    ubiro)
6769cbfa2efSAdriana Kobylak        reqmtd="$(echo "$2" | cut -d "+" -f 1)"
6779cbfa2efSAdriana Kobylak        reqmtd2="$(echo "$2" | cut -d "+" -f 2)"
6789cbfa2efSAdriana Kobylak        name="$3"
6799cbfa2efSAdriana Kobylak        version="$4"
6809cbfa2efSAdriana Kobylak        imgfile="image-rofs"
6819cbfa2efSAdriana Kobylak        ubi_ro
6829cbfa2efSAdriana Kobylak        ubi_updatevol
6839cbfa2efSAdriana Kobylak        ubi_block
6849cbfa2efSAdriana Kobylak        ;;
6859cbfa2efSAdriana Kobylak    ubikernel)
6869cbfa2efSAdriana Kobylak        reqmtd="$(echo "$2" | cut -d "+" -f 1)"
6879cbfa2efSAdriana Kobylak        reqmtd2="$(echo "$2" | cut -d "+" -f 2)"
6889cbfa2efSAdriana Kobylak        name="$3"
6899cbfa2efSAdriana Kobylak        version="$4"
6909cbfa2efSAdriana Kobylak        imgfile="image-kernel"
6919cbfa2efSAdriana Kobylak        ubi_ro
6929cbfa2efSAdriana Kobylak        ubi_updatevol
6939cbfa2efSAdriana Kobylak        create_vol_in_alt
6949cbfa2efSAdriana Kobylak        ;;
6959cbfa2efSAdriana Kobylak    ubiremove)
6969cbfa2efSAdriana Kobylak        name="$2"
6979cbfa2efSAdriana Kobylak        ubi_remove "${name}"
6989cbfa2efSAdriana Kobylak        ;;
6999cbfa2efSAdriana Kobylak    ubicleanup)
7009cbfa2efSAdriana Kobylak        ubi_cleanup
7019cbfa2efSAdriana Kobylak        ;;
7029cbfa2efSAdriana Kobylak    ubisetenv)
7039cbfa2efSAdriana Kobylak        ubi_setenv "$2"
7049cbfa2efSAdriana Kobylak        ;;
7059cbfa2efSAdriana Kobylak    ubiremount)
7069cbfa2efSAdriana Kobylak        remount_ubi
7076376964eSLei YU        mount_ubi_alt_rwfs
7089cbfa2efSAdriana Kobylak        ;;
7099cbfa2efSAdriana Kobylak    createenvbackup)
7109cbfa2efSAdriana Kobylak        backup_env_vars
7119cbfa2efSAdriana Kobylak        ;;
7129cbfa2efSAdriana Kobylak    updateubootvars)
71325773a7eSAdriana Kobylak        flashid="$2"
7149cbfa2efSAdriana Kobylak        update_env_vars
7159cbfa2efSAdriana Kobylak        ;;
7169cbfa2efSAdriana Kobylak    rebootguardenable)
7179cbfa2efSAdriana Kobylak        rebootguardenable
7189cbfa2efSAdriana Kobylak        ;;
7199cbfa2efSAdriana Kobylak    rebootguarddisable)
7209cbfa2efSAdriana Kobylak        rebootguarddisable
7219cbfa2efSAdriana Kobylak        ;;
7229cbfa2efSAdriana Kobylak    mirroruboot)
7239cbfa2efSAdriana Kobylak        mirroruboot
7249cbfa2efSAdriana Kobylak        ;;
72570f5bc04SAdriana Kobylak    mmc)
72670f5bc04SAdriana Kobylak        version="$2"
72770f5bc04SAdriana Kobylak        imgpath="$3"
72870f5bc04SAdriana Kobylak        mmc_update
72970f5bc04SAdriana Kobylak        ;;
7301e81f23cSAdriana Kobylak    mmc-mount)
7311e81f23cSAdriana Kobylak        mediaDir="$2"
7321e81f23cSAdriana Kobylak        mmc_mount
7331e81f23cSAdriana Kobylak        ;;
73470f5bc04SAdriana Kobylak    mmc-remove)
73525773a7eSAdriana Kobylak        flashid="$2"
73670f5bc04SAdriana Kobylak        mmc_remove
73770f5bc04SAdriana Kobylak        ;;
73834124351SAdriana Kobylak    mmc-setprimary)
73925773a7eSAdriana Kobylak        flashid="$2"
74034124351SAdriana Kobylak        mmc_setprimary
74134124351SAdriana Kobylak        ;;
74287c5b5bdSLakshmi Yadlapati    mmc-mirroruboot)
74387c5b5bdSLakshmi Yadlapati        mmc_mirroruboot
74487c5b5bdSLakshmi Yadlapati        ;;
7456376964eSLei YU    static-altfs)
7466376964eSLei YU        mount_static_alt "$2" "$3" "$4"
7476376964eSLei YU        ;;
748b5171acdSLei YU    umount-static-altfs)
749b5171acdSLei YU        umount_static_alt "$2"
750b5171acdSLei YU        ;;
7519cbfa2efSAdriana Kobylak    *)
7529cbfa2efSAdriana Kobylak        echo "Invalid argument"
7539cbfa2efSAdriana Kobylak        exit 1
7549cbfa2efSAdriana Kobylak        ;;
7559cbfa2efSAdriana Kobylakesac
756