
From: Simon Glass <sjg@chromium.org> The RSP is not valid unless the checksums are corrrect, so add a test for this. Add a few blank lines for readability while here. Signed-off-by: Simon Glass <sjg@chromium.org> --- lib/acpi/acpi.c | 25 ++++++++++++++++++++++++- test/dm/acpi.c | 4 ++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/lib/acpi/acpi.c b/lib/acpi/acpi.c index 596301a43fe..2174e570633 100644 --- a/lib/acpi/acpi.c +++ b/lib/acpi/acpi.c @@ -5,6 +5,8 @@ * Copyright 2023 Google LLC */ +#define LOG_CATEGORY LOGC_ACPI + #include <mapmem.h> #include <tables_csum.h> #include <acpi/acpi_table.h> @@ -18,6 +20,25 @@ void acpi_update_checksum(struct acpi_table_header *header) header->checksum = table_compute_checksum(header, header->length); } +static bool acpi_valid_rsdp(struct acpi_rsdp *rsdp) +{ + if (strncmp((char *)rsdp, RSDP_SIG, sizeof(RSDP_SIG) - 1) != 0) + return false; + + debug("Looking on %p for valid checksum\n", rsdp); + + if (table_compute_checksum((void *)rsdp, 20) != 0) + return false; + debug("acpi rsdp checksum 1 passed\n"); + + if (rsdp->revision > 1 && + table_compute_checksum((void *)rsdp, rsdp->length)) + return false; + debug("acpi rsdp checksum 2 passed\n"); + + return true; +} + struct acpi_table_header *acpi_find_table(const char *sig) { struct acpi_rsdp *rsdp; @@ -26,8 +47,9 @@ struct acpi_table_header *acpi_find_table(const char *sig) int len, i, count; rsdp = map_sysmem(gd_acpi_start(), 0); - if (!rsdp) + if (!rsdp || !acpi_valid_rsdp(rsdp)) return NULL; + if (rsdp->xsdt_address) { xsdt = nomap_sysmem(rsdp->xsdt_address, 0); len = xsdt->header.length - sizeof(xsdt->header); @@ -39,6 +61,7 @@ struct acpi_table_header *acpi_find_table(const char *sig) len = rsdt->header.length - sizeof(rsdt->header); count = len / sizeof(u32); } + for (i = 0; i < count; i++) { struct acpi_table_header *hdr; diff --git a/test/dm/acpi.c b/test/dm/acpi.c index db012b6d2f1..addc2773220 100644 --- a/test/dm/acpi.c +++ b/test/dm/acpi.c @@ -812,6 +812,8 @@ static int dm_test_acpi_find_table(struct unit_test_state *uts) /* Find with XSDT only */ rsdp->rsdt_address = 0; + acpi_udpate_rsdp_checksum(rsdp); + table = acpi_find_table("TST1"); ut_asserteq_ptr(table1, table); table = acpi_find_table("TST2"); @@ -822,6 +824,8 @@ static int dm_test_acpi_find_table(struct unit_test_state *uts) /* Find with RSDT only */ rsdp->xsdt_address = 0; + acpi_udpate_rsdp_checksum(rsdp); + table = acpi_find_table("TST1"); ut_asserteq_ptr(table1, table); table = acpi_find_table("TST2"); -- 2.43.0