From 9432a1e0481922c44c63977a5f71aaa1f3b18d23 Mon Sep 17 00:00:00 2001 From: Droga Date: Tue, 24 Nov 2026 08:52:02 -0300 Subject: [PATCH 1/3] Start with config.plist.bak when config.plist cannot be loaded A power cut while the editor saved config.plist could leave it missing or cut short, and OpenCore stopped before the picker. It now loads the backup the editor wrote and checked just before, says so in the options editor, and Undo puts it back. While running from the backup the editor never writes it. TestPickerEditor cuts the power after every number of writes and deletes in a save and in the repair, and checks that the next start always finds a config. --- Changelog.md | 1 + DrogaBox/WRITES.md | 42 +-- Include/Acidanthera/Library/OcMainLib.h | 15 +- Library/OcMainLib/OpenCoreConfigSelect.c | 20 +- Library/OcMainLib/OpenCoreEditor.c | 44 +++- Library/OcMainLib/OpenCoreEditorConfigs.c | 20 +- Library/OcMainLib/OpenCoreMisc.c | 3 +- Utilities/TestPickerEditor/PickerEditor.c | 304 +++++++++++++++++++++- 8 files changed, 409 insertions(+), 40 deletions(-) diff --git a/Changelog.md b/Changelog.md index 81efd6c42..bfee13ee8 100644 --- a/Changelog.md +++ b/Changelog.md @@ -8,6 +8,7 @@ OpenCore Changelog - Added `AllowPickerEdit` Security option: `F2` in the builtin picker and OpenCanopy opens an options editor to change boot arguments (typed or with a list of usual flags; for a single boot or permanently) and `config.plist` (switches, numbers, strings, data) without leaving OpenCore - Added `Kernel panic log` screen to the options editor to read a panic saved in NVRAM - Added `Compare configs` and `Undo the last save` to the options editor, and lists of kernel patches, blocks and booter patches +- Added starting with `config.plist.bak` when `config.plist` cannot be loaded (for example after a power cut while the editor saved it) instead of stopping, shown in the options editor - Added `Check the config` and `Log` screens to the options editor - Added `Drivers, ACPI, Tools` lists (on/off and order) and a `Quirks` screen with the usual switches to the options editor - Added `Kexts` item to the options editor: all kexts of `Kernel` -> `Add` in the load order, switching them on and off, moving them with `+` and `-`, and a check of the order with `OSBundleLibraries` of the kexts, which `F5` can fix diff --git a/DrogaBox/WRITES.md b/DrogaBox/WRITES.md index 01a5bae2e..d69bd05e0 100644 --- a/DrogaBox/WRITES.md +++ b/DrogaBox/WRITES.md @@ -1,7 +1,7 @@ # What the editor writes, and what a power cut would leave -Checked 2026-11-24 against `main` (after the split into one file per screen). Read from the code; none -of it has been tried with a real power cut. +Checked 2026-11-24 on the branch `d7-config-backup`, which starts with `config.plist.bak` when `config.plist` +cannot be loaded. Read from the code and tested on the Mac (below). ## Choosing a config (`OcMiscSetConfigChoice`, `OpenCoreConfigSelect.c`) @@ -29,25 +29,31 @@ Before step 1 the new text has already been parsed, and it is refused if it has file it replaces. - Cut during step 1: `config.plist` is untouched. Safe; `config.plist.bak` may be missing or cut. -- **Cut during step 2, between the delete and the end of the write: `config.plist` is missing or cut.** - At the next start `OcMiscLoadConfig` fails and OpenCore stops in `CpuDeadLoop` - (`OpenCoreMisc.c`, after `OcMiscLoadConfig`). The machine hangs before the picker. The previous file - is whole in `config.plist.bak`, but only another boot disk (the USB stick) can put it back. +- Cut during step 2, between the delete and the end of the write: `config.plist` is missing or cut. + At the next start `OcMiscLoadConfig` cannot load it and loads `config.plist.bak` instead, which + step 1 had written and checked. The editor's main menu and the Config files screen say so, and + "Undo the last save" puts the backup back as `config.plist`. Safe. - The read-back in steps 1 and 2 may come from the firmware's cache rather than the disk, so it shows that the write was accepted, not that it reached the disk. -## Proposal (not done: it needs the owner's yes) +## Starting with the backup (`OcMiscLoadConfig`) -Two parts, either one helps: +Only when `config.plist` itself cannot be loaded (missing, not a plist, cut short), after a chosen +config has failed or when there is none. Never with a vault: the vault only signs `config.plist`. The +name in use stays `config.plist`, the file to put right, and `OcMiscGetConfigSource` returns +`OcConfigSourceBackup`. -1. **At boot**, in `OcMiscLoadConfig`: if `config.plist` cannot be loaded, try `config.plist.bak` before - stopping, and show "started with the backup" in the editor's main menu, the way a failed chosen - config is shown today. This closes the gap completely, but it changes the code that runs on every - start, which stage 2 said it would leave alone. -2. **In the editor**: write the new file as `config.plist.new`, check it, then delete `config.plist` and - rename `config.plist.new` to `config.plist` (`SetInfo` with the new name). The window shrinks from a - whole write to one rename. If the firmware's FAT driver refuses the rename, it falls back to the - current steps. It cannot close the window on its own: FAT cannot rename over an existing file. +While OpenCore runs from the backup, the editor never writes `config.plist.bak`: it is the only +config known to work. Every save then writes `config.plist` alone, so a cut leaves the backup as it +was, and the next start uses it again. -Both can be tested on the Mac by making the harness's fake file system stop after the n-th write, for -every n, and checking that the next start finds a config it can load. +## How it is tested + +`TestPickerEditor` has a fake file system that can cut the power after any number of writes and +deletes: the next write leaves half the file, and nothing after it is written. `TestPowerCut` does it +at every step of a save (with an old backup and with none) and of putting the backup back (with +`config.plist` missing and broken), and each time loads the config the way OpenCore does at start. It +always gets a config, the old one or the new one. With the fallback at start taken out, 21 checks +fail; with the editor allowed to write the backup while running from it, 4 fail. + +None of it has been tried with a real power cut. diff --git a/Include/Acidanthera/Library/OcMainLib.h b/Include/Acidanthera/Library/OcMainLib.h index f02b2eca1..01ff845f3 100644 --- a/Include/Acidanthera/Library/OcMainLib.h +++ b/Include/Acidanthera/Library/OcMainLib.h @@ -375,6 +375,13 @@ OcMiscGetVersionString ( **/ #define OC_CONFIG_CHOICE_PATH L"config.choice" +/** + Added to the name of a configuration for the copy of it from before the last save in + the editor. OpenCore starts with the backup of the default configuration when the + default one cannot be loaded. +**/ +#define OC_CONFIG_BACKUP_SUFFIX L".bak" + /** Where the configuration in use comes from. **/ @@ -394,7 +401,13 @@ typedef enum { /// /// A choice was made, but it could not be used, and the default is in use instead. /// - OcConfigSourceFallback + OcConfigSourceFallback, + /// + /// The default could not be loaded either, and its backup from the last save in the + /// editor, OPEN_CORE_CONFIG_PATH with OC_CONFIG_BACKUP_SUFFIX, is in use instead. + /// The name of the configuration stays the default one, the file to repair. + /// + OcConfigSourceBackup } OC_CONFIG_SOURCE; /** diff --git a/Library/OcMainLib/OpenCoreConfigSelect.c b/Library/OcMainLib/OpenCoreConfigSelect.c index 53d6e6fad..a852384c4 100644 --- a/Library/OcMainLib/OpenCoreConfigSelect.c +++ b/Library/OcMainLib/OpenCoreConfigSelect.c @@ -346,6 +346,7 @@ OcMiscLoadConfig ( { EFI_STATUS Status; CHAR16 Chosen[OC_CONFIG_MAX_NAME + 1]; + CHAR16 Backup[ARRAY_SIZE (OPEN_CORE_CONFIG_PATH) + ARRAY_SIZE (OC_CONFIG_BACKUP_SUFFIX)]; BOOLEAN Persistent; mConfigSource = OcConfigSourceDefault; @@ -387,7 +388,24 @@ OcMiscLoadConfig ( } } - return ConfigLoadFile (Storage, OPEN_CORE_CONFIG_PATH, Config); + Status = ConfigLoadFile (Storage, OPEN_CORE_CONFIG_PATH, Config); + if (!EFI_ERROR (Status) || Storage->HasVault) { + return Status; + } + + // + // Without a configuration OpenCore cannot start at all. The editor keeps the previous + // default configuration next to it when it saves, and it is only missing or cut short + // if the save was interrupted, so the backup is the last one that worked. + // + UnicodeSPrint (Backup, sizeof (Backup), L"%s%s", OPEN_CORE_CONFIG_PATH, OC_CONFIG_BACKUP_SUFFIX); + DEBUG ((DEBUG_WARN, "OC: Cannot use %s - %r, trying %s\n", OPEN_CORE_CONFIG_PATH, Status, Backup)); + if (!EFI_ERROR (ConfigLoadFile (Storage, Backup, Config))) { + mConfigSource = OcConfigSourceBackup; + return EFI_SUCCESS; + } + + return Status; } CONST CHAR16 * diff --git a/Library/OcMainLib/OpenCoreEditor.c b/Library/OcMainLib/OpenCoreEditor.c index ab8bf8b1f..994eb41b0 100644 --- a/Library/OcMainLib/OpenCoreEditor.c +++ b/Library/OcMainLib/OpenCoreEditor.c @@ -192,8 +192,12 @@ EdVerifyFile ( /** Replace config.plist, with the previous file saved next to it. + While OpenCore runs from the backup, because config.plist could not be loaded, the + backup is the only configuration known to work and it is left alone: only config.plist + is written. + @param[in] Storage OpenCore storage. - @param[in] Old Contents of the file being replaced. + @param[in] Old Contents of the file being replaced, NULL if there is none. @param[in] OldSize Size of Old. @param[in] New New contents. @param[in] NewSize Size of New. @@ -235,18 +239,22 @@ EdWriteConfig ( // // First the backup, the configuration is not touched if it cannot be made. + // OpenCore starts with the backup if config.plist is missing or cut short, so from + // here on there is always one of the two it can start with. // - OcDeleteFile (Root, BackupPath); - Status = OcSetFileData (Root, BackupPath, Old, OldSize); - if (!EFI_ERROR (Status) && !EdVerifyFile (Root, BackupPath, Old, OldSize)) { - Status = EFI_VOLUME_CORRUPTED; - } - - if (EFI_ERROR (Status)) { - DEBUG ((DEBUG_WARN, "OCE: Failed to write backup - %r\n", Status)); + if ((Old != NULL) && (OcMiscGetConfigSource (NULL) != OcConfigSourceBackup)) { OcDeleteFile (Root, BackupPath); - Root->Close (Root); - return Status; + Status = OcSetFileData (Root, BackupPath, Old, OldSize); + if (!EFI_ERROR (Status) && !EdVerifyFile (Root, BackupPath, Old, OldSize)) { + Status = EFI_VOLUME_CORRUPTED; + } + + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_WARN, "OCE: Failed to write backup - %r\n", Status)); + OcDeleteFile (Root, BackupPath); + Root->Close (Root); + return Status; + } } OcDeleteFile (Root, ConfigPath); @@ -258,7 +266,9 @@ EdWriteConfig ( if (EFI_ERROR (Status)) { DEBUG ((DEBUG_WARN, "OCE: Failed to write configuration - %r, putting the previous one back\n", Status)); OcDeleteFile (Root, ConfigPath); - OcSetFileData (Root, ConfigPath, Old, OldSize); + if (Old != NULL) { + OcSetFileData (Root, ConfigPath, Old, OldSize); + } } Root->Close (Root); @@ -662,6 +672,16 @@ OcMiscShowEditor ( State->Storage = Context->StorageContext; State->Entry = BootEntry; + if (OcMiscGetConfigSource (NULL) == OcConfigSourceBackup) { + EdSetStatus ( + State->Status, + sizeof (State->Status), + &State->StatusIsError, + TRUE, + "config.plist failed to load, started with config.plist.bak (Undo puts it back)" + ); + } + Status = EdUiInit (&State->Ui, Context); if (EFI_ERROR (Status)) { FreePool (State); diff --git a/Library/OcMainLib/OpenCoreEditorConfigs.c b/Library/OcMainLib/OpenCoreEditorConfigs.c index 641f428f6..33c4049af 100644 --- a/Library/OcMainLib/OpenCoreEditorConfigs.c +++ b/Library/OcMainLib/OpenCoreEditorConfigs.c @@ -47,13 +47,21 @@ EdRestoreBackup ( Present = OcStorageReadFileUnicode (State->Storage, OcMiscGetConfigName (), &PresentSize); EdConfigNameAscii (Name, sizeof (Name)); - if ((Backup == NULL) || (Present == NULL)) { + if ((Backup == NULL) || ((Present == NULL) && (OcMiscGetConfigSource (NULL) != OcConfigSourceBackup))) { AsciiSPrint (Line, sizeof (Line), "There is no %a.bak: nothing was saved from the editor yet", Name); EdUiNotify (&State->Ui, Line, TRUE); } else if (EFI_ERROR (EditPlistCountErrors (Backup, BackupSize, &Errors))) { EdUiNotify (&State->Ui, "The backup is not a config OpenCore can load", TRUE); } else { - AsciiSPrint (Line, sizeof (Line), "Put %a.bak back as %a? The present one will be kept as the backup", Name, Name); + if (OcMiscGetConfigSource (NULL) == OcConfigSourceBackup) { + // + // The present one could not be loaded, and the backup stays as it is. + // + AsciiSPrint (Line, sizeof (Line), "Put %a.bak back as %a? The backup stays as it is", Name, Name); + } else { + AsciiSPrint (Line, sizeof (Line), "Put %a.bak back as %a? The present one will be kept as the backup", Name, Name); + } + if (EdUiConfirm (&State->Ui, Line)) { Status = EdWriteConfig (State->Storage, Present, PresentSize, Backup, BackupSize); if (EFI_ERROR (Status)) { @@ -92,6 +100,8 @@ EdConfigSourceNote ( return " (kept)"; case OcConfigSourceFallback: return " (the chosen one failed)"; + case OcConfigSourceBackup: + return " (it failed, started with its .bak)"; default: return ""; } @@ -111,6 +121,8 @@ EdConfigSourceDescription ( return "chosen for this start only"; case OcConfigSourceKept: return "chosen to stay until another one is chosen"; + case OcConfigSourceBackup: + return "could not be loaded, started with its .bak"; default: return "the default"; } @@ -378,7 +390,9 @@ EdConfigChooser ( EdUiTitle (Ui, "Config files", Subtitle); EdListDraw (Ui, 3, Visible, &List, EdConfigItem, Files); - if (Source == OcConfigSourceFallback) { + if (Source == OcConfigSourceBackup) { + EdUiStatus (Ui, Ui->AttrError, "config.plist failed to load, started with config.plist.bak (Undo puts it back)"); + } else if (Source == OcConfigSourceFallback) { EdUnicodeToAscii (Failed, Name, sizeof (Name)); AsciiSPrint (Failure, sizeof (Failure), "The chosen %a could not be used, the default config is in use", Name); EdUiStatus (Ui, Ui->AttrError, Failure); diff --git a/Library/OcMainLib/OpenCoreMisc.c b/Library/OcMainLib/OpenCoreMisc.c index c4d4a2838..cb165e68f 100644 --- a/Library/OcMainLib/OpenCoreMisc.c +++ b/Library/OcMainLib/OpenCoreMisc.c @@ -556,7 +556,8 @@ OcMiscEarlyInit ( OcMiscGetConfigName (), OcMiscGetConfigSource (NULL) == OcConfigSourceOnce ? "chosen for this start" : OcMiscGetConfigSource (NULL) == OcConfigSourceKept ? "chosen to stay" : - OcMiscGetConfigSource (NULL) == OcConfigSourceFallback ? "the chosen one could not be used" : "default" + OcMiscGetConfigSource (NULL) == OcConfigSourceFallback ? "the chosen one could not be used" : + OcMiscGetConfigSource (NULL) == OcConfigSourceBackup ? "it could not be loaded, its backup is in use" : "default" )); Status = gRT->GetTime (&BootTime, NULL); diff --git a/Utilities/TestPickerEditor/PickerEditor.c b/Utilities/TestPickerEditor/PickerEditor.c index 28a7c2e54..1541ed453 100644 --- a/Utilities/TestPickerEditor/PickerEditor.c +++ b/Utilities/TestPickerEditor/PickerEditor.c @@ -34,6 +34,7 @@ #include #include "../../Library/OcMainLib/OpenCoreEditorInternal.h" +#include "../../Library/OcMainLib/OpenCoreEditorPrivate.h" STATIC UINT32 mFailures; STATIC UINT32 mChecks; @@ -833,6 +834,43 @@ HasBackup ( STATIC CHAR8 mDirectory[256]; STATIC BOOLEAN mFileWriteProtected; +// +// A power cut: this many more deletes and writes go through, the next one is cut off +// halfway (a write leaves the first half of the file, a delete does nothing), and nothing +// is written after it. -1 is no power cut. +// +STATIC INT32 mFileOpsLeft = -1; +STATIC BOOLEAN mPowerWentOut; + +typedef enum { + FileOpDone, + FileOpCut, + FileOpNoPower +} FILE_OP; + +STATIC +FILE_OP +NextFileOp ( + VOID + ) +{ + if (mPowerWentOut) { + return FileOpNoPower; + } + + if (mFileOpsLeft < 0) { + return FileOpDone; + } + + if (mFileOpsLeft == 0) { + mPowerWentOut = TRUE; + return FileOpCut; + } + + --mFileOpsLeft; + return FileOpDone; +} + STATIC VOID HostPath ( @@ -884,6 +922,10 @@ TestDeleteFile ( return EFI_WRITE_PROTECTED; } + if (NextFileOp () != FileOpDone) { + return EFI_DEVICE_ERROR; + } + return unlink (Path) == 0 ? EFI_SUCCESS : EFI_NOT_FOUND; } @@ -895,22 +937,28 @@ TestSetFileData ( IN UINT32 Size ) { - CHAR8 Path[512]; - FILE *File; + CHAR8 Path[512]; + FILE *File; + FILE_OP Op; HostPath (FileName, Path, sizeof (Path)); if (mFileWriteProtected) { return EFI_WRITE_PROTECTED; } + Op = NextFileOp (); + if (Op == FileOpNoPower) { + return EFI_DEVICE_ERROR; + } + File = fopen (Path, "wb"); if (File == NULL) { return EFI_DEVICE_ERROR; } - fwrite (Buffer, 1, Size, File); + fwrite (Buffer, 1, Op == FileOpCut ? Size / 2 : Size, File); fclose (File); - return EFI_SUCCESS; + return Op == FileOpCut ? EFI_DEVICE_ERROR : EFI_SUCCESS; } STATIC @@ -5016,6 +5064,252 @@ TestBrokenConfig ( WriteTestFile ("config.plist", Original, Size); } +/** + Whether a test file has these contents. +**/ +STATIC +BOOLEAN +TestFileIs ( + IN CONST CHAR8 *Name, + IN CONST VOID *Data, + IN UINT32 Size + ) +{ + VOID *Read; + UINT32 ReadSize; + BOOLEAN Same; + + Read = ReadTestFile (Name, &ReadSize); + if (Read == NULL) { + return FALSE; + } + + Same = ReadSize == Size && CompareMem (Read, Data, Size) == 0; + FreePool (Read); + return Same; +} + +/** + OpenCore starts with config.plist.bak when config.plist cannot be loaded. +**/ +STATIC +VOID +TestConfigBackupStart ( + IN CONST VOID *Original, + IN UINT32 OriginalSize + ) +{ + EFI_STATUS Status; + UINT32 Timeout; + + RemoveTestFile ("config.choice"); + RemoveTestFile ("config2.plist"); + WriteConfigVariant ("config.plist.bak", 33, TRUE); + + // + // A good config.plist is used, whatever the backup is. + // + WriteTestFile ("config.plist", Original, OriginalSize); + Timeout = LoadConfigTimeout (&Status); + CHECK (!EFI_ERROR (Status) && Timeout == 5 && ConfigIs (L"config.plist", OcConfigSourceDefault)); + + // + // Missing, not a plist, cut short: the backup is used, and the name stays config.plist, + // the file to put right. + // + RemoveTestFile ("config.plist"); + Timeout = LoadConfigTimeout (&Status); + CHECK (!EFI_ERROR (Status) && Timeout == 33 && ConfigIs (L"config.plist", OcConfigSourceBackup)); + + WriteTestFile ("config.plist", "not a plist", 11); + Timeout = LoadConfigTimeout (&Status); + CHECK (!EFI_ERROR (Status) && Timeout == 33 && ConfigIs (L"config.plist", OcConfigSourceBackup)); + + WriteTestFile ("config.plist", Original, OriginalSize / 2); + Timeout = LoadConfigTimeout (&Status); + CHECK (!EFI_ERROR (Status) && Timeout == 33 && ConfigIs (L"config.plist", OcConfigSourceBackup)); + + // + // A chosen config that fails, then config.plist that fails: still the backup. + // + WriteMarker ("config9.plist\nkeep\n"); + RemoveTestFile ("config.plist"); + Timeout = LoadConfigTimeout (&Status); + CHECK (!EFI_ERROR (Status) && Timeout == 33 && ConfigIs (L"config.plist", OcConfigSourceBackup)); + RemoveTestFile ("config.choice"); + + // + // Without a usable backup OpenCore still cannot start. + // + WriteTestFile ("config.plist.bak", "not a plist", 11); + LoadConfigTimeout (&Status); + CHECK (EFI_ERROR (Status)); + RemoveTestFile ("config.plist.bak"); + LoadConfigTimeout (&Status); + CHECK (EFI_ERROR (Status)); + + // + // The vault only knows config.plist, so the backup is never used with it. + // + WriteConfigVariant ("config.plist.bak", 33, TRUE); + mStorage.HasVault = TRUE; + LoadConfigTimeout (&Status); + CHECK (EFI_ERROR (Status)); + mStorage.HasVault = FALSE; + + // + // The editor says so, and Undo puts the backup back as config.plist without touching it. + // + WriteTestFile ("config.plist.bak", Original, OriginalSize); + LoadConfigTimeout (&Status); + CHECK (!EFI_ERROR (Status) && ConfigIs (L"config.plist", OcConfigSourceBackup)); + ScriptReset (); + Press (KeyDown, MENU_UNDO); + Press (KeyEnter, 1); + Press (KeyEnter, 1); // yes + Press (KeyEsc, 1); + RUN_EDITOR (NULL); + CHECK (SeenOnScreen ("config.plist failed to load, started with config.plist.bak")); + CHECK (SeenOnScreen ("(it failed, started with its .bak)")); + CHECK (SeenOnScreen ("The backup stays as it is")); + CHECK (SeenOnScreen ("The previous config is back, restart to apply it")); + CHECK (TestFileIs ("config.plist", Original, OriginalSize)); + CHECK (TestFileIs ("config.plist.bak", Original, OriginalSize)); + Timeout = LoadConfigTimeout (&Status); + CHECK (!EFI_ERROR (Status) && Timeout == 5 && ConfigIs (L"config.plist", OcConfigSourceDefault)); + + // + // The same with a config.plist that is there but broken: it does not become the backup. + // + WriteTestFile ("config.plist", "not a plist", 11); + LoadConfigTimeout (&Status); + CHECK (!EFI_ERROR (Status) && ConfigIs (L"config.plist", OcConfigSourceBackup)); + ScriptReset (); + Press (KeyDown, MENU_UNDO); + Press (KeyEnter, 1); + Press (KeyEnter, 1); // yes + Press (KeyEsc, 1); + RUN_EDITOR (NULL); + CHECK (TestFileIs ("config.plist", Original, OriginalSize)); + CHECK (TestFileIs ("config.plist.bak", Original, OriginalSize)); + + WriteTestFile ("config.plist", Original, OriginalSize); + RemoveTestFile ("config.plist.bak"); + LoadConfigTimeout (NULL); +} + +/** + Cut the power after every number of writes and deletes in a save, and start again each + time: OpenCore has to find a config it can start with, the old one or the new one. + Present and Backup are config.plist and config.plist.bak before the save (NULL for no + file), Old is what the editor passes as the file being replaced. +**/ +STATIC +VOID +PowerCutAtEveryStep ( + IN CONST CHAR8 *What, + IN CONST VOID *Present, + IN UINT32 PresentSize, + IN CONST VOID *Old, + IN UINT32 OldSize, + IN CONST VOID *New, + IN UINT32 NewSize, + IN CONST VOID *Backup, + IN UINT32 BackupSize, + IN UINT32 OldTimeout, + IN UINT32 NewTimeout + ) +{ + EFI_STATUS Status; + EFI_STATUS SaveStatus; + UINT32 Timeout; + INT32 Steps; + UINT32 Before; + + for (Steps = 0; Steps < 32; ++Steps) { + if (Present != NULL) { + WriteTestFile ("config.plist", Present, PresentSize); + } else { + RemoveTestFile ("config.plist"); + } + + if (Backup != NULL) { + WriteTestFile ("config.plist.bak", Backup, BackupSize); + } else { + RemoveTestFile ("config.plist.bak"); + } + + LoadConfigTimeout (NULL); + + mFileOpsLeft = Steps; + mPowerWentOut = FALSE; + SaveStatus = EdWriteConfig (&mStorage, Old, OldSize, New, NewSize); + mFileOpsLeft = -1; + + Before = mFailures; + Timeout = LoadConfigTimeout (&Status); + CHECK (!EFI_ERROR (Status) && (Timeout == OldTimeout || Timeout == NewTimeout)); + if (mFailures != Before) { + printf (" (%s, power cut after %d writes and deletes)\n", What, (int)Steps); + } + + if (!mPowerWentOut) { + // + // The save had all it needed: the new config is in place. + // + CHECK (!EFI_ERROR (SaveStatus) && Timeout == NewTimeout && ConfigIs (L"config.plist", OcConfigSourceDefault)); + CHECK (Steps >= 1); + break; + } + + mPowerWentOut = FALSE; + } + + CHECK (Steps < 32); +} + +STATIC +VOID +TestPowerCut ( + IN CONST VOID *Original, + IN UINT32 OriginalSize + ) +{ + VOID *Changed; + UINT32 ChangedSize; + VOID *Stale; + UINT32 StaleSize; + + RemoveTestFile ("config.choice"); + WriteConfigVariant ("variant.plist", 44, TRUE); + Changed = ReadTestFile ("variant.plist", &ChangedSize); + WriteConfigVariant ("variant.plist", 66, TRUE); + Stale = ReadTestFile ("variant.plist", &StaleSize); + RemoveTestFile ("variant.plist"); + CHECK (Changed != NULL && Stale != NULL); + if ((Changed == NULL) || (Stale == NULL)) { + return; + } + + // + // A save from the editor, with an old backup from an earlier save, and with none. + // + PowerCutAtEveryStep ("save", Original, OriginalSize, Original, OriginalSize, Changed, ChangedSize, Stale, StaleSize, 5, 44); + PowerCutAtEveryStep ("first save", Original, OriginalSize, Original, OriginalSize, Changed, ChangedSize, NULL, 0, 5, 44); + + // + // Putting the backup back after starting with it: config.plist missing, or broken. + // + PowerCutAtEveryStep ("repair of a missing config.plist", NULL, 0, NULL, 0, Original, OriginalSize, Original, OriginalSize, 5, 5); + PowerCutAtEveryStep ("repair of a broken config.plist", "not a plist", 11, "not a plist", 11, Original, OriginalSize, Original, OriginalSize, 5, 5); + + FreePool (Changed); + FreePool (Stale); + WriteTestFile ("config.plist", Original, OriginalSize); + RemoveTestFile ("config.plist.bak"); + LoadConfigTimeout (NULL); +} + int ENTRY_POINT ( int argc, @@ -5087,6 +5381,8 @@ ENTRY_POINT ( TestConsoleSizes (Original, Size); TestLongNames (Original, Size); TestBrokenConfig (Original, Size); + TestConfigBackupStart (Original, Size); + TestPowerCut (Original, Size); free (Original); printf ("%u checks, %u failures\n", mChecks, mFailures); From c7d11663579c46d3058cd22a7495990fe4d5530f Mon Sep 17 00:00:00 2001 From: Droga Date: Tue, 24 Nov 2026 12:46:35 -0300 Subject: [PATCH 2/3] Do not halt on a configuration that is replaced by another one Messages from before the log is set up are logged again once it is, with HaltLevel of the configuration loaded in the end. The error about a config.plist that could not be loaded, or a chosen configuration that could not be used, then stopped OpenCore with the usual HaltLevel (DEBUG_ERROR) right after it had started with the backup or the default. Only the last failure is an error now. --- Library/OcMainLib/OpenCoreConfigSelect.c | 33 +++++++++++++++++++----- 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/Library/OcMainLib/OpenCoreConfigSelect.c b/Library/OcMainLib/OpenCoreConfigSelect.c index a852384c4..51830a6f4 100644 --- a/Library/OcMainLib/OpenCoreConfigSelect.c +++ b/Library/OcMainLib/OpenCoreConfigSelect.c @@ -308,12 +308,29 @@ ConfigReadChoice ( return Valid ? EFI_SUCCESS : EFI_INVALID_PARAMETER; } +/** + Load and parse one configuration file. + + Messages logged before the log is set up are kept and logged again once it is, with + Misc.Security.HaltLevel of the configuration that was loaded in the end. An error + about a file that OpenCore then replaces with another one would stop OpenCore right + there with the usual HaltLevel, so such a failure is only a warning. + + @param[in] Storage OpenCore storage. + @param[in] Name Name of the configuration file. + @param[out] Config Configuration. + @param[in] ErrorLevel Level of the message when the file cannot be used. + + @retval EFI_SUCCESS The configuration was loaded. + @retval other It could not be read or parsed. +**/ STATIC EFI_STATUS ConfigLoadFile ( IN OC_STORAGE_CONTEXT *Storage, IN CONST CHAR16 *Name, - OUT OC_GLOBAL_CONFIG *Config + OUT OC_GLOBAL_CONFIG *Config, + IN UINTN ErrorLevel ) { EFI_STATUS Status; @@ -322,7 +339,7 @@ ConfigLoadFile ( Data = OcStorageReadFileUnicode (Storage, Name, &Size); if (Data == NULL) { - DEBUG ((DEBUG_ERROR, "OC: Failed to load configuration %s!\n", Name)); + DEBUG ((ErrorLevel, "OC: Failed to load configuration %s!\n", Name)); return EFI_NOT_FOUND; } @@ -332,7 +349,7 @@ ConfigLoadFile ( FreePool (Data); if (EFI_ERROR (Status)) { - DEBUG ((DEBUG_ERROR, "OC: Failed to parse configuration %s!\n", Name)); + DEBUG ((ErrorLevel, "OC: Failed to parse configuration %s!\n", Name)); } return Status; @@ -375,7 +392,7 @@ OcMiscLoadConfig ( } if (!ConfigIsDefaultName (Chosen)) { - Status = ConfigLoadFile (Storage, Chosen, Config); + Status = ConfigLoadFile (Storage, Chosen, Config, DEBUG_WARN); if (!EFI_ERROR (Status)) { StrCpyS (mConfigName, ARRAY_SIZE (mConfigName), Chosen); mConfigSource = Persistent ? OcConfigSourceKept : OcConfigSourceOnce; @@ -388,7 +405,10 @@ OcMiscLoadConfig ( } } - Status = ConfigLoadFile (Storage, OPEN_CORE_CONFIG_PATH, Config); + // + // With a vault there is no backup to fall back to (see below). + // + Status = ConfigLoadFile (Storage, OPEN_CORE_CONFIG_PATH, Config, Storage->HasVault ? DEBUG_ERROR : DEBUG_WARN); if (!EFI_ERROR (Status) || Storage->HasVault) { return Status; } @@ -400,11 +420,12 @@ OcMiscLoadConfig ( // UnicodeSPrint (Backup, sizeof (Backup), L"%s%s", OPEN_CORE_CONFIG_PATH, OC_CONFIG_BACKUP_SUFFIX); DEBUG ((DEBUG_WARN, "OC: Cannot use %s - %r, trying %s\n", OPEN_CORE_CONFIG_PATH, Status, Backup)); - if (!EFI_ERROR (ConfigLoadFile (Storage, Backup, Config))) { + if (!EFI_ERROR (ConfigLoadFile (Storage, Backup, Config, DEBUG_WARN))) { mConfigSource = OcConfigSourceBackup; return EFI_SUCCESS; } + DEBUG ((DEBUG_ERROR, "OC: Failed to load configuration %s and its backup - %r!\n", OPEN_CORE_CONFIG_PATH, Status)); return Status; } From 361b80857f5de7ec8c2add4f18ee04b720238166 Mon Sep 17 00:00:00 2001 From: Droga Date: Tue, 24 Nov 2026 12:59:40 -0300 Subject: [PATCH 3/3] WRITES.md: what was tried in QEMU, and why a replaced file is only a warning --- DrogaBox/WRITES.md | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/DrogaBox/WRITES.md b/DrogaBox/WRITES.md index d69bd05e0..75a21f521 100644 --- a/DrogaBox/WRITES.md +++ b/DrogaBox/WRITES.md @@ -1,7 +1,7 @@ # What the editor writes, and what a power cut would leave -Checked 2026-11-24 on the branch `d7-config-backup`, which starts with `config.plist.bak` when `config.plist` -cannot be loaded. Read from the code and tested on the Mac (below). +Checked 2026-11-24. OpenCore starts with `config.plist.bak` when `config.plist` cannot be loaded. Read +from the code, tested on the Mac and in QEMU (below). ## Choosing a config (`OcMiscSetConfigChoice`, `OpenCoreConfigSelect.c`) @@ -56,4 +56,16 @@ at every step of a save (with an old backup and with none) and of putting the ba always gets a config, the old one or the new one. With the fallback at start taken out, 21 checks fail; with the editor allowed to write the backup while running from it, 4 fail. +In QEMU (OVMF, TCG) with the real OpenCore.efi, RELEASE and DEBUG, `Target` 67 and `HaltLevel` +0x80000000: a `config.plist` cut in half or missing, with a good backup, starts with the backup and +the editor says so; without a backup it stops, saying that both failed; with a vault the backup is +not used; "Undo the last save" writes `config.plist` back through the firmware's FAT driver and the +next start loads it. A chosen config that cannot be loaded falls back to `config.plist`, with and +without `keep`. + +Messages from before the log is set up are logged again once it is, with the `HaltLevel` of the +config loaded in the end. So a failed file that is then replaced by another one is logged as a +warning, not an error: as an error, the usual `HaltLevel` stopped OpenCore right after it had +started with the backup or the default. + None of it has been tried with a real power cut.