]> git.proxmox.com Git - mirror_edk2.git/blob - UefiPayloadPkg/Library/FlashDeviceLib/FlashDeviceLib.c
UefiPayloadPkg: Add FlashDeviceLib
[mirror_edk2.git] / UefiPayloadPkg / Library / FlashDeviceLib / FlashDeviceLib.c
1 /** @file
2 Flash Device Library based on SPI Flash library.
3
4 Copyright (c) 2018 - 2021, Intel Corporation. All rights reserved. <BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9 #include <PiDxe.h>
10 #include <Library/BaseMemoryLib.h>
11 #include <Library/DebugLib.h>
12 #include <Library/SpiFlashLib.h>
13
14 /**
15 Initialize spi flash device.
16
17 @retval EFI_SUCCESS The tested spi flash device is supported.
18 @retval EFI_UNSUPPORTED The tested spi flash device is not supported.
19
20 **/
21 EFI_STATUS
22 EFIAPI
23 LibFvbFlashDeviceInit (
24 VOID
25 )
26 {
27 return SpiConstructor ();
28 }
29
30
31 /**
32 Read NumBytes bytes of data from the address specified by
33 PAddress into Buffer.
34
35 @param[in] PAddress The starting physical address of the read.
36 @param[in,out] NumBytes On input, the number of bytes to read. On output, the number
37 of bytes actually read.
38 @param[out] Buffer The destination data buffer for the read.
39
40 @retval EFI_SUCCESS. Opertion is successful.
41 @retval EFI_DEVICE_ERROR If there is any device errors.
42
43 **/
44 EFI_STATUS
45 EFIAPI
46 LibFvbFlashDeviceRead (
47 IN UINTN PAddress,
48 IN OUT UINTN *NumBytes,
49 OUT UINT8 *Buffer
50 )
51 {
52 EFI_STATUS Status;
53 UINT32 ByteCount;
54 UINT32 RgnSize;
55 UINT32 AddrOffset;
56
57 Status = SpiGetRegionAddress (FlashRegionBios, NULL, &RgnSize);
58 if (EFI_ERROR (Status)) {
59 return Status;
60 }
61
62 // BIOS region offset can be calculated by (PAddress - (0x100000000 - RgnSize))
63 // which equal (PAddress + RgnSize) here.
64 AddrOffset = (UINT32)((UINT32)PAddress + RgnSize);
65 ByteCount = (UINT32)*NumBytes;
66 return SpiFlashRead (FlashRegionBios, AddrOffset, ByteCount, Buffer);
67 }
68
69
70 /**
71 Write NumBytes bytes of data from Buffer to the address specified by
72 PAddresss.
73
74 @param[in] PAddress The starting physical address of the write.
75 @param[in,out] NumBytes On input, the number of bytes to write. On output,
76 the actual number of bytes written.
77 @param[in] Buffer The source data buffer for the write.
78
79 @retval EFI_SUCCESS. Opertion is successful.
80 @retval EFI_DEVICE_ERROR If there is any device errors.
81
82 **/
83 EFI_STATUS
84 EFIAPI
85 LibFvbFlashDeviceWrite (
86 IN UINTN PAddress,
87 IN OUT UINTN *NumBytes,
88 IN UINT8 *Buffer
89 )
90 {
91 EFI_STATUS Status;
92 UINT32 ByteCount;
93 UINT32 RgnSize;
94 UINT32 AddrOffset;
95
96 Status = SpiGetRegionAddress (FlashRegionBios, NULL, &RgnSize);
97 if (EFI_ERROR (Status)) {
98 return Status;
99 }
100
101 // BIOS region offset can be calculated by (PAddress - (0x100000000 - RgnSize))
102 // which equal (PAddress + RgnSize) here.
103 AddrOffset = (UINT32)((UINT32)PAddress + RgnSize);
104 ByteCount = (UINT32)*NumBytes;
105 return SpiFlashWrite (FlashRegionBios, AddrOffset, ByteCount, Buffer);
106 }
107
108
109 /**
110 Erase the block starting at PAddress.
111
112 @param[in] PAddress The starting physical address of the block to be erased.
113 This library assume that caller garantee that the PAddress
114 is at the starting address of this block.
115 @param[in] LbaLength The length of the logical block to be erased.
116
117 @retval EFI_SUCCESS. Opertion is successful.
118 @retval EFI_DEVICE_ERROR If there is any device errors.
119
120 **/
121 EFI_STATUS
122 EFIAPI
123 LibFvbFlashDeviceBlockErase (
124 IN UINTN PAddress,
125 IN UINTN LbaLength
126 )
127 {
128 EFI_STATUS Status;
129 UINT32 RgnSize;
130 UINT32 AddrOffset;
131
132 Status = SpiGetRegionAddress (FlashRegionBios, NULL, &RgnSize);
133 if (EFI_ERROR (Status)) {
134 return Status;
135 }
136
137 // BIOS region offset can be calculated by (PAddress - (0x100000000 - RgnSize))
138 // which equal (PAddress + RgnSize) here.
139 AddrOffset = (UINT32)((UINT32)PAddress + RgnSize);
140 return SpiFlashErase (FlashRegionBios, AddrOffset, (UINT32)LbaLength);
141 }
142
143
144 /**
145 Lock or unlock the block starting at PAddress.
146
147 @param[in] PAddress The starting physical address of region to be (un)locked.
148 @param[in] LbaLength The length of the logical block to be erased.
149 @param[in] Lock TRUE to lock. FALSE to unlock.
150
151 @retval EFI_SUCCESS. Opertion is successful.
152 @retval EFI_DEVICE_ERROR If there is any device errors.
153
154 **/
155 EFI_STATUS
156 EFIAPI
157 LibFvbFlashDeviceBlockLock (
158 IN UINTN PAddress,
159 IN UINTN LbaLength,
160 IN BOOLEAN Lock
161 )
162 {
163 return EFI_SUCCESS;
164 }
165