Jump to content

[Good Info] Background Info on the Darwin/OSx86 Boot Process


14 posts in this topic

Recommended Posts

Background Info on the Darwin/OSx86 Boot Process



Keywords: MBR PBR HFS+ Partition FDISK FIXMBR DISKPART BLESS GRUB BIOS EFI OS X Linux Darwin chain0 boot0 boot1h startup-file




Author: Shimodax




This page is not intended as a step by step instruction to install Mac OS X, but to provide background information to understand the various procedures and to help providing the tools to fix the various errors/problems (if they are related to the boot process itself and not caused by hardware incompatibility, drivers, etc.).


It assumes basic knowledge of Linux/Unix/Darwin, i.e. you need to be able to run commands from the shell or find the correct /dev/xxxx designations for your hard drives or volumes/partitions. You should also know about sudo and mount/umount.


Most operations here can be performed from a Linux shell or a Windows XP or Vista repair console. A bootable Linux CD (e.g. Ubuntu) or Windows Installation CD are immensely helpful tools.


Last but not least, the information here is collected from experience and from other instructions and is in no way guaranteed to be perfect. The commands presented here are mostly dangerous and capable of making a disk unusable in a split second, so only use them in environments where you can afford to lose your disk content or where you have backups.







=Basics about Booting=





When booting from a hard drive on a computer, the machine first loads the Master Boot Record (MBR). The MBR can contain up to 446 bytes of assembler/machine code and is the second part of the bootstrap (the first step is BIOS loading the MBR).


The MBRs vary between operating systems (Windows XP, Linux Grub, Linux LILO, Darwin, etc.). Generally they load the partition table, which is stored in the same block on the disk as the MBR itself (first 512 bytes on the entire hard drive), then they locate a partition which matches the operating system, loads the first block from that partition and executes it (see discussion of PBR below).






==Partition Table==

In most cases (Windows/Linux based computers) the partitions on a hard drive are stored in the same block on the disk (first physical block) as the MBR, but with an offset of 446 bytes. It contains up to four records describing partitions, either primary or logical. Logical partitions are a way to overcome the limit of four partitions and store their sub-partitions (logical drives) somewhere else.


Most partition mangers (Linux fdisk, Linux gparted, Windows "Disk Management", Windows diskpart together with commercial hard disk managers like Acronis Disk Suite) use this partition table format.


OS X uses a different way called EFI which is supposed to replace the BIOS. When you partition a disk using OS X, the Mac Disk Utility (Utilities folder or Utilities menu on the Install CD) allows you to store the partition table in BIOS or EFI format.


Note: On original Macs, the Boot Camp software synchronizes both EFI and BIOS table to make sure that the Mac and the Guest operating systems are accessing the hard drive in a consistent way. Windows Vista uses a similar approach, being based on EFI or simulating EFI on BIOS based motherboards (via the /boot folder and BCDEDIT program).




For the rest of this document we will focus on BIOS partition tables.






==Creating Original MBRs from Windows/Darwin/Linux==

Each of the usual operating systems allows you to write their respective MBRs to disk (preserving the disk's partition table).



===Windows XP===

Boot from an installation CD, go to the recovery console prompt and type


# fixmbr



===Window Vista===

Boot from an installation CD, go to the recovery console prompt and type


# bootrec /fixmbr



===Linux (Grub)===

# grub-install /dev/??? (where ??? is the identifier of your hard drive, e.g. sda or hda).



===OSx86 (Darwin)===

Boot the installation CD, press F8 for the extended boot options, type -s and let it boot to the shell prompt.


# fdisk -e /dev/??? (where ??? is the identifier of your hard drive, e.g. rdisk0)


> update

> write

> exit






==Active (Bootable) Partition==

Besides the information about physical location, partitions also have a flag which marks them as bootable (also called active). The Windows MBR for example looks for an active partition to load its operating system from. Linux does not require this. With OS X Darwin it is not entirely clear if activation is required or not, documentation on the internet says that the Darwin MBR (Chain0) does not but it seems to help in some cases.


Setting a partition active can be achieved by various tools depending on the operating system.



===Windows (Command Prompt or Repair Console)===

# diskpart


> list disk

> select disk ? (replace ? with a disk number/index)

> list partition

> select partition ? (replace ? with a partition index)

> active

> exit



# fdisk /dev/??? (replace ??? with your hard drive identifier)


> p (list partitions)

> f ? (replace ? with a partition index)

> w

> x



# /sbin/bless --device /dev/??? --setBoot (note the upper case "B" and replace ??? with your partition identifier, e.g. rdisk0s2)




# fdisk -e /dev/??? (replace ??? with your hard drive identifier)


> print (list partitions)

> flag ? (replace ? with a partition index)

> write

> exit






==Juggling MBRs==

Under Linux, OS X and also Windows there is a command line tool named DD which allows you to read blocks directly from the physical disk. Assuming you know the name of your physical disk on your respective operating system (e.g. /dev/rdisk0, /dev/hda, /dev/sda) you can use dd to extract the MBR for use with boot menus.


The following command reads the MBR from disk and stores it in a file named mbr.dat (you may have to unmount your partition and/or execute the dd command with sudo in order to achieve this);


# dd if=/dev/hda of=mbr.dat bs=512 count=1


This file can subsequently be used in boot menus (see below).


Theoretically you can also use dd to write the MBR back. Practically this is quite dangerous (experienced administrators say dd stands for destroy disk).


As mentioned above, the MBR block (512 bytes) contains the **boot code and the partition table**. Writing back a bad or empty partition table is the quickest way to lose the data on your disk. Therefore writing the MBR code should only write up to 446 bytes and I would strongly advise against using this method at all (instead use the operating system commands for writing the original MBRs as described above). If you are adventurous and still want to write it via dd directly, the command would look like (again, note the 446 instead of 512):


# dd if=mbr.dat of=/dev/??? bs=446 count=1 (??? being the hard disk identifier, e.g. hda, sda)






==Boot Menus with custom MBRs==

Boot menus like Linux Grub or the Windows XP boot menu are essentially a way of juggling MBRs. Both Grub and Windows XP boot are tailored to their respective operating system but allow to make entries for other operating systems.


A simple entry for the Windows XP BOOT.INI for example looks like this:


\chain0="Mac OS X"



This means that Windows loads a file named "chain0" or "grubMBR" from its root folder, which should contain the same data that would normally be stored on the hard disk's MBR. It then executes it as if the MBR was loaded from disk through the BIOS directly. These files are usually 512 bytes (although technically 446 bytes or less would be sufficient).


Assuming that you have a chaiin0 file in the root folder of the system drive, for Vista and Windows 7 you can use the following procedure:

> bcdedit /create /d "Mac OS X" /application BOOTSECTOR

(now note down the UUID {xxxx-....-xxxx} for the new entry)


> bcdedit /set {xxxxx-xxxxx-xxxxx-xxxxx} path \chain0

> bcdedit /set {xxxxx-xxxxx-xxxxx-xxxxx} device boot

> bcdedit /displayorder {xxxxx-xxxxx-xxxxx-xxxxx} /addlast


(Note: There are similar instructions elsewhere which are based on "bcdedit

/copy" rather than "bcdedit /create", these however won't work with

Windows 7):

Similar methods exist to create MBR entries in boot menus under Linux (Grub) and are easy to find via Google.




==PBR (Partition Boot Record)==

Whatever MBR you are using, what the MBR does is to locate a boot partition (Windows looking for a primary partition marked active, Darwin looking for a HFS partition (code AF), etc.) and to load the PBR (first block from the partition/volume).


The PBR is just 512 bytes assembly/machine code and thus fairly limited in what it can do. But it has some knowledge about the file system on that partition/volume and is thus able to load a larger file from there, which is then executed to boot up the operating system


  • Windows XP: MBR -> PBR -> NTLDR
  • Darwin/OSx86: MBR -> PBR -> HFS+ startup-file
  • Linux: MBR -> PBR -> /boot/grub/



==Juggling PBRs==

Similar to the MBR, the dd command can be used to read and write PBR blocks to/from files:


# dd if=/dev/hda1 of=pbr.dat bs=512 count=1

# dd if=pbr.dat of=/dev/hda1 bs=512 count=1


Note: The device designation needs refer to a partition, not to a physical disk! E.g. hda1 is the first partition on the first physical drive. Making an error here (using hda instead of hda1) will wipe out your partition table or will possibly invalidate an operation system if you write to the wrong partition. Typical naming schemes are hda1, sda1 or rdisk0s1 (first disk, first partition) or hdb3, sdb3, rdisk1s3 (second disk, third partition), etc.







=Booting Darwin (Darwin Boot Sequence)=


==Darwin MBR==

As outlined above, to boot Darwin, the first step is to obtain a Darwin MBR and either put it into the disks MBR block or into a boot menu (Windows or Grub). There seem to be two flavors of the Darwin MBR (which seem to be exchangeable).


One version is used by the various OSx86 installers (available through a file named boot0 in the root of an OSx86 boot CD or in /usr/standalone/i386). The other is available through the Darwin project (see the Chain0 link at the end of this document).


The Darwin MBR tries to locate a partition which is designated as HFS+. The partition needs to match the following criteria

  • Primary partition
  • Type flag hex AF
  • PBR on the partition ending on hex 55 AA
  • In the partition table, the partition needs to be listed before any logical partition (Linux fdisk is able to adjust the order of entries from the x menu) (The "Simple and Accurate" link at the end of this text has more information about this)

Failure to find it, results in the message "Chain booting error" (chain0 MBR) or "b0 error" (boot0 MBR). In other words, if you get these errors, you have a valid MBR but it can't find the Mac partition for one of the reasons above.





==Darwin PBR==

The Darwin PBR (when loaded and executed through the MBR) then tries to locate the HFS+ startup-file.


If it works, it shows the Darwin boot message, if not, or if the PBR finds an error with the HFS+ volume, it emits an error message saying "HFS+ booting error". In other words, if you see this message, you have sucessfully mastered the first step (the MBR).




==Darwin startup-file==


The startup-file is stored in a specific area of the HFS file system and it is not accessible from the regular file system. It can be written using the bless or startupfiletool commands.


The binary code for the startup-file is avialble on bootable OSx86 CDs as a file named boot in the root folder or in /usr/standalone/i386/ (about 50KB). When executed, it will display the boot message or boot menu (via F8 key) or it will letsyou enter the boot options (partly according to the settings in /Library/Preferences/SystemConfiguration/com.apple.Boot.plist).






=Useful commands to fix Darwin booting=


==Fixing the MBR==

There are various ways to get a Darwin aware MBR.


1) Write the MBR code from the startup CD (boot with -s option) via fdisk -e /dev/rdisk? (see the section above about writing original MBRs)


2) Obtain a copy of chain0 (see links at the end of this text) or boot0 (look for the boot0 file on your OSx86 CD in the root folder or in /usr/standalone/i386/) and write it to disk via dd if=/usr/standalone/i386/boot0 of=/dev/hd? bs=446 count=1 (see "Juggling MBRs" above)


3) Use a copy of the chain0/boot0 file and put it into a Windows or Linux boot menu (see section "Boot Menus" above)


4) Another alternative is to use a Windows MBR (via fixmbr) and to mark the Darwin partition as active via fdisk (Linux/Darwin) or bless (Darwin) or diskpart (Windows) as described above. This is a bit of a hack, but the Windows MBR seems to do nothing but to load the PBR from the active primary partition and to execute it.




==Fixing the PBR==

To fix the PBR on a partition you need the boot1h file from a OSx86 CD or you need to snatch it via dd from a working Mac partition. Then put it back via dd if=boot1h of=/dev/???? bs=512 count=1 (see "Juggling PBRs" above)




==Writing/Fixing the HFS+ Startup-File==

To fix/create the HFS+ startup-file you need bless from the /sbin, /usr/sbin or /usr/bin folder. Also find the boot file (on your OSx86 CD in the root folder or in /usr/standalone/i386/). Last but not least determine the name of your drive and partition (e.g. rdisk0 and rdisk0s2).


Then run (adjust directories and device name)


# /sbin/bless --device /dev/??? --startup-file /usr/standalone/i386/boot (for ??? use your correct partition identifier, e.g. rdisk0s2 in the command, see links at the bottom of this text for a link on bless.)


Alternatively obtain the startupfiletool (e.g. here [ http://forum.insanelymac.com/index.php?sho...rt=#entry166040 ]), put it on a HFS+ formatted memory stick, mount it and run the equivalent (according to your mount/directory structure) of


# /Volumes/my-memorystick/startupfiletool /dev/??? /usr/standalone/i386/boot (??? is again the partition)




==Boot-Loader and Menu==

If all worked well, booting will then show the Darwin boot message. Pressing F8 will bring up the partition selection menu (to boot from multiple Mac or Windows partitions) and which offers the opportunity to enter boot options (-v for verbose booting, -s for the single user shell).


The boot loader will default to the partition marked active, otherwise you will have to select it from the F8 menu (activating partitions is described above).


If it is going too fast, you can edit the options file for the boot loader on the HFS/Mac partition, which is located in /Library/Preferences/SystemConfiguration/com.apple.Boot.plist and change the timeout to 5 seconds by adding (or modifying):






Note: Assuming you have Linux installed, using Linux grub boot loader's MBR can come pretty handy here because in it also has an option ot activate the selected partition, putting something like the entry below into Grub's menu.lst file:


title OS X 10.4.8

root HD(0,2) (0,2 identifying hard disk and partition)


chainloader +1







=Useful links=

Link to comment
Share on other sites

Background Info on the Darwin/OSx86 Boot Process

==Creating Original MBRs from Windows/Darwin/Linux==

Each of the usual operating systems allows you to write their respective MBRs to disk (preserving the disk's partition table).


===Linux (Grub)===

# grub-install /dev/??? (where ??? is the identifier of your hard drive, e.g. sda or hda).


Hey, great work.


Just a note, you can install grub in the Disk but also in a partition

with setup within grub shell:

On the disk:


grub> setup (hd0)


On the partition (this example is for the 2nd partition):


grub> root (hd0,1)

grub> setup (hd0,1)

Link to comment
Share on other sites

  • 5 weeks later...
  • 2 weeks later...

Nice guide. I messed up my mbr and 1 extended PBR because of that problem with the partitions order and Mac Installation(i put last partition as 3rd, and 3rd as last). Took me a lot of reading, wiki, ntfs.com etc.

Guess most of what i learned is here.


Ive a question: Is it really impossible to change the default value of the mac boot loader? Guess I'll have to make active my Mac partition and from there load win. I had win as active and the chain0 on its root directory, added the C:\chain0 to boot.ini

Link to comment
Share on other sites

  • 4 months later...
Ive a question: Is it really impossible to change the default value of the mac boot loader? Guess I'll have to make active my Mac partition and from there load win. I had win as active and the chain0 on its root directory, added the C:\chain0 to boot.ini


Sorry, been a while since I was there. I have no idea if or how this works, but if you have figured it out, post it here and I'll mash it into the text.

Link to comment
Share on other sites

Hi! Shimodax!


I read your article and it is really interesting ...


I installed OSX 10.5 from Toh and I get the error "boot chain error".


So I red in your article that the mac partition must have the following criterion, but visibly mine is not respecting these criterion!!


My question is How to make my mac partition respecting these criterion?


* Primary partition

* Type flag hex AF

* PBR on the partition ending on hex 55 AA

* In the partition table, the partition needs to be listed before a logical partition


My Mac partition is a primary partition, it is listed before all logical partition...


but what "Type flag hex AF" mean?

and What Partition ending on hex 55 AA mean?


but I mean that how I can set these last two criterion on my mac partition?


Thanks a lot.

Link to comment
Share on other sites

  • 3 months later...
  • 3 months later...

Hey there, nice read!


Having 3 OSes installed is both neat and useful for me. The newest system on my machine is the osx86, and I regularily update this one. Update might not be the most correct word here ... I rather reinstall OSx86 to its dedicated partition on my one-and-only HDD rather than update the old installation. The trouble is I loose the ability to boot all other OSes when I do so. So far, my solution was to reinstall Linux. Linux would write the MBR with its Grub, and Grub auto detects Windows, and then I manually add an entry for Mac OS. And that was about it.


Now after reading this fine article, I would think of 3 possibilities to avoid the Linux reinstall every time I upgrade my OSx86. Please let me know if I am wrong:


1. upon reinstalling Mac OS, place in the cd tray the linux CD, boot, and grub-install /dev/??? from the console. Then manually add an entry for Mac as usually.


2. First back up the MBR using dd or some other tools annd then, after the Mac OS upgrade, rewrite the old MBR, thus giving me my old Grub and leaving nothing at all to tweak.


3. The most straightforward to me seems to be adding 2 entries to Darwin / Chameleon to make it load WinXP / Linux PBRs.


But my question is .. how to accomplish this 3rd option ?


Thanks all,


Link to comment
Share on other sites

  • 7 months later...

  • Create New...