File: u-boot-update

package info (click to toggle)
u-boot-menu 4.2.4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 120 kB
  • sloc: sh: 283; makefile: 2
file content (232 lines) | stat: -rwxr-xr-x 5,086 bytes parent folder | download
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
#!/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"

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

. /usr/share/u-boot-menu/read-config

# 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

# 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_FILE}" = "/" ]
		then
			case "${_FS_SPEC}" in
				"#"*) ;;
				*) U_BOOT_ROOT="root=${_FS_SPEC}"
				   break ;;
			esac
		fi
	done < /etc/fstab
fi

# if not in fstab, try from current kernel arguments
if [ -z "${U_BOOT_ROOT}" ]
then
	for param in $(cat /proc/cmdline)
	do
		case $param in
		  root=*)
			U_BOOT_ROOT="$param"
			break
			;;
		esac
	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 ${U_BOOT_PROMPT}
timeout ${U_BOOT_TIMEOUT}
"

# Find linux versions
_KERNELS=$(linux-version list --paths | linux-version sort --reverse | sed -e 's,.*/boot/,,g')

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
	if [ -e "${U_BOOT_FDT}" ] && [ -n "${U_BOOT_FDT}" ] && [ "/" = $(echo "${U_BOOT_FDT}" | head -c1) ]
	then
		_FDT="fdt ${U_BOOT_FDT}"
	elif [ -e "${_BOOT_PATH}${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 "${_BOOT_PATH}${U_BOOT_FDT_DIR}${_VERSION}/" ]
	then
		_FDT="fdtdir ${U_BOOT_FDT_DIR}${_VERSION}/"
	elif [ -f "${_BOOT_PATH}/${U_BOOT_FDT:-dtb-${_VERSION}}" ]
	then
		_FDT="fdt /${U_BOOT_FDT:-dtb-${_VERSION}}"
	else
		_FDT=""
	fi

	if [ -d "${_BOOT_PATH}/${U_BOOT_FDT_OVERLAYS_DIR}${_VERSION}" ]
	then
		_U_BOOT_FDT_OVERLAYS_DIR=${U_BOOT_FDT_OVERLAYS_DIR}${_VERSION}
	elif [ -d "${_BOOT_PATH}/${U_BOOT_FDT_OVERLAYS_DIR}" ]
	then
		_U_BOOT_FDT_OVERLAYS_DIR=${U_BOOT_FDT_OVERLAYS_DIR}
	else
		_U_BOOT_FDT_OVERLAYS_DIR=""
	fi
	if [ -d "${_BOOT_PATH}/${_U_BOOT_FDT_OVERLAYS_DIR}" ]
	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}/${_DTBO}" ]
				then
					_DTBO_LIST="${_DTBO_LIST} ${_U_BOOT_FDT_OVERLAYS_DIR}/${_DTBO}"
				fi
			done
		else
			for _DTBO_PATH in "${_BOOT_PATH}/${_U_BOOT_FDT_OVERLAYS_DIR}/"*.dtbo
			do
				if [ -f "${_DTBO_PATH}" ]
				then
					_DTBO=$(basename "${_DTBO_PATH}")
					_DTBO_LIST="${_DTBO_LIST} ${_U_BOOT_FDT_OVERLAYS_DIR}/${_DTBO}"
				fi
			done
		fi
		if [ -n "${_DTBO_LIST}" ]
		then
			_FDTOVERLAYS="fdtoverlays ${_DTBO_LIST}"
		fi
	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}/${_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}"