Skip to content

[libc] init uefi #131246

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions libc/cmake/modules/LLVMLibCArchitectures.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ elseif(LIBC_TARGET_OS STREQUAL "windows")
set(LIBC_TARGET_OS_IS_WINDOWS TRUE)
elseif(LIBC_TARGET_OS STREQUAL "gpu")
set(LIBC_TARGET_OS_IS_GPU TRUE)
elseif(LIBC_TARGET_OS STREQUAL "uefi")
set(LIBC_TARGET_OS_IS_UEFI TRUE)
else()
message(FATAL_ERROR
"Unsupported libc target operating system ${LIBC_TARGET_OS}")
Expand Down
7 changes: 7 additions & 0 deletions libc/config/uefi/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"errno": {
"LIBC_CONF_ERRNO_MODE": {
"value": "LIBC_ERRNO_MODE_SHARED"
}
}
}
11 changes: 11 additions & 0 deletions libc/config/uefi/entrypoints.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
set(TARGET_LIBC_ENTRYPOINTS
# errno.h entrypoints
libc.src.errno.errno
)

set(TARGET_LIBM_ENTRYPOINTS)

set(TARGET_LLVMLIBC_ENTRYPOINTS
${TARGET_LIBC_ENTRYPOINTS}
${TARGET_LIBM_ENTRYPOINTS}
)
4 changes: 4 additions & 0 deletions libc/config/uefi/headers.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
set(TARGET_PUBLIC_HEADERS
libc.include.errno
libc.include.uefi
)
3 changes: 2 additions & 1 deletion libc/include/Uefi.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
header: Uefi.h
standards: UEFI
standards:
- uefi
macros: []
types:
- type_name: EFI_BOOT_SERVICES
Expand Down
2 changes: 2 additions & 0 deletions libc/src/__support/OSUtil/io.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#elif defined(__ELF__)
// TODO: Ideally we would have LIBC_TARGET_OS_IS_BAREMETAL.
#include "baremetal/io.h"
#elif defined(__UEFI__)
#include "uefi/io.h"
#endif

#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_IO_H
11 changes: 11 additions & 0 deletions libc/src/__support/OSUtil/uefi/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
add_object_library(
uefi_util
SRCS
io.cpp
exit.cpp
HDRS
io.h
DEPENDS
libc.src.__support.common
libc.src.__support.CPP.string_view
)
22 changes: 22 additions & 0 deletions libc/src/__support/OSUtil/uefi/exit.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//===-------- UEFI implementation of an exit function ------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===-----------------------------------------------------------------===//

#include "src/__support/OSUtil/exit.h"
#include "include/Uefi.h"
#include "src/__support/macros/config.h"

namespace LIBC_NAMESPACE_DECL {
namespace internal {

[[noreturn]] void exit(int status) {
efi_system_table->BootServices->Exit(efi_image_handle, status, 0, nullptr);
__builtin_unreachable();
}

} // namespace internal
} // namespace LIBC_NAMESPACE_DECL
36 changes: 36 additions & 0 deletions libc/src/__support/OSUtil/uefi/io.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//===---------- UEFI implementation of IO utils ------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===-----------------------------------------------------------------===//

#include "io.h"

#include "src/__support/CPP/string_view.h"
#include "src/__support/macros/config.h"

namespace LIBC_NAMESPACE_DECL {

ssize_t read_from_stdin(char *buf, size_t size) { return 0; }

void write_to_stdout(cpp::string_view msg) {
// TODO: use mbstowcs once implemented
for (size_t i = 0; i < msg.size(); i++) {
char16_t e[2] = {msg[i], 0};
efi_system_table->ConOut->OutputString(
efi_system_table->ConOut, reinterpret_cast<const char16_t *>(&e));
}
}

void write_to_stderr(cpp::string_view msg) {
// TODO: use mbstowcs once implemented
for (size_t i = 0; i < msg.size(); i++) {
char16_t e[2] = {msg[i], 0};
efi_system_table->StdErr->OutputString(
efi_system_table->StdErr, reinterpret_cast<const char16_t *>(&e));
}
}

} // namespace LIBC_NAMESPACE_DECL
25 changes: 25 additions & 0 deletions libc/src/__support/OSUtil/uefi/io.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//===---------- UEFI implementation of IO utils ------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===-----------------------------------------------------------------===//

#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_UEFI_IO_H
#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_UEFI_IO_H

#include "include/llvm-libc-types/size_t.h"
#include "include/llvm-libc-types/ssize_t.h"
#include "src/__support/CPP/string_view.h"
#include "src/__support/macros/config.h"

namespace LIBC_NAMESPACE_DECL {

ssize_t read_from_stdin(char *buf, size_t size);
void write_to_stderr(cpp::string_view msg);
void write_to_stdout(cpp::string_view msg);

} // namespace LIBC_NAMESPACE_DECL

#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_UEFI_IO_H
1 change: 1 addition & 0 deletions libc/utils/hdrgen/hdrgen/header.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"bsd": "BSD",
"gnu": "GNU",
"linux": "Linux",
"uefi": "UEFI",
}

HEADER_TEMPLATE = """\
Expand Down