]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Library/ResetUtilityLib/ResetUtility.c
MdeModulePkg/ResetUtilityLib: Use STATIC_ASSERT macro
[mirror_edk2.git] / MdeModulePkg / Library / ResetUtilityLib / ResetUtility.c
1 /** @file
2 This contains the business logic for the module-specific Reset Helper functions.
3
4 Copyright (c) 2017 - 2019 Intel Corporation. All rights reserved.<BR>
5 Copyright (c) 2016 Microsoft Corporation. All rights reserved.<BR>
6
7 SPDX-License-Identifier: BSD-2-Clause-Patent
8
9 **/
10 #include <Uefi.h>
11 #include <Library/BaseLib.h>
12 #include <Library/DebugLib.h>
13 #include <Library/BaseMemoryLib.h>
14 #include <Library/ResetSystemLib.h>
15
16 #pragma pack(1)
17 typedef struct {
18 CHAR16 NullTerminator;
19 GUID ResetSubtype;
20 } RESET_UTILITY_GUID_SPECIFIC_RESET_DATA;
21 #pragma pack()
22
23 STATIC_ASSERT (
24 sizeof (RESET_UTILITY_GUID_SPECIFIC_RESET_DATA) == 18,
25 "sizeof (RESET_UTILITY_GUID_SPECIFIC_RESET_DATA) is expected to be 18 bytes"
26 );
27
28 /**
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
31 a dozen parameters.
32
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.
36
37 NOTE: This call will fail if the architectural ResetSystem underpinnings
38 are not initialized. For DXE, you can add gEfiResetArchProtocolGuid
39 to your DEPEX.
40
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.
43
44 **/
45 VOID
46 EFIAPI
47 ResetSystemWithSubtype (
48 IN EFI_RESET_TYPE ResetType,
49 IN CONST GUID *ResetSubtype
50 )
51 {
52 RESET_UTILITY_GUID_SPECIFIC_RESET_DATA ResetData;
53
54 ResetData.NullTerminator = CHAR_NULL;
55 CopyGuid (
56 (GUID *)((UINT8 *)&ResetData + OFFSET_OF (RESET_UTILITY_GUID_SPECIFIC_RESET_DATA, ResetSubtype)),
57 ResetSubtype
58 );
59
60 ResetSystem (ResetType, EFI_SUCCESS, sizeof (ResetData), &ResetData);
61 }
62
63 /**
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.
67
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.
71
72 NOTE: This call will fail if the architectural ResetSystem underpinnings
73 are not initialized. For DXE, you can add gEfiResetArchProtocolGuid
74 to your DEPEX.
75
76 @param[in] ResetSubtype GUID pointer for the reset subtype to be used.
77
78 **/
79 VOID
80 EFIAPI
81 ResetPlatformSpecificGuid (
82 IN CONST GUID *ResetSubtype
83 )
84 {
85 ResetSystemWithSubtype (EfiResetPlatformSpecific, ResetSubtype);
86 }
87
88 /**
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.
93
94 @param[in] DataSize The size, in bytes, of ResetData.
95 @param[in] ResetData Pointer to the data buffer passed into ResetSystem().
96
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
100 Unicode string.
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.
104
105 **/
106 GUID *
107 EFIAPI
108 GetResetPlatformSpecificGuid (
109 IN UINTN DataSize,
110 IN CONST VOID *ResetData
111 )
112 {
113 UINTN ResetDataStringSize;
114 GUID *ResetSubtypeGuid;
115
116 //
117 // Make sure parameters are valid
118 //
119 if ((ResetData == NULL) || (DataSize < sizeof (GUID))) {
120 return NULL;
121 }
122
123 //
124 // Determine the number of bytes in the Null-terminated Unicode string
125 // at the beginning of ResetData including the Null terminator.
126 //
127 ResetDataStringSize = StrnSizeS (ResetData, (DataSize / sizeof (CHAR16)));
128
129 //
130 // Now, assuming that we have enough data for a GUID after the string, the
131 // GUID should be immediately after the string itself.
132 //
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;
137 }
138 return NULL;
139 }
140
141 /**
142 This is a helper function that creates the reset data buffer that can be
143 passed into ResetSystem().
144
145 The reset data buffer is returned in ResetData and contains ResetString
146 followed by the ResetSubtype GUID followed by the ExtraData.
147
148 NOTE: Strings are internally limited by MAX_UINT16.
149
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
154 final structure.
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.
163
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
169 UEFI spec.
170 @retval RETURN_BUFFER_TOO_SMALL An insufficient buffer was provided.
171 ResetDataSize is updated with minimum size
172 required.
173 **/
174 RETURN_STATUS
175 EFIAPI
176 BuildResetData (
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
183 )
184 {
185 UINTN ResetStringSize;
186 UINTN ResetDataBufferSize;
187 UINT8 *Data;
188
189 //
190 // If the size return pointer is NULL.
191 //
192 if (ResetDataSize == NULL) {
193 return RETURN_INVALID_PARAMETER;
194 }
195 //
196 // If extra data is indicated, but pointer is NULL.
197 //
198 if (ExtraDataSize > 0 && ExtraData == NULL) {
199 return RETURN_INVALID_PARAMETER;
200 }
201 //
202 // If extra data is indicated, but no subtype GUID is supplied.
203 //
204 if (ResetSubtype == NULL && ExtraDataSize > 0) {
205 return RETURN_INVALID_PARAMETER;
206 }
207
208 //
209 // Determine the final string.
210 //
211 if (ResetString == NULL) {
212 ResetString = L""; // Use an empty string.
213 }
214
215 //
216 // Calculate the total buffer required for ResetData.
217 //
218 ResetStringSize = StrnSizeS (ResetString, MAX_UINT16);
219 ResetDataBufferSize = ResetStringSize + ExtraDataSize;
220 if (ResetSubtype != NULL) {
221 ResetDataBufferSize += sizeof (GUID);
222 }
223
224 //
225 // At this point, if the buffer isn't large enough (or if
226 // the buffer is NULL) we cannot proceed.
227 //
228 if (*ResetDataSize < ResetDataBufferSize) {
229 *ResetDataSize = ResetDataBufferSize;
230 return RETURN_BUFFER_TOO_SMALL;
231 }
232 *ResetDataSize = ResetDataBufferSize;
233 if (ResetData == NULL) {
234 return RETURN_INVALID_PARAMETER;
235 }
236
237 //
238 // Fill in ResetData with ResetString, the ResetSubtype GUID, and extra data
239 //
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);
246 }
247 if (ExtraDataSize > 0) {
248 CopyMem (Data, ExtraData, ExtraDataSize);
249 }
250
251 return RETURN_SUCCESS;
252 }