summaryrefslogtreecommitdiff
path: root/lib/smbios-parser.c
diff options
context:
space:
mode:
authorTom Rini <trini@konsulko.com>2020-11-06 08:41:49 -0500
committerTom Rini <trini@konsulko.com>2020-11-06 09:46:43 -0500
commitd062c1344cefb6c368b96efea4c2a20e63657b8d (patch)
tree56dcc373217bbcb248c35f7253c9478c1ba7e715 /lib/smbios-parser.c
parent71d3fa7efa4fc07e6f161c742397ddbe4466c631 (diff)
parente4f8e543f1a905857a753a1d411997a81f4f52aa (diff)
Merge https://gitlab.denx.de/u-boot/custodians/u-boot-x86
- Add a new SMBIOS parser and enable it when booting from coreboot - Fix up various driver names to avoid dtoc warnings - Fully enable ACPI support on Google Chromebook Coral - Add a way to set SMBIOS properties using the devicetree - Update existing boards to use devicetree for SMBIOS using a new default sysinfo driver
Diffstat (limited to 'lib/smbios-parser.c')
-rw-r--r--lib/smbios-parser.c96
1 files changed, 96 insertions, 0 deletions
diff --git a/lib/smbios-parser.c b/lib/smbios-parser.c
new file mode 100644
index 00000000000..b89f988ef9f
--- /dev/null
+++ b/lib/smbios-parser.c
@@ -0,0 +1,96 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (C) 2020, Bachmann electronic GmbH
+ */
+
+#include <common.h>
+#include <smbios.h>
+
+static inline int verify_checksum(const struct smbios_entry *e)
+{
+ /*
+ * Checksums for SMBIOS tables are calculated to have a value, so that
+ * the sum over all bytes yields zero (using unsigned 8 bit arithmetic).
+ */
+ u8 *byte = (u8 *)e;
+ u8 sum = 0;
+
+ for (int i = 0; i < e->length; i++)
+ sum += byte[i];
+
+ return sum;
+}
+
+const struct smbios_entry *smbios_entry(u64 address, u32 size)
+{
+ const struct smbios_entry *entry = (struct smbios_entry *)(uintptr_t)address;
+
+ if (!address | !size)
+ return NULL;
+
+ if (memcmp(entry->anchor, "_SM_", 4))
+ return NULL;
+
+ if (verify_checksum(entry))
+ return NULL;
+
+ return entry;
+}
+
+static const struct smbios_header *next_header(const struct smbios_header *curr)
+{
+ u8 *pos = ((u8 *)curr) + curr->length;
+
+ /* search for _double_ NULL bytes */
+ while (!((*pos == 0) && (*(pos + 1) == 0)))
+ pos++;
+
+ /* step behind the double NULL bytes */
+ pos += 2;
+
+ return (struct smbios_header *)pos;
+}
+
+const struct smbios_header *smbios_header(const struct smbios_entry *entry, int type)
+{
+ const unsigned int num_header = entry->struct_count;
+ const struct smbios_header *header = (struct smbios_header *)entry->struct_table_address;
+
+ for (unsigned int i = 0; i < num_header; i++) {
+ if (header->type == type)
+ return header;
+
+ header = next_header(header);
+ }
+
+ return NULL;
+}
+
+static const char *string_from_smbios_table(const struct smbios_header *header,
+ int idx)
+{
+ unsigned int i = 1;
+ u8 *pos;
+
+ if (!header)
+ return NULL;
+
+ pos = ((u8 *)header) + header->length;
+
+ while (i < idx) {
+ if (*pos == 0x0)
+ i++;
+
+ pos++;
+ }
+
+ return (const char *)pos;
+}
+
+const char *smbios_string(const struct smbios_header *header, int index)
+{
+ if (!header)
+ return NULL;
+
+ return string_from_smbios_table(header, index);
+}