]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Network/UefiPxeBcDxe/PxeBcSupport.c
9327f6206731c39e99f5e2a1c3165834e9055231
[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 @return None
146
147 **/
148 VOID
149 EFIAPI
150 PxeBcCommonNotify (
151 IN EFI_EVENT Event,
152 IN VOID *Context
153 )
154 {
155 *((BOOLEAN *) Context) = TRUE;
156 }
157
158
159 /**
160 This function initialize(or configure) the Udp4Write instance.
161
162 @param Udp4 Pointer to the EFI_UDP4_PROTOCOL instance.
163 @param StationIp Pointer to the station ip address.
164 @param SubnetMask Pointer to the subnetmask of the station ip address.
165 @param Gateway Pointer to the gateway ip address.
166 @param SrcPort Pointer to the srouce port of the station.
167
168 @retval EFI_SUCCESS The configuration settings were set, changed, or reset successfully.
169 @retval EFI_NO_MAPPING When using a default address, configuration (DHCP, BOOTP,
170 RARP, etc.) is not finished yet.
171 @retval EFI_INVALID_PARAMETER One or more following conditions are TRUE:
172 @retval EFI_ALREADY_STARTED The EFI UDPv4 Protocol instance is already started/configured
173 and must be stopped/reset before it can be reconfigured.
174 @retval EFI_ACCESS_DENIED UdpConfigData. AllowDuplicatePort is FALSE
175 and UdpConfigData.StationPort is already used by
176 other instance.
177 @retval EFI_OUT_OF_RESOURCES The EFI UDPv4 Protocol driver cannot allocate memory for this
178 EFI UDPv4 Protocol instance.
179 @retval EFI_DEVICE_ERROR An unexpected network or system error occurred and this instance
180 was not opened.
181 @retval Others Please examine the function Udp4->Routes(Udp4, FALSE, &mZeroIp4Addr, &mZeroIp4Addr, Gateway) returns.
182
183 **/
184 EFI_STATUS
185 PxeBcConfigureUdpWriteInstance (
186 IN EFI_UDP4_PROTOCOL *Udp4,
187 IN EFI_IPv4_ADDRESS *StationIp,
188 IN EFI_IPv4_ADDRESS *SubnetMask,
189 IN EFI_IPv4_ADDRESS *Gateway,
190 IN OUT UINT16 *SrcPort
191 )
192 {
193 EFI_UDP4_CONFIG_DATA Udp4CfgData;
194 EFI_STATUS Status;
195
196 ZeroMem (&Udp4CfgData, sizeof (Udp4CfgData));
197
198 Udp4CfgData.ReceiveTimeout = 1000;
199 Udp4CfgData.TypeOfService = DEFAULT_ToS;
200 Udp4CfgData.TimeToLive = DEFAULT_TTL;
201 Udp4CfgData.AllowDuplicatePort = TRUE;
202
203 CopyMem (&Udp4CfgData.StationAddress, StationIp, sizeof (*StationIp));
204 CopyMem (&Udp4CfgData.SubnetMask, SubnetMask, sizeof (*SubnetMask));
205
206 Udp4CfgData.StationPort = *SrcPort;
207
208 //
209 // Reset the instance.
210 //
211 Udp4->Configure (Udp4, NULL);
212
213 Status = Udp4->Configure (Udp4, &Udp4CfgData);
214 if (!EFI_ERROR (Status) && (Gateway->Addr[0] != 0)) {
215 //
216 // basic configuration OK, need to add the default route entry
217 //
218 Status = Udp4->Routes (Udp4, FALSE, &mZeroIp4Addr, &mZeroIp4Addr, Gateway);
219 if (EFI_ERROR (Status)) {
220 //
221 // roll back
222 //
223 Udp4->Configure (Udp4, NULL);
224 }
225 }
226
227 if (!EFI_ERROR (Status) && (*SrcPort == 0)) {
228 Udp4->GetModeData (Udp4, &Udp4CfgData, NULL, NULL, NULL);
229 *SrcPort = Udp4CfgData.StationPort;
230 }
231
232 return Status;
233 }
234
235
236 /**
237 Convert number to ASCII value.
238
239 @param Number Numeric value to convert to decimal ASCII value.
240 @param Buffer Buffer to place ASCII version of the Number.
241 @param Length Length of Buffer.
242
243 **/
244 VOID
245 CvtNum (
246 IN UINTN Number,
247 IN UINT8 *Buffer,
248 IN UINTN Length
249 )
250 {
251 UINTN Remainder;
252
253 while (Length-- > 0) {
254 Remainder = Number % 10;
255 Number /= 10;
256 Buffer[Length] = (UINT8) ('0' + Remainder);
257 }
258 }
259
260
261 /**
262 Convert unsigned int number to decimal number.
263
264 @param Number The unsigned int number will be converted.
265 @param Buffer Pointer to the buffer to store the decimal number after transform.
266
267 @return the length of the number after transform.
268
269 **/
270 UINTN
271 UtoA10 (
272 IN UINTN Number,
273 IN CHAR8 *Buffer
274 )
275 {
276 UINTN Index;
277 CHAR8 TempStr[64];
278
279 Index = 63;
280 TempStr[Index] = 0;
281
282 do {
283 Index--;
284 TempStr[Index] = (CHAR8) ('0' + (Number % 10));
285 Number = Number / 10;
286 } while (Number != 0);
287
288 AsciiStrCpy (Buffer, &TempStr[Index]);
289
290 return AsciiStrLen (Buffer);
291 }
292
293
294 /**
295 Convert ASCII numeric string to a UINTN value.
296
297 @param Buffer Pointer to the 8-byte unsigned int value.
298
299 @return UINTN value of the ASCII string.
300
301 **/
302 UINT64
303 AtoU64 (
304 IN UINT8 *Buffer
305 )
306 {
307 UINT64 Value;
308 UINT8 Character;
309
310 Value = 0;
311 while ((Character = *Buffer++) != '\0') {
312 Value = MultU64x32 (Value, 10) + (Character - '0');
313 }
314
315 return Value;
316 }
317