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

📄 reactos.c

📁 ReactOS是一些高手根据Windows XP的内核编写出的类XP。内核实现机理和API函数调用几乎相同。甚至可以兼容XP的程序。喜欢研究系统内核的人可以看一看。
💻 C
📖 第 1 页 / 共 2 页
字号:
/*
 *  FreeLoader
 *
 *  Copyright (C) 1998-2003  Brian Palmer  <brianp@sginet.com>
 *  Copyright (C) 2005       Alex Ionescu  <alex@relsoft.net>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <freeldr.h>
#include <debug.h>

ROS_LOADER_PARAMETER_BLOCK LoaderBlock;
char					reactos_kernel_cmdline[255];	// Command line passed to kernel
LOADER_MODULE			reactos_modules[64];		// Array to hold boot module info loaded for the kernel
char					reactos_module_strings[64][256];	// Array to hold module names
unsigned long			reactos_memory_map_descriptor_size;
memory_map_t			reactos_memory_map[32];		// Memory map
ARC_DISK_SIGNATURE      reactos_arc_disk_info[32]; // ARC Disk Information
char                    reactos_arc_strings[32][256];
unsigned long           reactos_disk_count = 0;
CHAR szHalName[255];
CHAR szBootPath[255];
CHAR SystemRoot[255];
static CHAR szLoadingMsg[] = "Loading ReactOS...";
BOOLEAN FrLdrBootType;
extern ULONG_PTR KernelBase, KernelEntry;

BOOLEAN
FrLdrLoadDriver(PCHAR szFileName,
                INT nPos)
{
    PFILE FilePointer;
    CHAR value[256], *FinalSlash;
    LPSTR p;

    if (!_stricmp(szFileName, "hal.dll"))
    {
        /* Use the boot.ini name instead */
        szFileName = szHalName;
    }

    FinalSlash = strrchr(szFileName, '\\');
    if(FinalSlash)
	szFileName = FinalSlash + 1;

    /* Open the Driver */
    FilePointer = FsOpenFile(szFileName);

    /* Try under the system root in the main dir and drivers */
    if (FilePointer == NULL)
    {
	strcpy(value, SystemRoot);
	if(value[strlen(value)-1] != '\\')
	    strcat(value, "\\");
	strcat(value, szFileName);
	FilePointer = FsOpenFile(value);
    }

    if (FilePointer == NULL)
    {
	strcpy(value, SystemRoot);
	if(value[strlen(value)-1] != '\\')
	    strcat(value, "\\");
	strcat(value, "SYSTEM32\\");
	strcat(value, szFileName);
	FilePointer = FsOpenFile(value);
    }

    if (FilePointer == NULL)
    {
	strcpy(value, SystemRoot);
	if(value[strlen(value)-1] != '\\')
	    strcat(value, "\\");
	strcat(value, "SYSTEM32\\DRIVERS\\");
	strcat(value, szFileName);
	FilePointer = FsOpenFile(value);
    }

    /* Make sure we did */
    if (FilePointer == NULL) {

        /* Fail if file wasn't opened */
        strcpy(value, szFileName);
        strcat(value, " not found.");
        return(FALSE);
    }

    /* Update the status bar with the current file */
    strcpy(value, "Reading ");
    p = strrchr(szFileName, '\\');
    if (p == NULL) {

        strcat(value, szFileName);

    } else {

        strcat(value, p + 1);

    }
    UiDrawStatusText(value);

    /* Load the driver */
    FrLdrMapImage(FilePointer, szFileName, 0);

    /* Update status and return */
    UiDrawProgressBarCenter(nPos, 100, szLoadingMsg);
    return(TRUE);
}

PVOID
NTAPI
FrLdrLoadImage(IN PCHAR szFileName,
               IN INT nPos,
               IN ULONG ImageType)
{
    PFILE FilePointer;
    PCHAR szShortName;
    CHAR szBuffer[256], szFullPath[256];
    PVOID LoadBase;

    /* Extract filename without path */
    szShortName = strrchr(szFileName, '\\');
    if (!szShortName)
    {
        /* No path, leave it alone */
        szShortName = szFileName;

        /* Which means we need to build a path now */
        strcpy(szBuffer, szFileName);
        strcpy(szFullPath, szBootPath);
        if (!FrLdrBootType)
        {
            strcat(szFullPath, "SYSTEM32\\DRIVERS\\");
        }
        else
        {
            strcat(szFullPath, "\\");
        }
        strcat(szFullPath, szBuffer);
        szFileName = szFullPath;
    }
    else
    {
        /* Skip the path */
        szShortName = szShortName + 1;
    }

    /* Open the image */
    FilePointer = FsOpenFile(szFileName);
    if (!FilePointer)
    {
        /* Return failure on the short name */
        strcpy(szBuffer, szShortName);
        strcat(szBuffer, " not found.");
        UiMessageBox(szBuffer);
        return FALSE;
    }

    /* Update the status bar with the current file */
    strcpy(szBuffer, "Reading ");
    strcat(szBuffer, szShortName);
    UiDrawStatusText(szBuffer);

    /* Do the actual loading */
    LoadBase = FrLdrMapImage(FilePointer, szShortName, ImageType);

    /* Update Processbar and return success */
    if (!FrLdrBootType) UiDrawProgressBarCenter(nPos, 100, szLoadingMsg);
    return LoadBase;
}

static BOOLEAN
FrLdrLoadNlsFile(PCSTR szFileName,
                 PCSTR szModuleName)
{
    PFILE FilePointer;
    CHAR value[256];
    LPSTR p;

    /* Open the Driver */
    FilePointer = FsOpenFile(szFileName);

    /* Make sure we did */
    if (FilePointer == NULL) {

        /* Fail if file wasn't opened */
        strcpy(value, szFileName);
        strcat(value, " not found.");
        UiMessageBox(value);
        return(FALSE);
    }

    /* Update the status bar with the current file */
    strcpy(value, "Reading ");
    p = strrchr(szFileName, '\\');
    if (p == NULL) {

        strcat(value, szFileName);

    } else {

        strcat(value, p + 1);
    }
    UiDrawStatusText(value);

    /* Load the driver */
    FrLdrLoadModule(FilePointer, szModuleName, NULL);
    return(TRUE);
}

static BOOLEAN
FrLdrLoadNlsFiles(PCHAR szSystemRoot,
                  PCHAR szErrorOut)
{
    LONG rc = ERROR_SUCCESS;
    FRLDRHKEY hKey;
    WCHAR szIdBuffer[80];
    WCHAR szNameBuffer[80];
    CHAR szFileName[256];
    ULONG BufferSize;

    /* open the codepage key */
    rc = RegOpenKey(NULL,
                    L"\\Registry\\Machine\\SYSTEM\\CurrentControlSet\\Control\\NLS\\CodePage",
                    &hKey);
    if (rc != ERROR_SUCCESS) {

        strcpy(szErrorOut, "Couldn't open CodePage registry key");
        return(FALSE);
    }

    /* get ANSI codepage */
    BufferSize = sizeof(szIdBuffer);
    rc = RegQueryValue(hKey, L"ACP", NULL, (PUCHAR)szIdBuffer, &BufferSize);
    if (rc != ERROR_SUCCESS) {

        strcpy(szErrorOut, "Couldn't get ACP NLS setting");
        return(FALSE);
    }

    BufferSize = sizeof(szNameBuffer);
    rc = RegQueryValue(hKey, szIdBuffer, NULL, (PUCHAR)szNameBuffer, &BufferSize);
    if (rc != ERROR_SUCCESS) {

        strcpy(szErrorOut, "ACP NLS Setting exists, but isn't readable");
        return(FALSE);
    }

    /* load ANSI codepage table */
    sprintf(szFileName,"%ssystem32\\%S", szSystemRoot, szNameBuffer);
    DbgPrint((DPRINT_REACTOS, "ANSI file: %s\n", szFileName));
    if (!FrLdrLoadNlsFile(szFileName, "ansi.nls")) {

        strcpy(szErrorOut, "Couldn't load ansi.nls");
        return(FALSE);
    }

    /* get OEM codepage */
    BufferSize = sizeof(szIdBuffer);
    rc = RegQueryValue(hKey, L"OEMCP", NULL, (PUCHAR)szIdBuffer, &BufferSize);
    if (rc != ERROR_SUCCESS) {

        strcpy(szErrorOut, "Couldn't get OEMCP NLS setting");
        return(FALSE);
    }

    BufferSize = sizeof(szNameBuffer);
    rc = RegQueryValue(hKey, szIdBuffer, NULL, (PUCHAR)szNameBuffer, &BufferSize);
    if (rc != ERROR_SUCCESS) {

        strcpy(szErrorOut, "OEMCP NLS setting exists, but isn't readable");
        return(FALSE);
    }

    /* load OEM codepage table */
    sprintf(szFileName, "%ssystem32\\%S", szSystemRoot, szNameBuffer);
    DbgPrint((DPRINT_REACTOS, "Oem file: %s\n", szFileName));
    if (!FrLdrLoadNlsFile(szFileName, "oem.nls")) {

        strcpy(szErrorOut, "Couldn't load oem.nls");
        return(FALSE);
    }

    /* open the language key */
    rc = RegOpenKey(NULL,
                    L"\\Registry\\Machine\\SYSTEM\\CurrentControlSet\\Control\\NLS\\Language",
                    &hKey);
    if (rc != ERROR_SUCCESS) {

        strcpy(szErrorOut, "Couldn't open Language registry key");
        return(FALSE);
    }

    /* get the Unicode case table */
    BufferSize = sizeof(szIdBuffer);
    rc = RegQueryValue(hKey, L"Default", NULL, (PUCHAR)szIdBuffer, &BufferSize);
    if (rc != ERROR_SUCCESS) {

        strcpy(szErrorOut, "Couldn't get Language Default setting");
        return(FALSE);
    }

    BufferSize = sizeof(szNameBuffer);
    rc = RegQueryValue(hKey, szIdBuffer, NULL, (PUCHAR)szNameBuffer, &BufferSize);
    if (rc != ERROR_SUCCESS) {

        strcpy(szErrorOut, "Language Default setting exists, but isn't readable");
        return(FALSE);
    }

    /* load Unicode case table */
    sprintf(szFileName, "%ssystem32\\%S", szSystemRoot, szNameBuffer);
    DbgPrint((DPRINT_REACTOS, "Casemap file: %s\n", szFileName));
    if (!FrLdrLoadNlsFile(szFileName, "casemap.nls")) {

        strcpy(szErrorOut, "casemap.nls");
        return(FALSE);
    }

    return(TRUE);
}

static VOID
FrLdrLoadBootDrivers(PCHAR szSystemRoot,
                     INT nPos)
{
    LONG rc = 0;
    FRLDRHKEY hGroupKey, hOrderKey, hServiceKey, hDriverKey;
    WCHAR GroupNameBuffer[512];
    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];

    /* get 'service group order' key */
    rc = RegOpenKey(NULL,
                    L"\\Registry\\Machine\\SYSTEM\\CurrentControlSet\\Control\\ServiceGroupOrder",
                    &hGroupKey);
    if (rc != ERROR_SUCCESS) {

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

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

        DbgPrint((DPRINT_REACTOS, "Failed to open the 'Services' key (rc %d)\n", (int)rc));
        return;
    }

    /* Get the Name Group */
    BufferSize = sizeof(GroupNameBuffer);
    rc = RegQueryValue(hGroupKey, L"List", NULL, (PUCHAR)GroupNameBuffer, &BufferSize);
    DbgPrint((DPRINT_REACTOS, "RegQueryValue(): rc %d\n", (int)rc));
    if (rc != ERROR_SUCCESS) return;
    DbgPrint((DPRINT_REACTOS, "BufferSize: %d \n", (int)BufferSize));
    DbgPrint((DPRINT_REACTOS, "GroupNameBuffer: '%S' \n", GroupNameBuffer));

    /* Loop through each group */
    GroupName = GroupNameBuffer;
    while (*GroupName) {
        DbgPrint((DPRINT_REACTOS, "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);
                DbgPrint((DPRINT_REACTOS, "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) 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));

⌨️ 快捷键说明

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