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