summaryrefslogtreecommitdiffstats
path: root/EmbeddedPkg/Drivers/ConsolePrefDxe/ConsolePrefDxe.c
blob: 08bba1bbf111a213de2354216867dbee137424aa (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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/** @file
*
*  Copyright (c) 2017, Linaro, Ltd. All rights reserved.
*
*  SPDX-License-Identifier: BSD-2-Clause-Patent
*
**/

#include <Uefi.h>
#include <IndustryStandard/Acpi.h>
#include <libfdt.h>
#include <Library/BaseLib.h>
#include <Library/DebugLib.h>
#include <Library/DevicePathLib.h>
#include <Library/HiiLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/UefiDriverEntryPoint.h>
#include <Library/UefiLib.h>
#include <Library/UefiRuntimeServicesTableLib.h>

#include <Protocol/AcpiTable.h>
#include <Protocol/AcpiSystemDescriptionTable.h>

#include "ConsolePrefDxe.h"

#define SPCR_SIG    EFI_ACPI_2_0_SERIAL_PORT_CONSOLE_REDIRECTION_TABLE_SIGNATURE

extern  UINT8                     ConsolePrefHiiBin[];
extern  UINT8                     ConsolePrefDxeStrings[];

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

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

STATIC EFI_EVENT                  mReadyToBootEvent;

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

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

  HiiHandle = HiiAddPackages (&gConsolePrefFormSetGuid,
                              DriverHandle,
                              ConsolePrefDxeStrings,
                              ConsolePrefHiiBin,
                              NULL);

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

STATIC
VOID
RemoveDtStdoutPath (
  VOID
)
{
  VOID        *Dtb;
  INT32       Node;
  INT32       Error;
  EFI_STATUS  Status;

  Status = EfiGetSystemConfigurationTable (&gFdtTableGuid, &Dtb);
  if (EFI_ERROR (Status)) {
    DEBUG ((DEBUG_INFO, "%a: could not retrieve DT blob - %r\n", __FUNCTION__,
      Status));
    return;
  }

  Node = fdt_path_offset (Dtb, "/chosen");
  if (Node < 0) {
    return;
  }

  Error = fdt_delprop (Dtb, Node, "stdout-path");
  if (Error) {
    DEBUG ((DEBUG_INFO, "%a: Failed to delete 'stdout-path' property: %a\n",
      __FUNCTION__, fdt_strerror (Error)));
  }
}

STATIC
VOID
RemoveSpcrTable (
  VOID
  )
{
  EFI_ACPI_SDT_PROTOCOL           *Sdt;
  EFI_ACPI_TABLE_PROTOCOL         *AcpiTable;
  EFI_STATUS                      Status;
  UINTN                           TableIndex;
  EFI_ACPI_SDT_HEADER             *TableHeader;
  EFI_ACPI_TABLE_VERSION          TableVersion;
  UINTN                           TableKey;

  Status = gBS->LocateProtocol (&gEfiAcpiTableProtocolGuid, NULL,
                  (VOID **)&AcpiTable);
  if (EFI_ERROR (Status)) {
    return;
  }

  Status = gBS->LocateProtocol (&gEfiAcpiSdtProtocolGuid, NULL, (VOID **)&Sdt);
  if (EFI_ERROR (Status)) {
    return;
  }

  TableIndex  = 0;
  TableKey    = 0;
  TableHeader = NULL;

  do {
    Status = Sdt->GetAcpiTable (TableIndex++, &TableHeader, &TableVersion,
                    &TableKey);
    if (EFI_ERROR (Status)) {
      break;
    }

    if (TableHeader->Signature != SPCR_SIG) {
      continue;
    }

    Status = AcpiTable->UninstallAcpiTable (AcpiTable, TableKey);
    if (EFI_ERROR (Status)) {
      DEBUG ((DEBUG_WARN, "%a: failed to uninstall SPCR table - %r\n",
        __FUNCTION__, Status));
    }
    break;
  } while (TRUE);
}

STATIC
VOID
EFIAPI
OnReadyToBoot (
  IN EFI_EVENT  Event,
  IN VOID       *Context
  )
{
  CONSOLE_PREF_VARSTORE_DATA      ConsolePref;
  UINTN                           BufferSize;
  EFI_STATUS                      Status;
  VOID                            *Gop;

  BufferSize = sizeof (ConsolePref);
  Status = gRT->GetVariable (CONSOLE_PREF_VARIABLE_NAME,
                  &gConsolePrefFormSetGuid, NULL, &BufferSize, &ConsolePref);
  if (EFI_ERROR (Status)) {
    DEBUG ((DEBUG_ERROR,
      "%a: variable '%s' could not be read - bailing!\n", __FUNCTION__,
      CONSOLE_PREF_VARIABLE_NAME));
    return;
  }

  if (ConsolePref.Console == CONSOLE_PREF_SERIAL) {
    DEBUG ((DEBUG_INFO,
      "%a: serial console preferred - doing nothing\n", __FUNCTION__));
    return;
  }

  //
  // Check if any GOP instances exist: if so, disable stdout-path and SPCR
  //
  Status = gBS->LocateProtocol (&gEfiGraphicsOutputProtocolGuid, NULL, &Gop);
  if (EFI_ERROR (Status)) {
    DEBUG ((DEBUG_INFO,
      "%a: no GOP instances found - doing nothing (%r)\n", __FUNCTION__,
      Status));
    return;
  }

  RemoveDtStdoutPath ();
  RemoveSpcrTable ();
}

/**
  The entry point for ConsolePrefDxe 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_SUCCESS             All the related protocols are installed on
                                  the driver.

**/
EFI_STATUS
EFIAPI
ConsolePrefDxeEntryPoint (
  IN EFI_HANDLE                   ImageHandle,
  IN EFI_SYSTEM_TABLE             *SystemTable
  )
{
  EFI_STATUS                      Status;
  CONSOLE_PREF_VARSTORE_DATA      ConsolePref;
  UINTN                           BufferSize;

  //
  // Get the current console preference from the ConsolePref variable.
  //
  BufferSize = sizeof (ConsolePref);
  Status = gRT->GetVariable (CONSOLE_PREF_VARIABLE_NAME,
                  &gConsolePrefFormSetGuid, NULL, &BufferSize, &ConsolePref);
  if (EFI_ERROR (Status)) {
    DEBUG ((DEBUG_INFO,
      "%a: no console preference found, defaulting to graphical\n",
      __FUNCTION__));
    ConsolePref.Console = CONSOLE_PREF_GRAPHICAL;
  }

  if (!EFI_ERROR (Status) &&
      ConsolePref.Console != CONSOLE_PREF_GRAPHICAL &&
      ConsolePref.Console != CONSOLE_PREF_SERIAL) {
    DEBUG ((DEBUG_WARN, "%a: invalid value for %s, defaulting to graphical\n",
      __FUNCTION__, CONSOLE_PREF_VARIABLE_NAME));
    ConsolePref.Console = CONSOLE_PREF_GRAPHICAL;
    Status = EFI_INVALID_PARAMETER; // trigger setvar below
  }

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

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

  Status = gBS->CreateEventEx (EVT_NOTIFY_SIGNAL, TPL_CALLBACK,
                  OnReadyToBoot, NULL, &gEfiEventReadyToBootGuid,
                  &mReadyToBootEvent);
  ASSERT_EFI_ERROR (Status);

  return InstallHiiPages ();
}