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