Skip to content

Commit

Permalink
run binwalk without --run-as if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
ading2210 committed Jul 12, 2024
1 parent 91486b0 commit 3f213fc
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions shim_utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

#utilties for reading shim disk images

run_binwalk() {
if binwalk -h | grep -- '--run-as' >/dev/null; then
binwalk "$@" --run-as=root
else
binwalk "$@"
fi
}

#extract the initramfs from a kernel image
extract_initramfs() {
local kernel_bin="$1"
Expand All @@ -10,13 +18,13 @@ extract_initramfs() {

#extract the compressed kernel image from the partition data
local kernel_file="$(basename $kernel_bin)"
local binwalk_out=$(binwalk --extract $kernel_bin --directory=$working_dir --run-as=root)
local binwalk_out=$(run_binwalk --extract $kernel_bin --directory=$working_dir)
local stage1_file=$(echo $binwalk_out | pcregrep -o1 "\d+\s+0x([0-9A-F]+)\s+gzip compressed data")
local stage1_dir="$working_dir/_$kernel_file.extracted"
local stage1_path="$stage1_dir/$stage1_file"

#extract the initramfs cpio archive from the kernel image
binwalk --extract $stage1_path --directory=$stage1_dir --run-as=root > /dev/null
run_binwalk --extract $stage1_path --directory=$stage1_dir > /dev/null
local stage2_dir="$stage1_dir/_$stage1_file.extracted/"
local cpio_file=$(file $stage2_dir/* | pcregrep -o1 "([0-9A-F]+):\s+ASCII cpio archive")
local cpio_path="$stage2_dir/$cpio_file"
Expand All @@ -31,7 +39,7 @@ extract_initramfs_arm() {
local output_dir="$3"

#extract the kernel lz4 archive from the partition
local binwalk_out="$(binwalk $kernel_bin --run-as=root)"
local binwalk_out="$(run_binwalk $kernel_bin)"
local lz4_offset="$(echo "$binwalk_out" | pcregrep -o1 "(\d+).+?LZ4 compressed data" | head -n1)"
local lz4_file="$working_dir/kernel.lz4"
local kernel_img="$working_dir/kernel_decompressed.bin"
Expand All @@ -40,7 +48,7 @@ extract_initramfs_arm() {

#extract the initramfs cpio archive from the kernel image
local extracted_dir="$working_dir/_kernel_decompressed.bin.extracted"
binwalk --extract $kernel_img --directory=$working_dir --run-as=root > /dev/null
run_binwalk --extract $kernel_img --directory=$working_dir > /dev/null
local cpio_file=$(file $extracted_dir/* | pcregrep -o1 "([0-9A-F]+):\s+ASCII cpio archive")
local cpio_path="$extracted_dir/$cpio_file"

Expand Down

0 comments on commit 3f213fc

Please sign in to comment.