2016-07-06 12:33:05 -06:00
|
|
|
// Copyright 2009 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2+
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2017-03-03 12:43:52 -07:00
|
|
|
#include "DiscIO/Volume.h"
|
|
|
|
|
2016-07-06 12:33:05 -06:00
|
|
|
#include <algorithm>
|
|
|
|
#include <map>
|
2017-05-19 10:33:21 -06:00
|
|
|
#include <memory>
|
2017-06-04 02:33:14 -06:00
|
|
|
#include <optional>
|
2016-07-06 12:33:05 -06:00
|
|
|
#include <string>
|
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "Common/ColorUtil.h"
|
|
|
|
#include "Common/CommonTypes.h"
|
|
|
|
#include "Common/FileUtil.h"
|
2017-05-06 09:45:08 -06:00
|
|
|
#include "Common/NandPaths.h"
|
2016-07-06 12:33:05 -06:00
|
|
|
#include "Common/StringUtil.h"
|
2017-03-03 12:43:52 -07:00
|
|
|
#include "Common/Swap.h"
|
|
|
|
|
2017-05-19 10:33:21 -06:00
|
|
|
#include "DiscIO/Blob.h"
|
2016-07-06 12:33:05 -06:00
|
|
|
#include "DiscIO/Enums.h"
|
2017-05-19 10:33:21 -06:00
|
|
|
#include "DiscIO/VolumeDirectory.h"
|
|
|
|
#include "DiscIO/VolumeGC.h"
|
|
|
|
#include "DiscIO/VolumeWad.h"
|
|
|
|
#include "DiscIO/VolumeWiiCrypted.h"
|
2016-07-06 12:33:05 -06:00
|
|
|
|
|
|
|
namespace DiscIO
|
|
|
|
{
|
|
|
|
static const unsigned int WII_BANNER_WIDTH = 192;
|
|
|
|
static const unsigned int WII_BANNER_HEIGHT = 64;
|
|
|
|
static const unsigned int WII_BANNER_SIZE = WII_BANNER_WIDTH * WII_BANNER_HEIGHT * 2;
|
|
|
|
static const unsigned int WII_BANNER_OFFSET = 0xA0;
|
|
|
|
|
2017-05-20 10:33:36 -06:00
|
|
|
const IOS::ES::TicketReader IVolume::INVALID_TICKET{};
|
|
|
|
const IOS::ES::TMDReader IVolume::INVALID_TMD{};
|
|
|
|
|
2016-07-06 12:33:05 -06:00
|
|
|
std::vector<u32> IVolume::GetWiiBanner(int* width, int* height, u64 title_id)
|
|
|
|
{
|
|
|
|
*width = 0;
|
|
|
|
*height = 0;
|
|
|
|
|
2017-05-06 09:45:08 -06:00
|
|
|
const std::string file_name =
|
|
|
|
Common::GetTitleDataPath(title_id, Common::FROM_CONFIGURED_ROOT) + "banner.bin";
|
2016-07-06 12:33:05 -06:00
|
|
|
if (!File::Exists(file_name))
|
|
|
|
return std::vector<u32>();
|
|
|
|
|
|
|
|
if (File::GetSize(file_name) < WII_BANNER_OFFSET + WII_BANNER_SIZE)
|
|
|
|
return std::vector<u32>();
|
|
|
|
|
|
|
|
File::IOFile file(file_name, "rb");
|
|
|
|
if (!file.Seek(WII_BANNER_OFFSET, SEEK_SET))
|
|
|
|
return std::vector<u32>();
|
|
|
|
|
|
|
|
std::vector<u8> banner_file(WII_BANNER_SIZE);
|
|
|
|
if (!file.ReadBytes(banner_file.data(), banner_file.size()))
|
|
|
|
return std::vector<u32>();
|
|
|
|
|
|
|
|
std::vector<u32> image_buffer(WII_BANNER_WIDTH * WII_BANNER_HEIGHT);
|
|
|
|
ColorUtil::decode5A3image(image_buffer.data(), (u16*)banner_file.data(), WII_BANNER_WIDTH,
|
|
|
|
WII_BANNER_HEIGHT);
|
|
|
|
|
|
|
|
*width = WII_BANNER_WIDTH;
|
|
|
|
*height = WII_BANNER_HEIGHT;
|
|
|
|
return image_buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::map<Language, std::string> IVolume::ReadWiiNames(const std::vector<u8>& data)
|
|
|
|
{
|
|
|
|
std::map<Language, std::string> names;
|
|
|
|
for (size_t i = 0; i < NUMBER_OF_LANGUAGES; ++i)
|
|
|
|
{
|
|
|
|
size_t name_start = NAME_BYTES_LENGTH * i;
|
|
|
|
size_t name_end = name_start + NAME_BYTES_LENGTH;
|
|
|
|
if (data.size() >= name_end)
|
|
|
|
{
|
|
|
|
u16* temp = (u16*)(data.data() + name_start);
|
|
|
|
std::wstring out_temp(NAME_STRING_LENGTH, '\0');
|
|
|
|
std::transform(temp, temp + out_temp.size(), out_temp.begin(), (u16(&)(u16))Common::swap16);
|
|
|
|
out_temp.erase(std::find(out_temp.begin(), out_temp.end(), 0x00), out_temp.end());
|
|
|
|
std::string name = UTF16ToUTF8(out_temp);
|
|
|
|
if (!name.empty())
|
|
|
|
names[static_cast<Language>(i)] = name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return names;
|
|
|
|
}
|
2017-05-19 10:33:21 -06:00
|
|
|
|
|
|
|
std::unique_ptr<IVolume> CreateVolumeFromFilename(const std::string& filename)
|
|
|
|
{
|
|
|
|
std::unique_ptr<IBlobReader> reader(CreateBlobReader(filename));
|
|
|
|
if (reader == nullptr)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
// Check for Wii
|
2017-06-04 02:33:14 -06:00
|
|
|
const std::optional<u32> wii_magic = reader->ReadSwapped<u32>(0x18);
|
|
|
|
if (wii_magic == u32(0x5D1C9EA3))
|
Reimplement support for unencrypted Wii discs
You may want to read the PR #2047 comments before reading this.
Dolphin attempts to support an unencrypted type of Wii discs
that apparently is identified by a 4-byte integer at 0x60
being non-zero. I don't know what discs (if any) would be
using that format, so I haven't been able to test Dolphin's
support for it, but it has probably been broken for a while.
The old implementation is very short but also strange.
In CreateVolumeFromFilename, we read a 4-byte integer from
0x60, and if it's non-zero, we create a CVolumeGC object
instead of a CVolumeWiiCrypted object. This might seem like
it makes no sense, but it presumably worked in the past
because IsVolumeWiiDisc used to check the volume type by
reading the magic word for Wii straight from the disc,
meaning that CVolumeGC objects representing unencrypted Wii
discs would be treated as Wii discs by pretty much all of
Dolphin's code except for the volume implementation code.
(It wasn't possible to simply use CVolumeWiiCrypted, because
that class only handled encrypted discs, like the name says.)
However, that stopped working as intended because of ace0607.
And furthermore, bb93336 made it even more broken by making
parts of Dolphin expect that data read from Wii discs needed
to be decrypted (rather than the volume implementation
implicitly deciding whether to decrypt when Read was called).
Disclaimer: Like I said before, I haven't been able to test
any of this because I don't have any discs that use this
unencrypted Wii disc format, so this is all theoretical.
Later, PR #2047 tried to remove Dolphin's support for
the unencrypted Wii disc format because seemingly no
discs used it, but the PR got closed without being merged.
At the end of that PR, I said that I would make a new PR
with a better implementation for the format after PR #2353
was merged. Now that PR #2353 is merged (two years later...)
and PR #5521 is merged, the new implementation was easy to
make, and here it is!
Untested.
2017-06-05 06:54:37 -06:00
|
|
|
return std::make_unique<CVolumeWiiCrypted>(std::move(reader));
|
2017-05-19 10:33:21 -06:00
|
|
|
|
|
|
|
// Check for WAD
|
|
|
|
// 0x206962 for boot2 wads
|
2017-06-04 02:33:14 -06:00
|
|
|
const std::optional<u32> wad_magic = reader->ReadSwapped<u32>(0x02);
|
|
|
|
if (wad_magic == u32(0x00204973) || wad_magic == u32(0x00206962))
|
2017-05-19 10:33:21 -06:00
|
|
|
return std::make_unique<CVolumeWAD>(std::move(reader));
|
|
|
|
|
|
|
|
// Check for GC
|
2017-06-04 02:33:14 -06:00
|
|
|
const std::optional<u32> gc_magic = reader->ReadSwapped<u32>(0x1C);
|
|
|
|
if (gc_magic == u32(0xC2339F3D))
|
2017-05-19 10:33:21 -06:00
|
|
|
return std::make_unique<CVolumeGC>(std::move(reader));
|
|
|
|
|
|
|
|
// No known magic words found
|
|
|
|
return nullptr;
|
2016-07-06 12:33:05 -06:00
|
|
|
}
|
2017-05-19 10:33:21 -06:00
|
|
|
|
|
|
|
std::unique_ptr<IVolume> CreateVolumeFromDirectory(const std::string& directory, bool is_wii,
|
|
|
|
const std::string& apploader,
|
|
|
|
const std::string& dol)
|
|
|
|
{
|
|
|
|
if (CVolumeDirectory::IsValidDirectory(directory))
|
|
|
|
return std::make_unique<CVolumeDirectory>(directory, is_wii, apploader, dol);
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|