]> git.proxmox.com Git - mirror_edk2.git/blob - CorebootModulePkg/Library/CbParseLib/CbParseLib.c
Pkg-Module: CorebootModulePkg
[mirror_edk2.git] / CorebootModulePkg / Library / CbParseLib / CbParseLib.c
1 /** @file
2 This library will parse the coreboot table in memory and extract those required
3 information.
4
5 Copyright (c) 2014, Intel Corporation. All rights reserved.<BR>
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 <Uefi/UefiBaseType.h>
17 #include <Library/BaseLib.h>
18 #include <Library/BaseMemoryLib.h>
19 #include <Library/DebugLib.h>
20 #include <Library/PcdLib.h>
21 #include <Library/CbParseLib.h>
22
23 #include <IndustryStandard/Acpi.h>
24
25 #include "Coreboot.h"
26
27 /* Helpful inlines */
28
29 static UINT64 cb_unpack64(struct cbuint64 val)
30 {
31 return (((UINT64) val.hi) << 32) | val.lo;
32 }
33
34 static const char *cb_mb_vendor_string(const struct cb_mainboard *cbm)
35 {
36 return (char *)(cbm->strings + cbm->vendor_idx);
37 }
38
39 static const char *cb_mb_part_string(const struct cb_mainboard *cbm)
40 {
41 return (char *)(cbm->strings + cbm->part_number_idx);
42 }
43
44 UINT16
45 CbCheckSum16 (
46 IN UINT16 *Buffer,
47 IN UINTN Length
48 )
49 {
50 UINT32 Sum, TmpValue;
51 UINTN Idx;
52 UINT8 *TmpPtr;
53
54 Sum = 0;
55 TmpPtr = (UINT8 *)Buffer;
56 for(Idx = 0; Idx < Length; Idx++) {
57 TmpValue = TmpPtr[Idx];
58 if (Idx % 2 == 1) {
59 TmpValue <<= 8;
60 }
61
62 Sum += TmpValue;
63
64 // Wrap
65 if (Sum >= 0x10000) {
66 Sum = (Sum + (Sum >> 16)) & 0xFFFF;
67 }
68 }
69
70 return (UINT16)((~Sum) & 0xFFFF);
71 }
72
73 VOID *
74 FindCbTag (
75 IN VOID *Start,
76 IN UINT32 Tag
77 )
78 {
79 struct cb_header *Header;
80 struct cb_record *Record;
81 UINT8 *TmpPtr;
82 UINT8 *TagPtr;
83 UINTN Idx;
84 UINT16 CheckSum;
85
86 Header = NULL;
87 TmpPtr = (UINT8 *)Start;
88 for (Idx = 0; Idx < 4096; Idx += 16, TmpPtr += 16) {
89 Header = (struct cb_header *)TmpPtr;
90 if (Header->signature == CB_HEADER_SIGNATURE) {
91 break;
92 }
93 }
94
95 if (Idx >= 4096)
96 return NULL;
97
98 if (Header == NULL || !Header->table_bytes)
99 return NULL;
100
101 //
102 // Check the checksum of the coreboot table header
103 //
104 CheckSum = CbCheckSum16 ((UINT16 *)Header, sizeof (*Header));
105 if (CheckSum != 0) {
106 DEBUG ((EFI_D_ERROR, "Invalid coreboot table header checksum\n"));
107 return NULL;
108 }
109
110 CheckSum = CbCheckSum16 ((UINT16 *)(TmpPtr + sizeof (*Header)), Header->table_bytes);
111 if (CheckSum != Header->table_checksum) {
112 DEBUG ((EFI_D_ERROR, "Incorrect checksum of all the coreboot table entries\n"));
113 return NULL;
114 }
115
116 TagPtr = NULL;
117 TmpPtr += Header->header_bytes;
118 for (Idx = 0; Idx < Header->table_entries; Idx++) {
119 Record = (struct cb_record *)TmpPtr;
120 if (Record->tag == CB_TAG_FORWARD) {
121 TmpPtr = (VOID *)(UINTN)((struct cb_forward *)(UINTN)Record)->forward;
122 if (Tag == CB_TAG_FORWARD)
123 return TmpPtr;
124 else
125 return FindCbTag (TmpPtr, Tag);
126 }
127 if (Record->tag == Tag) {
128 TagPtr = TmpPtr;
129 break;
130 }
131 TmpPtr += Record->size;
132 }
133
134 return TagPtr;
135 }
136
137 RETURN_STATUS
138 FindCbMemTable (
139 struct cbmem_root *root,
140 IN UINT32 TableId,
141 IN VOID** pMemTable,
142 IN UINT32* pMemTableSize
143 )
144 {
145 UINTN Idx;
146
147 if ((!root) || (!pMemTable))
148 return RETURN_INVALID_PARAMETER;
149
150 for (Idx = 0; Idx < root->num_entries; Idx++) {
151 if (root->entries[Idx].id == TableId) {
152 *pMemTable = (VOID *) (UINTN)root->entries[Idx].start;
153 if (pMemTableSize)
154 *pMemTableSize = root->entries[Idx].size;
155
156 DEBUG ((EFI_D_ERROR, "Find CbMemTable Id 0x%x, base 0x%x, size 0x%x\n", TableId, *pMemTable, *pMemTableSize));
157 return RETURN_SUCCESS;
158 }
159 }
160
161 return RETURN_NOT_FOUND;
162 }
163
164
165 /**
166 Acquire the memory information from the coreboot table in memory.
167
168 @param pLowMemorySize Pointer to the variable of low memory size
169 @param pHighMemorySize Pointer to the variable of high memory size
170
171 @retval RETURN_SUCCESS Successfully find out the memory information.
172 @retval RETURN_INVALID_PARAMETER Invalid input parameters.
173 @retval RETURN_NOT_FOUND Failed to find the memory information.
174
175 **/
176 RETURN_STATUS
177 CbParseMemoryInfo (
178 IN UINT64* pLowMemorySize,
179 IN UINT64* pHighMemorySize
180 )
181 {
182 struct cb_memory* rec;
183 struct cb_memory_range* Range;
184 UINT64 Start;
185 UINT64 Size;
186 UINTN Index;
187
188 if ((!pLowMemorySize) || (!pHighMemorySize))
189 return RETURN_INVALID_PARAMETER;
190
191 //
192 // Get the coreboot memory table
193 //
194 rec = (struct cb_memory *)FindCbTag (0, CB_TAG_MEMORY);
195 if (!rec)
196 rec = (struct cb_memory *)FindCbTag ((VOID *)(UINTN)PcdGet32 (PcdCbHeaderPointer), CB_TAG_MEMORY);
197
198 if (!rec)
199 return RETURN_NOT_FOUND;
200
201 *pLowMemorySize = 0;
202 *pHighMemorySize = 0;
203
204 for (Index = 0; Index < MEM_RANGE_COUNT(rec); Index++) {
205 Range = MEM_RANGE_PTR(rec, Index);
206 Start = cb_unpack64(Range->start);
207 Size = cb_unpack64(Range->size);
208 DEBUG ((EFI_D_ERROR, "%d. %016lx - %016lx [%02x]\n",
209 Index, Start, Start + Size - 1, Range->type));
210
211 if (Range->type != CB_MEM_RAM) {
212 continue;
213 }
214
215 if (Start + Size < 0x100000000ULL) {
216 *pLowMemorySize = Start + Size;
217 } else {
218 *pHighMemorySize = Start + Size - 0x100000000ULL;
219 }
220 }
221
222 DEBUG ((EFI_D_ERROR, "Low memory 0x%x, High Memory 0x%x\n", *pLowMemorySize, *pHighMemorySize));
223
224 return RETURN_SUCCESS;
225 }
226
227
228 /**
229 Acquire the coreboot memory table with the given table id
230
231 @param TableId Table id to be searched
232 @param pMemTable Pointer to the base address of the memory table
233 @param pMemTableSize Pointer to the size of the memory table
234
235 @retval RETURN_SUCCESS Successfully find out the memory table.
236 @retval RETURN_INVALID_PARAMETER Invalid input parameters.
237 @retval RETURN_NOT_FOUND Failed to find the memory table.
238
239 **/
240 RETURN_STATUS
241 CbParseCbMemTable (
242 IN UINT32 TableId,
243 IN VOID** pMemTable,
244 IN UINT32* pMemTableSize
245 )
246 {
247 struct cb_memory* rec;
248 struct cb_memory_range* Range;
249 UINT64 Start;
250 UINT64 Size;
251 UINTN Index;
252
253 if (!pMemTable)
254 return RETURN_INVALID_PARAMETER;
255
256 *pMemTable = NULL;
257
258 //
259 // Get the coreboot memory table
260 //
261 rec = (struct cb_memory *)FindCbTag (0, CB_TAG_MEMORY);
262 if (!rec)
263 rec = (struct cb_memory *)FindCbTag ((VOID *)(UINTN)PcdGet32 (PcdCbHeaderPointer), CB_TAG_MEMORY);
264
265 if (!rec)
266 return RETURN_NOT_FOUND;
267
268 for (Index = 0; Index < MEM_RANGE_COUNT(rec); Index++) {
269 Range = MEM_RANGE_PTR(rec, Index);
270 Start = cb_unpack64(Range->start);
271 Size = cb_unpack64(Range->size);
272
273 if ((Range->type == CB_MEM_TABLE) && (Start > 0x1000)) {
274 if (FindCbMemTable ((struct cbmem_root *)(UINTN)(Start + Size - DYN_CBMEM_ALIGN_SIZE), TableId, pMemTable, pMemTableSize) == RETURN_SUCCESS)
275 return RETURN_SUCCESS;
276 }
277 }
278
279 return RETURN_NOT_FOUND;
280 }
281
282
283 /**
284 Acquire the acpi table from coreboot
285
286 @param pMemTable Pointer to the base address of the memory table
287 @param pMemTableSize Pointer to the size of the memory table
288
289 @retval RETURN_SUCCESS Successfully find out the memory table.
290 @retval RETURN_INVALID_PARAMETER Invalid input parameters.
291 @retval RETURN_NOT_FOUND Failed to find the memory table.
292
293 **/
294 RETURN_STATUS
295 CbParseAcpiTable (
296 IN VOID** pMemTable,
297 IN UINT32* pMemTableSize
298 )
299 {
300 return CbParseCbMemTable (SIGNATURE_32 ('I', 'P', 'C', 'A'), pMemTable, pMemTableSize);
301 }
302
303 /**
304 Acquire the smbios table from coreboot
305
306 @param pMemTable Pointer to the base address of the memory table
307 @param pMemTableSize Pointer to the size of the memory table
308
309 @retval RETURN_SUCCESS Successfully find out the memory table.
310 @retval RETURN_INVALID_PARAMETER Invalid input parameters.
311 @retval RETURN_NOT_FOUND Failed to find the memory table.
312
313 **/
314 RETURN_STATUS
315 CbParseSmbiosTable (
316 IN VOID** pMemTable,
317 IN UINT32* pMemTableSize
318 )
319 {
320 return CbParseCbMemTable (SIGNATURE_32 ('T', 'B', 'M', 'S'), pMemTable, pMemTableSize);
321 }
322
323 /**
324 Find the required fadt information
325
326 @param pPmCtrlReg Pointer to the address of power management control register
327 @param pPmTimerReg Pointer to the address of power management timer register
328 @param pResetReg Pointer to the address of system reset register
329 @param pResetValue Pointer to the value to be writen to the system reset register
330
331 @retval RETURN_SUCCESS Successfully find out all the required fadt information.
332 @retval RETURN_NOT_FOUND Failed to find the fadt table.
333
334 **/
335 RETURN_STATUS
336 CbParseFadtInfo (
337 IN UINTN* pPmCtrlReg,
338 IN UINTN* pPmTimerReg,
339 IN UINTN* pResetReg,
340 IN UINTN* pResetValue
341 )
342 {
343 EFI_ACPI_3_0_ROOT_SYSTEM_DESCRIPTION_POINTER* Rsdp;
344 EFI_ACPI_DESCRIPTION_HEADER* Rsdt;
345 UINT32* Entry32;
346 UINTN Entry32Num;
347 EFI_ACPI_3_0_FIXED_ACPI_DESCRIPTION_TABLE* Fadt;
348 EFI_ACPI_DESCRIPTION_HEADER* Xsdt;
349 UINT64* Entry64;
350 UINTN Entry64Num;
351 UINTN Idx;
352 RETURN_STATUS Status;
353
354 Rsdp = NULL;
355 Status = RETURN_SUCCESS;
356
357 Status = CbParseAcpiTable (&Rsdp, NULL);
358 if (RETURN_ERROR(Status))
359 return Status;
360
361 if (!Rsdp)
362 return RETURN_NOT_FOUND;
363
364 DEBUG ((EFI_D_ERROR, "Find Rsdp at 0x%x\n", Rsdp));
365 DEBUG ((EFI_D_ERROR, "Find Rsdt 0x%x, Xsdt 0x%x\n", Rsdp->RsdtAddress, Rsdp->XsdtAddress));
366
367 //
368 // Search Rsdt First
369 //
370 Rsdt = (EFI_ACPI_DESCRIPTION_HEADER *)(UINTN)(Rsdp->RsdtAddress);
371 if (Rsdt != NULL) {
372 Entry32 = (UINT32 *)(Rsdt + 1);
373 Entry32Num = (Rsdt->Length - sizeof(EFI_ACPI_DESCRIPTION_HEADER)) >> 2;
374 for (Idx = 0; Idx < Entry32Num; Idx++) {
375 if (*(UINT32 *)(UINTN)(Entry32[Idx]) == EFI_ACPI_3_0_FIXED_ACPI_DESCRIPTION_TABLE_SIGNATURE) {
376 Fadt = (EFI_ACPI_3_0_FIXED_ACPI_DESCRIPTION_TABLE *)(UINTN)(Entry32[Idx]);
377 if (pPmCtrlReg)
378 *pPmCtrlReg = Fadt->Pm1aCntBlk;
379 DEBUG ((EFI_D_ERROR, "PmCtrl Reg 0x%x\n", Fadt->Pm1aCntBlk));
380
381 if (pPmTimerReg)
382 *pPmTimerReg = Fadt->PmTmrBlk;
383 DEBUG ((EFI_D_ERROR, "PmTimer Reg 0x%x\n", Fadt->PmTmrBlk));
384
385 if (pResetReg)
386 *pResetReg = (UINTN)Fadt->ResetReg.Address;
387 DEBUG ((EFI_D_ERROR, "Reset Reg 0x%x\n", Fadt->ResetReg.Address));
388
389 if (pResetValue)
390 *pResetValue = Fadt->ResetValue;
391 DEBUG ((EFI_D_ERROR, "Reset Value 0x%x\n", Fadt->ResetValue));
392
393 return RETURN_SUCCESS;
394 }
395 }
396 }
397
398 //
399 // Search Xsdt Second
400 //
401 Xsdt = (EFI_ACPI_DESCRIPTION_HEADER *)(UINTN)(Rsdp->XsdtAddress);
402 if (Xsdt != NULL) {
403 Entry64 = (UINT64 *)(Xsdt + 1);
404 Entry64Num = (Xsdt->Length - sizeof(EFI_ACPI_DESCRIPTION_HEADER)) >> 3;
405 for (Idx = 0; Idx < Entry64Num; Idx++) {
406 if (*(UINT32 *)(UINTN)(Entry64[Idx]) == EFI_ACPI_3_0_FIXED_ACPI_DESCRIPTION_TABLE_SIGNATURE) {
407 Fadt = (EFI_ACPI_3_0_FIXED_ACPI_DESCRIPTION_TABLE *)(UINTN)(Entry64[Idx]);
408 if (pPmCtrlReg)
409 *pPmCtrlReg = Fadt->Pm1aCntBlk;
410 DEBUG ((EFI_D_ERROR, "PmCtrl Reg 0x%x\n", Fadt->Pm1aCntBlk));
411
412 if (pPmTimerReg)
413 *pPmTimerReg = Fadt->PmTmrBlk;
414 DEBUG ((EFI_D_ERROR, "PmTimer Reg 0x%x\n", Fadt->PmTmrBlk));
415
416 if (pResetReg)
417 *pResetReg = (UINTN)Fadt->ResetReg.Address;
418 DEBUG ((EFI_D_ERROR, "Reset Reg 0x%x\n", Fadt->ResetReg.Address));
419
420 if (pResetValue)
421 *pResetValue = Fadt->ResetValue;
422 DEBUG ((EFI_D_ERROR, "Reset Value 0x%x\n", Fadt->ResetValue));
423
424 return RETURN_SUCCESS;
425 }
426 }
427 }
428
429 return RETURN_NOT_FOUND;
430 }
431
432 /**
433 Find the serial port information
434
435 @param pRegBase Pointer to the base address of serial port registers
436 @param pRegAccessType Pointer to the access type of serial port registers
437 @param pBaudrate Pointer to the serial port baudrate
438
439 @retval RETURN_SUCCESS Successfully find the serial port information.
440 @retval RETURN_NOT_FOUND Failed to find the serial port information .
441
442 **/
443 RETURN_STATUS
444 CbParseSerialInfo (
445 IN UINT32* pRegBase,
446 IN UINT32* pRegAccessType,
447 IN UINT32* pBaudrate
448 )
449 {
450 struct cb_serial* CbSerial;
451
452 CbSerial = FindCbTag (0, CB_TAG_SERIAL);
453 if (!CbSerial)
454 CbSerial = FindCbTag ((VOID *)(UINTN)PcdGet32 (PcdCbHeaderPointer), CB_TAG_SERIAL);
455
456 if (!CbSerial)
457 return RETURN_NOT_FOUND;
458
459 if (pRegBase)
460 *pRegBase = CbSerial->baseaddr;
461
462 if (pRegAccessType)
463 *pRegAccessType = CbSerial->type;
464
465 if (pBaudrate)
466 *pBaudrate = CbSerial->baud;
467
468 return RETURN_SUCCESS;
469 }
470
471 /**
472 Search for the coreboot table header
473
474 @param Level Level of the search depth
475 @param HeaderPtr Pointer to the pointer of coreboot table header
476
477 @retval RETURN_SUCCESS Successfully find the coreboot table header .
478 @retval RETURN_NOT_FOUND Failed to find the coreboot table header .
479
480 **/
481 RETURN_STATUS
482 CbParseGetCbHeader (
483 IN UINTN Level,
484 IN VOID** HeaderPtr
485 )
486 {
487 UINTN Index;
488 VOID* TempPtr;
489
490 if (!HeaderPtr)
491 return RETURN_NOT_FOUND;
492
493 TempPtr = NULL;
494 for (Index = 0; Index < Level; Index++) {
495 TempPtr = FindCbTag (TempPtr, CB_TAG_FORWARD);
496 if (!TempPtr)
497 break;
498 }
499
500 if ((Index >= Level) && (TempPtr != NULL)) {
501 *HeaderPtr = TempPtr;
502 return RETURN_SUCCESS;
503 }
504
505 return RETURN_NOT_FOUND;
506 }
507
508 /**
509 Find the video frame buffer information
510
511 @param pFbInfo Pointer to the FRAME_BUFFER_INFO structure
512
513 @retval RETURN_SUCCESS Successfully find the video frame buffer information.
514 @retval RETURN_NOT_FOUND Failed to find the video frame buffer information .
515
516 **/
517 RETURN_STATUS
518 CbParseFbInfo (
519 IN FRAME_BUFFER_INFO* pFbInfo
520 )
521 {
522 struct cb_framebuffer* CbFbRec;
523
524 if (!pFbInfo)
525 return RETURN_INVALID_PARAMETER;
526
527 CbFbRec = FindCbTag (0, CB_TAG_FRAMEBUFFER);
528 if (!CbFbRec)
529 CbFbRec = FindCbTag ((VOID *)(UINTN)PcdGet32 (PcdCbHeaderPointer), CB_TAG_FRAMEBUFFER);
530
531 if (!CbFbRec)
532 return RETURN_NOT_FOUND;
533
534 DEBUG ((EFI_D_ERROR, "Found coreboot video frame buffer information\n"));
535 DEBUG ((EFI_D_ERROR, "physical_address: 0x%x\n", CbFbRec->physical_address));
536 DEBUG ((EFI_D_ERROR, "x_resolution: 0x%x\n", CbFbRec->x_resolution));
537 DEBUG ((EFI_D_ERROR, "y_resolution: 0x%x\n", CbFbRec->y_resolution));
538 DEBUG ((EFI_D_ERROR, "bits_per_pixel: 0x%x\n", CbFbRec->bits_per_pixel));
539 DEBUG ((EFI_D_ERROR, "bytes_per_line: 0x%x\n", CbFbRec->bytes_per_line));
540
541 DEBUG ((EFI_D_ERROR, "red_mask_size: 0x%x\n", CbFbRec->red_mask_size));
542 DEBUG ((EFI_D_ERROR, "red_mask_pos: 0x%x\n", CbFbRec->red_mask_pos));
543 DEBUG ((EFI_D_ERROR, "green_mask_size: 0x%x\n", CbFbRec->green_mask_size));
544 DEBUG ((EFI_D_ERROR, "green_mask_pos: 0x%x\n", CbFbRec->green_mask_pos));
545 DEBUG ((EFI_D_ERROR, "blue_mask_size: 0x%x\n", CbFbRec->blue_mask_size));
546 DEBUG ((EFI_D_ERROR, "blue_mask_pos: 0x%x\n", CbFbRec->blue_mask_pos));
547 DEBUG ((EFI_D_ERROR, "reserved_mask_size: 0x%x\n", CbFbRec->reserved_mask_size));
548 DEBUG ((EFI_D_ERROR, "reserved_mask_pos: 0x%x\n", CbFbRec->reserved_mask_pos));
549
550 pFbInfo->LinearFrameBuffer = CbFbRec->physical_address;
551 pFbInfo->HorizontalResolution = CbFbRec->x_resolution;
552 pFbInfo->VerticalResolution = CbFbRec->y_resolution;
553 pFbInfo->BitsPerPixel = CbFbRec->bits_per_pixel;
554 pFbInfo->BytesPerScanLine = (UINT16)CbFbRec->bytes_per_line;
555 pFbInfo->Red.Mask = (1 << CbFbRec->red_mask_size) - 1;
556 pFbInfo->Red.Position = CbFbRec->red_mask_pos;
557 pFbInfo->Green.Mask = (1 << CbFbRec->green_mask_size) - 1;
558 pFbInfo->Green.Position = CbFbRec->green_mask_pos;
559 pFbInfo->Blue.Mask = (1 << CbFbRec->blue_mask_size) - 1;
560 pFbInfo->Blue.Position = CbFbRec->blue_mask_pos;
561 pFbInfo->Reserved.Mask = (1 << CbFbRec->reserved_mask_size) - 1;
562 pFbInfo->Reserved.Position = CbFbRec->reserved_mask_pos;
563
564 return RETURN_SUCCESS;
565 }
566
567