blob: 61ac8179321121628fce8075de056346c626e869 [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)"
Adriana Kobylakf6d4d762021-04-21 10:46:14 -050026 mount ${base_dir}/hostfw-"${boot_label}" ${ro_dir} -o ro
Adriana Kobylak98f22322020-07-20 10:39:41 -050027
28 # Determine if the running dir contains the running version
Adriana Kobylake8606da2020-07-15 09:16:56 -050029 running_label=""
30 running_label_file="${running_dir}/partlabel"
31 if [ -f "${running_label_file}" ]; then
32 running_label=$(cat ${running_label_file})
33 fi
34 if [ "${running_label}" != "${boot_label}" ]; then
35 # Copy off the preserved partitions
36 # A line in the pnor.toc looks like this:
37 # partition05=SECBOOT,0x00381000,0x003a5000,00,ECC,PRESERVED
38 rm -f ${prsv_dir}/*
39 if [ -f ${running_dir}/pnor.toc ]; then
40 prsvs=$(grep PRESERVED ${running_dir}/pnor.toc)
41 for prsv in ${prsvs}; do
42 prsv=${prsv##partition*=}
Adriana Kobylakf6d4d762021-04-21 10:46:14 -050043 prsv=$(echo "${prsv}" | cut -d "," -f 1)
44 cp -p ${running_dir}/"${prsv}" ${prsv_dir}
Adriana Kobylake8606da2020-07-15 09:16:56 -050045 done
46 fi
47
48 # Copy contents of running image to running dir
Adriana Kobylake8606da2020-07-15 09:16:56 -050049 rm -f ${running_dir}/*
Adriana Kobylak98f22322020-07-20 10:39:41 -050050 cp -p ${ro_dir}/* ${running_dir}/
Adriana Kobylake8606da2020-07-15 09:16:56 -050051
Adriana Kobylak98f22322020-07-20 10:39:41 -050052 # Restore the preserved partitions. Ignore error, there may be none.
53 cp -p ${prsv_dir}/* ${running_dir}/ 2>/dev/null || true
Adriana Kobylakf6d4d762021-04-21 10:46:14 -050054 rm -f "${prsv_dir:?}/"*
Adriana Kobylake8606da2020-07-15 09:16:56 -050055
Adriana Kobylak87372ae2020-09-01 11:30:59 -050056 # Clean up the staging dir in case of a failed update
Adriana Kobylakf6d4d762021-04-21 10:46:14 -050057 rm -rf "${staging_dir:?}/"*
Adriana Kobylak87372ae2020-09-01 11:30:59 -050058
Adriana Kobylake8606da2020-07-15 09:16:56 -050059 # Save the label
60 echo "${boot_label}" > "${running_label_file}"
61
Adriana Kobylake8606da2020-07-15 09:16:56 -050062 fi
63
64 # Mount alternate dir
65 if [ "${boot_label}" = "a" ]; then
66 alternate_label="b"
67 else
68 alternate_label="a"
69 fi
70 alternate_dir="${base_dir}/alternate"
71 if [ ! -d "${alternate_dir}" ]; then
72 mkdir -p ${alternate_dir}
73 fi
74 mount ${base_dir}/hostfw-${alternate_label} ${alternate_dir} -o ro
75}
76
77mmc_patch() {
78 # Patching is disabled if field mode is set
Adriana Kobylakf6d4d762021-04-21 10:46:14 -050079 if [ "$(fw_printenv fieldmode 2>/dev/null)" = "fieldmode=true" ]; then
Adriana Kobylake8606da2020-07-15 09:16:56 -050080 return 0
81 fi
82
Adriana Kobylakf6608a82020-08-25 14:16:23 -050083 boot_label="$(fw_printenv -n bootside)"
84 if [ "${boot_label}" = "a" ]; then
85 alternate_label="b"
86 else
87 alternate_label="a"
Adriana Kobylake8606da2020-07-15 09:16:56 -050088 fi
89
Adriana Kobylakf6608a82020-08-25 14:16:23 -050090 # Create patch directories
91 patch_dir="/media/hostfw/patch-"
92 running_patch_dir="${patch_dir}${boot_label}"
93 if [ ! -d "${running_patch_dir}" ]; then
94 mkdir -p "${running_patch_dir}"
95 fi
96 alternate_patch_dir="${patch_dir}${alternate_label}"
97 if [ ! -d "${alternate_patch_dir}" ]; then
98 mkdir -p "${alternate_patch_dir}"
Adriana Kobylake8606da2020-07-15 09:16:56 -050099 fi
100
Adriana Kobylakf6608a82020-08-25 14:16:23 -0500101 # Create patch symlinks
102 symlink_base="/usr/local/share"
103 if [ ! -d "${symlink_base}" ]; then
104 mkdir -p "${symlink_base}"
105 fi
106 hostfw_symlink_base="${symlink_base}/hostfw"
107 if [ ! -d "${hostfw_symlink_base}" ]; then
108 mkdir -p "${hostfw_symlink_base}"
109 fi
110
111 ln -s "${running_patch_dir}" "${symlink_base}/pnor"
112 ln -s "${running_patch_dir}" "${hostfw_symlink_base}/running"
113 ln -s "${alternate_patch_dir}" "${hostfw_symlink_base}/alternate"
Adriana Kobylake8606da2020-07-15 09:16:56 -0500114}
115
116case "$1" in
117 mmc-init)
118 mmc_init
119 ;;
120 mmc-patch)
121 mmc_patch
122 ;;
123 *)
124 echo "Invalid argument"
125 exit 1
126 ;;
127esac