summaryrefslogtreecommitdiff
path: root/Platform/96Boards/LsConnectorDxe/LsConnectorDxe.c
blob: f19d9563505665288d493ea499ab828a69285d27 (plain)
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/** @file

  Copyright (c) 2018, Linaro, Ltd. All rights reserved.

  This program and the accompanying materials are licensed and made available
  under the terms and conditions of the BSD License which accompanies this
  distribution.  The full text of the license may be found at
  http://opensource.org/licenses/bsd-license.php

  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

**/

#include <Uefi.h>
#include <Library/BaseLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/DebugLib.h>
#include <Library/DevicePathLib.h>
#include <Library/HiiLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/UefiLib.h>
#include <Library/UefiRuntimeServicesTableLib.h>
#include <Protocol/LsConnector.h>
#include <Protocol/Mezzanine.h>

#include "LsConnectorDxe.h"

extern  UINT8                     LsConnectorHiiBin[];
extern  UINT8                     LsConnectorDxeStrings[];

typedef struct {
  VENDOR_DEVICE_PATH              VendorDevicePath;
  EFI_DEVICE_PATH_PROTOCOL        End;
} HII_VENDOR_DEVICE_PATH;

STATIC HII_VENDOR_DEVICE_PATH     m96BoardsDxeVendorDevicePath = {
  {
    {
      HARDWARE_DEVICE_PATH,
      HW_VENDOR_DP,
      {
        (UINT8) (sizeof (VENDOR_DEVICE_PATH)),
        (UINT8) ((sizeof (VENDOR_DEVICE_PATH)) >> 8)
      }
    },
    NINETY_SIX_BOARDS_FORMSET_GUID
  },
  {
    END_DEVICE_PATH_TYPE,
    END_ENTIRE_DEVICE_PATH_SUBTYPE,
    {
      (UINT8) (END_DEVICE_PATH_LENGTH),
      (UINT8) ((END_DEVICE_PATH_LENGTH) >> 8)
    }
  }
};

STATIC LS_CONNECTOR_PROTOCOL  mLsConnector;
STATIC EFI_EVENT              EndOfDxeEvent;

STATIC
EFI_STATUS
InstallHiiPages (
  VOID
  )
{
  EFI_STATUS                      Status;
  EFI_HII_HANDLE                  HiiHandle;
  EFI_HANDLE                      DriverHandle;

  DriverHandle = NULL;
  Status = gBS->InstallMultipleProtocolInterfaces (&DriverHandle,
                  &gEfiDevicePathProtocolGuid,
                  &m96BoardsDxeVendorDevicePath,
                  NULL);
  if (EFI_ERROR (Status)) {
    return Status;
  }

  HiiHandle = HiiAddPackages (&g96BoardsFormsetGuid,
                              DriverHandle,
                              LsConnectorDxeStrings,
                              LsConnectorHiiBin,
                              NULL);

  if (HiiHandle == NULL) {
    gBS->UninstallMultipleProtocolInterfaces (DriverHandle,
           &gEfiDevicePathProtocolGuid,
           &m96BoardsDxeVendorDevicePath,
           NULL);
    return EFI_OUT_OF_RESOURCES;
  }
  return EFI_SUCCESS;
}

STATIC
VOID
EFIAPI
ApplyDeviceTreeOverlay (
  EFI_EVENT           Event,
  VOID                *Context
  )
{
  VOID                    *Dtb;
  MEZZANINE_PROTOCOL      *Mezzanine;
  EFI_STATUS              Status;

  //
  // Find the DTB in the configuration table array. If it isn't there, just
  // bail without an error: we may be running on an ACPI platform even if
  // this driver does not support it [yet].
  //
  Status = EfiGetSystemConfigurationTable (&gFdtTableGuid, &Dtb);
  if (Status == EFI_NOT_FOUND) {
    return;
  }
  ASSERT_EFI_ERROR (Status);

  Status = gBS->LocateProtocol (&g96BoardsMezzanineProtocolGuid, NULL,
                  (VOID **)&Mezzanine);
  if (EFI_ERROR (Status)) {
    DEBUG ((DEBUG_INFO, "%a: no mezzanine driver active\n", __FUNCTION__));
    return;
  }

  Status = Mezzanine->ApplyDeviceTreeOverlay (Mezzanine, Dtb);
  if (EFI_ERROR (Status)) {
    DEBUG ((DEBUG_WARN, "%a: failed to apply DT overlay - %r\n", __FUNCTION__,
      Status));
  }
}

/**
  The entry point for 96BoardsDxe driver.

  @param[in] ImageHandle     The image handle of the driver.
  @param[in] SystemTable     The system table.

  @retval EFI_ALREADY_STARTED     The driver already exists in system.
  @retval EFI_OUT_OF_RESOURCES    Fail to execute entry point due to lack of
                                  resources.
  @retval EFI_SUCCES              All the related protocols are installed on
                                  the driver.

**/
EFI_STATUS
EFIAPI
EntryPoint (
  IN EFI_HANDLE                   ImageHandle,
  IN EFI_SYSTEM_TABLE             *SystemTable
  )
{
  EFI_STATUS                      Status;
  NINETY_SIX_BOARDS_CONFIG_DATA   ConfigData;
  UINTN                           BufferSize;

  //
  // Get the current config settings from the EFI variable.
  //
  BufferSize = sizeof (ConfigData);
  Status = gRT->GetVariable (NINETY_SIX_BOARDS_CONFIG_VARIABLE_NAME,
                  &g96BoardsFormsetGuid, NULL, &BufferSize, &ConfigData);
  if (EFI_ERROR (Status)) {
    DEBUG ((DEBUG_INFO, "%a: no config data found\n", __FUNCTION__));
    ConfigData.MezzanineType = MEZZANINE_NONE;
  }

  if (!EFI_ERROR (Status) &&
      ConfigData.MezzanineType >= MEZZANINE_MAX) {
    DEBUG ((DEBUG_WARN,
      "%a: invalid value for %s, defaulting to MEZZANINE_NONE\n",
      __FUNCTION__, NINETY_SIX_BOARDS_CONFIG_VARIABLE_NAME));
    ConfigData.MezzanineType = MEZZANINE_NONE;
    Status = EFI_INVALID_PARAMETER; // trigger setvar below
  }

  //
  // Write the newly selected value back to the variable store.
  //
  if (EFI_ERROR (Status)) {
    ZeroMem (&ConfigData.Reserved, sizeof (ConfigData.Reserved));
    Status = gRT->SetVariable (NINETY_SIX_BOARDS_CONFIG_VARIABLE_NAME,
                    &g96BoardsFormsetGuid,
                    EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,
                    sizeof (ConfigData), &ConfigData);

    if (EFI_ERROR (Status)) {
      DEBUG ((DEBUG_ERROR, "%a: gRT->SetVariable () failed - %r\n",
        __FUNCTION__, Status));
      return Status;
    }
  }

  switch (ConfigData.MezzanineType) {
  case MEZZANINE_SECURE96:
    mLsConnector.MezzanineType = MezzanineSecure96;
    break;
  default:
    mLsConnector.MezzanineType = MezzanineUnknown;
  }

  Status = gBS->InstallProtocolInterface (&ImageHandle,
                  &g96BoardsLsConnectorProtocolGuid,
                  EFI_NATIVE_INTERFACE,
                  &mLsConnector);
  if (EFI_ERROR (Status)) {
    return Status;
  }

  Status = gBS->CreateEventEx (
                  EVT_NOTIFY_SIGNAL,
                  TPL_NOTIFY,
                  ApplyDeviceTreeOverlay,
                  NULL,
                  &gEfiEndOfDxeEventGroupGuid,
                  &EndOfDxeEvent);
  ASSERT_EFI_ERROR (Status);

  return InstallHiiPages ();
}