Page 1 of 1

relocating bootloader

Posted: Thu May 12, 2011 9:24 pm
by schilds
My bootloader appears to relocate itself and run correctly, is anyone able (and willing, thanks in advance ;)) to verify that the code does what I think it does?

https://github.com/schilds/Serial-BootL ... loader.asm

It should relocate some of itself (the main loop and some functions) to just before where it used to be (i.e. just before the boot sector, 0x7c00). The function of the relocated main loop should be to read and parse bytes from the serial port, store them to the (now free to overwrite) boot sector and then execute them.

Basically I've written the relocation as if it were position independent (i.e. works with just a simple copy), but I'm not actually sure that it is. How do I know whether the assembler is creating absolute or relative jumps/calls?

Re: relocating bootloader

Posted: Fri May 13, 2011 1:36 am
by Combuster
You can always disassemble the output and check the jump opcodes to see if they are absolute (FF) or relative (EB/E9). Most assemblers default to the latter though because it's always the shorter encoding.

Re: relocating bootloader

Posted: Fri May 13, 2011 1:50 am
by Brendan
Hi,
schilds wrote:My bootloader appears to relocate itself and run correctly, is anyone able (and willing, thanks in advance ;)) to verify that the code does what I think it does?

https://github.com/schilds/Serial-BootL ... loader.asm
You forgot to make sure that the "direction" flag is clear. If the BIOS left it set, then the copy will work backwards and copy the wrong thing.
schilds wrote:Basically I've written the relocation as if it were position independent (i.e. works with just a simple copy), but I'm not actually sure that it is. How do I know whether the assembler is creating absolute or relative jumps/calls?
It's much easier to do the reverse. For example, use "org 0x7A00" at the start so that after relocation everything is as the assembler expects, and do the relocation first so that you don't need to care about "position independent" for almost all of your code.

For example:

Code: Select all

    org 0x7A00
    bits 16

start:
    jmp .initialise

;Put a BPB here, if necessary

.initialise:
    xor ax,ax
    mov es,ax
    mov ds,ax
    cli
    mov ss,ax
    mov sp,0x7A00
    sti
    cld
    mov cx,512/2
    mov si,0x7C00
    mov di,0x7A00
    rep movsw
    jmp 0x0000:main

main:
Cheers,

Brendan

Re: relocating bootloader

Posted: Fri May 13, 2011 2:25 am
by schilds
Oooh, neat.

Thanks guys.