annotate bootloader.c @ 0:f434d6ab8db1

xv6-UEFI bootloader
author nana
date Tue, 11 Apr 2023 11:12:12 +0900
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
1 #include<efi.h>
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
2 #include<efilib.h>
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
3
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
4
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
5 EFI_STATUS
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
6 efi_main(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable)
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
7 {
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
8
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
9 // UINTN UartBase = 0x09000000;
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
10 // UINTN UartClk = 24000000;
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
11 //CHAR8 buffer[256];
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
12 //UINTN bufferSize;
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
13
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
14 EFI_DEVICE_PATH *Path;
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
15 EFI_LOADED_IMAGE *LoadedImageParent;
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
16 EFI_LOADED_IMAGE *LoadedImage;
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
17 EFI_HANDLE Image;
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
18 CHAR16 *Options = L"root=/dev/sda2 rootfstype=btrfs rw quiet splash";
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
19 EFI_STATUS Status=EFI_SUCCESS;
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
20
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
21 InitializeLib(ImageHandle, SystemTable);
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
22 Print(L"Hello, EFI!\n");
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
23
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
24 // Status = InitializeUart(UartBase, UartClk);
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
25 // if (EFI_ERROR(Status)) {
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
26 // Print(L"Error initializing UART device: %r\n", Status);
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
27 // return Status;
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
28 // }
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
29 // Print(L"Hello, UART1!\n");
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
30
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
31 // Read kernel image from UART
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
32 // bufferSize = sizeof(buffer);
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
33 // Status = ReadUart(UartBase, &bufferSize, buffer);
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
34 // if (EFI_ERROR(Status)) {
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
35 // Print(L"Error reading kernel image from UART: %r\n", Status);
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
36 // return Status;
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
37 // }
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
38
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
39 // Print(L"Hello, UART2!\n");
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
40
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
41 // Status = uefi_call_wrapper(BS->OpenProtocol, 6, ImageHandle, &LoadedImageProtocol,(void**)&LoadedImageParent, ImageHandle, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
42 // if (EFI_ERROR(Status)) {
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
43 // Print(L"Could not get LoadedImageProtocol handler %r\n", Status);
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
44 // return Status;
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
45 // }
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
46
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
47 // Check the validity of the received data
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
48 // if (!validate_image(buffer, bufferSize)) {
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
49 // Print(L"Invalid kernel image received\n");
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
50 // return EFI_INVALID_PARAMETER;
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
51 // }
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
52
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
53
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
54 Print(L"Hello,2!\n");
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
55 Path = FileDevicePath(LoadedImageParent->DeviceHandle, L"\\kernel.elf");
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
56 if (Path == NULL) {
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
57 Print(L"Could not get device path.");
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
58 return EFI_INVALID_PARAMETER;
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
59 }
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
60 Print(L"Hello,3!\n");
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
61 Status = uefi_call_wrapper(BS->LoadImage, 6, FALSE, ImageHandle, Path, NULL, 0, &Image);
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
62 if (EFI_ERROR(Status)) {
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
63 Print(L"Could not load %r", Status);
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
64 FreePool(Path);
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
65 return Status;
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
66 }
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
67 Print(L"Hello,4!\n");
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
68 Status = uefi_call_wrapper(BS->OpenProtocol, 6, Image, &LoadedImageProtocol, (void**)&LoadedImage, ImageHandle, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
69 if (EFI_ERROR(Status)) {
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
70 Print(L"Could not get LoadedImageProtocol handler %r\n", Status);
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
71 uefi_call_wrapper(BS->UnloadImage, 1, Image);
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
72 FreePool(Path);
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
73 return Status;
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
74 }
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
75 Print(L"Hello,5!\n");
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
76 LoadedImage->LoadOptions = Options;
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
77 LoadedImage->LoadOptionsSize = (StrLen(LoadedImage->LoadOptions)+1) * sizeof(CHAR16);
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
78
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
79 Print(L"Hello,6!\n");
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
80 Status = uefi_call_wrapper(BS->StartImage, 3, Image, NULL, NULL);
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
81 uefi_call_wrapper(BS->UnloadImage, 1, Image);
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
82 FreePool(Path);
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
83 Print(L"Hello,7!\n");
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
84
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
85 return EFI_SUCCESS;
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
86 }
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
87