/
pi-uboot
executable file
·229 lines (207 loc) · 6.99 KB
/
pi-uboot
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#!/bin/bash
# MIT License
#
# Copyright (c) 2021 Matthias Lüscher
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
set -o nounset
set -o errexit
set -o pipefail
UBOOT_ENV_OVERLAY=/var/run/uboot-env-overlay
CONFIG_TXT=/boot/firmware/config.txt
TRYBOOT_TXT=/boot/firmware/tryboot.txt
print_and_exit()
{
local TYPE=$1
local MESSAGE=$2
if [ "${TYPE}" == "ERROR" ] ; then
>&2 echo "error: ${MESSAGE}"
exit 1
else
echo "${MESSAGE}"
exit 0
fi
}
print_environment()
{
echo "mender_boot_part=${mender_boot_part}"
echo "mender_boot_part_hex=${mender_boot_part_hex}"
echo "upgrade_available=${upgrade_available}"
echo "bootcount=${bootcount}"
echo "mender_uboot_separator=${mender_uboot_separator}"
}
update_environment()
{
local key=${1}
local value=${2}
case $key in
mender_boot_part)
mender_boot_part=${value}
MENDER_BOOT_PART_UPDATED=1
;;
mender_boot_part_hex)
mender_boot_part_hex=${value}
;;
upgrade_available)
upgrade_available=${value}
UPGRADE_AVAILABLE_UPDATED=1
;;
bootcount)
bootcount=${value}
;;
mender_uboot_separator)
mender_uboot_separator=${value}
;;
*)
print_and_exit "ERROR" "unsupported key ${key}"
;;
esac
}
if [[ $EUID -ne 0 ]]; then
print_and_exit "ERROR" "use root user to execute this script"
fi
CURRENT_BOOT_DEVICE=$(mount | sed -n 's|^/dev/\(.*\) on / .*|\1|p')
if [ -z "${CURRENT_BOOT_DEVICE}" ] ; then
print_and_exit "ERROR" "unable to detect root partition device"
fi
CURRENT_BOOT_PART=$(echo "${CURRENT_BOOT_DEVICE}" | grep -o '[1-9][0-9]*$')
if [ -z "${CURRENT_BOOT_PART}" ] ; then
print_and_exit "ERROR" "unable to extract partition number of ${CURRENT_BOOT_DEVICE}"
fi
COMMITTED_BOOT_PART=$(< "${CONFIG_TXT}" sed -n 's|^os_prefix=p\([[:digit:]]\)/$|\1|p')
if [ -z "${COMMITTED_BOOT_PART}" ] ; then
print_and_exit "ERROR" "unable to extract committed boot partition from ${CONFIG_TXT}"
fi
mender_boot_part=${CURRENT_BOOT_PART}
mender_boot_part_hex=$(printf '%x\n' "${CURRENT_BOOT_PART}")
if [[ ${CURRENT_BOOT_PART} -eq ${COMMITTED_BOOT_PART} ]]
then
upgrade_available=0
else
upgrade_available=1
fi
bootcount=1
mender_uboot_separator=""
if [[ -f "${UBOOT_ENV_OVERLAY}" ]]
then
source "${UBOOT_ENV_OVERLAY}"
fi
if [[ ${0} == *fw_printenv ]]
then
if [[ $# -eq 0 ]]
then
print_environment
else
for cli_arg in "$@"
do
print_environment | grep "^${cli_arg}=.*"
done
fi
elif [[ ${0} == *fw_setenv ]]
then
MENDER_BOOT_PART_UPDATED=0
UPGRADE_AVAILABLE_UPDATED=0
if [[ $# -eq 0 ]]
then
print_and_exit "ERROR" "variable name missing"
elif [[ $# -eq 1 ]] || [[ $# -eq 2 ]]
then
if [[ $1 == -s ]]
then
if [[ $# -ne 2 ]]
then
print_and_exit "ERROR" "missing script parameter"
fi
if [[ $2 == - ]]
then
SCRIPT=$(cat)
else
if [[ ! -f ${2} ]]
then
print_and_exit "ERROR" "file ${2} not found"
fi
SCRIPT=$(cat "${2}")
fi
while IFS= read -r line
do
read -r -a key_value_pair <<< "${line}"
if [[ ${#key_value_pair[@]} -eq 2 ]]
then
update_environment "${key_value_pair[0]}" "${key_value_pair[1]}"
elif [[ ${#key_value_pair[@]} -eq 1 ]]
then
update_environment "${key_value_pair[0]}" ""
else
print_and_exit "ERROR" "unsupported number of arguments in script"
fi
done <<< "${SCRIPT}"
else
if [[ $# -eq 2 ]]
then
update_environment "${1}" "${2}"
else
update_environment "${1}" ""
fi
fi
if [[ ${MENDER_BOOT_PART_UPDATED} -eq 1 ]] && [[ ${UPGRADE_AVAILABLE_UPDATED} -eq 1 ]] && [[ ${bootcount} -eq 0 ]] && [[ ${upgrade_available} -eq 1 ]]
then
# mender install transaction
if [[ ${CURRENT_BOOT_PART} -eq ${mender_boot_part} ]]
then
print_and_exit "ERROR" "invalid install transaction - new boot partition is equal to old boot partition"
elif [[ ${COMMITTED_BOOT_PART} -eq ${mender_boot_part} ]]
then
print_and_exit "ERROR" "invalid install transaction - new boot partition is equal to committed boot partition"
else
: # nothing to do - boot partition will be setup by state script
fi
elif [[ ${MENDER_BOOT_PART_UPDATED} -eq 1 ]] && [[ ${UPGRADE_AVAILABLE_UPDATED} -eq 1 ]] && [[ ${upgrade_available} -eq 0 ]]
then
# mender rollback transaction
# nothing to do - reboot will automatically boot into previously committed configuration
:
elif [[ ${MENDER_BOOT_PART_UPDATED} -eq 0 ]] && [[ ${UPGRADE_AVAILABLE_UPDATED} -eq 1 ]] && [[ ${upgrade_available} -eq 0 ]]
then
# mender commit transaction
if [[ ${CURRENT_BOOT_PART} -eq ${COMMITTED_BOOT_PART} ]]
then
: # nothing to do - commit has already been done
elif [[ ! -f "${TRYBOOT_TXT}" ]]
then
print_and_exit "ERROR" "missing file: ${TRYBOOT_TXT}"
else
mv ${TRYBOOT_TXT} ${CONFIG_TXT}
fi
fi
print_environment > "${UBOOT_ENV_OVERLAY}"
else
print_and_exit "ERROR" "unsupported number of arguments"
fi
elif [[ ${0} == *reboot ]]
then
if [[ $# -ge 1 ]]
then
/usr/sbin/reboot "$@"
elif [[ ${upgrade_available} -eq 1 ]] && [[ ${COMMITTED_BOOT_PART} -ne ${mender_boot_part} ]] && [[ ${bootcount} -eq 0 ]] && [[ -f "${TRYBOOT_TXT}" ]]
then
/usr/sbin/reboot '0 tryboot'
else
/usr/sbin/reboot
fi
fi