blob: ab353bf8cc4df4517d3a44c347ccf90948be0d12 [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
Lei YU63769642021-12-10 16:15:04 +0800248mount_ubi_alt_rwfs() {
Adriana Kobylak9cbfa2e2019-04-25 14:02:37 -0500249 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
Adriana Kobylak1e81f232022-01-18 22:28:47 +0000267 rootubi="$(findrootubi)"
268 rootname="$(findname "${rootubi}")"
269
Adriana Kobylak9cbfa2e2019-04-25 14:02:37 -0500270 IFS=',' read -r -a mtds <<< "$mtds"
271 mtds=($(echo "${mtds[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' '))
272 for mtd in ${mtds[@]}; do
273 # Get information on all ubi volumes
274 ubinfo=$(ubinfo -d ${mtd})
275 presentVolumes=${ubinfo##*:}
276 IFS=', ' read -r -a array <<< "$presentVolumes"
277 for element in ${array[@]}; do
278 elementProperties=$(ubinfo -d $mtd -n $element)
279 # Get ubi volume name by getting rid of additional properties
280 name=${elementProperties#*Name:}
281 name="${name%Character*}"
282 name="$(echo -e "${name}" | tr -d '[:space:]')"
283
284 if [[ ${name} == rofs-* ]]; then
Adriana Kobylak1e81f232022-01-18 22:28:47 +0000285 if [[ "${name}" == "${rootname}" ]]; then
286 mountdir="/media/${name}-functional"
287 else
288 mountdir="/media/${name}"
289 fi
Adriana Kobylak9cbfa2e2019-04-25 14:02:37 -0500290
291 if [ ! -d ${mountdir} ]; then
292 mkdir -p "${mountdir}"
293 # U-Boot will create the ubiblock for the running version, but not
294 # for the version on the other chip
295 if [ ! -e "/dev/ubiblock${mtd}_${element}" ]; then
296 ubiblock --create /dev/ubi${mtd}_${element}
297 fi
298 mount -t squashfs -o ro "/dev/ubiblock${mtd}_${element}" "${mountdir}"
299 fi
300 fi
301 done
302 done
303
304 set_wdt2bite
305}
306
Lei YU63769642021-12-10 16:15:04 +0800307mount_static_alt() {
308 typ=$1
309 altFs=$2
310 mountName=$3
311 altNum="$(findmtdnum ${altFs})"
312 if [ ! -z "${altNum}" ]; then
313 altFsMount="/run/media/${mountName}"
314 mkdir -p "${altFsMount}"
315 altFsBlock="/dev/mtdblock${altNum}"
316 mount -t "${typ}" "${altFsBlock}" "${altFsMount}"
317 fi
318}
319
Adriana Kobylak9cbfa2e2019-04-25 14:02:37 -0500320# Read the current env variable and set it on the alternate boot env
321copy_env_var_to_alt() {
322 varName=$1
323 value="$(fw_printenv -n "${varName}")"
324 fw_setenv -c /etc/alt_fw_env.config "${varName}" "${value}"
325}
326
327# When the alternate bmc chip boots, u-boot thinks its the primary mtdX.
328# Therefore need to swap the chip numbers when copying the ubiblock and root to
329# alternate bmc u-boot environment.
330copy_ubiblock_to_alt() {
331 value="$(fw_printenv -n ubiblock)"
332 bmcNum="$(findmtdnum "bmc")"
333 altNum="$(findmtdnum "alt-bmc")"
334 replaceAlt="${value/${altNum},/${bmcNum},}"
335
336 if [[ "${value}" == "${replaceAlt}" ]]; then
337 replaceBmc="${value/${bmcNum},/${altNum},}"
338 value=${replaceBmc}
339 else
340 value=${replaceAlt}
341 fi
342
343 fw_setenv -c /etc/alt_fw_env.config ubiblock "${value}"
344}
345
346copy_root_to_alt() {
347 value="$(fw_printenv -n root)"
348 bmcNum="$(findmtdnum "bmc")"
349 altNum="$(findmtdnum "alt-bmc")"
350 replaceAlt="${value/${altNum}_/${bmcNum}_}"
351
352 if [[ "${value}" == "${replaceAlt}" ]]; then
353 replaceBmc="${value/${bmcNum}_/${altNum}_}"
354 value=${replaceBmc}
355 else
356 value=${replaceAlt}
357 fi
358
359 fw_setenv -c /etc/alt_fw_env.config root "${value}"
360}
361
362ubi_setenv() {
363 # The U-Boot environment maintains two banks of environment variables.
364 # The banks need to be consistent with each other to ensure that these
365 # variables can reliably be read from file. In order to guarantee that the
366 # banks are both correct, we need to run fw_setenv twice.
367 variable=$1
368 if [[ "$variable" == *"="* ]]; then
369 varName="${variable%=*}"
370 value="${variable##*=}"
371 # Write only if var is not set already to the requested value
372 currentValue="$(fw_printenv -n "${varName}" 2>/dev/null)" || true
373 if [[ "${currenValue}" != "${value}" ]]; then
374 fw_setenv "$varName" "$value"
375 fw_setenv "$varName" "$value"
376 fi
377 else
378 fw_setenv "$variable"
379 fw_setenv "$variable"
380 fi
381}
382
383mtd_write() {
384 flashmtd="$(findmtd "${reqmtd}")"
385 img="/tmp/images/${version}/${imgfile}"
386 flashcp -v ${img} /dev/${flashmtd}
387}
388
389backup_env_vars() {
390 copy_env_var_to_alt kernelname
391 copy_ubiblock_to_alt
392 copy_root_to_alt
393}
394
395update_env_vars() {
Adriana Kobylak25773a72022-01-21 15:24:48 +0000396 vol="$(findubi rofs-"${flashid}")"
Adriana Kobylak9cbfa2e2019-04-25 14:02:37 -0500397 if [ -z "${vol}" ]; then
398 return 1
399 fi
400 ubidevid="${vol#ubi}"
401 block="/dev/ubiblock${ubidevid}"
402 if [ ! -e "${block}" ]; then
403 return 1
404 fi
Adriana Kobylak25773a72022-01-21 15:24:48 +0000405 ubi_setenv "kernelname=kernel-${flashid}"
Adriana Kobylak9cbfa2e2019-04-25 14:02:37 -0500406 ubi_setenv "ubiblock=$(echo "${ubidevid}" | sed 's/_/,/')"
407 ubi_setenv "root=${block}"
408}
409
410#TODO: Replace the implementation with systemd-inhibitors lock
411# once systemd/systemd#949 is resolved
412rebootguardenable() {
413 dir="/run/systemd/system/"
414 file="reboot-guard.conf"
415 units=("reboot" "poweroff" "halt")
416
417 for unit in "${units[@]}"; do
418 mkdir -p ${dir}${unit}.target.d
419 echo -e "[Unit]\nRefuseManualStart=yes" >> ${dir}${unit}.target.d/${file}
420 done
421}
422
423#TODO: Replace the implementation with systemd-inhibitors lock
424# once systemd/systemd#949 is resolved
425rebootguarddisable() {
426 dir="/run/systemd/system/"
427 file="reboot-guard.conf"
428 units=("reboot" "poweroff" "halt")
429
430 for unit in "${units[@]}"; do
431 rm -rf ${dir}${unit}.target.d/${file}
432 done
433}
434
435# Create a copy in the alt mtd
436create_vol_in_alt() {
437 alt="alt-bmc"
438 altmtd="$(findmtd "${alt}")"
439 if [ ! -z "${altmtd}" ]; then
440 reqmtd="${alt}"
441 reqmtd2="${alt}"
442 ubi_ro
443 ubi_updatevol
444 fi
445}
446
447# Copy contents of one MTD device to another
448mtd_copy() {
449 in=$1
450 out=$2
451
452 # Must erase MTD first to prevent corruption
453 flash_eraseall "${out}"
454 dd if="${in}" of="${out}"
455}
456
457mirroruboot() {
458 bmc="$(findmtd "u-boot")"
459 bmcdev="/dev/${bmc}"
460 alt="$(findmtd "alt-u-boot")"
461 altdev="/dev/${alt}"
462
463 checksum_bmc="$(md5sum "${bmcdev}")"
464 checksum_bmc="${checksum_bmc% *}"
465 checksum_alt="$(md5sum "${altdev}")"
466 checksum_alt="${checksum_alt% *}"
467
468 if [[ "${checksum_bmc}" != "${checksum_alt}" ]]; then
469 bmcenv="$(findmtd "u-boot-env")"
470 bmcenvdev="/dev/${bmcenv}"
471 altenv="$(findmtd "alt-u-boot-env")"
472 altenvdev="/dev/${altenv}"
473
474 echo "Mirroring U-boot to alt chip"
475 mtd_copy "${bmcdev}" "${altdev}"
476 mtd_copy "${bmcenvdev}" "${altenvdev}"
477
478 copy_ubiblock_to_alt
479 copy_root_to_alt
480 fi
481}
482
Adriana Kobylak62f38202020-09-29 13:04:25 -0500483# Compare the device where u-boot resides with an image file. Specify the full
484# path to the device and image file to use for the compare. Print a value of
485# "0" if identical, "1" otherwise.
486cmp_uboot() {
487 device="$1"
488 image="$2"
489
490 # Since the image file can be smaller than the device, copy the device to a
491 # tmp file and write the image file on top, then compare the sum of each.
492 # Use cat / redirection since busybox does not have the conv=notrunc option.
Adriana Kobylak9b8816f2020-12-02 11:08:15 -0600493 tmpFile="$(mktemp /tmp/ubootdev.XXXXXX)"
Adriana Kobylak62f38202020-09-29 13:04:25 -0500494 dd if="${device}" of="${tmpFile}"
495 devSum="$(sha256sum ${tmpFile})"
496 cat < "${image}" 1<> "${tmpFile}"
497 imgSum="$(sha256sum ${tmpFile})"
498 rm -f "${tmpFile}"
499
500 if [ "${imgSum}" == "${devSum}" ]; then
501 echo "0";
502 else
503 echo "1";
504 fi
505}
506
Adriana Kobylak70f5bc02020-05-13 14:08:14 -0500507# The eMMC partition labels for the kernel and rootfs are boot-a/b and rofs-a/b.
Adriana Kobylak34124352020-05-22 09:40:40 -0500508# Return the label (a or b) for the running partition.
509mmc_get_primary_label() {
Adriana Kobylak5312d852020-07-18 09:49:34 -0500510 # Get root device /dev/mmcblkpX
Adriana Kobylak34124352020-05-22 09:40:40 -0500511 rootmatch=" on / "
Adriana Kobylak5312d852020-07-18 09:49:34 -0500512 root="$(mount | grep "${rootmatch}")"
513 root="${root%${rootmatch}*}"
514
515 # Find the device label
516 if [[ $(readlink -f /dev/disk/by-partlabel/rofs-a) == "${root}" ]]; then
Adriana Kobylak34124352020-05-22 09:40:40 -0500517 echo "a"
Adriana Kobylak5312d852020-07-18 09:49:34 -0500518 elif [[ $(readlink -f /dev/disk/by-partlabel/rofs-b) == "${root}" ]]; then
Adriana Kobylak34124352020-05-22 09:40:40 -0500519 echo "b"
Adriana Kobylak5312d852020-07-18 09:49:34 -0500520 else
521 echo ""
Adriana Kobylak34124352020-05-22 09:40:40 -0500522 fi
523}
524
525# The eMMC partition labels for the kernel and rootfs are boot-a/b and rofs-a/b.
Adriana Kobylak70f5bc02020-05-13 14:08:14 -0500526# Return the label (a or b) for the non-running partition.
527mmc_get_secondary_label() {
Adriana Kobylak5312d852020-07-18 09:49:34 -0500528 root="$(mmc_get_primary_label)"
529 if [[ "${root}" == "a" ]]; then
Adriana Kobylak70f5bc02020-05-13 14:08:14 -0500530 echo "b"
Adriana Kobylak5312d852020-07-18 09:49:34 -0500531 elif [[ "${root}" == "b" ]]; then
Adriana Kobylak70f5bc02020-05-13 14:08:14 -0500532 echo "a"
Adriana Kobylak5312d852020-07-18 09:49:34 -0500533 else
534 echo ""
Adriana Kobylak70f5bc02020-05-13 14:08:14 -0500535 fi
536}
537
Adriana Kobylak1e81f232022-01-18 22:28:47 +0000538mmc_mount() {
539 primaryId="$(mmc_get_primary_label)"
540 secondaryId="$(mmc_get_secondary_label)"
541
542 primaryDir="${mediaDir}/rofs-${primaryId}-functional"
543 secondaryDir="${mediaDir}/rofs-${secondaryId}"
544
545 mkdir -p "${primaryDir}"
546 mkdir -p "${secondaryDir}"
547
548 mount PARTLABEL=rofs-${primaryId} "${primaryDir}" -t ext4 -o ro || rmdir "${primaryDir}"
549 mount PARTLABEL=rofs-${secondaryId} "${secondaryDir}" -t ext4 -o ro || rmdir "${secondaryDir}"
550}
551
Adriana Kobylak70f5bc02020-05-13 14:08:14 -0500552mmc_update() {
Adriana Kobylak62f38202020-09-29 13:04:25 -0500553 # Update u-boot if needed
554 bootPartition="mmcblk0boot0"
555 devUBoot="/dev/${bootPartition}"
556 imgUBoot="${imgpath}/${version}/image-u-boot"
557 if [ "$(cmp_uboot "${devUBoot}" "${imgUBoot}")" != "0" ]; then
558 echo 0 > "/sys/block/${bootPartition}/force_ro"
559 dd if="${imgUBoot}" of="${devUBoot}"
560 echo 1 > "/sys/block/${bootPartition}/force_ro"
561 fi
562
Adriana Kobylak70f5bc02020-05-13 14:08:14 -0500563 # Update the secondary (non-running) boot and rofs partitions.
564 label="$(mmc_get_secondary_label)"
565
566 # Update the boot and rootfs partitions, restore their labels after the update
567 # by getting the partition number mmcblk0pX from their label.
568 zstd -d -c ${imgpath}/${version}/image-kernel | dd of="/dev/disk/by-partlabel/boot-${label}"
569 number="$(readlink -f /dev/disk/by-partlabel/boot-${label})"
570 number="${number##*mmcblk0p}"
571 sgdisk --change-name=${number}:boot-${label} /dev/mmcblk0 1>/dev/null
572
573 zstd -d -c ${imgpath}/${version}/image-rofs | dd of="/dev/disk/by-partlabel/rofs-${label}"
574 number="$(readlink -f /dev/disk/by-partlabel/rofs-${label})"
575 number="${number##*mmcblk0p}"
576 sgdisk --change-name=${number}:rofs-${label} /dev/mmcblk0 1>/dev/null
577
578 # Run this after sgdisk for labels to take effect.
579 partprobe
580
Adriana Kobylak8c5209d2020-07-12 13:35:47 -0500581 # Update hostfw
582 if [ -f ${imgpath}/${version}/image-hostfw ]; then
Adriana Kobylakd148b4f2020-08-27 10:18:49 -0500583 # Remove patches
584 patchdir="/usr/local/share/hostfw/alternate"
585 if [ -d "${patchdir}" ]; then
586 rm -rf "${patchdir}"/*
587 fi
Adriana Kobylak8c5209d2020-07-12 13:35:47 -0500588 hostfwdir=$(grep "hostfw " /proc/mounts | cut -d " " -f 2)
589 cp ${imgpath}/${version}/image-hostfw ${hostfwdir}/hostfw-${label}
590 mkdir -p ${hostfwdir}/alternate
591 mount ${hostfwdir}/hostfw-${label} ${hostfwdir}/alternate -o ro
592 fi
593
Adriana Kobylaka84f06d2022-01-18 15:41:57 +0000594 set_flashid "${label}"
Adriana Kobylak70f5bc02020-05-13 14:08:14 -0500595}
596
597mmc_remove() {
598 # Render the filesystem unbootable by wiping out the first 1MB, this
599 # invalidates the filesystem header.
Adriana Kobylak25773a72022-01-21 15:24:48 +0000600 # Check if the requested id is the one the BMC is running from since dd
601 # can still write and corrupt the running partition.
602 primaryid="$(mmc_get_primary_label)"
603 if [[ "${flashid}" == "${primaryid}" ]]; then
604 return -1
Adriana Kobylak70f5bc02020-05-13 14:08:14 -0500605 fi
Adriana Kobylak25773a72022-01-21 15:24:48 +0000606 dd if=/dev/zero of=/dev/disk/by-partlabel/boot-${flashid} count=2048
607 dd if=/dev/zero of=/dev/disk/by-partlabel/rofs-${flashid} count=2048
Adriana Kobylak8c5209d2020-07-12 13:35:47 -0500608
609 hostfw_alt="hostfw/alternate"
610 if grep -q "${hostfw_alt}" /proc/mounts; then
611 hostfw_alt=$(grep "${hostfw_alt}" /proc/mounts | cut -d " " -f 2)
612 umount "${hostfw_alt}"
613 fi
614 hostfw_base="hostfw "
615 if grep -q "${hostfw_base}" /proc/mounts; then
616 hostfw_base=$(grep "${hostfw_base}" /proc/mounts | cut -d " " -f 2)
Adriana Kobylak25773a72022-01-21 15:24:48 +0000617 rm -f ${hostfw_base}/hostfw-${flashid}
Adriana Kobylak8c5209d2020-07-12 13:35:47 -0500618 fi
Adriana Kobylak70f5bc02020-05-13 14:08:14 -0500619}
620
Adriana Kobylak34124352020-05-22 09:40:40 -0500621# Set the requested version as primary for the BMC to boot from upon reboot.
622mmc_setprimary() {
Adriana Kobylak25773a72022-01-21 15:24:48 +0000623 # Point root to the flashid of the requested BMC rootfs.
624 fw_setenv bootside "${flashid}"
Adriana Kobylak34124352020-05-22 09:40:40 -0500625}
626
Adriana Kobylak9cbfa2e2019-04-25 14:02:37 -0500627case "$1" in
628 mtduboot)
629 reqmtd="$2"
630 version="$3"
631 imgfile="image-u-boot"
632 mtd_write
633 ;;
634 ubirw)
635 reqmtd="$2"
636 name="$3"
637 imgsize="$4"
638 ubi_rw
639 ;;
640 ubiro)
641 reqmtd="$(echo "$2" | cut -d "+" -f 1)"
642 reqmtd2="$(echo "$2" | cut -d "+" -f 2)"
643 name="$3"
644 version="$4"
645 imgfile="image-rofs"
646 ubi_ro
647 ubi_updatevol
648 ubi_block
649 ;;
650 ubikernel)
651 reqmtd="$(echo "$2" | cut -d "+" -f 1)"
652 reqmtd2="$(echo "$2" | cut -d "+" -f 2)"
653 name="$3"
654 version="$4"
655 imgfile="image-kernel"
656 ubi_ro
657 ubi_updatevol
658 create_vol_in_alt
659 ;;
660 ubiremove)
661 name="$2"
662 ubi_remove "${name}"
663 ;;
664 ubicleanup)
665 ubi_cleanup
666 ;;
667 ubisetenv)
668 ubi_setenv "$2"
669 ;;
670 ubiremount)
671 remount_ubi
Lei YU63769642021-12-10 16:15:04 +0800672 mount_ubi_alt_rwfs
Adriana Kobylak9cbfa2e2019-04-25 14:02:37 -0500673 ;;
674 createenvbackup)
675 backup_env_vars
676 ;;
677 updateubootvars)
Adriana Kobylak25773a72022-01-21 15:24:48 +0000678 flashid="$2"
Adriana Kobylak9cbfa2e2019-04-25 14:02:37 -0500679 update_env_vars
680 ;;
681 rebootguardenable)
682 rebootguardenable
683 ;;
684 rebootguarddisable)
685 rebootguarddisable
686 ;;
687 mirroruboot)
688 mirroruboot
689 ;;
Adriana Kobylak70f5bc02020-05-13 14:08:14 -0500690 mmc)
691 version="$2"
692 imgpath="$3"
693 mmc_update
694 ;;
Adriana Kobylak1e81f232022-01-18 22:28:47 +0000695 mmc-mount)
696 mediaDir="$2"
697 mmc_mount
698 ;;
Adriana Kobylak70f5bc02020-05-13 14:08:14 -0500699 mmc-remove)
Adriana Kobylak25773a72022-01-21 15:24:48 +0000700 flashid="$2"
Adriana Kobylak70f5bc02020-05-13 14:08:14 -0500701 mmc_remove
702 ;;
Adriana Kobylak34124352020-05-22 09:40:40 -0500703 mmc-setprimary)
Adriana Kobylak25773a72022-01-21 15:24:48 +0000704 flashid="$2"
Adriana Kobylak34124352020-05-22 09:40:40 -0500705 mmc_setprimary
706 ;;
Lei YU63769642021-12-10 16:15:04 +0800707 static-altfs)
708 mount_static_alt "$2" "$3" "$4"
709 ;;
Adriana Kobylak9cbfa2e2019-04-25 14:02:37 -0500710 *)
711 echo "Invalid argument"
712 exit 1
713 ;;
714esac