1 /*++
2 
3 Caution: This file is used for Duet platform only, do not use them in real platform.
4 All variable code, variable metadata, and variable data used by Duet platform are on
5 disk. They can be changed by user. BIOS is not able to protoect those.
6 Duet trusts all meta data from disk. If variable code, variable metadata and variable
7 data is modified in inproper way, the behavior is undefined.
8 
9 Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>
10 This program and the accompanying materials
11 are licensed and made available under the terms and conditions of the BSD License
12 which accompanies this distribution.  The full text of the license may be found at
13 http://opensource.org/licenses/bsd-license.php
14 
15 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
16 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 
18 Module Name:
19 
20     MemStorage.c
21 
22 Abstract:
23 
24     handles variable store/reads with emulated memory
25 
26 Revision History
27 
28 --*/
29 #include "FSVariable.h"
30 
31 VOID
32 EFIAPI
33 OnVirtualAddressChangeMs (
34   IN EFI_EVENT            Event,
35   IN VOID                 *Context
36   );
37 
38 EFI_STATUS
39 EFIAPI
40 MemEraseStore(
41   IN VARIABLE_STORAGE   *This
42   );
43 
44 EFI_STATUS
45 EFIAPI
46 MemWriteStore (
47   IN VARIABLE_STORAGE   *This,
48   IN UINTN                Offset,
49   IN UINTN                BufferSize,
50   IN VOID                 *Buffer
51   );
52 
53 EFI_STATUS
MemStorageConstructor(OUT VARIABLE_STORAGE ** VarStore,OUT EFI_EVENT_NOTIFY * GoVirtualEvent,IN UINTN Size)54 MemStorageConstructor (
55   OUT VARIABLE_STORAGE          **VarStore,
56   OUT EFI_EVENT_NOTIFY          *GoVirtualEvent,
57   IN  UINTN                     Size
58   )
59 {
60   EFI_STATUS                  Status;
61   VS_DEV                      *Dev;
62 
63   Status = gBS->AllocatePool (EfiRuntimeServicesData, sizeof(VS_DEV), (VOID **) &Dev);
64   ASSERT_EFI_ERROR (Status);
65 
66   ZeroMem (Dev, sizeof(VS_DEV));
67 
68   Dev->Signature   = VS_DEV_SIGNATURE;
69   Dev->Size        = Size;
70 
71   Dev->VarStore.Erase    = MemEraseStore;
72   Dev->VarStore.Write    = MemWriteStore;
73 
74   Status = gBS->AllocatePool (EfiRuntimeServicesData, Size, (VOID **) &VAR_DATA_PTR (Dev));
75   ASSERT_EFI_ERROR (Status);
76 
77   DEBUG ((EFI_D_ERROR, "VStorage: Size = 0x%x\n", Size));
78 
79   *VarStore       = &Dev->VarStore;
80   *GoVirtualEvent = OnVirtualAddressChangeMs;
81 
82   return EFI_SUCCESS;
83 }
84 
85 VOID
86 EFIAPI
OnVirtualAddressChangeMs(IN EFI_EVENT Event,IN VOID * Context)87 OnVirtualAddressChangeMs (
88   IN EFI_EVENT            Event,
89   IN VOID                 *Context
90   )
91 {
92   VS_DEV                  *Dev;
93 
94   Dev = DEV_FROM_THIS (Context);
95 
96   EfiConvertPointer (0, (VOID **)&VAR_DATA_PTR (Dev));
97   EfiConvertPointer (0, (VOID **)&Dev->VarStore.Erase);
98   EfiConvertPointer (0, (VOID **)&Dev->VarStore.Write);
99 }
100 
101 EFI_STATUS
102 EFIAPI
MemEraseStore(IN VARIABLE_STORAGE * This)103 MemEraseStore(
104   IN VARIABLE_STORAGE   *This
105   )
106 {
107   VS_DEV              *Dev;
108 
109   Dev = DEV_FROM_THIS(This);
110   SetMem (VAR_DATA_PTR (Dev), Dev->Size, VAR_DEFAULT_VALUE);
111   return EFI_SUCCESS;
112 }
113 
114 
115 EFI_STATUS
116 EFIAPI
MemWriteStore(IN VARIABLE_STORAGE * This,IN UINTN Offset,IN UINTN BufferSize,IN VOID * UserBuffer)117 MemWriteStore (
118   IN VARIABLE_STORAGE     *This,
119   IN UINTN                Offset,
120   IN UINTN                BufferSize,
121   IN VOID                 *UserBuffer
122   )
123 {
124   VS_DEV              *Dev;
125 
126   Dev = DEV_FROM_THIS(This);
127 
128   ASSERT (Offset + BufferSize < Dev->Size);
129 
130   // For better performance
131   if (VAR_DATA_PTR (Dev) + Offset != UserBuffer) {
132     CopyMem (VAR_DATA_PTR (Dev) + Offset, UserBuffer, BufferSize);
133   }
134   return EFI_SUCCESS;
135 }
136