Source code of Windows XP (NT5)
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.

354 lines
9.6 KiB

  1. /*++
  2. Copyright (c) 1990 Microsoft Corporation
  3. Module Name:
  4. firmware.c
  5. Abstract:
  6. This module implements Win32 firmware access APIs
  7. Author:
  8. Andrew Ritz (andrewr) 3-April-2001
  9. Revision History:
  10. --*/
  11. #include "basedll.h"
  12. #pragma hdrstop
  13. DWORD
  14. WINAPI
  15. GetFirmwareEnvironmentVariableA(
  16. IN LPCSTR lpName,
  17. IN LPCSTR lpGuid,
  18. OUT PVOID pBuffer,
  19. IN DWORD nSize
  20. )
  21. /*++
  22. Routine Description:
  23. The value of a firmware environment variable may be retrieved by using
  24. this API.
  25. This API is just a wrapper for NtQuerySystemEnvironmentValueEx. It's
  26. purpose is to provide a backwards compatible, documented interface into
  27. the Nt inteface. By having this wrapper, we do not have to document the
  28. Nt interface, and we have the freedom to change the NT interface in the
  29. future.
  30. Arguments:
  31. lpName - Pointer to a null terminate string that is the name of the
  32. firmware environment variable whose value is being requested.
  33. lpGuid - Pointer to a null terminate string that is the GUID namespace of
  34. the firmware environment variable whose value is being requested. On
  35. platforms that do not have a GUID based namespace, this value will be
  36. ignored.
  37. pBuffer - Pointer to a buffer that is to receive the value of the
  38. specified variable name.
  39. nSize - Specifies the maximum number of bytes that can be stored in
  40. the buffer pointed to by pBuffer.
  41. Return Value:
  42. The actual number of bytes stored in the memory pointed to by the
  43. pBuffer parameter. The return value is zero if the variable name was not
  44. found in the firmware or if another failure occurred (Call GetLastError()
  45. to get extended error information.)
  46. --*/
  47. {
  48. NTSTATUS Status;
  49. STRING Name,Guid;
  50. UNICODE_STRING UnicodeName,UnicodeGuid;
  51. DWORD RetVal;
  52. RtlInitString( &Name, lpName );
  53. Status = RtlAnsiStringToUnicodeString( &UnicodeName, &Name, TRUE );
  54. if (!NT_SUCCESS( Status )) {
  55. BaseSetLastNTError( Status );
  56. return ( 0 );
  57. }
  58. RtlInitString( &Guid, lpGuid );
  59. Status = RtlAnsiStringToUnicodeString( &UnicodeGuid, &Guid, TRUE );
  60. if (!NT_SUCCESS( Status )) {
  61. RtlFreeUnicodeString(&UnicodeName);
  62. BaseSetLastNTError( Status );
  63. return ( 0 );
  64. }
  65. RetVal = GetFirmwareEnvironmentVariableW(
  66. UnicodeName.Buffer,
  67. UnicodeGuid.Buffer,
  68. pBuffer,
  69. nSize );
  70. RtlFreeUnicodeString(&UnicodeName);
  71. RtlFreeUnicodeString(&UnicodeGuid);
  72. return( RetVal );
  73. }
  74. DWORD
  75. WINAPI
  76. GetFirmwareEnvironmentVariableW(
  77. IN LPCWSTR lpName,
  78. IN LPCWSTR lpGuid,
  79. OUT PVOID pBuffer,
  80. IN DWORD nSize
  81. )
  82. /*++
  83. Routine Description:
  84. The value of a firmware environment variable may be retrieved by using
  85. this API.
  86. This API is just a wrapper for NtQuerySystemEnvironmentValueEx. It's
  87. purpose is to provide a backwards compatible, documented interface into
  88. the Nt inteface. By having this wrapper, we do not have to document the
  89. Nt interface, and we have the freedom to change the NT interface in the
  90. future.
  91. Arguments:
  92. lpName - Pointer to a null terminate string that is the name of the
  93. firmware environment variable whose value is being requested.
  94. lpGuid - Pointer to a null terminate string that is the GUID namespace of
  95. the firmware environment variable whose value is being requested. On
  96. platforms that do not have a GUID based namespace, this value will be
  97. ignored.
  98. pBuffer - Pointer to a buffer that is to receive the value of the
  99. specified variable name.
  100. nSize - Specifies the maximum number of bytes that can be stored in
  101. the buffer pointed to by pBuffer.
  102. Return Value:
  103. The actual number of bytes stored in the memory pointed to by the
  104. pBuffer parameter. The return value is zero if the variable name was not
  105. found in the firmware or if another failure occurred (Call GetLastError()
  106. to get extended error information.)
  107. --*/
  108. {
  109. UNICODE_STRING uStringName,GuidString;
  110. GUID Guid;
  111. NTSTATUS Status;
  112. DWORD scratchSize;
  113. if (!lpName) {
  114. SetLastError(ERROR_INVALID_PARAMETER);
  115. return 0;
  116. }
  117. RtlInitUnicodeString(&uStringName, lpName);
  118. RtlInitUnicodeString(&GuidString, lpGuid);
  119. Status = RtlGUIDFromString(&GuidString, &Guid);
  120. if (!NT_SUCCESS(Status)) {
  121. BaseSetLastNTError(Status);
  122. return 0;
  123. }
  124. scratchSize = nSize;
  125. Status = NtQuerySystemEnvironmentValueEx(
  126. &uStringName,
  127. &Guid,
  128. pBuffer,
  129. &scratchSize,
  130. NULL); //bugbug need to give caller the attributes?
  131. if (!NT_SUCCESS(Status)) {
  132. BaseSetLastNTError(Status);
  133. return 0;
  134. }
  135. return scratchSize;
  136. }
  137. BOOL
  138. WINAPI
  139. SetFirmwareEnvironmentVariableA(
  140. IN LPCSTR lpName,
  141. IN LPCSTR lpGuid,
  142. IN PVOID pBuffer,
  143. IN DWORD nSize
  144. )
  145. /*++
  146. Routine Description:
  147. The value of a firmware environment variable may be set by using
  148. this API.
  149. This API is just a wrapper for NtSetSystemEnvironmentValueEx. It's
  150. purpose is to provide a backwards compatible, documented interface into
  151. the Nt inteface. By having this wrapper, we do not have to document the
  152. Nt interface, and we have the freedom to change the NT interface in the
  153. future.
  154. Arguments:
  155. lpName - Pointer to a null terminate string that is the name of the
  156. firmware environment variable whose value is being requested.
  157. lpGuid - Pointer to a null terminate string that is the GUID namespace of
  158. the firmware environment variable whose value is being requested. On
  159. platforms that do not have a GUID based namespace, this value will be
  160. ignored.
  161. pBuffer - Pointer to a buffer that contains the data for the specified
  162. variable name.
  163. nSize - Specifies the number of bytes that are stored in
  164. the buffer pointed to by pBuffer. Specifying 0 indicates that the
  165. caller wants the deleted.
  166. Return Value:
  167. TRUE indicates that the value was successfully set. The return value is
  168. FALSE if the variable name was not set. (Call GetLastError() to get
  169. extended error information.)
  170. --*/
  171. {
  172. NTSTATUS Status;
  173. STRING Name,Guid;
  174. UNICODE_STRING UnicodeName,UnicodeGuid;
  175. BOOL RetVal;
  176. RtlInitString( &Name, lpName );
  177. Status = RtlAnsiStringToUnicodeString( &UnicodeName, &Name, TRUE );
  178. if (!NT_SUCCESS( Status )) {
  179. BaseSetLastNTError( Status );
  180. return(FALSE);
  181. }
  182. RtlInitString( &Guid, lpGuid );
  183. Status = RtlAnsiStringToUnicodeString( &UnicodeGuid, &Guid, TRUE );
  184. if (!NT_SUCCESS( Status )) {
  185. RtlFreeUnicodeString(&UnicodeName);
  186. BaseSetLastNTError( Status );
  187. return(FALSE);
  188. }
  189. RetVal = SetFirmwareEnvironmentVariableW(
  190. UnicodeName.Buffer,
  191. UnicodeGuid.Buffer,
  192. pBuffer,
  193. nSize );
  194. RtlFreeUnicodeString(&UnicodeName);
  195. RtlFreeUnicodeString(&UnicodeGuid);
  196. return( RetVal );
  197. }
  198. BOOL
  199. WINAPI
  200. SetFirmwareEnvironmentVariableW(
  201. IN LPCWSTR lpName,
  202. IN LPCWSTR lpGuid,
  203. IN PVOID pBuffer,
  204. IN DWORD nSize
  205. )
  206. /*++
  207. Routine Description:
  208. The value of a firmware environment variable may be set by using
  209. this API.
  210. This API is just a wrapper for NtSetSystemEnvironmentValueEx. It's
  211. purpose is to provide a backwards compatible, documented interface into
  212. the Nt inteface. By having this wrapper, we do not have to document the
  213. Nt interface, and we have the freedom to change the NT interface in the
  214. future.
  215. Arguments:
  216. lpName - Pointer to a null terminate string that is the name of the
  217. firmware environment variable whose value is being requested.
  218. lpGuid - Pointer to a null terminate string that is the GUID namespace of
  219. the firmware environment variable whose value is being requested. On
  220. platforms that do not have a GUID based namespace, this value will be
  221. ignored.
  222. pBuffer - Pointer to a buffer that contains the data for the specified
  223. variable name.
  224. nSize - Specifies the number of bytes that are stored in
  225. the buffer pointed to by pBuffer. Specifying 0 indicates that the
  226. caller wants the deleted.
  227. Return Value:
  228. TRUE indicates that the value was successfully set. The return value is
  229. FALSE if the variable name was not set. (Call GetLastError() to get
  230. extended error information.)
  231. --*/
  232. {
  233. UNICODE_STRING uStringName,GuidString;
  234. GUID Guid;
  235. NTSTATUS Status;
  236. if (!lpName) {
  237. SetLastError(ERROR_INVALID_PARAMETER);
  238. return(FALSE);
  239. }
  240. RtlInitUnicodeString(&uStringName, lpName);
  241. RtlInitUnicodeString(&GuidString, lpGuid);
  242. Status = RtlGUIDFromString(&GuidString, &Guid);
  243. if (!NT_SUCCESS(Status)) {
  244. BaseSetLastNTError(Status);
  245. return(FALSE);
  246. }
  247. Status = NtSetSystemEnvironmentValueEx(
  248. &uStringName,
  249. &Guid,
  250. pBuffer,
  251. nSize,
  252. VARIABLE_ATTRIBUTE_NON_VOLATILE); //bugbug need to give caller the ability to set attributes?
  253. if (!NT_SUCCESS(Status)) {
  254. BaseSetLastNTError(Status);
  255. return(FALSE);
  256. }
  257. return( TRUE );
  258. }