VOID egInitScreen(VOID) { EFI_STATUS Status; UINT32 UGAWidth, UGAHeight, UGADepth, UGARefreshRate; // get protocols Status = LibLocateProtocol(&ConsoleControlProtocolGuid, (VOID **) &ConsoleControl); if (EFI_ERROR(Status)) ConsoleControl = NULL; Status = LibLocateProtocol(&UgaDrawProtocolGuid, (VOID **) &UgaDraw); if (EFI_ERROR(Status)) UgaDraw = NULL; Status = LibLocateProtocol(&GraphicsOutputProtocolGuid, (VOID **) &GraphicsOutput); if (EFI_ERROR(Status)) GraphicsOutput = NULL; // get screen size egHasGraphics = FALSE; if (GraphicsOutput != NULL) { egScreenWidth = GraphicsOutput->Mode->Info->HorizontalResolution; egScreenHeight = GraphicsOutput->Mode->Info->VerticalResolution; egHasGraphics = TRUE; } else if (UgaDraw != NULL) { Status = UgaDraw->GetMode(UgaDraw, &UGAWidth, &UGAHeight, &UGADepth, &UGARefreshRate); if (EFI_ERROR(Status)) { UgaDraw = NULL; // graphics not available } else { egScreenWidth = UGAWidth; egScreenHeight = UGAHeight; egHasGraphics = TRUE; } } }
static EFI_TCG * tcg1_interface_check(void) { EFI_GUID tpm_guid = EFI_TCG_PROTOCOL_GUID; EFI_STATUS status; EFI_TCG *tcg; TCG_BOOT_SERVICE_CAPABILITY capability; UINT32 features; EFI_PHYSICAL_ADDRESS event_log_location; EFI_PHYSICAL_ADDRESS event_log_last_entry; status = LibLocateProtocol(&tpm_guid, (void **) &tcg); if (EFI_ERROR(status)) return NULL; capability.Size = (UINT8) sizeof(capability); status = uefi_call_wrapper(tcg->StatusCheck, 5, tcg, &capability, &features, &event_log_location, &event_log_last_entry); if (EFI_ERROR(status)) return NULL; if (capability.TPMDeactivatedFlag) return NULL; if (!capability.TPMPresentFlag) return NULL; return tcg; }
static EFI_STATUS get_battery_status(struct battery_status *status) { CHARGING_APPLET_PROTOCOL *charging_protocol; EFI_STATUS ret; ret = LibLocateProtocol(&gChargingAppletProtocolGuid, (VOID **)&charging_protocol); if (EFI_ERROR(ret)) goto error; ret = uefi_call_wrapper(charging_protocol->GetBatteryInfo, 7, charging_protocol, &status->BatteryInfo, &status->BatteryPresent, &status->BatteryValid, &status->CapacityReadable, &status->BatteryVoltageLevel, &status->BatteryCapacityLevel); if (EFI_ERROR(ret)) goto error; return ret; error: efi_perror(ret, L"Failed to get the battery status"); return ret; }
EFI_STATUS console_key_read(UINT64 *key, BOOLEAN wait) { EFI_GUID EfiSimpleTextInputExProtocolGuid = EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID; static EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *TextInputEx; static BOOLEAN checked; UINTN index; EFI_INPUT_KEY k; EFI_STATUS err; if (!checked) { err = LibLocateProtocol(&EfiSimpleTextInputExProtocolGuid, (VOID **)&TextInputEx); if (EFI_ERROR(err)) TextInputEx = NULL; checked = TRUE; } /* wait until key is pressed */ if (wait) uefi_call_wrapper(BS->WaitForEvent, 3, 1, &ST->ConIn->WaitForKey, &index); if (TextInputEx) { EFI_KEY_DATA keydata; UINT64 keypress; err = uefi_call_wrapper(TextInputEx->ReadKeyStrokeEx, 2, TextInputEx, &keydata); if (!EFI_ERROR(err)) { UINT32 shift = 0; /* do not distinguish between left and right keys */ if (keydata.KeyState.KeyShiftState & EFI_SHIFT_STATE_VALID) { if (keydata.KeyState.KeyShiftState & (EFI_RIGHT_CONTROL_PRESSED|EFI_LEFT_CONTROL_PRESSED)) shift |= EFI_CONTROL_PRESSED; if (keydata.KeyState.KeyShiftState & (EFI_RIGHT_ALT_PRESSED|EFI_LEFT_ALT_PRESSED)) shift |= EFI_ALT_PRESSED; }; /* 32 bit modifier keys + 16 bit scan code + 16 bit unicode */ keypress = KEYPRESS(shift, keydata.Key.ScanCode, keydata.Key.UnicodeChar); if (keypress > 0) { *key = keypress; return 0; } } } /* fallback for firmware which does not support SimpleTextInputExProtocol * * This is also called in case ReadKeyStrokeEx did not return a key, because * some broken firmwares offer SimpleTextInputExProtocol, but never actually * handle any key. */ err = uefi_call_wrapper(ST->ConIn->ReadKeyStroke, 2, ST->ConIn, &k); if (EFI_ERROR(err)) return err; *key = KEYPRESS(0, k.ScanCode, k.UnicodeChar); return 0; }
static EFI_STATUS mode_auto(UINTN *mode) { const UINT32 HORIZONTAL_MAX_OK = 1920; const UINT32 VERTICAL_MAX_OK = 1080; const UINT64 VIEWPORT_RATIO = 10; UINT64 screen_area, text_area; EFI_GUID GraphicsOutputProtocolGuid = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID; EFI_GRAPHICS_OUTPUT_PROTOCOL *GraphicsOutput; EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *Info; EFI_STATUS err; BOOLEAN keep = FALSE; err = LibLocateProtocol(&GraphicsOutputProtocolGuid, (VOID **)&GraphicsOutput); if (!EFI_ERROR(err) && GraphicsOutput->Mode && GraphicsOutput->Mode->Info) { Info = GraphicsOutput->Mode->Info; /* Start verifying if we are in a resolution larger than Full HD * (1920x1080). If we're not, assume we're in a good mode and do not * try to change it. */ if (Info->HorizontalResolution <= HORIZONTAL_MAX_OK && Info->VerticalResolution <= VERTICAL_MAX_OK) keep = TRUE; /* For larger resolutions, calculate the ratio of the total screen * area to the text viewport area. If it's less than 10 times bigger, * then assume the text is readable and keep the text mode. */ else { screen_area = (UINT64)Info->HorizontalResolution * (UINT64)Info->VerticalResolution; text_area = text_area_from_font_size(); if (text_area != 0 && screen_area/text_area < VIEWPORT_RATIO) keep = TRUE; } } if (keep) { /* Just clear the screen instead of changing the mode and return. */ *mode = ST->ConOut->Mode->Mode; uefi_call_wrapper(ST->ConOut->ClearScreen, 1, ST->ConOut); return EFI_SUCCESS; } /* If we reached here, then we have a high resolution screen and the text * viewport is less than 10% the screen area, so the firmware developer * screwed up. Try to switch to a better mode. Mode number 2 is first non * standard mode, which is provided by the device manufacturer, so it should * be a good mode. * Note: MaxMode is the number of modes, not the last mode. */ if (ST->ConOut->Mode->MaxMode > 2) *mode = 2; /* Try again with mode different than zero (assume user requests * auto mode due to some problem with mode zero). */ else if (ST->ConOut->Mode->MaxMode == 2) *mode = 1; /* Else force mode change to zero. */ else *mode = 0; return change_mode(*mode); }
EFI_STATUS EfiDebugAssertInit ( VOID ) { EFI_STATUS Status; Status = LibLocateProtocol (&gEfiDebugAssertProtocolGuid, (VOID **) &mDebugAssert); return Status; }
EFI_STATUS EFIAPI efi_main (EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable) { EFI_INPUT_KEY efi_input_key; int a = 10, b = 20; InitializeLib(ImageHandle, SystemTable); //Print(L"Hello, world!\n"); terra_main(L"Hello, Terra from C!\n"); Print(L"%d + %d = %d\n", a, b, terra_add(a,b)); Print(L"%s\n", SystemTable->FirmwareVendor); EFI_STATUS status; EFI_GRAPHICS_OUTPUT_PROTOCOL *gop; status = LibLocateProtocol(&GraphicsOutputProtocol, (void **)&gop); if(EFI_ERROR(status)) return status; status = uefi_call_wrapper(SystemTable->BootServices->LocateProtocol, 3, &GraphicsOutputProtocol, NULL, &gop); Print(L"Framebuffer base is at %lx\n", gop->Mode->FrameBufferBase); print_modes(gop); Print(L"\n\n\nHit any key\n"); WaitForSingleEvent(SystemTable->ConIn->WaitForKey, 0); status = uefi_call_wrapper(SystemTable->ConIn->ReadKeyStroke, 2, SystemTable->ConIn, &efi_input_key); paint_screen(gop, (UINT32)0x00FF0000); uefi_call_wrapper(BS->Stall, 1, 2 * 1000 * 1000); paint_screen(gop, (UINT32)0x0000FF00); uefi_call_wrapper(BS->Stall, 1, 2 * 1000 * 1000); paint_screen(gop, (UINT32)0x000000FF0); // FROM: https://github.com/vathpela/gnu-efi/blob/master/apps/t7.c Print(L"\n\n\nHit any key to exit this image\n"); WaitForSingleEvent(SystemTable->ConIn->WaitForKey, 0); uefi_call_wrapper(SystemTable->ConOut->OutputString, 2, SystemTable->ConOut, L"\n\n"); status = uefi_call_wrapper(SystemTable->ConIn->ReadKeyStroke, 2, SystemTable->ConIn, &efi_input_key); Print(L"ScanCode: %xh UnicodeChar: %xh\n", efi_input_key.ScanCode, efi_input_key.UnicodeChar); return EFI_SUCCESS; }
EFI_STATUS EfiDebugAssertInit ( VOID ) { EFI_STATUS Status; EFI_GUID DebugAssertProtocolGuid = EFI_DEBUG_ASSERT_PROTOCOL_GUID; Status = LibLocateProtocol (&DebugAssertProtocolGuid, (VOID **)&mDebugAssert); return Status; }
EFI_STATUS efi_main (EFI_HANDLE image_handle, EFI_SYSTEM_TABLE *systab) { EFI_STATUS rc; EFI_GRAPHICS_OUTPUT_PROTOCOL *gop; InitializeLib(image_handle, systab); SetWatchdog(10); rc = LibLocateProtocol(&GraphicsOutputProtocol, (void **)&gop); if (EFI_ERROR(rc)) return rc; print_modes(gop); SetWatchdog(0); return EFI_SUCCESS; }
static EFI_TCG2 * tcg2_interface_check(void) { EFI_GUID tpm2_guid = EFI_TCG2_PROTOCOL_GUID; EFI_STATUS status; EFI_TCG2 *tcg; EFI_TCG2_BOOT_SERVICE_CAPABILITY capability; status = LibLocateProtocol(&tpm2_guid, (void **) &tcg); if (EFI_ERROR(status)) return NULL; capability.Size = (UINT8) sizeof(capability); status = uefi_call_wrapper(tcg->GetCapability, 2, tcg, &capability); if (EFI_ERROR(status)) return NULL; if (!capability.TPMPresentFlag) return NULL; return tcg; }
BOOLEAN is_charger_plugged_in(void) { CHARGING_APPLET_PROTOCOL *charging_protocol; CHARGER_TYPE type; EFI_STATUS ret; ret = LibLocateProtocol(&gChargingAppletProtocolGuid, (VOID **)&charging_protocol); if (EFI_ERROR(ret)) goto error; ret = uefi_call_wrapper(charging_protocol->GetChargerType, 2, charging_protocol, &type); if (EFI_ERROR(ret)) goto error; return type != ChargerUndefined; error: efi_perror(ret, L"Failed to get charger status"); return FALSE; }
EFI_STATUS LibInitializeStrings ( OUT EFI_HII_HANDLE *HiiHandle, IN UINT8 *StringPack, IN EFI_GUID *StringPackGuid ) /*++ Routine Description: Register our included string package to HII and return the HII handle to the data. If previously registered, simply return the handle. Arguments: HiiLibHandle - A pointer to the handle which is used to reference our string data. StringPack - String package StringPackGuid - String package guid Returns: EFI_SUCCESS - Command completed successfully --*/ { EFI_STATUS Status; #if (EFI_SPECIFICATION_VERSION >= 0x0002000A) EFI_HII_PACKAGE_LIST_HEADER *PackageList; EFI_GUID PackageListGuid; UINT64 MonotonicCount; #else EFI_HII_PACKAGES *HiiPackages; VOID **Package; #endif ASSERT (HiiHandle); // // If we've already initialized once, then don't do it again. This is // done to support building shell commands standalone. // if (HiiInitialized == TRUE) { NumStrings++; *HiiHandle = HiiLibHandle; return EFI_SUCCESS; } HiiInitialized = TRUE; #if (EFI_SPECIFICATION_VERSION >= 0x0002000A) LocateHiiProtocols (); // // Update the incoming StringPackGuid to make it unique to be a GUID of a // package list. // CopyMem (&PackageListGuid, StringPackGuid, sizeof (EFI_GUID)); BS->GetNextMonotonicCount (&MonotonicCount); MonotonicCount += *((UINT64 *) (PackageListGuid.Data4)); CopyMem (PackageListGuid.Data4, &MonotonicCount, sizeof (UINT64)); PackageList = PreparePackageList (1, &PackageListGuid, StringPack); ASSERT (PackageList != NULL); Status = gLibHiiDatabase->NewPackageList ( gLibHiiDatabase, PackageList, NULL, HiiHandle ); FreePool (PackageList); #else // // Find the HII protocol // Status = LibLocateProtocol (&gEfiHiiProtocolGuid, (VOID **) &HiiProt); if (EFI_ERROR (Status)) { return Status; } HiiPackages = AllocateZeroPool (sizeof (EFI_HII_PACKAGES) + sizeof (VOID *)); if (HiiPackages == NULL) { return EFI_OUT_OF_RESOURCES; } HiiPackages->GuidId = StringPackGuid; HiiPackages->NumberOfPackages = 1; Package = (VOID **) (((UINT8 *) HiiPackages) + sizeof (EFI_HII_PACKAGES)); *Package = (EFI_HII_STRING_PACK *) StringPack; Status = HiiProt->NewPack (HiiProt, HiiPackages, HiiHandle); FreePool (HiiPackages); #endif NumStrings++; HiiLibHandle = *HiiHandle; return Status; }
EFI_STATUS tpm_log_event(EFI_PHYSICAL_ADDRESS buf, UINTN size, UINT8 pcr, const CHAR8 *description) { EFI_STATUS status; efi_tpm_protocol_t *tpm; efi_tpm2_protocol_t *tpm2; status = LibLocateProtocol(&tpm2_guid, (VOID **)&tpm2); /* TPM 2.0 */ if (status == EFI_SUCCESS) { EFI_TCG2_EVENT *event; if (!tpm2_present(tpm2)) return EFI_SUCCESS; event = AllocatePool(sizeof(*event) + strlen(description) + 1); if (!event) { perror(L"Unable to allocate event structure\n"); return EFI_OUT_OF_RESOURCES; } event->Header.HeaderSize = sizeof(EFI_TCG2_EVENT_HEADER); event->Header.HeaderVersion = 1; event->Header.PCRIndex = pcr; event->Header.EventType = 0x0d; event->Size = sizeof(*event) - sizeof(event->Event) + strlen(description) + 1; memcpy(event->Event, description, strlen(description) + 1); status = uefi_call_wrapper(tpm2->hash_log_extend_event, 5, tpm2, 0, buf, (UINT64) size, event); FreePool(event); return status; } else { TCG_PCR_EVENT *event; UINT32 algorithm, eventnum = 0; EFI_PHYSICAL_ADDRESS lastevent; status = LibLocateProtocol(&tpm_guid, (VOID **)&tpm); if (status != EFI_SUCCESS) return EFI_SUCCESS; if (!tpm_present(tpm)) return EFI_SUCCESS; event = AllocatePool(sizeof(*event) + strlen(description) + 1); if (!event) { perror(L"Unable to allocate event structure\n"); return EFI_OUT_OF_RESOURCES; } event->PCRIndex = pcr; event->EventType = 0x0d; event->EventSize = strlen(description) + 1; algorithm = 0x00000004; status = uefi_call_wrapper(tpm->log_extend_event, 7, tpm, buf, (UINT64)size, algorithm, event, &eventnum, &lastevent); FreePool(event); return status; } return EFI_SUCCESS; }