bootim.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  1. /*++
  2. Copyright (c) 2014 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 21-Feb-2014
  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 "bootman.h"
  21. //
  22. // ---------------------------------------------------------------- Definitions
  23. //
  24. //
  25. // ------------------------------------------------------ Data Type Definitions
  26. //
  27. /*++
  28. Structure Description:
  29. This structure stores information about an opened file in the boot
  30. environment.
  31. Members:
  32. FileName - Stores a pointer to the name of the file.
  33. FileNameSize - Stores the size of the file name string including the null
  34. terminator.
  35. LoadedFileBuffer - Stores a pointer to the buffer containing the file.
  36. FileSize - Stores the size of the loaded file, in bytes.
  37. --*/
  38. typedef struct _BOOT_FILE_HANDLE {
  39. PSTR FileName;
  40. ULONG FileNameSize;
  41. PVOID LoadedFileBuffer;
  42. UINTN FileSize;
  43. } BOOT_FILE_HANDLE, *PBOOT_FILE_HANDLE;
  44. //
  45. // ----------------------------------------------- Internal Function Prototypes
  46. //
  47. PVOID
  48. BmpImAllocateMemory (
  49. ULONG Size,
  50. ULONG Tag
  51. );
  52. VOID
  53. BmpImFreeMemory (
  54. PVOID Allocation
  55. );
  56. KSTATUS
  57. BmpImOpenFile (
  58. PVOID SystemContext,
  59. PSTR BinaryName,
  60. PIMAGE_FILE_INFORMATION File
  61. );
  62. VOID
  63. BmpImCloseFile (
  64. PIMAGE_FILE_INFORMATION File
  65. );
  66. KSTATUS
  67. BmpImLoadFile (
  68. PIMAGE_FILE_INFORMATION File,
  69. PIMAGE_BUFFER Buffer
  70. );
  71. VOID
  72. BmpImUnloadBuffer (
  73. PIMAGE_FILE_INFORMATION File,
  74. PIMAGE_BUFFER Buffer
  75. );
  76. KSTATUS
  77. BmpImAllocateAddressSpace (
  78. PLOADED_IMAGE Image
  79. );
  80. VOID
  81. BmpImFreeAddressSpace (
  82. PLOADED_IMAGE Image
  83. );
  84. KSTATUS
  85. BmpImMapImageSegment (
  86. HANDLE AddressSpaceHandle,
  87. PVOID AddressSpaceAllocation,
  88. PIMAGE_FILE_INFORMATION File,
  89. ULONGLONG FileOffset,
  90. PIMAGE_SEGMENT Segment,
  91. PIMAGE_SEGMENT PreviousSegment
  92. );
  93. VOID
  94. BmpImUnmapImageSegment (
  95. HANDLE AddressSpaceHandle,
  96. PIMAGE_SEGMENT Segment
  97. );
  98. KSTATUS
  99. BmpImNotifyImageLoad (
  100. PLOADED_IMAGE Image
  101. );
  102. VOID
  103. BmpImNotifyImageUnload (
  104. PLOADED_IMAGE Image
  105. );
  106. VOID
  107. BmpImInvalidateInstructionCacheRegion (
  108. PVOID Address,
  109. ULONG Size
  110. );
  111. //
  112. // -------------------------------------------------------------------- Globals
  113. //
  114. //
  115. // Store a pointer to the boot device.
  116. //
  117. PVOID BmBootDevice;
  118. //
  119. // Store a pointer to the selected boot entry.
  120. //
  121. PBOOT_ENTRY BmBootEntry;
  122. //
  123. // Store the IDs of the directories to try when opening an image file.
  124. //
  125. FILE_ID BmSystemDirectoryId;
  126. //
  127. // Define the image library function table.
  128. //
  129. IM_IMPORT_TABLE BmImageFunctionTable = {
  130. BmpImAllocateMemory,
  131. BmpImFreeMemory,
  132. BmpImOpenFile,
  133. BmpImCloseFile,
  134. BmpImLoadFile,
  135. NULL,
  136. BmpImUnloadBuffer,
  137. BmpImAllocateAddressSpace,
  138. BmpImFreeAddressSpace,
  139. BmpImMapImageSegment,
  140. BmpImUnmapImageSegment,
  141. BmpImNotifyImageLoad,
  142. BmpImNotifyImageUnload,
  143. BmpImInvalidateInstructionCacheRegion,
  144. NULL,
  145. NULL,
  146. NULL
  147. };
  148. //
  149. // ------------------------------------------------------------------ Functions
  150. //
  151. KSTATUS
  152. BmpInitializeImageSupport (
  153. PVOID BootDevice,
  154. PBOOT_ENTRY BootEntry
  155. )
  156. /*++
  157. Routine Description:
  158. This routine initializes the image library for use in the boot manager.
  159. Arguments:
  160. BootDevice - Supplies a pointer to the boot volume token, used for loading
  161. images from disk.
  162. BootEntry - Supplies a pointer to the selected boot entry.
  163. Return Value:
  164. Status code.
  165. --*/
  166. {
  167. FILE_PROPERTIES Properties;
  168. KSTATUS Status;
  169. INITIALIZE_LIST_HEAD(&BmLoadedImageList);
  170. BmBootDevice = BootDevice;
  171. BmBootEntry = BootEntry;
  172. //
  173. // Open up the directories to search.
  174. //
  175. Status = BoLookupPath(BmBootDevice,
  176. NULL,
  177. BootEntry->SystemPath,
  178. &Properties);
  179. if (!KSUCCESS(Status)) {
  180. goto InitializeImageSupportEnd;
  181. }
  182. BmSystemDirectoryId = Properties.FileId;
  183. Status = ImInitialize(&BmImageFunctionTable);
  184. if (!KSUCCESS(Status)) {
  185. goto InitializeImageSupportEnd;
  186. }
  187. Status = STATUS_SUCCESS;
  188. InitializeImageSupportEnd:
  189. return Status;
  190. }
  191. //
  192. // --------------------------------------------------------- Internal Functions
  193. //
  194. PVOID
  195. BmpImAllocateMemory (
  196. ULONG Size,
  197. ULONG Tag
  198. )
  199. /*++
  200. Routine Description:
  201. This routine allocates memory from the boot environment for the image
  202. library.
  203. Arguments:
  204. Size - Supplies the number of bytes required for the memory allocation.
  205. Tag - Supplies a 32-bit ASCII identifier used to tag the memroy allocation.
  206. Return Value:
  207. Returns a pointer to the memory allocation on success.
  208. NULL on failure.
  209. --*/
  210. {
  211. return BoAllocateMemory(Size);
  212. }
  213. VOID
  214. BmpImFreeMemory (
  215. PVOID Allocation
  216. )
  217. /*++
  218. Routine Description:
  219. This routine frees memory to the boot environment allocated by the image
  220. library.
  221. Arguments:
  222. Allocation - Supplies a pointer the allocation to free.
  223. Return Value:
  224. None.
  225. --*/
  226. {
  227. BoFreeMemory(Allocation);
  228. return;
  229. }
  230. KSTATUS
  231. BmpImOpenFile (
  232. PVOID SystemContext,
  233. PSTR BinaryName,
  234. PIMAGE_FILE_INFORMATION File
  235. )
  236. /*++
  237. Routine Description:
  238. This routine opens a file.
  239. Arguments:
  240. SystemContext - Supplies the context pointer passed to the load executable
  241. function.
  242. BinaryName - Supplies the name of the executable image to open.
  243. File - Supplies a pointer where the information for the file including its
  244. open handle will be returned.
  245. Return Value:
  246. Status code.
  247. --*/
  248. {
  249. PBOOT_FILE_HANDLE BootFileHandle;
  250. KSTATUS Status;
  251. BootFileHandle = BoAllocateMemory(sizeof(BOOT_FILE_HANDLE));
  252. if (BootFileHandle == NULL) {
  253. Status = STATUS_INSUFFICIENT_RESOURCES;
  254. goto OpenFileEnd;
  255. }
  256. RtlZeroMemory(BootFileHandle, sizeof(BOOT_FILE_HANDLE));
  257. BootFileHandle->FileNameSize = RtlStringLength(BinaryName) + 1;
  258. BootFileHandle->FileName = BoAllocateMemory(BootFileHandle->FileNameSize);
  259. if (BootFileHandle->FileName == NULL) {
  260. Status = STATUS_INSUFFICIENT_RESOURCES;
  261. goto OpenFileEnd;
  262. }
  263. RtlCopyMemory(BootFileHandle->FileName,
  264. BinaryName,
  265. BootFileHandle->FileNameSize);
  266. //
  267. // Open the file enough to make sure it's there, but don't actually load
  268. // it just now.
  269. //
  270. Status = BoLoadFile(BmBootDevice,
  271. &BmSystemDirectoryId,
  272. BootFileHandle->FileName,
  273. NULL,
  274. &(BootFileHandle->FileSize),
  275. &(File->ModificationDate));
  276. if (!KSUCCESS(Status)) {
  277. goto OpenFileEnd;
  278. }
  279. File->Size = BootFileHandle->FileSize;
  280. OpenFileEnd:
  281. if (!KSUCCESS(Status)) {
  282. if (BootFileHandle != NULL) {
  283. if (BootFileHandle->FileName != NULL) {
  284. BoFreeMemory(BootFileHandle->FileName);
  285. }
  286. BoFreeMemory(BootFileHandle);
  287. BootFileHandle = INVALID_HANDLE;
  288. }
  289. }
  290. File->Handle = BootFileHandle;
  291. return Status;
  292. }
  293. VOID
  294. BmpImCloseFile (
  295. PIMAGE_FILE_INFORMATION File
  296. )
  297. /*++
  298. Routine Description:
  299. This routine closes an open file, invalidating any memory mappings to it.
  300. Arguments:
  301. File - Supplies a pointer to the file information.
  302. Return Value:
  303. None.
  304. --*/
  305. {
  306. PBOOT_FILE_HANDLE BootFileHandle;
  307. BootFileHandle = (PBOOT_FILE_HANDLE)(File->Handle);
  308. ASSERT(BootFileHandle->LoadedFileBuffer == NULL);
  309. if (BootFileHandle->FileName != NULL) {
  310. BoFreeMemory(BootFileHandle->FileName);
  311. }
  312. BoFreeMemory(BootFileHandle);
  313. return;
  314. }
  315. KSTATUS
  316. BmpImLoadFile (
  317. PIMAGE_FILE_INFORMATION File,
  318. PIMAGE_BUFFER Buffer
  319. )
  320. /*++
  321. Routine Description:
  322. This routine loads an entire file into memory so the image library can
  323. access it.
  324. Arguments:
  325. File - Supplies a pointer to the file information.
  326. Buffer - Supplies a pointer where the buffer will be returned on success.
  327. Return Value:
  328. Status code.
  329. --*/
  330. {
  331. PBOOT_FILE_HANDLE BootFileHandle;
  332. KSTATUS Status;
  333. BootFileHandle = (PBOOT_FILE_HANDLE)(File->Handle);
  334. if (BootFileHandle->LoadedFileBuffer == NULL) {
  335. Status = BoLoadFile(BmBootDevice,
  336. &BmSystemDirectoryId,
  337. BootFileHandle->FileName,
  338. &(BootFileHandle->LoadedFileBuffer),
  339. NULL,
  340. NULL);
  341. if (!KSUCCESS(Status)) {
  342. goto ImLoadFileEnd;
  343. }
  344. }
  345. Status = STATUS_SUCCESS;
  346. ImLoadFileEnd:
  347. if (KSUCCESS(Status)) {
  348. Buffer->Data = BootFileHandle->LoadedFileBuffer;
  349. Buffer->Size = BootFileHandle->FileSize;
  350. }
  351. return Status;
  352. }
  353. VOID
  354. BmpImUnloadBuffer (
  355. PIMAGE_FILE_INFORMATION File,
  356. PIMAGE_BUFFER Buffer
  357. )
  358. /*++
  359. Routine Description:
  360. This routine unloads a file and frees the buffer associated with a load
  361. image call.
  362. Arguments:
  363. File - Supplies a pointer to the file information.
  364. Buffer - Supplies the buffer returned by the load file function.
  365. Return Value:
  366. None.
  367. --*/
  368. {
  369. PBOOT_FILE_HANDLE BootFileHandle;
  370. BootFileHandle = (PBOOT_FILE_HANDLE)(File->Handle);
  371. if (BootFileHandle->LoadedFileBuffer != NULL) {
  372. BoFreeMemory(BootFileHandle->LoadedFileBuffer);
  373. BootFileHandle->LoadedFileBuffer = NULL;
  374. }
  375. return;
  376. }
  377. KSTATUS
  378. BmpImAllocateAddressSpace (
  379. PLOADED_IMAGE Image
  380. )
  381. /*++
  382. Routine Description:
  383. This routine allocates a section of virtual address space that an image
  384. can be mapped in to.
  385. Arguments:
  386. Image - Supplies a pointer to the image being loaded. The system context,
  387. size, file information, load flags, and preferred virtual address will
  388. be initialized. This routine should set up the loaded image buffer,
  389. loaded lowest address, and allocator handle if needed.
  390. Return Value:
  391. Status code.
  392. --*/
  393. {
  394. UINTN AlignedSize;
  395. UINTN PageOffset;
  396. UINTN PageSize;
  397. PHYSICAL_ADDRESS PhysicalAddress;
  398. PVOID PreferredAddress;
  399. KSTATUS Status;
  400. PreferredAddress = Image->PreferredLowestAddress;
  401. PageSize = MmPageSize();
  402. PageOffset = (UINTN)PreferredAddress -
  403. ALIGN_RANGE_DOWN((UINTN)PreferredAddress, PageSize);
  404. AlignedSize = ALIGN_RANGE_UP(Image->Size + PageOffset, PageSize);
  405. //
  406. // Allocate pages from the boot environment. This memory backs a boot
  407. // application image, so it is marked as loader temporary.
  408. //
  409. Status = FwAllocatePages(&PhysicalAddress,
  410. AlignedSize,
  411. PageSize,
  412. MemoryTypeLoaderTemporary);
  413. if (!KSUCCESS(Status)) {
  414. goto AllocateAddressSpaceEnd;
  415. }
  416. ASSERT((UINTN)PhysicalAddress == PhysicalAddress);
  417. Image->LoadedLowestAddress = (PVOID)(UINTN)PhysicalAddress + PageOffset;
  418. Image->LoadedImageBuffer = Image->LoadedLowestAddress;
  419. AllocateAddressSpaceEnd:
  420. return Status;
  421. }
  422. VOID
  423. BmpImFreeAddressSpace (
  424. PLOADED_IMAGE Image
  425. )
  426. /*++
  427. Routine Description:
  428. This routine frees a section of virtual address space that was previously
  429. allocated.
  430. Arguments:
  431. Image - Supplies a pointer to the loaded (or partially loaded) image.
  432. Return Value:
  433. None.
  434. --*/
  435. {
  436. return;
  437. }
  438. KSTATUS
  439. BmpImMapImageSegment (
  440. HANDLE AddressSpaceHandle,
  441. PVOID AddressSpaceAllocation,
  442. PIMAGE_FILE_INFORMATION File,
  443. ULONGLONG FileOffset,
  444. PIMAGE_SEGMENT Segment,
  445. PIMAGE_SEGMENT PreviousSegment
  446. )
  447. /*++
  448. Routine Description:
  449. This routine maps a section of the image to the given virtual address.
  450. Arguments:
  451. AddressSpaceHandle - Supplies the handle used to claim the overall region
  452. of address space.
  453. AddressSpaceAllocation - Supplies the original lowest virtual address for
  454. this image.
  455. File - Supplies an optional pointer to the file being mapped. If this
  456. parameter is NULL, then a zeroed memory section is being mapped.
  457. FileOffset - Supplies the offset from the beginning of the file to the
  458. beginning of the mapping, in bytes.
  459. Segment - Supplies a pointer to the segment information to map. On output,
  460. the virtual address will contain the actual mapped address, and the
  461. mapping handle may be set.
  462. PreviousSegment - Supplies an optional pointer to the previous segment
  463. that was mapped, so this routine can handle overlap appropriately. This
  464. routine can assume that segments are always mapped in increasing order.
  465. Return Value:
  466. Status code.
  467. --*/
  468. {
  469. PBOOT_FILE_HANDLE BootFileHandle;
  470. PVOID Source;
  471. BootFileHandle = NULL;
  472. if (File != NULL) {
  473. BootFileHandle = (PBOOT_FILE_HANDLE)(File->Handle);
  474. }
  475. //
  476. // Copy from the file buffer plus the given offset.
  477. //
  478. if (Segment->FileSize != 0) {
  479. ASSERT((BootFileHandle != NULL) &&
  480. (FileOffset + Segment->FileSize < BootFileHandle->FileSize));
  481. Source = (PUCHAR)(BootFileHandle->LoadedFileBuffer) + FileOffset;
  482. RtlCopyMemory(Segment->VirtualAddress, Source, Segment->FileSize);
  483. }
  484. if (Segment->MemorySize > Segment->FileSize) {
  485. RtlZeroMemory(Segment->VirtualAddress + Segment->FileSize,
  486. Segment->MemorySize - Segment->FileSize);
  487. }
  488. return STATUS_SUCCESS;
  489. }
  490. VOID
  491. BmpImUnmapImageSegment (
  492. HANDLE AddressSpaceHandle,
  493. PIMAGE_SEGMENT Segment
  494. )
  495. /*++
  496. Routine Description:
  497. This routine maps unmaps an image segment.
  498. Arguments:
  499. AddressSpaceHandle - Supplies the handle used to claim the overall region
  500. of address space.
  501. Segment - Supplies a pointer to the segment information to unmap.
  502. Return Value:
  503. None.
  504. --*/
  505. {
  506. //
  507. // Unmapping is currently not implemented.
  508. //
  509. return;
  510. }
  511. KSTATUS
  512. BmpImNotifyImageLoad (
  513. PLOADED_IMAGE Image
  514. )
  515. /*++
  516. Routine Description:
  517. This routine notifies the primary consumer of the image library that an
  518. image has been loaded.
  519. Arguments:
  520. Image - Supplies the image that has just been loaded. This image should
  521. be subsequently returned to the image library upon requests for loaded
  522. images with the given name.
  523. Return Value:
  524. Status code. Failing status codes veto the image load.
  525. --*/
  526. {
  527. ULONG AllocationSize;
  528. UINTN BaseDifference;
  529. PDEBUG_MODULE LoadedModule;
  530. ULONG NameSize;
  531. KSTATUS Status;
  532. NameSize = RtlStringLength(Image->BinaryName) + 1;
  533. AllocationSize = sizeof(DEBUG_MODULE) +
  534. ((NameSize - ANYSIZE_ARRAY) * sizeof(CHAR));
  535. LoadedModule = BoAllocateMemory(AllocationSize);
  536. if (LoadedModule == NULL) {
  537. Status = STATUS_INSUFFICIENT_RESOURCES;
  538. goto NotifyImageLoadEnd;
  539. }
  540. RtlZeroMemory(LoadedModule, AllocationSize);
  541. //
  542. // Initialize the loaded image parameters.
  543. //
  544. RtlStringCopy(LoadedModule->BinaryName, Image->BinaryName, NameSize);
  545. LoadedModule->StructureSize = AllocationSize;
  546. LoadedModule->Timestamp = Image->File.ModificationDate;
  547. BaseDifference = Image->LoadedLowestAddress - Image->PreferredLowestAddress;
  548. LoadedModule->BaseAddress = Image->DeclaredBase + BaseDifference;
  549. LoadedModule->LowestAddress = Image->LoadedLowestAddress;
  550. LoadedModule->Size = Image->Size;
  551. LoadedModule->EntryPoint = Image->EntryPoint;
  552. LoadedModule->Image = Image;
  553. Image->DebuggerModule = LoadedModule;
  554. KdReportModuleChange(LoadedModule, TRUE);
  555. Status = STATUS_SUCCESS;
  556. NotifyImageLoadEnd:
  557. if (!KSUCCESS(Status)) {
  558. if (LoadedModule != NULL) {
  559. BoFreeMemory(LoadedModule);
  560. }
  561. }
  562. return Status;
  563. }
  564. VOID
  565. BmpImNotifyImageUnload (
  566. PLOADED_IMAGE Image
  567. )
  568. /*++
  569. Routine Description:
  570. This routine notifies the primary consumer of the image library that an
  571. image is about to be unloaded from memory. Once this routine returns, the
  572. image should not be referenced again as it will be freed.
  573. Arguments:
  574. Image - Supplies the image that is about to be unloaded.
  575. Return Value:
  576. None.
  577. --*/
  578. {
  579. PDEBUG_MODULE UnloadingModule;
  580. UnloadingModule = Image->DebuggerModule;
  581. Image->DebuggerModule = NULL;
  582. KdReportModuleChange(UnloadingModule, FALSE);
  583. BoFreeMemory(UnloadingModule);
  584. return;
  585. }
  586. VOID
  587. BmpImInvalidateInstructionCacheRegion (
  588. PVOID Address,
  589. ULONG Size
  590. )
  591. /*++
  592. Routine Description:
  593. This routine invalidates an instruction cache region after code has been
  594. modified.
  595. Arguments:
  596. Address - Supplies the virtual address of the revion to invalidate.
  597. Size - Supplies the number of bytes to invalidate.
  598. Return Value:
  599. None.
  600. --*/
  601. {
  602. return;
  603. }