blob: 7d2a1a9700ceac9a75c4b518715b20c06c0e7c3a [file] [log] [blame]
Adriana Kobylake8606da2020-07-15 09:16:56 -05001#!/bin/sh
2
Isaac Kurth514a0942021-10-19 21:17:44 +00003bios_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 Kobylake8606da2020-07-15 09:16:56 -050019mmc_init() {
20 base_dir="/media/hostfw"
Adriana Kobylak98f22322020-07-20 10:39:41 -050021 ro_dir="${base_dir}/running-ro"
Adriana Kobylake8606da2020-07-15 09:16:56 -050022 running_dir="${base_dir}/running"
23 prsv_dir="${base_dir}/prsv"
Adriana Kobylak87372ae2020-09-01 11:30:59 -050024 staging_dir="${base_dir}/staging"
Adriana Kobylake8606da2020-07-15 09:16:56 -050025
Adriana Kobylak98f22322020-07-20 10:39:41 -050026 if [ ! -d "${ro_dir}" ]; then
27 mkdir -p "${ro_dir}"
28 fi
Adriana Kobylake8606da2020-07-15 09:16:56 -050029 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 Kobylak87372ae2020-09-01 11:30:59 -050035 if [ ! -d "${staging_dir}" ]; then
36 mkdir -p "${staging_dir}"
37 fi
Adriana Kobylake8606da2020-07-15 09:16:56 -050038
Adriana Kobylak98f22322020-07-20 10:39:41 -050039 # Mount the image that corresponds to the boot label as read-only to be used
40 # to populate the running directory.
Adriana Kobylake8606da2020-07-15 09:16:56 -050041 boot_label="$(fw_printenv -n bootside)"
Isaac Kurth0ddd4fa2021-07-14 17:35:37 -050042 if ! grep -q "${ro_dir}" /proc/mounts; then
43 mount ${base_dir}/hostfw-"${boot_label}" ${ro_dir} -o ro
44 fi
Adriana Kobylak98f22322020-07-20 10:39:41 -050045
46 # Determine if the running dir contains the running version
Adriana Kobylake8606da2020-07-15 09:16:56 -050047 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 Kobylak4e82bc82021-09-28 19:25:39 +000054 # A line in the pnor.toc (81e00994.lid) looks like this:
Adriana Kobylake8606da2020-07-15 09:16:56 -050055 # partition05=SECBOOT,0x00381000,0x003a5000,00,ECC,PRESERVED
56 rm -f ${prsv_dir}/*
Adriana Kobylak4e82bc82021-09-28 19:25:39 +000057 if [ -f "${ro_dir}/81e00994.lid" ]; then
58 prsvs=$(grep PRESERVED "${ro_dir}/81e00994.lid")
Adriana Kobylake8606da2020-07-15 09:16:56 -050059 for prsv in ${prsvs}; do
60 prsv=${prsv##partition*=}
Adriana Kobylakf6d4d762021-04-21 10:46:14 -050061 prsv=$(echo "${prsv}" | cut -d "," -f 1)
Adriana Kobylakff675db2021-09-07 18:00:57 +000062 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 Kobylake8606da2020-07-15 09:16:56 -050067 done
68 fi
69
Adriana Kobylakff675db2021-09-07 18:00:57 +000070 # Copy lid contents of running image to running dir
Adriana Kobylake8606da2020-07-15 09:16:56 -050071 rm -f ${running_dir}/*
Adriana Kobylakff675db2021-09-07 18:00:57 +000072 cp -p ${ro_dir}/*.lid ${running_dir}/
Adriana Kobylake8606da2020-07-15 09:16:56 -050073
Adriana Kobylak98f22322020-07-20 10:39:41 -050074 # Restore the preserved partitions. Ignore error, there may be none.
75 cp -p ${prsv_dir}/* ${running_dir}/ 2>/dev/null || true
Adriana Kobylakf6d4d762021-04-21 10:46:14 -050076 rm -f "${prsv_dir:?}/"*
Adriana Kobylake8606da2020-07-15 09:16:56 -050077
Adriana Kobylak87372ae2020-09-01 11:30:59 -050078 # Clean up the staging dir in case of a failed update
Adriana Kobylakf6d4d762021-04-21 10:46:14 -050079 rm -rf "${staging_dir:?}/"*
Adriana Kobylak87372ae2020-09-01 11:30:59 -050080
Adriana Kobylake8606da2020-07-15 09:16:56 -050081 # Save the label
82 echo "${boot_label}" > "${running_label_file}"
83
Adriana Kobylake8606da2020-07-15 09:16:56 -050084 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 Kurth0ddd4fa2021-07-14 17:35:37 -050096 if ! grep -q "${alternate_dir}" /proc/mounts; then
97 mount ${base_dir}/hostfw-${alternate_label} ${alternate_dir} -o ro
98 fi
Adriana Kobylake8606da2020-07-15 09:16:56 -050099}
100
101mmc_patch() {
102 # Patching is disabled if field mode is set
Adriana Kobylakf6d4d762021-04-21 10:46:14 -0500103 if [ "$(fw_printenv fieldmode 2>/dev/null)" = "fieldmode=true" ]; then
Adriana Kobylake8606da2020-07-15 09:16:56 -0500104 return 0
105 fi
106
Adriana Kobylakf6608a82020-08-25 14:16:23 -0500107 boot_label="$(fw_printenv -n bootside)"
108 if [ "${boot_label}" = "a" ]; then
109 alternate_label="b"
110 else
111 alternate_label="a"
Adriana Kobylake8606da2020-07-15 09:16:56 -0500112 fi
113
Adriana Kobylakf6608a82020-08-25 14:16:23 -0500114 # 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 Kobylake8606da2020-07-15 09:16:56 -0500123 fi
124
Adriana Kobylakf6608a82020-08-25 14:16:23 -0500125 # 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 Kurth0ddd4fa2021-07-14 17:35:37 -0500135 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 Kobylake8606da2020-07-15 09:16:56 -0500144}
145
146case "$1" in
Isaac Kurth514a0942021-10-19 21:17:44 +0000147 bios-reset-if-needed)
148 bios_reset_if_needed
149 ;;
Adriana Kobylake8606da2020-07-15 09:16:56 -0500150 mmc-init)
151 mmc_init
152 ;;
153 mmc-patch)
154 mmc_patch
155 ;;
156 *)
Isaac Kurth514a0942021-10-19 21:17:44 +0000157 echo "Invalid argument: $1" >&2
Adriana Kobylake8606da2020-07-15 09:16:56 -0500158 exit 1
159 ;;
160esac