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.

483 lines
18 KiB

  1. /****************************************************************************\
  2. OOBEUSB.C / OPK Wizard (OPKWIZ.EXE)
  3. Microsoft Confidential
  4. Copyright (c) Microsoft Corporation 1999
  5. All rights reserved
  6. Source file for the OPK Wizard that contains the external and internal
  7. functions used by the "OOBE USB Hardware Detection" wizard page.
  8. 09/99 - Added by A-STELO
  9. 09/2000 - Stephen Lodwick (STELO)
  10. Ported OPK Wizard to Whistler
  11. \****************************************************************************/
  12. //
  13. // Include File(s):
  14. //
  15. #include "pch.h"
  16. #include "wizard.h"
  17. #include "resource.h"
  18. //
  19. // Internal Defined Value(s):
  20. //
  21. #define INI_KEY_USBMOUSE _T("USBMouse")
  22. #define INI_KEY_USBKEYBOARD _T("USBKeyboard")
  23. #define FILE_USBMOUSE_HTM _T("nousbms.htm") // No USB mouse detected, director
  24. #define FILE_USBKEYBOARD_HTM _T("nousbkbd.htm") // No USB keyboard detected, director
  25. #define FILE_USBMSEKEY_HTM _T("nousbkm.htm") // No USB mouse/keyboard detected, director
  26. #define FILE_HARDWARE_HTM _T("oemhw.htm") // Hardware tutorial
  27. #define INI_SEC_OEMHW _T("OEMHardwareTutorial")
  28. #define DIR_USB DIR_OEM_OOBE _T("\\SETUP")
  29. #define DIR_HARDWARE DIR_OEM_OOBE _T("\\HTML\\OEMHW")
  30. //
  31. // Internal Function Prototype(s):
  32. //
  33. static BOOL OnInit(HWND, HWND, LPARAM);
  34. static void OnCommand(HWND, INT, HWND, UINT);
  35. static BOOL OnNext(HWND);
  36. static void EnableControls(HWND, UINT);
  37. static BOOL BrowseCopy(HWND hwnd, LPTSTR lpszPath, INT id, BOOL bBatch);
  38. //
  39. // External Function(s):
  40. //
  41. LRESULT CALLBACK OobeUSBDlgProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  42. {
  43. switch (uMsg)
  44. {
  45. HANDLE_MSG(hwnd, WM_INITDIALOG, OnInit);
  46. HANDLE_MSG(hwnd, WM_COMMAND, OnCommand);
  47. case WM_NOTIFY:
  48. switch ( ((NMHDR FAR *) lParam)->code )
  49. {
  50. case PSN_KILLACTIVE:
  51. case PSN_RESET:
  52. case PSN_WIZBACK:
  53. case PSN_WIZFINISH:
  54. break;
  55. case PSN_WIZNEXT:
  56. if ( !OnNext(hwnd) )
  57. WIZ_FAIL(hwnd);
  58. break;
  59. case PSN_QUERYCANCEL:
  60. WIZ_CANCEL(hwnd);
  61. break;
  62. case PSN_HELP:
  63. WIZ_HELP();
  64. break;
  65. case PSN_SETACTIVE:
  66. g_App.dwCurrentHelp = IDH_OOBEUSB;
  67. WIZ_BUTTONS(hwnd, PSWIZB_BACK | PSWIZB_NEXT);
  68. // Press next if the user is in auto mode
  69. //
  70. WIZ_NEXTONAUTO(hwnd, PSBTN_NEXT);
  71. break;
  72. default:
  73. return FALSE;
  74. }
  75. break;
  76. default:
  77. return FALSE;
  78. }
  79. return TRUE;
  80. }
  81. //
  82. // Internal Function(s):
  83. //
  84. static BOOL OnInit(HWND hwnd, HWND hwndFocus, LPARAM lParam)
  85. {
  86. TCHAR szData[MAX_URL] =NULLSTR,
  87. szHardwarePath[MAX_PATH] =NULLSTR;
  88. // Get information about local usb error files
  89. //
  90. GetPrivateProfileString(INI_SEC_OPTIONS, INI_KEY_USBERRORFILES, NULLSTR, szData, STRSIZE(szData), g_App.szOpkWizIniFile);
  91. // If the directory exists, then check the hardware box and populate the directory
  92. //
  93. if ( szData[0] )
  94. {
  95. CheckDlgButton(hwnd, IDC_USB_HARDWARE, TRUE);
  96. SetDlgItemText(hwnd, IDC_USB_DIR, szData);
  97. // Must simulate a copy if this is batch mode.
  98. //
  99. if ( GET_FLAG(OPK_BATCHMODE) )
  100. BrowseCopy(hwnd, szData, IDC_USB_BROWSE, TRUE);
  101. }
  102. // Check the USB Mouse detection if specified in oemaudit or opkwiz.inf (batchmode)
  103. //
  104. CheckDlgButton(hwnd, IDC_USB_MOUSE, GetPrivateProfileInt(INI_SEC_OPTIONS, INI_KEY_USBMOUSE, 0, GET_FLAG(OPK_BATCHMODE) ? g_App.szOpkWizIniFile : g_App.szOobeInfoIniFile) == 1);
  105. // Check the USB Keyboard detection if specified in oemaudit or opkwiz.inf (batchmode)
  106. //
  107. CheckDlgButton(hwnd, IDC_USB_KEYBOARD, GetPrivateProfileInt(INI_SEC_OPTIONS, INI_KEY_USBKEYBOARD, 0, GET_FLAG(OPK_BATCHMODE) ? g_App.szOpkWizIniFile : g_App.szOobeInfoIniFile) == 1);
  108. // If we've checked the hardware detection box, we must enable the proper controls
  109. //
  110. EnableControls(hwnd, IDC_USB_HARDWARE);
  111. // Get the file path to use for the hardware tutorials.
  112. //
  113. GetPrivateProfileString(INI_SEC_OPTIONS, INI_KEY_HARDWARE, NULLSTR, szHardwarePath, STRSIZE(szHardwarePath), g_App.szOpkWizIniFile);
  114. // Now init the hardware tutorial fields.
  115. //
  116. if ( szHardwarePath[0] )
  117. {
  118. CheckDlgButton(hwnd, IDC_HARDWARE_ON, TRUE);
  119. EnableControls(hwnd, IDC_HARDWARE_ON);
  120. SetDlgItemText(hwnd, IDC_HARDWARE_DIR, szHardwarePath);
  121. // Must simulate a copy if this is batch mode.
  122. //
  123. if ( GET_FLAG(OPK_BATCHMODE) )
  124. BrowseCopy(hwnd, szHardwarePath, IDC_HARDWARE_BROWSE, TRUE);
  125. }
  126. // Always return false to WM_INITDIALOG.
  127. //
  128. return FALSE;
  129. }
  130. static void OnCommand(HWND hwnd, INT id, HWND hwndCtl, UINT codeNotify)
  131. {
  132. TCHAR szPath[MAX_PATH];
  133. switch ( id )
  134. {
  135. case IDC_USB_BROWSE:
  136. case IDC_HARDWARE_BROWSE:
  137. // Try to use their current folder as the default.
  138. //
  139. szPath[0] = NULLCHR;
  140. GetDlgItemText(hwnd, (id == IDC_USB_BROWSE) ? IDC_USB_DIR : IDC_HARDWARE_DIR, szPath, AS(szPath));
  141. // If there is no current folder, just use the global browse default.
  142. //
  143. if ( szPath[0] == NULLCHR )
  144. lstrcpyn(szPath, g_App.szBrowseFolder,AS(szPath));
  145. // Now bring up the browse for folder dialog.
  146. //
  147. if ( BrowseForFolder(hwnd, IDS_BROWSEFOLDER, szPath, BIF_RETURNONLYFSDIRS | BIF_EDITBOX | BIF_VALIDATE) )
  148. BrowseCopy(hwnd, szPath, id, FALSE);
  149. break;
  150. case IDC_USB_KEYBOARD:
  151. case IDC_USB_MOUSE:
  152. // Check for required directory.
  153. //
  154. szPath[0] = NULLCHR;
  155. GetDlgItemText(hwnd, IDC_USB_DIR, szPath, AS(szPath));
  156. if ( szPath[0] )
  157. {
  158. // Check for required file(s).
  159. //
  160. lstrcpyn(szPath, g_App.szTempDir,AS(szPath));
  161. AddPathN(szPath, DIR_USB,AS(szPath));
  162. if ( DirectoryExists(szPath) )
  163. {
  164. LPTSTR lpEnd = szPath + lstrlen(szPath),
  165. lpFileName = (id == IDC_USB_KEYBOARD) ? FILE_USBKEYBOARD_HTM : FILE_USBMOUSE_HTM;
  166. // Check for keyboard or mouse file depending on what was checked.
  167. //
  168. if ( IsDlgButtonChecked(hwnd, id) == BST_CHECKED )
  169. {
  170. AddPathN(szPath, lpFileName,AS(szPath));
  171. if ( !FileExists(szPath) )
  172. MsgBox(GetParent(hwnd), IDS_ERR_USBFILE, IDS_APPNAME, MB_ICONWARNING | MB_OK | MB_APPLMODAL, lpFileName);
  173. *lpEnd = NULLCHR;
  174. }
  175. // Check for mouse/keyboard file.
  176. //
  177. if ( ( IsDlgButtonChecked(hwnd, IDC_USB_MOUSE) == BST_CHECKED ) &&
  178. ( IsDlgButtonChecked(hwnd, IDC_USB_KEYBOARD) == BST_CHECKED ) )
  179. {
  180. AddPathN(szPath, FILE_USBMSEKEY_HTM,AS(szPath));
  181. if ( !FileExists(szPath) )
  182. MsgBox(GetParent(hwnd), IDS_ERR_USBFILE, IDS_APPNAME, MB_ICONWARNING | MB_OK | MB_APPLMODAL, FILE_USBMSEKEY_HTM);
  183. *lpEnd = NULLCHR;
  184. }
  185. }
  186. }
  187. break;
  188. case IDC_USB_HARDWARE:
  189. case IDC_HARDWARE_ON:
  190. EnableControls(hwnd, id);
  191. break;
  192. }
  193. }
  194. static BOOL OnNext(HWND hwnd)
  195. {
  196. TCHAR szUsbPath[MAX_PATH],
  197. szHardwarePath[MAX_PATH];
  198. BOOL bSaveUsb;
  199. // Create the path to the directory that needs to be removed, or
  200. // must exist depending on the option selected.
  201. //
  202. lstrcpyn(szUsbPath, g_App.szTempDir,AS(szUsbPath));
  203. AddPathN(szUsbPath, DIR_USB,AS(szUsbPath));
  204. // If we are doing a custom USB hardware detection, check for a valid directory.
  205. //
  206. if ( bSaveUsb = (IsDlgButtonChecked(hwnd, IDC_USB_HARDWARE) == BST_CHECKED) )
  207. {
  208. TCHAR szBuffer[MAX_PATH];
  209. // One of the two boxes must be checked.
  210. //
  211. if ( ( IsDlgButtonChecked(hwnd, IDC_USB_MOUSE) != BST_CHECKED ) &&
  212. ( IsDlgButtonChecked(hwnd, IDC_USB_KEYBOARD) != BST_CHECKED ) )
  213. {
  214. MsgBox(GetParent(hwnd), IDS_ERR_USBHARDWARE, IDS_APPNAME, MB_ERRORBOX);
  215. SetFocus(GetDlgItem(hwnd, IDC_USB_HARDWARE));
  216. return FALSE;
  217. }
  218. // Make sure we have a valid target and source directory.
  219. //
  220. szBuffer[0] = NULLCHR;
  221. GetDlgItemText(hwnd, IDC_USB_DIR, szBuffer, AS(szBuffer));
  222. if ( !( szBuffer[0] && DirectoryExists(szUsbPath) ) )
  223. {
  224. MsgBox(GetParent(hwnd), IDS_ERR_USBDIR, IDS_APPNAME, MB_ERRORBOX);
  225. SetFocus(GetDlgItem(hwnd, IDC_USB_BROWSE));
  226. return FALSE;
  227. }
  228. }
  229. // Create the path to the directory that needs to be removed, or
  230. // must exist depending on the option selected.
  231. //
  232. lstrcpyn(szHardwarePath, g_App.szTempDir,AS(szHardwarePath));
  233. AddPathN(szHardwarePath, DIR_HARDWARE,AS(szHardwarePath));
  234. // If we are doing a custom hardware tutorial, check for a valid directory.
  235. //
  236. if ( IsDlgButtonChecked(hwnd, IDC_HARDWARE_ON) == BST_CHECKED )
  237. {
  238. TCHAR szBuffer[MAX_PATH];
  239. // Make sure we have a valid directory.
  240. //
  241. szBuffer[0] = NULLCHR;
  242. GetDlgItemText(hwnd, IDC_HARDWARE_DIR, szBuffer, AS(szBuffer));
  243. if ( !( szBuffer[0] && DirectoryExists(szHardwarePath) ) )
  244. {
  245. MsgBox(GetParent(hwnd), IDS_ERR_HARDWAREDIR, IDS_APPNAME, MB_ERRORBOX);
  246. SetFocus(GetDlgItem(hwnd, IDC_HARDWARE_BROWSE));
  247. return FALSE;
  248. }
  249. }
  250. else
  251. {
  252. // Remove the files that might be there.
  253. //
  254. if ( DirectoryExists(szHardwarePath) )
  255. DeletePath(szHardwarePath);
  256. // Clear out the display box so we know the files are
  257. // all gone now.
  258. //
  259. SetDlgItemText(hwnd, IDC_HARDWARE_DIR, NULLSTR);
  260. }
  261. // Now we want to remove the USB files if need be (we don't do
  262. // this above because only want to remove files after we have
  263. // made it passed all the cases where we can return.
  264. //
  265. if ( !bSaveUsb )
  266. {
  267. // We used to remove existing files here, but this also removes ISP files so we no longer do this.
  268. // Clear out the display box so we know the files are
  269. // all gone now.
  270. //
  271. SetDlgItemText(hwnd, IDC_USB_DIR, NULLSTR);
  272. }
  273. //
  274. // USB Section: Write out the path for hardware error files
  275. //
  276. szUsbPath[0] = NULLCHR;
  277. GetDlgItemText(hwnd, IDC_USB_DIR, szUsbPath, STRSIZE(szUsbPath));
  278. WritePrivateProfileString(INI_SEC_OPTIONS, INI_KEY_USBERRORFILES, ( IsDlgButtonChecked(hwnd, IDC_USB_HARDWARE) == BST_CHECKED ) ? szUsbPath : NULL, g_App.szOpkWizIniFile);
  279. // Write out the mouse detection settings
  280. //
  281. WritePrivateProfileString(INI_SEC_OPTIONS, INI_KEY_USBMOUSE, (((IsDlgButtonChecked(hwnd, IDC_USB_MOUSE) == BST_CHECKED) && ( IsDlgButtonChecked(hwnd, IDC_USB_HARDWARE) == BST_CHECKED )) ? STR_ONE : NULL), g_App.szOobeInfoIniFile);
  282. WritePrivateProfileString(INI_SEC_OPTIONS, INI_KEY_USBMOUSE, (((IsDlgButtonChecked(hwnd, IDC_USB_MOUSE) == BST_CHECKED) && ( IsDlgButtonChecked(hwnd, IDC_USB_HARDWARE) == BST_CHECKED )) ? STR_ONE : NULL), g_App.szOpkWizIniFile);
  283. // Write out the keyboard detection settings
  284. //
  285. WritePrivateProfileString(INI_SEC_OPTIONS, INI_KEY_USBKEYBOARD, (((IsDlgButtonChecked(hwnd, IDC_USB_KEYBOARD) == BST_CHECKED) && ( IsDlgButtonChecked(hwnd, IDC_USB_HARDWARE) == BST_CHECKED )) ? STR_ONE : NULL), g_App.szOobeInfoIniFile);
  286. WritePrivateProfileString(INI_SEC_OPTIONS, INI_KEY_USBKEYBOARD, (((IsDlgButtonChecked(hwnd, IDC_USB_KEYBOARD) == BST_CHECKED) && ( IsDlgButtonChecked(hwnd, IDC_USB_HARDWARE) == BST_CHECKED )) ? STR_ONE : NULL), g_App.szOpkWizIniFile);
  287. //
  288. // Hardware Detection: Write the custom hardware string.
  289. //
  290. szHardwarePath[0] = NULLCHR;
  291. GetDlgItemText(hwnd, IDC_HARDWARE_DIR, szHardwarePath, STRSIZE(szHardwarePath));
  292. WritePrivateProfileString(INI_SEC_OPTIONS, INI_KEY_HARDWARE, ( IsDlgButtonChecked(hwnd, IDC_HARDWARE_ON) == BST_CHECKED ) ? szHardwarePath : NULL, g_App.szOpkWizIniFile);
  293. // Write the hardware bit to the oobe ini file.
  294. //
  295. WritePrivateProfileString(INI_SEC_OEMHW, INI_KEY_HARDWARE, ( IsDlgButtonChecked(hwnd, IDC_HARDWARE_ON) == BST_CHECKED ) ? STR_ONE : STR_ZERO, g_App.szOobeInfoIniFile);
  296. return TRUE;
  297. }
  298. static void EnableControls(HWND hwnd, UINT uId)
  299. {
  300. BOOL fEnable = ( IsDlgButtonChecked(hwnd, uId) == BST_CHECKED );
  301. switch ( uId )
  302. {
  303. case IDC_USB_HARDWARE:
  304. EnableWindow(GetDlgItem(hwnd, IDC_USB_CAPTION), fEnable);
  305. EnableWindow(GetDlgItem(hwnd, IDC_USB_DIR), fEnable);
  306. EnableWindow(GetDlgItem(hwnd, IDC_USB_BROWSE), fEnable);
  307. EnableWindow(GetDlgItem(hwnd, IDC_USB_MOUSE), fEnable);
  308. EnableWindow(GetDlgItem(hwnd, IDC_USB_KEYBOARD), fEnable);
  309. break;
  310. case IDC_HARDWARE_ON:
  311. EnableWindow(GetDlgItem(hwnd, IDC_STATIC_HARDWARE), fEnable);
  312. EnableWindow(GetDlgItem(hwnd, IDC_HARDWARE_DIR), fEnable);
  313. EnableWindow(GetDlgItem(hwnd, IDC_HARDWARE_BROWSE), fEnable);
  314. break;
  315. }
  316. }
  317. static BOOL BrowseCopy(HWND hwnd, LPTSTR lpszPath, INT id, BOOL bBatch)
  318. {
  319. BOOL bRet = FALSE;
  320. TCHAR szDst[MAX_PATH];
  321. BOOL bOk = TRUE;
  322. // We need to create the path to the destination directory where
  323. // we are going to copy all the files.
  324. //
  325. lstrcpyn(szDst, g_App.szTempDir,AS(szDst));
  326. AddPathN(szDst, (id == IDC_USB_BROWSE) ? DIR_USB : DIR_HARDWARE,AS(szDst));
  327. // All these checks only need to happen if we are not copying in batch mode.
  328. //
  329. if ( !bBatch )
  330. {
  331. LPTSTR lpEnd;
  332. // If the pressed OK, save off the path in our last browse folder buffer.
  333. //
  334. lstrcpyn(g_App.szBrowseFolder, lpszPath,AS(g_App.szBrowseFolder));
  335. // Check for required file(s).
  336. //
  337. lpEnd = lpszPath + lstrlen(lpszPath);
  338. if ( id == IDC_HARDWARE_BROWSE )
  339. {
  340. AddPath(lpszPath, FILE_HARDWARE_HTM);
  341. bOk = ( FileExists(lpszPath) || ( MsgBox(GetParent(hwnd), IDS_ERR_HARDWAREFILES, IDS_APPNAME, MB_ICONSTOP | MB_OKCANCEL | MB_APPLMODAL) == IDOK ) );
  342. *lpEnd = NULLCHR;
  343. }
  344. else
  345. {
  346. // Check for keyboard file.
  347. //
  348. if ( bOk && ( IsDlgButtonChecked(hwnd, IDC_USB_KEYBOARD) == BST_CHECKED ) )
  349. {
  350. AddPath(lpszPath, FILE_USBKEYBOARD_HTM);
  351. bOk = ( FileExists(lpszPath) || ( MsgBox(GetParent(hwnd), IDS_ERR_USBFILE, IDS_APPNAME, MB_ICONSTOP | MB_OKCANCEL | MB_APPLMODAL, FILE_USBKEYBOARD_HTM) == IDOK ) );
  352. *lpEnd = NULLCHR;
  353. }
  354. // If we are doing a usb hardware detection, check for the required file, check for mouse error file.
  355. //
  356. if ( bOk && ( IsDlgButtonChecked(hwnd, IDC_USB_MOUSE) == BST_CHECKED ) )
  357. {
  358. AddPath(lpszPath, FILE_USBMOUSE_HTM);
  359. bOk = ( FileExists(lpszPath) || ( MsgBox(GetParent(hwnd), IDS_ERR_USBFILE, IDS_APPNAME, MB_ICONSTOP | MB_OKCANCEL | MB_APPLMODAL, FILE_USBMOUSE_HTM) == IDOK ) );
  360. *lpEnd = NULLCHR;
  361. }
  362. // Check for mouse/keyboard file.
  363. //
  364. if ( ( bOk ) &&
  365. ( IsDlgButtonChecked(hwnd, IDC_USB_MOUSE) == BST_CHECKED ) &&
  366. ( IsDlgButtonChecked(hwnd, IDC_USB_KEYBOARD) == BST_CHECKED ) )
  367. {
  368. AddPath(lpszPath, FILE_USBMSEKEY_HTM);
  369. bOk = ( FileExists(lpszPath) || ( MsgBox(GetParent(hwnd), IDS_ERR_USBFILE, IDS_APPNAME, MB_ICONSTOP | MB_OKCANCEL | MB_APPLMODAL, FILE_USBMSEKEY_HTM) == IDOK ) );
  370. *lpEnd = NULLCHR;
  371. }
  372. }
  373. }
  374. if ( bOk )
  375. {
  376. // We used to remove existing OobeUSB files here, but this also removes ISP files so we no longer do this.
  377. // Hardware is in unique directory, so it is OK to delete existing files for it
  378. if (id != IDC_USB_BROWSE) {
  379. if ( DirectoryExists(szDst) )
  380. DeletePath(szDst);
  381. }
  382. // Now try to copy all the new files over.
  383. //
  384. if ( !CopyDirectoryDialog(g_App.hInstance, hwnd, lpszPath, szDst) )
  385. {
  386. DeletePath(szDst);
  387. MsgBox(GetParent(hwnd), IDS_ERR_COPYINGFILES, IDS_APPNAME, MB_ERRORBOX, szDst[0], lpszPath);
  388. *lpszPath = NULLCHR;
  389. }
  390. else
  391. bRet = TRUE;
  392. // Reset the path display box.
  393. //
  394. SetDlgItemText(hwnd, (id == IDC_USB_BROWSE) ? IDC_USB_DIR : IDC_HARDWARE_DIR, lpszPath);
  395. }
  396. return bRet;
  397. }