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.

768 lines
16 KiB

  1. /*++
  2. Copyright (c) 1995-2001 Microsoft Corporation
  3. Module Name:
  4. sbentry.h
  5. Abstract:
  6. Contains the OS boot entry and boot options
  7. abstractions.
  8. Author:
  9. Vijay Jayaseelan (vijayj@microsoft.com) 14 Feb 2001
  10. Revision History:
  11. None.
  12. --*/
  13. #pragma once
  14. #include <nt.h>
  15. #include <ntrtl.h>
  16. #include <nturtl.h>
  17. #include <windows.h>
  18. #include <malloc.h>
  19. #include <tchar.h>
  20. //
  21. // Allocate & Deallocate routines
  22. //
  23. typedef void* (* SBEMemAllocateRoutine)(size_t Size);
  24. typedef void (* SBEMemFreeRoutine)(void *Memory);
  25. extern SBEMemAllocateRoutine AllocRoutine;
  26. extern SBEMemFreeRoutine FreeRoutine;
  27. #define ARRAY_SIZE(x) (sizeof((x))/sizeof((x)[0]))
  28. //
  29. // Internal attributes for the boot entry
  30. //
  31. #define OSBE_ATTRIBUTE_NEW 0x00000001
  32. #define OSBE_ATTRIBUTE_DELETED 0x00000002
  33. #define OSBE_ATTRIBUTE_OLDOS 0x00000004
  34. #define OSBE_ATTRIBUTE_DIRTY 0x10000000
  35. //
  36. // OS_BOOT_ENTRY abstraction
  37. //
  38. typedef struct _OS_BOOT_ENTRY *POS_BOOT_ENTRY;
  39. typedef struct _OS_BOOT_OPTIONS *POS_BOOT_OPTIONS;
  40. typedef VOID (* OSBEDeleteMethod)(
  41. IN POS_BOOT_ENTRY This
  42. );
  43. typedef BOOLEAN (* OSBEFlushMethod)(
  44. IN POS_BOOT_ENTRY This
  45. );
  46. typedef struct _OS_BOOT_ENTRY {
  47. //
  48. // Data members
  49. //
  50. ULONG Version;
  51. ULONG Id;
  52. TCHAR FriendlyName[MAX_PATH];
  53. TCHAR OsLoaderVolumeName[MAX_PATH];
  54. TCHAR OsLoaderPath[MAX_PATH];
  55. TCHAR BootVolumeName[MAX_PATH];
  56. TCHAR BootPath[MAX_PATH];
  57. TCHAR OsLoadOptions[MAX_PATH];
  58. ULONG Attributes;
  59. POS_BOOT_OPTIONS BootOptions;
  60. POS_BOOT_ENTRY NextEntry;
  61. //
  62. // Methods
  63. //
  64. OSBEDeleteMethod Delete;
  65. OSBEFlushMethod Flush;
  66. } OS_BOOT_ENTRY;
  67. #define OSBE_IS_DIRTY(_osbe) (((POS_BOOT_ENTRY)(_osbe))->Attributes & OSBE_ATTRIBUTE_DIRTY)
  68. #define OSBE_IS_NEW(_osbe) (((POS_BOOT_ENTRY)(_osbe))->Attributes & OSBE_ATTRIBUTE_NEW)
  69. #define OSBE_IS_DELETED(_osbe) (((POS_BOOT_ENTRY)(_osbe))->Attributes & OSBE_ATTRIBUTE_DELETED)
  70. #define OSBE_IS_OLDOS(_osbe) (((POS_BOOT_ENTRY)(_osbe))->Attributes & OSBE_ATTRIBUTE_OLDOS)
  71. #define OSBE_SET_DIRTY(_osbe) (((POS_BOOT_ENTRY)(_osbe))->Attributes |= OSBE_ATTRIBUTE_DIRTY)
  72. #define OSBE_SET_NEW(_osbe) (((POS_BOOT_ENTRY)(_osbe))->Attributes |= OSBE_ATTRIBUTE_NEW)
  73. #define OSBE_SET_DELETED(_osbe) (((POS_BOOT_ENTRY)(_osbe))->Attributes |= OSBE_ATTRIBUTE_DELETED)
  74. #define OSBE_SET_OLDOS(_osbe) (((POS_BOOT_ENTRY)(_osbe))->Attributes |= OSBE_ATTRIBUTE_OLDOS)
  75. #define OSBE_RESET_DIRTY(_osbe) (((POS_BOOT_ENTRY)(_osbe))->Attributes &= ~OSBE_ATTRIBUTE_DIRTY)
  76. #define OSBE_RESET_NEW(_osbe) (((POS_BOOT_ENTRY)(_osbe))->Attributes &= ~OSBE_ATTRIBUTE_NEW)
  77. #define OSBE_RESET_DELETED(_osbe) (((POS_BOOT_ENTRY)(_osbe))->Attributes &= ~OSBE_ATTRIBUTE_DELETED)
  78. #define OSBE_RESET_OLDOS(_osbe) (((POS_BOOT_ENTRY)(_osbe))->Attributes &= ~OSBE_ATTRIBUTE_OLDOS)
  79. //
  80. // Driver Entry Abstraction.
  81. //
  82. typedef struct _DRIVER_ENTRY *PDRIVER_ENTRY;
  83. typedef VOID (* DriverEntryDeleteMethod)(
  84. IN POS_BOOT_OPTIONS This,
  85. IN ULONG Id
  86. );
  87. typedef BOOLEAN (* DriverEntryFlushMethod)(
  88. IN PDRIVER_ENTRY This
  89. );
  90. typedef struct _DRIVER_ENTRY {
  91. ULONG Id;
  92. WCHAR FileName[MAX_PATH];
  93. WCHAR NtDevicePath[MAX_PATH];
  94. WCHAR DirPath[MAX_PATH];
  95. WCHAR FriendlyName[MAX_PATH];
  96. ULONG Attributes;
  97. ULONG FullPathLength;
  98. ULONG DevicePathLength;
  99. POS_BOOT_OPTIONS BootOptions;
  100. PDRIVER_ENTRY NextEntry;
  101. //
  102. // Methods
  103. //
  104. DriverEntryDeleteMethod Delete;
  105. DriverEntryFlushMethod Flush;
  106. } DRIVER_ENTRY;
  107. //
  108. // OS_BOOT_OPTIONS abstraction
  109. //
  110. typedef VOID (* OSBODeleteMethod)(
  111. IN POS_BOOT_OPTIONS This
  112. );
  113. typedef BOOLEAN (* OSBOFlushMethod)(
  114. IN POS_BOOT_OPTIONS This
  115. );
  116. typedef POS_BOOT_ENTRY (* OSBOAddNewBootEntryMethod)(
  117. IN POS_BOOT_OPTIONS This,
  118. IN PCTSTR FriendlyName,
  119. IN PCTSTR OsLoaderVolumeName,
  120. IN PCTSTR OsLoaderPath,
  121. IN PCTSTR BootVolumeName,
  122. IN PCTSTR BootPath,
  123. IN PCTSTR OsLoadOptions
  124. );
  125. typedef BOOLEAN (* OSBODeleteBootEntryMethod)(
  126. IN POS_BOOT_OPTIONS This,
  127. IN POS_BOOT_ENTRY BootEntry
  128. );
  129. typedef PDRIVER_ENTRY (* OSBOAddNewDriverEntryMethod)(
  130. IN POS_BOOT_OPTIONS This,
  131. IN PCWSTR FriendlyName,
  132. IN PCWSTR NtDevicePath,
  133. IN PCWSTR SrcNtFullPath
  134. );
  135. typedef BOOLEAN (* OSBODeleteDriverEntryMethod)(
  136. IN POS_BOOT_OPTIONS This,
  137. IN ULONG Id
  138. );
  139. typedef struct _OS_BOOT_OPTIONS {
  140. //
  141. // Data members
  142. //
  143. ULONG Version;
  144. ULONG Attributes;
  145. ULONG Timeout;
  146. POS_BOOT_ENTRY BootEntries;
  147. POS_BOOT_ENTRY CurrentEntry;
  148. ULONG EntryCount;
  149. PULONG BootOrder;
  150. ULONG BootOrderCount;
  151. PDRIVER_ENTRY DriverEntries;
  152. PDRIVER_ENTRY CurrentDriverEntry;
  153. ULONG DriverEntryCount;
  154. PULONG DriverEntryOrder;
  155. ULONG DriverEntryOrderCount;
  156. //
  157. // Methods
  158. //
  159. OSBODeleteMethod Delete;
  160. OSBOFlushMethod Flush;
  161. OSBOAddNewBootEntryMethod AddNewBootEntry;
  162. OSBODeleteBootEntryMethod DeleteBootEntry;
  163. OSBOAddNewDriverEntryMethod AddNewDriverEntry;
  164. OSBODeleteDriverEntryMethod DeleteDriverEntry;
  165. } OS_BOOT_OPTIONS;
  166. #define OSBO_IS_DIRTY(_osbo) (((POS_BOOT_OPTIONS)(_osbo))->Attributes & OSBE_ATTRIBUTE_DIRTY)
  167. #define OSBO_SET_DIRTY(_osbo) (((POS_BOOT_OPTIONS)(_osbo))->Attributes |= OSBE_ATTRIBUTE_DIRTY)
  168. #define OSBO_RESET_DIRTY(_osbo) (((POS_BOOT_OPTIONS)(_osbo))->Attributes &= ~OSBE_ATTRIBUTE_DIRTY)
  169. //
  170. // Dummy Driver Routines
  171. //
  172. PDRIVER_ENTRY
  173. OSBOFindDriverEntryByName(
  174. IN POS_BOOT_OPTIONS This,
  175. IN PCWSTR DriverName
  176. );
  177. //
  178. // OS_BOOT_ENTRY Methods
  179. //
  180. PCTSTR
  181. OSBEAddOsLoadOption(
  182. IN POS_BOOT_ENTRY This,
  183. IN PCTSTR BootOption
  184. );
  185. PCTSTR
  186. OSBERemoveOsLoadOption(
  187. IN POS_BOOT_ENTRY This,
  188. IN PCTSTR BootOption
  189. );
  190. BOOLEAN
  191. OSBEIsOsLoadOptionPresent(
  192. IN POS_BOOT_ENTRY This,
  193. IN PCTSTR BootOption
  194. );
  195. __inline
  196. VOID
  197. OSBEDelete(
  198. IN POS_BOOT_ENTRY This
  199. )
  200. {
  201. if (This) {
  202. (This->Delete)(This);
  203. }
  204. }
  205. __inline
  206. BOOLEAN
  207. OSBEFlush(
  208. IN POS_BOOT_ENTRY This
  209. )
  210. {
  211. return (This) ? This->Flush(This) : FALSE;
  212. }
  213. __inline
  214. ULONG
  215. OSBEGetId(
  216. IN POS_BOOT_ENTRY This
  217. )
  218. {
  219. return (This) ? This->Id : (-1);
  220. }
  221. __inline
  222. PCTSTR
  223. OSBEGetFriendlyName(
  224. IN POS_BOOT_ENTRY This
  225. )
  226. {
  227. return (This) ? This->FriendlyName : NULL;
  228. }
  229. __inline
  230. PCTSTR
  231. OSBESetFriendlyName(
  232. IN POS_BOOT_ENTRY This,
  233. IN PCTSTR Name
  234. )
  235. {
  236. PTSTR NewName = NULL;
  237. if (This && Name) {
  238. ULONG Size = ARRAY_SIZE(This->FriendlyName);
  239. _tcsncpy(This->FriendlyName, Name, Size - 1);
  240. This->FriendlyName[Size - 1] = UNICODE_NULL;
  241. NewName = This->FriendlyName;
  242. OSBE_SET_DIRTY(This);
  243. OSBO_SET_DIRTY(This->BootOptions);
  244. }
  245. return NewName;
  246. }
  247. __inline
  248. PCTSTR
  249. OSBEGetOsLoaderVolumeName(
  250. IN POS_BOOT_ENTRY This
  251. )
  252. {
  253. return (This) ? This->OsLoaderVolumeName : NULL;
  254. }
  255. __inline
  256. PCTSTR
  257. OSBESetOsLoaderVolumeName(
  258. IN POS_BOOT_ENTRY This,
  259. IN PCTSTR Name
  260. )
  261. {
  262. PTSTR NewName = NULL;
  263. if (This && Name) {
  264. ULONG Size = ARRAY_SIZE(This->OsLoaderVolumeName);
  265. _tcsncpy(This->OsLoaderVolumeName, Name, Size - 1);
  266. This->OsLoaderVolumeName[Size - 1] = UNICODE_NULL;
  267. NewName = This->OsLoaderVolumeName;
  268. OSBE_SET_DIRTY(This);
  269. OSBO_SET_DIRTY(This->BootOptions);
  270. }
  271. return NewName;
  272. }
  273. __inline
  274. PCTSTR
  275. OSBEGetOsLoaderPath(
  276. IN POS_BOOT_ENTRY This
  277. )
  278. {
  279. return (This) ? This->OsLoaderPath : NULL;
  280. }
  281. __inline
  282. PCTSTR
  283. OSBESetOsLoaderPath(
  284. IN POS_BOOT_ENTRY This,
  285. IN PCTSTR Name
  286. )
  287. {
  288. PTSTR NewName = NULL;
  289. if (This && Name) {
  290. ULONG Size = ARRAY_SIZE(This->OsLoaderPath);
  291. _tcsncpy(This->OsLoaderPath, Name, Size - 1);
  292. This->OsLoaderPath[Size - 1] = UNICODE_NULL;
  293. NewName = This->OsLoaderPath;
  294. OSBE_SET_DIRTY(This);
  295. OSBO_SET_DIRTY(This->BootOptions);
  296. }
  297. return NewName;
  298. }
  299. __inline
  300. PCTSTR
  301. OSBEGetBootVolumeName(
  302. IN POS_BOOT_ENTRY This
  303. )
  304. {
  305. return (This) ? This->BootVolumeName : NULL;
  306. }
  307. __inline
  308. PCTSTR
  309. OSBESetBootVolumeName(
  310. IN POS_BOOT_ENTRY This,
  311. IN PCTSTR Name
  312. )
  313. {
  314. PTSTR NewName = NULL;
  315. if (This && Name) {
  316. ULONG Size = ARRAY_SIZE(This->BootVolumeName);
  317. _tcsncpy(This->BootVolumeName, Name, Size - 1);
  318. This->BootVolumeName[Size - 1] = UNICODE_NULL;
  319. NewName = This->BootVolumeName;
  320. OSBE_SET_DIRTY(This);
  321. OSBO_SET_DIRTY(This->BootOptions);
  322. }
  323. return NewName;
  324. }
  325. __inline
  326. PCTSTR
  327. OSBEGetBootPath(
  328. IN POS_BOOT_ENTRY This
  329. )
  330. {
  331. return (This) ? This->BootPath : NULL;
  332. }
  333. __inline
  334. PCTSTR
  335. OSBESetBootPath(
  336. IN POS_BOOT_ENTRY This,
  337. IN PCTSTR Name
  338. )
  339. {
  340. PTSTR NewName = NULL;
  341. if (This && Name) {
  342. ULONG Size = ARRAY_SIZE(This->BootPath);
  343. _tcsncpy(This->BootPath, Name, Size - 1);
  344. This->BootPath[Size - 1] = UNICODE_NULL;
  345. NewName = This->BootPath;
  346. OSBE_SET_DIRTY(This);
  347. OSBO_SET_DIRTY(This->BootOptions);
  348. }
  349. return NewName;
  350. }
  351. __inline
  352. PCTSTR
  353. OSBEGetOsLoadOptions(
  354. IN POS_BOOT_ENTRY This
  355. )
  356. {
  357. return (This) ? This->OsLoadOptions : NULL;
  358. }
  359. __inline
  360. PCTSTR
  361. OSBESetOsLoadOptions(
  362. IN POS_BOOT_ENTRY This,
  363. IN PCTSTR LoadOptions
  364. )
  365. {
  366. TCHAR Buffer[MAX_PATH];
  367. PTSTR NewOptions = NULL;
  368. if (This && LoadOptions) {
  369. ULONG Size = ARRAY_SIZE(This->OsLoadOptions);
  370. _tcscpy(Buffer, LoadOptions);
  371. _tcsupr(Buffer);
  372. _tcsncpy(This->OsLoadOptions, Buffer, Size - 1);
  373. This->OsLoadOptions[Size - 1] = UNICODE_NULL;
  374. NewOptions = This->OsLoadOptions;
  375. OSBE_SET_DIRTY(This);
  376. OSBO_SET_DIRTY(This->BootOptions);
  377. }
  378. return NewOptions;
  379. }
  380. //
  381. // OS_BOOT_OPTIONS Methods
  382. //
  383. __inline
  384. BOOLEAN
  385. OSBOFlush(
  386. IN POS_BOOT_OPTIONS This
  387. )
  388. {
  389. return (This) ? (This->Flush(This)) : FALSE;
  390. }
  391. __inline
  392. VOID
  393. OSBODelete(
  394. IN POS_BOOT_OPTIONS This
  395. )
  396. {
  397. if (This) {
  398. This->Delete(This);
  399. }
  400. }
  401. __inline
  402. POS_BOOT_ENTRY
  403. OSBOAddNewBootEntry(
  404. IN POS_BOOT_OPTIONS This,
  405. IN PCTSTR FriendlyName,
  406. IN PCTSTR OsLoaderVolumeName,
  407. IN PCTSTR OsLoaderPath,
  408. IN PCTSTR BootVolumeName,
  409. IN PCTSTR BootPath,
  410. IN PCTSTR OsLoadOptions
  411. )
  412. {
  413. POS_BOOT_ENTRY Entry = NULL;
  414. if (This) {
  415. Entry = This->AddNewBootEntry(This,
  416. FriendlyName,
  417. OsLoaderVolumeName,
  418. OsLoaderPath,
  419. BootVolumeName,
  420. BootPath,
  421. OsLoadOptions);
  422. OSBO_SET_DIRTY(This);
  423. }
  424. return Entry;
  425. }
  426. __inline
  427. POS_BOOT_ENTRY
  428. OSBOGetActiveBootEntry(
  429. IN POS_BOOT_OPTIONS This
  430. )
  431. {
  432. POS_BOOT_ENTRY Entry = NULL;
  433. if (This) {
  434. Entry = This->CurrentEntry;
  435. }
  436. return Entry;
  437. }
  438. BOOLEAN
  439. OSBODeleteBootEntry(
  440. IN POS_BOOT_OPTIONS This,
  441. IN POS_BOOT_ENTRY BootEntry
  442. );
  443. POS_BOOT_ENTRY
  444. OSBOSetActiveBootEntry(
  445. IN POS_BOOT_OPTIONS This,
  446. IN POS_BOOT_ENTRY BootEntry
  447. );
  448. ULONG
  449. OSBOGetBootEntryCount(
  450. IN POS_BOOT_OPTIONS This
  451. );
  452. ULONG
  453. OSBOGetOrderedBootEntryCount(
  454. IN POS_BOOT_OPTIONS This
  455. );
  456. ULONG
  457. OSBOGetBootEntryIdByOrder(
  458. IN POS_BOOT_OPTIONS This,
  459. IN ULONG Index
  460. );
  461. POS_BOOT_ENTRY
  462. OSBOFindBootEntry(
  463. IN POS_BOOT_OPTIONS This,
  464. IN ULONG Id
  465. );
  466. ULONG
  467. OSBOFindBootEntryOrder(
  468. IN POS_BOOT_OPTIONS This,
  469. IN ULONG Id
  470. );
  471. __inline
  472. ULONG
  473. OSBOGetTimeOut(
  474. IN POS_BOOT_OPTIONS This
  475. )
  476. {
  477. return (This) ? This->Timeout : 0;
  478. }
  479. __inline
  480. ULONG
  481. OSBOSetTimeOut(
  482. IN POS_BOOT_OPTIONS This,
  483. IN ULONG Timeout
  484. )
  485. {
  486. ULONG OldTimeout = 0;
  487. if (This) {
  488. OldTimeout = This->Timeout;
  489. This->Timeout = Timeout;
  490. OSBE_SET_DIRTY(This);
  491. }
  492. return OldTimeout;
  493. }
  494. __inline
  495. ULONG
  496. OSBOGetBootEntryCount(
  497. IN POS_BOOT_OPTIONS This
  498. )
  499. {
  500. ULONG Count = 0;
  501. if (This) {
  502. Count = This->EntryCount;
  503. }
  504. return Count;
  505. }
  506. __inline
  507. ULONG
  508. OSBOGetOrderedBootEntryCount(
  509. IN POS_BOOT_OPTIONS This
  510. )
  511. {
  512. ULONG Count = 0;
  513. if (This) {
  514. Count = This->BootOrderCount;
  515. }
  516. return Count;
  517. }
  518. __inline
  519. ULONG
  520. OSBOGetBootEntryIdByOrder(
  521. IN POS_BOOT_OPTIONS This,
  522. IN ULONG Index
  523. )
  524. {
  525. ULONG Entry = -1;
  526. if (Index < OSBOGetOrderedBootEntryCount(This)) {
  527. Entry = This->BootOrder[Index];
  528. }
  529. return Entry;
  530. }
  531. __inline
  532. BOOLEAN
  533. OSBOLibraryInit(
  534. SBEMemAllocateRoutine AllocFunction,
  535. SBEMemFreeRoutine FreeFunction
  536. )
  537. {
  538. BOOLEAN Result = FALSE;
  539. if (AllocFunction && FreeFunction) {
  540. AllocRoutine = AllocFunction;
  541. FreeRoutine = FreeFunction;
  542. Result = TRUE;
  543. }
  544. return Result;
  545. }
  546. __inline
  547. POS_BOOT_ENTRY
  548. OSBOGetFirstBootEntry(
  549. IN POS_BOOT_OPTIONS This
  550. )
  551. {
  552. return (This) ? (This->BootEntries) : NULL;
  553. }
  554. __inline
  555. POS_BOOT_ENTRY
  556. OSBOGetNextBootEntry(
  557. IN POS_BOOT_OPTIONS This,
  558. IN POS_BOOT_ENTRY PrevEntry
  559. )
  560. {
  561. return (This && PrevEntry) ? PrevEntry->NextEntry : NULL;
  562. }
  563. //
  564. // Driver dummy routines
  565. //
  566. __inline
  567. ULONG
  568. OSDriverGetId(
  569. IN PDRIVER_ENTRY This
  570. )
  571. {
  572. return (This) ? This->Id : (-1);
  573. }
  574. __inline
  575. PCWSTR
  576. OSDriverGetFriendlyName(
  577. IN PDRIVER_ENTRY This
  578. )
  579. {
  580. return (This) ? This->FriendlyName : NULL;
  581. }
  582. __inline
  583. PCWSTR
  584. OSDriverGetFileName(
  585. IN PDRIVER_ENTRY This
  586. )
  587. {
  588. return (This) ? This->FileName : NULL;
  589. }
  590. __inline
  591. PCWSTR
  592. OSDriverGetDevicePath(
  593. IN PDRIVER_ENTRY This
  594. )
  595. {
  596. return (This) ? This->NtDevicePath : NULL;
  597. }
  598. __inline
  599. PCWSTR
  600. OSDriverGetFilePath(
  601. IN PDRIVER_ENTRY This
  602. )
  603. {
  604. return (This) ? (This->DirPath ) : NULL;
  605. }
  606. __inline
  607. BOOLEAN
  608. OSBODeleteDriverEntry(
  609. IN POS_BOOT_OPTIONS This,
  610. IN ULONG Id
  611. );
  612. //
  613. // memory allocation & deallocation routines
  614. //
  615. __inline
  616. void*
  617. __cdecl
  618. SBE_MALLOC(
  619. IN size_t Size
  620. )
  621. {
  622. return AllocRoutine ? AllocRoutine(Size) : NULL;
  623. }
  624. __inline
  625. void
  626. __cdecl
  627. SBE_FREE(
  628. IN void *Memory
  629. )
  630. {
  631. if (Memory && FreeRoutine) {
  632. FreeRoutine(Memory);
  633. }
  634. }