RuntimeLib.c revision a9b896f453c83a6f0af653c9c153014a37129453
142eedea958591087603bbacd1c2227d2494026afyshang/** @file
260c93673b3189b7a48acdb5c300f4ee3546ffb85lgao  UEFI Runtime Library implementation for non IPF processor types.
3ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang
460c93673b3189b7a48acdb5c300f4ee3546ffb85lgaoCopyright (c) 2006 - 2008 Intel Corporation. <BR>
5ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshangAll rights reserved. This program and the accompanying materials
6ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshangare licensed and made available under the terms and conditions of the BSD License
7ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshangwhich accompanies this distribution.  The full text of the license may be found at
8ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshanghttp://opensource.org/licenses/bsd-license.php
9ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang
10ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshangTHE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshangWITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang
13ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang**/
14ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang
156c401377339d53eb855a0b38986cfb6363dd6fb6qhuang#include "RuntimeLibInternal.h"
16ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang
17ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang///
18ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang/// Driver Lib Module Globals
19ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang///
20ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang
21fe467413d2230a2170bb3ffc91e5e8c909920628jjiEFI_EVENT              mEfiVirtualNotifyEvent;
22fe467413d2230a2170bb3ffc91e5e8c909920628jjiEFI_EVENT              mEfiExitBootServicesEvent;
23fe467413d2230a2170bb3ffc91e5e8c909920628jjiBOOLEAN                mEfiGoneVirtual         = FALSE;
24fe467413d2230a2170bb3ffc91e5e8c909920628jjiBOOLEAN                mEfiAtRuntime           = FALSE;
25a9b896f453c83a6f0af653c9c153014a37129453qhuangEFI_RUNTIME_SERVICES   *mRT;
26ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang
27ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang/**
2842eedea958591087603bbacd1c2227d2494026afyshang  Set AtRuntime flag as TRUE after ExitBootServices.
29ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang
30ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang  @param[in]  Event   The Event that is being processed
31ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang  @param[in]  Context Event Context
32ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang**/
33ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshangVOID
34ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshangEFIAPI
3521998b6778c9b77085330c4711f4cc55aa7c1e41yshangRuntimeLibExitBootServicesEvent (
36ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang  IN EFI_EVENT        Event,
37ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang  IN VOID             *Context
38ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang  )
39ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang{
40ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang  //
41ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang  // Clear out BootService globals
42ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang  //
43ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang  gBS             = NULL;
44ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang
45ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang  mEfiAtRuntime = TRUE;
46ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang}
47ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang
48ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang/**
49ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang  Fixup internal data so that EFI can be call in virtual mode.
50ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang  Call the passed in Child Notify event and convert any pointers in
51ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang  lib to virtual mode.
52ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang
53ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang  @param[in]    Event   The Event that is being processed
54ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang  @param[in]    Context Event Context
55ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang**/
56ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshangVOID
57ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshangEFIAPI
58ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshangRuntimeLibVirtualNotifyEvent (
59ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang  IN EFI_EVENT        Event,
60ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang  IN VOID             *Context
61ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang  )
62ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang{
63ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang  //
64ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang  // Update global for Runtime Services Table and IO
65ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang  //
66ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang  EfiConvertPointer (0, (VOID **) &mRT);
67ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang
68ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang  mEfiGoneVirtual = TRUE;
69ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang}
70ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang
71ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang/**
72a9b896f453c83a6f0af653c9c153014a37129453qhuang  Initialize runtime Driver Lib if it has not yet been initialized.
7360c93673b3189b7a48acdb5c300f4ee3546ffb85lgao  It will ASSERT() if gRT is NULL or gBS is NULL.
7460c93673b3189b7a48acdb5c300f4ee3546ffb85lgao  It will ASSERT() if that operation fails.
75ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang
76ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang  @param[in]  ImageHandle   The firmware allocated handle for the EFI image.
77ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang  @param[in]  SystemTable   A pointer to the EFI System Table.
78ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang
79ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang  @return     EFI_STATUS    always returns EFI_SUCCESS except EFI_ALREADY_STARTED if already started.
80ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang**/
81ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshangEFI_STATUS
82ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshangEFIAPI
83ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshangRuntimeDriverLibConstruct (
84ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang  IN EFI_HANDLE           ImageHandle,
85ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang  IN EFI_SYSTEM_TABLE     *SystemTable
86ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang  )
87ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang{
88ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang  EFI_STATUS  Status;
89ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang
9060c93673b3189b7a48acdb5c300f4ee3546ffb85lgao  ASSERT (gRT != NULL);
9160c93673b3189b7a48acdb5c300f4ee3546ffb85lgao  ASSERT (gBS != NULL);
9260c93673b3189b7a48acdb5c300f4ee3546ffb85lgao
93ad9e507a58a663b2035232360a7ade02399b80a8mdkinney  mRT = gRT;
94ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang  //
95ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang  // Register SetVirtualAddressMap () notify function
96ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang  //
9757a31578d374da211893541d236890850529e478jji  Status = gBS->CreateEventEx (
9857a31578d374da211893541d236890850529e478jji                  EVT_NOTIFY_SIGNAL,
99ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang                  TPL_NOTIFY,
100ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang                  RuntimeLibVirtualNotifyEvent,
101ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang                  NULL,
10257a31578d374da211893541d236890850529e478jji                  &gEfiEventVirtualAddressChangeGuid,
103ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang                  &mEfiVirtualNotifyEvent
104ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang                  );
105ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang
106ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang  ASSERT_EFI_ERROR (Status);
107ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang
10857a31578d374da211893541d236890850529e478jji  Status = gBS->CreateEventEx (
10957a31578d374da211893541d236890850529e478jji                  EVT_NOTIFY_SIGNAL,
11021998b6778c9b77085330c4711f4cc55aa7c1e41yshang                  TPL_NOTIFY,
11121998b6778c9b77085330c4711f4cc55aa7c1e41yshang                  RuntimeLibExitBootServicesEvent,
11221998b6778c9b77085330c4711f4cc55aa7c1e41yshang                  NULL,
11357a31578d374da211893541d236890850529e478jji                  &gEfiEventExitBootServicesGuid,
11421998b6778c9b77085330c4711f4cc55aa7c1e41yshang                  &mEfiExitBootServicesEvent
11521998b6778c9b77085330c4711f4cc55aa7c1e41yshang                  );
11621998b6778c9b77085330c4711f4cc55aa7c1e41yshang
11721998b6778c9b77085330c4711f4cc55aa7c1e41yshang  ASSERT_EFI_ERROR (Status);
11821998b6778c9b77085330c4711f4cc55aa7c1e41yshang
119ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang  return Status;
120ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang}
121ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang
122ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang/**
12360c93673b3189b7a48acdb5c300f4ee3546ffb85lgao  If a runtime driver exits with an error, it must call this routine
12460c93673b3189b7a48acdb5c300f4ee3546ffb85lgao  to free the allocated resource before the exiting.
12560c93673b3189b7a48acdb5c300f4ee3546ffb85lgao  It will ASSERT() if gBS is NULL.
12660c93673b3189b7a48acdb5c300f4ee3546ffb85lgao  It will ASSERT() if that operation fails.
127ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang
12842eedea958591087603bbacd1c2227d2494026afyshang  @param[in]  ImageHandle   The firmware allocated handle for the EFI image.
12942eedea958591087603bbacd1c2227d2494026afyshang  @param[in]  SystemTable   A pointer to the EFI System Table.
13042eedea958591087603bbacd1c2227d2494026afyshang
131ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang  @retval     EFI_SUCCESS       Shutdown the Runtime Driver Lib successfully
132ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang  @retval     EFI_UNSUPPORTED   Runtime Driver lib was not initialized at all
133ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang**/
134ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshangEFI_STATUS
135ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshangEFIAPI
136ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshangRuntimeDriverLibDeconstruct (
137ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang  IN EFI_HANDLE        ImageHandle,
138ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang  IN EFI_SYSTEM_TABLE  *SystemTable
139ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang  )
140ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang{
141ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang  EFI_STATUS  Status;
142ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang
143ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang  //
144ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang  // Close SetVirtualAddressMap () notify function
145ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang  //
146ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang  ASSERT (gBS != NULL);
147ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang  Status = gBS->CloseEvent (mEfiVirtualNotifyEvent);
148ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang  ASSERT_EFI_ERROR (Status);
149ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang
15021998b6778c9b77085330c4711f4cc55aa7c1e41yshang  Status = gBS->CloseEvent (mEfiExitBootServicesEvent);
15121998b6778c9b77085330c4711f4cc55aa7c1e41yshang  ASSERT_EFI_ERROR (Status);
15221998b6778c9b77085330c4711f4cc55aa7c1e41yshang
153ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang  return Status;
154ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang}
155ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang
156ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang/**
1571d37ab9fb9714451a58023c2515d1c5f9cbf6a31jji  This function allows the caller to determine if UEFI ExitBootServices() has been called.
1581d37ab9fb9714451a58023c2515d1c5f9cbf6a31jji
1591d37ab9fb9714451a58023c2515d1c5f9cbf6a31jji  This function returns TRUE after all the EVT_SIGNAL_EXIT_BOOT_SERVICES functions have
1601d37ab9fb9714451a58023c2515d1c5f9cbf6a31jji  executed as a result of the OS calling ExitBootServices().  Prior to this time FALSE
1611d37ab9fb9714451a58023c2515d1c5f9cbf6a31jji  is returned. This function is used by runtime code to decide it is legal to access
1621d37ab9fb9714451a58023c2515d1c5f9cbf6a31jji  services that go away after ExitBootServices().
1631d37ab9fb9714451a58023c2515d1c5f9cbf6a31jji
1641d37ab9fb9714451a58023c2515d1c5f9cbf6a31jji  @retval  TRUE  The system has finished executing the EVT_SIGNAL_EXIT_BOOT_SERVICES event.
1651d37ab9fb9714451a58023c2515d1c5f9cbf6a31jji  @retval  FALSE The system has not finished executing the EVT_SIGNAL_EXIT_BOOT_SERVICES event.
166ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang
167ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang**/
168ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshangBOOLEAN
169ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshangEFIAPI
170ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshangEfiAtRuntime (
171ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang  VOID
172ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang  )
173ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang{
174ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang  return mEfiAtRuntime;
175ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang}
176ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang
177ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang/**
1781d37ab9fb9714451a58023c2515d1c5f9cbf6a31jji  This function allows the caller to determine if UEFI SetVirtualAddressMap() has been called.
1791d37ab9fb9714451a58023c2515d1c5f9cbf6a31jji
1801d37ab9fb9714451a58023c2515d1c5f9cbf6a31jji  This function returns TRUE after all the EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE functions have
1811d37ab9fb9714451a58023c2515d1c5f9cbf6a31jji  executed as a result of the OS calling SetVirtualAddressMap(). Prior to this time FALSE
1821d37ab9fb9714451a58023c2515d1c5f9cbf6a31jji  is returned. This function is used by runtime code to decide it is legal to access services
1831d37ab9fb9714451a58023c2515d1c5f9cbf6a31jji  that go away after SetVirtualAddressMap().
1841d37ab9fb9714451a58023c2515d1c5f9cbf6a31jji
1851d37ab9fb9714451a58023c2515d1c5f9cbf6a31jji  @retval  TRUE  The system has finished executing the EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
1861d37ab9fb9714451a58023c2515d1c5f9cbf6a31jji  @retval  FALSE The system has not finished executing the EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
187ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang
188ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang**/
189ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshangBOOLEAN
190ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshangEFIAPI
191ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshangEfiGoneVirtual (
192ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang  VOID
193ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang  )
194ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang{
195ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang  return mEfiGoneVirtual;
196ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang}
197ebcc8fb7ba239020d307ce113fb30ae120b6fcafyshang
198