• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /** @file
2 Implementation of Helper routines for PEI enviroment.
3 
4 Copyright (c) 2013-2016 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 
67   if (FileNameGuid == NULL || SectionData == NULL || SectionDataSize == NULL) {
68     return EFI_INVALID_PARAMETER;
69   }
70   *SectionData = NULL;
71   *SectionDataSize = 0;
72 
73   SearchType = EFI_SECTION_RAW;
74   for (Instance = 0; !EFI_ERROR((PeiServicesFfsFindNextVolume (Instance, &VolumeHandle))); Instance++) {
75     if (FvNameGuid != NULL) {
76       Status = PeiServicesFfsGetVolumeInfo (VolumeHandle, &VolumeInfo);
77       if (EFI_ERROR (Status)) {
78         continue;
79       }
80       if (!CompareGuid (FvNameGuid, &VolumeInfo.FvName)) {
81         continue;
82       }
83     }
84     Status = PeiServicesFfsFindFileByName (FileNameGuid, VolumeHandle, &FileHandle);
85     if (!EFI_ERROR (Status)) {
86       Status = PeiServicesFfsGetFileInfo (FileHandle, &FileInfo);
87       if (EFI_ERROR (Status)) {
88         continue;
89       }
90       if (IS_SECTION2(FileInfo.Buffer)) {
91         *SectionDataSize = SECTION2_SIZE(FileInfo.Buffer) - sizeof(EFI_COMMON_SECTION_HEADER2);
92       } else {
93         *SectionDataSize = SECTION_SIZE(FileInfo.Buffer) - sizeof(EFI_COMMON_SECTION_HEADER);
94       }
95       Status = PeiServicesFfsFindSectionData (SearchType, FileHandle, SectionData);
96       if (!EFI_ERROR (Status)) {
97         return Status;
98       }
99     }
100   }
101   return EFI_NOT_FOUND;
102 }
103 
104 /**
105   Find free spi protect register and write to it to protect a flash region.
106 
107   @param   DirectValue      Value to directly write to register.
108                             if DirectValue == 0 the use Base & Length below.
109   @param   BaseAddress      Base address of region in Flash Memory Map.
110   @param   Length           Length of region to protect.
111 
112   @retval  EFI_SUCCESS      Free spi protect register found & written.
113   @retval  EFI_NOT_FOUND    Free Spi protect register not found.
114   @retval  EFI_DEVICE_ERROR Unable to write to spi protect register.
115 **/
116 EFI_STATUS
117 EFIAPI
PlatformWriteFirstFreeSpiProtect(IN CONST UINT32 DirectValue,IN CONST UINT32 BaseAddress,IN CONST UINT32 Length)118 PlatformWriteFirstFreeSpiProtect (
119   IN CONST UINT32                         DirectValue,
120   IN CONST UINT32                         BaseAddress,
121   IN CONST UINT32                         Length
122   )
123 {
124   return WriteFirstFreeSpiProtect (
125            QNC_RCRB_BASE,
126            DirectValue,
127            BaseAddress,
128            Length,
129            NULL
130            );
131 }
132 
133 /** Check if System booted with recovery Boot Stage1 image.
134 
135   @retval  TRUE    If system booted with recovery Boot Stage1 image.
136   @retval  FALSE   If system booted with normal stage1 image.
137 
138 **/
139 BOOLEAN
140 EFIAPI
PlatformIsBootWithRecoveryStage1(VOID)141 PlatformIsBootWithRecoveryStage1 (
142   VOID
143   )
144 {
145   BOOLEAN                           IsRecoveryBoot;
146   QUARK_EDKII_STAGE1_HEADER         *Edk2ImageHeader;
147 
148   Edk2ImageHeader = (QUARK_EDKII_STAGE1_HEADER *) PcdGet32 (PcdEsramStage1Base);
149   switch ((UINT8)Edk2ImageHeader->ImageIndex & QUARK_STAGE1_IMAGE_TYPE_MASK) {
150   case QUARK_STAGE1_RECOVERY_IMAGE_TYPE:
151     //
152     // Recovery Boot
153     //
154     IsRecoveryBoot = TRUE;
155     break;
156   default:
157     //
158     // Normal Boot
159     //
160     IsRecoveryBoot = FALSE;
161     break;
162   }
163 
164   return IsRecoveryBoot;
165 }
166