Monday, June 8, 2009

Thinking About Network Driver for Grub, Part II

In Part I, I had screwed up my bootloader and could no longer boot into Ubuntu. Fortunately, that problem was easy to fix with a bit of a side-effect.

I booted up the Ubuntu Live CD. From there:
sudo grub
which put me into the Grub thingy:
root (hd0,2) setup (hd0)
did the trick. The side effect--instead of starting to boot into the Windows bootloader, it starts to boot into the Ubuntu bootloader. So, apparently it touched the first partition. I was hoping the root (hd0,2) would make it not touch anything but the sda3 partition. Maybe it just changed the active partition? I should look into that.

Anyway, I can back into Ubuntu and Windows. Whew!

Time to put on my thinking cap. I found a couple of sites that said that when you do the netboot, stage1 loads stage2 from tftpserver. Huh?!?! Stage1 is 512 bytes and that includes the partition table. I don't believe it. I looked at the code. Stage 1 is all in assembly and makes a few int $0x13 calls, but there is nothing that indicates it can set up a tftp session. And the makefile defined NETBOOT macros but doesn't use them. They must mean that nbgrub is loaded during stage2.

Anyway, I modified my /boot/grub/menu.lst on my HDD Ubuntu drive and explicitly rebooted from the USB stick. It used the modified menu.lst, so I know that wherever stage1 is coming from, stage2 is definitely coming from the HDD. There are a few possibilities:
  1. The USB key stage1 is loading the HDD stage2
  2. There is an error and the fallback is the HDD
  3. The BIOS doesn't actually boot from the USB key event though it's in the list
I decided to test hypothesis 1 by changing my BIOS boot order to USB Stick, CD/DVD, Harddisk. When I boot with the stick in, it leapfrogs the CD/DVD. So it looks like my stick's stage1 is loading from the HDD.

What abuout grub itself. I learned a new command: which. I can type:
which grub
and it will tell me where it find the grub it's running. It's the system's grub. When I try to explicitly run my compiled grub, I get a segmentation fault. Running it under gdb, I find it's crashing because of a corrupted pointer in grub_printf when it tries to handle the %s by passing the incoming string to grub_putstr.

It seems gcc once compiled:
void my_printf(const char* format, ...)
{
int* dataptr = (int*)&format;
dataptr++; // now it points to the args of the
// args to be used for the format
// string's %s, %d, etc
}
but from gcc 3.4, according to the google-verse, this "cowboy" method no longer works--hence there are patches.

It's progress. And I'm reacquainting myself with gdb which I haven't touched in ages. Some people in Turkey working on pardus have a patch which forces GRUB it to use varargs. I downloaded a patch file from their svn (after allowing Firefox to let them use a self-signed certificate). Actually a google search on "grub varargs" turns up patches in places without Firefox getting pissed off about self-signed certificates.

And I get to learn a new Linux command: patch. OK, I have used it before since Subversion is based on it and I've used it there, albeit in Windows.
patch --verbose -p 1 -d ./grub-0.97 -i patches/varargs.diff
Did the trick except for one file which I had to do manually, though I'm not sure what the problem was--perhaps whitespace since it looked the unified diff should have matched. I had to guess on the -p 1 and of course I practiced with the --dry-run option.

So, off to make, but, naturally, it fails. First, misc.c doesn't have the #include , so I added it, but the worse problems seems to be the -nostdinc compiler option that's set all over preventing it from being found. I guess the patch isn't exactly complete or doesn't cover my set of code. Then I had to remove the -nostdinc from stage2. Once that was done--it compiled to the end!

But will it run? Yes!!! It even seemed to install stuff on my USB key. Now to shutdown and test.

And this seems like a good place to end Part II.

Useful links:

http://www.linuxselfhelp.com/gnu/grub/html_chapter/grub_3.html

http://www.freesoftwaremagazine.com/articles/grub_intro

No comments:

Post a Comment