Page 1 of 1

GRUB src code: forced reference?

Posted: Sat Jun 23, 2012 11:03 pm
by kikou
I was looking at grub2 stage1 source code and saw this:

Code: Select all

        /*
         *  Check if we have a forced disk reference here
         */
        movb   boot_drive, %al
        cmpb    $0xff, %al
        je      1f
        movb    %al, %dl
1:
"boot_drive" is defined in the same file as follows:

Code: Select all

boot_drive:
        .byte 0xff      /* the disk to load kernel from */
                        /* 0xff means use the boot drive */
First of all what is a "forced disk reference"?
The code above doesn't make sense to me. It loads %al with 0xff, then compares the same %al with 0xff, then jumps to 1. In what case movb %al, %dl is executed?
By the way, is 0xff a drive code used in INT 13H? Is there a list of those codes somewhere?

Here is the complete source code, just in case: http://paste.ideaslabs.com/show/VekL2gH914

Re: GRUB src code: forced reference?

Posted: Sun Jun 24, 2012 12:09 am
by sounds
The stage1 loader is compiled with 0xff at the location boot_drive:

As an option, the installer (also known as grub) will patch the 0xff there and write a different byte. Then when the stage1 loader runs, it will prefer that value and ignore the value in %dl

This is to fix bugs where the value supplied in %dl sends stage1 off looking for stage1.5 someplace where it can't find it, and preventing grub from booting.

Re: GRUB src code: forced reference?

Posted: Sun Jun 24, 2012 1:34 am
by shikhin
Hi,
sounds wrote:As an option, the installer (also known as grub) will patch the 0xff there and write a different byte. Then when the stage1 loader runs, it will prefer that value and ignore the value in %dl.

This is to fix bugs where the value supplied in %dl sends stage1 off looking for stage1.5 someplace where it can't find it, and preventing grub from booting.
Then, shouldn't that be

Code: Select all

jne 1f
, so that when the installer has patched the 0xFF and written a different byte, it prefers the different byte, and in case it is 0xFF, it uses the value in %dl?

Just asking..

Regards,
Shikhin

Re: GRUB src code: forced reference?

Posted: Sun Jun 24, 2012 2:48 am
by jnc100
Shikhin wrote:Then, shouldn't that be

Code: Select all

jne 1f
, so that when the installer has patched the 0xFF and written a different byte, it prefers the different byte, and in case it is 0xFF, it uses the value in %dl?
It's gas syntax. The 'output' from this little snippet is in dl. If boot_drive is not 0xff then dl is overwritten with whatever it contains, otherwise dl is left as it was set up by the bios.

Regards,
John.

Re: GRUB src code: forced reference?

Posted: Sun Jun 24, 2012 10:20 am
by kikou
sounds wrote:This is to fix bugs where the value supplied in %dl sends stage1 off looking for stage1.5 someplace where it can't find it, and preventing grub from booting.
But how does the grub installer know that a wrong value will be passed to %dl? And does the value it puts on boot_drive address corresponds to the device it is being installed, like 0x80 for hdd?