Skip to content

Commit

Permalink
chore: rework messages handling on boot images extraction
Browse files Browse the repository at this point in the history
 * While we are at it, resolve inconsistencies regarding 'init_boot.img'
   where the section presented the creation of folder not needed for the
   partition.

 * Also, for the formerly said partition, replace the nested 'if' into
   a simple double condition, considering that 'dumpyara' from Python would
   already do the necessary for it.
  • Loading branch information
AntoninoScordino committed Nov 2, 2024
1 parent 3191818 commit fb5008e
Showing 1 changed file with 74 additions and 87 deletions.
161 changes: 74 additions & 87 deletions extract_and_push.sh
Original file line number Diff line number Diff line change
Expand Up @@ -327,29 +327,31 @@ 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

## Output
OUTPUT=${PWD}/boot

# Create necessary directories
mkdir -p "${OUTPUT}/dts" "${OUTPUT}/dtb"
# Python rewrite automatically extracts such partitions
if [[ "${USE_ALT_DUMPER}" == "true" ]]; then
mkdir -p "${OUTPUT}/ramdisk"

# Extract device-tree blobs from 'boot.img'
echo "[INFO] 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"
# Unpack 'boot.img' through 'unpackbootimg'
echo "[INFO] Extracting 'boot.img' content..."
${UNPACKBOOTIMG} -i "${IMAGE}" -o "${OUTPUT}" > /dev/null || echo "[ERROR] Extraction unsuccessful."

# Do not run 'dtc' if no DTB was found
if [ "$(find "${OUTPUT}/dtb" -name "*.dtb")" ]; then
echo "[INFO] Decompiling device-tree blobs..."
# Decompile '.dtb' to '.dts'
for dtb in $(find "${PWD}/boot/dtb" -type f); do
dtc -q -I dtb -O dts "${dtb}" >> "${OUTPUT}/dts/$(basename "${dtb}" | sed 's/\.dtb/.dts/')" || echo "[ERROR] Failed to decompile."
done
# Decrompress 'boot.img-ramdisk'
## Run only if 'boot.img-ramdisk' is not empty
if file boot.img-ramdisk | grep -q LZ4 || file boot.img-ramdisk | grep -q gzip; then
echo "[INFO] Extracting ramdisk..."
unlz4 "${OUTPUT}/boot.img-ramdisk" "${OUTPUT}/ramdisk.lz4" > /dev/null
7zz -snld x "${OUTPUT}/ramdisk.lz4" -o"${OUTPUT}/ramdisk" > /dev/null || echo "[ERROR] Failed to extract ramdisk."

## Clean-up
rm -rf "${OUTPUT}/ramdisk.lz4"
fi
fi

# Extract 'ikconfig'
Expand All @@ -360,72 +362,51 @@ if [[ -f "${PWD}/boot.img" ]]; then
}
fi

# Kallsyms
# 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'"
}

# ELF
# 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'"
}

# Python rewrite automatically extracts such partitions
if [[ "${USE_ALT_DUMPER}" == "true" ]]; then
mkdir -p "${OUTPUT}/ramdisk"

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

# Decrompress 'boot.img-ramdisk'
## Run only if 'boot.img-ramdisk' is not empty
if file boot.img-ramdisk | grep -q LZ4 || file boot.img-ramdisk | grep -q gzip; then
echo "[INFO] Extracting ramdisk..."
unlz4 "${OUTPUT}/boot.img-ramdisk" "${OUTPUT}/ramdisk.lz4" > /dev/null
7zz -snld x "${OUTPUT}/ramdisk.lz4" -o"${OUTPUT}/ramdisk" > /dev/null || echo "[ERROR] Failed to extract ramdisk."
# 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"

## Clean-up
rm -rf "${OUTPUT}/ramdisk.lz4"
fi
# Do not run 'dtc' if no DTB was found
if [ "$(find "${OUTPUT}/dtb" -name "*.dtb")" ]; then
echo "[INFO] Decompiling device-tree blobs..."
# Decompile '.dtb' to '.dts'
for dtb in $(find "${PWD}/boot/dtb" -type f); do
dtc -q -I dtb -O dts "${dtb}" >> "${OUTPUT}/dts/$(basename "${dtb}" | sed 's/\.dtb/.dts/')" || echo "[ERROR] Failed to decompile."
done
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

## Output
OUTPUT=${PWD}/vendor_boot

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

# Extract device-tree blobs from 'vendor_boot.img'
echo "[INFO] 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..."
# Decompile '.dtb' to '.dts'
for dtb in $(find "${OUTPUT}/dtb" -type f); do
dtc -q -I dtb -O dts "${dtb}" >> "${OUTPUT}/dts/$(basename "${dtb}" | sed 's/\.dtb/.dts/')" || echo "[ERROR] Failed to decompile."
done
fi

# Python rewrite automatically extracts such partitions
if [[ "${USE_ALT_DUMPER}" == "true" ]]; 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 @@ -436,24 +417,12 @@ if [[ -f "${PWD}/vendor_boot.img" ]]; then
## Clean-up
rm -rf "${OUTPUT}/ramdisk.lz4"
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

## Output
OUTPUT=${PWD}/vendor_kernel_boot

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

# Extract device-tree blobs from 'vendor_kernel_boot.img'
echo "[INFO] Extracting device-tree blobs..."
# 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"

Expand All @@ -465,13 +434,23 @@ if [[ -f "${PWD}/vendor_kernel_boot.img" ]]; then
dtc -q -I dtb -O dts "${dtb}" >> "${OUTPUT}/dts/$(basename "${dtb}" | sed 's/\.dtb/.dts/')" || echo "[ERROR] Failed to decompile."
done
fi
fi

# Extract 'vendor_kernel_boot.img'
if [[ -f "${PWD}/vendor_kernel_boot.img" ]]; then
# Set a variable for each path
## Image
IMAGE=${PWD}/vendor_kernel_boot.img

## Output
OUTPUT=${PWD}/vendor_kernel_boot

# Python rewrite automatically extracts such partitions
if [[ "${USE_ALT_DUMPER}" == "true" ]]; 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 @@ -482,12 +461,27 @@ if [[ -f "${PWD}/vendor_kernel_boot.img" ]]; then
## Clean-up
rm -rf "${OUTPUT}/ramdisk.lz4"
fi

# Create necessary directories
mkdir -p "${OUTPUT}/dts" "${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..."
# Decompile '.dtb' to '.dts'
for dtb in $(find "${OUTPUT}/dtb" -type f); do
dtc -q -I dtb -O dts "${dtb}" >> "${OUTPUT}/dts/$(basename "${dtb}" | sed 's/\.dtb/.dts/')" || echo "[ERROR] Failed to decompile."
done
fi
fi

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

if [[ -f "${PWD}/init_boot.img" ]] && [[ "${USE_ALT_DUMPER}" == "false" ]]; then
# Set a variable for each path
## Image
IMAGE=${PWD}/init_boot.img
Expand All @@ -496,30 +490,23 @@ if [[ -f "${PWD}/init_boot.img" ]]; then
OUTPUT=${PWD}/init_boot

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

# Python rewrite automatically extracts such partitions
if [[ "${USE_ALT_DUMPER}" == "false" ]]; then
mkdir -p "${OUTPUT}/ramdisk"
# Unpack 'init_boot.img' through 'unpackbootimg'
echo "[INFO] Extracting 'init_boot.img' content..."
${UNPACKBOOTIMG} -i "${IMAGE}" -o "${OUTPUT}" > /dev/null || echo "[ERROR] Extraction unsuccessful."

# Unpack 'init_boot.img' through 'unpackbootimg'
echo "[INFO] Extracting 'init_boot.img' to 'init_boot/'..."
${UNPACKBOOTIMG} -i "${IMAGE}" -o "${OUTPUT}" > /dev/null || echo "[ERROR] Extraction unsuccessful."
# Decrompress 'init_boot.img-ramdisk'
echo "[INFO] Extracting ramdisk..."
unlz4 "${OUTPUT}/init_boot.img-ramdisk" "${OUTPUT}/ramdisk.lz4" > /dev/null
7zz -snld x "${OUTPUT}/ramdisk.lz4" -o"${OUTPUT}/ramdisk" > /dev/null || echo "[ERROR] Failed to extract ramdisk."

# Decrompress 'init_boot.img-ramdisk'
echo "[INFO] Extracting ramdisk..."
unlz4 "${OUTPUT}/init_boot.img-ramdisk" "${OUTPUT}/ramdisk.lz4" > /dev/null
7zz -snld x "${OUTPUT}/ramdisk.lz4" -o"${OUTPUT}/ramdisk" > /dev/null || echo "[ERROR] Failed to extract ramdisk."

## Clean-up
rm -rf "${OUTPUT}/ramdisk.lz4"
fi
## Clean-up
rm -rf "${OUTPUT}/ramdisk.lz4"
fi

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

# Set a variable for each path
## Image
IMAGE=${PWD}/dtbo.img
Expand All @@ -531,7 +518,7 @@ 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"

Expand Down

0 comments on commit fb5008e

Please sign in to comment.