Bootloader location in memory

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
tomik2256
Posts: 1
Joined: Mon Jul 14, 2014 2:38 pm

Bootloader location in memory

Post by tomik2256 »

What is location of bootloader in RAM memory? I have two computer with UEFI and I'm using legacy BIOS settings in both but it wont load into 0x7C00. How do I know where is bootloader loaded?
azblue
Member
Member
Posts: 147
Joined: Sat Feb 27, 2010 8:55 pm

Re: Bootloader location in memory

Post by azblue »

How do you know it's not loading to 0x7c00? You implied you're on real hardware rather than an emulator, which I would think would make it rather difficult to know what's where. What does your boot code look like? Have you tried running it in an emulator where you can more clearly see what's happening?
User avatar
Muazzam
Member
Member
Posts: 543
Joined: Mon Jun 16, 2014 5:59 am
Location: Shahpur, Layyah, Pakistan

Re: Bootloader location in memory

Post by Muazzam »

If you want to know bootloader location just do a call and pop IP from the stack, and push it again and ret, IP (Instruction Pointer) register is location of your code into memory. There are many ways to access IP. But normally it is not accessible.
Last edited by Muazzam on Sat Jan 24, 2015 7:25 am, edited 1 time in total.
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: Bootloader location in memory

Post by bluemoon »

Also note that most loader use the address 00000:7C00, but it's totally legal if its not, indeed some buggy BIOS put you to 07C0:0000.

The loader code therefore should be position independent, at least in the entrance before it setup a useful memory layout.
Gigasoft
Member
Member
Posts: 856
Joined: Sat Nov 21, 2009 5:11 pm

Re: Bootloader location in memory

Post by Gigasoft »

That's the exact same location. All PC BIOSes will load the boot sector to address 7c00h. If it then jumps to 07c0:0000 rather than 0000:7c00, it does not matter. Just don't depend on the value of CS, or use a jmp 0:xxxx instruction to fix it.
User avatar
iansjack
Member
Member
Posts: 4709
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Bootloader location in memory

Post by iansjack »

It may be the same location, but it is by no means the same thing. Suppose, for example, the boot loader was 0x9000 bytes long.
User avatar
Schol-R-LEA
Member
Member
Posts: 1925
Joined: Fri Oct 27, 2006 9:42 am
Location: Athens, GA, USA

Re: Bootloader location in memory

Post by Schol-R-LEA »

Gigasoft wrote:That's the exact same location. All PC BIOSes will load the boot sector to address 7c00h. If it then jumps to 07c0:0000 rather than 0000:7c00, it does not matter.
Actually, while it is true that 0000:7C00 and 07C0:0000 are the same physical address, it potentially does matter how the segment and address are arranged. If you have assembled the code to use one set of segment:base addresses, and the code is loaded differently from that, then any absolute addresses will not be aligned. Fortunately, most of the jumps are relative to the current IP, including non-FAR CALLs, but there are situations where absolute segment-local addresses do rear their heads.

The solution is to adjust the segment registers as the first action of the boot sector, something that you should be doing anyway. Something like this is advisable (assuming NASM):

Code: Select all

[bits 16]
org 0x7c00

entry:
    mov cs, 0x0000
    jmp far cs:adjust        ; force the IP to the correct offset
adjust:
    mov ss, my_stack_bottom  ; set the stack to a known starting point
    mov ds, cs               ; have the local data segment overlap the code segment
    mov es, my_second_stage  ; set up any other segments you are using, as needed
This will ensure that the segment layout is what you want it to be.

tomik2256: As has been mentioned before, if you aren't already, it is highly recommended that you test your code using an emulator (e.g., Bochs, QEMU) or a virtualizer (e.g., VirtualBox, Microsoft Virtual PC) before testing on live hardware. You'll get a much faster turn-around for debugging and development, and have a safer development environment (since it can't trash your hard disks). Bochs in particular is simple to use and has built-in debugging, making it a good first-line testing environment.
Rev. First Speaker Schol-R-LEA;2 LCF ELF JAM POEE KoR KCO PPWMTF
Ordo OS Project
Lisp programmers tend to seem very odd to outsiders, just like anyone else who has had a religious experience they can't quite explain to others.
Icee
Member
Member
Posts: 100
Joined: Wed Jan 08, 2014 8:41 am
Location: Moscow, Russia

Re: Bootloader location in memory

Post by Icee »

Schol-R-LEA wrote:Something like this is advisable (assuming NASM):
The idea is correct, except that there are no "mov sr, imm16" or "mov sr, sr" instructions in the x86 ISA, and "jmp far" expects a pair of immediates.
User avatar
Schol-R-LEA
Member
Member
Posts: 1925
Joined: Fri Oct 27, 2006 9:42 am
Location: Athens, GA, USA

Re: Bootloader location in memory

Post by Schol-R-LEA »

Ah, thank you for the corrections; I was working from memory, and it's been a while.
Rev. First Speaker Schol-R-LEA;2 LCF ELF JAM POEE KoR KCO PPWMTF
Ordo OS Project
Lisp programmers tend to seem very odd to outsiders, just like anyone else who has had a religious experience they can't quite explain to others.
Gigasoft
Member
Member
Posts: 856
Joined: Sat Nov 21, 2009 5:11 pm

Re: Bootloader location in memory

Post by Gigasoft »

Schol-R-LEA wrote:Actually
The word "actually" is reserved for those who can actually be bothered to read all four sentences of what they're replying to.
Post Reply