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 | |
| 31 | ubiattach /dev/ubi_ctrl -m "${pnor}" -d "${pnor}" |
| 32 | rc=$? |
| 33 | if [ ${rc} -ne 0 ]; then |
| 34 | # Check the pnor mtd device is formatted as ubi by reading the first 3 byes, |
| 35 | # which should be the ascii chars 'UBI' |
| 36 | magic="$(hexdump -C -n 3 ${pnordev})" |
| 37 | if [[ "${magic}" =~ "UBI" ]]; then |
| 38 | # Device already formatted as ubi, ubiattach failed for some other reason |
| 39 | return ${rc} |
| 40 | else |
| 41 | # Format device as ubi |
| 42 | echo "Starting ubiformat ${pnordev}" |
| 43 | ubiformat "${pnordev}" -y -q |
| 44 | # Retry the ubiattach |
| 45 | ubiattach /dev/ubi_ctrl -m "${pnor}" -d "${pnor}" |
| 46 | fi |
| 47 | fi |
| 48 | } |
| 49 | |
Adriana Kobylak | f15e77c | 2017-07-06 11:26:42 -0500 | [diff] [blame] | 50 | mount_squashfs() { |
| 51 | pnormtd="$(findmtd pnor)" |
| 52 | pnor="${pnormtd#mtd}" |
| 53 | ubidev="/dev/ubi${pnor}" |
| 54 | mountdir="/media/${name}" |
| 55 | |
| 56 | if [ ! -d "${mountdir}" ]; then |
| 57 | mkdir "${mountdir}" |
| 58 | fi |
| 59 | |
Adriana Kobylak | d316559 | 2017-07-18 12:24:24 -0500 | [diff] [blame] | 60 | # Create a static ubi volume of arbitrary size 24MB, |
| 61 | # the current pnor image is ~19MB |
| 62 | # TODO Set size based on file size openbmc/openbmc#1840 |
Adriana Kobylak | f15e77c | 2017-07-06 11:26:42 -0500 | [diff] [blame] | 63 | vol="$(findubi "${name}")" |
| 64 | if [ -z "${vol}" ]; then |
Adriana Kobylak | d316559 | 2017-07-18 12:24:24 -0500 | [diff] [blame] | 65 | ubimkvol "${ubidev}" -N "${name}" -s 24MiB --type=static |
Adriana Kobylak | f15e77c | 2017-07-06 11:26:42 -0500 | [diff] [blame] | 66 | vol="$(findubi "${name}")" |
| 67 | fi |
| 68 | |
| 69 | # Create a ubi block needed for read-only volumes, |
| 70 | # and update the volume with the pnor squashfs image |
| 71 | ubidevid="${vol#ubi}" |
| 72 | block="/dev/ubiblock${ubidevid}" |
| 73 | if [ ! -e "$block" ]; then |
| 74 | img="/tmp/images/${version}/pnor.xz.squashfs" |
| 75 | ubiblock --create "/dev/ubi${ubidevid}" |
| 76 | ubiupdatevol "/dev/ubi${ubidevid}" "${img}" |
| 77 | fi |
| 78 | |
| 79 | if ! is_mounted "${name}"; then |
| 80 | mount -t squashfs -o ro "${block}" "${mountdir}" |
| 81 | fi |
| 82 | } |
| 83 | |
Adriana Kobylak | 6aff574 | 2017-07-06 11:29:46 -0500 | [diff] [blame] | 84 | mount_ubi() { |
| 85 | pnormtd="$(findmtd pnor)" |
| 86 | pnor="${pnormtd#mtd}" |
| 87 | ubidev="/dev/ubi${pnor}" |
Adriana Kobylak | f287af3 | 2017-07-12 13:52:22 -0500 | [diff] [blame^] | 88 | |
| 89 | if [[ "${name}" == "pnor-patch" ]]; then |
| 90 | mountdir="/usr/local/share/pnor" |
| 91 | else |
| 92 | mountdir="/media/${name}" |
| 93 | fi |
Adriana Kobylak | 6aff574 | 2017-07-06 11:29:46 -0500 | [diff] [blame] | 94 | |
Adriana Kobylak | d316559 | 2017-07-18 12:24:24 -0500 | [diff] [blame] | 95 | if [[ "${name}" == "pnor-prsv" ]]; then |
| 96 | size="2MiB" |
| 97 | else |
| 98 | size="16MiB" |
| 99 | fi |
| 100 | |
Adriana Kobylak | 6aff574 | 2017-07-06 11:29:46 -0500 | [diff] [blame] | 101 | if [ ! -d "${mountdir}" ]; then |
Adriana Kobylak | f287af3 | 2017-07-12 13:52:22 -0500 | [diff] [blame^] | 102 | mkdir -p "${mountdir}" |
Adriana Kobylak | 6aff574 | 2017-07-06 11:29:46 -0500 | [diff] [blame] | 103 | fi |
| 104 | |
Adriana Kobylak | 6aff574 | 2017-07-06 11:29:46 -0500 | [diff] [blame] | 105 | vol="$(findubi "${name}")" |
| 106 | if [ -z "${vol}" ]; then |
Adriana Kobylak | d316559 | 2017-07-18 12:24:24 -0500 | [diff] [blame] | 107 | ubimkvol "${ubidev}" -N "${name}" -s "${size}" |
Adriana Kobylak | 6aff574 | 2017-07-06 11:29:46 -0500 | [diff] [blame] | 108 | fi |
| 109 | |
| 110 | if ! is_mounted "${name}"; then |
| 111 | mountdev="ubi${pnor}:${name}" |
| 112 | mount -t ubifs "${mountdev}" "${mountdir}" |
| 113 | fi |
| 114 | } |
| 115 | |
Adriana Kobylak | e5764af | 2017-06-07 15:24:52 -0500 | [diff] [blame] | 116 | umount_ubi() { |
| 117 | pnormtd="$(findmtd pnor)" |
| 118 | pnor="${pnormtd#mtd}" |
| 119 | ubidev="/dev/ubi${pnor}" |
| 120 | mountdir="/media/${name}" |
| 121 | |
| 122 | if is_mounted "${name}"; then |
| 123 | umount "${mountdir}" |
| 124 | fi |
| 125 | |
| 126 | vol="$(findubi "${name}")" |
| 127 | if [ -n "${vol}" ]; then |
| 128 | ubirmvol "${ubidev}" -N "${name}" |
| 129 | fi |
| 130 | |
| 131 | if [ -d "${mountdir}" ]; then |
| 132 | rm -r "${mountdir}" |
| 133 | fi |
| 134 | } |
| 135 | |
Saqib Khan | 58c870d | 2017-07-26 23:48:20 -0500 | [diff] [blame] | 136 | remount_ubi() { |
| 137 | pnormtd="$(findmtd pnor)" |
| 138 | pnor="${pnormtd#mtd}" |
Saqib Khan | ce61193 | 2017-08-01 09:46:22 -0500 | [diff] [blame] | 139 | pnordev="/dev/mtd${pnor}" |
| 140 | |
| 141 | # Re-Attach the pnor mtd device to ubi |
| 142 | ubiattach /dev/ubi_ctrl -m "${pnor}" -d "${pnor}" |
| 143 | rc=$? |
| 144 | if [ ${rc} -ne 0 ]; then |
| 145 | # Check the pnor mtd device is formatted as ubi by reading |
| 146 | # the first 3 byes, which should be the ascii chars 'UBI' |
| 147 | magic="$(hexdump -C -n 3 ${pnordev})" |
| 148 | if [[ ! "${magic}" =~ "UBI" ]]; then |
| 149 | # Device not formatted as ubi. |
| 150 | return |
| 151 | fi |
| 152 | fi |
| 153 | |
Saqib Khan | 58c870d | 2017-07-26 23:48:20 -0500 | [diff] [blame] | 154 | # Get information on all ubi volumes |
| 155 | ubinfo=$(ubinfo -d ${pnor}) |
| 156 | presentVolumes=${ubinfo##*:} |
| 157 | IFS=', ' read -r -a array <<< "$presentVolumes" |
| 158 | for element in ${array[@]}; |
| 159 | do |
| 160 | elementProperties=$(ubinfo -d $pnor -n $element) |
| 161 | # Get ubi volume name by getting rid of additional properties |
| 162 | name=${elementProperties#*Name:} |
| 163 | name="${name%Character*}" |
| 164 | name="$(echo -e "${name}" | tr -d '[:space:]')" |
| 165 | |
| 166 | if [[ ${name} == pnor-prsv ]] || [[ ${name} == pnor-rw* ]] || [[ ${name} == pnor-ro* ]]; then |
| 167 | mountdir="/media/${name}" |
Saqib Khan | 76b7b83 | 2017-08-01 11:32:27 -0500 | [diff] [blame] | 168 | if [ ! -d "${mountdir}" ]; then |
| 169 | mkdir -p "${mountdir}" |
| 170 | fi |
| 171 | |
Saqib Khan | 58c870d | 2017-07-26 23:48:20 -0500 | [diff] [blame] | 172 | if [[ ${name} == pnor-ro* ]] |
| 173 | then |
| 174 | ubiblock --create /dev/ubi${pnor}_${element} |
| 175 | mount -t squashfs -o ro "/dev/ubiblock${pnor}_${element}" "${mountdir}" |
| 176 | else |
| 177 | mount -t ubifs "ubi${pnor}:${name}" "${mountdir}" |
| 178 | fi |
| 179 | fi |
| 180 | done |
| 181 | } |
| 182 | |
Adriana Kobylak | e882f91 | 2017-07-06 10:31:59 -0500 | [diff] [blame] | 183 | case "$1" in |
| 184 | ubiattach) |
| 185 | attach_ubi |
| 186 | ;; |
Adriana Kobylak | f15e77c | 2017-07-06 11:26:42 -0500 | [diff] [blame] | 187 | squashfsmount) |
| 188 | name="$2" |
| 189 | version="$3" |
| 190 | mount_squashfs |
| 191 | ;; |
Adriana Kobylak | 6aff574 | 2017-07-06 11:29:46 -0500 | [diff] [blame] | 192 | ubimount) |
| 193 | name="$2" |
| 194 | mount_ubi |
| 195 | ;; |
Adriana Kobylak | e5764af | 2017-06-07 15:24:52 -0500 | [diff] [blame] | 196 | ubiumount) |
| 197 | name="$2" |
| 198 | umount_ubi |
| 199 | ;; |
Saqib Khan | 58c870d | 2017-07-26 23:48:20 -0500 | [diff] [blame] | 200 | ubiremount) |
| 201 | remount_ubi |
| 202 | ;; |
Adriana Kobylak | e882f91 | 2017-07-06 10:31:59 -0500 | [diff] [blame] | 203 | *) |
| 204 | echo "Invalid argument" |
| 205 | exit 1 |
| 206 | ;; |
| 207 | esac |
| 208 | rc=$? |
| 209 | if [ ${rc} -ne 0 ]; then |
| 210 | echo "$0: error ${rc}" |
| 211 | exit ${rc} |
| 212 | fi |