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