]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Network/UefiPxeBcDxe/PxeBcSupport.c
add security check.
[mirror_edk2.git] / MdeModulePkg / Universal / Network / UefiPxeBcDxe / PxeBcSupport.c
CommitLineData
dc361cc5 1/** @file\r
f737cfb9 2 Support routines for PxeBc.\r
3 \r
6d3ea23f 4Copyright (c) 2007 - 2009, Intel Corporation.<BR> \r
dc361cc5 5All rights reserved. This program and the accompanying materials\r
6are licensed and made available under the terms and conditions of the BSD License\r
7which accompanies this distribution. The full text of the license may be found at\r
8http://opensource.org/licenses/bsd-license.php\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
dc361cc5 13**/\r
14\r
15\r
16#include "PxeBcImpl.h"\r
17\r
18\r
19/**\r
f737cfb9 20 This function returns SMBIOS string given the string number.\r
21 \r
dc361cc5 22 @param Smbios Pointer to SMBIOS structure\r
23 @param StringNumber String number to return. 0 is used to skip all\r
24 strings and point to the next SMBIOS structure.\r
25\r
26 @return Pointer to string, or pointer to next SMBIOS strcuture if StringNumber == 0\r
27\r
28**/\r
dc361cc5 29CHAR8 *\r
30GetSmbiosString (\r
31 IN SMBIOS_STRUCTURE_POINTER *Smbios,\r
32 IN UINT16 StringNumber\r
33 )\r
34{\r
35 UINT16 Index;\r
36 CHAR8 *String;\r
37\r
38 //\r
39 // Skip over formatted section\r
40 //\r
41 String = (CHAR8 *) (Smbios->Raw + Smbios->Hdr->Length);\r
42\r
43 //\r
44 // Look through unformated section\r
45 //\r
46 for (Index = 1; Index <= StringNumber || StringNumber == 0; Index++) {\r
47 if (StringNumber == Index) {\r
48 return String;\r
49 }\r
50 //\r
51 // Skip string\r
52 //\r
53 for (; *String != 0; String++)\r
54 ;\r
55 String++;\r
56\r
57 if (*String == 0) {\r
58 //\r
59 // If double NULL then we are done.\r
60 // Return pointer to next structure in Smbios.\r
61 // if you pass in a 0 you will always get here\r
62 //\r
63 Smbios->Raw = (UINT8 *)++String;\r
64 return NULL;\r
65 }\r
66 }\r
67\r
68 return NULL;\r
69}\r
70\r
71\r
72/**\r
f737cfb9 73 This function gets system guid and serial number from the smbios table.\r
dc361cc5 74\r
f737cfb9 75 @param SystemGuid The pointer of returned system guid.\r
76 @param SystemSerialNumber The pointer of returned system serial number.\r
dc361cc5 77\r
78 @retval EFI_SUCCESS Successfully get the system guid and system serial\r
f737cfb9 79 number.\r
80 @retval EFI_NOT_FOUND Not find the SMBIOS table.\r
dc361cc5 81\r
82**/\r
83EFI_STATUS\r
84GetSmbiosSystemGuidAndSerialNumber (\r
85 IN EFI_GUID *SystemGuid,\r
86 OUT CHAR8 **SystemSerialNumber\r
87 )\r
88{\r
89 EFI_STATUS Status;\r
90 SMBIOS_TABLE_ENTRY_POINT *SmbiosTable;\r
91 SMBIOS_STRUCTURE_POINTER Smbios;\r
92 SMBIOS_STRUCTURE_POINTER SmbiosEnd;\r
93 UINT16 Index;\r
94\r
95 Status = EfiGetSystemConfigurationTable (&gEfiSmbiosTableGuid, (VOID **) &SmbiosTable);\r
96\r
97 if (EFI_ERROR (Status)) {\r
98 return EFI_NOT_FOUND;\r
99 }\r
100\r
101 Smbios.Hdr = (SMBIOS_STRUCTURE *) (UINTN) SmbiosTable->TableAddress;\r
102 SmbiosEnd.Raw = (UINT8 *) (UINTN) (SmbiosTable->TableAddress + SmbiosTable->TableLength);\r
103\r
104 for (Index = 0; Index < SmbiosTable->TableLength; Index++) {\r
105 if (Smbios.Hdr->Type == 1) {\r
106 if (Smbios.Hdr->Length < 0x19) {\r
107 //\r
108 // Older version did not support Guid and Serial number\r
109 //\r
110 continue;\r
111 }\r
112 //\r
113 // SMBIOS tables are byte packed so we need to do a byte copy to\r
114 // prevend alignment faults on Itanium-based platform.\r
115 //\r
116 CopyMem (SystemGuid, &Smbios.Type1->Uuid, sizeof (EFI_GUID));\r
117 *SystemSerialNumber = GetSmbiosString (&Smbios, Smbios.Type1->SerialNumber);\r
118\r
119 return EFI_SUCCESS;\r
120 }\r
121 //\r
122 // Make Smbios point to the next record\r
123 //\r
124 GetSmbiosString (&Smbios, 0);\r
125\r
126 if (Smbios.Raw >= SmbiosEnd.Raw) {\r
127 //\r
128 // SMBIOS 2.1 incorrectly stated the length of SmbiosTable as 0x1e.\r
129 // given this we must double check against the length of the structure.\r
130 //\r
131 return EFI_SUCCESS;\r
132 }\r
133 }\r
134\r
135 return EFI_SUCCESS;\r
136}\r
137\r
138\r
139/**\r
f737cfb9 140 The common notify function associated with various PxeBc events. \r
dc361cc5 141\r
f737cfb9 142 @param Event The event signaled.\r
143 @param Context The context.\r
dc361cc5 144\r
f737cfb9 145 @return None\r
dc361cc5 146\r
147**/\r
148VOID\r
6d3ea23f 149EFIAPI\r
dc361cc5 150PxeBcCommonNotify (\r
151 IN EFI_EVENT Event,\r
152 IN VOID *Context\r
153 )\r
154{\r
155 *((BOOLEAN *) Context) = TRUE;\r
156}\r
157\r
f737cfb9 158\r
159/**\r
160 This function initialize(or configure) the Udp4Write instance.\r
161 \r
162 @param Udp4 Pointer to the EFI_UDP4_PROTOCOL instance.\r
163 @param StationIp Pointer to the station ip address.\r
164 @param SubnetMask Pointer to the subnetmask of the station ip address.\r
165 @param Gateway Pointer to the gateway ip address.\r
166 @param SrcPort Pointer to the srouce port of the station.\r
167 \r
168 @retval EFI_SUCCESS The configuration settings were set, changed, or reset successfully.\r
169 @retval EFI_NO_MAPPING When using a default address, configuration (DHCP, BOOTP,\r
170 RARP, etc.) is not finished yet.\r
171 @retval EFI_INVALID_PARAMETER One or more following conditions are TRUE:\r
172 @retval EFI_ALREADY_STARTED The EFI UDPv4 Protocol instance is already started/configured\r
173 and must be stopped/reset before it can be reconfigured.\r
174 @retval EFI_ACCESS_DENIED UdpConfigData. AllowDuplicatePort is FALSE\r
175 and UdpConfigData.StationPort is already used by\r
176 other instance.\r
177 @retval EFI_OUT_OF_RESOURCES The EFI UDPv4 Protocol driver cannot allocate memory for this\r
178 EFI UDPv4 Protocol instance.\r
179 @retval EFI_DEVICE_ERROR An unexpected network or system error occurred and this instance\r
180 was not opened.\r
181 @retval Others Please examine the function Udp4->Routes(Udp4, FALSE, &mZeroIp4Addr, &mZeroIp4Addr, Gateway) returns.\r
182 \r
183**/\r
8792362f 184EFI_STATUS\r
185PxeBcConfigureUdpWriteInstance (\r
186 IN EFI_UDP4_PROTOCOL *Udp4,\r
187 IN EFI_IPv4_ADDRESS *StationIp,\r
188 IN EFI_IPv4_ADDRESS *SubnetMask,\r
189 IN EFI_IPv4_ADDRESS *Gateway,\r
190 IN OUT UINT16 *SrcPort\r
191 )\r
192{\r
193 EFI_UDP4_CONFIG_DATA Udp4CfgData;\r
194 EFI_STATUS Status;\r
195\r
196 ZeroMem (&Udp4CfgData, sizeof (Udp4CfgData));\r
197\r
198 Udp4CfgData.ReceiveTimeout = 1000;\r
199 Udp4CfgData.TypeOfService = DEFAULT_ToS;\r
200 Udp4CfgData.TimeToLive = DEFAULT_TTL;\r
319075ff 201 Udp4CfgData.AllowDuplicatePort = TRUE;\r
8792362f 202\r
203 CopyMem (&Udp4CfgData.StationAddress, StationIp, sizeof (*StationIp));\r
204 CopyMem (&Udp4CfgData.SubnetMask, SubnetMask, sizeof (*SubnetMask));\r
205\r
206 Udp4CfgData.StationPort = *SrcPort;\r
207\r
208 //\r
209 // Reset the instance.\r
210 //\r
211 Udp4->Configure (Udp4, NULL);\r
212\r
213 Status = Udp4->Configure (Udp4, &Udp4CfgData);\r
214 if (!EFI_ERROR (Status) && (Gateway->Addr[0] != 0)) {\r
215 //\r
216 // basic configuration OK, need to add the default route entry\r
217 //\r
218 Status = Udp4->Routes (Udp4, FALSE, &mZeroIp4Addr, &mZeroIp4Addr, Gateway);\r
219 if (EFI_ERROR (Status)) {\r
220 //\r
221 // roll back\r
222 //\r
223 Udp4->Configure (Udp4, NULL);\r
224 }\r
225 }\r
226\r
227 if (!EFI_ERROR (Status) && (*SrcPort == 0)) {\r
228 Udp4->GetModeData (Udp4, &Udp4CfgData, NULL, NULL, NULL);\r
229 *SrcPort = Udp4CfgData.StationPort;\r
230 }\r
231\r
232 return Status;\r
233}\r
234\r
dc361cc5 235\r
236/**\r
f737cfb9 237 Convert number to ASCII value.\r
dc361cc5 238\r
239 @param Number Numeric value to convert to decimal ASCII value.\r
f737cfb9 240 @param Buffer Buffer to place ASCII version of the Number.\r
dc361cc5 241 @param Length Length of Buffer.\r
242\r
dc361cc5 243**/\r
244VOID\r
245CvtNum (\r
246 IN UINTN Number,\r
247 IN UINT8 *Buffer,\r
f737cfb9 248 IN UINTN Length\r
dc361cc5 249 )\r
250{\r
251 UINTN Remainder;\r
252\r
894d038a 253 while (Length > 0) {\r
dc361cc5 254 Remainder = Number % 10;\r
255 Number /= 10;\r
894d038a 256 Length--;\r
dc361cc5 257 Buffer[Length] = (UINT8) ('0' + Remainder);\r
258 }\r
259}\r
260\r
261\r
262/**\r
f737cfb9 263 Convert unsigned int number to decimal number.\r
dc361cc5 264\r
f737cfb9 265 @param Number The unsigned int number will be converted.\r
266 @param Buffer Pointer to the buffer to store the decimal number after transform.\r
dc361cc5 267\r
f737cfb9 268 @return the length of the number after transform.\r
dc361cc5 269\r
270**/\r
271UINTN\r
272UtoA10 (\r
273 IN UINTN Number,\r
274 IN CHAR8 *Buffer\r
275 )\r
276{\r
277 UINTN Index;\r
278 CHAR8 TempStr[64];\r
279\r
280 Index = 63;\r
281 TempStr[Index] = 0;\r
282\r
283 do {\r
284 Index--;\r
285 TempStr[Index] = (CHAR8) ('0' + (Number % 10));\r
286 Number = Number / 10;\r
287 } while (Number != 0);\r
288\r
289 AsciiStrCpy (Buffer, &TempStr[Index]);\r
290\r
291 return AsciiStrLen (Buffer);\r
292}\r
293\r
294\r
295/**\r
f737cfb9 296 Convert ASCII numeric string to a UINTN value.\r
dc361cc5 297\r
f737cfb9 298 @param Buffer Pointer to the 8-byte unsigned int value.\r
dc361cc5 299\r
f737cfb9 300 @return UINTN value of the ASCII string.\r
dc361cc5 301\r
302**/\r
303UINT64\r
304AtoU64 (\r
305 IN UINT8 *Buffer\r
306 )\r
307{\r
308 UINT64 Value;\r
309 UINT8 Character;\r
310\r
311 Value = 0;\r
312 while ((Character = *Buffer++) != '\0') {\r
313 Value = MultU64x32 (Value, 10) + (Character - '0');\r
314 }\r
315\r
316 return Value;\r
317}\r
318\r