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