| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <stdio.h> |
| 4 |
|
| 5 |
#include "alloc-util.h" |
| 6 |
#include "fileio.h" |
| 7 |
#include "smbios11.h" |
| 8 |
#include "virt.h" |
| 9 |
|
| 10 |
int read_smbios11_field(unsigned i, size_t max_size, char **ret_data, size_t *ret_size) { |
| 11 |
_cleanup_free_ char *p = NULL, *contents = NULL; |
| 12 |
_cleanup_free_ void *data = NULL; |
| 13 |
size_t size, contents_size; |
| 14 |
int r; |
| 15 |
|
| 16 |
assert(ret_data); |
| 17 |
assert(ret_size); |
| 18 |
|
| 19 |
/* Parses DMI OEM strings fields (SMBIOS type 11), as settable with qemu's -smbios type=11,value=… switch. */ |
| 20 |
|
| 21 |
if (detect_container() > 0) /* don't access /sys/ in a container */ |
| 22 |
return -ENOENT; |
| 23 |
|
| 24 |
if (asprintf(&p, "/sys/firmware/dmi/entries/11-%u/raw", i) < 0) |
| 25 |
return -ENOMEM; |
| 26 |
|
| 27 |
struct dmi_field_header { |
| 28 |
uint8_t type; |
| 29 |
uint8_t length; |
| 30 |
uint16_t handle; |
| 31 |
uint8_t count; |
| 32 |
char contents[]; |
| 33 |
} _packed_ *dmi_field_header; |
| 34 |
|
| 35 |
assert_cc(offsetof(struct dmi_field_header, contents) == 5); |
| 36 |
|
| 37 |
/* We don't use read_virtual_file() because it only reads a single page of bytes from the DMI sysfs |
| 38 |
* file. Since the SMBIOS data is immutable after boot, it's safe to use read_full_file_full() here. */ |
| 39 |
r = read_full_file_full( |
| 40 |
AT_FDCWD, p, |
| 41 |
/* offset= */ UINT64_MAX, |
| 42 |
size_add(offsetof(struct dmi_field_header, contents), max_size), |
| 43 |
/* flags= */ 0, |
| 44 |
/* bind_name= */ NULL, |
| 45 |
(char**) &data, &size); |
| 46 |
if (r < 0) |
| 47 |
return r; |
| 48 |
|
| 49 |
if (size < offsetof(struct dmi_field_header, contents)) |
| 50 |
return -EBADMSG; |
| 51 |
|
| 52 |
dmi_field_header = data; |
| 53 |
if (dmi_field_header->type != 11 || |
| 54 |
dmi_field_header->length != offsetof(struct dmi_field_header, contents)) |
| 55 |
return -EBADMSG; |
| 56 |
|
| 57 |
contents_size = size - offsetof(struct dmi_field_header, contents); |
| 58 |
contents = memdup_suffix0(dmi_field_header->contents, contents_size); |
| 59 |
if (!contents) |
| 60 |
return -ENOMEM; |
| 61 |
|
| 62 |
*ret_data = TAKE_PTR(contents); |
| 63 |
*ret_size = contents_size; |
| 64 |
|
| 65 |
return r; /* NB! read_virtual_file() returns 0 on incomplete reads, and 1 in complete reads */ |
| 66 |
} |