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.

444 lines
19 KiB

  1. /********************************************************************
  2. Copyright (c) 1999 Microsoft Corporation
  3. Module Name:
  4. PCH_BIOS.CPP
  5. Abstract:
  6. WBEM provider class implementation for PCH_BIOS class
  7. Revision History:
  8. Ghim-Sim Chua (gschua) 05/05/99
  9. - Created
  10. Kalyani Narlanka (kalyanin) 05/12/99
  11. - Added Code to get all the properties of this class
  12. Kalyani Narlanka (kalyanin) 05/18/99
  13. ********************************************************************/
  14. #include "pchealth.h"
  15. #include "PCH_BIOS.h"
  16. /////////////////////////////////////////////////////////////////////////////
  17. // tracing stuff
  18. #ifdef THIS_FILE
  19. #undef THIS_FILE
  20. #endif
  21. static char __szTraceSourceFile[] = __FILE__;
  22. #define THIS_FILE __szTraceSourceFile
  23. #define TRACE_ID DCID_BIOS
  24. CPCH_BIOS MyPCH_BIOSSet (PROVIDER_NAME_PCH_BIOS, PCH_NAMESPACE) ;
  25. // Property names
  26. //===============
  27. const static WCHAR* pBIOSDate = L"BIOSDate" ;
  28. const static WCHAR* pBIOSName = L"BIOSName" ;
  29. const static WCHAR* pBIOSVersion = L"BIOSVersion" ;
  30. const static WCHAR* pCPU = L"CPU" ;
  31. const static WCHAR* pINFName = L"INFName" ;
  32. const static WCHAR* pMachineType = L"MachineType" ;
  33. const static WCHAR* pDriver = L"Driver" ;
  34. const static WCHAR* pDriverDate = L"DriverDate" ;
  35. const static WCHAR* pChange = L"Change";
  36. const static WCHAR* pTimeStamp = L"TimeStamp";
  37. /*****************************************************************************
  38. *
  39. * FUNCTION : CPCH_BIOS::EnumerateInstances
  40. *
  41. * DESCRIPTION : Returns the instance of this class
  42. *
  43. * INPUTS : A pointer to the MethodContext for communication with WinMgmt.
  44. * A long that contains the flags described in
  45. * IWbemServices::CreateInstanceEnumAsync. Note that the following
  46. * flags are handled by (and filtered out by) WinMgmt:
  47. * WBEM_FLAG_DEEP
  48. * WBEM_FLAG_SHALLOW
  49. * WBEM_FLAG_RETURN_IMMEDIATELY
  50. * WBEM_FLAG_FORWARD_ONLY
  51. * WBEM_FLAG_BIDIRECTIONAL
  52. *
  53. * RETURNS : WBEM_S_NO_ERROR if successful
  54. *
  55. * SYSNOPSIS : There is only instance of this class at any time. This function gives this
  56. * instance.
  57. * If there are no instances, returns WBEM_S_NO_ERROR.
  58. * It is not an error to have no instances.
  59. *
  60. *****************************************************************************/
  61. HRESULT CPCH_BIOS::EnumerateInstances ( MethodContext* pMethodContext, long lFlags )
  62. {
  63. // Begin Declarations
  64. //
  65. TraceFunctEnter("CPCH_BIOS::EnumerateInstances");
  66. HRESULT hRes = WBEM_S_NO_ERROR;
  67. HRESULT hRes1;
  68. HRESULT hRes2;
  69. // Query String
  70. CComBSTR bstrBIOSQuery = L"Select Name, ReleaseDate, Version FROM win32_BIOS";
  71. CComBSTR bstrProcessorQuery = L"Select DeviceId, Name FROM win32_processor";
  72. CComBSTR bstrComputerSystemQuery = L"Select Name, Description FROM win32_computerSystem";
  73. CComBSTR bstrDriver;
  74. // Registry Hive where BIOS Info is stored
  75. LPCTSTR lpctstrSystemHive = _T("System\\CurrentControlSet\\Services\\Class\\System");
  76. // Registry Names of interest
  77. LPCTSTR lpctstrDriverDesc = _T("DriverDesc");
  78. LPCTSTR lpctstrINFName = _T("INFPath");
  79. LPCTSTR lpctstrDriverDate = _T("DriverDate");
  80. LPCTSTR lpctstrSystem = _T("System\\");
  81. // Property Names
  82. LPCWSTR lpctstrReleaseDate = L"ReleaseDate";
  83. LPCWSTR lpctstrName = L"Name";
  84. LPCWSTR lpctstrVersion = L"Version";
  85. LPCWSTR lpctstrDescription = L"Description";
  86. LPCTSTR lpctstrSystemBoard = _T("System Board");
  87. // Strings
  88. TCHAR tchSubSystemKeyName[MAX_PATH];
  89. TCHAR tchDriverDescValue[MAX_PATH];
  90. TCHAR tchDriverDateValue[MAX_PATH];
  91. TCHAR tchINFNameValue[MAX_PATH];
  92. // Instances
  93. CComPtr<IEnumWbemClassObject> pBIOSEnumInst;
  94. CComPtr<IEnumWbemClassObject> pProcessorEnumInst;
  95. CComPtr<IEnumWbemClassObject> pComputerSystemEnumInst;
  96. // Instances
  97. // CInstancePtr pPCHBIOSInstance;
  98. // Objects
  99. IWbemClassObjectPtr pBIOSObj; // BUGBUG : WMI asserts if we use CComPtr
  100. IWbemClassObjectPtr pProcessorObj; // BUGBUG : WMI asserts if we use CComPtr
  101. IWbemClassObjectPtr pComputerSystemObj; // BUGBUG : WMI asserts if we use CComPtr
  102. // Variants
  103. CComVariant varDriver;
  104. CComVariant varDriverDate;
  105. CComVariant varINFName;
  106. CComVariant varSnapshot = "SnapShot";
  107. // Unsigned Longs....
  108. ULONG ulBIOSRetVal = 0;
  109. ULONG ulProcessorRetVal = 0;
  110. ULONG ulComputerSystemRetVal = 0;
  111. LONG lRegRetVal;
  112. // SystemTime
  113. SYSTEMTIME stUTCTime;
  114. // Registry Keys
  115. HKEY hkeySystem;
  116. HKEY hkeySubSystem;
  117. // DWORDs
  118. DWORD dwIndex = 0;
  119. DWORD dwSize = MAX_PATH;
  120. DWORD dwType;
  121. // Boolean
  122. BOOL fContinueEnum = FALSE;
  123. BOOL fCommit = FALSE;
  124. // FileTime
  125. PFILETIME pFileTime = NULL;
  126. // End Declarations
  127. // Create a new instance of PCH_BIOS Class based on the passed-in MethodContext
  128. CInstancePtr pPCHBIOSInstance(CreateNewInstance(pMethodContext), false);
  129. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  130. // TIME STAMP //
  131. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  132. // Get the date and time to update the TimeStamp Field
  133. GetSystemTime(&stUTCTime);
  134. hRes = pPCHBIOSInstance->SetDateTime(pTimeStamp, WBEMTime(stUTCTime));
  135. if (FAILED(hRes))
  136. {
  137. // Could not Set the Time Stamp
  138. // Continue anyway
  139. ErrorTrace(TRACE_ID, "SetDateTime on Timestamp Field failed.");
  140. }
  141. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  142. // CHANGE //
  143. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  144. hRes = pPCHBIOSInstance->SetVariant(pChange, varSnapshot);
  145. if (FAILED(hRes))
  146. {
  147. // Could not Set the Change Property
  148. // Continue anyway
  149. ErrorTrace(TRACE_ID, "Set Variant on Change Field failed.");
  150. }
  151. // Execute the query to get Name, ReleaseDate, Version FROM Win32_BIOS
  152. // Class.
  153. // pBIOSEnumInst contains a pointer to the instance returned.
  154. hRes = ExecWQLQuery(&pBIOSEnumInst, bstrBIOSQuery );
  155. if (SUCCEEDED(hRes))
  156. {
  157. // Query Succeeded!
  158. // Get the instance Object.
  159. if((pBIOSEnumInst->Next(WBEM_INFINITE, 1, &pBIOSObj, &ulBIOSRetVal)) == WBEM_S_NO_ERROR)
  160. {
  161. // Get Name, Date and Version
  162. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  163. // BIOSDATE //
  164. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  165. CopyProperty(pBIOSObj, lpctstrReleaseDate, pPCHBIOSInstance, pBIOSDate);
  166. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  167. // BIOSNAME //
  168. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  169. hRes = CopyProperty(pBIOSObj, lpctstrName, pPCHBIOSInstance, pBIOSName);
  170. if(SUCCEEDED(hRes))
  171. {
  172. fCommit = TRUE;
  173. }
  174. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  175. // BIOSVERSION //
  176. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  177. CopyProperty(pBIOSObj, lpctstrVersion, pPCHBIOSInstance, pBIOSVersion);
  178. }
  179. }
  180. // Done with Win32_BIOS Class
  181. // Now query Win32_Processor Class to get "CPU" property
  182. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  183. // CPU //
  184. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  185. hRes = ExecWQLQuery(&pProcessorEnumInst, bstrProcessorQuery);
  186. if (SUCCEEDED(hRes))
  187. {
  188. // Query Succeeded!
  189. // Get the instance Object.
  190. if((pProcessorEnumInst->Next(WBEM_INFINITE, 1, &pProcessorObj, &ulProcessorRetVal)) == WBEM_S_NO_ERROR)
  191. {
  192. // Get Name
  193. CopyProperty(pProcessorObj, lpctstrName, pPCHBIOSInstance, pCPU);
  194. }
  195. }
  196. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  197. // MACHINETYPE //
  198. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  199. hRes = ExecWQLQuery(&pComputerSystemEnumInst, bstrComputerSystemQuery);
  200. if (SUCCEEDED(hRes))
  201. {
  202. // Query Succeeded!
  203. // Get the instance Object.
  204. if((pComputerSystemEnumInst->Next(WBEM_INFINITE, 1, &pComputerSystemObj, &ulComputerSystemRetVal)) == WBEM_S_NO_ERROR)
  205. {
  206. // Get "Description"
  207. CopyProperty(pComputerSystemObj, lpctstrDescription, pPCHBIOSInstance, pMachineType);
  208. }
  209. }
  210. // Get the remaining properties i.e. INFName, Driver and DriverDate from the Registry
  211. // This is present in one of the keys under the HIVE "HKLM\System\CCS\Services\Class\System"
  212. // Enumerate keys under this hive until the regname "DeviceDesc" equals "System Board"
  213. // Once you hit "DeviceDesc" = "System Board" get the INFpath, Driver
  214. // DriverDate from there.
  215. lRegRetVal = RegOpenKeyEx(HKEY_LOCAL_MACHINE, lpctstrSystemHive, 0, KEY_READ, &hkeySystem);
  216. if(lRegRetVal == ERROR_SUCCESS)
  217. {
  218. // Opened the Registry key.
  219. // Enumerate the keys under this hive. One of the keys has
  220. // DeviceDesc = "system Board".
  221. lRegRetVal = RegEnumKeyEx(hkeySystem, dwIndex, tchSubSystemKeyName, &dwSize, NULL, NULL, NULL, pFileTime);
  222. if(lRegRetVal == ERROR_SUCCESS)
  223. {
  224. fContinueEnum = TRUE;
  225. }
  226. while(fContinueEnum)
  227. {
  228. // Open the SubKey.
  229. lRegRetVal = RegOpenKeyEx(hkeySystem, tchSubSystemKeyName, 0, KEY_READ, &hkeySubSystem);
  230. if(lRegRetVal == ERROR_SUCCESS)
  231. {
  232. // Opened the SubKey
  233. // Query for , regname "DriverDesc "
  234. dwSize = MAX_PATH;
  235. lRegRetVal = RegQueryValueEx(hkeySubSystem, lpctstrDriverDesc , NULL, &dwType, (LPBYTE)tchDriverDescValue, &dwSize);
  236. if(lRegRetVal == ERROR_SUCCESS)
  237. {
  238. // Compare if the value is equal to "System Board"
  239. if(_tcsicmp(tchDriverDescValue, lpctstrSystemBoard) == 0)
  240. {
  241. // The following statements could
  242. try
  243. {
  244. // Found the Right DriverDesc
  245. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  246. // DRIVER //
  247. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  248. // Driver = system+lptstrSubSystemKeyName
  249. bstrDriver = lpctstrSystem;
  250. bstrDriver.Append(tchSubSystemKeyName);
  251. varDriver = bstrDriver.Copy();
  252. hRes2 = pPCHBIOSInstance->SetVariant(pDriver, varDriver);
  253. if(FAILED(hRes2))
  254. {
  255. // Could not Set the DRIVER Property
  256. // Continue anyway
  257. ErrorTrace(TRACE_ID, "Set variant on Driver Failed.");
  258. }
  259. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  260. // DRIVERDATE //
  261. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  262. // Query for DriverDate
  263. dwSize = MAX_PATH;
  264. lRegRetVal = RegQueryValueEx(hkeySubSystem, lpctstrDriverDate, NULL, &dwType, (LPBYTE)tchDriverDescValue, &dwSize);
  265. if(lRegRetVal == ERROR_SUCCESS)
  266. {
  267. // Set the DriverDate
  268. varDriverDate = tchDriverDescValue;
  269. hRes2 = pPCHBIOSInstance->SetVariant(pDriverDate, varDriverDate);
  270. if(FAILED(hRes2))
  271. {
  272. // Could not Set the DRIVERDATE Property
  273. // Continue anyway
  274. ErrorTrace(TRACE_ID, "Set variant on DriverDate Failed.");
  275. }
  276. }
  277. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  278. // INFNAME //
  279. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  280. // Query for INFName
  281. dwSize = MAX_PATH;
  282. lRegRetVal = RegQueryValueEx(hkeySubSystem, lpctstrINFName, NULL, &dwType, (LPBYTE)tchINFNameValue, &dwSize);
  283. if(lRegRetVal == ERROR_SUCCESS)
  284. {
  285. // Set the INFName
  286. varINFName = tchINFNameValue;
  287. hRes2 = pPCHBIOSInstance->SetVariant(pINFName, varINFName);
  288. if(FAILED(hRes2))
  289. {
  290. // Could not Set the INFNAME Property
  291. // Continue anyway
  292. ErrorTrace(TRACE_ID, "Set variant on INFNAME Property Failed.");
  293. }
  294. }
  295. // Need not enumerate the rest of the keys
  296. fContinueEnum = FALSE;
  297. }
  298. catch(...)
  299. {
  300. lRegRetVal = RegCloseKey(hkeySubSystem);
  301. lRegRetVal = RegCloseKey(hkeySystem);
  302. throw;
  303. }
  304. } // end of strcmp
  305. } // end of Succeeded hRes2
  306. // Close the Opened Regkey
  307. lRegRetVal = RegCloseKey(hkeySubSystem);
  308. if(lRegRetVal != ERROR_SUCCESS)
  309. {
  310. // Could not close the reg Key
  311. ErrorTrace(TRACE_ID, "RegClose Sub Key Failed.");
  312. }
  313. }
  314. // Check to see if further enumeration is required.
  315. // continue to enumerate.
  316. if(fContinueEnum)
  317. {
  318. dwSize = MAX_PATH;
  319. dwIndex++;
  320. lRegRetVal = RegEnumKeyEx(hkeySystem, dwIndex, tchSubSystemKeyName, &dwSize, NULL, NULL, NULL, pFileTime);
  321. if(lRegRetVal != ERROR_SUCCESS)
  322. {
  323. fContinueEnum = FALSE;
  324. }
  325. }
  326. } // end of while
  327. lRegRetVal = RegCloseKey(hkeySystem);
  328. if(lRegRetVal != ERROR_SUCCESS)
  329. {
  330. // Could not close the reg Key
  331. ErrorTrace(TRACE_ID, "RegClose Key Failed.");
  332. }
  333. }
  334. // Got all the properties for PCH_BIOS Class
  335. if(fCommit)
  336. {
  337. hRes = pPCHBIOSInstance->Commit();
  338. if(FAILED(hRes))
  339. {
  340. // Could not Commit the instance
  341. ErrorTrace(TRACE_ID, "Commit on PCHBiosInstance Failed");
  342. }
  343. }
  344. TraceFunctLeave();
  345. return hRes ;
  346. }