From: Apple Date: Sat, 3 Feb 2018 22:02:36 +0000 (+0000) Subject: system_cmds-790.30.1.tar.gz X-Git-Tag: macos-10132^0 X-Git-Url: https://git.saurik.com/apple/system_cmds.git/commitdiff_plain/c00fdd175635bfca33c58d1b2951a1565ce91f17?ds=inline system_cmds-790.30.1.tar.gz --- diff --git a/nvram.tproj/nvram.c b/nvram.tproj/nvram.c index 89f27fc..3711499 100644 --- a/nvram.tproj/nvram.c +++ b/nvram.tproj/nvram.c @@ -56,6 +56,27 @@ static io_registry_entry_t gOptionsRef; static bool gUseXML; static bool gUseForceSync; +#if TARGET_OS_BRIDGE /* Stuff for nvram bridge -> intel */ +#include +#include + +static kern_return_t LinkMacNVRAMSymbols(void); +static kern_return_t GetMacOFVariable(char *name, char **value); +static kern_return_t SetMacOFVariable(char *name, char *value); +static void DeleteMacOFVariable(char *name); + +static bool gBridgeToIntel; +static void *gDL_handle; +static void *gNvramInterface; + +static void (*hostInterfaceInitialize_fptr)(void); +static void *(*createNvramHostInterface_fptr)(const char *handle); +static kern_return_t (*destroyNvramHostInterface_fptr)(void *interface); +static kern_return_t (*getNVRAMVariable_fptr)(void *interface, char *name, char **buffer, uint32_t *size); +static kern_return_t (*setNVRAMVariable_fptr)(void *interface, char *name, char *buffer, uint32_t size); +static void (*hostInterfaceDeinitialize_fptr)(void); /* may not need? */ + +#endif /* TARGET_OS_BRIDGE */ int main(int argc, char **argv) { @@ -88,6 +109,12 @@ int main(int argc, char **argv) for (str += 1 ; *str; str++) { switch (*str) { case 'p' : +#if TARGET_OS_BRIDGE + if (gBridgeToIntel) { + fprintf(stderr, "-p not supported for Mac NVRAM store.\n"); + return 1; + } +#endif PrintOFVariables(); break; @@ -96,6 +123,12 @@ int main(int argc, char **argv) break; case 'f': +#if TARGET_OS_BRIDGE + if (gBridgeToIntel) { + fprintf(stderr, "-f not supported for Mac NVRAM store.\n"); + return 1; + } +#endif cnt++; if (cnt < argc && *argv[cnt] != '-') { ParseFile(argv[cnt]); @@ -105,6 +138,12 @@ int main(int argc, char **argv) break; case 'd': +#if TARGET_OS_BRIDGE + if (gBridgeToIntel) { + fprintf(stderr, "-d not supported for Mac NVRAM store.\n"); + return 1; + } +#endif cnt++; if (cnt < argc && *argv[cnt] != '-') { DeleteOFVariable(argv[cnt]); @@ -114,6 +153,12 @@ int main(int argc, char **argv) break; case 'c': +#if TARGET_OS_BRIDGE + if (gBridgeToIntel) { + fprintf(stderr, "-c not supported for Mac NVRAM store.\n"); + return 1; + } +#endif ClearOFVariables(); break; case 's': @@ -121,6 +166,17 @@ int main(int argc, char **argv) // commit the variable to nonvolatile storage gUseForceSync = true; break; +#if TARGET_OS_BRIDGE + case 'm': + // -m option is unadvertised -- used to set nvram variables on the Intel side + // from the ARM side (Bridge -> Mac) + fprintf(stdout, "Using Mac NVRAM store.\n"); + + LinkMacNVRAMSymbols(); + gBridgeToIntel = true; + break; +#endif + default: strcpy(errorMessage, "no such option as --"); errorMessage[strlen(errorMessage)-1] = *str; @@ -405,18 +461,39 @@ static void SetOrGetOFVariable(char *str) if (set == 1) { // On sets, the OF variable's value follows the equal sign. value = str; - - result = SetOFVariable(name, value); - NVRamSyncNow(name); /* Try syncing the new data to device, best effort! */ +#if TARGET_OS_BRIDGE + if (gBridgeToIntel) { + result = SetMacOFVariable(name, value); + } + else +#endif + { + result = SetOFVariable(name, value); + NVRamSyncNow(name); /* Try syncing the new data to device, best effort! */ + } if (result != KERN_SUCCESS) { errx(1, "Error setting variable - '%s': %s", name, mach_error_string(result)); } } else { - result = GetOFVariable(name, &nameRef, &valueRef); - if (result != KERN_SUCCESS) { - errx(1, "Error getting variable - '%s': %s", name, - mach_error_string(result)); +#if TARGET_OS_BRIDGE + if (gBridgeToIntel) { + result = GetMacOFVariable(name, &value); + if (result != KERN_SUCCESS) { + errx(1, "Error getting variable - '%s': %s", name, + mach_error_string(result)); + } + nameRef = CFStringCreateWithCString(kCFAllocatorDefault, name, kCFStringEncodingUTF8); + valueRef = CFStringCreateWithCString(kCFAllocatorDefault, value, kCFStringEncodingUTF8); + } + else +#endif + { + result = GetOFVariable(name, &nameRef, &valueRef); + if (result != KERN_SUCCESS) { + errx(1, "Error getting variable - '%s': %s", name, + mach_error_string(result)); + } } PrintOFVariable(nameRef, valueRef, 0); @@ -425,6 +502,47 @@ static void SetOrGetOFVariable(char *str) } } +#if TARGET_OS_BRIDGE +static kern_return_t LinkMacNVRAMSymbols() +{ + gDL_handle = dlopen("libMacEFIHostInterface.dylib", RTLD_LAZY); + if (gDL_handle == NULL) { + errx(errno, "Failed to dlopen libMacEFIHostInterface.dylib"); + return KERN_FAILURE; /* NOTREACHED */ + } + + hostInterfaceInitialize_fptr = dlsym(gDL_handle, "hostInterfaceInitialize"); + if (hostInterfaceInitialize_fptr == NULL) { + errx(errno, "failed to link hostInterfaceInitialize"); + } + createNvramHostInterface_fptr = dlsym(gDL_handle, "createNvramHostInterface"); + if (createNvramHostInterface_fptr == NULL) { + errx(errno, "failed to link createNvramHostInterface"); + } + destroyNvramHostInterface_fptr = dlsym(gDL_handle, "destroyNvramHostInterface"); + if (destroyNvramHostInterface_fptr == NULL) { + errx(errno, "failed to link destroyNvramHostInterface"); + } + getNVRAMVariable_fptr = dlsym(gDL_handle, "getNVRAMVariable"); + if (getNVRAMVariable_fptr == NULL) { + errx(errno, "failed to link getNVRAMVariable"); + } + setNVRAMVariable_fptr = dlsym(gDL_handle, "setNVRAMVariable"); + if (setNVRAMVariable_fptr == NULL) { + errx(errno, "failed to link setNVRAMVariable"); + } + hostInterfaceDeinitialize_fptr = dlsym(gDL_handle, "hostInterfaceDeinitialize"); + if (hostInterfaceDeinitialize_fptr == NULL) { + errx(errno, "failed to link hostInterfaceDeinitialize"); + } + + /* also do the initialization */ + hostInterfaceInitialize_fptr(); + gNvramInterface = createNvramHostInterface_fptr(NULL); + + return KERN_SUCCESS; +} +#endif // GetOFVariable(name, nameRef, valueRef) // @@ -446,6 +564,23 @@ static kern_return_t GetOFVariable(char *name, CFStringRef *nameRef, return KERN_SUCCESS; } +#if TARGET_OS_BRIDGE +// GetMacOFVariable(name, value) +// +// Get the named firmware variable from the Intel side. +// Return the value in value +// +static kern_return_t GetMacOFVariable(char *name, char **value) +{ + uint32_t value_size; + kern_return_t result = KERN_FAILURE; + assert(getNVRAMVariable_fptr != NULL); + + result = getNVRAMVariable_fptr(gNvramInterface, name, value, &value_size); + + return result; +} +#endif // SetOFVariable(name, value) // @@ -510,6 +645,17 @@ static kern_return_t SetOFVariable(char *name, char *value) return result; } +#if TARGET_OS_BRIDGE +static kern_return_t SetMacOFVariable(char *name, char *value) +{ + kern_return_t result = KERN_FAILURE; + assert(setNVRAMVariable_fptr != NULL); + + result = setNVRAMVariable_fptr(gNvramInterface, name, value, strlen(value)); + + return result; +} +#endif // DeleteOFVariable(name) // @@ -521,6 +667,13 @@ static void DeleteOFVariable(char *name) SetOFVariable(kIONVRAMDeletePropertyKey, name); } +#if TARGET_OS_BRIDGE +static void DeleteMacOFVariable(char *name) +{ + /* Not yet implementable */ +} +#endif + static void NVRamSyncNow(char *name) { if (!gUseForceSync) { diff --git a/system_cmds.xcodeproj/project.pbxproj b/system_cmds.xcodeproj/project.pbxproj index 4611e1b..06d5b8d 100644 --- a/system_cmds.xcodeproj/project.pbxproj +++ b/system_cmds.xcodeproj/project.pbxproj @@ -7434,6 +7434,7 @@ 1873300218CBD4A700275344 /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { + HEADER_SEARCH_PATHS = "$(SDKROOT)/usr/local/include"; INSTALL_PATH = /usr/sbin; PRODUCT_NAME = nvram; }; @@ -8334,6 +8335,7 @@ BAE589A4137836A00049DD3B /* Release */ = { isa = XCBuildConfiguration; buildSettings = { + HEADER_SEARCH_PATHS = "$(SDKROOT)/usr/local/include"; INSTALL_PATH = /usr/sbin; PRODUCT_NAME = nvram; };