]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Network/UefiPxeBcDxe/PxeBcSupport.c
MdeModulePkg/UefiPxeBcDxe: Refine the PXE boot displayed information
[mirror_edk2.git] / MdeModulePkg / Universal / Network / UefiPxeBcDxe / PxeBcSupport.c
1 /** @file
2 Support routines for PxeBc.
3
4 Copyright (c) 2007 - 2017, 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 This function is to display the IPv4 address.
119
120 @param[in] Ip The pointer to the IPv4 address.
121
122 **/
123 VOID
124 PxeBcShowIp4Addr (
125 IN EFI_IPv4_ADDRESS *Ip
126 )
127 {
128 UINTN Index;
129
130 for (Index = 0; Index < 4; Index++) {
131 AsciiPrint ("%d", Ip->Addr[Index]);
132 if (Index < 3) {
133 AsciiPrint (".");
134 }
135 }
136 }
137
138 /**
139 Convert number to ASCII value.
140
141 @param Number Numeric value to convert to decimal ASCII value.
142 @param Buffer Buffer to place ASCII version of the Number.
143 @param Length Length of Buffer.
144
145 **/
146 VOID
147 CvtNum (
148 IN UINTN Number,
149 IN UINT8 *Buffer,
150 IN UINTN Length
151 )
152 {
153 UINTN Remainder;
154
155 for (; Length > 0; Length--) {
156 Remainder = Number % 10;
157 Number /= 10;
158 Buffer[Length - 1] = (UINT8) ('0' + Remainder);
159 }
160 }
161
162
163 /**
164 Convert unsigned int number to decimal number.
165
166 @param Number The unsigned int number will be converted.
167 @param Buffer Pointer to the buffer to store the decimal number after transform.
168 @param[in] BufferSize The maxsize of the buffer.
169
170 @return the length of the number after transform.
171
172 **/
173 UINTN
174 UtoA10 (
175 IN UINTN Number,
176 IN CHAR8 *Buffer,
177 IN UINTN BufferSize
178 )
179 {
180 UINTN Index;
181 CHAR8 TempStr[64];
182
183 Index = 63;
184 TempStr[Index] = 0;
185
186 do {
187 Index--;
188 TempStr[Index] = (CHAR8) ('0' + (Number % 10));
189 Number = Number / 10;
190 } while (Number != 0);
191
192 AsciiStrCpyS (Buffer, BufferSize, &TempStr[Index]);
193
194 return AsciiStrLen (Buffer);
195 }
196
197
198 /**
199 Convert ASCII numeric string to a UINTN value.
200
201 @param Buffer Pointer to the 8-byte unsigned int value.
202
203 @return UINTN value of the ASCII string.
204
205 **/
206 UINT64
207 AtoU64 (
208 IN UINT8 *Buffer
209 )
210 {
211 UINT64 Value;
212 UINT8 Character;
213
214 Value = 0;
215 while ((Character = *Buffer++) != '\0') {
216 Value = MultU64x32 (Value, 10) + (Character - '0');
217 }
218
219 return Value;
220 }
221