Skip to content

Commit e5a4692

Browse files
[libc][uefi] add errno and uefi status conversion
1 parent 40a26a0 commit e5a4692

File tree

5 files changed

+119
-5
lines changed

5 files changed

+119
-5
lines changed

libc/src/__support/OSUtil/uefi/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@ add_object_library(
33
SRCS
44
io.cpp
55
exit.cpp
6+
error.cpp
67
HDRS
78
io.h
9+
error.h
810
DEPENDS
11+
libc.include.errno
12+
libc.include.limits
913
libc.src.__support.common
1014
libc.src.__support.CPP.string_view
1115
)
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
//===---------- UEFI implementation of error utils ------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===-------------------------------------------------------------------===//
8+
9+
#include "error.h"
10+
#include "errno.h"
11+
#include "limits.h"
12+
#include "src/__support/macros/config.h"
13+
14+
#define ERROR_BIT (sizeof(size_t) * CHAR_BIT)
15+
16+
namespace LIBC_NAMESPACE_DECL {
17+
18+
static const struct {
19+
EFI_STATUS status;
20+
int errno;
21+
} uefi_status_errno_map[] = {
22+
{EFI_SUCCESS, 0},
23+
{ERROR_BIT | EFI_LOAD_ERROR, EINVAL},
24+
{ERROR_BIT | EFI_INVALID_PARAMETER, EINVAL},
25+
{ERROR_BIT | EFI_BAD_BUFFER_SIZE, EINVAL},
26+
{ERROR_BIT | EFI_NOT_READY, EBUSY},
27+
{ERROR_BIT | EFI_DEVICE_ERROR, EIO},
28+
{ERROR_BIT | EFI_WRITE_PROTECTED, EPERM},
29+
{ERROR_BIT | EFI_OUT_OF_RESOURCES, ENOMEM},
30+
{ERROR_BIT | EFI_VOLUME_CORRUPTED, EROFS},
31+
{ERROR_BIT | EFI_VOLUME_FULL, ENOSPC},
32+
{ERROR_BIT | EFI_NO_MEDIA, ENODEV},
33+
{ERROR_BIT | EFI_MEDIA_CHANGED, ENXIO},
34+
{ERROR_BIT | EFI_NOT_FOUND, ENOENT},
35+
{ERROR_BIT | EFI_ACCESS_DENIED, EACCES},
36+
{ERROR_BIT | EFI_NO_RESPONSE, EBUSY},
37+
{ERROR_BIT | EFI_NO_MAPPING, ENODEV},
38+
{ERROR_BIT | EFI_TIMEOUT, EBUSY},
39+
{ERROR_BIT | EFI_NOT_STARTED, EAGAIN},
40+
{ERROR_BIT | EFI_ALREADY_STARTED, EINVAL},
41+
{ERROR_BIT | EFI_ABORTED, EFAULT},
42+
{ERROR_BIT | EFI_ICMP_ERROR, EIO},
43+
{ERROR_BIT | EFI_TFTP_ERROR, EIO},
44+
{ERROR_BIT | EFI_PROTOCOL_ERROR, EINVAL},
45+
{ERROR_BIT | EFI_INCOMPATIBLE_VERSION, EINVAL},
46+
{ERROR_BIT | EFI_SECURITY_VIOLATION, EPERM},
47+
{ERROR_BIT | EFI_CRC_ERROR, EINVAL},
48+
{ERROR_BIT | EFI_END_OF_MEDIA, EPIPE},
49+
{ERROR_BIT | EFI_END_OF_FILE, EPIPE},
50+
{ERROR_BIT | EFI_INVALID_LANGUAGE, EINVAL},
51+
{ERROR_BIT | EFI_COMPROMISED_DATA, EINVAL},
52+
{ERROR_BIT | EFI_IP_ADDRESS_CONFLICT, EINVAL},
53+
{ERROR_BIT | EFI_HTTP_ERROR, EIO},
54+
{EFI_WARN_UNKNOWN_GLYPH, EINVAL},
55+
{EFI_WARN_DELETE_FAILURE, EROFS},
56+
{EFI_WARN_WRITE_FAILURE, EROFS},
57+
{EFI_WARN_BUFFER_TOO_SMALL, E2BIG},
58+
{EFI_WARN_STALE_DATA, EINVAL},
59+
{EFI_WARN_FILE_SYSTEM, EROFS},
60+
{EFI_WARN_RESET_REQUIRED, EINTR},
61+
};
62+
63+
static constexpr size_t uefi_status_errno_map_length =
64+
sizeof(uefi_status_errno_map) / sizeof(uefi_status_errno_map[0]);
65+
66+
int uefi_status_to_errno(EFI_STATUS status) {
67+
for (size_t i = 0; i < uefi_status_errno_map_length; i++) {
68+
if (uefi_status_errno_map[i].status == status) {
69+
return uefi_status_errno_map[i].errno;
70+
}
71+
}
72+
73+
// Unknown type
74+
__builtin_unreachable();
75+
}
76+
77+
EFI_STATUS errno_to_uefi_status(int errno) {
78+
for (size_t i = 0; i < uefi_status_errno_map_length; i++) {
79+
if (uefi_status_errno_map[i].errno == errno) {
80+
return uefi_status_errno_map[i].status;
81+
}
82+
}
83+
84+
// Unknown type
85+
__builtin_unreachable();
86+
}
87+
88+
} // namespace LIBC_NAMESPACE_DECL
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//===---------- UEFI implementation of error utils ------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===-------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_UEFI_ERROR_H
10+
#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_UEFI_ERROR_H
11+
12+
#include "include/llvm-libc-types/EFI_STATUS.h"
13+
#include "src/__support/macros/config.h"
14+
15+
namespace LIBC_NAMESPACE_DECL {
16+
17+
int uefi_status_to_errno(EFI_STATUS status);
18+
EFI_STATUS errno_to_uefi_status(int errno);
19+
20+
} // namespace LIBC_NAMESPACE_DECL
21+
22+
#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_UEFI_ERROR_H

libc/startup/uefi/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ endfunction()
3131
add_startup_object(
3232
crt1
3333
SRCS
34-
crt1.cpp
34+
crt1.cpp
35+
DEPENDS
36+
libc.src.__support.OSUtil.uefi.uefi_util
3537
)
3638

3739
add_custom_target(libc-startup)

libc/startup/uefi/crt1.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "include/llvm-libc-types/EFI_HANDLE.h"
1111
#include "include/llvm-libc-types/EFI_STATUS.h"
1212
#include "include/llvm-libc-types/EFI_SYSTEM_TABLE.h"
13+
#include "src/__support/OSUtil/uefi/error.h"
1314
#include "src/__support/macros/config.h"
1415

1516
extern "C" {
@@ -24,9 +25,6 @@ EFI_STATUS EfiMain(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable) {
2425

2526
// TODO: we need the EFI_SHELL_PROTOCOL, malloc, free, and UTF16 -> UTF8
2627
// conversion.
27-
main(0, nullptr, nullptr);
28-
29-
// TODO: convert the return value of main to EFI_STATUS
30-
return EFI_SUCCESS;
28+
return LIBC_NAMESPACE::errno_to_uefi_status(main(0, nullptr, nullptr));
3129
}
3230
}

0 commit comments

Comments
 (0)