12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136 |
- /*++
- Copyright (c) 2012 Minoca Corp.
- This file is licensed under the terms of the GNU General Public License
- version 3. Alternative licensing terms are available. Contact
- info@minocacorp.com for details. See the LICENSE file at the root of this
- project for complete licensing information.
- Module Name:
- bootim.c
- Abstract:
- This module implements the underlying support routines for the image
- library to be run in the boot environment.
- Author:
- Evan Green 13-Oct-2012
- Environment:
- Boot
- --*/
- //
- // ------------------------------------------------------------------- Includes
- //
- #include <minoca/kernel/kernel.h>
- #include <minoca/lib/fat/fat.h>
- #include "firmware.h"
- #include "bootlib.h"
- #include "loader.h"
- #include "paging.h"
- //
- // ---------------------------------------------------------------- Definitions
- //
- //
- // ------------------------------------------------------ Data Type Definitions
- //
- /*++
- Structure Description:
- This structure stores information about an opened file in the boot
- environment.
- Members:
- FileName - Stores a pointer to the name of the file.
- FileNameSize - Stores the size of the file name string including the null
- terminator.
- LoadedFileBuffer - Stores a pointer to the buffer containing the file.
- FileSize - Stores the size of the loaded file, in bytes.
- --*/
- typedef struct _BOOT_FILE_HANDLE {
- PSTR FileName;
- ULONG FileNameSize;
- PVOID LoadedFileBuffer;
- UINTN FileSize;
- } BOOT_FILE_HANDLE, *PBOOT_FILE_HANDLE;
- /*++
- Structure Description:
- This structure stores information about an allocation of virtual address
- space by the boot environment.
- Members:
- PhysicalAddress - Stores the physical address of the memory backing the
- allocation.
- VirtualAddress - Stores the virtual address of the allocation.
- --*/
- typedef struct _BOOT_ADDRESS_SPACE_ALLOCATION {
- PHYSICAL_ADDRESS PhysicalAddress;
- PVOID VirtualAddress;
- } BOOT_ADDRESS_SPACE_ALLOCATION, *PBOOT_ADDRESS_SPACE_ALLOCATION;
- //
- // ----------------------------------------------- Internal Function Prototypes
- //
- PVOID
- BopImAllocateMemory (
- ULONG Size,
- ULONG Tag
- );
- VOID
- BopImFreeMemory (
- PVOID Allocation
- );
- KSTATUS
- BopImOpenFile (
- PVOID SystemContext,
- PCSTR BinaryName,
- PIMAGE_FILE_INFORMATION File
- );
- VOID
- BopImCloseFile (
- PIMAGE_FILE_INFORMATION File
- );
- KSTATUS
- BopImLoadFile (
- PIMAGE_FILE_INFORMATION File,
- PIMAGE_BUFFER Buffer
- );
- VOID
- BopImUnloadBuffer (
- PIMAGE_FILE_INFORMATION File,
- PIMAGE_BUFFER Buffer
- );
- KSTATUS
- BopImAllocateAddressSpace (
- PLOADED_IMAGE Image
- );
- VOID
- BopImFreeAddressSpace (
- PLOADED_IMAGE Image
- );
- KSTATUS
- BopImMapImageSegment (
- HANDLE AddressSpaceHandle,
- PVOID AddressSpaceAllocation,
- PIMAGE_FILE_INFORMATION File,
- ULONGLONG FileOffset,
- PIMAGE_SEGMENT Segment,
- PIMAGE_SEGMENT PreviousSegment
- );
- VOID
- BopImUnmapImageSegment (
- HANDLE AddressSpaceHandle,
- PIMAGE_SEGMENT Segment
- );
- KSTATUS
- BopImNotifyImageLoad (
- PLOADED_IMAGE Image
- );
- VOID
- BopImNotifyImageUnload (
- PLOADED_IMAGE Image
- );
- VOID
- BopImInvalidateInstructionCacheRegion (
- PVOID Address,
- ULONG Size
- );
- PSTR
- BopImGetEnvironmentVariable (
- PSTR Variable
- );
- KSTATUS
- BopImFinalizeSegments (
- HANDLE AddressSpaceHandle,
- PIMAGE_SEGMENT Segments,
- UINTN SegmentCount
- );
- //
- // -------------------------------------------------------------------- Globals
- //
- //
- // Store a pointer to the boot device.
- //
- PBOOT_VOLUME BoBootDevice;
- //
- // Store the IDs of the directories to try when opening an image file.
- //
- FILE_ID BoDriversDirectoryId;
- FILE_ID BoSystemDirectoryId;
- //
- // Define the image library function table.
- //
- IM_IMPORT_TABLE BoImageFunctionTable = {
- BopImAllocateMemory,
- BopImFreeMemory,
- BopImOpenFile,
- BopImCloseFile,
- BopImLoadFile,
- NULL,
- BopImUnloadBuffer,
- BopImAllocateAddressSpace,
- BopImFreeAddressSpace,
- BopImMapImageSegment,
- BopImUnmapImageSegment,
- BopImNotifyImageLoad,
- BopImNotifyImageUnload,
- BopImInvalidateInstructionCacheRegion,
- BopImGetEnvironmentVariable,
- BopImFinalizeSegments,
- NULL
- };
- //
- // ------------------------------------------------------------------ Functions
- //
- KSTATUS
- BoInitializeImageSupport (
- PBOOT_VOLUME BootDevice,
- PBOOT_ENTRY BootEntry
- )
- /*++
- Routine Description:
- This routine initializes the image library for use in the boot
- environment.
- Arguments:
- BootDevice - Supplies a pointer to the boot volume token, used for loading
- images from disk.
- BootEntry - Supplies a pointer to the boot entry being launched.
- Return Value:
- Status code.
- --*/
- {
- PSTR DriversDirectoryPath;
- FILE_PROPERTIES Properties;
- KSTATUS Status;
- PCSTR SystemRootPath;
- INITIALIZE_LIST_HEAD(&BoLoadedImageList);
- //
- // Save the boot volume.
- //
- BoBootDevice = BootDevice;
- //
- // Open up the system root.
- //
- SystemRootPath = DEFAULT_SYSTEM_ROOT_PATH;
- if (BootEntry != NULL) {
- SystemRootPath = BootEntry->SystemPath;
- }
- Status = BoLookupPath(BootDevice, NULL, SystemRootPath, &Properties);
- if (!KSUCCESS(Status)) {
- goto InitializeImageSupportEnd;
- }
- BoSystemDirectoryId = Properties.FileId;
- //
- // Open up the drivers directory.
- //
- DriversDirectoryPath = DEFAULT_DRIVERS_DIRECTORY_PATH;
- Status = BoLookupPath(BootDevice,
- &BoSystemDirectoryId,
- DriversDirectoryPath,
- &Properties);
- if (!KSUCCESS(Status)) {
- goto InitializeImageSupportEnd;
- }
- BoDriversDirectoryId = Properties.FileId;
- Status = ImInitialize(&BoImageFunctionTable);
- if (!KSUCCESS(Status)) {
- goto InitializeImageSupportEnd;
- }
- Status = STATUS_SUCCESS;
- InitializeImageSupportEnd:
- return Status;
- }
- //
- // --------------------------------------------------------- Internal Functions
- //
- PVOID
- BopImAllocateMemory (
- ULONG Size,
- ULONG Tag
- )
- /*++
- Routine Description:
- This routine allocates memory from the boot environment for the image
- library.
- Arguments:
- Size - Supplies the number of bytes required for the memory allocation.
- Tag - Supplies a 32-bit ASCII identifier used to tag the memroy allocation.
- Return Value:
- Returns a pointer to the memory allocation on success.
- NULL on failure.
- --*/
- {
- return BoAllocateMemory(Size);
- }
- VOID
- BopImFreeMemory (
- PVOID Allocation
- )
- /*++
- Routine Description:
- This routine frees memory to the boot environment allocated by the image
- library.
- Arguments:
- Allocation - Supplies a pointer the allocation to free.
- Return Value:
- None.
- --*/
- {
- BoFreeMemory(Allocation);
- return;
- }
- KSTATUS
- BopImOpenFile (
- PVOID SystemContext,
- PCSTR BinaryName,
- PIMAGE_FILE_INFORMATION File
- )
- /*++
- Routine Description:
- This routine opens a file.
- Arguments:
- SystemContext - Supplies the context pointer passed to the load executable
- function.
- BinaryName - Supplies the name of the executable image to open.
- File - Supplies a pointer where the information for the file including its
- open handle will be returned.
- Return Value:
- Status code.
- --*/
- {
- PBOOT_FILE_HANDLE BootFileHandle;
- ULONGLONG LocalFileSize;
- FILE_PROPERTIES Properties;
- KSTATUS Status;
- BootFileHandle = BoAllocateMemory(sizeof(BOOT_FILE_HANDLE));
- if (BootFileHandle == NULL) {
- Status = STATUS_INSUFFICIENT_RESOURCES;
- goto OpenFileEnd;
- }
- RtlZeroMemory(BootFileHandle, sizeof(BOOT_FILE_HANDLE));
- BootFileHandle->FileNameSize = RtlStringLength(BinaryName) + 1;
- BootFileHandle->FileName = BoAllocateMemory(BootFileHandle->FileNameSize);
- if (BootFileHandle->FileName == NULL) {
- Status = STATUS_INSUFFICIENT_RESOURCES;
- goto OpenFileEnd;
- }
- RtlCopyMemory(BootFileHandle->FileName,
- BinaryName,
- BootFileHandle->FileNameSize);
- //
- // Open the file enough to make sure it's there, but don't actually load
- // it just now.
- //
- Status = BoLookupPath(BoBootDevice,
- &BoSystemDirectoryId,
- BootFileHandle->FileName,
- &Properties);
- if (Status == STATUS_PATH_NOT_FOUND) {
- Status = BoLookupPath(BoBootDevice,
- &BoDriversDirectoryId,
- BootFileHandle->FileName,
- &Properties);
- }
- if (!KSUCCESS(Status)) {
- goto OpenFileEnd;
- }
- if (Properties.Type != IoObjectRegularFile) {
- Status = STATUS_FILE_IS_DIRECTORY;
- goto OpenFileEnd;
- }
- LocalFileSize = Properties.Size;
- if ((UINTN)LocalFileSize != LocalFileSize) {
- Status = STATUS_FILE_CORRUPT;
- goto OpenFileEnd;
- }
- BootFileHandle->FileSize = (UINTN)LocalFileSize;
- File->Size = LocalFileSize;
- File->ModificationDate = Properties.ModifiedTime.Seconds;
- File->DeviceId = 0;
- File->FileId = 0;
- OpenFileEnd:
- if (!KSUCCESS(Status)) {
- if (BootFileHandle != NULL) {
- if (BootFileHandle->FileName != NULL) {
- BoFreeMemory(BootFileHandle->FileName);
- }
- BoFreeMemory(BootFileHandle);
- BootFileHandle = INVALID_HANDLE;
- }
- }
- File->Handle = BootFileHandle;
- return Status;
- }
- VOID
- BopImCloseFile (
- PIMAGE_FILE_INFORMATION File
- )
- /*++
- Routine Description:
- This routine closes an open file, invalidating any memory mappings to it.
- Arguments:
- File - Supplies a pointer to the file information.
- Return Value:
- None.
- --*/
- {
- PBOOT_FILE_HANDLE BootFileHandle;
- BootFileHandle = (PBOOT_FILE_HANDLE)(File->Handle);
- ASSERT(BootFileHandle->LoadedFileBuffer == NULL);
- if (BootFileHandle->FileName != NULL) {
- BoFreeMemory(BootFileHandle->FileName);
- }
- BoFreeMemory(BootFileHandle);
- return;
- }
- KSTATUS
- BopImLoadFile (
- PIMAGE_FILE_INFORMATION File,
- PIMAGE_BUFFER Buffer
- )
- /*++
- Routine Description:
- This routine loads an entire file into memory so the image library can
- access it.
- Arguments:
- File - Supplies a pointer to the file information.
- Buffer - Supplies a pointer where the buffer will be returned on success.
- Return Value:
- Status code.
- --*/
- {
- PBOOT_FILE_HANDLE BootFileHandle;
- KSTATUS Status;
- BootFileHandle = (PBOOT_FILE_HANDLE)(File->Handle);
- if (BootFileHandle->LoadedFileBuffer == NULL) {
- Status = BoLoadFile(BoBootDevice,
- &BoSystemDirectoryId,
- BootFileHandle->FileName,
- &(BootFileHandle->LoadedFileBuffer),
- NULL,
- NULL);
- if (Status == STATUS_PATH_NOT_FOUND) {
- Status = BoLoadFile(BoBootDevice,
- &BoDriversDirectoryId,
- BootFileHandle->FileName,
- &(BootFileHandle->LoadedFileBuffer),
- NULL,
- NULL);
- }
- if (!KSUCCESS(Status)) {
- goto ImLoadFileEnd;
- }
- }
- Status = STATUS_SUCCESS;
- ImLoadFileEnd:
- if (KSUCCESS(Status)) {
- Buffer->Data = BootFileHandle->LoadedFileBuffer;
- Buffer->Size = BootFileHandle->FileSize;
- }
- return Status;
- }
- VOID
- BopImUnloadBuffer (
- PIMAGE_FILE_INFORMATION File,
- PIMAGE_BUFFER Buffer
- )
- /*++
- Routine Description:
- This routine unloads a file buffer created from either the load file or
- read file function, and frees the buffer.
- Arguments:
- File - Supplies a pointer to the file information.
- Buffer - Supplies the buffer returned by the load file function.
- Return Value:
- None.
- --*/
- {
- PBOOT_FILE_HANDLE BootFileHandle;
- BootFileHandle = (PBOOT_FILE_HANDLE)(File->Handle);
- if (BootFileHandle->LoadedFileBuffer != NULL) {
- BoFreeMemory(BootFileHandle->LoadedFileBuffer);
- BootFileHandle->LoadedFileBuffer = NULL;
- }
- return;
- }
- KSTATUS
- BopImAllocateAddressSpace (
- PLOADED_IMAGE Image
- )
- /*++
- Routine Description:
- This routine allocates a section of virtual address space that an image
- can be mapped in to.
- Arguments:
- Image - Supplies a pointer to the image being loaded. The system context,
- size, file information, load flags, and preferred virtual address will
- be initialized. This routine should set up the loaded image buffer,
- loaded lowest address, and allocator handle if needed.
- Return Value:
- Status code.
- --*/
- {
- UINTN AlignedSize;
- PBOOT_ADDRESS_SPACE_ALLOCATION Allocation;
- UINTN PageOffset;
- UINTN PageSize;
- PVOID PreferredAddress;
- KSTATUS Status;
- Image->AllocatorHandle = INVALID_HANDLE;
- PreferredAddress = Image->PreferredLowestAddress;
- Allocation = BoAllocateMemory(sizeof(BOOT_ADDRESS_SPACE_ALLOCATION));
- if (Allocation == NULL) {
- Status = STATUS_INSUFFICIENT_RESOURCES;
- goto AllocateAddressSpaceEnd;
- }
- RtlZeroMemory(Allocation, sizeof(BOOT_ADDRESS_SPACE_ALLOCATION));
- PageSize = MmPageSize();
- PageOffset = (UINTN)PreferredAddress -
- ALIGN_RANGE_DOWN((UINTN)PreferredAddress, PageSize);
- AlignedSize = ALIGN_RANGE_UP(Image->Size + PageOffset, PageSize);
- //
- // Allocate pages from the boot environment. This memory backs a boot
- // driver image, so it is marked as loader permanent.
- //
- Status = FwAllocatePages(&(Allocation->PhysicalAddress),
- AlignedSize,
- PageSize,
- MemoryTypeLoaderPermanent);
- if (!KSUCCESS(Status)) {
- goto AllocateAddressSpaceEnd;
- }
- //
- // Map the memory to find out where it lands in virtual space.
- //
- Allocation->VirtualAddress = (PVOID)-1;
- Status = BoMapPhysicalAddress(&(Allocation->VirtualAddress),
- Allocation->PhysicalAddress,
- AlignedSize,
- MAP_FLAG_GLOBAL | MAP_FLAG_EXECUTE,
- MemoryTypeLoaderPermanent);
- if (!KSUCCESS(Status)) {
- goto AllocateAddressSpaceEnd;
- }
- AllocateAddressSpaceEnd:
- if (!KSUCCESS(Status)) {
- if (Allocation != NULL) {
- BoFreeMemory(Allocation);
- }
- } else {
- Image->AllocatorHandle = Allocation;
- Image->LoadedImageBuffer = (PVOID)(UINTN)Allocation->PhysicalAddress +
- PageOffset;
- Image->BaseDifference = Allocation->VirtualAddress + PageOffset -
- Image->PreferredLowestAddress;
- ASSERT((UINTN)Allocation->PhysicalAddress ==
- Allocation->PhysicalAddress);
- }
- return Status;
- }
- VOID
- BopImFreeAddressSpace (
- PLOADED_IMAGE Image
- )
- /*++
- Routine Description:
- This routine frees a section of virtual address space that was previously
- allocated.
- Arguments:
- Image - Supplies a pointer to the loaded (or partially loaded) image.
- Return Value:
- None.
- --*/
- {
- BoFreeMemory((PBOOT_ADDRESS_SPACE_ALLOCATION)(Image->AllocatorHandle));
- return;
- }
- KSTATUS
- BopImMapImageSegment (
- HANDLE AddressSpaceHandle,
- PVOID AddressSpaceAllocation,
- PIMAGE_FILE_INFORMATION File,
- ULONGLONG FileOffset,
- PIMAGE_SEGMENT Segment,
- PIMAGE_SEGMENT PreviousSegment
- )
- /*++
- Routine Description:
- This routine maps a section of the image to the given virtual address.
- Arguments:
- AddressSpaceHandle - Supplies the handle used to claim the overall region
- of address space.
- AddressSpaceAllocation - Supplies the original lowest virtual address for
- this image.
- File - Supplies an optional pointer to the file being mapped. If this
- parameter is NULL, then a zeroed memory section is being mapped.
- FileOffset - Supplies the offset from the beginning of the file to the
- beginning of the mapping, in bytes.
- Segment - Supplies a pointer to the segment information to map. On output,
- the virtual address will contain the actual mapped address, and the
- mapping handle may be set.
- PreviousSegment - Supplies an optional pointer to the previous segment
- that was mapped, so this routine can handle overlap appropriately. This
- routine can assume that segments are always mapped in increasing order.
- Return Value:
- Status code.
- --*/
- {
- PBOOT_FILE_HANDLE BootFileHandle;
- PVOID Destination;
- PBOOT_ADDRESS_SPACE_ALLOCATION Region;
- PVOID Source;
- Region = (PBOOT_ADDRESS_SPACE_ALLOCATION)AddressSpaceHandle;
- BootFileHandle = NULL;
- if (File != NULL) {
- BootFileHandle = (PBOOT_FILE_HANDLE)(File->Handle);
- }
- //
- // Copy to the physical address of the buffer plus the offset from the
- // base VA corresponding to that physical address.
- //
- ASSERT((UINTN)Region->PhysicalAddress == Region->PhysicalAddress);
- Destination = (PUCHAR)(UINTN)Region->PhysicalAddress +
- (UINTN)Segment->VirtualAddress -
- (UINTN)Region->VirtualAddress;
- //
- // Copy from the file buffer plus the given offset.
- //
- if (Segment->FileSize != 0) {
- ASSERT((BootFileHandle != NULL) &&
- (FileOffset + Segment->FileSize < BootFileHandle->FileSize));
- Source = (PUCHAR)(BootFileHandle->LoadedFileBuffer) + FileOffset;
- RtlCopyMemory(Destination, Source, Segment->FileSize);
- }
- if (Segment->MemorySize > Segment->FileSize) {
- RtlZeroMemory(Destination + Segment->FileSize,
- Segment->MemorySize - Segment->FileSize);
- }
- return STATUS_SUCCESS;
- }
- VOID
- BopImUnmapImageSegment (
- HANDLE AddressSpaceHandle,
- PIMAGE_SEGMENT Segment
- )
- /*++
- Routine Description:
- This routine maps unmaps an image segment.
- Arguments:
- AddressSpaceHandle - Supplies the handle used to claim the overall region
- of address space.
- Segment - Supplies a pointer to the segment information to unmap.
- Return Value:
- None.
- --*/
- {
- //
- // Unmapping is currently not implemented.
- //
- return;
- }
- KSTATUS
- BopImNotifyImageLoad (
- PLOADED_IMAGE Image
- )
- /*++
- Routine Description:
- This routine notifies the primary consumer of the image library that an
- image has been loaded.
- Arguments:
- Image - Supplies the image that has just been loaded. This image should
- be subsequently returned to the image library upon requests for loaded
- images with the given name.
- Return Value:
- Status code. Failing status codes veto the image load.
- --*/
- {
- ULONG AllocationSize;
- PSTR FileName;
- PDEBUG_MODULE LoadedModule;
- ULONG NameSize;
- KSTATUS Status;
- FileName = RtlStringFindCharacterRight(Image->FileName, '/', -1);
- if (FileName != NULL) {
- FileName += 1;
- } else {
- FileName = Image->FileName;
- }
- NameSize = RtlStringLength(FileName) + 1;
- AllocationSize = sizeof(DEBUG_MODULE) +
- ((NameSize - ANYSIZE_ARRAY) * sizeof(CHAR));
- LoadedModule = BoAllocateMemory(AllocationSize);
- if (LoadedModule == NULL) {
- Status = STATUS_INSUFFICIENT_RESOURCES;
- goto NotifyImageLoadEnd;
- }
- RtlZeroMemory(LoadedModule, AllocationSize);
- //
- // Initialize the loaded image parameters.
- //
- RtlStringCopy(LoadedModule->BinaryName, FileName, NameSize);
- LoadedModule->StructureSize = AllocationSize;
- LoadedModule->Timestamp = Image->File.ModificationDate;
- LoadedModule->LowestAddress = Image->PreferredLowestAddress +
- Image->BaseDifference;
- LoadedModule->Size = Image->Size;
- LoadedModule->EntryPoint = Image->EntryPoint;
- LoadedModule->Image = Image;
- Image->DebuggerModule = LoadedModule;
- KdReportModuleChange(LoadedModule, TRUE);
- Status = STATUS_SUCCESS;
- NotifyImageLoadEnd:
- if (!KSUCCESS(Status)) {
- if (LoadedModule != NULL) {
- BoFreeMemory(LoadedModule);
- }
- }
- return Status;
- }
- VOID
- BopImNotifyImageUnload (
- PLOADED_IMAGE Image
- )
- /*++
- Routine Description:
- This routine notifies the primary consumer of the image library that an
- image is about to be unloaded from memory. Once this routine returns, the
- image should not be referenced again as it will be freed.
- Arguments:
- Image - Supplies the image that is about to be unloaded.
- Return Value:
- None.
- --*/
- {
- PDEBUG_MODULE UnloadingModule;
- UnloadingModule = Image->DebuggerModule;
- Image->DebuggerModule = NULL;
- KdReportModuleChange(UnloadingModule, FALSE);
- BoFreeMemory(UnloadingModule);
- return;
- }
- VOID
- BopImInvalidateInstructionCacheRegion (
- PVOID Address,
- ULONG Size
- )
- /*++
- Routine Description:
- This routine invalidates an instruction cache region after code has been
- modified.
- Arguments:
- Address - Supplies the virtual address of the revion to invalidate.
- Size - Supplies the number of bytes to invalidate.
- Return Value:
- None.
- --*/
- {
- return;
- }
- PSTR
- BopImGetEnvironmentVariable (
- PSTR Variable
- )
- /*++
- Routine Description:
- This routine gets an environment variable value for the image library.
- Arguments:
- Variable - Supplies a pointer to a null terminated string containing the
- name of the variable to get.
- Return Value:
- Returns a pointer to the value of the environment variable. The image
- library will not free or modify this value.
- NULL if the given environment variable is not set.
- --*/
- {
- if (RtlAreStringsEqual(Variable, IMAGE_LOAD_LIBRARY_PATH_VARIABLE, -1) !=
- FALSE) {
- return ".";
- }
- return NULL;
- }
- KSTATUS
- BopImFinalizeSegments (
- HANDLE AddressSpaceHandle,
- PIMAGE_SEGMENT Segments,
- UINTN SegmentCount
- )
- /*++
- Routine Description:
- This routine applies the final memory protection attributes to the given
- segments. Read and execute bits can be applied at the time of mapping, but
- write protection may be applied here.
- Arguments:
- AddressSpaceHandle - Supplies the handle used to claim the overall region
- of address space.
- Segments - Supplies the final array of segments.
- SegmentCount - Supplies the number of segments.
- Return Value:
- Status code.
- --*/
- {
- UINTN End;
- ULONG MapFlags;
- UINTN PageSize;
- PIMAGE_SEGMENT Segment;
- UINTN SegmentIndex;
- UINTN Size;
- End = 0;
- PageSize = MmPageSize();
- for (SegmentIndex = 0; SegmentIndex < SegmentCount; SegmentIndex += 1) {
- Segment = &(Segments[SegmentIndex]);
- if (Segment->Type == ImageSegmentInvalid) {
- continue;
- }
- //
- // If the segment has no protection features, then there's nothing to
- // tighten up.
- //
- if ((Segment->Flags & IMAGE_MAP_FLAG_WRITE) != 0) {
- continue;
- }
- //
- // Compute the region whose protection should actually be changed.
- //
- End = (UINTN)(Segment->VirtualAddress) + Segment->MemorySize;
- End = ALIGN_RANGE_UP(End, PageSize);
- //
- // If the region has a real size, change its protection to read-only.
- //
- if (End > (UINTN)Segment->VirtualAddress) {
- Size = End - (UINTN)Segment->VirtualAddress;
- MapFlags = (MAP_FLAG_READ_ONLY << MAP_FLAG_PROTECT_SHIFT) |
- MAP_FLAG_READ_ONLY;
- BoChangeMappingAttributes(Segment->VirtualAddress, Size, MapFlags);
- }
- }
- return STATUS_SUCCESS;
- }
|