Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should the stack be initialized to in an x86 real mode bootloader to prevent conflicts with BIOS?

Suppose I want to initialize the stack to a size of S bytes.

I would like to chose the base position of the stack B so that as the stack grows downward from B, I do not end up overwriting any code or other memory being used by the bootloader or the BIOS.

Since I am writing the bootloader myself (and the initial MBR sector is loaded to linear address 0x7c00), preventing conflicts with the bootloader seems like a matter of careful planning.

How do I know where the BIOS code is located, and if my stack may be overwriting any memory being used by the BIOS?

Also, is there any guarantee about where the initial ss:sp is pointing, and how much stack space can I safely use without setting a new value?

like image 578
Diggs Avatar asked Apr 18 '20 22:04

Diggs


1 Answers

Typically, a bootloader will set ss:sp to 00000h:07c00h:. I did a web search, and apparently what ss:sp is set to when the BIOS jumps to the boot loader depends on the BIOS. In the case of Microsoft MBR, the bootloader sets ss:sp to 00000h:07c00h, and transfers most of its code to 00000h:00600h, and jumps to 00000h:006xxh. That in turn looks at the partition table to find a bootable sector and repeats the process, load and jump to 00000h:07c00h. This may repeat one more time. Eventually the boot process will load several sectors and start the actual boot of an Microsoft operating system. I don't know the sequence for Linux type operating systems.

like image 153
rcgldr Avatar answered Nov 06 '22 23:11

rcgldr