Back to Home

How I learned to directly reboot into the correct OS via UEFI

uefi · linux · windows · efibootmgr · bcdedit

How I learned to directly reboot into the correct OS via UEFI

  • Tutorial

Good evening, Habrahabr!


Today we will learn how to reboot from Linux directly to Windows and vice versa in just one (double) click.


image


Given:


  • Two GPT disks with different OS
  • Motherboard with UEFI and disabled Legacy Mode
  • Windows carefully delivered with full UEFI support
  • Linux (I have Manjaro), which was carefully installed by cutting out any mention of GRUB and other old-fashioned things
  • rEFInd - a beautiful boot manager (no, not a bootloader)

It is necessary:


  • Write two scripts on * sh and cmd (bat), which will allow you to reboot into the desired OS

Notes:


  • Working options are designated as Solution , the rest is my reasoning and description of the path to the goal.
  • I deliberately omit the description of those details that are not relevant to the essence of the setting, or that vary easily. These details are mentioned because for me they were not obvious at the time of tuning, so the article becomes more understandable for inexperienced readers who will not see instructions for drawing an owl in it.
  • Why you should be careful when modifying NVRAM

Linux:


Linux is your bro
Linux is an excellent base for working with a computer, so everything we need is already in the repositories, and the collective mind of the community knows everything and is always ready to help. Therefore, after a brief study of the Internet, put efibootmgr :


I had it like that
sudo pacman -S efibootmgr

Ok, now run:


sudo efibootmgr

We see something like this
Timeout: 1 seconds
BootOrder: 0001,0000
Boot0000* Windows Boot Manager
Boot0001* rEFInd Boot Manager

Attentive readers have probably noticed that something is wrong in the conclusion, but then I was inspired by the thought that in 15 minutes I would fly between OSes without problems, and did not pay due attention to it.
Ok, 3 minutes for the documentation, and we find the necessary parameter " -n ", which sets the custom boot order exactly once. We try to execute this command:
Solution


sudo efibootmgr -n 0000 && sync && reboot

And we find ourselves in Windows, as desired. Now we write this command into the script / alias / *. Desktop-file and enjoy how everything turned out great.


Windows:


Typical Windows
It all started with the search for an efibootmgr analogue for Windows, which in its pure form, of course, does not exist. To calm my conscience, I even tried to use the Linux Subsystem, but this, of course, did not work.
A quick search on the Internet showed that the bcdedit utility has similar functionality for modifying NVRAM . Great, I think, I start PowerShell from under the Administrator and write


bcdedit /enum firmware

Here is what i saw

Firmware Boot Manager
---------------------
identifier              {fwbootmgr}
displayorder            {6893bb38-946b-11e7-b175-9301bd8a88f4}
                        {bootmgr}
timeout                 1
Windows Boot Manager
--------------------
identifier              {bootmgr}
device                  partition=\Device\HarddiskVolume2
path                    \EFI\Microsoft\Boot\bootmgfw.efi
description             Windows Boot Manager
locale                  ru-RU
inherit                 {globalsettings}
default                 {current}
resumeobject            {6893bb40-946b-11e7-b175-9301bd8a88f4}
toolsdisplayorder       {memdiag}
timeout                 30
Firmware Application (101fffff)
-------------------------------
identifier              {6893bb38-946b-11e7-b175-9301bd8a88f4}
device                  partition=\Device\HarddiskVolume2
path                    \EFI\REFIND\REFIND_X64.EFI
description             rEFInd Boot Manager

Where is linux? Where to load?I hate Windows
In fact, it turned out that it was not Windows that was to blame, but I (yes, that is the moment for attentive users), and here's why: rEFInd is a great utility that usually requires only installing itself to work correctly. She knows how to pick up all .efi files, different distributions with different kernels, she substitutes icons. A charm, not a tool. But this played a trick on me, as it turned out that UEFI did not know anything about Linux, since the corresponding .efi file was missing.
Therefore, we reboot back to Linux, configure systemd-boot (bootctl). Now everything looks like this:


Timeout: 1 seconds
BootOrder: 0001,0003,0000,0002
Boot0000* Windows Boot Manager
Boot0001* rEFInd Boot Manager
Boot0002* Linux Boot Manager
Boot0003* Manjaro

Go back and run again.


bcdedit /enum firmware

Here is what I saw now

Firmware Boot Manager
---------------------
identifier              {fwbootmgr}
displayorder            {6893bb38-946b-11e7-b175-9301bd8a88f4}
                        {bootmgr}
                        {ff0bc716-c088-11e7-bf74-000acd2dac7d}
                        {ff0bc716-c088-11e7-bf74-000acd2dac7d}
timeout                 1
Windows Boot Manager
--------------------
identifier              {bootmgr}
device                  partition=\Device\HarddiskVolume2
path                    \EFI\Microsoft\Boot\bootmgfw.efi
description             Windows Boot Manager
locale                  ru-RU
inherit                 {globalsettings}
default                 {current}
resumeobject            {6893bb40-946b-11e7-b175-9301bd8a88f4}
toolsdisplayorder       {memdiag}
timeout                 30
Firmware Application (101fffff)
-------------------------------
identifier              {6893bb38-946b-11e7-b175-9301bd8a88f4}
device                  partition=\Device\HarddiskVolume2
path                    \EFI\REFIND\REFIND_X64.EFI
description             rEFInd Boot Manager
Firmware Application (101fffff)
-------------------------------
identifier              {ff0bc716-c088-11e7-bf74-000acd2dac7d}
device                  partition=\Device\HarddiskVolume2
path                    \EFI\SYSTEMD\SYSTEMD-BOOTX64.EFI
description             Linux Boot Manager
Firmware Application (101fffff)
-------------------------------
identifier              {ff0bc717-c088-11e7-bf74-000acd2dac7d}
device                  partition=\Device\HarddiskVolume1
path                    \EFI\manjaro\vmlinuz-4.13-x86_64
description             Manjaro

It is worth mentioning here that reddit users helped me solve the problem . Thanks to them, we have the next step:


bcdedit /bootsequence {ff0bc716-c088-11e7-bf74-000acd2dac7d}

Where the argument is the identifier of the required option - Linux Boot Manager.


Troubleshooting

Powershell does not allow this command to execute normally, swearing at


The entry list data is not valid as specified.

All because Microsoft periodically likes to break something. The solution is simple and elegant - we call the classic CMD and work in it. This can be done by the team.


cmd

We reboot and see that the boot order has not changed, and we ended up in the first element in BootOrder (I had rEFInd), select Windows and see a terrible hello from DOS that tells us that \ EFI \ SYSTEMD \ SYSTEMD-BOOTX64. EFI not found. Yes, we changed the parameters of the Windows boot loader, but not UEFI.
The fight against this mistake took me all the holidays, but nothing good came of it. I was already thinking of writing a small program in C ++ for this (that this is possible follows from the existence of software such as EasyUEFI).
But here on the next site I found this design


 bcdedit /set {bootmgr} path ....

And then it dawned on me that you can directly tell bcdedit what and where to write. Further it was worth checking guess only:
Solution


bcdedit.exe /set {fwbootmgr} bootsequence {ff0bc716-c088-11e7-bf74-000acd2dac7d} /addfirst

It is important here that we explicitly said to write not in {bootmgr} (as it apparently does by default), but in {fwbootmgr}, which is our UEFI settings.
We reboot and everything works the way we wanted it to. We save this matter in bat / lnk, add


shutdown /r /t 0

We expose the launch from under the administrator and you're done!
Thanks for attention! I would be very glad to comments on the technical part in the comments, comments on the design - in the PM.

Read Next