summaryrefslogtreecommitdiffstats
path: root/meta-phosphor/common/recipes-phosphor/flash/phosphor-software-manager/obmc-flash-bmc
blob: c6879cf7932b9839cd8bd66240969e8890c4b611 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/bin/sh

# Get the root mtd device number (mtdX) from "/dev/ubiblockX_Y on /"
findrootmtd() {
  rootmatch=" on / "
  m="$(mount | grep "${rootmatch}" | grep "ubiblock")"
  m="${m##*ubiblock}"
  m="${m%_*}"
  if [ -z "${m}" ]; then
    # default to bmc mtd (0)
    m=0
  fi
  echo "mtd${m}"
}

# Get the mtd device number (mtdX)
findmtd() {
  m="$(grep -xl "$1" /sys/class/mtd/*/name)"
  m="${m%/name}"
  m="${m##*/}"
  echo "${m}"
}

# Get the ubi device number (ubiX_Y)
findubi() {
  u="$(grep -xl "$1" /sys/class/ubi/ubi?/subsystem/ubi*/name)"
  u="${u%/name}"
  u="${u##*/}"
  echo "${u}"
}

ubi_rw() {
  rwmtd="$(findmtd "${reqmtd}")"
  rw="${rwmtd#mtd}"
  ubidev="/dev/ubi${rw}"

  if [ "$(fw_printenv rwreset)" == "rwreset=true" ]; then
    ubi_remove
    fw_setenv rwreset
  fi

  # Create a ubi volume of size 4MB, that is the current size of the rwfs image
  vol="$(findubi "${name}")"
  if [ -z "${vol}" ]; then
    ubimkvol "${ubidev}" -N "${name}" -s 4MiB
  fi
}

ubi_ro() {
  romtd="$(findmtd "${reqmtd}")"
  romtd2="$(findmtd "${reqmtd2}")"

  if [ ! "${romtd}" == "${romtd2}" ]; then
    # Request to use alternate mtd device, choose the non-root one
    rootmtd="$(findrootmtd)"
    if [ "${rootmtd}" == "${romtd}" ]; then
      romtd="${romtd2}"
    fi
  fi
  ro="${romtd#mtd}"
  ubidev="/dev/ubi${ro}"

  # Create a static ubi volume
  # TODO Get the actual image size openbmc/openbmc#1840
  vol="$(findubi "${name}")"
  if [ -z "${vol}" ]; then
    ubimkvol "${ubidev}" -N "${name}" -s "${imgsize}" --type=static
    vol="$(findubi "${name}")"
  fi
}

# Squashfs images need a ubi block
ubi_block() {
  vol="$(findubi "${name}")"
  ubidevid="${vol#ubi}"
  block="/dev/ubiblock${ubidevid}"
  if [ ! -e "$block" ]; then
    ubiblock --create "/dev/ubi${ubidevid}"
    if [ $? != 0 ]; then
      echo "Unable to create ubiblock ${name}:${ubidevid}"
      return 1
    fi
  fi
  copy_env_var_to_alt ubiblock
  ubi_setenv "ubiblock=$(echo "${ubidevid}" | sed 's/_/,/')"
  copy_env_var_to_alt root
  ubi_setenv "root=${block}"
}

ubi_updatevol() {
  vol="$(findubi "${name}")"
  ubidevid="${vol#ubi}"
  img="/tmp/images/${version}/${imgfile}"
  ubiupdatevol "/dev/ubi${ubidevid}" "${img}"
  if [ $? != 0 ]; then
    echo "Unable to update volume ${name}!"
    return 1
  fi
  if [[ "${name}" =~ "kernel" ]]; then
    copy_env_var_to_alt kernelname
    ubi_setenv "kernelname=${name}"
  fi
}

ubi_remove() {
    vol="$(findubi "${name}")"

    if [ ! -z "$vol" ]; then
        vol="${vol%_*}"

        if grep -q "${vol}:$name" /proc/mounts; then
            mountdir=$(grep "${vol}:$name" /proc/mounts | cut -d " " -f 2)
            umount "$mountdir"
            rm -r "$mountdir"
        fi

        ubirmvol "/dev/${vol}" -N "$name"
    fi
}

remount_ubi() {
  # Get a list of all devices formatted as UBI
  for file in /dev/mtd*; do
    if [[ $(hexdump -C -n 3 $file) =~ "UBI" ]]; then
      mtd="${file: -1}"
      if [[ $mtd =~ ^-?[0-9]+$ ]]; then
        mtds=${mtd},${mtds}
      fi
    fi
  done

  IFS=',' read -r -a mtds <<< "$mtds"
  mtds=($(echo "${mtds[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' '))
  for mtd in ${mtds[@]}; do
    # Re-attach mtd device to ubi if not already done
    ubiattach /dev/ubi_ctrl -m "${mtd}" -d "${mtd}" &> /dev/null
    # Get information on all ubi volumes
    ubinfo=$(ubinfo -d ${mtd})
    presentVolumes=${ubinfo##*:}
    IFS=', ' read -r -a array <<< "$presentVolumes"
    for element in ${array[@]}; do
      elementProperties=$(ubinfo -d $mtd -n $element)
      # Get ubi volume name by getting rid of additional properties
      name=${elementProperties#*Name:}
      name="${name%Character*}"
      name="$(echo -e "${name}" | tr -d '[:space:]')"

      if [[ ${name} == rofs-* ]]; then
        mountdir="/media/${name}"
        mkdir -p "${mountdir}"
        ubiblock --create /dev/ubi${mtd}_${element} &> /dev/null
        mount -t squashfs -o ro "/dev/ubiblock${mtd}_${element}" "${mountdir}"
      fi
    done
  done
}

# Read the current env variable and set it on the alternate boot env
copy_env_var_to_alt() {
  varName=$1
  value="$(fw_printenv -n "${varName}")"
  fw_setenv -c /etc/alt_fw_env.config "${varName}" "${value}"
}

ubi_setenv() {
    variable=$1
    varName="${variable%=*}"
    value="${variable##*=}"
    fw_setenv "$varName" "$value"
}

mtd_write() {
  flashmtd="$(findmtd "${reqmtd}")"
  img="/tmp/images/${version}/${imgfile}"
  flashcp -v ${img} /dev/${flashmtd}
}

case "$1" in
  mtduboot)
    reqmtd="$2"
    version="$3"
    imgfile="image-u-boot"
    mtd_write
    ;;
  ubirw)
    reqmtd="$2"
    name="$3"
    ubi_rw
    ;;
  ubiro)
    reqmtd="$(echo "$2" | cut -d "+" -f 1)"
    reqmtd2="$(echo "$2" | cut -d "+" -f 2)"
    name="$3"
    version="$4"
    imgfile="image-rofs"
    imgsize="16MiB"
    ubi_ro
    ubi_updatevol
    ubi_block
    ;;
  ubikernel)
    reqmtd="$(echo "$2" | cut -d "+" -f 1)"
    reqmtd2="$(echo "$2" | cut -d "+" -f 2)"
    name="$3"
    version="$4"
    imgfile="image-kernel"
    imgsize="4MiB"
    ubi_ro
    ubi_updatevol
    ;;
  ubiremove)
    name="$2"
    ubi_remove
    ;;
  ubisetenv)
    ubi_setenv "$2"
    ;;
  ubiremount)
    remount_ubi
    ;;
  *)
    echo "Invalid argument"
    exit 1
    ;;
esac
rc=$?
if [ ${rc} -ne 0 ]; then
  echo "$0: error ${rc}"
  exit ${rc}
fi
OpenPOWER on IntegriCloud