Christian Gmeiner | 415eab0 | 2020-11-03 15:34:51 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (C) 2020, Bachmann electronic GmbH |
| 4 | */ |
| 5 | |
Simon Glass | 272e62c | 2021-03-15 18:00:11 +1300 | [diff] [blame] | 6 | #define LOG_CATEGORY LOGC_BOOT |
| 7 | |
Christian Gmeiner | 415eab0 | 2020-11-03 15:34:51 +0100 | [diff] [blame] | 8 | #include <common.h> |
| 9 | #include <smbios.h> |
| 10 | |
| 11 | static inline int verify_checksum(const struct smbios_entry *e) |
| 12 | { |
| 13 | /* |
| 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). |
| 16 | */ |
| 17 | u8 *byte = (u8 *)e; |
| 18 | u8 sum = 0; |
| 19 | |
| 20 | for (int i = 0; i < e->length; i++) |
| 21 | sum += byte[i]; |
| 22 | |
| 23 | return sum; |
| 24 | } |
| 25 | |
| 26 | const struct smbios_entry *smbios_entry(u64 address, u32 size) |
| 27 | { |
| 28 | const struct smbios_entry *entry = (struct smbios_entry *)(uintptr_t)address; |
| 29 | |
| 30 | if (!address | !size) |
| 31 | return NULL; |
| 32 | |
| 33 | if (memcmp(entry->anchor, "_SM_", 4)) |
| 34 | return NULL; |
| 35 | |
| 36 | if (verify_checksum(entry)) |
| 37 | return NULL; |
| 38 | |
| 39 | return entry; |
| 40 | } |
| 41 | |
| 42 | static const struct smbios_header *next_header(const struct smbios_header *curr) |
| 43 | { |
| 44 | u8 *pos = ((u8 *)curr) + curr->length; |
| 45 | |
| 46 | /* search for _double_ NULL bytes */ |
| 47 | while (!((*pos == 0) && (*(pos + 1) == 0))) |
| 48 | pos++; |
| 49 | |
| 50 | /* step behind the double NULL bytes */ |
| 51 | pos += 2; |
| 52 | |
| 53 | return (struct smbios_header *)pos; |
| 54 | } |
| 55 | |
| 56 | const struct smbios_header *smbios_header(const struct smbios_entry *entry, int type) |
| 57 | { |
| 58 | const unsigned int num_header = entry->struct_count; |
| 59 | const struct smbios_header *header = (struct smbios_header *)entry->struct_table_address; |
| 60 | |
| 61 | for (unsigned int i = 0; i < num_header; i++) { |
| 62 | if (header->type == type) |
| 63 | return header; |
| 64 | |
| 65 | header = next_header(header); |
| 66 | } |
| 67 | |
| 68 | return NULL; |
| 69 | } |
| 70 | |
| 71 | static const char *string_from_smbios_table(const struct smbios_header *header, |
| 72 | int idx) |
| 73 | { |
| 74 | unsigned int i = 1; |
| 75 | u8 *pos; |
| 76 | |
| 77 | if (!header) |
| 78 | return NULL; |
| 79 | |
| 80 | pos = ((u8 *)header) + header->length; |
| 81 | |
| 82 | while (i < idx) { |
| 83 | if (*pos == 0x0) |
| 84 | i++; |
| 85 | |
| 86 | pos++; |
| 87 | } |
| 88 | |
| 89 | return (const char *)pos; |
| 90 | } |
| 91 | |
| 92 | const char *smbios_string(const struct smbios_header *header, int index) |
| 93 | { |
| 94 | if (!header) |
| 95 | return NULL; |
| 96 | |
| 97 | return string_from_smbios_table(header, index); |
| 98 | } |
Simon Glass | 272e62c | 2021-03-15 18:00:11 +1300 | [diff] [blame] | 99 | |
| 100 | int smbios_update_version_full(void *smbios_tab, const char *version) |
| 101 | { |
| 102 | const struct smbios_header *hdr; |
| 103 | struct smbios_type0 *bios; |
| 104 | uint old_len, len; |
| 105 | char *ptr; |
| 106 | |
| 107 | log_info("Updating SMBIOS table at %p\n", smbios_tab); |
| 108 | hdr = smbios_header(smbios_tab, SMBIOS_BIOS_INFORMATION); |
| 109 | if (!hdr) |
| 110 | return log_msg_ret("tab", -ENOENT); |
| 111 | bios = (struct smbios_type0 *)hdr; |
| 112 | ptr = (char *)smbios_string(hdr, bios->bios_ver); |
| 113 | if (!ptr) |
| 114 | return log_msg_ret("str", -ENOMEDIUM); |
| 115 | |
| 116 | /* |
| 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() |
| 121 | */ |
| 122 | old_len = strnlen(ptr, SMBIOS_STR_MAX); |
| 123 | len = strnlen(version, SMBIOS_STR_MAX); |
| 124 | if (len > old_len) |
| 125 | return log_ret(-ENOSPC); |
| 126 | |
| 127 | log_debug("Replacing SMBIOS type 0 version string '%s'\n", ptr); |
| 128 | memcpy(ptr, version, len); |
| 129 | #ifdef LOG_DEBUG |
| 130 | print_buffer((ulong)ptr, ptr, 1, old_len + 1, 0); |
| 131 | #endif |
| 132 | |
| 133 | return 0; |
| 134 | } |