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 | e8606da | 2020-07-15 09:16:56 -0500 | [diff] [blame] | 25 | |
Adriana Kobylak | 98f2232 | 2020-07-20 10:39:41 -0500 | [diff] [blame] | 26 | if [ ! -d "${ro_dir}" ]; then |
| 27 | mkdir -p "${ro_dir}" |
| 28 | fi |
Adriana Kobylak | e8606da | 2020-07-15 09:16:56 -0500 | [diff] [blame] | 29 | if [ ! -d "${running_dir}" ]; then |
| 30 | mkdir -p ${running_dir} |
| 31 | fi |
| 32 | if [ ! -d "${prsv_dir}" ]; then |
| 33 | mkdir -p "${prsv_dir}" |
| 34 | fi |
Adriana Kobylak | 87372ae | 2020-09-01 11:30:59 -0500 | [diff] [blame] | 35 | if [ ! -d "${staging_dir}" ]; then |
| 36 | mkdir -p "${staging_dir}" |
| 37 | fi |
Adriana Kobylak | e8606da | 2020-07-15 09:16:56 -0500 | [diff] [blame] | 38 | |
Adriana Kobylak | 98f2232 | 2020-07-20 10:39:41 -0500 | [diff] [blame] | 39 | # Mount the image that corresponds to the boot label as read-only to be used |
| 40 | # to populate the running directory. |
Adriana Kobylak | e8606da | 2020-07-15 09:16:56 -0500 | [diff] [blame] | 41 | boot_label="$(fw_printenv -n bootside)" |
Isaac Kurth | 0ddd4fa | 2021-07-14 17:35:37 -0500 | [diff] [blame] | 42 | if ! grep -q "${ro_dir}" /proc/mounts; then |
| 43 | mount ${base_dir}/hostfw-"${boot_label}" ${ro_dir} -o ro |
| 44 | fi |
Adriana Kobylak | 98f2232 | 2020-07-20 10:39:41 -0500 | [diff] [blame] | 45 | |
| 46 | # Determine if the running dir contains the running version |
Adriana Kobylak | e8606da | 2020-07-15 09:16:56 -0500 | [diff] [blame] | 47 | running_label="" |
| 48 | running_label_file="${running_dir}/partlabel" |
| 49 | if [ -f "${running_label_file}" ]; then |
| 50 | running_label=$(cat ${running_label_file}) |
| 51 | fi |
| 52 | if [ "${running_label}" != "${boot_label}" ]; then |
| 53 | # Copy off the preserved partitions |
Adriana Kobylak | 4e82bc8 | 2021-09-28 19:25:39 +0000 | [diff] [blame] | 54 | # A line in the pnor.toc (81e00994.lid) looks like this: |
Adriana Kobylak | e8606da | 2020-07-15 09:16:56 -0500 | [diff] [blame] | 55 | # partition05=SECBOOT,0x00381000,0x003a5000,00,ECC,PRESERVED |
| 56 | rm -f ${prsv_dir}/* |
Adriana Kobylak | 4e82bc8 | 2021-09-28 19:25:39 +0000 | [diff] [blame] | 57 | if [ -f "${ro_dir}/81e00994.lid" ]; then |
| 58 | prsvs=$(grep PRESERVED "${ro_dir}/81e00994.lid") |
Adriana Kobylak | e8606da | 2020-07-15 09:16:56 -0500 | [diff] [blame] | 59 | for prsv in ${prsvs}; do |
| 60 | prsv=${prsv##partition*=} |
Adriana Kobylak | f6d4d76 | 2021-04-21 10:46:14 -0500 | [diff] [blame] | 61 | prsv=$(echo "${prsv}" | cut -d "," -f 1) |
Adriana Kobylak | ff675db | 2021-09-07 18:00:57 +0000 | [diff] [blame] | 62 | if [ -L "${running_dir}/${prsv}" ]; then |
| 63 | # Preserve the symlink target file |
| 64 | prsv="$(readlink "${running_dir}/${prsv}")" |
| 65 | cp -p ${running_dir}/"${prsv}" ${prsv_dir} |
| 66 | fi |
Adriana Kobylak | e8606da | 2020-07-15 09:16:56 -0500 | [diff] [blame] | 67 | done |
| 68 | fi |
| 69 | |
Adriana Kobylak | ff675db | 2021-09-07 18:00:57 +0000 | [diff] [blame] | 70 | # Copy lid contents of running image to running dir |
Adriana Kobylak | e8606da | 2020-07-15 09:16:56 -0500 | [diff] [blame] | 71 | rm -f ${running_dir}/* |
Adriana Kobylak | ff675db | 2021-09-07 18:00:57 +0000 | [diff] [blame] | 72 | cp -p ${ro_dir}/*.lid ${running_dir}/ |
Adriana Kobylak | e8606da | 2020-07-15 09:16:56 -0500 | [diff] [blame] | 73 | |
Adriana Kobylak | 98f2232 | 2020-07-20 10:39:41 -0500 | [diff] [blame] | 74 | # Restore the preserved partitions. Ignore error, there may be none. |
| 75 | cp -p ${prsv_dir}/* ${running_dir}/ 2>/dev/null || true |
Adriana Kobylak | f6d4d76 | 2021-04-21 10:46:14 -0500 | [diff] [blame] | 76 | rm -f "${prsv_dir:?}/"* |
Adriana Kobylak | e8606da | 2020-07-15 09:16:56 -0500 | [diff] [blame] | 77 | |
Adriana Kobylak | 87372ae | 2020-09-01 11:30:59 -0500 | [diff] [blame] | 78 | # Clean up the staging dir in case of a failed update |
Adriana Kobylak | f6d4d76 | 2021-04-21 10:46:14 -0500 | [diff] [blame] | 79 | rm -rf "${staging_dir:?}/"* |
Adriana Kobylak | 87372ae | 2020-09-01 11:30:59 -0500 | [diff] [blame] | 80 | |
Adriana Kobylak | e8606da | 2020-07-15 09:16:56 -0500 | [diff] [blame] | 81 | # Save the label |
| 82 | echo "${boot_label}" > "${running_label_file}" |
| 83 | |
Adriana Kobylak | e8606da | 2020-07-15 09:16:56 -0500 | [diff] [blame] | 84 | fi |
| 85 | |
| 86 | # Mount alternate dir |
| 87 | if [ "${boot_label}" = "a" ]; then |
| 88 | alternate_label="b" |
| 89 | else |
| 90 | alternate_label="a" |
| 91 | fi |
| 92 | alternate_dir="${base_dir}/alternate" |
| 93 | if [ ! -d "${alternate_dir}" ]; then |
| 94 | mkdir -p ${alternate_dir} |
| 95 | fi |
Isaac Kurth | 0ddd4fa | 2021-07-14 17:35:37 -0500 | [diff] [blame] | 96 | if ! grep -q "${alternate_dir}" /proc/mounts; then |
| 97 | mount ${base_dir}/hostfw-${alternate_label} ${alternate_dir} -o ro |
| 98 | fi |
Adriana Kobylak | e8606da | 2020-07-15 09:16:56 -0500 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | mmc_patch() { |
| 102 | # Patching is disabled if field mode is set |
Adriana Kobylak | f6d4d76 | 2021-04-21 10:46:14 -0500 | [diff] [blame] | 103 | if [ "$(fw_printenv fieldmode 2>/dev/null)" = "fieldmode=true" ]; then |
Adriana Kobylak | e8606da | 2020-07-15 09:16:56 -0500 | [diff] [blame] | 104 | return 0 |
| 105 | fi |
| 106 | |
Adriana Kobylak | f6608a8 | 2020-08-25 14:16:23 -0500 | [diff] [blame] | 107 | boot_label="$(fw_printenv -n bootside)" |
| 108 | if [ "${boot_label}" = "a" ]; then |
| 109 | alternate_label="b" |
| 110 | else |
| 111 | alternate_label="a" |
Adriana Kobylak | e8606da | 2020-07-15 09:16:56 -0500 | [diff] [blame] | 112 | fi |
| 113 | |
Adriana Kobylak | f6608a8 | 2020-08-25 14:16:23 -0500 | [diff] [blame] | 114 | # Create patch directories |
| 115 | patch_dir="/media/hostfw/patch-" |
| 116 | running_patch_dir="${patch_dir}${boot_label}" |
| 117 | if [ ! -d "${running_patch_dir}" ]; then |
| 118 | mkdir -p "${running_patch_dir}" |
| 119 | fi |
| 120 | alternate_patch_dir="${patch_dir}${alternate_label}" |
| 121 | if [ ! -d "${alternate_patch_dir}" ]; then |
| 122 | mkdir -p "${alternate_patch_dir}" |
Adriana Kobylak | e8606da | 2020-07-15 09:16:56 -0500 | [diff] [blame] | 123 | fi |
| 124 | |
Adriana Kobylak | f6608a8 | 2020-08-25 14:16:23 -0500 | [diff] [blame] | 125 | # Create patch symlinks |
| 126 | symlink_base="/usr/local/share" |
| 127 | if [ ! -d "${symlink_base}" ]; then |
| 128 | mkdir -p "${symlink_base}" |
| 129 | fi |
| 130 | hostfw_symlink_base="${symlink_base}/hostfw" |
| 131 | if [ ! -d "${hostfw_symlink_base}" ]; then |
| 132 | mkdir -p "${hostfw_symlink_base}" |
| 133 | fi |
| 134 | |
Isaac Kurth | 0ddd4fa | 2021-07-14 17:35:37 -0500 | [diff] [blame] | 135 | if [ "$(readlink -f "${symlink_base}/pnor")" != "${running_patch_dir}" ]; then |
| 136 | ln -s "${running_patch_dir}" "${symlink_base}/pnor" |
| 137 | fi |
| 138 | if [ "$(readlink -f "${hostfw_symlink_base}/running")" != "${running_patch_dir}" ]; then |
| 139 | ln -s "${running_patch_dir}" "${hostfw_symlink_base}/running" |
| 140 | fi |
| 141 | if [ "$(readlink -f "${hostfw_symlink_base}/alternate")" != "${alternate_patch_dir}" ]; then |
| 142 | ln -s "${alternate_patch_dir}" "${hostfw_symlink_base}/alternate" |
| 143 | fi |
Adriana Kobylak | e8606da | 2020-07-15 09:16:56 -0500 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | case "$1" in |
Isaac Kurth | 514a094 | 2021-10-19 21:17:44 +0000 | [diff] [blame^] | 147 | bios-reset-if-needed) |
| 148 | bios_reset_if_needed |
| 149 | ;; |
Adriana Kobylak | e8606da | 2020-07-15 09:16:56 -0500 | [diff] [blame] | 150 | mmc-init) |
| 151 | mmc_init |
| 152 | ;; |
| 153 | mmc-patch) |
| 154 | mmc_patch |
| 155 | ;; |
| 156 | *) |
Isaac Kurth | 514a094 | 2021-10-19 21:17:44 +0000 | [diff] [blame^] | 157 | echo "Invalid argument: $1" >&2 |
Adriana Kobylak | e8606da | 2020-07-15 09:16:56 -0500 | [diff] [blame] | 158 | exit 1 |
| 159 | ;; |
| 160 | esac |