-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathu-boot-update
executable file
·272 lines (227 loc) · 7.72 KB
/
u-boot-update
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#!/bin/sh
## Copyright (C) 2006-2012 Daniel Baumann <daniel.baumann@progress-technologies.net>
## Copyright (C) 2016-2017 Riku Voipio <riku.voipio@linaro.org>
##
## This program comes with ABSOLUTELY NO WARRANTY; for details see COPYING.
## This is free software, and you are welcome to redistribute it
## under certain conditions; see COPYING for details.
set -e
_U_BOOT_DIRECTORY="/boot/extlinux"
_U_BOOT_DTB_DIR="/boot"
Update() {
# Upate target file using source content
_TARGET="${1}"
_SOURCE="${2}"
_TMPFILE="${_TARGET}.tmp"
rm -f "${_TMPFILE}"
echo "${_SOURCE}" >"${_TMPFILE}"
if [ -e "${_TARGET}" ] && cmp -s "${_TARGET}" "${_TMPFILE}"; then
rm -f "${_TMPFILE}"
else
# FIXME: should use fsync here
echo "P: Updating ${_TARGET}..."
mv -f "${_TMPFILE}" "${_TARGET}"
fi
}
# FIXME: switch to check extlinux file can be written to
# User is unprivileged
if [ "$(id -u)" -ne 0 ]; then
echo "E: need root privileges"
exit 1
fi
# Redirect stdout to stderr due Debconf usage
exec 1>&2
# Reading the default file
if [ -e /etc/default/u-boot ]; then
. /etc/default/u-boot
fi
# Reading config file fragments if they exist
for file in /usr/share/u-boot-menu/conf.d/*.conf /etc/u-boot-menu/conf.d/*.conf; do
if [ -e "${file}" ]; then
. "${file}"
fi
done
# Reading the os-release file
if [ -e /etc/os-release ]; then
. /etc/os-release
elif [ -e /usr/lib/os-release ]; then
. /usr/lib/os-release
fi
U_BOOT_UPDATE="${U_BOOT_UPDATE:-true}"
if [ "${U_BOOT_UPDATE}" != "true" ]; then
echo "P: u-boot-update is disabled in /etc/default/u-boot."
exit 0
fi
# Checking extlinux directory
printf '%s' "P: Checking for EXTLINUX directory..."
# Creating extlinux directory
if [ ! -e "${_U_BOOT_DIRECTORY}" ]; then
echo " not found."
printf '%s' "P: Creating EXTLINUX directory..."
mkdir -p "${_U_BOOT_DIRECTORY}"
echo " done: ${_U_BOOT_DIRECTORY}"
else
echo " found."
fi
# Setting defaults if /etc/default/u-boot is missing
U_BOOT_ALTERNATIVES="${U_BOOT_ALTERNATIVES:-default recovery}"
U_BOOT_DEFAULT="${U_BOOT_DEFAULT:-l0}"
U_BOOT_PROMPT="${U_BOOT_PROMPT:-0}"
U_BOOT_ENTRIES="${U_BOOT_ENTRIES:-all}"
U_BOOT_TIMEOUT="${U_BOOT_TIMEOUT:-50}"
U_BOOT_MENU_LABEL="${U_BOOT_MENU_LABEL:-${PRETTY_NAME:-Debian GNU/Linux kernel}}"
U_BOOT_FDT_DIR="${U_BOOT_FDT_DIR:-/usr/lib/linux-image-}"
U_BOOT_FDT_OVERLAYS="${U_BOOT_FDT_OVERLAYS:-}"
U_BOOT_FDT_OVERLAYS_DIR="${U_BOOT_FDT_OVERLAYS_DIR:-/boot/dtbo}"
U_BOOT_INITRD="${U_BOOT_INITRD:-initrd.img}"
if [ -z "${U_BOOT_PARAMETERS}" ] && [ -f /etc/kernel/cmdline ]; then
U_BOOT_PARAMETERS="$(sed -e 's/root=[^[:space:]]*//' -e 's/^[[:space:]]*//' /etc/kernel/cmdline)"
if [ -z "${U_BOOT_ROOT}" ]; then
U_BOOT_ROOT="$(sed -re 's/.*(root=[^[:space:]]*).*/\1/' /etc/kernel/cmdline)"
fi
fi
U_BOOT_PARAMETERS="${U_BOOT_PARAMETERS:-ro quiet}"
# Loop through all matching DTB files
for dtb in $(find "$_U_BOOT_DTB_DIR" -type f -name "*.dtb"); do
filename=$(basename "$dtb") # Get just the filename
U_BOOT_FDT="$U_BOOT_FDT $filename"
done
# Trim the trailing space
U_BOOT_FDT=$(echo "$U_BOOT_FDT" | sed 's/ *$//')
# Create extlinux.conf
_CONFIG="\
## ${_U_BOOT_DIRECTORY}/extlinux.conf
##
## IMPORTANT WARNING
##
## The configuration of this file is generated automatically.
## Do not edit this file manually, use: u-boot-update
default ${U_BOOT_DEFAULT}
menu title U-Boot menu
prompt ${U_BOOT_PROMPT}
timeout ${U_BOOT_TIMEOUT}
"
# Find linux versions
_KERNELS=$(find /boot -maxdepth 1 -type f -name "Image*" | sed -e 's,.*/boot/,,g' | sort -rf)
# Find boot directory as seen in u-boot, and path prefix while in linux
if [ "$(stat --printf %d /)" = "$(stat --printf %d /boot)" ]; then
# / and /boot are on the same filesystem
_BOOT_DIRECTORY="/boot"
_BOOT_PATH=""
else
# / and /boot are not on the same filesystem
_BOOT_DIRECTORY=""
_BOOT_PATH="/boot"
fi
for _KERNEL in ${_KERNELS}; do
# Strip kernel prefix to derive version.
_VERSION=${_KERNEL#*-}
echo "P: Writing config for ${_KERNEL}..."
_NUMBER="${_NUMBER:-0}"
_ENTRY="${_ENTRY:-1}"
if [ -e "/boot/${U_BOOT_INITRD}-${_VERSION}" ]; then
_INITRD="initrd ${_BOOT_DIRECTORY}/${U_BOOT_INITRD}-${_VERSION}"
elif [ -e "/boot/${U_BOOT_INITRD}" ]; then
_INITRD="initrd ${_BOOT_DIRECTORY}/${U_BOOT_INITRD}"
else
_INITRD=""
fi
for dtb in ${U_BOOT_FDT}; do
# Check if the DTB matches the current kernel version
_IMG_VERSION=$(echo "${_KERNEL#*-}" | sed 's/-.[a-z]+//')
DTB_VERSION=$(echo "$dtb" | sed 's/.*linux-//' | sed 's/\.dtb.*//')
if [ "$DTB_VERSION" = "$_IMG_VERSION" ]; then
if [ -e "${dtb}" ] && [ -n "${dtb}" ] && [ "/" = "$(echo "${dtb}" | head -c1)" ]; then
_FDT="fdt ${dtb}"
elif [ -e "${_BOOT_PATH}${U_BOOT_FDT_DIR}${_VERSION}/${dtb}" ] && [ -n "${dtb}" ]; then
_FDT="fdt ${U_BOOT_FDT_DIR}${_VERSION}/${dtb}"
elif [ -d "${_BOOT_PATH}${U_BOOT_FDT_DIR}${_VERSION}/" ]; then
_FDT="fdtdir ${U_BOOT_FDT_DIR}${_VERSION}/"
elif [ -e "${_BOOT_PATH}/${dtb}" ] && [ -n "${dtb}" ]; then
_FDT="fdt /${dtb}"
elif [ -f "${_BOOT_PATH}/${U_BOOT_FDT:-dtb-${_VERSION}}" ] && [ /usr/lib/linux-image- = "${U_BOOT_FDT_DIR}" ]; then
_FDT="fdt /${dtb:-dtb-${_VERSION}}"
else
_FDT=""
fi
break
fi
done
for overlays in "${_U_BOOT_DTB_DIR}"/overlays-*; do
_OVRLY_VERSION=$(echo "${overlays#*-}" | sed 's/-.*-[a-z]+//')
if [ -d "${overlays}" ] && [ "${_OVRLY_VERSION}" = "${_IMG_VERSION}" ]; then
_DTBO_LIST=""
if [ -n "${U_BOOT_FDT_OVERLAYS}" ]; then
for _DTBO in ${U_BOOT_FDT_OVERLAYS}; do
if [ -f "${U_BOOT_FDT_OVERLAYS_DIR}-${_IMG_VERSION}/${_DTBO}" ]; then
_DTBO_LIST="${_DTBO_LIST} ${U_BOOT_FDT_OVERLAYS_DIR}/${_DTBO}"
fi
done
else
for _DTBO_PATH in "${U_BOOT_FDT_OVERLAYS_DIR}-${_IMG_VERSION}/"*.dtbo; do
if [ -f "${_DTBO_PATH}" ]; then
_DTBO=$(basename "${_DTBO_PATH}")
_DTBO_LIST="${_DTBO_LIST} ${U_BOOT_FDT_OVERLAYS_DIR}-${_IMG_VERSION}/${_DTBO}"
fi
done
fi
if [ -n "${_DTBO_LIST}" ]; then
if echo "${_DTBO_LIST}" | grep -q "/boot"; then
_DTBO_LIST=$(echo "${_DTBO_LIST}" | sed -e "s/\/boot//g")
fi
_FDTOVERLAYS="fdtoverlays ${_DTBO_LIST}"
else
_FDTOVERLAYS=""
fi
elif [ -d "${_BOOT_PATH}/${U_BOOT_FDT_OVERLAYS_DIR}-${_IMG_VERSION}" ]; then
_DTBO_LIST=""
if [ -n "${U_BOOT_FDT_OVERLAYS}" ]; then
for _DTBO in ${U_BOOT_FDT_OVERLAYS}; do
if [ -f "${_BOOT_PATH}/${U_BOOT_FDT_OVERLAYS_DIR}-${_IMG_VERSION}/${_DTBO}" ]; then
_DTBO_LIST="${_DTBO_LIST} ${U_BOOT_FDT_OVERLAYS_DIR}-${_IMG_VERSION}/${_DTBO}"
fi
done
else
for _DTBO_PATH in "${_BOOT_PATH}/${U_BOOT_FDT_OVERLAYS_DIR}-${_IMG_VERSION}/"*.dtbo; do
if [ -f "${_DTBO_PATH}" ]; then
_DTBO=$(basename "${_DTBO_PATH}")
_DTBO_LIST="${_DTBO_LIST} ${U_BOOT_FDT_OVERLAYS_DIR}-${_IMG_VERSION}/${_DTBO}"
fi
done
fi
if [ -n "${_DTBO_LIST}" ]; then
echo "Removing ${_BOOT_PATH} prefix from ${_DTBO_LIST}" 2>&1 | tee -a /var/log/u-boot-update.log
_DTBO_LIST=$(echo "${_DTBO_LIST}" | sed -e "s,^'/boot',,g")
_FDTOVERLAYS="fdtoverlays ${_DTBO_LIST}"
fi
fi
done
if echo "${U_BOOT_ALTERNATIVES}" | grep -q default; then
# Writing default entry
_CONFIG="${_CONFIG}
label l${_NUMBER}
menu label ${U_BOOT_MENU_LABEL} ${_VERSION}
linux ${_BOOT_DIRECTORY}/${_KERNEL}
${_INITRD}
${_FDT}
${_FDTOVERLAYS}
append ${U_BOOT_ROOT} ${U_BOOT_PARAMETERS}"
fi
if echo "${U_BOOT_ALTERNATIVES}" | grep -q recovery; then
# Writing recovery entry
_CONFIG="${_CONFIG}
label l${_NUMBER}r
menu label ${U_BOOT_MENU_LABEL} ${_VERSION} (rescue target)
linux ${_BOOT_DIRECTORY}/${_KERNEL}
${_INITRD}
${_FDT}
append ${U_BOOT_ROOT} $(echo "${U_BOOT_PARAMETERS}" | sed -e 's| quiet||') single
"
fi
_NUMBER="$((_NUMBER + 1))"
if [ "${U_BOOT_ENTRIES}" = "${_ENTRY}" ]; then
break
fi
done
_NUMBER=""
Update "${_U_BOOT_DIRECTORY}/extlinux.conf" "${_CONFIG}"