1. Import UEFI PxeBc module in MdeModulePkg
[mirror_edk2.git] / MdeModulePkg / Universal / Network / UefiPxeBcDxe / PxeBcSupport.c
1 /** @file
2
3 Copyright (c) 2007, 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
166 /**
167 Convert number to ASCII value
168
169 @param Number Numeric value to convert to decimal ASCII value.
170 @param Buffer Buffer to place ASCII version of the Number
171 @param Length Length of Buffer.
172
173 @retval none none
174
175 **/
176 VOID
177 CvtNum (
178 IN UINTN Number,
179 IN UINT8 *Buffer,
180 IN INTN Length
181 )
182 {
183 UINTN Remainder;
184
185 while (Length--) {
186 Remainder = Number % 10;
187 Number /= 10;
188 Buffer[Length] = (UINT8) ('0' + Remainder);
189 }
190 }
191
192
193 /**
194 GC_NOTO: Add function description
195
196 @param Number GC_NOTO: add argument description
197 @param Buffer GC_NOTO: add argument description
198
199 @return GC_NOTO: add return values
200
201 **/
202 UINTN
203 UtoA10 (
204 IN UINTN Number,
205 IN CHAR8 *Buffer
206 )
207 {
208 UINTN Index;
209 CHAR8 TempStr[64];
210
211 Index = 63;
212 TempStr[Index] = 0;
213
214 do {
215 Index--;
216 TempStr[Index] = (CHAR8) ('0' + (Number % 10));
217 Number = Number / 10;
218 } while (Number != 0);
219
220 AsciiStrCpy (Buffer, &TempStr[Index]);
221
222 return AsciiStrLen (Buffer);
223 }
224
225
226 /**
227 Convert ASCII numeric string to a UINTN value
228
229 @param Number Numeric value to convert to decimal ASCII value.
230 @param Buffer Buffer to place ASCII version of the Number
231
232 @retval Value UINTN value of the ASCII string.
233
234 **/
235 UINT64
236 AtoU64 (
237 IN UINT8 *Buffer
238 )
239 {
240 UINT64 Value;
241 UINT8 Character;
242
243 Value = 0;
244 while ((Character = *Buffer++) != '\0') {
245 Value = MultU64x32 (Value, 10) + (Character - '0');
246 }
247
248 return Value;
249 }
250