From 86269dfa0559a3c14af009ffe4fcf81cb0b2025c Mon Sep 17 00:00:00 2001 From: ading2210 Date: Wed, 20 Dec 2023 22:05:17 -0800 Subject: [PATCH] move initramfs extraction to separate script --- .gitignore | 2 +- build.sh | 28 ++-------------------------- build_squashfs.sh | 10 ++++++++++ shim_utils.sh | 25 +++++++++++++++++++++++++ 4 files changed, 38 insertions(+), 27 deletions(-) create mode 100755 build_squashfs.sh create mode 100755 shim_utils.sh diff --git a/.gitignore b/.gitignore index f5e92d4..61fa5b9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ /data -/test.bin \ No newline at end of file +/*.bin \ No newline at end of file diff --git a/build.sh b/build.sh index d62440c..920e1ae 100755 --- a/build.sh +++ b/build.sh @@ -9,6 +9,7 @@ fi . ./patch_initramfs.sh . ./build_image.sh +. ./shim_utils.sh print_help() { echo "Usage: ./build.sh output_path shim_path rootfs_dir" @@ -55,34 +56,9 @@ mkdir $kernel_dir -p dd if=$kernel_loop of=$kernel_dir/kernel.bin bs=1M status=none echo "extracting data from kernel" -previous_dir=$(pwd) -cd $kernel_dir -if [ -e "${kernel_dir}/binwalk.out" ]; then - #don't run binwalk again if we don't need to - binwalk_out=$(cat $kernel_dir/binwalk.out) -else - binwalk_out=$(binwalk --extract kernel.bin --run-as=root) - echo $binwalk_out > $kernel_dir/binwalk.out -fi -#i can't be bothered to learn how to use sed -extracted_file=$(echo $binwalk_out | pcregrep -o1 "\d+\s+0x([0-9A-F]+)\s+gzip compressed data") - -echo "extracting initramfs archive from kernel (this may take a while)" -cd _kernel.bin.extracted/ -if [ ! -e "_${extracted_file}.extracted/" ]; then - binwalk --extract $extracted_file --run-as=root > /dev/null -fi -cd "_${extracted_file}.extracted/" -cpio_file=$(file ./* | pcregrep -o1 "([0-9A-F]+):\s+ASCII cpio archive") - -echo "extracting initramfs cpio archive" initramfs_dir=/tmp/shim_initramfs rm -rf $initramfs_dir -cat $cpio_file | cpio -D $initramfs_dir -imd --quiet -echo "shim initramfs extracted to ${initramfs_dir}" - -#leave /tmp -cd $previous_dir +extract_initramfs $kernel_dir/kernel.bin $kernel_dir $initramfs_dir echo "patching initramfs" patch_initramfs $initramfs_dir diff --git a/build_squashfs.sh b/build_squashfs.sh new file mode 100755 index 0000000..6e1f508 --- /dev/null +++ b/build_squashfs.sh @@ -0,0 +1,10 @@ +#!/bin/bash + +#build a rootfs that uses a squashfs + unionfs +#consists of a minimal busybox system containing: +# - FUSE kernel modules from the shim +# - unionfs-fuse statically compiled +# - the main squashfs, compressed with gzip + + +#todo - gotta refactor the other build scripts first \ No newline at end of file diff --git a/shim_utils.sh b/shim_utils.sh new file mode 100755 index 0000000..e2e5582 --- /dev/null +++ b/shim_utils.sh @@ -0,0 +1,25 @@ +#!/bin/bash + +#utilties for reading shim disk images + +extract_initramfs() { + local kernel_bin="$1" + local working_dir="$2" + local output_dir="$3" + + #first stage + local kernel_file="$(basename $kernel_bin)" + local binwalk_out=$(binwalk --extract $kernel_bin --directory=$working_dir --run-as=root) + 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" + + #second stage + binwalk --extract $stage1_path --directory=$stage1_dir --run-as=root + 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" + + rm -rf $output_dir + cat $cpio_path | cpio -D $output_dir -imd --quiet +} \ No newline at end of file