]> git.proxmox.com Git - mirror_edk2.git/blame - OvmfPkg/VirtioNetDxe/SnpReceive.c
ShellPkg/for: Fix potential null pointer deference
[mirror_edk2.git] / OvmfPkg / VirtioNetDxe / SnpReceive.c
CommitLineData
e12fadc3
LE
1/** @file\r
2\r
3 Implementation of the SNP.Receive() function and its private helpers if any.\r
4\r
5 Copyright (C) 2013, Red Hat, Inc.\r
8258c4e6 6 Copyright (c) 2006 - 2013, Intel Corporation. All rights reserved.<BR>\r
e12fadc3
LE
7\r
8 This program and the accompanying materials are licensed and made available\r
9 under the terms and conditions of the BSD License which accompanies this\r
10 distribution. The full text of the license may be found at\r
11 http://opensource.org/licenses/bsd-license.php\r
12\r
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT\r
14 WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
15\r
16**/\r
17\r
18#include <Library/BaseLib.h>\r
19#include <Library/BaseMemoryLib.h>\r
20#include <Library/UefiBootServicesTableLib.h>\r
21\r
22#include "VirtioNet.h"\r
23\r
24/**\r
25 Receives a packet from a network interface.\r
26\r
27 @param This The protocol instance pointer.\r
28 @param HeaderSize The size, in bytes, of the media header received on the\r
29 network interface. If this parameter is NULL, then the\r
30 media header size will not be returned.\r
31 @param BufferSize On entry, the size, in bytes, of Buffer. On exit, the\r
32 size, in bytes, of the packet that was received on the\r
33 network interface.\r
34 @param Buffer A pointer to the data buffer to receive both the media\r
35 header and the data.\r
36 @param SrcAddr The source HW MAC address. If this parameter is NULL, the\r
37 HW MAC source address will not be extracted from the media\r
38 header.\r
39 @param DestAddr The destination HW MAC address. If this parameter is NULL,\r
40 the HW MAC destination address will not be extracted from\r
41 the media header.\r
42 @param Protocol The media header type. If this parameter is NULL, then the\r
43 protocol will not be extracted from the media header. See\r
44 RFC 1700 section "Ether Types" for examples.\r
45\r
46 @retval EFI_SUCCESS The received data was stored in Buffer, and\r
47 BufferSize has been updated to the number of\r
48 bytes received.\r
49 @retval EFI_NOT_STARTED The network interface has not been started.\r
50 @retval EFI_NOT_READY The network interface is too busy to accept\r
51 this transmit request.\r
52 @retval EFI_BUFFER_TOO_SMALL The BufferSize parameter is too small.\r
53 @retval EFI_INVALID_PARAMETER One or more of the parameters has an\r
54 unsupported value.\r
55 @retval EFI_DEVICE_ERROR The command could not be sent to the network\r
56 interface.\r
57 @retval EFI_UNSUPPORTED This function is not supported by the network\r
58 interface.\r
59\r
60**/\r
61\r
62EFI_STATUS\r
63EFIAPI\r
64VirtioNetReceive (\r
65 IN EFI_SIMPLE_NETWORK_PROTOCOL *This,\r
66 OUT UINTN *HeaderSize OPTIONAL,\r
67 IN OUT UINTN *BufferSize,\r
68 OUT VOID *Buffer,\r
69 OUT EFI_MAC_ADDRESS *SrcAddr OPTIONAL,\r
70 OUT EFI_MAC_ADDRESS *DestAddr OPTIONAL,\r
71 OUT UINT16 *Protocol OPTIONAL\r
72 )\r
73{\r
74 VNET_DEV *Dev;\r
75 EFI_TPL OldTpl;\r
76 EFI_STATUS Status;\r
77 UINT16 RxCurUsed;\r
78 UINT16 UsedElemIdx;\r
79 UINT32 DescIdx;\r
80 UINT32 RxLen;\r
81 UINTN OrigBufferSize;\r
82 UINT8 *RxPtr;\r
83 UINT16 AvailIdx;\r
84 EFI_STATUS NotifyStatus;\r
46b11f00 85 UINTN RxBufOffset;\r
e12fadc3
LE
86\r
87 if (This == NULL || BufferSize == NULL || Buffer == NULL) {\r
88 return EFI_INVALID_PARAMETER;\r
89 }\r
90\r
91 Dev = VIRTIO_NET_FROM_SNP (This);\r
92 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
93 switch (Dev->Snm.State) {\r
94 case EfiSimpleNetworkStopped:\r
95 Status = EFI_NOT_STARTED;\r
96 goto Exit;\r
97 case EfiSimpleNetworkStarted:\r
98 Status = EFI_DEVICE_ERROR;\r
99 goto Exit;\r
100 default:\r
101 break;\r
102 }\r
103\r
104 //\r
105 // virtio-0.9.5, 2.4.2 Receiving Used Buffers From the Device\r
106 //\r
107 MemoryFence ();\r
108 RxCurUsed = *Dev->RxRing.Used.Idx;\r
dc9447bd 109 MemoryFence ();\r
e12fadc3
LE
110\r
111 if (Dev->RxLastUsed == RxCurUsed) {\r
112 Status = EFI_NOT_READY;\r
113 goto Exit;\r
114 }\r
115\r
116 UsedElemIdx = Dev->RxLastUsed % Dev->RxRing.QueueSize;\r
117 DescIdx = Dev->RxRing.Used.UsedElem[UsedElemIdx].Id;\r
118 RxLen = Dev->RxRing.Used.UsedElem[UsedElemIdx].Len;\r
119\r
120 //\r
121 // the virtio-net request header must be complete; we skip it\r
122 //\r
123 ASSERT (RxLen >= Dev->RxRing.Desc[DescIdx].Len);\r
124 RxLen -= Dev->RxRing.Desc[DescIdx].Len;\r
125 //\r
126 // the host must not have filled in more data than requested\r
127 //\r
128 ASSERT (RxLen <= Dev->RxRing.Desc[DescIdx + 1].Len);\r
129\r
130 OrigBufferSize = *BufferSize;\r
131 *BufferSize = RxLen;\r
132\r
133 if (OrigBufferSize < RxLen) {\r
134 Status = EFI_BUFFER_TOO_SMALL;\r
135 goto Exit; // keep the packet\r
136 }\r
137\r
138 if (RxLen < Dev->Snm.MediaHeaderSize) {\r
139 Status = EFI_DEVICE_ERROR;\r
140 goto RecycleDesc; // drop useless short packet\r
141 }\r
142\r
143 if (HeaderSize != NULL) {\r
144 *HeaderSize = Dev->Snm.MediaHeaderSize;\r
145 }\r
146\r
46b11f00
BS
147 RxBufOffset = (UINTN)(Dev->RxRing.Desc[DescIdx + 1].Addr -\r
148 Dev->RxBufDeviceBase);\r
149 RxPtr = Dev->RxBuf + RxBufOffset;\r
e12fadc3
LE
150 CopyMem (Buffer, RxPtr, RxLen);\r
151\r
152 if (DestAddr != NULL) {\r
56f65ed8 153 CopyMem (DestAddr, RxPtr, SIZE_OF_VNET (Mac));\r
e12fadc3 154 }\r
56f65ed8 155 RxPtr += SIZE_OF_VNET (Mac);\r
e12fadc3
LE
156\r
157 if (SrcAddr != NULL) {\r
56f65ed8 158 CopyMem (SrcAddr, RxPtr, SIZE_OF_VNET (Mac));\r
e12fadc3 159 }\r
56f65ed8 160 RxPtr += SIZE_OF_VNET (Mac);\r
e12fadc3
LE
161\r
162 if (Protocol != NULL) {\r
9f3acbb5 163 *Protocol = (UINT16) ((RxPtr[0] << 8) | RxPtr[1]);\r
e12fadc3
LE
164 }\r
165 RxPtr += sizeof (UINT16);\r
166\r
167 Status = EFI_SUCCESS;\r
168\r
169RecycleDesc:\r
170 ++Dev->RxLastUsed;\r
171\r
172 //\r
173 // virtio-0.9.5, 2.4.1 Supplying Buffers to The Device\r
174 //\r
175 AvailIdx = *Dev->RxRing.Avail.Idx;\r
9f3acbb5
LE
176 Dev->RxRing.Avail.Ring[AvailIdx++ % Dev->RxRing.QueueSize] =\r
177 (UINT16) DescIdx;\r
e12fadc3
LE
178\r
179 MemoryFence ();\r
180 *Dev->RxRing.Avail.Idx = AvailIdx;\r
181\r
182 MemoryFence ();\r
56f65ed8 183 NotifyStatus = Dev->VirtIo->SetQueueNotify (Dev->VirtIo, VIRTIO_NET_Q_RX);\r
e12fadc3
LE
184 if (!EFI_ERROR (Status)) { // earlier error takes precedence\r
185 Status = NotifyStatus;\r
186 }\r
187\r
188Exit:\r
189 gBS->RestoreTPL (OldTpl);\r
190 return Status;\r
191}\r