SimpleKernel  0.0.1
boot.cpp File Reference

boot cpp More...

#include <exception>
#include <stdexcept>
#include "load_elf.h"
#include "ostream.hpp"
#include "project_config.h"
Include dependency graph for boot.cpp:

Go to the source code of this file.

Functions

EFI_STATUS EFIAPI efi_main (EFI_HANDLE _image_handle, [[maybe_unused]] EFI_SYSTEM_TABLE *_system_table)
 

Variables

uintptr_t ImageBase = 0
 

Detailed Description

boot cpp

Author
Zone.N (Zone..nosp@m.Niuz.nosp@m.h@hot.nosp@m.mail.nosp@m..com)
Version
1.0
Date
2023-07-15
change log:
DateAuthorDescription
2023-07-15Zone.N (Zone..nosp@m.Niuz.nosp@m.h@hot.nosp@m.mail.nosp@m..com)创建文件

Definition in file boot.cpp.

Function Documentation

◆ efi_main()

EFI_STATUS EFIAPI efi_main ( EFI_HANDLE  _image_handle,
[[maybe_unused] ] EFI_SYSTEM_TABLE *  _system_table 
)

Definition at line 27 of file boot.cpp.

28  {
29  EFI_STATUS status = EFI_SUCCESS;
30  uint64_t kernel_addr = 0;
31  try {
32  // 输出 efi 信息
33  EFI_LOADED_IMAGE *loaded_image = nullptr;
34  status = LibLocateProtocol(&LoadedImageProtocol,
35  reinterpret_cast<void **>(&loaded_image));
36  if (EFI_ERROR(status)) {
37  debug << L"LibLocateProtocol: " << status << ostream::endl;
38  }
39 
40  debug << L"Revision: " << ostream::hex_X << loaded_image->Revision
41  << ostream::endl;
42  debug << L"ParentHandle: " << ostream::hex_X
43  << loaded_image->ParentHandle << ostream::endl;
44  debug << L"SystemTable: " << ostream::hex_X << loaded_image->SystemTable
45  << ostream::endl;
46  debug << L"DeviceHandle: " << ostream::hex_X
47  << loaded_image->DeviceHandle << ostream::endl;
48  debug << L"FilePath: " << ostream::hex_X << loaded_image->FilePath
49  << ostream::endl;
50  debug << L"Reserved: " << ostream::hex_X << loaded_image->Reserved
51  << ostream::endl;
52  debug << L"LoadOptionsSize: " << ostream::hex_X
53  << loaded_image->LoadOptionsSize << ostream::endl;
54  debug << L"LoadOptions: " << ostream::hex_X << loaded_image->LoadOptions
55  << ostream::endl;
56  debug << L"ImageBase: " << ostream::hex_X << loaded_image->ImageBase
57  << ostream::endl;
58  debug << L"ImageSize: " << ostream::hex_X << loaded_image->ImageSize
59  << ostream::endl;
60  debug << L"ImageCodeType: " << ostream::hex_X
61  << loaded_image->ImageCodeType << ostream::endl;
62  debug << L"ImageDataType: " << ostream::hex_X
63  << loaded_image->ImageDataType << ostream::endl;
64  debug << L"Unload: " << ostream::hex_X << loaded_image->Unload
65  << ostream::endl;
66 
67  // 初始化 Graphics
68  auto graphics = Graphics();
69  // 打印图形信息
70  graphics.print_info();
71  // 设置为 1920*1080
72  graphics.set_mode();
73  // 初始化 Memory
74  auto memory = Memory();
75  memory.print_info();
76  // 加载内核
77  auto elf = Elf(KERNEL_NAME);
78  // kernel_addr = elf.load_kernel_image();
79  kernel_addr = elf.load();
80  } catch (const std::exception &_e) {
81  debug << L"Fatal Error: " << _e.what() << ostream::endl;
82  return EFI_LOAD_ERROR;
83  }
84  debug << L"Set Kernel Entry Point to: [" << ostream::hex_X << kernel_addr
85  << L"]" << ostream::endl;
86  // 退出 boot service
87  uint64_t desc_count = 0;
88  EFI_MEMORY_DESCRIPTOR *memory_map = nullptr;
89  uint64_t map_key = 0;
90  uint64_t desc_size = 0;
91  uint32_t desc_version = 0;
92  memory_map = LibMemoryMap(&desc_count, &map_key, &desc_size, &desc_version);
93  if (memory_map == nullptr) {
94  debug << L"LibMemoryMap failed: memory_map == nullptr" << ostream::endl;
95  throw std::runtime_error("memory_map == nullptr");
96  }
97  status = uefi_call_wrapper(gBS->ExitBootServices, 2, _image_handle, map_key);
98  if (EFI_ERROR(status)) {
99  debug << L"ExitBootServices failed, Memory Map has Changed " << status
100  << ostream::endl;
101  }
102 
103  auto kernel_entry = (void (*)())kernel_addr;
104  kernel_entry();
105 
106  return EFI_SUCCESS;
107 }
Definition: load_elf.h:131
static auto endl(ostream &_ostream) -> ostream &
Definition: ostream.cpp:42
static auto hex_X(ostream &_ostream) -> ostream &
Definition: ostream.cpp:37
static ostream debug
全局输出流
Definition: ostream.hpp:194
#define KERNEL_NAME
Here is the call graph for this function:

Variable Documentation

◆ ImageBase

uintptr_t ImageBase = 0

Definition at line 24 of file boot.cpp.