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