hc
2024-03-22 a0752693d998599af469473b8dc239ef973a012f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/** @file
*
*  Copyright (c) 2018, ARM Limited. All rights reserved.
*
*  SPDX-License-Identifier: BSD-2-Clause-Patent
*
**/
 
#include <Library/DebugLib.h>
#include <Library/HobLib.h>
#include <Library/PeimEntryPoint.h>
#include <Library/PeiServicesLib.h>
#include <libfdt.h>
#include <Ppi/SgiPlatformId.h>
#include <SgiPlatform.h>
 
/**
 
  This function returns the System ID of the platform
 
  This functions locates the NtFwConfig PPI and gets the base address of
  NT_FW_CONFIG DT from which the platform ID and config ID is obtained
  using FDT helper functions
 
  @param[out]      Pointer to the SGI_PLATFORM_DESCRIPTOR HoB
  @return          returns EFI_SUCCESS on success and EFI_INVALID_PARAMETER on error
 
**/
 
STATIC
EFI_STATUS
GetSgiSystemId (
  OUT SGI_PLATFORM_DESCRIPTOR   *HobData
)
{
  CONST UINT32                  *Property;
  INT32                         Offset;
  CONST VOID                    *NtFwCfgDtBlob;
  SGI_NT_FW_CONFIG_INFO_PPI     *NtFwConfigInfoPpi;
  EFI_STATUS                    Status;
 
  Status = PeiServicesLocatePpi (&gNtFwConfigDtInfoPpiGuid, 0, NULL,
             (VOID**)&NtFwConfigInfoPpi);
  if (EFI_ERROR (Status)) {
    DEBUG ((DEBUG_ERROR,
      "PeiServicesLocatePpi failed with error %r\n", Status));
    return EFI_INVALID_PARAMETER;
  }
 
  NtFwCfgDtBlob = (VOID *)(UINTN)NtFwConfigInfoPpi->NtFwConfigDtAddr;
  if (fdt_check_header (NtFwCfgDtBlob) != 0) {
    DEBUG ((DEBUG_ERROR, "Invalid DTB file %p passed\n", NtFwCfgDtBlob));
    return EFI_INVALID_PARAMETER;
  }
 
  Offset = fdt_subnode_offset (NtFwCfgDtBlob, 0, "system-id");
  if (Offset == -FDT_ERR_NOTFOUND) {
    DEBUG ((DEBUG_ERROR, "Invalid DTB : system-id node not found\n"));
    return EFI_INVALID_PARAMETER;
  }
 
  Property = fdt_getprop (NtFwCfgDtBlob, Offset, "platform-id", NULL);
  if (Property == NULL) {
    DEBUG ((DEBUG_ERROR, "platform-id property not found\n"));
    return EFI_INVALID_PARAMETER;
  }
 
  HobData->PlatformId = fdt32_to_cpu (*Property);
 
  Property = fdt_getprop (NtFwCfgDtBlob, Offset, "config-id", NULL);
  if (Property == NULL) {
    DEBUG ((DEBUG_ERROR, "config-id property not found\n"));
    return EFI_INVALID_PARAMETER;
  }
 
  HobData->ConfigId = fdt32_to_cpu (*Property);
 
  Property = fdt_getprop (NtFwCfgDtBlob, Offset, "multi-chip-mode", NULL);
  if (Property == NULL) {
    DEBUG ((DEBUG_WARN, "multi-chip-mode property not found\n"));
    HobData->MultiChipMode = 0;
  } else {
    HobData->MultiChipMode = fdt32_to_cpu (*Property);
  }
 
  return EFI_SUCCESS;
}
 
/**
 
 This function creates a System ID HOB and assigns PlatformId and
 ConfigId as the HobData
 
 @return asserts on error and returns EFI_INVALID_PARAMETER. On success
 returns EFI_SUCCESS
 
**/
EFI_STATUS
EFIAPI
SgiPlatformPeim (
 IN       EFI_PEI_FILE_HANDLE  FileHandle,
 IN CONST EFI_PEI_SERVICES     **PeiServices
)
{
  SGI_PLATFORM_DESCRIPTOR       *HobData;
  EFI_STATUS                    Status;
 
  // Create platform descriptor HOB
  HobData = (SGI_PLATFORM_DESCRIPTOR *)BuildGuidHob (
                                         &gArmSgiPlatformIdDescriptorGuid,
                                         sizeof (SGI_PLATFORM_DESCRIPTOR));
 
  // Get the system id from the platform specific nt_fw_config device tree
  if (HobData == NULL) {
    DEBUG ((DEBUG_ERROR, "Platform HOB is NULL\n"));
    ASSERT (FALSE);
    return EFI_OUT_OF_RESOURCES;
  }
 
  Status = GetSgiSystemId (HobData);
  if (EFI_ERROR (Status)) {
    ASSERT_EFI_ERROR (Status);
    return EFI_INVALID_PARAMETER;
  }
 
  if (HobData->PlatformId == 0) {
    ASSERT (FALSE);
    return EFI_INVALID_PARAMETER;
  }
 
  return EFI_SUCCESS;
}