OSDev.org

The Place to Start for Operating System Developers
It is currently Sat Apr 20, 2024 4:00 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: Bootloader in several files.
PostPosted: Sun Dec 25, 2016 1:25 pm 
Offline

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


Top
 Profile  
 
 Post subject: Re: Bootloader in several files.
PostPosted: Mon Dec 26, 2016 7:56 am 
Offline
Member
Member
User avatar

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


Top
 Profile  
 
 Post subject: Re: Bootloader in several files.
PostPosted: Mon Dec 26, 2016 9:28 am 
Offline
Member
Member
User avatar

Joined: Thu Mar 27, 2014 3:57 am
Posts: 568
Location: Moscow, Russia
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


Top
 Profile  
 
 Post subject: Re: Bootloader in several files.
PostPosted: Mon Dec 26, 2016 10:32 am 
Offline
Member
Member
User avatar

Joined: Mon Dec 11, 2006 10:54 am
Posts: 190
Location: Dunedin, New Zealand
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


Top
 Profile  
 
 Post subject: Re: Bootloader in several files.
PostPosted: Mon Dec 26, 2016 3:04 pm 
Offline

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


Top
 Profile  
 
 Post subject: Re: Bootloader in several files.
PostPosted: Mon Dec 26, 2016 10:28 pm 
Offline
Member
Member
User avatar

Joined: Mon Dec 11, 2006 10:54 am
Posts: 190
Location: Dunedin, New Zealand
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


Top
 Profile  
 
 Post subject: Re: Bootloader in several files.
PostPosted: Tue Dec 27, 2016 6:58 am 
Offline

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


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 19 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group