diff --git a/Core/ELF/PrxDecrypter.cpp b/Core/ELF/PrxDecrypter.cpp index 1ccc6aa669..d0f37d3cf2 100644 --- a/Core/ELF/PrxDecrypter.cpp +++ b/Core/ELF/PrxDecrypter.cpp @@ -345,6 +345,17 @@ static const u32 g_key_INDEXDAT1xx[] = { 0xA34D8C80, 0x962B235D, 0x3E420548, 0x09CF9FFE, 0xD4883F5C, 0xD90E9CB5, 0x00AEF4E9, 0xF0886DE9, 0x62A58A5B, 0x52A55546, 0x971941B5, 0xF5B79FAC}; +// The blocks inside the 1.00 updater's DATA.PSAR (a version 1 archive) carry this tag. Stored raw, +// as in 1.50's mesg_led.prx, which keeps its tags and keys in a table: this one follows tag +// 0x06000000 at 0x2028 of the decrypted module. +static const u32 g_keyUPDATER_PSAR_100[] = { + 0x8C121584, 0x80D783A8, 0xDB881EEF, 0x239661BC, 0xFC88F32B, 0xDEB53FE5, + 0x6BA05A98, 0xDB550ADE, 0x3644F8F2, 0x5594D1EB, 0x933E394A, 0x02E33D7C, + 0xF5E78812, 0xEBC1F0F8, 0xC68D1B25, 0x442B1EB8, 0x7E6AB7A5, 0x924639D0, + 0x07DE716D, 0x102FB897, 0x53DDBA18, 0x982B07C6, 0x0D748A24, 0xFE5D645C, + 0x4367E78E, 0xA1B39693, 0x12ECA8A1, 0x4458FBC4, 0x9C0A55FC, 0xFA37301E, + 0x03D32454, 0x31BD92E2, 0xF7EA0323, 0x09DE73E7, 0x7983B33B, 0x0E8517DA}; + // The blocks inside an official updater's DATA.PSAR carry this tag. See Core/Util/PSARUnpack.cpp. static const u32 g_keyUPDATER_PSAR[] = { 0x77B757DE, 0xEE62DD17, 0x5D03787B, 0x59CA8644, 0x93F68D20, 0x21819328, @@ -403,7 +414,8 @@ static const TAG_INFO g_tagInfo[] = { 0x207BBF2F, g_keyUNK1, 0x5A, 0x5A }, { 0x09000000, g_key_GAMESHARE1xx, 0x4C }, { 0xBB67C59F, g_key_GAMESHARE2xx, 0x5E, 0x5E }, - { 0x0E000000, g_keyUPDATER_PSAR, 0x51, 0x00, true } + { 0x0E000000, g_keyUPDATER_PSAR, 0x51, 0x00, true }, + { 0x06000000, g_keyUPDATER_PSAR_100, 0x49, 0x00, true }, }; static const TAG_INFO *GetTagInfo(u32 tagFind) diff --git a/Core/Util/PSARUnpack.cpp b/Core/Util/PSARUnpack.cpp index 9d60f37848..61d009f781 100644 --- a/Core/Util/PSARUnpack.cpp +++ b/Core/Util/PSARUnpack.cpp @@ -619,7 +619,9 @@ bool PSARReader::Init(std::string *error) { return false; } - // The first block is a text record ending in the firmware version, e.g. "...,6.61". + // The first block is a text record ending in the firmware version, e.g. "...,6.61". In a version + // 1 archive it's only numbers (1.00's: the build, 106, and the record count, 214) and this finds + // no text, so UnpackPSAR() takes the version from the firmware's version.txt instead. const char *text = (const char *)block_.data() + 0x10; const size_t textLen = strnlen(text, PSAR_ENTRY_SIZE - 0x10); const std::string_view firstLine(text, textLen); @@ -729,7 +731,12 @@ void PSARReader::EnsureContents() { if (entryCompression_ == PSARCompression::Zlib) { entryData_.resize(contentExpandedSize_); uLongf destLen = contentExpandedSize_; - const int zResult = uncompress(entryData_.data(), &destLen, block2_.data(), decoded); + uLong sourceLen = decoded; + int zResult = uncompress2(entryData_.data(), &destLen, block2_.data(), &sourceLen); + // Handle zip streams missing their Adler-32 checksum. + if (zResult == Z_BUF_ERROR && sourceLen == (uLong)decoded && destLen == contentExpandedSize_ && decrypted_) { + zResult = Z_OK; + } if (zResult != Z_OK || destLen != contentExpandedSize_) { WARN_LOG(Log::Loader, "PSAR: inflate failed for '%s' (%d)", entryName_.c_str(), zResult); entryData_.clear(); @@ -860,6 +867,8 @@ private: bool quit_ = false; }; +static void ParseVersionTxt(std::string_view contents, InstalledFirmwareInfo *info); + bool UnpackPSAR(const u8 *psar, size_t psarSize, const Path &outputDir, const PSARUnpackOptions &options, PSARUnpackStats *stats, std::string *error) { PSARUnpackStats localStats; if (!stats) { @@ -989,6 +998,15 @@ bool UnpackPSAR(const u8 *psar, size_t psarSize, const Path &outputDir, const PS realName = CanonicalFlashPath(realName); + // A version 1 archive (1.00) has no version record, so take it from the firmware's own + // version.txt, whether or not a filter wants that file. + if (stats->firmwareVersion.empty() && equalsNoCase(realName, "flash0:/vsh/etc/version.txt")) { + const std::vector &data = reader.entryData(); + InstalledFirmwareInfo info; + ParseVersionTxt(std::string_view((const char *)data.data(), data.size()), &info); + stats->firmwareVersion = info.version; + } + if (realName.empty()) { // No list claimed this one. Still worth writing out under its short name, but a // filter has nothing to match it against. diff --git a/docs/PsarFileFormat.md b/docs/PsarFileFormat.md index ce00ffc4f5..a6cc1fa31a 100644 --- a/docs/PsarFileFormat.md +++ b/docs/PsarFileFormat.md @@ -5,7 +5,8 @@ walk one and extract the files out of it without emulating the updater, which is `Core/Util/PSARUnpack.cpp` does. The main use is getting `flash0:/font` - the PGF system fonts - out of an updater the user already has, since most UMDs carry one. -This document is what was learned reading 43 different firmware versions, from 1.50 to 6.61. It's +This document is what was learned reading every official updater from 1.00 to 6.61, plus the +pre-decrypted archives going around for the versions that never had a downloadable one. It's a description of the format rather than of the code; the code is the same shape but has the error handling. @@ -69,11 +70,14 @@ Every encrypted block - entry descriptions and file contents alike - is decoded 2. **PRX decrypt.** The block is now an ordinary PRX blob, with its tag at block+0xD0. Run it through the normal PRX decrypter (`pspDecryptPRX`). The tag official updaters use is - **0x0E000000**; `0x06000000` also appears in the wild. + **0x0E000000**. The 1.00 updater's version 1 archive uses **0x06000000** (with KIRK code + 0x49) instead. - Note that `0x0E000000`'s key is stored *unscrambled*, unlike almost every other key in - PPSSPP's tag table, so it needs the `kirk7` pass applied before use - that's what the - `scrambleKey` flag on `TAG_INFO` is for. + Both keys are stored *unscrambled*, unlike almost every other key in PPSSPP's tag table, so + they need the `kirk7` pass applied before use - that's what the `scrambleKey` flag on + `TAG_INFO` is for. They come from 1.50's `mesg_led.prx`, which keeps its keys raw in a table + of `tag, 0x90-byte key, 4 zero bytes` entries: decrypt it with `--re-decrypt` and the key + follows its tag. One wrinkle worth knowing: the decrypter reads a little past the end of the block and **uses what it finds there**, so a decoder should hand it about 16 bytes of slack from the archive. The last @@ -99,6 +103,11 @@ The first record (at +0x10) is not a file. Decoded, it holds a text string at +0 comma-separated field is the firmware version, e.g. `...,6.61`. That's the authoritative version for choosing decryption keys further down. +A version 1 archive's first record has no text. It's three u32s, `0, 106, 214` in 1.00's, which +are the build number (`build:106` in its `version.txt`) and the number of records counting this +one, and the rest is zero. The version has to come from the firmware's own +`flash0:/vsh/etc/version.txt`, which is in the archive. + There is a second record after it whose length isn't recorded anywhere. In practice it is `overhead + 100`, or `overhead + 144` on 2.7x, or `overhead + u16 at +0x90 of the first record`. Try them in that order and take the first that decodes. Round the result up to a multiple of 16 @@ -115,7 +124,7 @@ The contents block decodes to a compressed stream. Sniff the first bytes: | `KL3E` | KL3E - likewise | | `2RLZ` | LZR | -Across all 43 firmware versions examined, **every file in every archive was zlib**. KL4E does turn +In every firmware version examined, **every file in every archive was zlib**. KL4E does turn up on the PSP, but a layer further in: two kernel modules (`memlmd_01g.prx` and `loadexec_01g.prx`) are themselves KL4E-compressed *inside* their own PRX, which is a separate problem from this one. @@ -192,7 +201,7 @@ Three structural eras, visible in the shape of the output: | Firmware | Naming | Directory entries | Fonts | | --- | --- | --- | --- | -| 1.50 - 3.52 | real paths | 0-16 | 17-20 | +| 1.00 - 3.52 | real paths | 0-16 | 17-20 | | 3.71 - 4.05 | `com:`/`NNg:` groups | 50 | 21 | | 5.01 - 6.61 | flat + per-model lists | 16-17 | 21 | @@ -212,6 +221,7 @@ The "Fonts" column above counts files in `flash0:/font`, not PGFs specifically. - Don't assume there are file lists at all - 1.x and 2.x have none. - `ipl:` is a device too. - A 3.x path has no colon; a 6.x path does. +- Don't count on the first record for the version: a version 1 archive (1.00) has no text there. ## See also diff --git a/headless/Headless.cpp b/headless/Headless.cpp index 68bec897d3..06cc53ad9a 100644 --- a/headless/Headless.cpp +++ b/headless/Headless.cpp @@ -1060,7 +1060,8 @@ int main(int argc, const char* argv[]) { stats.compressionCounts[(int)PSARCompression::KL3E], stats.compressionCounts[(int)PSARCompression::LZR], stats.compressionCounts[(int)PSARCompression::Unknown]); - return ok ? 0 : 1; + // A walk that got to the end can still have failed on every file, so count those too. + return ok && stats.failed == 0 ? 0 : 1; } // Same deal for installing a game update package.