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