From 4bc9bc1e4a03c6e4a2138d03b8b82bcc3cc6a6aa Mon Sep 17 00:00:00 2001 From: Kelvin Zhang Date: Fri, 14 Jun 2024 09:58:37 -0700 Subject: [PATCH] Add UEFI protocol headers These headers define the API boundary between UEFI loader and UEFI applications. Test: th Bug: 294283461 Change-Id: Idf064563bb033a8bf2b994261bcd77c0ed7aa2d8 --- lib/uefi/include/boot_service.h | 138 ++++++++++++++++++ lib/uefi/include/efi.h | 37 +++++ .../include/protocols/android_boot_protocol.h | 38 +++++ .../include/protocols/block_io_protocol.h | 57 ++++++++ .../include/protocols/device_path_protocol.h | 39 +++++ .../include/protocols/loaded_image_protocol.h | 38 +++++ .../protocols/riscv_efi_boot_protocol.h | 32 ++++ .../protocols/simple_network_protocol.h | 131 +++++++++++++++++ .../protocols/simple_text_input_protocol.h | 38 +++++ .../protocols/simple_text_output_protocol.h | 47 ++++++ lib/uefi/include/system_table.h | 47 ++++++ lib/uefi/include/types.h | 131 +++++++++++++++++ 12 files changed, 773 insertions(+) create mode 100644 lib/uefi/include/boot_service.h create mode 100644 lib/uefi/include/efi.h create mode 100644 lib/uefi/include/protocols/android_boot_protocol.h create mode 100644 lib/uefi/include/protocols/block_io_protocol.h create mode 100644 lib/uefi/include/protocols/device_path_protocol.h create mode 100644 lib/uefi/include/protocols/loaded_image_protocol.h create mode 100644 lib/uefi/include/protocols/riscv_efi_boot_protocol.h create mode 100644 lib/uefi/include/protocols/simple_network_protocol.h create mode 100644 lib/uefi/include/protocols/simple_text_input_protocol.h create mode 100644 lib/uefi/include/protocols/simple_text_output_protocol.h create mode 100644 lib/uefi/include/system_table.h create mode 100644 lib/uefi/include/types.h diff --git a/lib/uefi/include/boot_service.h b/lib/uefi/include/boot_service.h new file mode 100644 index 000000000..5959f0092 --- /dev/null +++ b/lib/uefi/include/boot_service.h @@ -0,0 +1,138 @@ +/* + * Copyright (C) 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#ifndef __BOOT_SERVICE_TABLE_H__ +#define __BOOT_SERVICE_TABLE_H__ + +#include "protocols/device_path_protocol.h" +#include "types.h" + +typedef enum EFI_LOCATE_HANDLE_SEARCH_TYPE { + ALL_HANDLES = 0, + BY_REGITER_NOTIFY, + BY_PROTOCOL, +} EFI_LOCATE_HANDLE_SEARCH_TYPE; + +typedef EFI_LOCATE_HANDLE_SEARCH_TYPE EfiLocateHandleSearchType; + +typedef enum { EFI_NATIVE_INTERFACE } EFI_INTERFACE_TYPE; + +typedef EFI_INTERFACE_TYPE EfiInterfaceType; + +typedef enum EFI_ALLOCATOR_TYPE { + ALLOCATE_ANY_PAGES, + ALLOCATE_MAX_ADDRESS, + ALLOCATE_ADDRESS, + MAX_ALLOCATE_TYPE +} EfiAllocatorType; + +typedef enum EFI_OPEN_PROTOCOL_ATTRIBUTE : uint32_t { + BY_HANDLE_PROTOCOL = 0x00000001, + GET_PROTOCOL = 0x00000002, + TEST_PROTOCOL = 0x00000004, + BY_CHILD_CONTROLLER = 0x00000008, + BY_DRIVER = 0x00000010, + EXCLUSIVE = 0x00000020, +} EfiOpenProtocolAttributes; + +typedef struct { + uint32_t memory_type; + uint32_t padding; + EfiPhysicalAddr physical_start; + EfiVirtualAddr virtual_start; + uint64_t number_of_pages; + uint64_t attributes; +} EfiMemoryDescriptor; + +typedef struct { + EfiHandle agent_handle; + EfiHandle controller_handle; + uint32_t attributes; + uint32_t open_count; +} EfiOpenProtocolInformationEntry; + +typedef struct { + EfiTableHeader hdr; + EfiTpl (*raise_tpl)(EfiTpl new_tpl); + void (*restore_tpl)(EfiTpl old_tpl); + EfiStatus (*allocate_pages)(EfiAllocatorType type, EfiMemoryType memory_type, size_t pages, + EfiPhysicalAddr* memory); + EfiStatus (*free_pages)(EfiPhysicalAddr memory, size_t pages); + EfiStatus (*get_memory_map)(size_t* memory_map_size, EfiMemoryDescriptor* memory_map, + size_t* map_key, size_t* desc_size, uint32_t* desc_version); + EfiStatus (*allocate_pool)(EfiMemoryType pool_type, size_t size, void** buf); + EfiStatus (*free_pool)(void* buf); + EfiStatus (*create_event)(uint32_t type, EfiTpl notify_tpl, EfiEventNotify notify_fn, + void* notify_ctx, EfiEvent* event); + EfiStatus (*set_timer)(EfiEvent event, EfiTimerDelay type, uint64_t trigger_time); + EfiStatus (*wait_for_event)(size_t num_events, EfiEvent* event, size_t* index); + EfiStatus (*signal_event)(EfiEvent event); + EfiStatus (*close_event)(EfiEvent event); + EfiStatus (*check_event)(EfiEvent event); + EfiStatus (*install_protocol_interface)(EfiHandle* handle, const EfiGuid* protocol, + EfiInterfaceType intf_type, void* intf); + EfiStatus (*reinstall_protocol_interface)(EfiHandle hadle, const EfiGuid* protocol, + void* old_intf, void* new_intf); + EfiStatus (*uninstall_protocol_interface)(EfiHandle handle, const EfiGuid* protocol, void* intf); + EfiStatus (*handle_protocol)(EfiHandle handle, const EfiGuid* protocol, void** intf); + void* reserved; + EfiStatus (*register_protocol_notify)(const EfiGuid* protocol, EfiEvent event, + void** registration); + EfiStatus (*locate_handle)(EfiLocateHandleSearchType search_type, const EfiGuid* protocol, + void* search_key, size_t* buf_size, EfiHandle* buf); + EfiStatus (*locate_device_path)(const EfiGuid* protocol, EfiDevicePathProtocol** path, + EfiHandle* device); + EfiStatus (*install_configuration_table)(const EfiGuid* guid, void* table); + EfiStatus (*load_image)(bool boot_policy, EfiHandle parent_image_handle, + EfiDevicePathProtocol* path, void* src, size_t src_size, + EfiHandle* image_handle); + EfiStatus (*start_image)(EfiHandle image_handle, size_t* exit_data_size, char16_t** exit_data); + EfiStatus (*exit)(EfiHandle image_handle, EfiStatus exit_status, size_t exit_data_size, + char16_t* exit_data); + EfiStatus (*unload_image)(EfiHandle image_handle); + EfiStatus (*exit_boot_services)(EfiHandle image_handle, size_t map_key); + EfiStatus (*get_next_monotonic_count)(uint64_t* count); + EfiStatus (*stall)(size_t microseconds); + EfiStatus (*set_watchdog_timer)(size_t timeout, uint64_t watchdog_code, size_t data_size, + char16_t* watchdog_data); + EfiStatus (*connect_controller)(EfiHandle controller_handle, EfiHandle* driver_image_handle, + EfiDevicePathProtocol* remaining_path, bool recursive); + EfiStatus (*disconnect_controller)(EfiHandle controller_handle, EfiHandle driver_image_handle, + EfiHandle child_handle); + EfiStatus (*open_protocol)(EfiHandle handle, const EfiGuid* protocol, void** intf, + EfiHandle agent_handle, EfiHandle controller_handle, + EfiOpenProtocolAttributes attr); + EfiStatus (*close_protocol)(EfiHandle handle, const EfiGuid* protocol, EfiHandle agent_handle, + EfiHandle controller_handle); + EfiStatus (*open_protocol_information)(EfiHandle handle, const EfiGuid* protocol, + EfiOpenProtocolInformationEntry** entry_buf, + size_t* entry_count); + EfiStatus (*protocols_per_handle)(EfiHandle handle, EfiGuid*** protocol_buf, + size_t* protocol_buf_count); + EfiStatus (*locate_handle_buffer)(EfiLocateHandleSearchType search_type, const EfiGuid* protocol, + void* search_key, size_t* num_handles, EfiHandle** buf); + EfiStatus (*locate_protocol)(const EfiGuid* protocol, void* registration, void** intf); + EfiStatus (*install_multiple_protocol_interfaces)(EfiHandle* handle, ...); + EfiStatus (*uninstall_multiple_protocol_interfaces)(EfiHandle handle, ...); + EfiStatus (*calculate_crc32)(void* data, size_t len, uint32_t* crc32); + void (*copy_mem)(void* dest, const void* src, size_t len); + void (*set_mem)(void* buf, size_t len, uint8_t val); + EfiStatus (*create_event_ex)(EfiEventType type, EfiTpl notify_tpl, EfiEventNotify notify_fn, + const void* notify_ctx, const EfiGuid* event_group, EfiEvent* event); +} EfiBootService; + +#endif // __BOOT_SERVICE_TABLE_H__ \ No newline at end of file diff --git a/lib/uefi/include/efi.h b/lib/uefi/include/efi.h new file mode 100644 index 000000000..2bbe5e388 --- /dev/null +++ b/lib/uefi/include/efi.h @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#ifndef __EFI_H__ +#define __EFI_H__ + +#include +#include +#include + +#include "boot_service.h" +#include "protocols/android_boot_protocol.h" +#include "protocols/block_io_protocol.h" +#include "protocols/device_path_protocol.h" +#include "protocols/loaded_image_protocol.h" +#include "protocols/riscv_efi_boot_protocol.h" +#include "protocols/simple_network_protocol.h" +#include "protocols/simple_text_input_protocol.h" +#include "protocols/simple_text_output_protocol.h" +#include "system_table.h" +#include "types.h" + +#endif // __EFI_H__ diff --git a/lib/uefi/include/protocols/android_boot_protocol.h b/lib/uefi/include/protocols/android_boot_protocol.h new file mode 100644 index 000000000..14c727b7b --- /dev/null +++ b/lib/uefi/include/protocols/android_boot_protocol.h @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2024 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +// This is a custom protocol introduced by GBL. +// See gbl/docs/EFI_ANDROID_BOOT_PROTOCOL.md for details. + +#include "types.h" + +#ifndef __ANDROID_BOOT_PROTOCOL_H__ +#define __ANDROID_BOOT_PROTOCOL_H__ + +typedef struct EfiAndroidBootProtocol { + uint64_t revision; + EfiStatus (*fastboot_usb_interface_start)(struct EfiAndroidBootProtocol* self, + size_t* max_packet_size); + EfiStatus (*fastboot_usb_interface_stop)(struct EfiAndroidBootProtocol* self); + EfiStatus (*fastboot_usb_receive)(struct EfiAndroidBootProtocol* self, + size_t* buffer_size, void* buffer); + EfiStatus (*fastboot_usb_send)(struct EfiAndroidBootProtocol* self, + size_t* buffer_size, const void* buffer); + EfiEvent wait_for_send_completion; +} EfiAndroidBootProtocol; + +#endif //__ANDROID_BOOT_PROTOCOL_H__ diff --git a/lib/uefi/include/protocols/block_io_protocol.h b/lib/uefi/include/protocols/block_io_protocol.h new file mode 100644 index 000000000..48a975dda --- /dev/null +++ b/lib/uefi/include/protocols/block_io_protocol.h @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include "types.h" + +#ifndef __BLOCK_IO_PROTOCOL_H__ +#define __BLOCK_IO_PROTOCOL_H__ + +typedef struct EfiBlockIoMedia EfiBlockIoMedia; +typedef struct EfiBlockIoProtocol EfiBlockIoProtocol; + +struct EfiBlockIoProtocol { + uint64_t revision; + EfiBlockIoMedia* media; + EfiStatus (*reset)(EfiBlockIoProtocol* self, bool extended_verification); + EfiStatus (*read_blocks)(EfiBlockIoProtocol* self, uint32_t media_id, uint64_t lba, + size_t buffer_size, void* buffer); + EfiStatus (*write_blocks)(EfiBlockIoProtocol* self, uint32_t media_id, uint64_t lba, + size_t buffer_size, const void* buffer); + EfiStatus (*flush_blocks)(EfiBlockIoProtocol* self); +}; + +struct EfiBlockIoMedia { + // present in rev1 + uint32_t media_id; + bool removable_media; + bool media_present; + bool logical_partition; + bool read_only; + bool write_caching; + uint32_t block_size; + uint32_t io_align; + uint64_t last_block; + + // present in rev2 + uint64_t lowest_aligned_lba; + uint32_t logical_blocks_per_physical_block; + + // present in rev3 + uint32_t optimal_transfer_length_granularity; +}; + +#endif //__BLOCK_IO_PROTOCOL_H__ diff --git a/lib/uefi/include/protocols/device_path_protocol.h b/lib/uefi/include/protocols/device_path_protocol.h new file mode 100644 index 000000000..874e833ba --- /dev/null +++ b/lib/uefi/include/protocols/device_path_protocol.h @@ -0,0 +1,39 @@ +/* + * Copyright (C) 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include "types.h" + +#ifndef __DEVICE_PATH_PROTOCOL_H__ +#define __DEVICE_PATH_PROTOCOL_H__ + +typedef struct EfiDevicePathProtocol { + uint8_t type; + uint8_t sub_type; + uint8_t length[2]; +} EfiDevicePathProtocol; + +#define EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID \ + EFI_GUID(0x8b843e20, 0x8132, 0x4852, 0x90, 0xcc, 0x55, 0x1a, 0x4e, 0x4a, 0x7f, 0x1c) + +typedef struct EfiDevicePathToTextProtocol { + uint16_t* (*convert_device_node_to_text)(struct EfiDevicePathProtocol* device_node, + bool display_only, bool allow_shortcuts); + uint16_t* (*convert_device_path_to_text)(struct EfiDevicePathProtocol* device_path, + bool display_only, bool allow_shortcuts); +} EfiDevicePathToTextProtocol; + +#endif //__DEVICE_PATH_PROTOCOL_H__ diff --git a/lib/uefi/include/protocols/loaded_image_protocol.h b/lib/uefi/include/protocols/loaded_image_protocol.h new file mode 100644 index 000000000..820907ae5 --- /dev/null +++ b/lib/uefi/include/protocols/loaded_image_protocol.h @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include "system_table.h" +#include "types.h" + +#include "protocols/device_path_protocol.h" + +typedef struct { + uint32_t revision; + EfiHandle parent_handle; + EfiSystemTable* system_table; + EfiHandle device_handle; + EfiDevicePathToTextProtocol* file_path; + void* reserved; + uint32_t load_options_size; + void* load_options; + void* image_base; + uint64_t image_size; + EfiMemoryType image_code_type; + EfiMemoryType image_data_type; + + EfiStatus (*unload)(EfiHandle img); +} EfiLoadedImageProtocol; diff --git a/lib/uefi/include/protocols/riscv_efi_boot_protocol.h b/lib/uefi/include/protocols/riscv_efi_boot_protocol.h new file mode 100644 index 000000000..528844bf3 --- /dev/null +++ b/lib/uefi/include/protocols/riscv_efi_boot_protocol.h @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#ifndef __RISCV_EFI_BOOT_PROTOCOL_H__ +#define __RISCV_EFI_BOOT_PROTOCOL_H__ + +#include + +#include "types.h" + +// Source: https://github.com/riscv-non-isa/riscv-uefi +struct EfiRiscvBootProtocol { + uint64_t revision; + + EfiStatus (*get_boot_hartid)(struct EfiRiscvBootProtocol* self, size_t* boot_hartid); +}; + +#endif // __RISCV_EFI_BOOT_PROTOCOL_H__ diff --git a/lib/uefi/include/protocols/simple_network_protocol.h b/lib/uefi/include/protocols/simple_network_protocol.h new file mode 100644 index 000000000..7c29b843d --- /dev/null +++ b/lib/uefi/include/protocols/simple_network_protocol.h @@ -0,0 +1,131 @@ +/* + * Copyright (C) 2024 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +// Reference: +// https://uefi.org/specs/UEFI/2.10/24_Network_Protocols_SNP_PXE_BIS.html#simple-network-protocol + +#ifndef __SIMPLE_NETWORK_PROTOCOL_H__ +#define __SIMPLE_NETWORK_PROTOCOL_H__ + +#include + +#include "types.h" + +#define EFI_SIMPLE_NETWORK_PROTOCOL_REVISION 0x00010000 + +#define MAX_MCAST_FILTER_CNT 16 + +typedef struct { + uint8_t addr[32]; +} EfiMacAddress; + +typedef struct { + uint8_t addr[4]; +} EfiIpV4Address; + +typedef struct { + uint8_t addr[16]; +} EfiIpV6Address; + +typedef union { + EfiIpV4Address v4; + EfiIpV6Address v6; + uint32_t addr[4]; +} EfiIpAddr; + +typedef struct { + uint32_t state; + uint32_t hw_address_size; + uint32_t media_header_size; + uint32_t max_packet_size; + uint32_t nv_ram_size; + uint32_t nv_ram_access_size; + uint32_t receive_filter_mask; + uint32_t receive_filter_setting; + uint32_t max_m_cast_filter_count; + uint32_t m_cast_filter_count; + EfiMacAddress m_cast_filter[MAX_MCAST_FILTER_CNT]; + EfiMacAddress current_address; + EfiMacAddress broadcast_address; + EfiMacAddress permanent_address; + uint8_t if_type; + bool mac_address_changeable; + bool multiple_tx_supported; + bool media_present_supported; + bool media_present; +} EfiSimpleNetworkMode; + +typedef struct { + uint64_t rx_total_frames; + uint64_t rx_good_frames; + uint64_t rx_undersize_frames; + uint64_t rx_oversize_frames; + uint64_t rx_dropped_frames; + uint64_t rx_unicast_frames; + uint64_t rx_broadcast_frames; + uint64_t rx_multicast_frames; + uint64_t rx_crc_error_frames; + uint64_t rx_total_bytes; + uint64_t tx_total_frames; + uint64_t tx_good_frames; + uint64_t tx_undersize_frames; + uint64_t tx_oversize_frames; + uint64_t tx_dropped_frames; + uint64_t tx_unicast_frames; + uint64_t tx_broadcast_frames; + uint64_t tx_multicast_frames; + uint64_t tx_crc_error_frames; + uint64_t tx_total_bytes; + uint64_t collisions; + uint64_t unsupported_protocol; + uint64_t rx_duplicated_frames; + uint64_t rx_decrypt_error_frames; + uint64_t tx_error_frames; + uint64_t tx_retry_frames; +} EfiNetworkStatistics; + +typedef struct EfiSimpleNetworkProtocol { + uint64_t revision; + EfiStatus (*start)(struct EfiSimpleNetworkProtocol* self); + EfiStatus (*stop)(struct EfiSimpleNetworkProtocol* self); + EfiStatus (*initialize)(struct EfiSimpleNetworkProtocol* self, size_t extra_rx_buffer_size, + size_t extra_tx_buffer_size); + EfiStatus (*reset)(struct EfiSimpleNetworkProtocol* self, bool extended_verification); + EfiStatus (*shutdown)(struct EfiSimpleNetworkProtocol* self); + EfiStatus (*receive_filters)(struct EfiSimpleNetworkProtocol* self, uint32_t enable, + uint32_t disable, bool reset_mcast_filter, size_t mcast_filter_count, + EfiMacAddress* mcast_filter); + EfiStatus (*station_address)(struct EfiSimpleNetworkProtocol* self, bool reset, + EfiMacAddress* new_addr); + EfiStatus (*statistics)(struct EfiSimpleNetworkProtocol* self, bool reset, size_t* stats_size, + EfiNetworkStatistics* stats_table); + EfiStatus (*m_cast_ip_to_mac)(struct EfiSimpleNetworkProtocol* self, bool ipv6, EfiIpAddr* ip, + EfiMacAddress* mac); + EfiStatus (*nv_data)(struct EfiSimpleNetworkProtocol* self, bool read_write, size_t offset, + size_t buf_size, void* buf); + EfiStatus (*get_status)(struct EfiSimpleNetworkProtocol* self, uint32_t* interrupt_status, + void** tx_buf); + EfiStatus (*transmit)(struct EfiSimpleNetworkProtocol* self, size_t header_size, size_t buf_size, + void* buf, EfiMacAddress* src, EfiMacAddress* dest, uint16_t* protocol); + EfiStatus (*receive)(struct EfiSimpleNetworkProtocol* self, size_t* header_size, size_t* buf_size, + void* buf, EfiMacAddress* src, EfiMacAddress* dest, uint16_t* protocol); + + EfiEvent wait_for_packet; + EfiSimpleNetworkMode* mode; +} EfiSimpleNetworkProtocol; + +#endif // __SIMPLE_NETWORK_PROTOCOL_H__ diff --git a/lib/uefi/include/protocols/simple_text_input_protocol.h b/lib/uefi/include/protocols/simple_text_input_protocol.h new file mode 100644 index 000000000..ff4682387 --- /dev/null +++ b/lib/uefi/include/protocols/simple_text_input_protocol.h @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2024 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#ifndef __SIMPLE_TEXT_INPUT_PROTOCOL_H__ +#define __SIMPLE_TEXT_INPUT_PROTOCOL_H__ + +#include "types.h" + +typedef struct EfiInputKey { + uint16_t scan_code; + char16_t unicode_char; +} EfiInputKey; + +typedef struct EfiSimpleTextInputProtocol { + EfiStatus (*reset)(struct EfiSimpleTextInputProtocol* self, + bool extendend_verification); + + EfiStatus (*read_key_stroke)(struct EfiSimpleTextInputProtocol* self, + EfiInputKey* key); + + EfiEvent wait_for_key; +} EfiSimpleTextInputProtocol; + +#endif // __SIMPLE_TEXT_INPUT_PROTOCOL_H__ diff --git a/lib/uefi/include/protocols/simple_text_output_protocol.h b/lib/uefi/include/protocols/simple_text_output_protocol.h new file mode 100644 index 000000000..67c7c8c6d --- /dev/null +++ b/lib/uefi/include/protocols/simple_text_output_protocol.h @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#ifndef __SIMPLE_TEXT_OUTPUT_PROTOCOL_H__ +#define __SIMPLE_TEXT_OUTPUT_PROTOCOL_H__ + +#include "types.h" + +typedef struct { + int32_t max_mode; + int32_t mode; + int32_t attribute; + int32_t cursor_column; + int32_t cursor_row; + bool cursor_visible; +} SimpleTextOutputMode; + +typedef struct EfiSimpleTextOutputProtocol { + EfiStatus (*reset)(struct EfiSimpleTextOutputProtocol* self, bool extended_verification); + EfiStatus (*output_string)(struct EfiSimpleTextOutputProtocol* self, char16_t* string); + EfiStatus (*test_string)(struct EfiSimpleTextOutputProtocol* self, char16_t* string); + EfiStatus (*query_mode)(struct EfiSimpleTextOutputProtocol* self, size_t mode_num, size_t* cols, + size_t* rows); + EfiStatus (*set_mode)(struct EfiSimpleTextOutputProtocol* self, size_t mode_num); + EfiStatus (*set_attribute)(struct EfiSimpleTextOutputProtocol* self, size_t attribute); + EfiStatus (*clear_screen)(struct EfiSimpleTextOutputProtocol* self); + EfiStatus (*set_cursor_position)(struct EfiSimpleTextOutputProtocol* self, size_t col, + size_t row); + EfiStatus (*enable_cursor)(struct EfiSimpleTextOutputProtocol* self, bool visible); + SimpleTextOutputMode* mode; +} EfiSimpleTextOutputProtocol; + +#endif // __SIMPLE_TEXT_OUTPUT_PROTOCOL_H__ diff --git a/lib/uefi/include/system_table.h b/lib/uefi/include/system_table.h new file mode 100644 index 000000000..92beb6896 --- /dev/null +++ b/lib/uefi/include/system_table.h @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#ifndef __SYSTEM_TABLE_H__ +#define __SYSTEM_TABLE_H__ + +#include "types.h" + +#include "boot_service.h" +#include "protocols/simple_text_output_protocol.h" + +typedef struct { + EfiGuid vendor_guid; + const void* vendor_table; +} EfiConfigurationTable; + +typedef struct EfiSystemTable { + EfiTableHeader header; + char16_t* firmware_vendor; + uint32_t firmware_revision; + EfiHandle console_in_handle; + void* con_in; + EfiHandle console_out_handle; + EfiSimpleTextOutputProtocol* con_out; + EfiHandle standard_error_handle; + EfiSimpleTextOutputProtocol* std_err; + void* runtime_service; + EfiBootService* boot_services; + size_t number_of_table_entries; + const EfiConfigurationTable* configuration_table; +} EfiSystemTable; + +#endif // __SYSTEM_TABLE_H__ diff --git a/lib/uefi/include/types.h b/lib/uefi/include/types.h new file mode 100644 index 000000000..6787cef61 --- /dev/null +++ b/lib/uefi/include/types.h @@ -0,0 +1,131 @@ +/* + * Copyright (C) 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#ifndef __EFI_TYPES_H__ +#define __EFI_TYPES_H__ +#include +#include + +typedef struct EfiTableHeader { + uint64_t signature; + uint32_t revision; + uint32_t header_size; + uint32_t crc32; + uint32_t reserved; +} EfiTableHeader; + +typedef struct EfiGuid { + uint32_t data1; + uint16_t data2; + uint16_t data3; + uint8_t data4[8]; +} EfiGuid; + +typedef void *EfiHandle; +typedef void *EfiEvent; +typedef uint64_t EfiPhysicalAddr; +typedef uint64_t EfiVirtualAddr; + +typedef void (*EfiEventNotify)(EfiEvent event, void *ctx); + +typedef enum EFI_EVENT_TYPE : uint32_t { + TIMER = 0x80000000, + RUNTIME = 0x40000000, + NOTIFY_WAIT = 0x00000100, + NOTIFY_SIGNAL = 0x00000200, + SIGNAL_EXIT_BOOT_SERVICES = 0x00000201, + SIGNAL_VIRTUAL_ADDRESS_CHANGE = 0x60000202, +} EfiEventType; + +typedef enum EFI_TPL : size_t { + APPLICATION = 4, + CALLBACK = 8, + NOTIFY = 16, + HIGH_LEVEL = 31, +} EfiTpl; + +typedef enum EFI_TIMER_DELAY { + TIMER_CANCEL, + TIMER_PERIODIC, + TIMER_RELATIVE +} EfiTimerDelay; + +typedef enum { + RESERVED_MEMORY_TYPE, + LOADER_CODE, + LOADER_DATA, + BOOT_SERVICES_CODE, + BOOT_SERVICES_DATA, + RUNTIME_SERVICES_CODE, + RUNTIME_SERVICES_DATA, + CONVENTIONAL_MEMORY, + UNUSABLE_MEMORY, + ACPIRECLAIM_MEMORY, + ACPIMEMORY_NVS, + MEMORY_MAPPED_IO, + MEMORY_MAPPED_IOPORT_SPACE, + PAL_CODE, + PERSISTENT_MEMORY, + MAX_MEMORY_TYPE +} EFI_MEMORY_TYPE; + +typedef EFI_MEMORY_TYPE EfiMemoryType; + +#define EFI_ERROR_MASK ((uintptr_t)INTPTR_MAX + 1) +#define EFI_ERR(x) (EFI_ERROR_MASK | (x)) + +typedef enum EFI_STATUS : uintptr_t { + SUCCESS = 0u, + LOAD_ERROR = EFI_ERR(1), + INVALID_PARAMETER = EFI_ERR(2), + UNSUPPORTED = EFI_ERR(3), + BAD_BUFFER_SIZE = EFI_ERR(4), + BUFFER_TOO_SMALL = EFI_ERR(5), + NOT_READY = EFI_ERR(6), + DEVICE_ERROR = EFI_ERR(7), + WRITE_PROTECTED = EFI_ERR(8), + OUT_OF_RESOURCES = EFI_ERR(9), + VOLUME_CORRUPTED = EFI_ERR(10), + VOLUME_FULL = EFI_ERR(11), + NO_MEDIA = EFI_ERR(12), + MEDIA_CHANGED = EFI_ERR(13), + NOT_FOUND = EFI_ERR(14), + ACCESS_DENIED = EFI_ERR(15), + NO_RESPONSE = EFI_ERR(16), + NO_MAPPING = EFI_ERR(17), + TIMEOUT = EFI_ERR(18), + NOT_STARTED = EFI_ERR(19), + ALREADY_STARTED = EFI_ERR(20), + ABORTED = EFI_ERR(21), + ICMP_ERROR = EFI_ERR(22), + TFTP_ERROR = EFI_ERR(23), + PROTOCOL_ERROR = EFI_ERR(24), + INCOMPATIBLE_VERSION = EFI_ERR(25), + SECURITY_VIOLATION = EFI_ERR(26), + CRC_ERROR = EFI_ERR(27), + END_OF_MEDIA = EFI_ERR(28), + END_OF_FILE = EFI_ERR(31), + INVALID_LANGUAGE = EFI_ERR(32), + COMPROMISED_DATA = EFI_ERR(33), + IP_ADDRESS_CONFLICT = EFI_ERR(34), + HTTP_ERROR = EFI_ERR(35), + CONNECTION_FIN = EFI_ERR(104), + CONNECTION_RESET = EFI_ERR(105), + CONNECTION_REFUSED = EFI_ERR(106), +} EfiStatus; + +#endif // __EFI_TYPES_H__