⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 reactos.c

📁 ReactOS是一些高手根据Windows XP的内核编写出的类XP。内核实现机理和API函数调用几乎相同。甚至可以兼容XP的程序。喜欢研究系统内核的人可以看一看。
💻 C
📖 第 1 页 / 共 2 页
字号:

                    /* Read the driver's group */
                    DriverGroupSize = sizeof(DriverGroup);
                    rc = RegQueryValue(hDriverKey, L"Group", NULL, (PUCHAR)DriverGroup, &DriverGroupSize);
                    DbgPrint((DPRINT_REACTOS, "  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) {
                            DbgPrint((DPRINT_REACTOS, "  ImagePath: not found\n"));
                            sprintf(ImagePath, "%s\\system32\\drivers\\%S.sys", szSystemRoot, ServiceName);
                        } else if (TempImagePath[0] != L'\\') {
                            sprintf(ImagePath, "%s%S", szSystemRoot, TempImagePath);
                        } else {
                            sprintf(ImagePath, "%S", TempImagePath);
                            DbgPrint((DPRINT_REACTOS, "  ImagePath: '%s'\n", ImagePath));
                        }

                        DbgPrint((DPRINT_REACTOS, "  Loading driver: '%s'\n", ImagePath));

                        /* Update the position if needed */
                        if (nPos < 100) nPos += 5;

                        FrLdrLoadImage(ImagePath, nPos, 2);

                    } else {

                        DbgPrint((DPRINT_REACTOS, "  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);

            DbgPrint((DPRINT_REACTOS, "RegEnumKey(): rc %d\n", (int)rc));
            if (rc == ERROR_NO_MORE_ITEMS) break;
            if (rc != ERROR_SUCCESS) return;
            DbgPrint((DPRINT_REACTOS, "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;
                DbgPrint((DPRINT_REACTOS, "  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;
                DbgPrint((DPRINT_REACTOS, "  Tag:   %x  \n", (int)TagValue));

                /* Read the driver's group */
                DriverGroupSize = sizeof(DriverGroup);
                rc = RegQueryValue(hDriverKey, L"Group", NULL, (PUCHAR)DriverGroup, &DriverGroupSize);
                DbgPrint((DPRINT_REACTOS, "  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) {
                            DbgPrint((DPRINT_REACTOS, "  ImagePath: not found\n"));
                            sprintf(ImagePath, "%ssystem32\\drivers\\%S.sys", szSystemRoot, ServiceName);
                        } else if (TempImagePath[0] != L'\\') {
                            sprintf(ImagePath, "%s%S", szSystemRoot, TempImagePath);
                        } else {
                            sprintf(ImagePath, "%S", TempImagePath);
                            DbgPrint((DPRINT_REACTOS, "  ImagePath: '%s'\n", ImagePath));
                        }
                    DbgPrint((DPRINT_REACTOS, "  Loading driver: '%s'\n", ImagePath));

                    if (nPos < 100) nPos += 5;

                    FrLdrLoadImage(ImagePath, nPos, 2);

                } else {

                    DbgPrint((DPRINT_REACTOS, "  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;
    }
}

VOID
LoadAndBootReactOS(PCSTR OperatingSystemName)
{
	PFILE FilePointer;
	CHAR name[255];
	CHAR value[255];
	CHAR SystemPath[255];
	CHAR szKernelName[255];
	CHAR szFileName[255];
	CHAR  MsgBuffer[256];
	ULONG SectionId;
    PIMAGE_NT_HEADERS NtHeader;
    PVOID LoadBase;

	ULONG_PTR Base;
	ULONG Size;

	extern BOOLEAN AcpiPresent;

	//
	// Open the operating system section
	// specified in the .ini file
	//
	if (!IniOpenSection(OperatingSystemName, &SectionId))
	{
		sprintf(MsgBuffer,"Operating System section '%s' not found in freeldr.ini", OperatingSystemName);
		UiMessageBox(MsgBuffer);
		return;
	}

	UiDrawBackdrop();
	UiDrawStatusText("Detecting Hardware...");
    UiDrawProgressBarCenter(1, 100, szLoadingMsg);

	/*
	 * Setup multiboot information structure
	 */
	LoaderBlock.CommandLine = reactos_kernel_cmdline;
	LoaderBlock.ModsCount = 0;
	LoaderBlock.ModsAddr = reactos_modules;
    LoaderBlock.DrivesAddr = reactos_arc_disk_info;
    LoaderBlock.MmapLength = (unsigned long)MachGetMemoryMap((PBIOS_MEMORY_MAP)reactos_memory_map, 32) * sizeof(memory_map_t);
    if (LoaderBlock.MmapLength)
    {
        ULONG i;
        LoaderBlock.Flags |= MB_FLAGS_MEM_INFO | MB_FLAGS_MMAP_INFO;
        LoaderBlock.MmapAddr = (unsigned long)&reactos_memory_map;
        reactos_memory_map_descriptor_size = sizeof(memory_map_t); // GetBiosMemoryMap uses a fixed value of 24
        for (i=0; i<(LoaderBlock.MmapLength/sizeof(memory_map_t)); i++)
        {
            if (BiosMemoryUsable == reactos_memory_map[i].type &&
                0 == reactos_memory_map[i].base_addr_low)
            {
                LoaderBlock.MemLower = (reactos_memory_map[i].base_addr_low + reactos_memory_map[i].length_low) / 1024;
                if (640 < LoaderBlock.MemLower)
                {
                    LoaderBlock.MemLower = 640;
                }
            }
            if (BiosMemoryUsable == reactos_memory_map[i].type &&
                reactos_memory_map[i].base_addr_low <= 1024 * 1024 &&
                1024 * 1024 <= reactos_memory_map[i].base_addr_low + reactos_memory_map[i].length_low)
            {
                LoaderBlock.MemHigher = (reactos_memory_map[i].base_addr_low + reactos_memory_map[i].length_low) / 1024 - 1024;
            }
        }
    }

	/*
	 * Initialize the registry
	 */
	RegInitializeRegistry();

	/*
	 * Make sure the system path is set in the .ini file
	 */
	if (!IniReadSettingByName(SectionId, "SystemPath", SystemPath, sizeof(SystemPath)))
	{
		UiMessageBox("System path not specified for selected operating system.");
		return;
	}

	/*
	 * Special case for Live CD.
	 */
	if (!_stricmp(SystemPath, "LiveCD"))
	{
		/* Normalize */
		MachDiskGetBootPath(SystemPath, sizeof(SystemPath));
		strcat(SystemPath, "\\reactos");
		strcat(strcpy(reactos_kernel_cmdline, SystemPath),
		       " /MININT");
	}
	else
	{
		if (! MachDiskNormalizeSystemPath(SystemPath,
		                                  sizeof(SystemPath)))
		{
			UiMessageBox("Invalid system path");
			return;
		}
		/* copy system path into kernel command line */
		strcpy(reactos_kernel_cmdline, SystemPath);
	}

	/*
	 * Read the optional kernel parameters (if any)
	 */
	if (IniReadSettingByName(SectionId, "Options", value, sizeof(value)))
	{
		strcat(reactos_kernel_cmdline, " ");
		strcat(reactos_kernel_cmdline, value);
	}

	/*
	 * Detect hardware
	 */
	MachHwDetect();
    UiDrawProgressBarCenter(5, 100, szLoadingMsg);

	if (AcpiPresent) LoaderBlock.Flags |= MB_FLAGS_ACPI_TABLE;
    LoaderBlock.DrivesCount = reactos_disk_count;

	UiDrawStatusText("Loading...");

	/*
	 * Try to open system drive
	 */
	if (!FsOpenSystemVolume(SystemPath, szBootPath, &LoaderBlock.BootDevice))
	{
		UiMessageBox("Failed to open boot drive.");
		return;
	}

	/* append a backslash */
	if ((strlen(szBootPath)==0) ||
	    szBootPath[strlen(szBootPath)] != '\\')
		strcat(szBootPath, "\\");

	DbgPrint((DPRINT_REACTOS,"SystemRoot: '%s'\n", szBootPath));
	strcpy(SystemRoot, szBootPath);

	/*
	 * Find the kernel image name
	 * and try to load the kernel off the disk
	 */
	if(IniReadSettingByName(SectionId, "Kernel", value, sizeof(value)))
	{
		/*
		 * Set the name and
		 */
		if (value[0] == '\\')
		{
			strcpy(szKernelName, value);
		}
		else
		{
			strcpy(szKernelName, szBootPath);
			strcat(szKernelName, "SYSTEM32\\");
			strcat(szKernelName, value);
		}
	}
	else
	{
		strcpy(value, "NTOSKRNL.EXE");
		strcpy(szKernelName, szBootPath);
		strcat(szKernelName, "SYSTEM32\\");
		strcat(szKernelName, value);
	}

	/*
	 * Find the HAL image name
	 * and try to load the kernel off the disk
	 */
	if(IniReadSettingByName(SectionId, "Hal", value, sizeof(value)))
	{
		/*
		 * Set the name and
		 */
		if (value[0] == '\\')
		{
			strcpy(szHalName, value);
		}
		else
		{
			strcpy(szHalName, szBootPath);
			strcat(szHalName, "SYSTEM32\\");
			strcat(szHalName, value);
		}
	}
	else
	{
		strcpy(value, "HAL.DLL");
		strcpy(szHalName, szBootPath);
		strcat(szHalName, "SYSTEM32\\");
		strcat(szHalName, value);
	}

    /* Load the kernel */
    LoadBase = FrLdrLoadImage(szKernelName, 5, 1);
    if (!LoadBase) return;

    /* Get the NT header, kernel base and kernel entry */
    NtHeader = RtlImageNtHeader(LoadBase);
    KernelBase = NtHeader->OptionalHeader.ImageBase;
    KernelEntry = RaToPa(NtHeader->OptionalHeader.AddressOfEntryPoint);
    LoaderBlock.KernelBase = KernelBase;

	/*
	 * Load the System hive from disk
	 */
	strcpy(szFileName, szBootPath);
	strcat(szFileName, "SYSTEM32\\CONFIG\\SYSTEM");

	DbgPrint((DPRINT_REACTOS, "SystemHive: '%s'", szFileName));

	FilePointer = FsOpenFile(szFileName);
	if (FilePointer == NULL)
	{
		UiMessageBox("Could not find the System hive!");
		return;
	}

	/*
	 * Update the status bar with the current file
	 */
	strcpy(name, "Reading ");
	strcat(name, value);
	while (strlen(name) < 80)
		strcat(name, " ");
	UiDrawStatusText(name);

	/*
	 * Load the System hive
	 */
	Base = FrLdrLoadModule(FilePointer, szFileName, &Size);
	if (Base == 0 || Size == 0)
	{
		UiMessageBox("Could not load the System hive!\n");
		return;
	}
	DbgPrint((DPRINT_REACTOS, "SystemHive loaded at 0x%x size %u", (unsigned)Base, (unsigned)Size));

	/*
	 * Import the loaded system hive
	 */
	RegImportBinaryHive((PCHAR)Base, Size);

	/*
	 * Initialize the 'CurrentControlSet' link
	 */
	RegInitCurrentControlSet(FALSE);

	UiDrawProgressBarCenter(15, 100, szLoadingMsg);

	/*
	 * Export the hardware hive
	 */
	Base = FrLdrCreateModule ("HARDWARE");
	RegExportBinaryHive (L"\\Registry\\Machine\\HARDWARE", (PCHAR)Base, &Size);
	FrLdrCloseModule (Base, Size);

	UiDrawProgressBarCenter(20, 100, szLoadingMsg);

	/*
	 * Load NLS files
	 */
	if (!FrLdrLoadNlsFiles(szBootPath, MsgBuffer))
	{
	        UiMessageBox(MsgBuffer);
		return;
	}
	UiDrawProgressBarCenter(30, 100, szLoadingMsg);

	/*
	 * Load boot drivers
	 */
	FrLdrLoadBootDrivers(szBootPath, 40);
	//UiUnInitialize("Booting ReactOS...");

	/*
	 * Now boot the kernel
	 */
	DiskStopFloppyMotor();
    MachVideoPrepareForReactOS(FALSE);
    FrLdrStartup(0x2badb002);
}

#undef DbgPrint
ULONG
DbgPrint(const char *Format, ...)
{
	va_list ap;
	CHAR Buffer[512];
	ULONG Length;

	va_start(ap, Format);

	/* Construct a string */
	Length = _vsnprintf(Buffer, 512, Format, ap);

	/* Check if we went past the buffer */
	if (Length == -1)
	{
		/* Terminate it if we went over-board */
		Buffer[sizeof(Buffer) - 1] = '\n';

		/* Put maximum */
		Length = sizeof(Buffer);
	}

	/* Show it as a message box */
	UiMessageBox(Buffer);

	/* Cleanup and exit */
	va_end(ap);
	return 0;
}

/* EOF */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -