DriverEntryPoint.c revision f6d2bcc6fa8161d20ff2b3601c94c566b5a9bc35
1/** @file
2  Entry point to a EFI/DXE driver.
3
4Copyright (c) 2006 - 2007, Intel Corporation<BR>
5All rights reserved. This program and the accompanying materials
6are licensed and made available under the terms and conditions of the BSD License
7which accompanies this distribution.  The full text of the license may be found at
8http://opensource.org/licenses/bsd-license.php
9
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13**/
14
15
16
17#include <Uefi.h>
18
19#include <Protocol/LoadedImage.h>
20
21#include <Library/UefiDriverEntryPoint.h>
22#include <Library/DebugLib.h>
23#include <Library/UefiBootServicesTableLib.h>
24
25
26/**
27  Unload function that is registered in the LoadImage protocol.  It un-installs
28  protocols produced and deallocates pool used by the driver.  Called by the core
29  when unloading the driver.
30
31  @param  ImageHandle ImageHandle of the loaded driver.
32
33  @return Status returned by all unload().
34
35**/
36EFI_STATUS
37EFIAPI
38_DriverUnloadHandler (
39  EFI_HANDLE ImageHandle
40  )
41{
42  EFI_STATUS  Status;
43
44  //
45  // If an UnloadImage() handler is specified, then call it
46  //
47  Status = ProcessModuleUnloadList (ImageHandle);
48
49  //
50  // If the driver specific unload handler does not return an error, then call all of the
51  // library destructors.  If the unload handler returned an error, then the driver can not be
52  // unloaded, and the library destructors should not be called
53  //
54  if (!EFI_ERROR (Status)) {
55    ProcessLibraryDestructorList (ImageHandle, gST);
56  }
57
58  //
59  // Return the status from the driver specific unload handler
60  //
61  return Status;
62}
63
64
65/**
66  The entry point of PE/COFF Image for a DXE Driver, DXE Runtime Driver, DXE SMM Driver, or UEFI Driver.
67
68  This function is the entry point for a DXE Driver, DXE Runtime Driver, DXE SMM Driver,
69  or UEFI Driver.  This function must call ProcessLibraryConstructorList() and
70  ProcessModuleEntryPointList(). If the return status from ProcessModuleEntryPointList()
71  is an error status, then ProcessLibraryDestructorList() must be called. The return value
72  from ProcessModuleEntryPointList() is returned. If _gDriverUnloadImageCount is greater
73  than zero, then an unload handler must be registered for this image and the unload handler
74  must invoke ProcessModuleUnloadList().
75  If _gUefiDriverRevision is not zero and SystemTable->Hdr.Revision is less than _gUefiDriverRevison,
76  then return EFI_INCOMPATIBLE_VERSION.
77
78
79  @param  ImageHandle  The image handle of the DXE Driver, DXE Runtime Driver, DXE SMM Driver, or UEFI Driver.
80  @param  SystemTable  A pointer to the EFI System Table.
81
82  @retval  EFI_SUCCESS               The DXE Driver, DXE Runtime Driver, DXE SMM Driver,
83                                     or UEFI Driver exited normally.
84  @retval  EFI_INCOMPATIBLE_VERSION  _gUefiDriverRevision is greater than SystemTable->Hdr.Revision.
85  @retval  Other                     Return value from ProcessModuleEntryPointList().
86
87**/
88EFI_STATUS
89EFIAPI
90_ModuleEntryPoint (
91  IN EFI_HANDLE        ImageHandle,
92  IN EFI_SYSTEM_TABLE  *SystemTable
93  )
94{
95  EFI_STATUS                 Status;
96  EFI_LOADED_IMAGE_PROTOCOL  *LoadedImage;
97
98  if (_gUefiDriverRevision != 0) {
99    //
100    // Make sure that the EFI/UEFI spec revision of the platform is >= EFI/UEFI spec revision of the driver
101    //
102    if (SystemTable->Hdr.Revision < _gUefiDriverRevision) {
103      return EFI_INCOMPATIBLE_VERSION;
104    }
105  }
106
107  //
108  // Call constructor for all libraries
109  //
110  ProcessLibraryConstructorList (ImageHandle, SystemTable);
111
112  //
113  //  Install unload handler...
114  //
115  if (_gDriverUnloadImageCount != 0) {
116    Status = gBS->HandleProtocol (
117                    ImageHandle,
118                    &gEfiLoadedImageProtocolGuid,
119                    (VOID **)&LoadedImage
120                    );
121    ASSERT_EFI_ERROR (Status);
122    LoadedImage->Unload = _DriverUnloadHandler;
123  }
124
125  //
126  // Call the driver entry point
127  //
128  Status = ProcessModuleEntryPointList (ImageHandle, SystemTable);
129
130  //
131  // If all of the drivers returned errors, then invoke all of the library destructors
132  //
133  if (EFI_ERROR (Status)) {
134    ProcessLibraryDestructorList (ImageHandle, SystemTable);
135  }
136
137  //
138  // Return the cummalative return status code from all of the driver entry points
139  //
140  return Status;
141}
142
143
144/**
145  Required by the EBC compiler and identical in functionality to _ModuleEntryPoint().
146
147  This function is required to call _ModuleEntryPoint() passing in ImageHandle, and SystemTable.
148
149  @param  ImageHandle  The image handle of the DXE Driver, DXE Runtime Driver, DXE SMM Driver, or UEFI Driver.
150  @param  SystemTable  A pointer to the EFI System Table.
151
152  @retval  EFI_SUCCESS               The DXE Driver, DXE Runtime Driver, DXE SMM Driver,
153                                     or UEFI Driver exited normally.
154  @retval  EFI_INCOMPATIBLE_VERSION  _gUefiDriverRevision is greater than SystemTable->Hdr.Revision.
155  @retval  Other                     Return value from ProcessModuleEntryPointList().
156**/
157EFI_STATUS
158EFIAPI
159EfiMain (
160  IN EFI_HANDLE        ImageHandle,
161  IN EFI_SYSTEM_TABLE  *SystemTable
162  )
163{
164  return _ModuleEntryPoint (ImageHandle, SystemTable);
165}
166