Bootloader in several files.

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
rgmf
Posts: 17
Joined: Wed Sep 28, 2016 1:45 pm

Bootloader in several files.

Post 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.
User avatar
sleephacker
Member
Member
Posts: 97
Joined: Thu Aug 06, 2015 6:41 am
Location: Netherlands

Re: Bootloader in several files.

Post 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.
User avatar
Roman
Member
Member
Posts: 568
Joined: Thu Mar 27, 2014 3:57 am
Location: Moscow, Russia
Contact:

Re: Bootloader in several files.

Post 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.
"If you don't fail at least 90 percent of the time, you're not aiming high enough."
- Alan Kay
User avatar
elderK
Member
Member
Posts: 190
Joined: Mon Dec 11, 2006 10:54 am
Location: Dunedin, New Zealand
Contact:

Re: Bootloader in several files.

Post 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
rgmf
Posts: 17
Joined: Wed Sep 28, 2016 1:45 pm

Re: Bootloader in several files.

Post 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.
User avatar
elderK
Member
Member
Posts: 190
Joined: Mon Dec 11, 2006 10:54 am
Location: Dunedin, New Zealand
Contact:

Re: Bootloader in several files.

Post 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
rgmf
Posts: 17
Joined: Wed Sep 28, 2016 1:45 pm

Re: Bootloader in several files.

Post 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 ;)
Post Reply