diff --git a/src/fix/main.c b/src/fix/main.c index 20477555..af8c33f0 100644 --- a/src/fix/main.c +++ b/src/fix/main.c @@ -653,6 +653,22 @@ static ssize_t writeBytes(int fd, void *buf, size_t len) return total; } +/** + * @param rom0 A pointer to rom0 + * @param startAddr What address to begin checking from + * @param size How many bytes to check + * @param areaName Name to be displayed in the warning message + */ +static void warnNonZero(uint8_t *rom0, uint16_t startAddr, uint8_t size, char const *areaName) +{ + for (uint8_t i = 0; i < size; i++) { + if (rom0[i + startAddr] != 0) { + fprintf(stderr, "warning: Overwrote a non-zero byte in the %s\n", areaName); + break; + } + } +} + /** * @param input File descriptor to be used for reading * @param output File descriptor to be used for writing, may be equal to `input` @@ -681,6 +697,7 @@ static void processFile(int input, int output, char const *name, off_t fileSize) // Accept partial reads if the file contains at least the header if (fixSpec & (FIX_LOGO | TRASH_LOGO)) { + warnNonZero(rom0, 0x0104, sizeof(ninLogo), "Nintendo logo"); if (fixSpec & FIX_LOGO) { memcpy(&rom0[0x104], ninLogo, sizeof(ninLogo)); } else { @@ -689,36 +706,56 @@ static void processFile(int input, int output, char const *name, off_t fileSize) } } - if (title) + if (title) { + warnNonZero(rom0, 0x134, titleLen, "title"); memcpy(&rom0[0x134], title, titleLen); + } - if (gameID) + if (gameID) { + warnNonZero(rom0, 0x13f, gameIDLen, "manufacturer code"); memcpy(&rom0[0x13f], gameID, gameIDLen); + } - if (model != DMG) + if (model != DMG) { + warnNonZero(rom0, 0x143, 1, "CGB flag"); rom0[0x143] = model == BOTH ? 0x80 : 0xc0; + } - if (newLicensee) + if (newLicensee) { + warnNonZero(rom0, 0x144, newLicenseeLen, "new licensee code"); memcpy(&rom0[0x144], newLicensee, newLicenseeLen); + } - if (sgb) + if (sgb) { + warnNonZero(rom0, 0x146, 1, "SGB flag"); rom0[0x146] = 0x03; + } // If a valid MBC was specified... - if (cartridgeType < MBC_NONE) + if (cartridgeType < MBC_NONE) { + warnNonZero(rom0, 0x147, 1, "cartridge type"); rom0[0x147] = cartridgeType; + } - if (ramSize != UNSPECIFIED) + if (ramSize != UNSPECIFIED) { + warnNonZero(rom0, 0x149, 1, "RAM size"); rom0[0x149] = ramSize; + } - if (!japanese) + if (!japanese) { + warnNonZero(rom0, 0x14a, 1, "destination code"); rom0[0x14a] = 0x01; + } - if (oldLicensee != UNSPECIFIED) + if (oldLicensee != UNSPECIFIED) { + warnNonZero(rom0, 0x14b, 1, "old licensee code"); rom0[0x14b] = oldLicensee; + } - if (romVersion != UNSPECIFIED) + if (romVersion != UNSPECIFIED) { + warnNonZero(rom0, 0x14c, 1, "mask ROM version number"); rom0[0x14c] = romVersion; + } // Remain to be handled the ROM size, and header checksum. // The latter depends on the former, and so will be handled after it. @@ -818,6 +855,7 @@ static void processFile(int input, int output, char const *name, off_t fileSize) for (uint16_t i = 0x134; i < 0x14d; i++) sum -= rom0[i] + 1; + warnNonZero(rom0, 0x14d, 1, "header checksum"); rom0[0x14d] = fixSpec & TRASH_HEADER_SUM ? ~sum : sum; } @@ -841,6 +879,7 @@ static void processFile(int input, int output, char const *name, off_t fileSize) if (fixSpec & TRASH_GLOBAL_SUM) globalSum = ~globalSum; + warnNonZero(rom0, 0x14e, 2, "global checksum"); rom0[0x14e] = globalSum >> 8; rom0[0x14f] = globalSum & 0xff; } @@ -915,8 +954,6 @@ free_romx: free(romx); } -#undef trySeek - static bool processFilename(char const *name) { nbErrors = 0; @@ -1015,7 +1052,7 @@ do { \ #define SPEC_H TRASH_HEADER_SUM #define SPEC_g FIX_GLOBAL_SUM #define SPEC_G TRASH_GLOBAL_SUM -#define or(new, bad) \ +#define overrideSpec(new, bad) \ do { \ if (fixSpec & SPEC_##bad) \ fprintf(stderr, \ @@ -1023,30 +1060,30 @@ do { \ fixSpec = (fixSpec & ~SPEC_##bad) | SPEC_##new; \ } while (0) case 'l': - or(l, L); + overrideSpec(l, L); break; case 'L': - or(L, l); + overrideSpec(L, l); break; case 'h': - or(h, H); + overrideSpec(h, H); break; case 'H': - or(H, h); + overrideSpec(H, h); break; case 'g': - or(g, G); + overrideSpec(g, G); break; case 'G': - or(G, g); + overrideSpec(G, g); break; default: fprintf(stderr, "warning: Ignoring '%c' in fix spec\n", *musl_optarg); -#undef or +#undef overrideSpec } musl_optarg++; } diff --git a/test/fix/bad-fix-char.err b/test/fix/bad-fix-char.err index 114b1d62..80b1bba6 100644 --- a/test/fix/bad-fix-char.err +++ b/test/fix/bad-fix-char.err @@ -1,3 +1,4 @@ warning: Ignoring 'm' in fix spec warning: Ignoring 'a' in fix spec warning: Ignoring 'o' in fix spec +warning: Overwrote a non-zero byte in the Nintendo logo diff --git a/test/fix/color.err b/test/fix/color.err index e69de29b..59c96e73 100644 --- a/test/fix/color.err +++ b/test/fix/color.err @@ -0,0 +1 @@ +warning: Overwrote a non-zero byte in the CGB flag diff --git a/test/fix/compatible.err b/test/fix/compatible.err index e69de29b..59c96e73 100644 --- a/test/fix/compatible.err +++ b/test/fix/compatible.err @@ -0,0 +1 @@ +warning: Overwrote a non-zero byte in the CGB flag diff --git a/test/fix/fix-override.err b/test/fix/fix-override.err index 0ca83c19..066f831f 100644 --- a/test/fix/fix-override.err +++ b/test/fix/fix-override.err @@ -1 +1,2 @@ warning: 'l' overriding 'L' in fix spec +warning: Overwrote a non-zero byte in the Nintendo logo diff --git a/test/fix/gameid-trunc.err b/test/fix/gameid-trunc.err index 67e81afa..5394dd8d 100644 --- a/test/fix/gameid-trunc.err +++ b/test/fix/gameid-trunc.err @@ -1 +1,2 @@ warning: Truncating game ID "FOUR!" to 4 chars +warning: Overwrote a non-zero byte in the manufacturer code diff --git a/test/fix/gameid.err b/test/fix/gameid.err index e69de29b..4e420e16 100644 --- a/test/fix/gameid.err +++ b/test/fix/gameid.err @@ -0,0 +1 @@ +warning: Overwrote a non-zero byte in the manufacturer code diff --git a/test/fix/header-edit.err b/test/fix/header-edit.err index e69de29b..76e06c7c 100644 --- a/test/fix/header-edit.err +++ b/test/fix/header-edit.err @@ -0,0 +1,2 @@ +warning: Overwrote a non-zero byte in the CGB flag +warning: Overwrote a non-zero byte in the header checksum diff --git a/test/fix/header-trash.err b/test/fix/header-trash.err index e69de29b..5fa313aa 100644 --- a/test/fix/header-trash.err +++ b/test/fix/header-trash.err @@ -0,0 +1 @@ +warning: Overwrote a non-zero byte in the header checksum diff --git a/test/fix/header.err b/test/fix/header.err index e69de29b..5fa313aa 100644 --- a/test/fix/header.err +++ b/test/fix/header.err @@ -0,0 +1 @@ +warning: Overwrote a non-zero byte in the header checksum diff --git a/test/fix/jp.err b/test/fix/jp.err index e69de29b..73328e27 100644 --- a/test/fix/jp.err +++ b/test/fix/jp.err @@ -0,0 +1 @@ +warning: Overwrote a non-zero byte in the destination code diff --git a/test/fix/logo-trash.err b/test/fix/logo-trash.err index e69de29b..41ef05b1 100644 --- a/test/fix/logo-trash.err +++ b/test/fix/logo-trash.err @@ -0,0 +1 @@ +warning: Overwrote a non-zero byte in the Nintendo logo diff --git a/test/fix/logo.err b/test/fix/logo.err index e69de29b..41ef05b1 100644 --- a/test/fix/logo.err +++ b/test/fix/logo.err @@ -0,0 +1 @@ +warning: Overwrote a non-zero byte in the Nintendo logo diff --git a/test/fix/mbc.err b/test/fix/mbc.err index e69de29b..84c93183 100644 --- a/test/fix/mbc.err +++ b/test/fix/mbc.err @@ -0,0 +1 @@ +warning: Overwrote a non-zero byte in the cartridge type diff --git a/test/fix/mbcless-ram.err b/test/fix/mbcless-ram.err index d4246354..92a31043 100644 --- a/test/fix/mbcless-ram.err +++ b/test/fix/mbcless-ram.err @@ -1 +1,3 @@ warning: MBC "ROM" has no RAM, but RAM size was set to 2 +warning: Overwrote a non-zero byte in the cartridge type +warning: Overwrote a non-zero byte in the RAM size diff --git a/test/fix/new-lic-trunc.err b/test/fix/new-lic-trunc.err index ceb621c9..44850e93 100644 --- a/test/fix/new-lic-trunc.err +++ b/test/fix/new-lic-trunc.err @@ -1 +1,2 @@ warning: Truncating new licensee "HOMEBREW" to 2 chars +warning: Overwrote a non-zero byte in the new licensee code diff --git a/test/fix/new-lic.err b/test/fix/new-lic.err index e69de29b..a5726611 100644 --- a/test/fix/new-lic.err +++ b/test/fix/new-lic.err @@ -0,0 +1 @@ +warning: Overwrote a non-zero byte in the new licensee code diff --git a/test/fix/old-lic-hex.err b/test/fix/old-lic-hex.err index e69de29b..113f98fc 100644 --- a/test/fix/old-lic-hex.err +++ b/test/fix/old-lic-hex.err @@ -0,0 +1 @@ +warning: Overwrote a non-zero byte in the old licensee code diff --git a/test/fix/old-lic.err b/test/fix/old-lic.err index e69de29b..113f98fc 100644 --- a/test/fix/old-lic.err +++ b/test/fix/old-lic.err @@ -0,0 +1 @@ +warning: Overwrote a non-zero byte in the old licensee code diff --git a/test/fix/ram.err b/test/fix/ram.err index e69de29b..76d799b7 100644 --- a/test/fix/ram.err +++ b/test/fix/ram.err @@ -0,0 +1 @@ +warning: Overwrote a non-zero byte in the RAM size diff --git a/test/fix/ramful-mbc-no-ram.err b/test/fix/ramful-mbc-no-ram.err index 3c369b70..019f604b 100644 --- a/test/fix/ramful-mbc-no-ram.err +++ b/test/fix/ramful-mbc-no-ram.err @@ -1 +1,3 @@ warning: MBC "MBC3+RAM" has RAM, but RAM size was set to 0 +warning: Overwrote a non-zero byte in the cartridge type +warning: Overwrote a non-zero byte in the RAM size diff --git a/test/fix/ramful-mbc.err b/test/fix/ramful-mbc.err index e69de29b..35ef63d9 100644 --- a/test/fix/ramful-mbc.err +++ b/test/fix/ramful-mbc.err @@ -0,0 +1,2 @@ +warning: Overwrote a non-zero byte in the cartridge type +warning: Overwrote a non-zero byte in the RAM size diff --git a/test/fix/ramless-mbc.err b/test/fix/ramless-mbc.err index e69de29b..35ef63d9 100644 --- a/test/fix/ramless-mbc.err +++ b/test/fix/ramless-mbc.err @@ -0,0 +1,2 @@ +warning: Overwrote a non-zero byte in the cartridge type +warning: Overwrote a non-zero byte in the RAM size diff --git a/test/fix/rom-ram.err b/test/fix/rom-ram.err index 9fc890e7..327a6903 100644 --- a/test/fix/rom-ram.err +++ b/test/fix/rom-ram.err @@ -1 +1,2 @@ warning: ROM+RAM / ROM+RAM+BATTERY are under-specified and poorly supported +warning: Overwrote a non-zero byte in the cartridge type diff --git a/test/fix/sgb-licensee.err b/test/fix/sgb-licensee.err index 91b7f58b..6075fb04 100644 --- a/test/fix/sgb-licensee.err +++ b/test/fix/sgb-licensee.err @@ -1 +1,3 @@ warning: SGB compatibility enabled, but old licensee is 0x45, not 0x33 +warning: Overwrote a non-zero byte in the SGB flag +warning: Overwrote a non-zero byte in the old licensee code diff --git a/test/fix/sgb.err b/test/fix/sgb.err index e69de29b..3c4caeaa 100644 --- a/test/fix/sgb.err +++ b/test/fix/sgb.err @@ -0,0 +1 @@ +warning: Overwrote a non-zero byte in the SGB flag diff --git a/test/fix/title-color-trunc-rev.err b/test/fix/title-color-trunc-rev.err index 59a9ecfe..da4c5c68 100644 --- a/test/fix/title-color-trunc-rev.err +++ b/test/fix/title-color-trunc-rev.err @@ -1 +1,3 @@ warning: Truncating title "0123456789ABCDEF" to 15 chars +warning: Overwrote a non-zero byte in the title +warning: Overwrote a non-zero byte in the CGB flag diff --git a/test/fix/title-color-trunc.err b/test/fix/title-color-trunc.err index 59a9ecfe..da4c5c68 100644 --- a/test/fix/title-color-trunc.err +++ b/test/fix/title-color-trunc.err @@ -1 +1,3 @@ warning: Truncating title "0123456789ABCDEF" to 15 chars +warning: Overwrote a non-zero byte in the title +warning: Overwrote a non-zero byte in the CGB flag diff --git a/test/fix/title-compat-trunc-rev.err b/test/fix/title-compat-trunc-rev.err index 59a9ecfe..da4c5c68 100644 --- a/test/fix/title-compat-trunc-rev.err +++ b/test/fix/title-compat-trunc-rev.err @@ -1 +1,3 @@ warning: Truncating title "0123456789ABCDEF" to 15 chars +warning: Overwrote a non-zero byte in the title +warning: Overwrote a non-zero byte in the CGB flag diff --git a/test/fix/title-compat-trunc.err b/test/fix/title-compat-trunc.err index 59a9ecfe..da4c5c68 100644 --- a/test/fix/title-compat-trunc.err +++ b/test/fix/title-compat-trunc.err @@ -1 +1,3 @@ warning: Truncating title "0123456789ABCDEF" to 15 chars +warning: Overwrote a non-zero byte in the title +warning: Overwrote a non-zero byte in the CGB flag diff --git a/test/fix/title-gameid-trunc-rev.err b/test/fix/title-gameid-trunc-rev.err index a0aff509..7c5d5237 100644 --- a/test/fix/title-gameid-trunc-rev.err +++ b/test/fix/title-gameid-trunc-rev.err @@ -1 +1,3 @@ warning: Truncating title "0123456789ABCDEF" to 11 chars +warning: Overwrote a non-zero byte in the title +warning: Overwrote a non-zero byte in the manufacturer code diff --git a/test/fix/title-gameid-trunc.err b/test/fix/title-gameid-trunc.err index a0aff509..7c5d5237 100644 --- a/test/fix/title-gameid-trunc.err +++ b/test/fix/title-gameid-trunc.err @@ -1 +1,3 @@ warning: Truncating title "0123456789ABCDEF" to 11 chars +warning: Overwrote a non-zero byte in the title +warning: Overwrote a non-zero byte in the manufacturer code diff --git a/test/fix/title-pad.err b/test/fix/title-pad.err index e69de29b..8d4a2b4e 100644 --- a/test/fix/title-pad.err +++ b/test/fix/title-pad.err @@ -0,0 +1 @@ +warning: Overwrote a non-zero byte in the title diff --git a/test/fix/title-trunc.err b/test/fix/title-trunc.err index 24e5093a..7f4fb89c 100644 --- a/test/fix/title-trunc.err +++ b/test/fix/title-trunc.err @@ -1 +1,2 @@ warning: Truncating title "0123456789ABCDEFGHIJK" to 16 chars +warning: Overwrote a non-zero byte in the title diff --git a/test/fix/title.err b/test/fix/title.err index e69de29b..8d4a2b4e 100644 --- a/test/fix/title.err +++ b/test/fix/title.err @@ -0,0 +1 @@ +warning: Overwrote a non-zero byte in the title diff --git a/test/fix/verify-pad.err b/test/fix/verify-pad.err index e69de29b..09950e4f 100644 --- a/test/fix/verify-pad.err +++ b/test/fix/verify-pad.err @@ -0,0 +1,2 @@ +warning: Overwrote a non-zero byte in the Nintendo logo +warning: Overwrote a non-zero byte in the header checksum diff --git a/test/fix/verify-trash.err b/test/fix/verify-trash.err index e69de29b..09950e4f 100644 --- a/test/fix/verify-trash.err +++ b/test/fix/verify-trash.err @@ -0,0 +1,2 @@ +warning: Overwrote a non-zero byte in the Nintendo logo +warning: Overwrote a non-zero byte in the header checksum diff --git a/test/fix/verify.err b/test/fix/verify.err index e69de29b..09950e4f 100644 --- a/test/fix/verify.err +++ b/test/fix/verify.err @@ -0,0 +1,2 @@ +warning: Overwrote a non-zero byte in the Nintendo logo +warning: Overwrote a non-zero byte in the header checksum diff --git a/test/fix/version.err b/test/fix/version.err index e69de29b..a882d2c6 100644 --- a/test/fix/version.err +++ b/test/fix/version.err @@ -0,0 +1 @@ +warning: Overwrote a non-zero byte in the mask ROM version number