Skip to content

Commit

Permalink
Add universal flashing script
Browse files Browse the repository at this point in the history
The 'flash.sh' script makes image flashing easy and hassle-free,
both for compressed and uncompressed images.

Signed-off-by: Ivan Nikolaenko <[email protected]>
Co-authored-by: Valentin Kharin <[email protected]>
  • Loading branch information
2 people authored and brianmcgillion committed Aug 14, 2024
1 parent 388a8b1 commit 217e7a6
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 10 deletions.
18 changes: 9 additions & 9 deletions docs/src/ref_impl/build_and_run.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/<YOUR_USB_DRIVE> bs=32M status=progress oflag=direct
./packages/flash/flash.sh -d /dev/<YOUR_USB_DRIVE> -i result/<IMAGE_NAME>
```
3. Boot the computer from the USB media.
Expand All @@ -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/<YOUR_USB_DRIVE> bs=32M status=progress oflag=direct
./packages/flash/flash.sh -d /dev/<YOUR_USB_DRIVE> -i result/<IMAGE_NAME>
```
3. Boot the computer from the USB media.
Expand Down Expand Up @@ -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/<YOUR_USB_DRIVE> bs=32M status=progress oflag=direct
./packages/flash/flash.sh -d /dev/<YOUR_USB_DRIVE> -i result/sd-image/<IMAGE_NAME>
```
3. Boot the hardware from the USB media.
Expand Down Expand Up @@ -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/<YOUR_USB_DRIVE> 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/<YOUR_USB_DRIVE> -i result/<IMAGE_NAME>`.
2. Insert an SD card and USB boot media into the board and switch the power on.
Expand All @@ -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/<YOUR_SD_DEVICE> bs=32M status=progress oflag=direct`.
* Prepare the SD card with the target HW image you built: `./packages/flash/flash.sh -d /dev/<YOUR_SD_CARD> -i result/<IMAGE_NAME>`.
* 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/<YOUR_MMC_DEVICE> bs=32M status=progress oflag=direct`.
* You can directly flash a NixOS image to an onboard MMC card: `./packages/flash/flash.sh -d /dev/<YOUR_MMC_DEVICE> -i result/<IMAGE_NAME>`.
For more information on how to access the MMC card as a USB disk, see [MPFS Icicle Kit User Guide](https://tinyurl.com/48wycdka).
3 changes: 3 additions & 0 deletions nix/devshell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 17 additions & 0 deletions packages/flash/default.nix
Original file line number Diff line number Diff line change
@@ -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;
}
71 changes: 71 additions & 0 deletions packages/flash/flash.sh
Original file line number Diff line number Diff line change
@@ -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 <disk> -i <imagefile>"
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
2 changes: 2 additions & 0 deletions packages/installer/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# SPDX-License-Identifier: Apache-2.0
{
coreutils,
util-linux,
hwinfo,
writeShellApplication,
zstd,
Expand All @@ -10,6 +11,7 @@ writeShellApplication {
name = "ghaf-installer";
runtimeInputs = [
coreutils
util-linux
zstd
hwinfo
];
Expand Down
14 changes: 13 additions & 1 deletion packages/installer/ghaf-installer.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"

0 comments on commit 217e7a6

Please sign in to comment.