зеркало из https://github.com/microsoft/lsvmtools.git
Added serialize spec file for testing
This commit is contained in:
Родитель
425eba74d7
Коммит
89ffe14735
|
@ -142,7 +142,7 @@ int LoadDecryptCopySpecializeFile(
|
|||
UINTN mkSize;
|
||||
UINT8* specializeData = NULL;
|
||||
UINTN specializeSize = 0;
|
||||
SPECIALIZATION_RESULT* specFiles = NULL;
|
||||
SPECIALIZATION_FILE* specFiles = NULL;
|
||||
UINTN numSpecFiles;
|
||||
|
||||
/* Check for null parameters */
|
||||
|
|
|
@ -4411,6 +4411,155 @@ done:
|
|||
return status;
|
||||
}
|
||||
|
||||
static int _serialize_specfile_command(
|
||||
int argc,
|
||||
const char **argv)
|
||||
{
|
||||
const char* indir;
|
||||
DIR* dir = NULL;
|
||||
struct dirent *files = NULL;
|
||||
UINT32 numfiles = 0;
|
||||
SPECIALIZATION_FILE* specFiles = NULL;
|
||||
UINT32 numSpecFiles = 0;
|
||||
size_t pathLen;
|
||||
const char* outfile;
|
||||
UINT8* outData = NULL;
|
||||
UINT32 outDataSize;
|
||||
int status = -1;
|
||||
char path[PATH_MAX];
|
||||
|
||||
if (argc != 3)
|
||||
{
|
||||
fprintf(stderr, "Usage: %s INDIR OUTFILE\n", argv[0]);
|
||||
goto done;
|
||||
}
|
||||
|
||||
indir = argv[1];
|
||||
outfile = argv[2];
|
||||
pathLen = strlen(indir);
|
||||
|
||||
if (pathLen + 1 + NAME_MAX + 1 > sizeof(path))
|
||||
{
|
||||
fprintf(stderr, "%s: directory path too long: %s\n", argv[0], indir);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Get the number of files, so we can allocate. */
|
||||
dir = opendir(indir);
|
||||
if (dir == NULL)
|
||||
{
|
||||
fprintf(stderr, "%s: Failed to open dir: %s\n", argv[0], indir);
|
||||
return -1;
|
||||
}
|
||||
|
||||
while ((files = readdir(dir)) != NULL)
|
||||
{
|
||||
numfiles++;
|
||||
}
|
||||
|
||||
if (closedir(dir) != 0)
|
||||
{
|
||||
fprintf(stderr, "%s: failed to close dir: %s\n", argv[0], indir);
|
||||
return -1;
|
||||
}
|
||||
dir = NULL;
|
||||
|
||||
/* Now actually get the files. */
|
||||
specFiles = (SPECIALIZATION_FILE*) malloc(numfiles * sizeof(SPECIALIZATION_FILE));
|
||||
if (specFiles == NULL)
|
||||
{
|
||||
fprintf(stderr, "%s: failed to allocate spec files\n", argv[0]);
|
||||
goto done;
|
||||
}
|
||||
|
||||
dir = opendir(indir);
|
||||
if (dir == NULL)
|
||||
{
|
||||
fprintf(stderr, "%s: Failed to open dir: %s\n", argv[0], indir);
|
||||
return -1;
|
||||
}
|
||||
|
||||
strncpy(path, indir, pathLen);
|
||||
while ((files = readdir(dir)) != NULL)
|
||||
{
|
||||
struct stat statbuf;
|
||||
SPECIALIZATION_FILE* cur = specFiles + numSpecFiles;
|
||||
size_t tmpSize;
|
||||
|
||||
path[pathLen] = '/';
|
||||
strncpy(path + pathLen + 1, files->d_name, NAME_MAX);
|
||||
path[pathLen + 1 + NAME_MAX] = 0;
|
||||
|
||||
if (lstat(path, &statbuf) != 0)
|
||||
{
|
||||
fprintf(stderr, "%s: Failed to stat: %s\n", argv[0], path);
|
||||
goto done;
|
||||
}
|
||||
|
||||
if (!S_ISREG(statbuf.st_mode))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
cur->FileName = (char*) malloc(strlen(files->d_name) + 1);
|
||||
if (cur->FileName == NULL)
|
||||
{
|
||||
fprintf(stderr, "%s: Failed to allocate specfile: %s\n", argv[0], path);
|
||||
goto done;
|
||||
}
|
||||
strncpy(cur->FileName, files->d_name, strlen(files->d_name) + 1);
|
||||
|
||||
|
||||
if (LoadFile(path, 0, &cur->PayloadData, &tmpSize) != 0)
|
||||
{
|
||||
free(cur->FileName);
|
||||
fprintf(stderr, "%s: Failed to load file. %s\n", argv[0], path);
|
||||
goto done;
|
||||
}
|
||||
|
||||
/* Truncate since we don't expect anything > 32 bits. */
|
||||
cur->PayloadSize = (UINT32) tmpSize;
|
||||
numSpecFiles++;
|
||||
}
|
||||
|
||||
if (CombineSpecFiles(
|
||||
specFiles,
|
||||
numSpecFiles,
|
||||
&outData,
|
||||
&outDataSize) != 0)
|
||||
{
|
||||
fprintf(stderr, "%s: failed to combine spec file.\n", argv[0]);
|
||||
goto done;
|
||||
}
|
||||
|
||||
if (PutFile(outfile, outData, outDataSize) != 0)
|
||||
{
|
||||
fprintf(stderr, "%s: failed to put outfile: %s\n", argv[0], outfile);
|
||||
goto done;
|
||||
}
|
||||
|
||||
status = 0;
|
||||
|
||||
|
||||
done:
|
||||
if (dir != NULL)
|
||||
{
|
||||
closedir(dir);
|
||||
}
|
||||
|
||||
if (specFiles != NULL)
|
||||
{
|
||||
FreeSpecFiles(specFiles, numSpecFiles);
|
||||
}
|
||||
|
||||
if (outData != NULL)
|
||||
{
|
||||
free(outData);
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
static int _deserialize_specfile_command(
|
||||
int argc,
|
||||
const char **argv)
|
||||
|
@ -4420,7 +4569,7 @@ static int _deserialize_specfile_command(
|
|||
const char* outfile;
|
||||
unsigned char* inData = NULL;
|
||||
size_t inDataSize;
|
||||
SPECIALIZATION_RESULT *r = NULL;
|
||||
SPECIALIZATION_FILE *r = NULL;
|
||||
UINTN rSize;
|
||||
UINTN i = 0;
|
||||
|
||||
|
@ -4860,6 +5009,11 @@ static Command _commands[] =
|
|||
"Deserializes the boot+rootkey from unsealed data",
|
||||
_deserializekeys_command,
|
||||
},
|
||||
{
|
||||
"serialize_specfile",
|
||||
"Serialize files in a directory to generate a specialization file.",
|
||||
_serialize_specfile_command,
|
||||
},
|
||||
{
|
||||
"deserialize_specfile",
|
||||
"Deserializes the specialization file",
|
||||
|
|
|
@ -32,6 +32,7 @@
|
|||
#include "specialize.h"
|
||||
#include "alloc.h"
|
||||
#include "strings.h"
|
||||
#include "print.h"
|
||||
|
||||
static BOOLEAN _Equals(CHAR16* p1, char* p2, UINTN s1, UINTN s2)
|
||||
{
|
||||
|
@ -53,7 +54,7 @@ static BOOLEAN _Equals(CHAR16* p1, char* p2, UINTN s1, UINTN s2)
|
|||
static int _WriteSingleSpecFile(
|
||||
const UINT8* dataCur,
|
||||
SPECIALIZATION_CLEAR_DATA_FILE_ENTRY* entry,
|
||||
SPECIALIZATION_RESULT* result)
|
||||
SPECIALIZATION_FILE* result)
|
||||
{
|
||||
/* FileNameSize does not have null terminator. */
|
||||
UINTN i = 0;
|
||||
|
@ -84,8 +85,8 @@ static int _WriteSingleSpecFile(
|
|||
fileNameSize = (sizeof(SPEC_SPECIALIZE_FILENAME) - 1) * sizeof(CHAR16);
|
||||
}
|
||||
|
||||
resultFileNameSize = fileNameSize / sizeof(CHAR16) + 1;
|
||||
result->FileName = (char *) Malloc(resultFileNameSize);
|
||||
resultFileNameSize = fileNameSize / sizeof(CHAR16);
|
||||
result->FileName = (char *) Malloc(resultFileNameSize + 1);
|
||||
if (result->FileName == NULL)
|
||||
{
|
||||
goto CleanupErr;
|
||||
|
@ -101,8 +102,8 @@ static int _WriteSingleSpecFile(
|
|||
{
|
||||
result->FileName[i] = (char) fileName[i];
|
||||
}
|
||||
fileName[resultFileNameSize] = 0;
|
||||
|
||||
result->FileName[resultFileNameSize] = 0;
|
||||
|
||||
Memcpy(result->PayloadData, dataCur + entry->FilePayloadOffset, entry->FilePayloadSize);
|
||||
result->PayloadSize = entry->FilePayloadSize;
|
||||
rc = 0;
|
||||
|
@ -124,11 +125,11 @@ Cleanup:
|
|||
{
|
||||
Free(fileNameTmp);
|
||||
}
|
||||
return -rc;
|
||||
return rc;
|
||||
}
|
||||
|
||||
static void _CleanupSpecFilesList(
|
||||
SPECIALIZATION_RESULT* result,
|
||||
SPECIALIZATION_FILE* result,
|
||||
UINTN resultSize)
|
||||
{
|
||||
UINTN i = 0;
|
||||
|
@ -136,6 +137,7 @@ static void _CleanupSpecFilesList(
|
|||
{
|
||||
Free(result->FileName);
|
||||
Free(result->PayloadData);
|
||||
result++;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -144,11 +146,12 @@ static int _WriteSpecResults(
|
|||
SPECIALIZATION_CLEAR_DATA_HEADER* hdr,
|
||||
const UINT8* data,
|
||||
UINTN size,
|
||||
SPECIALIZATION_RESULT* result,
|
||||
SPECIALIZATION_FILE* result,
|
||||
UINTN resultSize)
|
||||
{
|
||||
const UINT8* dataCur = data;
|
||||
UINT32 i = 0;
|
||||
int rc = -1;
|
||||
|
||||
if (resultSize != hdr->FileCount)
|
||||
{
|
||||
|
@ -174,7 +177,7 @@ static int _WriteSpecResults(
|
|||
goto Cleanup;
|
||||
}
|
||||
|
||||
if (_WriteSingleSpecFile(dataCur, entry, &result[i]) != 0)
|
||||
if (_WriteSingleSpecFile(dataCur, entry, result + i) != 0)
|
||||
{
|
||||
goto Cleanup;
|
||||
}
|
||||
|
@ -187,7 +190,8 @@ static int _WriteSpecResults(
|
|||
}
|
||||
dataCur += max;
|
||||
}
|
||||
return 0;
|
||||
rc = 0;
|
||||
return rc;
|
||||
|
||||
Cleanup:
|
||||
/* We allocated some memory from _WriteSingleSpecFile that we should clean up. */
|
||||
|
@ -198,11 +202,11 @@ Cleanup:
|
|||
int ExtractSpecFiles(
|
||||
const UINT8* data,
|
||||
UINTN size,
|
||||
SPECIALIZATION_RESULT** result,
|
||||
SPECIALIZATION_FILE** result,
|
||||
UINTN* resultSize)
|
||||
{
|
||||
SPECIALIZATION_CLEAR_DATA_HEADER* hdr;
|
||||
SPECIALIZATION_RESULT* resultLocal = NULL;
|
||||
SPECIALIZATION_FILE* resultLocal = NULL;
|
||||
int rc = -1;
|
||||
|
||||
/* Check size of data */
|
||||
|
@ -216,7 +220,7 @@ int ExtractSpecFiles(
|
|||
data += sizeof(*hdr);
|
||||
size -= sizeof(*hdr);
|
||||
|
||||
resultLocal = (SPECIALIZATION_RESULT*) Malloc(hdr->FileCount * sizeof(SPECIALIZATION_RESULT));
|
||||
resultLocal = (SPECIALIZATION_FILE*) Malloc(hdr->FileCount * sizeof(SPECIALIZATION_FILE));
|
||||
if (resultLocal == NULL)
|
||||
{
|
||||
goto Cleanup;
|
||||
|
@ -239,8 +243,68 @@ Cleanup:
|
|||
return rc;
|
||||
}
|
||||
|
||||
int CombineSpecFiles(
|
||||
const SPECIALIZATION_FILE* files,
|
||||
UINT32 numFiles,
|
||||
UINT8** result,
|
||||
UINT32* resultSize)
|
||||
{
|
||||
UINT32 i;
|
||||
UINT8* resultLocal = NULL;
|
||||
UINT32 totalSize = SPECIALIZATION_CLEAR_DATA_HEADER_SIZE +
|
||||
numFiles * SPECIALIZATION_CLEAR_DATA_FILE_ENTRY_SIZE;
|
||||
|
||||
for (i = 0; i < numFiles; i++)
|
||||
{
|
||||
totalSize += files[i].PayloadSize + sizeof(CHAR16)*Strlen(files[i].FileName);
|
||||
}
|
||||
|
||||
resultLocal = (UINT8*) Malloc(totalSize);
|
||||
if (resultLocal == NULL)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
*result = resultLocal;
|
||||
*resultSize = totalSize;
|
||||
|
||||
/* Set main header. */
|
||||
{
|
||||
SPECIALIZATION_CLEAR_DATA_HEADER* tmp = (SPECIALIZATION_CLEAR_DATA_HEADER*) resultLocal;
|
||||
tmp->Length = totalSize;
|
||||
tmp->FileCount = numFiles;
|
||||
resultLocal += sizeof(*tmp);
|
||||
|
||||
}
|
||||
|
||||
/* Now do each individual file. */
|
||||
for (i = 0; i < numFiles; i++)
|
||||
{
|
||||
SPECIALIZATION_CLEAR_DATA_FILE_ENTRY* entry;
|
||||
UINT32 j;
|
||||
|
||||
/* Set the entry values */
|
||||
entry = (SPECIALIZATION_CLEAR_DATA_FILE_ENTRY*) resultLocal;
|
||||
entry->FileType = 0;
|
||||
entry->FileNameSize = Strlen(files[i].FileName) * sizeof(CHAR16);
|
||||
entry->FileNameOffset = SPECIALIZATION_CLEAR_DATA_FILE_ENTRY_SIZE;
|
||||
entry->FilePayloadSize = files[i].PayloadSize;
|
||||
entry->FilePayloadOffset = entry->FileNameOffset + entry->FileNameSize;
|
||||
|
||||
/* Copy the actual data. */
|
||||
resultLocal += sizeof(*entry);
|
||||
for (j = 0; j < entry->FileNameSize / sizeof(CHAR16); j++)
|
||||
{
|
||||
*((CHAR16*) resultLocal + j) = (CHAR16) files[i].FileName[j];
|
||||
}
|
||||
resultLocal += entry->FileNameSize;
|
||||
Memcpy(resultLocal, files[i].PayloadData, files[i].PayloadSize);
|
||||
resultLocal += entry->FilePayloadSize;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void FreeSpecFiles(
|
||||
SPECIALIZATION_RESULT* r,
|
||||
SPECIALIZATION_FILE* r,
|
||||
UINTN rSize)
|
||||
{
|
||||
_CleanupSpecFilesList(r, rSize);
|
||||
|
|
|
@ -60,24 +60,29 @@ typedef struct _SPECIALIZATION_CLEAR_DATA_FILE_ENTRY
|
|||
|
||||
#define SPECIALIZATION_CLEAR_DATA_FILE_ENTRY_SIZE (sizeof(SPECIALIZATION_CLEAR_DATA_FILE_ENTRY))
|
||||
|
||||
typedef struct _SPECIALIZATION_RESULT
|
||||
typedef struct _SPECIALIZATION_FILE
|
||||
{
|
||||
char* FileName;
|
||||
UINT8* PayloadData;
|
||||
UINT32 PayloadSize;
|
||||
} SPECIALIZATION_RESULT, *PSPECIALIZATION_RESULT;
|
||||
} SPECIALIZATION_FILE, *PSPECIALIZATION_FILE;
|
||||
|
||||
/* Parses the specialization file format and returns the list of files + data in the
|
||||
* SPECIALIZATION_RESULT format.. */
|
||||
int ExtractSpecFiles(
|
||||
const UINT8* data,
|
||||
UINTN size,
|
||||
SPECIALIZATION_RESULT** result,
|
||||
UINTN* resultSize
|
||||
);
|
||||
SPECIALIZATION_FILE** result,
|
||||
UINTN* resultSize);
|
||||
|
||||
int CombineSpecFiles(
|
||||
const SPECIALIZATION_FILE* files,
|
||||
UINT32 numFiles,
|
||||
UINT8** result,
|
||||
UINT32* resultSize);
|
||||
|
||||
void FreeSpecFiles(
|
||||
SPECIALIZATION_RESULT* r,
|
||||
SPECIALIZATION_FILE* r,
|
||||
UINTN rSize);
|
||||
|
||||
#endif /* _lsvmutils_specialize_h */
|
||||
|
|
Загрузка…
Ссылка в новой задаче