1 | /* $Id: RTSystemFirmware-linux.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - System firmware information, linux.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2019-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Header Files *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | #include "internal/iprt.h"
|
---|
42 | #include <iprt/system.h>
|
---|
43 |
|
---|
44 | #include <iprt/err.h>
|
---|
45 | #include <iprt/file.h>
|
---|
46 | #include <iprt/string.h>
|
---|
47 | #include <iprt/linux/sysfs.h>
|
---|
48 |
|
---|
49 |
|
---|
50 | /*********************************************************************************************************************************
|
---|
51 | * Defined Constants And Macros *
|
---|
52 | *********************************************************************************************************************************/
|
---|
53 | /** Defines the UEFI Globals UUID that is used here as filename suffix (case sensitive). */
|
---|
54 | #define VBOX_UEFI_UUID_GLOBALS "8be4df61-93ca-11d2-aa0d-00e098032b8c"
|
---|
55 |
|
---|
56 |
|
---|
57 | RTDECL(int) RTSystemQueryFirmwareType(PRTSYSFWTYPE penmFirmwareType)
|
---|
58 | {
|
---|
59 | if (RTLinuxSysFsExists("firmware/efi/"))
|
---|
60 | *penmFirmwareType = RTSYSFWTYPE_UEFI;
|
---|
61 | else if (RTLinuxSysFsExists(""))
|
---|
62 | *penmFirmwareType = RTSYSFWTYPE_BIOS;
|
---|
63 | else
|
---|
64 | {
|
---|
65 | *penmFirmwareType = RTSYSFWTYPE_INVALID;
|
---|
66 | return VERR_NOT_SUPPORTED;
|
---|
67 | }
|
---|
68 | return VINF_SUCCESS;
|
---|
69 | }
|
---|
70 | RT_EXPORT_SYMBOL(RTSystemQueryFirmwareType);
|
---|
71 |
|
---|
72 |
|
---|
73 | RTDECL(int) RTSystemQueryFirmwareBoolean(RTSYSFWBOOL enmBoolean, bool *pfValue)
|
---|
74 | {
|
---|
75 | *pfValue = false;
|
---|
76 |
|
---|
77 | /*
|
---|
78 | * Translate the property to variable base filename.
|
---|
79 | */
|
---|
80 | const char *pszName;
|
---|
81 | switch (enmBoolean)
|
---|
82 | {
|
---|
83 | case RTSYSFWBOOL_SECURE_BOOT:
|
---|
84 | pszName = "firmware/efi/efivars/SecureBoot";
|
---|
85 | break;
|
---|
86 |
|
---|
87 | default:
|
---|
88 | AssertReturn(enmBoolean > RTSYSFWBOOL_INVALID && enmBoolean < RTSYSFWBOOL_END, VERR_INVALID_PARAMETER);
|
---|
89 | return VERR_SYS_UNSUPPORTED_FIRMWARE_PROPERTY;
|
---|
90 |
|
---|
91 | }
|
---|
92 |
|
---|
93 | /*
|
---|
94 | * Try open and read the variable value.
|
---|
95 | */
|
---|
96 | RTFILE hFile;
|
---|
97 | int rc = RTLinuxSysFsOpen(&hFile, "%s-" VBOX_UEFI_UUID_GLOBALS, pszName);
|
---|
98 | /** @todo try other suffixes if file-not-found. */
|
---|
99 | if (RT_SUCCESS(rc))
|
---|
100 | {
|
---|
101 | uint8_t abBuf[16];
|
---|
102 | size_t cbRead = 0;
|
---|
103 | rc = RTLinuxSysFsReadFile(hFile, abBuf, sizeof(abBuf), &cbRead);
|
---|
104 | *pfValue = cbRead > 1 && abBuf[cbRead - 1] != 0;
|
---|
105 | RTFileClose(hFile);
|
---|
106 | }
|
---|
107 | else if (rc == VERR_FILE_NOT_FOUND || rc == VERR_PATH_NOT_FOUND)
|
---|
108 | rc = VINF_SUCCESS;
|
---|
109 | else if (rc == VERR_PERMISSION_DENIED)
|
---|
110 | rc = VERR_NOT_SUPPORTED;
|
---|
111 |
|
---|
112 | return rc;
|
---|
113 | }
|
---|
114 | RT_EXPORT_SYMBOL(RTSystemQueryFirmwareBoolean);
|
---|
115 |
|
---|