1 /******************************************************************************
3 * Module Name: osefixf - EFI OSL interfaces
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2014, Intel Corp.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
50 /* Local definitions */
52 #define ACPI_EFI_PRINT_LENGTH 256
55 /* Local prototypes */
76 static ACPI_PHYSICAL_ADDRESS
77 AcpiEfiGetRsdpViaGuid (
91 static EFI_FILE_HANDLE AcpiGbl_EfiCurrentVolume = NULL;
94 /******************************************************************************
96 * FUNCTION: AcpiEfiGetRsdpViaGuid
98 * PARAMETERS: Guid1 - GUID to compare
99 * Guid2 - GUID to compare
101 * RETURN: TRUE if Guid1 == Guid2
103 * DESCRIPTION: Compares two GUIDs
105 *****************************************************************************/
117 g1 = (INT32 *) Guid1;
118 g2 = (INT32 *) Guid2;
125 return (r ? FALSE : TRUE);
129 /******************************************************************************
131 * FUNCTION: AcpiEfiGetRsdpViaGuid
135 * RETURN: RSDP address if found
137 * DESCRIPTION: Find RSDP address via EFI using specified GUID.
139 *****************************************************************************/
141 static ACPI_PHYSICAL_ADDRESS
142 AcpiEfiGetRsdpViaGuid (
145 unsigned long Address = 0;
149 for (i = 0; i < ST->NumberOfTableEntries; i++)
151 if (AcpiEfiCompareGuid (&ST->ConfigurationTable[i].VendorGuid, Guid))
153 Address = (ACPI_PHYSICAL_ADDRESS)
154 ST->ConfigurationTable[i].VendorTable;
159 return ((ACPI_PHYSICAL_ADDRESS) (Address));
163 /******************************************************************************
165 * FUNCTION: AcpiOsGetRootPointer
169 * RETURN: RSDP physical address
171 * DESCRIPTION: Gets the ACPI root pointer (RSDP)
173 *****************************************************************************/
175 ACPI_PHYSICAL_ADDRESS
176 AcpiOsGetRootPointer (
179 ACPI_PHYSICAL_ADDRESS Address;
180 EFI_GUID Guid10 = ACPI_TABLE_GUID;
181 EFI_GUID Guid20 = ACPI_20_TABLE_GUID;
184 Address = AcpiEfiGetRsdpViaGuid (&Guid20);
187 Address = AcpiEfiGetRsdpViaGuid (&Guid10);
194 /******************************************************************************
196 * FUNCTION: AcpiOsMapMemory
198 * PARAMETERS: where - Physical address of memory to be mapped
199 * length - How much memory to map
201 * RETURN: Pointer to mapped memory. Null on error.
203 * DESCRIPTION: Map physical memory into caller's address space
205 *****************************************************************************/
209 ACPI_PHYSICAL_ADDRESS where,
213 return (ACPI_TO_POINTER ((ACPI_SIZE) where));
217 /******************************************************************************
219 * FUNCTION: AcpiOsUnmapMemory
221 * PARAMETERS: where - Logical address of memory to be unmapped
222 * length - How much memory to unmap
226 * DESCRIPTION: Delete a previously created mapping. Where and Length must
227 * correspond to a previous mapping exactly.
229 *****************************************************************************/
241 /******************************************************************************
243 * FUNCTION: Spinlock interfaces
245 * DESCRIPTION: No-op on single threaded BIOS
247 *****************************************************************************/
251 ACPI_SPINLOCK *OutHandle)
258 ACPI_SPINLOCK Handle)
264 ACPI_SPINLOCK Handle)
271 ACPI_SPINLOCK Handle,
272 ACPI_CPU_FLAGS Flags)
277 /******************************************************************************
279 * FUNCTION: AcpiOsAllocate
281 * PARAMETERS: Size - Amount to allocate, in bytes
283 * RETURN: Pointer to the new allocation. Null on error.
285 * DESCRIPTION: Allocate memory. Algorithm is dependent on the OS.
287 *****************************************************************************/
293 EFI_STATUS EfiStatus;
297 EfiStatus = uefi_call_wrapper (BS->AllocatePool, 3,
298 EfiLoaderData, Size, &Mem);
299 if (EFI_ERROR (EfiStatus))
301 AcpiLogError ("EFI_BOOT_SERVICES->AllocatePool(EfiLoaderData) failure.\n");
309 #ifdef USE_NATIVE_ALLOCATE_ZEROED
310 /******************************************************************************
312 * FUNCTION: AcpiOsAllocateZeroed
314 * PARAMETERS: Size - Amount to allocate, in bytes
316 * RETURN: Pointer to the new allocation. Null on error.
318 * DESCRIPTION: Allocate and zero memory. Algorithm is dependent on the OS.
320 *****************************************************************************/
323 AcpiOsAllocateZeroed (
329 Mem = AcpiOsAllocate (Size);
332 ACPI_MEMSET (Mem, 0, Size);
340 /******************************************************************************
342 * FUNCTION: AcpiOsFree
344 * PARAMETERS: Mem - Pointer to previously allocated memory
348 * DESCRIPTION: Free memory allocated via AcpiOsAllocate
350 *****************************************************************************/
357 uefi_call_wrapper (BS->FreePool, 1, Mem);
361 /*******************************************************************************
363 * FUNCTION: AcpiOsOpenFile
365 * PARAMETERS: Path - File path
366 * Modes - File operation type
368 * RETURN: File descriptor
370 * DESCRIPTION: Open a file for reading (ACPI_FILE_READING) or/and writing
371 * (ACPI_FILE_WRITING).
373 ******************************************************************************/
380 EFI_STATUS EfiStatus = EFI_SUCCESS;
382 EFI_FILE_HANDLE EfiFile = NULL;
383 CHAR16 *Path16 = NULL;
396 OpenModes = EFI_FILE_MODE_READ;
397 if (Modes & ACPI_FILE_WRITING)
399 OpenModes |= (EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE);
402 /* Allocate path buffer */
404 Count = ACPI_STRLEN (Path);
405 Path16 = ACPI_ALLOCATE_ZEROED ((Count + 1) * sizeof (CHAR16));
408 EfiStatus = EFI_BAD_BUFFER_SIZE;
413 while (*Pos == '/' || *Pos == '\\')
418 for (i = 0; i < Count; i++)
432 EfiStatus = uefi_call_wrapper (AcpiGbl_EfiCurrentVolume->Open, 5,
433 AcpiGbl_EfiCurrentVolume, &EfiFile, Path16, OpenModes, 0);
434 if (EFI_ERROR (EfiStatus))
436 AcpiLogError ("EFI_FILE_HANDLE->Open() failure.\n");
447 return ((ACPI_FILE) EfiFile);
451 /*******************************************************************************
453 * FUNCTION: AcpiOsCloseFile
455 * PARAMETERS: File - File descriptor
459 * DESCRIPTION: Close a file.
461 ******************************************************************************/
467 EFI_FILE_HANDLE EfiFile;
470 if (File == ACPI_FILE_OUT ||
471 File == ACPI_FILE_ERR)
475 EfiFile = (EFI_FILE_HANDLE) File;
476 (void) uefi_call_wrapper (AcpiGbl_EfiCurrentVolume->Close, 1, EfiFile);
482 /*******************************************************************************
484 * FUNCTION: AcpiOsReadFile
486 * PARAMETERS: File - File descriptor
487 * Buffer - Data buffer
488 * Size - Data block size
489 * Count - Number of data blocks
491 * RETURN: Size of successfully read buffer
493 * DESCRIPTION: Read from a file.
495 ******************************************************************************/
505 EFI_FILE_HANDLE EfiFile;
507 EFI_STATUS EfiStatus;
510 if (File == ACPI_FILE_OUT ||
511 File == ACPI_FILE_ERR)
516 EfiFile = (EFI_FILE_HANDLE) File;
521 ReadSize = Size * Count;
523 EfiStatus = uefi_call_wrapper (AcpiGbl_EfiCurrentVolume->Read, 3,
524 EfiFile, &ReadSize, Buffer);
525 if (EFI_ERROR (EfiStatus))
527 AcpiLogError ("EFI_FILE_HANDLE->Read() failure.\n");
539 /*******************************************************************************
541 * FUNCTION: AcpiEfiFlushFile
543 * PARAMETERS: File - File descriptor
544 * Begin - String with boundary
545 * End - Boundary of the string
546 * Pos - Current position
547 * FlushAll - Whether checking boundary before flushing
549 * RETURN: Updated position
551 * DESCRIPTION: Flush cached buffer to the file.
553 ******************************************************************************/
564 if (FlushAll || Pos >= (End - 1))
567 uefi_call_wrapper (File->OutputString, 2, File, Begin);
575 /*******************************************************************************
577 * FUNCTION: AcpiOsWriteFile
579 * PARAMETERS: File - File descriptor
580 * Buffer - Data buffer
581 * Size - Data block size
582 * Count - Number of data blocks
584 * RETURN: Size of successfully written buffer
586 * DESCRIPTION: Write to a file.
588 ******************************************************************************/
598 CHAR16 String[ACPI_EFI_PRINT_LENGTH];
603 EFI_FILE_HANDLE EfiFile;
605 EFI_STATUS EfiStatus;
608 if (File == ACPI_FILE_OUT ||
609 File == ACPI_FILE_ERR)
612 End = String + ACPI_EFI_PRINT_LENGTH - 1;
613 Ascii = ACPI_CAST_PTR (const char, Buffer);
616 for (j = 0; j < Count; j++)
618 for (i = 0; i < Size; i++)
623 Pos = AcpiEfiFlushFile (File, String,
628 Pos = AcpiEfiFlushFile (File, String,
632 Pos = AcpiEfiFlushFile (File, String, End, Pos, TRUE);
636 EfiFile = (EFI_FILE_HANDLE) File;
641 WriteSize = Size * Count;
643 EfiStatus = uefi_call_wrapper (AcpiGbl_EfiCurrentVolume->Write, 3,
644 EfiFile, &WriteSize, Buffer);
645 if (EFI_ERROR (EfiStatus))
647 AcpiLogError ("EFI_FILE_HANDLE->Write() failure.\n");
659 /*******************************************************************************
661 * FUNCTION: AcpiOsGetFileOffset
663 * PARAMETERS: File - File descriptor
665 * RETURN: Size of current position
667 * DESCRIPTION: Get current file offset.
669 ******************************************************************************/
672 AcpiOsGetFileOffset (
682 /*******************************************************************************
684 * FUNCTION: AcpiOsSetFileOffset
686 * PARAMETERS: File - File descriptor
687 * Offset - File offset
688 * From - From begin/end of file
692 * DESCRIPTION: Set current file offset.
694 ******************************************************************************/
697 AcpiOsSetFileOffset (
707 /******************************************************************************
709 * FUNCTION: AcpiOsPrintf
711 * PARAMETERS: Format, ... - Standard printf format
715 * DESCRIPTION: Formatted output.
717 *****************************************************************************/
719 void ACPI_INTERNAL_VAR_XFACE
727 va_start (Args, Format);
728 AcpiOsVprintf (Format, Args);
733 /******************************************************************************
735 * FUNCTION: AcpiOsVprintf
737 * PARAMETERS: Format - Standard printf format
738 * Args - Argument list
742 * DESCRIPTION: Formatted output with arguments list pointer.
744 *****************************************************************************/
752 (void) AcpiUtFileVprintf (ACPI_FILE_OUT, Format, Args);
756 /******************************************************************************
758 * FUNCTION: AcpiOsInitialize
764 * DESCRIPTION: Initialize this module.
766 *****************************************************************************/
777 /******************************************************************************
779 * FUNCTION: AcpiEfiArgify
781 * PARAMETERS: String - Pointer to command line argument strings
782 * which are seperated with spaces
783 * ArgcPtr - Return number of the arguments
784 * ArgvPtr - Return vector of the arguments
788 * DESCRIPTION: Convert EFI arguments into C arguments.
790 *****************************************************************************/
799 int MaxArgc = *ArgcPtr;
801 char **Argv = *ArgvPtr;
803 BOOLEAN IsSingleQuote = FALSE;
804 BOOLEAN IsDoubleQuote = FALSE;
805 BOOLEAN IsEscape = FALSE;
810 return (AE_BAD_PARAMETER);
815 while (*String != '\0')
817 while (ACPI_IS_SPACE (*String))
822 while (*String != '\0')
824 if (ACPI_IS_SPACE (*String) &&
825 !IsSingleQuote && !IsDoubleQuote && !IsEscape)
836 else if (*String == '\\')
840 else if (IsSingleQuote)
844 IsSingleQuote = FALSE;
852 else if (IsDoubleQuote)
856 IsDoubleQuote = FALSE;
868 IsSingleQuote = TRUE;
870 else if (*String == '"')
872 IsDoubleQuote = TRUE;
881 if (Argv && Argc < MaxArgc)
883 Argv[Argc] = CopyBuffer;
888 if (Argv && Argc < MaxArgc)
896 return ((MaxArgc < Argc) ? AE_NO_MEMORY : AE_OK);
900 /******************************************************************************
902 * FUNCTION: AcpiEfiConvertArgcv
904 * PARAMETERS: LoadOptions - Pointer to the EFI options buffer, which
906 * LoadOptionsSize - Size of the EFI options buffer
907 * ArgcPtr - Return number of the arguments
908 * ArgvPtr - Return vector of the arguments
909 * BufferPtr - Buffer to contain the argument strings
913 * DESCRIPTION: Convert EFI arguments into C arguments.
915 *****************************************************************************/
918 AcpiEfiConvertArgcv (
920 UINT32 LoadOptionsSize,
925 ACPI_STATUS Status = AE_OK;
926 UINT32 Count = LoadOptionsSize / sizeof (CHAR16);
935 /* Prepare a buffer to contain the argument strings */
937 Buffer = ACPI_ALLOCATE_ZEROED (Count);
940 Status = AE_NO_MEMORY;
946 /* Extend the argument vector */
955 Argv = ACPI_ALLOCATE_ZEROED (sizeof (char *) * (Argc + 1));
958 Status = AE_NO_MEMORY;
964 * Note: As AcpiEfiArgify() will modify the content of the buffer, so
965 * we need to restore it each time before invoking
969 To = ACPI_CAST_PTR (char, Buffer);
970 for (i = 0; i < Count; i++)
972 *To++ = (char) *From++;
976 * The "Buffer" will contain NULL terminated strings after invoking
977 * AcpiEfiArgify(). The number of the strings are saved in Argc and the
978 * pointers of the strings are saved in Argv.
980 Status = AcpiEfiArgify (Buffer, &Argc, &Argv);
981 if (ACPI_FAILURE (Status))
983 if (Status == AE_NO_MEMORY)
991 if (ACPI_FAILURE (Status))
1000 *BufferPtr = Buffer;
1006 /******************************************************************************
1008 * FUNCTION: efi_main
1010 * PARAMETERS: Image - EFI image handle
1011 * SystemTab - EFI system table
1013 * RETURN: EFI Status
1015 * DESCRIPTION: Entry point of EFI executable
1017 *****************************************************************************/
1022 EFI_SYSTEM_TABLE *SystemTab)
1024 EFI_LOADED_IMAGE *Info;
1025 EFI_STATUS EfiStatus = EFI_SUCCESS;
1029 char *OptBuffer = NULL;
1030 EFI_FILE_IO_INTERFACE *Volume = NULL;
1033 /* Initialize EFI library */
1035 InitializeLib (Image, SystemTab);
1037 /* Retrieve image information */
1039 EfiStatus = uefi_call_wrapper (BS->HandleProtocol, 3,
1040 Image, &LoadedImageProtocol, ACPI_CAST_PTR (VOID, &Info));
1041 if (EFI_ERROR (EfiStatus))
1043 AcpiLogError ("EFI_BOOT_SERVICES->HandleProtocol(LoadedImageProtocol) failure.\n");
1046 EfiStatus = uefi_call_wrapper (BS->HandleProtocol, 3,
1047 Info->DeviceHandle, &FileSystemProtocol, (void **) &Volume);
1048 if (EFI_ERROR (EfiStatus))
1050 AcpiLogError ("EFI_BOOT_SERVICES->HandleProtocol(FileSystemProtocol) failure.\n");
1053 EfiStatus = uefi_call_wrapper (Volume->OpenVolume, 2,
1054 Volume, &AcpiGbl_EfiCurrentVolume);
1055 if (EFI_ERROR (EfiStatus))
1057 AcpiLogError ("EFI_FILE_IO_INTERFACE->OpenVolume() failure.\n");
1061 Status = AcpiEfiConvertArgcv (Info->LoadOptions,
1062 Info->LoadOptionsSize, &argc, &argv, &OptBuffer);
1063 if (ACPI_FAILURE (Status))
1065 EfiStatus = EFI_DEVICE_ERROR;
1069 acpi_main (argc, argv);
1079 ACPI_FREE (OptBuffer);