make kernel bootable

This commit is contained in:
ading2210 2023-10-03 10:20:41 -07:00
parent 2db4cdc814
commit ffbe350dd4
3 changed files with 11 additions and 4 deletions

View File

@ -13,7 +13,7 @@ set -x
. /lib/init.sh
setup_environment() {
initialize
#initialize
# Install additional utility programs.
/bin/busybox --install /bin || true

View File

@ -15,7 +15,7 @@ print_help() {
}
check_deps() {
local needed_commands="cpio binwalk pcregrep realpath"
local needed_commands="cpio binwalk pcregrep realpath cgpt"
for command in $needed_commands; do
if ! command -v $command &> /dev/null; then
echo $command
@ -61,7 +61,7 @@ binwalk_out=$(binwalk --extract kernel.bin --run-as=root)
#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"
echo "extracting initramfs archive from kernel (this may take a while)"
cd _kernel.bin.extracted/
binwalk --extract $extracted_file --run-as=root > /dev/null
cd "_${extracted_file}.extracted/"

View File

@ -17,6 +17,11 @@ make_mountable() {
printf '\000' | dd of=$1 seek=$((0x464 + 3)) conv=notrunc count=1 bs=1
}
#set required flags on the kernel partition
make_bootable() {
cgpt add -i 2 -S 1 -T 5 -P 10 -l kernel $1
}
partition_disk() {
local image_path=$(realpath "${1}")
local bootloader_size=${2}
@ -61,7 +66,7 @@ partition_disk() {
}
safe_mount() {
umount $2 || /bin/true
umount $2 2> /dev/null || /bin/true
rm -rf $2
mkdir -p $2
mount $1 $2
@ -75,6 +80,7 @@ create_partitions() {
mkfs.ext4 "${image_loop}p1"
#copy kernel
dd if=$kernel_path of="${image_loop}p2" bs=1M oflag=sync
make_bootable $image_loop
#create bootloader partition
mkfs.ext2 "${image_loop}p3"
#create rootfs partition
@ -117,6 +123,7 @@ create_image() {
partition_disk $image_path $bootloader_size
}
#for testing only
if [ $0 == "./build_image.sh" ]; then
create_image ./test.bin 20 200
image_loop=$(create_loop ./test.bin)