Skip to content

Commit

Permalink
chore: rework messages handling on boot images extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoninoScordino committed Nov 1, 2024
1 parent 7108aba commit 7feac41
Showing 1 changed file with 81 additions and 21 deletions.
102 changes: 81 additions & 21 deletions extract_and_push.sh
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ UNPACKBOOTIMG="${HOME}/Firmware_extractor/tools/unpackbootimg"

# Extract 'boot.img'
if [[ -f "${PWD}/boot.img" ]]; then
echo "[INFO] Extracting 'boot.img' content"

# Set a variable for each path
## Image
IMAGE=${PWD}/boot.img
Expand Down Expand Up @@ -375,7 +375,7 @@ if [[ -f "${PWD}/boot.img" ]]; then
mkdir -p "${OUTPUT}/ramdisk"

# Unpack 'boot.img' through 'unpackbootimg'
echo "[INFO] Extracting 'boot.img' to 'boot/'..."
echo "[INFO] Extracting 'boot.img' content..."
${UNPACKBOOTIMG} -i "${IMAGE}" -o "${OUTPUT}" > /dev/null || echo "[ERROR] Extraction unsuccessful."

# Decrompress 'boot.img-ramdisk'
Expand All @@ -389,11 +389,45 @@ if [[ -f "${PWD}/boot.img" ]]; then
rm -rf "${OUTPUT}/ramdisk.lz4"
fi
fi

# Extract 'ikconfig'
echo "[INFO] Extract 'ikconfig'..."
if command -v extract-ikconfig > /dev/null ; then
extract-ikconfig "${PWD}"/boot.img > "${PWD}"/ikconfig || {
echo "[ERROR] Failed to generate 'ikconfig'"
}
fi

# Generate non-stack symbols
echo "[INFO] Generating 'kallsyms.txt'..."
uvx --from git+https://github.com/marin-m/vmlinux-to-elf@da14e789596d493f305688e221e9e34ebf63cbb8 kallsyms-finder "${IMAGE}" > kallsyms.txt || {
echo "[ERROR] Failed to generate 'kallsyms.txt'"
}

# Generate analyzable '.elf'
echo "[INFO] Extracting 'boot.elf'..."
uvx --from git+https://github.com/marin-m/vmlinux-to-elf@da14e789596d493f305688e221e9e34ebf63cbb8 vmlinux-to-elf "${IMAGE}" boot.elf > /dev/null || {
echo "[ERROR] Failed to generate 'boot.elf'"
}

# Create necessary directories
mkdir -p "${OUTPUT}/dts" "${OUTPUT}/dtb"

# Extract device-tree blobs from 'boot.img'
echo "[INFO] boot.img: Extracting device-tree blobs..."
extract-dtb "${IMAGE}" -o "${OUTPUT}/dtb" > /dev/null || echo "[INFO] No device-tree blobs found."
rm -rf "${OUTPUT}/dtb/00_kernel"

# Decompile '.dtb' to '.dts'
if find "${OUTPUT}/dtb" -name "*.dtb"; then
echo "[INFO] Decompiling device-tree blobs..."
find "${OUTPUT}/dtb" -type f \
-exec sh -c 'dtc -q -I dtb -O dts "${1}" >> "${OUTPUT}/dts/$(basename "${1}" | sed 's/\.dtb/.dts/')"' shell {} \;
fi
fi

# Extract 'vendor_boot.img'
if [[ -f "${PWD}/vendor_boot.img" ]]; then
echo "[INFO] Extracting 'vendor_boot.img' content"
# Set a variable for each path
## Image
IMAGE=${PWD}/vendor_boot.img
Expand Down Expand Up @@ -421,7 +455,7 @@ if [[ -f "${PWD}/vendor_boot.img" ]]; then
mkdir -p "${OUTPUT}/ramdisk"

## Unpack 'vendor_boot.img' through 'unpackbootimg'
echo "[INFO] Extracting 'vendor_boot.img' to 'vendor_boot/'..."
echo "[INFO] Extracting 'vendor_boot.img' content..."
${UNPACKBOOTIMG} -i "${IMAGE}" -o "${OUTPUT}" > /dev/null || echo "[ERROR] Extraction unsuccessful."

# Decrompress 'vendor_boot.img-vendor_ramdisk'
Expand All @@ -432,12 +466,25 @@ if [[ -f "${PWD}/vendor_boot.img" ]]; then
## Clean-up
rm -rf "${OUTPUT}/ramdisk.lz4"
fi

# Create necessary directories
mkdir -p "${OUTPUT}/dts" "${OUTPUT}/dtb" "${OUTPUT}/ramdisk"

# Extract device-tree blobs from 'vendor_boot.img'
echo "[INFO] vendor_boot.img: Extracting device-tree blobs..."
extract-dtb "${IMAGE}" -o "${OUTPUT}/dtb" > /dev/null || echo "[INFO] No device-tree blobs found."
rm -rf "${OUTPUT}/dtb/00_kernel"

# Decompile '.dtb' to '.dts'
if find "${OUTPUT}/dtb" -name "*.dtb"; then
echo "[INFO] Decompiling device-tree blobs..."
find "${OUTPUT}/dtb" -type f \
-exec sh -c 'dtc -q -I dtb -O dts "${1}" >> "${OUTPUT}/dts/$(basename "${1}" | sed 's/\.dtb/.dts/')"' shell {} \;
fi
fi

# Extract 'vendor_kernel_boot.img'
if [[ -f "${PWD}/vendor_kernel_boot.img" ]]; then
echo "[INFO] Extracting 'vendor_kernel_boot.img' content"

# Set a variable for each path
## Image
IMAGE=${PWD}/vendor_kernel_boot.img
Expand Down Expand Up @@ -465,7 +512,7 @@ if [[ -f "${PWD}/vendor_kernel_boot.img" ]]; then
mkdir -p "${OUTPUT}/ramdisk"

# Unpack 'vendor_kernel_boot.img' through 'unpackbootimg'
echo "[INFO] Extracting 'vendor_kernel_boot.img' to 'vendor_kernel_boot/'..."
echo "[INFO] Extracting 'vendor_kernel_boot.img' content..."
${UNPACKBOOTIMG} -i "${IMAGE}" -o "${OUTPUT}" > /dev/null || echo "[ERROR] Extraction unsuccessful."

# Decrompress 'vendor_kernel_boot.img-vendor_ramdisk'
Expand All @@ -476,22 +523,25 @@ if [[ -f "${PWD}/vendor_kernel_boot.img" ]]; then
## Clean-up
rm -rf "${OUTPUT}/ramdisk.lz4"
fi
fi

# Extract 'init_boot.img'
if [[ -f "${PWD}/init_boot.img" ]]; then
echo "[INFO] Extracting 'init_boot.img' content"

# Set a variable for each path
## Image
IMAGE=${PWD}/init_boot.img

## Output
OUTPUT=${PWD}/init_boot

# Create necessary directories
mkdir -p "${OUTPUT}/dts" mkdir -p "${OUTPUT}/dtb"

# Extract device-tree blobs from 'vendor_kernel_boot.img'
echo "[INFO] vendor_kernel_boot.img: Extracting device-tree blobs..."
extract-dtb "${IMAGE}" -o "${OUTPUT}/dtb" > /dev/null || echo "[INFO] No device-tree blobs found."
rm -rf "${OUTPUT}/dtb/00_kernel"

# Decompile '.dtb' to '.dts'
if find "${OUTPUT}/dtb" -name "*.dtb"; then
echo "[INFO] Decompiling device-tree blobs..."
find "${OUTPUT}/dtb" -type f \
-exec sh -c 'dtc -q -I dtb -O dts "${1}" >> "${OUTPUT}/dts/$(basename "${1}" | sed 's/\.dtb/.dts/')"' shell {} \;
fi
fi

# Extract 'init_boot.img'
if [[ -f "${PWD}/init_boot.img" ]]; then
# Python rewrite automatically extracts such partitions
if [[ "${USE_ALT_DUMPER}" == "false" ]]; then
mkdir -p "${OUTPUT}/ramdisk"
Expand All @@ -508,6 +558,16 @@ if [[ -f "${PWD}/init_boot.img" ]]; then
## Clean-up
rm -rf "${OUTPUT}/ramdisk.lz4"
fi

# Set a variable for each path
## Image
IMAGE=${PWD}/init_boot.img

## Output
OUTPUT=${PWD}/init_boot

# Create necessary directories
mkdir -p "${OUTPUT}/dts" mkdir -p "${OUTPUT}/dtb"
fi

# Extract 'dtbo.img'
Expand All @@ -525,12 +585,12 @@ if [[ -f "${PWD}/dtbo.img" ]]; then
mkdir -p "${OUTPUT}/dts"

# Extract device-tree blobs from 'dtbo.img'
echo "[INFO] Extracting device-tree blobs..."
echo "[INFO] dbto.img: Extracting device-tree blobs..."
extract-dtb "${IMAGE}" -o "${OUTPUT}" > /dev/null || echo "[INFO] No device-tree blobs found."
rm -rf "${OUTPUT}/00_kernel"

# Decompile '.dtb' to '.dts'
echo "[INFO] Decompiling device-tree blobs..."
echo "[INFO] dbto.img: Decompiling device-tree blobs..."
find "${OUTPUT}/dtb" -type f \
-exec sh -c 'dtc -q -I dtb -O dts "${1}" >> "${OUTPUT}/dts/$(basename "${1}" | sed 's/\.dtb/.dts/')"' shell {} \;
fi
Expand Down

0 comments on commit 7feac41

Please sign in to comment.