2 This library will parse the coreboot table in memory and extract those required
5 Copyright (c) 2014 - 2015, 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
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.
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>
23 #include <IndustryStandard/Acpi.h>
29 static UINT64
cb_unpack64(struct cbuint64 val
)
31 return LShiftU64 (val
.hi
, 32) | val
.lo
;
45 TmpPtr
= (UINT8
*)Buffer
;
46 for(Idx
= 0; Idx
< Length
; Idx
++) {
47 TmpValue
= TmpPtr
[Idx
];
56 Sum
= (Sum
+ (Sum
>> 16)) & 0xFFFF;
60 return (UINT16
)((~Sum
) & 0xFFFF);
69 struct cb_header
*Header
;
70 struct cb_record
*Record
;
77 TmpPtr
= (UINT8
*)Start
;
78 for (Idx
= 0; Idx
< 4096; Idx
+= 16, TmpPtr
+= 16) {
79 Header
= (struct cb_header
*)TmpPtr
;
80 if (Header
->signature
== CB_HEADER_SIGNATURE
) {
88 if (Header
== NULL
|| !Header
->table_bytes
)
92 // Check the checksum of the coreboot table header
94 CheckSum
= CbCheckSum16 ((UINT16
*)Header
, sizeof (*Header
));
96 DEBUG ((EFI_D_ERROR
, "Invalid coreboot table header checksum\n"));
100 CheckSum
= CbCheckSum16 ((UINT16
*)(TmpPtr
+ sizeof (*Header
)), Header
->table_bytes
);
101 if (CheckSum
!= Header
->table_checksum
) {
102 DEBUG ((EFI_D_ERROR
, "Incorrect checksum of all the coreboot table entries\n"));
107 TmpPtr
+= Header
->header_bytes
;
108 for (Idx
= 0; Idx
< Header
->table_entries
; Idx
++) {
109 Record
= (struct cb_record
*)TmpPtr
;
110 if (Record
->tag
== CB_TAG_FORWARD
) {
111 TmpPtr
= (VOID
*)(UINTN
)((struct cb_forward
*)(UINTN
)Record
)->forward
;
112 if (Tag
== CB_TAG_FORWARD
)
115 return FindCbTag (TmpPtr
, Tag
);
117 if (Record
->tag
== Tag
) {
121 TmpPtr
+= Record
->size
;
129 IN
struct cbmem_root
*Root
,
131 OUT VOID
**pMemTable
,
132 OUT UINT32
*pMemTableSize
137 struct cbmem_entry
*Entries
;
139 if ((Root
== NULL
) || (pMemTable
== NULL
)) {
140 return RETURN_INVALID_PARAMETER
;
143 // Check if the entry is CBMEM or IMD
144 // and handle them separately
146 Entries
= Root
->entries
;
147 if (Entries
[0].magic
== CBMEM_ENTRY_MAGIC
) {
150 Entries
= (struct cbmem_entry
*)((struct imd_root
*)Root
)->entries
;
151 if (Entries
[0].magic
== IMD_ENTRY_MAGIC
) {
154 return RETURN_NOT_FOUND
;
158 for (Idx
= 0; Idx
< Root
->num_entries
; Idx
++) {
159 if (Entries
[Idx
].id
== TableId
) {
161 *pMemTable
= (VOID
*) ((UINTN
)Entries
[Idx
].start
+ (UINTN
)Root
);
163 *pMemTable
= (VOID
*) (UINTN
)Entries
[Idx
].start
;
165 if (pMemTableSize
!= NULL
) {
166 *pMemTableSize
= Entries
[Idx
].size
;
169 DEBUG ((EFI_D_INFO
, "Find CbMemTable Id 0x%x, base %p, size 0x%x\n", TableId
, *pMemTable
, *pMemTableSize
));
170 return RETURN_SUCCESS
;
174 return RETURN_NOT_FOUND
;
179 Acquire the memory information from the coreboot table in memory.
181 @param pLowMemorySize Pointer to the variable of low memory size
182 @param pHighMemorySize Pointer to the variable of high memory size
184 @retval RETURN_SUCCESS Successfully find out the memory information.
185 @retval RETURN_INVALID_PARAMETER Invalid input parameters.
186 @retval RETURN_NOT_FOUND Failed to find the memory information.
191 IN UINT64
* pLowMemorySize
,
192 IN UINT64
* pHighMemorySize
195 struct cb_memory
* rec
;
196 struct cb_memory_range
* Range
;
201 if ((!pLowMemorySize
) || (!pHighMemorySize
))
202 return RETURN_INVALID_PARAMETER
;
205 // Get the coreboot memory table
207 rec
= (struct cb_memory
*)FindCbTag (0, CB_TAG_MEMORY
);
209 rec
= (struct cb_memory
*)FindCbTag ((VOID
*)(UINTN
)PcdGet32 (PcdCbHeaderPointer
), CB_TAG_MEMORY
);
212 return RETURN_NOT_FOUND
;
215 *pHighMemorySize
= 0;
217 for (Index
= 0; Index
< MEM_RANGE_COUNT(rec
); Index
++) {
218 Range
= MEM_RANGE_PTR(rec
, Index
);
219 Start
= cb_unpack64(Range
->start
);
220 Size
= cb_unpack64(Range
->size
);
221 DEBUG ((EFI_D_ERROR
, "%d. %016lx - %016lx [%02x]\n",
222 Index
, Start
, Start
+ Size
- 1, Range
->type
));
224 if (Range
->type
!= CB_MEM_RAM
) {
228 if (Start
+ Size
< 0x100000000ULL
) {
229 *pLowMemorySize
= Start
+ Size
;
231 *pHighMemorySize
= Start
+ Size
- 0x100000000ULL
;
235 DEBUG ((EFI_D_ERROR
, "Low memory 0x%lx, High Memory 0x%lx\n", *pLowMemorySize
, *pHighMemorySize
));
237 return RETURN_SUCCESS
;
242 Acquire the coreboot memory table with the given table id
244 @param TableId Table id to be searched
245 @param pMemTable Pointer to the base address of the memory table
246 @param pMemTableSize Pointer to the size of the memory table
248 @retval RETURN_SUCCESS Successfully find out the memory table.
249 @retval RETURN_INVALID_PARAMETER Invalid input parameters.
250 @retval RETURN_NOT_FOUND Failed to find the memory table.
257 IN UINT32
* pMemTableSize
260 struct cb_memory
* rec
;
261 struct cb_memory_range
* Range
;
267 return RETURN_INVALID_PARAMETER
;
272 // Get the coreboot memory table
274 rec
= (struct cb_memory
*)FindCbTag (0, CB_TAG_MEMORY
);
276 rec
= (struct cb_memory
*)FindCbTag ((VOID
*)(UINTN
)PcdGet32 (PcdCbHeaderPointer
), CB_TAG_MEMORY
);
279 return RETURN_NOT_FOUND
;
281 for (Index
= 0; Index
< MEM_RANGE_COUNT(rec
); Index
++) {
282 Range
= MEM_RANGE_PTR(rec
, Index
);
283 Start
= cb_unpack64(Range
->start
);
284 Size
= cb_unpack64(Range
->size
);
286 if ((Range
->type
== CB_MEM_TABLE
) && (Start
> 0x1000)) {
287 if (FindCbMemTable ((struct cbmem_root
*)(UINTN
)(Start
+ Size
- DYN_CBMEM_ALIGN_SIZE
), TableId
, pMemTable
, pMemTableSize
) == RETURN_SUCCESS
)
288 return RETURN_SUCCESS
;
292 return RETURN_NOT_FOUND
;
297 Acquire the acpi table from coreboot
299 @param pMemTable Pointer to the base address of the memory table
300 @param pMemTableSize Pointer to the size of the memory table
302 @retval RETURN_SUCCESS Successfully find out the memory table.
303 @retval RETURN_INVALID_PARAMETER Invalid input parameters.
304 @retval RETURN_NOT_FOUND Failed to find the memory table.
310 IN UINT32
* pMemTableSize
313 return CbParseCbMemTable (SIGNATURE_32 ('I', 'P', 'C', 'A'), (VOID
**)pMemTable
, pMemTableSize
);
317 Acquire the smbios table from coreboot
319 @param pMemTable Pointer to the base address of the memory table
320 @param pMemTableSize Pointer to the size of the memory table
322 @retval RETURN_SUCCESS Successfully find out the memory table.
323 @retval RETURN_INVALID_PARAMETER Invalid input parameters.
324 @retval RETURN_NOT_FOUND Failed to find the memory table.
330 IN UINT32
* pMemTableSize
333 return CbParseCbMemTable (SIGNATURE_32 ('T', 'B', 'M', 'S'), pMemTable
, pMemTableSize
);
337 Find the required fadt information
339 @param pPmCtrlReg Pointer to the address of power management control register
340 @param pPmTimerReg Pointer to the address of power management timer register
341 @param pResetReg Pointer to the address of system reset register
342 @param pResetValue Pointer to the value to be writen to the system reset register
344 @retval RETURN_SUCCESS Successfully find out all the required fadt information.
345 @retval RETURN_NOT_FOUND Failed to find the fadt table.
350 IN UINTN
* pPmCtrlReg
,
351 IN UINTN
* pPmTimerReg
,
353 IN UINTN
* pResetValue
356 EFI_ACPI_3_0_ROOT_SYSTEM_DESCRIPTION_POINTER
* Rsdp
;
357 EFI_ACPI_DESCRIPTION_HEADER
* Rsdt
;
360 EFI_ACPI_3_0_FIXED_ACPI_DESCRIPTION_TABLE
* Fadt
;
361 EFI_ACPI_DESCRIPTION_HEADER
* Xsdt
;
365 RETURN_STATUS Status
;
368 Status
= RETURN_SUCCESS
;
370 Status
= CbParseAcpiTable (&Rsdp
, NULL
);
371 if (RETURN_ERROR(Status
))
375 return RETURN_NOT_FOUND
;
377 DEBUG ((EFI_D_ERROR
, "Find Rsdp at %p\n", Rsdp
));
378 DEBUG ((EFI_D_ERROR
, "Find Rsdt 0x%x, Xsdt 0x%lx\n", Rsdp
->RsdtAddress
, Rsdp
->XsdtAddress
));
383 Rsdt
= (EFI_ACPI_DESCRIPTION_HEADER
*)(UINTN
)(Rsdp
->RsdtAddress
);
385 Entry32
= (UINT32
*)(Rsdt
+ 1);
386 Entry32Num
= (Rsdt
->Length
- sizeof(EFI_ACPI_DESCRIPTION_HEADER
)) >> 2;
387 for (Idx
= 0; Idx
< Entry32Num
; Idx
++) {
388 if (*(UINT32
*)(UINTN
)(Entry32
[Idx
]) == EFI_ACPI_3_0_FIXED_ACPI_DESCRIPTION_TABLE_SIGNATURE
) {
389 Fadt
= (EFI_ACPI_3_0_FIXED_ACPI_DESCRIPTION_TABLE
*)(UINTN
)(Entry32
[Idx
]);
391 *pPmCtrlReg
= Fadt
->Pm1aCntBlk
;
392 DEBUG ((EFI_D_ERROR
, "PmCtrl Reg 0x%x\n", Fadt
->Pm1aCntBlk
));
395 *pPmTimerReg
= Fadt
->PmTmrBlk
;
396 DEBUG ((EFI_D_ERROR
, "PmTimer Reg 0x%x\n", Fadt
->PmTmrBlk
));
399 *pResetReg
= (UINTN
)Fadt
->ResetReg
.Address
;
400 DEBUG ((EFI_D_ERROR
, "Reset Reg 0x%lx\n", Fadt
->ResetReg
.Address
));
403 *pResetValue
= Fadt
->ResetValue
;
404 DEBUG ((EFI_D_ERROR
, "Reset Value 0x%x\n", Fadt
->ResetValue
));
406 return RETURN_SUCCESS
;
412 // Search Xsdt Second
414 Xsdt
= (EFI_ACPI_DESCRIPTION_HEADER
*)(UINTN
)(Rsdp
->XsdtAddress
);
416 Entry64
= (UINT64
*)(Xsdt
+ 1);
417 Entry64Num
= (Xsdt
->Length
- sizeof(EFI_ACPI_DESCRIPTION_HEADER
)) >> 3;
418 for (Idx
= 0; Idx
< Entry64Num
; Idx
++) {
419 if (*(UINT32
*)(UINTN
)(Entry64
[Idx
]) == EFI_ACPI_3_0_FIXED_ACPI_DESCRIPTION_TABLE_SIGNATURE
) {
420 Fadt
= (EFI_ACPI_3_0_FIXED_ACPI_DESCRIPTION_TABLE
*)(UINTN
)(Entry64
[Idx
]);
422 *pPmCtrlReg
= Fadt
->Pm1aCntBlk
;
423 DEBUG ((EFI_D_ERROR
, "PmCtrl Reg 0x%x\n", Fadt
->Pm1aCntBlk
));
426 *pPmTimerReg
= Fadt
->PmTmrBlk
;
427 DEBUG ((EFI_D_ERROR
, "PmTimer Reg 0x%x\n", Fadt
->PmTmrBlk
));
430 *pResetReg
= (UINTN
)Fadt
->ResetReg
.Address
;
431 DEBUG ((EFI_D_ERROR
, "Reset Reg 0x%lx\n", Fadt
->ResetReg
.Address
));
434 *pResetValue
= Fadt
->ResetValue
;
435 DEBUG ((EFI_D_ERROR
, "Reset Value 0x%x\n", Fadt
->ResetValue
));
437 return RETURN_SUCCESS
;
442 return RETURN_NOT_FOUND
;
446 Find the serial port information
448 @param pRegBase Pointer to the base address of serial port registers
449 @param pRegAccessType Pointer to the access type of serial port registers
450 @param pBaudrate Pointer to the serial port baudrate
452 @retval RETURN_SUCCESS Successfully find the serial port information.
453 @retval RETURN_NOT_FOUND Failed to find the serial port information .
459 IN UINT32
* pRegAccessType
,
463 struct cb_serial
* CbSerial
;
465 CbSerial
= FindCbTag (0, CB_TAG_SERIAL
);
467 CbSerial
= FindCbTag ((VOID
*)(UINTN
)PcdGet32 (PcdCbHeaderPointer
), CB_TAG_SERIAL
);
470 return RETURN_NOT_FOUND
;
473 *pRegBase
= CbSerial
->baseaddr
;
476 *pRegAccessType
= CbSerial
->type
;
479 *pBaudrate
= CbSerial
->baud
;
481 return RETURN_SUCCESS
;
485 Search for the coreboot table header
487 @param Level Level of the search depth
488 @param HeaderPtr Pointer to the pointer of coreboot table header
490 @retval RETURN_SUCCESS Successfully find the coreboot table header .
491 @retval RETURN_NOT_FOUND Failed to find the coreboot table header .
504 return RETURN_NOT_FOUND
;
507 for (Index
= 0; Index
< Level
; Index
++) {
508 TempPtr
= FindCbTag (TempPtr
, CB_TAG_FORWARD
);
513 if ((Index
>= Level
) && (TempPtr
!= NULL
)) {
514 *HeaderPtr
= TempPtr
;
515 return RETURN_SUCCESS
;
518 return RETURN_NOT_FOUND
;
522 Find the video frame buffer information
524 @param pFbInfo Pointer to the FRAME_BUFFER_INFO structure
526 @retval RETURN_SUCCESS Successfully find the video frame buffer information.
527 @retval RETURN_NOT_FOUND Failed to find the video frame buffer information .
532 IN FRAME_BUFFER_INFO
* pFbInfo
535 struct cb_framebuffer
* CbFbRec
;
538 return RETURN_INVALID_PARAMETER
;
540 CbFbRec
= FindCbTag (0, CB_TAG_FRAMEBUFFER
);
542 CbFbRec
= FindCbTag ((VOID
*)(UINTN
)PcdGet32 (PcdCbHeaderPointer
), CB_TAG_FRAMEBUFFER
);
545 return RETURN_NOT_FOUND
;
547 DEBUG ((EFI_D_ERROR
, "Found coreboot video frame buffer information\n"));
548 DEBUG ((EFI_D_ERROR
, "physical_address: 0x%lx\n", CbFbRec
->physical_address
));
549 DEBUG ((EFI_D_ERROR
, "x_resolution: 0x%x\n", CbFbRec
->x_resolution
));
550 DEBUG ((EFI_D_ERROR
, "y_resolution: 0x%x\n", CbFbRec
->y_resolution
));
551 DEBUG ((EFI_D_ERROR
, "bits_per_pixel: 0x%x\n", CbFbRec
->bits_per_pixel
));
552 DEBUG ((EFI_D_ERROR
, "bytes_per_line: 0x%x\n", CbFbRec
->bytes_per_line
));
554 DEBUG ((EFI_D_ERROR
, "red_mask_size: 0x%x\n", CbFbRec
->red_mask_size
));
555 DEBUG ((EFI_D_ERROR
, "red_mask_pos: 0x%x\n", CbFbRec
->red_mask_pos
));
556 DEBUG ((EFI_D_ERROR
, "green_mask_size: 0x%x\n", CbFbRec
->green_mask_size
));
557 DEBUG ((EFI_D_ERROR
, "green_mask_pos: 0x%x\n", CbFbRec
->green_mask_pos
));
558 DEBUG ((EFI_D_ERROR
, "blue_mask_size: 0x%x\n", CbFbRec
->blue_mask_size
));
559 DEBUG ((EFI_D_ERROR
, "blue_mask_pos: 0x%x\n", CbFbRec
->blue_mask_pos
));
560 DEBUG ((EFI_D_ERROR
, "reserved_mask_size: 0x%x\n", CbFbRec
->reserved_mask_size
));
561 DEBUG ((EFI_D_ERROR
, "reserved_mask_pos: 0x%x\n", CbFbRec
->reserved_mask_pos
));
563 pFbInfo
->LinearFrameBuffer
= CbFbRec
->physical_address
;
564 pFbInfo
->HorizontalResolution
= CbFbRec
->x_resolution
;
565 pFbInfo
->VerticalResolution
= CbFbRec
->y_resolution
;
566 pFbInfo
->BitsPerPixel
= CbFbRec
->bits_per_pixel
;
567 pFbInfo
->BytesPerScanLine
= (UINT16
)CbFbRec
->bytes_per_line
;
568 pFbInfo
->Red
.Mask
= (1 << CbFbRec
->red_mask_size
) - 1;
569 pFbInfo
->Red
.Position
= CbFbRec
->red_mask_pos
;
570 pFbInfo
->Green
.Mask
= (1 << CbFbRec
->green_mask_size
) - 1;
571 pFbInfo
->Green
.Position
= CbFbRec
->green_mask_pos
;
572 pFbInfo
->Blue
.Mask
= (1 << CbFbRec
->blue_mask_size
) - 1;
573 pFbInfo
->Blue
.Position
= CbFbRec
->blue_mask_pos
;
574 pFbInfo
->Reserved
.Mask
= (1 << CbFbRec
->reserved_mask_size
) - 1;
575 pFbInfo
->Reserved
.Position
= CbFbRec
->reserved_mask_pos
;
577 return RETURN_SUCCESS
;