]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Network/UefiPxeBcDxe/PxeBcSupport.c
Use siaddr in DHCP packet, if zero, use option 54 instead.
[mirror_edk2.git] / MdeModulePkg / Universal / Network / UefiPxeBcDxe / PxeBcSupport.c
1 /** @file
2 Support routines for PxeBc.
3
4 Copyright (c) 2007 - 2009, Intel Corporation.<BR>
5 All rights reserved. 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 This function returns SMBIOS string given the string number.
21
22 @param Smbios Pointer to SMBIOS structure
23 @param StringNumber String number to return. 0 is used to skip all
24 strings and point to the next SMBIOS structure.
25
26 @return Pointer to string, or pointer to next SMBIOS strcuture if StringNumber == 0
27
28 **/
29 CHAR8 *
30 GetSmbiosString (
31 IN SMBIOS_STRUCTURE_POINTER *Smbios,
32 IN UINT16 StringNumber
33 )
34 {
35 UINT16 Index;
36 CHAR8 *String;
37
38 //
39 // Skip over formatted section
40 //
41 String = (CHAR8 *) (Smbios->Raw + Smbios->Hdr->Length);
42
43 //
44 // Look through unformated section
45 //
46 for (Index = 1; Index <= StringNumber || StringNumber == 0; Index++) {
47 if (StringNumber == Index) {
48 return String;
49 }
50 //
51 // Skip string
52 //
53 for (; *String != 0; String++)
54 ;
55 String++;
56
57 if (*String == 0) {
58 //
59 // If double NULL then we are done.
60 // Return pointer to next structure in Smbios.
61 // if you pass in a 0 you will always get here
62 //
63 Smbios->Raw = (UINT8 *)++String;
64 return NULL;
65 }
66 }
67
68 return NULL;
69 }
70
71
72 /**
73 This function gets system guid and serial number from the smbios table.
74
75 @param SystemGuid The pointer of returned system guid.
76 @param SystemSerialNumber The pointer of returned system serial number.
77
78 @retval EFI_SUCCESS Successfully get the system guid and system serial
79 number.
80 @retval EFI_NOT_FOUND Not find the SMBIOS table.
81
82 **/
83 EFI_STATUS
84 GetSmbiosSystemGuidAndSerialNumber (
85 IN EFI_GUID *SystemGuid,
86 OUT CHAR8 **SystemSerialNumber
87 )
88 {
89 EFI_STATUS Status;
90 SMBIOS_TABLE_ENTRY_POINT *SmbiosTable;
91 SMBIOS_STRUCTURE_POINTER Smbios;
92 SMBIOS_STRUCTURE_POINTER SmbiosEnd;
93 UINT16 Index;
94
95 Status = EfiGetSystemConfigurationTable (&gEfiSmbiosTableGuid, (VOID **) &SmbiosTable);
96
97 if (EFI_ERROR (Status)) {
98 return EFI_NOT_FOUND;
99 }
100
101 Smbios.Hdr = (SMBIOS_STRUCTURE *) (UINTN) SmbiosTable->TableAddress;
102 SmbiosEnd.Raw = (UINT8 *) (UINTN) (SmbiosTable->TableAddress + SmbiosTable->TableLength);
103
104 for (Index = 0; Index < SmbiosTable->TableLength; Index++) {
105 if (Smbios.Hdr->Type == 1) {
106 if (Smbios.Hdr->Length < 0x19) {
107 //
108 // Older version did not support Guid and Serial number
109 //
110 continue;
111 }
112 //
113 // SMBIOS tables are byte packed so we need to do a byte copy to
114 // prevend alignment faults on Itanium-based platform.
115 //
116 CopyMem (SystemGuid, &Smbios.Type1->Uuid, sizeof (EFI_GUID));
117 *SystemSerialNumber = GetSmbiosString (&Smbios, Smbios.Type1->SerialNumber);
118
119 return EFI_SUCCESS;
120 }
121 //
122 // Make Smbios point to the next record
123 //
124 GetSmbiosString (&Smbios, 0);
125
126 if (Smbios.Raw >= SmbiosEnd.Raw) {
127 //
128 // SMBIOS 2.1 incorrectly stated the length of SmbiosTable as 0x1e.
129 // given this we must double check against the length of the structure.
130 //
131 return EFI_SUCCESS;
132 }
133 }
134
135 return EFI_SUCCESS;
136 }
137
138
139 /**
140 The common notify function associated with various PxeBc events.
141
142 @param Event The event signaled.
143 @param Context The context.
144
145 **/
146 VOID
147 EFIAPI
148 PxeBcCommonNotify (
149 IN EFI_EVENT Event,
150 IN VOID *Context
151 )
152 {
153 *((BOOLEAN *) Context) = TRUE;
154 }
155
156
157 /**
158 This function initialize(or configure) the Udp4Write instance.
159
160 @param Udp4 Pointer to the EFI_UDP4_PROTOCOL instance.
161 @param StationIp Pointer to the station ip address.
162 @param SubnetMask Pointer to the subnetmask of the station ip address.
163 @param Gateway Pointer to the gateway ip address.
164 @param SrcPort Pointer to the srouce port of the station.
165
166 @retval EFI_SUCCESS The configuration settings were set, changed, or reset successfully.
167 @retval EFI_NO_MAPPING When using a default address, configuration (DHCP, BOOTP,
168 RARP, etc.) is not finished yet.
169 @retval EFI_INVALID_PARAMETER One or more following conditions are TRUE:
170 @retval EFI_ALREADY_STARTED The EFI UDPv4 Protocol instance is already started/configured
171 and must be stopped/reset before it can be reconfigured.
172 @retval EFI_ACCESS_DENIED UdpConfigData. AllowDuplicatePort is FALSE
173 and UdpConfigData.StationPort is already used by
174 other instance.
175 @retval EFI_OUT_OF_RESOURCES The EFI UDPv4 Protocol driver cannot allocate memory for this
176 EFI UDPv4 Protocol instance.
177 @retval EFI_DEVICE_ERROR An unexpected network or system error occurred and this instance
178 was not opened.
179 @retval Others Please examine the function Udp4->Routes(Udp4, FALSE, &mZeroIp4Addr, &mZeroIp4Addr, Gateway) returns.
180
181 **/
182 EFI_STATUS
183 PxeBcConfigureUdpWriteInstance (
184 IN EFI_UDP4_PROTOCOL *Udp4,
185 IN EFI_IPv4_ADDRESS *StationIp,
186 IN EFI_IPv4_ADDRESS *SubnetMask,
187 IN EFI_IPv4_ADDRESS *Gateway,
188 IN OUT UINT16 *SrcPort
189 )
190 {
191 EFI_UDP4_CONFIG_DATA Udp4CfgData;
192 EFI_STATUS Status;
193
194 ZeroMem (&Udp4CfgData, sizeof (Udp4CfgData));
195
196 Udp4CfgData.ReceiveTimeout = 1000;
197 Udp4CfgData.TypeOfService = DEFAULT_ToS;
198 Udp4CfgData.TimeToLive = DEFAULT_TTL;
199 Udp4CfgData.AllowDuplicatePort = TRUE;
200
201 CopyMem (&Udp4CfgData.StationAddress, StationIp, sizeof (*StationIp));
202 CopyMem (&Udp4CfgData.SubnetMask, SubnetMask, sizeof (*SubnetMask));
203
204 Udp4CfgData.StationPort = *SrcPort;
205
206 //
207 // Reset the instance.
208 //
209 Udp4->Configure (Udp4, NULL);
210
211 Status = Udp4->Configure (Udp4, &Udp4CfgData);
212 if (!EFI_ERROR (Status) && (Gateway->Addr[0] != 0)) {
213 //
214 // basic configuration OK, need to add the default route entry
215 //
216 Status = Udp4->Routes (Udp4, FALSE, &mZeroIp4Addr, &mZeroIp4Addr, Gateway);
217 if (EFI_ERROR (Status)) {
218 //
219 // roll back
220 //
221 Udp4->Configure (Udp4, NULL);
222 }
223 }
224
225 if (!EFI_ERROR (Status) && (*SrcPort == 0)) {
226 Udp4->GetModeData (Udp4, &Udp4CfgData, NULL, NULL, NULL);
227 *SrcPort = Udp4CfgData.StationPort;
228 }
229
230 return Status;
231 }
232
233
234 /**
235 Convert number to ASCII value.
236
237 @param Number Numeric value to convert to decimal ASCII value.
238 @param Buffer Buffer to place ASCII version of the Number.
239 @param Length Length of Buffer.
240
241 **/
242 VOID
243 CvtNum (
244 IN UINTN Number,
245 IN UINT8 *Buffer,
246 IN UINTN Length
247 )
248 {
249 UINTN Remainder;
250
251 while (Length > 0) {
252 Remainder = Number % 10;
253 Number /= 10;
254 Length--;
255 Buffer[Length] = (UINT8) ('0' + Remainder);
256 }
257 }
258
259
260 /**
261 Convert unsigned int number to decimal number.
262
263 @param Number The unsigned int number will be converted.
264 @param Buffer Pointer to the buffer to store the decimal number after transform.
265
266 @return the length of the number after transform.
267
268 **/
269 UINTN
270 UtoA10 (
271 IN UINTN Number,
272 IN CHAR8 *Buffer
273 )
274 {
275 UINTN Index;
276 CHAR8 TempStr[64];
277
278 Index = 63;
279 TempStr[Index] = 0;
280
281 do {
282 Index--;
283 TempStr[Index] = (CHAR8) ('0' + (Number % 10));
284 Number = Number / 10;
285 } while (Number != 0);
286
287 AsciiStrCpy (Buffer, &TempStr[Index]);
288
289 return AsciiStrLen (Buffer);
290 }
291
292
293 /**
294 Convert ASCII numeric string to a UINTN value.
295
296 @param Buffer Pointer to the 8-byte unsigned int value.
297
298 @return UINTN value of the ASCII string.
299
300 **/
301 UINT64
302 AtoU64 (
303 IN UINT8 *Buffer
304 )
305 {
306 UINT64 Value;
307 UINT8 Character;
308
309 Value = 0;
310 while ((Character = *Buffer++) != '\0') {
311 Value = MultU64x32 (Value, 10) + (Character - '0');
312 }
313
314 return Value;
315 }
316