]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Network/UefiPxeBcDxe/PxeBcSupport.c
Update the copyright notice format
[mirror_edk2.git] / MdeModulePkg / Universal / Network / UefiPxeBcDxe / PxeBcSupport.c
CommitLineData
dc361cc5 1/** @file\r
f737cfb9 2 Support routines for PxeBc.\r
e2851998 3\r
e5eed7d3
HT
4Copyright (c) 2007 - 2010, Intel Corporation. All rights reserved.<BR>\r
5This program and the accompanying materials\r
dc361cc5 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
e2851998 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
e2851998 100 ASSERT (SmbiosTable != NULL);\r
dc361cc5 101\r
102 Smbios.Hdr = (SMBIOS_STRUCTURE *) (UINTN) SmbiosTable->TableAddress;\r
103 SmbiosEnd.Raw = (UINT8 *) (UINTN) (SmbiosTable->TableAddress + SmbiosTable->TableLength);\r
104\r
105 for (Index = 0; Index < SmbiosTable->TableLength; Index++) {\r
106 if (Smbios.Hdr->Type == 1) {\r
107 if (Smbios.Hdr->Length < 0x19) {\r
108 //\r
109 // Older version did not support Guid and Serial number\r
110 //\r
111 continue;\r
112 }\r
113 //\r
114 // SMBIOS tables are byte packed so we need to do a byte copy to\r
115 // prevend alignment faults on Itanium-based platform.\r
116 //\r
117 CopyMem (SystemGuid, &Smbios.Type1->Uuid, sizeof (EFI_GUID));\r
118 *SystemSerialNumber = GetSmbiosString (&Smbios, Smbios.Type1->SerialNumber);\r
119\r
120 return EFI_SUCCESS;\r
121 }\r
122 //\r
123 // Make Smbios point to the next record\r
124 //\r
125 GetSmbiosString (&Smbios, 0);\r
126\r
127 if (Smbios.Raw >= SmbiosEnd.Raw) {\r
128 //\r
129 // SMBIOS 2.1 incorrectly stated the length of SmbiosTable as 0x1e.\r
130 // given this we must double check against the length of the structure.\r
131 //\r
132 return EFI_SUCCESS;\r
133 }\r
134 }\r
135\r
136 return EFI_SUCCESS;\r
137}\r
138\r
139\r
140/**\r
e2851998 141 The common notify function associated with various PxeBc events.\r
dc361cc5 142\r
f737cfb9 143 @param Event The event signaled.\r
144 @param Context The context.\r
dc361cc5 145\r
dc361cc5 146**/\r
147VOID\r
6d3ea23f 148EFIAPI\r
dc361cc5 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
e2851998 160\r
f737cfb9 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
e2851998 166\r
f737cfb9 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
e2851998 181\r
f737cfb9 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
7bc01e86 197 Udp4CfgData.ReceiveTimeout = PXEBC_DEFAULT_LIFETIME;\r
8792362f 198 Udp4CfgData.TypeOfService = DEFAULT_ToS;\r
199 Udp4CfgData.TimeToLive = DEFAULT_TTL;\r
319075ff 200 Udp4CfgData.AllowDuplicatePort = TRUE;\r
8792362f 201\r
202 CopyMem (&Udp4CfgData.StationAddress, StationIp, sizeof (*StationIp));\r
203 CopyMem (&Udp4CfgData.SubnetMask, SubnetMask, sizeof (*SubnetMask));\r
204\r
205 Udp4CfgData.StationPort = *SrcPort;\r
206\r
207 //\r
208 // Reset the instance.\r
209 //\r
210 Udp4->Configure (Udp4, NULL);\r
211\r
212 Status = Udp4->Configure (Udp4, &Udp4CfgData);\r
213 if (!EFI_ERROR (Status) && (Gateway->Addr[0] != 0)) {\r
214 //\r
215 // basic configuration OK, need to add the default route entry\r
216 //\r
217 Status = Udp4->Routes (Udp4, FALSE, &mZeroIp4Addr, &mZeroIp4Addr, Gateway);\r
218 if (EFI_ERROR (Status)) {\r
219 //\r
220 // roll back\r
221 //\r
222 Udp4->Configure (Udp4, NULL);\r
223 }\r
224 }\r
225\r
226 if (!EFI_ERROR (Status) && (*SrcPort == 0)) {\r
227 Udp4->GetModeData (Udp4, &Udp4CfgData, NULL, NULL, NULL);\r
228 *SrcPort = Udp4CfgData.StationPort;\r
229 }\r
230\r
231 return Status;\r
232}\r
233\r
dc361cc5 234\r
235/**\r
f737cfb9 236 Convert number to ASCII value.\r
dc361cc5 237\r
238 @param Number Numeric value to convert to decimal ASCII value.\r
f737cfb9 239 @param Buffer Buffer to place ASCII version of the Number.\r
dc361cc5 240 @param Length Length of Buffer.\r
241\r
dc361cc5 242**/\r
243VOID\r
244CvtNum (\r
245 IN UINTN Number,\r
246 IN UINT8 *Buffer,\r
f737cfb9 247 IN UINTN Length\r
dc361cc5 248 )\r
249{\r
250 UINTN Remainder;\r
251\r
894d038a 252 while (Length > 0) {\r
dc361cc5 253 Remainder = Number % 10;\r
254 Number /= 10;\r
894d038a 255 Length--;\r
dc361cc5 256 Buffer[Length] = (UINT8) ('0' + Remainder);\r
257 }\r
258}\r
259\r
260\r
261/**\r
f737cfb9 262 Convert unsigned int number to decimal number.\r
dc361cc5 263\r
f737cfb9 264 @param Number The unsigned int number will be converted.\r
265 @param Buffer Pointer to the buffer to store the decimal number after transform.\r
dc361cc5 266\r
f737cfb9 267 @return the length of the number after transform.\r
dc361cc5 268\r
269**/\r
270UINTN\r
271UtoA10 (\r
272 IN UINTN Number,\r
273 IN CHAR8 *Buffer\r
274 )\r
275{\r
276 UINTN Index;\r
277 CHAR8 TempStr[64];\r
278\r
279 Index = 63;\r
280 TempStr[Index] = 0;\r
281\r
282 do {\r
283 Index--;\r
284 TempStr[Index] = (CHAR8) ('0' + (Number % 10));\r
285 Number = Number / 10;\r
286 } while (Number != 0);\r
287\r
288 AsciiStrCpy (Buffer, &TempStr[Index]);\r
289\r
290 return AsciiStrLen (Buffer);\r
291}\r
292\r
293\r
294/**\r
f737cfb9 295 Convert ASCII numeric string to a UINTN value.\r
dc361cc5 296\r
f737cfb9 297 @param Buffer Pointer to the 8-byte unsigned int value.\r
dc361cc5 298\r
f737cfb9 299 @return UINTN value of the ASCII string.\r
dc361cc5 300\r
301**/\r
302UINT64\r
303AtoU64 (\r
304 IN UINT8 *Buffer\r
305 )\r
306{\r
307 UINT64 Value;\r
308 UINT8 Character;\r
309\r
310 Value = 0;\r
311 while ((Character = *Buffer++) != '\0') {\r
312 Value = MultU64x32 (Value, 10) + (Character - '0');\r
313 }\r
314\r
315 return Value;\r
316}\r
317\r