Defragging my old Dell's UEFI NVRAM
artemis.sh
artemis.sh
NVRAM must maintain atomicity of memory transactions for power failures. Its whole purpose is to store data when you turn your computer off. As a result, when deleteing an EFI variable, you can't manage the individual bytes - you have to delete a whole entry (which can be rather large - based on the EFI specification and the code used for edk2, e.g. https://github.com/tianocore/edk2/blob/83a86f465ccb1a792f5c5...). Deleting these entries might become a problem when you start running against memory constraints and what slots in memory are actually available; hence a possible fragmentation issue.
Additionally, I appreciated how short and specific this blog post was. I enjoy this style of post of someone encountering a problem and solving it.
Just an observation from when I was debugging a board that selfdestructed when booting a particular efi-file so I had to dig into the flash contents to figure out why, but I think this particular code was straight from tianocore.
Yes, this achieves atomicity, yes this gets you wear leveling (with the caveat that the more data you store, the worse the lifetime gets because you need to do more swaps) but it also is a consequence of HW constraints and the approach flows directly from it. It might be the consequence of HW/SW co-design at some point in the past as well, but I have no idea whether this is true.
This information is based on my experience in automotive.
I have a toyota where the odometer resets back to 299,995 miles on every power cycle because whoever designed the wear levelling didn't plan that far ahead...
SPI flash typically have somewhere between 10k to 100k write cycles. Without wear leveling, lets say a write is made on every boot, and lets say a machine is booted 3 times a day you are still going to have a 9+ year life on a flash chip with only 10k writes.
EDIT: Did a search for a random "replacement SPI flash MSI" and found an MX25L25673G being used. Using that as an example it has 100k write cycles, using 3 writes a day, that's like 91 years. Basically flash memory is at the point where its "good enough" not to worry about.
Edit: however its not unheard of for SPI flash to have specs of 500k write cycles and a 100 year data retention (below 500k write cycles) these days, so if a 2025 machine was using such a chip you just got to make sure the caps don't leak and erode the PCB, or the thin layer of epoxy bonding the multiple layers of the PCB breaking down causing it to delaminate.
So yeah, the CMOS battery not clearing the bios + my own stupidity cost me ~150$.
Booting to the UEFI shell, Shellx64.efi, will automatically map the drives and bring you to the Shell command line.
Lots of motherboards did not actually have Shellx64.efi built-in, in that case you would see a boot option in the firmware to boot to UEFI shell found on a boot device (an internal or external drive).
Plus some of the built-in Shellx64.efi don't actually include the BCFG command, so you might need to use an external boot device containing a more complete Shellx64.efi anyway.
At the shell command line, bcfg boot dump -b is what you enter to list the boot entries, starting with entry 00.
bcfg boot rm 04 would then be the shell command to remove entry 04, for instance.
You don't need Windows or Linux for this since you're booting to the UEFI shell to access the firmware directly.
This doesn't do anything to the boot entries in the EFI folder itself, which on Windows will likely have an entry in its BCD for every one it has been exposed to, and they can come right back into the firmware unless you also delete them from the BCD.
Remember, a properly crafted EFI folder will ideally boot as expected when there are not any boot entries in the firmware at all. Then the ideal firmware will autoinclude the entries found on the disk and you might not even need an EFI folder after that. But things are not usually ideal. Not every OS gets this right the first time, and it can change for the better or worse after a while.
Alternatively, if you are on Windows, at the admin command prompt, BCDEDIT /enum All, will display all entries, with the firmware ones towards the top. Then you can simultaneously delete an unwanted entry from both the EFI folder and the firmware with Bcdedit /delete {target-guid-here}, or in in powershell Bcdedit /delete "{target-guid-here}", since powershell is still having trouble with the curly braces.
on the plus side, you can probably get a copy of the state before wiping it, at least as a logical structure. but what kind of fallback boot path you are on, is very specific to what the machine likes to do.
A lot of your "gaming" motherboards come with dual bios. In such cases, if you toast one you flip a switch and use the other. In most cases with motherboard with 2 bios chips you can boot from the good bios, download the latest version of your bios from your motherboard manufacturer, flip the selector switch back to the bad bios while still booted, and reflash over the bad bios it from within your OS. You would most likely lose any custom efi vars but your could easily reconfigure them as needed.
If your motherboard doesn't have dual bios or bios recovery system built in, all is not lost but you probably going to have to reflash the bios via an external programmer, but such tools are dirt cheap these days. heck you could do it from a Raspberry Pi and a SOP8 test clip if you don't have any other computer to reflash the chip.
If you want to be super safe, you could dump the contents of the bios flash chip using an external programmer before attempting to do any of this (Same tools as you would need to flash the chip, Another computer, and if that other computer isn't a SBC with SPI GPIO a cheap USB SPI tool - You could use a RPI Pico for this for just a few bucks).
(Dumping the contents of the chip via the bios provider / motherboard manufactures own bios flasher tool will often skip over parts of the chip. Looking at you Intel ME! - but if your only touching efi vars, a backup from the bios flashing tool should be good enough, just make sure you don't overwrite the areas the tool didn't back up, which is why I suggested using an external tool to dump the contents, much safer.)
EDIT: As for the risk? Well again it depends, you gotta be pretty unlucky to kill the bios because of a power failure right as your messing about with it, but it can happen. I went though a spell of "tinkering in the bios" for a while *cough*Windows SLIC*cough*, and only messed up the bios once during those years and I did a far bit of flashing back then, but I was able recover the bios using an external programmer.
I mean the whole point of dual bios is for disaster recovery so not having separate storage space while having dual bios seems a bit pointless imo. But tbf, doing something like that wouldn't be the most stupid thing I heard a manufacturer do.