Merge pull request #8 from 505e06b2/master

Implement ELF header parsing
This commit is contained in:
jvyden 2022-02-20 19:31:49 -05:00 committed by GitHub
commit f421129ff8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 128 additions and 13 deletions

View file

@ -1,4 +1,5 @@
using System; using System;
using System.IO;
using System.Diagnostics; using System.Diagnostics;
using Eto; using Eto;
using Eto.Drawing; using Eto.Drawing;
@ -53,21 +54,38 @@ namespace LBPUnion.UnionPatcher.Gui {
}; };
control.Click += delegate { control.Click += delegate {
if(string.IsNullOrEmpty(this.filePicker.FilePath)) { if(string.IsNullOrWhiteSpace(this.filePicker.FilePath)) {
this.CreateOkDialog("Form Error", "No file specified!").ShowModal(); this.CreateOkDialog("Form Error", "No file specified!").ShowModal();
return; return;
} }
if(string.IsNullOrEmpty(this.serverUrl.Text)) { if(string.IsNullOrWhiteSpace(this.serverUrl.Text)) {
this.CreateOkDialog("Form Error", "No server URL specified!").ShowModal(); this.CreateOkDialog("Form Error", "No server URL specified!").ShowModal();
return; return;
} }
if(string.IsNullOrEmpty(this.outputFileName.FilePath)) { if(string.IsNullOrWhiteSpace(this.outputFileName.FilePath)) {
this.CreateOkDialog("Form Error", "No output file specified!").ShowModal(); this.CreateOkDialog("Form Error", "No output file specified!").ShowModal();
return; return;
} }
ElfFile eboot = new(this.filePicker.FilePath);
if(eboot.IsValid == false) {
this.CreateOkDialog("EBOOT Error", $"{eboot.Name} is not a valid ELF file (magic number mismatch)\nThe EBOOT must be decrypted before using this tool").ShowModal();
return;
}
if(eboot.Is64Bit == null) {
this.CreateOkDialog("EBOOT Error", $"{eboot.Name} does not target a valid system").ShowModal();
return;
}
if(string.IsNullOrWhiteSpace(eboot.Architecture)) {
this.CreateOkDialog("EBOOT Error", $"{eboot.Name} does not target a valid architecture (PowerPC or ARM)").ShowModal();
return;
}
try { try {
Patcher.PatchFile(this.filePicker.FilePath, this.serverUrl.Text, this.outputFileName.FilePath); Patcher.PatchFile(this.filePicker.FilePath, this.serverUrl.Text, this.outputFileName.FilePath);
} }

77
UnionPatcher/ElfFile.cs Normal file
View file

@ -0,0 +1,77 @@
using System;
using System.IO;
using System.Linq;
using System.Buffers.Binary;
/*
Linux ELF header refspec: https://refspecs.linuxfoundation.org/elf/gabi4+/ch4.eheader.html
Wikipedia entry on ELF: https://en.wikipedia.org/wiki/Executable_and_Linkable_Format
*/
namespace LBPUnion.UnionPatcher {
public class ElfFile {
private enum WordSize : byte {
ThirtyTwoBits = 0x01,
SixtyFourBits = 0x02
}
private enum Endianness : byte {
Little = 0x01,
Big = 0x02
}
private enum InstructionSetArchitecture : UInt16 {
PowerPC = 0x15, //64-bit PowerPC (PS3)
ARM = 0x28 //32-bit ARM (Vita)
}
public string Name { get; } = "Binary Blob";
public bool IsValid { get; } = false;
public bool? Is64Bit { get; } = null;
public bool? IsBigEndian { get; } = null;
public string Architecture { get; } = null;
public byte[] Contents { get; } = null;
public ElfFile(byte[] fileContents) {
if(fileContents.Length < 52)
return;
IsValid = fileContents[0x00..0x04].SequenceEqual(new byte[] {0x7F, (byte)'E', (byte)'L', (byte)'F'});
if(!IsValid) return;
byte identClassValue = fileContents[0x04];
byte identDataValue = fileContents[0x05];
if(identClassValue == (byte)WordSize.ThirtyTwoBits || identClassValue == (byte)WordSize.SixtyFourBits)
Is64Bit = identClassValue == (byte)WordSize.SixtyFourBits;
if(identDataValue == (byte)Endianness.Little || identDataValue == (byte)Endianness.Big)
IsBigEndian = identDataValue == (byte)Endianness.Big;
Architecture = GetFileArchitecture(fileContents, IsBigEndian == true);
Contents = fileContents;
}
public ElfFile(FileInfo file) : this(File.ReadAllBytes(file.FullName)) {
Name = file.Name;
}
public ElfFile(string fileName) : this(new FileInfo(fileName)) {}
private string GetFileArchitecture(byte[] elfHeader, bool isBigEndian) {
byte[] architectureBytes = elfHeader[0x12..0x14];
UInt16 fileArch = (isBigEndian) ?
BinaryPrimitives.ReadUInt16BigEndian(architectureBytes) :
BinaryPrimitives.ReadUInt16LittleEndian(architectureBytes);
foreach(InstructionSetArchitecture arch in Enum.GetValues(typeof(InstructionSetArchitecture))) {
if(fileArch == (UInt16)arch)
return arch.ToString();
}
return null;
}
}
}

View file

@ -22,6 +22,26 @@ namespace LBPUnion.UnionPatcher {
return; return;
} }
ElfFile eboot = new(new FileInfo(args[0]));
if(!eboot.IsValid) {
Console.WriteLine($"{eboot.Name} is not a valid ELF file (magic number mismatch)");
Console.WriteLine("The EBOOT must be decrypted before using this tool");
return;
}
if(eboot.Is64Bit == null) {
Console.WriteLine($"{eboot.Name} does not target a valid system");
return;
}
if(string.IsNullOrWhiteSpace(eboot.Architecture)) {
Console.WriteLine($"{eboot.Name} does not target a valid architecture (PowerPC or ARM)");
return;
}
Console.WriteLine($"{eboot.Name} targets {eboot.Architecture}");
Patcher.PatchFile(args[0], args[1], args[2]); Patcher.PatchFile(args[0], args[1], args[2]);
Console.WriteLine($"Successfully patched Server URL to {args[1]}."); Console.WriteLine($"Successfully patched Server URL to {args[1]}.");
} }