BOOLEAN IniAddSection(PCSTR SectionName, ULONG_PTR* SectionId) { PINI_SECTION Section; // Allocate a new section structure Section = MmHeapAlloc(sizeof(INI_SECTION)); if (!Section) { return FALSE; } RtlZeroMemory(Section, sizeof(INI_SECTION)); // Allocate the section name buffer Section->SectionName = MmHeapAlloc(strlen(SectionName)); if (!Section->SectionName) { MmHeapFree(Section); return FALSE; } // Get the section name strcpy(Section->SectionName, SectionName); // Add it to the section list head IniFileSectionCount++; InsertHeadList(&IniFileSectionListHead, &Section->ListEntry); *SectionId = (ULONG_PTR)Section; return TRUE; }
VOID RegInitializeRegistry (VOID) { /* Create root key */ RootKey = MmHeapAlloc(sizeof(KEY)); InitializeListHead(&RootKey->SubKeyList); InitializeListHead(&RootKey->ValueList); InitializeListHead(&RootKey->KeyList); RootKey->SubKeyCount = 0; RootKey->ValueCount = 0; RootKey->NameSize = 4; RootKey->Name = MmHeapAlloc(4); wcscpy (RootKey->Name, L"\\"); RootKey->DataType = 0; RootKey->DataSize = 0; RootKey->Data = NULL; /* Create 'SYSTEM' key */ RegCreateKey (RootKey, L"Registry\\Machine\\SYSTEM", NULL); }
PCACHE_BLOCK CacheInternalAddBlockToCache(PCACHE_DRIVE CacheDrive, ULONG BlockNumber) { PCACHE_BLOCK CacheBlock = NULL; TRACE("CacheInternalAddBlockToCache() BlockNumber = %d\n", BlockNumber); // Check the size of the cache so we don't exceed our limits CacheInternalCheckCacheSizeLimits(CacheDrive); // We will need to add the block to the // drive's list of cached blocks. So allocate // the block memory. CacheBlock = MmHeapAlloc(sizeof(CACHE_BLOCK)); if (CacheBlock == NULL) { return NULL; } // Now initialize the structure and // allocate room for the block data RtlZeroMemory(CacheBlock, sizeof(CACHE_BLOCK)); CacheBlock->BlockNumber = BlockNumber; CacheBlock->BlockData = MmHeapAlloc(CacheDrive->BlockSize * CacheDrive->BytesPerSector); if (CacheBlock->BlockData ==NULL) { MmHeapFree(CacheBlock); return NULL; } // Now try to read in the block if (!MachDiskReadLogicalSectors(CacheDrive->DriveNumber, (BlockNumber * CacheDrive->BlockSize), CacheDrive->BlockSize, (PVOID)DISKREADBUFFER)) { MmHeapFree(CacheBlock->BlockData); MmHeapFree(CacheBlock); return NULL; } RtlCopyMemory(CacheBlock->BlockData, (PVOID)DISKREADBUFFER, CacheDrive->BlockSize * CacheDrive->BytesPerSector); // Add it to our list of blocks managed by the cache InsertTailList(&CacheDrive->CacheBlockHead, &CacheBlock->ListEntry); // Update the cache data CacheBlockCount++; CacheSizeCurrent = CacheBlockCount * (CacheDrive->BlockSize * CacheDrive->BytesPerSector); CacheInternalDumpBlockList(CacheDrive); return CacheBlock; }
PVOID NTAPI RtlpAllocateMemory(ULONG Bytes, ULONG Tag) { return MmHeapAlloc(Bytes); }
VOID NTAPI FldrCreateSystemKey(OUT PCONFIGURATION_COMPONENT_DATA *SystemNode) { PCONFIGURATION_COMPONENT Component; /* Allocate the root */ FldrArcHwTreeRoot = MmHeapAlloc(sizeof(CONFIGURATION_COMPONENT_DATA)); if (!FldrArcHwTreeRoot) return; /* Set it up */ Component = &FldrArcHwTreeRoot->ComponentEntry; Component->Class = SystemClass; Component->Type = MaximumType; Component->ConfigurationDataLength = 0; Component->Identifier = 0; Component->IdentifierLength = 0; Component->Flags = 0; Component->Version = 0; Component->Revision = 0; Component->Key = 0; Component->AffinityMask = 0xFFFFFFFF; /* Return the node */ *SystemNode = FldrArcHwTreeRoot; }
static PVOID InfpAddFieldToLine( PINFCACHELINE Line, PCHAR Data) { PINFCACHEFIELD Field; SIZE_T Size; Size = sizeof(INFCACHEFIELD) + strlen(Data); Field = (PINFCACHEFIELD)MmHeapAlloc(Size); if (Field == NULL) { return NULL; } memset(Field, 0, Size); strcpy(Field->Data, Data); /* Append key */ if (Line->FirstField == NULL) { Line->FirstField = Field; Line->LastField = Field; } else { Line->LastField->Next = Field; Field->Prev = Line->LastField; Line->LastField = Field; } Line->FieldCount++; return (PVOID)Field; }
LONG IsoOpen(CHAR* Path, OPENMODE OpenMode, ULONG* FileId) { ISO_FILE_INFO TempFileInfo; PISO_FILE_INFO FileHandle; ULONG DeviceId; LONG ret; if (OpenMode != OpenReadOnly) return EACCES; DeviceId = FsGetDeviceId(*FileId); TRACE("IsoOpen() FileName = %s\n", Path); RtlZeroMemory(&TempFileInfo, sizeof(TempFileInfo)); ret = IsoLookupFile(Path, DeviceId, &TempFileInfo); if (ret != ESUCCESS) return ENOENT; FileHandle = MmHeapAlloc(sizeof(ISO_FILE_INFO)); if (!FileHandle) return ENOMEM; RtlCopyMemory(FileHandle, &TempFileInfo, sizeof(ISO_FILE_INFO)); FsSetDeviceSpecific(*FileId, FileHandle); return ESUCCESS; }
VOID TuiFadeOut(VOID) { PPALETTE_ENTRY TuiFadePalette = NULL; if (UiUseSpecialEffects && ! MachVideoIsPaletteFixed()) { TuiFadePalette = (PPALETTE_ENTRY)MmHeapAlloc(sizeof(PALETTE_ENTRY) * 64); if (TuiFadePalette != NULL) { VideoSavePaletteState(TuiFadePalette, 64); } } if (UiUseSpecialEffects && ! MachVideoIsPaletteFixed() && TuiFadePalette != NULL) { VideoFadeOut(64); } MachVideoSetDisplayMode(NULL, FALSE); if (UiUseSpecialEffects && ! MachVideoIsPaletteFixed() && TuiFadePalette != NULL) { VideoRestorePaletteState(TuiFadePalette, 64); MmHeapFree(TuiFadePalette); } }
static PVOID NTAPI CmpAllocate (SIZE_T Size, BOOLEAN Paged, ULONG Tag) { return MmHeapAlloc(Size); }
VOID VideoFadeIn(PPALETTE_ENTRY Palette, ULONG ColorCount) { ULONG Index; UCHAR Color; PPALETTE_ENTRY PaletteColors; PaletteColors = MmHeapAlloc(sizeof(PALETTE_ENTRY) * ColorCount); if (!PaletteColors) return; for (Index=0; Index<RGB_MAX; Index++) { for (Color=0; Color<ColorCount; Color++) { MachVideoGetPaletteColor(Color, &PaletteColors[Color].Red, &PaletteColors[Color].Green, &PaletteColors[Color].Blue); // Increment each color so it approaches its real value if (PaletteColors[Color].Red < Palette[Color].Red) { PaletteColors[Color].Red++; } if (PaletteColors[Color].Green < Palette[Color].Green) { PaletteColors[Color].Green++; } if (PaletteColors[Color].Blue < Palette[Color].Blue) { PaletteColors[Color].Blue++; } // Make sure we haven't exceeded the real value if (PaletteColors[Color].Red > Palette[Color].Red) { PaletteColors[Color].Red = Palette[Color].Red; } if (PaletteColors[Color].Green > Palette[Color].Green) { PaletteColors[Color].Green = Palette[Color].Green; } if (PaletteColors[Color].Blue > Palette[Color].Blue) { PaletteColors[Color].Blue = Palette[Color].Blue; } } // Set the colors for (Color=0; Color<ColorCount; Color++) { if ((Color % RGB_MAX_PER_ITERATION) == 0) { MachVideoSync(); } MachVideoSetPaletteColor(Color, PaletteColors[Color].Red, PaletteColors[Color].Green, PaletteColors[Color].Blue); } } MmHeapFree(PaletteColors); }
static VOID DetectPciIrqRoutingTable(PCONFIGURATION_COMPONENT_DATA BusKey) { PCM_PARTIAL_RESOURCE_LIST PartialResourceList; PCM_PARTIAL_RESOURCE_DESCRIPTOR PartialDescriptor; PPCI_IRQ_ROUTING_TABLE Table; PCONFIGURATION_COMPONENT_DATA TableKey; ULONG Size; Table = GetPciIrqRoutingTable(); if (Table != NULL) { TRACE("Table size: %u\n", Table->TableSize); /* Set 'Configuration Data' value */ Size = FIELD_OFFSET(CM_PARTIAL_RESOURCE_LIST, PartialDescriptors) + 2 * sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR) + Table->TableSize; PartialResourceList = MmHeapAlloc(Size); if (PartialResourceList == NULL) { ERR("Failed to allocate resource descriptor\n"); return; } /* Initialize resource descriptor */ memset(PartialResourceList, 0, Size); PartialResourceList->Version = 1; PartialResourceList->Revision = 1; PartialResourceList->Count = 2; PartialDescriptor = &PartialResourceList->PartialDescriptors[0]; PartialDescriptor->Type = CmResourceTypeBusNumber; PartialDescriptor->ShareDisposition = CmResourceShareDeviceExclusive; PartialDescriptor->u.BusNumber.Start = 0; PartialDescriptor->u.BusNumber.Length = 1; PartialDescriptor = &PartialResourceList->PartialDescriptors[1]; PartialDescriptor->Type = CmResourceTypeDeviceSpecific; PartialDescriptor->ShareDisposition = CmResourceShareUndetermined; PartialDescriptor->u.DeviceSpecificData.DataSize = Table->TableSize; memcpy(&PartialResourceList->PartialDescriptors[2], Table, Table->TableSize); FldrCreateComponentKey(BusKey, PeripheralClass, RealModeIrqRoutingTable, 0x0, 0x0, 0xFFFFFFFF, "PCI Real-mode IRQ Routing Table", PartialResourceList, Size, &TableKey); MmHeapFree(PartialResourceList); } }
PVOID NTAPI FldrpHwHeapAlloc(IN SIZE_T Size) { PVOID Buffer; /* Allocate memory from generic bootloader heap */ Buffer = MmHeapAlloc(Size); return Buffer; }
BOOLEAN IniAddSettingValueToSection(ULONG_PTR SectionId, PCSTR SettingName, PCSTR SettingValue) { PINI_SECTION Section = (PINI_SECTION)SectionId; PINI_SECTION_ITEM SectionItem; // Allocate a new item structure SectionItem = MmHeapAlloc(sizeof(INI_SECTION_ITEM)); if (!SectionItem) { return FALSE; } RtlZeroMemory(SectionItem, sizeof(INI_SECTION_ITEM)); // Allocate the setting name buffer SectionItem->ItemName = MmHeapAlloc(strlen(SettingName) + 1); if (!SectionItem->ItemName) { MmHeapFree(SectionItem); return FALSE; } // Allocate the setting value buffer SectionItem->ItemValue = MmHeapAlloc(strlen(SettingValue) + 1); if (!SectionItem->ItemValue) { MmHeapFree(SectionItem->ItemName); MmHeapFree(SectionItem); return FALSE; } strcpy(SectionItem->ItemName, SettingName); strcpy(SectionItem->ItemValue, SettingValue); // Add it to the current section Section->SectionItemCount++; InsertTailList(&Section->SectionItemList, &SectionItem->ListEntry); return TRUE; }
static LONG DiskOpen(CHAR* Path, OPENMODE OpenMode, ULONG* FileId) { DISKCONTEXT* Context; ULONG DrivePartition, SectorSize; UCHAR DriveNumber; ULONGLONG SectorOffset = 0; ULONGLONG SectorCount = 0; PARTITION_TABLE_ENTRY PartitionTableEntry; CHAR FileName[1]; if (!DissectArcPath(Path, FileName, &DriveNumber, &DrivePartition)) return EINVAL; if (DrivePartition == 0xff) { /* This is a CD-ROM device */ SectorSize = 2048; } else { /* This is either a floppy disk device (DrivePartition == 0) or * a hard disk device (DrivePartition != 0 && DrivePartition != 0xFF) but * it doesn't matter which one because they both have 512 bytes per sector */ SectorSize = 512; } if (DrivePartition != 0xff && DrivePartition != 0) { if (!XboxDiskGetPartitionEntry(DriveNumber, DrivePartition, &PartitionTableEntry)) return EINVAL; SectorOffset = PartitionTableEntry.SectorCountBeforePartition; SectorCount = PartitionTableEntry.PartitionSectorCount; } else { SectorCount = 0; /* FIXME */ } Context = MmHeapAlloc(sizeof(DISKCONTEXT)); if (!Context) return ENOMEM; Context->DriveNumber = DriveNumber; Context->SectorSize = SectorSize; Context->SectorOffset = SectorOffset; Context->SectorCount = SectorCount; Context->SectorNumber = 0; FsSetDeviceSpecific(*FileId, Context); return ESUCCESS; }
VOID NTAPI FldrCreateComponentKey(IN PCONFIGURATION_COMPONENT_DATA SystemNode, IN CONFIGURATION_CLASS Class, IN CONFIGURATION_TYPE Type, IN IDENTIFIER_FLAG Flags, IN ULONG Key, IN ULONG Affinity, IN PCHAR IdentifierString, IN PCM_PARTIAL_RESOURCE_LIST ResourceList, IN ULONG Size, OUT PCONFIGURATION_COMPONENT_DATA *ComponentKey) { PCONFIGURATION_COMPONENT_DATA ComponentData; PCONFIGURATION_COMPONENT Component; /* Allocate the node for this component */ ComponentData = MmHeapAlloc(sizeof(CONFIGURATION_COMPONENT_DATA)); if (!ComponentData) return; /* Now save our parent */ ComponentData->Parent = SystemNode; /* Link us to the parent */ if (SystemNode) FldrLinkToParent(SystemNode, ComponentData); /* Set us up */ Component = &ComponentData->ComponentEntry; Component->Class = Class; Component->Type = Type; Component->Flags = Flags; Component->Key = Key; Component->AffinityMask = Affinity; /* Set identifier */ if (IdentifierString) FldrSetIdentifier(ComponentData, IdentifierString); /* Set configuration data */ if (ResourceList) { ComponentData->ConfigurationData = ResourceList; ComponentData->ComponentEntry.ConfigurationDataLength = Size; } /* Return the child */ *ComponentKey = ComponentData; }
VOID UiShowMessageBoxesInSection(PCSTR SectionName) { ULONG Idx; CHAR SettingName[80]; CHAR SettingValue[80]; PCHAR MessageBoxText; ULONG MessageBoxTextSize; ULONG_PTR SectionId; if (!IniOpenSection(SectionName, &SectionId)) { return; } // // Find all the message box settings and run them // for (Idx=0; Idx<IniGetNumSectionItems(SectionId); Idx++) { IniReadSettingByNumber(SectionId, Idx, SettingName, sizeof(SettingName), SettingValue, sizeof(SettingValue)); if (_stricmp(SettingName, "MessageBox") == 0) { // Get the real length of the MessageBox text MessageBoxTextSize = IniGetSectionSettingValueSize(SectionId, Idx); //if (MessageBoxTextSize > 0) { // Allocate enough memory to hold the text MessageBoxText = MmHeapAlloc(MessageBoxTextSize); if (MessageBoxText) { // Get the MessageBox text IniReadSettingByNumber(SectionId, Idx, SettingName, sizeof(SettingName), MessageBoxText, MessageBoxTextSize); // Fix it up UiEscapeString(MessageBoxText); // Display it UiMessageBox(MessageBoxText); // Free the memory MmHeapFree(MessageBoxText); } } } } }
VOID TuiMessageBox(PCSTR MessageText) { PVOID ScreenBuffer; // Save the screen contents ScreenBuffer = MmHeapAlloc(UiScreenWidth * UiScreenHeight * 2); TuiSaveScreen(ScreenBuffer); // Display the message box TuiMessageBoxCritical(MessageText); // Restore the screen contents TuiRestoreScreen(ScreenBuffer); MmHeapFree(ScreenBuffer); }
static VOID DetectIsaBios(PCONFIGURATION_COMPONENT_DATA SystemKey, ULONG *BusNumber) { PCM_PARTIAL_RESOURCE_LIST PartialResourceList; PCONFIGURATION_COMPONENT_DATA BusKey; ULONG Size; /* Set 'Configuration Data' value */ Size = sizeof(CM_PARTIAL_RESOURCE_LIST) - sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR); PartialResourceList = MmHeapAlloc(Size); if (PartialResourceList == NULL) { TRACE( "Failed to allocate resource descriptor\n"); return; } /* Initialize resource descriptor */ memset(PartialResourceList, 0, Size); PartialResourceList->Version = 1; PartialResourceList->Revision = 1; PartialResourceList->Count = 0; /* Create new bus key */ FldrCreateComponentKey(SystemKey, AdapterClass, MultiFunctionAdapter, 0x0, 0x0, 0xFFFFFFFF, "ISA", PartialResourceList, Size, &BusKey); /* Increment bus number */ (*BusNumber)++; MmHeapFree(PartialResourceList); /* Detect ISA/BIOS devices */ DetectBiosDisks(SystemKey, BusKey); /* FIXME: Detect more ISA devices */ }
static PINFCACHESECTION InfpCacheAddSection( PINFCACHE Cache, PCHAR Name) { PINFCACHESECTION Section = NULL; SIZE_T Size; if ((Cache == NULL) || (Name == NULL)) { // DPRINT("Invalid parameter\n"); return NULL; } /* Allocate and initialize the new section */ Size = sizeof(INFCACHESECTION) + strlen(Name); Section = (PINFCACHESECTION)MmHeapAlloc(Size); if (Section == NULL) { // DPRINT("RtlAllocateHeap() failed\n"); return NULL; } memset(Section, 0, Size); /* Copy section name */ strcpy(Section->Name, Name); /* Append section */ if (Cache->FirstSection == NULL) { Cache->FirstSection = Section; Cache->LastSection = Section; } else { Cache->LastSection->Next = Section; Section->Prev = Cache->LastSection; Cache->LastSection = Section; } return Section; }
/* * IsoBufferDirectory() * This function allocates a buffer, reads the specified directory * and returns a pointer to that buffer into pDirectoryBuffer. The * function returns an ARC error code. The directory is specified * by its starting sector and length. */ static LONG IsoBufferDirectory(ULONG DeviceId, ULONG DirectoryStartSector, ULONG DirectoryLength, PVOID* pDirectoryBuffer) { PVOID DirectoryBuffer; ULONG SectorCount; LARGE_INTEGER Position; ULONG Count; ULONG ret; TRACE("IsoBufferDirectory() DirectoryStartSector = %d DirectoryLength = %d\n", DirectoryStartSector, DirectoryLength); SectorCount = ROUND_UP(DirectoryLength, SECTORSIZE) / SECTORSIZE; TRACE("Trying to read (DirectoryCount) %d sectors.\n", SectorCount); // // Attempt to allocate memory for directory buffer // TRACE("Trying to allocate (DirectoryLength) %d bytes.\n", DirectoryLength); DirectoryBuffer = MmHeapAlloc(DirectoryLength); if (!DirectoryBuffer) return ENOMEM; // // Now read directory contents into DirectoryBuffer // Position.HighPart = 0; Position.LowPart = DirectoryStartSector * SECTORSIZE; ret = ArcSeek(DeviceId, &Position, SeekAbsolute); if (ret != ESUCCESS) { MmHeapFree(DirectoryBuffer); return ret; } ret = ArcRead(DeviceId, DirectoryBuffer, SectorCount * SECTORSIZE, &Count); if (ret != ESUCCESS || Count != SectorCount * SECTORSIZE) { MmHeapFree(DirectoryBuffer); return EIO; } *pDirectoryBuffer = DirectoryBuffer; return ESUCCESS; }
static PVOID InfpAddKeyToLine( PINFCACHELINE Line, PCHAR Key) { if (Line == NULL) return NULL; if (Line->Key != NULL) return NULL; Line->Key = MmHeapAlloc(strlen(Key) + 1); if (Line->Key == NULL) return NULL; strcpy(Line->Key, Key); return (PVOID)Line->Key; }
VOID NTAPI FldrSetIdentifier(IN PCONFIGURATION_COMPONENT_DATA ComponentData, IN PCHAR IdentifierString) { SIZE_T IdentifierLength; PCONFIGURATION_COMPONENT Component = &ComponentData->ComponentEntry; PCHAR Identifier; /* Allocate memory for the identifier */ IdentifierLength = strlen(IdentifierString) + 1; Identifier = MmHeapAlloc(IdentifierLength); if (!Identifier) return; /* Copy the identifier */ RtlCopyMemory(Identifier, IdentifierString, IdentifierLength); /* Set component information */ Component->IdentifierLength = (ULONG)IdentifierLength; Component->Identifier = Identifier; }
static PINFCACHELINE InfpCacheAddLine(PINFCACHESECTION Section) { PINFCACHELINE Line; if (Section == NULL) { // DPRINT("Invalid parameter\n"); return NULL; } Line = (PINFCACHELINE)MmHeapAlloc(sizeof(INFCACHELINE)); if (Line == NULL) { // DPRINT("RtlAllocateHeap() failed\n"); return NULL; } memset(Line, 0, sizeof(INFCACHELINE)); /* Append line */ if (Section->FirstLine == NULL) { Section->FirstLine = Line; Section->LastLine = Line; } else { Section->LastLine->Next = Line; Line->Prev = Section->LastLine; Section->LastLine = Line; } Section->LineCount++; return Line; }
VOID TuiFadeInBackdrop(VOID) { PPALETTE_ENTRY TuiFadePalette = NULL; if (UiUseSpecialEffects && ! MachVideoIsPaletteFixed()) { TuiFadePalette = (PPALETTE_ENTRY)MmHeapAlloc(sizeof(PALETTE_ENTRY) * 64); if (TuiFadePalette != NULL) { VideoSavePaletteState(TuiFadePalette, 64); VideoSetAllColorsToBlack(64); } } // Draw the backdrop and title box TuiDrawBackdrop(); if (UiUseSpecialEffects && ! MachVideoIsPaletteFixed() && TuiFadePalette != NULL) { VideoFadeIn(TuiFadePalette, 64); MmHeapFree(TuiFadePalette); } }
BOOLEAN WinLdrAddDriverToList(LIST_ENTRY *BootDriverListHead, LPWSTR RegistryPath, LPWSTR ImagePath, LPWSTR ServiceName) { PBOOT_DRIVER_LIST_ENTRY BootDriverEntry; NTSTATUS Status; USHORT PathLength; BootDriverEntry = MmHeapAlloc(sizeof(BOOT_DRIVER_LIST_ENTRY)); if (!BootDriverEntry) return FALSE; // DTE will be filled during actual load of the driver BootDriverEntry->LdrEntry = NULL; // Check - if we have a valid ImagePath, if not - we need to build it // like "System32\\Drivers\\blah.sys" if (ImagePath && (wcslen(ImagePath) > 0)) { // Just copy ImagePath to the corresponding field in the structure PathLength = wcslen(ImagePath) * sizeof(WCHAR) + sizeof(UNICODE_NULL); BootDriverEntry->FilePath.Length = 0; BootDriverEntry->FilePath.MaximumLength = PathLength; BootDriverEntry->FilePath.Buffer = MmHeapAlloc(PathLength); if (!BootDriverEntry->FilePath.Buffer) { MmHeapFree(BootDriverEntry); return FALSE; } Status = RtlAppendUnicodeToString(&BootDriverEntry->FilePath, ImagePath); if (!NT_SUCCESS(Status)) { MmHeapFree(BootDriverEntry->FilePath.Buffer); MmHeapFree(BootDriverEntry); return FALSE; } } else { // we have to construct ImagePath ourselves PathLength = wcslen(ServiceName)*sizeof(WCHAR) + sizeof(L"system32\\drivers\\.sys"); BootDriverEntry->FilePath.Length = 0; BootDriverEntry->FilePath.MaximumLength = PathLength; BootDriverEntry->FilePath.Buffer = MmHeapAlloc(PathLength); if (!BootDriverEntry->FilePath.Buffer) { MmHeapFree(BootDriverEntry); return FALSE; } Status = RtlAppendUnicodeToString(&BootDriverEntry->FilePath, L"system32\\drivers\\"); if (!NT_SUCCESS(Status)) { MmHeapFree(BootDriverEntry->FilePath.Buffer); MmHeapFree(BootDriverEntry); return FALSE; } Status = RtlAppendUnicodeToString(&BootDriverEntry->FilePath, ServiceName); if (!NT_SUCCESS(Status)) { MmHeapFree(BootDriverEntry->FilePath.Buffer); MmHeapFree(BootDriverEntry); return FALSE; } Status = RtlAppendUnicodeToString(&BootDriverEntry->FilePath, L".sys"); if (!NT_SUCCESS(Status)) { MmHeapFree(BootDriverEntry->FilePath.Buffer); MmHeapFree(BootDriverEntry); return FALSE; } } // Add registry path PathLength = (wcslen(RegistryPath) + wcslen(ServiceName))*sizeof(WCHAR) + sizeof(UNICODE_NULL); BootDriverEntry->RegistryPath.Length = 0; BootDriverEntry->RegistryPath.MaximumLength = PathLength; BootDriverEntry->RegistryPath.Buffer = MmHeapAlloc(PathLength); if (!BootDriverEntry->RegistryPath.Buffer) return FALSE; Status = RtlAppendUnicodeToString(&BootDriverEntry->RegistryPath, RegistryPath); if (!NT_SUCCESS(Status)) return FALSE; Status = RtlAppendUnicodeToString(&BootDriverEntry->RegistryPath, ServiceName); if (!NT_SUCCESS(Status)) return FALSE; // Insert entry at top of the list InsertTailList(BootDriverListHead, &BootDriverEntry->Link); return TRUE; }
VOID WinLdrScanRegistry(IN OUT PLOADER_PARAMETER_BLOCK LoaderBlock, IN LPCSTR DirectoryPath) { LONG rc = 0; FRLDRHKEY hGroupKey, hOrderKey, hServiceKey, hDriverKey; LPWSTR GroupNameBuffer; WCHAR ServiceName[256]; ULONG OrderList[128]; ULONG BufferSize; ULONG Index; ULONG TagIndex; LPWSTR GroupName; ULONG ValueSize; ULONG ValueType; ULONG StartValue; ULONG TagValue; WCHAR DriverGroup[256]; ULONG DriverGroupSize; CHAR ImagePath[256]; WCHAR TempImagePath[256]; BOOLEAN Status; /* get 'service group order' key */ rc = RegOpenKey(NULL, L"\\Registry\\Machine\\SYSTEM\\CurrentControlSet\\Control\\ServiceGroupOrder", &hGroupKey); if (rc != ERROR_SUCCESS) { TRACE_CH(ODYSSEY, "Failed to open the 'ServiceGroupOrder' key (rc %d)\n", (int)rc); return; } /* get 'group order list' key */ rc = RegOpenKey(NULL, L"\\Registry\\Machine\\SYSTEM\\CurrentControlSet\\Control\\GroupOrderList", &hOrderKey); if (rc != ERROR_SUCCESS) { TRACE_CH(ODYSSEY, "Failed to open the 'GroupOrderList' key (rc %d)\n", (int)rc); return; } /* enumerate drivers */ rc = RegOpenKey(NULL, L"\\Registry\\Machine\\SYSTEM\\CurrentControlSet\\Services", &hServiceKey); if (rc != ERROR_SUCCESS) { TRACE_CH(ODYSSEY, "Failed to open the 'Services' key (rc %d)\n", (int)rc); return; } /* Get the Name Group */ BufferSize = 4096; GroupNameBuffer = MmHeapAlloc(BufferSize); rc = RegQueryValue(hGroupKey, L"List", NULL, (PUCHAR)GroupNameBuffer, &BufferSize); TRACE_CH(ODYSSEY, "RegQueryValue(): rc %d\n", (int)rc); if (rc != ERROR_SUCCESS) return; TRACE_CH(ODYSSEY, "BufferSize: %d \n", (int)BufferSize); TRACE_CH(ODYSSEY, "GroupNameBuffer: '%S' \n", GroupNameBuffer); /* Loop through each group */ GroupName = GroupNameBuffer; while (*GroupName) { TRACE("Driver group: '%S'\n", GroupName); /* Query the Order */ BufferSize = sizeof(OrderList); rc = RegQueryValue(hOrderKey, GroupName, NULL, (PUCHAR)OrderList, &BufferSize); if (rc != ERROR_SUCCESS) OrderList[0] = 0; /* enumerate all drivers */ for (TagIndex = 1; TagIndex <= OrderList[0]; TagIndex++) { Index = 0; while (TRUE) { /* Get the Driver's Name */ ValueSize = sizeof(ServiceName); rc = RegEnumKey(hServiceKey, Index, ServiceName, &ValueSize); //TRACE_CH(ODYSSEY, "RegEnumKey(): rc %d\n", (int)rc); /* Makre sure it's valid, and check if we're done */ if (rc == ERROR_NO_MORE_ITEMS) break; if (rc != ERROR_SUCCESS) { MmHeapFree(GroupNameBuffer); return; } //TRACE_CH(ODYSSEY, "Service %d: '%S'\n", (int)Index, ServiceName); /* open driver Key */ rc = RegOpenKey(hServiceKey, ServiceName, &hDriverKey); if (rc == ERROR_SUCCESS) { /* Read the Start Value */ ValueSize = sizeof(ULONG); rc = RegQueryValue(hDriverKey, L"Start", &ValueType, (PUCHAR)&StartValue, &ValueSize); if (rc != ERROR_SUCCESS) StartValue = (ULONG)-1; //TRACE_CH(ODYSSEY, " Start: %x \n", (int)StartValue); /* Read the Tag */ ValueSize = sizeof(ULONG); rc = RegQueryValue(hDriverKey, L"Tag", &ValueType, (PUCHAR)&TagValue, &ValueSize); if (rc != ERROR_SUCCESS) TagValue = (ULONG)-1; //TRACE_CH(ODYSSEY, " Tag: %x \n", (int)TagValue); /* Read the driver's group */ DriverGroupSize = sizeof(DriverGroup); rc = RegQueryValue(hDriverKey, L"Group", NULL, (PUCHAR)DriverGroup, &DriverGroupSize); //TRACE_CH(ODYSSEY, " Group: '%S' \n", DriverGroup); /* Make sure it should be started */ if ((StartValue == 0) && (TagValue == OrderList[TagIndex]) && (_wcsicmp(DriverGroup, GroupName) == 0)) { /* Get the Driver's Location */ ValueSize = sizeof(TempImagePath); rc = RegQueryValue(hDriverKey, L"ImagePath", NULL, (PUCHAR)TempImagePath, &ValueSize); /* Write the whole path if it suceeded, else prepare to fail */ if (rc != ERROR_SUCCESS) { TRACE_CH(ODYSSEY, "ImagePath: not found\n"); TempImagePath[0] = 0; sprintf(ImagePath, "%s\\system32\\drivers\\%S.sys", DirectoryPath, ServiceName); } else if (TempImagePath[0] != L'\\') { sprintf(ImagePath, "%s%S", DirectoryPath, TempImagePath); } else { sprintf(ImagePath, "%S", TempImagePath); TRACE_CH(ODYSSEY, "ImagePath: '%s'\n", ImagePath); } TRACE("Adding boot driver: '%s'\n", ImagePath); Status = WinLdrAddDriverToList(&LoaderBlock->BootDriverListHead, L"\\Registry\\Machine\\System\\CurrentControlSet\\Services\\", TempImagePath, ServiceName); if (!Status) ERR("Failed to add boot driver\n"); } else { //TRACE(" Skipping driver '%S' with Start %d, Tag %d and Group '%S' (Current Tag %d, current group '%S')\n", // ServiceName, StartValue, TagValue, DriverGroup, OrderList[TagIndex], GroupName); } } Index++; } } Index = 0; while (TRUE) { /* Get the Driver's Name */ ValueSize = sizeof(ServiceName); rc = RegEnumKey(hServiceKey, Index, ServiceName, &ValueSize); //TRACE_CH(ODYSSEY, "RegEnumKey(): rc %d\n", (int)rc); if (rc == ERROR_NO_MORE_ITEMS) break; if (rc != ERROR_SUCCESS) { MmHeapFree(GroupNameBuffer); return; } //TRACE_CH(ODYSSEY, "Service %d: '%S'\n", (int)Index, ServiceName); /* open driver Key */ rc = RegOpenKey(hServiceKey, ServiceName, &hDriverKey); if (rc == ERROR_SUCCESS) { /* Read the Start Value */ ValueSize = sizeof(ULONG); rc = RegQueryValue(hDriverKey, L"Start", &ValueType, (PUCHAR)&StartValue, &ValueSize); if (rc != ERROR_SUCCESS) StartValue = (ULONG)-1; //TRACE_CH(ODYSSEY, " Start: %x \n", (int)StartValue); /* Read the Tag */ ValueSize = sizeof(ULONG); rc = RegQueryValue(hDriverKey, L"Tag", &ValueType, (PUCHAR)&TagValue, &ValueSize); if (rc != ERROR_SUCCESS) TagValue = (ULONG)-1; //TRACE_CH(ODYSSEY, " Tag: %x \n", (int)TagValue); /* Read the driver's group */ DriverGroupSize = sizeof(DriverGroup); rc = RegQueryValue(hDriverKey, L"Group", NULL, (PUCHAR)DriverGroup, &DriverGroupSize); //TRACE_CH(ODYSSEY, " Group: '%S' \n", DriverGroup); for (TagIndex = 1; TagIndex <= OrderList[0]; TagIndex++) { if (TagValue == OrderList[TagIndex]) break; } if ((StartValue == 0) && (TagIndex > OrderList[0]) && (_wcsicmp(DriverGroup, GroupName) == 0)) { ValueSize = sizeof(TempImagePath); rc = RegQueryValue(hDriverKey, L"ImagePath", NULL, (PUCHAR)TempImagePath, &ValueSize); if (rc != ERROR_SUCCESS) { TRACE_CH(ODYSSEY, "ImagePath: not found\n"); TempImagePath[0] = 0; sprintf(ImagePath, "%ssystem32\\drivers\\%S.sys", DirectoryPath, ServiceName); } else if (TempImagePath[0] != L'\\') { sprintf(ImagePath, "%s%S", DirectoryPath, TempImagePath); } else { sprintf(ImagePath, "%S", TempImagePath); TRACE_CH(ODYSSEY, "ImagePath: '%s'\n", ImagePath); } TRACE(" Adding boot driver: '%s'\n", ImagePath); Status = WinLdrAddDriverToList(&LoaderBlock->BootDriverListHead, L"\\Registry\\Machine\\System\\CurrentControlSet\\Services\\", TempImagePath, ServiceName); if (!Status) ERR(" Failed to add boot driver\n"); } else { //TRACE(" Skipping driver '%S' with Start %d, Tag %d and Group '%S' (Current group '%S')\n", // ServiceName, StartValue, TagValue, DriverGroup, GroupName); } } Index++; } /* Move to the next group name */ GroupName = GroupName + wcslen(GroupName) + 1; } /* Free allocated memory */ MmHeapFree(GroupNameBuffer); }
BOOLEAN IniFileInitialize(VOID) { FILEINFORMATION FileInformation; ULONG FileId; // File handle for freeldr.ini PCHAR FreeLoaderIniFileData; ULONG FreeLoaderIniFileSize, Count; LONG ret; BOOLEAN Success; TRACE("IniFileInitialize()\n"); // // Open freeldr.ini // ret = IniOpenIniFile(&FileId); if (ret != ESUCCESS) { UiMessageBoxCritical("Error opening freeldr.ini or file not found.\nYou need to re-install FreeLoader."); return FALSE; } // // Get the file size // ret = ArcGetFileInformation(FileId, &FileInformation); if (ret != ESUCCESS || FileInformation.EndingAddress.HighPart != 0) { UiMessageBoxCritical("Error while getting informations about freeldr.ini.\nYou need to re-install FreeLoader."); return FALSE; } FreeLoaderIniFileSize = FileInformation.EndingAddress.LowPart; // // Allocate memory to cache the whole freeldr.ini // FreeLoaderIniFileData = MmHeapAlloc(FreeLoaderIniFileSize); if (!FreeLoaderIniFileData) { UiMessageBoxCritical("Out of memory while loading freeldr.ini."); ArcClose(FileId); return FALSE; } // // Read freeldr.ini off the disk // ret = ArcRead(FileId, FreeLoaderIniFileData, FreeLoaderIniFileSize, &Count); if (ret != ESUCCESS || Count != FreeLoaderIniFileSize) { UiMessageBoxCritical("Error while reading freeldr.ini."); ArcClose(FileId); MmHeapFree(FreeLoaderIniFileData); return FALSE; } // // Parse the .ini file data // Success = IniParseFile(FreeLoaderIniFileData, FreeLoaderIniFileSize); // // Do some cleanup, and return // ArcClose(FileId); MmHeapFree(FreeLoaderIniFileData); return Success; }
static PCM_PARTIAL_RESOURCE_LIST GetHarddiskConfigurationData(UCHAR DriveNumber, ULONG* pSize) { PCM_PARTIAL_RESOURCE_LIST PartialResourceList; PCM_DISK_GEOMETRY_DEVICE_DATA DiskGeometry; //EXTENDED_GEOMETRY ExtGeometry; GEOMETRY Geometry; ULONG Size; // // Initialize returned size // *pSize = 0; /* Set 'Configuration Data' value */ Size = sizeof(CM_PARTIAL_RESOURCE_LIST) + sizeof(CM_DISK_GEOMETRY_DEVICE_DATA); PartialResourceList = MmHeapAlloc(Size); if (PartialResourceList == NULL) { ERR("Failed to allocate a full resource descriptor\n"); return NULL; } memset(PartialResourceList, 0, Size); PartialResourceList->Version = 1; PartialResourceList->Revision = 1; PartialResourceList->Count = 1; PartialResourceList->PartialDescriptors[0].Type = CmResourceTypeDeviceSpecific; // PartialResourceList->PartialDescriptors[0].ShareDisposition = // PartialResourceList->PartialDescriptors[0].Flags = PartialResourceList->PartialDescriptors[0].u.DeviceSpecificData.DataSize = sizeof(CM_DISK_GEOMETRY_DEVICE_DATA); /* Get pointer to geometry data */ DiskGeometry = (PVOID)(((ULONG_PTR)PartialResourceList) + sizeof(CM_PARTIAL_RESOURCE_LIST)); /* Get the disk geometry */ //ExtGeometry.Size = sizeof(EXTENDED_GEOMETRY); if(MachDiskGetDriveGeometry(DriveNumber, &Geometry)) { DiskGeometry->BytesPerSector = Geometry.BytesPerSector; DiskGeometry->NumberOfCylinders = Geometry.Cylinders; DiskGeometry->SectorsPerTrack = Geometry.Sectors; DiskGeometry->NumberOfHeads = Geometry.Heads; } else { ERR("Reading disk geometry failed\n"); MmHeapFree(PartialResourceList); return NULL; } TRACE("Disk %x: %u Cylinders %u Heads %u Sectors %u Bytes\n", DriveNumber, DiskGeometry->NumberOfCylinders, DiskGeometry->NumberOfHeads, DiskGeometry->SectorsPerTrack, DiskGeometry->BytesPerSector); // // Return configuration data // *pSize = Size; return PartialResourceList; }
static VOID DetectBiosDisks(PCONFIGURATION_COMPONENT_DATA SystemKey, PCONFIGURATION_COMPONENT_DATA BusKey) { PCM_PARTIAL_RESOURCE_LIST PartialResourceList; PCM_INT13_DRIVE_PARAMETER Int13Drives; GEOMETRY Geometry; PCONFIGURATION_COMPONENT_DATA DiskKey, ControllerKey; UCHAR DiskCount, i; ULONG Size; BOOLEAN Changed; /* Count the number of visible drives */ DiskReportError(FALSE); DiskCount = 0; /* There are some really broken BIOSes out there. There are even BIOSes * that happily report success when you ask them to read from non-existent * harddisks. So, we set the buffer to known contents first, then try to * read. If the BIOS reports success but the buffer contents haven't * changed then we fail anyway */ memset((PVOID) DISKREADBUFFER, 0xcd, 512); while (MachDiskReadLogicalSectors(0x80 + DiskCount, 0ULL, 1, (PVOID)DISKREADBUFFER)) { Changed = FALSE; for (i = 0; ! Changed && i < 512; i++) { Changed = ((PUCHAR)DISKREADBUFFER)[i] != 0xcd; } if (! Changed) { TRACE("BIOS reports success for disk %d but data didn't change\n", (int)DiskCount); break; } DiskCount++; memset((PVOID) DISKREADBUFFER, 0xcd, 512); } DiskReportError(TRUE); TRACE("BIOS reports %d harddisk%s\n", (int)DiskCount, (DiskCount == 1) ? "": "s"); //DetectBiosFloppyController(BusKey); /* Allocate resource descriptor */ Size = sizeof(CM_PARTIAL_RESOURCE_LIST) + sizeof(CM_INT13_DRIVE_PARAMETER) * DiskCount; PartialResourceList = MmHeapAlloc(Size); if (PartialResourceList == NULL) { ERR("Failed to allocate resource descriptor\n"); return; } /* Initialize resource descriptor */ memset(PartialResourceList, 0, Size); PartialResourceList->Version = 1; PartialResourceList->Revision = 1; PartialResourceList->Count = 1; PartialResourceList->PartialDescriptors[0].Type = CmResourceTypeDeviceSpecific; PartialResourceList->PartialDescriptors[0].ShareDisposition = 0; PartialResourceList->PartialDescriptors[0].Flags = 0; PartialResourceList->PartialDescriptors[0].u.DeviceSpecificData.DataSize = sizeof(CM_INT13_DRIVE_PARAMETER) * DiskCount; /* Get harddisk Int13 geometry data */ Int13Drives = (PVOID)(((ULONG_PTR)PartialResourceList) + sizeof(CM_PARTIAL_RESOURCE_LIST)); for (i = 0; i < DiskCount; i++) { if (MachDiskGetDriveGeometry(0x80 + i, &Geometry)) { Int13Drives[i].DriveSelect = 0x80 + i; Int13Drives[i].MaxCylinders = Geometry.Cylinders - 1; Int13Drives[i].SectorsPerTrack = (USHORT)Geometry.Sectors; Int13Drives[i].MaxHeads = (USHORT)Geometry.Heads - 1; Int13Drives[i].NumberDrives = DiskCount; TRACE( "Disk %x: %u Cylinders %u Heads %u Sectors %u Bytes\n", 0x80 + i, Geometry.Cylinders - 1, Geometry.Heads -1, Geometry.Sectors, Geometry.BytesPerSector); } } FldrCreateComponentKey(BusKey, ControllerClass, DiskController, Output | Input, 0, 0xFFFFFFFF, NULL, PartialResourceList, Size, &ControllerKey); TRACE("Created key: DiskController\\0\n"); MmHeapFree(PartialResourceList); /* Create and fill subkey for each harddisk */ for (i = 0; i < DiskCount; i++) { PCM_PARTIAL_RESOURCE_LIST PartialResourceList; ULONG Size; CHAR Identifier[20]; /* Get disk values */ PartialResourceList = GetHarddiskConfigurationData(0x80 + i, &Size); GetHarddiskIdentifier(Identifier, 0x80 + i); /* Create disk key */ FldrCreateComponentKey(ControllerKey, PeripheralClass, DiskPeripheral, Output | Input, 0, 0xFFFFFFFF, Identifier, PartialResourceList, Size, &DiskKey); if (PartialResourceList) MmHeapFree(PartialResourceList); } }
BOOLEAN InfOpenFile( PHINF InfHandle, PCSTR FileName, PULONG ErrorLine) { FILEINFORMATION Information; ULONG FileId; PCHAR FileBuffer; ULONG FileSize, Count; PINFCACHE Cache; BOOLEAN Success; LONG ret; *InfHandle = NULL; *ErrorLine = (ULONG) - 1; // // Open the .inf file // FileId = FsOpenFile(FileName); if (!FileId) { return FALSE; } // // Query file size // ret = ArcGetFileInformation(FileId, &Information); if ((ret != ESUCCESS) || (Information.EndingAddress.HighPart != 0)) { ArcClose(FileId); return FALSE; } FileSize = Information.EndingAddress.LowPart; // // Allocate buffer to cache the file // FileBuffer = MmHeapAlloc(FileSize + 1); if (!FileBuffer) { ArcClose(FileId); return FALSE; } // // Read file into memory // ret = ArcRead(FileId, FileBuffer, FileSize, &Count); if ((ret != ESUCCESS) || (Count != FileSize)) { ArcClose(FileId); MmHeapFree(FileBuffer); return FALSE; } // // We don't need the file anymore. Close it // ArcClose(FileId); // // Append string terminator // FileBuffer[FileSize] = 0; // // Allocate infcache header // Cache = (PINFCACHE)MmHeapAlloc(sizeof(INFCACHE)); if (!Cache) { MmHeapFree(FileBuffer); return FALSE; } // // Initialize inicache header // RtlZeroMemory(Cache, sizeof(INFCACHE)); // // Parse the inf buffer // Success = InfpParseBuffer(Cache, FileBuffer, FileBuffer + FileSize, ErrorLine); if (!Success) { MmHeapFree(Cache); Cache = NULL; } // // Free file buffer, as it has been parsed // MmHeapFree(FileBuffer); // // Return .inf parsed contents // *InfHandle = (HINF)Cache; return Success; }