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

📄 kfcopy.c

📁 操作系统引导的一些介绍,对学习引导的有很大的帮助与提高
💻 C
📖 第 1 页 / 共 2 页
字号:
//
// (C) Copyright 1997 OSR Open Systems Resources, Inc.
// All Rights Reserved.
//
//
// This program is intended for use with the KFC kernel mode driver.
//

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory.h>
#include <../drv/kfc.h>

#define KFC_DRIVER_NAME "osr-kfc"
#define KFC_DRIVER_BINARY "\\osr-kfc.sys"

//
// Forward declarations.
//

BOOL
InstallDriver( IN SC_HANDLE  SchSCManager,
               IN LPCTSTR    DriverName,
               IN LPCTSTR    ServiceExe );

BOOL
RemoveDriver( IN SC_HANDLE  SchSCManager,
              IN LPCTSTR    DriverName );

BOOL
StartDriver( IN SC_HANDLE  SchSCManager,
             IN LPCTSTR    DriverName );

BOOL
StopDriver( IN SC_HANDLE  SchSCManager,
            IN LPCTSTR    DriverName );



//
// Main:
//
//  This is the entry point for this function.  Caller provides the input and output
//  names on the command line.
//
// Inputs:
//   argc - the argument count (# of arguments).  Minimum is one (the name of the program)
//   argv - the array of pointers to the arguments on the input line.
//
// Outputs:
//   None.
//
// Returns:
//  Various.


int _cdecl main(ULONG argc, LPSTR *argv)
{
    LPSTR sourceFileName = 0, targetFileName = 0;
    HANDLE sourceFileHandle, targetFileHandle;
    HANDLE kfcDriverHandle;
    HANDLE handles[2];
    DWORD outputSize;
    SC_HANDLE scmHandle;
    PVOID driverBinaryName;
    DWORD driverBinaryNameLength;
    LPVOID lpMsgBuf;

    if (argc != 3) {
        //
        // This version requires that both names be specified
        // on the command line.
        //

        fprintf(stderr, "Usage: kfcopy <source> <target>\n");

        return 1;

    }

    sourceFileName = argv[1];
    
    targetFileName = argv[2];

    //
    // Open the service control manager
    //

    scmHandle = OpenSCManager(NULL, // use local system's SCM
                              NULL, // default database
                              SC_MANAGER_ALL_ACCESS); // we want to do EVERYTHING

    if (INVALID_HANDLE_VALUE == scmHandle) {
        //
        // Could not open SCM.
        //

        fprintf(stderr, "Unable to contact the Service Control Manager\n");

        return 2;
    }

    //
    // First, try to open the KFC driver.
    //

    kfcDriverHandle = CreateFile("\\\\.\\OsrKfc",
                                 0, // any access
                                 FILE_SHARE_READ|FILE_SHARE_WRITE,
                                 0, // no security
                                 OPEN_EXISTING, // must already exist
                                 0,
                                 0);

    if (INVALID_HANDLE_VALUE == kfcDriverHandle) {
        //
        // Open failed.  Try to install and load the KFC driver.
        //

        //
        // Compute the path to the binary (this directory)
        //

        driverBinaryNameLength = GetCurrentDirectory(0, driverBinaryName);

        driverBinaryNameLength += strlen(KFC_DRIVER_BINARY);

        driverBinaryName = malloc(driverBinaryNameLength);

        if (!driverBinaryName) {
            //
            // malloc failed
            //

            fprintf(stderr, "Malloc failed\n");

            return 3;

        }

        GetCurrentDirectory(driverBinaryNameLength, driverBinaryName);

        strcat(driverBinaryName, KFC_DRIVER_BINARY);

        printf("driverBinaryName = %s\n", driverBinaryName);

        //
        // First, remove it but ignore the error value.  That's to
        // ensure we start with a clean registry key.
        //

        (void) RemoveDriver(scmHandle, KFC_DRIVER_NAME);

        //
        // Now, install the driver.
        //

        if (!InstallDriver(scmHandle, KFC_DRIVER_NAME, driverBinaryName)) {

            //
            // Driver installation failed
            //

            fprintf(stderr, "KFC driver could not be installed\n");

            return 4;

        }

        if (!StartDriver(scmHandle, KFC_DRIVER_NAME)) {
            //
            // Driver start failed.
            //

            fprintf(stderr, "KFC driver could not be started\n");

            return 5;
        }

        //
        // Try opening the KFC driver again.
        //

        kfcDriverHandle = CreateFile("\\\\.\\OsrKfc",
                                     0, // any access
                                     FILE_SHARE_READ|FILE_SHARE_WRITE,
                                     0, // no security
                                     OPEN_EXISTING, // must already exist
                                     0,
                                     0);

        if (INVALID_HANDLE_VALUE == kfcDriverHandle) {
            //
            // Everything failed
            //

            fprintf(stderr, "Unable to load/open driver\n");

            return 6;
        }

    }

    //
    // Now, open the source file.
    //

    sourceFileHandle = CreateFile(sourceFileName,
                                  GENERIC_READ,
                                  FILE_SHARE_READ,
                                  0,
                                  OPEN_EXISTING,
                                  0,
                                  0);

    if (INVALID_HANDLE_VALUE == sourceFileHandle) {
        //
        // Open failed.
        //

        fprintf(stderr, "Unable to open source file %s\n", sourceFileName);

        // XXX: for testing purposes ignore errors here.
        // return 7;
    }

    //
    // Finally, create the target file.
    //

    targetFileHandle = CreateFile(targetFileName,
                                  GENERIC_WRITE,
                                  0, // no sharing
                                  0,
                                  CREATE_ALWAYS,
                                  FILE_ATTRIBUTE_ARCHIVE,
                                  0);

    if (INVALID_HANDLE_VALUE == sourceFileHandle) {
        //
        // Open failed.
        //

        fprintf(stderr, "Unable to open taret file %s\n", targetFileName);

        // XXX: for testing purposes ignore errors here.
        // return 8;

    }
    
    //
    // OK.  Build the two handle array we need for the call to KFC.
    //

    handles[0] = sourceFileHandle;

    handles[1] = targetFileHandle;

    printf("handles[0] = 0x%x\n", handles[0]);

    printf("handles[1] = 0x%x\n", handles[1]);

    if (!DeviceIoControl(kfcDriverHandle,
                         KFC_COPY_FILE,
                         handles,
                         sizeof(handles),
                         0, // no output
                         0,
                         &outputSize,
                         0)) {
        DWORD lastError = GetLastError();

        fprintf(stderr, "Copy failed (0x%x)\n", lastError);

        FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
                       NULL,
                       lastError,
                       MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
                       (LPTSTR) &lpMsgBuf,
                       0,
                       NULL);

        fprintf(stderr, "*** %s\n", lpMsgBuf);

        LocalFree( lpMsgBuf );

        return 9;

    }

    //
    // If we make it to this point, we've succeeded.
    //

    CloseHandle(sourceFileHandle);

    FlushFileBuffers(targetFileHandle);

    CloseHandle(targetFileHandle);

    //
    // Stop the driver
    //

    (void) StopDriver(scmHandle, KFC_DRIVER_NAME);

    //
    // Uninstall the driver
    //

    (void) RemoveDriver(scmHandle, KFC_DRIVER_NAME);

    //
    // Close the SCM
    //

    CloseServiceHandle(scmHandle);

    return 0;
}



⌨️ 快捷键说明

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