blob: 60490b25b8ef0ab4e41bfdfe07e6aa01206c4fbc [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}/*
Adriana Kobylakff675db2021-09-07 18:00:57 +000041 if [ -f ${ro_dir}/pnor.toc ]; then
42 prsvs=$(grep PRESERVED ${ro_dir}/pnor.toc)
Adriana Kobylake8606da2020-07-15 09:16:56 -050043 for prsv in ${prsvs}; do
44 prsv=${prsv##partition*=}
Adriana Kobylakf6d4d762021-04-21 10:46:14 -050045 prsv=$(echo "${prsv}" | cut -d "," -f 1)
Adriana Kobylakff675db2021-09-07 18:00:57 +000046 if [ -L "${running_dir}/${prsv}" ]; then
47 # Preserve the symlink target file
48 prsv="$(readlink "${running_dir}/${prsv}")"
49 cp -p ${running_dir}/"${prsv}" ${prsv_dir}
50 fi
Adriana Kobylake8606da2020-07-15 09:16:56 -050051 done
52 fi
53
Adriana Kobylakff675db2021-09-07 18:00:57 +000054 # Copy lid contents of running image to running dir
Adriana Kobylake8606da2020-07-15 09:16:56 -050055 rm -f ${running_dir}/*
Adriana Kobylakff675db2021-09-07 18:00:57 +000056 cp -p ${ro_dir}/*.lid ${running_dir}/
Adriana Kobylake8606da2020-07-15 09:16:56 -050057
Adriana Kobylak98f22322020-07-20 10:39:41 -050058 # Restore the preserved partitions. Ignore error, there may be none.
59 cp -p ${prsv_dir}/* ${running_dir}/ 2>/dev/null || true
Adriana Kobylakf6d4d762021-04-21 10:46:14 -050060 rm -f "${prsv_dir:?}/"*
Adriana Kobylake8606da2020-07-15 09:16:56 -050061
Adriana Kobylak87372ae2020-09-01 11:30:59 -050062 # Clean up the staging dir in case of a failed update
Adriana Kobylakf6d4d762021-04-21 10:46:14 -050063 rm -rf "${staging_dir:?}/"*
Adriana Kobylak87372ae2020-09-01 11:30:59 -050064
Adriana Kobylake8606da2020-07-15 09:16:56 -050065 # Save the label
66 echo "${boot_label}" > "${running_label_file}"
67
Adriana Kobylake8606da2020-07-15 09:16:56 -050068 fi
69
70 # Mount alternate dir
71 if [ "${boot_label}" = "a" ]; then
72 alternate_label="b"
73 else
74 alternate_label="a"
75 fi
76 alternate_dir="${base_dir}/alternate"
77 if [ ! -d "${alternate_dir}" ]; then
78 mkdir -p ${alternate_dir}
79 fi
Isaac Kurth0ddd4fa2021-07-14 17:35:37 -050080 if ! grep -q "${alternate_dir}" /proc/mounts; then
81 mount ${base_dir}/hostfw-${alternate_label} ${alternate_dir} -o ro
82 fi
Adriana Kobylake8606da2020-07-15 09:16:56 -050083}
84
85mmc_patch() {
86 # Patching is disabled if field mode is set
Adriana Kobylakf6d4d762021-04-21 10:46:14 -050087 if [ "$(fw_printenv fieldmode 2>/dev/null)" = "fieldmode=true" ]; then
Adriana Kobylake8606da2020-07-15 09:16:56 -050088 return 0
89 fi
90
Adriana Kobylakf6608a82020-08-25 14:16:23 -050091 boot_label="$(fw_printenv -n bootside)"
92 if [ "${boot_label}" = "a" ]; then
93 alternate_label="b"
94 else
95 alternate_label="a"
Adriana Kobylake8606da2020-07-15 09:16:56 -050096 fi
97
Adriana Kobylakf6608a82020-08-25 14:16:23 -050098 # Create patch directories
99 patch_dir="/media/hostfw/patch-"
100 running_patch_dir="${patch_dir}${boot_label}"
101 if [ ! -d "${running_patch_dir}" ]; then
102 mkdir -p "${running_patch_dir}"
103 fi
104 alternate_patch_dir="${patch_dir}${alternate_label}"
105 if [ ! -d "${alternate_patch_dir}" ]; then
106 mkdir -p "${alternate_patch_dir}"
Adriana Kobylake8606da2020-07-15 09:16:56 -0500107 fi
108
Adriana Kobylakf6608a82020-08-25 14:16:23 -0500109 # Create patch symlinks
110 symlink_base="/usr/local/share"
111 if [ ! -d "${symlink_base}" ]; then
112 mkdir -p "${symlink_base}"
113 fi
114 hostfw_symlink_base="${symlink_base}/hostfw"
115 if [ ! -d "${hostfw_symlink_base}" ]; then
116 mkdir -p "${hostfw_symlink_base}"
117 fi
118
Isaac Kurth0ddd4fa2021-07-14 17:35:37 -0500119 if [ "$(readlink -f "${symlink_base}/pnor")" != "${running_patch_dir}" ]; then
120 ln -s "${running_patch_dir}" "${symlink_base}/pnor"
121 fi
122 if [ "$(readlink -f "${hostfw_symlink_base}/running")" != "${running_patch_dir}" ]; then
123 ln -s "${running_patch_dir}" "${hostfw_symlink_base}/running"
124 fi
125 if [ "$(readlink -f "${hostfw_symlink_base}/alternate")" != "${alternate_patch_dir}" ]; then
126 ln -s "${alternate_patch_dir}" "${hostfw_symlink_base}/alternate"
127 fi
Adriana Kobylake8606da2020-07-15 09:16:56 -0500128}
129
130case "$1" in
131 mmc-init)
132 mmc_init
133 ;;
134 mmc-patch)
135 mmc_patch
136 ;;
137 *)
138 echo "Invalid argument"
139 exit 1
140 ;;
141esac