blob: 07e730ec93dfe88e012e503bc7ed021b0a3f33d0 [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
Lei YUb5171ac2021-12-16 18:36:27 +0800320umount_static_alt() {
321 altFs=$1
322 altFsMount="/run/media/${altFs}"
323 umount "${altFsMount}"
324}
325
Adriana Kobylak9cbfa2e2019-04-25 14:02:37 -0500326# Read the current env variable and set it on the alternate boot env
327copy_env_var_to_alt() {
328 varName=$1
329 value="$(fw_printenv -n "${varName}")"
330 fw_setenv -c /etc/alt_fw_env.config "${varName}" "${value}"
331}
332
333# When the alternate bmc chip boots, u-boot thinks its the primary mtdX.
334# Therefore need to swap the chip numbers when copying the ubiblock and root to
335# alternate bmc u-boot environment.
336copy_ubiblock_to_alt() {
337 value="$(fw_printenv -n ubiblock)"
338 bmcNum="$(findmtdnum "bmc")"
339 altNum="$(findmtdnum "alt-bmc")"
340 replaceAlt="${value/${altNum},/${bmcNum},}"
341
342 if [[ "${value}" == "${replaceAlt}" ]]; then
343 replaceBmc="${value/${bmcNum},/${altNum},}"
344 value=${replaceBmc}
345 else
346 value=${replaceAlt}
347 fi
348
349 fw_setenv -c /etc/alt_fw_env.config ubiblock "${value}"
350}
351
352copy_root_to_alt() {
353 value="$(fw_printenv -n root)"
354 bmcNum="$(findmtdnum "bmc")"
355 altNum="$(findmtdnum "alt-bmc")"
356 replaceAlt="${value/${altNum}_/${bmcNum}_}"
357
358 if [[ "${value}" == "${replaceAlt}" ]]; then
359 replaceBmc="${value/${bmcNum}_/${altNum}_}"
360 value=${replaceBmc}
361 else
362 value=${replaceAlt}
363 fi
364
365 fw_setenv -c /etc/alt_fw_env.config root "${value}"
366}
367
368ubi_setenv() {
369 # The U-Boot environment maintains two banks of environment variables.
370 # The banks need to be consistent with each other to ensure that these
371 # variables can reliably be read from file. In order to guarantee that the
372 # banks are both correct, we need to run fw_setenv twice.
373 variable=$1
374 if [[ "$variable" == *"="* ]]; then
375 varName="${variable%=*}"
376 value="${variable##*=}"
377 # Write only if var is not set already to the requested value
378 currentValue="$(fw_printenv -n "${varName}" 2>/dev/null)" || true
379 if [[ "${currenValue}" != "${value}" ]]; then
380 fw_setenv "$varName" "$value"
381 fw_setenv "$varName" "$value"
382 fi
383 else
384 fw_setenv "$variable"
385 fw_setenv "$variable"
386 fi
387}
388
389mtd_write() {
390 flashmtd="$(findmtd "${reqmtd}")"
391 img="/tmp/images/${version}/${imgfile}"
392 flashcp -v ${img} /dev/${flashmtd}
393}
394
395backup_env_vars() {
396 copy_env_var_to_alt kernelname
397 copy_ubiblock_to_alt
398 copy_root_to_alt
399}
400
401update_env_vars() {
Adriana Kobylak25773a72022-01-21 15:24:48 +0000402 vol="$(findubi rofs-"${flashid}")"
Adriana Kobylak9cbfa2e2019-04-25 14:02:37 -0500403 if [ -z "${vol}" ]; then
404 return 1
405 fi
406 ubidevid="${vol#ubi}"
407 block="/dev/ubiblock${ubidevid}"
408 if [ ! -e "${block}" ]; then
409 return 1
410 fi
Adriana Kobylak25773a72022-01-21 15:24:48 +0000411 ubi_setenv "kernelname=kernel-${flashid}"
Adriana Kobylak9cbfa2e2019-04-25 14:02:37 -0500412 ubi_setenv "ubiblock=$(echo "${ubidevid}" | sed 's/_/,/')"
413 ubi_setenv "root=${block}"
414}
415
416#TODO: Replace the implementation with systemd-inhibitors lock
417# once systemd/systemd#949 is resolved
418rebootguardenable() {
419 dir="/run/systemd/system/"
420 file="reboot-guard.conf"
421 units=("reboot" "poweroff" "halt")
422
423 for unit in "${units[@]}"; do
424 mkdir -p ${dir}${unit}.target.d
425 echo -e "[Unit]\nRefuseManualStart=yes" >> ${dir}${unit}.target.d/${file}
426 done
427}
428
429#TODO: Replace the implementation with systemd-inhibitors lock
430# once systemd/systemd#949 is resolved
431rebootguarddisable() {
432 dir="/run/systemd/system/"
433 file="reboot-guard.conf"
434 units=("reboot" "poweroff" "halt")
435
436 for unit in "${units[@]}"; do
437 rm -rf ${dir}${unit}.target.d/${file}
438 done
439}
440
441# Create a copy in the alt mtd
442create_vol_in_alt() {
443 alt="alt-bmc"
444 altmtd="$(findmtd "${alt}")"
445 if [ ! -z "${altmtd}" ]; then
446 reqmtd="${alt}"
447 reqmtd2="${alt}"
448 ubi_ro
449 ubi_updatevol
450 fi
451}
452
453# Copy contents of one MTD device to another
454mtd_copy() {
455 in=$1
456 out=$2
457
458 # Must erase MTD first to prevent corruption
459 flash_eraseall "${out}"
460 dd if="${in}" of="${out}"
461}
462
463mirroruboot() {
464 bmc="$(findmtd "u-boot")"
465 bmcdev="/dev/${bmc}"
466 alt="$(findmtd "alt-u-boot")"
467 altdev="/dev/${alt}"
468
469 checksum_bmc="$(md5sum "${bmcdev}")"
470 checksum_bmc="${checksum_bmc% *}"
471 checksum_alt="$(md5sum "${altdev}")"
472 checksum_alt="${checksum_alt% *}"
473
474 if [[ "${checksum_bmc}" != "${checksum_alt}" ]]; then
475 bmcenv="$(findmtd "u-boot-env")"
476 bmcenvdev="/dev/${bmcenv}"
477 altenv="$(findmtd "alt-u-boot-env")"
478 altenvdev="/dev/${altenv}"
479
480 echo "Mirroring U-boot to alt chip"
481 mtd_copy "${bmcdev}" "${altdev}"
482 mtd_copy "${bmcenvdev}" "${altenvdev}"
483
484 copy_ubiblock_to_alt
485 copy_root_to_alt
486 fi
487}
488
Adriana Kobylak62f38202020-09-29 13:04:25 -0500489# Compare the device where u-boot resides with an image file. Specify the full
490# path to the device and image file to use for the compare. Print a value of
491# "0" if identical, "1" otherwise.
492cmp_uboot() {
493 device="$1"
494 image="$2"
495
496 # Since the image file can be smaller than the device, copy the device to a
497 # tmp file and write the image file on top, then compare the sum of each.
498 # Use cat / redirection since busybox does not have the conv=notrunc option.
Adriana Kobylak9b8816f2020-12-02 11:08:15 -0600499 tmpFile="$(mktemp /tmp/ubootdev.XXXXXX)"
Adriana Kobylak62f38202020-09-29 13:04:25 -0500500 dd if="${device}" of="${tmpFile}"
501 devSum="$(sha256sum ${tmpFile})"
502 cat < "${image}" 1<> "${tmpFile}"
503 imgSum="$(sha256sum ${tmpFile})"
504 rm -f "${tmpFile}"
505
506 if [ "${imgSum}" == "${devSum}" ]; then
507 echo "0";
508 else
509 echo "1";
510 fi
511}
512
Adriana Kobylak70f5bc02020-05-13 14:08:14 -0500513# The eMMC partition labels for the kernel and rootfs are boot-a/b and rofs-a/b.
Adriana Kobylak34124352020-05-22 09:40:40 -0500514# Return the label (a or b) for the running partition.
515mmc_get_primary_label() {
Adriana Kobylak5312d852020-07-18 09:49:34 -0500516 # Get root device /dev/mmcblkpX
Adriana Kobylak34124352020-05-22 09:40:40 -0500517 rootmatch=" on / "
Adriana Kobylak5312d852020-07-18 09:49:34 -0500518 root="$(mount | grep "${rootmatch}")"
519 root="${root%${rootmatch}*}"
520
521 # Find the device label
522 if [[ $(readlink -f /dev/disk/by-partlabel/rofs-a) == "${root}" ]]; then
Adriana Kobylak34124352020-05-22 09:40:40 -0500523 echo "a"
Adriana Kobylak5312d852020-07-18 09:49:34 -0500524 elif [[ $(readlink -f /dev/disk/by-partlabel/rofs-b) == "${root}" ]]; then
Adriana Kobylak34124352020-05-22 09:40:40 -0500525 echo "b"
Adriana Kobylak5312d852020-07-18 09:49:34 -0500526 else
527 echo ""
Adriana Kobylak34124352020-05-22 09:40:40 -0500528 fi
529}
530
531# The eMMC partition labels for the kernel and rootfs are boot-a/b and rofs-a/b.
Adriana Kobylak70f5bc02020-05-13 14:08:14 -0500532# Return the label (a or b) for the non-running partition.
533mmc_get_secondary_label() {
Adriana Kobylak5312d852020-07-18 09:49:34 -0500534 root="$(mmc_get_primary_label)"
535 if [[ "${root}" == "a" ]]; then
Adriana Kobylak70f5bc02020-05-13 14:08:14 -0500536 echo "b"
Adriana Kobylak5312d852020-07-18 09:49:34 -0500537 elif [[ "${root}" == "b" ]]; then
Adriana Kobylak70f5bc02020-05-13 14:08:14 -0500538 echo "a"
Adriana Kobylak5312d852020-07-18 09:49:34 -0500539 else
540 echo ""
Adriana Kobylak70f5bc02020-05-13 14:08:14 -0500541 fi
542}
543
Adriana Kobylak1e81f232022-01-18 22:28:47 +0000544mmc_mount() {
545 primaryId="$(mmc_get_primary_label)"
546 secondaryId="$(mmc_get_secondary_label)"
547
548 primaryDir="${mediaDir}/rofs-${primaryId}-functional"
549 secondaryDir="${mediaDir}/rofs-${secondaryId}"
550
551 mkdir -p "${primaryDir}"
552 mkdir -p "${secondaryDir}"
553
554 mount PARTLABEL=rofs-${primaryId} "${primaryDir}" -t ext4 -o ro || rmdir "${primaryDir}"
555 mount PARTLABEL=rofs-${secondaryId} "${secondaryDir}" -t ext4 -o ro || rmdir "${secondaryDir}"
556}
557
Adriana Kobylak70f5bc02020-05-13 14:08:14 -0500558mmc_update() {
Adriana Kobylak62f38202020-09-29 13:04:25 -0500559 # Update u-boot if needed
560 bootPartition="mmcblk0boot0"
561 devUBoot="/dev/${bootPartition}"
562 imgUBoot="${imgpath}/${version}/image-u-boot"
563 if [ "$(cmp_uboot "${devUBoot}" "${imgUBoot}")" != "0" ]; then
564 echo 0 > "/sys/block/${bootPartition}/force_ro"
565 dd if="${imgUBoot}" of="${devUBoot}"
566 echo 1 > "/sys/block/${bootPartition}/force_ro"
567 fi
568
Adriana Kobylak70f5bc02020-05-13 14:08:14 -0500569 # Update the secondary (non-running) boot and rofs partitions.
570 label="$(mmc_get_secondary_label)"
571
572 # Update the boot and rootfs partitions, restore their labels after the update
573 # by getting the partition number mmcblk0pX from their label.
574 zstd -d -c ${imgpath}/${version}/image-kernel | dd of="/dev/disk/by-partlabel/boot-${label}"
575 number="$(readlink -f /dev/disk/by-partlabel/boot-${label})"
576 number="${number##*mmcblk0p}"
577 sgdisk --change-name=${number}:boot-${label} /dev/mmcblk0 1>/dev/null
578
579 zstd -d -c ${imgpath}/${version}/image-rofs | dd of="/dev/disk/by-partlabel/rofs-${label}"
580 number="$(readlink -f /dev/disk/by-partlabel/rofs-${label})"
581 number="${number##*mmcblk0p}"
582 sgdisk --change-name=${number}:rofs-${label} /dev/mmcblk0 1>/dev/null
583
584 # Run this after sgdisk for labels to take effect.
585 partprobe
586
Adriana Kobylak8c5209d2020-07-12 13:35:47 -0500587 # Update hostfw
588 if [ -f ${imgpath}/${version}/image-hostfw ]; then
Adriana Kobylakd148b4f2020-08-27 10:18:49 -0500589 # Remove patches
590 patchdir="/usr/local/share/hostfw/alternate"
591 if [ -d "${patchdir}" ]; then
592 rm -rf "${patchdir}"/*
593 fi
Adriana Kobylak8c5209d2020-07-12 13:35:47 -0500594 hostfwdir=$(grep "hostfw " /proc/mounts | cut -d " " -f 2)
595 cp ${imgpath}/${version}/image-hostfw ${hostfwdir}/hostfw-${label}
596 mkdir -p ${hostfwdir}/alternate
597 mount ${hostfwdir}/hostfw-${label} ${hostfwdir}/alternate -o ro
598 fi
599
Adriana Kobylaka84f06d2022-01-18 15:41:57 +0000600 set_flashid "${label}"
Adriana Kobylak70f5bc02020-05-13 14:08:14 -0500601}
602
603mmc_remove() {
604 # Render the filesystem unbootable by wiping out the first 1MB, this
605 # invalidates the filesystem header.
Adriana Kobylak25773a72022-01-21 15:24:48 +0000606 # Check if the requested id is the one the BMC is running from since dd
607 # can still write and corrupt the running partition.
608 primaryid="$(mmc_get_primary_label)"
609 if [[ "${flashid}" == "${primaryid}" ]]; then
610 return -1
Adriana Kobylak70f5bc02020-05-13 14:08:14 -0500611 fi
Adriana Kobylak25773a72022-01-21 15:24:48 +0000612 dd if=/dev/zero of=/dev/disk/by-partlabel/boot-${flashid} count=2048
613 dd if=/dev/zero of=/dev/disk/by-partlabel/rofs-${flashid} count=2048
Adriana Kobylak8c5209d2020-07-12 13:35:47 -0500614
615 hostfw_alt="hostfw/alternate"
616 if grep -q "${hostfw_alt}" /proc/mounts; then
617 hostfw_alt=$(grep "${hostfw_alt}" /proc/mounts | cut -d " " -f 2)
618 umount "${hostfw_alt}"
619 fi
620 hostfw_base="hostfw "
621 if grep -q "${hostfw_base}" /proc/mounts; then
622 hostfw_base=$(grep "${hostfw_base}" /proc/mounts | cut -d " " -f 2)
Adriana Kobylak25773a72022-01-21 15:24:48 +0000623 rm -f ${hostfw_base}/hostfw-${flashid}
Adriana Kobylak8c5209d2020-07-12 13:35:47 -0500624 fi
Adriana Kobylak70f5bc02020-05-13 14:08:14 -0500625}
626
Adriana Kobylak34124352020-05-22 09:40:40 -0500627# Set the requested version as primary for the BMC to boot from upon reboot.
628mmc_setprimary() {
Adriana Kobylak25773a72022-01-21 15:24:48 +0000629 # Point root to the flashid of the requested BMC rootfs.
630 fw_setenv bootside "${flashid}"
Adriana Kobylak34124352020-05-22 09:40:40 -0500631}
632
Adriana Kobylak9cbfa2e2019-04-25 14:02:37 -0500633case "$1" in
634 mtduboot)
635 reqmtd="$2"
636 version="$3"
637 imgfile="image-u-boot"
638 mtd_write
639 ;;
640 ubirw)
641 reqmtd="$2"
642 name="$3"
643 imgsize="$4"
644 ubi_rw
645 ;;
646 ubiro)
647 reqmtd="$(echo "$2" | cut -d "+" -f 1)"
648 reqmtd2="$(echo "$2" | cut -d "+" -f 2)"
649 name="$3"
650 version="$4"
651 imgfile="image-rofs"
652 ubi_ro
653 ubi_updatevol
654 ubi_block
655 ;;
656 ubikernel)
657 reqmtd="$(echo "$2" | cut -d "+" -f 1)"
658 reqmtd2="$(echo "$2" | cut -d "+" -f 2)"
659 name="$3"
660 version="$4"
661 imgfile="image-kernel"
662 ubi_ro
663 ubi_updatevol
664 create_vol_in_alt
665 ;;
666 ubiremove)
667 name="$2"
668 ubi_remove "${name}"
669 ;;
670 ubicleanup)
671 ubi_cleanup
672 ;;
673 ubisetenv)
674 ubi_setenv "$2"
675 ;;
676 ubiremount)
677 remount_ubi
Lei YU63769642021-12-10 16:15:04 +0800678 mount_ubi_alt_rwfs
Adriana Kobylak9cbfa2e2019-04-25 14:02:37 -0500679 ;;
680 createenvbackup)
681 backup_env_vars
682 ;;
683 updateubootvars)
Adriana Kobylak25773a72022-01-21 15:24:48 +0000684 flashid="$2"
Adriana Kobylak9cbfa2e2019-04-25 14:02:37 -0500685 update_env_vars
686 ;;
687 rebootguardenable)
688 rebootguardenable
689 ;;
690 rebootguarddisable)
691 rebootguarddisable
692 ;;
693 mirroruboot)
694 mirroruboot
695 ;;
Adriana Kobylak70f5bc02020-05-13 14:08:14 -0500696 mmc)
697 version="$2"
698 imgpath="$3"
699 mmc_update
700 ;;
Adriana Kobylak1e81f232022-01-18 22:28:47 +0000701 mmc-mount)
702 mediaDir="$2"
703 mmc_mount
704 ;;
Adriana Kobylak70f5bc02020-05-13 14:08:14 -0500705 mmc-remove)
Adriana Kobylak25773a72022-01-21 15:24:48 +0000706 flashid="$2"
Adriana Kobylak70f5bc02020-05-13 14:08:14 -0500707 mmc_remove
708 ;;
Adriana Kobylak34124352020-05-22 09:40:40 -0500709 mmc-setprimary)
Adriana Kobylak25773a72022-01-21 15:24:48 +0000710 flashid="$2"
Adriana Kobylak34124352020-05-22 09:40:40 -0500711 mmc_setprimary
712 ;;
Lei YU63769642021-12-10 16:15:04 +0800713 static-altfs)
714 mount_static_alt "$2" "$3" "$4"
715 ;;
Lei YUb5171ac2021-12-16 18:36:27 +0800716 umount-static-altfs)
717 umount_static_alt "$2"
718 ;;
Adriana Kobylak9cbfa2e2019-04-25 14:02:37 -0500719 *)
720 echo "Invalid argument"
721 exit 1
722 ;;
723esac