Browse Source

chore: add build-img script for making images

greatbridf 3 months ago
parent
commit
c561df82d2
4 changed files with 54 additions and 16 deletions
  1. 1 12
      CMakeLists.txt
  2. 13 0
      Makefile.src
  3. 5 4
      configure
  4. 35 0
      script/build-img.sh

+ 1 - 12
CMakeLists.txt

@@ -85,18 +85,7 @@ add_custom_command(OUTPUT mbr_hole.bin
 add_custom_target(boot.img
     DEPENDS mbr_hole.bin
     DEPENDS user_space_programs
-    COMMAND dd if=mbr_hole.bin of=boot.img
-    COMMAND dd if=/dev/zero of=boot.img bs=`expr 512 \\* 1024 \\* 1024` count=0 seek=1
-    COMMAND sh -c \"echo n\; echo\; echo \; echo 8192\; echo\; echo a\; echo w\" | ${FDISK_BIN} boot.img
-    COMMAND mkfs.fat --offset=8192 -v -n SYSTEM boot.img
-    COMMAND mcopy -i boot.img@@4M ${CMAKE_BINARY_DIR}/user-space-program/hello-world.out ::hello
-    COMMAND mcopy -i boot.img@@4M ${CMAKE_BINARY_DIR}/user-space-program/interrupt-test.out ::int
-    COMMAND mcopy -i boot.img@@4M ${CMAKE_BINARY_DIR}/user-space-program/stack-test.out ::stack
-    COMMAND mcopy -i boot.img@@4M ${CMAKE_BINARY_DIR}/user-space-program/init.out ::init
-    COMMAND mcopy -i boot.img@@4M ${CMAKE_BINARY_DIR}/user-space-program/priv-test.out ::priv
-    COMMAND mcopy -i boot.img@@4M ${CMAKE_SOURCE_DIR}/busybox-minimal ::busybox_
-    COMMAND mcopy -i boot.img@@4M ${CMAKE_SOURCE_DIR}/busybox ::busybox
-    COMMAND mcopy -i boot.img@@4M ${CMAKE_SOURCE_DIR}/init_script.sh ::initsh
+    COMMAND make -C ${CMAKE_SOURCE_DIR} image
 )
 
 add_custom_command(OUTPUT run

+ 13 - 0
Makefile.src

@@ -7,6 +7,8 @@ QEMU_ARGS=-machine q35 -drive id=disk,file=build/boot.img,format=raw,if=none \
 	-device ahci,id=ahci -device ide-hd,drive=disk,bus=ahci.0 -smp 4 \
 	-no-reboot -no-shutdown $(QEMU_ACCELERATION_FLAG) $(QEMU_DEBUG_FLAG)
 
+FDISK_BIN ?= ##PLACEHOLDER_5##
+
 CROSS_COMPILE=##PLACEHOLDER_4##
 .PHONY: run
 run: build
@@ -60,6 +62,17 @@ tmux-debug:
 	-tmux attach -t gbos-debug
 	tmux kill-session -t gbos-debug
 
+build/fs.img:
+	sh script/build-img.sh
+
+build/boot.img: build/fs.img build/mbr_hole.bin
+	dd if=build/mbr_hole.bin of=build/boot.img
+	dd if=build/fs.img of=build/boot.img bs=$(shell expr 4 \* 1024 \* 1024) seek=1 conv=notrunc
+	sh -c 'echo n; echo; echo; echo 8192; echo; echo a; echo w' | $(FDISK_BIN) build/boot.img
+
 build/boot.vdi: build/boot.img
 	-rm build/boot.vdi
 	VBoxManage convertfromraw $< $@ --format VDI
+
+.PHONY: image
+image: build/boot.img

+ 5 - 4
configure

@@ -144,8 +144,9 @@ else
 fi
 
 cp Makefile.src Makefile
-sed -i '' -e "s/##PLACEHOLDER_1##/$QEMU/" Makefile > /dev/null 2>&1
-sed -i '' -e "s/##PLACEHOLDER_2##/$GDB/" Makefile > /dev/null 2>&1
-sed -i '' -e "s/##PLACEHOLDER_3##/$QEMU_ACCEL/" Makefile > /dev/null 2>&1
-sed -i '' -e "s/##PLACEHOLDER_4##/$CROSS_COMPILE_FLAG/" Makefile > /dev/null 2>&1
+sed -i '' -e "s|##PLACEHOLDER_1##|$QEMU|" Makefile > /dev/null 2>&1
+sed -i '' -e "s|##PLACEHOLDER_2##|$GDB|" Makefile > /dev/null 2>&1
+sed -i '' -e "s|##PLACEHOLDER_3##|$QEMU_ACCEL|" Makefile > /dev/null 2>&1
+sed -i '' -e "s|##PLACEHOLDER_4##|$CROSS_COMPILE_FLAG|" Makefile > /dev/null 2>&1
+sed -i '' -e "s|##PLACEHOLDER_5##|$FDISK_BIN|" Makefile > /dev/null 2>&1
 exit 0

+ 35 - 0
script/build-img.sh

@@ -0,0 +1,35 @@
+#!/bin/sh
+
+OS=`uname -s`
+
+dd if=/dev/zero of=build/fs.img bs=`expr 1024 \* 1024` count=512
+mkfs.fat -n SYSTEM build/fs.img
+
+if [ "$OS" = "Darwin" ]; then
+    hdiutil detach build/mnt > /dev/null 2>&1 || true
+    hdiutil attach build/fs.img -mountpoint build/mnt
+else
+    mkdir -p build/mnt
+    sudo mount disk.img build/mnt
+fi
+
+cp build/user-space-program/hello-world.out build/mnt/hello
+cp build/user-space-program/interrupt-test.out build/mnt/int
+cp build/user-space-program/stack-test.out build/mnt/stack
+cp build/user-space-program/init.out build/mnt/init
+cp build/user-space-program/priv-test.out build/mnt/priv
+cp ./busybox build/mnt/busybox
+cp ./busybox-minimal build/mnt/busybox_
+cp ./init_script.sh build/mnt/initsh
+
+# Add your custom files here
+
+cp -r $HOME/.local/i486-linux-musl-cross build/mnt/
+
+# End of custom files
+
+if [ "$OS" = "Darwin" ]; then
+    hdiutil detach build/mnt
+else
+    sudo umount build/mnt
+fi