2 Internal macro definitions, type definitions, and function declarations for
3 the Virtio Filesystem device driver.
5 Copyright (C) 2020, Red Hat, Inc.
7 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #ifndef VIRTIO_FS_DXE_H_
11 #define VIRTIO_FS_DXE_H_
13 #include <Base.h> // SIGNATURE_64()
14 #include <IndustryStandard/VirtioFs.h> // VIRTIO_FS_TAG_BYTES
15 #include <Library/DebugLib.h> // CR()
16 #include <Protocol/SimpleFileSystem.h> // EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
17 #include <Protocol/VirtioDevice.h> // VIRTIO_DEVICE_PROTOCOL
18 #include <Uefi/UefiBaseType.h> // EFI_EVENT
20 #define VIRTIO_FS_SIG SIGNATURE_64 ('V', 'I', 'R', 'T', 'I', 'O', 'F', 'S')
22 #define VIRTIO_FS_FILE_SIG \
23 SIGNATURE_64 ('V', 'I', 'O', 'F', 'S', 'F', 'I', 'L')
26 // Filesystem label encoded in UCS-2, transformed from the UTF-8 representation
27 // in "VIRTIO_FS_CONFIG.Tag", and NUL-terminated. Only the printable ASCII code
28 // points (U+0020 through U+007E) are supported.
30 typedef CHAR16 VIRTIO_FS_LABEL
[VIRTIO_FS_TAG_BYTES
+ 1];
33 // Main context structure, expressing an EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
34 // interface on top of the Virtio Filesystem device.
38 // Parts of this structure are initialized / torn down in various functions
39 // at various call depths. The table to the right should make it easier to
42 // field init function init depth
43 // ----------- ------------------ ----------
44 UINT64 Signature
; // DriverBindingStart 0
45 VIRTIO_DEVICE_PROTOCOL
*Virtio
; // DriverBindingStart 0
46 VIRTIO_FS_LABEL Label
; // VirtioFsInit 1
47 UINT16 QueueSize
; // VirtioFsInit 1
48 VRING Ring
; // VirtioRingInit 2
49 VOID
*RingMap
; // VirtioRingMap 2
50 UINT64 RequestId
; // FuseInitSession 1
51 EFI_EVENT ExitBoot
; // DriverBindingStart 0
52 LIST_ENTRY OpenFiles
; // DriverBindingStart 0
53 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL SimpleFs
; // DriverBindingStart 0
56 #define VIRTIO_FS_FROM_SIMPLE_FS(SimpleFsReference) \
57 CR (SimpleFsReference, VIRTIO_FS, SimpleFs, VIRTIO_FS_SIG);
60 // Structure for describing a contiguous buffer, potentially mapped for Virtio
65 // The following fields originate from the owner of the buffer.
70 // All of the fields below, until the end of the structure, are
71 // zero-initialized when the structure is initially validated.
73 // Mapped, MappedAddress and Mapping are updated when the buffer is mapped
74 // for VirtioOperationBusMasterRead or VirtioOperationBusMasterWrite. They
75 // are again updated when the buffer is unmapped.
78 EFI_PHYSICAL_ADDRESS MappedAddress
;
81 // Transferred is updated after VirtioFlush() returns successfully:
82 // - for VirtioOperationBusMasterRead, Transferred is set to Size;
83 // - for VirtioOperationBusMasterWrite, Transferred is calculated from the
84 // UsedLen output parameter of VirtioFlush().
87 } VIRTIO_FS_IO_VECTOR
;
90 // Structure for describing a list of IO Vectors.
94 // The following fields originate from the owner of the buffers.
96 VIRTIO_FS_IO_VECTOR
*IoVec
;
99 // TotalSize is calculated when the scatter-gather list is initially
103 } VIRTIO_FS_SCATTER_GATHER_LIST
;
106 // Private context structure that exposes EFI_FILE_PROTOCOL on top of an open
107 // FUSE file reference.
111 EFI_FILE_PROTOCOL SimpleFile
;
114 LIST_ENTRY OpenFilesEntry
;
116 // In the FUSE wire protocol, every request except FUSE_INIT refers to a
117 // file, namely by the "VIRTIO_FS_FUSE_REQUEST.NodeId" field; that is, by the
118 // inode number of the file. However, some of the FUSE requests that we need
119 // for some of the EFI_FILE_PROTOCOL member functions require an open file
120 // handle *in addition* to the inode number. For simplicity, whenever a
121 // VIRTIO_FS_FILE object is created, primarily defined by its NodeId field,
122 // we also *open* the referenced file at once, and save the returned file
123 // handle in the FuseHandle field. This way, when an EFI_FILE_PROTOCOL member
124 // function must send a FUSE request that needs the file handle *in addition*
125 // to the inode number, FuseHandle will be at our disposal at once.
131 #define VIRTIO_FS_FILE_FROM_SIMPLE_FILE(SimpleFileReference) \
132 CR (SimpleFileReference, VIRTIO_FS_FILE, SimpleFile, VIRTIO_FS_FILE_SIG);
134 #define VIRTIO_FS_FILE_FROM_OPEN_FILES_ENTRY(OpenFilesEntryReference) \
135 CR (OpenFilesEntryReference, VIRTIO_FS_FILE, OpenFilesEntry, \
139 // Initialization and helper routines for the Virtio Filesystem device.
144 IN OUT VIRTIO_FS
*VirtioFs
149 IN OUT VIRTIO_FS
*VirtioFs
155 IN EFI_EVENT ExitBootEvent
,
156 IN VOID
*VirtioFsAsVoid
160 VirtioFsSgListsValidate (
161 IN VIRTIO_FS
*VirtioFs
,
162 IN OUT VIRTIO_FS_SCATTER_GATHER_LIST
*RequestSgList
,
163 IN OUT VIRTIO_FS_SCATTER_GATHER_LIST
*ResponseSgList OPTIONAL
167 VirtioFsSgListsSubmit (
168 IN OUT VIRTIO_FS
*VirtioFs
,
169 IN OUT VIRTIO_FS_SCATTER_GATHER_LIST
*RequestSgList
,
170 IN OUT VIRTIO_FS_SCATTER_GATHER_LIST
*ResponseSgList OPTIONAL
174 VirtioFsFuseNewRequest (
175 IN OUT VIRTIO_FS
*VirtioFs
,
176 OUT VIRTIO_FS_FUSE_REQUEST
*Request
,
177 IN UINT32 RequestSize
,
178 IN VIRTIO_FS_FUSE_OPCODE Opcode
,
183 VirtioFsFuseCheckResponse (
184 IN VIRTIO_FS_SCATTER_GATHER_LIST
*ResponseSgList
,
186 OUT UINTN
*TailBufferFill
190 VirtioFsErrnoToEfiStatus (
195 // Wrapper functions for FUSE commands (primitives).
200 IN OUT VIRTIO_FS
*VirtioFs
,
205 VirtioFsFuseReleaseFileOrDir (
206 IN OUT VIRTIO_FS
*VirtioFs
,
208 IN UINT64 FuseHandle
,
213 VirtioFsFuseFsyncFileOrDir (
214 IN OUT VIRTIO_FS
*VirtioFs
,
216 IN UINT64 FuseHandle
,
221 VirtioFsFuseInitSession (
222 IN OUT VIRTIO_FS
*VirtioFs
226 VirtioFsFuseOpenDir (
227 IN OUT VIRTIO_FS
*VirtioFs
,
229 OUT UINT64
*FuseHandle
233 // EFI_SIMPLE_FILE_SYSTEM_PROTOCOL member functions for the Virtio Filesystem
240 IN EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*This
,
241 OUT EFI_FILE_PROTOCOL
**Root
245 // EFI_FILE_PROTOCOL member functions for the Virtio Filesystem driver.
250 VirtioFsSimpleFileClose (
251 IN EFI_FILE_PROTOCOL
*This
256 VirtioFsSimpleFileDelete (
257 IN EFI_FILE_PROTOCOL
*This
262 VirtioFsSimpleFileFlush (
263 IN EFI_FILE_PROTOCOL
*This
268 VirtioFsSimpleFileGetInfo (
269 IN EFI_FILE_PROTOCOL
*This
,
270 IN EFI_GUID
*InformationType
,
271 IN OUT UINTN
*BufferSize
,
277 VirtioFsSimpleFileGetPosition (
278 IN EFI_FILE_PROTOCOL
*This
,
284 VirtioFsSimpleFileOpen (
285 IN EFI_FILE_PROTOCOL
*This
,
286 OUT EFI_FILE_PROTOCOL
**NewHandle
,
294 VirtioFsSimpleFileRead (
295 IN EFI_FILE_PROTOCOL
*This
,
296 IN OUT UINTN
*BufferSize
,
302 VirtioFsSimpleFileSetInfo (
303 IN EFI_FILE_PROTOCOL
*This
,
304 IN EFI_GUID
*InformationType
,
311 VirtioFsSimpleFileSetPosition (
312 IN EFI_FILE_PROTOCOL
*This
,
318 VirtioFsSimpleFileWrite (
319 IN EFI_FILE_PROTOCOL
*This
,
320 IN OUT UINTN
*BufferSize
,
324 #endif // VIRTIO_FS_DXE_H_