1 | /* $Id: RTSystemFirmware-linux.cpp 81065 2019-09-28 00:12:32Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - System firmware information, linux.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2019 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #include "internal/iprt.h"
|
---|
32 | #include <iprt/system.h>
|
---|
33 |
|
---|
34 | #include <iprt/err.h>
|
---|
35 | #include <iprt/file.h>
|
---|
36 | #include <iprt/string.h>
|
---|
37 | #include <iprt/linux/sysfs.h>
|
---|
38 |
|
---|
39 |
|
---|
40 | /*********************************************************************************************************************************
|
---|
41 | * Defined Constants And Macros *
|
---|
42 | *********************************************************************************************************************************/
|
---|
43 | /** Defines the UEFI Globals UUID that is used here as filename suffix (case sensitive). */
|
---|
44 | #define VBOX_UEFI_UUID_GLOBALS "8be4df61-93ca-11d2-aa0d-00e098032b8c"
|
---|
45 |
|
---|
46 |
|
---|
47 | RTDECL(int) RTSystemFirmwareQueryType(PRTSYSFWTYPE penmFirmwareType)
|
---|
48 | {
|
---|
49 | if (RTLinuxSysFsExists("firmware/efi/"))
|
---|
50 | *penmFirmwareType = RTSYSFWTYPE_UEFI;
|
---|
51 | else if (RTLinuxSysFsExists(""))
|
---|
52 | *penmFirmwareType = RTSYSFWTYPE_BIOS;
|
---|
53 | else
|
---|
54 | {
|
---|
55 | *penmFirmwareType = RTSYSFWTYPE_INVALID;
|
---|
56 | return VERR_NOT_SUPPORTED;
|
---|
57 | }
|
---|
58 | return VINF_SUCCESS;
|
---|
59 | }
|
---|
60 | RT_EXPORT_SYMBOL(RTSystemFirmwareQueryType);
|
---|
61 |
|
---|
62 |
|
---|
63 | RTDECL(void) RTSystemFirmwareFreeValue(PRTSYSFWVALUE pValue)
|
---|
64 | {
|
---|
65 | RT_NOREF(pValue);
|
---|
66 | }
|
---|
67 | RT_EXPORT_SYMBOL(RTSystemFirmwareFreeValue);
|
---|
68 |
|
---|
69 |
|
---|
70 | RTDECL(int) RTSystemFirmwareQueryValue(RTSYSFWPROP enmProp, PRTSYSFWVALUE pValue)
|
---|
71 | {
|
---|
72 | RT_ZERO(*pValue);
|
---|
73 |
|
---|
74 | /*
|
---|
75 | * Translate the property into type and variable base filename.
|
---|
76 | */
|
---|
77 | const char *pszName;
|
---|
78 | switch (enmProp)
|
---|
79 | {
|
---|
80 | case RTSYSFWPROP_SECURE_BOOT:
|
---|
81 | pValue->enmType = RTSYSFWVALUETYPE_BOOLEAN;
|
---|
82 | pszName = "firmware/efi/efivars/SecureBoot";
|
---|
83 | break;
|
---|
84 |
|
---|
85 | default:
|
---|
86 | AssertReturn(enmProp > RTSYSFWPROP_INVALID && enmProp < RTSYSFWPROP_END, VERR_INVALID_PARAMETER);
|
---|
87 | return VERR_SYS_UNSUPPORTED_FIRMWARE_PROPERTY;
|
---|
88 |
|
---|
89 | }
|
---|
90 |
|
---|
91 | /*
|
---|
92 | * Try open and read the variable value.
|
---|
93 | */
|
---|
94 | RTFILE hFile;
|
---|
95 | int rc = RTLinuxSysFsOpen(&hFile, "%s-" VBOX_UEFI_UUID_GLOBALS, pszName);
|
---|
96 | /** @todo try other suffixes if file-not-found. */
|
---|
97 |
|
---|
98 | switch (pValue->enmType)
|
---|
99 | {
|
---|
100 | case RTSYSFWVALUETYPE_BOOLEAN:
|
---|
101 | {
|
---|
102 | if (RT_SUCCESS(rc))
|
---|
103 | {
|
---|
104 | uint8_t abBuf[16];
|
---|
105 | size_t cbRead = 0;
|
---|
106 | rc = RTLinuxSysFsReadFile(hFile, abBuf, sizeof(abBuf), &cbRead);
|
---|
107 | pValue->u.fVal = cbRead > 1 && abBuf[cbRead - 1] != 0;
|
---|
108 | RTFileClose(hFile);
|
---|
109 | }
|
---|
110 | else if (rc == VERR_PERMISSION_DENIED)
|
---|
111 | rc = VINF_SUCCESS;
|
---|
112 | break;
|
---|
113 | }
|
---|
114 |
|
---|
115 | default:
|
---|
116 | AssertFailedReturn(VERR_INTERNAL_ERROR);
|
---|
117 | }
|
---|
118 |
|
---|
119 | return rc;
|
---|
120 | }
|
---|
121 | RT_EXPORT_SYMBOL(RTSystemFirmwareQueryValue);
|
---|
122 |
|
---|