2 This contains the business logic for the module-specific Reset Helper functions.
4 Copyright (c) 2017 - 2019 Intel Corporation. All rights reserved.<BR>
5 Copyright (c) 2016 Microsoft Corporation. All rights reserved.<BR>
7 SPDX-License-Identifier: BSD-2-Clause-Patent
11 #include <Library/BaseLib.h>
12 #include <Library/DebugLib.h>
13 #include <Library/BaseMemoryLib.h>
14 #include <Library/ResetSystemLib.h>
18 CHAR16 NullTerminator
;
20 } RESET_UTILITY_GUID_SPECIFIC_RESET_DATA
;
24 sizeof (RESET_UTILITY_GUID_SPECIFIC_RESET_DATA
) == 18,
25 "sizeof (RESET_UTILITY_GUID_SPECIFIC_RESET_DATA) is expected to be 18 bytes"
29 This is a shorthand helper function to reset with reset type and a subtype
30 so that the caller doesn't have to bother with a function that has half
33 This will generate a reset with status EFI_SUCCESS, a NULL string, and
34 no custom data. The subtype will be formatted in such a way that it can be
35 picked up by notification registrations and custom handlers.
37 NOTE: This call will fail if the architectural ResetSystem underpinnings
38 are not initialized. For DXE, you can add gEfiResetArchProtocolGuid
41 @param[in] ResetType The default EFI_RESET_TYPE of the reset.
42 @param[in] ResetSubtype GUID pointer for the reset subtype to be used.
47 ResetSystemWithSubtype (
48 IN EFI_RESET_TYPE ResetType
,
49 IN CONST GUID
*ResetSubtype
52 RESET_UTILITY_GUID_SPECIFIC_RESET_DATA ResetData
;
54 ResetData
.NullTerminator
= CHAR_NULL
;
56 (GUID
*)((UINT8
*)&ResetData
+ OFFSET_OF (RESET_UTILITY_GUID_SPECIFIC_RESET_DATA
, ResetSubtype
)),
60 ResetSystem (ResetType
, EFI_SUCCESS
, sizeof (ResetData
), &ResetData
);
64 This is a shorthand helper function to reset with the reset type
65 'EfiResetPlatformSpecific' and a subtype so that the caller doesn't
66 have to bother with a function that has half a dozen parameters.
68 This will generate a reset with status EFI_SUCCESS, a NULL string, and
69 no custom data. The subtype will be formatted in such a way that it can be
70 picked up by notification registrations and custom handlers.
72 NOTE: This call will fail if the architectural ResetSystem underpinnings
73 are not initialized. For DXE, you can add gEfiResetArchProtocolGuid
76 @param[in] ResetSubtype GUID pointer for the reset subtype to be used.
81 ResetPlatformSpecificGuid (
82 IN CONST GUID
*ResetSubtype
85 ResetSystemWithSubtype (EfiResetPlatformSpecific
, ResetSubtype
);
89 This function examines the DataSize and ResetData parameters passed to
90 to ResetSystem() and detemrines if the ResetData contains a Null-terminated
91 Unicode string followed by a GUID specific subtype. If the GUID specific
92 subtype is present, then a pointer to the GUID value in ResetData is returned.
94 @param[in] DataSize The size, in bytes, of ResetData.
95 @param[in] ResetData Pointer to the data buffer passed into ResetSystem().
97 @retval Pointer Pointer to the GUID value in ResetData.
98 @retval NULL ResetData is NULL.
99 @retval NULL ResetData does not start with a Null-terminated
101 @retval NULL A Null-terminated Unicode string is present, but there
102 are less than sizeof (GUID) bytes after the string.
103 @retval NULL No subtype is found.
108 GetResetPlatformSpecificGuid (
110 IN CONST VOID
*ResetData
113 UINTN ResetDataStringSize
;
114 GUID
*ResetSubtypeGuid
;
117 // Make sure parameters are valid
119 if ((ResetData
== NULL
) || (DataSize
< sizeof (GUID
))) {
124 // Determine the number of bytes in the Null-terminated Unicode string
125 // at the beginning of ResetData including the Null terminator.
127 ResetDataStringSize
= StrnSizeS (ResetData
, (DataSize
/ sizeof (CHAR16
)));
130 // Now, assuming that we have enough data for a GUID after the string, the
131 // GUID should be immediately after the string itself.
133 if ((ResetDataStringSize
< DataSize
) && (DataSize
- ResetDataStringSize
) >= sizeof (GUID
)) {
134 ResetSubtypeGuid
= (GUID
*)((UINT8
*)ResetData
+ ResetDataStringSize
);
135 DEBUG ((DEBUG_VERBOSE
, "%a - Detected reset subtype %g...\n", __FUNCTION__
, ResetSubtypeGuid
));
136 return ResetSubtypeGuid
;
142 This is a helper function that creates the reset data buffer that can be
143 passed into ResetSystem().
145 The reset data buffer is returned in ResetData and contains ResetString
146 followed by the ResetSubtype GUID followed by the ExtraData.
148 NOTE: Strings are internally limited by MAX_UINT16.
150 @param[in, out] ResetDataSize On input, the size of the ResetData buffer. On
151 output, either the total number of bytes
152 copied, or the required buffer size.
153 @param[in, out] ResetData A pointer to the buffer in which to place the
155 @param[in] ResetSubtype Pointer to the GUID specific subtype. This
156 parameter is optional and may be NULL.
157 @param[in] ResetString Pointer to a Null-terminated Unicode string
158 that describes the reset. This parameter is
159 optional and may be NULL.
160 @param[in] ExtraDataSize The size, in bytes, of ExtraData buffer.
161 @param[in] ExtraData Pointer to a buffer of extra data. This
162 parameter is optional and may be NULL.
164 @retval RETURN_SUCCESS ResetDataSize and ResetData are updated.
165 @retval RETURN_INVALID_PARAMETER ResetDataSize is NULL.
166 @retval RETURN_INVALID_PARAMETER ResetData is NULL.
167 @retval RETURN_INVALID_PARAMETER ExtraData was provided without a
168 ResetSubtype. This is not supported by the
170 @retval RETURN_BUFFER_TOO_SMALL An insufficient buffer was provided.
171 ResetDataSize is updated with minimum size
177 IN OUT UINTN
*ResetDataSize
,
178 IN OUT VOID
*ResetData
,
179 IN CONST GUID
*ResetSubtype OPTIONAL
,
180 IN CONST CHAR16
*ResetString OPTIONAL
,
181 IN UINTN ExtraDataSize OPTIONAL
,
182 IN CONST VOID
*ExtraData OPTIONAL
185 UINTN ResetStringSize
;
186 UINTN ResetDataBufferSize
;
190 // If the size return pointer is NULL.
192 if (ResetDataSize
== NULL
) {
193 return RETURN_INVALID_PARAMETER
;
196 // If extra data is indicated, but pointer is NULL.
198 if (ExtraDataSize
> 0 && ExtraData
== NULL
) {
199 return RETURN_INVALID_PARAMETER
;
202 // If extra data is indicated, but no subtype GUID is supplied.
204 if (ResetSubtype
== NULL
&& ExtraDataSize
> 0) {
205 return RETURN_INVALID_PARAMETER
;
209 // Determine the final string.
211 if (ResetString
== NULL
) {
212 ResetString
= L
""; // Use an empty string.
216 // Calculate the total buffer required for ResetData.
218 ResetStringSize
= StrnSizeS (ResetString
, MAX_UINT16
);
219 ResetDataBufferSize
= ResetStringSize
+ ExtraDataSize
;
220 if (ResetSubtype
!= NULL
) {
221 ResetDataBufferSize
+= sizeof (GUID
);
225 // At this point, if the buffer isn't large enough (or if
226 // the buffer is NULL) we cannot proceed.
228 if (*ResetDataSize
< ResetDataBufferSize
) {
229 *ResetDataSize
= ResetDataBufferSize
;
230 return RETURN_BUFFER_TOO_SMALL
;
232 *ResetDataSize
= ResetDataBufferSize
;
233 if (ResetData
== NULL
) {
234 return RETURN_INVALID_PARAMETER
;
238 // Fill in ResetData with ResetString, the ResetSubtype GUID, and extra data
240 Data
= (UINT8
*)ResetData
;
241 CopyMem (Data
, ResetString
, ResetStringSize
);
242 Data
+= ResetStringSize
;
243 if (ResetSubtype
!= NULL
) {
244 CopyMem (Data
, ResetSubtype
, sizeof (GUID
));
245 Data
+= sizeof (GUID
);
247 if (ExtraDataSize
> 0) {
248 CopyMem (Data
, ExtraData
, ExtraDataSize
);
251 return RETURN_SUCCESS
;