Restore Windows UEFI boot
Tags: efi, linux, bootmgr, refind
Published: 2021-12-06 06:21:03

## Introduction

_Sometime last week, I decided to move my entire ESP folder into a dedicated boot partition mounted at `/boot`. (I did this because I liked the idea of having everything boot-related in a partition for easy access when I dual boot or need any file from there). Unfortunately, I didn't attempt to reboot in order to check if anything was broken...until today_

## Windows boot is broken...
And by "broken", I mean that in the rEFInd boot menu, there was nothing showing for windows. It only showed my linux installation.

At first, I was sure this was an easy fix and all I had to do was boot from a linux disk image, and run the commands:

![hand holding wrench](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jifat8bn0djm494ujghw.jpg)
```
mount /dev/sda3 /mnt
mount /dev/sda1 /mnt/boot
refind-install --root /mnt
```
...unfortunately, that didn't work :(. Attempting to boot directly from `Windows Manager` also didn't work, so at this point I knew something had gone very wrong.

As my sunday evening grew into sunday night, I decided to actually stop being lazy with this fix, and discover why windows was being soo elusive.

## Steps to recovery
Using the steps outlined in the [arch wiki](https://wiki.archlinux.org/title/REFInd#UEFI_shell) for rEFInd, I was able to get my hands on an EFI shell. Using some of the commands from the arch wiki, I was able to discover that the windows boot EFI file didn't exist anymore in the boot partition.

![output of the command bcfg boot dump -v](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8f7yw0qqp5ofy51qb7x6.gif)
_output of `bcfg boot dump -v`_

_Well that explains a lot!_

Both rEFInd and Windows boot manager expected to find a `\EFI\Microsoft\Boot\bootmgfw.efi` file, but it wasn't finding it, so the boot never worked.

### Step 1. Add `bootmgfw.efi` to rEFInd
Boot into Linux and mount your windows partition, then copy the existing file into where rEFInd was expecting to find it.
```
sudo mkdir -p /boot/EFI/Microsoft/Boot
sudo cp /run/media/smac89/Windows/Windows/Boot/EFI/bootmgfw.efi /boot/EFI/Microsoft/Boot
```

_At this point, you will see the blue windows logo from the rEFInd boot menu, but attempting to boot from it may result in the following screen:_

![Windows failed to start](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ieppjw0b82nv4ubelm6i.jpg)

_...if so, continue to the next step_

### Step 2: Get access to a windows terminal
There are [many](https://www.laptopmag.com/articles/repair-windows-10) ways to do [this](https://support.microsoft.com/en-us/windows/create-installation-media-for-windows-99a58364-8c02-206f-aa6f-40c3b507420d). Pick the one that is most convenient, and proceed.

Inside the terminal type the commands:
```
diskpart
list volumes
```
Find the volume containing the boot partition, and assign it a label if it doesn't have one:
```
select volume N
assign letter=m
```
Exit `diskpart`
```
exit
```

Finally, we use the following command to install windows boot entries to the boot partition:

```
bcdboot C:\Windows /l en-US /s m: /f ALL
```

After running the above command, the boot partition now contains all the entries needed to boot windows.

Reboot.

---

Thanks for reading.