]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/TianoTools/Common/CommonLib.c
Fixes for Cygwin gcc builds.
[mirror_edk2.git] / Tools / Source / TianoTools / Common / CommonLib.c
1 /*++
2
3 Copyright (c) 2004, 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 CommonLib.c
15
16 Abstract:
17
18 Common Library Functions
19
20 --*/
21
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include "CommonLib.h"
26
27 VOID
28 PeiZeroMem (
29 IN VOID *Buffer,
30 IN UINTN Size
31 )
32 /*++
33
34 Routine Description:
35
36 Set Buffer to zero for Size bytes.
37
38 Arguments:
39
40 Buffer - Memory to set.
41
42 Size - Number of bytes to set
43
44 Returns:
45
46 None
47
48 --*/
49 {
50 INT8 *Ptr;
51
52 Ptr = Buffer;
53 while (Size--) {
54 *(Ptr++) = 0;
55 }
56 }
57
58 VOID
59 PeiCopyMem (
60 IN VOID *Destination,
61 IN VOID *Source,
62 IN UINTN Length
63 )
64 /*++
65
66 Routine Description:
67
68 Copy Length bytes from Source to Destination.
69
70 Arguments:
71
72 Destination - Target of copy
73
74 Source - Place to copy from
75
76 Length - Number of bytes to copy
77
78 Returns:
79
80 None
81
82 --*/
83 {
84 CHAR8 *Destination8;
85 CHAR8 *Source8;
86
87 Destination8 = Destination;
88 Source8 = Source;
89 while (Length--) {
90 *(Destination8++) = *(Source8++);
91 }
92 }
93
94 VOID
95 ZeroMem (
96 IN VOID *Buffer,
97 IN UINTN Size
98 )
99 {
100 PeiZeroMem (Buffer, Size);
101 }
102
103 VOID
104 CopyMem (
105 IN VOID *Destination,
106 IN VOID *Source,
107 IN UINTN Length
108 )
109 {
110 PeiCopyMem (Destination, Source, Length);
111 }
112
113 INTN
114 CompareGuid (
115 IN EFI_GUID *Guid1,
116 IN EFI_GUID *Guid2
117 )
118 /*++
119
120 Routine Description:
121
122 Compares to GUIDs
123
124 Arguments:
125
126 Guid1 - guid to compare
127 Guid2 - guid to compare
128
129 Returns:
130 = 0 if Guid1 == Guid2
131 != 0 if Guid1 != Guid2
132
133 --*/
134 {
135 INT32 *g1;
136 INT32 *g2;
137 INT32 r;
138
139 //
140 // Compare 32 bits at a time
141 //
142 g1 = (INT32 *) Guid1;
143 g2 = (INT32 *) Guid2;
144
145 r = g1[0] - g2[0];
146 r |= g1[1] - g2[1];
147 r |= g1[2] - g2[2];
148 r |= g1[3] - g2[3];
149
150 return r;
151 }
152
153 EFI_STATUS
154 GetFileImage (
155 IN CHAR8 *InputFileName,
156 OUT CHAR8 **InputFileImage,
157 OUT UINT32 *BytesRead
158 )
159 /*++
160
161 Routine Description:
162
163 This function opens a file and reads it into a memory buffer. The function
164 will allocate the memory buffer and returns the size of the buffer.
165
166 Arguments:
167
168 InputFileName The name of the file to read.
169 InputFileImage A pointer to the memory buffer.
170 BytesRead The size of the memory buffer.
171
172 Returns:
173
174 EFI_SUCCESS The function completed successfully.
175 EFI_INVALID_PARAMETER One of the input parameters was invalid.
176 EFI_ABORTED An error occurred.
177 EFI_OUT_OF_RESOURCES No resource to complete operations.
178
179 --*/
180 {
181 FILE *InputFile;
182 UINT32 FileSize;
183
184 //
185 // Verify input parameters.
186 //
187 if (InputFileName == NULL || strlen (InputFileName) == 0 || InputFileImage == NULL) {
188 return EFI_INVALID_PARAMETER;
189 }
190 //
191 // Open the file and copy contents into a memory buffer.
192 //
193 //
194 // Open the file
195 //
196 InputFile = fopen (InputFileName, "rb");
197 if (InputFile == NULL) {
198 printf ("ERROR: Could not open input file \"%s\".\n", InputFileName);
199 return EFI_ABORTED;
200 }
201 //
202 // Go to the end so that we can determine the file size
203 //
204 if (fseek (InputFile, 0, SEEK_END)) {
205 printf ("ERROR: System error reading input file \"%s\".\n", InputFileName);
206 fclose (InputFile);
207 return EFI_ABORTED;
208 }
209 //
210 // Get the file size
211 //
212 FileSize = ftell (InputFile);
213 if (FileSize == -1) {
214 printf ("ERROR: System error parsing input file \"%s\".\n", InputFileName);
215 fclose (InputFile);
216 return EFI_ABORTED;
217 }
218 //
219 // Allocate a buffer
220 //
221 *InputFileImage = malloc (FileSize);
222 if (*InputFileImage == NULL) {
223 fclose (InputFile);
224 return EFI_OUT_OF_RESOURCES;
225 }
226 //
227 // Reset to the beginning of the file
228 //
229 if (fseek (InputFile, 0, SEEK_SET)) {
230 printf ("ERROR: System error reading input file \"%s\".\n", InputFileName);
231 fclose (InputFile);
232 free (*InputFileImage);
233 *InputFileImage = NULL;
234 return EFI_ABORTED;
235 }
236 //
237 // Read all of the file contents.
238 //
239 *BytesRead = fread (*InputFileImage, sizeof (UINT8), FileSize, InputFile);
240 if (*BytesRead != sizeof (UINT8) * FileSize) {
241 printf ("ERROR: Reading file \"%s\"%i.\n", InputFileName);
242 fclose (InputFile);
243 free (*InputFileImage);
244 *InputFileImage = NULL;
245 return EFI_ABORTED;
246 }
247 //
248 // Close the file
249 //
250 fclose (InputFile);
251
252 return EFI_SUCCESS;
253 }
254
255 UINT8
256 CalculateChecksum8 (
257 IN UINT8 *Buffer,
258 IN UINTN Size
259 )
260 /*++
261
262 Routine Description:
263
264 This function calculates the value needed for a valid UINT8 checksum
265
266 Arguments:
267
268 Buffer Pointer to buffer containing byte data of component.
269 Size Size of the buffer
270
271 Returns:
272
273 The 8 bit checksum value needed.
274
275 --*/
276 {
277 return (UINT8) (0x100 - CalculateSum8 (Buffer, Size));
278 }
279
280 UINT8
281 CalculateSum8 (
282 IN UINT8 *Buffer,
283 IN UINT32 Size
284 )
285 /*++
286
287 Routine Description::
288
289 This function calculates the UINT8 sum for the requested region.
290
291 Arguments:
292
293 Buffer Pointer to buffer containing byte data of component.
294 Size Size of the buffer
295
296 Returns:
297
298 The 8 bit checksum value needed.
299
300 --*/
301 {
302 UINTN Index;
303 UINT8 Sum;
304
305 Sum = 0;
306
307 //
308 // Perform the byte sum for buffer
309 //
310 for (Index = 0; Index < Size; Index++) {
311 Sum = (UINT8) (Sum + Buffer[Index]);
312 }
313
314 return Sum;
315 }
316
317 UINT16
318 CalculateChecksum16 (
319 IN UINT16 *Buffer,
320 IN UINTN Size
321 )
322 /*++
323
324 Routine Description::
325
326 This function calculates the value needed for a valid UINT16 checksum
327
328 Arguments:
329
330 Buffer Pointer to buffer containing byte data of component.
331 Size Size of the buffer
332
333 Returns:
334
335 The 16 bit checksum value needed.
336
337 --*/
338 {
339 return (UINT16) (0x10000 - CalculateSum16 (Buffer, Size));
340 }
341
342 UINT16
343 CalculateSum16 (
344 IN UINT16 *Buffer,
345 IN UINTN Size
346 )
347 /*++
348
349 Routine Description:
350
351 This function calculates the UINT16 sum for the requested region.
352
353 Arguments:
354
355 Buffer Pointer to buffer containing byte data of component.
356 Size Size of the buffer
357
358 Returns:
359
360 The 16 bit checksum
361
362 --*/
363 {
364 UINTN Index;
365 UINT16 Sum;
366
367 Sum = 0;
368
369 //
370 // Perform the word sum for buffer
371 //
372 for (Index = 0; Index < Size; Index++) {
373 Sum = (UINT16) (Sum + Buffer[Index]);
374 }
375
376 return (UINT16) Sum;
377 }
378
379 EFI_STATUS
380 PrintGuid (
381 IN EFI_GUID *Guid
382 )
383 /*++
384
385 Routine Description:
386
387 This function prints a GUID to STDOUT.
388
389 Arguments:
390
391 Guid Pointer to a GUID to print.
392
393 Returns:
394
395 EFI_SUCCESS The GUID was printed.
396 EFI_INVALID_PARAMETER The input was NULL.
397
398 --*/
399 {
400 if (Guid == NULL) {
401 printf ("ERROR: PrintGuid called with a NULL value.\n");
402 return EFI_INVALID_PARAMETER;
403 }
404
405 printf (
406 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x\n",
407 Guid->Data1,
408 Guid->Data2,
409 Guid->Data3,
410 Guid->Data4[0],
411 Guid->Data4[1],
412 Guid->Data4[2],
413 Guid->Data4[3],
414 Guid->Data4[4],
415 Guid->Data4[5],
416 Guid->Data4[6],
417 Guid->Data4[7]
418 );
419 return EFI_SUCCESS;
420 }
421
422 EFI_STATUS
423 PrintGuidToBuffer (
424 IN EFI_GUID *Guid,
425 IN OUT UINT8 *Buffer,
426 IN UINT32 BufferLen,
427 IN BOOLEAN Uppercase
428 )
429 /*++
430
431 Routine Description:
432
433 This function prints a GUID to a buffer
434
435 Arguments:
436
437 Guid - Pointer to a GUID to print.
438 Buffer - Pointer to a user-provided buffer to print to
439 BufferLen - Size of the Buffer
440 Uppercase - If use upper case.
441
442 Returns:
443
444 EFI_SUCCESS The GUID was printed.
445 EFI_INVALID_PARAMETER The input was NULL.
446 EFI_BUFFER_TOO_SMALL The input buffer was not big enough
447
448 --*/
449 {
450 if (Guid == NULL) {
451 printf ("ERROR: PrintGuidToBuffer() called with a NULL value\n");
452 return EFI_INVALID_PARAMETER;
453 }
454
455 if (BufferLen < PRINTED_GUID_BUFFER_SIZE) {
456 printf ("ERORR: PrintGuidToBuffer() called with invalid buffer size\n");
457 return EFI_BUFFER_TOO_SMALL;
458 }
459
460 if (Uppercase) {
461 sprintf (
462 Buffer,
463 "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X",
464 Guid->Data1,
465 Guid->Data2,
466 Guid->Data3,
467 Guid->Data4[0],
468 Guid->Data4[1],
469 Guid->Data4[2],
470 Guid->Data4[3],
471 Guid->Data4[4],
472 Guid->Data4[5],
473 Guid->Data4[6],
474 Guid->Data4[7]
475 );
476 } else {
477 sprintf (
478 Buffer,
479 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
480 Guid->Data1,
481 Guid->Data2,
482 Guid->Data3,
483 Guid->Data4[0],
484 Guid->Data4[1],
485 Guid->Data4[2],
486 Guid->Data4[3],
487 Guid->Data4[4],
488 Guid->Data4[5],
489 Guid->Data4[6],
490 Guid->Data4[7]
491 );
492 }
493
494 return EFI_SUCCESS;
495 }
496
497 #ifdef __GNUC__
498 #ifndef __CYGWIN__
499 char *strlwr(char *s)
500 {
501 char *p = s;
502 for(;*s;s++) {
503 *s = tolower(*s);
504 }
505 return p;
506 }
507 #endif
508 #endif