Page 1 of 1

Bootloader in several files.

Posted: Sun Dec 25, 2016 1:25 pm
by rgmf
Hello,

I am writing a simple bootloader following the wiki (http://wiki.osdev.org/Rolling_Your_Own_Bootloader). At this moment, the bootloader do:
- Memory detection.
- Test and enable A20 gate.

At this point I think that would be interesting to split the bootloader in several files to put all about gate in a separate file. I tried it but there is something wrong because the behaviour of the bootloader is odd.

All I did was put a a20_enable function in a separate file (a20_gate.S) and then I build all project:

Code: Select all

floppy.img: bootloader.bin os.bin
	dd if=bootloader.bin of=floppy.img
	dd if=os.bin of=floppy.img seek=1 bs=512

bootloader.bin: bootloader.o a20_gate.o
	ld -Ttext=0x7c00 --oformat=binary bootloader.o a20_gate.o -o bootloader.bin

bootloader.o: bootloader.S
	as bootloader.S -o bootloader.o

a20_gate.o: a20_gate.S
        as a20_gate.S -o a20_gate.o

a20_gate.o: a20_gate.S
	as a20_gate.S -o a20_gate.o

os.bin: os.o bootloader.o
	ld -Ttext=0x1000 --oformat=binary os.o -o os.bin

os.o: os.S
	as os.S -o os.o

bochs: bochsrc.txt
	bochs -f bochsrc.txt -q

clean:
	rm *.o *.bin
I can see that SS and SP register are set with a value that I don't know where come from (and I think is from call instruction ¿?¿?). If the a20_enable function is in the bootloader.S file then nothing wrong happen but if the a20_enable function is in a separate file does not work.

Is a far jump issue or something like that? I need a hint to know where looking.

Regards.

Re: Bootloader in several files.

Posted: Mon Dec 26, 2016 7:56 am
by sleephacker
To split up an assembly project into multiple files I recommend you to just use the include directive, this will tell the assembler to treat e.g. "a20_gate.S" file as if it were part of "bootloader.S". That way you don't have to assemble the two files separately and hot-glue the binaries together.

Re: Bootloader in several files.

Posted: Mon Dec 26, 2016 9:28 am
by Roman
Have you tried disassembling your bootloader.bin? The linker might have reordered your code somehow, so the entry point is no longer in the beginning. Consider using 'include' as suggested above.

Re: Bootloader in several files.

Posted: Mon Dec 26, 2016 10:32 am
by elderK
Hey,

You should read into linker scripts. Those allow you to specify where the contents of various object files should be placed in a resulting binary.

https://sourceware.org/binutils/docs/ld/Scripts.html

Also regarding your makefile, you may also want to read up on pattern rules :)

I think it'll save you a bunch of typing and duplication :)
https://www.gnu.org/software/make/manua ... Rules.html

~elderK

Re: Bootloader in several files.

Posted: Mon Dec 26, 2016 3:04 pm
by rgmf
Roman wrote:Have you tried disassembling your bootloader.bin? The linker might have reordered your code somehow, so the entry point is no longer in the beginning. Consider using 'include' as suggested above.
sleephacker wrote:To split up an assembly project into multiple files I recommend you to just use the include directive, this will tell the assembler to treat e.g. "a20_gate.S" file as if it were part of "bootloader.S". That way you don't have to assemble the two files separately and hot-glue the binaries together.
Thank you for your advice.
elderK wrote:Hey,

You should read into linker scripts. Those allow you to specify where the contents of various object files should be placed in a resulting binary.

https://sourceware.org/binutils/docs/ld/Scripts.html

Also regarding your makefile, you may also want to read up on pattern rules :)

I think it'll save you a bunch of typing and duplication :)
https://www.gnu.org/software/make/manua ... Rules.html

~elderK
Thank you, I am reading the documentation about linker and makefile because my knowledge about these topics are pretty bad. Anyway I am learning a lot with the osdev wiki.

Re: Bootloader in several files.

Posted: Mon Dec 26, 2016 10:28 pm
by elderK
Perhaps studying some of my code will help:
https://bitbucket.org/elderK/spell

You'll want to clone that sooner that later if you're interested as I intend to erase it in a day or so :)

~elderK

Re: Bootloader in several files.

Posted: Tue Dec 27, 2016 6:58 am
by rgmf
elderK wrote:Perhaps studying some of my code will help:
https://bitbucket.org/elderK/spell

You'll want to clone that sooner that later if you're interested as I intend to erase it in a day or so :)

~elderK
Thank you. I will look at it. All code I can read is welcome ;)