xref: /openbmc/openpower-pnor-code-mgmt/mmc/obmc-flash-bios (revision 1e01078ee7d37aa01db48687433704e73c34b68c)
1#!/bin/sh
2
3bios_reset_if_needed() {
4  resetval=$(fw_printenv -n rwreset 2>/dev/nell)
5  if [ "$resetval" = "0" ] || [ "$resetval" = "1" ]; then
6    varstore="/media/hostfw/reset-gpio-val"
7    if [ -f "$varstore" ]; then
8      if [ "$resetval" != "$(cat $varstore)" ]; then
9        if busctl call org.open_power.Software.Host.Updater /xyz/openbmc_project/software xyz.openbmc_project.Common.FactoryReset Reset; then
10          echo "$resetval" > "$varstore"
11        fi
12      fi
13    else
14      echo "$resetval" > "$varstore"
15    fi
16  fi
17}
18
19mmc_init() {
20  base_dir="/media/hostfw"
21  ro_dir="${base_dir}/running-ro"
22  running_dir="${base_dir}/running"
23  prsv_dir="${base_dir}/prsv"
24  staging_dir="${base_dir}/staging"
25  nvram_dir="${base_dir}/nvram"
26
27  if [ ! -d "${ro_dir}" ]; then
28    mkdir -p "${ro_dir}"
29  fi
30  if [ ! -d "${running_dir}" ]; then
31    mkdir -p ${running_dir}
32  fi
33  if [ ! -d "${prsv_dir}" ]; then
34    mkdir -p "${prsv_dir}"
35  fi
36  if [ ! -d "${staging_dir}" ]; then
37    mkdir -p "${staging_dir}"
38  fi
39  if [ ! -d "${nvram_dir}" ]; then
40    mkdir -p "${nvram_dir}"
41  fi
42
43  # Mount the image that corresponds to the boot label as read-only to be used
44  # to populate the running directory.
45  boot_label="$(fw_printenv -n bootside)"
46  if ! grep -q "${ro_dir}" /proc/mounts; then
47    mount ${base_dir}/hostfw-"${boot_label}" ${ro_dir} -o ro
48  fi
49
50  # Determine if the running dir contains the running version
51  running_label=""
52  running_label_file="${running_dir}/partlabel"
53  if [ -f "${running_label_file}" ]; then
54    running_label=$(cat ${running_label_file})
55  fi
56  if [ "${running_label}" != "${boot_label}" ]; then
57    # Copy off the preserved partitions
58    # A line in the pnor.toc (81e00994.lid) looks like this:
59    # partition05=SECBOOT,0x00381000,0x003a5000,00,ECC,PRESERVED
60    rm -f ${prsv_dir}/*
61    if [ -f "${ro_dir}/81e00994.lid" ]; then
62      prsvs=$(grep PRESERVED "${ro_dir}/81e00994.lid")
63      for prsv in ${prsvs}; do
64        prsv=${prsv##partition*=}
65        prsv=$(echo "${prsv}" | cut -d "," -f 1)
66        if [ -L "${running_dir}/${prsv}" ]; then
67          # Preserve the symlink target file
68          prsv="$(readlink "${running_dir}/${prsv}")"
69          runsize="$(stat -c '%s' "${running_dir}/${prsv}")"
70          rosize="$(stat -c '%s' "${ro_dir}/${prsv}")"
71          if [ "$runsize" != "$rosize" ]; then
72              # Partition size may have changed or became corrupted
73              # So it will not be copied to the preserved directory
74              # Log PEL to indicate such
75              busctl call xyz.openbmc_project.Logging \
76              /xyz/openbmc_project/logging \
77              xyz.openbmc_project.Logging.Create Create "ssa{ss}" \
78              xyz.openbmc_project.Software.Version.Error.HostFile \
79              xyz.openbmc_project.Logging.Entry.Level.Error 3 "FILE_NAME" \
80              "${prsv}" "CURRENT_FILE_SIZE" "${runsize}" "EXPECTED_FILE_SIZE" \
81              "${rosize}"
82              # Initiate dump
83              busctl call xyz.openbmc_project.Dump.Manager \
84              /xyz/openbmc_project/dump/bmc xyz.openbmc_project.Dump.Create \
85              CreateDump "a{sv}" 0
86          else
87              cp -p ${running_dir}/"${prsv}" ${prsv_dir}
88          fi
89        fi
90      done
91    fi
92
93    # Copy lid contents of running image to running dir
94    rm -f ${running_dir}/*
95    cp -p ${ro_dir}/*.lid ${running_dir}/
96
97    # Restore the preserved partitions. Ignore error, there may be none.
98    cp -p ${prsv_dir}/* ${running_dir}/ 2>/dev/null || true
99    rm -f "${prsv_dir:?}/"*
100
101    # Clean up the staging dir in case of a failed update
102    rm -rf "${staging_dir:?}/"*
103
104    # Save the label
105    echo "${boot_label}" > "${running_label_file}"
106
107  fi
108
109  # Mount alternate dir
110  if [ "${boot_label}" = "a" ]; then
111    alternate_label="b"
112  else
113    alternate_label="a"
114  fi
115  alternate_dir="${base_dir}/alternate"
116  if [ ! -d "${alternate_dir}" ]; then
117    mkdir -p ${alternate_dir}
118  fi
119  if ! grep -q "${alternate_dir}" /proc/mounts; then
120    mount ${base_dir}/hostfw-${alternate_label} ${alternate_dir} -o ro
121  fi
122}
123
124mmc_patch() {
125  # Patching is disabled if field mode is set
126  if [ "$(fw_printenv fieldmode 2>/dev/null)" = "fieldmode=true" ]; then
127    return 0
128  fi
129
130  boot_label="$(fw_printenv -n bootside)"
131  if [ "${boot_label}" = "a" ]; then
132    alternate_label="b"
133  else
134    alternate_label="a"
135  fi
136
137  # Create patch directories
138  patch_dir="/media/hostfw/patch-"
139  running_patch_dir="${patch_dir}${boot_label}"
140  if [ ! -d "${running_patch_dir}" ]; then
141    mkdir -p "${running_patch_dir}"
142  fi
143  alternate_patch_dir="${patch_dir}${alternate_label}"
144  if [ ! -d "${alternate_patch_dir}" ]; then
145    mkdir -p "${alternate_patch_dir}"
146  fi
147
148  # Create patch symlinks
149  symlink_base="/usr/local/share"
150  if [ ! -d "${symlink_base}" ]; then
151    mkdir -p "${symlink_base}"
152  fi
153  hostfw_symlink_base="${symlink_base}/hostfw"
154  if [ ! -d "${hostfw_symlink_base}" ]; then
155    mkdir -p "${hostfw_symlink_base}"
156  fi
157
158  if [ "$(readlink -f "${symlink_base}/pnor")" != "${running_patch_dir}" ]; then
159    ln -s "${running_patch_dir}" "${symlink_base}/pnor"
160  fi
161  if [ "$(readlink -f "${hostfw_symlink_base}/running")" != "${running_patch_dir}" ]; then
162    ln -s "${running_patch_dir}" "${hostfw_symlink_base}/running"
163  fi
164  if [ "$(readlink -f "${hostfw_symlink_base}/alternate")" != "${alternate_patch_dir}" ]; then
165    ln -s "${alternate_patch_dir}" "${hostfw_symlink_base}/alternate"
166  fi
167}
168
169case "$1" in
170  bios-reset-if-needed)
171    bios_reset_if_needed
172    ;;
173  mmc-init)
174    mmc_init
175    ;;
176  mmc-patch)
177    mmc_patch
178    ;;
179  *)
180    echo "Invalid argument: $1" >&2
181    exit 1
182    ;;
183esac
184