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