/ scripts / build-ovmf-tcp.sh
build-ovmf-tcp.sh
  1  #!/bin/bash
  2  #
  3  # Build OVMF with TCP4 protocol support for testing kabashira-efi
  4  #
  5  # Prerequisites (Debian/Ubuntu):
  6  #   sudo apt install build-essential uuid-dev iasl nasm python3 python3-distutils
  7  #
  8  # Prerequisites (Arch):
  9  #   sudo pacman -S base-devel util-linux-libs iasl nasm python
 10  #
 11  # Prerequisites (Fedora):
 12  #   sudo dnf install gcc gcc-c++ make libuuid-devel iasl nasm python3
 13  #
 14  
 15  set -e
 16  
 17  SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
 18  PROJECT_DIR="$(dirname "$SCRIPT_DIR")"
 19  EDK2_DIR="${EDK2_DIR:-$PROJECT_DIR/edk2}"
 20  BUILD_DIR="$PROJECT_DIR/ovmf-build"
 21  
 22  # Colors for output
 23  RED='\033[0;31m'
 24  GREEN='\033[0;32m'
 25  YELLOW='\033[1;33m'
 26  NC='\033[0m' # No Color
 27  
 28  info() { echo -e "${GREEN}[INFO]${NC} $*"; }
 29  warn() { echo -e "${YELLOW}[WARN]${NC} $*"; }
 30  error() { echo -e "${RED}[ERROR]${NC} $*"; exit 1; }
 31  
 32  # Check prerequisites
 33  check_prereqs() {
 34      info "Checking prerequisites..."
 35  
 36      local missing=()
 37  
 38      command -v gcc >/dev/null 2>&1 || missing+=("gcc")
 39      command -v g++ >/dev/null 2>&1 || missing+=("g++")
 40      command -v make >/dev/null 2>&1 || missing+=("make")
 41      command -v nasm >/dev/null 2>&1 || missing+=("nasm")
 42      command -v iasl >/dev/null 2>&1 || missing+=("iasl (acpica-tools)")
 43      command -v python3 >/dev/null 2>&1 || missing+=("python3")
 44      command -v uuid-dev >/dev/null 2>&1 || [ -f /usr/include/uuid/uuid.h ] || missing+=("uuid-dev/libuuid-devel")
 45  
 46      if [ ${#missing[@]} -ne 0 ]; then
 47          error "Missing prerequisites: ${missing[*]}
 48  
 49  Install with:
 50    Debian/Ubuntu: sudo apt install build-essential uuid-dev iasl nasm python3
 51    Arch Linux:    sudo pacman -S base-devel util-linux-libs iasl nasm python
 52    Fedora:        sudo dnf install gcc gcc-c++ make libuuid-devel iasl nasm python3"
 53      fi
 54  
 55      info "All prerequisites found"
 56  }
 57  
 58  # Clone or update EDK2
 59  setup_edk2() {
 60      if [ -d "$EDK2_DIR" ]; then
 61          info "EDK2 directory exists at $EDK2_DIR"
 62          read -p "Update EDK2? [y/N] " -n 1 -r
 63          echo
 64          if [[ $REPLY =~ ^[Yy]$ ]]; then
 65              info "Updating EDK2..."
 66              cd "$EDK2_DIR"
 67              git fetch origin
 68              git checkout edk2-stable202411 2>/dev/null || git checkout origin/master
 69              git submodule update --init --recursive
 70          fi
 71      else
 72          info "Cloning EDK2 to $EDK2_DIR..."
 73          git clone --depth 1 --branch edk2-stable202411 \
 74              https://github.com/tianocore/edk2.git "$EDK2_DIR" || \
 75          git clone --depth 1 https://github.com/tianocore/edk2.git "$EDK2_DIR"
 76  
 77          cd "$EDK2_DIR"
 78          git submodule update --init --recursive
 79      fi
 80  }
 81  
 82  # Build base tools
 83  build_basetools() {
 84      info "Building EDK2 BaseTools..."
 85      cd "$EDK2_DIR"
 86  
 87      # Source the build environment
 88      source edksetup.sh
 89  
 90      # Build base tools
 91      make -C BaseTools -j$(nproc)
 92  }
 93  
 94  # Build OVMF with TCP4
 95  build_ovmf() {
 96      info "Building OVMF with TCP4 support..."
 97      cd "$EDK2_DIR"
 98  
 99      # Source environment
100      source edksetup.sh
101  
102      # Set build variables
103      export GCC5_X64_PREFIX=""
104      export WORKSPACE="$EDK2_DIR"
105      export PACKAGES_PATH="$EDK2_DIR"
106  
107      # Build OVMF X64 with full network stack
108      build -a X64 -t GCC5 -p OvmfPkg/OvmfPkgX64.dsc -b RELEASE \
109          -D NETWORK_IP4_ENABLE=TRUE \
110          -D NETWORK_IP6_ENABLE=FALSE \
111          -D NETWORK_TLS_ENABLE=FALSE \
112          -D NETWORK_HTTP_BOOT_ENABLE=FALSE \
113          -D NETWORK_ISCSI_ENABLE=FALSE \
114          -D NETWORK_ALLOW_HTTP_CONNECTIONS=TRUE \
115          -D SECURE_BOOT_ENABLE=FALSE \
116          -D TPM_ENABLE=FALSE \
117          -D TPM2_ENABLE=FALSE \
118          -D SMM_REQUIRE=FALSE \
119          -D DEBUG_ON_SERIAL_PORT=TRUE \
120          -n $(nproc)
121  
122      info "Build complete!"
123  }
124  
125  # Copy artifacts
126  copy_artifacts() {
127      info "Copying build artifacts..."
128  
129      mkdir -p "$BUILD_DIR"
130  
131      local ovmf_code="$EDK2_DIR/Build/OvmfX64/RELEASE_GCC5/FV/OVMF_CODE.fd"
132      local ovmf_vars="$EDK2_DIR/Build/OvmfX64/RELEASE_GCC5/FV/OVMF_VARS.fd"
133      local ovmf_full="$EDK2_DIR/Build/OvmfX64/RELEASE_GCC5/FV/OVMF.fd"
134  
135      if [ -f "$ovmf_code" ]; then
136          cp "$ovmf_code" "$BUILD_DIR/"
137          info "Copied OVMF_CODE.fd"
138      fi
139  
140      if [ -f "$ovmf_vars" ]; then
141          cp "$ovmf_vars" "$BUILD_DIR/"
142          info "Copied OVMF_VARS.fd"
143      fi
144  
145      if [ -f "$ovmf_full" ]; then
146          cp "$ovmf_full" "$BUILD_DIR/"
147          info "Copied OVMF.fd"
148      fi
149  
150      # Create a vars file copy for runtime modifications
151      if [ -f "$BUILD_DIR/OVMF_VARS.fd" ]; then
152          cp "$BUILD_DIR/OVMF_VARS.fd" "$BUILD_DIR/OVMF_VARS_runtime.fd"
153      fi
154  
155      info "Artifacts in: $BUILD_DIR"
156      ls -lh "$BUILD_DIR"/*.fd 2>/dev/null || true
157  }
158  
159  # Print usage instructions
160  print_usage() {
161      cat << EOF
162  
163  ${GREEN}=== OVMF with TCP4 Build Complete ===${NC}
164  
165  Firmware files are in: $BUILD_DIR
166  
167  To use with QEMU, run:
168  
169    qemu-system-x86_64 \\
170      -machine q35 \\
171      -m 512M \\
172      -drive if=pflash,format=raw,readonly=on,file=$BUILD_DIR/OVMF_CODE.fd \\
173      -drive if=pflash,format=raw,file=$BUILD_DIR/OVMF_VARS_runtime.fd \\
174      -netdev user,id=net0,hostfwd=tcp::8080-:80 \\
175      -device virtio-net-pci,netdev=net0 \\
176      -drive format=raw,file=fat:rw:/path/to/efi/files \\
177      -nographic
178  
179  Or use the run-qemu-tcp.sh script:
180  
181    ./scripts/run-qemu-tcp.sh
182  
183  ${YELLOW}Note:${NC} The TCP4 protocol requires a network device. The virtio-net-pci
184  device with user-mode networking is recommended for testing.
185  
186  EOF
187  }
188  
189  # Main
190  main() {
191      info "Building OVMF with TCP4 support for kabashira-efi"
192      echo
193  
194      check_prereqs
195      setup_edk2
196      build_basetools
197      build_ovmf
198      copy_artifacts
199      print_usage
200  }
201  
202  # Run if executed directly
203  if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
204      main "$@"
205  fi