add security check.
[edk2.git] / MdeModulePkg / Universal / Network / UefiPxeBcDxe / PxeBcSupport.c
blob0a67fdeda26a2d8133e96906b2bcc400d348aab9
1 /** @file
2 Support routines for PxeBc.
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
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.
13 **/
16 #include "PxeBcImpl.h"
19 /**
20 This function returns SMBIOS string given the string number.
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.
26 @return Pointer to string, or pointer to next SMBIOS strcuture if StringNumber == 0
28 **/
29 CHAR8 *
30 GetSmbiosString (
31 IN SMBIOS_STRUCTURE_POINTER *Smbios,
32 IN UINT16 StringNumber
35 UINT16 Index;
36 CHAR8 *String;
39 // Skip over formatted section
41 String = (CHAR8 *) (Smbios->Raw + Smbios->Hdr->Length);
44 // Look through unformated section
46 for (Index = 1; Index <= StringNumber || StringNumber == 0; Index++) {
47 if (StringNumber == Index) {
48 return String;
51 // Skip string
53 for (; *String != 0; String++)
55 String++;
57 if (*String == 0) {
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
63 Smbios->Raw = (UINT8 *)++String;
64 return NULL;
68 return NULL;
72 /**
73 This function gets system guid and serial number from the smbios table.
75 @param SystemGuid The pointer of returned system guid.
76 @param SystemSerialNumber The pointer of returned system serial number.
78 @retval EFI_SUCCESS Successfully get the system guid and system serial
79 number.
80 @retval EFI_NOT_FOUND Not find the SMBIOS table.
82 **/
83 EFI_STATUS
84 GetSmbiosSystemGuidAndSerialNumber (
85 IN EFI_GUID *SystemGuid,
86 OUT CHAR8 **SystemSerialNumber
89 EFI_STATUS Status;
90 SMBIOS_TABLE_ENTRY_POINT *SmbiosTable;
91 SMBIOS_STRUCTURE_POINTER Smbios;
92 SMBIOS_STRUCTURE_POINTER SmbiosEnd;
93 UINT16 Index;
95 Status = EfiGetSystemConfigurationTable (&gEfiSmbiosTableGuid, (VOID **) &SmbiosTable);
97 if (EFI_ERROR (Status)) {
98 return EFI_NOT_FOUND;
101 Smbios.Hdr = (SMBIOS_STRUCTURE *) (UINTN) SmbiosTable->TableAddress;
102 SmbiosEnd.Raw = (UINT8 *) (UINTN) (SmbiosTable->TableAddress + SmbiosTable->TableLength);
104 for (Index = 0; Index < SmbiosTable->TableLength; Index++) {
105 if (Smbios.Hdr->Type == 1) {
106 if (Smbios.Hdr->Length < 0x19) {
108 // Older version did not support Guid and Serial number
110 continue;
113 // SMBIOS tables are byte packed so we need to do a byte copy to
114 // prevend alignment faults on Itanium-based platform.
116 CopyMem (SystemGuid, &Smbios.Type1->Uuid, sizeof (EFI_GUID));
117 *SystemSerialNumber = GetSmbiosString (&Smbios, Smbios.Type1->SerialNumber);
119 return EFI_SUCCESS;
122 // Make Smbios point to the next record
124 GetSmbiosString (&Smbios, 0);
126 if (Smbios.Raw >= SmbiosEnd.Raw) {
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.
131 return EFI_SUCCESS;
135 return EFI_SUCCESS;
140 The common notify function associated with various PxeBc events.
142 @param Event The event signaled.
143 @param Context The context.
146 VOID
147 EFIAPI
148 PxeBcCommonNotify (
149 IN EFI_EVENT Event,
150 IN VOID *Context
153 *((BOOLEAN *) Context) = TRUE;
158 This function initialize(or configure) the Udp4Write instance.
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.
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.
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
191 EFI_UDP4_CONFIG_DATA Udp4CfgData;
192 EFI_STATUS Status;
194 ZeroMem (&Udp4CfgData, sizeof (Udp4CfgData));
196 Udp4CfgData.ReceiveTimeout = 1000;
197 Udp4CfgData.TypeOfService = DEFAULT_ToS;
198 Udp4CfgData.TimeToLive = DEFAULT_TTL;
199 Udp4CfgData.AllowDuplicatePort = TRUE;
201 CopyMem (&Udp4CfgData.StationAddress, StationIp, sizeof (*StationIp));
202 CopyMem (&Udp4CfgData.SubnetMask, SubnetMask, sizeof (*SubnetMask));
204 Udp4CfgData.StationPort = *SrcPort;
207 // Reset the instance.
209 Udp4->Configure (Udp4, NULL);
211 Status = Udp4->Configure (Udp4, &Udp4CfgData);
212 if (!EFI_ERROR (Status) && (Gateway->Addr[0] != 0)) {
214 // basic configuration OK, need to add the default route entry
216 Status = Udp4->Routes (Udp4, FALSE, &mZeroIp4Addr, &mZeroIp4Addr, Gateway);
217 if (EFI_ERROR (Status)) {
219 // roll back
221 Udp4->Configure (Udp4, NULL);
225 if (!EFI_ERROR (Status) && (*SrcPort == 0)) {
226 Udp4->GetModeData (Udp4, &Udp4CfgData, NULL, NULL, NULL);
227 *SrcPort = Udp4CfgData.StationPort;
230 return Status;
235 Convert number to ASCII value.
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.
242 VOID
243 CvtNum (
244 IN UINTN Number,
245 IN UINT8 *Buffer,
246 IN UINTN Length
249 UINTN Remainder;
251 while (Length > 0) {
252 Remainder = Number % 10;
253 Number /= 10;
254 Length--;
255 Buffer[Length] = (UINT8) ('0' + Remainder);
261 Convert unsigned int number to decimal number.
263 @param Number The unsigned int number will be converted.
264 @param Buffer Pointer to the buffer to store the decimal number after transform.
266 @return the length of the number after transform.
269 UINTN
270 UtoA10 (
271 IN UINTN Number,
272 IN CHAR8 *Buffer
275 UINTN Index;
276 CHAR8 TempStr[64];
278 Index = 63;
279 TempStr[Index] = 0;
281 do {
282 Index--;
283 TempStr[Index] = (CHAR8) ('0' + (Number % 10));
284 Number = Number / 10;
285 } while (Number != 0);
287 AsciiStrCpy (Buffer, &TempStr[Index]);
289 return AsciiStrLen (Buffer);
294 Convert ASCII numeric string to a UINTN value.
296 @param Buffer Pointer to the 8-byte unsigned int value.
298 @return UINTN value of the ASCII string.
301 UINT64
302 AtoU64 (
303 IN UINT8 *Buffer
306 UINT64 Value;
307 UINT8 Character;
309 Value = 0;
310 while ((Character = *Buffer++) != '\0') {
311 Value = MultU64x32 (Value, 10) + (Character - '0');
314 return Value;