OpenCore  1.0.4
OpenCore Bootloader
Loading...
Searching...
No Matches
OpenCoreDevProps.c
Go to the documentation of this file.
1
15#include <Library/OcMainLib.h>
16
17#include <Library/BaseLib.h>
18#include <Library/DebugLib.h>
19#include <Library/DevicePathLib.h>
20#include <Library/MemoryAllocationLib.h>
22#include <Library/OcMiscLib.h>
23#include <Library/OcStringLib.h>
24#include <Library/PrintLib.h>
25#include <Library/UefiBootServicesTableLib.h>
26
27#include <Protocol/DevicePath.h>
29
30VOID
32 IN OC_GLOBAL_CONFIG *Config
33 )
34{
35 EFI_STATUS Status;
36 UINT32 DeviceIndex;
37 UINT32 PropertyIndex;
39 OC_ASSOC *PropertyMap;
40 CHAR8 *AsciiDevicePath;
41 CHAR16 *UnicodeDevicePath;
42 CHAR8 *AsciiProperty;
43 CHAR16 *UnicodeProperty;
44 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
45 UINTN OriginalSize;
46
47 PropertyDatabase = OcDevicePathPropertyInstallProtocol (FALSE);
48 if (PropertyDatabase == NULL) {
49 DEBUG ((DEBUG_ERROR, "OC: Device property database protocol is missing\n"));
50 return;
51 }
52
53 for (DeviceIndex = 0; DeviceIndex < Config->DeviceProperties.Delete.Count; ++DeviceIndex) {
54 AsciiDevicePath = OC_BLOB_GET (Config->DeviceProperties.Delete.Keys[DeviceIndex]);
55 UnicodeDevicePath = AsciiStrCopyToUnicode (AsciiDevicePath, 0);
56 DevicePath = NULL;
57
58 if (UnicodeDevicePath != NULL) {
59 DevicePath = ConvertTextToDevicePath (UnicodeDevicePath);
60 FreePool (UnicodeDevicePath);
61 }
62
63 if (DevicePath == NULL) {
64 DEBUG ((DEBUG_WARN, "OC: Failed to parse %a device path\n", AsciiDevicePath));
65 continue;
66 }
67
68 for (PropertyIndex = 0; PropertyIndex < Config->DeviceProperties.Delete.Values[DeviceIndex]->Count; ++PropertyIndex) {
69 AsciiProperty = OC_BLOB_GET (Config->DeviceProperties.Delete.Values[DeviceIndex]->Values[PropertyIndex]);
70 //
71 // '#' is filtered in all keys, but for values we need to do it ourselves.
72 //
73 if (AsciiProperty[0] == '#') {
74 DEBUG ((DEBUG_INFO, "OC: Device property skip deleting %a\n", AsciiProperty));
75 continue;
76 }
77
78 UnicodeProperty = AsciiStrCopyToUnicode (AsciiProperty, 0);
79
80 if (UnicodeProperty == NULL) {
81 DEBUG ((DEBUG_WARN, "OC: Failed to convert %a property\n", AsciiProperty));
82 continue;
83 }
84
85 Status = PropertyDatabase->RemoveProperty (
86 PropertyDatabase,
87 DevicePath,
88 UnicodeProperty
89 );
90
91 DEBUG ((
92 EFI_ERROR (Status) && Status != EFI_NOT_FOUND ? DEBUG_WARN : DEBUG_INFO,
93 "OC: Removing devprop %a:%a - %r\n",
94 AsciiDevicePath,
95 AsciiProperty,
96 Status
97 ));
98
99 FreePool (UnicodeProperty);
100 }
101
102 FreePool (DevicePath);
103 }
104
105 for (DeviceIndex = 0; DeviceIndex < Config->DeviceProperties.Add.Count; ++DeviceIndex) {
106 PropertyMap = Config->DeviceProperties.Add.Values[DeviceIndex];
107 AsciiDevicePath = OC_BLOB_GET (Config->DeviceProperties.Add.Keys[DeviceIndex]);
108 UnicodeDevicePath = AsciiStrCopyToUnicode (AsciiDevicePath, 0);
109 DevicePath = NULL;
110
111 if (UnicodeDevicePath != NULL) {
112 DevicePath = ConvertTextToDevicePath (UnicodeDevicePath);
113 FreePool (UnicodeDevicePath);
114 }
115
116 if (DevicePath == NULL) {
117 DEBUG ((DEBUG_WARN, "OC: Failed to parse %a device path\n", AsciiDevicePath));
118 continue;
119 }
120
121 for (PropertyIndex = 0; PropertyIndex < PropertyMap->Count; ++PropertyIndex) {
122 AsciiProperty = OC_BLOB_GET (PropertyMap->Keys[PropertyIndex]);
123 UnicodeProperty = AsciiStrCopyToUnicode (AsciiProperty, 0);
124 if (UnicodeProperty == NULL) {
125 DEBUG ((DEBUG_WARN, "OC: Failed to convert %a property\n", AsciiProperty));
126 continue;
127 }
128
129 OriginalSize = 0;
130 Status = PropertyDatabase->GetProperty (
131 PropertyDatabase,
132 DevicePath,
133 UnicodeProperty,
134 NULL,
135 &OriginalSize
136 );
137
138 if (Status != EFI_BUFFER_TOO_SMALL) {
139 Status = PropertyDatabase->SetProperty (
140 PropertyDatabase,
141 DevicePath,
142 UnicodeProperty,
143 OC_BLOB_GET (PropertyMap->Values[PropertyIndex]),
144 PropertyMap->Values[PropertyIndex]->Size
145 );
146
147 DEBUG ((
148 EFI_ERROR (Status) ? DEBUG_WARN : DEBUG_INFO,
149 "OC: Setting devprop %a:%a - %r\n",
150 AsciiDevicePath,
151 AsciiProperty,
152 Status
153 ));
154 } else {
155 DEBUG ((
156 DEBUG_INFO,
157 "OC: Setting devprop %a:%a - ignored, exists\n",
158 AsciiDevicePath,
159 AsciiProperty
160 ));
161 }
162
163 FreePool (UnicodeProperty);
164 }
165
166 FreePool (DevicePath);
167 }
168}
EFI_DEVICE_PATH_PROPERTY_DATABASE_PROTOCOL * OcDevicePathPropertyInstallProtocol(IN BOOLEAN Reinstall)
CHAR16 * AsciiStrCopyToUnicode(IN CONST CHAR8 *String, IN UINTN Length)
Definition OcAsciiLib.c:119
#define OC_BLOB_GET(Blob)
VOID OcLoadDevPropsSupport(IN OC_GLOBAL_CONFIG *Config)
The structure exposed by the EFI_DEVICE_PATH_PROPERTY_DATABASE_PROTOCOL.
DPP_DATABASE_GET_PROPERTY GetProperty
Locates a device property in the database and returns its value into Value.
DPP_DATABASE_SET_PROPERTY SetProperty
Sets the sepcified property of the given device path to the provided Value.
DPP_DATABASE_REMOVE_PROPERTY RemoveProperty
Removes the sepcified property from the given device path.