:: commit 98086e38293724a47b6e3d95296e84b229069722

mintsuki <mintsuki@protonmail.com> — 2020-04-15 08:14

parents: 609f9d86de

Add option to change where stage 2 is located

diff --git a/qloader2-install b/qloader2-install
index c3ca44bf..3a183081 100755
--- a/qloader2-install
+++ b/qloader2-install
@@ -1,21 +1,33 @@
 #!/bin/sh
 
-set -e
+set -ex
 
-# We take 1 argument, the device to use.
 if [ -z "$2" ]; then
-    echo "Usage: $0 <path to qloader2 binary> <device>"
+    echo "Usage: $0 <path to qloader2 binary> <device> [stage2 start sector]"
     exit 1
 fi
 
 # Variables.
 DEVICE="$2"
 MBR="$(mktemp)"
+BINARY_SECT_SRC="$(mktemp)"
+BINARY_SECT_BIN="$(mktemp)"
 QLOADER2="$1"
 
+if [ -z "$3" ]; then
+    STAGE2_SECT=1
+else
+    STAGE2_SECT="$3"
+fi
+
+echo "dd $STAGE2_SECT" > "$BINARY_SECT_SRC"
+nasm "$BINARY_SECT_SRC" -f bin -o "$BINARY_SECT_BIN"
+
 # Copy the loader to the device.
-dd if="$DEVICE"   of="$MBR"    bs=1 count=64 skip=446
-dd if="$QLOADER2" of="$DEVICE" conv=notrunc
-dd if="$MBR"      of="$DEVICE" conv=notrunc bs=1 count=64 seek=446
+dd if="$DEVICE"          of="$MBR"    bs=1   count=64 skip=446
+dd if="$QLOADER2"        of="$DEVICE" bs=512 count=1  conv=notrunc
+dd if="$QLOADER2"        of="$DEVICE" bs=512 count=63 skip=1   seek=$(( $STAGE2_SECT )) conv=notrunc
+dd if="$BINARY_SECT_BIN" of="$DEVICE" bs=1   count=4  seek=$(( 0x1b0 )) conv=notrunc
+dd if="$MBR"             of="$DEVICE" bs=1   count=64 seek=446 conv=notrunc
 
-rm "$MBR"
+rm "$MBR" "$BINARY_SECT_SRC" "$BINARY_SECT_BIN"
diff --git a/src/Makefile b/src/Makefile
index 664e056a..797a4df0 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -30,15 +30,12 @@ OBJ := $(C_FILES:.c=.o)
 
 all: qloader2.bin
 
-qloader2.bin: bootsect/bootsect.bin $(OBJ)
+qloader2.bin: $(OBJ)
 	$(CC) $(OBJ) $(LDFLAGS) $(INTERNAL_LDFLAGS) -o stage2.bin
-	cat bootsect/bootsect.bin stage2.bin > $@
-
-bootsect/bootsect.bin: bootsect/bootsect.asm
-	cd bootsect && nasm bootsect.asm -fbin -o bootsect.bin
+	cd bootsect && nasm bootsect.asm -fbin -o ../qloader2.bin
 
 %.o: %.c
 	$(CC) $(CFLAGS) $(INTERNAL_CFLAGS) -c $< -o $@
 
 clean:
-	rm -f $(OBJ) bootsect/bootsect.bin
+	rm -f $(OBJ)
diff --git a/src/bootsect/bootsect.asm b/src/bootsect/bootsect.asm
index 69802afb..641bff89 100644
--- a/src/bootsect/bootsect.asm
+++ b/src/bootsect/bootsect.asm
@@ -1,4 +1,4 @@
-org 0x7C00
+org 0x7c00
 bits 16
 
 start:
@@ -23,7 +23,7 @@ start:
     mov si, Stage2Msg
     call simple_print
 
-    mov eax, 1
+    mov eax, dword [stage2_sector]
     mov ebx, 0x7e00
     mov ecx, 1
     call read_sectors
@@ -65,6 +65,9 @@ times 6 db 0
 %include 'simple_print.inc'
 %include 'disk.inc'
 
+times 0x1b0-($-$$) db 0
+stage2_sector: dd 1
+
 times 0x1b8-($-$$) db 0
 times 510-($-$$) db 0
 dw 0xaa55
@@ -72,19 +75,17 @@ dw 0xaa55
 ; ********************* Stage 2 *********************
 
 stage2:
-    ; Load stage 3
-    mov eax, 2
+    mov eax, dword [stage2_sector]
+    inc eax
     mov ebx, 0x8000
     mov ecx, 62
     call read_sectors
     jc err_reading_disk
 
-    ; Enable A20
     call enable_a20
     jc err_enabling_a20
 
-    ; Enter 32 bit pmode
-    lgdt [GDT]						; Load the GDT
+    lgdt [GDT]
 
     cli
 
@@ -109,3 +110,7 @@ bits 16
 %include 'gdt.inc'
 
 times 1024-($-$$) db 0
+
+incbin '../stage2.bin'
+
+times 32768-($-$$) db 0
diff --git a/src/linker.ld b/src/linker.ld
index 8767b209..2c5167f5 100644
--- a/src/linker.ld
+++ b/src/linker.ld
@@ -17,10 +17,6 @@ SECTIONS
 
     .data : {
         *(.data*)
-        . += 31744 - (. - bootsect_begin);  /* Limit how big the thing can get
-                                               31744 = 32768 - (512 * 2)
-                                               32KiB of MBR gap minus 2 sectors
-                                               for stages 1 and 2 */
     }
 
     .bss : {
tab: 248 wrap: offon