summaryrefslogtreecommitdiff
path: root/src/VBox/Devices/EFI/Firmware/VBoxPkg/VBoxVariable/InitVariable.c
blob: 4b4d6d0a852ef8af09e046ca201c66ebaa959058 (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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
/* $Id: InitVariable.c $ */
/** @file
 * InitVariable.h
 */

/*
 * Copyright (C) 2012 Oracle Corporation
 *
 * This file is part of VirtualBox Open Source Edition (OSE), as
 * available from http://www.virtualbox.org. This file is free software;
 * you can redistribute it and/or modify it under the terms of the GNU
 * General Public License (GPL) as published by the Free Software
 * Foundation, in version 2 as it comes in the "COPYING" file of the
 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
 *
 * The contents of this file may alternatively be used under the terms
 * of the Common Development and Distribution License Version 1.0
 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
 * VirtualBox OSE distribution, in which case the provisions of the
 * CDDL are applicable instead of those of the GPL.
 *
 * You may elect to license modified versions of this file under the
 * terms and conditions of either the GPL or the CDDL or both.
 */

/** @file

  Implment all four UEFI runtime variable services and
  install variable architeture protocol.

Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>
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 "Variable.h"


EFI_EVENT   mVirtualAddressChangeEvent = NULL;

#ifdef VBOX
# include <Library/PrintLib.h>
# include <Library/TimerLib.h>
# include "VBoxPkg.h"
# include "DevEFI.h"
# include "iprt/asm.h"


static UINT32 VBoxReadNVRAM(UINT8 *pu8Buffer, UINT32 cbBuffer)
{
    UINT32 idxBuffer = 0;
    for (idxBuffer = 0; idxBuffer < cbBuffer; ++idxBuffer)
        pu8Buffer[idxBuffer] = ASMInU8(EFI_PORT_VARIABLE_OP);
    return idxBuffer;
}

DECLINLINE(void) VBoxWriteNVRAMU32Param(UINT32 u32CodeParam, UINT32 u32Param)
{
    ASMOutU32(EFI_PORT_VARIABLE_OP, u32CodeParam);
    ASMOutU32(EFI_PORT_VARIABLE_PARAM, u32Param);
}

static UINT32 VBoxWriteNVRAMByteArrayParam(const UINT8 *pbParam, UINT32 cbParam)
{
    UINT32 idxParam = 0;
    for (idxParam = 0; idxParam < cbParam; ++idxParam)
        ASMOutU8(EFI_PORT_VARIABLE_PARAM, pbParam[idxParam]);
    return idxParam;
}

static void VBoxWriteNVRAMNameParam(const CHAR16 *pwszName)
{
    UINTN i;
    UINTN cwcName = StrLen(pwszName);

    ASMOutU32(EFI_PORT_VARIABLE_OP, EFI_VM_VARIABLE_OP_NAME_UTF16);
    for (i = 0; i <= cwcName; i++)
        ASMOutU16(EFI_PORT_VARIABLE_PARAM, pwszName[i]);
}

DECLINLINE(UINT32) VBoxWriteNVRAMGuidParam(const EFI_GUID *pGuid)
{
    ASMOutU32(EFI_PORT_VARIABLE_OP, EFI_VM_VARIABLE_OP_GUID);
    return VBoxWriteNVRAMByteArrayParam((UINT8 *)pGuid, sizeof(EFI_GUID));
}

static UINT32 VBoxWriteNVRAMDoOp(UINT32 u32Operation)
{
    UINT32 u32Rc;
    VBoxLogFlowFuncEnter();
    VBoxLogFlowFuncMarkVar(u32Operation, "%x");
    VBoxWriteNVRAMU32Param(EFI_VM_VARIABLE_OP_START, u32Operation);

    while ((u32Rc = ASMInU32(EFI_PORT_VARIABLE_OP)) == EFI_VARIABLE_OP_STATUS_BSY)
    {
#if 0
        MicroSecondDelay (400);
#endif
        /* @todo: sleep here. bird: won't ever happen, so don't bother. */
    }
    VBoxLogFlowFuncMarkVar(u32Rc, "%x");
    VBoxLogFlowFuncLeave();
    return u32Rc;
}
#endif

/**

  This code finds variable in storage blocks (Volatile or Non-Volatile).

  @param VariableName               Name of Variable to be found.
  @param VendorGuid                 Variable vendor GUID.
  @param Attributes                 Attribute value of the variable found.
  @param DataSize                   Size of Data found. If size is less than the
                                    data, this value contains the required size.
  @param Data                       Data pointer.

  @return EFI_INVALID_PARAMETER     Invalid parameter
  @return EFI_SUCCESS               Find the specified variable
  @return EFI_NOT_FOUND             Not found
  @return EFI_BUFFER_TO_SMALL       DataSize is too small for the result

**/
EFI_STATUS
EFIAPI
RuntimeServiceGetVariable (
  IN CHAR16        *VariableName,
  IN EFI_GUID      *VendorGuid,
  OUT UINT32       *Attributes OPTIONAL,
  IN OUT UINTN     *DataSize,
  OUT VOID         *Data
  )
{
#ifndef VBOX
  return EmuGetVariable (
          VariableName,
          VendorGuid,
          Attributes OPTIONAL,
          DataSize,
          Data,
          &mVariableModuleGlobal->VariableGlobal[Physical]
          );
#else
    EFI_STATUS rc;
    UINT32 u32Rc;

    VBoxLogFlowFuncEnter();

    /*
     * Tell DevEFI to look for the specified variable.
     */
    VBoxWriteNVRAMGuidParam(VendorGuid);
    VBoxWriteNVRAMNameParam(VariableName);
    u32Rc = VBoxWriteNVRAMDoOp(EFI_VARIABLE_OP_QUERY);
    if (u32Rc == EFI_VARIABLE_OP_STATUS_OK)
    {
        /*
         * Check if we got enought space for the value.
         */
        UINT32 VarLen;
        ASMOutU32(EFI_PORT_VARIABLE_OP, EFI_VM_VARIABLE_OP_VALUE_LENGTH);
        VarLen = ASMInU32(EFI_PORT_VARIABLE_OP);
        VBoxLogFlowFuncMarkVar(*DataSize, "%d");
        VBoxLogFlowFuncMarkVar(VarLen, "%d");
        if (   VarLen <= *DataSize
            && Data)
        {
            /*
             * We do, then read it and, if requrest, the attribute.
             */
            *DataSize = VarLen;
            ASMOutU32(EFI_PORT_VARIABLE_OP, EFI_VM_VARIABLE_OP_VALUE);
            VBoxReadNVRAM((UINT8 *)Data, VarLen);

            if (Attributes)
            {
                ASMOutU32(EFI_PORT_VARIABLE_OP, EFI_VM_VARIABLE_OP_ATTRIBUTE);
                *Attributes = ASMInU32(EFI_PORT_VARIABLE_OP);
                VBoxLogFlowFuncMarkVar(Attributes, "%x");
            }

            rc = EFI_SUCCESS;
        }
        else
        {
            *DataSize = VarLen;
            rc = EFI_BUFFER_TOO_SMALL;
        }
    }
    else
    {
        rc = EFI_NOT_FOUND;
    }

    VBoxLogFlowFuncLeaveRC(rc);
    return rc;
#endif
}

/**

  This code Finds the Next available variable.

  @param VariableNameSize           Size of the variable name
  @param VariableName               Pointer to variable name
  @param VendorGuid                 Variable Vendor Guid

  @return EFI_INVALID_PARAMETER     Invalid parameter
  @return EFI_SUCCESS               Find the specified variable
  @return EFI_NOT_FOUND             Not found
  @return EFI_BUFFER_TO_SMALL       DataSize is too small for the result

**/
EFI_STATUS
EFIAPI
RuntimeServiceGetNextVariableName (
  IN OUT UINTN     *VariableNameSize,
  IN OUT CHAR16    *VariableName,
  IN OUT EFI_GUID  *VendorGuid
  )
{
#ifndef VBOX
  return EmuGetNextVariableName (
          VariableNameSize,
          VariableName,
          VendorGuid,
          &mVariableModuleGlobal->VariableGlobal[Physical]
          );
#else
    uint32_t    u32Rc;
    EFI_STATUS  rc;
    VBoxLogFlowFuncEnter();

    /*
     * Validate inputs.
     */
    if (!VariableNameSize || !VariableName || !VendorGuid)
    {
        VBoxLogFlowFuncLeaveRC(EFI_INVALID_PARAMETER);
        return EFI_INVALID_PARAMETER;
    }

    /*
     * Tell DevEFI which the current variable is, then ask for the next one.
     */
    if (!VariableName[0])
        u32Rc = VBoxWriteNVRAMDoOp(EFI_VARIABLE_OP_QUERY_REWIND);
    else
    {
        VBoxWriteNVRAMGuidParam(VendorGuid);
        VBoxWriteNVRAMNameParam(VariableName);
        u32Rc = VBoxWriteNVRAMDoOp(EFI_VARIABLE_OP_QUERY);
    }
    if (u32Rc == EFI_VARIABLE_OP_STATUS_OK)
        u32Rc = VBoxWriteNVRAMDoOp(EFI_VARIABLE_OP_QUERY_NEXT);
    /** @todo We're supposed to skip stuff depending on attributes and
     *        runtime/boottime, at least if EmuGetNextVariableName is something
     *        to go by... */

    if (u32Rc == EFI_VARIABLE_OP_STATUS_OK)
    {
        /*
         * Output buffer check.
         */
        UINT32      cwcName;
        ASMOutU32(EFI_PORT_VARIABLE_OP, EFI_VM_VARIABLE_OP_NAME_LENGTH_UTF16);
        cwcName = ASMInU32(EFI_PORT_VARIABLE_OP);
        if ((cwcName + 1) * 2 <= *VariableNameSize) /* ASSUMES byte size is specified */
        {
            UINT32 i;

            /*
             * Read back the result.
             */
            ASMOutU32(EFI_PORT_VARIABLE_OP, EFI_VM_VARIABLE_OP_GUID);
            VBoxReadNVRAM((UINT8 *)VendorGuid, sizeof(EFI_GUID));

            ASMOutU32(EFI_PORT_VARIABLE_OP, EFI_VM_VARIABLE_OP_NAME_UTF16);
            for (i = 0; i < cwcName; i++)
                VariableName[i] = ASMInU16(EFI_PORT_VARIABLE_OP);
            VariableName[i] = '\0';

            rc = EFI_SUCCESS;
        }
        else
            rc = EFI_BUFFER_TOO_SMALL;
        *VariableNameSize = (cwcName + 1) * 2;
    }
    else
        rc = EFI_NOT_FOUND; /* whatever */

    VBoxLogFlowFuncLeaveRC(rc);
    return rc;
#endif
}

/**

  This code sets variable in storage blocks (Volatile or Non-Volatile).

  @param VariableName                     Name of Variable to be found
  @param VendorGuid                       Variable vendor GUID
  @param Attributes                       Attribute value of the variable found
  @param DataSize                         Size of Data found. If size is less than the
                                          data, this value contains the required size.
  @param Data                             Data pointer

  @return EFI_INVALID_PARAMETER           Invalid parameter
  @return EFI_SUCCESS                     Set successfully
  @return EFI_OUT_OF_RESOURCES            Resource not enough to set variable
  @return EFI_NOT_FOUND                   Not found
  @return EFI_WRITE_PROTECTED             Variable is read-only

**/
EFI_STATUS
EFIAPI
RuntimeServiceSetVariable (
  IN CHAR16        *VariableName,
  IN EFI_GUID      *VendorGuid,
  IN UINT32        Attributes,
  IN UINTN         DataSize,
  IN VOID          *Data
  )
{
#ifndef VBOX
  return EmuSetVariable (
          VariableName,
          VendorGuid,
          Attributes,
          DataSize,
          Data,
          &mVariableModuleGlobal->VariableGlobal[Physical],
          &mVariableModuleGlobal->VolatileLastVariableOffset,
          &mVariableModuleGlobal->NonVolatileLastVariableOffset
          );
#else
    UINT32 u32Rc;
    VBoxLogFlowFuncEnter();
    VBoxLogFlowFuncMarkVar(VendorGuid, "%g");
    VBoxLogFlowFuncMarkVar(VariableName, "%s");
    VBoxLogFlowFuncMarkVar(DataSize, "%d");
    /* set guid */
    VBoxWriteNVRAMGuidParam(VendorGuid);
    /* set name */
    VBoxWriteNVRAMNameParam(VariableName);
    /* set attribute */
    VBoxWriteNVRAMU32Param(EFI_VM_VARIABLE_OP_ATTRIBUTE, Attributes);
    /* set value length */
    VBoxWriteNVRAMU32Param(EFI_VM_VARIABLE_OP_VALUE_LENGTH, (UINT32)DataSize);
    /* fill value bytes */
    ASMOutU32(EFI_PORT_VARIABLE_OP, EFI_VM_VARIABLE_OP_VALUE);
    VBoxWriteNVRAMByteArrayParam(Data, (UINT32)DataSize);
    /* start fetch operation */
    u32Rc = VBoxWriteNVRAMDoOp(EFI_VARIABLE_OP_ADD);
    /* process errors */
    VBoxLogFlowFuncLeave();
    switch (u32Rc)
    {
        case EFI_VARIABLE_OP_STATUS_OK:
            return EFI_SUCCESS;
        case EFI_VARIABLE_OP_STATUS_NOT_WP:
        default:
            return EFI_WRITE_PROTECTED;
    }
#endif
}

/**

  This code returns information about the EFI variables.

  @param Attributes                     Attributes bitmask to specify the type of variables
                                        on which to return information.
  @param MaximumVariableStorageSize     Pointer to the maximum size of the storage space available
                                        for the EFI variables associated with the attributes specified.
  @param RemainingVariableStorageSize   Pointer to the remaining size of the storage space available
                                        for EFI variables associated with the attributes specified.
  @param MaximumVariableSize            Pointer to the maximum size of an individual EFI variables
                                        associated with the attributes specified.

  @return EFI_INVALID_PARAMETER         An invalid combination of attribute bits was supplied.
  @return EFI_SUCCESS                   Query successfully.
  @return EFI_UNSUPPORTED               The attribute is not supported on this platform.

**/
EFI_STATUS
EFIAPI
RuntimeServiceQueryVariableInfo (
  IN  UINT32                 Attributes,
  OUT UINT64                 *MaximumVariableStorageSize,
  OUT UINT64                 *RemainingVariableStorageSize,
  OUT UINT64                 *MaximumVariableSize
  )
{
#ifndef VBOX
  return EmuQueryVariableInfo (
          Attributes,
          MaximumVariableStorageSize,
          RemainingVariableStorageSize,
          MaximumVariableSize,
          &mVariableModuleGlobal->VariableGlobal[Physical]
          );
#else
    *MaximumVariableStorageSize = 64 * 1024 * 1024;
    *MaximumVariableSize = 1024;
    *RemainingVariableStorageSize = 32 * 1024 * 1024;
    return EFI_SUCCESS;
#endif
}

/**
  Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.

  This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
  It convers pointer to new virtual address.

  @param  Event        Event whose notification function is being invoked.
  @param  Context      Pointer to the notification function's context.

**/
VOID
EFIAPI
VariableClassAddressChangeEvent (
  IN EFI_EVENT        Event,
  IN VOID             *Context
  )
{
#ifndef VBOX
  EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->PlatformLangCodes);
  EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->LangCodes);
  EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->PlatformLang);
  EfiConvertPointer (
    0x0,
    (VOID **) &mVariableModuleGlobal->VariableGlobal[Physical].NonVolatileVariableBase
    );
  EfiConvertPointer (
    0x0,
    (VOID **) &mVariableModuleGlobal->VariableGlobal[Physical].VolatileVariableBase
    );
  EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal);
#endif
}

/**
  EmuVariable Driver main entry point. The Variable driver places the 4 EFI
  runtime services in the EFI System Table and installs arch protocols
  for variable read and write services being available. It also registers
  notification function for EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.

  @param[in] ImageHandle    The firmware allocated handle for the EFI image.
  @param[in] SystemTable    A pointer to the EFI System Table.

  @retval EFI_SUCCESS       Variable service successfully initialized.

**/
EFI_STATUS
EFIAPI
VariableServiceInitialize (
  IN EFI_HANDLE         ImageHandle,
  IN EFI_SYSTEM_TABLE   *SystemTable
  )
{
  EFI_HANDLE  NewHandle;
  EFI_STATUS  Status;

  Status = VariableCommonInitialize (ImageHandle, SystemTable);
  ASSERT_EFI_ERROR (Status);

  SystemTable->RuntimeServices->GetVariable         = RuntimeServiceGetVariable;
  SystemTable->RuntimeServices->GetNextVariableName = RuntimeServiceGetNextVariableName;
  SystemTable->RuntimeServices->SetVariable         = RuntimeServiceSetVariable;
  SystemTable->RuntimeServices->QueryVariableInfo   = RuntimeServiceQueryVariableInfo;

  //
  // Now install the Variable Runtime Architectural Protocol on a new handle
  //
  NewHandle = NULL;
  Status = gBS->InstallMultipleProtocolInterfaces (
                  &NewHandle,
                  &gEfiVariableArchProtocolGuid,
                  NULL,
                  &gEfiVariableWriteArchProtocolGuid,
                  NULL,
                  NULL
                  );
  ASSERT_EFI_ERROR (Status);

  Status = gBS->CreateEventEx (
                  EVT_NOTIFY_SIGNAL,
                  TPL_NOTIFY,
                  VariableClassAddressChangeEvent,
                  NULL,
                  &gEfiEventVirtualAddressChangeGuid,
                  &mVirtualAddressChangeEvent
                  );
  ASSERT_EFI_ERROR (Status);

  /* Self Test */
    {
        EFI_GUID TestUUID = {0xe660597e, 0xb94d, 0x4209, {0x9c, 0x80, 0x18, 0x05, 0xb5, 0xd1, 0x9b, 0x69}};
        const char *pszVariable0 = "This is test!!!";
        const CHAR16 *pszVariable1 = L"This is test!!!";
        char szTestVariable[512];
#if 0
        rc = runtime->SetVariable(&TestUUID,
            NULL ,
            (EFI_VARIABLE_NON_VOLATILE|EFI_VARIABLE_BOOTSERVICE_ACCESS| EFI_VARIABLE_RUNTIME_ACCESS),
            0,
            NULL );
        ASSERT(rc == EFI_INVALID_PARAMETER);
#endif
        UINTN size = sizeof(szTestVariable),
        rc = RuntimeServiceSetVariable(
            L"Test0" ,
            &TestUUID,
            (EFI_VARIABLE_NON_VOLATILE|EFI_VARIABLE_BOOTSERVICE_ACCESS| EFI_VARIABLE_RUNTIME_ACCESS),
            AsciiStrSize(pszVariable0),
            (void *)pszVariable0);
        ASSERT_EFI_ERROR(rc);
        SetMem(szTestVariable, 512, 0);
        rc = RuntimeServiceGetVariable(
            L"Test0" ,
            &TestUUID,
            NULL,
            &size,
            (void *)szTestVariable);
        VBoxLogFlowFuncMarkVar(szTestVariable, "%a");

        ASSERT(CompareMem(szTestVariable, pszVariable0, size) == 0);

        rc = RuntimeServiceSetVariable(
            L"Test1" ,
            &TestUUID,
            (EFI_VARIABLE_NON_VOLATILE|EFI_VARIABLE_BOOTSERVICE_ACCESS| EFI_VARIABLE_RUNTIME_ACCESS),
            StrSize(pszVariable1),
            (void *)pszVariable1);
        ASSERT_EFI_ERROR(rc);
        SetMem(szTestVariable, 512, 0);
        size = StrSize(pszVariable1);
        rc = RuntimeServiceGetVariable(
            L"Test1" ,
            &TestUUID,
            NULL,
            &size,
            (void *)szTestVariable);
        VBoxLogFlowFuncMarkVar((CHAR16 *)szTestVariable, "%s");
        ASSERT(CompareMem(szTestVariable, pszVariable1, size) == 0);
    }

  return EFI_SUCCESS;
}