CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
orangepi-xunlong

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: orangepi-xunlong/orangepi-build
Path: blob/next/external/extensions/flash-kernel.sh
Views: 3956
1
# This runs *after* user_config. Don't change anything not coming from other variables or meant to be configured by the user.
2
function extension_prepare_config__prepare_flash_kernel() {
3
# Configuration defaults, or lack thereof.
4
export FK__TOOL_PACKAGE="${FK__TOOL_PACKAGE:-flash-kernel}"
5
export FK__PUBLISHED_KERNEL_VERSION="${FK__PUBLISHED_KERNEL_VERSION:-undefined-flash-kernel-version}"
6
export FK__EXTRA_PACKAGES="${FK__EXTRA_PACKAGES:-undefined-flash-kernel-kernel-package}"
7
export FK__KERNEL_PACKAGES="${FK__KERNEL_PACKAGES:-}"
8
export FK__MACHINE_MODEL="${FK__MACHINE_MODEL:-Undefined Flash-Kernel Machine}"
9
10
# Override certain variables. A case of "this extension knows better and modifies user configurable stuff".
11
export BOOTCONFIG="none" # To try and convince lib/ to not build or install u-boot.
12
unset BOOTSOURCE # To try and convince lib/ to not build or install u-boot.
13
export UEFISIZE=256 # in MiB. Not really UEFI, but partition layout is the same.
14
export BOOTSIZE=0 # No separate /boot, flash-kernel will "flash" the kernel+initrd to the firmware part.
15
export UEFI_MOUNT_POINT="/boot/firmware" # mount uefi partition at /boot/firmware
16
export CLOUD_INIT_CONFIG_LOCATION="/boot/firmware" # use /boot/firmware for cloud-init as well
17
export VER="${FK__PUBLISHED_KERNEL_VERSION}" # For the VERSION
18
export EXTRA_BSP_NAME="${EXTRA_BSP_NAME}-fk${FK__PUBLISHED_KERNEL_VERSION}" # Unique bsp name.
19
echo "-- starting" >"${DEST}"/"${LOG_SUBPATH}"/flash-kernel.log # Zero out the log for this extension.
20
}
21
22
function post_install_kernel_debs__install_kernel_and_flash_packages() {
23
export INSTALL_ARMBIAN_FIRMWARE="no" # Disable Armbian-firmware install, which would happen after this method.
24
25
if [[ "${FK__EXTRA_PACKAGES}" != "" ]]; then
26
display_alert "Installing flash-kernel extra packages" "${FK__EXTRA_PACKAGES}"
27
echo "-- install extra pkgs" >>"${DEST}"/"${LOG_SUBPATH}"/flash-kernel.log
28
chroot "${SDCARD}" /bin/bash -c "DEBIAN_FRONTEND=noninteractive apt-get ${APT_EXTRA_DIST_PARAMS} -yqq --no-install-recommends install ${FK__EXTRA_PACKAGES}" >>"${DEST}"/"${LOG_SUBPATH}"/flash-kernel.log || {
29
display_alert "Failed to install flash-kernel's extra packages." "${EXTENSION}" "err"
30
exit 28
31
}
32
fi
33
34
if [[ "${FK__KERNEL_PACKAGES}" != "" ]]; then
35
display_alert "Installing flash-kernel kernel packages" "${FK__KERNEL_PACKAGES}"
36
echo "-- install kernel pkgs" >>"${DEST}"/"${LOG_SUBPATH}"/flash-kernel.log
37
chroot "${SDCARD}" /bin/bash -c "DEBIAN_FRONTEND=noninteractive apt-get ${APT_EXTRA_DIST_PARAMS} -yqq --no-install-recommends install ${FK__KERNEL_PACKAGES}" >>"${DEST}"/"${LOG_SUBPATH}"/flash-kernel.log || {
38
display_alert "Failed to install flash-kernel's kernel packages." "${EXTENSION}" "err"
39
exit 28
40
}
41
fi
42
43
display_alert "Installing flash-kernel package" "${FK__TOOL_PACKAGE}"
44
# Create a fake /sys/firmware/efi directory so that flash-kernel does not try to do anything when installed
45
# @TODO: this might or not work after flash-kernel 3.104 or later
46
umount "${SDCARD}"/sys
47
mkdir -p "${SDCARD}"/sys/firmware/efi
48
49
echo "-- install flash-kernel package" >>"${DEST}"/"${LOG_SUBPATH}"/flash-kernel.log
50
chroot "${SDCARD}" /bin/bash -c "DEBIAN_FRONTEND=noninteractive apt-get ${APT_EXTRA_DIST_PARAMS} -yqq --no-install-recommends install ${FK__TOOL_PACKAGE}" >>"${DEST}"/"${LOG_SUBPATH}"/flash-kernel.log || {
51
display_alert "Failed to install flash-kernel package." "${EXTENSION}" "err"
52
exit 28
53
}
54
55
# Remove fake /sys/firmware (/efi) directory
56
rm -rf "${SDCARD}"/sys/firmware
57
}
58
59
# @TODO: extract u-boot into an extension, so that core bsps don't have this stuff in there to begin with.
60
# @TODO: this code is duplicated in grub.sh extension, so another reason to refactor the root of the evil
61
post_family_tweaks_bsp__remove_uboot_flash_kernel() {
62
display_alert "Removing uboot from BSP" "${EXTENSION}" "info"
63
# Simply remove everything with 'uboot' or 'u-boot' in their filenames from the BSP package.
64
# shellcheck disable=SC2154 # $destination is the target dir of the bsp building function
65
find "$destination" -type f | grep -e "uboot" -e "u-boot" | xargs rm
66
}
67
68
pre_umount_final_image__remove_uboot_initramfs_hook_flash_kernel() {
69
# even if BSP still contained this (cached .deb), make sure by removing from ${MOUNT}
70
[[ -f "$MOUNT"/etc/initramfs/post-update.d/99-uboot ]] && rm -v "$MOUNT"/etc/initramfs/post-update.d/99-uboot
71
}
72
73
function pre_update_initramfs__setup_flash_kernel() {
74
local chroot_target=$MOUNT
75
cp /usr/bin/"$QEMU_BINARY" "$chroot_target"/usr/bin/
76
mount_chroot "$chroot_target/" # this already handles /boot/firmware which is required for it to work.
77
# hack, umount the chroot's /sys, otherwise flash-kernel tries to EFI flash due to the build host (!) being EFI
78
umount "$chroot_target/sys"
79
80
echo "-- flash-kernel disabling hooks" >>"${DEST}"/"${LOG_SUBPATH}"/flash-kernel.log
81
chroot "$chroot_target" /bin/bash -c "chmod -v -x /etc/kernel/postinst.d/initramfs-tools" >>"${DEST}"/"${LOG_SUBPATH}"/flash-kernel.log 2>&1
82
chroot "$chroot_target" /bin/bash -c "chmod -v -x /etc/initramfs/post-update.d/flash-kernel" >>"${DEST}"/"${LOG_SUBPATH}"/flash-kernel.log 2>&1
83
84
export FIRMWARE_DIR="${MOUNT}"/boot/firmware
85
call_extension_method "pre_initramfs_flash_kernel" <<-'PRE_INITRAMFS_FLASH_KERNEL'
86
*prepare to update-initramfs before flashing kernel via flash_kernel*
87
A good spot to write firmware config to ${FIRMWARE_DIR} (/boot/firmware) before flash-kernel actually runs.
88
PRE_INITRAMFS_FLASH_KERNEL
89
90
local update_initramfs_cmd="update-initramfs -c -k all"
91
display_alert "Updating flash-kernel initramfs..." "$update_initramfs_cmd" ""
92
echo "-- flash-kernel initramfs" >>"${DEST}"/"${LOG_SUBPATH}"/flash-kernel.log
93
chroot "$chroot_target" /bin/bash -c "$update_initramfs_cmd" >>"${DEST}"/"${LOG_SUBPATH}"/flash-kernel.log 2>&1 || {
94
display_alert "Failed to run '$update_initramfs_cmd'" "Check ${DEST}/"${LOG_SUBPATH}"/flash-kernel.log" "err"
95
exit 29
96
}
97
98
call_extension_method "pre_flash_kernel" <<-'PRE_FLASH_KERNEL'
99
*run before running flash-kernel*
100
Each board might need different stuff for flash-kernel to work. Implement it here.
101
Write to `${MOUNT}`, eg: `"${MOUNT}"/etc/flash-kernel`
102
PRE_FLASH_KERNEL
103
104
local flash_kernel_cmd="flash-kernel --machine '${FK__MACHINE_MODEL}'"
105
display_alert "flash-kernel" "${FK__MACHINE_MODEL}" "info"
106
echo "-- flash-kernel itself" >>"${DEST}"/"${LOG_SUBPATH}"/flash-kernel.log
107
chroot "$chroot_target" /bin/bash -c "${flash_kernel_cmd}" >>"${DEST}"/"${LOG_SUBPATH}"/flash-kernel.log 2>&1 || {
108
display_alert "Failed to run '${flash_kernel_cmd}'" "Check ${DEST}/"${LOG_SUBPATH}"/flash-kernel.log" "err"
109
exit 29
110
}
111
112
display_alert "Re-enabling" "initramfs-tools/flash-kernel hook for kernel"
113
echo "-- flash-kernel re-enabling hooks" >>"${DEST}"/"${LOG_SUBPATH}"/flash-kernel.log
114
chroot "$chroot_target" /bin/bash -c "chmod -v +x /etc/kernel/postinst.d/initramfs-tools" >>"${DEST}"/"${LOG_SUBPATH}"/flash-kernel.log 2>&1
115
chroot "$chroot_target" /bin/bash -c "chmod -v +x /etc/initramfs/post-update.d/flash-kernel" >>"${DEST}"/"${LOG_SUBPATH}"/flash-kernel.log 2>&1
116
117
umount_chroot "$chroot_target/"
118
rm "$chroot_target"/usr/bin/"$QEMU_BINARY"
119
120
display_alert "Disabling Armbian-core update_initramfs, was already done above." "${EXTENSION}"
121
unset KERNELSOURCE # ugly. sorry. we'll have better mechanism for this soon. this is tested at lib/debootstrap.sh:844
122
}
123
124