]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Network/UefiPxeBcDxe/PxeBcSupport.c
sync comments, fix function header, rename variable name to follow coding style.
[mirror_edk2.git] / MdeModulePkg / Universal / Network / UefiPxeBcDxe / PxeBcSupport.c
1 /** @file
2
3 Copyright (c) 2007 - 2008, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12 Module Name:
13
14 PxeBcSupport.c
15
16 Abstract:
17
18 Support routines for PxeBc
19
20
21 **/
22
23
24 #include "PxeBcImpl.h"
25
26
27 /**
28
29 @param Smbios Pointer to SMBIOS structure
30 @param StringNumber String number to return. 0 is used to skip all
31 strings and point to the next SMBIOS structure.
32
33 @return Pointer to string, or pointer to next SMBIOS strcuture if StringNumber == 0
34
35 **/
36 // GC_NOTO: function comment is missing 'Routine Description:'
37 CHAR8 *
38 GetSmbiosString (
39 IN SMBIOS_STRUCTURE_POINTER *Smbios,
40 IN UINT16 StringNumber
41 )
42 {
43 UINT16 Index;
44 CHAR8 *String;
45
46 //
47 // Skip over formatted section
48 //
49 String = (CHAR8 *) (Smbios->Raw + Smbios->Hdr->Length);
50
51 //
52 // Look through unformated section
53 //
54 for (Index = 1; Index <= StringNumber || StringNumber == 0; Index++) {
55 if (StringNumber == Index) {
56 return String;
57 }
58 //
59 // Skip string
60 //
61 for (; *String != 0; String++)
62 ;
63 String++;
64
65 if (*String == 0) {
66 //
67 // If double NULL then we are done.
68 // Return pointer to next structure in Smbios.
69 // if you pass in a 0 you will always get here
70 //
71 Smbios->Raw = (UINT8 *)++String;
72 return NULL;
73 }
74 }
75
76 return NULL;
77 }
78
79
80 /**
81 This function gets system guid and serial number from the smbios table
82
83 @param SystemGuid The pointer of returned system guid
84 @param SystemSerialNumber The pointer of returned system serial number
85
86 @retval EFI_SUCCESS Successfully get the system guid and system serial
87 number
88 @retval EFI_NOT_FOUND Not find the SMBIOS table
89
90 **/
91 EFI_STATUS
92 GetSmbiosSystemGuidAndSerialNumber (
93 IN EFI_GUID *SystemGuid,
94 OUT CHAR8 **SystemSerialNumber
95 )
96 {
97 EFI_STATUS Status;
98 SMBIOS_TABLE_ENTRY_POINT *SmbiosTable;
99 SMBIOS_STRUCTURE_POINTER Smbios;
100 SMBIOS_STRUCTURE_POINTER SmbiosEnd;
101 UINT16 Index;
102
103 Status = EfiGetSystemConfigurationTable (&gEfiSmbiosTableGuid, (VOID **) &SmbiosTable);
104
105 if (EFI_ERROR (Status)) {
106 return EFI_NOT_FOUND;
107 }
108
109 Smbios.Hdr = (SMBIOS_STRUCTURE *) (UINTN) SmbiosTable->TableAddress;
110 SmbiosEnd.Raw = (UINT8 *) (UINTN) (SmbiosTable->TableAddress + SmbiosTable->TableLength);
111
112 for (Index = 0; Index < SmbiosTable->TableLength; Index++) {
113 if (Smbios.Hdr->Type == 1) {
114 if (Smbios.Hdr->Length < 0x19) {
115 //
116 // Older version did not support Guid and Serial number
117 //
118 continue;
119 }
120 //
121 // SMBIOS tables are byte packed so we need to do a byte copy to
122 // prevend alignment faults on Itanium-based platform.
123 //
124 CopyMem (SystemGuid, &Smbios.Type1->Uuid, sizeof (EFI_GUID));
125 *SystemSerialNumber = GetSmbiosString (&Smbios, Smbios.Type1->SerialNumber);
126
127 return EFI_SUCCESS;
128 }
129 //
130 // Make Smbios point to the next record
131 //
132 GetSmbiosString (&Smbios, 0);
133
134 if (Smbios.Raw >= SmbiosEnd.Raw) {
135 //
136 // SMBIOS 2.1 incorrectly stated the length of SmbiosTable as 0x1e.
137 // given this we must double check against the length of the structure.
138 //
139 return EFI_SUCCESS;
140 }
141 }
142
143 return EFI_SUCCESS;
144 }
145
146
147 /**
148 GC_NOTO: Add function description
149
150 @param Event GC_NOTO: add argument description
151 @param Context GC_NOTO: add argument description
152
153 @return GC_NOTO: add return values
154
155 **/
156 VOID
157 PxeBcCommonNotify (
158 IN EFI_EVENT Event,
159 IN VOID *Context
160 )
161 {
162 *((BOOLEAN *) Context) = TRUE;
163 }
164
165 EFI_STATUS
166 PxeBcConfigureUdpWriteInstance (
167 IN EFI_UDP4_PROTOCOL *Udp4,
168 IN EFI_IPv4_ADDRESS *StationIp,
169 IN EFI_IPv4_ADDRESS *SubnetMask,
170 IN EFI_IPv4_ADDRESS *Gateway,
171 IN OUT UINT16 *SrcPort
172 )
173 {
174 EFI_UDP4_CONFIG_DATA Udp4CfgData;
175 EFI_STATUS Status;
176
177 ZeroMem (&Udp4CfgData, sizeof (Udp4CfgData));
178
179 Udp4CfgData.ReceiveTimeout = 1000;
180 Udp4CfgData.TypeOfService = DEFAULT_ToS;
181 Udp4CfgData.TimeToLive = DEFAULT_TTL;
182
183 CopyMem (&Udp4CfgData.StationAddress, StationIp, sizeof (*StationIp));
184 CopyMem (&Udp4CfgData.SubnetMask, SubnetMask, sizeof (*SubnetMask));
185
186 Udp4CfgData.StationPort = *SrcPort;
187
188 //
189 // Reset the instance.
190 //
191 Udp4->Configure (Udp4, NULL);
192
193 Status = Udp4->Configure (Udp4, &Udp4CfgData);
194 if (!EFI_ERROR (Status) && (Gateway->Addr[0] != 0)) {
195 //
196 // basic configuration OK, need to add the default route entry
197 //
198 Status = Udp4->Routes (Udp4, FALSE, &mZeroIp4Addr, &mZeroIp4Addr, Gateway);
199 if (EFI_ERROR (Status)) {
200 //
201 // roll back
202 //
203 Udp4->Configure (Udp4, NULL);
204 }
205 }
206
207 if (!EFI_ERROR (Status) && (*SrcPort == 0)) {
208 Udp4->GetModeData (Udp4, &Udp4CfgData, NULL, NULL, NULL);
209 *SrcPort = Udp4CfgData.StationPort;
210 }
211
212 return Status;
213 }
214
215
216 /**
217 Convert number to ASCII value
218
219 @param Number Numeric value to convert to decimal ASCII value.
220 @param Buffer Buffer to place ASCII version of the Number
221 @param Length Length of Buffer.
222
223 @retval none none
224
225 **/
226 VOID
227 CvtNum (
228 IN UINTN Number,
229 IN UINT8 *Buffer,
230 IN INTN Length
231 )
232 {
233 UINTN Remainder;
234
235 while (Length--) {
236 Remainder = Number % 10;
237 Number /= 10;
238 Buffer[Length] = (UINT8) ('0' + Remainder);
239 }
240 }
241
242
243 /**
244 GC_NOTO: Add function description
245
246 @param Number GC_NOTO: add argument description
247 @param Buffer GC_NOTO: add argument description
248
249 @return GC_NOTO: add return values
250
251 **/
252 UINTN
253 UtoA10 (
254 IN UINTN Number,
255 IN CHAR8 *Buffer
256 )
257 {
258 UINTN Index;
259 CHAR8 TempStr[64];
260
261 Index = 63;
262 TempStr[Index] = 0;
263
264 do {
265 Index--;
266 TempStr[Index] = (CHAR8) ('0' + (Number % 10));
267 Number = Number / 10;
268 } while (Number != 0);
269
270 AsciiStrCpy (Buffer, &TempStr[Index]);
271
272 return AsciiStrLen (Buffer);
273 }
274
275
276 /**
277 Convert ASCII numeric string to a UINTN value
278
279 @param Number Numeric value to convert to decimal ASCII value.
280 @param Buffer Buffer to place ASCII version of the Number
281
282 @retval Value UINTN value of the ASCII string.
283
284 **/
285 UINT64
286 AtoU64 (
287 IN UINT8 *Buffer
288 )
289 {
290 UINT64 Value;
291 UINT8 Character;
292
293 Value = 0;
294 while ((Character = *Buffer++) != '\0') {
295 Value = MultU64x32 (Value, 10) + (Character - '0');
296 }
297
298 return Value;
299 }
300