efildrimage.c

来自「EFI BIOS是Intel提出的下一代的BIOS标准。这里上传的Edk源代码是」· C语言 代码 · 共 187 行

C
187
字号
/*++

Copyright 2006, Intel Corporation                                                         
All rights reserved. This program and the accompanying materials                          
are licensed and made available under the terms and conditions of the BSD License         
which accompanies this distribution.  The full text of the license may be found at        
http://opensource.org/licenses/bsd-license.php                                            
                                                                                          
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             

Module Name:

    efildrimage.c
    
Abstract:

    Creates and EFILDR image

Revision History

--*/


#include <windows.h>
#include <stdio.h>
#include "Tiano.h"

#define MAX_PE_IMAGES                  63
#define FILE_TYPE_FIXED_LOADER         0
#define FILE_TYPE_RELOCATABLE_PE_IMAGE 1

typedef struct {
    UINT32 CheckSum;
    UINT32 Offset;
    UINT32 Length;
    UINT8  FileName[52];
} EFILDR_IMAGE;

typedef struct {          
    UINT32       Signature;     
    UINT32       HeaderCheckSum;
    UINT32       FileLength;
    UINT32       NumberOfImages;
} EFILDR_HEADER;



VOID
Usage (
    VOID
    )
{
    printf ("Usage: EfiLdrImage OutImage LoaderImage PeImage1 PeImage2 ... PeImageN");
    exit (1);
}

ULONG
FCopyFile (
    FILE    *in,
    FILE    *out
    )
{
    ULONG           filesize, offset, length;
    UCHAR           Buffer[8*1024];

    fseek (in, 0, SEEK_END);
    filesize = ftell(in);

    fseek (in, 0, SEEK_SET);

    offset = 0;
    while (offset < filesize)  {
        length = sizeof(Buffer);
        if (filesize-offset < length) {
            length = filesize-offset;
        }

        fread (Buffer, length, 1, in);
        fwrite (Buffer, length, 1, out);
        offset += length;
    }

    return(filesize);
}


int
main (
    int argc,
    char *argv[]
    )
/*++

Routine Description:


Arguments:


Returns:


--*/
{
    ULONG         i;
    ULONG         filesize;
    FILE          *fpIn, *fpOut;
    EFILDR_HEADER EfiLdrHeader;
    EFILDR_IMAGE  EfiLdrImage[MAX_PE_IMAGES];

    if (argc < 4) {
        Usage();
    }

    memset(&EfiLdrHeader,0,sizeof(EfiLdrHeader));
    strcpy((UCHAR *)(&EfiLdrHeader.Signature),"EFIL");

    //
    // open output file
    //

    fpOut = fopen(argv[1], "w+b");
    if (!fpOut) {
        printf ("efildrimage: Could not open output file %s\n", argv[1]);
    exit(1);
    }
    fseek (fpOut, 0, SEEK_SET);
    fwrite (&EfiLdrHeader, sizeof(EFILDR_HEADER)        , 1, fpOut);
    fwrite (&EfiLdrImage , sizeof(EFILDR_IMAGE)*(argc-2), 1, fpOut);


    EfiLdrHeader.FileLength = sizeof(EFILDR_HEADER) + sizeof(EFILDR_IMAGE)*(argc-2);

    //
    // copy all the input files to the output file
    //

    for(i=2;i<(ULONG)argc;i++) {
        
        //
        // open a PeImage file
        //

        fpIn = fopen (argv[i], "rb");
        if (!fpIn) {
            printf ("efildrimage: Could not open input file %s\n", argv[i-2]);
        exit(1);
        }

        //
        // Copy the file
        //

        filesize = FCopyFile (fpIn, fpOut);

        EfiLdrImage[i-2].Offset = EfiLdrHeader.FileLength;
        EfiLdrImage[i-2].Length = filesize;
        strcpy(EfiLdrImage[i-2].FileName,argv[i]);
        EfiLdrHeader.FileLength += filesize;
        EfiLdrHeader.NumberOfImages++;

        //
        // Close the PeImage file
        //

        fclose(fpIn);
    }

    //
    // Write the image header to the output file
    //

    fseek (fpOut, 0, SEEK_SET);
    fwrite (&EfiLdrHeader, sizeof(EFILDR_HEADER)        , 1, fpOut);
    fwrite (&EfiLdrImage , sizeof(EFILDR_IMAGE)*(argc-2), 1, fpOut);

    //
    // Close the OutImage file
    //

    fclose(fpOut);

    printf ("Created %s\n", argv[1]);
    return 0;
}

⌨️ 快捷键说明

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