-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathu-boot-update
executable file
·211 lines (167 loc) · 4.27 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
#!/bin/bash
## 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"
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
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
echo -n "P: Checking for EXTLINUX directory..."
# Creating extlinux directory
if [ ! -e "${_U_BOOT_DIRECTORY}" ]
then
echo " not found."
echo -n "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_ENTRIES="${U_BOOT_ENTRIES:-all}"
U_BOOT_TIMEOUT="${U_BOOT_TIMEOUT:-50}"
U_BOOT_MENU_LABEL="Debian GNU/Linux kernel"
U_BOOT_FDT_DIR="${U_BOOT_FDT_DIR:-/usr/lib/linux-image-}"
# Find parameter for root from fstab
if [ -z "${U_BOOT_ROOT}" ]
then
# Find root partition
while read _LINE
do
read _FS_SPEC _FS_FILE _FS_VFSTYPE _FS_MNTOPS _FS_FREQ _FS_PASSNO << EOF
${_LINE}
EOF
if [ "${_FS_SPEC}" != "#" ] && [ "${_FS_FILE}" = "/" ]
then
U_BOOT_ROOT="root=${_FS_SPEC}"
break
fi
done < /etc/fstab
fi
# if not in fsrab, try from current kernel arguments
if [ -z "${U_BOOT_ROOT}" ]
then
for param in `cat /proc/cmdline`
do
if [[ $param == root=* ]]
then
U_BOOT_ROOT="$param"
break
fi
done
fi
# 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 0
timeout ${U_BOOT_TIMEOUT}
"
# Find linux versions
_VERSIONS="$(linux-version list | linux-version sort --reverse)"
if [ "$(stat --printf %d /)" = "$(stat --printf %d /boot)" ]
then
# / and /boot are on the same filesystem
_BOOT_DIRECTORY="/boot"
else
# / and /boot are not on the same filesystem
_BOOT_DIRECTORY=""
fi
for _VERSION in ${_VERSIONS}
do
echo "P: Writing config for /boot/vmlinuz-${_VERSION}..."
_NUMBER="${_NUMBER:-0}"
_ENTRY="${_ENTRY:-1}"
if [ -e /boot/initrd.img-${_VERSION} ]
then
_INITRD="initrd ${_BOOT_DIRECTORY}/initrd.img-${_VERSION}"
else
_INITRD=""
fi
if [ -e ${U_BOOT_FDT_DIR}${_VERSION}/${U_BOOT_FDT} ] && [ -n "${U_BOOT_FDT}" ]
then
_FDT="fdt ${U_BOOT_FDT_DIR}${_VERSION}/${U_BOOT_FDT}"
elif [ -d ${U_BOOT_FDT_DIR}${_VERSION}/ ]
then
_FDT="fdtdir ${U_BOOT_FDT_DIR}${_VERSION}/"
else
_FDT=""
fi
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}/vmlinuz-${_VERSION}
${_INITRD}
${_FDT}
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}/vmlinuz-${_VERSION}
${_INITRD}
${_FDT}
append ${U_BOOT_ROOT} $(echo ${U_BOOT_PARAMETERS} | sed -e 's| quiet||') systemd.unit=rescue.target
"
fi
_NUMBER="$((${_NUMBER} + 1))"
if [ "${U_BOOT_ENTRIES}" = "${_ENTRY}" ]
then
break
fi
done
_NUMBER=""
Update "${_U_BOOT_DIRECTORY}/extlinux.conf" "${_CONFIG}"