]> git.proxmox.com Git - mirror_edk2.git/blob - NetworkPkg/SnpDxe/Nvdata.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / NetworkPkg / SnpDxe / Nvdata.c
1 /** @file
2 Implementation of reading and writing operations on the NVRAM device
3 attached to a network interface.
4
5 Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
7
8 **/
9
10 #include "Snp.h"
11
12 /**
13 This routine calls Undi to read the desired number of eeprom bytes.
14
15 @param Snp pointer to the snp driver structure
16 @param Offset eeprom register value relative to the base address
17 @param BufferSize number of bytes to read
18 @param Buffer pointer where to read into
19
20 @retval EFI_SUCCESS The NVRAM access was performed.
21 @retval EFI_INVALID_PARAMETER Invalid UNDI command.
22 @retval EFI_UNSUPPORTED Command is not supported by UNDI.
23 @retval EFI_DEVICE_ERROR Fail to execute UNDI command.
24
25 **/
26 EFI_STATUS
27 PxeNvDataRead (
28 IN SNP_DRIVER *Snp,
29 IN UINTN Offset,
30 IN UINTN BufferSize,
31 IN OUT VOID *Buffer
32 )
33 {
34 PXE_DB_NVDATA *Db;
35
36 Db = Snp->Db;
37 Snp->Cdb.OpCode = PXE_OPCODE_NVDATA;
38
39 Snp->Cdb.OpFlags = PXE_OPFLAGS_NVDATA_READ;
40
41 Snp->Cdb.CPBsize = PXE_CPBSIZE_NOT_USED;
42 Snp->Cdb.CPBaddr = PXE_CPBADDR_NOT_USED;
43
44 Snp->Cdb.DBsize = (UINT16)sizeof (PXE_DB_NVDATA);
45 Snp->Cdb.DBaddr = (UINT64)(UINTN)Db;
46
47 Snp->Cdb.StatCode = PXE_STATCODE_INITIALIZE;
48 Snp->Cdb.StatFlags = PXE_STATFLAGS_INITIALIZE;
49 Snp->Cdb.IFnum = Snp->IfNum;
50 Snp->Cdb.Control = PXE_CONTROL_LAST_CDB_IN_LIST;
51
52 //
53 // Issue UNDI command and check result.
54 //
55 DEBUG ((DEBUG_NET, "\nsnp->undi.nvdata () "));
56
57 (*Snp->IssueUndi32Command)((UINT64)(UINTN)&Snp->Cdb);
58
59 switch (Snp->Cdb.StatCode) {
60 case PXE_STATCODE_SUCCESS:
61 break;
62
63 case PXE_STATCODE_UNSUPPORTED:
64 DEBUG (
65 (DEBUG_NET,
66 "\nsnp->undi.nvdata() %xh:%xh\n",
67 Snp->Cdb.StatFlags,
68 Snp->Cdb.StatCode)
69 );
70
71 return EFI_UNSUPPORTED;
72
73 default:
74 DEBUG (
75 (DEBUG_NET,
76 "\nsnp->undi.nvdata() %xh:%xh\n",
77 Snp->Cdb.StatFlags,
78 Snp->Cdb.StatCode)
79 );
80
81 return EFI_DEVICE_ERROR;
82 }
83
84 ASSERT (Offset < sizeof (Db->Data));
85
86 CopyMem (Buffer, &Db->Data.Byte[Offset], BufferSize);
87
88 return EFI_SUCCESS;
89 }
90
91 /**
92 Performs read and write operations on the NVRAM device attached to a network
93 interface.
94
95 This function performs read and write operations on the NVRAM device attached
96 to a network interface. If ReadWrite is TRUE, a read operation is performed.
97 If ReadWrite is FALSE, a write operation is performed. Offset specifies the
98 byte offset at which to start either operation. Offset must be a multiple of
99 NvRamAccessSize , and it must have a value between zero and NvRamSize.
100 BufferSize specifies the length of the read or write operation. BufferSize must
101 also be a multiple of NvRamAccessSize, and Offset + BufferSize must not exceed
102 NvRamSize.
103 If any of the above conditions is not met, then EFI_INVALID_PARAMETER will be
104 returned.
105 If all the conditions are met and the operation is "read," the NVRAM device
106 attached to the network interface will be read into Buffer and EFI_SUCCESS
107 will be returned. If this is a write operation, the contents of Buffer will be
108 used to update the contents of the NVRAM device attached to the network
109 interface and EFI_SUCCESS will be returned.
110
111 It does the basic checking on the input parameters and retrieves snp structure
112 and then calls the read_nvdata() call which does the actual reading
113
114 @param This A pointer to the EFI_SIMPLE_NETWORK_PROTOCOL instance.
115 @param ReadWrite TRUE for read operations, FALSE for write operations.
116 @param Offset Byte offset in the NVRAM device at which to start the read or
117 write operation. This must be a multiple of NvRamAccessSize
118 and less than NvRamSize. (See EFI_SIMPLE_NETWORK_MODE)
119 @param BufferSize The number of bytes to read or write from the NVRAM device.
120 This must also be a multiple of NvramAccessSize.
121 @param Buffer A pointer to the data buffer.
122
123 @retval EFI_SUCCESS The NVRAM access was performed.
124 @retval EFI_NOT_STARTED The network interface has not been started.
125 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
126 * The This parameter is NULL
127 * The This parameter does not point to a valid
128 EFI_SIMPLE_NETWORK_PROTOCOL structure
129 * The Offset parameter is not a multiple of
130 EFI_SIMPLE_NETWORK_MODE.NvRamAccessSize
131 * The Offset parameter is not less than
132 EFI_SIMPLE_NETWORK_MODE.NvRamSize
133 * The BufferSize parameter is not a multiple of
134 EFI_SIMPLE_NETWORK_MODE.NvRamAccessSize
135 * The Buffer parameter is NULL
136 @retval EFI_DEVICE_ERROR The command could not be sent to the network
137 interface.
138 @retval EFI_UNSUPPORTED This function is not supported by the network
139 interface.
140
141 **/
142 EFI_STATUS
143 EFIAPI
144 SnpUndi32NvData (
145 IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
146 IN BOOLEAN ReadWrite,
147 IN UINTN Offset,
148 IN UINTN BufferSize,
149 IN OUT VOID *Buffer
150 )
151 {
152 SNP_DRIVER *Snp;
153 EFI_TPL OldTpl;
154 EFI_STATUS Status;
155
156 //
157 // Get pointer to SNP driver instance for *this.
158 //
159 if (This == NULL) {
160 return EFI_INVALID_PARAMETER;
161 }
162
163 Snp = EFI_SIMPLE_NETWORK_DEV_FROM_THIS (This);
164
165 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
166
167 //
168 // Return error if the SNP is not initialized.
169 //
170 switch (Snp->Mode.State) {
171 case EfiSimpleNetworkInitialized:
172 break;
173
174 case EfiSimpleNetworkStopped:
175 Status = EFI_NOT_STARTED;
176 goto ON_EXIT;
177
178 default:
179 Status = EFI_DEVICE_ERROR;
180 goto ON_EXIT;
181 }
182
183 //
184 // Return error if non-volatile memory variables are not valid.
185 //
186 if ((Snp->Mode.NvRamSize == 0) || (Snp->Mode.NvRamAccessSize == 0)) {
187 Status = EFI_UNSUPPORTED;
188 goto ON_EXIT;
189 }
190
191 //
192 // Check for invalid parameter combinations.
193 //
194 if ((BufferSize == 0) ||
195 (Buffer == NULL) ||
196 (Offset >= Snp->Mode.NvRamSize) ||
197 (Offset + BufferSize > Snp->Mode.NvRamSize) ||
198 (BufferSize % Snp->Mode.NvRamAccessSize != 0) ||
199 (Offset % Snp->Mode.NvRamAccessSize != 0)
200 )
201 {
202 Status = EFI_INVALID_PARAMETER;
203 goto ON_EXIT;
204 }
205
206 //
207 // check the implementation flags of undi if we can write the nvdata!
208 //
209 if (!ReadWrite) {
210 Status = EFI_UNSUPPORTED;
211 } else {
212 Status = PxeNvDataRead (Snp, Offset, BufferSize, Buffer);
213 }
214
215 ON_EXIT:
216 gBS->RestoreTPL (OldTpl);
217
218 return Status;
219 }