RuntimeLib.c revision 6c401377339d53eb855a0b38986cfb6363dd6fb6
1/** @file
2  UEFI Runtime Library implementation for non IPF processor types.
3
4Copyright (c) 2006 - 2008 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#include "RuntimeLibInternal.h"
16
17///
18/// Driver Lib Module Globals
19///
20
21EFI_EVENT              mEfiVirtualNotifyEvent;
22EFI_EVENT              mEfiExitBootServicesEvent;
23BOOLEAN                mEfiGoneVirtual         = FALSE;
24BOOLEAN                mEfiAtRuntime           = FALSE;
25EFI_RUNTIME_SERVICES          *mRT;
26
27/**
28  Set AtRuntime flag as TRUE after ExitBootServices.
29
30  @param[in]  Event   The Event that is being processed
31  @param[in]  Context Event Context
32**/
33VOID
34EFIAPI
35RuntimeLibExitBootServicesEvent (
36  IN EFI_EVENT        Event,
37  IN VOID             *Context
38  )
39{
40  //
41  // Clear out BootService globals
42  //
43  gBS             = NULL;
44
45  mEfiAtRuntime = TRUE;
46}
47
48/**
49  Fixup internal data so that EFI can be call in virtual mode.
50  Call the passed in Child Notify event and convert any pointers in
51  lib to virtual mode.
52
53  @param[in]    Event   The Event that is being processed
54  @param[in]    Context Event Context
55**/
56VOID
57EFIAPI
58RuntimeLibVirtualNotifyEvent (
59  IN EFI_EVENT        Event,
60  IN VOID             *Context
61  )
62{
63  //
64  // Update global for Runtime Services Table and IO
65  //
66  EfiConvertPointer (0, (VOID **) &mRT);
67
68  mEfiGoneVirtual = TRUE;
69}
70
71/**
72  Intialize runtime Driver Lib if it has not yet been initialized.
73  It will ASSERT() if gRT is NULL or gBS is NULL.
74  It will ASSERT() if that operation fails.
75
76  @param[in]  ImageHandle   The firmware allocated handle for the EFI image.
77  @param[in]  SystemTable   A pointer to the EFI System Table.
78
79  @return     EFI_STATUS    always returns EFI_SUCCESS except EFI_ALREADY_STARTED if already started.
80**/
81EFI_STATUS
82EFIAPI
83RuntimeDriverLibConstruct (
84  IN EFI_HANDLE           ImageHandle,
85  IN EFI_SYSTEM_TABLE     *SystemTable
86  )
87{
88  EFI_STATUS  Status;
89
90  ASSERT (gRT != NULL);
91  ASSERT (gBS != NULL);
92
93  mRT = gRT;
94  //
95  // Register SetVirtualAddressMap () notify function
96  //
97  Status = gBS->CreateEvent (
98                  EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE,
99                  TPL_NOTIFY,
100                  RuntimeLibVirtualNotifyEvent,
101                  NULL,
102                  &mEfiVirtualNotifyEvent
103                  );
104
105  ASSERT_EFI_ERROR (Status);
106
107  Status = gBS->CreateEvent (
108                  EVT_SIGNAL_EXIT_BOOT_SERVICES,
109                  TPL_NOTIFY,
110                  RuntimeLibExitBootServicesEvent,
111                  NULL,
112                  &mEfiExitBootServicesEvent
113                  );
114
115  ASSERT_EFI_ERROR (Status);
116
117  return Status;
118}
119
120/**
121  If a runtime driver exits with an error, it must call this routine
122  to free the allocated resource before the exiting.
123  It will ASSERT() if gBS is NULL.
124  It will ASSERT() if that operation fails.
125
126  @param[in]  ImageHandle   The firmware allocated handle for the EFI image.
127  @param[in]  SystemTable   A pointer to the EFI System Table.
128
129  @retval     EFI_SUCCESS       Shutdown the Runtime Driver Lib successfully
130  @retval     EFI_UNSUPPORTED   Runtime Driver lib was not initialized at all
131**/
132EFI_STATUS
133EFIAPI
134RuntimeDriverLibDeconstruct (
135  IN EFI_HANDLE        ImageHandle,
136  IN EFI_SYSTEM_TABLE  *SystemTable
137  )
138{
139  EFI_STATUS  Status;
140
141  //
142  // Close SetVirtualAddressMap () notify function
143  //
144  ASSERT (gBS != NULL);
145  Status = gBS->CloseEvent (mEfiVirtualNotifyEvent);
146  ASSERT_EFI_ERROR (Status);
147
148  Status = gBS->CloseEvent (mEfiExitBootServicesEvent);
149  ASSERT_EFI_ERROR (Status);
150
151  return Status;
152}
153
154/**
155  Return TRUE if ExitBootServices () has been called.
156
157  @retval TRUE If ExitBootServices () has been called
158**/
159BOOLEAN
160EFIAPI
161EfiAtRuntime (
162  VOID
163  )
164{
165  return mEfiAtRuntime;
166}
167
168/**
169  Return TRUE if SetVirtualAddressMap () has been called.
170
171  @retval TRUE  If SetVirtualAddressMap () has been called
172**/
173BOOLEAN
174EFIAPI
175EfiGoneVirtual (
176  VOID
177  )
178{
179  return mEfiGoneVirtual;
180}
181
182