diff --git a/docs/src/ref_impl/build_and_run.md b/docs/src/ref_impl/build_and_run.md index d1468b031..e5acdc2ed 100644 --- a/docs/src/ref_impl/build_and_run.md +++ b/docs/src/ref_impl/build_and_run.md @@ -57,9 +57,9 @@ Do the following: ``` nix build github:tiiuae/ghaf#generic-x86_64-debug ``` -2. After the build is completed, prepare a USB boot media with the target image you built: +2. After the build is completed, prepare a USB boot media with the target image you built using the `flash.sh` script: ``` - dd if=./result/nixos.img of=/dev/ bs=32M status=progress oflag=direct + ./packages/flash/flash.sh -d /dev/ -i result/ ``` 3. Boot the computer from the USB media. @@ -74,9 +74,9 @@ Do the following: ``` nix build github:tiiuae/ghaf#lenovo-x1-carbon-gen11-debug ``` -2. After the build is completed, prepare a USB boot media with the target image you built: +2. After the build is completed, prepare a USB boot media with the target image you built using the `flash.sh` script: ``` - dd if=./result/nixos.img of=/dev/ bs=32M status=progress oflag=direct + ./packages/flash/flash.sh -d /dev/ -i result/ ``` 3. Boot the computer from the USB media. @@ -128,9 +128,9 @@ After the latest firmware is [flashed](./build_and_run.md#flashing-nvidia-jetson ``` nix build github:tiiuae/ghaf#nvidia-jetson-orin-agx-debug ``` -2. After the build is completed, prepare a USB boot media with the target image you built: +2. After the build is completed, prepare a USB boot media with the target image you built using the `flash.sh` script: ``` - dd if=./result/nixos.img of=/dev/ bs=32M status=progress oflag=direct + ./packages/flash/flash.sh -d /dev/ -i result/sd-image/ ``` 3. Boot the hardware from the USB media. @@ -168,7 +168,7 @@ In the case of i.MX8, Ghaf deployment consists of creating a bootable SD card an 1. To build and flash the Ghaf image: 1. Run the `nix build .#packages.aarch64-linux.imx8mp-evk-release` command. - 2. Prepare the USB boot media with the target HW image you built: `dd if=./result/nixos.img of=/dev/ bs=32M status=progress oflag=direct`. + 2. Prepare the USB boot media with the target HW image you built: `./packages/flash/flash.sh -d /dev/ -i result/`. 2. Insert an SD card and USB boot media into the board and switch the power on. @@ -192,11 +192,11 @@ In the case of the Icicle Kit, Ghaf deployment consists of creating an SD image 2. Flash the Ghaf SD image: * If you want to use a SD card: - * Prepare the SD card with the target HW image you built: `dd if=./result/nixos.img of=/dev/ bs=32M status=progress oflag=direct`. + * Prepare the SD card with the target HW image you built: `./packages/flash/flash.sh -d /dev/ -i result/`. * Insert an SD card into the board and switch the power on. * If you want to use the onboard MMC: - * You can directly flash a NixOS image to onboard an MMC card: `dd if=./result/nixos.img of=/dev/ bs=32M status=progress oflag=direct`. + * You can directly flash a NixOS image to an onboard MMC card: `./packages/flash/flash.sh -d /dev/ -i result/`. For more information on how to access the MMC card as a USB disk, see [MPFS Icicle Kit User Guide](https://tinyurl.com/48wycdka). diff --git a/nix/devshell.nix b/nix/devshell.nix index 19db76f91..7b8b7bf17 100644 --- a/nix/devshell.nix +++ b/nix/devshell.nix @@ -32,6 +32,9 @@ ++ [ inputs'.nix-fast-build.packages.default ] + ++ [ + (pkgs.callPackage ../packages/flash {}) + ] ++ lib.optional (pkgs.hostPlatform.system != "riscv64-linux") pkgs.cachix; # TODO Add pre-commit.devShell (needs to exclude RiscV) diff --git a/packages/flash/default.nix b/packages/flash/default.nix new file mode 100644 index 000000000..5a7c4ecbe --- /dev/null +++ b/packages/flash/default.nix @@ -0,0 +1,17 @@ +# Copyright 2022-2024 TII (SSRC) and the Ghaf contributors +# SPDX-License-Identifier: Apache-2.0 +{ + coreutils, + util-linux, + writeShellApplication, + zstd, +}: +writeShellApplication { + name = "flash-script"; + runtimeInputs = [ + coreutils + util-linux + zstd + ]; + text = builtins.readFile ./flash.sh; +} diff --git a/packages/flash/flash.sh b/packages/flash/flash.sh new file mode 100755 index 000000000..b952000eb --- /dev/null +++ b/packages/flash/flash.sh @@ -0,0 +1,71 @@ +#!/usr/bin/env bash +# Copyright 2022-2024 TII (SSRC) and the Ghaf contributors +# SPDX-License-Identifier: Apache-2.0 + +# Function to print usage and exit +print_usage() { + echo "Usage: $0 -d -i " + exit 1 +} + +if [ "$EUID" -ne 0 ]; then + echo "Please run as root" + exit +fi + +# Check the number of parameters +if [ "$#" -ne 4 ]; then + print_usage +fi + +# Parse the parameters +while getopts "d:i:" opt; do + case $opt in + d) DEVICE="$OPTARG" ;; + i) FILENAME="$OPTARG" ;; + *) print_usage ;; + esac +done + +# Check if disk and imagefile exist +if [ ! -e "$DEVICE" ]; then + echo "No such block device: ${DEVICE}" + exit 1 +fi + +if [ ! -e "$FILENAME" ]; then + echo "No such file: ${FILENAME}" + exit 1 +fi + +# Function to wipe any ZFS leftovers exising on the disk +wipe_filesystem () { + echo "Wiping filesystem..." + # Set sector size to 512 bytes + SECTOR=512 + # 10 MiB in 512-byte sectors + MIB_TO_SECTORS=20480 + # Disk size in 512-byte sectors + SECTORS=$(blockdev --getsz "$DEVICE") + # Unmount possible mounted filesystems + sync; umount -q "$DEVICE"* || true; + # Wipe first 10MiB of disk + dd if=/dev/zero of="$DEVICE" bs="$SECTOR" count="$MIB_TO_SECTORS" conv=fsync status=none + # Wipe last 10MiB of disk + dd if=/dev/zero of="$DEVICE" bs="$SECTOR" count="$MIB_TO_SECTORS" seek="$((SECTORS - MIB_TO_SECTORS))" conv=fsync status=none + echo "Flashing..." +} + +echo "Found ${FILENAME}..." + +# Check the extension of the image file and run appropriate command +if [[ "$FILENAME" == *.zst ]]; then + wipe_filesystem + zstdcat "$FILENAME" | dd of="$DEVICE" bs=32M status=progress conv=fsync oflag=direct iflag=fullblock +elif [[ "$FILENAME" == *.iso || "$FILENAME" == *.img ]]; then + wipe_filesystem + dd if="$FILENAME" of="$DEVICE" bs=32M status=progress conv=fsync oflag=direct iflag=fullblock +else + echo "Unsupported file format" + exit 1 +fi diff --git a/packages/installer/default.nix b/packages/installer/default.nix index 9444ef4f0..18fe555d6 100644 --- a/packages/installer/default.nix +++ b/packages/installer/default.nix @@ -2,6 +2,7 @@ # SPDX-License-Identifier: Apache-2.0 { coreutils, + util-linux, hwinfo, writeShellApplication, zstd, @@ -10,6 +11,7 @@ writeShellApplication { name = "ghaf-installer"; runtimeInputs = [ coreutils + util-linux zstd hwinfo ]; diff --git a/packages/installer/ghaf-installer.sh b/packages/installer/ghaf-installer.sh index 44dfb38f9..b272fa170 100755 --- a/packages/installer/ghaf-installer.sh +++ b/packages/installer/ghaf-installer.sh @@ -72,7 +72,19 @@ case "$response" in ;; esac +# Wipe any possible ZFS leftovers from previous installations +# Set sector size to 512 bytes +SECTOR=512 +# 10 MiB in 512-byte sectors +MIB_TO_SECTORS=20480 +# Disk size in 512-byte sectors +SECTORS=$(blockdev --getsz "$DEVICE_NAME") +# Wipe first 10MiB of disk +dd if=/dev/zero of="$DEVICE_NAME" bs="$SECTOR" count="$MIB_TO_SECTORS" conv=fsync status=none +# Wipe last 10MiB of disk +dd if=/dev/zero of="$DEVICE_NAME" bs="$SECTOR" count="$MIB_TO_SECTORS" seek="$((SECTORS - MIB_TO_SECTORS))" conv=fsync status=none + echo "Installing..." -zstdcat "$IMG_PATH" | dd of="${DEVICE_NAME}" bs=32M status=progress +zstdcat "$IMG_PATH" | dd of="$DEVICE_NAME" bs=32M status=progress echo "Installation done. Please remove the installation media and reboot"