DriverEntryPoint.c revision 6517edbe5156bdf40f4bbd46af001d10ef81c8ed
1/** @file
2  Entry point to a EFI/DXE driver.
3
4Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>
5This 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/BaseLib.h>
23#include <Library/DebugLib.h>
24#include <Library/UefiBootServicesTableLib.h>
25
26
27/**
28  Unload function that is registered in the LoadImage protocol.  It un-installs
29  protocols produced and deallocates pool used by the driver.  Called by the core
30  when unloading the driver.
31
32  @param  ImageHandle ImageHandle of the loaded driver.
33
34  @return Status returned by all unload().
35
36**/
37EFI_STATUS
38EFIAPI
39_DriverUnloadHandler (
40  EFI_HANDLE ImageHandle
41  )
42{
43  EFI_STATUS  Status;
44
45  //
46  // If an UnloadImage() handler is specified, then call it
47  //
48  Status = ProcessModuleUnloadList (ImageHandle);
49
50  //
51  // If the driver specific unload handler does not return an error, then call all of the
52  // library destructors.  If the unload handler returned an error, then the driver can not be
53  // unloaded, and the library destructors should not be called
54  //
55  if (!EFI_ERROR (Status)) {
56    ProcessLibraryDestructorList (ImageHandle, gST);
57  }
58
59  //
60  // Return the status from the driver specific unload handler
61  //
62  return Status;
63}
64
65
66/**
67  The entry point of PE/COFF Image for a DXE Driver, DXE Runtime Driver, DXE SMM
68  Driver, or UEFI Driver.
69
70  This function is the entry point for a DXE Driver, DXE Runtime Driver, DXE SMM Driver,
71  or UEFI Driver.  This function must call ProcessLibraryConstructorList() and
72  ProcessModuleEntryPointList(). If the return status from ProcessModuleEntryPointList()
73  is an error status, then ProcessLibraryDestructorList() must be called. The return
74  value from ProcessModuleEntryPointList() is returned. If _gDriverUnloadImageCount
75  is greater than zero, then an unload handler must be registered for this image
76  and the unload handler must invoke ProcessModuleUnloadList().
77  If _gUefiDriverRevision is not zero and SystemTable->Hdr.Revision is less than
78  _gUefiDriverRevison, then return EFI_INCOMPATIBLE_VERSION.
79
80
81  @param  ImageHandle  The image handle of the DXE Driver, DXE Runtime Driver,
82                       DXE SMM Driver, or UEFI Driver.
83  @param  SystemTable  A pointer to the EFI System Table.
84
85  @retval  EFI_SUCCESS               The DXE Driver, DXE Runtime Driver, DXE SMM
86                                     Driver, or UEFI Driver exited normally.
87  @retval  EFI_INCOMPATIBLE_VERSION  _gUefiDriverRevision is greater than
88                                    SystemTable->Hdr.Revision.
89  @retval  Other                     Return value from ProcessModuleEntryPointList().
90
91**/
92EFI_STATUS
93EFIAPI
94_ModuleEntryPoint (
95  IN EFI_HANDLE        ImageHandle,
96  IN EFI_SYSTEM_TABLE  *SystemTable
97  )
98{
99  EFI_STATUS                 Status;
100  EFI_LOADED_IMAGE_PROTOCOL  *LoadedImage;
101
102  if (_gUefiDriverRevision != 0) {
103    //
104    // Make sure that the EFI/UEFI spec revision of the platform is >= EFI/UEFI spec revision of the driver
105    //
106    if (SystemTable->Hdr.Revision < _gUefiDriverRevision) {
107      return EFI_INCOMPATIBLE_VERSION;
108    }
109  }
110
111  //
112  // Call constructor for all libraries
113  //
114  ProcessLibraryConstructorList (ImageHandle, SystemTable);
115
116  //
117  //  Install unload handler...
118  //
119  if (_gDriverUnloadImageCount != 0) {
120    Status = gBS->HandleProtocol (
121                    ImageHandle,
122                    &gEfiLoadedImageProtocolGuid,
123                    (VOID **)&LoadedImage
124                    );
125    ASSERT_EFI_ERROR (Status);
126    LoadedImage->Unload = _DriverUnloadHandler;
127  }
128
129  //
130  // Call the driver entry point
131  //
132  Status = ProcessModuleEntryPointList (ImageHandle, SystemTable);
133
134  //
135  // If all of the drivers returned errors, then invoke all of the library destructors
136  //
137  if (EFI_ERROR (Status)) {
138    ProcessLibraryDestructorList (ImageHandle, SystemTable);
139  }
140
141  //
142  // Return the cummalative return status code from all of the driver entry points
143  //
144  return Status;
145}
146
147
148/**
149  Required by the EBC compiler and identical in functionality to _ModuleEntryPoint().
150
151  This function is required to call _ModuleEntryPoint() passing in ImageHandle,
152  and SystemTable.
153
154  @param  ImageHandle  The image handle of the DXE Driver, DXE Runtime Driver, DXE
155                       SMM Driver, or UEFI Driver.
156  @param  SystemTable  A pointer to the EFI System Table.
157
158  @retval  EFI_SUCCESS               The DXE Driver, DXE Runtime Driver, DXE SMM
159                                     Driver, or UEFI Driver exited normally.
160  @retval  EFI_INCOMPATIBLE_VERSION  _gUefiDriverRevision is greater than
161                                     SystemTable->Hdr.Revision.
162  @retval  Other                     Return value from ProcessModuleEntryPointList().
163**/
164EFI_STATUS
165EFIAPI
166EfiMain (
167  IN EFI_HANDLE        ImageHandle,
168  IN EFI_SYSTEM_TABLE  *SystemTable
169  )
170{
171  return _ModuleEntryPoint (ImageHandle, SystemTable);
172}
173