enable_extension "flash-kernel"
export LINUXFAMILY=bcm2711
export ARCH=arm64
export UEFI_FS_LABEL="RPICFG"               # Windows/Mac users will see this if they mount the SD card. Configurable, but should be uppercase always
export SKIP_BOOTSPLASH="yes"                # video is init-ed before us
export KERNELDIR='linux-rpi'                # Avoid sharing a source tree with others, until we know it's safe.
export FK__PUBLISHED_KERNEL_VERSION="raspi" # flash kernel (FK) configuration
export FK__KERNEL_PACKAGES=""
export RASPI_ROOT_FS_LABEL="armbian"
export CPUMIN=500000
export CPUMAX=2000000
export GOVERNOR=ondemand

case "${BRANCH}" in

legacy)
	export RASPI_DISTRO_KERNEL=yes # This will cause board to include distro's prebuilt kernel, not from source
	;;

current)
        export RASPI_DISTRO_KERNEL=no
	export KERNELSOURCE='https://github.com/raspberrypi/linux'
        export KERNELBRANCH="branch:rpi-5.15.y"
        export KERNELPATCHDIR="${LINUXFAMILY}-${BRANCH}"
        export LINUXCONFIG="linux-${LINUXFAMILY}-${BRANCH}"
	;;
edge)
	export RASPI_DISTRO_KERNEL=no
        export KERNELSOURCE='https://github.com/raspberrypi/linux'
        export KERNELBRANCH="branch:rpi-5.16.y"
	export KERNELPATCHDIR="${LINUXFAMILY}-${BRANCH}"
	export LINUXCONFIG="linux-${LINUXFAMILY}-${BRANCH}"
	;;
esac

# Add a label to the root partition - this is common, should refactor into a separate segment
prepare_partitions_custom__add_rootfs_raspi_label_to_mkfs() {
	display_alert "raspi rootfs label ${RASPI_ROOT_FS_LABEL}" "boot with root=LABEL=${RASPI_ROOT_FS_LABEL}" "info"
	mkopts[ext4]="-L ${RASPI_ROOT_FS_LABEL} ${mkopts[ext4]}"
}

pre_initramfs_flash_kernel__write_raspi_cmdline() {
	cat <<-EOD >"${FIRMWARE_DIR}/cmdline.txt"
		root=LABEL=${RASPI_ROOT_FS_LABEL} rootfstype=ext4 elevator=deadline rootwait fixrtc cgroup_enable=memory cgroup_memory=1 console=tty1 logo.nologo loglevel=1
	EOD
}

pre_flash_kernel__symlink_dtb_and_kernel() {
	if [[ "${RASPI_DISTRO_KERNEL}" != "yes" ]]; then # and firmware.
		display_alert "Preparing DTBs and Kernel..." "bcm2711" "info"
		mkdir -p "${MOUNT}"/etc/flash-kernel/dtbs

		cat <<- EOD >> "${MOUNT}"/etc/flash-kernel/db
			# Armbian kernels don't have a 'flavour'. Ignore flavors for all rpi revisions.
			Machine: Raspberry Pi *
			Kernel-Flavors: any
		EOD

		## @TODO: rpardini: a horrible hack. I'll sort this out together with overlays, later.
		local oneDTB dtbBase
		for oneDTB in "${MOUNT}"/boot/dtb/broadcom/*.dtb; do
			dtbBase=$(basename "${oneDTB}")
			cp "${MOUNT}"/boot/dtb/broadcom/"${dtbBase}" "${MOUNT}"/etc/flash-kernel/dtbs/"${dtbBase}"
		done
		
		rm -rf "${MOUNT}"/boot/dtb* || true

		# @TODO: rpardini: packaging could maybe already use the correct names? I can't figure out how.
		ln -s ./Image "${MOUNT}"/boot/vmlinuz
		ln -s ./Image "${MOUNT}"/boot/vmlinuz-
	fi
}

extension_prepare_config__prepare_rpi_flash_kernel() {
	display_alert "Preparing bcm2711" "${RELEASE}, distro kernel?: ${RASPI_DISTRO_KERNEL}" "info"
	export RASPI_DISTRO_KERNEL="${RASPI_DISTRO_KERNEL:-no}" # Include a distro-built kernel?
	export SERIALCON="${RASPI_SERIALCON:-tty1}"             # HDMI etc, not serial. most people don't have UART on rpi
	local usable_releases="focal|hirsute|impish|jammy"

	if [[ "$RELEASE" =~ ^(${usable_releases})$ ]]; then
		export FK__EXTRA_PACKAGES="rpi-eeprom linux-firmware linux-firmware-raspi2 libraspberrypi-bin cloud-initramfs-growroot"

		if [[ "$RELEASE" =~ ^(hirsute|impish|jammy)$ ]]; then # Add raspi-config for those releases that have it; it might be useful.
			export FK__EXTRA_PACKAGES="${FK__EXTRA_PACKAGES} raspi-config"
		fi

		if [[ "${RASPI_DISTRO_KERNEL}" == "yes" ]]; then # and firmware.
			unset KERNELSOURCE # Make sure Armbian will not try to compile from source.
			export FK__KERNEL_PACKAGES="${FK__KERNEL_PACKAGES} linux-tools-raspi linux-raspi linux-image-raspi "
			# Ubuntu Impish+ split the kernel modules, add the extra ones too.
			if [[ "$RELEASE" =~ ^(impish|jammy)$ ]]; then
				export FK__KERNEL_PACKAGES="${FK__KERNEL_PACKAGES} linux-modules-extra-raspi"
			fi
		fi
	else
		if [[ "${KERNEL_ONLY}" != "yes" ]]; then
			display_alert "Can't use release for ${BOARD}. Try: ${usable_releases}" "${RELEASE}" "err"
			exit 27
		fi
	fi
}
