Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

314 lines
7.0 KiB

  1. /*++
  2. Copyright (c) 1991 - 2001 Microsoft Corporation
  3. Module Name:
  4. ### ### ## # ## ## ##### ### ## ## #### ##### #####
  5. ## # ### ### # ## ## ## ## ### ### ### ## # ## ## ## ##
  6. ### ## ## #### # ## ## ## ## ## ## ######## ## ## ## ## ##
  7. ### ## ## # #### #### ##### ## ## # ### ## ## ## ## ## ##
  8. ### ####### # ### #### #### ####### # # ## ## ##### #####
  9. # ## ## ## # ## ## ## ## ## ## # ## ## ## # ## ##
  10. ### ## ## # # ## ## ## ## ## # ## ## #### ## ##
  11. Abstract:
  12. This module contains the implementation for
  13. the ISaNvram interface class.
  14. Author:
  15. Wesley Witt (wesw) 1-Oct-2001
  16. Environment:
  17. User mode only.
  18. Notes:
  19. --*/
  20. #include "internal.h"
  21. CSaNvram::CSaNvram()
  22. {
  23. m_hFile = OpenSaDevice( SA_DEVICE_NVRAM );
  24. if (m_hFile != INVALID_HANDLE_VALUE) {
  25. ULONG Bytes;
  26. m_NvramCaps.SizeOfStruct = sizeof(SA_NVRAM_CAPS);
  27. BOOL b = DeviceIoControl(
  28. m_hFile,
  29. IOCTL_SA_GET_CAPABILITIES,
  30. NULL,
  31. 0,
  32. &m_NvramCaps,
  33. sizeof(SA_NVRAM_CAPS),
  34. &Bytes,
  35. NULL
  36. );
  37. if (!b) {
  38. CloseHandle( m_hFile );
  39. m_hFile = NULL;
  40. }
  41. b = DeviceIoControl(
  42. m_hFile,
  43. IOCTL_SA_GET_VERSION,
  44. NULL,
  45. 0,
  46. &m_InterfaceVersion,
  47. sizeof(ULONG),
  48. &Bytes,
  49. NULL
  50. );
  51. if (!b) {
  52. CloseHandle( m_hFile );
  53. m_hFile = NULL;
  54. }
  55. } else {
  56. m_hFile = NULL;
  57. }
  58. }
  59. CSaNvram::~CSaNvram()
  60. {
  61. if (m_hFile != NULL) {
  62. CloseHandle( m_hFile );
  63. }
  64. }
  65. STDMETHODIMP CSaNvram::get_InterfaceVersion(long *pVal)
  66. {
  67. *pVal = (long)m_InterfaceVersion;
  68. return S_OK;
  69. }
  70. STDMETHODIMP CSaNvram::get_BootCounter(long Number, long *pVal)
  71. {
  72. BOOL b;
  73. SA_NVRAM_BOOT_COUNTER BootCounter;
  74. ULONG Bytes;
  75. if (m_hFile == NULL){
  76. return HRESULT_FROM_WIN32(ERROR_INVALID_HANDLE);
  77. }
  78. BootCounter.SizeOfStruct = sizeof(SA_NVRAM_BOOT_COUNTER);
  79. BootCounter.Number = Number;
  80. BootCounter.Value = 0;
  81. BootCounter.DeviceId = 0;
  82. b = DeviceIoControl(
  83. m_hFile,
  84. IOCTL_NVRAM_READ_BOOT_COUNTER,
  85. NULL,
  86. 0,
  87. &BootCounter,
  88. sizeof(SA_NVRAM_BOOT_COUNTER),
  89. &Bytes,
  90. NULL
  91. );
  92. if (!b) {
  93. return HRESULT_FROM_WIN32(GetLastError());
  94. }
  95. *pVal = BootCounter.Value;
  96. return S_OK;
  97. }
  98. STDMETHODIMP CSaNvram::put_BootCounter(long Number, long newVal)
  99. {
  100. BOOL b;
  101. SA_NVRAM_BOOT_COUNTER BootCounter;
  102. ULONG Bytes;
  103. if (m_hFile == NULL){
  104. return HRESULT_FROM_WIN32(ERROR_INVALID_HANDLE);
  105. }
  106. BootCounter.SizeOfStruct = sizeof(SA_NVRAM_BOOT_COUNTER);
  107. BootCounter.Number = Number;
  108. BootCounter.Value = 0;
  109. BootCounter.DeviceId = 0;
  110. b = DeviceIoControl(
  111. m_hFile,
  112. IOCTL_NVRAM_READ_BOOT_COUNTER,
  113. NULL,
  114. 0,
  115. &BootCounter,
  116. sizeof(SA_NVRAM_BOOT_COUNTER),
  117. &Bytes,
  118. NULL
  119. );
  120. if (!b) {
  121. return HRESULT_FROM_WIN32(GetLastError());
  122. }
  123. BootCounter.SizeOfStruct = sizeof(SA_NVRAM_BOOT_COUNTER);
  124. BootCounter.Number = Number;
  125. BootCounter.Value = newVal;
  126. b = DeviceIoControl(
  127. m_hFile,
  128. IOCTL_NVRAM_WRITE_BOOT_COUNTER,
  129. &BootCounter,
  130. sizeof(SA_NVRAM_BOOT_COUNTER),
  131. NULL,
  132. 0,
  133. &Bytes,
  134. NULL
  135. );
  136. if (!b) {
  137. return HRESULT_FROM_WIN32(GetLastError());
  138. }
  139. return S_OK;
  140. }
  141. STDMETHODIMP CSaNvram::get_DeviceId(long Number, long *pVal)
  142. {
  143. BOOL b;
  144. SA_NVRAM_BOOT_COUNTER BootCounter;
  145. ULONG Bytes;
  146. if (m_hFile == NULL){
  147. return HRESULT_FROM_WIN32(ERROR_INVALID_HANDLE);
  148. }
  149. BootCounter.SizeOfStruct = sizeof(SA_NVRAM_BOOT_COUNTER);
  150. BootCounter.Number = Number;
  151. BootCounter.Value = 0;
  152. BootCounter.DeviceId = 0;
  153. b = DeviceIoControl(
  154. m_hFile,
  155. IOCTL_NVRAM_READ_BOOT_COUNTER,
  156. NULL,
  157. 0,
  158. &BootCounter,
  159. sizeof(SA_NVRAM_BOOT_COUNTER),
  160. &Bytes,
  161. NULL
  162. );
  163. if (!b) {
  164. return HRESULT_FROM_WIN32(GetLastError());
  165. }
  166. *pVal = BootCounter.DeviceId;
  167. return S_OK;
  168. }
  169. STDMETHODIMP CSaNvram::put_DeviceId(long Number, long newVal)
  170. {
  171. BOOL b;
  172. SA_NVRAM_BOOT_COUNTER BootCounter;
  173. ULONG Bytes;
  174. if (m_hFile == NULL){
  175. return HRESULT_FROM_WIN32(ERROR_INVALID_HANDLE);
  176. }
  177. BootCounter.SizeOfStruct = sizeof(SA_NVRAM_BOOT_COUNTER);
  178. BootCounter.Number = Number;
  179. BootCounter.Value = 0;
  180. BootCounter.DeviceId = 0;
  181. b = DeviceIoControl(
  182. m_hFile,
  183. IOCTL_NVRAM_READ_BOOT_COUNTER,
  184. NULL,
  185. 0,
  186. &BootCounter,
  187. sizeof(SA_NVRAM_BOOT_COUNTER),
  188. &Bytes,
  189. NULL
  190. );
  191. if (!b) {
  192. return HRESULT_FROM_WIN32(GetLastError());
  193. }
  194. BootCounter.SizeOfStruct = sizeof(SA_NVRAM_BOOT_COUNTER);
  195. BootCounter.Number = Number;
  196. BootCounter.DeviceId = newVal;
  197. b = DeviceIoControl(
  198. m_hFile,
  199. IOCTL_NVRAM_WRITE_BOOT_COUNTER,
  200. &BootCounter,
  201. sizeof(SA_NVRAM_BOOT_COUNTER),
  202. NULL,
  203. 0,
  204. &Bytes,
  205. NULL
  206. );
  207. if (!b) {
  208. return HRESULT_FROM_WIN32(GetLastError());
  209. }
  210. return S_OK;
  211. }
  212. STDMETHODIMP CSaNvram::get_Size(long *pVal)
  213. {
  214. *pVal = m_NvramCaps.NvramSize;
  215. return S_OK;
  216. }
  217. STDMETHODIMP CSaNvram::get_DataSlot(long Number, long *pVal)
  218. {
  219. BOOL b;
  220. ULONG Bytes;
  221. ULONG Position;
  222. ULONG NewPosition;
  223. if (m_hFile == NULL){
  224. return HRESULT_FROM_WIN32(ERROR_INVALID_HANDLE);
  225. }
  226. Position = (Number - 1) * sizeof(ULONG);
  227. NewPosition = SetFilePointer( m_hFile, Position, NULL, FILE_BEGIN );
  228. if (NewPosition != Position) {
  229. return HRESULT_FROM_WIN32(GetLastError());
  230. }
  231. b = ReadFile( m_hFile, pVal, sizeof(ULONG), &Bytes, NULL );
  232. if (!b ) {
  233. return HRESULT_FROM_WIN32(GetLastError());
  234. }
  235. return S_OK;
  236. }
  237. STDMETHODIMP CSaNvram::put_DataSlot(long Number, long newVal)
  238. {
  239. BOOL b;
  240. ULONG Bytes;
  241. ULONG Position;
  242. ULONG NewPosition;
  243. if (m_hFile == NULL){
  244. return HRESULT_FROM_WIN32(ERROR_INVALID_HANDLE);
  245. }
  246. Position = (Number - 1) * sizeof(ULONG);
  247. NewPosition = SetFilePointer( m_hFile, Position, NULL, FILE_BEGIN );
  248. if (NewPosition != Position) {
  249. return HRESULT_FROM_WIN32(GetLastError());
  250. }
  251. b = WriteFile( m_hFile, &newVal, sizeof(ULONG), &Bytes, NULL );
  252. if (!b ) {
  253. return HRESULT_FROM_WIN32(GetLastError());
  254. }
  255. return S_OK;
  256. }