]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Network/UefiPxeBcDxe/PxeBcSupport.c
code scrub for UefiPxeBcDxe.
[mirror_edk2.git] / MdeModulePkg / Universal / Network / UefiPxeBcDxe / PxeBcSupport.c
CommitLineData
dc361cc5 1/** @file\r
f737cfb9 2 Support routines for PxeBc.\r
3 \r
4Copyright (c) 2007 - 2008, 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
149PxeBcCommonNotify (\r
150 IN EFI_EVENT Event,\r
151 IN VOID *Context\r
152 )\r
153{\r
154 *((BOOLEAN *) Context) = TRUE;\r
155}\r
156\r
f737cfb9 157\r
158/**\r
159 This function initialize(or configure) the Udp4Write instance.\r
160 \r
161 @param Udp4 Pointer to the EFI_UDP4_PROTOCOL instance.\r
162 @param StationIp Pointer to the station ip address.\r
163 @param SubnetMask Pointer to the subnetmask of the station ip address.\r
164 @param Gateway Pointer to the gateway ip address.\r
165 @param SrcPort Pointer to the srouce port of the station.\r
166 \r
167 @retval EFI_SUCCESS The configuration settings were set, changed, or reset successfully.\r
168 @retval EFI_NO_MAPPING When using a default address, configuration (DHCP, BOOTP,\r
169 RARP, etc.) is not finished yet.\r
170 @retval EFI_INVALID_PARAMETER One or more following conditions are TRUE:\r
171 @retval EFI_ALREADY_STARTED The EFI UDPv4 Protocol instance is already started/configured\r
172 and must be stopped/reset before it can be reconfigured.\r
173 @retval EFI_ACCESS_DENIED UdpConfigData. AllowDuplicatePort is FALSE\r
174 and UdpConfigData.StationPort is already used by\r
175 other instance.\r
176 @retval EFI_OUT_OF_RESOURCES The EFI UDPv4 Protocol driver cannot allocate memory for this\r
177 EFI UDPv4 Protocol instance.\r
178 @retval EFI_DEVICE_ERROR An unexpected network or system error occurred and this instance\r
179 was not opened.\r
180 @retval Others Please examine the function Udp4->Routes(Udp4, FALSE, &mZeroIp4Addr, &mZeroIp4Addr, Gateway) returns.\r
181 \r
182**/\r
8792362f 183EFI_STATUS\r
184PxeBcConfigureUdpWriteInstance (\r
185 IN EFI_UDP4_PROTOCOL *Udp4,\r
186 IN EFI_IPv4_ADDRESS *StationIp,\r
187 IN EFI_IPv4_ADDRESS *SubnetMask,\r
188 IN EFI_IPv4_ADDRESS *Gateway,\r
189 IN OUT UINT16 *SrcPort\r
190 )\r
191{\r
192 EFI_UDP4_CONFIG_DATA Udp4CfgData;\r
193 EFI_STATUS Status;\r
194\r
195 ZeroMem (&Udp4CfgData, sizeof (Udp4CfgData));\r
196\r
197 Udp4CfgData.ReceiveTimeout = 1000;\r
198 Udp4CfgData.TypeOfService = DEFAULT_ToS;\r
199 Udp4CfgData.TimeToLive = DEFAULT_TTL;\r
200\r
201 CopyMem (&Udp4CfgData.StationAddress, StationIp, sizeof (*StationIp));\r
202 CopyMem (&Udp4CfgData.SubnetMask, SubnetMask, sizeof (*SubnetMask));\r
203\r
204 Udp4CfgData.StationPort = *SrcPort;\r
205\r
206 //\r
207 // Reset the instance.\r
208 //\r
209 Udp4->Configure (Udp4, NULL);\r
210\r
211 Status = Udp4->Configure (Udp4, &Udp4CfgData);\r
212 if (!EFI_ERROR (Status) && (Gateway->Addr[0] != 0)) {\r
213 //\r
214 // basic configuration OK, need to add the default route entry\r
215 //\r
216 Status = Udp4->Routes (Udp4, FALSE, &mZeroIp4Addr, &mZeroIp4Addr, Gateway);\r
217 if (EFI_ERROR (Status)) {\r
218 //\r
219 // roll back\r
220 //\r
221 Udp4->Configure (Udp4, NULL);\r
222 }\r
223 }\r
224\r
225 if (!EFI_ERROR (Status) && (*SrcPort == 0)) {\r
226 Udp4->GetModeData (Udp4, &Udp4CfgData, NULL, NULL, NULL);\r
227 *SrcPort = Udp4CfgData.StationPort;\r
228 }\r
229\r
230 return Status;\r
231}\r
232\r
dc361cc5 233\r
234/**\r
f737cfb9 235 Convert number to ASCII value.\r
dc361cc5 236\r
237 @param Number Numeric value to convert to decimal ASCII value.\r
f737cfb9 238 @param Buffer Buffer to place ASCII version of the Number.\r
dc361cc5 239 @param Length Length of Buffer.\r
240\r
dc361cc5 241**/\r
242VOID\r
243CvtNum (\r
244 IN UINTN Number,\r
245 IN UINT8 *Buffer,\r
f737cfb9 246 IN UINTN Length\r
dc361cc5 247 )\r
248{\r
249 UINTN Remainder;\r
250\r
f737cfb9 251 while (Length-- > 0) {\r
dc361cc5 252 Remainder = Number % 10;\r
253 Number /= 10;\r
254 Buffer[Length] = (UINT8) ('0' + Remainder);\r
255 }\r
256}\r
257\r
258\r
259/**\r
f737cfb9 260 Convert unsigned int number to decimal number.\r
dc361cc5 261\r
f737cfb9 262 @param Number The unsigned int number will be converted.\r
263 @param Buffer Pointer to the buffer to store the decimal number after transform.\r
dc361cc5 264\r
f737cfb9 265 @return the length of the number after transform.\r
dc361cc5 266\r
267**/\r
268UINTN\r
269UtoA10 (\r
270 IN UINTN Number,\r
271 IN CHAR8 *Buffer\r
272 )\r
273{\r
274 UINTN Index;\r
275 CHAR8 TempStr[64];\r
276\r
277 Index = 63;\r
278 TempStr[Index] = 0;\r
279\r
280 do {\r
281 Index--;\r
282 TempStr[Index] = (CHAR8) ('0' + (Number % 10));\r
283 Number = Number / 10;\r
284 } while (Number != 0);\r
285\r
286 AsciiStrCpy (Buffer, &TempStr[Index]);\r
287\r
288 return AsciiStrLen (Buffer);\r
289}\r
290\r
291\r
292/**\r
f737cfb9 293 Convert ASCII numeric string to a UINTN value.\r
dc361cc5 294\r
f737cfb9 295 @param Buffer Pointer to the 8-byte unsigned int value.\r
dc361cc5 296\r
f737cfb9 297 @return UINTN value of the ASCII string.\r
dc361cc5 298\r
299**/\r
300UINT64\r
301AtoU64 (\r
302 IN UINT8 *Buffer\r
303 )\r
304{\r
305 UINT64 Value;\r
306 UINT8 Character;\r
307\r
308 Value = 0;\r
309 while ((Character = *Buffer++) != '\0') {\r
310 Value = MultU64x32 (Value, 10) + (Character - '0');\r
311 }\r
312\r
313 return Value;\r
314}\r
315\r