From bd30f4b340dc2cb75da0d9f1a9d53f4177807260 Mon Sep 17 00:00:00 2001 From: PQCraft <0456523@gmail.com> Date: Thu, 20 Jul 2023 11:11:55 -0400 Subject: [PATCH 1/7] cxbe: Add the ability to handle long section names - Increased the buffer sizes from 9 to 256 which now makes the max section name length 255 chars - Added code in Exe.cpp to parse long section names (they begin with /) - Modified code in Xbe.cpp to write out sections names longer than 8 chars --- tools/cxbe/Exe.cpp | 47 +++++++++++++++++++++++++++++++++++++++++++++- tools/cxbe/Exe.h | 7 +++++++ tools/cxbe/Xbe.cpp | 31 ++++++++++++++++++++++++------ tools/cxbe/Xbe.h | 4 ++-- 4 files changed, 80 insertions(+), 9 deletions(-) diff --git a/tools/cxbe/Exe.cpp b/tools/cxbe/Exe.cpp index a43c373b2..789e1a9c1 100644 --- a/tools/cxbe/Exe.cpp +++ b/tools/cxbe/Exe.cpp @@ -100,6 +100,7 @@ Exe::Exe(const char *x_szFilename) // read section headers { m_SectionHeader = new SectionHeader[m_Header.m_sections]; + m_SectionHeader_longname = new SectionHeader_longname[m_Header.m_sections]; printf("Exe::Exe: Reading Section Headers...\n"); @@ -115,7 +116,46 @@ Exe::Exe(const char *x_szFilename) goto cleanup; } - printf("OK %d\n", v); + // interpret long section names + if(m_SectionHeader[v].m_name[0] == '/') + { + m_SectionHeader_longname[v].m_offset = 0; + for(uint32 i = 1; i < 8; ++i) + { + char c = m_SectionHeader[v].m_name[i]; + if(!c) + break; + if(c < '0' || c > '9') // not a long section after all? + goto notlong; + m_SectionHeader_longname[v].m_offset *= 10; + m_SectionHeader_longname[v].m_offset += c - '0'; + } + m_SectionHeader_longname[v].m_longname = new char[256](); + + long tmppos = ftell(ExeFile); + fseek(ExeFile, m_Header.m_symbol_table_addr + m_SectionHeader_longname[v].m_offset, + SEEK_SET); + + uint32 i; + for(i = 0; i < 255; ++i) + { + int c = fgetc(ExeFile); + if(!c || c == EOF) + break; + m_SectionHeader_longname[v].m_longname[i] = c; + } + m_SectionHeader_longname[v].m_longname[i] = 0; + + fseek(ExeFile, tmppos, SEEK_SET); + printf("OK %d (long)\n", v, m_SectionHeader_longname[v].m_offset, + m_SectionHeader_longname[v].m_longname); + } + else + { + notlong:; + m_SectionHeader_longname[v].m_longname = NULL; + printf("OK %d\n", v); + } } } @@ -184,6 +224,7 @@ Exe::Exe(const char *x_szFilename) void Exe::ConstructorInit() { m_SectionHeader = NULL; + m_SectionHeader_longname = NULL; m_bzSection = NULL; } @@ -193,12 +234,16 @@ Exe::~Exe() if(m_bzSection != 0) { for(uint32 v = 0; v < m_Header.m_sections; v++) + { + delete[] m_SectionHeader_longname[v].m_longname; delete[] m_bzSection[v]; + } delete[] m_bzSection; } delete[] m_SectionHeader; + delete[] m_SectionHeader_longname; } // export to Exe file diff --git a/tools/cxbe/Exe.h b/tools/cxbe/Exe.h index d1adcaff5..523cef6a1 100644 --- a/tools/cxbe/Exe.h +++ b/tools/cxbe/Exe.h @@ -129,6 +129,13 @@ class Exe : public Error uint32 m_characteristics; // characteristics for this segment } __attribute((packed)) * m_SectionHeader; + // array to store long section names + struct SectionHeader_longname + { + char *m_longname; + uint32 m_offset; + } __attribute((packed)) * m_SectionHeader_longname; + // array of section data uint08 **m_bzSection; diff --git a/tools/cxbe/Xbe.cpp b/tools/cxbe/Xbe.cpp index b02b9996a..2e2cbd855 100644 --- a/tools/cxbe/Xbe.cpp +++ b/tools/cxbe/Xbe.cpp @@ -125,8 +125,16 @@ Xbe::Xbe(class Exe *x_Exe, const char *x_szTitle, bool x_bRetail, const std::vec { uint32 s = 0; - while(s < 8 && x_Exe->m_SectionHeader[v].m_name[s] != '\0') - s++; + if(x_Exe->m_SectionHeader_longname[v].m_longname) + { + while(s < 255 && x_Exe->m_SectionHeader_longname[v].m_longname[s] != '\0') + s++; + } + else + { + while(s < 8 && x_Exe->m_SectionHeader[v].m_name[s] != '\0') + s++; + } mrc += s + 1; } @@ -345,7 +353,7 @@ Xbe::Xbe(class Exe *x_Exe, const char *x_szTitle, bool x_bRetail, const std::vec // write section headers / section names { - m_szSectionName = new char[m_Header.dwSections][9]; + m_szSectionName = new char[m_Header.dwSections][256]; m_SectionHeader = new SectionHeader[m_Header.dwSections]; @@ -431,10 +439,21 @@ Xbe::Xbe(class Exe *x_Exe, const char *x_szTitle, bool x_bRetail, const std::vec memset(secn, 0, 8); m_SectionHeader[v].dwSectionNameAddr = hwc_secn; - while(s < 8 && x_Exe->m_SectionHeader[v].m_name[s] != '\0') + if(x_Exe->m_SectionHeader_longname[v].m_longname) { - m_szSectionName[v][s] = secn[s] = x_Exe->m_SectionHeader[v].m_name[s]; - s++; + while(s < 255 && x_Exe->m_SectionHeader_longname[v].m_longname[s] != '\0') + { + m_szSectionName[v][s] = secn[s] = x_Exe->m_SectionHeader_longname[v].m_longname[s]; + s++; + } + } + else + { + while(s < 8 && x_Exe->m_SectionHeader[v].m_name[s] != '\0') + { + m_szSectionName[v][s] = secn[s] = x_Exe->m_SectionHeader[v].m_name[s]; + s++; + } } m_szSectionName[v][s] = '\0'; diff --git a/tools/cxbe/Xbe.h b/tools/cxbe/Xbe.h index fe685a0fb..9e2005b09 100644 --- a/tools/cxbe/Xbe.h +++ b/tools/cxbe/Xbe.h @@ -170,8 +170,8 @@ class Xbe : public Error uint32 dwCharacteristics; // characteristics } __attribute((packed)) * m_TLS; - // Xbe section names, each 8 bytes max and null terminated - char (*m_szSectionName)[9]; + // Xbe section names, each 255 bytes max and null terminated + char (*m_szSectionName)[256]; // Xbe sections uint08 **m_bzSection; From 2f0deb50ab531653adb8fac7dd06ebedfcf4112c Mon Sep 17 00:00:00 2001 From: PQCraft <0456523@gmail.com> Date: Thu, 20 Jul 2023 11:47:21 -0400 Subject: [PATCH 2/7] cxbe: Add -TITLEID:{%c%c-%u|%x} The Title ID is interpreted in Main.cpp and passed as x_dwTitleID when creating the XBE. Title IDs can be in human-readable form (like CX-9999) or a hex code (like 4358270F). Strings without - in them are tried as hex codes. --- tools/cxbe/Main.cpp | 52 ++++++++++++++++++++++++++++++++++++++++----- tools/cxbe/Xbe.cpp | 12 ++++------- tools/cxbe/Xbe.h | 2 +- 3 files changed, 52 insertions(+), 14 deletions(-) diff --git a/tools/cxbe/Main.cpp b/tools/cxbe/Main.cpp index d24b5d12d..f885c15bc 100644 --- a/tools/cxbe/Main.cpp +++ b/tools/cxbe/Main.cpp @@ -19,18 +19,26 @@ int main(int argc, char *argv[]) char szXbeFilename[OPTION_LEN + 1] = { 0 }; char szDumpFilename[OPTION_LEN + 1] = { 0 }; char szXbeTitle[OPTION_LEN + 1] = "Untitled"; + char szXbeTitleID[OPTION_LEN + 1] = ""; char szMode[OPTION_LEN + 1] = "retail"; char szLogo[OPTION_LEN + 1] = ""; char szDebugPath[OPTION_LEN + 1] = ""; + bool bRetail; + uint32 dwTitleId = 0xFFFF0002; const char *program = argv[0]; const char *program_desc = "CXBE EXE to XBE (win32 to Xbox) Relinker (Version: " VERSION ")"; Option options[] = { - { szExeFilename, NULL, "exefile" }, { szXbeFilename, "OUT", "filename" }, - { szDumpFilename, "DUMPINFO", "filename" }, { szXbeTitle, "TITLE", "title" }, - { szMode, "MODE", "{debug|retail}" }, { szLogo, "LOGO", "filename" }, - { szDebugPath, "DEBUGPATH", "path" }, { NULL } + { szExeFilename, NULL, "exefile" }, + { szXbeFilename, "OUT", "filename" }, + { szDumpFilename, "DUMPINFO", "filename" }, + { szXbeTitle, "TITLE", "title" }, + { szXbeTitleID, "TITLEID", "{%c%c-%u|%x}" }, + { szMode, "MODE", "{debug|retail}" }, + { szLogo, "LOGO", "filename" }, + { szDebugPath, "DEBUGPATH", "path" }, + { NULL } }; if(ParseOptions(argv, argc, options, szErrorMessage)) @@ -54,6 +62,40 @@ int main(int argc, char *argv[]) szXbeTitle[40] = '\0'; } + // interpret title id + if(szXbeTitleID[0]) + { + bool hex = true; + for(int i = 0; szXbeTitleID[i]; ++i) + { + if(szXbeTitleID[i] == '-') + { + hex = false; + break; + } + } + if(hex) + { + sscanf(szXbeTitleID, "%x", &dwTitleId); + } + else + { + char titlechar[2]; + unsigned titleno; + if (sscanf(szXbeTitleID, "%c%c-%u", &titlechar[0], &titlechar[1], &titleno) != 3) + { + strncpy(szErrorMessage, "invalid TITLEID", ERROR_LEN); + goto cleanup; + } + if(titleno > 0xFFFF) + { + printf("WARNING: Title ID number too high (max is 65535)\n"); + titleno = 0xFFFF; + } + dwTitleId = (titlechar[0] << 24) | (titlechar[1] << 16) | titleno; + } + } + // verify we received the required parameters if(szExeFilename[0] == '\0') { @@ -90,7 +132,7 @@ int main(int argc, char *argv[]) LogoPtr = &logo; } - Xbe *XbeFile = new Xbe(ExeFile, szXbeTitle, bRetail, LogoPtr, szDebugPath); + Xbe *XbeFile = new Xbe(ExeFile, szXbeTitle, dwTitleId, bRetail, LogoPtr, szDebugPath); if(XbeFile->GetError() != 0) { diff --git a/tools/cxbe/Xbe.cpp b/tools/cxbe/Xbe.cpp index 2e2cbd855..4d7687293 100644 --- a/tools/cxbe/Xbe.cpp +++ b/tools/cxbe/Xbe.cpp @@ -32,8 +32,8 @@ static size_t BasenameOffset(const std::string &path) } // construct via Exe file object -Xbe::Xbe(class Exe *x_Exe, const char *x_szTitle, bool x_bRetail, const std::vector *logo, - const char *x_szDebugPath) +Xbe::Xbe(class Exe *x_Exe, const char *x_szTitle, uint32 x_dwTitleID, bool x_bRetail, + const std::vector *logo, const char *x_szDebugPath) { ConstructorInit(); @@ -279,8 +279,7 @@ Xbe::Xbe(class Exe *x_Exe, const char *x_szTitle, bool x_bRetail, const std::vec m_Certificate.dwTimeDate = CurrentTime; - // TODO: generate in the form CX-9999 - m_Certificate.dwTitleId = 0xFFFF0002; + m_Certificate.dwTitleId = x_dwTitleID; // title name memset(m_Certificate.wszTitleName, 0, sizeof(m_Certificate.wszTitleName)); @@ -305,10 +304,7 @@ Xbe::Xbe(class Exe *x_Exe, const char *x_szTitle, bool x_bRetail, const std::vec XBEIMAGE_MEDIA_TYPE_MEDIA_BOARD | XBEIMAGE_MEDIA_TYPE_NONSECURE_HARD_DISK | XBEIMAGE_MEDIA_TYPE_NONSECURE_MODE; - // TODO: allow configuration - m_Certificate.dwGameRegion = XBEIMAGE_GAME_REGION_MANUFACTURING | - XBEIMAGE_GAME_REGION_NA | XBEIMAGE_GAME_REGION_JAPAN | - XBEIMAGE_GAME_REGION_RESTOFWORLD; + m_Certificate.dwGameRegion = x_dwRegions; // TODO: allow configuration m_Certificate.dwGameRatings = 0xFFFFFFFF; diff --git a/tools/cxbe/Xbe.h b/tools/cxbe/Xbe.h index 9e2005b09..046d99a9e 100644 --- a/tools/cxbe/Xbe.h +++ b/tools/cxbe/Xbe.h @@ -26,7 +26,7 @@ class Xbe : public Error { public: // construct via Exe file object - Xbe(class Exe *x_Exe, const char *x_szTitle, bool x_bRetail, + Xbe(class Exe *x_Exe, const char *x_szTitle, uint32 x_dwTitleID, bool x_bRetail, const std::vector *logo = nullptr, const char *x_szDebugPath = nullptr); // deconstructor From c0a40d68ceb725d41be5c93430a991655b4b5b47 Mon Sep 17 00:00:00 2001 From: PQCraft <0456523@gmail.com> Date: Tue, 19 Sep 2023 15:16:03 -0400 Subject: [PATCH 3/7] cxbe: Add -REGION:{-|[n][j][w][m]} Region flags can be - for no regions, or any combo of n for North America, j for Japan, w for world, and/or m for manufacturing --- tools/cxbe/Main.cpp | 48 ++++++++++++++++++++++++++++++++++++++++++++- tools/cxbe/Xbe.cpp | 4 ++-- tools/cxbe/Xbe.h | 4 ++-- 3 files changed, 51 insertions(+), 5 deletions(-) diff --git a/tools/cxbe/Main.cpp b/tools/cxbe/Main.cpp index f885c15bc..70e0d00e3 100644 --- a/tools/cxbe/Main.cpp +++ b/tools/cxbe/Main.cpp @@ -20,12 +20,14 @@ int main(int argc, char *argv[]) char szDumpFilename[OPTION_LEN + 1] = { 0 }; char szXbeTitle[OPTION_LEN + 1] = "Untitled"; char szXbeTitleID[OPTION_LEN + 1] = ""; + char szXbeRegions[OPTION_LEN + 1] = ""; char szMode[OPTION_LEN + 1] = "retail"; char szLogo[OPTION_LEN + 1] = ""; char szDebugPath[OPTION_LEN + 1] = ""; bool bRetail; uint32 dwTitleId = 0xFFFF0002; + uint32 dwRegions; const char *program = argv[0]; const char *program_desc = "CXBE EXE to XBE (win32 to Xbox) Relinker (Version: " VERSION ")"; @@ -35,6 +37,9 @@ int main(int argc, char *argv[]) { szDumpFilename, "DUMPINFO", "filename" }, { szXbeTitle, "TITLE", "title" }, { szXbeTitleID, "TITLEID", "{%c%c-%u|%x}" }, + { szXbeRegions, "REGION", + "{-|[n][j][w][m]}\n" + " -=none, n=North America, j=Japan, w=world, m=manufacturing" }, { szMode, "MODE", "{debug|retail}" }, { szLogo, "LOGO", "filename" }, { szDebugPath, "DEBUGPATH", "path" }, @@ -96,6 +101,47 @@ int main(int argc, char *argv[]) } } + // interpret region flags + if(szXbeRegions[0]) + { + char c; + for(int i = 0; (c = szXbeRegions[i]); ++i) + { + switch(c) + { + case '-':; + dwRegions = 0; + goto breakfor; + case 'a':; + dwRegions = XBEIMAGE_GAME_REGION_NA | XBEIMAGE_GAME_REGION_JAPAN | + XBEIMAGE_GAME_REGION_RESTOFWORLD | + XBEIMAGE_GAME_REGION_MANUFACTURING; + goto breakfor; + case 'n':; + dwRegions |= XBEIMAGE_GAME_REGION_NA; + break; + case 'j':; + dwRegions |= XBEIMAGE_GAME_REGION_JAPAN; + break; + case 'w':; + dwRegions |= XBEIMAGE_GAME_REGION_RESTOFWORLD; + break; + case 'm':; + dwRegions |= XBEIMAGE_GAME_REGION_MANUFACTURING; + break; + default:; + printf("WARNING: Invalid region char: %c\n", c); + break; + } + } + breakfor:; + } + else + { + dwRegions = XBEIMAGE_GAME_REGION_NA | XBEIMAGE_GAME_REGION_JAPAN | + XBEIMAGE_GAME_REGION_RESTOFWORLD | XBEIMAGE_GAME_REGION_MANUFACTURING; + } + // verify we received the required parameters if(szExeFilename[0] == '\0') { @@ -132,7 +178,7 @@ int main(int argc, char *argv[]) LogoPtr = &logo; } - Xbe *XbeFile = new Xbe(ExeFile, szXbeTitle, dwTitleId, bRetail, LogoPtr, szDebugPath); + Xbe *XbeFile = new Xbe(ExeFile, szXbeTitle, dwTitleId, dwRegions, bRetail, LogoPtr, szDebugPath); if(XbeFile->GetError() != 0) { diff --git a/tools/cxbe/Xbe.cpp b/tools/cxbe/Xbe.cpp index 4d7687293..27fc86122 100644 --- a/tools/cxbe/Xbe.cpp +++ b/tools/cxbe/Xbe.cpp @@ -32,8 +32,8 @@ static size_t BasenameOffset(const std::string &path) } // construct via Exe file object -Xbe::Xbe(class Exe *x_Exe, const char *x_szTitle, uint32 x_dwTitleID, bool x_bRetail, - const std::vector *logo, const char *x_szDebugPath) +Xbe::Xbe(class Exe *x_Exe, const char *x_szTitle, uint32 x_dwTitleID, uint32 x_dwRegions, + bool x_bRetail, const std::vector *logo, const char *x_szDebugPath) { ConstructorInit(); diff --git a/tools/cxbe/Xbe.h b/tools/cxbe/Xbe.h index 046d99a9e..b1a7c016d 100644 --- a/tools/cxbe/Xbe.h +++ b/tools/cxbe/Xbe.h @@ -26,8 +26,8 @@ class Xbe : public Error { public: // construct via Exe file object - Xbe(class Exe *x_Exe, const char *x_szTitle, uint32 x_dwTitleID, bool x_bRetail, - const std::vector *logo = nullptr, const char *x_szDebugPath = nullptr); + Xbe(class Exe *x_Exe, const char *x_szTitle, uint32 x_dwTitleID, uint32 x_dwRegions, + bool x_bRetail, const std::vector *logo = nullptr, const char *x_szDebugPath = nullptr); // deconstructor ~Xbe(); From b62a0ae6bdff656dcdaddf58f31e80dc506035a3 Mon Sep 17 00:00:00 2001 From: PQCraft <0456523@gmail.com> Date: Thu, 20 Jul 2023 12:15:21 -0400 Subject: [PATCH 4/7] cxbe: Add -VERSION The version is interpreted by strtoul() which can do decimal, hex, and octal --- tools/cxbe/Main.cpp | 17 ++++++++++++++++- tools/cxbe/Xbe.cpp | 5 ++--- tools/cxbe/Xbe.h | 2 +- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/tools/cxbe/Main.cpp b/tools/cxbe/Main.cpp index 70e0d00e3..9614b7efe 100644 --- a/tools/cxbe/Main.cpp +++ b/tools/cxbe/Main.cpp @@ -9,6 +9,7 @@ #include "Exe.h" #include "Xbe.h" +#include #include // program entry point @@ -21,6 +22,7 @@ int main(int argc, char *argv[]) char szXbeTitle[OPTION_LEN + 1] = "Untitled"; char szXbeTitleID[OPTION_LEN + 1] = ""; char szXbeRegions[OPTION_LEN + 1] = ""; + char szXbeVersion[OPTION_LEN + 1] = ""; char szMode[OPTION_LEN + 1] = "retail"; char szLogo[OPTION_LEN + 1] = ""; char szDebugPath[OPTION_LEN + 1] = ""; @@ -28,6 +30,7 @@ int main(int argc, char *argv[]) bool bRetail; uint32 dwTitleId = 0xFFFF0002; uint32 dwRegions; + uint32 dwVersion; const char *program = argv[0]; const char *program_desc = "CXBE EXE to XBE (win32 to Xbox) Relinker (Version: " VERSION ")"; @@ -40,6 +43,7 @@ int main(int argc, char *argv[]) { szXbeRegions, "REGION", "{-|[n][j][w][m]}\n" " -=none, n=North America, j=Japan, w=world, m=manufacturing" }, + { szXbeVersion, "VERSION", "version" }, { szMode, "MODE", "{debug|retail}" }, { szLogo, "LOGO", "filename" }, { szDebugPath, "DEBUGPATH", "path" }, @@ -142,6 +146,16 @@ int main(int argc, char *argv[]) XBEIMAGE_GAME_REGION_RESTOFWORLD | XBEIMAGE_GAME_REGION_MANUFACTURING; } + // interpret version + if(szXbeVersion[0]) + { + dwVersion = strtoul(szXbeVersion, NULL, 0); + } + else + { + dwVersion = 0; + } + // verify we received the required parameters if(szExeFilename[0] == '\0') { @@ -178,7 +192,8 @@ int main(int argc, char *argv[]) LogoPtr = &logo; } - Xbe *XbeFile = new Xbe(ExeFile, szXbeTitle, dwTitleId, dwRegions, bRetail, LogoPtr, szDebugPath); + Xbe *XbeFile = + new Xbe(ExeFile, szXbeTitle, dwTitleId, dwRegions, dwVersion, bRetail, LogoPtr, szDebugPath); if(XbeFile->GetError() != 0) { diff --git a/tools/cxbe/Xbe.cpp b/tools/cxbe/Xbe.cpp index 27fc86122..56a22bf65 100644 --- a/tools/cxbe/Xbe.cpp +++ b/tools/cxbe/Xbe.cpp @@ -33,7 +33,7 @@ static size_t BasenameOffset(const std::string &path) // construct via Exe file object Xbe::Xbe(class Exe *x_Exe, const char *x_szTitle, uint32 x_dwTitleID, uint32 x_dwRegions, - bool x_bRetail, const std::vector *logo, const char *x_szDebugPath) + uint32 x_dwVersion, bool x_bRetail, const std::vector *logo, const char *x_szDebugPath) { ConstructorInit(); @@ -312,8 +312,7 @@ Xbe::Xbe(class Exe *x_Exe, const char *x_szTitle, uint32 x_dwTitleID, uint32 x_d // always disk 0, AFAIK m_Certificate.dwDiskNumber = 0; - // TODO: allow configuration - m_Certificate.dwVersion = 0; + m_Certificate.dwVersion = x_dwVersion; // generate blank LAN, signature, and alternate signature keys { diff --git a/tools/cxbe/Xbe.h b/tools/cxbe/Xbe.h index b1a7c016d..10a050eb6 100644 --- a/tools/cxbe/Xbe.h +++ b/tools/cxbe/Xbe.h @@ -27,7 +27,7 @@ class Xbe : public Error public: // construct via Exe file object Xbe(class Exe *x_Exe, const char *x_szTitle, uint32 x_dwTitleID, uint32 x_dwRegions, - bool x_bRetail, const std::vector *logo = nullptr, const char *x_szDebugPath = nullptr); + uint32 x_dwVersion, bool x_bRetail, const std::vector *logo = nullptr, const char *x_szDebugPath = nullptr); // deconstructor ~Xbe(); From fc892e78b2c52896ee183cf775f4168de56b2631 Mon Sep 17 00:00:00 2001 From: PQCraft <0456523@gmail.com> Date: Fri, 21 Jul 2023 07:11:34 -0400 Subject: [PATCH 5/7] cxbe: Set the proper flags for *IMAGE sections bInsertedFile, bHeadPageRO, and bTailPageRO should be set and bPreload should be cleared otherwise it causes memory/stack corruption when running the XBE (my theory is that it overwrites some program data with the image data unless you clear bPreload). A more permanent solution would probably be to add an option set the flags for specific sections. --- tools/cxbe/Xbe.cpp | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/tools/cxbe/Xbe.cpp b/tools/cxbe/Xbe.cpp index 56a22bf65..344c82222 100644 --- a/tools/cxbe/Xbe.cpp +++ b/tools/cxbe/Xbe.cpp @@ -376,14 +376,27 @@ Xbe::Xbe(class Exe *x_Exe, const char *x_szTitle, uint32 x_dwTitleID, uint32 x_d memset(&m_SectionHeader[v].dwFlags, 0, sizeof(m_SectionHeader->dwFlags)); - if(characteristics & IMAGE_SCN_MEM_WRITE) - m_SectionHeader[v].dwFlags.bWritable = true; + // check for $$XTIMAGE or $$XSIMAGE and set the correct flags + if(x_Exe->m_SectionHeader_longname[v].m_longname && + (!strcmp(x_Exe->m_SectionHeader_longname[v].m_longname, "$$XTIMAGE") || + !strcmp(x_Exe->m_SectionHeader_longname[v].m_longname, "$$XSIMAGE"))) + { + m_SectionHeader[v].dwFlags.bInsertedFile = true; + m_SectionHeader[v].dwFlags.bHeadPageRO = true; + m_SectionHeader[v].dwFlags.bTailPageRO = true; + } + else + { + if(characteristics & IMAGE_SCN_MEM_WRITE) + m_SectionHeader[v].dwFlags.bWritable = true; + + if((characteristics & IMAGE_SCN_MEM_EXECUTE) || + (characteristics & IMAGE_SCN_CNT_CODE)) + m_SectionHeader[v].dwFlags.bExecutable = true; - if((characteristics & IMAGE_SCN_MEM_EXECUTE) || - (characteristics & IMAGE_SCN_CNT_CODE)) - m_SectionHeader[v].dwFlags.bExecutable = true; + m_SectionHeader[v].dwFlags.bPreload = true; + } - m_SectionHeader[v].dwFlags.bPreload = true; m_SectionHeader[v].dwVirtualAddr = x_Exe->m_SectionHeader[v].m_virtual_addr + m_Header.dwPeBaseAddr; From 25c32e4bf9afba11aded40374bfda9127831961b Mon Sep 17 00:00:00 2001 From: PQCraft <0456523@gmail.com> Date: Sat, 22 Jul 2023 15:07:18 -0400 Subject: [PATCH 6/7] cxbe: Set proper flags on debug sections Another little "hack" to clear the preload flag on sections named .debug or starting with .debug_ so they won't be loaded and waste memory --- tools/cxbe/Main.cpp | 9 +++++---- tools/cxbe/Xbe.cpp | 9 +++++++-- tools/cxbe/Xbe.h | 3 ++- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/tools/cxbe/Main.cpp b/tools/cxbe/Main.cpp index 9614b7efe..0f21e9459 100644 --- a/tools/cxbe/Main.cpp +++ b/tools/cxbe/Main.cpp @@ -29,7 +29,8 @@ int main(int argc, char *argv[]) bool bRetail; uint32 dwTitleId = 0xFFFF0002; - uint32 dwRegions; + uint32 dwRegions = XBEIMAGE_GAME_REGION_NA | XBEIMAGE_GAME_REGION_JAPAN | + XBEIMAGE_GAME_REGION_RESTOFWORLD | XBEIMAGE_GAME_REGION_MANUFACTURING; uint32 dwVersion; const char *program = argv[0]; @@ -91,7 +92,7 @@ int main(int argc, char *argv[]) { char titlechar[2]; unsigned titleno; - if (sscanf(szXbeTitleID, "%c%c-%u", &titlechar[0], &titlechar[1], &titleno) != 3) + if(sscanf(szXbeTitleID, "%c%c-%u", &titlechar[0], &titlechar[1], &titleno) != 3) { strncpy(szErrorMessage, "invalid TITLEID", ERROR_LEN); goto cleanup; @@ -192,8 +193,8 @@ int main(int argc, char *argv[]) LogoPtr = &logo; } - Xbe *XbeFile = - new Xbe(ExeFile, szXbeTitle, dwTitleId, dwRegions, dwVersion, bRetail, LogoPtr, szDebugPath); + Xbe *XbeFile = new Xbe( + ExeFile, szXbeTitle, dwTitleId, dwRegions, dwVersion, bRetail, LogoPtr, szDebugPath); if(XbeFile->GetError() != 0) { diff --git a/tools/cxbe/Xbe.cpp b/tools/cxbe/Xbe.cpp index 344c82222..4421c4271 100644 --- a/tools/cxbe/Xbe.cpp +++ b/tools/cxbe/Xbe.cpp @@ -33,7 +33,8 @@ static size_t BasenameOffset(const std::string &path) // construct via Exe file object Xbe::Xbe(class Exe *x_Exe, const char *x_szTitle, uint32 x_dwTitleID, uint32 x_dwRegions, - uint32 x_dwVersion, bool x_bRetail, const std::vector *logo, const char *x_szDebugPath) + uint32 x_dwVersion, bool x_bRetail, const std::vector *logo, + const char *x_szDebugPath) { ConstructorInit(); @@ -394,7 +395,11 @@ Xbe::Xbe(class Exe *x_Exe, const char *x_szTitle, uint32 x_dwTitleID, uint32 x_d (characteristics & IMAGE_SCN_CNT_CODE)) m_SectionHeader[v].dwFlags.bExecutable = true; - m_SectionHeader[v].dwFlags.bPreload = true; + char *name = (x_Exe->m_SectionHeader_longname[v].m_longname) + ? x_Exe->m_SectionHeader_longname[v].m_longname + : (char *)x_Exe->m_SectionHeader[v].m_name; + m_SectionHeader[v].dwFlags.bPreload = + (strcmp(name, ".debug") && strncmp(name, ".debug_", 7)); } m_SectionHeader[v].dwVirtualAddr = diff --git a/tools/cxbe/Xbe.h b/tools/cxbe/Xbe.h index 10a050eb6..fd47a203a 100644 --- a/tools/cxbe/Xbe.h +++ b/tools/cxbe/Xbe.h @@ -27,7 +27,8 @@ class Xbe : public Error public: // construct via Exe file object Xbe(class Exe *x_Exe, const char *x_szTitle, uint32 x_dwTitleID, uint32 x_dwRegions, - uint32 x_dwVersion, bool x_bRetail, const std::vector *logo = nullptr, const char *x_szDebugPath = nullptr); + uint32 x_dwVersion, bool x_bRetail, const std::vector *logo = nullptr, + const char *x_szDebugPath = nullptr); // deconstructor ~Xbe(); From e13a346bb4d7175c0165e9b79e30a72f7bae798e Mon Sep 17 00:00:00 2001 From: PQCraft <0456523@gmail.com> Date: Thu, 20 Jul 2023 12:19:46 -0400 Subject: [PATCH 7/7] Add features to Makefile - Added XBE_TITLEID, XBE_REGION, and XBE_VERSION to take advantage of the new Cxbe features --- Makefile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index bfa98d086..58cfc69f6 100644 --- a/Makefile +++ b/Makefile @@ -98,7 +98,8 @@ DEPS += $(filter %.cpp.d, $(SRCS:.cpp=.cpp.d)) $(OUTPUT_DIR)/default.xbe: main.exe $(OUTPUT_DIR) $(CXBE) @echo "[ CXBE ] $@" - $(VE)$(CXBE) -OUT:$@ -TITLE:$(XBE_TITLE) $< $(QUIET) + $(VE)$(CXBE) -OUT:$@ -TITLE:$(XBE_TITLE) -TITLEID:$(XBE_TITLEID) \ + -REGION:$(XBE_REGION) -VERSION:$(XBE_VERSION) $< $(QUIET) $(OUTPUT_DIR): @mkdir -p $(OUTPUT_DIR);