]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Network/UefiPxeBcDxe/PxeBcSupport.c
refine the code and add more security check.
[mirror_edk2.git] / MdeModulePkg / Universal / Network / UefiPxeBcDxe / PxeBcSupport.c
1 /** @file
2 Support routines for PxeBc.
3
4 Copyright (c) 2007 - 2010, 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 ASSERT (SmbiosTable != NULL);
101
102 Smbios.Hdr = (SMBIOS_STRUCTURE *) (UINTN) SmbiosTable->TableAddress;
103 SmbiosEnd.Raw = (UINT8 *) (UINTN) (SmbiosTable->TableAddress + SmbiosTable->TableLength);
104
105 for (Index = 0; Index < SmbiosTable->TableLength; Index++) {
106 if (Smbios.Hdr->Type == 1) {
107 if (Smbios.Hdr->Length < 0x19) {
108 //
109 // Older version did not support Guid and Serial number
110 //
111 continue;
112 }
113 //
114 // SMBIOS tables are byte packed so we need to do a byte copy to
115 // prevend alignment faults on Itanium-based platform.
116 //
117 CopyMem (SystemGuid, &Smbios.Type1->Uuid, sizeof (EFI_GUID));
118 *SystemSerialNumber = GetSmbiosString (&Smbios, Smbios.Type1->SerialNumber);
119
120 return EFI_SUCCESS;
121 }
122 //
123 // Make Smbios point to the next record
124 //
125 GetSmbiosString (&Smbios, 0);
126
127 if (Smbios.Raw >= SmbiosEnd.Raw) {
128 //
129 // SMBIOS 2.1 incorrectly stated the length of SmbiosTable as 0x1e.
130 // given this we must double check against the length of the structure.
131 //
132 return EFI_SUCCESS;
133 }
134 }
135
136 return EFI_SUCCESS;
137 }
138
139
140 /**
141 The common notify function associated with various PxeBc events.
142
143 @param Event The event signaled.
144 @param Context The context.
145
146 **/
147 VOID
148 EFIAPI
149 PxeBcCommonNotify (
150 IN EFI_EVENT Event,
151 IN VOID *Context
152 )
153 {
154 *((BOOLEAN *) Context) = TRUE;
155 }
156
157
158 /**
159 This function initialize(or configure) the Udp4Write instance.
160
161 @param Udp4 Pointer to the EFI_UDP4_PROTOCOL instance.
162 @param StationIp Pointer to the station ip address.
163 @param SubnetMask Pointer to the subnetmask of the station ip address.
164 @param Gateway Pointer to the gateway ip address.
165 @param SrcPort Pointer to the srouce port of the station.
166
167 @retval EFI_SUCCESS The configuration settings were set, changed, or reset successfully.
168 @retval EFI_NO_MAPPING When using a default address, configuration (DHCP, BOOTP,
169 RARP, etc.) is not finished yet.
170 @retval EFI_INVALID_PARAMETER One or more following conditions are TRUE:
171 @retval EFI_ALREADY_STARTED The EFI UDPv4 Protocol instance is already started/configured
172 and must be stopped/reset before it can be reconfigured.
173 @retval EFI_ACCESS_DENIED UdpConfigData. AllowDuplicatePort is FALSE
174 and UdpConfigData.StationPort is already used by
175 other instance.
176 @retval EFI_OUT_OF_RESOURCES The EFI UDPv4 Protocol driver cannot allocate memory for this
177 EFI UDPv4 Protocol instance.
178 @retval EFI_DEVICE_ERROR An unexpected network or system error occurred and this instance
179 was not opened.
180 @retval Others Please examine the function Udp4->Routes(Udp4, FALSE, &mZeroIp4Addr, &mZeroIp4Addr, Gateway) returns.
181
182 **/
183 EFI_STATUS
184 PxeBcConfigureUdpWriteInstance (
185 IN EFI_UDP4_PROTOCOL *Udp4,
186 IN EFI_IPv4_ADDRESS *StationIp,
187 IN EFI_IPv4_ADDRESS *SubnetMask,
188 IN EFI_IPv4_ADDRESS *Gateway,
189 IN OUT UINT16 *SrcPort
190 )
191 {
192 EFI_UDP4_CONFIG_DATA Udp4CfgData;
193 EFI_STATUS Status;
194
195 ZeroMem (&Udp4CfgData, sizeof (Udp4CfgData));
196
197 Udp4CfgData.ReceiveTimeout = 1000;
198 Udp4CfgData.TypeOfService = DEFAULT_ToS;
199 Udp4CfgData.TimeToLive = DEFAULT_TTL;
200 Udp4CfgData.AllowDuplicatePort = TRUE;
201
202 CopyMem (&Udp4CfgData.StationAddress, StationIp, sizeof (*StationIp));
203 CopyMem (&Udp4CfgData.SubnetMask, SubnetMask, sizeof (*SubnetMask));
204
205 Udp4CfgData.StationPort = *SrcPort;
206
207 //
208 // Reset the instance.
209 //
210 Udp4->Configure (Udp4, NULL);
211
212 Status = Udp4->Configure (Udp4, &Udp4CfgData);
213 if (!EFI_ERROR (Status) && (Gateway->Addr[0] != 0)) {
214 //
215 // basic configuration OK, need to add the default route entry
216 //
217 Status = Udp4->Routes (Udp4, FALSE, &mZeroIp4Addr, &mZeroIp4Addr, Gateway);
218 if (EFI_ERROR (Status)) {
219 //
220 // roll back
221 //
222 Udp4->Configure (Udp4, NULL);
223 }
224 }
225
226 if (!EFI_ERROR (Status) && (*SrcPort == 0)) {
227 Udp4->GetModeData (Udp4, &Udp4CfgData, NULL, NULL, NULL);
228 *SrcPort = Udp4CfgData.StationPort;
229 }
230
231 return Status;
232 }
233
234
235 /**
236 Convert number to ASCII value.
237
238 @param Number Numeric value to convert to decimal ASCII value.
239 @param Buffer Buffer to place ASCII version of the Number.
240 @param Length Length of Buffer.
241
242 **/
243 VOID
244 CvtNum (
245 IN UINTN Number,
246 IN UINT8 *Buffer,
247 IN UINTN Length
248 )
249 {
250 UINTN Remainder;
251
252 while (Length > 0) {
253 Remainder = Number % 10;
254 Number /= 10;
255 Length--;
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