1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2020, Bachmann electronic GmbH
6 #define LOG_CATEGORY LOGC_BOOT
11 static inline int verify_checksum(const struct smbios_entry *e)
14 * Checksums for SMBIOS tables are calculated to have a value, so that
15 * the sum over all bytes yields zero (using unsigned 8 bit arithmetic).
20 for (int i = 0; i < e->length; i++)
26 const struct smbios_entry *smbios_entry(u64 address, u32 size)
28 const struct smbios_entry *entry = (struct smbios_entry *)(uintptr_t)address;
33 if (memcmp(entry->anchor, "_SM_", 4))
36 if (verify_checksum(entry))
42 static const struct smbios_header *next_header(const struct smbios_header *curr)
44 u8 *pos = ((u8 *)curr) + curr->length;
46 /* search for _double_ NULL bytes */
47 while (!((*pos == 0) && (*(pos + 1) == 0)))
50 /* step behind the double NULL bytes */
53 return (struct smbios_header *)pos;
56 const struct smbios_header *smbios_header(const struct smbios_entry *entry, int type)
58 const unsigned int num_header = entry->struct_count;
59 const struct smbios_header *header = (struct smbios_header *)entry->struct_table_address;
61 for (unsigned int i = 0; i < num_header; i++) {
62 if (header->type == type)
65 header = next_header(header);
71 static const char *string_from_smbios_table(const struct smbios_header *header,
80 pos = ((u8 *)header) + header->length;
89 return (const char *)pos;
92 const char *smbios_string(const struct smbios_header *header, int index)
97 return string_from_smbios_table(header, index);
100 int smbios_update_version_full(void *smbios_tab, const char *version)
102 const struct smbios_header *hdr;
103 struct smbios_type0 *bios;
107 log_info("Updating SMBIOS table at %p\n", smbios_tab);
108 hdr = smbios_header(smbios_tab, SMBIOS_BIOS_INFORMATION);
110 return log_msg_ret("tab", -ENOENT);
111 bios = (struct smbios_type0 *)hdr;
112 ptr = (char *)smbios_string(hdr, bios->bios_ver);
114 return log_msg_ret("str", -ENOMEDIUM);
117 * This string is supposed to have at least enough bytes and is
118 * padded with spaces. Update it, taking care not to move the
119 * \0 terminator, so that other strings in the string table
120 * are not disturbed. See smbios_add_string()
122 old_len = strnlen(ptr, SMBIOS_STR_MAX);
123 len = strnlen(version, SMBIOS_STR_MAX);
125 return log_ret(-ENOSPC);
127 log_debug("Replacing SMBIOS type 0 version string '%s'\n", ptr);
128 memcpy(ptr, version, len);
130 print_buffer((ulong)ptr, ptr, 1, old_len + 1, 0);