Page 1 of 1

Trying to chain load Ubuntu Boot Loader from my own Boot loa

Posted: Tue Feb 04, 2014 1:43 pm
by oorosco2
To sum up, I wrote a boot loader, it starts from a "floppy disk" and prints out a message, I'm using Oracle VM box to emulate a computer. Now after it prints the message I'd like to launch the installed Ubuntu... now I'm a tad confused because the one bit of information I found was to "move" my code from [ORG 0x7c00] other than that I've found nothing else online, any help would be much appreciated!

Re: Trying to chain load Ubuntu Boot Loader from my own Boot

Posted: Tue Feb 04, 2014 1:59 pm
by iansjack
Well, you need to write code that understands the file system that your Ubuntu kernel is installed on, parse an ELF file and load the sections in the appropriate places in memory, switch the processor to Protected Mode with a minimal GDT and then jump to the start of the Ubuntu kernel. That's assuming the simple case of a 32-bit kernel. If your Ubuntu is 64-bit there a bit more work to do.

You'll also have to pass a bit more information to the kernel telling it where your root file system is located, but that's enough work to be getting on with.

All in all, it's easier just to use Grub. Writing a boot loader to run a Linux kernel is not a trivial undertaking.

Re: Trying to chain load Ubuntu Boot Loader from my own Boot

Posted: Tue Feb 04, 2014 3:38 pm
by xenos
Uh, I think the OP wants to chain load Ubuntu, which would rather be loading the Ubuntu boot sector to 0x7C00 and jumping there, just as if the BIOS had done this... And yes, in order to do that, one first needs to move ones own boot sector to a different place. So the easiest think would be to copy 512 bytes from 0x7C00 to some other place, jump to that copy, load the Ubuntu boot sector to 0x7C00 and jump to it.

Re: Trying to chain load Ubuntu Boot Loader from my own Boot

Posted: Tue Feb 04, 2014 3:43 pm
by iansjack
Ah, well that would be a bit easier. Just a question of reading the partition table to find out where the Ubuntu partition is (somehow you have to tell the boot loader which partition that is, unless you hard code it), read the boot sector from that partition, etc.

It still seems to me to be a lot of work when you can just use Grub.

Re: Trying to chain load Ubuntu Boot Loader from my own Boot

Posted: Tue Feb 04, 2014 6:38 pm
by mathematician
If Ubuntu is present on the hard disk, your boot loader will be just one more link in the chain of boot loaders, and it will need to do what the bios normally does. That is, it will need to read the hard disks master boot record into memory at 0x7c00, load the value 0x80 into the dl register, and jump to the code it has just loaded. In order to do that it will need to relocate itself, which is what the following section of code accomplishes.

Code: Select all

org 600h

cld
cli
xor ax,ax
mov ds,ax
mov es,ax
mov ss,ax
mov sp, 7c00h

push ax					;push seg address of relocated code
push offset relocate			;push offset address of relocated code
mov si, 7c00h				;ds:si -> current location of boot sector
mov di, 600h				;es:di -> eventual destination
mov cx, 256				;number of two byte words in boot sector
rep movsw				        ;relocate boot sector
retf					        ;jump to relocated code

relocate:
;the rest of your code goes here.

Re: Trying to chain load Ubuntu Boot Loader from my own Boot

Posted: Wed Feb 05, 2014 1:54 am
by xenos
@mathematician: I think your code needs an ORG 600h somewhere at the beginning, otherwise it won't work...

Re: Trying to chain load Ubuntu Boot Loader from my own Boot

Posted: Wed Feb 05, 2014 9:00 am
by mathematician
XenOS wrote:@mathematician: I think your code needs an ORG 600h somewhere at the beginning, otherwise it won't work...
Corrected.

Re: Trying to chain load Ubuntu Boot Loader from my own Boot

Posted: Wed Feb 05, 2014 9:54 am
by Griwes
I'd like to shed some light on the context of this user asking his question.

He came to C++ chat roo-- I'm sorry, to C++ Lounge on Stack Overflow and asked this question there (why? I have no sincere idea. Probably he googled that we sometimes talked about architecture or OS level or bootloader level stuff, I have no idea). He was linked to the page about bootloaders on the OSDev wiki, specifically to the section about chainloading (which highlights the relevant parts of chainloading process). He then said that he has to google every single term used there. I gave him the steps a chainloader needs to perform in more compact version than on the wiki, it didn't help.

Also keep in mind that the assignment he got is "write a bootloader that prints text" or something; then he proceeded, for some unknown reason he couldn't explain, to chainloading Ubuntu bootloader (we even got through the "oh, so you want to *load*, not chainload Ubuntu?" phase, just to get to "oh, so you really mean chainloading", and OP continued to confirm what we say).

I'd like to encourage the community not to spoon feed this one for any reason, as the questions he will ask will be the same that are already answered elsewhere. Also, referring to 0x7c00 physical address as "[org 0x7c00]" shows that he needs to be pointed in the direction of assembly and real mode addressing basics, NOT anywhere near the direction of bootloaders.