bootim.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119
  1. /*++
  2. Copyright (c) 2012 Minoca Corp. All Rights Reserved
  3. Module Name:
  4. bootim.c
  5. Abstract:
  6. This module implements the underlying support routines for the image
  7. library to be run in the boot environment.
  8. Author:
  9. Evan Green 13-Oct-2012
  10. Environment:
  11. Boot
  12. --*/
  13. //
  14. // ------------------------------------------------------------------- Includes
  15. //
  16. #include <minoca/kernel/kernel.h>
  17. #include <minoca/lib/fat/fat.h>
  18. #include "firmware.h"
  19. #include "bootlib.h"
  20. #include "loader.h"
  21. #include "paging.h"
  22. //
  23. // ---------------------------------------------------------------- Definitions
  24. //
  25. //
  26. // ------------------------------------------------------ Data Type Definitions
  27. //
  28. /*++
  29. Structure Description:
  30. This structure stores information about an opened file in the boot
  31. environment.
  32. Members:
  33. FileName - Stores a pointer to the name of the file.
  34. FileNameSize - Stores the size of the file name string including the null
  35. terminator.
  36. LoadedFileBuffer - Stores a pointer to the buffer containing the file.
  37. FileSize - Stores the size of the loaded file, in bytes.
  38. --*/
  39. typedef struct _BOOT_FILE_HANDLE {
  40. PSTR FileName;
  41. ULONG FileNameSize;
  42. PVOID LoadedFileBuffer;
  43. UINTN FileSize;
  44. } BOOT_FILE_HANDLE, *PBOOT_FILE_HANDLE;
  45. /*++
  46. Structure Description:
  47. This structure stores information about an allocation of virtual address
  48. space by the boot environment.
  49. Members:
  50. PhysicalAddress - Stores the physical address of the memory backing the
  51. allocation.
  52. VirtualAddress - Stores the virtual address of the allocation.
  53. --*/
  54. typedef struct _BOOT_ADDRESS_SPACE_ALLOCATION {
  55. PHYSICAL_ADDRESS PhysicalAddress;
  56. PVOID VirtualAddress;
  57. } BOOT_ADDRESS_SPACE_ALLOCATION, *PBOOT_ADDRESS_SPACE_ALLOCATION;
  58. //
  59. // ----------------------------------------------- Internal Function Prototypes
  60. //
  61. PVOID
  62. BopImAllocateMemory (
  63. ULONG Size,
  64. ULONG Tag
  65. );
  66. VOID
  67. BopImFreeMemory (
  68. PVOID Allocation
  69. );
  70. KSTATUS
  71. BopImOpenFile (
  72. PVOID SystemContext,
  73. PSTR BinaryName,
  74. PIMAGE_FILE_INFORMATION File
  75. );
  76. VOID
  77. BopImCloseFile (
  78. PIMAGE_FILE_INFORMATION File
  79. );
  80. KSTATUS
  81. BopImLoadFile (
  82. PIMAGE_FILE_INFORMATION File,
  83. PIMAGE_BUFFER Buffer
  84. );
  85. VOID
  86. BopImUnloadBuffer (
  87. PIMAGE_FILE_INFORMATION File,
  88. PIMAGE_BUFFER Buffer
  89. );
  90. KSTATUS
  91. BopImAllocateAddressSpace (
  92. PLOADED_IMAGE Image
  93. );
  94. VOID
  95. BopImFreeAddressSpace (
  96. PLOADED_IMAGE Image
  97. );
  98. KSTATUS
  99. BopImMapImageSegment (
  100. HANDLE AddressSpaceHandle,
  101. PVOID AddressSpaceAllocation,
  102. PIMAGE_FILE_INFORMATION File,
  103. ULONGLONG FileOffset,
  104. PIMAGE_SEGMENT Segment,
  105. PIMAGE_SEGMENT PreviousSegment
  106. );
  107. VOID
  108. BopImUnmapImageSegment (
  109. HANDLE AddressSpaceHandle,
  110. PIMAGE_SEGMENT Segment
  111. );
  112. KSTATUS
  113. BopImNotifyImageLoad (
  114. PLOADED_IMAGE Image
  115. );
  116. VOID
  117. BopImNotifyImageUnload (
  118. PLOADED_IMAGE Image
  119. );
  120. VOID
  121. BopImInvalidateInstructionCacheRegion (
  122. PVOID Address,
  123. ULONG Size
  124. );
  125. PSTR
  126. BopImGetEnvironmentVariable (
  127. PSTR Variable
  128. );
  129. KSTATUS
  130. BopImFinalizeSegments (
  131. HANDLE AddressSpaceHandle,
  132. PIMAGE_SEGMENT Segments,
  133. UINTN SegmentCount
  134. );
  135. //
  136. // -------------------------------------------------------------------- Globals
  137. //
  138. //
  139. // Store a pointer to the boot device.
  140. //
  141. PBOOT_VOLUME BoBootDevice;
  142. //
  143. // Store the IDs of the directories to try when opening an image file.
  144. //
  145. FILE_ID BoDriversDirectoryId;
  146. FILE_ID BoSystemDirectoryId;
  147. //
  148. // Define the image library function table.
  149. //
  150. IM_IMPORT_TABLE BoImageFunctionTable = {
  151. BopImAllocateMemory,
  152. BopImFreeMemory,
  153. BopImOpenFile,
  154. BopImCloseFile,
  155. BopImLoadFile,
  156. NULL,
  157. BopImUnloadBuffer,
  158. BopImAllocateAddressSpace,
  159. BopImFreeAddressSpace,
  160. BopImMapImageSegment,
  161. BopImUnmapImageSegment,
  162. BopImNotifyImageLoad,
  163. BopImNotifyImageUnload,
  164. BopImInvalidateInstructionCacheRegion,
  165. BopImGetEnvironmentVariable,
  166. BopImFinalizeSegments,
  167. NULL
  168. };
  169. //
  170. // ------------------------------------------------------------------ Functions
  171. //
  172. KSTATUS
  173. BoInitializeImageSupport (
  174. PBOOT_VOLUME BootDevice,
  175. PBOOT_ENTRY BootEntry
  176. )
  177. /*++
  178. Routine Description:
  179. This routine initializes the image library for use in the boot
  180. environment.
  181. Arguments:
  182. BootDevice - Supplies a pointer to the boot volume token, used for loading
  183. images from disk.
  184. BootEntry - Supplies a pointer to the boot entry being launched.
  185. Return Value:
  186. Status code.
  187. --*/
  188. {
  189. PSTR DriversDirectoryPath;
  190. FILE_PROPERTIES Properties;
  191. KSTATUS Status;
  192. PSTR SystemRootPath;
  193. INITIALIZE_LIST_HEAD(&BoLoadedImageList);
  194. //
  195. // Save the boot volume.
  196. //
  197. BoBootDevice = BootDevice;
  198. //
  199. // Open up the system root.
  200. //
  201. SystemRootPath = DEFAULT_SYSTEM_ROOT_PATH;
  202. if (BootEntry != NULL) {
  203. SystemRootPath = BootEntry->SystemPath;
  204. }
  205. Status = BoLookupPath(BootDevice, NULL, SystemRootPath, &Properties);
  206. if (!KSUCCESS(Status)) {
  207. goto InitializeImageSupportEnd;
  208. }
  209. BoSystemDirectoryId = Properties.FileId;
  210. //
  211. // Open up the drivers directory.
  212. //
  213. DriversDirectoryPath = DEFAULT_DRIVERS_DIRECTORY_PATH;
  214. Status = BoLookupPath(BootDevice,
  215. &BoSystemDirectoryId,
  216. DriversDirectoryPath,
  217. &Properties);
  218. if (!KSUCCESS(Status)) {
  219. goto InitializeImageSupportEnd;
  220. }
  221. BoDriversDirectoryId = Properties.FileId;
  222. Status = ImInitialize(&BoImageFunctionTable);
  223. if (!KSUCCESS(Status)) {
  224. goto InitializeImageSupportEnd;
  225. }
  226. Status = STATUS_SUCCESS;
  227. InitializeImageSupportEnd:
  228. return Status;
  229. }
  230. //
  231. // --------------------------------------------------------- Internal Functions
  232. //
  233. PVOID
  234. BopImAllocateMemory (
  235. ULONG Size,
  236. ULONG Tag
  237. )
  238. /*++
  239. Routine Description:
  240. This routine allocates memory from the boot environment for the image
  241. library.
  242. Arguments:
  243. Size - Supplies the number of bytes required for the memory allocation.
  244. Tag - Supplies a 32-bit ASCII identifier used to tag the memroy allocation.
  245. Return Value:
  246. Returns a pointer to the memory allocation on success.
  247. NULL on failure.
  248. --*/
  249. {
  250. return BoAllocateMemory(Size);
  251. }
  252. VOID
  253. BopImFreeMemory (
  254. PVOID Allocation
  255. )
  256. /*++
  257. Routine Description:
  258. This routine frees memory to the boot environment allocated by the image
  259. library.
  260. Arguments:
  261. Allocation - Supplies a pointer the allocation to free.
  262. Return Value:
  263. None.
  264. --*/
  265. {
  266. BoFreeMemory(Allocation);
  267. return;
  268. }
  269. KSTATUS
  270. BopImOpenFile (
  271. PVOID SystemContext,
  272. PSTR BinaryName,
  273. PIMAGE_FILE_INFORMATION File
  274. )
  275. /*++
  276. Routine Description:
  277. This routine opens a file.
  278. Arguments:
  279. SystemContext - Supplies the context pointer passed to the load executable
  280. function.
  281. BinaryName - Supplies the name of the executable image to open.
  282. File - Supplies a pointer where the information for the file including its
  283. open handle will be returned.
  284. Return Value:
  285. Status code.
  286. --*/
  287. {
  288. PBOOT_FILE_HANDLE BootFileHandle;
  289. ULONGLONG LocalFileSize;
  290. FILE_PROPERTIES Properties;
  291. KSTATUS Status;
  292. BootFileHandle = BoAllocateMemory(sizeof(BOOT_FILE_HANDLE));
  293. if (BootFileHandle == NULL) {
  294. Status = STATUS_INSUFFICIENT_RESOURCES;
  295. goto OpenFileEnd;
  296. }
  297. RtlZeroMemory(BootFileHandle, sizeof(BOOT_FILE_HANDLE));
  298. BootFileHandle->FileNameSize = RtlStringLength(BinaryName) + 1;
  299. BootFileHandle->FileName = BoAllocateMemory(BootFileHandle->FileNameSize);
  300. if (BootFileHandle->FileName == NULL) {
  301. Status = STATUS_INSUFFICIENT_RESOURCES;
  302. goto OpenFileEnd;
  303. }
  304. RtlCopyMemory(BootFileHandle->FileName,
  305. BinaryName,
  306. BootFileHandle->FileNameSize);
  307. //
  308. // Open the file enough to make sure it's there, but don't actually load
  309. // it just now.
  310. //
  311. Status = BoLookupPath(BoBootDevice,
  312. &BoSystemDirectoryId,
  313. BootFileHandle->FileName,
  314. &Properties);
  315. if (Status == STATUS_PATH_NOT_FOUND) {
  316. Status = BoLookupPath(BoBootDevice,
  317. &BoDriversDirectoryId,
  318. BootFileHandle->FileName,
  319. &Properties);
  320. }
  321. if (!KSUCCESS(Status)) {
  322. goto OpenFileEnd;
  323. }
  324. if (Properties.Type != IoObjectRegularFile) {
  325. Status = STATUS_FILE_IS_DIRECTORY;
  326. goto OpenFileEnd;
  327. }
  328. READ_INT64_SYNC(&(Properties.FileSize), &LocalFileSize);
  329. if ((UINTN)LocalFileSize != LocalFileSize) {
  330. Status = STATUS_FILE_CORRUPT;
  331. goto OpenFileEnd;
  332. }
  333. BootFileHandle->FileSize = (UINTN)LocalFileSize;
  334. File->Size = LocalFileSize;
  335. File->ModificationDate = Properties.ModifiedTime.Seconds;
  336. OpenFileEnd:
  337. if (!KSUCCESS(Status)) {
  338. if (BootFileHandle != NULL) {
  339. if (BootFileHandle->FileName != NULL) {
  340. BoFreeMemory(BootFileHandle->FileName);
  341. }
  342. BoFreeMemory(BootFileHandle);
  343. BootFileHandle = INVALID_HANDLE;
  344. }
  345. }
  346. File->Handle = BootFileHandle;
  347. return Status;
  348. }
  349. VOID
  350. BopImCloseFile (
  351. PIMAGE_FILE_INFORMATION File
  352. )
  353. /*++
  354. Routine Description:
  355. This routine closes an open file, invalidating any memory mappings to it.
  356. Arguments:
  357. File - Supplies a pointer to the file information.
  358. Return Value:
  359. None.
  360. --*/
  361. {
  362. PBOOT_FILE_HANDLE BootFileHandle;
  363. BootFileHandle = (PBOOT_FILE_HANDLE)(File->Handle);
  364. ASSERT(BootFileHandle->LoadedFileBuffer == NULL);
  365. if (BootFileHandle->FileName != NULL) {
  366. BoFreeMemory(BootFileHandle->FileName);
  367. }
  368. BoFreeMemory(BootFileHandle);
  369. return;
  370. }
  371. KSTATUS
  372. BopImLoadFile (
  373. PIMAGE_FILE_INFORMATION File,
  374. PIMAGE_BUFFER Buffer
  375. )
  376. /*++
  377. Routine Description:
  378. This routine loads an entire file into memory so the image library can
  379. access it.
  380. Arguments:
  381. File - Supplies a pointer to the file information.
  382. Buffer - Supplies a pointer where the buffer will be returned on success.
  383. Return Value:
  384. Status code.
  385. --*/
  386. {
  387. PBOOT_FILE_HANDLE BootFileHandle;
  388. KSTATUS Status;
  389. BootFileHandle = (PBOOT_FILE_HANDLE)(File->Handle);
  390. if (BootFileHandle->LoadedFileBuffer == NULL) {
  391. Status = BoLoadFile(BoBootDevice,
  392. &BoSystemDirectoryId,
  393. BootFileHandle->FileName,
  394. &(BootFileHandle->LoadedFileBuffer),
  395. NULL,
  396. NULL);
  397. if (Status == STATUS_PATH_NOT_FOUND) {
  398. Status = BoLoadFile(BoBootDevice,
  399. &BoDriversDirectoryId,
  400. BootFileHandle->FileName,
  401. &(BootFileHandle->LoadedFileBuffer),
  402. NULL,
  403. NULL);
  404. }
  405. if (!KSUCCESS(Status)) {
  406. goto ImLoadFileEnd;
  407. }
  408. }
  409. Status = STATUS_SUCCESS;
  410. ImLoadFileEnd:
  411. if (KSUCCESS(Status)) {
  412. Buffer->Data = BootFileHandle->LoadedFileBuffer;
  413. Buffer->Size = BootFileHandle->FileSize;
  414. }
  415. return Status;
  416. }
  417. VOID
  418. BopImUnloadBuffer (
  419. PIMAGE_FILE_INFORMATION File,
  420. PIMAGE_BUFFER Buffer
  421. )
  422. /*++
  423. Routine Description:
  424. This routine unloads a file buffer created from either the load file or
  425. read file function, and frees the buffer.
  426. Arguments:
  427. File - Supplies a pointer to the file information.
  428. Buffer - Supplies the buffer returned by the load file function.
  429. Return Value:
  430. None.
  431. --*/
  432. {
  433. PBOOT_FILE_HANDLE BootFileHandle;
  434. BootFileHandle = (PBOOT_FILE_HANDLE)(File->Handle);
  435. if (BootFileHandle->LoadedFileBuffer != NULL) {
  436. BoFreeMemory(BootFileHandle->LoadedFileBuffer);
  437. BootFileHandle->LoadedFileBuffer = NULL;
  438. }
  439. return;
  440. }
  441. KSTATUS
  442. BopImAllocateAddressSpace (
  443. PLOADED_IMAGE Image
  444. )
  445. /*++
  446. Routine Description:
  447. This routine allocates a section of virtual address space that an image
  448. can be mapped in to.
  449. Arguments:
  450. Image - Supplies a pointer to the image being loaded. The system context,
  451. size, file information, load flags, and preferred virtual address will
  452. be initialized. This routine should set up the loaded image buffer,
  453. loaded lowest address, and allocator handle if needed.
  454. Return Value:
  455. Status code.
  456. --*/
  457. {
  458. UINTN AlignedSize;
  459. PBOOT_ADDRESS_SPACE_ALLOCATION Allocation;
  460. UINTN PageOffset;
  461. UINTN PageSize;
  462. PVOID PreferredAddress;
  463. KSTATUS Status;
  464. Image->AllocatorHandle = INVALID_HANDLE;
  465. PreferredAddress = Image->PreferredLowestAddress;
  466. Allocation = BoAllocateMemory(sizeof(BOOT_ADDRESS_SPACE_ALLOCATION));
  467. if (Allocation == NULL) {
  468. Status = STATUS_INSUFFICIENT_RESOURCES;
  469. goto AllocateAddressSpaceEnd;
  470. }
  471. RtlZeroMemory(Allocation, sizeof(BOOT_ADDRESS_SPACE_ALLOCATION));
  472. PageSize = MmPageSize();
  473. PageOffset = (UINTN)PreferredAddress -
  474. ALIGN_RANGE_DOWN((UINTN)PreferredAddress, PageSize);
  475. AlignedSize = ALIGN_RANGE_UP(Image->Size + PageOffset, PageSize);
  476. //
  477. // Allocate pages from the boot environment. This memory backs a boot
  478. // driver image, so it is marked as loader permanent.
  479. //
  480. Status = FwAllocatePages(&(Allocation->PhysicalAddress),
  481. AlignedSize,
  482. PageSize,
  483. MemoryTypeLoaderPermanent);
  484. if (!KSUCCESS(Status)) {
  485. goto AllocateAddressSpaceEnd;
  486. }
  487. //
  488. // Map the memory to find out where it lands in virtual space.
  489. //
  490. Allocation->VirtualAddress = (PVOID)-1;
  491. Status = BoMapPhysicalAddress(&(Allocation->VirtualAddress),
  492. Allocation->PhysicalAddress,
  493. AlignedSize,
  494. MAP_FLAG_GLOBAL | MAP_FLAG_EXECUTE,
  495. MemoryTypeLoaderPermanent);
  496. if (!KSUCCESS(Status)) {
  497. goto AllocateAddressSpaceEnd;
  498. }
  499. AllocateAddressSpaceEnd:
  500. if (!KSUCCESS(Status)) {
  501. if (Allocation != NULL) {
  502. BoFreeMemory(Allocation);
  503. }
  504. } else {
  505. Image->AllocatorHandle = Allocation;
  506. Image->LoadedLowestAddress = Allocation->VirtualAddress + PageOffset;
  507. ASSERT((UINTN)Allocation->PhysicalAddress ==
  508. Allocation->PhysicalAddress);
  509. Image->LoadedImageBuffer = (PVOID)(UINTN)Allocation->PhysicalAddress +
  510. PageOffset;
  511. }
  512. return Status;
  513. }
  514. VOID
  515. BopImFreeAddressSpace (
  516. PLOADED_IMAGE Image
  517. )
  518. /*++
  519. Routine Description:
  520. This routine frees a section of virtual address space that was previously
  521. allocated.
  522. Arguments:
  523. Image - Supplies a pointer to the loaded (or partially loaded) image.
  524. Return Value:
  525. None.
  526. --*/
  527. {
  528. BoFreeMemory((PBOOT_ADDRESS_SPACE_ALLOCATION)(Image->AllocatorHandle));
  529. return;
  530. }
  531. KSTATUS
  532. BopImMapImageSegment (
  533. HANDLE AddressSpaceHandle,
  534. PVOID AddressSpaceAllocation,
  535. PIMAGE_FILE_INFORMATION File,
  536. ULONGLONG FileOffset,
  537. PIMAGE_SEGMENT Segment,
  538. PIMAGE_SEGMENT PreviousSegment
  539. )
  540. /*++
  541. Routine Description:
  542. This routine maps a section of the image to the given virtual address.
  543. Arguments:
  544. AddressSpaceHandle - Supplies the handle used to claim the overall region
  545. of address space.
  546. AddressSpaceAllocation - Supplies the original lowest virtual address for
  547. this image.
  548. File - Supplies an optional pointer to the file being mapped. If this
  549. parameter is NULL, then a zeroed memory section is being mapped.
  550. FileOffset - Supplies the offset from the beginning of the file to the
  551. beginning of the mapping, in bytes.
  552. Segment - Supplies a pointer to the segment information to map. On output,
  553. the virtual address will contain the actual mapped address, and the
  554. mapping handle may be set.
  555. PreviousSegment - Supplies an optional pointer to the previous segment
  556. that was mapped, so this routine can handle overlap appropriately. This
  557. routine can assume that segments are always mapped in increasing order.
  558. Return Value:
  559. Status code.
  560. --*/
  561. {
  562. PBOOT_FILE_HANDLE BootFileHandle;
  563. PVOID Destination;
  564. PBOOT_ADDRESS_SPACE_ALLOCATION Region;
  565. PVOID Source;
  566. Region = (PBOOT_ADDRESS_SPACE_ALLOCATION)AddressSpaceHandle;
  567. BootFileHandle = NULL;
  568. if (File != NULL) {
  569. BootFileHandle = (PBOOT_FILE_HANDLE)(File->Handle);
  570. }
  571. //
  572. // Copy to the physical address of the buffer plus the offset from the
  573. // base VA corresponding to that physical address.
  574. //
  575. ASSERT((UINTN)Region->PhysicalAddress == Region->PhysicalAddress);
  576. Destination = (PUCHAR)(UINTN)Region->PhysicalAddress +
  577. (UINTN)Segment->VirtualAddress -
  578. (UINTN)Region->VirtualAddress;
  579. //
  580. // Copy from the file buffer plus the given offset.
  581. //
  582. if (Segment->FileSize != 0) {
  583. ASSERT((BootFileHandle != NULL) &&
  584. (FileOffset + Segment->FileSize < BootFileHandle->FileSize));
  585. Source = (PUCHAR)(BootFileHandle->LoadedFileBuffer) + FileOffset;
  586. RtlCopyMemory(Destination, Source, Segment->FileSize);
  587. }
  588. if (Segment->MemorySize > Segment->FileSize) {
  589. RtlZeroMemory(Destination + Segment->FileSize,
  590. Segment->MemorySize - Segment->FileSize);
  591. }
  592. return STATUS_SUCCESS;
  593. }
  594. VOID
  595. BopImUnmapImageSegment (
  596. HANDLE AddressSpaceHandle,
  597. PIMAGE_SEGMENT Segment
  598. )
  599. /*++
  600. Routine Description:
  601. This routine maps unmaps an image segment.
  602. Arguments:
  603. AddressSpaceHandle - Supplies the handle used to claim the overall region
  604. of address space.
  605. Segment - Supplies a pointer to the segment information to unmap.
  606. Return Value:
  607. None.
  608. --*/
  609. {
  610. //
  611. // Unmapping is currently not implemented.
  612. //
  613. return;
  614. }
  615. KSTATUS
  616. BopImNotifyImageLoad (
  617. PLOADED_IMAGE Image
  618. )
  619. /*++
  620. Routine Description:
  621. This routine notifies the primary consumer of the image library that an
  622. image has been loaded.
  623. Arguments:
  624. Image - Supplies the image that has just been loaded. This image should
  625. be subsequently returned to the image library upon requests for loaded
  626. images with the given name.
  627. Return Value:
  628. Status code. Failing status codes veto the image load.
  629. --*/
  630. {
  631. ULONG AllocationSize;
  632. UINTN BaseDifference;
  633. PDEBUG_MODULE LoadedModule;
  634. ULONG NameSize;
  635. KSTATUS Status;
  636. NameSize = RtlStringLength(Image->BinaryName) + 1;
  637. AllocationSize = sizeof(DEBUG_MODULE) +
  638. ((NameSize - ANYSIZE_ARRAY) * sizeof(CHAR));
  639. LoadedModule = BoAllocateMemory(AllocationSize);
  640. if (LoadedModule == NULL) {
  641. Status = STATUS_INSUFFICIENT_RESOURCES;
  642. goto NotifyImageLoadEnd;
  643. }
  644. RtlZeroMemory(LoadedModule, AllocationSize);
  645. //
  646. // Initialize the loaded image parameters.
  647. //
  648. RtlStringCopy(LoadedModule->BinaryName, Image->BinaryName, NameSize);
  649. LoadedModule->StructureSize = AllocationSize;
  650. LoadedModule->Timestamp = Image->File.ModificationDate;
  651. BaseDifference = Image->LoadedLowestAddress - Image->PreferredLowestAddress;
  652. LoadedModule->BaseAddress = Image->DeclaredBase + BaseDifference;
  653. LoadedModule->LowestAddress = Image->LoadedLowestAddress;
  654. LoadedModule->Size = Image->Size;
  655. LoadedModule->EntryPoint = Image->EntryPoint;
  656. LoadedModule->Image = Image;
  657. Image->DebuggerModule = LoadedModule;
  658. KdReportModuleChange(LoadedModule, TRUE);
  659. Status = STATUS_SUCCESS;
  660. NotifyImageLoadEnd:
  661. if (!KSUCCESS(Status)) {
  662. if (LoadedModule != NULL) {
  663. BoFreeMemory(LoadedModule);
  664. }
  665. }
  666. return Status;
  667. }
  668. VOID
  669. BopImNotifyImageUnload (
  670. PLOADED_IMAGE Image
  671. )
  672. /*++
  673. Routine Description:
  674. This routine notifies the primary consumer of the image library that an
  675. image is about to be unloaded from memory. Once this routine returns, the
  676. image should not be referenced again as it will be freed.
  677. Arguments:
  678. Image - Supplies the image that is about to be unloaded.
  679. Return Value:
  680. None.
  681. --*/
  682. {
  683. PDEBUG_MODULE UnloadingModule;
  684. UnloadingModule = Image->DebuggerModule;
  685. Image->DebuggerModule = NULL;
  686. KdReportModuleChange(UnloadingModule, FALSE);
  687. BoFreeMemory(UnloadingModule);
  688. return;
  689. }
  690. VOID
  691. BopImInvalidateInstructionCacheRegion (
  692. PVOID Address,
  693. ULONG Size
  694. )
  695. /*++
  696. Routine Description:
  697. This routine invalidates an instruction cache region after code has been
  698. modified.
  699. Arguments:
  700. Address - Supplies the virtual address of the revion to invalidate.
  701. Size - Supplies the number of bytes to invalidate.
  702. Return Value:
  703. None.
  704. --*/
  705. {
  706. return;
  707. }
  708. PSTR
  709. BopImGetEnvironmentVariable (
  710. PSTR Variable
  711. )
  712. /*++
  713. Routine Description:
  714. This routine gets an environment variable value for the image library.
  715. Arguments:
  716. Variable - Supplies a pointer to a null terminated string containing the
  717. name of the variable to get.
  718. Return Value:
  719. Returns a pointer to the value of the environment variable. The image
  720. library will not free or modify this value.
  721. NULL if the given environment variable is not set.
  722. --*/
  723. {
  724. if (RtlAreStringsEqual(Variable, IMAGE_DYNAMIC_LIBRARY_PATH_VARIABLE, -1) !=
  725. FALSE) {
  726. return ".";
  727. }
  728. return NULL;
  729. }
  730. KSTATUS
  731. BopImFinalizeSegments (
  732. HANDLE AddressSpaceHandle,
  733. PIMAGE_SEGMENT Segments,
  734. UINTN SegmentCount
  735. )
  736. /*++
  737. Routine Description:
  738. This routine applies the final memory protection attributes to the given
  739. segments. Read and execute bits can be applied at the time of mapping, but
  740. write protection may be applied here.
  741. Arguments:
  742. AddressSpaceHandle - Supplies the handle used to claim the overall region
  743. of address space.
  744. Segments - Supplies the final array of segments.
  745. SegmentCount - Supplies the number of segments.
  746. Return Value:
  747. Status code.
  748. --*/
  749. {
  750. UINTN End;
  751. ULONG MapFlags;
  752. UINTN PageSize;
  753. PIMAGE_SEGMENT Segment;
  754. UINTN SegmentIndex;
  755. UINTN Size;
  756. End = 0;
  757. PageSize = MmPageSize();
  758. for (SegmentIndex = 0; SegmentIndex < SegmentCount; SegmentIndex += 1) {
  759. Segment = &(Segments[SegmentIndex]);
  760. if (Segment->Type == ImageSegmentInvalid) {
  761. continue;
  762. }
  763. //
  764. // If the segment has no protection features, then there's nothing to
  765. // tighten up.
  766. //
  767. if ((Segment->Flags & IMAGE_MAP_FLAG_WRITE) != 0) {
  768. continue;
  769. }
  770. //
  771. // Compute the region whose protection should actually be changed.
  772. //
  773. End = (UINTN)(Segment->VirtualAddress) + Segment->MemorySize;
  774. End = ALIGN_RANGE_UP(End, PageSize);
  775. //
  776. // If the region has a real size, change its protection to read-only.
  777. //
  778. if (End > (UINTN)Segment->VirtualAddress) {
  779. Size = End - (UINTN)Segment->VirtualAddress;
  780. MapFlags = (MAP_FLAG_READ_ONLY << MAP_FLAG_PROTECT_SHIFT) |
  781. MAP_FLAG_READ_ONLY;
  782. BoChangeMappingAttributes(Segment->VirtualAddress, Size, MapFlags);
  783. }
  784. }
  785. return STATUS_SUCCESS;
  786. }