Skip to content

Commit

Permalink
feat: Add script to extract binary from ARM Linux image
Browse files Browse the repository at this point in the history
Add a new script `armlinux_loopdev_extract.sh` to extract the binary
file from an ARM Linux image. This script sets up loop devices, mounts
the partition, and copies the binary file to the specified output file.

The script checks if the necessary arguments are provided and verifies
the existence of the image file. It then sets up a loop device for the
image, identifies and mounts the second partition, copies the binary,
and unmounts/detaches loop devices upon completion.

The addition enhances automation by facilitating extraction of binaries
from ARM Linux images efficiently.
  • Loading branch information
25077667 committed May 18, 2024
1 parent 2c78f08 commit e39e3bb
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 11 deletions.
17 changes: 6 additions & 11 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -121,20 +121,15 @@ jobs:
mkdir -p ${{ matrix.copy_artifact_dest }}
- name: Extract Artifact from output image
run: |
# debugging purpose, we upload image to artifacts
mkdir -p ./release
cp ${{ steps.arm_runner_install.outputs.image }} cg-${{ matrix.arch }}.img
sudo apt-get install -y fdisk parted
chmod +x ./ci/armlinux_loopdev_extract.sh
./ci/armlinux_loopdev_extract.sh ${{ steps.arm_runner_install.outputs.image }} cg-${{ matrix.arch }}.elf
- name: Upload Artifact (ARM)
uses: actions/upload-artifact@v4
with:
name: cg-${{ matrix.arch }}.img
path: cg-${{ matrix.arch }}.img

# - name: Upload Artifact (ARM)
# uses: actions/upload-artifact@v4
# with:
# name: release/cg-${{ matrix.arch }}.elf
# path: release/cg-${{ matrix.arch }}.elf
name: cg-${{ matrix.arch }}.elf
path: cg-${{ matrix.arch }}.elf

release:
needs: [build, build_arm]
Expand Down
89 changes: 89 additions & 0 deletions ci/armlinux_loopdev_extract.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#!/bin/bash

# Check if the user provided an image file and output file
if [ "$#" -ne 2 ]; then
echo "Usage: $0 <image_file> <output_file>"
exit 1
fi

IMG_FILE=$1
OUTPUT_FILE=$2
PWD=$(pwd)
MNT_POINT="$PWD/mnt"

# Check if the file exists
if [ ! -f "$IMG_FILE" ]; then
echo "File not found: $IMG_FILE"
exit 1
fi

# Setup loop device and get partitions info
LOOP_DEVICE=$(sudo losetup -fP --show "$IMG_FILE")

if [ $? -ne 0 ]; then
echo "Failed to setup loop device for $IMG_FILE"
exit 1
fi

echo "Created loopback device ${LOOP_DEVICE}"

# Check the partition table
sudo parted -s "$LOOP_DEVICE" print

# Get partition information
PARTITION_INFO=$(sudo parted -s "$LOOP_DEVICE" unit s print | grep "^ [0-9]")

if [ $? -ne 0 ]; then
echo "Failed to get partition information"
sudo losetup -d "$LOOP_DEVICE"
exit 1
fi

# Parse the second partition details
PARTITION_OFFSET=$(echo "$PARTITION_INFO" | awk 'NR==2 {print $2}' | sed 's/s//')
PARTITION_SIZE=$(echo "$PARTITION_INFO" | awk 'NR==2 {print $4}' | sed 's/s//')

# Create a loop device for the second partition
PARTITION_LOOP_DEVICE=$(sudo losetup -o $((PARTITION_OFFSET * 512)) --sizelimit $((PARTITION_SIZE * 512)) --show -f "$LOOP_DEVICE")

if [ $? -ne 0 ]; then
echo "Failed to create loop device for partition"
sudo losetup -d "$LOOP_DEVICE"
exit 1
fi

# Create the mount point directory if it doesn't exist
mkdir -p $MNT_POINT

# Mount the partition
sudo mount "$PARTITION_LOOP_DEVICE" $MNT_POINT

if [ $? -ne 0 ]; then
echo "Failed to mount partition"
sudo losetup -d "$PARTITION_LOOP_DEVICE"
sudo losetup -d "$LOOP_DEVICE"
exit 1
fi

# Copy the binary file from the mounted partition to the output file
sudo cp $MNT_POINT/cg/*.elf $OUTPUT_FILE

if [ $? -ne 0 ]; then
echo "Failed to copy the binary file"
sudo umount $MNT_POINT
sudo losetup -d "$PARTITION_LOOP_DEVICE"
sudo losetup -d "$LOOP_DEVICE"
exit 1
fi

echo "Binary file copied successfully to $OUTPUT_FILE"

# Cleanup function to unmount and detach loop devices
cleanup() {
sudo umount $MNT_POINT
sudo losetup -d "$PARTITION_LOOP_DEVICE"
sudo losetup -d "$LOOP_DEVICE"
}

# Trap EXIT signal to ensure cleanup is done
trap cleanup EXIT

0 comments on commit e39e3bb

Please sign in to comment.