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