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          cp -p ${running_dir}/"${prsv}" ${prsv_dir}
70        fi
71      done
72    fi
73
74    # Copy lid contents of running image to running dir
75    rm -f ${running_dir}/*
76    cp -p ${ro_dir}/*.lid ${running_dir}/
77
78    # Restore the preserved partitions. Ignore error, there may be none.
79    cp -p ${prsv_dir}/* ${running_dir}/ 2>/dev/null || true
80    rm -f "${prsv_dir:?}/"*
81
82    # Clean up the staging dir in case of a failed update
83    rm -rf "${staging_dir:?}/"*
84
85    # Save the label
86    echo "${boot_label}" > "${running_label_file}"
87
88  fi
89
90  # Mount alternate dir
91  if [ "${boot_label}" = "a" ]; then
92    alternate_label="b"
93  else
94    alternate_label="a"
95  fi
96  alternate_dir="${base_dir}/alternate"
97  if [ ! -d "${alternate_dir}" ]; then
98    mkdir -p ${alternate_dir}
99  fi
100  if ! grep -q "${alternate_dir}" /proc/mounts; then
101    mount ${base_dir}/hostfw-${alternate_label} ${alternate_dir} -o ro
102  fi
103}
104
105mmc_patch() {
106  # Patching is disabled if field mode is set
107  if [ "$(fw_printenv fieldmode 2>/dev/null)" = "fieldmode=true" ]; then
108    return 0
109  fi
110
111  boot_label="$(fw_printenv -n bootside)"
112  if [ "${boot_label}" = "a" ]; then
113    alternate_label="b"
114  else
115    alternate_label="a"
116  fi
117
118  # Create patch directories
119  patch_dir="/media/hostfw/patch-"
120  running_patch_dir="${patch_dir}${boot_label}"
121  if [ ! -d "${running_patch_dir}" ]; then
122    mkdir -p "${running_patch_dir}"
123  fi
124  alternate_patch_dir="${patch_dir}${alternate_label}"
125  if [ ! -d "${alternate_patch_dir}" ]; then
126    mkdir -p "${alternate_patch_dir}"
127  fi
128
129  # Create patch symlinks
130  symlink_base="/usr/local/share"
131  if [ ! -d "${symlink_base}" ]; then
132    mkdir -p "${symlink_base}"
133  fi
134  hostfw_symlink_base="${symlink_base}/hostfw"
135  if [ ! -d "${hostfw_symlink_base}" ]; then
136    mkdir -p "${hostfw_symlink_base}"
137  fi
138
139  if [ "$(readlink -f "${symlink_base}/pnor")" != "${running_patch_dir}" ]; then
140    ln -s "${running_patch_dir}" "${symlink_base}/pnor"
141  fi
142  if [ "$(readlink -f "${hostfw_symlink_base}/running")" != "${running_patch_dir}" ]; then
143    ln -s "${running_patch_dir}" "${hostfw_symlink_base}/running"
144  fi
145  if [ "$(readlink -f "${hostfw_symlink_base}/alternate")" != "${alternate_patch_dir}" ]; then
146    ln -s "${alternate_patch_dir}" "${hostfw_symlink_base}/alternate"
147  fi
148}
149
150case "$1" in
151  bios-reset-if-needed)
152    bios_reset_if_needed
153    ;;
154  mmc-init)
155    mmc_init
156    ;;
157  mmc-patch)
158    mmc_patch
159    ;;
160  *)
161    echo "Invalid argument: $1" >&2
162    exit 1
163    ;;
164esac
165