]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Network/UefiPxeBcDxe/PxeBcSupport.c
MdeModulePkg: Update PXE driver to follow edk2 coding standards.
[mirror_edk2.git] / MdeModulePkg / Universal / Network / UefiPxeBcDxe / PxeBcSupport.c
1 /** @file
2 Support routines for PxeBc.
3
4 Copyright (c) 2007 - 2016, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15
16 #include "PxeBcImpl.h"
17
18
19 /**
20 The common notify function associated with various PxeBc events.
21
22 @param Event The event signaled.
23 @param Context The context.
24
25 **/
26 VOID
27 EFIAPI
28 PxeBcCommonNotify (
29 IN EFI_EVENT Event,
30 IN VOID *Context
31 )
32 {
33 *((BOOLEAN *) Context) = TRUE;
34 }
35
36
37 /**
38 This function initialize(or configure) the Udp4Write instance.
39
40 @param Udp4 Pointer to the EFI_UDP4_PROTOCOL instance.
41 @param StationIp Pointer to the station ip address.
42 @param SubnetMask Pointer to the subnetmask of the station ip address.
43 @param Gateway Pointer to the gateway ip address.
44 @param SrcPort Pointer to the srouce port of the station.
45 @param Ttl The time to live field of the IP header.
46 @param ToS The type of service field of the IP header.
47
48 @retval EFI_SUCCESS The configuration settings were set, changed, or reset successfully.
49 @retval EFI_NO_MAPPING When using a default address, configuration (DHCP, BOOTP,
50 RARP, etc.) is not finished yet.
51 @retval EFI_INVALID_PARAMETER One or more following conditions are TRUE:
52 @retval EFI_ALREADY_STARTED The EFI UDPv4 Protocol instance is already started/configured
53 and must be stopped/reset before it can be reconfigured.
54 @retval EFI_ACCESS_DENIED UdpConfigData. AllowDuplicatePort is FALSE
55 and UdpConfigData.StationPort is already used by
56 other instance.
57 @retval EFI_OUT_OF_RESOURCES The EFI UDPv4 Protocol driver cannot allocate memory for this
58 EFI UDPv4 Protocol instance.
59 @retval EFI_DEVICE_ERROR An unexpected network or system error occurred and this instance
60 was not opened.
61 @retval Others Please examine the function Udp4->Routes(Udp4, FALSE, &mZeroIp4Addr, &mZeroIp4Addr, Gateway) returns.
62
63 **/
64 EFI_STATUS
65 PxeBcConfigureUdpWriteInstance (
66 IN EFI_UDP4_PROTOCOL *Udp4,
67 IN EFI_IPv4_ADDRESS *StationIp,
68 IN EFI_IPv4_ADDRESS *SubnetMask,
69 IN EFI_IPv4_ADDRESS *Gateway,
70 IN OUT UINT16 *SrcPort,
71 IN UINT8 Ttl,
72 IN UINT8 ToS
73 )
74 {
75 EFI_UDP4_CONFIG_DATA Udp4CfgData;
76 EFI_STATUS Status;
77
78 ZeroMem (&Udp4CfgData, sizeof (Udp4CfgData));
79
80 Udp4CfgData.ReceiveTimeout = PXEBC_DEFAULT_LIFETIME;
81 Udp4CfgData.TypeOfService = ToS;
82 Udp4CfgData.TimeToLive = TTL;
83 Udp4CfgData.AllowDuplicatePort = TRUE;
84
85 CopyMem (&Udp4CfgData.StationAddress, StationIp, sizeof (*StationIp));
86 CopyMem (&Udp4CfgData.SubnetMask, SubnetMask, sizeof (*SubnetMask));
87
88 Udp4CfgData.StationPort = *SrcPort;
89
90 //
91 // Reset the instance.
92 //
93 Udp4->Configure (Udp4, NULL);
94
95 Status = Udp4->Configure (Udp4, &Udp4CfgData);
96 if (!EFI_ERROR (Status) && (Gateway->Addr[0] != 0)) {
97 //
98 // basic configuration OK, need to add the default route entry
99 //
100 Status = Udp4->Routes (Udp4, FALSE, &mZeroIp4Addr, &mZeroIp4Addr, Gateway);
101 if (EFI_ERROR (Status)) {
102 //
103 // roll back
104 //
105 Udp4->Configure (Udp4, NULL);
106 }
107 }
108
109 if (!EFI_ERROR (Status) && (*SrcPort == 0)) {
110 Udp4->GetModeData (Udp4, &Udp4CfgData, NULL, NULL, NULL);
111 *SrcPort = Udp4CfgData.StationPort;
112 }
113
114 return Status;
115 }
116
117
118 /**
119 Convert number to ASCII value.
120
121 @param Number Numeric value to convert to decimal ASCII value.
122 @param Buffer Buffer to place ASCII version of the Number.
123 @param Length Length of Buffer.
124
125 **/
126 VOID
127 CvtNum (
128 IN UINTN Number,
129 IN UINT8 *Buffer,
130 IN UINTN Length
131 )
132 {
133 UINTN Remainder;
134
135 while (Length > 0) {
136 Remainder = Number % 10;
137 Number /= 10;
138 Length--;
139 Buffer[Length] = (UINT8) ('0' + Remainder);
140 }
141 }
142
143
144 /**
145 Convert unsigned int number to decimal number.
146
147 @param Number The unsigned int number will be converted.
148 @param Buffer Pointer to the buffer to store the decimal number after transform.
149 @param[in] BufferSize The maxsize of the buffer.
150
151 @return the length of the number after transform.
152
153 **/
154 UINTN
155 UtoA10 (
156 IN UINTN Number,
157 IN CHAR8 *Buffer,
158 IN UINTN BufferSize
159 )
160 {
161 UINTN Index;
162 CHAR8 TempStr[64];
163
164 Index = 63;
165 TempStr[Index] = 0;
166
167 do {
168 Index--;
169 TempStr[Index] = (CHAR8) ('0' + (Number % 10));
170 Number = Number / 10;
171 } while (Number != 0);
172
173 AsciiStrCpyS (Buffer, BufferSize, &TempStr[Index]);
174
175 return AsciiStrLen (Buffer);
176 }
177
178
179 /**
180 Convert ASCII numeric string to a UINTN value.
181
182 @param Buffer Pointer to the 8-byte unsigned int value.
183
184 @return UINTN value of the ASCII string.
185
186 **/
187 UINT64
188 AtoU64 (
189 IN UINT8 *Buffer
190 )
191 {
192 UINT64 Value;
193 UINT8 Character;
194
195 Value = 0;
196 while ((Character = *Buffer++) != '\0') {
197 Value = MultU64x32 (Value, 10) + (Character - '0');
198 }
199
200 return Value;
201 }
202