From 27c654f3f0a069113b6abb70817cfe2c5096711e Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Wed, 15 Apr 2026 20:46:29 +0200 Subject: acpi: add basic table type --- libs/acpi/CMakeLists.txt | 4 +++- libs/acpi/acpi/acpi.hpp | 10 +++++----- libs/acpi/acpi/checksum.cpp | 19 ------------------- libs/acpi/acpi/checksum.hpp | 20 -------------------- libs/acpi/acpi/common/basic_table.cpp | 26 ++++++++++++++++++++++++++ libs/acpi/acpi/common/basic_table.hpp | 26 ++++++++++++++++++++++++++ libs/acpi/acpi/common/basic_table.test.cpp | 21 +++++++++++++++++++++ libs/acpi/acpi/common/checksum.cpp | 19 +++++++++++++++++++ libs/acpi/acpi/common/checksum.hpp | 20 ++++++++++++++++++++ libs/acpi/acpi/common/table_header.cpp | 16 ++++++++-------- libs/acpi/acpi/common/table_header.hpp | 5 +---- libs/acpi/acpi/common/table_header.test.cpp | 4 ++-- libs/acpi/acpi/pointers.cpp | 2 +- 13 files changed, 132 insertions(+), 60 deletions(-) delete mode 100644 libs/acpi/acpi/checksum.cpp delete mode 100644 libs/acpi/acpi/checksum.hpp create mode 100644 libs/acpi/acpi/common/basic_table.cpp create mode 100644 libs/acpi/acpi/common/basic_table.hpp create mode 100644 libs/acpi/acpi/common/basic_table.test.cpp create mode 100644 libs/acpi/acpi/common/checksum.cpp create mode 100644 libs/acpi/acpi/common/checksum.hpp (limited to 'libs') diff --git a/libs/acpi/CMakeLists.txt b/libs/acpi/CMakeLists.txt index b9c607d..f850fe4 100644 --- a/libs/acpi/CMakeLists.txt +++ b/libs/acpi/CMakeLists.txt @@ -19,7 +19,8 @@ file(GLOB_RECURSE ACPI_HEADERS ) target_sources("acpi" PRIVATE - "acpi/checksum.cpp" + "acpi/common/basic_table.cpp" + "acpi/common/checksum.cpp" "acpi/common/table_header.cpp" "acpi/madt.cpp" "acpi/pointers.cpp" @@ -60,6 +61,7 @@ if(NOT CMAKE_CROSSCOMPILING) set_source_files_properties("test_data/tables.S" PROPERTIES OBJECT_DEPENDS "${GENERATED_TABLE_BLOBS}") add_executable("acpi_tests" + "acpi/common/basic_table.test.cpp" "acpi/common/table_header.test.cpp" "acpi/madt.test.cpp" "acpi/pointers.test.cpp" diff --git a/libs/acpi/acpi/acpi.hpp b/libs/acpi/acpi/acpi.hpp index fb358cc..47050f2 100644 --- a/libs/acpi/acpi/acpi.hpp +++ b/libs/acpi/acpi/acpi.hpp @@ -1,10 +1,10 @@ #ifndef ACPI_ACPI_HPP #define ACPI_ACPI_HPP -#include // IWYU pragma: export -#include // IWYU pragma: export -#include // IWYU pragma: export -#include // IWYU pragma: export -#include // IWYU pragma: export +#include // IWYU pragma: export +#include // IWYU pragma: export +#include // IWYU pragma: export +#include // IWYU pragma: export +#include // IWYU pragma: export #endif diff --git a/libs/acpi/acpi/checksum.cpp b/libs/acpi/acpi/checksum.cpp deleted file mode 100644 index 56275c8..0000000 --- a/libs/acpi/acpi/checksum.cpp +++ /dev/null @@ -1,19 +0,0 @@ -#include - -#include -#include -#include -#include - -namespace acpi -{ - - auto validate_checksum(std::span data) -> bool - { - auto sum = std::ranges::fold_left(data, std::uint8_t{}, [](auto acc, auto byte) { - return static_cast(acc + static_cast(byte)); - }); - return sum == 0; - } - -} // namespace acpi diff --git a/libs/acpi/acpi/checksum.hpp b/libs/acpi/acpi/checksum.hpp deleted file mode 100644 index a92c242..0000000 --- a/libs/acpi/acpi/checksum.hpp +++ /dev/null @@ -1,20 +0,0 @@ -#ifndef ACPI_CHECKSUM_HPP -#define ACPI_CHECKSUM_HPP - -// IWYU pragma: private, include - -#include -#include - -namespace acpi -{ - - //! Validate and ACPI entity checksum. - //! - //! @param data The data to validate the checksum of. - //! @return true iff. the checksum is valid, false otherwise. - auto validate_checksum(std::span data) -> bool; - -} // namespace acpi - -#endif diff --git a/libs/acpi/acpi/common/basic_table.cpp b/libs/acpi/acpi/common/basic_table.cpp new file mode 100644 index 0000000..5cec91a --- /dev/null +++ b/libs/acpi/acpi/common/basic_table.cpp @@ -0,0 +1,26 @@ +#include +#include +#include + +#include +#include + +namespace acpi +{ + + auto basic_table::validate_checksum() const noexcept -> bool + { + return acpi::validate_checksum({reinterpret_cast(this), m_header.length().value}); + } + + auto basic_table::as_span() const noexcept -> std::span + { + return {reinterpret_cast(this), m_header.length().value}; + } + + auto basic_table::header() const noexcept -> table_header const & + { + return m_header; + } + +} // namespace acpi \ No newline at end of file diff --git a/libs/acpi/acpi/common/basic_table.hpp b/libs/acpi/acpi/common/basic_table.hpp new file mode 100644 index 0000000..43bf533 --- /dev/null +++ b/libs/acpi/acpi/common/basic_table.hpp @@ -0,0 +1,26 @@ +#ifndef ACPI_COMMON_BASIC_TABLE_HPP +#define ACPI_COMMON_BASIC_TABLE_HPP + +#include + +#include +#include + +namespace acpi +{ + + struct basic_table + { + [[nodiscard]] auto validate_checksum() const noexcept -> bool; + + [[nodiscard]] auto as_span() const noexcept -> std::span; + + [[nodiscard]] auto header() const noexcept -> table_header const &; + + private: + table_header m_header; + }; + +} // namespace acpi + +#endif \ No newline at end of file diff --git a/libs/acpi/acpi/common/basic_table.test.cpp b/libs/acpi/acpi/common/basic_table.test.cpp new file mode 100644 index 0000000..e292b9f --- /dev/null +++ b/libs/acpi/acpi/common/basic_table.test.cpp @@ -0,0 +1,21 @@ +#include +#include +#include + +SCENARIO("Basic table functions", "[basic_table]") +{ + GIVEN("A valid compiled table header") + { + auto data = acpi::test_data::tables::table_header(); + + WHEN("parsing the table") + { + auto table = reinterpret_cast(data.data()); + + THEN("the checksum is valid") + { + REQUIRE(table->validate_checksum()); + } + } + } +} diff --git a/libs/acpi/acpi/common/checksum.cpp b/libs/acpi/acpi/common/checksum.cpp new file mode 100644 index 0000000..09425dd --- /dev/null +++ b/libs/acpi/acpi/common/checksum.cpp @@ -0,0 +1,19 @@ +#include + +#include +#include +#include +#include + +namespace acpi +{ + + auto validate_checksum(std::span data) -> bool + { + auto sum = std::ranges::fold_left(data, std::uint8_t{}, [](auto acc, auto byte) { + return static_cast(acc + static_cast(byte)); + }); + return sum == 0; + } + +} // namespace acpi diff --git a/libs/acpi/acpi/common/checksum.hpp b/libs/acpi/acpi/common/checksum.hpp new file mode 100644 index 0000000..a92c242 --- /dev/null +++ b/libs/acpi/acpi/common/checksum.hpp @@ -0,0 +1,20 @@ +#ifndef ACPI_CHECKSUM_HPP +#define ACPI_CHECKSUM_HPP + +// IWYU pragma: private, include + +#include +#include + +namespace acpi +{ + + //! Validate and ACPI entity checksum. + //! + //! @param data The data to validate the checksum of. + //! @return true iff. the checksum is valid, false otherwise. + auto validate_checksum(std::span data) -> bool; + +} // namespace acpi + +#endif diff --git a/libs/acpi/acpi/common/table_header.cpp b/libs/acpi/acpi/common/table_header.cpp index 17a8219..c69ff5d 100644 --- a/libs/acpi/acpi/common/table_header.cpp +++ b/libs/acpi/acpi/common/table_header.cpp @@ -27,7 +27,7 @@ namespace acpi static_assert(sizeof(common_table_header_data) == common_table_header_size); - auto common_table_header::creator_revision() const noexcept -> decltype(common_table_header_data::creator_revision) + auto table_header::creator_revision() const noexcept -> decltype(common_table_header_data::creator_revision) { using type = decltype(common_table_header_data::creator_revision); @@ -42,7 +42,7 @@ namespace acpi return value; } - auto common_table_header::creator_id() const noexcept -> std::string_view + auto table_header::creator_id() const noexcept -> std::string_view { constexpr auto size = sizeof(common_table_header_data::creator_id); constexpr auto offset = offsetof(common_table_header_data, creator_id); @@ -52,7 +52,7 @@ namespace acpi return std::string_view{base + offset, size}; } - auto common_table_header::length() const noexcept -> kstd::units::bytes + auto table_header::length() const noexcept -> kstd::units::bytes { using type = decltype(common_table_header_data::length); @@ -67,7 +67,7 @@ namespace acpi return kstd::units::bytes{raw_value}; } - auto common_table_header::oem_id() const noexcept -> std::string_view + auto table_header::oem_id() const noexcept -> std::string_view { constexpr auto size = sizeof(common_table_header_data::oem_id); constexpr auto offset = offsetof(common_table_header_data, oem_id); @@ -77,7 +77,7 @@ namespace acpi return std::string_view{base + offset, size}; } - auto common_table_header::oem_table_id() const noexcept -> std::string_view + auto table_header::oem_table_id() const noexcept -> std::string_view { constexpr auto size = sizeof(common_table_header_data::oem_table_id); constexpr auto offset = offsetof(common_table_header_data, oem_table_id); @@ -87,7 +87,7 @@ namespace acpi return std::string_view{base + offset, size}; } - auto common_table_header::oem_revision() const noexcept -> decltype(common_table_header_data::oem_revision) + auto table_header::oem_revision() const noexcept -> decltype(common_table_header_data::oem_revision) { using type = decltype(common_table_header_data::oem_revision); @@ -102,7 +102,7 @@ namespace acpi return value; } - auto common_table_header::revision() const noexcept -> decltype(common_table_header_data::revision) + auto table_header::revision() const noexcept -> decltype(common_table_header_data::revision) { using type = decltype(common_table_header_data::revision); @@ -117,7 +117,7 @@ namespace acpi return value; } - auto common_table_header::signature() const noexcept -> std::string_view + auto table_header::signature() const noexcept -> std::string_view { constexpr auto size = sizeof(common_table_header_data::signature); constexpr auto offset = offsetof(common_table_header_data, signature); diff --git a/libs/acpi/acpi/common/table_header.hpp b/libs/acpi/acpi/common/table_header.hpp index 8ecfd0a..529da81 100644 --- a/libs/acpi/acpi/common/table_header.hpp +++ b/libs/acpi/acpi/common/table_header.hpp @@ -19,7 +19,7 @@ namespace acpi //! Multibyte fields in the header are not guaranteed to be naturally aligned by the firmware. Therefore, no direct //! access to multibyte fields is provided. Instead, the provided member functions handle alignment of the multibyte //! values and thus provide a safe interface to the header fields. - struct common_table_header + struct table_header { //! Get the revision of the utility used to create this table. [[nodiscard]] auto creator_revision() const noexcept -> std::uint32_t; @@ -45,9 +45,6 @@ namespace acpi //! Get the signature of this table. [[nodiscard]] auto signature() const noexcept -> std::string_view; - //! Validate this table's checksum - [[nodiscard]] auto validate() const noexcept -> bool; - private: std::array m_data; }; diff --git a/libs/acpi/acpi/common/table_header.test.cpp b/libs/acpi/acpi/common/table_header.test.cpp index e43e403..d6976b3 100644 --- a/libs/acpi/acpi/common/table_header.test.cpp +++ b/libs/acpi/acpi/common/table_header.test.cpp @@ -12,7 +12,7 @@ SCENARIO("Common table header parsing", "[common_table_header]") WHEN("parsing the header") { - auto header = reinterpret_cast(data.data()); + auto header = reinterpret_cast(data.data()); THEN("the signature is correct") { @@ -26,7 +26,7 @@ SCENARIO("Common table header parsing", "[common_table_header]") THEN("the length is correct") { - REQUIRE(header->length() == kstd::type_size); + REQUIRE(header->length() == kstd::type_size); } THEN("the oem id is correct") diff --git a/libs/acpi/acpi/pointers.cpp b/libs/acpi/acpi/pointers.cpp index 8a8629f..45a42ce 100644 --- a/libs/acpi/acpi/pointers.cpp +++ b/libs/acpi/acpi/pointers.cpp @@ -1,6 +1,6 @@ #include -#include +#include #include #include -- cgit v1.2.3