1 /** @file
2 *
3 *  Copyright (c) 2011-2015, ARM Limited. All rights reserved.
4 *  Copyright (c) 2015, Hisilicon Limited. All rights reserved.
5 *  Copyright (c) 2015, Linaro Limited. All rights reserved.
6 *
7 *  This program and the accompanying materials
8 *  are licensed and made available under the terms and conditions of the BSD License
9 *  which accompanies this distribution.  The full text of the license may be found at
10 *  http://opensource.org/licenses/bsd-license.php
11 *
12 *  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14 *
15 **/
16 
17 
18 #ifndef __FLASH_FVB_DXE_H__
19 #define __FLASH_FVB_DXE_H__
20 
21 
22 #include <Uefi.h>
23 #include <PiDxe.h>
24 #include <Library/BaseLib.h>
25 #include <Library/HobLib.h>
26 #include <Protocol/BlockIo.h>
27 #include <Protocol/FirmwareVolumeBlock.h>
28 #include <Library/DebugLib.h>
29 
30 #include <Library/IoLib.h>
31 #include <Library/UefiLib.h>
32 
33 #include <Library/PcdLib.h>
34 #include <Library/BaseMemoryLib.h>
35 #include <Library/MemoryAllocationLib.h>
36 #include <Library/UefiBootServicesTableLib.h>
37 #include <Protocol/HisiSpiFlashProtocol.h>
38 
39 #include <Library/UefiRuntimeServicesTableLib.h>
40 #include <Library/UefiRuntimeLib.h>
41 
42 #include <Guid/VariableFormat.h>
43 #include <Guid/SystemNvDataGuid.h>
44 
45 
46 #define FLASH_ERASE_RETRY                     10
47 #define FLASH_DEVICE_COUNT                     1
48 
49 // Device access macros
50 // These are necessary because we use 2 x 16bit parts to make up 32bit data
51 typedef struct
52 {
53     UINTN       DeviceBaseAddress;    // Start address of the Device Base Address (DBA)
54     UINTN       RegionBaseAddress;    // Start address of one single region
55     UINTN       Size;
56     UINTN       BlockSize;
57     EFI_GUID    Guid;
58 } FLASH_DESCRIPTION;
59 
60 #define GET_BLOCK_ADDRESS(BaseAddr,Lba,LbaSize)( BaseAddr + (UINTN)((Lba) * LbaSize) )
61 
62 #define FLASH_SIGNATURE                       SIGNATURE_32('s', 'p', 'i', '0')
63 #define INSTANCE_FROM_FVB_THIS(a)             CR(a, FLASH_INSTANCE, FvbProtocol, FLASH_SIGNATURE)
64 #define INSTANCE_FROM_BLKIO_THIS(a)           CR(a, FLASH_INSTANCE, BlockIoProtocol, FLASH_SIGNATURE)
65 
66 typedef struct _FLASH_INSTANCE                FLASH_INSTANCE;
67 
68 typedef EFI_STATUS (*FLASH_INITIALIZE)        (FLASH_INSTANCE* Instance);
69 
70 typedef struct
71 {
72     VENDOR_DEVICE_PATH                  Vendor;
73     EFI_DEVICE_PATH_PROTOCOL            End;
74 } FLASH_DEVICE_PATH;
75 
76 struct _FLASH_INSTANCE
77 {
78     UINT32                              Signature;
79     EFI_HANDLE                          Handle;
80 
81     BOOLEAN                             Initialized;
82     FLASH_INITIALIZE                    Initialize;
83 
84     UINTN                               DeviceBaseAddress;
85     UINTN                               RegionBaseAddress;
86     UINTN                               Size;
87     EFI_LBA                             StartLba;
88 
89     EFI_BLOCK_IO_PROTOCOL               BlockIoProtocol;
90     EFI_BLOCK_IO_MEDIA                  Media;
91 
92     BOOLEAN                             SupportFvb;
93     EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL FvbProtocol;
94 
95     FLASH_DEVICE_PATH                   DevicePath;
96 };
97 
98 
99 //
100 // BlockIO Protocol function EFI_BLOCK_IO_PROTOCOL.ReadBlocks
101 //
102 EFI_STATUS
103 EFIAPI
104 FlashBlockIoReadBlocks (
105     IN  EFI_BLOCK_IO_PROTOCOL*   This,
106     IN  UINT32                   MediaId,
107     IN  EFI_LBA                  Lba,
108     IN  UINTN                    BufferSizeInBytes,
109     OUT VOID*                    Buffer
110 );
111 
112 //
113 // BlockIO Protocol function EFI_BLOCK_IO_PROTOCOL.WriteBlocks
114 //
115 EFI_STATUS
116 EFIAPI
117 FlashBlockIoWriteBlocks (
118     IN  EFI_BLOCK_IO_PROTOCOL*   This,
119     IN  UINT32                   MediaId,
120     IN  EFI_LBA                  Lba,
121     IN  UINTN                    BufferSizeInBytes,
122     IN  VOID*                    Buffer
123 );
124 
125 //
126 // BlockIO Protocol function EFI_BLOCK_IO_PROTOCOL.FlushBlocks
127 //
128 EFI_STATUS
129 EFIAPI
130 FlashBlockIoFlushBlocks (
131     IN EFI_BLOCK_IO_PROTOCOL*    This
132 );
133 
134 
135 //
136 // FvbHw.c
137 //
138 
139 EFI_STATUS
140 EFIAPI
141 FvbInitialize (
142     IN FLASH_INSTANCE*                            Instance
143 );
144 
145 EFI_STATUS
146 EFIAPI
147 FvbGetAttributes(
148     IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL*     This,
149     OUT       EFI_FVB_ATTRIBUTES_2*                    Attributes
150 );
151 
152 EFI_STATUS
153 EFIAPI
154 FvbSetAttributes(
155     IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL*     This,
156     IN OUT    EFI_FVB_ATTRIBUTES_2*                    Attributes
157 );
158 
159 EFI_STATUS
160 EFIAPI
161 FvbGetPhysicalAddress(
162     IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL*     This,
163     OUT       EFI_PHYSICAL_ADDRESS*                    Address
164 );
165 
166 EFI_STATUS
167 EFIAPI
168 FvbGetBlockSize(
169     IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL*     This,
170     IN        EFI_LBA                                  Lba,
171     OUT       UINTN*                                   BlockSize,
172     OUT       UINTN*                                   NumberOfBlocks
173 );
174 
175 EFI_STATUS
176 EFIAPI
177 FvbRead(
178     IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL*     This,
179     IN        EFI_LBA                                  Lba,
180     IN        UINTN                                   Offset,
181     IN OUT    UINTN*                                   NumBytes,
182     IN OUT    UINT8*                                   Buffer
183 );
184 
185 EFI_STATUS
186 EFIAPI
187 FvbWrite(
188     IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL*     This,
189     IN        EFI_LBA                                  Lba,
190     IN        UINTN                                    Offset,
191     IN OUT    UINTN*                                   NumBytes,
192     IN        UINT8*                                   Buffer
193 );
194 
195 EFI_STATUS
196 EFIAPI
197 FvbEraseBlocks(
198     IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL*     This,
199     ...
200 );
201 
202 //
203 // FlashFvbDxe.c
204 //
205 
206 EFI_STATUS
207 FlashUnlockAndEraseSingleBlock (
208     IN FLASH_INSTANCE*         Instance,
209     IN UINTN                   BlockAddress
210 );
211 
212 EFI_STATUS
213 FlashWriteBlocks (
214     IN  FLASH_INSTANCE*    Instance,
215     IN  EFI_LBA            Lba,
216     IN  UINTN              BufferSizeInBytes,
217     IN  VOID*              Buffer
218 );
219 
220 EFI_STATUS
221 FlashReadBlocks (
222     IN FLASH_INSTANCE*       Instance,
223     IN EFI_LBA               Lba,
224     IN UINTN                 BufferSizeInBytes,
225     OUT VOID*                Buffer
226 );
227 
228 #endif
229