]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/Library/HardwareInfoLib/QemuFwCfgHardwareInfoLib.c
48d9dcd8ad0edb129ef2299a9f9b5b7e59addca5
[mirror_edk2.git] / OvmfPkg / Library / HardwareInfoLib / QemuFwCfgHardwareInfoLib.c
1 /*/@file
2 Qemu fw-cfg wrappers for hardware info parsing.
3 Provides an alternative to parse hardware information from a fw-cfg
4 file without relying on dynamic memory allocations.
5
6 Copyright 2021 - 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
7 SPDX-License-Identifier: BSD-2-Clause-Patent
8
9 **/
10
11 #include <Library/DebugLib.h>
12 #include <Library/QemuFwCfgLib.h>
13
14 #include <Library/HardwareInfoLib.h>
15
16 /**
17 Update an optional pointer value if possible
18
19 @param[out] DataSize Pointer to variable to be updated
20 @param[in] Value Value to set the pointed variable to.
21 **/
22 STATIC
23 VOID
24 UpdateDataSize (
25 OUT UINTN *DataSize,
26 IN UINTN Value
27 )
28 {
29 if (DataSize == NULL) {
30 return;
31 }
32
33 *DataSize = Value;
34 }
35
36 EFI_STATUS
37 QemuFwCfgReadNextHardwareInfoByType (
38 IN HARDWARE_INFO_TYPE Type,
39 IN UINTN TypeSize,
40 IN UINTN TotalFileSize,
41 OUT VOID *Data,
42 OUT UINTN *DataSize OPTIONAL,
43 IN OUT UINTN *ReadIndex
44 )
45 {
46 HARDWARE_INFO_HEADER Header;
47
48 if ((Data == NULL) ||
49 (ReadIndex == NULL) ||
50 (TypeSize == 0) ||
51 (Type == HardwareInfoTypeUndefined) ||
52 (TotalFileSize == 0))
53 {
54 return EFI_INVALID_PARAMETER;
55 }
56
57 UpdateDataSize (DataSize, 0);
58
59 while (*ReadIndex < TotalFileSize) {
60 QemuFwCfgReadBytes (sizeof (Header), &Header);
61 *ReadIndex += sizeof (Header);
62
63 if ((Header.Size > MAX_UINTN) || (((UINT64)*ReadIndex + Header.Size) > TotalFileSize)) {
64 *ReadIndex = TotalFileSize;
65 return EFI_ABORTED;
66 }
67
68 if ((Header.Type.Value == Type) && (Header.Size <= TypeSize)) {
69 QemuFwCfgReadBytes ((UINTN)Header.Size, Data);
70
71 *ReadIndex += (UINTN)Header.Size;
72 UpdateDataSize (DataSize, (UINTN)Header.Size);
73
74 return EFI_SUCCESS;
75 }
76
77 //
78 // Skip the bytes corresponding to the next element as it is
79 // not of the expected type and/or size. The TotalFileSize
80 // and individual elements sizes should match so the size
81 // check is skipped.
82 //
83 QemuFwCfgSkipBytes ((UINTN)Header.Size);
84 *ReadIndex += (UINTN)Header.Size;
85 }
86
87 return EFI_END_OF_FILE;
88 }