|
1 | | -#include <efibind.h> |
2 | | -#include <efidef.h> |
3 | | -#include <efidevp.h> |
4 | | -#include <eficon.h> |
5 | | -#include <efiprot.h> |
6 | | -#include <efiapi.h> |
7 | | -#include <efierr.h> |
8 | | - |
9 | | -// Define the GUID for EFI_LOADED_IMAGE_PROTOCOL |
10 | | -static EFI_GUID LoadedImageProtocol = EFI_LOADED_IMAGE_PROTOCOL_GUID; |
11 | | - |
12 | | -#define DEFAULT_STALL_TIME_MS 1000 |
13 | | - |
14 | | -EFI_STATUS efi_main(EFI_HANDLE image_handle, EFI_SYSTEM_TABLE *system_table) { |
15 | | - EFI_STATUS status; |
16 | | - EFI_LOADED_IMAGE *loaded_image; |
17 | | - EFI_BOOT_SERVICES *boot_services = system_table->BootServices; |
18 | | - EFI_SIMPLE_TEXT_OUT_PROTOCOL *console_output = system_table->ConOut; |
19 | | - |
20 | | - // Call HandleProtocol directly for EFI 1.1 to retrieve the loaded image protocol |
21 | | - status = boot_services->HandleProtocol(image_handle, &LoadedImageProtocol, (void **)&loaded_image); |
22 | | - if (EFI_ERROR(status)) { |
23 | | - // Create a simple error message |
24 | | - CHAR16 error_message[] = L"Failed to get loaded image protocol. Halting.\r\n"; |
25 | | - console_output->OutputString(console_output, error_message); |
26 | | - |
27 | | - boot_services->Stall(DEFAULT_STALL_TIME_MS * 1000); |
28 | | - return status; |
29 | | - } |
30 | | - |
31 | | - // Check if there are command-line parameters |
32 | | - if (loaded_image->LoadOptionsSize > 0 && loaded_image->LoadOptions != NULL) { |
33 | | - CHAR16 *cmdline = (CHAR16 *)loaded_image->LoadOptions; |
34 | | - |
35 | | - // Output the command line arguments |
36 | | - CHAR16 cmdline_message[] = L"Received command line arguments:\r\n"; |
37 | | - console_output->OutputString(console_output, cmdline_message); |
38 | | - console_output->OutputString(console_output, cmdline); // Print the command-line options |
39 | | - console_output->OutputString(console_output, L"\r\n"); |
40 | | - } else { |
41 | | - CHAR16 no_params_message[] = L"No command line arguments received.\r\n"; |
42 | | - console_output->OutputString(console_output, no_params_message); |
| 1 | +#include <efi.h> |
| 2 | +#include <efilib.h> |
| 3 | + |
| 4 | + |
| 5 | +EFI_STATUS ParseCommandLine(CHAR16 *command_line, EFI_PHYSICAL_ADDRESS *Addr, EFI_PHYSICAL_ADDRESS *EndAddr, UINTN *Stalltime) { |
| 6 | + // Ensure command_line is not null |
| 7 | + if (command_line == NULL) { |
| 8 | + Print(L"No command line arguments provided.\n"); |
| 9 | + return EFI_INVALID_PARAMETER; |
43 | 10 | } |
44 | 11 |
|
45 | | - // Success message (optional) |
46 | | - CHAR16 success_message[] = L"Loaded image protocol retrieved successfully.\r\n"; |
47 | | - console_output->OutputString(console_output, success_message); |
| 12 | + Print(L"Command line: %s\n", command_line); |
| 13 | + |
| 14 | + // Parse the first address (Hex) |
| 15 | + *Addr = 0; |
| 16 | + while (*command_line && *command_line != ' ') { |
| 17 | + if (*command_line >= '0' && *command_line <= '9') { |
| 18 | + *Addr = (*Addr << 4) | (*command_line - '0'); |
| 19 | + } else if (*command_line >= 'a' && *command_line <= 'f') { |
| 20 | + *Addr = (*Addr << 4) | (*command_line - 'a' + 10); |
| 21 | + } else if (*command_line >= 'A' && *command_line <= 'F') { |
| 22 | + *Addr = (*Addr << 4) | (*command_line - 'A' + 10); |
| 23 | + } |
| 24 | + command_line++; |
| 25 | + } |
| 26 | + |
| 27 | + while (*command_line == ' ') command_line++; // Skip spaces |
| 28 | + |
| 29 | + // Parse the second address (Hex) |
| 30 | + *EndAddr = 0; |
| 31 | + while (*command_line && *command_line != ' ') { |
| 32 | + if (*command_line >= '0' && *command_line <= '9') { |
| 33 | + *EndAddr = (*EndAddr << 4) | (*command_line - '0'); |
| 34 | + } else if (*command_line >= 'a' && *command_line <= 'f') { |
| 35 | + *EndAddr = (*EndAddr << 4) | (*command_line - 'a' + 10); |
| 36 | + } else if (*command_line >= 'A' && *command_line <= 'F') { |
| 37 | + *EndAddr = (*EndAddr << 4) | (*command_line - 'A' + 10); |
| 38 | + } |
| 39 | + command_line++; |
| 40 | + } |
| 41 | + |
| 42 | + while (*command_line == ' ') command_line++; // Skip spaces |
| 43 | + |
| 44 | + // Parse the stall time (Decimal) |
| 45 | + *Stalltime = 0; |
| 46 | + while (*command_line && *command_line >= '0' && *command_line <= '9') { |
| 47 | + *Stalltime = (*Stalltime * 10) + (*command_line - '0'); |
| 48 | + command_line++; |
| 49 | + } |
48 | 50 |
|
49 | | - boot_services->Stall(DEFAULT_STALL_TIME_MS * 1000); |
50 | | - |
51 | 51 | return EFI_SUCCESS; |
52 | 52 | } |
53 | 53 |
|
| 54 | +EFI_STATUS |
| 55 | +efi_main (EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable) { |
| 56 | + EFI_STATUS Status; |
| 57 | + EFI_LOADED_IMAGE *loaded_image; |
| 58 | + EFI_PHYSICAL_ADDRESS Addr, EndAddr; |
| 59 | + UINTN Stalltime; |
| 60 | + |
| 61 | + Status = uefi_call_wrapper(BS->HandleProtocol, 3, ImageHandle, &LoadedImageProtocol, (void **)&loaded_image); |
| 62 | + if (EFI_ERROR(Status)) { |
| 63 | + Print(L"Error HandleProtocol: %r\n", Status); |
| 64 | + uefi_call_wrapper(BS->Stall, 1, 5000000); |
| 65 | + return Status; |
| 66 | + } |
| 67 | + |
| 68 | + CHAR16 *command_line = (CHAR16 *)loaded_image->LoadOptions; |
| 69 | + if (command_line == NULL) { |
| 70 | + Print(L"No command line arguments provided.\n"); |
| 71 | + return EFI_INVALID_PARAMETER; |
| 72 | + } |
| 73 | + |
| 74 | + Status = ParseCommandLine(command_line, &Addr, &EndAddr, &Stalltime); |
| 75 | + if (EFI_ERROR(Status)) { |
| 76 | + Print(L"Failed to parse command line: %r\n", Status); |
| 77 | + return Status; |
| 78 | + } |
| 79 | + |
| 80 | + Print(L"Parsed values - Addr: 0x%lx, EndAddr: 0x%lx, Stalltime: %lu ms\n", Addr, EndAddr, Stalltime); |
| 81 | + |
| 82 | + Print(L"Disable RAM Area...!\n"); |
| 83 | + |
| 84 | + // Align the starting address (Addr) to the previous page boundary |
| 85 | + Addr = Addr & ~(EFI_PAGE_SIZE - 1); |
| 86 | + |
| 87 | + // Calculate the number of pages between the aligned Addr and EndAddr |
| 88 | + UINTN NumPages = ((EndAddr - Addr) + EFI_PAGE_SIZE - 1) / EFI_PAGE_SIZE; |
| 89 | + |
| 90 | + Print(L"Disable from 0x%lx to 0x%lx, which is %lu pages\n\n", Addr, EndAddr, NumPages); |
| 91 | + |
| 92 | + Status = uefi_call_wrapper(BS->AllocatePages, 4, 2, 8, NumPages, &Addr); |
| 93 | + |
| 94 | + if (EFI_ERROR(Status)) { |
| 95 | + Print(L"Error!\n"); |
| 96 | + Print(L"AllocatePages failed: %r\n", Status); |
| 97 | + uefi_call_wrapper(BS->Stall, 1, Stalltime * 1000); |
| 98 | + return Status; // Return the error status |
| 99 | + } else { |
| 100 | + Print(L"Success!\n"); |
| 101 | + Print(L"Memory allocated at address: 0x%lx for %lu pages\n", Addr, NumPages); |
| 102 | + } |
| 103 | + |
| 104 | + uefi_call_wrapper(BS->Stall, 1, Stalltime * 1000); |
| 105 | + |
| 106 | + Exit(EFI_SUCCESS, 0,0); |
| 107 | + return EFI_SUCCESS; |
| 108 | +} |
0 commit comments