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}")" |
| 60 | |
| 61 | if is_mounted "${name}"; then |
| 62 | echo "${name} is already mounted." |
| 63 | return 0 |
| 64 | fi |
| 65 | |
| 66 | if [ ! -z "${vol}" ]; then |
| 67 | ubirmvol "${ubidev}" -N "${name}" |
| 68 | fi |
Adriana Kobylak | f15e77c | 2017-07-06 11:26:42 -0500 | [diff] [blame] | 69 | |
| 70 | if [ ! -d "${mountdir}" ]; then |
| 71 | mkdir "${mountdir}" |
| 72 | fi |
| 73 | |
Adriana Kobylak | d316559 | 2017-07-18 12:24:24 -0500 | [diff] [blame] | 74 | # Create a static ubi volume of arbitrary size 24MB, |
| 75 | # the current pnor image is ~19MB |
| 76 | # TODO Set size based on file size openbmc/openbmc#1840 |
Michael Tritz | 45e55f8 | 2017-08-08 13:35:17 -0500 | [diff] [blame] | 77 | ubimkvol "${ubidev}" -N "${name}" -s 24MiB --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 | img="/tmp/images/${version}/pnor.xz.squashfs" |
| 87 | ubiupdatevol "/dev/ubi${ubidevid}" "${img}" |
| 88 | |
| 89 | if [ $? != 0 ]; then |
| 90 | echo "Unable to update RO volume!" |
| 91 | return 1 |
Adriana Kobylak | f15e77c | 2017-07-06 11:26:42 -0500 | [diff] [blame] | 92 | fi |
| 93 | |
Michael Tritz | 45e55f8 | 2017-08-08 13:35:17 -0500 | [diff] [blame] | 94 | ubiblock --create "/dev/ubi${ubidevid}" |
| 95 | |
| 96 | if [ $? != 0 ]; then |
| 97 | echo "Unable to create UBI block for RO volume!" |
| 98 | return 1 |
| 99 | fi |
| 100 | |
| 101 | mount -t squashfs -o ro "/dev/ubiblock${ubidevid}" "${mountdir}" |
| 102 | |
| 103 | if [ $? != 0 ]; then |
| 104 | echo "Unable to mount RO volume!" |
| 105 | return 1 |
Adriana Kobylak | f15e77c | 2017-07-06 11:26:42 -0500 | [diff] [blame] | 106 | fi |
| 107 | } |
| 108 | |
Adriana Kobylak | 6aff574 | 2017-07-06 11:29:46 -0500 | [diff] [blame] | 109 | mount_ubi() { |
| 110 | pnormtd="$(findmtd pnor)" |
| 111 | pnor="${pnormtd#mtd}" |
| 112 | ubidev="/dev/ubi${pnor}" |
Saqib Khan | 14ec4bd | 2017-08-18 11:14:17 -0500 | [diff] [blame] | 113 | pnordev="/dev/mtd${pnor}" |
Adriana Kobylak | f287af3 | 2017-07-12 13:52:22 -0500 | [diff] [blame] | 114 | |
| 115 | if [[ "${name}" == "pnor-patch" ]]; then |
Saqib Khan | 14ec4bd | 2017-08-18 11:14:17 -0500 | [diff] [blame] | 116 | if [[ ! "$(hexdump -C -n 3 ${pnordev})" =~ "UBI" ]]; then |
| 117 | return 0 |
| 118 | fi |
Adriana Kobylak | f287af3 | 2017-07-12 13:52:22 -0500 | [diff] [blame] | 119 | mountdir="/usr/local/share/pnor" |
| 120 | else |
| 121 | mountdir="/media/${name}" |
| 122 | fi |
Adriana Kobylak | 6aff574 | 2017-07-06 11:29:46 -0500 | [diff] [blame] | 123 | |
Adriana Kobylak | d316559 | 2017-07-18 12:24:24 -0500 | [diff] [blame] | 124 | if [[ "${name}" == "pnor-prsv" ]]; then |
| 125 | size="2MiB" |
| 126 | else |
| 127 | size="16MiB" |
| 128 | fi |
| 129 | |
Adriana Kobylak | 6aff574 | 2017-07-06 11:29:46 -0500 | [diff] [blame] | 130 | if [ ! -d "${mountdir}" ]; then |
Adriana Kobylak | f287af3 | 2017-07-12 13:52:22 -0500 | [diff] [blame] | 131 | mkdir -p "${mountdir}" |
Adriana Kobylak | 6aff574 | 2017-07-06 11:29:46 -0500 | [diff] [blame] | 132 | fi |
| 133 | |
Adriana Kobylak | 6aff574 | 2017-07-06 11:29:46 -0500 | [diff] [blame] | 134 | vol="$(findubi "${name}")" |
| 135 | if [ -z "${vol}" ]; then |
Adriana Kobylak | d316559 | 2017-07-18 12:24:24 -0500 | [diff] [blame] | 136 | ubimkvol "${ubidev}" -N "${name}" -s "${size}" |
Adriana Kobylak | 6aff574 | 2017-07-06 11:29:46 -0500 | [diff] [blame] | 137 | fi |
| 138 | |
| 139 | if ! is_mounted "${name}"; then |
| 140 | mountdev="ubi${pnor}:${name}" |
| 141 | mount -t ubifs "${mountdev}" "${mountdir}" |
| 142 | fi |
| 143 | } |
| 144 | |
Adriana Kobylak | e5764af | 2017-06-07 15:24:52 -0500 | [diff] [blame] | 145 | umount_ubi() { |
| 146 | pnormtd="$(findmtd pnor)" |
| 147 | pnor="${pnormtd#mtd}" |
| 148 | ubidev="/dev/ubi${pnor}" |
| 149 | mountdir="/media/${name}" |
| 150 | |
| 151 | if is_mounted "${name}"; then |
| 152 | umount "${mountdir}" |
| 153 | fi |
| 154 | |
| 155 | vol="$(findubi "${name}")" |
| 156 | if [ -n "${vol}" ]; then |
| 157 | ubirmvol "${ubidev}" -N "${name}" |
| 158 | fi |
| 159 | |
| 160 | if [ -d "${mountdir}" ]; then |
| 161 | rm -r "${mountdir}" |
| 162 | fi |
| 163 | } |
| 164 | |
Saqib Khan | 58c870d | 2017-07-26 23:48:20 -0500 | [diff] [blame] | 165 | remount_ubi() { |
| 166 | pnormtd="$(findmtd pnor)" |
| 167 | pnor="${pnormtd#mtd}" |
Saqib Khan | ce61193 | 2017-08-01 09:46:22 -0500 | [diff] [blame] | 168 | pnordev="/dev/mtd${pnor}" |
| 169 | |
| 170 | # Re-Attach the pnor mtd device to ubi |
Saqib Khan | 7bfc76f | 2017-08-07 12:51:48 -0500 | [diff] [blame] | 171 | if [[ $(hexdump -C -n 3 ${pnordev}) =~ "UBI" ]]; then |
| 172 | ubiattach /dev/ubi_ctrl -m "${pnor}" -d "${pnor}" |
| 173 | else |
| 174 | # Device not formatted as ubi. |
| 175 | return 0 |
Saqib Khan | ce61193 | 2017-08-01 09:46:22 -0500 | [diff] [blame] | 176 | fi |
| 177 | |
Saqib Khan | 58c870d | 2017-07-26 23:48:20 -0500 | [diff] [blame] | 178 | # Get information on all ubi volumes |
| 179 | ubinfo=$(ubinfo -d ${pnor}) |
| 180 | presentVolumes=${ubinfo##*:} |
| 181 | IFS=', ' read -r -a array <<< "$presentVolumes" |
| 182 | for element in ${array[@]}; |
| 183 | do |
| 184 | elementProperties=$(ubinfo -d $pnor -n $element) |
| 185 | # Get ubi volume name by getting rid of additional properties |
| 186 | name=${elementProperties#*Name:} |
| 187 | name="${name%Character*}" |
| 188 | name="$(echo -e "${name}" | tr -d '[:space:]')" |
| 189 | |
| 190 | if [[ ${name} == pnor-prsv ]] || [[ ${name} == pnor-rw* ]] || [[ ${name} == pnor-ro* ]]; then |
| 191 | mountdir="/media/${name}" |
Saqib Khan | 76b7b83 | 2017-08-01 11:32:27 -0500 | [diff] [blame] | 192 | if [ ! -d "${mountdir}" ]; then |
| 193 | mkdir -p "${mountdir}" |
| 194 | fi |
| 195 | |
Saqib Khan | 58c870d | 2017-07-26 23:48:20 -0500 | [diff] [blame] | 196 | if [[ ${name} == pnor-ro* ]] |
| 197 | then |
| 198 | ubiblock --create /dev/ubi${pnor}_${element} |
| 199 | mount -t squashfs -o ro "/dev/ubiblock${pnor}_${element}" "${mountdir}" |
| 200 | else |
| 201 | mount -t ubifs "ubi${pnor}:${name}" "${mountdir}" |
| 202 | fi |
| 203 | fi |
| 204 | done |
| 205 | } |
| 206 | |
Saqib Khan | d536f0d | 2017-08-02 12:48:12 -0500 | [diff] [blame] | 207 | update_symlinks() { |
| 208 | PNOR_ACTIVE_PATH="/var/lib/phosphor-software-manager/pnor/" |
| 209 | PNOR_RO_ACTIVE_PATH="/var/lib/phosphor-software-manager/pnor/ro" |
| 210 | PNOR_RO_PREFIX="/media/pnor-ro-" |
| 211 | PNOR_RW_ACTIVE_PATH="/var/lib/phosphor-software-manager/pnor/rw" |
| 212 | PNOR_RW_PREFIX="/media/pnor-rw-" |
| 213 | PNOR_PRSV_ACTIVE_PATH="/var/lib/phosphor-software-manager/pnor/prsv" |
| 214 | PNOR_PRSV="/media/pnor-prsv" |
Saqib Khan | d2249d6 | 2017-08-17 11:40:40 -0500 | [diff] [blame] | 215 | PERSISTENCE_PATH="/var/lib/obmc/openpower-pnor-code-mgmt/" |
Saqib Khan | 9473408 | 2017-09-10 00:44:16 -0500 | [diff] [blame^] | 216 | PNOR_PATCH_LOCATION="/usr/local/share/pnor/" |
Saqib Khan | d536f0d | 2017-08-02 12:48:12 -0500 | [diff] [blame] | 217 | |
Saqib Khan | d2249d6 | 2017-08-17 11:40:40 -0500 | [diff] [blame] | 218 | # Get a list of all active PNOR versions |
| 219 | data="$(ls -d ${PNOR_RO_PREFIX}*)" |
| 220 | IFS=$'\n' array=(${data}) |
Saqib Khan | d536f0d | 2017-08-02 12:48:12 -0500 | [diff] [blame] | 221 | |
Saqib Khan | d2249d6 | 2017-08-17 11:40:40 -0500 | [diff] [blame] | 222 | currentVersion="" |
| 223 | lowestPriority=255 |
| 224 | for element in ${array[@]}; do |
| 225 | #Remove the PNOR_RO_PREFIX from the path to get version ID. |
| 226 | versionId="${element#${PNOR_RO_PREFIX}}" |
| 227 | |
| 228 | # Get the priority of active versions from persistence files. |
| 229 | if [[ -f "${PERSISTENCE_PATH}${versionId}" ]]; then |
| 230 | data="$(grep -r "priority" ${PERSISTENCE_PATH}${versionId})" |
| 231 | priority="${data: -1}" |
| 232 | if [[ priority -le lowestPriority ]]; then |
| 233 | lowestPriority=${priority} |
| 234 | currentVersion=${versionId} |
| 235 | fi |
Saqib Khan | d536f0d | 2017-08-02 12:48:12 -0500 | [diff] [blame] | 236 | fi |
| 237 | done |
| 238 | |
Saqib Khan | d2249d6 | 2017-08-17 11:40:40 -0500 | [diff] [blame] | 239 | # Return if no active version found |
| 240 | if [ -z $currentVersion ]; then |
| 241 | return 0; |
| 242 | fi |
| 243 | |
Saqib Khan | d536f0d | 2017-08-02 12:48:12 -0500 | [diff] [blame] | 244 | if [ ! -d "${PNOR_ACTIVE_PATH}" ]; then |
| 245 | mkdir -p "${PNOR_ACTIVE_PATH}" |
| 246 | fi |
| 247 | |
| 248 | # If the RW or RO active links doesn't point to the version with |
| 249 | # lowest priority, then remove the symlink and create new ones. |
| 250 | if [[ $(readlink -f "${PNOR_RO_ACTIVE_PATH}") != ${PNOR_RO_PREFIX}${currentVersion} ]]; then |
| 251 | rm -f ${PNOR_RO_ACTIVE_PATH} |
Saqib Khan | 9473408 | 2017-09-10 00:44:16 -0500 | [diff] [blame^] | 252 | rm -rf ${PNOR_PATCH_LOCATION}* |
Saqib Khan | d536f0d | 2017-08-02 12:48:12 -0500 | [diff] [blame] | 253 | ln -sfv ${PNOR_RO_PREFIX}${currentVersion} ${PNOR_RO_ACTIVE_PATH} |
| 254 | fi |
| 255 | |
| 256 | if [[ $(readlink -f "${PNOR_RW_ACTIVE_PATH}") != ${PNOR_RW_PREFIX}${currentVersion} ]]; then |
| 257 | rm -f ${PNOR_RW_ACTIVE_PATH} |
| 258 | ln -sfv ${PNOR_RW_PREFIX}${currentVersion} ${PNOR_RW_ACTIVE_PATH} |
| 259 | fi |
| 260 | |
| 261 | if [[ ! -h ${PNOR_PRSV_ACTIVE_PATH} ]]; then |
| 262 | ln -sfv ${PNOR_PRSV} ${PNOR_PRSV_ACTIVE_PATH} |
| 263 | fi |
| 264 | } |
| 265 | |
Adriana Kobylak | e882f91 | 2017-07-06 10:31:59 -0500 | [diff] [blame] | 266 | case "$1" in |
| 267 | ubiattach) |
| 268 | attach_ubi |
| 269 | ;; |
Adriana Kobylak | f15e77c | 2017-07-06 11:26:42 -0500 | [diff] [blame] | 270 | squashfsmount) |
| 271 | name="$2" |
| 272 | version="$3" |
| 273 | mount_squashfs |
| 274 | ;; |
Adriana Kobylak | 6aff574 | 2017-07-06 11:29:46 -0500 | [diff] [blame] | 275 | ubimount) |
| 276 | name="$2" |
| 277 | mount_ubi |
| 278 | ;; |
Adriana Kobylak | e5764af | 2017-06-07 15:24:52 -0500 | [diff] [blame] | 279 | ubiumount) |
| 280 | name="$2" |
| 281 | umount_ubi |
| 282 | ;; |
Saqib Khan | 58c870d | 2017-07-26 23:48:20 -0500 | [diff] [blame] | 283 | ubiremount) |
| 284 | remount_ubi |
| 285 | ;; |
Saqib Khan | d536f0d | 2017-08-02 12:48:12 -0500 | [diff] [blame] | 286 | updatesymlinks) |
| 287 | update_symlinks |
| 288 | ;; |
Adriana Kobylak | e882f91 | 2017-07-06 10:31:59 -0500 | [diff] [blame] | 289 | *) |
| 290 | echo "Invalid argument" |
| 291 | exit 1 |
| 292 | ;; |
| 293 | esac |
| 294 | rc=$? |
| 295 | if [ ${rc} -ne 0 ]; then |
| 296 | echo "$0: error ${rc}" |
| 297 | exit ${rc} |
| 298 | fi |