1 /** @file
2 Implementation of Helper routines for PEI enviroment.
3 
4 Copyright (c) 2013-2015 Intel Corporation.
5 
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution.  The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10 
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13 
14 **/
15 
16 #include <PiPei.h>
17 
18 #include <Library/PeiServicesTablePointerLib.h>
19 #include <Library/PeiServicesLib.h>
20 #include <Library/I2cLib.h>
21 
22 #include "CommonHeader.h"
23 
24 //
25 // Routines defined in other source modules of this component.
26 //
27 
28 //
29 // Routines local to this source module.
30 //
31 
32 //
33 // Routines exported by this source module.
34 //
35 
36 /**
37   Find pointer to RAW data in Firmware volume file.
38 
39   @param   FvNameGuid       Firmware volume to search. If == NULL search all.
40   @param   FileNameGuid     Firmware volume file to search for.
41   @param   SectionData      Pointer to RAW data section of found file.
42   @param   SectionDataSize  Pointer to UNITN to get size of RAW data.
43 
44   @retval  EFI_SUCCESS            Raw Data found.
45   @retval  EFI_INVALID_PARAMETER  FileNameGuid == NULL.
46   @retval  EFI_NOT_FOUND          Firmware volume file not found.
47   @retval  EFI_UNSUPPORTED        Unsupported in current enviroment (PEI or DXE).
48 
49 **/
50 EFI_STATUS
51 EFIAPI
PlatformFindFvFileRawDataSection(IN CONST EFI_GUID * FvNameGuid OPTIONAL,IN CONST EFI_GUID * FileNameGuid,OUT VOID ** SectionData,OUT UINTN * SectionDataSize)52 PlatformFindFvFileRawDataSection (
53   IN CONST EFI_GUID                 *FvNameGuid OPTIONAL,
54   IN CONST EFI_GUID                 *FileNameGuid,
55   OUT VOID                          **SectionData,
56   OUT UINTN                         *SectionDataSize
57   )
58 {
59   EFI_STATUS                        Status;
60   UINTN                             Instance;
61   EFI_PEI_FV_HANDLE                 VolumeHandle;
62   EFI_PEI_FILE_HANDLE               FileHandle;
63   EFI_SECTION_TYPE                  SearchType;
64   EFI_FV_INFO                       VolumeInfo;
65   EFI_FV_FILE_INFO                  FileInfo;
66   CONST EFI_PEI_SERVICES            **PeiServices;
67 
68   if (FileNameGuid == NULL || SectionData == NULL || SectionDataSize == NULL) {
69     return EFI_INVALID_PARAMETER;
70   }
71   *SectionData = NULL;
72   *SectionDataSize = 0;
73 
74   PeiServices = GetPeiServicesTablePointer ();
75   SearchType = EFI_SECTION_RAW;
76   for (Instance = 0; !EFI_ERROR((PeiServicesFfsFindNextVolume (Instance, &VolumeHandle))); Instance++) {
77     if (FvNameGuid != NULL) {
78       Status = PeiServicesFfsGetVolumeInfo (VolumeHandle, &VolumeInfo);
79       if (EFI_ERROR (Status)) {
80         continue;
81       }
82       if (!CompareGuid (FvNameGuid, &VolumeInfo.FvName)) {
83         continue;
84       }
85     }
86     Status = PeiServicesFfsFindFileByName (FileNameGuid, VolumeHandle, &FileHandle);
87     if (!EFI_ERROR (Status)) {
88       Status = PeiServicesFfsGetFileInfo (FileHandle, &FileInfo);
89       if (EFI_ERROR (Status)) {
90         continue;
91       }
92       if (IS_SECTION2(FileInfo.Buffer)) {
93         *SectionDataSize = SECTION2_SIZE(FileInfo.Buffer) - sizeof(EFI_COMMON_SECTION_HEADER2);
94       } else {
95         *SectionDataSize = SECTION_SIZE(FileInfo.Buffer) - sizeof(EFI_COMMON_SECTION_HEADER);
96       }
97       Status = PeiServicesFfsFindSectionData (SearchType, FileHandle, SectionData);
98       if (!EFI_ERROR (Status)) {
99         return Status;
100       }
101     }
102   }
103   return EFI_NOT_FOUND;
104 }
105 
106 /**
107   Find free spi protect register and write to it to protect a flash region.
108 
109   @param   DirectValue      Value to directly write to register.
110                             if DirectValue == 0 the use Base & Length below.
111   @param   BaseAddress      Base address of region in Flash Memory Map.
112   @param   Length           Length of region to protect.
113 
114   @retval  EFI_SUCCESS      Free spi protect register found & written.
115   @retval  EFI_NOT_FOUND    Free Spi protect register not found.
116   @retval  EFI_DEVICE_ERROR Unable to write to spi protect register.
117 **/
118 EFI_STATUS
119 EFIAPI
PlatformWriteFirstFreeSpiProtect(IN CONST UINT32 DirectValue,IN CONST UINT32 BaseAddress,IN CONST UINT32 Length)120 PlatformWriteFirstFreeSpiProtect (
121   IN CONST UINT32                         DirectValue,
122   IN CONST UINT32                         BaseAddress,
123   IN CONST UINT32                         Length
124   )
125 {
126   return WriteFirstFreeSpiProtect (
127            QNC_RCRB_BASE,
128            DirectValue,
129            BaseAddress,
130            Length,
131            NULL
132            );
133 }
134 
135 /** Check if System booted with recovery Boot Stage1 image.
136 
137   @retval  TRUE    If system booted with recovery Boot Stage1 image.
138   @retval  FALSE   If system booted with normal stage1 image.
139 
140 **/
141 BOOLEAN
142 EFIAPI
PlatformIsBootWithRecoveryStage1(VOID)143 PlatformIsBootWithRecoveryStage1 (
144   VOID
145   )
146 {
147   BOOLEAN                           IsRecoveryBoot;
148   QUARK_EDKII_STAGE1_HEADER         *Edk2ImageHeader;
149 
150   Edk2ImageHeader = (QUARK_EDKII_STAGE1_HEADER *) PcdGet32 (PcdEsramStage1Base);
151   switch ((UINT8)Edk2ImageHeader->ImageIndex & QUARK_STAGE1_IMAGE_TYPE_MASK) {
152   case QUARK_STAGE1_RECOVERY_IMAGE_TYPE:
153     //
154     // Recovery Boot
155     //
156     IsRecoveryBoot = TRUE;
157     break;
158   default:
159     //
160     // Normal Boot
161     //
162     IsRecoveryBoot = FALSE;
163     break;
164   }
165 
166   return IsRecoveryBoot;
167 }
168