blob: aa9773f0ac3a6b3e318faf07f4e6af93c70e7250 [file] [log] [blame]
Adriana Kobylak9cbfa2e2019-04-25 14:02:37 -05001#!/bin/bash
2set -eo pipefail
3
4# Get the root mtd device number (mtdX) from "/dev/ubiblockX_Y on /"
5findrootmtd() {
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
17findrootubi() {
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)
26findmtd() {
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)
34findmtdnum() {
35 m="$(findmtd "$1")"
36 m="${m##mtd}"
37 echo "${m}"
38}
39
40# Get the ubi device number (ubiX_Y)
41findubi() {
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
49findubi_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
57findubiname_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
65findname() {
66 n="$(cat /sys/class/ubi/$1/name)"
67 echo "${n}"
68}
69
Adriana Kobylaka84f06d2022-01-18 15:41:57 +000070# Set the version path property to the flash location where the image was
71# successfully flashed
72set_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 Kobylak9cbfa2e2019-04-25 14:02:37 -050079# Set the u-boot envs that perform a side switch on failure to boot
80set_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
91ubi_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
125ubi_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
144ubi_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 Kobylaka84f06d2022-01-18 15:41:57 +0000182
183 set_flashid "${version}"
Adriana Kobylak9cbfa2e2019-04-25 14:02:37 -0500184}
185
186# Squashfs images need a ubi block
187ubi_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
196ubi_updatevol() {
197 vol="$(findubi "${name}")"
198 ubidevid="${vol#ubi}"
199 img="/tmp/images/${version}/${imgfile}"
200 ubiupdatevol "/dev/ubi${ubidevid}" "${img}"
201}
202
203ubi_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
225ubi_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 Kobylak25773a72022-01-21 15:24:48 +0000235 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 Kobylak9cbfa2e2019-04-25 14:02:37 -0500238 vols=$(ubinfo -a | grep "rofs-" | \
Adriana Kobylak25773a72022-01-21 15:24:48 +0000239 grep -v "$flashid" | cut -c 14-) || true
Adriana Kobylak9cbfa2e2019-04-25 14:02:37 -0500240 vols=(${vols})
241 fi
242
243 for (( index=0; index<${#vols[@]}; index++ )); do
244 ubi_remove ${vols[index]}
245 done
246}
247
248mount_alt_rwfs() {
249 altNum="$(findmtdnum "alt-bmc")"
250 if [ ! -z "${altNum}" ]; then
251 altRwfs=$(ubinfo -a -d ${altNum} | grep -w "rwfs") || true
252 if [ ! -z "${altRwfs}" ]; then
253 altVarMount="/media/alt/var"
254 mkdir -p "${altVarMount}"
255 if mount ubi"${altNum}":rwfs "${altVarMount}" -t ubifs -o defaults; then
256 mkdir -p "${altVarMount}"/persist/etc
257 fi
258 fi
259 fi
260}
261
262remount_ubi() {
263 bmcmtd="$(findmtd "bmc")"
264 altbmcmtd="$(findmtd "alt-bmc")"
265 mtds="${bmcmtd: -1}","${altbmcmtd: -1}"
266
267 IFS=',' read -r -a mtds <<< "$mtds"
268 mtds=($(echo "${mtds[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' '))
269 for mtd in ${mtds[@]}; do
270 # Get information on all ubi volumes
271 ubinfo=$(ubinfo -d ${mtd})
272 presentVolumes=${ubinfo##*:}
273 IFS=', ' read -r -a array <<< "$presentVolumes"
274 for element in ${array[@]}; do
275 elementProperties=$(ubinfo -d $mtd -n $element)
276 # Get ubi volume name by getting rid of additional properties
277 name=${elementProperties#*Name:}
278 name="${name%Character*}"
279 name="$(echo -e "${name}" | tr -d '[:space:]')"
280
281 if [[ ${name} == rofs-* ]]; then
282 mountdir="/media/${name}"
283
284 if [ ! -d ${mountdir} ]; then
285 mkdir -p "${mountdir}"
286 # U-Boot will create the ubiblock for the running version, but not
287 # for the version on the other chip
288 if [ ! -e "/dev/ubiblock${mtd}_${element}" ]; then
289 ubiblock --create /dev/ubi${mtd}_${element}
290 fi
291 mount -t squashfs -o ro "/dev/ubiblock${mtd}_${element}" "${mountdir}"
292 fi
293 fi
294 done
295 done
296
297 set_wdt2bite
298}
299
300# Read the current env variable and set it on the alternate boot env
301copy_env_var_to_alt() {
302 varName=$1
303 value="$(fw_printenv -n "${varName}")"
304 fw_setenv -c /etc/alt_fw_env.config "${varName}" "${value}"
305}
306
307# When the alternate bmc chip boots, u-boot thinks its the primary mtdX.
308# Therefore need to swap the chip numbers when copying the ubiblock and root to
309# alternate bmc u-boot environment.
310copy_ubiblock_to_alt() {
311 value="$(fw_printenv -n ubiblock)"
312 bmcNum="$(findmtdnum "bmc")"
313 altNum="$(findmtdnum "alt-bmc")"
314 replaceAlt="${value/${altNum},/${bmcNum},}"
315
316 if [[ "${value}" == "${replaceAlt}" ]]; then
317 replaceBmc="${value/${bmcNum},/${altNum},}"
318 value=${replaceBmc}
319 else
320 value=${replaceAlt}
321 fi
322
323 fw_setenv -c /etc/alt_fw_env.config ubiblock "${value}"
324}
325
326copy_root_to_alt() {
327 value="$(fw_printenv -n root)"
328 bmcNum="$(findmtdnum "bmc")"
329 altNum="$(findmtdnum "alt-bmc")"
330 replaceAlt="${value/${altNum}_/${bmcNum}_}"
331
332 if [[ "${value}" == "${replaceAlt}" ]]; then
333 replaceBmc="${value/${bmcNum}_/${altNum}_}"
334 value=${replaceBmc}
335 else
336 value=${replaceAlt}
337 fi
338
339 fw_setenv -c /etc/alt_fw_env.config root "${value}"
340}
341
342ubi_setenv() {
343 # The U-Boot environment maintains two banks of environment variables.
344 # The banks need to be consistent with each other to ensure that these
345 # variables can reliably be read from file. In order to guarantee that the
346 # banks are both correct, we need to run fw_setenv twice.
347 variable=$1
348 if [[ "$variable" == *"="* ]]; then
349 varName="${variable%=*}"
350 value="${variable##*=}"
351 # Write only if var is not set already to the requested value
352 currentValue="$(fw_printenv -n "${varName}" 2>/dev/null)" || true
353 if [[ "${currenValue}" != "${value}" ]]; then
354 fw_setenv "$varName" "$value"
355 fw_setenv "$varName" "$value"
356 fi
357 else
358 fw_setenv "$variable"
359 fw_setenv "$variable"
360 fi
361}
362
363mtd_write() {
364 flashmtd="$(findmtd "${reqmtd}")"
365 img="/tmp/images/${version}/${imgfile}"
366 flashcp -v ${img} /dev/${flashmtd}
367}
368
369backup_env_vars() {
370 copy_env_var_to_alt kernelname
371 copy_ubiblock_to_alt
372 copy_root_to_alt
373}
374
375update_env_vars() {
Adriana Kobylak25773a72022-01-21 15:24:48 +0000376 vol="$(findubi rofs-"${flashid}")"
Adriana Kobylak9cbfa2e2019-04-25 14:02:37 -0500377 if [ -z "${vol}" ]; then
378 return 1
379 fi
380 ubidevid="${vol#ubi}"
381 block="/dev/ubiblock${ubidevid}"
382 if [ ! -e "${block}" ]; then
383 return 1
384 fi
Adriana Kobylak25773a72022-01-21 15:24:48 +0000385 ubi_setenv "kernelname=kernel-${flashid}"
Adriana Kobylak9cbfa2e2019-04-25 14:02:37 -0500386 ubi_setenv "ubiblock=$(echo "${ubidevid}" | sed 's/_/,/')"
387 ubi_setenv "root=${block}"
388}
389
390#TODO: Replace the implementation with systemd-inhibitors lock
391# once systemd/systemd#949 is resolved
392rebootguardenable() {
393 dir="/run/systemd/system/"
394 file="reboot-guard.conf"
395 units=("reboot" "poweroff" "halt")
396
397 for unit in "${units[@]}"; do
398 mkdir -p ${dir}${unit}.target.d
399 echo -e "[Unit]\nRefuseManualStart=yes" >> ${dir}${unit}.target.d/${file}
400 done
401}
402
403#TODO: Replace the implementation with systemd-inhibitors lock
404# once systemd/systemd#949 is resolved
405rebootguarddisable() {
406 dir="/run/systemd/system/"
407 file="reboot-guard.conf"
408 units=("reboot" "poweroff" "halt")
409
410 for unit in "${units[@]}"; do
411 rm -rf ${dir}${unit}.target.d/${file}
412 done
413}
414
415# Create a copy in the alt mtd
416create_vol_in_alt() {
417 alt="alt-bmc"
418 altmtd="$(findmtd "${alt}")"
419 if [ ! -z "${altmtd}" ]; then
420 reqmtd="${alt}"
421 reqmtd2="${alt}"
422 ubi_ro
423 ubi_updatevol
424 fi
425}
426
427# Copy contents of one MTD device to another
428mtd_copy() {
429 in=$1
430 out=$2
431
432 # Must erase MTD first to prevent corruption
433 flash_eraseall "${out}"
434 dd if="${in}" of="${out}"
435}
436
437mirroruboot() {
438 bmc="$(findmtd "u-boot")"
439 bmcdev="/dev/${bmc}"
440 alt="$(findmtd "alt-u-boot")"
441 altdev="/dev/${alt}"
442
443 checksum_bmc="$(md5sum "${bmcdev}")"
444 checksum_bmc="${checksum_bmc% *}"
445 checksum_alt="$(md5sum "${altdev}")"
446 checksum_alt="${checksum_alt% *}"
447
448 if [[ "${checksum_bmc}" != "${checksum_alt}" ]]; then
449 bmcenv="$(findmtd "u-boot-env")"
450 bmcenvdev="/dev/${bmcenv}"
451 altenv="$(findmtd "alt-u-boot-env")"
452 altenvdev="/dev/${altenv}"
453
454 echo "Mirroring U-boot to alt chip"
455 mtd_copy "${bmcdev}" "${altdev}"
456 mtd_copy "${bmcenvdev}" "${altenvdev}"
457
458 copy_ubiblock_to_alt
459 copy_root_to_alt
460 fi
461}
462
Adriana Kobylak62f38202020-09-29 13:04:25 -0500463# Compare the device where u-boot resides with an image file. Specify the full
464# path to the device and image file to use for the compare. Print a value of
465# "0" if identical, "1" otherwise.
466cmp_uboot() {
467 device="$1"
468 image="$2"
469
470 # Since the image file can be smaller than the device, copy the device to a
471 # tmp file and write the image file on top, then compare the sum of each.
472 # Use cat / redirection since busybox does not have the conv=notrunc option.
Adriana Kobylak9b8816f2020-12-02 11:08:15 -0600473 tmpFile="$(mktemp /tmp/ubootdev.XXXXXX)"
Adriana Kobylak62f38202020-09-29 13:04:25 -0500474 dd if="${device}" of="${tmpFile}"
475 devSum="$(sha256sum ${tmpFile})"
476 cat < "${image}" 1<> "${tmpFile}"
477 imgSum="$(sha256sum ${tmpFile})"
478 rm -f "${tmpFile}"
479
480 if [ "${imgSum}" == "${devSum}" ]; then
481 echo "0";
482 else
483 echo "1";
484 fi
485}
486
Adriana Kobylak70f5bc02020-05-13 14:08:14 -0500487# The eMMC partition labels for the kernel and rootfs are boot-a/b and rofs-a/b.
Adriana Kobylak34124352020-05-22 09:40:40 -0500488# Return the label (a or b) for the running partition.
489mmc_get_primary_label() {
Adriana Kobylak5312d852020-07-18 09:49:34 -0500490 # Get root device /dev/mmcblkpX
Adriana Kobylak34124352020-05-22 09:40:40 -0500491 rootmatch=" on / "
Adriana Kobylak5312d852020-07-18 09:49:34 -0500492 root="$(mount | grep "${rootmatch}")"
493 root="${root%${rootmatch}*}"
494
495 # Find the device label
496 if [[ $(readlink -f /dev/disk/by-partlabel/rofs-a) == "${root}" ]]; then
Adriana Kobylak34124352020-05-22 09:40:40 -0500497 echo "a"
Adriana Kobylak5312d852020-07-18 09:49:34 -0500498 elif [[ $(readlink -f /dev/disk/by-partlabel/rofs-b) == "${root}" ]]; then
Adriana Kobylak34124352020-05-22 09:40:40 -0500499 echo "b"
Adriana Kobylak5312d852020-07-18 09:49:34 -0500500 else
501 echo ""
Adriana Kobylak34124352020-05-22 09:40:40 -0500502 fi
503}
504
505# The eMMC partition labels for the kernel and rootfs are boot-a/b and rofs-a/b.
Adriana Kobylak70f5bc02020-05-13 14:08:14 -0500506# Return the label (a or b) for the non-running partition.
507mmc_get_secondary_label() {
Adriana Kobylak5312d852020-07-18 09:49:34 -0500508 root="$(mmc_get_primary_label)"
509 if [[ "${root}" == "a" ]]; then
Adriana Kobylak70f5bc02020-05-13 14:08:14 -0500510 echo "b"
Adriana Kobylak5312d852020-07-18 09:49:34 -0500511 elif [[ "${root}" == "b" ]]; then
Adriana Kobylak70f5bc02020-05-13 14:08:14 -0500512 echo "a"
Adriana Kobylak5312d852020-07-18 09:49:34 -0500513 else
514 echo ""
Adriana Kobylak70f5bc02020-05-13 14:08:14 -0500515 fi
516}
517
518mmc_update() {
Adriana Kobylak62f38202020-09-29 13:04:25 -0500519 # Update u-boot if needed
520 bootPartition="mmcblk0boot0"
521 devUBoot="/dev/${bootPartition}"
522 imgUBoot="${imgpath}/${version}/image-u-boot"
523 if [ "$(cmp_uboot "${devUBoot}" "${imgUBoot}")" != "0" ]; then
524 echo 0 > "/sys/block/${bootPartition}/force_ro"
525 dd if="${imgUBoot}" of="${devUBoot}"
526 echo 1 > "/sys/block/${bootPartition}/force_ro"
527 fi
528
Adriana Kobylak70f5bc02020-05-13 14:08:14 -0500529 # Update the secondary (non-running) boot and rofs partitions.
530 label="$(mmc_get_secondary_label)"
531
532 # Update the boot and rootfs partitions, restore their labels after the update
533 # by getting the partition number mmcblk0pX from their label.
534 zstd -d -c ${imgpath}/${version}/image-kernel | dd of="/dev/disk/by-partlabel/boot-${label}"
535 number="$(readlink -f /dev/disk/by-partlabel/boot-${label})"
536 number="${number##*mmcblk0p}"
537 sgdisk --change-name=${number}:boot-${label} /dev/mmcblk0 1>/dev/null
538
539 zstd -d -c ${imgpath}/${version}/image-rofs | dd of="/dev/disk/by-partlabel/rofs-${label}"
540 number="$(readlink -f /dev/disk/by-partlabel/rofs-${label})"
541 number="${number##*mmcblk0p}"
542 sgdisk --change-name=${number}:rofs-${label} /dev/mmcblk0 1>/dev/null
543
544 # Run this after sgdisk for labels to take effect.
545 partprobe
546
Adriana Kobylak8c5209d2020-07-12 13:35:47 -0500547 # Update hostfw
548 if [ -f ${imgpath}/${version}/image-hostfw ]; then
Adriana Kobylakd148b4f2020-08-27 10:18:49 -0500549 # Remove patches
550 patchdir="/usr/local/share/hostfw/alternate"
551 if [ -d "${patchdir}" ]; then
552 rm -rf "${patchdir}"/*
553 fi
Adriana Kobylak8c5209d2020-07-12 13:35:47 -0500554 hostfwdir=$(grep "hostfw " /proc/mounts | cut -d " " -f 2)
555 cp ${imgpath}/${version}/image-hostfw ${hostfwdir}/hostfw-${label}
556 mkdir -p ${hostfwdir}/alternate
557 mount ${hostfwdir}/hostfw-${label} ${hostfwdir}/alternate -o ro
558 fi
559
Adriana Kobylaka84f06d2022-01-18 15:41:57 +0000560 set_flashid "${label}"
Adriana Kobylak70f5bc02020-05-13 14:08:14 -0500561}
562
563mmc_remove() {
564 # Render the filesystem unbootable by wiping out the first 1MB, this
565 # invalidates the filesystem header.
Adriana Kobylak25773a72022-01-21 15:24:48 +0000566 # Check if the requested id is the one the BMC is running from since dd
567 # can still write and corrupt the running partition.
568 primaryid="$(mmc_get_primary_label)"
569 if [[ "${flashid}" == "${primaryid}" ]]; then
570 return -1
Adriana Kobylak70f5bc02020-05-13 14:08:14 -0500571 fi
Adriana Kobylak25773a72022-01-21 15:24:48 +0000572 dd if=/dev/zero of=/dev/disk/by-partlabel/boot-${flashid} count=2048
573 dd if=/dev/zero of=/dev/disk/by-partlabel/rofs-${flashid} count=2048
Adriana Kobylak8c5209d2020-07-12 13:35:47 -0500574
575 hostfw_alt="hostfw/alternate"
576 if grep -q "${hostfw_alt}" /proc/mounts; then
577 hostfw_alt=$(grep "${hostfw_alt}" /proc/mounts | cut -d " " -f 2)
578 umount "${hostfw_alt}"
579 fi
580 hostfw_base="hostfw "
581 if grep -q "${hostfw_base}" /proc/mounts; then
582 hostfw_base=$(grep "${hostfw_base}" /proc/mounts | cut -d " " -f 2)
Adriana Kobylak25773a72022-01-21 15:24:48 +0000583 rm -f ${hostfw_base}/hostfw-${flashid}
Adriana Kobylak8c5209d2020-07-12 13:35:47 -0500584 fi
Adriana Kobylak70f5bc02020-05-13 14:08:14 -0500585}
586
Adriana Kobylak34124352020-05-22 09:40:40 -0500587# Set the requested version as primary for the BMC to boot from upon reboot.
588mmc_setprimary() {
Adriana Kobylak25773a72022-01-21 15:24:48 +0000589 # Point root to the flashid of the requested BMC rootfs.
590 fw_setenv bootside "${flashid}"
Adriana Kobylak34124352020-05-22 09:40:40 -0500591}
592
Adriana Kobylak9cbfa2e2019-04-25 14:02:37 -0500593case "$1" in
594 mtduboot)
595 reqmtd="$2"
596 version="$3"
597 imgfile="image-u-boot"
598 mtd_write
599 ;;
600 ubirw)
601 reqmtd="$2"
602 name="$3"
603 imgsize="$4"
604 ubi_rw
605 ;;
606 ubiro)
607 reqmtd="$(echo "$2" | cut -d "+" -f 1)"
608 reqmtd2="$(echo "$2" | cut -d "+" -f 2)"
609 name="$3"
610 version="$4"
611 imgfile="image-rofs"
612 ubi_ro
613 ubi_updatevol
614 ubi_block
615 ;;
616 ubikernel)
617 reqmtd="$(echo "$2" | cut -d "+" -f 1)"
618 reqmtd2="$(echo "$2" | cut -d "+" -f 2)"
619 name="$3"
620 version="$4"
621 imgfile="image-kernel"
622 ubi_ro
623 ubi_updatevol
624 create_vol_in_alt
625 ;;
626 ubiremove)
627 name="$2"
628 ubi_remove "${name}"
629 ;;
630 ubicleanup)
631 ubi_cleanup
632 ;;
633 ubisetenv)
634 ubi_setenv "$2"
635 ;;
636 ubiremount)
637 remount_ubi
638 mount_alt_rwfs
639 ;;
640 createenvbackup)
641 backup_env_vars
642 ;;
643 updateubootvars)
Adriana Kobylak25773a72022-01-21 15:24:48 +0000644 flashid="$2"
Adriana Kobylak9cbfa2e2019-04-25 14:02:37 -0500645 update_env_vars
646 ;;
647 rebootguardenable)
648 rebootguardenable
649 ;;
650 rebootguarddisable)
651 rebootguarddisable
652 ;;
653 mirroruboot)
654 mirroruboot
655 ;;
Adriana Kobylak70f5bc02020-05-13 14:08:14 -0500656 mmc)
657 version="$2"
658 imgpath="$3"
659 mmc_update
660 ;;
661 mmc-remove)
Adriana Kobylak25773a72022-01-21 15:24:48 +0000662 flashid="$2"
Adriana Kobylak70f5bc02020-05-13 14:08:14 -0500663 mmc_remove
664 ;;
Adriana Kobylak34124352020-05-22 09:40:40 -0500665 mmc-setprimary)
Adriana Kobylak25773a72022-01-21 15:24:48 +0000666 flashid="$2"
Adriana Kobylak34124352020-05-22 09:40:40 -0500667 mmc_setprimary
668 ;;
Adriana Kobylak9cbfa2e2019-04-25 14:02:37 -0500669 *)
670 echo "Invalid argument"
671 exit 1
672 ;;
673esac