rename
[mirror_edk2.git] / MdeModulePkg / Universal / Network / SnpDxe / Nvdata.c
1 /** @file
2 Copyright (c) 2004 - 2007, Intel Corporation
3 All rights reserved. This program and the accompanying materials
4 are licensed and made available under the terms and conditions of the BSD License
5 which accompanies this distribution. The full text of the license may be found at
6 http://opensource.org/licenses/bsd-license.php
7
8 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
9 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
10
11 Module name:
12 nvdata.c
13
14 Abstract:
15
16 Revision history:
17 2000-Feb-03 M(f)J Genesis.
18
19 **/
20
21 #include "snp.h"
22
23
24 /**
25 This routine calls Undi to read the desired number of eeprom bytes.
26
27 @param snp pointer to the snp driver structure
28 @param RegOffset eeprom register value relative to the base address
29 @param NumBytes number of bytes to read
30 @param BufferPtr pointer where to read into
31
32
33 **/
34 STATIC
35 EFI_STATUS
36 pxe_nvdata_read (
37 IN SNP_DRIVER *snp,
38 IN UINTN RegOffset,
39 IN UINTN NumBytes,
40 IN OUT VOID *BufferPtr
41 )
42 {
43 PXE_DB_NVDATA *db;
44
45 db = snp->db;
46 snp->cdb.OpCode = PXE_OPCODE_NVDATA;
47
48 snp->cdb.OpFlags = PXE_OPFLAGS_NVDATA_READ;
49
50 snp->cdb.CPBsize = PXE_CPBSIZE_NOT_USED;
51 snp->cdb.CPBaddr = PXE_CPBADDR_NOT_USED;
52
53 snp->cdb.DBsize = sizeof (PXE_DB_NVDATA);
54 snp->cdb.DBaddr = (UINT64)(UINTN) db;
55
56 snp->cdb.StatCode = PXE_STATCODE_INITIALIZE;
57 snp->cdb.StatFlags = PXE_STATFLAGS_INITIALIZE;
58 snp->cdb.IFnum = snp->if_num;
59 snp->cdb.Control = PXE_CONTROL_LAST_CDB_IN_LIST;
60
61 //
62 // Issue UNDI command and check result.
63 //
64 DEBUG ((EFI_D_NET, "\nsnp->undi.nvdata () "));
65
66 (*snp->issue_undi32_command) ((UINT64)(UINTN) &snp->cdb);
67
68 switch (snp->cdb.StatCode) {
69 case PXE_STATCODE_SUCCESS:
70 break;
71
72 case PXE_STATCODE_UNSUPPORTED:
73 DEBUG (
74 (EFI_D_NET,
75 "\nsnp->undi.nvdata() %xh:%xh\n",
76 snp->cdb.StatFlags,
77 snp->cdb.StatCode)
78 );
79
80 return EFI_UNSUPPORTED;
81
82 default:
83 DEBUG (
84 (EFI_D_NET,
85 "\nsnp->undi.nvdata() %xh:%xh\n",
86 snp->cdb.StatFlags,
87 snp->cdb.StatCode)
88 );
89
90 return EFI_DEVICE_ERROR;
91 }
92
93 CopyMem (BufferPtr, db->Data.Byte + RegOffset, NumBytes);
94
95 return EFI_SUCCESS;
96 }
97
98
99 /**
100 This is an interface call provided by SNP.
101 It does the basic checking on the input parameters and retrieves snp structure
102 and then calls the read_nvdata() call which does the actual reading
103
104 @param this context pointer
105 @param ReadOrWrite true for reading and false for writing
106 @param RegOffset eeprom register relative to the base
107 @param NumBytes how many bytes to read
108 @param BufferPtr address of memory to read into
109
110
111 **/
112 EFI_STATUS
113 EFIAPI
114 snp_undi32_nvdata (
115 IN EFI_SIMPLE_NETWORK_PROTOCOL *this,
116 IN BOOLEAN ReadOrWrite,
117 IN UINTN RegOffset,
118 IN UINTN NumBytes,
119 IN OUT VOID *BufferPtr
120 )
121 {
122 SNP_DRIVER *snp;
123 EFI_TPL OldTpl;
124 EFI_STATUS Status;
125
126 //
127 // Get pointer to SNP driver instance for *this.
128 //
129 if (this == NULL) {
130 return EFI_INVALID_PARAMETER;
131 }
132
133 snp = EFI_SIMPLE_NETWORK_DEV_FROM_THIS (this);
134
135 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
136
137 //
138 // Return error if the SNP is not initialized.
139 //
140 switch (snp->mode.State) {
141 case EfiSimpleNetworkInitialized:
142 break;
143
144 case EfiSimpleNetworkStopped:
145 Status = EFI_NOT_STARTED;
146 goto ON_EXIT;
147
148 default:
149 Status = EFI_DEVICE_ERROR;
150 goto ON_EXIT;
151 }
152 //
153 // Return error if non-volatile memory variables are not valid.
154 //
155 if (snp->mode.NvRamSize == 0 || snp->mode.NvRamAccessSize == 0) {
156 Status = EFI_UNSUPPORTED;
157 goto ON_EXIT;
158 }
159 //
160 // Check for invalid parameter combinations.
161 //
162 if ((NumBytes == 0) ||
163 (BufferPtr == NULL) ||
164 (RegOffset >= snp->mode.NvRamSize) ||
165 (RegOffset + NumBytes > snp->mode.NvRamSize) ||
166 (NumBytes % snp->mode.NvRamAccessSize != 0) ||
167 (RegOffset % snp->mode.NvRamAccessSize != 0)
168 ) {
169 Status = EFI_INVALID_PARAMETER;
170 goto ON_EXIT;
171 }
172 //
173 // check the implementation flags of undi if we can write the nvdata!
174 //
175 if (!ReadOrWrite) {
176 Status = EFI_UNSUPPORTED;
177 } else {
178 Status = pxe_nvdata_read (snp, RegOffset, NumBytes, BufferPtr);
179 }
180
181 ON_EXIT:
182 gBS->RestoreTPL (OldTpl);
183
184 return Status;
185 }