]> git.proxmox.com Git - mirror_edk2.git/blob - ArmPkg/Library/BdsLib/BdsHelper.c
Sync up ArmPkg with patch from mailing list. Changed name of BdsLib.h to BdsUnixLib...
[mirror_edk2.git] / ArmPkg / Library / BdsLib / BdsHelper.c
1 /** @file
2 *
3 * Copyright (c) 2011, ARM Limited. All rights reserved.
4 *
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
9 *
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.
12 *
13 **/
14
15 #include "BdsInternal.h"
16
17 #include <Library/DxeServicesTableLib.h>
18 #include <Library/HobLib.h>
19
20 EFI_STATUS
21 ShutdownUefiBootServices( VOID )
22 {
23 EFI_STATUS Status;
24 UINTN MemoryMapSize;
25 EFI_MEMORY_DESCRIPTOR *MemoryMap;
26 UINTN MapKey;
27 UINTN DescriptorSize;
28 UINT32 DescriptorVersion;
29 UINTN Pages;
30
31 MemoryMap = NULL;
32 MemoryMapSize = 0;
33 do {
34 Status = gBS->GetMemoryMap (
35 &MemoryMapSize,
36 MemoryMap,
37 &MapKey,
38 &DescriptorSize,
39 &DescriptorVersion
40 );
41 if (Status == EFI_BUFFER_TOO_SMALL) {
42
43 Pages = EFI_SIZE_TO_PAGES (MemoryMapSize) + 1;
44 MemoryMap = AllocatePages (Pages);
45
46 //
47 // Get System MemoryMap
48 //
49 Status = gBS->GetMemoryMap (
50 &MemoryMapSize,
51 MemoryMap,
52 &MapKey,
53 &DescriptorSize,
54 &DescriptorVersion
55 );
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);
61 MemoryMap = NULL;
62 MemoryMapSize = 0;
63 }
64 }
65 }
66 } while (EFI_ERROR (Status));
67
68 return Status;
69 }
70
71 EFI_STATUS
72 BdsConnectAllDrivers( VOID ) {
73 UINTN HandleCount, Index;
74 EFI_HANDLE *HandleBuffer;
75 EFI_STATUS Status;
76
77 do {
78 // Locate all the driver handles
79 Status = gBS->LocateHandleBuffer (
80 AllHandles,
81 NULL,
82 NULL,
83 &HandleCount,
84 &HandleBuffer
85 );
86 if (EFI_ERROR (Status)) {
87 break;
88 }
89
90 // Connect every handles
91 for (Index = 0; Index < HandleCount; Index++) {
92 gBS->ConnectController(HandleBuffer[Index], NULL, NULL, TRUE);
93 }
94
95 if (HandleBuffer != NULL) {
96 FreePool (HandleBuffer);
97 }
98
99 // Check if new handles have been created after the start of the previous handles
100 Status = gDS->Dispatch ();
101 } while (!EFI_ERROR(Status));
102
103 return EFI_SUCCESS;
104 }
105
106 STATIC EFI_STATUS InsertSystemMemoryResources(LIST_ENTRY *ResourceList, EFI_HOB_RESOURCE_DESCRIPTOR *ResHob) {
107 BDS_SYSTEM_MEMORY_RESOURCE NewResource;
108 LIST_ENTRY *Link;
109 BDS_SYSTEM_MEMORY_RESOURCE *Resource;
110
111 //DEBUG ((EFI_D_ERROR, "** InsertSystemMemoryResources(0x%X,0x%X)\n",(UINT32)ResHob->PhysicalStart,(UINT32)ResHob->ResourceLength));
112
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);
118 return EFI_SUCCESS;
119 }
120
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));
126
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))));
131
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));
136 return EFI_SUCCESS;
137 }
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));
143 return EFI_SUCCESS;
144 }
145 Link = Link->ForwardLink;
146 }
147
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);
153 return EFI_SUCCESS;
154 }
155
156 EFI_STATUS GetSystemMemoryResources(LIST_ENTRY *ResourceList) {
157 EFI_HOB_RESOURCE_DESCRIPTOR *ResHob;
158
159 InitializeListHead (ResourceList);
160
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));
165 }
166
167 // Did not find any
168 if (ResHob == NULL) {
169 return EFI_NOT_FOUND;
170 } else {
171 InsertSystemMemoryResources(ResourceList, ResHob);
172 }
173
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);
178 }
179 ResHob = (EFI_HOB_RESOURCE_DESCRIPTOR *)GetNextHob (EFI_HOB_TYPE_RESOURCE_DESCRIPTOR,(VOID *)((UINTN)ResHob + ResHob->Header.HobLength));
180 }
181
182 return EFI_SUCCESS;
183 }