Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
54 changes: 36 additions & 18 deletions DrogaBox/WRITES.md
Original file line number Diff line number Diff line change
@@ -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. 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`)

Expand Down Expand Up @@ -29,25 +29,43 @@ 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.

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.
15 changes: 14 additions & 1 deletion Include/Acidanthera/Library/OcMainLib.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
**/
Expand All @@ -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;

/**
Expand Down
49 changes: 44 additions & 5 deletions Library/OcMainLib/OpenCoreConfigSelect.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

Expand All @@ -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;
Expand All @@ -346,6 +363,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;
Expand Down Expand Up @@ -374,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;
Expand All @@ -387,7 +405,28 @@ OcMiscLoadConfig (
}
}

return 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;
}

//
// 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, 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;
}

CONST CHAR16 *
Expand Down
44 changes: 32 additions & 12 deletions Library/OcMainLib/OpenCoreEditor.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
20 changes: 17 additions & 3 deletions Library/OcMainLib/OpenCoreEditorConfigs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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 "";
}
Expand All @@ -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";
}
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion Library/OcMainLib/OpenCoreMisc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading
Loading