TianoCore EDK2 master
Loading...
Searching...
No Matches
NorFlashInfoLib.c
Go to the documentation of this file.
1
10#include <PiDxe.h>
11
12#include <Library/BaseLib.h>
14#include <Library/DebugLib.h>
18
19STATIC CONST NOR_FLASH_INFO NorFlashIds[] = {
20 /* ATMEL */
21 { L"at45db011d", { 0x1f, 0x22, 0x00 }, 3, 256, 64 * 1024, 4, NOR_FLASH_ERASE_4K },
22 { L"at45db021d", { 0x1f, 0x23, 0x00 }, 3, 256, 64 * 1024, 8, NOR_FLASH_ERASE_4K },
23 { L"at45db041d", { 0x1f, 0x24, 0x00 }, 3, 256, 64 * 1024, 8, NOR_FLASH_ERASE_4K },
24 { L"at45db081d", { 0x1f, 0x25, 0x00 }, 3, 256, 64 * 1024, 16, NOR_FLASH_ERASE_4K },
25 { L"at45db161d", { 0x1f, 0x26, 0x00 }, 3, 256, 64 * 1024, 32, NOR_FLASH_ERASE_4K },
26 { L"at45db321d", { 0x1f, 0x27, 0x00 }, 3, 256, 64 * 1024, 64, NOR_FLASH_ERASE_4K },
27 { L"at45db641d", { 0x1f, 0x28, 0x00 }, 3, 256, 64 * 1024, 128, NOR_FLASH_ERASE_4K },
28 { L"at25df321a", { 0x1f, 0x47, 0x01 }, 3, 256, 64 * 1024, 64, NOR_FLASH_ERASE_4K },
29 { L"at25df321", { 0x1f, 0x47, 0x00 }, 3, 256, 64 * 1024, 64, NOR_FLASH_ERASE_4K },
30 { L"at26df081a", { 0x1f, 0x45, 0x01 }, 3, 256, 64 * 1024, 16, NOR_FLASH_ERASE_4K },
31 /* EON */
32 { L"en25q32b", { 0x1c, 0x30, 0x16 }, 3, 256, 64 * 1024, 64, 0 },
33 { L"en25q64", { 0x1c, 0x30, 0x17 }, 3, 256, 64 * 1024, 128, NOR_FLASH_ERASE_4K },
34 { L"en25q128b", { 0x1c, 0x30, 0x18 }, 3, 256, 64 * 1024, 256, 0 },
35 { L"en25s64", { 0x1c, 0x38, 0x17 }, 3, 256, 64 * 1024, 128, 0 },
36 /* GIGADEVICE */
37 { L"gd25q16", { 0xc8, 0x40, 0x15 }, 3, 256, 64 * 1024, 32, NOR_FLASH_ERASE_4K },
38 { L"gd25q32", { 0xc8, 0x40, 0x16 }, 3, 256, 64 * 1024, 64, NOR_FLASH_ERASE_4K },
39 { L"gd25lq32", { 0xc8, 0x60, 0x16 }, 3, 256, 64 * 1024, 64, NOR_FLASH_ERASE_4K },
40 { L"gd25q64b", { 0xc8, 0x40, 0x17 }, 3, 256, 64 * 1024, 128, NOR_FLASH_ERASE_4K },
41 { L"gd25lq64c", { 0xc8, 0x60, 0x17 }, 3, 256, 64 * 1024, 128, NOR_FLASH_ERASE_4K },
42 { L"gd25q128", { 0xc8, 0x40, 0x18 }, 3, 256, 64 * 1024, 256, NOR_FLASH_ERASE_4K },
43 { L"gd25lq128", { 0xc8, 0x60, 0x18 }, 3, 256, 64 * 1024, 256, NOR_FLASH_ERASE_4K },
44 { L"gd25q256", { 0xc8, 0x40, 0x19 }, 3, 256, 64 * 1024, 512, NOR_FLASH_ERASE_4K },
45 /* ISSI */
46 { L"is25lp032", { 0x9d, 0x60, 0x16 }, 3, 256, 64 * 1024, 64, 0 },
47 { L"is25lp064", { 0x9d, 0x60, 0x17 }, 3, 256, 64 * 1024, 128, 0 },
48 { L"is25lp128", { 0x9d, 0x60, 0x18 }, 3, 256, 64 * 1024, 256, 0 },
49 /* XINKAI / SILICON KAISER */
50 { L"sk25lp128", { 0x27, 0x70, 0x18 }, 3, 256, 64 * 1024, 256, NOR_FLASH_ERASE_4K },
51 /* MACRONIX */
52 { L"mx25l2006e", { 0xc2, 0x20, 0x12 }, 3, 256, 64 * 1024, 4, 0 },
53 { L"mx25l4005", { 0xc2, 0x20, 0x13 }, 3, 256, 64 * 1024, 8, 0 },
54 { L"mx25l8005", { 0xc2, 0x20, 0x14 }, 3, 256, 64 * 1024, 16, 0 },
55 { L"mx25l1605d", { 0xc2, 0x20, 0x15 }, 3, 256, 64 * 1024, 32, 0 },
56 { L"mx25l3205d", { 0xc2, 0x20, 0x16 }, 3, 256, 64 * 1024, 64, 0 },
57 { L"mx25l6405d", { 0xc2, 0x20, 0x17 }, 3, 256, 64 * 1024, 128, 0 },
58 { L"mx25l12805", { 0xc2, 0x20, 0x18 }, 3, 256, 64 * 1024, 256, 0 },
59 { L"mx25l25635f", { 0xc2, 0x20, 0x19 }, 3, 256, 64 * 1024, 512, 0 },
60 { L"mx25l51235f", { 0xc2, 0x20, 0x1a }, 3, 256, 64 * 1024, 1024, 0 },
61 { L"mx25l12855e", { 0xc2, 0x26, 0x18 }, 3, 256, 64 * 1024, 256, 0 },
62 { L"mx66u51235f", { 0xc2, 0x25, 0x3a }, 3, 256, 64 * 1024, 1024, 0 },
63 { L"mx66u1g45g", { 0xc2, 0x25, 0x3b }, 3, 256, 64 * 1024, 2048, 0 },
64 { L"mx66l1g45g", { 0xc2, 0x20, 0x1b }, 3, 256, 64 * 1024, 2048, 0 },
65 /* SPANSION */
66 { L"s25fl008a", { 0x01, 0x02, 0x13 }, 3, 256, 64 * 1024, 16, 0 },
67 { L"s25fl016a", { 0x01, 0x02, 0x14 }, 3, 256, 64 * 1024, 32, 0 },
68 { L"s25fl032a", { 0x01, 0x02, 0x15 }, 3, 256, 64 * 1024, 64, 0 },
69 { L"s25fl064a", { 0x01, 0x02, 0x16 }, 3, 256, 64 * 1024, 128, 0 },
70 { L"s25fl116k", { 0x01, 0x40, 0x15 }, 3, 256, 64 * 1024, 128, 0 },
71 { L"s25fl164k", { 0x01, 0x40, 0x17, 0x01, 0x40}, 5, 256, 64 * 1024, 128, 0 },
72 { L"s25fl128p_256k", { 0x01, 0x20, 0x18, 0x03, 0x00}, 5, 256, 256 * 1024, 64, 0 },
73 { L"s25fl128p_64k", { 0x01, 0x20, 0x18, 0x03, 0x01}, 5, 256, 64 * 1024, 256, 0 },
74 { L"s25fl032p", { 0x01, 0x02, 0x15, 0x4d, 0x00}, 5, 256, 64 * 1024, 64, 0 },
75 { L"s25fl064p", { 0x01, 0x02, 0x16, 0x4d, 0x00}, 5, 256, 64 * 1024, 128, 0 },
76 { L"s25fl128s_256k", { 0x01, 0x20, 0x18, 0x4d, 0x00}, 5, 256, 256 * 1024, 64, 0 },
77 { L"s25fl128s_64k", { 0x01, 0x20, 0x18, 0x4d, 0x01}, 5, 256, 64 * 1024, 256, 0 },
78 { L"s25fl256s_256k", { 0x01, 0x02, 0x19, 0x4d, 0x00}, 5, 256, 256 * 1024, 128, 0 },
79 { L"s25fl256s_64k", { 0x01, 0x02, 0x19, 0x4d, 0x01}, 5, 256, 64 * 1024, 512, 0 },
80 { L"s25fl512s_256k", { 0x01, 0x02, 0x20, 0x4d, 0x00}, 5, 256, 256 * 1024, 256, 0 },
81 { L"s25fl512s_64k", { 0x01, 0x02, 0x20, 0x4d, 0x01}, 5, 256, 64 * 1024, 1024, 0 },
82 { L"s25fl512s_512k", { 0x01, 0x02, 0x20, 0x4f, 0x00}, 5, 256, 256 * 1024, 256, 0 },
83 /* STMICRO */
84 { L"m25p10", { 0x20, 0x20, 0x11 }, 3, 256, 32 * 1024, 4, 0 },
85 { L"m25p20", { 0x20, 0x20, 0x12 }, 3, 256, 64 * 1024, 4, 0 },
86 { L"m25p40", { 0x20, 0x20, 0x13 }, 3, 256, 64 * 1024, 8, 0 },
87 { L"m25p80", { 0x20, 0x20, 0x14 }, 3, 256, 64 * 1024, 16, 0 },
88 { L"m25p16", { 0x20, 0x20, 0x15 }, 3, 256, 64 * 1024, 32, 0 },
89 { L"m25pE16", { 0x20, 0x80, 0x15, 0x10, 0x00}, 5, 256, 64 * 1024, 32, 0 },
90 { L"m25pX16", { 0x20, 0x71, 0x15, 0x10, 0x00}, 5, 256, 64 * 1024, 32, 0 },
91 { L"m25p32", { 0x20, 0x20, 0x16 }, 3, 256, 64 * 1024, 64, 0 },
92 { L"m25p64", { 0x20, 0x20, 0x17 }, 3, 256, 64 * 1024, 128, 0 },
93 { L"m25p128", { 0x20, 0x20, 0x18 }, 3, 256, 256 * 1024, 64, 0 },
94 { L"m25pX64", { 0x20, 0x71, 0x17 }, 3, 256, 64 * 1024, 128, NOR_FLASH_ERASE_4K },
95 { L"n25q016a", { 0x20, 0xbb, 0x15 }, 3, 256, 64 * 1024, 32, NOR_FLASH_ERASE_4K },
96 { L"n25q32", { 0x20, 0xba, 0x16 }, 3, 256, 64 * 1024, 64, NOR_FLASH_ERASE_4K },
97 { L"n25q32a", { 0x20, 0xbb, 0x16 }, 3, 256, 64 * 1024, 64, NOR_FLASH_ERASE_4K },
98 { L"n25q64", { 0x20, 0xba, 0x17 }, 3, 256, 64 * 1024, 128, NOR_FLASH_ERASE_4K },
99 { L"n25q64a", { 0x20, 0xbb, 0x17 }, 3, 256, 64 * 1024, 128, NOR_FLASH_ERASE_4K },
100 { L"n25q128", { 0x20, 0xba, 0x18 }, 3, 256, 64 * 1024, 256, 0 },
101 { L"n25q128a", { 0x20, 0xbb, 0x18 }, 3, 256, 64 * 1024, 256, 0 },
102 { L"n25q256", { 0x20, 0xba, 0x19 }, 3, 256, 64 * 1024, 512, NOR_FLASH_ERASE_4K },
103 { L"n25q256a", { 0x20, 0xbb, 0x19 }, 3, 256, 64 * 1024, 512, NOR_FLASH_ERASE_4K },
104 { L"n25q512", { 0x20, 0xba, 0x20 }, 3, 256, 64 * 1024, 1024, NOR_FLASH_WRITE_FSR | NOR_FLASH_ERASE_4K },
105 { L"n25q512a", { 0x20, 0xbb, 0x20 }, 3, 256, 64 * 1024, 1024, NOR_FLASH_WRITE_FSR | NOR_FLASH_ERASE_4K },
106 { L"n25q1024", { 0x20, 0xba, 0x21 }, 3, 256, 64 * 1024, 2048, NOR_FLASH_WRITE_FSR | NOR_FLASH_ERASE_4K },
107 { L"n25q1024a", { 0x20, 0xbb, 0x21 }, 3, 256, 64 * 1024, 2048, NOR_FLASH_WRITE_FSR | NOR_FLASH_ERASE_4K },
108 { L"mt25qu02g", { 0x20, 0xbb, 0x22 }, 3, 256, 64 * 1024, 4096, NOR_FLASH_WRITE_FSR | NOR_FLASH_ERASE_4K },
109 { L"mt25ql02g", { 0x20, 0xba, 0x22 }, 3, 256, 64 * 1024, 4096, NOR_FLASH_WRITE_FSR | NOR_FLASH_ERASE_4K },
110 /* SST */
111 { L"sst25vf040b", { 0xbf, 0x25, 0x8d }, 3, 256, 64 * 1024, 8, NOR_FLASH_ERASE_4K },
112 { L"sst25vf080b", { 0xbf, 0x25, 0x8e }, 3, 256, 64 * 1024, 16, NOR_FLASH_ERASE_4K },
113 { L"sst25vf016b", { 0xbf, 0x25, 0x41 }, 3, 256, 64 * 1024, 32, NOR_FLASH_ERASE_4K },
114 { L"sst25vf032b", { 0xbf, 0x25, 0x4a }, 3, 256, 64 * 1024, 64, NOR_FLASH_ERASE_4K },
115 { L"sst25vf064c", { 0xbf, 0x25, 0x4b }, 3, 256, 64 * 1024, 128, NOR_FLASH_ERASE_4K },
116 { L"sst25wf512", { 0xbf, 0x25, 0x01 }, 3, 256, 64 * 1024, 1, NOR_FLASH_ERASE_4K },
117 { L"sst25wf010", { 0xbf, 0x25, 0x02 }, 3, 256, 64 * 1024, 2, NOR_FLASH_ERASE_4K },
118 { L"sst25wf020", { 0xbf, 0x25, 0x03 }, 3, 256, 64 * 1024, 4, NOR_FLASH_ERASE_4K },
119 { L"sst25wf040", { 0xbf, 0x25, 0x04 }, 3, 256, 64 * 1024, 8, NOR_FLASH_ERASE_4K },
120 { L"sst25wf040b", { 0x62, 0x16, 0x13 }, 3, 256, 64 * 1024, 8, NOR_FLASH_ERASE_4K },
121 { L"sst25wf080", { 0xbf, 0x25, 0x05 }, 3, 256, 64 * 1024, 16, NOR_FLASH_ERASE_4K },
122 /* WINBOND */
123 { L"w25p80", { 0xef, 0x20, 0x14 }, 3, 256, 64 * 1024, 16, 0 },
124 { L"w25p16", { 0xef, 0x20, 0x15 }, 3, 256, 64 * 1024, 32, 0 },
125 { L"w25p32", { 0xef, 0x20, 0x16 }, 3, 256, 64 * 1024, 64, 0 },
126 { L"w25x40", { 0xef, 0x30, 0x13 }, 3, 256, 64 * 1024, 8, NOR_FLASH_ERASE_4K },
127 { L"w25x16", { 0xef, 0x30, 0x15 }, 3, 256, 64 * 1024, 32, NOR_FLASH_ERASE_4K },
128 { L"w25x32", { 0xef, 0x30, 0x16 }, 3, 256, 64 * 1024, 64, NOR_FLASH_ERASE_4K },
129 { L"w25x64", { 0xef, 0x30, 0x17 }, 3, 256, 64 * 1024, 128, NOR_FLASH_ERASE_4K },
130 { L"w25q80bl", { 0xef, 0x40, 0x14 }, 3, 256, 64 * 1024, 16, NOR_FLASH_ERASE_4K },
131 { L"w25q16cl", { 0xef, 0x40, 0x15 }, 3, 256, 64 * 1024, 32, NOR_FLASH_ERASE_4K },
132 { L"w25q32bv", { 0xef, 0x40, 0x16 }, 3, 256, 64 * 1024, 64, NOR_FLASH_ERASE_4K },
133 { L"w25q64cv", { 0xef, 0x40, 0x17 }, 3, 256, 64 * 1024, 128, NOR_FLASH_ERASE_4K },
134 { L"w25q128bv", { 0xef, 0x40, 0x18 }, 3, 256, 64 * 1024, 256, NOR_FLASH_ERASE_4K },
135 { L"w25q256", { 0xef, 0x40, 0x19 }, 3, 256, 64 * 1024, 512, NOR_FLASH_ERASE_4K },
136 { L"w25q80bw", { 0xef, 0x50, 0x14 }, 3, 256, 64 * 1024, 16, NOR_FLASH_ERASE_4K },
137 { L"w25q16dw", { 0xef, 0x60, 0x15 }, 3, 256, 64 * 1024, 32, NOR_FLASH_ERASE_4K },
138 { L"w25q32dw", { 0xef, 0x60, 0x16 }, 3, 256, 64 * 1024, 64, NOR_FLASH_ERASE_4K },
139 { L"w25q64dw", { 0xef, 0x60, 0x17 }, 3, 256, 64 * 1024, 128, NOR_FLASH_ERASE_4K },
140 { L"w25q128fw", { 0xef, 0x60, 0x18 }, 3, 256, 64 * 1024, 256, NOR_FLASH_ERASE_4K },
141 { }, /* Empty entry to terminate the list */
142};
143
161EFIAPI
163 IN UINT8 *Id,
164 IN OUT NOR_FLASH_INFO **FlashInfo,
165 IN BOOLEAN AllocateForRuntime
166 )
167{
168 CONST NOR_FLASH_INFO *TmpInfo;
169
170 /*
171 * Iterate over NorFlashIds table, in order to find matching entry.
172 */
173 TmpInfo = NorFlashIds;
174 for ( ; TmpInfo->Name != NULL; TmpInfo++) {
175 if (CompareMem (TmpInfo->Id, Id, TmpInfo->IdLen) == 0) {
176 break;
177 }
178 }
179
180 /*
181 * Matching entry was not found.
182 */
183 if (TmpInfo->Name == NULL) {
184 return EFI_NOT_FOUND;
185 }
186
187 /*
188 * Allocate and copy NOR flash information structure.
189 */
190 if (AllocateForRuntime) {
191 *FlashInfo = AllocateRuntimeCopyPool (sizeof (NOR_FLASH_INFO), TmpInfo);
192 } else {
193 *FlashInfo = AllocateCopyPool (sizeof (NOR_FLASH_INFO), TmpInfo);
194 }
195
196 if (FlashInfo == NULL) {
197 return EFI_OUT_OF_RESOURCES;
198 }
199
200 return EFI_SUCCESS;
201}
202
210VOID
211EFIAPI
213 IN NOR_FLASH_INFO *Info
214 )
215{
216 UINTN EraseSize;
217
218 if (Info->Flags & NOR_FLASH_ERASE_4K) {
219 EraseSize = SIZE_4KB;
220 } else {
221 EraseSize = Info->SectorSize;
222 }
223
224 DEBUG ((
225 DEBUG_ERROR,
226 "Detected %s SPI NOR flash with page size %d B, erase size %d KB, total %d MB\n",
227 Info->Name,
228 Info->PageSize,
229 EraseSize / 1024,
230 (Info->SectorSize * Info->SectorCount) / 1024 / 1024
231 ));
232}
UINT64 UINTN
INTN EFIAPI CompareMem(IN CONST VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
VOID *EFIAPI AllocateRuntimeCopyPool(IN UINTN AllocationSize, IN CONST VOID *Buffer)
VOID *EFIAPI AllocateCopyPool(IN UINTN AllocationSize, IN CONST VOID *Buffer)
#define NULL
Definition: Base.h:319
#define CONST
Definition: Base.h:259
#define STATIC
Definition: Base.h:264
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
#define DEBUG(Expression)
Definition: DebugLib.h:434
EFI_STATUS EFIAPI NorFlashGetInfo(IN UINT8 *Id, IN OUT NOR_FLASH_INFO **FlashInfo, IN BOOLEAN AllocateForRuntime)
VOID EFIAPI NorFlashPrintInfo(IN NOR_FLASH_INFO *Info)
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
#define EFI_SUCCESS
Definition: UefiBaseType.h:112