#!/bin/bash # grub-install — Install GRUB on a device (Red Bear OS wrapper) set -euo pipefail PROG="$(basename "$0")" TARGET="" EFI_DIRECTORY="" BOOTLOADER_ID="REDBEAR" BOOT_DIRECTORY="" DISK_IMAGE="" MODULES="" REMOVABLE=0 VERBOSE=0 INSTALL_DEVICE="" usage() { cat <&2 echo "Try '$PROG --help' for more information." >&2 exit 1 ;; *) INSTALL_DEVICE="$1" shift ;; esac done [ "$VERBOSE" -eq 1 ] && echo "$PROG: target=$TARGET efi_directory=$EFI_DIRECTORY bootloader_id=$BOOTLOADER_ID removable=$REMOVABLE disk_image=$DISK_IMAGE device=$INSTALL_DEVICE" # Validate target if [ -n "$TARGET" ] && [ "$TARGET" != "x86_64-efi" ]; then echo "$PROG: unsupported target '$TARGET' (only x86_64-efi is supported on Red Bear OS)" >&2 exit 1 fi SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" REPO_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)" FAT_TOOL="$SCRIPT_DIR/fat_tool.py" # Mode 1: Disk image installation (Red Bear OS build workflow) if [ -n "$DISK_IMAGE" ]; then if [ ! -f "$DISK_IMAGE" ]; then echo "$PROG: disk image not found: $DISK_IMAGE" >&2 exit 1 fi if [ ! -f "$FAT_TOOL" ]; then echo "$PROG: fat_tool.py not found at $FAT_TOOL" >&2 exit 1 fi # Find GRUB EFI binary GRUB_EFI="" for f in "$REPO_ROOT/local/recipes/core/grub/target/x86_64-unknown-redox/stage/usr/lib/boot/grub.efi" \ "$REPO_ROOT/recipes/core/grub/target/x86_64-unknown-redox/stage/usr/lib/boot/grub.efi" \ "$REPO_ROOT/repo/x86_64-unknown-redox/grub/root/usr/lib/boot/grub.efi"; do if [ -f "$f" ]; then GRUB_EFI="$f" break fi done if [ -z "$GRUB_EFI" ]; then echo "$PROG: grub.efi not found. Build the GRUB recipe first: make r.grub" >&2 exit 1 fi [ "$VERBOSE" -eq 1 ] && echo "$PROG: using grub.efi: $GRUB_EFI" # Find Redox bootloader REDBEAR_EFI="" for f in "$REPO_ROOT/recipes/core/bootloader/target/x86_64-unknown-redox/stage/usr/lib/boot/bootloader.efi" \ "$REPO_ROOT/repo/x86_64-unknown-redox/bootloader/root/usr/lib/boot/bootloader.efi"; do if [ -f "$f" ]; then REDBEAR_EFI="$f" break fi done if [ -z "$REDBEAR_EFI" ]; then echo "$PROG: bootloader.efi not found. Build the bootloader recipe first." >&2 exit 1 fi [ "$VERBOSE" -eq 1 ] && echo "$PROG: using bootloader.efi: $REDBEAR_EFI" # Find grub.cfg GRUB_CFG="$REPO_ROOT/local/recipes/core/grub/grub.cfg" if [ ! -f "$GRUB_CFG" ]; then echo "$PROG: grub.cfg not found at $GRUB_CFG" >&2 exit 1 fi # ESP at LBA 2048 (standard Redox GPT layout) ESP_OFFSET=$((2048 * 512)) # GRUB always goes to EFI/BOOT — this matches the installer-native path # (with_whole_disk creates EFI/BOOT/BOOTX64.EFI for GRUB). # The --bootloader-id flag is accepted for Linux compatibility but # on Red Bear OS the UEFI firmware expects the standard removable path. BOOT_PATH="EFI/BOOT" "$FAT_TOOL" mkdir "$DISK_IMAGE" "$ESP_OFFSET" "EFI" "$FAT_TOOL" mkdir "$DISK_IMAGE" "$ESP_OFFSET" "$BOOT_PATH" "$FAT_TOOL" mkdir "$DISK_IMAGE" "$ESP_OFFSET" "EFI/REDBEAR" "$FAT_TOOL" cp-in "$DISK_IMAGE" "$ESP_OFFSET" "$GRUB_EFI" "$BOOT_PATH/BOOTX64.EFI" [ "$VERBOSE" -eq 1 ] && echo "$PROG: installed GRUB to $BOOT_PATH/BOOTX64.EFI" "$FAT_TOOL" cp-in "$DISK_IMAGE" "$ESP_OFFSET" "$GRUB_CFG" "$BOOT_PATH/grub.cfg" [ "$VERBOSE" -eq 1 ] && echo "$PROG: installed grub.cfg to $BOOT_PATH/grub.cfg" "$FAT_TOOL" cp-in "$DISK_IMAGE" "$ESP_OFFSET" "$REDBEAR_EFI" "EFI/REDBEAR/redbear.efi" [ "$VERBOSE" -eq 1 ] && echo "$PROG: installed Redox bootloader to EFI/REDBEAR/redbear.efi" echo "Installation finished. No error reported." exit 0 fi if [ -n "$INSTALL_DEVICE" ]; then echo "$PROG: block device installation is not supported by this wrapper." >&2 echo "$PROG: For Red Bear OS, use one of:" >&2 echo "$PROG: make all CONFIG_NAME=redbear-full-grub" >&2 echo "$PROG: redox_installer -c redbear-full-grub.toml $INSTALL_DEVICE" >&2 exit 1 fi if [ -z "$DISK_IMAGE" ] && [ -z "$INSTALL_DEVICE" ]; then echo "$PROG: no installation device specified." >&2 echo "Try '$PROG --help' for more information." >&2 exit 1 fi