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