Adriana Kobylak | e882f91 | 2017-07-06 10:31:59 -0500 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
| 3 | # Get the mtd device number (mtdX) |
| 4 | findmtd() { |
| 5 | m="$(grep -xl "$1" /sys/class/mtd/*/name)" |
| 6 | m="${m%/name}" |
| 7 | m="${m##*/}" |
| 8 | echo "${m}" |
| 9 | } |
| 10 | |
Adriana Kobylak | f15e77c | 2017-07-06 11:26:42 -0500 | [diff] [blame] | 11 | # Get the ubi device number (ubiX_Y) |
| 12 | findubi() { |
| 13 | u="$(grep -xl "$1" /sys/class/ubi/ubi?/subsystem/ubi*/name)" |
| 14 | u="${u%/name}" |
| 15 | u="${u##*/}" |
| 16 | echo "${u}" |
| 17 | } |
| 18 | |
| 19 | # Get the mount information |
| 20 | is_mounted() { |
| 21 | grep -q "$1" /proc/mounts |
| 22 | return $? |
| 23 | } |
| 24 | |
Saqib Khan | ce61193 | 2017-08-01 09:46:22 -0500 | [diff] [blame] | 25 | # Attach the pnor mtd device to ubi. |
Adriana Kobylak | e882f91 | 2017-07-06 10:31:59 -0500 | [diff] [blame] | 26 | attach_ubi() { |
| 27 | pnormtd="$(findmtd pnor)" |
| 28 | pnor="${pnormtd#mtd}" |
| 29 | pnordev="/dev/mtd${pnor}" |
| 30 | |
Saqib Khan | 4f609b8 | 2017-08-02 15:13:26 -0500 | [diff] [blame] | 31 | if [ -d "/sys/class/ubi/ubi${pnor}" ]; then |
| 32 | # Already attached |
| 33 | return 0 |
| 34 | fi |
| 35 | |
Adriana Kobylak | e882f91 | 2017-07-06 10:31:59 -0500 | [diff] [blame] | 36 | ubiattach /dev/ubi_ctrl -m "${pnor}" -d "${pnor}" |
| 37 | rc=$? |
| 38 | if [ ${rc} -ne 0 ]; then |
| 39 | # Check the pnor mtd device is formatted as ubi by reading the first 3 byes, |
| 40 | # which should be the ascii chars 'UBI' |
| 41 | magic="$(hexdump -C -n 3 ${pnordev})" |
| 42 | if [[ "${magic}" =~ "UBI" ]]; then |
| 43 | # Device already formatted as ubi, ubiattach failed for some other reason |
| 44 | return ${rc} |
| 45 | else |
| 46 | # Format device as ubi |
| 47 | echo "Starting ubiformat ${pnordev}" |
| 48 | ubiformat "${pnordev}" -y -q |
| 49 | # Retry the ubiattach |
| 50 | ubiattach /dev/ubi_ctrl -m "${pnor}" -d "${pnor}" |
| 51 | fi |
| 52 | fi |
| 53 | } |
| 54 | |
Adriana Kobylak | f15e77c | 2017-07-06 11:26:42 -0500 | [diff] [blame] | 55 | mount_squashfs() { |
| 56 | pnormtd="$(findmtd pnor)" |
Michael Tritz | 45e55f8 | 2017-08-08 13:35:17 -0500 | [diff] [blame] | 57 | ubidev="/dev/ubi${pnormtd#mtd}" |
Adriana Kobylak | f15e77c | 2017-07-06 11:26:42 -0500 | [diff] [blame] | 58 | mountdir="/media/${name}" |
Michael Tritz | 45e55f8 | 2017-08-08 13:35:17 -0500 | [diff] [blame] | 59 | vol="$(findubi "${name}")" |
Saqib Khan | 541e622 | 2017-10-08 22:30:30 -0500 | [diff] [blame^] | 60 | img="/tmp/images/${version}/pnor.xz.squashfs" |
| 61 | filesize="$(ls -sh $img | awk -F " " {'print $1'})" |
Michael Tritz | 45e55f8 | 2017-08-08 13:35:17 -0500 | [diff] [blame] | 62 | |
| 63 | if is_mounted "${name}"; then |
| 64 | echo "${name} is already mounted." |
| 65 | return 0 |
| 66 | fi |
| 67 | |
| 68 | if [ ! -z "${vol}" ]; then |
| 69 | ubirmvol "${ubidev}" -N "${name}" |
| 70 | fi |
Adriana Kobylak | f15e77c | 2017-07-06 11:26:42 -0500 | [diff] [blame] | 71 | |
| 72 | if [ ! -d "${mountdir}" ]; then |
| 73 | mkdir "${mountdir}" |
| 74 | fi |
| 75 | |
Saqib Khan | 541e622 | 2017-10-08 22:30:30 -0500 | [diff] [blame^] | 76 | # Set size of read-only partition equal to pnor.xz.squashfs |
| 77 | ubimkvol "${ubidev}" -N "${name}" -s "${filesize}"KiB --type=static |
Adriana Kobylak | f15e77c | 2017-07-06 11:26:42 -0500 | [diff] [blame] | 78 | vol="$(findubi "${name}")" |
Michael Tritz | 45e55f8 | 2017-08-08 13:35:17 -0500 | [diff] [blame] | 79 | |
| 80 | if [ $? != 0 ]; then |
| 81 | echo "Unable to create RO volume!" |
| 82 | return 1 |
Adriana Kobylak | f15e77c | 2017-07-06 11:26:42 -0500 | [diff] [blame] | 83 | fi |
| 84 | |
Adriana Kobylak | f15e77c | 2017-07-06 11:26:42 -0500 | [diff] [blame] | 85 | ubidevid="${vol#ubi}" |
Michael Tritz | 45e55f8 | 2017-08-08 13:35:17 -0500 | [diff] [blame] | 86 | ubiupdatevol "/dev/ubi${ubidevid}" "${img}" |
| 87 | |
| 88 | if [ $? != 0 ]; then |
| 89 | echo "Unable to update RO volume!" |
| 90 | return 1 |
Adriana Kobylak | f15e77c | 2017-07-06 11:26:42 -0500 | [diff] [blame] | 91 | fi |
| 92 | |
Michael Tritz | 45e55f8 | 2017-08-08 13:35:17 -0500 | [diff] [blame] | 93 | ubiblock --create "/dev/ubi${ubidevid}" |
| 94 | |
| 95 | if [ $? != 0 ]; then |
| 96 | echo "Unable to create UBI block for RO volume!" |
| 97 | return 1 |
| 98 | fi |
| 99 | |
| 100 | mount -t squashfs -o ro "/dev/ubiblock${ubidevid}" "${mountdir}" |
| 101 | |
| 102 | if [ $? != 0 ]; then |
| 103 | echo "Unable to mount RO volume!" |
| 104 | return 1 |
Adriana Kobylak | f15e77c | 2017-07-06 11:26:42 -0500 | [diff] [blame] | 105 | fi |
| 106 | } |
| 107 | |
Adriana Kobylak | 6aff574 | 2017-07-06 11:29:46 -0500 | [diff] [blame] | 108 | mount_ubi() { |
| 109 | pnormtd="$(findmtd pnor)" |
| 110 | pnor="${pnormtd#mtd}" |
| 111 | ubidev="/dev/ubi${pnor}" |
Saqib Khan | 14ec4bd | 2017-08-18 11:14:17 -0500 | [diff] [blame] | 112 | pnordev="/dev/mtd${pnor}" |
Adriana Kobylak | f287af3 | 2017-07-12 13:52:22 -0500 | [diff] [blame] | 113 | |
| 114 | if [[ "${name}" == "pnor-patch" ]]; then |
Saqib Khan | 14ec4bd | 2017-08-18 11:14:17 -0500 | [diff] [blame] | 115 | if [[ ! "$(hexdump -C -n 3 ${pnordev})" =~ "UBI" ]]; then |
| 116 | return 0 |
| 117 | fi |
Adriana Kobylak | f287af3 | 2017-07-12 13:52:22 -0500 | [diff] [blame] | 118 | mountdir="/usr/local/share/pnor" |
| 119 | else |
| 120 | mountdir="/media/${name}" |
| 121 | fi |
Adriana Kobylak | 6aff574 | 2017-07-06 11:29:46 -0500 | [diff] [blame] | 122 | |
Adriana Kobylak | d316559 | 2017-07-18 12:24:24 -0500 | [diff] [blame] | 123 | if [[ "${name}" == "pnor-prsv" ]]; then |
| 124 | size="2MiB" |
| 125 | else |
| 126 | size="16MiB" |
| 127 | fi |
| 128 | |
Adriana Kobylak | 6aff574 | 2017-07-06 11:29:46 -0500 | [diff] [blame] | 129 | if [ ! -d "${mountdir}" ]; then |
Adriana Kobylak | f287af3 | 2017-07-12 13:52:22 -0500 | [diff] [blame] | 130 | mkdir -p "${mountdir}" |
Adriana Kobylak | 6aff574 | 2017-07-06 11:29:46 -0500 | [diff] [blame] | 131 | fi |
| 132 | |
Adriana Kobylak | 6aff574 | 2017-07-06 11:29:46 -0500 | [diff] [blame] | 133 | vol="$(findubi "${name}")" |
| 134 | if [ -z "${vol}" ]; then |
Adriana Kobylak | d316559 | 2017-07-18 12:24:24 -0500 | [diff] [blame] | 135 | ubimkvol "${ubidev}" -N "${name}" -s "${size}" |
Adriana Kobylak | 6aff574 | 2017-07-06 11:29:46 -0500 | [diff] [blame] | 136 | fi |
| 137 | |
| 138 | if ! is_mounted "${name}"; then |
| 139 | mountdev="ubi${pnor}:${name}" |
| 140 | mount -t ubifs "${mountdev}" "${mountdir}" |
| 141 | fi |
| 142 | } |
| 143 | |
Adriana Kobylak | e5764af | 2017-06-07 15:24:52 -0500 | [diff] [blame] | 144 | umount_ubi() { |
| 145 | pnormtd="$(findmtd pnor)" |
| 146 | pnor="${pnormtd#mtd}" |
| 147 | ubidev="/dev/ubi${pnor}" |
| 148 | mountdir="/media/${name}" |
| 149 | |
| 150 | if is_mounted "${name}"; then |
| 151 | umount "${mountdir}" |
| 152 | fi |
| 153 | |
| 154 | vol="$(findubi "${name}")" |
| 155 | if [ -n "${vol}" ]; then |
| 156 | ubirmvol "${ubidev}" -N "${name}" |
| 157 | fi |
| 158 | |
| 159 | if [ -d "${mountdir}" ]; then |
| 160 | rm -r "${mountdir}" |
| 161 | fi |
| 162 | } |
| 163 | |
Saqib Khan | 58c870d | 2017-07-26 23:48:20 -0500 | [diff] [blame] | 164 | remount_ubi() { |
| 165 | pnormtd="$(findmtd pnor)" |
| 166 | pnor="${pnormtd#mtd}" |
Saqib Khan | ce61193 | 2017-08-01 09:46:22 -0500 | [diff] [blame] | 167 | pnordev="/dev/mtd${pnor}" |
| 168 | |
| 169 | # Re-Attach the pnor mtd device to ubi |
Saqib Khan | 7bfc76f | 2017-08-07 12:51:48 -0500 | [diff] [blame] | 170 | if [[ $(hexdump -C -n 3 ${pnordev}) =~ "UBI" ]]; then |
| 171 | ubiattach /dev/ubi_ctrl -m "${pnor}" -d "${pnor}" |
| 172 | else |
| 173 | # Device not formatted as ubi. |
| 174 | return 0 |
Saqib Khan | ce61193 | 2017-08-01 09:46:22 -0500 | [diff] [blame] | 175 | fi |
| 176 | |
Saqib Khan | 58c870d | 2017-07-26 23:48:20 -0500 | [diff] [blame] | 177 | # Get information on all ubi volumes |
| 178 | ubinfo=$(ubinfo -d ${pnor}) |
| 179 | presentVolumes=${ubinfo##*:} |
| 180 | IFS=', ' read -r -a array <<< "$presentVolumes" |
| 181 | for element in ${array[@]}; |
| 182 | do |
| 183 | elementProperties=$(ubinfo -d $pnor -n $element) |
| 184 | # Get ubi volume name by getting rid of additional properties |
| 185 | name=${elementProperties#*Name:} |
| 186 | name="${name%Character*}" |
| 187 | name="$(echo -e "${name}" | tr -d '[:space:]')" |
| 188 | |
| 189 | if [[ ${name} == pnor-prsv ]] || [[ ${name} == pnor-rw* ]] || [[ ${name} == pnor-ro* ]]; then |
| 190 | mountdir="/media/${name}" |
Saqib Khan | 76b7b83 | 2017-08-01 11:32:27 -0500 | [diff] [blame] | 191 | if [ ! -d "${mountdir}" ]; then |
| 192 | mkdir -p "${mountdir}" |
| 193 | fi |
| 194 | |
Saqib Khan | 58c870d | 2017-07-26 23:48:20 -0500 | [diff] [blame] | 195 | if [[ ${name} == pnor-ro* ]] |
| 196 | then |
| 197 | ubiblock --create /dev/ubi${pnor}_${element} |
| 198 | mount -t squashfs -o ro "/dev/ubiblock${pnor}_${element}" "${mountdir}" |
| 199 | else |
| 200 | mount -t ubifs "ubi${pnor}:${name}" "${mountdir}" |
| 201 | fi |
| 202 | fi |
| 203 | done |
| 204 | } |
| 205 | |
Saqib Khan | d536f0d | 2017-08-02 12:48:12 -0500 | [diff] [blame] | 206 | update_symlinks() { |
| 207 | PNOR_ACTIVE_PATH="/var/lib/phosphor-software-manager/pnor/" |
| 208 | PNOR_RO_ACTIVE_PATH="/var/lib/phosphor-software-manager/pnor/ro" |
| 209 | PNOR_RO_PREFIX="/media/pnor-ro-" |
| 210 | PNOR_RW_ACTIVE_PATH="/var/lib/phosphor-software-manager/pnor/rw" |
| 211 | PNOR_RW_PREFIX="/media/pnor-rw-" |
| 212 | PNOR_PRSV_ACTIVE_PATH="/var/lib/phosphor-software-manager/pnor/prsv" |
| 213 | PNOR_PRSV="/media/pnor-prsv" |
Saqib Khan | d2249d6 | 2017-08-17 11:40:40 -0500 | [diff] [blame] | 214 | PERSISTENCE_PATH="/var/lib/obmc/openpower-pnor-code-mgmt/" |
Saqib Khan | 9473408 | 2017-09-10 00:44:16 -0500 | [diff] [blame] | 215 | PNOR_PATCH_LOCATION="/usr/local/share/pnor/" |
Saqib Khan | d536f0d | 2017-08-02 12:48:12 -0500 | [diff] [blame] | 216 | |
Saqib Khan | d2249d6 | 2017-08-17 11:40:40 -0500 | [diff] [blame] | 217 | # Get a list of all active PNOR versions |
| 218 | data="$(ls -d ${PNOR_RO_PREFIX}*)" |
| 219 | IFS=$'\n' array=(${data}) |
Saqib Khan | d536f0d | 2017-08-02 12:48:12 -0500 | [diff] [blame] | 220 | |
Saqib Khan | d2249d6 | 2017-08-17 11:40:40 -0500 | [diff] [blame] | 221 | currentVersion="" |
| 222 | lowestPriority=255 |
| 223 | for element in ${array[@]}; do |
| 224 | #Remove the PNOR_RO_PREFIX from the path to get version ID. |
| 225 | versionId="${element#${PNOR_RO_PREFIX}}" |
| 226 | |
| 227 | # Get the priority of active versions from persistence files. |
| 228 | if [[ -f "${PERSISTENCE_PATH}${versionId}" ]]; then |
| 229 | data="$(grep -r "priority" ${PERSISTENCE_PATH}${versionId})" |
| 230 | priority="${data: -1}" |
| 231 | if [[ priority -le lowestPriority ]]; then |
| 232 | lowestPriority=${priority} |
| 233 | currentVersion=${versionId} |
| 234 | fi |
Saqib Khan | d536f0d | 2017-08-02 12:48:12 -0500 | [diff] [blame] | 235 | fi |
| 236 | done |
| 237 | |
Saqib Khan | d2249d6 | 2017-08-17 11:40:40 -0500 | [diff] [blame] | 238 | # Return if no active version found |
| 239 | if [ -z $currentVersion ]; then |
| 240 | return 0; |
| 241 | fi |
| 242 | |
Saqib Khan | d536f0d | 2017-08-02 12:48:12 -0500 | [diff] [blame] | 243 | if [ ! -d "${PNOR_ACTIVE_PATH}" ]; then |
| 244 | mkdir -p "${PNOR_ACTIVE_PATH}" |
| 245 | fi |
| 246 | |
| 247 | # If the RW or RO active links doesn't point to the version with |
| 248 | # lowest priority, then remove the symlink and create new ones. |
| 249 | if [[ $(readlink -f "${PNOR_RO_ACTIVE_PATH}") != ${PNOR_RO_PREFIX}${currentVersion} ]]; then |
| 250 | rm -f ${PNOR_RO_ACTIVE_PATH} |
Saqib Khan | 9473408 | 2017-09-10 00:44:16 -0500 | [diff] [blame] | 251 | rm -rf ${PNOR_PATCH_LOCATION}* |
Saqib Khan | d536f0d | 2017-08-02 12:48:12 -0500 | [diff] [blame] | 252 | ln -sfv ${PNOR_RO_PREFIX}${currentVersion} ${PNOR_RO_ACTIVE_PATH} |
| 253 | fi |
| 254 | |
| 255 | if [[ $(readlink -f "${PNOR_RW_ACTIVE_PATH}") != ${PNOR_RW_PREFIX}${currentVersion} ]]; then |
| 256 | rm -f ${PNOR_RW_ACTIVE_PATH} |
| 257 | ln -sfv ${PNOR_RW_PREFIX}${currentVersion} ${PNOR_RW_ACTIVE_PATH} |
| 258 | fi |
| 259 | |
| 260 | if [[ ! -h ${PNOR_PRSV_ACTIVE_PATH} ]]; then |
| 261 | ln -sfv ${PNOR_PRSV} ${PNOR_PRSV_ACTIVE_PATH} |
| 262 | fi |
| 263 | } |
| 264 | |
Michael Tritz | e8219b7 | 2017-09-22 15:00:51 -0500 | [diff] [blame] | 265 | clear_ubi() { |
| 266 | mountdir="/media/${name}" |
| 267 | if is_mounted "${name}"; then |
| 268 | rm -rf $mountdir/..?* $mountdir/.[!.]* $mountdir/* |
| 269 | fi |
| 270 | } |
| 271 | |
Michael Tritz | cebaac0 | 2017-09-21 00:41:15 -0500 | [diff] [blame] | 272 | ubi_cleanup() { |
| 273 | # When ubi_cleanup is run, it expects one or no active version. |
| 274 | activeVersion=$(busctl --list --no-pager tree \ |
| 275 | org.open_power.Software.Host.Updater | \ |
| 276 | grep /xyz/openbmc_project/software/ | tail -c 9) |
| 277 | |
| 278 | if [[ -z "$activeVersion" ]]; then |
| 279 | vols=$(ubinfo -a | grep -e "pnor-ro-" -e "pnor-rw-" | cut -c 14-) |
| 280 | vols=(${vols}) |
| 281 | else |
| 282 | vols=$(ubinfo -a | grep -e "pnor-ro-" -e "pnor-rw-" | \ |
| 283 | grep -v "$activeVersion" | cut -c 14-) |
| 284 | vols=(${vols}) |
| 285 | fi |
| 286 | |
| 287 | for (( index=0; index<${#vols[@]}; index++ )); do |
| 288 | name=${vols[index]} |
| 289 | umount_ubi |
| 290 | done |
| 291 | } |
| 292 | |
Adriana Kobylak | e882f91 | 2017-07-06 10:31:59 -0500 | [diff] [blame] | 293 | case "$1" in |
| 294 | ubiattach) |
| 295 | attach_ubi |
| 296 | ;; |
Adriana Kobylak | f15e77c | 2017-07-06 11:26:42 -0500 | [diff] [blame] | 297 | squashfsmount) |
| 298 | name="$2" |
| 299 | version="$3" |
| 300 | mount_squashfs |
| 301 | ;; |
Adriana Kobylak | 6aff574 | 2017-07-06 11:29:46 -0500 | [diff] [blame] | 302 | ubimount) |
| 303 | name="$2" |
| 304 | mount_ubi |
| 305 | ;; |
Adriana Kobylak | e5764af | 2017-06-07 15:24:52 -0500 | [diff] [blame] | 306 | ubiumount) |
| 307 | name="$2" |
| 308 | umount_ubi |
| 309 | ;; |
Saqib Khan | 58c870d | 2017-07-26 23:48:20 -0500 | [diff] [blame] | 310 | ubiremount) |
| 311 | remount_ubi |
| 312 | ;; |
Saqib Khan | d536f0d | 2017-08-02 12:48:12 -0500 | [diff] [blame] | 313 | updatesymlinks) |
| 314 | update_symlinks |
| 315 | ;; |
Michael Tritz | e8219b7 | 2017-09-22 15:00:51 -0500 | [diff] [blame] | 316 | ubiclear) |
| 317 | name="$2" |
| 318 | clear_ubi |
| 319 | ;; |
Michael Tritz | cebaac0 | 2017-09-21 00:41:15 -0500 | [diff] [blame] | 320 | ubicleanup) |
| 321 | ubi_cleanup |
| 322 | ;; |
Adriana Kobylak | e882f91 | 2017-07-06 10:31:59 -0500 | [diff] [blame] | 323 | *) |
| 324 | echo "Invalid argument" |
| 325 | exit 1 |
| 326 | ;; |
| 327 | esac |
| 328 | rc=$? |
| 329 | if [ ${rc} -ne 0 ]; then |
| 330 | echo "$0: error ${rc}" |
| 331 | exit ${rc} |
| 332 | fi |