Skip to content

Commit

Permalink
fix(boot): remove stdexcept in efi codes
Browse files Browse the repository at this point in the history
Signed-off-by: Zone.N <[email protected]>
  • Loading branch information
MRNIU committed Mar 4, 2024
1 parent 1814606 commit e9b1d55
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 159 deletions.
1 change: 0 additions & 1 deletion cmake/compile_config.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ list(APPEND COMMON_COMPILE_OPTIONS
# 启用 free-standing 环境
-ffreestanding


# 目标平台编译选项
# @todo clang 交叉编译参数
$<$<STREQUAL:${TARGET_ARCH},x86_64>:
Expand Down
105 changes: 52 additions & 53 deletions src/boot/boot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
* </table>
*/

#include <exception>
#include <stdexcept>

#include "load_elf.h"
#include "ostream.hpp"
#include "project_config.h"
Expand All @@ -28,59 +25,61 @@ efi_main(EFI_HANDLE _image_handle,
[[maybe_unused]] EFI_SYSTEM_TABLE *_system_table) {
EFI_STATUS status = EFI_SUCCESS;
uint64_t kernel_addr = 0;
try {
// 输出 efi 信息
EFI_LOADED_IMAGE *loaded_image = nullptr;
status = LibLocateProtocol(&LoadedImageProtocol,
reinterpret_cast<void **>(&loaded_image));
if (EFI_ERROR(status)) {
debug << L"LibLocateProtocol: " << status << ostream::endl;
}

debug << L"Revision: " << ostream::hex_X << loaded_image->Revision
<< ostream::endl;
debug << L"ParentHandle: " << ostream::hex_X
<< loaded_image->ParentHandle << ostream::endl;
debug << L"SystemTable: " << ostream::hex_X << loaded_image->SystemTable
<< ostream::endl;
debug << L"DeviceHandle: " << ostream::hex_X
<< loaded_image->DeviceHandle << ostream::endl;
debug << L"FilePath: " << ostream::hex_X << loaded_image->FilePath
<< ostream::endl;
debug << L"Reserved: " << ostream::hex_X << loaded_image->Reserved
<< ostream::endl;
debug << L"LoadOptionsSize: " << ostream::hex_X
<< loaded_image->LoadOptionsSize << ostream::endl;
debug << L"LoadOptions: " << ostream::hex_X << loaded_image->LoadOptions
<< ostream::endl;
debug << L"ImageBase: " << ostream::hex_X << loaded_image->ImageBase
<< ostream::endl;
debug << L"ImageSize: " << ostream::hex_X << loaded_image->ImageSize
<< ostream::endl;
debug << L"ImageCodeType: " << ostream::hex_X
<< loaded_image->ImageCodeType << ostream::endl;
debug << L"ImageDataType: " << ostream::hex_X
<< loaded_image->ImageDataType << ostream::endl;
debug << L"Unload: " << ostream::hex_X << loaded_image->Unload
<< ostream::endl;
// 输出 efi 信息
EFI_LOADED_IMAGE *loaded_image = nullptr;
status = LibLocateProtocol(&LoadedImageProtocol,
reinterpret_cast<void **>(&loaded_image));
if (EFI_ERROR(status)) {
debug << L"LibLocateProtocol: " << status << ostream::endl;
return status;
}

debug << L"Revision: " << ostream::hex_X << loaded_image->Revision
<< ostream::endl;
debug << L"ParentHandle: " << ostream::hex_X << loaded_image->ParentHandle
<< ostream::endl;
debug << L"SystemTable: " << ostream::hex_X << loaded_image->SystemTable
<< ostream::endl;
debug << L"DeviceHandle: " << ostream::hex_X << loaded_image->DeviceHandle
<< ostream::endl;
debug << L"FilePath: " << ostream::hex_X << loaded_image->FilePath
<< ostream::endl;
debug << L"Reserved: " << ostream::hex_X << loaded_image->Reserved
<< ostream::endl;
debug << L"LoadOptionsSize: " << ostream::hex_X
<< loaded_image->LoadOptionsSize << ostream::endl;
debug << L"LoadOptions: " << ostream::hex_X << loaded_image->LoadOptions
<< ostream::endl;
debug << L"ImageBase: " << ostream::hex_X << loaded_image->ImageBase
<< ostream::endl;
debug << L"ImageSize: " << ostream::hex_X << loaded_image->ImageSize
<< ostream::endl;
debug << L"ImageCodeType: " << ostream::hex_X << loaded_image->ImageCodeType
<< ostream::endl;
debug << L"ImageDataType: " << ostream::hex_X << loaded_image->ImageDataType
<< ostream::endl;
debug << L"Unload: " << ostream::hex_X << loaded_image->Unload
<< ostream::endl;

// 初始化 Graphics
auto graphics = Graphics();
// 打印图形信息
graphics.print_info();
// 设置为 1920*1080
graphics.set_mode();
// 初始化 Memory
auto memory = Memory();
memory.print_info();
// 加载内核
auto elf = Elf(KERNEL_NAME);
// kernel_addr = elf.load_kernel_image();
kernel_addr = elf.load();
} catch (const std::exception &_e) {
debug << L"Fatal Error: " << _e.what() << ostream::endl;
// 初始化 Graphics
auto graphics = Graphics();
// 打印图形信息
graphics.print_info();
// 设置为 1920*1080
graphics.set_mode();
// 初始化 Memory
auto memory = Memory();
memory.print_info();
// 加载内核
auto elf = Elf(KERNEL_NAME);
// kernel_addr = elf.load_kernel_image();
kernel_addr = elf.load();
if (kernel_addr == 0) {
debug << L"Failed to load kernel" << ostream::endl;
return EFI_LOAD_ERROR;
}

debug << L"Set Kernel Entry Point to: [" << ostream::hex_X << kernel_addr
<< L"]" << ostream::endl;
// 退出 boot service
Expand All @@ -92,12 +91,12 @@ efi_main(EFI_HANDLE _image_handle,
memory_map = LibMemoryMap(&desc_count, &map_key, &desc_size, &desc_version);
if (memory_map == nullptr) {
debug << L"LibMemoryMap failed: memory_map == nullptr" << ostream::endl;
throw std::runtime_error("memory_map == nullptr");
}
status = uefi_call_wrapper(gBS->ExitBootServices, 2, _image_handle, map_key);
if (EFI_ERROR(status)) {
debug << L"ExitBootServices failed, Memory Map has Changed " << status
<< ostream::endl;
return status;
}

auto kernel_entry = (void (*)())kernel_addr;
Expand Down
38 changes: 21 additions & 17 deletions src/boot/graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,22 @@
* </table>
*/

#include <stdexcept>

#include "load_elf.h"
#include "ostream.hpp"

Graphics::Graphics() {
auto status = LibLocateProtocol(&GraphicsOutputProtocol,
reinterpret_cast<void **>(&gop));
if (EFI_ERROR(status)) {
debug << L"Could not locate GOP: " << status << ostream::endl;
throw std::runtime_error("EFI_ERROR(status)");
debug << L"Graphics::Graphics() Could not locate GOP: " << status
<< ostream::endl;
return;
}
if (gop == nullptr) {
debug << L"LibLocateProtocol(GraphicsOutputProtocol, &gop) returned "
debug << L"Graphics::Graphics() LibLocateProtocol(GraphicsOutputProtocol, "
L"&gop) returned "
<< status << " but gop is nullptr" << ostream::endl;

throw std::runtime_error("gop == nullptr");
return;
}
}

Expand All @@ -44,23 +43,26 @@ void Graphics::set_mode(EFI_GRAPHICS_PIXEL_FORMAT _format, uint32_t _width,
status = uefi_call_wrapper(gop->QueryMode, 4, gop, i, &mode_info_size,
&mode_info);
if (EFI_ERROR(status)) {
debug << L"QueryMode failed: " << status << ostream::endl;
throw std::runtime_error("EFI_ERROR(status)");
debug << L"Graphics::set_mode QueryMode failed: " << status
<< ostream::endl;
return;
}

if ((mode_info->PixelFormat == _format) &&
(mode_info->HorizontalResolution == _width) &&
(mode_info->VerticalResolution == _height)) {
status = uefi_call_wrapper(gop->SetMode, 2, gop, i);
if (EFI_ERROR(status)) {
debug << L"SetMode failed: " << status << ostream::endl;
throw std::runtime_error("EFI_ERROR(status)");
debug << L"Graphics::set_mode SetMode failed: " << status
<< ostream::endl;
return;
}
}
status = uefi_call_wrapper(gBS->FreePool, 1, mode_info);
if (EFI_ERROR(status)) {
debug << L"FreePool failed: " << status << ostream::endl;
throw std::runtime_error("EFI_ERROR(status)");
debug << L"Graphics::set_mode FreePool failed: " << status
<< ostream::endl;
return;
}
}

Expand Down Expand Up @@ -92,8 +94,9 @@ void Graphics::print_info() const {
auto status = uefi_call_wrapper(gop->QueryMode, 4, gop, i, &mode_info_size,
&mode_info);
if (EFI_ERROR(status)) {
debug << L"QueryMode failed: " << status << ostream::endl;
throw std::runtime_error("EFI_ERROR(status)");
debug << L"Graphics::print_info() QueryMode failed: " << status
<< ostream::endl;
return;
}

debug << L"Mode: " << i << L", Version: " << ostream::hex_x
Expand All @@ -105,8 +108,9 @@ void Graphics::print_info() const {

status = uefi_call_wrapper(gBS->FreePool, 1, mode_info);
if (EFI_ERROR(status)) {
debug << L"FreePool failed: " << status << ostream::endl;
throw std::runtime_error("EFI_ERROR(status)");
debug << L"Graphics::print_info() FreePool failed: " << status
<< ostream::endl;
return;
}
}
}
11 changes: 7 additions & 4 deletions src/boot/include/load_elf.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,9 @@ class Memory {

/**
* 更新内存映射信息
* @return 失败返回 false
*/
void flush_desc();
bool flush_desc();
};

/**
Expand Down Expand Up @@ -152,12 +153,13 @@ class Elf {

/**
* 加载 elf 内核
* @return 内核入口点
* @return 成功返回内核入口地址,失败返回 0
*/
[[nodiscard]] auto load_kernel_image() const -> uint64_t;

/**
* 将 elf 文件加载进内存
* @return 成功返回内核入口地址,失败返回 0
*/
auto load() const -> uintptr_t;

Expand Down Expand Up @@ -229,12 +231,13 @@ class Elf {
* 将 elf 段加载到内存
* @param _phdr 要加载的程序段 phdr
*/
void load_sections(const Elf64_Phdr &_phdr) const;
bool load_sections(const Elf64_Phdr &_phdr) const;

/**
* 加载程序段
* @return 失败返回 false
*/
void load_program_sections() const;
bool load_program_sections() const;
};

#endif /* SIMPLEKERNEL_LOAD_ELF_H */
Loading

0 comments on commit e9b1d55

Please sign in to comment.