From 7b1e1857908a6eca5cacccd03dd9b1382443001e Mon Sep 17 00:00:00 2001 From: mykola2312 <49044616+mykola2312@users.noreply.github.com> Date: Sun, 4 Aug 2024 19:28:15 +0300 Subject: [PATCH] improve linker script --- src/mbr_test.ld | 36 +++++++++++++++++++++++------------- src/mbr_test.s | 34 ++++++++++++++++------------------ 2 files changed, 39 insertions(+), 31 deletions(-) diff --git a/src/mbr_test.ld b/src/mbr_test.ld index a4b74c9..8b8504d 100644 --- a/src/mbr_test.ld +++ b/src/mbr_test.ld @@ -1,35 +1,45 @@ MEMORY { - BOOTLOADER (RWX) : ORIGIN = 0x0000, LENGTH = 440 - UNIQUE_ID (R) : ORIGIN = 0x01B8, LENGTH = 4 - RESERVED (R) : ORIGIN = 0x01BC, LENGTH = 2 - PTABLE (R) : ORIGIN = 0x01BE, LENGTH = 64 - MBR_MAGIC (R) : ORIGIN = 0x01FE, LENGTH = 2 + RAM (RWX) : ORIGIN = 0x0000, LENGTH = 492032 } SECTIONS { - .text 0x0000 : { + RAM = 0x7E00; + RAM_SEGMENT = (RAM >> 8); + + .text 0x0000 : { BOOTLOADER_START = .; *(.text) *(.rodata) BOOTLOADER_END = .; - *(.bss) BOOTLOADER_SIZE = BOOTLOADER_END - BOOTLOADER_START; + } > RAM - RAM = 0x7E00; - RAM_SEGMENT = (RAM >> 8); - } + .stack (NOLOAD): { + . = ALIGN(8); + STACK_SIZE = 0x100; + + STACK_START = .; + . = . + STACK_SIZE; + . = ALIGN(8); + STACK_END = .; + } > RAM - .unique_id 0x01B8 : { + .bss (NOLOAD) : { + . = ALIGN(8); + *(.bss) + } > RAM + + .unique_id 0x01B8 : { SHORT(0x1234) } - .reserved 0x01BC : { + .reserved 0x01BC : { SHORT(0x0000) } - .mbr_magic 0x01FE : { + .mbr_magic 0x01FE : { SHORT(0xAA55) } } diff --git a/src/mbr_test.s b/src/mbr_test.s index 0cb9b2f..e0d99ef 100644 --- a/src/mbr_test.s +++ b/src/mbr_test.s @@ -1,17 +1,20 @@ .section .text .code16 - # we're moving to 0050, 0x7C00 -> 0x0500 - # so, DS is 0x07C0 and ES is 0x0050 - mov $0x07C0, %ax - mov %ax, %ds - mov $0x0050, %ax - mov %ax, %es + # initialize stack + mov $RAM_SEGMENT, %ax + mov %ax, %ss + mov $STACK_END, %sp + + # we're moving to 0x7C00 -> 0x7E00 + push $0x07C0 + pop %ds + push %ss + pop %es # set both source and destination pointers to zero, since we using segments - xor %ax, %ax - mov %ax, %si - mov %ax, %di + xor %si, %si + xor %di, %di # set counter with determined size by linker mov $BOOTLOADER_SIZE, %cx @@ -22,16 +25,11 @@ loop .copy # far jump to new memory region - jmp $0x0050,$.bootloader + jmp $RAM_SEGMENT,$.bootloader .bootloader: - # initialize stack - mov $0x07DF, %ax - mov %ax, %ss - xor %sp, %sp - - # set ES segment - push %ds - pop %es + # set DS segment + push %es + pop %ds .entry: call serial_init