summaryrefslogtreecommitdiff
path: root/EdkCompatibilityPkg/Foundation/Library/Pei/PeiLib/FindFv.c
blob: 99b4b1cb2186fa04ab234196fcc89c31f919468b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*++

Copyright (c) 2006 - 2008, Intel Corporation                                                         
All rights reserved. This program and the accompanying materials                          
are licensed and made available under the terms and conditions of the BSD License         
which accompanies this distribution.  The full text of the license may be found at        
http://opensource.org/licenses/bsd-license.php                                            
                                                                                          
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             


Module Name:
  
    FindFv.c
   
Abstract:

  Library function to find fv by hob.

--*/

#include "Tiano.h"
#include "Pei.h"
#include "PeiLib.h"
#include "PeiHobLib.h"
#include "EfiCommonLib.h"
#include EFI_GUID_DEFINITION (FirmwareFileSystem)

static
VOID *
FindFvGetHob (
  IN UINT16  Type,
  IN VOID    *HobStart
  )
/*++

Routine Description:

  This function returns the first instance of a HOB type in a HOB list.
  
Arguments:

  Type          The HOB type to return.
  HobStart      The first HOB in the HOB list.
    
Returns:

  HobStart      There were no HOBs found with the requested type.
  else          Returns the first HOB with the matching type.

--*/
{
  EFI_PEI_HOB_POINTERS  Hob;

  Hob.Raw = HobStart;
  //
  // Return input if not found
  //
  if (HobStart == NULL) {
    return HobStart;
  }

  //
  // Parse the HOB list, stop if end of list or matching type found.
  //
  while (!END_OF_HOB_LIST (Hob)) {

    if (Hob.Header->HobType == Type) {
      break;
    }

    Hob.Raw = GET_NEXT_HOB (Hob);
  }
  
  //
  // Return input if not found
  //
  if (END_OF_HOB_LIST (Hob)) {
    return HobStart;
  }

  return (VOID *) (Hob.Raw);
}

EFI_STATUS
EFIAPI
FindFv (
  IN     EFI_FIND_FV_PPI             *This,
  IN     EFI_PEI_SERVICES            **PeiServices,
  IN OUT UINT8                       *FvNumber,
  IN OUT EFI_FIRMWARE_VOLUME_HEADER  **FvAddress
  )
/*++

Routine Description:

  Search Fv which supports FFS.

Arguments:
  
  This        - Interface pointer that implement the Find Fv PPI
  
  PeiServices - Pointer to the PEI Service Table
  
  FvNumber    - On input,  the number of the fireware volume which supports FFS to locate
                On output, the next FV number which supports FFS.
  
  FVAddress   - The address of the volume which supports FFS to discover

Returns:

  EFI_SUCCESS           - An addtional FV which supports FFS found
  EFI_OUT_OF_RESOURCES  - There are no fireware volume which supports FFS for given fvnumber
  EFI_INVALID_PARAMETER - FvAddress is NULL

--*/
{
  EFI_STATUS              Status;
  EFI_PEI_HOB_POINTERS    HobStart;
  EFI_PEI_HOB_POINTERS    Hob;
  EFI_HOB_FIRMWARE_VOLUME *FvHob;
  UINT8                   FvIndex;

  if (FvAddress == NULL){
    return EFI_INVALID_PARAMETER;
  }

  Hob.Raw = NULL;
  FvIndex = 0;

  //
  // Get the Hob table pointer
  //
  Status = (*PeiServices)->GetHobList (
                            PeiServices,
                            (VOID **) &HobStart.Raw
                            );

  if (EFI_ERROR (Status)) {
    return EFI_OUT_OF_RESOURCES;
  }
  
  //
  // Loop to search the wanted FirmwareVolume which supports FFS
  //
  //
  while (FvIndex <= *FvNumber) {
    
    Hob.Raw = FindFvGetHob (EFI_HOB_TYPE_FV, HobStart.Raw); 
    
    //
    //  If the Hob is not EFI_HOB_TYPE_FV, it indicates that
    //  we have finished all FV volumes search, and there is no
    //  the FFS FV specified by FvNumber.
    //
    if (Hob.Header->HobType != EFI_HOB_TYPE_FV) {
      *FvNumber = 0;
      return EFI_OUT_OF_RESOURCES;
    }
  
    HobStart.Raw = Hob.Raw + Hob.Header->HobLength;
    FvHob        = Hob.FirmwareVolume;
    *FvAddress  = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) FvHob->BaseAddress);
    //
    // Check if the FV supports FFS
    //
    if (EfiCompareGuid (&((*FvAddress)->FileSystemGuid), &gEfiFirmwareFileSystemGuid)) {
      FvIndex++;
    }
  }
  
  //
  // Return the next FV number which supports FFS.
  //
  (*FvNumber)++;
  
  return EFI_SUCCESS;

}