int main ( int argc, char **argv ) { VOID *BaseMemory; INTN result; CHAR8 *OutputFile = NULL; CHAR8 *InputFile = NULL; EFI_STATUS Status; UINT64 TempValue; SetUtilityName("GenPage"); if (argc == 1) { Usage(); return STATUS_ERROR; } argc --; argv ++; if ((stricmp (argv[0], "-h") == 0) || (stricmp (argv[0], "--help") == 0)) { Usage(); return 0; } if (stricmp (argv[0], "--version") == 0) { Version(); return 0; } while (argc > 0) { if ((stricmp (argv[0], "-o") == 0) || (stricmp (argv[0], "--output") == 0)) { if (argv[1] == NULL || argv[1][0] == '-') { Error (NULL, 0, 1003, "Invalid option value", "Output file is missing for -o option"); return STATUS_ERROR; } OutputFile = argv[1]; argc -= 2; argv += 2; continue; } if ((stricmp (argv[0], "-b") == 0) || (stricmp (argv[0], "--baseaddr") == 0)) { if (argv[1] == NULL || argv[1][0] == '-') { Error (NULL, 0, 1003, "Invalid option value", "Base address is missing for -b option"); return STATUS_ERROR; } Status = AsciiStringToUint64 (argv[1], FALSE, &TempValue); if (EFI_ERROR (Status)) { Error (NULL, 0, 1003, "Invalid option value", "Base address is not valid intergrator"); return STATUS_ERROR; } gPageTableBaseAddress = (UINT32) TempValue; argc -= 2; argv += 2; continue; } if ((stricmp (argv[0], "-f") == 0) || (stricmp (argv[0], "--offset") == 0)) { if (argv[1] == NULL || argv[1][0] == '-') { Error (NULL, 0, 1003, "Invalid option value", "Offset is missing for -f option"); return STATUS_ERROR; } Status = AsciiStringToUint64 (argv[1], FALSE, &TempValue); if (EFI_ERROR (Status)) { Error (NULL, 0, 1003, "Invalid option value", "Offset is not valid intergrator"); return STATUS_ERROR; } gPageTableOffsetInFile = (UINT32) TempValue; argc -= 2; argv += 2; continue; } if ((stricmp (argv[0], "-q") == 0) || (stricmp (argv[0], "--quiet") == 0)) { argc --; argv ++; continue; } if ((stricmp (argv[0], "-v") ==0) || (stricmp (argv[0], "--verbose") == 0)) { argc --; argv ++; continue; } if ((stricmp (argv[0], "-d") == 0) || (stricmp (argv[0], "--debug") == 0)) { if (argv[1] == NULL || argv[1][0] == '-') { Error (NULL, 0, 1003, "Invalid option value", "Debug Level is not specified."); return STATUS_ERROR; } Status = AsciiStringToUint64 (argv[1], FALSE, &TempValue); if (EFI_ERROR (Status)) { Error (NULL, 0, 1003, "Invalid option value", "Debug Level is not valid intergrator."); return STATUS_ERROR; } if (TempValue > 9) { Error (NULL, 0, 1003, "Invalid option value", "Debug Level range is 0-9, currnt input level is %d", (int) TempValue); return STATUS_ERROR; } argc -= 2; argv += 2; continue; } if (argv[0][0] == '-') { Error (NULL, 0, 1000, "Unknown option", argv[0]); return STATUS_ERROR; } // // Don't recognize the parameter. // InputFile = argv[0]; argc--; argv++; } if (InputFile == NULL) { Error (NULL, 0, 1003, "Invalid option value", "Input file is not specified"); return STATUS_ERROR; } // // Create X64 page table // BaseMemory = CreateIdentityMappingPageTables (); // // Add page table to binary file // result = GenBinPage (BaseMemory, InputFile, OutputFile); if (result < 0) { return STATUS_ERROR; } return 0; }
int main ( int argc, char *argv[] ) /*++ Routine Description: Arguments: Returns: --*/ { UINT64 i; UINT64 filesize; FILE *fpIn, *fpOut; EFILDR_HEADER EfiLdrHeader; EFILDR_IMAGE EfiLdrImage[MAX_PE_IMAGES]; CHAR8* OutputFileName = NULL; CHAR8* InputFileNames[MAX_PE_IMAGES + 1]; UINT8 InputFileCount = 0; UINT64 DebugLevel = 0; UINT64 VerboseLevel = 0; EFI_STATUS Status = EFI_SUCCESS; SetUtilityName (UTILITY_NAME); if (argc == 1) { Usage(); return STATUS_ERROR; } argc --; argv ++; if ((stricmp (argv[0], "-h") == 0) || (stricmp (argv[0], "--help") == 0)) { Usage(); return STATUS_SUCCESS; } if (stricmp (argv[0], "--version") == 0) { Version(); return STATUS_SUCCESS; } while (argc > 0) { if ((stricmp (argv[0], "-o") == 0) || (stricmp (argv[0], "--output") == 0)) { OutputFileName = argv[1]; if (OutputFileName == NULL) { Error (NULL, 0, 1003, "Invalid option value", "Output file can't be null"); return STATUS_ERROR; } argc -= 2; argv += 2; continue; } if ((stricmp (argv[0], "-q") == 0) || (stricmp (argv[0], "--quiet") == 0)) { argc --; argv ++; continue; } if ((strlen(argv[0]) >= 2 && argv[0][0] == '-' && (argv[0][1] == 'v' || argv[0][1] == 'V')) || (stricmp (argv[0], "--verbose") == 0)) { VerboseLevel = 1; if (strlen(argv[0]) > 2) { Status = CountVerboseLevel (&argv[0][2], strlen(argv[0]) - 2, &VerboseLevel); if (EFI_ERROR (Status)) { Error (NULL, 0, 1003, "Invalid option value", argv[0]); return STATUS_ERROR; } } argc --; argv ++; continue; } if ((stricmp (argv[0], "-d") == 0) || (stricmp (argv[0], "--debug") == 0)) { Status = AsciiStringToUint64 (argv[1], FALSE, &DebugLevel); if (EFI_ERROR (Status)) { Error (NULL, 0, 1003, "Invalid option value", "%s = %s", argv[0], argv[1]); return STATUS_ERROR; } argc -= 2; argv += 2; continue; } // // Don't recognize the parameter, should be regarded as the input file name. // InputFileNames[InputFileCount] = argv[0]; InputFileCount++; argc--; argv++; } if (InputFileCount == 0) { Error (NULL, 0, 1001, "Missing option", "No input file"); return STATUS_ERROR; } // // Open output file for write // if (OutputFileName == NULL) { Error (NULL, 0, 1001, "Missing option", "No output file"); return STATUS_ERROR; } fpOut = fopen (LongFilePath (OutputFileName), "w+b"); if (!fpOut) { Error (NULL, 0, 0001, "Could not open output file", OutputFileName); return STATUS_ERROR; } memset (&EfiLdrHeader, 0, sizeof (EfiLdrHeader)); memset (&EfiLdrImage, 0, sizeof (EFILDR_IMAGE) * (InputFileCount)); memcpy (&EfiLdrHeader.Signature, "EFIL", 4); EfiLdrHeader.FileLength = sizeof(EFILDR_HEADER) + sizeof(EFILDR_IMAGE)*(InputFileCount); // // Skip the file header first // fseek (fpOut, EfiLdrHeader.FileLength, SEEK_SET); // // copy all the input files to the output file // for(i=0;i<InputFileCount;i++) { // // Copy the content of PeImage file to output file // fpIn = fopen (LongFilePath (InputFileNames[i]), "rb"); if (!fpIn) { Error (NULL, 0, 0001, "Could not open input file", InputFileNames[i]); fclose (fpOut); return STATUS_ERROR; } filesize = FCopyFile (fpIn, fpOut); fclose(fpIn); // // And in the same time update the EfiLdrHeader and EfiLdrImage array // EfiLdrImage[i].Offset = EfiLdrHeader.FileLength; EfiLdrImage[i].Length = (UINT32) filesize; strncpy ((CHAR8*) EfiLdrImage[i].FileName, InputFileNames[i], sizeof (EfiLdrImage[i].FileName) - 1); EfiLdrHeader.FileLength += (UINT32) filesize; EfiLdrHeader.NumberOfImages++; } // // Write the image header to the output file finally // fseek (fpOut, 0, SEEK_SET); fwrite (&EfiLdrHeader, sizeof(EFILDR_HEADER) , 1, fpOut); fwrite (&EfiLdrImage , sizeof(EFILDR_IMAGE)*(InputFileCount), 1, fpOut); fclose (fpOut); printf ("Created %s\n", OutputFileName); return 0; }