#!/bin/bash # BBeOS Kernel Build Script for MSM8960 # Builds Linux kernel for BlackBerry Classic Q20 set -e SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PROJECT_ROOT="$(dirname "$SCRIPT_DIR")" KERNEL_DIR="$PROJECT_ROOT/vendor/kernel-source" BUILD_DIR="$PROJECT_ROOT/build/kernel" TOOLCHAIN_DIR="$PROJECT_ROOT/toolchain" # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # Logging functions log_info() { echo -e "${BLUE}[INFO]${NC} $1" } log_success() { echo -e "${GREEN}[SUCCESS]${NC} $1" } log_warning() { echo -e "${YELLOW}[WARNING]${NC} $1" } log_error() { echo -e "${RED}[ERROR]${NC} $1" } # Configuration KERNEL_VERSION="6.1" KERNEL_SOURCE="linux-$KERNEL_VERSION" KERNEL_URL="https://cdn.kernel.org/pub/linux/kernel/v6.x/$KERNEL_SOURCE.tar.xz" TOOLCHAIN_URL="https://developer.arm.com/-/media/Files/downloads/gnu-a/10.3-2021.07/binrel/gcc-arm-10.3-2021.07-x86_64-arm-none-linux-gnueabihf.tar.xz" ARCH="arm" CROSS_COMPILE="arm-none-linux-gnueabihf-" DEFCONFIG="msm8960_defconfig" # Check dependencies check_dependencies() { log_info "Checking build dependencies..." local deps=("wget" "tar" "make" "gcc" "g++" "flex" "bison" "libssl-dev" "libncurses5-dev" "bc") local missing_deps=() for dep in "${deps[@]}"; do if ! command -v "$dep" &> /dev/null; then missing_deps+=("$dep") fi done if [ ${#missing_deps[@]} -ne 0 ]; then log_error "Missing dependencies: ${missing_deps[*]}" log_info "Please install the missing packages:" log_info "sudo apt-get install ${missing_deps[*]}" exit 1 fi log_success "All dependencies found" } # Setup build environment setup_build_env() { log_info "Setting up build environment..." mkdir -p "$BUILD_DIR" mkdir -p "$TOOLCHAIN_DIR" # Set environment variables export ARCH="$ARCH" export CROSS_COMPILE="$CROSS_COMPILE" export PATH="$TOOLCHAIN_DIR/gcc-arm-10.3-2021.07-x86_64-arm-none-linux-gnueabihf/bin:$PATH" log_success "Build environment configured" } # Download and setup toolchain setup_toolchain() { log_info "Setting up ARM toolchain..." local toolchain_file="gcc-arm-10.3-2021.07-x86_64-arm-none-linux-gnueabihf.tar.xz" local toolchain_path="$TOOLCHAIN_DIR/$toolchain_file" if [ ! -d "$TOOLCHAIN_DIR/gcc-arm-10.3-2021.07-x86_64-arm-none-linux-gnueabihf" ]; then if [ ! -f "$toolchain_path" ]; then log_info "Downloading ARM toolchain..." wget -O "$toolchain_path" "$TOOLCHAIN_URL" fi log_info "Extracting toolchain..." tar -xf "$toolchain_path" -C "$TOOLCHAIN_DIR" fi log_success "Toolchain setup completed" } # Download kernel source download_kernel() { log_info "Setting up kernel source..." local kernel_file="$KERNEL_SOURCE.tar.xz" local kernel_path="$BUILD_DIR/$kernel_file" if [ ! -d "$BUILD_DIR/$KERNEL_SOURCE" ]; then if [ ! -f "$kernel_path" ]; then log_info "Downloading kernel source..." wget -O "$kernel_path" "$KERNEL_URL" fi log_info "Extracting kernel source..." tar -xf "$kernel_path" -C "$BUILD_DIR" fi log_success "Kernel source setup completed" } # Apply patches and configuration setup_kernel_config() { log_info "Setting up kernel configuration..." local kernel_build_dir="$BUILD_DIR/$KERNEL_SOURCE" # Copy our defconfig cp "$KERNEL_DIR/msm8960-defconfig" "$kernel_build_dir/arch/arm/configs/$DEFCONFIG" # Copy device tree cp "$KERNEL_DIR/q20.dts" "$kernel_build_dir/arch/arm/boot/dts/" # Apply any patches if [ -d "$KERNEL_DIR/patches" ]; then log_info "Applying kernel patches..." for patch in "$KERNEL_DIR/patches"/*.patch; do if [ -f "$patch" ]; then log_info "Applying patch: $(basename "$patch")" patch -d "$kernel_build_dir" -p1 < "$patch" fi done fi log_success "Kernel configuration setup completed" } # Build kernel build_kernel() { log_info "Building kernel..." local kernel_build_dir="$BUILD_DIR/$KERNEL_SOURCE" cd "$kernel_build_dir" # Clean previous build make clean # Configure kernel make "$DEFCONFIG" # Build kernel make -j$(nproc) zImage dtbs modules # Build device tree blob dtc -I dts -O dtb -o "$BUILD_DIR/q20.dtb" "$KERNEL_DIR/q20.dts" log_success "Kernel build completed" } # Create boot image create_boot_image() { log_info "Creating boot image..." local kernel_build_dir="$BUILD_DIR/$KERNEL_SOURCE" local boot_dir="$BUILD_DIR/boot" mkdir -p "$boot_dir" # Copy kernel image cp "$kernel_build_dir/arch/arm/boot/zImage" "$boot_dir/" # Copy device tree cp "$BUILD_DIR/q20.dtb" "$boot_dir/" # Create initramfs (minimal) create_initramfs # Create boot.img (Android format) create_android_boot_image log_success "Boot image created" } # Create minimal initramfs create_initramfs() { log_info "Creating minimal initramfs..." local initramfs_dir="$BUILD_DIR/initramfs" local initramfs_file="$BUILD_DIR/initramfs.cpio.gz" mkdir -p "$initramfs_dir" # Create basic initramfs structure cat > "$initramfs_dir/init" << 'EOF' #!/bin/sh mount -t proc none /proc mount -t sysfs none /sys mount -t devtmpfs none /dev exec /bin/sh EOF chmod +x "$initramfs_dir/init" # Create initramfs archive cd "$initramfs_dir" find . | cpio -o -H newc | gzip > "$initramfs_file" log_success "Initramfs created" } # Create Android boot image create_android_boot_image() { log_info "Creating Android boot image..." local boot_dir="$BUILD_DIR/boot" local boot_img="$BUILD_DIR/boot.img" # This is a placeholder - you'd need mkbootimg tool log_warning "Android boot image creation requires mkbootimg tool" log_info "Manual steps required:" log_info "1. Install mkbootimg tool" log_info "2. Run: mkbootimg --kernel $boot_dir/zImage --dtb $boot_dir/q20.dtb --ramdisk $BUILD_DIR/initramfs.cpio.gz --output $boot_img" } # Main build process main() { log_info "Starting BBeOS kernel build for MSM8960..." check_dependencies setup_build_env setup_toolchain download_kernel setup_kernel_config build_kernel create_boot_image log_success "Kernel build completed successfully!" log_info "Build artifacts:" log_info " Kernel: $BUILD_DIR/$KERNEL_SOURCE/arch/arm/boot/zImage" log_info " Device Tree: $BUILD_DIR/q20.dtb" log_info " Initramfs: $BUILD_DIR/initramfs.cpio.gz" log_info " Boot Directory: $BUILD_DIR/boot/" } # Run main function main "$@"