blob: 8bd8ca7538646ef3e9b2701bc7640853f5d9b13e [file] [log] [blame]
Adriana Kobylake8606da2020-07-15 09:16:56 -05001#!/bin/sh
2
3mmc_init() {
4 base_dir="/media/hostfw"
Adriana Kobylak98f22322020-07-20 10:39:41 -05005 ro_dir="${base_dir}/running-ro"
Adriana Kobylake8606da2020-07-15 09:16:56 -05006 running_dir="${base_dir}/running"
7 prsv_dir="${base_dir}/prsv"
Adriana Kobylak87372ae2020-09-01 11:30:59 -05008 staging_dir="${base_dir}/staging"
Adriana Kobylake8606da2020-07-15 09:16:56 -05009
Adriana Kobylak98f22322020-07-20 10:39:41 -050010 if [ ! -d "${ro_dir}" ]; then
11 mkdir -p "${ro_dir}"
12 fi
Adriana Kobylake8606da2020-07-15 09:16:56 -050013 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 Kobylak87372ae2020-09-01 11:30:59 -050019 if [ ! -d "${staging_dir}" ]; then
20 mkdir -p "${staging_dir}"
21 fi
Adriana Kobylake8606da2020-07-15 09:16:56 -050022
Adriana Kobylak98f22322020-07-20 10:39:41 -050023 # Mount the image that corresponds to the boot label as read-only to be used
24 # to populate the running directory.
Adriana Kobylake8606da2020-07-15 09:16:56 -050025 boot_label="$(fw_printenv -n bootside)"
Isaac Kurth0ddd4fa2021-07-14 17:35:37 -050026 if ! grep -q "${ro_dir}" /proc/mounts; then
27 mount ${base_dir}/hostfw-"${boot_label}" ${ro_dir} -o ro
28 fi
Adriana Kobylak98f22322020-07-20 10:39:41 -050029
30 # Determine if the running dir contains the running version
Adriana Kobylake8606da2020-07-15 09:16:56 -050031 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 Kobylakf6d4d762021-04-21 10:46:14 -050045 prsv=$(echo "${prsv}" | cut -d "," -f 1)
46 cp -p ${running_dir}/"${prsv}" ${prsv_dir}
Adriana Kobylake8606da2020-07-15 09:16:56 -050047 done
48 fi
49
50 # Copy contents of running image to running dir
Adriana Kobylake8606da2020-07-15 09:16:56 -050051 rm -f ${running_dir}/*
Adriana Kobylak98f22322020-07-20 10:39:41 -050052 cp -p ${ro_dir}/* ${running_dir}/
Adriana Kobylake8606da2020-07-15 09:16:56 -050053
Adriana Kobylak98f22322020-07-20 10:39:41 -050054 # Restore the preserved partitions. Ignore error, there may be none.
55 cp -p ${prsv_dir}/* ${running_dir}/ 2>/dev/null || true
Adriana Kobylakf6d4d762021-04-21 10:46:14 -050056 rm -f "${prsv_dir:?}/"*
Adriana Kobylake8606da2020-07-15 09:16:56 -050057
Adriana Kobylak87372ae2020-09-01 11:30:59 -050058 # Clean up the staging dir in case of a failed update
Adriana Kobylakf6d4d762021-04-21 10:46:14 -050059 rm -rf "${staging_dir:?}/"*
Adriana Kobylak87372ae2020-09-01 11:30:59 -050060
Adriana Kobylake8606da2020-07-15 09:16:56 -050061 # Save the label
62 echo "${boot_label}" > "${running_label_file}"
63
Adriana Kobylake8606da2020-07-15 09:16:56 -050064 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 Kurth0ddd4fa2021-07-14 17:35:37 -050076 if ! grep -q "${alternate_dir}" /proc/mounts; then
77 mount ${base_dir}/hostfw-${alternate_label} ${alternate_dir} -o ro
78 fi
Adriana Kobylake8606da2020-07-15 09:16:56 -050079}
80
81mmc_patch() {
82 # Patching is disabled if field mode is set
Adriana Kobylakf6d4d762021-04-21 10:46:14 -050083 if [ "$(fw_printenv fieldmode 2>/dev/null)" = "fieldmode=true" ]; then
Adriana Kobylake8606da2020-07-15 09:16:56 -050084 return 0
85 fi
86
Adriana Kobylakf6608a82020-08-25 14:16:23 -050087 boot_label="$(fw_printenv -n bootside)"
88 if [ "${boot_label}" = "a" ]; then
89 alternate_label="b"
90 else
91 alternate_label="a"
Adriana Kobylake8606da2020-07-15 09:16:56 -050092 fi
93
Adriana Kobylakf6608a82020-08-25 14:16:23 -050094 # 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 Kobylake8606da2020-07-15 09:16:56 -0500103 fi
104
Adriana Kobylakf6608a82020-08-25 14:16:23 -0500105 # 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 Kurth0ddd4fa2021-07-14 17:35:37 -0500115 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 Kobylake8606da2020-07-15 09:16:56 -0500124}
125
126case "$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 ;;
137esac