-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuild.sh
executable file
·497 lines (410 loc) · 15.2 KB
/
build.sh
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
#!/bin/bash
# shellcheck disable=SC1117
# This script builds the kernel, kernel modules, device trees and boot scripts for the A20 linux system that we use.
# Check for a valid cross compiler. When unset, the kernel tries to build itself
# using arm-none-eabi-gcc, so we need to ensure it exists. Because printenv and
# which can cause bash -e to exit, so run this before setting this up.
if [ "${CROSS_COMPILE}" == "" ]; then
# if [ "$(command -v aarch64-linux-gnu-gcc)" != "" ]; then
CROSS_COMPILE="aarch64-linux-gnu-"
# fi
if [ "${CROSS_COMPILE}" == "" ]; then
echo "No suitable cross-compiler found."
echo "One can be set explicitly via the environment variable CROSS_COMPILE='arm-linux-gnueabihf-' for example."
exit 1
fi
fi
export CROSS_COMPILE="${CROSS_COMPILE}"
set -eu
cpu_cnt="$(nproc)"
export MAKEFLAGS="-j ${cpu_cnt}"
echo "Compiling with ${cpu_cnt} CPUs..."
ARCH="${ARCH:-arm64}"
UM_ARCH="imx8mm" # Empty string, or sun7i for R1, or imx6dl for R2, or imx8mm for Colorado
# common directory variablesS
SYSCONFDIR="${SYSCONFDIR:-/etc}"
SRC_DIR="$(pwd)"
BUILD_DIR_TEMPLATE="_build"
BUILD_DIR="${BUILD_DIR:-${SRC_DIR}/${BUILD_DIR_TEMPLATE}}"
# Debian package information
PACKAGE_NAME="${PACKAGE_NAME:-um-kernel}"
RELEASE_VERSION="${RELEASE_VERSION:-999.999.999}"
# Which kernel to build
LINUX_SRC_DIR="${SRC_DIR}/linux"
# Which kernel config to build.
BUILDCONFIG="sx8m"
# Setup internal variables
KCONFIG="${SRC_DIR}/configs/${BUILDCONFIG}_config"
KERNEL_BUILD_DIR="${SRC_DIR}/_build/${BUILDCONFIG}-linux"
KERNEL_IMAGE="uImage-${BUILDCONFIG}"
DEBIAN_DIR="${BUILD_DIR}/debian"
BOOT_FILE_OUTPUT_DIR="${DEBIAN_DIR}/boot"
# We need freescale proprietary DMA drivers for the UART they are binary blobs in the Linux mainline
# They can be found in git://git.kernel.org/pub/scm/linux/kernel/git/firmware/linux-firmware.git
PROPRIETARY_FIRMWARE_DIR="${SRC_DIR}/proprietary_firmware"
PROPRIETARY_FIRMWARE_OUTPUT_DIR="${DEBIAN_DIR}/lib/firmware"
INITRAMFS_MODULES_REQUIRED="loop.ko"
INITRAMFS_SOURCE="${INITRAMFS_SOURCE:-initramfs/initramfs.lst}"
BB_VERSION="1.31.0"
BB_URL="https://busybox.net/downloads/busybox-${BB_VERSION}.tar.bz2"
BB_BIN="busybox"
BB_PKG="busybox-${BB_VERSION}.tar.bz2"
BB_DIR="busybox-${BB_VERSION}"
DEPMOD="${DEPMOD:-/sbin/depmod}"
if [ ! -x "${DEPMOD}" ]; then
DEPMOD="busybox depmod"
if [ ! -x "${DEPMOD}" ]; then
echo "No depmod binary available. Cannot continue."
exit 1
fi
fi
# Add the UM_ARCH to release version keeping a possible -dev on the most right side
if [[ ${RELEASE_VERSION} == *'-dev' ]]; then
RELEASE_VERSION="${RELEASE_VERSION/-dev/-${UM_ARCH}-dev}"
else
RELEASE_VERSION="${RELEASE_VERSION}-${UM_ARCH}"
fi;
##
# busybox_get() - Download and build the Busybox package
# param1: Writable path where to store the Busybox binary
#
# Busybox is downloaded from the global variable ${BB_PKG}.
busybox_get()
{
DEST_DIR="${1}"
if [ ! -d "${DEST_DIR}" ]; then
echo "No initramfs dir set to download busybox into."
exit 1
fi
if [ ! -f "${BB_PKG}" ]; then
if ! wget -q "${BB_URL}"; then
echo "Unable to download the busybox package '${BB_URL}'. Update the download URL."
exit 1
fi
fi
if [ ! -d "${BB_DIR}" ]; then
if ! tar xvjf "${BB_PKG}" > /dev/null 2>&1; then
echo "Unable to extract Busybox package '${BB_PKG}'."
exit 1
fi
fi
cd "${BB_DIR}"
cp "${SRC_DIR}/configs/busybox_config" ".config"
ARCH="${ARCH}" CROSS_COMPILE="${CROSS_COMPILE}" make
mv "${BB_BIN}" "${DEST_DIR}/${BB_BIN}"
cd "${SRC_DIR}"
if [ ! -x "${DEST_DIR}/${BB_BIN}" ]; then
echo "Failed to get busybox."
exit 1
fi
}
##
# initramfs_add_modules()
#
# In initramfs we can make drivers available by adding them to
# the 'INITRAMFS_MODULES_REQUIRED' variable. This function
# will check for dependencies in the installed modules 'modules.dep' file
# and add the requested modules and its dependencies to initramfs.
initramfs_add_modules()
{
echo "Adding initramfs modules."
KERNEL_RELEASE=$(cat "${KERNEL_BUILD_DIR}/include/config/kernel.release")
if [ ! -d "${DEBIAN_DIR}/lib" ] || [ -z "${KERNEL_RELEASE}" ] || \
[ ! -f "${DEBIAN_DIR}/lib/modules/${KERNEL_RELEASE}/modules.dep" ]; then
echo "Error, no modules installed, cannot continue."
exit 1
fi
if [ -d "${INITRAMFS_MODULES_DIR}" ] && [ -z "${INITRAMFS_MODULES_DIR##*/initramfs/lib/modules*}" ]; then
rm -rf "${INITRAMFS_MODULES_DIR}"
fi
if [ -n "${INITRAMFS_MODULES_REQUIRED}" ]; then
mkdir -p "${INITRAMFS_MODULES_DIR}/${KERNEL_RELEASE}"
{
echo -e "\n# kernel modules"
echo "dir /lib/modules/ 0755 0 0"
echo "dir /lib/modules/${KERNEL_RELEASE}/ 0755 0 0"
} >> "${INITRAMFS_DEST}"
MODULES_DIR="${DEBIAN_DIR}/lib/modules/${KERNEL_RELEASE}"
INITRAMFS_MODULES="${INITRAMFS_MODULES_REQUIRED}"
for module in ${INITRAMFS_MODULES_REQUIRED}; do
dependencies="$(grep "${module}:" "${MODULES_DIR}/modules.dep" | sed -e "s|^.*:\s*||")"
for dependency in ${dependencies}; do
dep_module="$(basename "${dependency}")"
echo "Adding dependency: '${dep_module}' for module: '${module}'"
if [ -n "${INITRAMFS_MODULES##*"${dep_module}"*}" ]; then
INITRAMFS_MODULES="${INITRAMFS_MODULES} ${dep_module}"
fi
done
done
fi
for module in ${INITRAMFS_MODULES}; do
if [ -z "$(find "${MODULES_DIR}" -name "${module}" -print -exec cp "{}" "${INITRAMFS_MODULES_DIR}/${KERNEL_RELEASE}" \;)" ]; then
echo "Error: kernel module: '${module}' not available."
exit 1
fi
echo "Adding kernel module: '${module}' to initrd."
echo "file /lib/modules/${KERNEL_RELEASE}/${module} ${INITRAMFS_MODULES_DIR}/${KERNEL_RELEASE}/${module} 0755 0 0" >> "${INITRAMFS_DEST}"
done
if [ -n "${INITRAMFS_MODULES}" ] && ! ${DEPMOD} -ab "${INITRAMFS_DST_DIR}" "${KERNEL_RELEASE}"; then
echo "Failed to generate module dependencies."
exit 1
fi
for moddep in "${INITRAMFS_MODULES_DIR}/${KERNEL_RELEASE}/modules."*; do
if [ -f "${moddep}" ]; then
moddep="$(basename "${moddep}")"
echo "file /lib/modules/${KERNEL_RELEASE}/${moddep} ${INITRAMFS_MODULES_DIR}/${KERNEL_RELEASE}/${moddep} 0755 0 0" >> "${INITRAMFS_DEST}"
fi
done
echo "Finished adding initramfs modules."
}
##
# initramfs_prepare() - Prepare the initramfs tree
#
# To be able to create an initramfs in the temporary build directory, where
# the kernel expects these files due to 'INITRAMFS_SOURCE' being set, we need
# to copy the source initramfs files and put some expected binaries in place.
initramfs_prepare()
{
echo "Preparing initramfs."
INITRAMFS_SRC_DIR="${SRC_DIR}/initramfs"
INITRAMFS_DST_DIR="${KERNEL_BUILD_DIR}/initramfs"
INITRAMFS_MODULES_DIR="${KERNEL_BUILD_DIR}/initramfs/lib/modules"
INITRAMFS_DEST="${INITRAMFS_DST_DIR}/$(basename "${INITRAMFS_SOURCE}")"
if [ -d "${INITRAMFS_DST_DIR}" ]; then
rm -rf "${INITRAMFS_DST_DIR}"
fi
mkdir -p "${INITRAMFS_DST_DIR}"
cp -a "${INITRAMFS_SRC_DIR}/"* "${INITRAMFS_DST_DIR}"
busybox_get "${INITRAMFS_DST_DIR}"
echo "Finished preparing initramfs."
}
##
# kernel_build_command() - Wrapper function for Kernel build commands
#
# Wrap the argument into a Linux Kernel cross-compile command.
kernel_build_command()
{
if [ ! -d "${KERNEL_BUILD_DIR}" ]; then
mkdir -p "${KERNEL_BUILD_DIR}"
fi
cd "${LINUX_SRC_DIR}"
ARCH="${ARCH}" CROSS_COMPILE="${CROSS_COMPILE}" make O="${KERNEL_BUILD_DIR}" KCONFIG_CONFIG="${KCONFIG}" "${@}"
cd "${SRC_DIR}"
}
##
# kernel_build() - Build the Linux Kernel image
#
# Builds the Kernel
# Installs the modules in the build output lib directory
# Creates the 'initramfs'
# Creates a uImage binary in the build output boot directory.
kernel_build()
{
echo "Building Kernel."
# Prepare the initramfs 1st time
initramfs_prepare
# Configure the kernel
kernel_build_command
# Build the Kernel modules and generate dependency list
kernel_modules_install
# New that all modules have been build and the dependency file is properly generated,
# we can add the required Kernel modules to initramfs
initramfs_add_modules
# Build the uImage file for a bootable kernel
kernel_build_command LOADADDR=0x40480000 Image
# Install Kernel image
if [ -d "${BOOT_FILE_OUTPUT_DIR}" ] && [ -z "${BOOT_FILE_OUTPUT_DIR##*_build*}" ]; then
rm -r "${BOOT_FILE_OUTPUT_DIR}"
fi
mkdir -p "${BOOT_FILE_OUTPUT_DIR}"
cp "${KERNEL_BUILD_DIR}/arch/arm64/boot/Image" "${BOOT_FILE_OUTPUT_DIR}/${KERNEL_IMAGE}"
echo "Finished building Kernel."
}
##
# kernel_modules_install() - Install the Kernel modules
#
# Generates a 'lib/modules/[Kernel version]' directory structure
# in the build output directory.
kernel_modules_install()
{
echo "Install Kernel modules."
KERNEL_RELEASE=$(cat "${KERNEL_BUILD_DIR}/include/config/kernel.release")
if [ -z "${KERNEL_RELEASE}" ]; then
echo "Error, unable to get kernel release version, cannot continue."
exit 1
fi
if [ -d "${DEBIAN_DIR}/lib/modules" ] && [ -z "${BOOT_FILE_OUTPUT_DIR##*_build*}" ]; then
rm -r "${DEBIAN_DIR}/lib/modules"
fi
kernel_build_command INSTALL_MOD_PATH="${DEBIAN_DIR}" modules_install
if ! "${DEPMOD}" -ab "${DEBIAN_DIR}" "${KERNEL_RELEASE}"; then
echo "Error, failed to generate module dependencies."
fi
echo "Finished installing Kernel modules."
}
##
# dtb_build() - Compile product specific device-tree binary files
#
# In the U-Boot stage the boot script is executed, that in turn reads
# the machine article number from the I2C EEPROM. This article number
# is in Hexadecimal format. The boot-script will then load a device-tree
# with corresponding article number into memory.
#
# This function will parse a file called article.links and compile the device-tree
# binaries described above.
dtb_build()
{
echo "Building Device-trees."
if [ -d "${KERNEL_BUILD_DIR}/dtb" ]; then
rm -rf "${KERNEL_BUILD_DIR}/dtb"
fi
mkdir -p "${KERNEL_BUILD_DIR}/dtb"
if [ ! -d "${BOOT_FILE_OUTPUT_DIR}" ]; then
mkdir -p "${BOOT_FILE_OUTPUT_DIR}"
fi
rm -rf "${BOOT_FILE_OUTPUT_DIR}/"*".dtb"
# Build the device trees that we need
for dts in "dts/"*".dts"; do
dts="$(basename "${dts}")"
dt="${dts%.dts}"
echo "Building devicetree blob '${dt}'"
cpp -nostdinc -undef -D__DTS__ -x assembler-with-cpp \
-I "${LINUX_SRC_DIR}/include" -I "${LINUX_SRC_DIR}/arch/${ARCH}/boot/dts" \
-o "${KERNEL_BUILD_DIR}/dtb/.${dt}.dtb.tmp" "dts/${dts}"
dtc -I dts -o "${BOOT_FILE_OUTPUT_DIR}/${dt}.dtb" -O dtb "${KERNEL_BUILD_DIR}/dtb/.${dt}.dtb.tmp"
done
echo "Finished building Device-trees."
}
# We need this because the imx8m uart uses this.
# We can choose not to use it and configure it differently in the device-tree.
copy_dma_firmware()
{
if [ ! -d "${PROPRIETARY_FIRMWARE_OUTPUT_DIR}/imx/sdma" ]; then
mkdir -p "${PROPRIETARY_FIRMWARE_OUTPUT_DIR}/imx/sdma"
fi
cp "${PROPRIETARY_FIRMWARE_DIR}/imx/sdma/sdma-imx7d.bin" "${PROPRIETARY_FIRMWARE_OUTPUT_DIR}/imx/sdma/"
cp "${PROPRIETARY_FIRMWARE_DIR}/imx/sdma/sdma-imx6q.bin" "${PROPRIETARY_FIRMWARE_OUTPUT_DIR}/imx/sdma/"
}
create_debian_package()
{
echo "Building Debian package."
if [ ! -d "${BOOT_FILE_OUTPUT_DIR}" ]; then
echo "Error, boot directory not created, no boot files to package."config
exit 1
fi
if ! ls "${BOOT_FILE_OUTPUT_DIR}/uImage"* 1> /dev/null 2>&1; then
echo "Error, no Kernel binary installed, run 'kernel' build first."
exit 1
fi
if [ ! -d "${DEBIAN_DIR}/lib" ] || [ ! -f "${DEBIAN_DIR}/lib/modules/${KERNEL_RELEASE}/modules.dep" ]; then
echo "Error, no modules installed, run 'kernel' build first."
exit 1
fi
if ! ls "${BOOT_FILE_OUTPUT_DIR}/"*".dtb" 1> /dev/null 2>&1; then
echo "Error, no Kernel device-tree files installed, run 'dtbs' build first."
exit 1
fi
if ! ls "${PROPRIETARY_FIRMWARE_OUTPUT_DIR}/imx/sdma/"*".bin" 1> /dev/null 2>&1; then
echo "Error, linux-firmware proprietary directory not found for DMA drivers."
exit 1
fi
mkdir -p "${DEBIAN_DIR}/DEBIAN"
sed -e 's|@ARCH@|'"${ARCH}"'|g' \
-e 's|@PACKAGE_NAME@|'"${PACKAGE_NAME}"'|g' \
-e 's|@RELEASE_VERSION@|'"${RELEASE_VERSION}"'|g' \
"${SRC_DIR}/debian/control.in" > "${DEBIAN_DIR}/DEBIAN/control"
cp "${SRC_DIR}/debian/preinst" "${DEBIAN_DIR}/DEBIAN/"
DEB_PACKAGE="${PACKAGE_NAME}_${RELEASE_VERSION}_${ARCH}.deb"
# Build the Debian package
fakeroot dpkg-deb --build "${DEBIAN_DIR}" "${BUILD_DIR}/${DEB_PACKAGE}"
cp "${BUILD_DIR}/${DEB_PACKAGE}" "${SRC_DIR}"
echo "Finished building Debian package."
echo "To check the contents of the Debian package run 'dpkg-deb -c um-kernel*.deb'"
}
usage()
{
echo ""
echo "This is the build script for Linux Kernel related build artifacts and configure the Kernel."
echo ""
echo " Usage: ${0} [kernel|dtbs|menuconfig|oldconfig|deb|clean]"
echo " For Kernel config modification use: ${0} menuconfig"
echo ""
echo " -h Print this help text and exit"
echo ""
echo " By default the script can be executed with 'no' arguments, all required artifacts"
echo " will be build resulting in a 'um-kernel-[RELEASE_VERSION].deb' package."
echo " Either one of the above optional arguments can be passed to the script to build that"
echo " specific artifact."
echo ""
echo " The package release version can be passed by passing 'RELEASE_VERSION' through the run environment."
}
while getopts ":h" options; do
case "${options}" in
h)
usage
exit 0
;;
:)
echo "Option -${OPTARG} requires an argument."
exit 1
;;
?)
echo "Invalid option: -${OPTARG}"
exit 1
;;
esac
done
shift "$((OPTIND - 1))"
if [ "${#}" -gt 1 ]; then
echo "Too many arguments."
usage
exit 1
fi
if [ "${#}" -eq 0 ]; then
kernel_build
dtb_build
copy_dma_firmware
create_debian_package
exit 0
fi
case "${1-}" in
deb)
kernel_build
dtb_build
copy_dma_firmware
create_debian_package
;;
dtbs)
dtb_build
;;
kernel)
kernel_build
;;
menuconfig)
kernel_build_command menuconfig
;;
oldconfig)
kernel_build_command oldconfig
;;
clean)
if [ -d "${BUILD_DIR}" ] && [ -z "${BUILD_DIR##*_build*}" ]; then
rm -rf "${BUILD_DIR}"
fi
echo "Cleaned up '${BUILD_DIR}'."
if [ -d "${BB_DIR}" ] && [ -z "${BB_DIR##*busybox-*}" ]; then
rm -rf "${BB_DIR}"
fi
echo "Cleaned up '${BB_DIR}'."
if [ -f "${BB_PKG}" ]; then
unlink "${BB_PKG}"
fi
echo "Removed '${BB_PKG}'."
exit 0
;;
*)
echo "Error, unknown build option given"
usage
exit 1
;;
esac
exit 0