|
| 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 |
0 commit comments