write linker script for MBR sector, as well as test MBR code

This commit is contained in:
mykola2312 2024-08-03 02:29:03 +03:00
parent 818b222f2a
commit d62f1dad13
3 changed files with 74 additions and 1 deletions

View file

@ -1,11 +1,19 @@
CC = gcc
AS = as
LD = ld
OBJCOPY = objcopy
CLFAGS = -g -Wall
ASFLAGS =
LDFLAGS = -g
SRC_DIR = src
OBJ_DIR = obj
BIN_DIR = bin
UID := $(shell id -u)
GID := $(shell id -g)
DISK = bin/disk.img
DISK = $(BIN_DIR)/disk.img
DISK_SIZE = 268435456
DISK_LOOP = /dev/loop690
@ -84,6 +92,19 @@ $(DISK): mnt
sudo losetup -d $(PART3_LOOP)
sudo losetup -d $(DISK_LOOP)
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.s
$(AS) $(ASFLAGS) -o $@ $<
$(OBJCOPY) --remove-section .note.gnu.property $@
MBR_TEST_OBJ = obj/mbr_test.o
mbr_test: $(MBR_TEST_OBJ) $(DISK)
$(LD) -T src/mbr_test.ld -o $(BIN_DIR)/mbr_test.bin $(MBR_TEST_OBJ)
mbr_test_clean:
rm $(BIN_DIR)/mbr_test.bin
rm $(OBJ_DIR)/mbr_test.o
clean:
rm -f bin/*
rm -f obj/*

31
src/mbr_test.ld Normal file
View file

@ -0,0 +1,31 @@
MEMORY
{
BOOTLOADER (RWX) : ORIGIN = 0x7C00, LENGTH = 440
UNIQUE_ID (R) : ORIGIN = 0x7DB8, LENGTH = 4
RESERVED (R) : ORIGIN = 0x7DBC, LENGTH = 2
PTABLE (R) : ORIGIN = 0x7DBE, LENGTH = 64
MBR_MAGIC (R) : ORIGIN = 0x7DFE, LENGTH = 2
}
SECTIONS
{
. = 0x7C00;
.text : {
*(.text)
*(.data)
} > BOOTLOADER
.unique_id : {
SHORT(0x1234)
} > UNIQUE_ID
.reserved : {
SHORT(0x0000)
} > RESERVED
.mbr_magic : {
SHORT(0xAA55)
} > MBR_MAGIC
}
OUTPUT_FORMAT(binary)

21
src/mbr_test.s Normal file
View file

@ -0,0 +1,21 @@
.section .text
.code16
mov $0x07C0, %ax
mov %ax, %dx
mov msg, %si
.putc_loop:
lodsb
or %al, %al
jz .halt
mov $0x0E, %ah
mov $0, %al
int $0x10
jmp .putc_loop
.halt:
hlt
.section .data
msg: .asciz "Test MBR hello world\x0D\x0A" # len 22