xref: /linux/drivers/firmware/efi/libstub/smbios.c (revision e65e175b07bef5974045cc42238de99057669ca7)
1 // SPDX-License-Identifier: GPL-2.0-only
2 // Copyright 2022 Google LLC
3 // Author: Ard Biesheuvel <ardb@google.com>
4 
5 #include <linux/efi.h>
6 
7 #include "efistub.h"
8 
9 typedef struct efi_smbios_protocol efi_smbios_protocol_t;
10 
11 struct efi_smbios_protocol {
12 	efi_status_t (__efiapi *add)(efi_smbios_protocol_t *, efi_handle_t,
13 				     u16 *, struct efi_smbios_record *);
14 	efi_status_t (__efiapi *update_string)(efi_smbios_protocol_t *, u16 *,
15 					       unsigned long *, u8 *);
16 	efi_status_t (__efiapi *remove)(efi_smbios_protocol_t *, u16);
17 	efi_status_t (__efiapi *get_next)(efi_smbios_protocol_t *, u16 *, u8 *,
18 					  struct efi_smbios_record **,
19 					  efi_handle_t *);
20 
21 	u8 major_version;
22 	u8 minor_version;
23 };
24 
25 const u8 *__efi_get_smbios_string(u8 type, int offset, int recsize)
26 {
27 	struct efi_smbios_record *record;
28 	efi_smbios_protocol_t *smbios;
29 	efi_status_t status;
30 	u16 handle = 0xfffe;
31 	const u8 *strtable;
32 
33 	status = efi_bs_call(locate_protocol, &EFI_SMBIOS_PROTOCOL_GUID, NULL,
34 			     (void **)&smbios) ?:
35 		 efi_call_proto(smbios, get_next, &handle, &type, &record, NULL);
36 	if (status != EFI_SUCCESS)
37 		return NULL;
38 
39 	strtable = (u8 *)record + recsize;
40 	for (int i = 1; i < ((u8 *)record)[offset]; i++) {
41 		int len = strlen(strtable);
42 
43 		if (!len)
44 			return NULL;
45 		strtable += len + 1;
46 	}
47 	return strtable;
48 }
49