summaryrefslogtreecommitdiff
path: root/Silicon/Hisilicon/Drivers/FlashFvbDxe/FlashFvbDxe.h
blob: 76385b6b6597c9f9b79ffd5ae93dd457a48190ee (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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/** @file
*
*  Copyright (c) 2011-2015, ARM Limited. All rights reserved.
*  Copyright (c) 2015, Hisilicon Limited. All rights reserved.
*  Copyright (c) 2015, Linaro Limited. 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.
*
**/


#ifndef __FLASH_FVB_DXE_H__
#define __FLASH_FVB_DXE_H__


#include <Uefi.h>
#include <PiDxe.h>
#include <Library/BaseLib.h>
#include <Library/HobLib.h>
#include <Protocol/BlockIo.h>
#include <Protocol/FirmwareVolumeBlock.h>
#include <Library/DebugLib.h>

#include <Library/IoLib.h>
#include <Library/UefiLib.h>

#include <Library/PcdLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Protocol/HisiSpiFlashProtocol.h>

#include <Library/UefiRuntimeServicesTableLib.h>
#include <Library/UefiRuntimeLib.h>

#include <Guid/VariableFormat.h>
#include <Guid/SystemNvDataGuid.h>


#define FLASH_ERASE_RETRY                     10
#define FLASH_DEVICE_COUNT                     1

// Device access macros
// These are necessary because we use 2 x 16bit parts to make up 32bit data
typedef struct
{
    UINTN       DeviceBaseAddress;    // Start address of the Device Base Address (DBA)
    UINTN       RegionBaseAddress;    // Start address of one single region
    UINTN       Size;
    UINTN       BlockSize;
    EFI_GUID    Guid;
} FLASH_DESCRIPTION;

#define GET_BLOCK_ADDRESS(BaseAddr,Lba,LbaSize)( BaseAddr + (UINTN)((Lba) * LbaSize) )

#define FLASH_SIGNATURE                       SIGNATURE_32('s', 'p', 'i', '0')
#define INSTANCE_FROM_FVB_THIS(a)             CR(a, FLASH_INSTANCE, FvbProtocol, FLASH_SIGNATURE)
#define INSTANCE_FROM_BLKIO_THIS(a)           CR(a, FLASH_INSTANCE, BlockIoProtocol, FLASH_SIGNATURE)

typedef struct _FLASH_INSTANCE                FLASH_INSTANCE;

typedef EFI_STATUS (*FLASH_INITIALIZE)        (FLASH_INSTANCE* Instance);

typedef struct
{
    VENDOR_DEVICE_PATH                  Vendor;
    EFI_DEVICE_PATH_PROTOCOL            End;
} FLASH_DEVICE_PATH;

struct _FLASH_INSTANCE
{
    UINT32                              Signature;
    EFI_HANDLE                          Handle;

    BOOLEAN                             Initialized;
    FLASH_INITIALIZE                    Initialize;

    UINTN                               DeviceBaseAddress;
    UINTN                               RegionBaseAddress;
    UINTN                               Size;
    EFI_LBA                             StartLba;

    EFI_BLOCK_IO_PROTOCOL               BlockIoProtocol;
    EFI_BLOCK_IO_MEDIA                  Media;

    BOOLEAN                             SupportFvb;
    EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL FvbProtocol;

    FLASH_DEVICE_PATH                   DevicePath;
};


//
// BlockIO Protocol function EFI_BLOCK_IO_PROTOCOL.ReadBlocks
//
EFI_STATUS
EFIAPI
FlashBlockIoReadBlocks (
    IN  EFI_BLOCK_IO_PROTOCOL*   This,
    IN  UINT32                   MediaId,
    IN  EFI_LBA                  Lba,
    IN  UINTN                    BufferSizeInBytes,
    OUT VOID*                    Buffer
);

//
// BlockIO Protocol function EFI_BLOCK_IO_PROTOCOL.WriteBlocks
//
EFI_STATUS
EFIAPI
FlashBlockIoWriteBlocks (
    IN  EFI_BLOCK_IO_PROTOCOL*   This,
    IN  UINT32                   MediaId,
    IN  EFI_LBA                  Lba,
    IN  UINTN                    BufferSizeInBytes,
    IN  VOID*                    Buffer
);

//
// BlockIO Protocol function EFI_BLOCK_IO_PROTOCOL.FlushBlocks
//
EFI_STATUS
EFIAPI
FlashBlockIoFlushBlocks (
    IN EFI_BLOCK_IO_PROTOCOL*    This
);


//
// FvbHw.c
//

EFI_STATUS
EFIAPI
FvbInitialize (
    IN FLASH_INSTANCE*                            Instance
);

EFI_STATUS
EFIAPI
FvbGetAttributes(
    IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL*     This,
    OUT       EFI_FVB_ATTRIBUTES_2*                    Attributes
);

EFI_STATUS
EFIAPI
FvbSetAttributes(
    IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL*     This,
    IN OUT    EFI_FVB_ATTRIBUTES_2*                    Attributes
);

EFI_STATUS
EFIAPI
FvbGetPhysicalAddress(
    IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL*     This,
    OUT       EFI_PHYSICAL_ADDRESS*                    Address
);

EFI_STATUS
EFIAPI
FvbGetBlockSize(
    IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL*     This,
    IN        EFI_LBA                                  Lba,
    OUT       UINTN*                                   BlockSize,
    OUT       UINTN*                                   NumberOfBlocks
);

EFI_STATUS
EFIAPI
FvbRead(
    IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL*     This,
    IN        EFI_LBA                                  Lba,
    IN        UINTN                                   Offset,
    IN OUT    UINTN*                                   NumBytes,
    IN OUT    UINT8*                                   Buffer
);

EFI_STATUS
EFIAPI
FvbWrite(
    IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL*     This,
    IN        EFI_LBA                                  Lba,
    IN        UINTN                                    Offset,
    IN OUT    UINTN*                                   NumBytes,
    IN        UINT8*                                   Buffer
);

EFI_STATUS
EFIAPI
FvbEraseBlocks(
    IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL*     This,
    ...
);

//
// FlashFvbDxe.c
//

EFI_STATUS
FlashUnlockAndEraseSingleBlock (
    IN FLASH_INSTANCE*         Instance,
    IN UINTN                   BlockAddress
);

EFI_STATUS
FlashWriteBlocks (
    IN  FLASH_INSTANCE*    Instance,
    IN  EFI_LBA            Lba,
    IN  UINTN              BufferSizeInBytes,
    IN  VOID*              Buffer
);

EFI_STATUS
FlashReadBlocks (
    IN FLASH_INSTANCE*       Instance,
    IN EFI_LBA               Lba,
    IN UINTN                 BufferSizeInBytes,
    OUT VOID*                Buffer
);

#endif