]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Library/ResetUtilityLib/ResetUtility.c
MdeModulePkg/ResetUtilityLib: Use STATIC_ASSERT macro
[mirror_edk2.git] / MdeModulePkg / Library / ResetUtilityLib / ResetUtility.c
CommitLineData
e12ceb40
MK
1/** @file\r
2 This contains the business logic for the module-specific Reset Helper functions.\r
3\r
0851d7a5 4 Copyright (c) 2017 - 2019 Intel Corporation. All rights reserved.<BR>\r
e12ceb40
MK
5 Copyright (c) 2016 Microsoft Corporation. All rights reserved.<BR>\r
6\r
9d510e61 7 SPDX-License-Identifier: BSD-2-Clause-Patent\r
e12ceb40
MK
8\r
9**/\r
10#include <Uefi.h>\r
11#include <Library/BaseLib.h>\r
12#include <Library/DebugLib.h>\r
13#include <Library/BaseMemoryLib.h>\r
14#include <Library/ResetSystemLib.h>\r
15\r
93f5a54f 16#pragma pack(1)\r
e12ceb40
MK
17typedef struct {\r
18 CHAR16 NullTerminator;\r
19 GUID ResetSubtype;\r
20} RESET_UTILITY_GUID_SPECIFIC_RESET_DATA;\r
93f5a54f
RN
21#pragma pack()\r
22\r
a9521a6b
VC
23STATIC_ASSERT (\r
24 sizeof (RESET_UTILITY_GUID_SPECIFIC_RESET_DATA) == 18,\r
25 "sizeof (RESET_UTILITY_GUID_SPECIFIC_RESET_DATA) is expected to be 18 bytes"\r
26 );\r
e12ceb40
MK
27\r
28/**\r
0851d7a5
BB
29 This is a shorthand helper function to reset with reset type and a subtype\r
30 so that the caller doesn't have to bother with a function that has half\r
31 a dozen parameters.\r
e12ceb40
MK
32\r
33 This will generate a reset with status EFI_SUCCESS, a NULL string, and\r
34 no custom data. The subtype will be formatted in such a way that it can be\r
35 picked up by notification registrations and custom handlers.\r
36\r
37 NOTE: This call will fail if the architectural ResetSystem underpinnings\r
38 are not initialized. For DXE, you can add gEfiResetArchProtocolGuid\r
39 to your DEPEX.\r
40\r
0851d7a5 41 @param[in] ResetType The default EFI_RESET_TYPE of the reset.\r
e12ceb40
MK
42 @param[in] ResetSubtype GUID pointer for the reset subtype to be used.\r
43\r
44**/\r
45VOID\r
46EFIAPI\r
0851d7a5
BB
47ResetSystemWithSubtype (\r
48 IN EFI_RESET_TYPE ResetType,\r
e12ceb40
MK
49 IN CONST GUID *ResetSubtype\r
50 )\r
51{\r
52 RESET_UTILITY_GUID_SPECIFIC_RESET_DATA ResetData;\r
53\r
54 ResetData.NullTerminator = CHAR_NULL;\r
93f5a54f
RN
55 CopyGuid (\r
56 (GUID *)((UINT8 *)&ResetData + OFFSET_OF (RESET_UTILITY_GUID_SPECIFIC_RESET_DATA, ResetSubtype)),\r
57 ResetSubtype\r
58 );\r
0851d7a5
BB
59\r
60 ResetSystem (ResetType, EFI_SUCCESS, sizeof (ResetData), &ResetData);\r
61}\r
62\r
63/**\r
64 This is a shorthand helper function to reset with the reset type\r
65 'EfiResetPlatformSpecific' and a subtype so that the caller doesn't\r
66 have to bother with a function that has half a dozen parameters.\r
67\r
68 This will generate a reset with status EFI_SUCCESS, a NULL string, and\r
69 no custom data. The subtype will be formatted in such a way that it can be\r
70 picked up by notification registrations and custom handlers.\r
71\r
72 NOTE: This call will fail if the architectural ResetSystem underpinnings\r
73 are not initialized. For DXE, you can add gEfiResetArchProtocolGuid\r
74 to your DEPEX.\r
75\r
76 @param[in] ResetSubtype GUID pointer for the reset subtype to be used.\r
77\r
78**/\r
79VOID\r
80EFIAPI\r
81ResetPlatformSpecificGuid (\r
82 IN CONST GUID *ResetSubtype\r
83 )\r
84{\r
85 ResetSystemWithSubtype (EfiResetPlatformSpecific, ResetSubtype);\r
e12ceb40
MK
86}\r
87\r
88/**\r
89 This function examines the DataSize and ResetData parameters passed to\r
90 to ResetSystem() and detemrines if the ResetData contains a Null-terminated\r
d1102dba 91 Unicode string followed by a GUID specific subtype. If the GUID specific\r
e12ceb40
MK
92 subtype is present, then a pointer to the GUID value in ResetData is returned.\r
93\r
94 @param[in] DataSize The size, in bytes, of ResetData.\r
95 @param[in] ResetData Pointer to the data buffer passed into ResetSystem().\r
96\r
97 @retval Pointer Pointer to the GUID value in ResetData.\r
98 @retval NULL ResetData is NULL.\r
99 @retval NULL ResetData does not start with a Null-terminated\r
100 Unicode string.\r
101 @retval NULL A Null-terminated Unicode string is present, but there\r
102 are less than sizeof (GUID) bytes after the string.\r
103 @retval NULL No subtype is found.\r
104\r
105**/\r
106GUID *\r
107EFIAPI\r
108GetResetPlatformSpecificGuid (\r
109 IN UINTN DataSize,\r
110 IN CONST VOID *ResetData\r
111 )\r
112{\r
113 UINTN ResetDataStringSize;\r
114 GUID *ResetSubtypeGuid;\r
115\r
116 //\r
117 // Make sure parameters are valid\r
118 //\r
119 if ((ResetData == NULL) || (DataSize < sizeof (GUID))) {\r
120 return NULL;\r
121 }\r
122\r
123 //\r
124 // Determine the number of bytes in the Null-terminated Unicode string\r
125 // at the beginning of ResetData including the Null terminator.\r
126 //\r
127 ResetDataStringSize = StrnSizeS (ResetData, (DataSize / sizeof (CHAR16)));\r
128\r
129 //\r
130 // Now, assuming that we have enough data for a GUID after the string, the\r
131 // GUID should be immediately after the string itself.\r
132 //\r
133 if ((ResetDataStringSize < DataSize) && (DataSize - ResetDataStringSize) >= sizeof (GUID)) {\r
134 ResetSubtypeGuid = (GUID *)((UINT8 *)ResetData + ResetDataStringSize);\r
5eecb45a 135 DEBUG ((DEBUG_VERBOSE, "%a - Detected reset subtype %g...\n", __FUNCTION__, ResetSubtypeGuid));\r
e12ceb40
MK
136 return ResetSubtypeGuid;\r
137 }\r
138 return NULL;\r
139}\r
140\r
141/**\r
d1102dba 142 This is a helper function that creates the reset data buffer that can be\r
e12ceb40
MK
143 passed into ResetSystem().\r
144\r
145 The reset data buffer is returned in ResetData and contains ResetString\r
146 followed by the ResetSubtype GUID followed by the ExtraData.\r
147\r
148 NOTE: Strings are internally limited by MAX_UINT16.\r
149\r
150 @param[in, out] ResetDataSize On input, the size of the ResetData buffer. On\r
151 output, either the total number of bytes\r
152 copied, or the required buffer size.\r
153 @param[in, out] ResetData A pointer to the buffer in which to place the\r
154 final structure.\r
155 @param[in] ResetSubtype Pointer to the GUID specific subtype. This\r
156 parameter is optional and may be NULL.\r
157 @param[in] ResetString Pointer to a Null-terminated Unicode string\r
158 that describes the reset. This parameter is\r
159 optional and may be NULL.\r
160 @param[in] ExtraDataSize The size, in bytes, of ExtraData buffer.\r
161 @param[in] ExtraData Pointer to a buffer of extra data. This\r
162 parameter is optional and may be NULL.\r
163\r
164 @retval RETURN_SUCCESS ResetDataSize and ResetData are updated.\r
165 @retval RETURN_INVALID_PARAMETER ResetDataSize is NULL.\r
166 @retval RETURN_INVALID_PARAMETER ResetData is NULL.\r
167 @retval RETURN_INVALID_PARAMETER ExtraData was provided without a\r
168 ResetSubtype. This is not supported by the\r
169 UEFI spec.\r
170 @retval RETURN_BUFFER_TOO_SMALL An insufficient buffer was provided.\r
171 ResetDataSize is updated with minimum size\r
172 required.\r
173**/\r
174RETURN_STATUS\r
175EFIAPI\r
176BuildResetData (\r
177 IN OUT UINTN *ResetDataSize,\r
178 IN OUT VOID *ResetData,\r
179 IN CONST GUID *ResetSubtype OPTIONAL,\r
180 IN CONST CHAR16 *ResetString OPTIONAL,\r
181 IN UINTN ExtraDataSize OPTIONAL,\r
182 IN CONST VOID *ExtraData OPTIONAL\r
183 )\r
184{\r
185 UINTN ResetStringSize;\r
186 UINTN ResetDataBufferSize;\r
187 UINT8 *Data;\r
188\r
189 //\r
190 // If the size return pointer is NULL.\r
191 //\r
192 if (ResetDataSize == NULL) {\r
193 return RETURN_INVALID_PARAMETER;\r
194 }\r
195 //\r
196 // If extra data is indicated, but pointer is NULL.\r
197 //\r
198 if (ExtraDataSize > 0 && ExtraData == NULL) {\r
199 return RETURN_INVALID_PARAMETER;\r
200 }\r
201 //\r
202 // If extra data is indicated, but no subtype GUID is supplied.\r
203 //\r
204 if (ResetSubtype == NULL && ExtraDataSize > 0) {\r
205 return RETURN_INVALID_PARAMETER;\r
206 }\r
207\r
208 //\r
209 // Determine the final string.\r
210 //\r
211 if (ResetString == NULL) {\r
212 ResetString = L""; // Use an empty string.\r
213 }\r
d1102dba 214\r
e12ceb40
MK
215 //\r
216 // Calculate the total buffer required for ResetData.\r
217 //\r
218 ResetStringSize = StrnSizeS (ResetString, MAX_UINT16);\r
219 ResetDataBufferSize = ResetStringSize + ExtraDataSize;\r
220 if (ResetSubtype != NULL) {\r
221 ResetDataBufferSize += sizeof (GUID);\r
222 }\r
223\r
224 //\r
225 // At this point, if the buffer isn't large enough (or if\r
226 // the buffer is NULL) we cannot proceed.\r
227 //\r
228 if (*ResetDataSize < ResetDataBufferSize) {\r
229 *ResetDataSize = ResetDataBufferSize;\r
230 return RETURN_BUFFER_TOO_SMALL;\r
231 }\r
232 *ResetDataSize = ResetDataBufferSize;\r
233 if (ResetData == NULL) {\r
234 return RETURN_INVALID_PARAMETER;\r
235 }\r
236\r
237 //\r
238 // Fill in ResetData with ResetString, the ResetSubtype GUID, and extra data\r
239 //\r
240 Data = (UINT8 *)ResetData;\r
241 CopyMem (Data, ResetString, ResetStringSize);\r
242 Data += ResetStringSize;\r
243 if (ResetSubtype != NULL) {\r
244 CopyMem (Data, ResetSubtype, sizeof (GUID));\r
245 Data += sizeof (GUID);\r
246 }\r
247 if (ExtraDataSize > 0) {\r
248 CopyMem (Data, ExtraData, ExtraDataSize);\r
249 }\r
d1102dba 250\r
e12ceb40
MK
251 return RETURN_SUCCESS;\r
252}\r