summaryrefslogtreecommitdiffstats
path: root/meta-phosphor/common/recipes-phosphor/flash/phosphor-software-manager/obmc-flash-bmc
blob: 0b1b16ed3fc779729f67e1b0a30f45a306aa1854 (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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
#!/bin/bash
set -eo pipefail

# 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}"
}

findrootubi() {
  rootmatch=" on / "
  m="$(mount | grep "${rootmatch}")"
  m="${m##*ubiblock}"
  m="${m% on*}"
  echo "ubi${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 mtd device number only (return X of mtdX)
findmtdnum() {
  m="$(findmtd "$1")"
  m="${m##mtd}"
  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}"
}

# Get the ubi device number (ubiX_Y) on a specific mtd
findubi_onmtd() {
  u="$(grep -xl "$1" /sys/class/ubi/ubi"$2"/subsystem/ubi"$2"*/name)"
  u="${u%/name}"
  u="${u##*/}"
  echo "${u}"
}

# Get all ubi device names on a specific mtd that match requested string
findubiname_onmtd() {
  u="$(grep -h "$1" /sys/class/ubi/ubi"$2"/subsystem/ubi"$2"*/name)"
  u="${u%/name}"
  u="${u##*/}"
  echo "${u}"
}

# Get the name from the requested ubiX_Y volume
findname() {
  n="$(cat /sys/class/ubi/$1/name)"
  echo "${n}"
}

# Set the u-boot envs that perform a side switch on failure to boot
set_wdt2bite() {
  if ! fw_printenv wdt2bite 2>/dev/null; then
    fw_setenv wdt2bite "mw.l 0x1e785024 0xa 1; mw.b 0x1e78502c 0xb3 1"
    fw_setenv bootalt "run wdt2bite"
    fw_setenv obmc_bootcmd "ubi part obmc-ubi; run do_rwreset; ubi read \
\${loadaddr} \${kernelname}; bootm \${loadaddr} || run bootalt"
  fi
}

# Make space on flash before creating new volumes. This can be enhanced
# determine current flash usage. For now only keep a "keepmax" number of them
ubi_remove_volumes()
{
  rootubi="$(findrootubi)"
  rootname="$(findname "${rootubi}")"
  rootversion="${rootname##*-}"
  rootkernel="kernel-${rootversion}"

  # Just keep max number of volumes before updating, don't delete the version
  # the BMC is booted from, and when a version is identified to be deleted,
  # delete both the rofs and kernel volumes for that version.
  rmnames="$(findubiname_onmtd "${name%-*}-" "${ro}")"
  rmnames=(${rmnames})
  ubicount="${#rmnames[@]}"
  while [ ${ubicount} -ge ${keepmax} ]; do
    # Loop through existing volumes and skip currently active ones
    for (( index=0; index<${#rmnames[@]}; index++ )); do
      rmname="${rmnames[${index}]}"
      rmversion="${rmname##*-}"
      [ "${rmversion}" == "${version}" ] && continue
      rmubi="$(findubi_onmtd "rofs-${rmversion}" "${ro}")"
      if [[ ( "${rmubi}" != "${rootubi}" ) &&
            ( "${rmname}" != "${rootkernel}" ) ]]; then
        ubi_remove "rofs-${rmversion}" "${ro}"
        ubi_remove "kernel-${rmversion}" "${ro}"
        # Remove priority value
        fw_setenv "${rmversion}"
        break
      fi
    done
    # Decrease count regardless to avoid an infinite loop
    (( ubicount-- ))
  done
}

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

  # Update rwfs_size, check imgsize was specified, otherwise it'd clear the var
  if [ ! -z "$imgsize" ]; then
    rwsize="$(fw_printenv -n rwfs_size 2>/dev/null)" || true
    if [[ "${imgsize}" != "${rwsize}" ]]; then
      fw_setenv rwfs_size "${imgsize}"
    fi
  fi

  vol="$(findubi "${name}")"
  if [ -z "${vol}" ]; then
    ubimkvol "${ubidev}" -N "${name}" -s "${imgsize}"
  fi
}

ubi_ro() {
  keepmax=2 # Default 2 volumes per mtd
  romtd="$(findmtd "${reqmtd}")"
  romtd2="$(findmtd "${reqmtd2}")"

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

  ubi_remove_volumes

  if [ -z "${imgfile}" ]; then
    echo "Unable to create read-only volume. Image file not specified."
    return 1
  fi

  # Create a ubi volume, dynamically sized to fit BMC image if size unspecified
  img="/tmp/images/${version}/${imgfile}"
  imgsize="$(stat -c '%s' ${img})"

  vol="$(findubi "${name}")"
  if [ ! -z "${vol}" ]; then
    # Allow a duplicate kernel volume on the alt mtd
    if [[ "${name}" =~ "kernel" ]]; then
      vol="$(findubi_onmtd "${name}" "${ro}")"
    fi
  fi
  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}"
  fi
}

ubi_updatevol() {
  vol="$(findubi "${name}")"
  ubidevid="${vol#ubi}"
  img="/tmp/images/${version}/${imgfile}"
  ubiupdatevol "/dev/ubi${ubidevid}" "${img}"
}

ubi_remove() {
    rmname="$1"
    rmmtd="$2"
    if [ ! -z "${rmmtd}" ]; then
      vol="$(findubi_onmtd "${rmname}" "${rmmtd}")"
    else
      vol="$(findubi "${rmname}")"
    fi

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

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

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

ubi_cleanup() {
    # When ubi_cleanup is run, it expects one or no active version.
    activeVersion=$(busctl --list --no-pager tree \
            xyz.openbmc_project.Software.BMC.Updater | \
            grep /xyz/openbmc_project/software/ | tail -c 9)

    if [[ -z "$activeVersion" ]]; then
        vols=$(ubinfo -a | grep "rofs-" | cut -c 14-)
        vols=(${vols})
    else
        vols=$(ubinfo -a | grep "rofs-" | \
                grep -v "$activeVersion" | cut -c 14-)
        vols=(${vols})
    fi

    for (( index=0; index<${#vols[@]}; index++ )); do
         ubi_remove ${vols[index]}
    done
}

mount_alt_rwfs() {
  altNum="$(findmtdnum "alt-bmc")"
  if [ ! -z "${altNum}" ]; then
    altRwfs=$(ubinfo -a -d ${altNum} | grep -w "rwfs") || true
    if [ ! -z "${altRwfs}" ]; then
      altVarMount="/media/alt/var"
      mkdir -p "${altVarMount}"
      if mount ubi"${altNum}":rwfs "${altVarMount}" -t ubifs -o defaults; then
        mkdir -p "${altVarMount}"/persist/etc
      fi
    fi
  fi
}

remount_ubi() {
  bmcmtd="$(findmtd "bmc")"
  altbmcmtd="$(findmtd "alt-bmc")"
  mtds="${bmcmtd: -1}","${altbmcmtd: -1}"

  IFS=',' read -r -a mtds <<< "$mtds"
  mtds=($(echo "${mtds[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' '))
  for mtd in ${mtds[@]}; do
    # 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}"

        if [ ! -d ${mountdir} ]; then
          mkdir -p "${mountdir}"
          # U-Boot will create the ubiblock for the running version, but not
          # for the version on the other chip
          if [ ! -e "/dev/ubiblock${mtd}_${element}" ]; then
            ubiblock --create /dev/ubi${mtd}_${element}
          fi
          mount -t squashfs -o ro "/dev/ubiblock${mtd}_${element}" "${mountdir}"
        fi
      fi
    done
  done

  set_wdt2bite
}

# 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}"
}

# When the alternate bmc chip boots, u-boot thinks its the primary mtdX.
# Therefore need to swap the chip numbers when copying the ubiblock and root to
# alternate bmc u-boot environment.
copy_ubiblock_to_alt() {
  value="$(fw_printenv -n ubiblock)"
  bmcNum="$(findmtdnum "bmc")"
  altNum="$(findmtdnum "alt-bmc")"
  replaceAlt="${value/${altNum},/${bmcNum},}"

  if [[ "${value}" == "${replaceAlt}" ]]; then
    replaceBmc="${value/${bmcNum},/${altNum},}"
    value=${replaceBmc}
  else
    value=${replaceAlt}
  fi

  fw_setenv -c /etc/alt_fw_env.config ubiblock "${value}"
}

copy_root_to_alt() {
  value="$(fw_printenv -n root)"
  bmcNum="$(findmtdnum "bmc")"
  altNum="$(findmtdnum "alt-bmc")"
  replaceAlt="${value/${altNum}_/${bmcNum}_}"

  if [[ "${value}" == "${replaceAlt}" ]]; then
    replaceBmc="${value/${bmcNum}_/${altNum}_}"
    value=${replaceBmc}
  else
    value=${replaceAlt}
  fi

  fw_setenv -c /etc/alt_fw_env.config root "${value}"
}

ubi_setenv() {
    # The U-Boot environment maintains two banks of environment variables.
    # The banks need to be consistent with each other to ensure that these
    # variables can reliably be read from file. In order to guarantee that the
    # banks are both correct, we need to run fw_setenv twice.
    variable=$1
    if [[ "$variable" == *"="* ]]; then
        varName="${variable%=*}"
        value="${variable##*=}"
        # Write only if var is not set already to the requested value
        currentValue="$(fw_printenv -n "${varName}" 2>/dev/null)" || true
        if [[ "${currenValue}" != "${value}" ]]; then
          fw_setenv "$varName" "$value"
          fw_setenv "$varName" "$value"
        fi
    else
        fw_setenv "$variable"
        fw_setenv "$variable"
    fi
}

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

backup_env_vars() {
  copy_env_var_to_alt kernelname
  copy_ubiblock_to_alt
  copy_root_to_alt
}

update_env_vars() {
  vol="$(findubi rofs-"${version}")"
  if [ -z "${vol}" ]; then
    return 1
  fi
  ubidevid="${vol#ubi}"
  block="/dev/ubiblock${ubidevid}"
  if [ ! -e "${block}" ]; then
    return 1
  fi
  ubi_setenv "kernelname=kernel-${version}"
  ubi_setenv "ubiblock=$(echo "${ubidevid}" | sed 's/_/,/')"
  ubi_setenv "root=${block}"
}

#TODO: Replace the implementation with systemd-inhibitors lock
#      once systemd/systemd#949 is resolved
rebootguardenable() {
  dir="/run/systemd/system/"
  file="reboot-guard.conf"
  units=("reboot" "poweroff" "halt")

  for unit in "${units[@]}"; do
    mkdir -p ${dir}${unit}.target.d
    echo -e "[Unit]\nRefuseManualStart=yes" >> ${dir}${unit}.target.d/${file}
  done
}

#TODO: Replace the implementation with systemd-inhibitors lock
#      once systemd/systemd#949 is resolved
rebootguarddisable() {
  dir="/run/systemd/system/"
  file="reboot-guard.conf"
  units=("reboot" "poweroff" "halt")

  for unit in "${units[@]}"; do
    rm -rf ${dir}${unit}.target.d/${file}
  done
}

# Create a copy in the alt mtd
create_vol_in_alt() {
  alt="alt-bmc"
  altmtd="$(findmtd "${alt}")"
  if [ ! -z "${altmtd}" ]; then
    reqmtd="${alt}"
    reqmtd2="${alt}"
    ubi_ro
    ubi_updatevol
  fi
}

# Copy contents of one MTD device to another
mtd_copy() {
  in=$1
  out=$2

  # Must erase MTD first to prevent corruption
  flash_eraseall "${out}"
  dd if="${in}" of="${out}"
}

mirroruboot() {
  bmc="$(findmtd "u-boot")"
  bmcdev="/dev/${bmc}"
  alt="$(findmtd "alt-u-boot")"
  altdev="/dev/${alt}"

  checksum_bmc="$(md5sum "${bmcdev}")"
  checksum_bmc="${checksum_bmc% *}"
  checksum_alt="$(md5sum "${altdev}")"
  checksum_alt="${checksum_alt% *}"

  if [[ "${checksum_bmc}" != "${checksum_alt}" ]]; then
    bmcenv="$(findmtd "u-boot-env")"
    bmcenvdev="/dev/${bmcenv}"
    altenv="$(findmtd "alt-u-boot-env")"
    altenvdev="/dev/${altenv}"

    echo "Mirroring U-boot to alt chip"
    mtd_copy "${bmcdev}" "${altdev}"
    mtd_copy "${bmcenvdev}" "${altenvdev}"

    copy_ubiblock_to_alt
    copy_root_to_alt
  fi
}

case "$1" in
  mtduboot)
    reqmtd="$2"
    version="$3"
    imgfile="image-u-boot"
    mtd_write
    ;;
  ubirw)
    reqmtd="$2"
    name="$3"
    imgsize="$4"
    ubi_rw
    ;;
  ubiro)
    reqmtd="$(echo "$2" | cut -d "+" -f 1)"
    reqmtd2="$(echo "$2" | cut -d "+" -f 2)"
    name="$3"
    version="$4"
    imgfile="image-rofs"
    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"
    ubi_ro
    ubi_updatevol
    create_vol_in_alt
    ;;
  ubiremove)
    name="$2"
    ubi_remove "${name}"
    ;;
  ubicleanup)
    ubi_cleanup
    ;;
  ubisetenv)
    ubi_setenv "$2"
    ;;
  ubiremount)
    remount_ubi
    mount_alt_rwfs
    ;;
  createenvbackup)
    backup_env_vars
    ;;
  updateubootvars)
    version="$2"
    update_env_vars
    ;;
  rebootguardenable)
    rebootguardenable
    ;;
  rebootguarddisable)
    rebootguarddisable
    ;;
  mirroruboot)
    mirroruboot
    ;;
  *)
    echo "Invalid argument"
    exit 1
    ;;
esac
OpenPOWER on IntegriCloud