Skip to content
This repository has been archived by the owner on Jan 3, 2021. It is now read-only.

Commit

Permalink
common.h
Browse files Browse the repository at this point in the history
  • Loading branch information
tuxuser committed Aug 22, 2014
2 parents 83c847c + 13a93d5 commit b2792a8
Show file tree
Hide file tree
Showing 10 changed files with 387 additions and 265 deletions.
12 changes: 6 additions & 6 deletions OZMTool/README
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ It includes the following useful tools to help you in this process:
--ozmupdate Updates clean BIOS with files from old OZM-flavoured one
--ozmextract Extracts Ozmosis files (ffs) from BIOS
--ozmcreate Patches Original BIOS with Ozmosis
--ffsconvert Converts kext-directories to FFS
--kext2ffs Converts kext-directories to FFS
--dsdt2bios Injects (bigger) DSDT into AmiBoardInfo
--help, -h Print usage (append command to print cmd-usage!)

Expand All @@ -30,7 +30,7 @@ It includes the following useful tools to help you in this process:
Extracts Ozmosis files from a stock (HermitCrabLabs) Ozmosis Bios.

* OZMCreate
Creates an Ozmosis image from supplied FFS/Kext/DSDT files (Kext & DSDT is optional).
Creates an Ozmosis image from supplied FFS/Kext/DSDT/Efi files (Kext, DSDT & Efi is optional).
If the image originally does not have enough space, use the -a, --aggressivity switch
Possible values for -a :
--aggressivity 0: Try to inject the files as-is
Expand All @@ -42,8 +42,8 @@ It includes the following useful tools to help you in this process:

If all attempts fail, the output image wont be created (obviously)

* FFSConvert
Converts files to FFS. It accepts *.kext and OzmosisDefaults.plist.
* Kext2Ffs
Converts files to FFS. It accepts *.kext, Defaults.plist and Theme.bin.
Uses UEFITool's technique to create the FFS :)

* DSDT2Bios
Expand All @@ -66,8 +66,8 @@ USAGE
./OZMTool --dsdtinject --input BIOS.ROM --dsdt DSDT.aml --out outputfile
./OZMTool --ozmupdate --aggressivity 1 --compressdxe --out RECENT_OZM_BIOS.ROM --input OLD_OZM.ROM --recent NEW_CLEAN_BIOS.ROM
./OZMTool --ozmextract --out outputdir --input OZM.ROM
./OZMTool --ozmcreate --aggressivity 1 --compressdxe --kext kextdir --ffs ffsdir --dsdt DSDT.aml --out outputfile --input BIOS.ROM
./OZMTool --ffsconvert --out outputdir --input kextsdir
./OZMTool --ozmcreate --aggressivity 1 --compressdxe --kext kextdir --ffs ffsdir --efi efidir --dsdt DSDT.aml --out outputfile --input BIOS.ROM
./OZMTool --kext2ffs --out outputdir --input kextsdir
./OZMTool --dsdt2bios --input AmiBoardInfo.bin --dsdt DSDT.aml --out patchedAmiBoardInfo.bin


Expand Down
101 changes: 54 additions & 47 deletions OZMTool/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,44 @@

#include <QString>
#include "../peimage.h"
#include "../ffs.h"

#if defined(_WIN32) || defined(_WIN64)
#define strcasecmp _stricmp
#endif

/* RETURN CODES */
#define ERR_DIR_NOT_EXIST 0xEB
#define ERR_FILE_NOT_FOUND 0xEC
#define ERR_FILE_EXISTS 0xED
#define ERR_REPLACE 0xEE
#define ERR_RELOCATION 0xEF
#define ERR_ERROR 0xF0

/* DSDT STUFF */
#define DSDT_HEADER "DSDT"
#define DSDT_HEADER_SZ 4

#define UNPATCHABLE_SECTION ".ROM"
#define DYNAMIC_BASE 0x40
#define MAX_DSDT 0x3FFFF

/* COMPRESS RUN */
#define RUN_AS_IS 0
#define RUN_DELETE 1
#define RUN_DEL_OZM_NREQ 2

/* KEXT CONVERSION */
#define MIN_KEXT_ID 0x9
#define MAX_KEXT_ID 0xF

#define ALIGN16(Value) (((Value)+15) & ~15)
#define ALIGN32(Value) (((Value)+31) & ~31)

#define MAX_DSDT 0x3FFFF

const static QString ozmDefaultsFilename = "OzmosisDefaults.plist";
static const QString DSDTFilename = "DSDT.aml";
const static QString kextGUID = "DADE100%1-1B31-4FE4-8557-26FCEFC78275";

/* PE IMAGE */
///
/// @attention
/// EFI_IMAGE_HEADERS64 is for use ONLY by tools.
Expand All @@ -56,67 +60,70 @@ typedef struct {

#define EFI_IMAGE_SIZEOF_RELOC_ENTRY sizeof (RELOC_ENTRY)

#define SRC_NOT_SET 0
#define SRC_KEXT 1
#define SRC_BINARY 2
#define SRC_EFI 3

/* FILE ENTRIES */
struct sectionEntry {
QString name;
QString GUID;
QString srcName;
UINT8 srcType;
UINT8 sectionType;
BOOLEAN required;
};

#define requiredFfsCount 6

static const sectionEntry requiredFfs[] = {
{"PartitionDxe","1FA1F39E-FEFF-4AAE-BD7B-38A070A3B609",TRUE},
{"EnhancedFat","961578FE-B6B7-44C3-AF35-6BC705CD2B1F",TRUE},
{"HfsPlus","4CF484CD-135F-4FDC-BAFB-1AA104B48D36",TRUE},
{"Ozmosis","AAE65279-0761-41D1-BA13-4A3C1383603F",TRUE},
{"OzmosisDefaults","99F2839C-57C3-411E-ABC3-ADE5267D960D",TRUE},// not required, but small
{"SmcEmulatorKext","DADE1001-1B31-4FE4-8557-26FCEFC78275",TRUE}
};

#define optionalFfsCount 10

static const sectionEntry optionalFfs[] = {
{"ExtFs","B34E5765-2E04-4DAF-867F-7F40BE6FC33D", FALSE},
{"HermitShellX64","C57AD6B7-0515-40A8-9D21-551652854E37", FALSE},
{"OzmosisTheme","AC255206-DCF9-4837-8353-72BBBC0AC849", FALSE},
{"DisablerKext","DADE1002-1B31-4FE4-8557-26FCEFC78275", FALSE},
{"InjectorKext","DADE1003-1B31-4FE4-8557-26FCEFC78275", FALSE},
{"PostbootMounter","DADE1004-1B31-4FE4-8557-26FCEFC78275", FALSE},
{"PostbootSymbols","DADE1005-1B31-4FE4-8557-26FCEFC78275", FALSE},
{"CpuSensorsKext","DADE1006-1B31-4FE4-8557-26FCEFC78275", FALSE},
{"LpcSensorsKext","DADE1007-1B31-4FE4-8557-26FCEFC78275", FALSE},
{"GpuSensorsKext","DADE1008-1B31-4FE4-8557-26FCEFC78275", FALSE}
static const QList<sectionEntry> OzmFfs{
// Filesystem
{"PartitionDxe","1FA1F39E-FEFF-4AAE-BD7B-38A070A3B609", "partitiondxe.efi", SRC_EFI, EFI_SECTION_PE32, TRUE},
{"EnhancedFat","961578FE-B6B7-44C3-AF35-6BC705CD2B1F", "enhancedfat.efi", SRC_EFI, EFI_SECTION_PE32, TRUE},
{"HfsPlus","4CF484CD-135F-4FDC-BAFB-1AA104B48D36", "hfsplus.efi", SRC_EFI, EFI_SECTION_PE32, TRUE},
{"ExtFs","B34E5765-2E04-4DAF-867F-7F40BE6FC33D", "extfs.efi", SRC_EFI, EFI_SECTION_PE32, FALSE},
// Bootloader
{"Ozmosis","AAE65279-0761-41D1-BA13-4A3C1383603F", "ozmosis.efi", SRC_EFI, EFI_SECTION_PE32, TRUE},
// EFI / EDK Shell
{"HermitShellX64","C57AD6B7-0515-40A8-9D21-551652854E37", "hermitshellx64.efi", SRC_EFI, EFI_SECTION_PE32, FALSE},
{"Shell", "7C04A583-9E3E-4F1C-AD65-E05268D0B4D1", "shell.efi", SRC_EFI, EFI_SECTION_PE32, FALSE},
// Defaults
{"OzmosisDefaults","99F2839C-57C3-411E-ABC3-ADE5267D960D", "Defaults.plist", SRC_BINARY, EFI_SECTION_RAW, TRUE},
// Theme
{"OzmosisTheme","AC255206-DCF9-4837-8353-72BBBC0AC849", "Theme.bin", SRC_BINARY, EFI_SECTION_RAW, FALSE},
// Kernel Extensions
{"SmcEmulatorKext","DADE1001-1B31-4FE4-8557-26FCEFC78275", "FakeSMC.kext", SRC_KEXT, EFI_SECTION_RAW, TRUE},
{"DisablerKext","DADE1002-1B31-4FE4-8557-26FCEFC78275", "Disabler.kext", SRC_KEXT, EFI_SECTION_RAW, FALSE},
{"InjectorKext","DADE1003-1B31-4FE4-8557-26FCEFC78275", "Injector.kext", SRC_KEXT, EFI_SECTION_RAW, FALSE},
{"PostbootMounter","DADE1004-1B31-4FE4-8557-26FCEFC78275", "PostbootMounter.kext", SRC_KEXT, EFI_SECTION_RAW, FALSE},
{"PostbootSymbols","DADE1005-1B31-4FE4-8557-26FCEFC78275", "PostbootSymbols.kext", SRC_KEXT, EFI_SECTION_RAW, FALSE},
{"CpuSensorsKext","DADE1006-1B31-4FE4-8557-26FCEFC78275", "CPUSensors.kext", SRC_KEXT, EFI_SECTION_RAW, FALSE},
{"LpcSensorsKext","DADE1007-1B31-4FE4-8557-26FCEFC78275", "LPCSensors.kext", SRC_KEXT, EFI_SECTION_RAW, FALSE},
{"GpuSensorsKext","DADE1008-1B31-4FE4-8557-26FCEFC78275", "GPUSensors.kext", SRC_KEXT, EFI_SECTION_RAW, FALSE}
};

#define deletableFfsCount 10

static const sectionEntry deletableFfs[] = {
{"TcpDxe","B1625D3C-9D2D-4E0D-B864-8A763EE4EC50",FALSE},
{"Dhcp4Dxe","8DD9176E-EE87-4F0E-8A84-3F998311F930",FALSE},
{"Ip4ConfigDxe","8F9296EF-2880-4659-B857-915A8901BDC8",FALSE},
{"Ip4Dxe","8F92960F-2880-4659-B857-915A8901BDC8",FALSE},
{"Mtftp4Dxe","61AFA223-8AC8-4440-9AB5-762B1BF05156",FALSE},
{"Udp4Dxe","10EE5462-B207-4A4F-ABD8-CB522ECAA3A4",FALSE},
{"Dhcp6Dxe","8DD9176D-EE87-4F0E-8A84-3F998311F930",FALSE},
{"Ip6Dxe","8F92960E-2880-4659-B857-915A8901BDC8",FALSE},
{"Mtftp6Dxe","61AFA251-8AC8-4440-9AB5-762B1BF05156",FALSE},
{"Udp6Dxe","10EE54AE-B207-4A4F-ABD8-CB522ECAA3A4",FALSE}
static const QList<sectionEntry> deleteFfs{
{"TcpDxe","B1625D3C-9D2D-4E0D-B864-8A763EE4EC50", "tcpdxe.efi", SRC_EFI, EFI_SECTION_PE32, FALSE},
{"Dhcp4Dxe","8DD9176E-EE87-4F0E-8A84-3F998311F930", "dhcp4dxe.efi", SRC_EFI, EFI_SECTION_PE32, FALSE},
{"Ip4ConfigDxe","8F9296EF-2880-4659-B857-915A8901BDC8", "ip4configdxe.efi", SRC_EFI, EFI_SECTION_PE32, FALSE},
{"Ip4Dxe","8F92960F-2880-4659-B857-915A8901BDC8", "ip4dxe.efi", SRC_EFI, EFI_SECTION_PE32, FALSE},
{"Mtftp4Dxe","61AFA223-8AC8-4440-9AB5-762B1BF05156", "mtftp4dxe.efi", SRC_EFI, EFI_SECTION_PE32, FALSE},
{"Udp4Dxe","10EE5462-B207-4A4F-ABD8-CB522ECAA3A4", "udp4dxe.efi", SRC_EFI, EFI_SECTION_PE32, FALSE},
{"Dhcp6Dxe","8DD9176D-EE87-4F0E-8A84-3F998311F930", "dhcp4dxe.efi", SRC_EFI, EFI_SECTION_PE32, FALSE},
{"Ip6Dxe","8F92960E-2880-4659-B857-915A8901BDC8", "ip6dxe.efi", SRC_EFI, EFI_SECTION_PE32, FALSE},
{"Mtftp6Dxe","61AFA251-8AC8-4440-9AB5-762B1BF05156", "mtftp6dxe.efi", SRC_EFI, EFI_SECTION_PE32, FALSE},
{"Udp6Dxe","10EE54AE-B207-4A4F-ABD8-CB522ECAA3A4", "udp6dxe.efi", SRC_EFI, EFI_SECTION_PE32, FALSE}
};

static const sectionEntry coreDxeSection = {
"CORE_DXE","5AE3F37E-4EAE-41AE-8240-35465B5E81EB", TRUE
"CORE_DXE","5AE3F37E-4EAE-41AE-8240-35465B5E81EB", "coredxe.efi", SRC_EFI, EFI_SECTION_PE32, TRUE
};

static const sectionEntry amiBoardSection = {
"AmiBoardInfo","9F3A0016-AE55-4288-829D-D22FD344C347", TRUE
"AmiBoardInfo","9F3A0016-AE55-4288-829D-D22FD344C347", "amiboardinfo.efi", SRC_EFI, EFI_SECTION_PE32, TRUE
};

static const sectionEntry filesystemSection = {
"FileSystem","93022F8C-1F09-47EF-BBB2-5814FF609DF5", FALSE
};

static const sectionEntry ozmosisDefaults = {
"OzmosisDefaults","99F2839C-57C3-411E-ABC3-ADE5267D960D", TRUE
"FileSystem","93022F8C-1F09-47EF-BBB2-5814FF609DF5", "filesystem.efi", SRC_EFI, EFI_SECTION_PE32, FALSE
};

#endif // COMMON_H
102 changes: 47 additions & 55 deletions OZMTool/ffsutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -335,50 +335,34 @@ UINT8 FFSUtil::deleteFilesystemFfs()
return ERR_SUCCESS;
}


UINT8 FFSUtil::compressDXE()
{
UINT8 ret;
QModelIndex rootIdx, tmpIdx, currIdx;
QByteArray compressed, created, newHeader, body;
QModelIndex rootIdx, currIdx;
QByteArray buf, compressed;

getRootIndex(rootIdx);

printf("Compressing CORE_DXE to save space...\n");

ret = findFileByGUID(rootIdx, coreDxeSection.GUID, tmpIdx);
ret = findFileByGUID(rootIdx, coreDxeSection.GUID, currIdx);
if(ret)
return ret;

ret = findSectionByIndex(tmpIdx, EFI_SECTION_COMPRESSION, currIdx);
if(ret) {
printf("Warning: Failed to get compressed section for [%s] for compression!\n", qPrintable(coreDxeSection.GUID));
ret = dumpFileByGUID(coreDxeSection.GUID, buf, EXTRACT_MODE_AS_IS);
if (ret) {
printf("ERROR: Failed to dump '%s' [%s] !\n", qPrintable(coreDxeSection.name), qPrintable(coreDxeSection.GUID));
return ret;
}

newHeader = ffsEngine->treeModel()->header(currIdx);
body = ffsEngine->treeModel()->body(currIdx);

EFI_COMPRESSION_SECTION* compressionHeader = (EFI_COMPRESSION_SECTION*)newHeader.data();

if(compressionHeader->CompressionType != EFI_NOT_COMPRESSED){
printf("Info: CORE_DXE is already compressed!\n");
return ERR_SUCCESS;
}

printf("* Trying to compress '%s' [%s]\n", qPrintable(coreDxeSection.name), qPrintable(coreDxeSection.GUID));
ret = compress(body, COMPRESSION_ALGORITHM_TIANO, compressed);
if(ret) {
printf("Warning: Compressing '%s' [%s] failed!\n", qPrintable(coreDxeSection.name), qPrintable(coreDxeSection.GUID));
ret = compressFFS(buf, compressed);
if (ret) {
printf("ERROR: Failed to re/compress '%s'' [%s] !\n", qPrintable(coreDxeSection.name), qPrintable(coreDxeSection.GUID));
return ret;
}

/* Set new compressionType + size */
compressionHeader->CompressionType = EFI_STANDARD_COMPRESSION;
uint32ToUint24(newHeader.size() + compressed.size(), compressionHeader->Size);

created.append(newHeader).append(compressed);

ret = replace(currIdx, created, REPLACE_MODE_AS_IS);
ret = replace(currIdx, compressed, REPLACE_MODE_AS_IS);
if(ret) {
printf("Warning: Injecting compressed '%s' [%s] failed!\n", qPrintable(coreDxeSection.name), qPrintable(coreDxeSection.GUID));
return ret;
Expand All @@ -387,67 +371,75 @@ UINT8 FFSUtil::compressDXE()
return ERR_SUCCESS;
}


UINT8 FFSUtil::compressFFS(QByteArray ffs, QByteArray & out)
{
UINT8 ret;
QByteArray newHeader, body, compressedBody, fileHeader;
QByteArray body, compressedBody;
EFI_COMPRESSION_SECTION* compressionHeader = {0};

/* Split ffs into ffs-header and body */
fileHeader = ffs.left(sizeof(EFI_FFS_FILE_HEADER));
body = ffs.mid(sizeof(EFI_FFS_FILE_HEADER));
unsigned char *buf = (unsigned char*) ffs.data();

/* Create new compression header to tack infront of compressed body */
newHeader.fill(0, sizeof(EFI_COMPRESSION_SECTION));
EFI_COMPRESSION_SECTION* compressionHeader = (EFI_COMPRESSION_SECTION*)newHeader.data();
// Split ffs into ffs-header and body
EFI_FFS_FILE_HEADER* ffsHeader = (EFI_FFS_FILE_HEADER*)buf;
EFI_COMMON_SECTION_HEADER* sectionHeader = (EFI_COMMON_SECTION_HEADER*)&buf[sizeof(EFI_FFS_FILE_HEADER)];
UINT8 algo;

if(sectionHeader->Type == EFI_SECTION_COMPRESSION) {
compressionHeader = (EFI_COMPRESSION_SECTION*) sectionHeader;

if(compressionHeader->CompressionType == EFI_STANDARD_COMPRESSION ||
compressionHeader->CompressionType == EFI_CUSTOMIZED_COMPRESSION) {
printf("Info: File seems already compressed!\n");
out = ffs;
return ERR_SUCCESS;
}
compressedBody = ffs.mid(sizeof(EFI_FFS_FILE_HEADER)+sizeof(EFI_COMPRESSION_SECTION));
ret = decompress(compressedBody, compressionHeader->CompressionType, body, &algo);
if (ret) {
printf("ERROR: Decompression failed!\n");
return ERR_ERROR;
}
compressedBody.clear();
}
else {
body = ffs.mid(sizeof(EFI_FFS_FILE_HEADER));
}

ret = ffsEngine->compress(body, COMPRESSION_ALGORITHM_TIANO, compressedBody);
if (ret) {
printf("ERROR: Compression failed!\n");
return ERR_ERROR;
}

/* Assign infos to compression header */
// Assign infos to compression header
compressionHeader->Type = EFI_SECTION_COMPRESSION;
compressionHeader->CompressionType = EFI_STANDARD_COMPRESSION;
uint32ToUint24(newHeader.size() + compressedBody.size(), compressionHeader->Size);
uint32ToUint24(sizeof(EFI_COMPRESSION_SECTION) + compressedBody.size(), compressionHeader->Size);
compressionHeader->UncompressedLength = body.size();

out.append(newHeader);
out.append((const char*)compressionHeader, sizeof(EFI_COMPRESSION_SECTION));
out.append(compressedBody);

/* Set new size and checksums in ffs header */
EFI_FFS_FILE_HEADER* ffsHeader = (EFI_FFS_FILE_HEADER*)fileHeader.data();
uint32ToUint24(sizeof(EFI_FFS_FILE_HEADER)+newHeader.size(), ffsHeader->Size);
// Set new size and checksums in ffs header
uint32ToUint24(sizeof(EFI_FFS_FILE_HEADER)+out.size(), ffsHeader->Size);
ffsHeader->IntegrityCheck.Checksum.Header = 0;
ffsHeader->IntegrityCheck.Checksum.File = 0;
ffsHeader->IntegrityCheck.Checksum.Header = calculateChecksum8((UINT8*)ffsHeader, sizeof(EFI_FFS_FILE_HEADER)-1);
ffsHeader->IntegrityCheck.Checksum.File = FFS_FIXED_CHECKSUM2;

out.prepend(fileHeader);
out.prepend((const char *)ffsHeader, sizeof(EFI_FFS_FILE_HEADER));

return ERR_SUCCESS;
}


UINT8 FFSUtil::runFreeSomeSpace(int aggressivity)
{
int i;
UINT8 ret;
QModelIndex rootIdx, currIdx;

static QList<sectionEntry> deleteFfs;
static QList<sectionEntry> OzmFfs;

/* Just temporary... */
for(int i = 0; i < requiredFfsCount; i++) {
OzmFfs.append(requiredFfs[i]);
}
for(int i = 0; i < optionalFfsCount; i++) {
OzmFfs.append(optionalFfs[i]);
}
for(int i = 0; i < deletableFfsCount; i++) {
deleteFfs.append(deletableFfs[i]);
}

getRootIndex(rootIdx);

switch(aggressivity) {
Expand Down
1 change: 0 additions & 1 deletion OZMTool/ffsutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ class FFSUtil
UINT8 compressDXE();
UINT8 compressFFS(QByteArray ffs, QByteArray & out);
UINT8 runFreeSomeSpace(int aggressivity);
UINT8 workaroundRecompressEFI11();
UINT8 parseBIOSFile(QByteArray & buf);
private:
FfsEngine* ffsEngine;
Expand Down
Loading

0 comments on commit b2792a8

Please sign in to comment.