]> xenbits.xensource.com Git - unikraft/unikraft.git/commitdiff
plat/common/x86/acpi.c: Generalize checksum verification
authorSergiu Moga <sergiu.moga@protonmail.com>
Mon, 24 Apr 2023 09:48:57 +0000 (12:48 +0300)
committerUnikraft <monkey@unikraft.io>
Fri, 11 Aug 2023 15:57:48 +0000 (15:57 +0000)
Since all structures calculate their checksum in the same manner,
use only one general checksum verification function for all of them.

Signed-off-by: Sergiu Moga <sergiu.moga@protonmail.com>
Reviewed-by: Dragos Petre <dragos.petre27@gmail.com>
Reviewed-by: Marco Schlumpp <marco@unikraft.io>
Approved-by: Razvan Deaconescu <razvand@unikraft.io>
Tested-by: Unikraft CI <monkey@unikraft.io>
GitHub-Closes: #911

plat/Config.uk
plat/common/x86/acpi.c

index 81fcdba2c9d1be43ca4201a1906dee1070cc2bdd..95b59b6bdfe5474a73b230faa59b0cebe109bbc0 100644 (file)
@@ -15,6 +15,10 @@ config UKPLAT_MEMREGION_MAX_COUNT
                the amount of statically allocated space in the image made available
                for storing memory region descriptors' metadata.
 
+config UKPLAT_ACPI_CHECKSUM
+       bool "Verify ACPI structures' checksum"
+       default n
+
 config UKPLAT_LCPU_MAXCOUNT
        int "Maximum number of supported logical CPUs"
        range 1 256
index 8f1c0a223ca3614ef51b6a12d2cf42967fda4fb6..3b90f587d6d04765f169ba4d6d5e00d124657193 100644 (file)
 #include <errno.h>
 
 #define RSDT_ENTRIES(rsdt) (((rsdt)->hdr.tab_len - sizeof((rsdt)->hdr)) / 4)
+#define RSDP10_LEN             20
 
 static __u8 acpi_version;
 static struct acpi_rsdp *acpi_rsdp;
 static struct acpi_rsdt *acpi_rsdt;
 static struct acpi_madt *acpi_madt;
 
-/*
- * Compute checksum for ACPI RSDP table.
- */
-
-static inline int verify_rsdp_checksum(struct acpi_rsdp *rsdp)
+static __u8 get_acpi_checksum(void __maybe_unused *buf, __sz __maybe_unused len)
 {
+#ifdef CONFIG_UKPLAT_ACPI_CHECKSUM
+       const __u8 *const ptr_end = (__u8 *)buf + len;
+       const __u8 *ptr = (__u8 *)buf;
        __u8 checksum = 0;
-       __u8 *ptr = (__u8 *)rsdp;
 
-       while (ptr < (__u8 *)(rsdp + 1))
+       while (ptr < ptr_end)
                checksum += *ptr++;
 
-       return checksum == 0 ? 0 : -1;
-}
-
-/*
- * Compute checksum for any ACPI table, except RSDP.
- */
-
-static inline int verify_acpi_checksum(struct acpi_sdt_hdr *h)
-{
-       __u8 checksum = 0;
-       __u32 i;
-
-       for (i = 0; i < h->tab_len; i++)
-               checksum += ((__u8 *)h)[i];
-
-       return checksum == 0 ? 0 : -1;
+       return checksum;
+#else
+       return 0;
+#endif
 }
 
 /**
@@ -105,7 +92,7 @@ static int detect_acpi_version(void)
                return -ENOENT;
        }
 
-       if (verify_rsdp_checksum(acpi_rsdp) != 0) {
+       if (unlikely(!get_acpi_checksum(acpi_rsdp, RSDP10_LEN))) {
                uk_pr_err("ACPI RSDP corrupted\n");
 
                acpi_rsdp = NULL;
@@ -143,7 +130,8 @@ static int acpi10_find_rsdt(void)
        acpi_rsdt = (struct acpi_rsdt *)((__uptr)acpi_rsdp->rsdt_paddr);
        uk_pr_debug("ACPI RSDT present at %p\n", acpi_rsdt);
 
-       if (unlikely(verify_acpi_checksum(&acpi_rsdt->hdr) != 0)) {
+       if (unlikely(!get_acpi_checksum(&acpi_rsdt->hdr,
+                    acpi_rsdt->hdr.tab_len))) {
                uk_pr_err("ACPI RSDT corrupted\n");
 
                acpi_rsdt = NULL;
@@ -178,7 +166,7 @@ static int acpi10_find_madt(void)
 
                uk_pr_debug("ACPI MADT present at %p\n", h);
 
-               if (verify_acpi_checksum(h) != 0) {
+               if (unlikely(!get_acpi_checksum(h, h->tab_len))) {
                        uk_pr_err("ACPI MADT corrupted\n");
                        return -ENOENT;
                }