123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119 |
- /*++
- Copyright (c) 2012 Minoca Corp. All Rights Reserved
- 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,
- PSTR 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;
- PSTR 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,
- PSTR 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;
- }
- READ_INT64_SYNC(&(Properties.FileSize), &LocalFileSize);
- if ((UINTN)LocalFileSize != LocalFileSize) {
- Status = STATUS_FILE_CORRUPT;
- goto OpenFileEnd;
- }
- BootFileHandle->FileSize = (UINTN)LocalFileSize;
- File->Size = LocalFileSize;
- File->ModificationDate = Properties.ModifiedTime.Seconds;
- 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->LoadedLowestAddress = Allocation->VirtualAddress + PageOffset;
- ASSERT((UINTN)Allocation->PhysicalAddress ==
- Allocation->PhysicalAddress);
- Image->LoadedImageBuffer = (PVOID)(UINTN)Allocation->PhysicalAddress +
- PageOffset;
- }
- 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;
- UINTN BaseDifference;
- PDEBUG_MODULE LoadedModule;
- ULONG NameSize;
- KSTATUS Status;
- NameSize = RtlStringLength(Image->BinaryName) + 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, Image->BinaryName, NameSize);
- LoadedModule->StructureSize = AllocationSize;
- LoadedModule->Timestamp = Image->File.ModificationDate;
- BaseDifference = Image->LoadedLowestAddress - Image->PreferredLowestAddress;
- LoadedModule->BaseAddress = Image->DeclaredBase + BaseDifference;
- LoadedModule->LowestAddress = Image->LoadedLowestAddress;
- 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_DYNAMIC_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;
- }
|