]> git.proxmox.com Git - mirror_edk2.git/blame - OvmfPkg/VirtioFsDxe/VirtioFsDxe.h
OvmfPkg/VirtioFsDxe: implement EFI_FILE_PROTOCOL.Read() for regular files
[mirror_edk2.git] / OvmfPkg / VirtioFsDxe / VirtioFsDxe.h
CommitLineData
b55d6622
LE
1/** @file\r
2 Internal macro definitions, type definitions, and function declarations for\r
3 the Virtio Filesystem device driver.\r
4\r
5 Copyright (C) 2020, Red Hat, Inc.\r
6\r
7 SPDX-License-Identifier: BSD-2-Clause-Patent\r
8**/\r
9\r
10#ifndef VIRTIO_FS_DXE_H_\r
11#define VIRTIO_FS_DXE_H_\r
12\r
13#include <Base.h> // SIGNATURE_64()\r
cd473d41 14#include <Guid/FileInfo.h> // EFI_FILE_INFO\r
eaa7115d 15#include <IndustryStandard/VirtioFs.h> // VIRTIO_FS_TAG_BYTES\r
b55d6622
LE
16#include <Library/DebugLib.h> // CR()\r
17#include <Protocol/SimpleFileSystem.h> // EFI_SIMPLE_FILE_SYSTEM_PROTOCOL\r
18#include <Protocol/VirtioDevice.h> // VIRTIO_DEVICE_PROTOCOL\r
eaa7115d 19#include <Uefi/UefiBaseType.h> // EFI_EVENT\r
b55d6622
LE
20\r
21#define VIRTIO_FS_SIG SIGNATURE_64 ('V', 'I', 'R', 'T', 'I', 'O', 'F', 'S')\r
22\r
334c13e1
LE
23#define VIRTIO_FS_FILE_SIG \\r
24 SIGNATURE_64 ('V', 'I', 'O', 'F', 'S', 'F', 'I', 'L')\r
25\r
9307d7c7
LE
26//\r
27// The following limit applies to two kinds of pathnames.\r
28//\r
29// - The length of a POSIX-style, canonical pathname *at rest* never exceeds\r
30// VIRTIO_FS_MAX_PATHNAME_LENGTH. (Length is defined as the number of CHAR8\r
31// elements in the canonical pathname, excluding the terminating '\0'.) This\r
32// is an invariant that is ensured for canonical pathnames created, and that\r
33// is assumed about canonical pathname inputs (which all originate\r
34// internally).\r
35//\r
36// - If the length of a UEFI-style pathname *argument*, originating directly or\r
37// indirectly from the EFI_FILE_PROTOCOL caller, exceeds\r
38// VIRTIO_FS_MAX_PATHNAME_LENGTH, then the argument is rejected. (Length is\r
39// defined as the number of CHAR16 elements in the UEFI-style pathname,\r
40// excluding the terminating L'\0'.) This is a restriction that's checked on\r
41// external UEFI-style pathname inputs.\r
42//\r
43// The limit is not expected to be a practical limitation; it's only supposed\r
44// to prevent attempts at overflowing size calculations. For both kinds of\r
45// pathnames, separate limits could be used; a common limit is used purely for\r
46// simplicity.\r
47//\r
48#define VIRTIO_FS_MAX_PATHNAME_LENGTH ((UINTN)65535)\r
49\r
eaa7115d
LE
50//\r
51// Filesystem label encoded in UCS-2, transformed from the UTF-8 representation\r
52// in "VIRTIO_FS_CONFIG.Tag", and NUL-terminated. Only the printable ASCII code\r
53// points (U+0020 through U+007E) are supported.\r
54//\r
55typedef CHAR16 VIRTIO_FS_LABEL[VIRTIO_FS_TAG_BYTES + 1];\r
56\r
b55d6622
LE
57//\r
58// Main context structure, expressing an EFI_SIMPLE_FILE_SYSTEM_PROTOCOL\r
59// interface on top of the Virtio Filesystem device.\r
60//\r
61typedef struct {\r
62 //\r
63 // Parts of this structure are initialized / torn down in various functions\r
64 // at various call depths. The table to the right should make it easier to\r
65 // track them.\r
66 //\r
67 // field init function init depth\r
68 // ----------- ------------------ ----------\r
69 UINT64 Signature; // DriverBindingStart 0\r
70 VIRTIO_DEVICE_PROTOCOL *Virtio; // DriverBindingStart 0\r
eaa7115d
LE
71 VIRTIO_FS_LABEL Label; // VirtioFsInit 1\r
72 UINT16 QueueSize; // VirtioFsInit 1\r
73 VRING Ring; // VirtioRingInit 2\r
74 VOID *RingMap; // VirtioRingMap 2\r
fa97e372 75 UINT64 RequestId; // FuseInitSession 1\r
eaa7115d 76 EFI_EVENT ExitBoot; // DriverBindingStart 0\r
334c13e1 77 LIST_ENTRY OpenFiles; // DriverBindingStart 0\r
b55d6622
LE
78 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL SimpleFs; // DriverBindingStart 0\r
79} VIRTIO_FS;\r
80\r
81#define VIRTIO_FS_FROM_SIMPLE_FS(SimpleFsReference) \\r
82 CR (SimpleFsReference, VIRTIO_FS, SimpleFs, VIRTIO_FS_SIG);\r
83\r
6578cacb
LE
84//\r
85// Structure for describing a contiguous buffer, potentially mapped for Virtio\r
86// transfer.\r
87//\r
88typedef struct {\r
89 //\r
90 // The following fields originate from the owner of the buffer.\r
91 //\r
92 VOID *Buffer;\r
93 UINTN Size;\r
94 //\r
95 // All of the fields below, until the end of the structure, are\r
96 // zero-initialized when the structure is initially validated.\r
97 //\r
98 // Mapped, MappedAddress and Mapping are updated when the buffer is mapped\r
99 // for VirtioOperationBusMasterRead or VirtioOperationBusMasterWrite. They\r
100 // are again updated when the buffer is unmapped.\r
101 //\r
102 BOOLEAN Mapped;\r
103 EFI_PHYSICAL_ADDRESS MappedAddress;\r
104 VOID *Mapping;\r
105 //\r
106 // Transferred is updated after VirtioFlush() returns successfully:\r
107 // - for VirtioOperationBusMasterRead, Transferred is set to Size;\r
108 // - for VirtioOperationBusMasterWrite, Transferred is calculated from the\r
109 // UsedLen output parameter of VirtioFlush().\r
110 //\r
111 UINTN Transferred;\r
112} VIRTIO_FS_IO_VECTOR;\r
113\r
114//\r
115// Structure for describing a list of IO Vectors.\r
116//\r
117typedef struct {\r
118 //\r
119 // The following fields originate from the owner of the buffers.\r
120 //\r
121 VIRTIO_FS_IO_VECTOR *IoVec;\r
122 UINTN NumVec;\r
123 //\r
124 // TotalSize is calculated when the scatter-gather list is initially\r
125 // validated.\r
126 //\r
127 UINT32 TotalSize;\r
128} VIRTIO_FS_SCATTER_GATHER_LIST;\r
129\r
334c13e1
LE
130//\r
131// Private context structure that exposes EFI_FILE_PROTOCOL on top of an open\r
132// FUSE file reference.\r
133//\r
134typedef struct {\r
135 UINT64 Signature;\r
136 EFI_FILE_PROTOCOL SimpleFile;\r
137 BOOLEAN IsDirectory;\r
28092a39 138 BOOLEAN IsOpenForWriting;\r
334c13e1
LE
139 VIRTIO_FS *OwnerFs;\r
140 LIST_ENTRY OpenFilesEntry;\r
7e8c83f7 141 CHAR8 *CanonicalPathname;\r
c4edb49b 142 UINT64 FilePosition;\r
334c13e1
LE
143 //\r
144 // In the FUSE wire protocol, every request except FUSE_INIT refers to a\r
145 // file, namely by the "VIRTIO_FS_FUSE_REQUEST.NodeId" field; that is, by the\r
146 // inode number of the file. However, some of the FUSE requests that we need\r
147 // for some of the EFI_FILE_PROTOCOL member functions require an open file\r
148 // handle *in addition* to the inode number. For simplicity, whenever a\r
149 // VIRTIO_FS_FILE object is created, primarily defined by its NodeId field,\r
150 // we also *open* the referenced file at once, and save the returned file\r
151 // handle in the FuseHandle field. This way, when an EFI_FILE_PROTOCOL member\r
152 // function must send a FUSE request that needs the file handle *in addition*\r
153 // to the inode number, FuseHandle will be at our disposal at once.\r
154 //\r
155 UINT64 NodeId;\r
156 UINT64 FuseHandle;\r
157} VIRTIO_FS_FILE;\r
158\r
159#define VIRTIO_FS_FILE_FROM_SIMPLE_FILE(SimpleFileReference) \\r
160 CR (SimpleFileReference, VIRTIO_FS_FILE, SimpleFile, VIRTIO_FS_FILE_SIG);\r
161\r
162#define VIRTIO_FS_FILE_FROM_OPEN_FILES_ENTRY(OpenFilesEntryReference) \\r
163 CR (OpenFilesEntryReference, VIRTIO_FS_FILE, OpenFilesEntry, \\r
164 VIRTIO_FS_FILE_SIG);\r
165\r
eaa7115d
LE
166//\r
167// Initialization and helper routines for the Virtio Filesystem device.\r
168//\r
169\r
170EFI_STATUS\r
171VirtioFsInit (\r
172 IN OUT VIRTIO_FS *VirtioFs\r
173 );\r
174\r
175VOID\r
176VirtioFsUninit (\r
177 IN OUT VIRTIO_FS *VirtioFs\r
178 );\r
179\r
180VOID\r
181EFIAPI\r
182VirtioFsExitBoot (\r
183 IN EFI_EVENT ExitBootEvent,\r
184 IN VOID *VirtioFsAsVoid\r
185 );\r
186\r
6578cacb
LE
187EFI_STATUS\r
188VirtioFsSgListsValidate (\r
189 IN VIRTIO_FS *VirtioFs,\r
190 IN OUT VIRTIO_FS_SCATTER_GATHER_LIST *RequestSgList,\r
191 IN OUT VIRTIO_FS_SCATTER_GATHER_LIST *ResponseSgList OPTIONAL\r
192 );\r
193\r
194EFI_STATUS\r
195VirtioFsSgListsSubmit (\r
196 IN OUT VIRTIO_FS *VirtioFs,\r
197 IN OUT VIRTIO_FS_SCATTER_GATHER_LIST *RequestSgList,\r
198 IN OUT VIRTIO_FS_SCATTER_GATHER_LIST *ResponseSgList OPTIONAL\r
199 );\r
200\r
6a2dc768
LE
201EFI_STATUS\r
202VirtioFsFuseNewRequest (\r
203 IN OUT VIRTIO_FS *VirtioFs,\r
204 OUT VIRTIO_FS_FUSE_REQUEST *Request,\r
205 IN UINT32 RequestSize,\r
fa97e372 206 IN VIRTIO_FS_FUSE_OPCODE Opcode,\r
6a2dc768
LE
207 IN UINT64 NodeId\r
208 );\r
209\r
210EFI_STATUS\r
211VirtioFsFuseCheckResponse (\r
212 IN VIRTIO_FS_SCATTER_GATHER_LIST *ResponseSgList,\r
213 IN UINT64 RequestId,\r
214 OUT UINTN *TailBufferFill\r
215 );\r
216\r
e8a74c9a
LE
217EFI_STATUS\r
218VirtioFsErrnoToEfiStatus (\r
219 IN INT32 Errno\r
220 );\r
221\r
9307d7c7
LE
222EFI_STATUS\r
223VirtioFsAppendPath (\r
224 IN CHAR8 *LhsPath8,\r
225 IN CHAR16 *RhsPath16,\r
226 OUT CHAR8 **ResultPath8,\r
227 OUT BOOLEAN *RootEscape\r
228 );\r
229\r
ca61b845
LE
230EFI_STATUS\r
231VirtioFsLookupMostSpecificParentDir (\r
232 IN OUT VIRTIO_FS *VirtioFs,\r
233 IN OUT CHAR8 *Path,\r
234 OUT UINT64 *DirNodeId,\r
235 OUT CHAR8 **LastComponent\r
236 );\r
237\r
44bc7879
LE
238EFI_STATUS\r
239VirtioFsGetBasename (\r
240 IN CHAR8 *Path,\r
241 OUT CHAR16 *Basename OPTIONAL,\r
242 IN OUT UINTN *BasenameSize\r
243 );\r
244\r
cd473d41
LE
245EFI_STATUS\r
246VirtioFsFuseAttrToEfiFileInfo (\r
247 IN VIRTIO_FS_FUSE_ATTRIBUTES_RESPONSE *FuseAttr,\r
248 OUT EFI_FILE_INFO *FileInfo\r
249 );\r
250\r
fa97e372
LE
251//\r
252// Wrapper functions for FUSE commands (primitives).\r
253//\r
254\r
b6ce961a
LE
255EFI_STATUS\r
256VirtioFsFuseLookup (\r
257 IN OUT VIRTIO_FS *VirtioFs,\r
258 IN UINT64 DirNodeId,\r
259 IN CHAR8 *Name,\r
260 OUT UINT64 *NodeId,\r
261 OUT VIRTIO_FS_FUSE_ATTRIBUTES_RESPONSE *FuseAttr\r
262 );\r
263\r
92a4d30e
LE
264EFI_STATUS\r
265VirtioFsFuseForget (\r
266 IN OUT VIRTIO_FS *VirtioFs,\r
267 IN UINT64 NodeId\r
268 );\r
269\r
e3bc9577
LE
270EFI_STATUS\r
271VirtioFsFuseGetAttr (\r
272 IN OUT VIRTIO_FS *VirtioFs,\r
273 IN UINT64 NodeId,\r
274 OUT VIRTIO_FS_FUSE_ATTRIBUTES_RESPONSE *FuseAttr\r
275 );\r
276\r
f058cb69
LE
277EFI_STATUS\r
278VirtioFsFuseMkDir (\r
279 IN OUT VIRTIO_FS *VirtioFs,\r
280 IN UINT64 ParentNodeId,\r
281 IN CHAR8 *Name,\r
282 OUT UINT64 *NodeId\r
283 );\r
284\r
0771671c
LE
285EFI_STATUS\r
286VirtioFsFuseRemoveFileOrDir (\r
287 IN OUT VIRTIO_FS *VirtioFs,\r
288 IN UINT64 ParentNodeId,\r
289 IN CHAR8 *Name,\r
290 IN BOOLEAN IsDir\r
291 );\r
292\r
da82d2e3
LE
293EFI_STATUS\r
294VirtioFsFuseOpen (\r
295 IN OUT VIRTIO_FS *VirtioFs,\r
296 IN UINT64 NodeId,\r
297 IN BOOLEAN ReadWrite,\r
298 OUT UINT64 *FuseHandle\r
299 );\r
300\r
d98d7e30
LE
301EFI_STATUS\r
302VirtioFsFuseReadFileOrDir (\r
303 IN OUT VIRTIO_FS *VirtioFs,\r
304 IN UINT64 NodeId,\r
305 IN UINT64 FuseHandle,\r
306 IN BOOLEAN IsDir,\r
307 IN UINT64 Offset,\r
308 IN OUT UINT32 *Size,\r
309 OUT VOID *Data\r
310 );\r
311\r
ba118463
LE
312EFI_STATUS\r
313VirtioFsFuseStatFs (\r
314 IN OUT VIRTIO_FS *VirtioFs,\r
315 IN UINT64 NodeId,\r
316 OUT VIRTIO_FS_FUSE_STATFS_RESPONSE *FilesysAttr\r
317 );\r
318\r
72d4f133
LE
319EFI_STATUS\r
320VirtioFsFuseReleaseFileOrDir (\r
321 IN OUT VIRTIO_FS *VirtioFs,\r
322 IN UINT64 NodeId,\r
323 IN UINT64 FuseHandle,\r
324 IN BOOLEAN IsDir\r
325 );\r
326\r
2e151d26
LE
327EFI_STATUS\r
328VirtioFsFuseFsyncFileOrDir (\r
329 IN OUT VIRTIO_FS *VirtioFs,\r
330 IN UINT64 NodeId,\r
331 IN UINT64 FuseHandle,\r
332 IN BOOLEAN IsDir\r
333 );\r
334\r
d0474399
LE
335EFI_STATUS\r
336VirtioFsFuseFlush (\r
337 IN OUT VIRTIO_FS *VirtioFs,\r
338 IN UINT64 NodeId,\r
339 IN UINT64 FuseHandle\r
340 );\r
341\r
fa97e372
LE
342EFI_STATUS\r
343VirtioFsFuseInitSession (\r
344 IN OUT VIRTIO_FS *VirtioFs\r
345 );\r
346\r
b62a0c56
LE
347EFI_STATUS\r
348VirtioFsFuseOpenDir (\r
349 IN OUT VIRTIO_FS *VirtioFs,\r
350 IN UINT64 NodeId,\r
351 OUT UINT64 *FuseHandle\r
352 );\r
353\r
a70860f4
LE
354EFI_STATUS\r
355VirtioFsFuseOpenOrCreate (\r
356 IN OUT VIRTIO_FS *VirtioFs,\r
357 IN UINT64 ParentNodeId,\r
358 IN CHAR8 *Name,\r
359 OUT UINT64 *NodeId,\r
360 OUT UINT64 *FuseHandle\r
361 );\r
362\r
b55d6622
LE
363//\r
364// EFI_SIMPLE_FILE_SYSTEM_PROTOCOL member functions for the Virtio Filesystem\r
365// driver.\r
366//\r
367\r
368EFI_STATUS\r
369EFIAPI\r
370VirtioFsOpenVolume (\r
371 IN EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *This,\r
372 OUT EFI_FILE_PROTOCOL **Root\r
373 );\r
374\r
334c13e1
LE
375//\r
376// EFI_FILE_PROTOCOL member functions for the Virtio Filesystem driver.\r
377//\r
378\r
379EFI_STATUS\r
380EFIAPI\r
381VirtioFsSimpleFileClose (\r
382 IN EFI_FILE_PROTOCOL *This\r
383 );\r
384\r
385EFI_STATUS\r
386EFIAPI\r
387VirtioFsSimpleFileDelete (\r
388 IN EFI_FILE_PROTOCOL *This\r
389 );\r
390\r
391EFI_STATUS\r
392EFIAPI\r
393VirtioFsSimpleFileFlush (\r
394 IN EFI_FILE_PROTOCOL *This\r
395 );\r
396\r
397EFI_STATUS\r
398EFIAPI\r
399VirtioFsSimpleFileGetInfo (\r
400 IN EFI_FILE_PROTOCOL *This,\r
401 IN EFI_GUID *InformationType,\r
402 IN OUT UINTN *BufferSize,\r
403 OUT VOID *Buffer\r
404 );\r
405\r
406EFI_STATUS\r
407EFIAPI\r
408VirtioFsSimpleFileGetPosition (\r
409 IN EFI_FILE_PROTOCOL *This,\r
410 OUT UINT64 *Position\r
411 );\r
412\r
413EFI_STATUS\r
414EFIAPI\r
415VirtioFsSimpleFileOpen (\r
416 IN EFI_FILE_PROTOCOL *This,\r
417 OUT EFI_FILE_PROTOCOL **NewHandle,\r
418 IN CHAR16 *FileName,\r
419 IN UINT64 OpenMode,\r
420 IN UINT64 Attributes\r
421 );\r
422\r
423EFI_STATUS\r
424EFIAPI\r
425VirtioFsSimpleFileRead (\r
426 IN EFI_FILE_PROTOCOL *This,\r
427 IN OUT UINTN *BufferSize,\r
428 OUT VOID *Buffer\r
429 );\r
430\r
431EFI_STATUS\r
432EFIAPI\r
433VirtioFsSimpleFileSetInfo (\r
434 IN EFI_FILE_PROTOCOL *This,\r
435 IN EFI_GUID *InformationType,\r
436 IN UINTN BufferSize,\r
437 IN VOID *Buffer\r
438 );\r
439\r
440EFI_STATUS\r
441EFIAPI\r
442VirtioFsSimpleFileSetPosition (\r
443 IN EFI_FILE_PROTOCOL *This,\r
444 IN UINT64 Position\r
445 );\r
446\r
447EFI_STATUS\r
448EFIAPI\r
449VirtioFsSimpleFileWrite (\r
450 IN EFI_FILE_PROTOCOL *This,\r
451 IN OUT UINTN *BufferSize,\r
452 IN VOID *Buffer\r
453 );\r
454\r
b55d6622 455#endif // VIRTIO_FS_DXE_H_\r