# How to recover efi booting



## kceiw (Feb 7, 2022)

Hi,

I have installed FreeBSD 13 and everything went well, including booting. But after one failed shutdown (by failed shutdown I meant it's waiting for something and I cut off the power after a long time), it never boot.

I use efi, and FreeBSD is installed in a separate SSD. I used to be able to set that SSD as the first boot option. It worked until the failed shutdown. I think for some reason the efi system is corrupted.

I have no clue what I can do to restore the efi system. So I hope to get some guidelines how to recover it and be able to boot from the SSD again.

Below is my current situation and the system information.

1. How do I boot into FreeBSD now.
I have Gentoo Linux installed in a different SSD and that uses GRUB. I add an entry in the GRUB configuration and set that SSD as the first boot option. I don't like that because once I complete remove or repurpose that SSD, I won't be able to boot FreeBSD. The entry in GRUB in case that helps is

```
menuentry "FreeBSD load kernel" {
savedefault
        insmod ufs2
        insmod part_gpt
        search --no-floppy --fs-uuid --set=root --hint-bios=hd0,gpt2 --hint-efi=hd0,gpt2 --hint-baremetal=ahci0,gpt2 61d236690097e4d2
        kfreebsd /boot/loader
}
```

2. I'm using 64 bit FreeBSD 13-RELEASE-p4
3. My partition is

```
~ $ sudo gpart show /dev/ada0
=>      2048  1000213135  ada0  GPT  (477G)
        2048      532480     1  efi  (260M)
      534528   104857600     2  freebsd-ufs  (50G)
   105392128    67108864     3  freebsd-swap  (32G)
   172500992   167772160     4  freebsd-ufs  (80G)
   340273152   209715200     5  freebsd-ufs  (100G)
   549988352   450226831     6  freebsd-ufs  (215G)
```
4. The contents in EFI file system

```
~ $ ls -l /boot/efi/efi/*
/boot/efi/efi/boot:
total 875
-rwxr-xr-x  1 root  wheel  896000 Jan  2 23:36 bootx64.efi

/boot/efi/efi/freebsd:
total 875
-rwxr-xr-x  1 root  wheel  896000 Jan  2 23:36 loader.efi
```
5. efibootmgr doesn't work properly too

```
~ $ sudo efibootmgr
efibootmgr: efi variables not supported on this system. root? kldload efirt?
~ $ sudo kldload efirt
kldload: can't load efirt: module already loaded or in kernel
```

Please let me know if I didn't provide any information. I don't find anything obvious wrong from my investigation. If anybody knows what direction I can continue investigate, please let me know. Thanks so much.


----------



## grahamperrin@ (Feb 7, 2022)

Welcome to FreeBSD Forums.



kceiw said:


> … I think for some reason the efi system is corrupted. …



If the computer did previously work with the EFI system partition (ESP), I should not expect corruption of the `msdosfs` type file system there. Writes there are unlikely.



> … I cut off the power …



Have you checked the `freebsd-ufs` file system at adap04? Assuming partition 4 is where you installed the OS.



> … installed FreeBSD 13 …



If you have the installer handy, boot from that medium into a shell then use fsck(8) or more specifically fsck_ffs(8). Until you gain a rough idea of the state of the file system(s), it may be sensible to respond _no_ in places.

If all file UFS file systems can be verifiably clean (and not corrupt despite cleanliness) then rEFInd should be able to boot the OS, but let's not rush to that.



> … (by failed shutdown I meant it's waiting for something …



Can you recall what that thing was? Or, were you doing anything in particular _before_ the attempt to shut down?


----------



## SirDice (Feb 7, 2022)

kceiw said:


> ```
> ~ $ sudo efibootmgr
> efibootmgr: efi variables not supported on this system. root? kldload efirt?
> ```


You aren't EFI booted. Check `sysctl machdep.bootmethod`, if it says BIOS you cannot use efibootmgr(8), it only works if you actually EFI booted.


----------



## sko (Feb 7, 2022)

kceiw said:


> I used to be able to set that SSD as the first boot option.


You shouldn't need to select a physical drive for EFI booting. Make sure you don't boot in BIOS/CSM/legacy or some differently named "mode" that isn't EFI.

If you've selected BIOS+EFI-boot during setup there should be everything in place to successfully boot whatever mode the firmware is set to/capable of.


----------



## SirDice (Feb 7, 2022)

sko said:


> If you've selected BIOS+EFI-boot during setup there should be everything in place to successfully boot whatever mode the firmware is set to/capable of.


There is no freebsd-boot partition. I suspect the OP is CSM booting via Grub.


----------



## grahamperrin@ (Feb 7, 2022)

SirDice said:


> There is no freebsd-boot partition. I suspect the OP is CSM booting via Grub.



Good point. 

My installation here is EFI only with no `freebsd-boot` partition, but this is unusual. Not what I'd expect from a modern installer for 13.0-RELEASE, at least not with _guided_ partitioning.


----------



## chrbr (Feb 7, 2022)

Dear kceiw,
it might be that the GRUB stuff has an issue and not FreeBSD. There has been a time I have had a dual boot installation with Debian and FreeBSD. I do not remember if I had Debian and FreeBSD on the same disk or on two separate discs. It has been a surprise to me that FreeBSD did not boot without the Debian counterpart. The reason has been that GRUB finally accesses the data from its origin. GRUB is quite a powerful monster. And that requires more space than the old MBR.

A different issue could be a swap of the disks or connectors.


----------



## grahamperrin@ (Feb 7, 2022)

kceiw said:


> … FreeBSD is installed in a separate SSD. …



kceiw please, is that _separate_ drive something _other_ than the ada0 in your opening post? Sorry if I confused myself at any point above.


----------



## kceiw (Feb 8, 2022)

Thanks all for the replies. It's such a great community. sysctl machdep.bootmethod

To answer your questions and to clarify,
1. ada0p2 is where to install the root, including /boot. The others ufs partitions are for /usr, /var, and /home.
    I can run fsck at a later time after I prepare the installer. I just wonder how that can be related to booting from ada0 if I can use the kernel in ada0p2 to boot from GRUB?
2. The issue before I cut off the power, from my vague memory, is about syncing or flushing to an external hard drive. I set up GELI on the external hard drive and copied a large amount of data to it before shut down.
3. `sysctl machdep.bootmethod` returns BIOS.
    It's interesting booting from GRUB returns this value. I didn't check it or ran efibootmgr when I was able to boot from ada0.
4. GRUB is installed in ada2 with Gentoo only. FreeBSD and only FreeBSD is installed in ada0.
5. I may be still confused about the booting process. From BIOS I can tell there is legacy booting order and another booting order that's not under legacy mode. I set the one for the non-legacy one. It should be efi. If I boot to Gentoo, and efibootmgr can work. I ran that before (a few months back, at least before I decided installing FreeBSD) in Gentoo and I'll double check it now just in case something is changed in BIOS.
6. Right now I boot to FreeBSD via GRUB since I cannot boot from ada0 directly. GRUB is the only way for me to boot FreeBSD now. And I would like to be able to boot from ada0 directly.
7. I don't remember the details how I set up GRUB. It's done about 8 years ago.
8. I used manual partitioning when I install FreeBSD.
9. Again, I am still not clear about the booting process. But whatever I set up during installation worked. But suddenly it didn't work after a reboot. The only thing I can relate to that is I cut off the power.

Thanks again for all the replies. I will reboot and
1. Check BIOS about legacy booting and if possible disable that.
2. Run fsck on all the ufs file systems.
3. I can boot to Gentoo and check what I get from 'efibootmgr'.


UPDATES

1. It's set up and boot in CSM. There is an option in BIOS to disable CSM. And I cannot select the hard drives for GRUB and FreeBSD to boot. I must have it enabled and installed FreeBSD so I must have CSM enabled for GRUB and FreeBSD.
    Is this the "BIOS+EFI-boot"? I had the wrong impression for my own computer setup.
2. I ran fsck. There are some issues mainly about free blk count. I ran it and fixed that. The file systems are clean now.
3. I couldn't run efibootmgr from Gentoo. It may be due to 1 and I thought it the other way.

So my question is still that whatever I set up when I installed FreeBSD doesn't work now, and I don't have any idea how to get it work again. I'm also happy to convert it to EFI only booting if that is possible too. My main point is not to rely on a separate disk to boot FreeBSD.


----------



## sko (Feb 8, 2022)

kceiw said:


> 1. It's set up and boot in CSM. There is an option in BIOS to disable CSM. And I cannot select the hard drives for GRUB and FreeBSD to boot. I must have it enabled and installed FreeBSD so I must have CSM enabled for GRUB and FreeBSD.


You don't have to choose any hard drive for EFI booting. The EFI boot manager will search for any EFI bootloader and offer those as boot options.
However, if your GRUB install is not EFI-capable (haven't used this fragile thing for ages...) you might not see GRUB as a boot option.

With many UEFI you have to disable CSM, then save & exit & reboot first to see all EFI boot options


----------



## kceiw (Feb 9, 2022)

Hi sko, Thanks for your reply. I disabled CSM, save, exit, and reboot. That didn't find whatever bootloader for FreeBSD. There is only one boot option for Windows which is only a separate disk I installed many years back.

It looks like my GRUB isn't EFI-capable but that is not what I'm trying to fix. It's used currently as a way for me to boot to FreeBSD since the ada0 (where FreeBSD is installed) isn't bootable for some reason.

Since FreeBSD uses the whole disk and I used to be able to boot from it. Maybe it's not set up in the EFI way. I don't know how to check that since I cannot boot from it. And whatever I set up on the disk is what I'm looking for to recover. I don't know what is the right way to repair or recover the bootloader on that disk.


----------



## grahamperrin@ (Feb 9, 2022)

kceiw said:


> … ada0 (where FreeBSD is installed) isn't bootable for some reason. …



OK, back to this: 



grahamperrin said:


> … If all file UFS file systems can be verifiably clean (and not corrupt despite cleanliness) then rEFInd should be able to boot the OS, …



If you can, try rEFInd.


----------



## sko (Feb 9, 2022)

+1 for rEFInd

it is also available as a ready-to-use USB image, so you won't have to change anything on disk.
If your UEFI/BIOS won't even show/boot rEFInd, EFI is most likely broken. Try upgrading the firmware and/or nag the vendor.
Maybe you also have to disable anything related to 'secure-boot' to get EFI working as it should...


----------



## kceiw (Feb 12, 2022)

Many thanks to all of you who are so patient with me. I learned a lot and understand my computer configiration better. I think i solve my issue. 

Before I try rEFInd, I think I might try one thing first. That is to add the FreeBSD to the efi boot option. I remember therebis one EFI bootable OS.  So I disable CSM.  It only boots to Windows. Then I use DiskGenuis to add the FreeBSD (both freebsd/loader.efi and boot/bootx64.efi) as the boot options. I also add the Grub one to the boot option. Configure the freebsd/loader.efi as the first boot option. That works.  I try all the boot options and they all work. 

I think as long as I can EFI boot to a system, I can also use efibootmgr to do the same thing to fix it.

I'm still reading rEFInd and the page the author writes. That's a good read and clear.


----------

