Skip to content

OC: Option for NVRAM persistence to enable boot-arg modification and SIP disable #575

Closed

Description

@osy

In the current implementation, we can add UEFI variables using the Add key under NVRAM. The way OcSetNvramVariable works is that it attempts to read the variable. If it's not found, then it will add it with the flags EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS. Block can be used to delete the variable first in order to always force the Add value instead.

I propose a third option. Either a new key Persist that operates like Add but creates the variable with EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS. Or a new option PersistAdd with a boolean value that will mask EFI_VARIABLE_NON_VOLATILE for Add variables.

Why is this useful? Consider the following use case: We need some base set of boot-args to boot up OSX. So we add them with Add. However, in the process of development/testing unrelated stuff, we wish to modify boot-args by appending some new string. Using sudo nvram boot-args="new value" fails because OSX will try to use the attribute EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS and it fails because in the EFI specs:

If a preexisting variable is rewritten with different attributes, SetVariable() shall not modify the variable and shall return EFI_INVALID_PARAMETER.

Okay, so what if we just don't put boot-args into the config.plist. Instead we just set sudo nvram boot-args="some value" initially. That works, except if we require certain boot-args to boot, then forgetting to append them will make the system unbootable (even more painful if the vault is used). So ideally, we can use OC's "erase nvram" option to wipe all the variables and then reset them with a clean default.

A second use case: csr-active-config is used for SIP. OSX does the checks to make sure csr-active-config is only modified in recovery OS. However, setting it in recovery OS still doesn't work if there is already an Add entry. But if we do not include csr-active-config in both Add then using OC's "erase nvram" option will always enable SIP.

An option to allow the NVRAM options in config.plist to act as a signed/verified golden default configuration but still allow modification to NVRAM through normal OSX techniques would be useful.

Activity

  1. vit9696 commented on Nov 15, 2019

    @vit9696
    Contributor

    Hi,

    Firstly, regarding the concept.

    • OpenCore NVRAM Add works mostly the way Mac firmwares work. It is meant to set the default variable values when they are missing due to previous NVRAM reset. This way you can get default user interface scaling (UIScale), enabled System Integrity Protection (csr-active-config), or many other variables like host name, backlight level, volume level, and so on.
    • OpenCore and kext projects in Acidanthera are built around the way that boot arguments are a temporary way of configuration, allowing you to quickly set a parameter through UEFI Shell. We do not believe any machine should need any boot arguments to work, if it does need our boot arguments, these should have DeviceProperties aliases.
    • OpenCore NVRAM Block concept is a hack or rather a security measure against the operating system. In certain cases we believe that the operating system cannot give enough protection for NVRAM variables. For example, Windows can modify csr-active-config at no issue in any mode. Currently FwRuntimeServices does not protect from that, and that is why we do provide an option to force some values.
    • OpenCore uses volatile variables to avoid accidentally triggering some driver bug. Many NVRAM drivers in laptops fail to reclaim space after resetting or deleting variables, so we have to workaround. Nothing changes if we use non-volatile variables, however, if there is any sensible reason to, we could consider.

    Secondly, about the expected workflow.

    • The user has empty Block section (unless he wants that security I explained above part in), csr-active-config <00 00 00 00>, boot-args "-v".
    • Before using OpenCore the user resets NVRAM. When using a new computer this is not needed, but there also are no macOS variables, when migrating from e.g. Clover this is essential, as it leaves tons of garbage variables.
    • First start gives verbose mode and fully enabled SIP.
    • Then the user reboots to recovery and uses csrutil disable. This makes csr-active-config non-volatile, and sets it to non-zero value, effectively disabling SIP.
    • OpenCore no longer overrides csr-active-config at boot, but still provides -v for boot-args.
    • The user sets new boot-args, "-v batman=0xff". The same thing as with csrutil disable happens. The variable becomes non-volatile, OpenCore no longer sets it at next boot.
    • The user runs NVRAM cleaning with OpenCore. This removes all variables and effectively gives the user what he originally had. SIP is now fully on, and boot-args are back to -v. This is natural and matches Mac behaviour.

    Given the above, I do not see an issue or the need to change anything. The logic looks perfectly natural to me.

  2. osy commented on Nov 15, 2019

    @osy
    Author

    Let me do some more tests and I will get back to you on the override behaviour I observed.

    Regarding "We do not believe any machine should need any boot arguments to work", is there a device injection option for shikigva=32 shiki-id=Mac-BE088AF8C5EB4FA2?

  3. vit9696 commented on Nov 15, 2019

    @vit9696
    Contributor

    Regarding "We do not believe any machine should need any boot arguments to work", is there a device injection option for shikigva=32 shiki-id=Mac-BE088AF8C5EB4FA2?

    That's a good point, should really implement them as IGPU properties. Could you submit a bugreport please?

    If a preexisting variable is rewritten with different attributes, SetVariable() shall not modify the variable and shall return EFI_INVALID_PARAMETER.

    I believe macOS firstly deletes variables. Or my firmware is non-conformant, as we tested the behaviour above and it worked. We could consider some routes to fix this in case it causes problems. One of them is to make FwRuntimeServices.efi delete volatile variables on set failure. @Download-Fritz, what do you think about it?

  4. mhaeuser commented on Nov 16, 2019

    @mhaeuser
    Member

    @vit9696 This is a tough question because in the end it's a bug in Apple's nvram utility if it really does not delete the variables first. If you "fix" it in FwRuntimeServices, it will explicitly violate the spec. On the other hand, I don't see a good point for the specified behaviour either except as some sort of request sanitizing, imo wait for what @osy86 has to report later.

  5. osy commented on Nov 16, 2019

    @osy
    Author

    This is what I've tested

    First boot:

    $ sudo nvram boot-args
    boot-args	alcid=11 shikigva=32 shiki-id=Mac-BE088AF8C5EB4FA2 -disablegfxfirmware
    $ sudo nvram boot-args="alcid=11 shikigva=32 shiki-id=Mac-BE088AF8C5EB4FA2 -disablegfxfirmware test=test"
    $ sudo nvram boot-args
    boot-args	alcid=11 shikigva=32 shiki-id=Mac-BE088AF8C5EB4FA2 -disablegfxfirmware test=test
    

    After rebooting:

    $ sudo nvram boot-args
    boot-args	alcid=11 shikigva=32 shiki-id=Mac-BE088AF8C5EB4FA2 -disablegfxfirmware
    

    I've also reversed /System/Library/Extensions/AppleEFIRuntime.kext/Contents/PlugIns/AppleEFINVRAM.kext/Contents/MacOS/AppleEFINVRAM and looked into AppleEFINVRAM::setVariable (called by AppleEFINVRAM::setProperty) and could not find a delete before set. SetVariable is always called with EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS

  6. vit9696 commented on Nov 16, 2019

    @vit9696
    Contributor

    Thanks. In this case I do not mind ignoring the spec and incorporating the following change in SetVariable wrapper of FwRuntimeServices.efi.

    1. If SetVariable returned EFI_INVALID_PARAMETER and passed Attributes are EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS
    2. Call GetVariable with NULL buffer, zero size, and obtain current Attributes.
    3. If GetVariable returned EFI_BUFFER_TOO_SMALL and current Attributes are EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS
    4. Call SetVariable with NULL buffer, zero size, and current Attributes.
    5. Call SetVariable with passed buffer, size, and Attributes (forward return).
  7. vit9696 commented on Nov 17, 2019

    @vit9696
    Contributor

    Implemented in

    [master 269e18a] FwRuntimeServices: Added workaround for V to NV variable upgrade
     3 files changed, 43 insertions(+), 1 deletion(-)
    
  8. vit9696 commented on Dec 5, 2019

    @vit9696
    Contributor

    @osy86 can we close this now?

  9. osy commented on Dec 5, 2019

    @osy
    Author

    Sorry, will test tomorrow. Just got back tonight.

  10. osy commented on Dec 6, 2019

    @osy
    Author

    On latest release of OC and AppleSupport:

    $ sudo nvram boot-args
    boot-args	alcid=11 shikigva=32 shiki-id=Mac-BE088AF8C5EB4FA2 -disablegfxfirmware brcmfx-country=#a
    $ sudo nvram boot-args="alcid=11 shikigva=32 shiki-id=Mac-BE088AF8C5EB4FA2 -disablegfxfirmware test=test"
    nvram: Error setting variable - 'boot-args': (iokit/common) not permitted
    
  11. vit9696 commented on Dec 6, 2019

    @vit9696
    Contributor

    Are you positive this is not caused by System Integrity Protection or alike?

  12. osy commented on Dec 6, 2019

    @osy
    Author

    This is the same computer/configuration as #575 (comment) and also SIP shouldn't affect boot-args variable right

  13. vit9696 commented on Dec 6, 2019

    @vit9696
    Contributor

    From what I remember SIP does affect boot-args. You cannot e.g. add -s argument at the very least, and only a few boot arguments from a white list were allowed in older macOS versions. I do not know how it is now, but it should be like just -v or such. That is why NVIDIA was forced to stop using a boot argument for their web drivers for instance.

  14. osy commented on Dec 6, 2019

    @osy
    Author

    So creating a new variable still works. Setting a variable already marked NV (I used "prev-lang:kbd") still works. It seems only previously volatile variables now trigger that error instead of silently failing to change attribute.

    That's strange to me. Looking at the edit, https://github.com/acidanthera/AppleSupportPkg/blob/master/Platform/FwRuntimeServices/UefiRuntimeServices.c#L981 this implies that previously Status == EFI_INVALID_PARAMETER otherwise this new code path wouldn't be taken. But in that case, I should have seen an error message before.

  15. 32 remaining items

  16. vit9696 commented on Jan 4, 2020

    @vit9696
    Contributor

    @osy86 I believe the issue is fully resolved in master. Sorry for taking so long. Please report whether it works fine now.

  17. cattyhouse commented on Jan 4, 2020

    @cattyhouse
    Contributor

    Just did a test, the new option WriteFlash=true does not work on my Asrock B360M.

    • in normal mode, not able to change boot-args with sudo boot-args="existing-args -v", -v is the added args, it says no permission.
    • in recovery mode, i was able to change the boot-args. but the -v was not actually active after reboot, macOS still booted with no verbose/debug.

    is this due to broken nvram?

  18. vit9696 commented on Jan 4, 2020

    @vit9696
    Contributor

    is this due to broken nvram?

    yes.

  19. cattyhouse commented on Jan 4, 2020

    @cattyhouse
    Contributor

    is this due to broken nvram?

    yes.

    thanks, off topic, it would be wonderful if motherboards like B360M get hardware nvram, sometimes macOS panic or suddenly rebooted during daily usage (like yesterday it rebooted with no reason after 10 days uptime), there is barely no report to see what was the cause of the panic/reboot.

  20. vit9696 commented on Jan 4, 2020

    @vit9696
    Contributor

    It would be wonderful if people that need it go and do it.

  21. osy commented on Jan 5, 2020

    @osy
    Author

    @vit9696 I can confirm it works. Thanks for the help!

  22. meaganmargaret commented on Jan 26, 2020

    @meaganmargaret

    It doesn't work for Asus Sage C621 systems. Doesn't work for mine. And, no, a Asus X299 system is NOT a Z390.....

  23. tarkh commented on Mar 6, 2020

    @tarkh

    Having same problem with ASUS x99 Deluxe. Real and Emulated NVRAM did not work. While everything fine with creating nvram.plist in EFI root with proper content, OC didn't get it on the boot. I.e altering boot-args with -uia_exclude_ss while configuring USB with USBMap fails after reboot. Emulating NVRAM like in manual: https://khronokernel-2.gitbook.io/opencore-vanilla-desktop-guide/post-install/nvram

  24. carlitossuper1 commented on Apr 19, 2020

    @carlitossuper1

    Sorry to chime in.

    I get

    Setting NVRAM 7C436110-AB2A-4BBB-A880-FE41995C9F82:fmm-mobileme-token-FMM - Invalid Parameter

    Only with this variable. And I've been reading here that you have a limit for the variables. In this case. The Find My Mac Token is really long (1422 bytes in my case). And I get the error either setting it up on the config.plist (via NVRAM > ADD parameters) or just on the Legacy nvram I have (nvram.plist).

    Can you give me a hand?

    I know. It's not that important. Actually I have a desktop system and FMM is not that useful, but eventually I'd like to get it working.

  25. vit9696 commented on Apr 19, 2020

    @vit9696
    Contributor

    It is your firmware NVRAM implementation that does not allow such long variables I am afraid. Not much to do here.

  26. carlitossuper1 commented on Apr 19, 2020

    @carlitossuper1

    I don't have hardware NVRAM. My system is old (P55 Motherboard) with legacy BIOS. I use emulated NVRAM.

  27. vit9696 commented on Apr 19, 2020

    @vit9696
    Contributor

    Well, that means the emulated NVRAM driver has such limitation. In this case it might be possible to increase the variable size by reconfiguring the driver.

  28. carlitossuper1 commented on Apr 19, 2020

    @carlitossuper1

    Can you guide me how to do that? Or can you increase the limitation for a future commit of OC?

    Like I said, the priority is way too low, so don't stress for that.

    Also thanks in advance. I'm very grateful for OpenCore. It's amazing!

    OT: I couldn't install windows in UEFI mode through OpenCore. I can format a hard drive and it creates all the partitions is UEFI mode (recovery, MSR, System and the actual NTFS) but after selecting it, it won't install. It says that the partition could'n be located. Do you know someone can help me?

  29. vit9696 commented on Apr 19, 2020

    @vit9696
    Contributor

    As for increasing variable size in our version of legacy installation please file a separate issue, but it is indeed unlikely we will look into it soon, sorry.

    As for installing Windows in UEFI mode, please be more specific. I am pretty sure it works on legacy installations, as @roddy20 has no issues with it.

  30. carlitossuper1 commented on Apr 19, 2020

    @carlitossuper1

    Here's the error:

    image

    The windows installer can format the hard disk and create the proper partitions (I have OpenCore installed on another disk). But, when I select the partition, I get the above message and can't continue. Note that the created partitions are the ones created on an actual UEFI system. On legacy, windows creates System Reserved and the primary partition.

    Maybe @roddy20 can help me?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions