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