ImageTool: Distinguish between non-ELF/PE and incompatible ELF/PE

This commit is contained in:
Marvin Häuser 2023-04-02 14:53:46 +02:00
parent 1fc65bdb8d
commit 75fcd420f6
4 changed files with 20 additions and 19 deletions

View File

@ -214,23 +214,23 @@ ReadElfFile (
fprintf (stderr, "ImageTool: mEhdr->e_ident[4] = 0x%x expected 0x%x\n", mEhdr->e_ident[4], Ident[4]); fprintf (stderr, "ImageTool: mEhdr->e_ident[4] = 0x%x expected 0x%x\n", mEhdr->e_ident[4], Ident[4]);
fprintf (stderr, "ImageTool: mEhdr->e_ident[5] = 0x%x expected 0x%x\n", mEhdr->e_ident[5], Ident[5]); fprintf (stderr, "ImageTool: mEhdr->e_ident[5] = 0x%x expected 0x%x\n", mEhdr->e_ident[5], Ident[5]);
fprintf (stderr, "ImageTool: FileSize = 0x%x sizeof(*mEhdr) = 0x%lx\n", FileSize, sizeof (*mEhdr)); fprintf (stderr, "ImageTool: FileSize = 0x%x sizeof(*mEhdr) = 0x%lx\n", FileSize, sizeof (*mEhdr));
return RETURN_VOLUME_CORRUPTED; return RETURN_UNSUPPORTED;
} }
if ((mEhdr->e_type != ET_EXEC) && (mEhdr->e_type != ET_DYN)) { if ((mEhdr->e_type != ET_EXEC) && (mEhdr->e_type != ET_DYN)) {
fprintf (stderr, "ImageTool: ELF e_type not ET_EXEC or ET_DYN\n"); fprintf (stderr, "ImageTool: ELF e_type not ET_EXEC or ET_DYN\n");
return RETURN_UNSUPPORTED; return RETURN_INCOMPATIBLE_VERSION;
} }
#if defined(EFI_TARGET64) #if defined(EFI_TARGET64)
if ((mEhdr->e_machine != EM_X86_64) && (mEhdr->e_machine != EM_AARCH64)) { if ((mEhdr->e_machine != EM_X86_64) && (mEhdr->e_machine != EM_AARCH64)) {
fprintf (stderr, "ImageTool: Unsupported ELF e_machine\n"); fprintf (stderr, "ImageTool: Unsupported ELF e_machine\n");
return RETURN_UNSUPPORTED; return RETURN_INCOMPATIBLE_VERSION;
} }
#elif defined(EFI_TARGET32) #elif defined(EFI_TARGET32)
if ((mEhdr->e_machine != EM_386) && (mEhdr->e_machine != EM_ARM)) { if ((mEhdr->e_machine != EM_386) && (mEhdr->e_machine != EM_ARM)) {
fprintf (stderr, "ImageTool: Unsupported ELF e_machine\n"); fprintf (stderr, "ImageTool: Unsupported ELF e_machine\n");
return RETURN_UNSUPPORTED; return RETURN_INCOMPATIBLE_VERSION;
} }
#endif #endif
@ -809,7 +809,7 @@ ScanElf (
default: default:
fprintf (stderr, "ImageTool: Unknown ELF architecture %d\n", mEhdr->e_machine); fprintf (stderr, "ImageTool: Unknown ELF architecture %d\n", mEhdr->e_machine);
free (mEhdr); free (mEhdr);
return RETURN_UNSUPPORTED; return RETURN_INCOMPATIBLE_VERSION;
} }
mImageInfo.DebugInfo.SymbolsPath = malloc (mImageInfo.DebugInfo.SymbolsPathLen + 1); mImageInfo.DebugInfo.SymbolsPath = malloc (mImageInfo.DebugInfo.SymbolsPathLen + 1);

View File

@ -24,7 +24,7 @@ PeXip (
{ {
void *Pe; void *Pe;
uint32_t PeSize; uint32_t PeSize;
bool Result; RETURN_STATUS Status;
image_tool_image_info_t Image; image_tool_image_info_t Image;
assert (OldName != NULL); assert (OldName != NULL);
@ -37,12 +37,12 @@ PeXip (
return RETURN_ABORTED; return RETURN_ABORTED;
} }
Result = ToolContextConstructPe (&Image, Pe, PeSize, ModuleType); Status = ToolContextConstructPe (&Image, Pe, PeSize, ModuleType);
free (Pe); free (Pe);
Pe = NULL; Pe = NULL;
if (!Result) { if (RETURN_ERROR (Status)) {
return RETURN_ABORTED; return RETURN_ABORTED;
} }

View File

@ -148,7 +148,7 @@ ImageConvertToXip (
image_tool_image_info_t *Image image_tool_image_info_t *Image
); );
bool RETURN_STATUS
ToolContextConstructPe ( ToolContextConstructPe (
OUT image_tool_image_info_t *Image, OUT image_tool_image_info_t *Image,
IN const void *File, IN const void *File,

View File

@ -393,7 +393,7 @@ ScanPeGetHiiInfo (
return true; return true;
} }
bool RETURN_STATUS
ToolContextConstructPe ( ToolContextConstructPe (
OUT image_tool_image_info_t *Image, OUT image_tool_image_info_t *Image,
IN const void *File, IN const void *File,
@ -415,13 +415,12 @@ ToolContextConstructPe (
if (FileSize > MAX_UINT32) { if (FileSize > MAX_UINT32) {
fprintf (stderr, "ImageTool: FileSize is too huge\n"); fprintf (stderr, "ImageTool: FileSize is too huge\n");
return false; return RETURN_UNSUPPORTED;
} }
Status = PeCoffInitializeContext (&Context, File, (UINT32)FileSize); Status = PeCoffInitializeContext (&Context, File, (UINT32)FileSize);
if (RETURN_ERROR (Status)) { if (RETURN_ERROR (Status)) {
fprintf (stderr, "ImageTool: Could not initialise Context\n"); return Status;
return false;
} }
ImageSize = PeCoffGetSizeOfImage (&Context); ImageSize = PeCoffGetSizeOfImage (&Context);
@ -435,14 +434,14 @@ ToolContextConstructPe (
); );
if (Destination == NULL) { if (Destination == NULL) {
fprintf (stderr, "ImageTool: Could not allocate Destination buffer\n"); fprintf (stderr, "ImageTool: Could not allocate Destination buffer\n");
return false; return RETURN_OUT_OF_RESOURCES;
} }
Status = PeCoffLoadImage (&Context, Destination, DestinationSize); Status = PeCoffLoadImage (&Context, Destination, DestinationSize);
if (RETURN_ERROR (Status)) { if (RETURN_ERROR (Status)) {
fprintf (stderr, "ImageTool: Could not Load Image\n"); fprintf (stderr, "ImageTool: Could not Load Image\n");
FreeAlignedPages (Destination, DestinationPages); FreeAlignedPages (Destination, DestinationPages);
return false; return RETURN_VOLUME_CORRUPTED;
} }
memset (Image, 0, sizeof (*Image)); memset (Image, 0, sizeof (*Image));
@ -452,7 +451,7 @@ ToolContextConstructPe (
fprintf (stderr, "ImageTool: Could not retrieve header info\n"); fprintf (stderr, "ImageTool: Could not retrieve header info\n");
ToolImageDestruct (Image); ToolImageDestruct (Image);
FreeAlignedPages (Destination, DestinationPages); FreeAlignedPages (Destination, DestinationPages);
return false; return RETURN_VOLUME_CORRUPTED;
} }
Result = ScanPeGetDebugInfo (&Image->DebugInfo, &Context); Result = ScanPeGetDebugInfo (&Image->DebugInfo, &Context);
@ -460,7 +459,7 @@ ToolContextConstructPe (
fprintf (stderr, "ImageTool: Could not retrieve debug info\n"); fprintf (stderr, "ImageTool: Could not retrieve debug info\n");
ToolImageDestruct (Image); ToolImageDestruct (Image);
FreeAlignedPages (Destination, DestinationPages); FreeAlignedPages (Destination, DestinationPages);
return false; return RETURN_VOLUME_CORRUPTED;
} }
Result = ScanPeGetSegmentInfo (&Image->SegmentInfo, &Image->HiiInfo, &Context); Result = ScanPeGetSegmentInfo (&Image->SegmentInfo, &Image->HiiInfo, &Context);
@ -468,16 +467,18 @@ ToolContextConstructPe (
fprintf (stderr, "ImageTool: Could not retrieve segment info\n"); fprintf (stderr, "ImageTool: Could not retrieve segment info\n");
ToolImageDestruct (Image); ToolImageDestruct (Image);
FreeAlignedPages (Destination, DestinationPages); FreeAlignedPages (Destination, DestinationPages);
return false; return RETURN_VOLUME_CORRUPTED;
} }
Result = ScanPeGetRelocInfo (&Image->RelocInfo, &Context); Result = ScanPeGetRelocInfo (&Image->RelocInfo, &Context);
if (!Result) { if (!Result) {
fprintf (stderr, "ImageTool: Could not retrieve reloc info\n"); fprintf (stderr, "ImageTool: Could not retrieve reloc info\n");
ToolImageDestruct (Image); ToolImageDestruct (Image);
FreeAlignedPages (Destination, DestinationPages);
return RETURN_VOLUME_CORRUPTED;
} }
FreeAlignedPages (Destination, DestinationPages); FreeAlignedPages (Destination, DestinationPages);
return Result; return RETURN_SUCCESS;
} }