MacOS – How to fix broken EFI partition

efimacospartition

I have an iMac running OSX Lion. I also installed bootcamp and Windows 7. During the install of Windows 7 I wasn't paying complete attention and deleted and formatted the EFI partition.

I didn't really realise the significance of this as the machine continued to work fine, booting both into Windows and OSX.

I did notice that the EFI firmware updates failed to install, but paid this no mind.

However now I want to install Mountain Lion and there's a whole heap of trouble. Mountain Lion won't install without a valid EFI partition.

Here's my partition table.

   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *1.0 TB     disk0
   1:                  Apple_HFS EFI                     209.7 MB   disk0s1
   2:                  Apple_HFS Macintosh HD            799.0 GB   disk0s2
   3:                 Apple_Boot Recovery HD             650.0 MB   disk0s3
   4:       Microsoft Basic Data BOOTCAMP                200.3 GB   disk0s4

As you can see I've had a go at fixing the EFI partition with the following command:

newfs_hfs -v EFI /dev/disk0s1

This didn't seem to create the correct type of partition, it created an Apple_HFS instead of EFI.

My question is (without formatting the disk) how do I create an EFI partition?

Thanks
Rich

Best Answer

You can recreate the EFI System Partition (ESP) using the command-line gpt tool. It should start at sector 40, and it should be 409600 sectors (exactly 200MiB) long. The GPT entry's type should be C12A7328-F81F-11D2-BA4B-00A0C93EC93B, and if you have a hybrid MBR, the type there should be EE. The format is actually a subset of FAT, not HFS+. You can read the GPT using the command sudo gpt -r show disk0 in a Terminal window, this produces output that looks something like this:

      start       size  index  contents
          0          1         PMBR
          1          1         Pri GPT header
          2         32         Pri GPT table
         34          6         
         40     409600      1  GPT part - C12A7328-F81F-11D2-BA4B-00A0C93EC93B
     409640  447801712      2  GPT part - 48465300-0000-11AA-AA11-00306543ECAC
  448211352    1269536      3  GPT part - 426F6F74-0000-11AA-AA11-00306543ECAC
  449480888   40753831         
  490234719         32         Sec GPT table
  490234751          1         Sec GPT header

You can see the correct EFI partition at index 1. If that line doesn't match exactly, the easiest way is to delete the partition in its place and recreate it.

Make sure you know what you're doing before proceeding from here - you may lose data if you mess up.

You can't edit the partition table using gpt while partitions on the drive are mounted, so you'll need to boot from another drive (e.g. a USB stick with the OSX installer) or run the mac in target disk mode and do the partitioning from another mac. You may need to unmount any automatically mounted volumes using diskutil unmountDisk disk0 before proceeding, and in between commands.

To delete your "bad" EFI partition, run this command:

sudo gpt remove -i 1 disk0

Make sure disk0 is really the disk you want to change - the numbers can change between reboots. Also, this will only work if you already have a non-ESP partition in the place of the ESP - if not, deleting partition 1 could be disastrous! In this case, you'll need to move the indices along, as I think the ESP must have index 1. By the sound of it, this won't be necessary in your case.

Once deleted, re-add the partition with the correct layout and type:

sudo gpt add -b 40 -i 1 -s 409600 -t C12A7328-F81F-11D2-BA4B-00A0C93EC93B disk0

This should sort out the partition itself, but as you've created an HFS partition in its place, you need to fix its format as well.

The format of the EFI System Partition is documented by Apple here. As you can see, they recommend you clone an existing ESP rather than recreating the file system with newfs_msdos. If you have an external drive with a GPT partition table (OSX install stick should have it), you can clone that.

Make sure you have your disk and partition numbers the right way around or you may overwrite data:

If disk1 is your external drive, and it contains an ESP as the first partition (disk1s1) and disk0 is the drive whose ESP (disk0s1) you want to fix, run this command:

sudo dd if=/dev/disk1s1 of=/dev/disk0s1

This will copy every single block of disk1's ESP and overwrite the corresponding block in disk0's ESP with it.

Alternatively, take a gamble with newfs_msdos.