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