[PATCH v5 1/6] hw/acpi: Make BIOS linker optional

Oliver Steffen posted 6 patches 1 week, 6 days ago
Maintainers: Gerd Hoffmann <kraxel@redhat.com>, Stefano Garzarella <sgarzare@redhat.com>, Ani Sinha <anisinha@redhat.com>, "Michael S. Tsirkin" <mst@redhat.com>, Igor Mammedov <imammedo@redhat.com>, Paolo Bonzini <pbonzini@redhat.com>, Richard Henderson <richard.henderson@linaro.org>, Eduardo Habkost <eduardo@habkost.net>, Marcel Apfelbaum <marcel.apfelbaum@gmail.com>, Marcelo Tosatti <mtosatti@redhat.com>, Zhao Liu <zhao1.liu@intel.com>
There is a newer version of this series
[PATCH v5 1/6] hw/acpi: Make BIOS linker optional
Posted by Oliver Steffen 1 week, 6 days ago
Make the BIOS linker optional in acpi_table_end() and calculate the ACPI
table checksum directly if no linker is provided.

This makes it possible to call for example
acpi_build_madt() from outside the ACPI table builder context.

Signed-off-by: Oliver Steffen <osteffen@redhat.com>
---
 hw/acpi/aml-build.c | 29 +++++++++++++++++++++++++++--
 1 file changed, 27 insertions(+), 2 deletions(-)

diff --git a/hw/acpi/aml-build.c b/hw/acpi/aml-build.c
index dad4cfcc7d..6a3650076f 100644
--- a/hw/acpi/aml-build.c
+++ b/hw/acpi/aml-build.c
@@ -1730,6 +1730,25 @@ void acpi_table_begin(AcpiTable *desc, GArray *array)
     build_append_int_noprefix(array, 1, 4); /* Creator Revision */
 }
 
+static uint8_t calculate_acpi_checksum(const gchar *data, size_t len)
+{
+    size_t i;
+    uint8_t sum = 0;
+
+    for (i = 0; i < len; ++i) {
+        sum += (uint8_t)data[i];
+    }
+
+    return sum;
+}
+
+static void update_acpi_checksum(gchar *data, size_t start_offset,
+                                 size_t table_len, size_t checksum_offset)
+{
+    uint8_t sum = calculate_acpi_checksum(&data[start_offset], table_len);
+    data[checksum_offset] = 0xff - sum + 1;
+}
+
 void acpi_table_end(BIOSLinker *linker, AcpiTable *desc)
 {
     /*
@@ -1748,8 +1767,14 @@ void acpi_table_end(BIOSLinker *linker, AcpiTable *desc)
      */
     memcpy(len_ptr, &table_len_le, sizeof table_len_le);
 
-    bios_linker_loader_add_checksum(linker, ACPI_BUILD_TABLE_FILE,
-        desc->table_offset, table_len, desc->table_offset + checksum_offset);
+    if (linker != NULL) {
+        bios_linker_loader_add_checksum(linker, ACPI_BUILD_TABLE_FILE,
+                                        desc->table_offset, table_len,
+                                        desc->table_offset + checksum_offset);
+    } else {
+        update_acpi_checksum(desc->array->data, desc->table_offset,
+                             table_len, desc->table_offset + checksum_offset);
+    }
 }
 
 void *acpi_data_push(GArray *table_data, unsigned size)
-- 
2.52.0
Re: [PATCH v5 1/6] hw/acpi: Make BIOS linker optional
Posted by Luigi Leonardi 1 week, 3 days ago
Hi Oliver,

On Tue, Jan 27, 2026 at 11:02:52AM +0100, Oliver Steffen wrote:
>Make the BIOS linker optional in acpi_table_end() and calculate the ACPI
>table checksum directly if no linker is provided.
>
>This makes it possible to call for example
>acpi_build_madt() from outside the ACPI table builder context.
>
>Signed-off-by: Oliver Steffen <osteffen@redhat.com>
>---
> hw/acpi/aml-build.c | 29 +++++++++++++++++++++++++++--
> 1 file changed, 27 insertions(+), 2 deletions(-)
>
>diff --git a/hw/acpi/aml-build.c b/hw/acpi/aml-build.c
>index dad4cfcc7d..6a3650076f 100644
>--- a/hw/acpi/aml-build.c
>+++ b/hw/acpi/aml-build.c
>@@ -1730,6 +1730,25 @@ void acpi_table_begin(AcpiTable *desc, GArray *array)
>     build_append_int_noprefix(array, 1, 4); /* Creator Revision */
> }
>
>+static uint8_t calculate_acpi_checksum(const gchar *data, size_t len)
>+{
>+    size_t i;
>+    uint8_t sum = 0;
>+
>+    for (i = 0; i < len; ++i) {
>+        sum += (uint8_t)data[i];
>+    }
>+
>+    return sum;
>+}
>+

In `hw/acpi/core.c` there is a `acpi_checksum` function that does 
exactly this. Can we reuse this to reduce code duplication? Currently 
that function is marked as static.

Thanks for all the work so far.

Luigi