1 | /** @file
|
---|
2 | EFI_FILE_PROTOCOL.Write() member function for the Virtio Filesystem driver.
|
---|
3 |
|
---|
4 | Copyright (C) 2020, Red Hat, Inc.
|
---|
5 |
|
---|
6 | SPDX-License-Identifier: BSD-2-Clause-Patent
|
---|
7 | **/
|
---|
8 |
|
---|
9 | #include "VirtioFsDxe.h"
|
---|
10 |
|
---|
11 | EFI_STATUS
|
---|
12 | EFIAPI
|
---|
13 | VirtioFsSimpleFileWrite (
|
---|
14 | IN EFI_FILE_PROTOCOL *This,
|
---|
15 | IN OUT UINTN *BufferSize,
|
---|
16 | IN VOID *Buffer
|
---|
17 | )
|
---|
18 | {
|
---|
19 | VIRTIO_FS_FILE *VirtioFsFile;
|
---|
20 | VIRTIO_FS *VirtioFs;
|
---|
21 | EFI_STATUS Status;
|
---|
22 | UINTN Transferred;
|
---|
23 | UINTN Left;
|
---|
24 |
|
---|
25 | VirtioFsFile = VIRTIO_FS_FILE_FROM_SIMPLE_FILE (This);
|
---|
26 | VirtioFs = VirtioFsFile->OwnerFs;
|
---|
27 |
|
---|
28 | if (VirtioFsFile->IsDirectory) {
|
---|
29 | return EFI_UNSUPPORTED;
|
---|
30 | }
|
---|
31 | if (!VirtioFsFile->IsOpenForWriting) {
|
---|
32 | return EFI_ACCESS_DENIED;
|
---|
33 | }
|
---|
34 |
|
---|
35 | Status = EFI_SUCCESS;
|
---|
36 | Transferred = 0;
|
---|
37 | Left = *BufferSize;
|
---|
38 | while (Left > 0) {
|
---|
39 | UINT32 WriteSize;
|
---|
40 |
|
---|
41 | //
|
---|
42 | // Honor the write buffer size limit.
|
---|
43 | //
|
---|
44 | WriteSize = (UINT32)MIN ((UINTN)VirtioFs->MaxWrite, Left);
|
---|
45 | Status = VirtioFsFuseWrite (
|
---|
46 | VirtioFs,
|
---|
47 | VirtioFsFile->NodeId,
|
---|
48 | VirtioFsFile->FuseHandle,
|
---|
49 | VirtioFsFile->FilePosition + Transferred,
|
---|
50 | &WriteSize,
|
---|
51 | (UINT8 *)Buffer + Transferred
|
---|
52 | );
|
---|
53 | if (!EFI_ERROR (Status) && WriteSize == 0) {
|
---|
54 | //
|
---|
55 | // Progress should have been made.
|
---|
56 | //
|
---|
57 | Status = EFI_DEVICE_ERROR;
|
---|
58 | }
|
---|
59 | if (EFI_ERROR (Status)) {
|
---|
60 | break;
|
---|
61 | }
|
---|
62 | Transferred += WriteSize;
|
---|
63 | Left -= WriteSize;
|
---|
64 | }
|
---|
65 |
|
---|
66 | *BufferSize = Transferred;
|
---|
67 | VirtioFsFile->FilePosition += Transferred;
|
---|
68 | //
|
---|
69 | // According to the UEFI spec,
|
---|
70 | //
|
---|
71 | // - 'Partial writes only occur when there has been a data error during the
|
---|
72 | // write attempt (such as "file space full")', and
|
---|
73 | //
|
---|
74 | // - (as an example) EFI_VOLUME_FULL is returned when 'The volume is full'.
|
---|
75 | //
|
---|
76 | // These together imply that after a partial write, we have to return an
|
---|
77 | // error. In other words, (Transferred > 0) is inconsequential for the return
|
---|
78 | // value.
|
---|
79 | //
|
---|
80 | return Status;
|
---|
81 | }
|
---|