3 * Copyright (c) 2011, ARM Limited. All rights reserved.
5 * This program and the accompanying materials
6 * are licensed and made available under the terms and conditions of the BSD License
7 * which accompanies this distribution. The full text of the license may be found at
8 * http://opensource.org/licenses/bsd-license.php
10 * THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "BdsInternal.h"
17 #include <Library/DxeServicesTableLib.h>
18 #include <Library/HobLib.h>
21 ShutdownUefiBootServices( VOID
)
25 EFI_MEMORY_DESCRIPTOR
*MemoryMap
;
28 UINT32 DescriptorVersion
;
34 Status
= gBS
->GetMemoryMap (
41 if (Status
== EFI_BUFFER_TOO_SMALL
) {
43 Pages
= EFI_SIZE_TO_PAGES (MemoryMapSize
) + 1;
44 MemoryMap
= AllocatePages (Pages
);
47 // Get System MemoryMap
49 Status
= gBS
->GetMemoryMap (
56 // Don't do anything between the GetMemoryMap() and ExitBootServices()
57 if (!EFI_ERROR (Status
)) {
58 Status
= gBS
->ExitBootServices (gImageHandle
, MapKey
);
59 if (EFI_ERROR (Status
)) {
60 FreePages (MemoryMap
, Pages
);
66 } while (EFI_ERROR (Status
));
72 BdsConnectAllDrivers( VOID
) {
73 UINTN HandleCount
, Index
;
74 EFI_HANDLE
*HandleBuffer
;
78 // Locate all the driver handles
79 Status
= gBS
->LocateHandleBuffer (
86 if (EFI_ERROR (Status
)) {
90 // Connect every handles
91 for (Index
= 0; Index
< HandleCount
; Index
++) {
92 gBS
->ConnectController(HandleBuffer
[Index
], NULL
, NULL
, TRUE
);
95 if (HandleBuffer
!= NULL
) {
96 FreePool (HandleBuffer
);
99 // Check if new handles have been created after the start of the previous handles
100 Status
= gDS
->Dispatch ();
101 } while (!EFI_ERROR(Status
));
106 STATIC EFI_STATUS
InsertSystemMemoryResources(LIST_ENTRY
*ResourceList
, EFI_HOB_RESOURCE_DESCRIPTOR
*ResHob
) {
107 BDS_SYSTEM_MEMORY_RESOURCE NewResource
;
109 BDS_SYSTEM_MEMORY_RESOURCE
*Resource
;
111 //DEBUG ((EFI_D_ERROR, "** InsertSystemMemoryResources(0x%X,0x%X)\n",(UINT32)ResHob->PhysicalStart,(UINT32)ResHob->ResourceLength));
113 if (IsListEmpty (ResourceList
)) {
114 ZeroMem(&NewResource
,sizeof(BDS_SYSTEM_MEMORY_RESOURCE
));
115 NewResource
.PhysicalStart
= ResHob
->PhysicalStart
;
116 NewResource
.ResourceLength
= ResHob
->ResourceLength
;
117 InsertTailList (ResourceList
, &NewResource
.Link
);
121 //for (Link = GetFirstNode (ResourceList); !IsNull (ResourceList,Link); Link = GetNextNode (ResourceList,Link)) {
122 Link
= ResourceList
->ForwardLink
;
123 while (Link
!= NULL
&& Link
!= ResourceList
) {
124 Resource
= (BDS_SYSTEM_MEMORY_RESOURCE
*)Link
;
125 //DEBUG ((EFI_D_ERROR, " - (0x%X,0x%X)\n",(UINT32)Resource->PhysicalStart,(UINT32)Resource->ResourceLength));
127 // Sanity Check. The resources should not overlapped.
128 ASSERT(!((ResHob
->PhysicalStart
>= Resource
->PhysicalStart
) && (ResHob
->PhysicalStart
< (Resource
->PhysicalStart
+ Resource
->ResourceLength
))));
129 ASSERT(!((ResHob
->PhysicalStart
+ ResHob
->ResourceLength
>= Resource
->PhysicalStart
) &&
130 ((ResHob
->PhysicalStart
+ ResHob
->ResourceLength
) < (Resource
->PhysicalStart
+ Resource
->ResourceLength
))));
132 // The new resource is attached after this resource descriptor
133 if (ResHob
->PhysicalStart
== Resource
->PhysicalStart
+ Resource
->ResourceLength
) {
134 Resource
->ResourceLength
= Resource
->ResourceLength
+ ResHob
->ResourceLength
;
135 //DEBUG ((EFI_D_ERROR, "** Attached new Length:0x%X\n",(UINT32)Resource->ResourceLength));
138 // The new resource is attached before this resource descriptor
139 else if (ResHob
->PhysicalStart
+ ResHob
->ResourceLength
== Resource
->PhysicalStart
) {
140 Resource
->PhysicalStart
= ResHob
->PhysicalStart
;
141 Resource
->ResourceLength
= Resource
->ResourceLength
+ ResHob
->ResourceLength
;
142 //DEBUG ((EFI_D_ERROR, "** Attached2 new Length:0x%X\n",(UINT32)Resource->ResourceLength));
145 Link
= Link
->ForwardLink
;
148 // None of the Resource of the list is attached to this ResHob. Create a new entry for it
149 ZeroMem(&NewResource
,sizeof(BDS_SYSTEM_MEMORY_RESOURCE
));
150 NewResource
.PhysicalStart
= ResHob
->PhysicalStart
;
151 NewResource
.ResourceLength
= ResHob
->ResourceLength
;
152 InsertTailList (ResourceList
, &NewResource
.Link
);
156 EFI_STATUS
GetSystemMemoryResources(LIST_ENTRY
*ResourceList
) {
157 EFI_HOB_RESOURCE_DESCRIPTOR
*ResHob
;
159 InitializeListHead (ResourceList
);
161 // Find the first System Memory Resource Descriptor
162 ResHob
= (EFI_HOB_RESOURCE_DESCRIPTOR
*)GetFirstHob (EFI_HOB_TYPE_RESOURCE_DESCRIPTOR
);
163 while ((ResHob
!= NULL
) && (ResHob
->ResourceType
!= EFI_RESOURCE_SYSTEM_MEMORY
)) {
164 ResHob
= (EFI_HOB_RESOURCE_DESCRIPTOR
*)GetNextHob (EFI_HOB_TYPE_RESOURCE_DESCRIPTOR
,(VOID
*)((UINTN
)ResHob
+ ResHob
->Header
.HobLength
));
168 if (ResHob
== NULL
) {
169 return EFI_NOT_FOUND
;
171 InsertSystemMemoryResources(ResourceList
, ResHob
);
174 ResHob
= (EFI_HOB_RESOURCE_DESCRIPTOR
*)GetNextHob (EFI_HOB_TYPE_RESOURCE_DESCRIPTOR
,(VOID
*)((UINTN
)ResHob
+ ResHob
->Header
.HobLength
));
175 while (ResHob
!= NULL
) {
176 if (ResHob
->ResourceType
== EFI_RESOURCE_SYSTEM_MEMORY
) {
177 InsertSystemMemoryResources(ResourceList
, ResHob
);
179 ResHob
= (EFI_HOB_RESOURCE_DESCRIPTOR
*)GetNextHob (EFI_HOB_TYPE_RESOURCE_DESCRIPTOR
,(VOID
*)((UINTN
)ResHob
+ ResHob
->Header
.HobLength
));