Gunnar Mills | bde5822 | 2018-01-22 18:29:31 -0600 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | set -eo pipefail |
Adriana Kobylak | a290eff | 2017-06-27 09:39:57 -0500 | [diff] [blame] | 3 | |
Eddie James | 63fa611 | 2018-03-01 09:55:07 -0600 | [diff] [blame] | 4 | # Check if BMC is currently booted from the alternate chip; returns "1" if true |
| 5 | check_if_alt() { |
| 6 | res=0 |
| 7 | wdiof_cardreset_bit=5 |
| 8 | wdiof_cardreset=$((1 << $wdiof_cardreset_bit)) |
| 9 | |
| 10 | # Read the systemd watchdog config to see which device we should use |
| 11 | line=$(cat /etc/systemd/system.conf | grep "WatchdogDevice") |
| 12 | if [ ! -z ${line} ]; then |
| 13 | watchdog_device=${line##WatchdogDevice=/dev/} |
| 14 | |
| 15 | # Check for legacy symlink to /dev/watchdog0 |
| 16 | if [ ${watchdog_device} == "watchdog" ]; then |
| 17 | watchdog_device="watchdog0" |
| 18 | fi |
| 19 | |
| 20 | # Check the bootstatus to see if we've been reset |
| 21 | path="/sys/class/watchdog/${watchdog_device}/bootstatus" |
| 22 | if [ -f ${path} ]; then |
| 23 | bootstatus=$(cat ${path}) |
| 24 | res=$((($bootstatus & $wdiof_cardreset) >> $wdiof_cardreset_bit)) |
| 25 | fi |
| 26 | fi |
| 27 | |
| 28 | echo "${res}" |
| 29 | } |
| 30 | |
Adriana Kobylak | 54c9f79 | 2017-08-17 14:08:20 -0500 | [diff] [blame] | 31 | # Get the root mtd device number (mtdX) from "/dev/ubiblockX_Y on /" |
| 32 | findrootmtd() { |
| 33 | rootmatch=" on / " |
| 34 | m="$(mount | grep "${rootmatch}" | grep "ubiblock")" |
| 35 | m="${m##*ubiblock}" |
| 36 | m="${m%_*}" |
| 37 | if [ -z "${m}" ]; then |
| 38 | # default to bmc mtd (0) |
| 39 | m=0 |
| 40 | fi |
| 41 | echo "mtd${m}" |
| 42 | } |
| 43 | |
Adriana Kobylak | 582bdea | 2017-08-22 14:51:18 -0500 | [diff] [blame] | 44 | findrootubi() { |
| 45 | rootmatch=" on / " |
| 46 | m="$(mount | grep "${rootmatch}")" |
| 47 | m="${m##*ubiblock}" |
| 48 | m="${m% on*}" |
| 49 | echo "ubi${m}" |
| 50 | } |
| 51 | |
Adriana Kobylak | a290eff | 2017-06-27 09:39:57 -0500 | [diff] [blame] | 52 | # Get the mtd device number (mtdX) |
| 53 | findmtd() { |
| 54 | m="$(grep -xl "$1" /sys/class/mtd/*/name)" |
| 55 | m="${m%/name}" |
| 56 | m="${m##*/}" |
| 57 | echo "${m}" |
| 58 | } |
| 59 | |
Eddie James | a804bc4 | 2018-02-01 16:46:40 -0600 | [diff] [blame] | 60 | # Get the mtd device number only (return X of mtdX) |
| 61 | findmtdnum() { |
| 62 | m="$(findmtd "$1")" |
| 63 | m="${m##mtd}" |
| 64 | echo "${m}" |
| 65 | } |
| 66 | |
Adriana Kobylak | a290eff | 2017-06-27 09:39:57 -0500 | [diff] [blame] | 67 | # Get the ubi device number (ubiX_Y) |
| 68 | findubi() { |
| 69 | u="$(grep -xl "$1" /sys/class/ubi/ubi?/subsystem/ubi*/name)" |
| 70 | u="${u%/name}" |
| 71 | u="${u##*/}" |
| 72 | echo "${u}" |
| 73 | } |
| 74 | |
Adriana Kobylak | ac5ce00 | 2017-09-13 12:59:26 -0500 | [diff] [blame] | 75 | # Get the ubi device number (ubiX_Y) on a specific mtd |
Adriana Kobylak | 582bdea | 2017-08-22 14:51:18 -0500 | [diff] [blame] | 76 | findubi_onmtd() { |
Adriana Kobylak | ac5ce00 | 2017-09-13 12:59:26 -0500 | [diff] [blame] | 77 | u="$(grep -xl "$1" /sys/class/ubi/ubi"$2"/subsystem/ubi"$2"*/name)" |
| 78 | u="${u%/name}" |
| 79 | u="${u##*/}" |
| 80 | echo "${u}" |
| 81 | } |
| 82 | |
| 83 | # Get all ubi device names on a specific mtd that match requested string |
| 84 | findubiname_onmtd() { |
Adriana Kobylak | 582bdea | 2017-08-22 14:51:18 -0500 | [diff] [blame] | 85 | u="$(grep -h "$1" /sys/class/ubi/ubi"$2"/subsystem/ubi"$2"*/name)" |
| 86 | u="${u%/name}" |
| 87 | u="${u##*/}" |
| 88 | echo "${u}" |
| 89 | } |
| 90 | |
Adriana Kobylak | ac5ce00 | 2017-09-13 12:59:26 -0500 | [diff] [blame] | 91 | # Get the name from the requested ubiX_Y volume |
| 92 | findname() { |
| 93 | n="$(cat /sys/class/ubi/$1/name)" |
| 94 | echo "${n}" |
| 95 | } |
| 96 | |
Adriana Kobylak | 582bdea | 2017-08-22 14:51:18 -0500 | [diff] [blame] | 97 | # Make space on flash before creating new volumes. This can be enhanced |
| 98 | # determine current flash usage. For now only keep a "keepmax" number of them |
| 99 | ubi_remove_volumes() |
| 100 | { |
Adriana Kobylak | 582bdea | 2017-08-22 14:51:18 -0500 | [diff] [blame] | 101 | rootubi="$(findrootubi)" |
Adriana Kobylak | ac5ce00 | 2017-09-13 12:59:26 -0500 | [diff] [blame] | 102 | rootname="$(findname "${rootubi}")" |
| 103 | rootversion="${rootname##*-}" |
| 104 | rootkernel="kernel-${rootversion}" |
Adriana Kobylak | 582bdea | 2017-08-22 14:51:18 -0500 | [diff] [blame] | 105 | |
| 106 | # Just keep max number of volumes before updating, don't delete the version |
| 107 | # the BMC is booted from, and when a version is identified to be deleted, |
| 108 | # delete both the rofs and kernel volumes for that version. |
Adriana Kobylak | ac5ce00 | 2017-09-13 12:59:26 -0500 | [diff] [blame] | 109 | rmnames="$(findubiname_onmtd "${name%-*}-" "${ro}")" |
Adriana Kobylak | 582bdea | 2017-08-22 14:51:18 -0500 | [diff] [blame] | 110 | rmnames=(${rmnames}) |
| 111 | ubicount="${#rmnames[@]}" |
| 112 | while [ ${ubicount} -ge ${keepmax} ]; do |
| 113 | # Loop through existing volumes and skip currently active ones |
| 114 | for (( index=0; index<${#rmnames[@]}; index++ )); do |
| 115 | rmname="${rmnames[${index}]}" |
| 116 | rmversion="${rmname##*-}" |
Adriana Kobylak | ac5ce00 | 2017-09-13 12:59:26 -0500 | [diff] [blame] | 117 | [ "${rmversion}" == "${version}" ] && continue |
| 118 | rmubi="$(findubi_onmtd "rofs-${rmversion}" "${ro}")" |
Adriana Kobylak | 582bdea | 2017-08-22 14:51:18 -0500 | [diff] [blame] | 119 | if [[ ( "${rmubi}" != "${rootubi}" ) && |
Adriana Kobylak | ac5ce00 | 2017-09-13 12:59:26 -0500 | [diff] [blame] | 120 | ( "${rmname}" != "${rootkernel}" ) ]]; then |
| 121 | ubi_remove "rofs-${rmversion}" "${ro}" |
| 122 | ubi_remove "kernel-${rmversion}" "${ro}" |
| 123 | # Remove priority value |
| 124 | fw_setenv "${rmversion}" |
Adriana Kobylak | 582bdea | 2017-08-22 14:51:18 -0500 | [diff] [blame] | 125 | break |
| 126 | fi |
| 127 | done |
| 128 | # Decrease count regardless to avoid an infinite loop |
| 129 | (( ubicount-- )) |
| 130 | done |
| 131 | } |
| 132 | |
Adriana Kobylak | a290eff | 2017-06-27 09:39:57 -0500 | [diff] [blame] | 133 | ubi_rw() { |
| 134 | rwmtd="$(findmtd "${reqmtd}")" |
| 135 | rw="${rwmtd#mtd}" |
| 136 | ubidev="/dev/ubi${rw}" |
| 137 | |
Adriana Kobylak | f846e17 | 2018-03-06 14:12:03 -0600 | [diff] [blame] | 138 | # Update rwfs_size, check imgsize was specified, otherwise it'd clear the var |
| 139 | if [ ! -z "$imgsize" ]; then |
| 140 | rwsize="$(fw_printenv -n rwfs_size 2>/dev/null)" || true |
| 141 | if [[ "${imgsize}" != "${rwsize}" ]]; then |
| 142 | fw_setenv rwfs_size "${imgsize}" |
| 143 | fi |
Adriana Kobylak | b705998 | 2018-02-14 16:35:30 -0600 | [diff] [blame] | 144 | fi |
| 145 | |
Adriana Kobylak | a290eff | 2017-06-27 09:39:57 -0500 | [diff] [blame] | 146 | vol="$(findubi "${name}")" |
| 147 | if [ -z "${vol}" ]; then |
Adriana Kobylak | b705998 | 2018-02-14 16:35:30 -0600 | [diff] [blame] | 148 | ubimkvol "${ubidev}" -N "${name}" -s "${imgsize}" |
Adriana Kobylak | a290eff | 2017-06-27 09:39:57 -0500 | [diff] [blame] | 149 | fi |
| 150 | } |
| 151 | |
Adriana Kobylak | e993949 | 2017-07-11 11:34:23 -0500 | [diff] [blame] | 152 | ubi_ro() { |
Adriana Kobylak | 582bdea | 2017-08-22 14:51:18 -0500 | [diff] [blame] | 153 | keepmax=2 # Default 2 volumes per mtd |
Adriana Kobylak | e993949 | 2017-07-11 11:34:23 -0500 | [diff] [blame] | 154 | romtd="$(findmtd "${reqmtd}")" |
Adriana Kobylak | 54c9f79 | 2017-08-17 14:08:20 -0500 | [diff] [blame] | 155 | romtd2="$(findmtd "${reqmtd2}")" |
| 156 | |
| 157 | if [ ! "${romtd}" == "${romtd2}" ]; then |
| 158 | # Request to use alternate mtd device, choose the non-root one |
Adriana Kobylak | 582bdea | 2017-08-22 14:51:18 -0500 | [diff] [blame] | 159 | keepmax=1 # 1 volume on each of the requested mtds |
Adriana Kobylak | 54c9f79 | 2017-08-17 14:08:20 -0500 | [diff] [blame] | 160 | rootmtd="$(findrootmtd)" |
| 161 | if [ "${rootmtd}" == "${romtd}" ]; then |
| 162 | romtd="${romtd2}" |
| 163 | fi |
| 164 | fi |
Adriana Kobylak | e993949 | 2017-07-11 11:34:23 -0500 | [diff] [blame] | 165 | ro="${romtd#mtd}" |
| 166 | ubidev="/dev/ubi${ro}" |
| 167 | |
Adriana Kobylak | 582bdea | 2017-08-22 14:51:18 -0500 | [diff] [blame] | 168 | ubi_remove_volumes |
| 169 | |
Michael Tritz | d8ac7f2 | 2018-02-07 17:04:07 -0600 | [diff] [blame] | 170 | if [ -z "${imgfile}" ]; then |
| 171 | echo "Unable to create read-only volume. Image file not specified." |
| 172 | return 1 |
| 173 | fi |
| 174 | |
| 175 | # Create a ubi volume, dynamically sized to fit BMC image if size unspecified |
| 176 | img="/tmp/images/${version}/${imgfile}" |
| 177 | imgsize="$(stat -c '%s' ${img})" |
| 178 | |
Adriana Kobylak | e993949 | 2017-07-11 11:34:23 -0500 | [diff] [blame] | 179 | vol="$(findubi "${name}")" |
Adriana Kobylak | ddc9dca | 2017-09-05 15:44:35 -0500 | [diff] [blame] | 180 | if [ ! -z "${vol}" ]; then |
| 181 | # Allow a duplicate kernel volume on the alt mtd |
| 182 | if [[ "${name}" =~ "kernel" ]]; then |
Adriana Kobylak | ac5ce00 | 2017-09-13 12:59:26 -0500 | [diff] [blame] | 183 | vol="$(findubi_onmtd "${name}" "${ro}")" |
Adriana Kobylak | ddc9dca | 2017-09-05 15:44:35 -0500 | [diff] [blame] | 184 | fi |
| 185 | fi |
Adriana Kobylak | e993949 | 2017-07-11 11:34:23 -0500 | [diff] [blame] | 186 | if [ -z "${vol}" ]; then |
| 187 | ubimkvol "${ubidev}" -N "${name}" -s "${imgsize}" --type=static |
| 188 | vol="$(findubi "${name}")" |
| 189 | fi |
| 190 | } |
| 191 | |
| 192 | # Squashfs images need a ubi block |
| 193 | ubi_block() { |
| 194 | vol="$(findubi "${name}")" |
| 195 | ubidevid="${vol#ubi}" |
| 196 | block="/dev/ubiblock${ubidevid}" |
| 197 | if [ ! -e "$block" ]; then |
| 198 | ubiblock --create "/dev/ubi${ubidevid}" |
| 199 | fi |
| 200 | } |
| 201 | |
| 202 | ubi_updatevol() { |
| 203 | vol="$(findubi "${name}")" |
| 204 | ubidevid="${vol#ubi}" |
| 205 | img="/tmp/images/${version}/${imgfile}" |
| 206 | ubiupdatevol "/dev/ubi${ubidevid}" "${img}" |
| 207 | } |
| 208 | |
Michael Tritz | a694eed | 2017-07-13 16:26:15 -0500 | [diff] [blame] | 209 | ubi_remove() { |
Adriana Kobylak | 582bdea | 2017-08-22 14:51:18 -0500 | [diff] [blame] | 210 | rmname="$1" |
Adriana Kobylak | ac5ce00 | 2017-09-13 12:59:26 -0500 | [diff] [blame] | 211 | rmmtd="$2" |
| 212 | if [ ! -z "${rmmtd}" ]; then |
| 213 | vol="$(findubi_onmtd "${rmname}" "${rmmtd}")" |
| 214 | else |
| 215 | vol="$(findubi "${rmname}")" |
| 216 | fi |
Michael Tritz | a694eed | 2017-07-13 16:26:15 -0500 | [diff] [blame] | 217 | |
| 218 | if [ ! -z "$vol" ]; then |
| 219 | vol="${vol%_*}" |
| 220 | |
Saqib Khan | f8d2a66 | 2017-09-07 13:57:43 -0500 | [diff] [blame] | 221 | if grep -q "$rmname" /proc/mounts; then |
| 222 | mountdir=$(grep "$rmname" /proc/mounts | cut -d " " -f 2) |
Michael Tritz | a694eed | 2017-07-13 16:26:15 -0500 | [diff] [blame] | 223 | umount "$mountdir" |
| 224 | rm -r "$mountdir" |
| 225 | fi |
| 226 | |
Adriana Kobylak | 582bdea | 2017-08-22 14:51:18 -0500 | [diff] [blame] | 227 | ubirmvol "/dev/${vol}" -N "$rmname" |
Michael Tritz | a694eed | 2017-07-13 16:26:15 -0500 | [diff] [blame] | 228 | fi |
| 229 | } |
| 230 | |
Michael Tritz | 1878117 | 2017-09-21 00:41:15 -0500 | [diff] [blame] | 231 | ubi_cleanup() { |
| 232 | # When ubi_cleanup is run, it expects one or no active version. |
| 233 | activeVersion=$(busctl --list --no-pager tree \ |
| 234 | xyz.openbmc_project.Software.BMC.Updater | \ |
| 235 | grep /xyz/openbmc_project/software/ | tail -c 9) |
| 236 | |
| 237 | if [[ -z "$activeVersion" ]]; then |
| 238 | vols=$(ubinfo -a | grep -e "kernel-" -e "rofs-" | cut -c 14-) |
| 239 | vols=(${vols}) |
| 240 | else |
| 241 | vols=$(ubinfo -a | grep -e "kernel-" -e "rofs-" | \ |
| 242 | grep -v "$activeVersion" | cut -c 14-) |
| 243 | vols=(${vols}) |
| 244 | fi |
| 245 | |
| 246 | for (( index=0; index<${#vols[@]}; index++ )); do |
| 247 | ubi_remove ${vols[index]} |
| 248 | done |
| 249 | } |
| 250 | |
Saqib Khan | bb93e64 | 2017-08-10 11:24:38 -0500 | [diff] [blame] | 251 | remount_ubi() { |
Saqib Khan | d6d1c44 | 2017-09-06 23:24:40 -0500 | [diff] [blame] | 252 | bmcmtd="$(findmtd "bmc")" |
| 253 | altbmcmtd="$(findmtd "alt-bmc")" |
| 254 | mtds="${bmcmtd: -1}","${altbmcmtd: -1}" |
Saqib Khan | bb93e64 | 2017-08-10 11:24:38 -0500 | [diff] [blame] | 255 | |
| 256 | IFS=',' read -r -a mtds <<< "$mtds" |
| 257 | mtds=($(echo "${mtds[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' ')) |
| 258 | for mtd in ${mtds[@]}; do |
Saqib Khan | bb93e64 | 2017-08-10 11:24:38 -0500 | [diff] [blame] | 259 | # Get information on all ubi volumes |
| 260 | ubinfo=$(ubinfo -d ${mtd}) |
| 261 | presentVolumes=${ubinfo##*:} |
| 262 | IFS=', ' read -r -a array <<< "$presentVolumes" |
| 263 | for element in ${array[@]}; do |
| 264 | elementProperties=$(ubinfo -d $mtd -n $element) |
| 265 | # Get ubi volume name by getting rid of additional properties |
| 266 | name=${elementProperties#*Name:} |
| 267 | name="${name%Character*}" |
| 268 | name="$(echo -e "${name}" | tr -d '[:space:]')" |
| 269 | |
Adriana Kobylak | 2c7c8d1 | 2017-08-21 11:00:56 -0500 | [diff] [blame] | 270 | if [[ ${name} == rofs-* ]]; then |
Saqib Khan | bb93e64 | 2017-08-10 11:24:38 -0500 | [diff] [blame] | 271 | mountdir="/media/${name}" |
Saqib Khan | d793f2d | 2017-09-13 14:05:31 -0500 | [diff] [blame] | 272 | |
| 273 | if [ ! -d ${mountdir} ]; then |
| 274 | mkdir -p "${mountdir}" |
Gunnar Mills | bde5822 | 2018-01-22 18:29:31 -0600 | [diff] [blame] | 275 | # U-Boot will create the ubiblock for the running version, but not |
| 276 | # for the version on the other chip |
| 277 | if [ ! -e "/dev/ubiblock${mtd}_${element}" ]; then |
| 278 | ubiblock --create /dev/ubi${mtd}_${element} |
| 279 | fi |
Saqib Khan | d793f2d | 2017-09-13 14:05:31 -0500 | [diff] [blame] | 280 | mount -t squashfs -o ro "/dev/ubiblock${mtd}_${element}" "${mountdir}" |
| 281 | fi |
Saqib Khan | bb93e64 | 2017-08-10 11:24:38 -0500 | [diff] [blame] | 282 | fi |
| 283 | done |
| 284 | done |
| 285 | } |
| 286 | |
Adriana Kobylak | 8cdb82e | 2017-08-22 14:18:39 -0500 | [diff] [blame] | 287 | # Read the current env variable and set it on the alternate boot env |
| 288 | copy_env_var_to_alt() { |
| 289 | varName=$1 |
| 290 | value="$(fw_printenv -n "${varName}")" |
| 291 | fw_setenv -c /etc/alt_fw_env.config "${varName}" "${value}" |
| 292 | } |
| 293 | |
Eddie James | a804bc4 | 2018-02-01 16:46:40 -0600 | [diff] [blame] | 294 | # When the alternate bmc chip boots, u-boot thinks its the primary mtdX. |
| 295 | # Therefore need to swap the chip numbers when copying the ubiblock and root to |
| 296 | # alternate bmc u-boot environment. |
| 297 | copy_ubiblock_to_alt() { |
| 298 | value="$(fw_printenv -n ubiblock)" |
| 299 | bmcNum="$(findmtdnum "bmc")" |
| 300 | altNum="$(findmtdnum "alt-bmc")" |
| 301 | replaceAlt="${value/${altNum},/${bmcNum},}" |
| 302 | |
| 303 | if [[ "${value}" == "${replaceAlt}" ]]; then |
| 304 | replaceBmc="${value/${bmcNum},/${altNum},}" |
| 305 | value=${replaceBmc} |
| 306 | else |
| 307 | value=${replaceAlt} |
| 308 | fi |
| 309 | |
| 310 | fw_setenv -c /etc/alt_fw_env.config ubiblock "${value}" |
| 311 | } |
| 312 | |
| 313 | copy_root_to_alt() { |
| 314 | value="$(fw_printenv -n root)" |
| 315 | bmcNum="$(findmtdnum "bmc")" |
| 316 | altNum="$(findmtdnum "alt-bmc")" |
| 317 | replaceAlt="${value/${altNum}_/${bmcNum}_}" |
| 318 | |
| 319 | if [[ "${value}" == "${replaceAlt}" ]]; then |
| 320 | replaceBmc="${value/${bmcNum}_/${altNum}_}" |
| 321 | value=${replaceBmc} |
| 322 | else |
| 323 | value=${replaceAlt} |
| 324 | fi |
| 325 | |
| 326 | fw_setenv -c /etc/alt_fw_env.config root "${value}" |
| 327 | } |
| 328 | |
Michael Tritz | a694eed | 2017-07-13 16:26:15 -0500 | [diff] [blame] | 329 | ubi_setenv() { |
Michael Tritz | 3e0b067 | 2017-12-15 15:35:14 -0600 | [diff] [blame] | 330 | # The U-Boot environment maintains two banks of environment variables. |
| 331 | # The banks need to be consistent with each other to ensure that these |
| 332 | # variables can reliably be read from file. In order to guarantee that the |
| 333 | # banks are both correct, we need to run fw_setenv twice. |
Adriana Kobylak | 8cdb82e | 2017-08-22 14:18:39 -0500 | [diff] [blame] | 334 | variable=$1 |
Michael Tritz | 8863f70 | 2017-08-25 15:47:07 -0500 | [diff] [blame] | 335 | if [[ "$variable" == *"="* ]]; then |
| 336 | varName="${variable%=*}" |
| 337 | value="${variable##*=}" |
Adriana Kobylak | f5ac32c | 2018-02-09 11:21:57 -0600 | [diff] [blame] | 338 | # Write only if var is not set already to the requested value |
Adriana Kobylak | ffd46ba | 2018-03-02 14:16:43 -0600 | [diff] [blame] | 339 | currentValue="$(fw_printenv -n "${varName}" 2>/dev/null)" || true |
Adriana Kobylak | f5ac32c | 2018-02-09 11:21:57 -0600 | [diff] [blame] | 340 | if [[ "${currenValue}" != "${value}" ]]; then |
| 341 | fw_setenv "$varName" "$value" |
| 342 | fw_setenv "$varName" "$value" |
| 343 | fi |
Michael Tritz | 8863f70 | 2017-08-25 15:47:07 -0500 | [diff] [blame] | 344 | else |
| 345 | fw_setenv "$variable" |
Michael Tritz | 3e0b067 | 2017-12-15 15:35:14 -0600 | [diff] [blame] | 346 | fw_setenv "$variable" |
Michael Tritz | 8863f70 | 2017-08-25 15:47:07 -0500 | [diff] [blame] | 347 | fi |
Michael Tritz | a694eed | 2017-07-13 16:26:15 -0500 | [diff] [blame] | 348 | } |
| 349 | |
Adriana Kobylak | 1fa612c | 2017-08-17 21:56:14 -0500 | [diff] [blame] | 350 | mtd_write() { |
| 351 | flashmtd="$(findmtd "${reqmtd}")" |
| 352 | img="/tmp/images/${version}/${imgfile}" |
| 353 | flashcp -v ${img} /dev/${flashmtd} |
| 354 | } |
| 355 | |
Adriana Kobylak | e2fd46d | 2017-09-05 12:38:19 -0500 | [diff] [blame] | 356 | backup_env_vars() { |
| 357 | copy_env_var_to_alt kernelname |
Eddie James | a804bc4 | 2018-02-01 16:46:40 -0600 | [diff] [blame] | 358 | copy_ubiblock_to_alt |
| 359 | copy_root_to_alt |
Adriana Kobylak | e2fd46d | 2017-09-05 12:38:19 -0500 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | update_env_vars() { |
| 363 | vol="$(findubi rofs-"${version}")" |
| 364 | if [ -z "${vol}" ]; then |
| 365 | return 1 |
| 366 | fi |
| 367 | ubidevid="${vol#ubi}" |
| 368 | block="/dev/ubiblock${ubidevid}" |
| 369 | if [ ! -e "${block}" ]; then |
| 370 | return 1 |
| 371 | fi |
| 372 | ubi_setenv "kernelname=kernel-${version}" |
| 373 | ubi_setenv "ubiblock=$(echo "${ubidevid}" | sed 's/_/,/')" |
| 374 | ubi_setenv "root=${block}" |
| 375 | } |
| 376 | |
Saqib Khan | dfc9b0c | 2017-09-08 10:10:08 -0500 | [diff] [blame] | 377 | #TODO: Replace the implementation with systemd-inhibitors lock |
| 378 | # once systemd/systemd#949 is resolved |
| 379 | rebootguardenable() { |
| 380 | dir="/run/systemd/system/" |
| 381 | file="reboot-guard.conf" |
| 382 | units=("reboot" "poweroff" "halt") |
| 383 | |
| 384 | for unit in "${units[@]}"; do |
| 385 | mkdir -p ${dir}${unit}.target.d |
| 386 | echo -e "[Unit]\nRefuseManualStart=yes" >> ${dir}${unit}.target.d/${file} |
| 387 | done |
| 388 | } |
| 389 | |
| 390 | #TODO: Replace the implementation with systemd-inhibitors lock |
| 391 | # once systemd/systemd#949 is resolved |
| 392 | rebootguarddisable() { |
| 393 | dir="/run/systemd/system/" |
| 394 | file="reboot-guard.conf" |
| 395 | units=("reboot" "poweroff" "halt") |
| 396 | |
| 397 | for unit in "${units[@]}"; do |
| 398 | rm -rf ${dir}${unit}.target.d/${file} |
| 399 | done |
| 400 | } |
| 401 | |
Adriana Kobylak | ddc9dca | 2017-09-05 15:44:35 -0500 | [diff] [blame] | 402 | # Create a copy in the alt mtd |
| 403 | create_vol_in_alt() { |
| 404 | alt="alt-bmc" |
| 405 | altmtd="$(findmtd "${alt}")" |
| 406 | if [ ! -z "${altmtd}" ]; then |
| 407 | reqmtd="${alt}" |
| 408 | reqmtd2="${alt}" |
| 409 | ubi_ro |
| 410 | ubi_updatevol |
| 411 | fi |
| 412 | } |
| 413 | |
Eddie James | 63fa611 | 2018-03-01 09:55:07 -0600 | [diff] [blame] | 414 | # Copy contents of one MTD device to another |
| 415 | mtd_copy() { |
| 416 | in=$1 |
| 417 | out=$2 |
| 418 | |
| 419 | # Must erase MTD first to prevent corruption |
| 420 | flash_eraseall "${out}" |
| 421 | dd if="${in}" of="${out}" |
| 422 | } |
| 423 | |
| 424 | mirroruboot() { |
| 425 | bmc="$(findmtd "u-boot")" |
| 426 | bmcdev="/dev/${bmc}" |
| 427 | alt="$(findmtd "alt-u-boot")" |
| 428 | altdev="/dev/${alt}" |
| 429 | |
| 430 | checksum_bmc="$(md5sum "${bmcdev}")" |
| 431 | checksum_bmc="${checksum_bmc% *}" |
| 432 | checksum_alt="$(md5sum "${altdev}")" |
| 433 | checksum_alt="${checksum_alt% *}" |
| 434 | |
| 435 | if [[ "${checksum_bmc}" != "${checksum_alt}" ]]; then |
| 436 | bmcenv="$(findmtd "u-boot-env")" |
| 437 | bmcenvdev="/dev/${bmcenv}" |
| 438 | altenv="$(findmtd "alt-u-boot-env")" |
| 439 | altenvdev="/dev/${altenv}" |
| 440 | is_alt="$(check_if_alt)" |
| 441 | |
| 442 | if [[ "${is_alt}" == "1" ]]; then |
| 443 | echo "Mirroring U-boot; alt to bmc" |
| 444 | mtd_copy "${altdev}" "${bmcdev}" |
| 445 | mtd_copy "${altenvdev}" "${bmcenvdev}" |
| 446 | else |
| 447 | echo "Mirroring U-boot; bmc to alt" |
| 448 | mtd_copy "${bmcdev}" "${altdev}" |
| 449 | mtd_copy "${bmcenvdev}" "${altenvdev}" |
| 450 | fi |
| 451 | |
| 452 | copy_ubiblock_to_alt |
| 453 | copy_root_to_alt |
| 454 | fi |
| 455 | } |
| 456 | |
Adriana Kobylak | a290eff | 2017-06-27 09:39:57 -0500 | [diff] [blame] | 457 | case "$1" in |
Adriana Kobylak | 1fa612c | 2017-08-17 21:56:14 -0500 | [diff] [blame] | 458 | mtduboot) |
| 459 | reqmtd="$2" |
| 460 | version="$3" |
| 461 | imgfile="image-u-boot" |
| 462 | mtd_write |
| 463 | ;; |
Adriana Kobylak | a290eff | 2017-06-27 09:39:57 -0500 | [diff] [blame] | 464 | ubirw) |
| 465 | reqmtd="$2" |
| 466 | name="$3" |
Adriana Kobylak | b705998 | 2018-02-14 16:35:30 -0600 | [diff] [blame] | 467 | imgsize="$4" |
Adriana Kobylak | a290eff | 2017-06-27 09:39:57 -0500 | [diff] [blame] | 468 | ubi_rw |
| 469 | ;; |
Adriana Kobylak | e993949 | 2017-07-11 11:34:23 -0500 | [diff] [blame] | 470 | ubiro) |
Adriana Kobylak | 54c9f79 | 2017-08-17 14:08:20 -0500 | [diff] [blame] | 471 | reqmtd="$(echo "$2" | cut -d "+" -f 1)" |
| 472 | reqmtd2="$(echo "$2" | cut -d "+" -f 2)" |
Adriana Kobylak | e993949 | 2017-07-11 11:34:23 -0500 | [diff] [blame] | 473 | name="$3" |
| 474 | version="$4" |
| 475 | imgfile="image-rofs" |
Adriana Kobylak | e993949 | 2017-07-11 11:34:23 -0500 | [diff] [blame] | 476 | ubi_ro |
Adriana Kobylak | e993949 | 2017-07-11 11:34:23 -0500 | [diff] [blame] | 477 | ubi_updatevol |
Adriana Kobylak | 8cdb82e | 2017-08-22 14:18:39 -0500 | [diff] [blame] | 478 | ubi_block |
Adriana Kobylak | e993949 | 2017-07-11 11:34:23 -0500 | [diff] [blame] | 479 | ;; |
| 480 | ubikernel) |
Adriana Kobylak | 54c9f79 | 2017-08-17 14:08:20 -0500 | [diff] [blame] | 481 | reqmtd="$(echo "$2" | cut -d "+" -f 1)" |
| 482 | reqmtd2="$(echo "$2" | cut -d "+" -f 2)" |
Adriana Kobylak | e993949 | 2017-07-11 11:34:23 -0500 | [diff] [blame] | 483 | name="$3" |
| 484 | version="$4" |
| 485 | imgfile="image-kernel" |
Adriana Kobylak | e993949 | 2017-07-11 11:34:23 -0500 | [diff] [blame] | 486 | ubi_ro |
| 487 | ubi_updatevol |
Adriana Kobylak | ddc9dca | 2017-09-05 15:44:35 -0500 | [diff] [blame] | 488 | create_vol_in_alt |
Adriana Kobylak | e993949 | 2017-07-11 11:34:23 -0500 | [diff] [blame] | 489 | ;; |
Michael Tritz | a694eed | 2017-07-13 16:26:15 -0500 | [diff] [blame] | 490 | ubiremove) |
| 491 | name="$2" |
Adriana Kobylak | 582bdea | 2017-08-22 14:51:18 -0500 | [diff] [blame] | 492 | ubi_remove "${name}" |
Michael Tritz | a694eed | 2017-07-13 16:26:15 -0500 | [diff] [blame] | 493 | ;; |
Michael Tritz | 1878117 | 2017-09-21 00:41:15 -0500 | [diff] [blame] | 494 | ubicleanup) |
| 495 | ubi_cleanup |
| 496 | ;; |
Michael Tritz | a694eed | 2017-07-13 16:26:15 -0500 | [diff] [blame] | 497 | ubisetenv) |
Adriana Kobylak | 8cdb82e | 2017-08-22 14:18:39 -0500 | [diff] [blame] | 498 | ubi_setenv "$2" |
Michael Tritz | a694eed | 2017-07-13 16:26:15 -0500 | [diff] [blame] | 499 | ;; |
Saqib Khan | bb93e64 | 2017-08-10 11:24:38 -0500 | [diff] [blame] | 500 | ubiremount) |
| 501 | remount_ubi |
| 502 | ;; |
Saqib Khan | 7e4a3f2 | 2017-09-12 09:23:28 -0500 | [diff] [blame] | 503 | createenvbackup) |
Adriana Kobylak | e2fd46d | 2017-09-05 12:38:19 -0500 | [diff] [blame] | 504 | backup_env_vars |
| 505 | ;; |
Saqib Khan | 7e4a3f2 | 2017-09-12 09:23:28 -0500 | [diff] [blame] | 506 | updateubootvars) |
Adriana Kobylak | e2fd46d | 2017-09-05 12:38:19 -0500 | [diff] [blame] | 507 | version="$2" |
| 508 | update_env_vars |
| 509 | ;; |
Saqib Khan | dfc9b0c | 2017-09-08 10:10:08 -0500 | [diff] [blame] | 510 | rebootguardenable) |
| 511 | rebootguardenable |
| 512 | ;; |
| 513 | rebootguarddisable) |
| 514 | rebootguarddisable |
| 515 | ;; |
Eddie James | 63fa611 | 2018-03-01 09:55:07 -0600 | [diff] [blame] | 516 | mirroruboot) |
| 517 | mirroruboot |
| 518 | ;; |
Adriana Kobylak | a290eff | 2017-06-27 09:39:57 -0500 | [diff] [blame] | 519 | *) |
| 520 | echo "Invalid argument" |
| 521 | exit 1 |
| 522 | ;; |
| 523 | esac |