summaryrefslogtreecommitdiffstats
path: root/OvmfPkg/VirtioFsDxe/DriverBinding.c
blob: e273c9f362eb5fc92c7cde1ebfd3ceb29e6101fc (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
/** @file
  Provide EFI_SIMPLE_FILE_SYSTEM_PROTOCOL instances on virtio-fs devices.

  Copyright (C) 2020, Red Hat, Inc.

  SPDX-License-Identifier: BSD-2-Clause-Patent
**/

#include <Library/BaseLib.h>                  // AsciiStrCmp()
#include <Library/MemoryAllocationLib.h>      // AllocatePool()
#include <Library/UefiBootServicesTableLib.h> // gBS
#include <Protocol/ComponentName2.h>          // EFI_COMPONENT_NAME2_PROTOCOL
#include <Protocol/DriverBinding.h>           // EFI_DRIVER_BINDING_PROTOCOL

#include "VirtioFsDxe.h"

//
// UEFI Driver Model protocol instances.
//
STATIC EFI_DRIVER_BINDING_PROTOCOL  mDriverBinding;
STATIC EFI_COMPONENT_NAME2_PROTOCOL mComponentName2;

//
// UEFI Driver Model protocol member functions.
//
EFI_STATUS
EFIAPI
VirtioFsBindingSupported (
  IN EFI_DRIVER_BINDING_PROTOCOL *This,
  IN EFI_HANDLE                  ControllerHandle,
  IN EFI_DEVICE_PATH_PROTOCOL    *RemainingDevicePath OPTIONAL
  )
{
  EFI_STATUS             Status;
  VIRTIO_DEVICE_PROTOCOL *Virtio;
  EFI_STATUS             CloseStatus;

  Status = gBS->OpenProtocol (ControllerHandle, &gVirtioDeviceProtocolGuid,
                  (VOID **)&Virtio, This->DriverBindingHandle,
                  ControllerHandle, EFI_OPEN_PROTOCOL_BY_DRIVER);
  if (EFI_ERROR (Status)) {
    return Status;
  }

  if (Virtio->SubSystemDeviceId != VIRTIO_SUBSYSTEM_FILESYSTEM) {
    Status = EFI_UNSUPPORTED;
  }

  CloseStatus = gBS->CloseProtocol (ControllerHandle,
                       &gVirtioDeviceProtocolGuid, This->DriverBindingHandle,
                       ControllerHandle);
  ASSERT_EFI_ERROR (CloseStatus);

  return Status;
}

EFI_STATUS
EFIAPI
VirtioFsBindingStart (
  IN EFI_DRIVER_BINDING_PROTOCOL *This,
  IN EFI_HANDLE                  ControllerHandle,
  IN EFI_DEVICE_PATH_PROTOCOL    *RemainingDevicePath OPTIONAL
  )
{
  VIRTIO_FS  *VirtioFs;
  EFI_STATUS Status;
  EFI_STATUS CloseStatus;

  VirtioFs = AllocatePool (sizeof *VirtioFs);
  if (VirtioFs == NULL) {
    return EFI_OUT_OF_RESOURCES;
  }
  VirtioFs->Signature = VIRTIO_FS_SIG;

  Status = gBS->OpenProtocol (ControllerHandle, &gVirtioDeviceProtocolGuid,
                  (VOID **)&VirtioFs->Virtio, This->DriverBindingHandle,
                  ControllerHandle, EFI_OPEN_PROTOCOL_BY_DRIVER);
  if (EFI_ERROR (Status)) {
    goto FreeVirtioFs;
  }

  Status = VirtioFsInit (VirtioFs);
  if (EFI_ERROR (Status)) {
    goto CloseVirtio;
  }

  Status = VirtioFsFuseInitSession (VirtioFs);
  if (EFI_ERROR (Status)) {
    goto UninitVirtioFs;
  }

  Status = gBS->CreateEvent (EVT_SIGNAL_EXIT_BOOT_SERVICES, TPL_CALLBACK,
                  VirtioFsExitBoot, VirtioFs, &VirtioFs->ExitBoot);
  if (EFI_ERROR (Status)) {
    goto UninitVirtioFs;
  }

  InitializeListHead (&VirtioFs->OpenFiles);
  VirtioFs->SimpleFs.Revision   = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_REVISION;
  VirtioFs->SimpleFs.OpenVolume = VirtioFsOpenVolume;

  Status = gBS->InstallProtocolInterface (&ControllerHandle,
                  &gEfiSimpleFileSystemProtocolGuid, EFI_NATIVE_INTERFACE,
                  &VirtioFs->SimpleFs);
  if (EFI_ERROR (Status)) {
    goto CloseExitBoot;
  }

  return EFI_SUCCESS;

CloseExitBoot:
  CloseStatus = gBS->CloseEvent (VirtioFs->ExitBoot);
  ASSERT_EFI_ERROR (CloseStatus);

UninitVirtioFs:
  VirtioFsUninit (VirtioFs);

CloseVirtio:
  CloseStatus = gBS->CloseProtocol (ControllerHandle,
                       &gVirtioDeviceProtocolGuid, This->DriverBindingHandle,
                       ControllerHandle);
  ASSERT_EFI_ERROR (CloseStatus);

FreeVirtioFs:
  FreePool (VirtioFs);

  return Status;
}

EFI_STATUS
EFIAPI
VirtioFsBindingStop (
  IN EFI_DRIVER_BINDING_PROTOCOL *This,
  IN EFI_HANDLE                  ControllerHandle,
  IN UINTN                       NumberOfChildren,
  IN EFI_HANDLE                  *ChildHandleBuffer OPTIONAL
  )
{
  EFI_STATUS                      Status;
  EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *SimpleFs;
  VIRTIO_FS                       *VirtioFs;

  Status = gBS->OpenProtocol (ControllerHandle,
                  &gEfiSimpleFileSystemProtocolGuid, (VOID **)&SimpleFs,
                  This->DriverBindingHandle, ControllerHandle,
                  EFI_OPEN_PROTOCOL_GET_PROTOCOL);
  if (EFI_ERROR (Status)) {
    return Status;
  }

  VirtioFs = VIRTIO_FS_FROM_SIMPLE_FS (SimpleFs);

  if (!IsListEmpty (&VirtioFs->OpenFiles)) {
    return EFI_ACCESS_DENIED;
  }

  Status = gBS->UninstallProtocolInterface (ControllerHandle,
                  &gEfiSimpleFileSystemProtocolGuid, SimpleFs);
  if (EFI_ERROR (Status)) {
    return Status;
  }

  Status = gBS->CloseEvent (VirtioFs->ExitBoot);
  ASSERT_EFI_ERROR (Status);

  VirtioFsUninit (VirtioFs);

  Status = gBS->CloseProtocol (ControllerHandle, &gVirtioDeviceProtocolGuid,
                  This->DriverBindingHandle, ControllerHandle);
  ASSERT_EFI_ERROR (Status);

  FreePool (VirtioFs);

  return EFI_SUCCESS;
}

EFI_STATUS
EFIAPI
VirtioFsGetDriverName (
  IN  EFI_COMPONENT_NAME2_PROTOCOL *This,
  IN  CHAR8                        *Language,
  OUT CHAR16                       **DriverName
  )
{
  if (AsciiStrCmp (Language, "en") != 0) {
    return EFI_UNSUPPORTED;
  }
  *DriverName = L"Virtio Filesystem Driver";
  return EFI_SUCCESS;
}

EFI_STATUS
EFIAPI
VirtioFsGetControllerName (
  IN  EFI_COMPONENT_NAME2_PROTOCOL *This,
  IN  EFI_HANDLE                   ControllerHandle,
  IN  EFI_HANDLE                   ChildHandle OPTIONAL,
  IN  CHAR8                        *Language,
  OUT CHAR16                       **ControllerName
  )
{
  return EFI_UNSUPPORTED;
}

//
// Entry point of this driver.
//
EFI_STATUS
EFIAPI
VirtioFsEntryPoint (
  IN EFI_HANDLE       ImageHandle,
  IN EFI_SYSTEM_TABLE *SystemTable
  )
{
  EFI_STATUS Status;

  mDriverBinding.Supported           = VirtioFsBindingSupported;
  mDriverBinding.Start               = VirtioFsBindingStart;
  mDriverBinding.Stop                = VirtioFsBindingStop;
  mDriverBinding.Version             = 0x10;
  mDriverBinding.ImageHandle         = ImageHandle;
  mDriverBinding.DriverBindingHandle = ImageHandle;

  mComponentName2.GetDriverName      = VirtioFsGetDriverName;
  mComponentName2.GetControllerName  = VirtioFsGetControllerName;
  mComponentName2.SupportedLanguages = "en";

  Status = gBS->InstallMultipleProtocolInterfaces (&ImageHandle,
                  &gEfiDriverBindingProtocolGuid, &mDriverBinding,
                  &gEfiComponentName2ProtocolGuid, &mComponentName2, NULL);
  return Status;
}