]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Network/UefiPxeBcDxe/PxeBcSupport.c
NetworkPkg: Do not use hard coded TTL/ToS in PXE driver.
[mirror_edk2.git] / MdeModulePkg / Universal / Network / UefiPxeBcDxe / PxeBcSupport.c
CommitLineData
dc361cc5 1/** @file\r
f737cfb9 2 Support routines for PxeBc.\r
e2851998 3\r
206b5f51 4Copyright (c) 2007 - 2015, Intel Corporation. All rights reserved.<BR>\r
e5eed7d3 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
dc361cc5 19/**\r
e2851998 20 The common notify function associated with various PxeBc events.\r
dc361cc5 21\r
f737cfb9 22 @param Event The event signaled.\r
23 @param Context The context.\r
dc361cc5 24\r
dc361cc5 25**/\r
26VOID\r
6d3ea23f 27EFIAPI\r
dc361cc5 28PxeBcCommonNotify (\r
29 IN EFI_EVENT Event,\r
30 IN VOID *Context\r
31 )\r
32{\r
33 *((BOOLEAN *) Context) = TRUE;\r
34}\r
35\r
f737cfb9 36\r
37/**\r
38 This function initialize(or configure) the Udp4Write instance.\r
e2851998 39\r
f737cfb9 40 @param Udp4 Pointer to the EFI_UDP4_PROTOCOL instance.\r
41 @param StationIp Pointer to the station ip address.\r
42 @param SubnetMask Pointer to the subnetmask of the station ip address.\r
43 @param Gateway Pointer to the gateway ip address.\r
44 @param SrcPort Pointer to the srouce port of the station.\r
e2851998 45\r
f737cfb9 46 @retval EFI_SUCCESS The configuration settings were set, changed, or reset successfully.\r
47 @retval EFI_NO_MAPPING When using a default address, configuration (DHCP, BOOTP,\r
48 RARP, etc.) is not finished yet.\r
49 @retval EFI_INVALID_PARAMETER One or more following conditions are TRUE:\r
50 @retval EFI_ALREADY_STARTED The EFI UDPv4 Protocol instance is already started/configured\r
51 and must be stopped/reset before it can be reconfigured.\r
52 @retval EFI_ACCESS_DENIED UdpConfigData. AllowDuplicatePort is FALSE\r
53 and UdpConfigData.StationPort is already used by\r
54 other instance.\r
55 @retval EFI_OUT_OF_RESOURCES The EFI UDPv4 Protocol driver cannot allocate memory for this\r
56 EFI UDPv4 Protocol instance.\r
57 @retval EFI_DEVICE_ERROR An unexpected network or system error occurred and this instance\r
58 was not opened.\r
59 @retval Others Please examine the function Udp4->Routes(Udp4, FALSE, &mZeroIp4Addr, &mZeroIp4Addr, Gateway) returns.\r
e2851998 60\r
f737cfb9 61**/\r
8792362f 62EFI_STATUS\r
63PxeBcConfigureUdpWriteInstance (\r
64 IN EFI_UDP4_PROTOCOL *Udp4,\r
65 IN EFI_IPv4_ADDRESS *StationIp,\r
66 IN EFI_IPv4_ADDRESS *SubnetMask,\r
67 IN EFI_IPv4_ADDRESS *Gateway,\r
68 IN OUT UINT16 *SrcPort\r
69 )\r
70{\r
71 EFI_UDP4_CONFIG_DATA Udp4CfgData;\r
72 EFI_STATUS Status;\r
73\r
74 ZeroMem (&Udp4CfgData, sizeof (Udp4CfgData));\r
75\r
7bc01e86 76 Udp4CfgData.ReceiveTimeout = PXEBC_DEFAULT_LIFETIME;\r
8792362f 77 Udp4CfgData.TypeOfService = DEFAULT_ToS;\r
78 Udp4CfgData.TimeToLive = DEFAULT_TTL;\r
319075ff 79 Udp4CfgData.AllowDuplicatePort = TRUE;\r
8792362f 80\r
81 CopyMem (&Udp4CfgData.StationAddress, StationIp, sizeof (*StationIp));\r
82 CopyMem (&Udp4CfgData.SubnetMask, SubnetMask, sizeof (*SubnetMask));\r
83\r
84 Udp4CfgData.StationPort = *SrcPort;\r
85\r
86 //\r
87 // Reset the instance.\r
88 //\r
89 Udp4->Configure (Udp4, NULL);\r
90\r
91 Status = Udp4->Configure (Udp4, &Udp4CfgData);\r
92 if (!EFI_ERROR (Status) && (Gateway->Addr[0] != 0)) {\r
93 //\r
94 // basic configuration OK, need to add the default route entry\r
95 //\r
96 Status = Udp4->Routes (Udp4, FALSE, &mZeroIp4Addr, &mZeroIp4Addr, Gateway);\r
97 if (EFI_ERROR (Status)) {\r
98 //\r
99 // roll back\r
100 //\r
101 Udp4->Configure (Udp4, NULL);\r
102 }\r
103 }\r
104\r
105 if (!EFI_ERROR (Status) && (*SrcPort == 0)) {\r
106 Udp4->GetModeData (Udp4, &Udp4CfgData, NULL, NULL, NULL);\r
107 *SrcPort = Udp4CfgData.StationPort;\r
108 }\r
109\r
110 return Status;\r
111}\r
112\r
dc361cc5 113\r
114/**\r
f737cfb9 115 Convert number to ASCII value.\r
dc361cc5 116\r
117 @param Number Numeric value to convert to decimal ASCII value.\r
f737cfb9 118 @param Buffer Buffer to place ASCII version of the Number.\r
dc361cc5 119 @param Length Length of Buffer.\r
120\r
dc361cc5 121**/\r
122VOID\r
123CvtNum (\r
124 IN UINTN Number,\r
125 IN UINT8 *Buffer,\r
f737cfb9 126 IN UINTN Length\r
dc361cc5 127 )\r
128{\r
129 UINTN Remainder;\r
130\r
894d038a 131 while (Length > 0) {\r
dc361cc5 132 Remainder = Number % 10;\r
133 Number /= 10;\r
894d038a 134 Length--;\r
dc361cc5 135 Buffer[Length] = (UINT8) ('0' + Remainder);\r
136 }\r
137}\r
138\r
139\r
140/**\r
f737cfb9 141 Convert unsigned int number to decimal number.\r
dc361cc5 142\r
206b5f51
ZL
143 @param Number The unsigned int number will be converted.\r
144 @param Buffer Pointer to the buffer to store the decimal number after transform.\r
145 @param[in] BufferSize The maxsize of the buffer.\r
146 \r
f737cfb9 147 @return the length of the number after transform.\r
dc361cc5 148\r
149**/\r
150UINTN\r
151UtoA10 (\r
152 IN UINTN Number,\r
206b5f51
ZL
153 IN CHAR8 *Buffer,\r
154 IN UINTN BufferSize\r
dc361cc5 155 )\r
156{\r
157 UINTN Index;\r
158 CHAR8 TempStr[64];\r
159\r
160 Index = 63;\r
161 TempStr[Index] = 0;\r
162\r
163 do {\r
164 Index--;\r
165 TempStr[Index] = (CHAR8) ('0' + (Number % 10));\r
166 Number = Number / 10;\r
167 } while (Number != 0);\r
168\r
206b5f51 169 AsciiStrCpyS (Buffer, BufferSize, &TempStr[Index]);\r
dc361cc5 170\r
171 return AsciiStrLen (Buffer);\r
172}\r
173\r
174\r
175/**\r
f737cfb9 176 Convert ASCII numeric string to a UINTN value.\r
dc361cc5 177\r
f737cfb9 178 @param Buffer Pointer to the 8-byte unsigned int value.\r
dc361cc5 179\r
f737cfb9 180 @return UINTN value of the ASCII string.\r
dc361cc5 181\r
182**/\r
183UINT64\r
184AtoU64 (\r
185 IN UINT8 *Buffer\r
186 )\r
187{\r
188 UINT64 Value;\r
189 UINT8 Character;\r
190\r
191 Value = 0;\r
192 while ((Character = *Buffer++) != '\0') {\r
193 Value = MultU64x32 (Value, 10) + (Character - '0');\r
194 }\r
195\r
196 return Value;\r
197}\r
198\r