#!/bin/bash # BBeOS Packaging System Build Script # BlackBerry Classic Q20 Complete System Image Creation set -e # Configuration BBEOS_VERSION="1.0.0" BBEOS_BUILD_DATE=$(date +%Y%m%d) BBEOS_BUILD_TIME=$(date +%H%M%S) BBEOS_ARCH="armv7" BBEOS_TARGET="blackberry-q20" # Directories SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PROJECT_ROOT="$(dirname "$SCRIPT_DIR")" PACKAGING_DIR="$PROJECT_ROOT/packaging" BUILD_DIR="$PROJECT_ROOT/build" OUTPUT_DIR="$PROJECT_ROOT/output" TEMP_DIR="$PROJECT_ROOT/temp" # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color # 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" } # Cleanup function cleanup() { log_info "Cleaning up temporary files..." rm -rf "$TEMP_DIR" } # Error handling trap cleanup EXIT trap 'log_error "Build failed at line $LINENO"; exit 1' ERR # Check dependencies check_dependencies() { log_info "Checking build dependencies..." local deps=("gcc" "make" "tar" "gzip" "openssl" "curl") 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 and try again." exit 1 fi # Check for ARM cross-compiler if ! command -v arm-linux-gnueabihf-gcc &> /dev/null; then log_warning "ARM cross-compiler not found. Building native versions only." export CROSS_COMPILE="" else log_success "ARM cross-compiler found" export CROSS_COMPILE="arm-linux-gnueabihf-" fi log_success "Dependencies check passed" } # Create build directories create_directories() { log_info "Creating build directories..." mkdir -p "$BUILD_DIR" mkdir -p "$OUTPUT_DIR" mkdir -p "$TEMP_DIR" mkdir -p "$TEMP_DIR/system" mkdir -p "$TEMP_DIR/boot" mkdir -p "$TEMP_DIR/keys" mkdir -p "$TEMP_DIR/updates" log_success "Build directories created" } # Build packaging tools build_packaging_tools() { log_info "Building packaging tools..." cd "$PACKAGING_DIR" # Check dependencies for packaging tools if ! pkg-config --exists libssl; then log_error "OpenSSL development libraries not found" log_info "Install with: sudo apt-get install libssl-dev" exit 1 fi if ! pkg-config --exists libcurl; then log_error "libcurl development libraries not found" log_info "Install with: sudo apt-get install libcurl4-openssl-dev" exit 1 fi # Build tools make clean make all if [ -n "$CROSS_COMPILE" ]; then log_info "Building ARM cross-compiled versions..." make arm-all fi log_success "Packaging tools built successfully" } # Generate secure boot keys generate_keys() { log_info "Generating secure boot keys..." cd "$TEMP_DIR/keys" if [ -f "bbeos_private_key.pem" ] && [ -f "bbeos_public_key.pem" ]; then log_warning "Keys already exist, skipping generation" else "$PACKAGING_DIR/secure-boot" generate . log_success "Secure boot keys generated" fi } # Build kernel and rootfs build_system_components() { log_info "Building system components..." cd "$PROJECT_ROOT" # Build kernel if not already built if [ ! -f "kernel-source/arch/arm/boot/zImage" ]; then log_info "Building kernel..." ./scripts/build-kernel-minimal.sh else log_info "Kernel already built, skipping" fi # Build rootfs if not already built if [ ! -f "initramfs.img" ]; then log_info "Building rootfs..." ./scripts/build-rootfs.sh else log_info "Rootfs already built, skipping" fi # Copy components to temp directory cp kernel-source/arch/arm/boot/zImage "$TEMP_DIR/boot/" cp initramfs.img "$TEMP_DIR/boot/" cp -r rootfs/* "$TEMP_DIR/system/" 2>/dev/null || true log_success "System components built" } # Build UI components build_ui_components() { log_info "Building UI components..." cd "$PROJECT_ROOT" # Build UI if available if [ -d "ui" ]; then ./scripts/build-ui.sh cp -r ui/build/* "$TEMP_DIR/system/usr/bin/" 2>/dev/null || true else log_warning "UI components not found, skipping" fi log_success "UI components built" } # Build telephony components build_telephony_components() { log_info "Building telephony components..." cd "$PROJECT_ROOT" # Build telephony if available if [ -d "telephony" ]; then ./scripts/build-telephony.sh cp -r telephony/build/* "$TEMP_DIR/system/usr/bin/" 2>/dev/null || true else log_warning "Telephony components not found, skipping" fi log_success "Telephony components built" } # Sign system components sign_components() { log_info "Signing system components..." cd "$TEMP_DIR" local private_key="$TEMP_DIR/keys/bbeos_private_key.pem" # Sign kernel if [ -f "boot/zImage" ]; then "$PACKAGING_DIR/secure-boot" sign boot/zImage boot/zImage.sig "$private_key" log_info "Kernel signed" fi # Sign initramfs if [ -f "boot/initramfs.img" ]; then "$PACKAGING_DIR/secure-boot" sign boot/initramfs.img boot/initramfs.img.sig "$private_key" log_info "Initramfs signed" fi # Sign system files if [ -d "system" ]; then find system -type f -exec "$PACKAGING_DIR/secure-boot" sign {} {}.sig "$private_key" \; log_info "System files signed" fi log_success "All components signed" } # Create system image create_system_image() { log_info "Creating system image..." cd "$TEMP_DIR" # Create system image "$PACKAGING_DIR/image-builder" "$OUTPUT_DIR/bbeos-system-$BBEOS_VERSION.img" if [ $? -eq 0 ]; then log_success "System image created: $OUTPUT_DIR/bbeos-system-$BBEOS_VERSION.img" else log_error "Failed to create system image" exit 1 fi } # Create update package create_update_package() { log_info "Creating update package..." cd "$TEMP_DIR" # Create update manifest cat > update-manifest.txt << EOF BBEOSMAN 1.0.0 $(find system boot -type f | wc -l) $(du -sb system boot | awk '{sum += $1} END {print sum}') BBeOS $BBEOS_VERSION Update - System improvements - Bug fixes - Security updates EOF # Create update package tar -czf "$OUTPUT_DIR/bbeos-update-$BBEOS_VERSION.pkg" \ update-manifest.txt system/ boot/ keys/bbeos_public_key.pem log_success "Update package created: $OUTPUT_DIR/bbeos-update-$BBEOS_VERSION.pkg" } # Create flashable image create_flashable_image() { log_info "Creating flashable image..." cd "$OUTPUT_DIR" # Create flash script cat > flash-bbeos.sh << 'EOF' #!/bin/bash # BBeOS Flash Script # BlackBerry Classic Q20 System Flashing set -e if [ "$EUID" -ne 0 ]; then echo "Please run as root (use sudo)" exit 1 fi if [ $# -ne 1 ]; then echo "Usage: $0 " echo "Example: $0 /dev/sdb" exit 1 fi DEVICE="$1" IMAGE="bbeos-system-1.0.0.img" if [ ! -f "$IMAGE" ]; then echo "Error: System image not found: $IMAGE" exit 1 fi echo "WARNING: This will overwrite all data on $DEVICE" echo "Make sure you have selected the correct device!" read -p "Continue? (y/N): " -n 1 -r echo if [[ ! $REPLY =~ ^[Yy]$ ]]; then echo "Aborted" exit 1 fi echo "Flashing BBeOS to $DEVICE..." dd if="$IMAGE" of="$DEVICE" bs=4M status=progress echo "Flashing completed successfully!" echo "You can now boot your BlackBerry Classic Q20 with BBeOS" EOF chmod +x flash-bbeos.sh log_success "Flash script created: $OUTPUT_DIR/flash-bbeos.sh" } # Create documentation create_documentation() { log_info "Creating documentation..." cd "$OUTPUT_DIR" # Create README cat > README.md << EOF # BBeOS System Image ## Version: $BBEOS_VERSION ## Build Date: $BBEOS_BUILD_DATE ## Target: $BBEOS_TARGET ## Files - \`bbeos-system-$BBEOS_VERSION.img\` - Complete system image - \`bbeos-update-$BBEOS_VERSION.pkg\` - OTA update package - \`flash-bbeos.sh\` - Flashing script - \`keys/\` - Secure boot keys ## Installation ### Method 1: Direct Flash (Recommended) \`\`\`bash sudo ./flash-bbeos.sh /dev/sdX \`\`\` ### Method 2: Fastboot \`\`\`bash fastboot flash system bbeos-system-$BBEOS_VERSION.img fastboot reboot \`\`\` ## Update To update an existing BBeOS installation: \`\`\`bash bbeos-ota-updater update https://updates.bbeos.org/bbeos-update-$BBEOS_VERSION.pkg \`\`\` ## Security This system image includes secure boot verification. The public key is included in the image for verification purposes. ## Support For support and documentation, visit: https://github.com/bbeos/bbeos ## License BBeOS is licensed under the GPL v3 License. EOF log_success "Documentation created: $OUTPUT_DIR/README.md" } # Main build process main() { log_info "Starting BBeOS packaging build..." log_info "Version: $BBEOS_VERSION" log_info "Target: $BBEOS_TARGET" log_info "Architecture: $BBEOS_ARCH" # Check dependencies check_dependencies # Create directories create_directories # Build packaging tools build_packaging_tools # Generate keys generate_keys # Build system components build_system_components # Build UI components build_ui_components # Build telephony components build_telephony_components # Sign components sign_components # Create system image create_system_image # Create update package create_update_package # Create flashable image create_flashable_image # Create documentation create_documentation # Final summary log_success "BBeOS packaging build completed successfully!" log_info "Output files:" log_info " - $OUTPUT_DIR/bbeos-system-$BBEOS_VERSION.img" log_info " - $OUTPUT_DIR/bbeos-update-$BBEOS_VERSION.pkg" log_info " - $OUTPUT_DIR/flash-bbeos.sh" log_info " - $OUTPUT_DIR/README.md" # Show file sizes echo log_info "File sizes:" ls -lh "$OUTPUT_DIR"/*.img "$OUTPUT_DIR"/*.pkg 2>/dev/null || true } # Run main function main "$@"