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