LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 04-03-2014, 12:36 AM   #1
ron7000
Member
 
Registered: Nov 2007
Location: CT
Posts: 248

Rep: Reputation: 26
help understand 446 bytes of boot code in MBR


i've been messing with grub version 0.97 and trying to figure some things out. I've gotten to the point where i have things working, but i don't know why exactly.
some background info here, and what i'm doing is with Novell SLES 11.3: http://www.linuxquestions.org/questi...lp-4175500045/

so now i'm looking to understand what the first 446 bytes in the MBR pertain to. if you look at my attached pic in the link above, SLES gives you a yast bootloader window with options to boot from the boot partition, extended partition, MBR, or root partition. What gets selected by default and that i am using is "boot from root partition".

That said, i have two hard disks of different make and size. Both are formatted /dev/sda1 as swap 1gb in size then /dev/sda2 remainder of disk as ext3. I have SLES 11.3 on each and both boot fine and work in 2 different systems like there was no difference.

if i compare the /boot/grub/stage1 file from each disk in a hex editor, they are identical. they both are 512 bytes in size, starting with "EB 48 90 00 00..." and then about 3/4 way to the end the hex data reads in ascii "GRUB .Geom.Hard.Disk.Read. Error." but both these disks boot fine and i don't really care about the stored error message in there, except maybe they are at different locations in each file.

if i disk dump using 'dd if=/dev/sda of=mbr bs=512 count=1' for both hard disks and compare these 2 mbr files the first 446 bytes of the boot code are different.

I was reading online and started to get the impression that the stage1 file might get copied into the MBR, but that is not at all the case here. I also have 'boot from root partition' selected in my boot loader settings. If I had told grub to 'boot from MBR' would that actually copy part of the stage1 file to the MBR? And if it does what parts because the MBR is 446 bytes in size but stage1 is 512 in size.
I also have the options in my sles yast bootloader window which are not shown in my pic it's under 'Boot Loader Options' button, to 'Write generic boot code to MBR' along with 'Set Active Flag in Partition Table'.
I sort of proved the active flag setting does not happen if i choose that, but that's a separate issue which i manually do with a hex editor.

But does anyone know what this generic boot code would be and how it works?

At very least i'm looking to understand the chain of events in lamens terms.
so far i know the bios of the pc begins to read the MBR of the selected boot drive. I understand the 64 bytes of the MSDOS partition table and the ending 55AA value and the 80 value in the partition table that's flagging the partition to be the bootable partition- i assume this is how grub knows how to boot from root partition or from boot partition.
But for the first 446 bytes in the mbr i'm clueless all i've read is, it's executable code? And i cannot even see any similarity between the stage1 file and the mbr of the disk, although i have not specifically tried telling grub to 'boot from master boot record' then compare stage1 and the mbr.

I also ask all this because (as explained in linked thread above) i am trying to clone disks and everything works except this bootloader, which i believe is bugged by Novell. And i can boot the install dvd and do a repair bootloader but that's not even straightforward anymore and is now bugged in SLES 11.3, it alters the menu.lst file which i then have to manually edit afterwards to tell it 'kernel vmlinuz' and 'root=/dev/sda2'. and this repair bootloader also reports errors saying cannot install boot loader because of partition table. but if i select various options i eventually get it to work... and so far i've tracked it down to i somehow manage to get grub to install something correct in that 446 bytes of the MBR.
if you've made it this far, thanks for reading and for any help or understanding you can provide.
 
Old 04-03-2014, 08:02 PM   #2
jefro
Moderator
 
Registered: Mar 2008
Posts: 21,982

Rep: Reputation: 3625Reputation: 3625Reputation: 3625Reputation: 3625Reputation: 3625Reputation: 3625Reputation: 3625Reputation: 3625Reputation: 3625Reputation: 3625Reputation: 3625
I think this is it.

https://www.gnu.org/software/grub/ma...tml#Memory-map
 
Old 04-03-2014, 10:20 PM   #3
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,779

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
I'll briefly describe the operation of GRUB Legacy, but GRUB 2 is basically the same in these early stages of booting.

There isn't room for much function in the 446 bytes available for executable code in the boot sector. The sole function of this stage1 code is to load the much larger stage2 boot program. When stage1 is installed in the MBR, it is configured with the BIOS drive number and the absolute LBA of the first sector of the stage2 file in the boot partition. It loads that one sector into a fixed location in memory and transfers control to it. It is the job of the code in that sector to load the rest of stage2 and execute it. It is the stage2 code that presents the GRUB menu and handles all of the GRUB commands.

Note that at no time is there any check for an "active" partition. GRUB doesn't care about that.

A disadvantage of the above scenario is that if the stage2 file in the boot partition is ever moved, the boot may fail. To avoid that problem, a stage1.5 program can be installed in the sectors immediately following the MBR if there is free space there before the start of the first partition. In that case, the stage1 code loads the stage1.5 program. The stage1.5 code understands the filesystem used in the boot partition (there is a different stage1.5 for each supported filesystem). The stage1.5 code reads the stage2 program from the filesystem and transfers control to it.

You can find the GRUB stage1 boot loader described in gory detail at http://thestarman.pcministry.com/asm/mbr/GRUB.htm .
 
Old 04-07-2014, 09:58 PM   #4
ron7000
Member
 
Registered: Nov 2007
Location: CT
Posts: 248

Original Poster
Rep: Reputation: 26
http://thestarman.pcministry.com/asm/mbr/GRUB.htm

this explained everything and confirmed my suspicions about the locations of stage1.5 and stage2 being hardcoded in stage1.
when copying one file system to another that all needed to be corrected.
the grub interface in sles is for the most part terrible.
i think i need to make a supergrub disk and correct the grub boot loader on a new disk that way.

thanks for the help.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Why MBR is limited to 512 bytes? RaviTezu Linux - Newbie 8 11-21-2012 07:47 AM
Which program can boot a copy of the MBR (fake MBR) using mbr.bin Xeratul Linux - Software 6 12-03-2010 11:40 PM
Danbooru,postgresql,Code. Cant understand this code Vodkaholic1983 Linux - Newbie 7 07-03-2010 03:47 PM
How to detect how many bytes of scan-code are coming for a key press? sridhar_ub Programming 4 03-25-2008 06:32 AM
How to detect how many bytes of scan-code are coming for a key press? sridhar_ub Linux - Newbie 3 03-24-2008 12:39 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 06:53 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration