static EFI_STATUS tpm1_measure_to_pcr_and_event_log(const EFI_TCG *tcg, UINT32 pcrindex, const EFI_PHYSICAL_ADDRESS buffer, UINTN buffer_size, const CHAR16 *description) { EFI_STATUS status; TCG_PCR_EVENT *tcg_event; UINT32 event_number; EFI_PHYSICAL_ADDRESS event_log_last; UINTN desc_len; desc_len = (StrLen(description) + 1) * sizeof(CHAR16); tcg_event = AllocateZeroPool(desc_len + sizeof(TCG_PCR_EVENT)); if (tcg_event == NULL) return EFI_OUT_OF_RESOURCES; tcg_event->EventSize = desc_len; CopyMem((VOID *) & tcg_event->Event[0], (VOID *) description, desc_len); tcg_event->PCRIndex = pcrindex; tcg_event->EventType = EV_IPL; event_number = 1; status = uefi_call_wrapper(tcg->HashLogExtendEvent, 7, tcg, buffer, buffer_size, TCG_ALG_SHA, tcg_event, &event_number, &event_log_last); if (EFI_ERROR(status)) return status; uefi_call_wrapper(BS->FreePool, 1, tcg_event); return EFI_SUCCESS; }
EFI_STATUS simple_file_read_all(EFI_FILE *file, UINTN *size, void **buffer) { EFI_STATUS efi_status; EFI_FILE_INFO *fi; char buf[1024]; *size = sizeof(buf); fi = (void *)buf; efi_status = uefi_call_wrapper(file->GetInfo, 4, file, &FILE_INFO, size, fi); if (efi_status != EFI_SUCCESS) { Print(L"Failed to get file info\n"); return efi_status; } *size = fi->FileSize; *buffer = AllocatePool(*size); if (!*buffer) { Print(L"Failed to allocate buffer of size %d\n", *size); return EFI_OUT_OF_RESOURCES; } efi_status = uefi_call_wrapper(file->Read, 3, file, size, *buffer); return efi_status; }
EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *systab) { EFI_STATUS rc; InitializeLib(image, systab); rc = uefi_call_wrapper(BS->HandleProtocol, 3, image, &LoadedImageProtocol, (void *)&this_image); if (EFI_ERROR(rc)) { Print(L"Error: could not find loaded image: %d\n", rc); return rc; } Print(L"System BootOrder not found. Initializing defaults.\n"); set_boot_order(); rc = find_boot_options(this_image->DeviceHandle); if (EFI_ERROR(rc)) { Print(L"Error: could not find boot options: %d\n", rc); return rc; } try_start_first_option(image); Print(L"Reset System\n"); uefi_call_wrapper(RT->ResetSystem, 4, EfiResetCold, EFI_SUCCESS, 0, NULL); return EFI_SUCCESS; }
EFI_STATUS execute(EFI_HANDLE image, CHAR16 *name) { EFI_STATUS status; EFI_HANDLE h; EFI_LOADED_IMAGE *li; EFI_DEVICE_PATH *devpath; CHAR16 *PathName; status = uefi_call_wrapper(BS->HandleProtocol, 3, image, &IMAGE_PROTOCOL, (void **)&li); if (status != EFI_SUCCESS) return status; status = generate_path(name, li, &devpath, &PathName); if (status != EFI_SUCCESS) return status; status = uefi_call_wrapper(BS->LoadImage, 6, FALSE, image, devpath, NULL, 0, &h); if (status != EFI_SUCCESS) goto out; status = uefi_call_wrapper(BS->StartImage, 3, h, NULL, NULL); uefi_call_wrapper(BS->UnloadImage, 1, h); out: FreePool(PathName); FreePool(devpath); return status; }
EFI_STATUS get_variable_attr(CHAR16 *var, UINT8 **data, UINTN *len, EFI_GUID owner, UINT32 *attributes) { EFI_STATUS efi_status; *len = 0; efi_status = uefi_call_wrapper(RT->GetVariable, 5, var, &owner, NULL, len, NULL); if (efi_status != EFI_BUFFER_TOO_SMALL) return efi_status; *data = AllocateZeroPool(*len); if (!*data) return EFI_OUT_OF_RESOURCES; efi_status = uefi_call_wrapper(RT->GetVariable, 5, var, &owner, attributes, len, *data); if (efi_status != EFI_SUCCESS) { FreePool(*data); *data = NULL; } return efi_status; }
static EFI_STATUS try_start_first_option(EFI_HANDLE parent_image_handle) { EFI_STATUS rc; EFI_HANDLE image_handle; if (!first_new_option) { return EFI_SUCCESS; } rc = uefi_call_wrapper(BS->LoadImage, 6, 0, parent_image_handle, first_new_option, NULL, 0, &image_handle); if (EFI_ERROR(rc)) { Print(L"LoadImage failed: %d\n", rc); uefi_call_wrapper(BS->Stall, 1, 2000000); return rc; } EFI_LOADED_IMAGE *image; rc = uefi_call_wrapper(BS->HandleProtocol, 3, image_handle, &LoadedImageProtocol, (void *)&image); if (!EFI_ERROR(rc)) { image->LoadOptions = first_new_option_args; image->LoadOptionsSize = first_new_option_size; } rc = uefi_call_wrapper(BS->StartImage, 3, image_handle, NULL, NULL); if (EFI_ERROR(rc)) { Print(L"StartImage failed: %d\n", rc); uefi_call_wrapper(BS->Stall, 1, 2000000); } return rc; }
/* * Allocate some raw pages that aren't part of the pool allocator. */ VOID * fwup_malloc_raw(UINTN size) { UINTN pages = size / 4096 + ((size % 4096) ? 1 : 0); /* page size is always 4096 */ EFI_STATUS rc; EFI_PHYSICAL_ADDRESS pageaddr = 0; EFI_ALLOCATE_TYPE type = AllocateAnyPages; if (sizeof(VOID *) == 4) { pageaddr = 0xffffffffULL - 8192; type = AllocateMaxAddress; } rc = uefi_call_wrapper(BS->AllocatePages, 4, type, EfiLoaderData, pages, &pageaddr); if (EFI_ERROR(rc)) { fwup_warning(L"Could not allocate %d", size); return NULL; } if (sizeof(VOID *) == 4 && pageaddr > 0xffffffffULL) { uefi_call_wrapper(BS->FreePages, 2, pageaddr, pages); fwup_warning(L"Got bad allocation at 0x%016x", (UINT64)pageaddr); return NULL; } return (VOID *)(UINTN)pageaddr; }
EFI_STATUS simple_file_open_by_handle(EFI_HANDLE device, CHAR16 *name, EFI_FILE **file, UINT64 mode) { EFI_STATUS efi_status; EFI_FILE_IO_INTERFACE *drive; EFI_FILE *root; efi_status = uefi_call_wrapper(BS->HandleProtocol, 3, device, &SIMPLE_FS_PROTOCOL, &drive); if (efi_status != EFI_SUCCESS) { Print(L"Unable to find simple file protocol (%d)\n", efi_status); goto error; } efi_status = uefi_call_wrapper(drive->OpenVolume, 2, drive, &root); if (efi_status != EFI_SUCCESS) { Print(L"Failed to open drive volume (%d)\n", efi_status); goto error; } efi_status = uefi_call_wrapper(root->Open, 5, root, file, name, mode, 0); error: return efi_status; }
EFI_STATUS update_boot_order(void) { UINTN size; UINTN len = 0; EFI_GUID global = EFI_GLOBAL_VARIABLE; CHAR16 *newbootorder = NULL; EFI_STATUS rc; size = nbootorder * sizeof(CHAR16); newbootorder = AllocateZeroPool(size); if (!newbootorder) return EFI_OUT_OF_RESOURCES; CopyMem(newbootorder, bootorder, size); #ifdef DEBUG_FALLBACK Print(L"nbootorder: %d\nBootOrder: ", size / sizeof (CHAR16)); UINTN j; for (j = 0 ; j < size / sizeof (CHAR16); j++) Print(L"%04x ", newbootorder[j]); Print(L"\n"); #endif rc = uefi_call_wrapper(RT->GetVariable, 5, L"BootOrder", &global, NULL, &len, NULL); if (rc == EFI_BUFFER_TOO_SMALL) LibDeleteVariable(L"BootOrder", &global); rc = uefi_call_wrapper(RT->SetVariable, 5, L"BootOrder", &global, EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS, size, newbootorder); FreePool(newbootorder); return rc; }
EFI_STATUS simple_file_read_all(EFI_FILE *file, UINTN *size, void **buffer) { EFI_STATUS efi_status; EFI_FILE_INFO *fi; char buf[1024]; *size = sizeof(buf); fi = (void *)buf; efi_status = uefi_call_wrapper(file->GetInfo, 4, file, &FILE_INFO, size, fi); if (efi_status != EFI_SUCCESS) { Print(L"Failed to get file info\n"); return efi_status; } *size = fi->FileSize; /* might use memory mapped, so align up to nearest page */ *buffer = AllocateZeroPool(ALIGN_VALUE(*size, 4096)); if (!*buffer) { Print(L"Failed to allocate buffer of size %d\n", *size); return EFI_OUT_OF_RESOURCES; } efi_status = uefi_call_wrapper(file->Read, 3, file, size, *buffer); return efi_status; }
static EFI_STATUS apply_capsules(EFI_CAPSULE_HEADER **capsules, EFI_CAPSULE_BLOCK_DESCRIPTOR *cbd, UINTN num_updates, EFI_RESET_TYPE *reset) { UINT64 max_capsule_size; EFI_STATUS rc; rc = uefi_call_wrapper(RT->QueryCapsuleCapabilities, 4, capsules, num_updates, &max_capsule_size, reset); if (debugging) { Print(L"QueryCapsuleCapabilities: %r max: %ld reset:%d\n", rc, max_capsule_size, *reset); Print(L"Capsules: %d\n", num_updates); } uefi_call_wrapper(BS->Stall, 1, 1000000); rc = uefi_call_wrapper(RT->UpdateCapsule, 3, capsules, num_updates, (EFI_PHYSICAL_ADDRESS)(VOID *)cbd); if (EFI_ERROR(rc)) { Print(L"%a:%a():%d: Could not apply capsule update: %r\n", __FILE__, __func__, __LINE__, rc); return rc; } return EFI_SUCCESS; }
EFI_STATUS uefi_get_file_size(EFI_FILE_IO_INTERFACE *io, CHAR16 *filename, UINTN *size) { EFI_STATUS ret; EFI_FILE_INFO *info; UINTN info_size; EFI_FILE *file; ret = uefi_open_file(io, filename, &file); if (EFI_ERROR(ret)) goto out; info_size = SIZE_OF_EFI_FILE_INFO + FILENAME_MAX_LENGTH; info = AllocatePool(info_size); if (!info) { ret = EFI_OUT_OF_RESOURCES; goto close; } ret = uefi_call_wrapper(file->GetInfo, 4, file, &GenericFileInfo, &info_size, info); if (EFI_ERROR(ret)) goto free_info; *size = info->FileSize; free_info: FreePool(info); close: uefi_call_wrapper(file->Close, 1, file); out: if (EFI_ERROR(ret)) efi_perror(ret, L"Failed to read file %s", filename); return ret; }
EFI_STATUS memory_map(EFI_MEMORY_DESCRIPTOR **map_buf, UINTN *map_size, UINTN *map_key, UINTN *desc_size, UINT32 *desc_version) { EFI_STATUS err = EFI_SUCCESS; *map_size = sizeof(**map_buf) * 31; get_map: *map_size += sizeof(**map_buf); err = uefi_call_wrapper(BS->AllocatePool, 3, EfiLoaderData, *map_size, (void **)map_buf); if (err != EFI_SUCCESS) { Print(L"ERROR: Failed to allocate pool for memory map"); return err; } err = uefi_call_wrapper(BS->GetMemoryMap, 5, map_size, *map_buf, map_key, desc_size, desc_version); if (err != EFI_SUCCESS) { if (err == EFI_BUFFER_TOO_SMALL) { uefi_call_wrapper(BS->FreePool, 1, (void *)*map_buf); goto get_map; } Print(L"ERROR: Failed to get memory map"); } return err; }
; ןונגוירא� ןנמדנאללא הכ� UEFI EFI_STATUS efi_main (EFI_HANDLE image, EFI_SYSTEM_TABLE *systab) { InitializeLib(image, systab); UINTN mms=65536; int i; int s; EFI_MEMORY_DESCRIPTOR *mm; uefi_call_wrapper(systab->BootServices->AllocatePool, 3, EfiLoaderData, sizeof(EFI_MEMORY_DESCRIPTOR)*65536, ((void*)&mm)); UINTN mk,ds,dv; uefi_call_wrapper(systab->BootServices->GetMemoryMap, 5, &mms, mm, &mk, &ds, &dv); s=0; for (i=0;i<mms;i++) if((mm[i].Type == EfiBootServicesCode) ||(mm[i].Type == EfiBootServicesData) ||(mm[i].Type == EfiConventionalMemory) ||(mm[i].Type == EfiACPIReclaimMemory) )s+=mm[i].NumberOfPages; Print(L"%d\r\n",s); return EFI_SUCCESS; }
static EFI_STATUS get_file_size(EFI_FILE_HANDLE fh, UINTN *retsize) { EFI_STATUS rc; void *buffer = NULL; UINTN bs = 0; EFI_GUID finfo = EFI_FILE_INFO_ID; /* The API here is "Call it once with bs=0, it fills in bs, * then allocate a buffer and ask again to get it filled. */ rc = uefi_call_wrapper(fh->GetInfo, 4, fh, &finfo, &bs, NULL); if (rc == EFI_BUFFER_TOO_SMALL) { buffer = AllocateZeroPool(bs); if (!buffer) { Print(L"Could not allocate memory\n"); return EFI_OUT_OF_RESOURCES; } rc = uefi_call_wrapper(fh->GetInfo, 4, fh, &finfo, &bs, buffer); } /* This checks *either* the error from the first GetInfo, if it isn't * the EFI_BUFFER_TOO_SMALL we're expecting, or the second GetInfo call * in *any* case. */ if (EFI_ERROR(rc)) { Print(L"Could not get file info: %d\n", rc); if (buffer) FreePool(buffer); return rc; } EFI_FILE_INFO *fi = buffer; *retsize = fi->FileSize; FreePool(buffer); return EFI_SUCCESS; }
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; }
EFI_STATUS WaitForSingleEvent ( IN EFI_EVENT Event, IN UINT64 Timeout OPTIONAL ) { EFI_STATUS Status; UINTN Index; EFI_EVENT TimerEvent; EFI_EVENT WaitList[2]; if (Timeout) { // // Create a timer event // Status = uefi_call_wrapper(BS->CreateEvent, 5, EVT_TIMER, 0, NULL, NULL, &TimerEvent); if (!EFI_ERROR(Status)) { // // Set the timer event // uefi_call_wrapper(BS->SetTimer, 3, TimerEvent, TimerRelative, Timeout); // // Wait for the original event or the timer // WaitList[0] = Event; WaitList[1] = TimerEvent; Status = uefi_call_wrapper(BS->WaitForEvent, 3, 2, WaitList, &Index); uefi_call_wrapper(BS->CloseEvent, 1, TimerEvent); // // If the timer expired, change the return to timed out // if (!EFI_ERROR(Status) && Index == 1) { Status = EFI_TIMEOUT; } } } else { // // No timeout... just wait on the event // Status = uefi_call_wrapper(BS->WaitForEvent, 3, 1, &Event, &Index); ASSERT (!EFI_ERROR(Status)); ASSERT (Index == 0); } return Status; }
EFI_expressionUS efi_main(EFI_HANDLE image_handle, EFI_SYSTEM_TABLE *systab ) { InitializeLib(image_handle, systab); EFI_expressionUS err; UINTN memorymapsize, mapkey, descriptorsize;; UINT32 descriptorversion; EFI_MEMORY_DESCRIPTOR *memorymap; int i,z; long long int memtouse=0; err=uefi_call_wrapper(systab->BootServices->GetMemoryMap,5,&memorymapsize, memorymap, &mapkey, &descriptorsize, &descriptorversion); if(err!=EFI_BUFFER_TOO_SMALL) { Print(L"Error in getting memory map\n"); return EFI_SUCCESS; } err=uefi_call_wrapper(systab->BootServices->AllocatePool,3, EfiLoaderData,memorymapsize, ((void*)&memorymap)); if(err!=EFI_SUCCESS) { Print(L"Error in allocating memory\n"); return EFI_SUCCESS; } err=uefi_call_wrapper(systab->BootServices->GetMemoryMap, 5, &memorymapsize, memorymap, &mapkey, &descriptorsize, &descriptorversion); if(err!=EFI_SUCCESS) { Print(L"Error in getting memory map\n"); return EFI_SUCCESS; } z=memorymapsize/(sizeof(EFI_MEMORY_DESCRIPTOR)); for(i=0;i<z;i++){ if((memorymap[i].Type==EfiBootServicesCode)|| (memorymap[i].Type==EfiBootServicesData) ||(memorymap[i].Type==EfiConventionalMemory)) {memtouse+=memorymap[i].NumberOfPages;} } memtouse=memtouse*4096; err=uefi_call_wrapper(systab->BootServices->FreePool, 1, ((void*)memorymap)); if(err!=EFI_SUCCESS) { Print(L"Error in free pool fuction\n"); return err; } Print(L"%d bytes of memory is avaliable for general use\n",memtouse); return EFI_SUCCESS; }
static void print_modes(EFI_GRAPHICS_OUTPUT_PROTOCOL *gop) { int i, imax; EFI_STATUS rc; imax = gop->Mode->MaxMode; Print(L"GOP reports MaxMode %d\n", imax); for (i = 0; i < imax; i++) { EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *info; UINTN SizeOfInfo; rc = uefi_call_wrapper(gop->QueryMode, 4, gop, i, &SizeOfInfo, &info); if (EFI_ERROR(rc) && rc == EFI_NOT_STARTED) { rc = uefi_call_wrapper(gop->SetMode, 2, gop, gop->Mode->Mode); rc = uefi_call_wrapper(gop->QueryMode, 4, gop, i, &SizeOfInfo, &info); } if (EFI_ERROR(rc)) { CHAR16 Buffer[64]; StatusToString(Buffer, rc); Print(L"%d: Bad response from QueryMode: %s (%d)\n", i, Buffer, rc); continue; } Print(L"%c%d: %dx%d ", memcmp(info,gop->Mode->Info,sizeof(*info)) == 0 ? '*' : ' ', i, info->HorizontalResolution, info->VerticalResolution); switch(info->PixelFormat) { case PixelRedGreenBlueReserved8BitPerColor: Print(L"RGBR"); break; case PixelBlueGreenRedReserved8BitPerColor: Print(L"BGRR"); break; case PixelBitMask: Print(L"R:%08x G:%08x B:%08x X:%08x", info->PixelInformation.RedMask, info->PixelInformation.GreenMask, info->PixelInformation.BlueMask, info->PixelInformation.ReservedMask); break; case PixelBltOnly: Print(L"(blt only)"); break; default: Print(L"(Invalid pixel format)"); break; } Print(L" pitch %d\n", info->PixelsPerScanLine); } }
EFI_INPUT_KEY console_get_keystroke(void) { EFI_INPUT_KEY key; UINTN EventIndex; uefi_call_wrapper(BS->WaitForEvent, 3, 1, &ST->ConIn->WaitForKey, &EventIndex); uefi_call_wrapper(ST->ConIn->ReadKeyStroke, 2, ST->ConIn, &key); return key; }
EFI_STATUS efi_main (EFI_HANDLE *image, EFI_SYSTEM_TABLE *systab) { UINTN index; InitializeLib(image, systab); uefi_call_wrapper(systab->ConOut->OutputString, 2, systab->ConOut, L"Hello application started\r\n"); uefi_call_wrapper(systab->ConOut->OutputString, 2, systab->ConOut, L"\r\n\r\n\r\nHit any key to exit\r\n"); uefi_call_wrapper(systab->BootServices->WaitForEvent, 3, 1, &systab->ConIn->WaitForKey, &index); return EFI_SUCCESS; }
INTN GetShellArgcArgv(EFI_HANDLE ImageHandle, CHAR16 **Argv[]) { // Code inspired from EDK2's // ShellPkg/Library/UefiShellCEntryLib/UefiShellCEntryLib.c (BSD) EFI_STATUS Status; // @@@ // static const EFI_GUID EfiShellParametersProtocolGuid // = EFI_SHELL_PARAMETERS_PROTOCOL_GUID; // static const EFI_GUID ShellInterfaceProtocolGuid // = SHELL_INTERFACE_PROTOCOL_GUID; static EFI_GUID EfiShellParametersProtocolGuid = EFI_SHELL_PARAMETERS_PROTOCOL_GUID; static EFI_GUID ShellInterfaceProtocolGuid = SHELL_INTERFACE_PROTOCOL_GUID; // @@@ EFI_SHELL_PARAMETERS_PROTOCOL *EfiShellParametersProtocol = NULL; EFI_SHELL_INTERFACE *EfiShellInterfaceProtocol = NULL; Status = uefi_call_wrapper(BS->OpenProtocol, 6, ImageHandle, &EfiShellParametersProtocolGuid, (VOID **)&EfiShellParametersProtocol, ImageHandle, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL ); if (!EFI_ERROR(Status)) { // use shell 2.0 interface // Print(L"Got argc/argv from shell intf proto\n"); *Argv = EfiShellParametersProtocol->Argv; return EfiShellParametersProtocol->Argc; } // try to get shell 1.0 interface instead. Status = uefi_call_wrapper(BS->OpenProtocol, 6, ImageHandle, &ShellInterfaceProtocolGuid, (VOID **)&EfiShellInterfaceProtocol, ImageHandle, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL ); if (!EFI_ERROR(Status)) { // Print(L"Got argc/argv from shell params proto\n"); *Argv = EfiShellInterfaceProtocol->Argv; return EfiShellInterfaceProtocol->Argc; } // shell 1.0 and 2.0 interfaces failed return GetShellArgcArgvFromLoadedImage(ImageHandle, Argv); }
static EFI_STATUS change_mode(UINTN mode) { EFI_STATUS err; err = uefi_call_wrapper(ST->ConOut->SetMode, 2, ST->ConOut, mode); /* Special case mode 1: when using OVMF and qemu, setting it returns error * and breaks console output. */ if (EFI_ERROR(err) && mode == 1) uefi_call_wrapper(ST->ConOut->SetMode, 2, ST->ConOut, (UINTN)0); return err; }
EFI_STATUS uefi_write_file_with_dir(EFI_FILE_IO_INTERFACE *io, CHAR16 *filename, void *data, UINTN size) { EFI_STATUS ret; EFI_FILE *dirs[MAX_SUBDIR]; EFI_FILE *file; CHAR16 *start; CHAR16 *end; INTN subdir = 0; ret = uefi_call_wrapper(io->OpenVolume, 2, io, &dirs[0]); if (EFI_ERROR(ret)) { efi_perror(ret, L"Failed to open root directory"); return ret; } start = filename; for (end = filename; *end; end++) { if (*end != '/') continue; if (start == end) { start++; continue; } *end = 0; debug(L"create directory %s", start); ret = uefi_create_dir(dirs[subdir], &dirs[subdir + 1], start); *end = '/'; if (EFI_ERROR(ret)) goto out; subdir++; if (subdir >= MAX_SUBDIR - 1) { error(L"too many subdirectories, limit is %d", MAX_SUBDIR); ret = EFI_INVALID_PARAMETER; goto out; } start = end + 1; } debug(L"write file %s", start); ret = uefi_call_wrapper(dirs[subdir]->Open, 5, dirs[subdir], &file, start, EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE, 0); if (EFI_ERROR(ret)) goto out; ret = uefi_call_wrapper(file->Write, 3, file, &size, data); uefi_call_wrapper(file->Close, 1, file); out: for (; subdir >= 0; subdir--) uefi_call_wrapper(dirs[subdir]->Close, 1, dirs[subdir]); if (EFI_ERROR(ret)) efi_perror(ret, L"Failed to write file %s", filename); return ret; }
EFI_STATUS simple_dir_read_all_by_handle(EFI_HANDLE image, EFI_FILE *file, CHAR16* name, EFI_FILE_INFO **entries, int *count) { EFI_STATUS status; char buf[4096]; UINTN size = sizeof(buf); EFI_FILE_INFO *fi = (void *)buf; status = uefi_call_wrapper(file->GetInfo, 4, file, &FILE_INFO, &size, fi); if (status != EFI_SUCCESS) { Print(L"Failed to get file info\n"); goto out; } if ((fi->Attribute & EFI_FILE_DIRECTORY) == 0) { Print(L"Not a directory %s\n", name); status = EFI_INVALID_PARAMETER; goto out; } size = 0; *count = 0; for (;;) { UINTN len = sizeof(buf); status = uefi_call_wrapper(file->Read, 3, file, &len, buf); if (status != EFI_SUCCESS || len == 0) break; (*count)++; size += len; } uefi_call_wrapper(file->SetPosition, 2, file, 0); char *ptr = AllocatePool(size); *entries = (EFI_FILE_INFO *)ptr; if (!*entries) return EFI_OUT_OF_RESOURCES; int i; for (i = 0; i < *count; i++) { UINTN len = size; uefi_call_wrapper(file->Read, 3, file, &len, ptr); ptr += len; size -= len; } status = EFI_SUCCESS; out: simple_file_close(file); if (status != EFI_SUCCESS && *entries) { FreePool(*entries); *entries = NULL; } return status; }
EFI_STATUS efi_main(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable) { EFI_MEMORY_DESCRIPTOR *descriptors; EFI_STATUS status; UINTN neededMemory = 0, actualSize, descriptors_size; UINT64 totalMemory = 0; InitializeLib(ImageHandle, SystemTable); status = uefi_call_wrapper(SystemTable->BootServices->GetMemoryMap, 5, &neededMemory, NULL, NULL, NULL, NULL); if ( status != EFI_BUFFER_TOO_SMALL ) { Print(L"Something going wrong\n"); return EFI_SUCCESS; } status = uefi_call_wrapper(SystemTable->BootServices->AllocatePool, 3, EfiLoaderData, neededMemory, &descriptors); if (status != EFI_SUCCESS) { Print (L"AllocatePool failed\n"); return EFI_SUCCESS; } status = uefi_call_wrapper( SystemTable->BootServices->GetMemoryMap, 5, &descriptors_size, descriptors, NULL, &actualSize, NULL); if ( status == EFI_SUCCESS ) { UINTN count = descriptors_size / actualSize; UINTN i; for ( i = 0; i < count; ++i ) { if ( descriptors[i].Type == EfiLoaderData || descriptors[i].Type == EfiBootServicesData || descriptors[i].Type == EfiRuntimeServicesData || descriptors[i].Type == EfiConventionalMemory) { totalMemory += descriptors[i].NumberOfPages * 4096; } } Print(L"%d\n", totalMemory); } uefi_call_wrapper(SystemTable->BootServices->FreePool, 1, descriptors); return EFI_SUCCESS; }
/* Open the tos partition and load the tos image into memory * Parameters: * label - Label for the partition in the GPT * image - the image pointer after loading from the GPT * Return values: * EFI_SUCCESS - image is loaded * EFI_ACCESS_DENIED - Error in image loading * EFI_INVALID_PARAMETER - wrong image size * EFI_OUT_OF_RESOURCES - Out of memory */ static EFI_STATUS tos_image_load_partition(IN const CHAR16 *label, OUT VOID **image) { UINT32 MediaId; UINT32 img_size; EFI_STATUS ret; struct gpt_partition_interface gpart; UINT64 partition_start; UINT64 partition_size; VOID *bootimg; struct boot_img_hdr aosp_header; ret = gpt_get_partition_by_label(label, &gpart, LOGICAL_UNIT_USER); if (EFI_ERROR(ret)) { efi_perror(ret, L"Partition %s not found", label); return ret; } MediaId = gpart.bio->Media->MediaId; partition_start = gpart.part.starting_lba * gpart.bio->Media->BlockSize; partition_size = (gpart.part.ending_lba + 1 - gpart.part.starting_lba) * gpart.bio->Media->BlockSize; debug(L"Reading TOS image header"); ret = uefi_call_wrapper(gpart.dio->ReadDisk, 5, gpart.dio, MediaId, partition_start, sizeof(aosp_header), &aosp_header); if (EFI_ERROR(ret)) { efi_perror(ret, L"ReadDisk (aosp_header)"); return ret; } img_size = bootimage_size(&aosp_header) + BOOT_SIGNATURE_MAX_SIZE; if (img_size > partition_size) { error(L"TOS image is larger than partition size"); return EFI_INVALID_PARAMETER; } bootimg = AllocatePool(img_size); if (!bootimg) { error(L"Alloc memory for TOS image failed"); return EFI_OUT_OF_RESOURCES; } debug(L"Reading Tos image: %d bytes", img_size); ret = uefi_call_wrapper(gpart.dio->ReadDisk, 5, gpart.dio, MediaId, partition_start, img_size, bootimg); if (EFI_ERROR(ret)) { efi_perror(ret, L"ReadDisk Error for TOS image read"); FreePool(bootimg); return ret; } *image = bootimg; return EFI_SUCCESS; }
EFI_EVENT LibCreateProtocolNotifyEvent ( IN EFI_GUID *ProtocolGuid, IN EFI_TPL NotifyTpl, IN EFI_EVENT_NOTIFY NotifyFunction, IN VOID *NotifyContext, OUT VOID *Registration ) { EFI_STATUS Status; EFI_EVENT Event; // // Create the event // Status = uefi_call_wrapper( BS->CreateEvent, 5, EVT_NOTIFY_SIGNAL, NotifyTpl, NotifyFunction, NotifyContext, &Event ); if ( EFI_ERROR( Status ) ) return NULL ; ASSERT (!EFI_ERROR(Status)); // // Register for protocol notifactions on this event // Status = uefi_call_wrapper( BS->RegisterProtocolNotify, 3, ProtocolGuid, Event, Registration ); if ( EFI_ERROR( Status ) ) return NULL ; ASSERT (!EFI_ERROR(Status)); // // Kick the event so we will perform an initial pass of // current installed drivers // uefi_call_wrapper(BS->SignalEvent, 1, Event); return Event; }
EFI_STATUS uefi_open_file(EFI_FILE_IO_INTERFACE *io, CHAR16 *filename, EFI_FILE **file) { EFI_STATUS ret; ret = uefi_call_wrapper(io->OpenVolume, 2, io, file); if (EFI_ERROR(ret)) return ret; ret = uefi_call_wrapper((*file)->Open, 5, *file, file, filename, EFI_FILE_MODE_READ, 0); if (EFI_ERROR(ret)) return ret; return EFI_SUCCESS; }
BOOLEAN uefi_exist_file(EFI_FILE *parent, CHAR16 *filename) { EFI_STATUS ret; EFI_FILE *file; ret = uefi_call_wrapper(parent->Open, 5, parent, &file, filename, EFI_FILE_MODE_READ, 0); if (!EFI_ERROR(ret)) uefi_call_wrapper(file->Close, 1, file); else if (ret != EFI_NOT_FOUND) // IO error efi_perror(ret, L"Failed to found file %s", filename); return ret == EFI_SUCCESS; }