Nvdata.c 7.0 KB

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