2021-10-09 00:58:55 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Copyright (C) 2016-2021 Maschell
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
****************************************************************************/
|
|
|
|
#include "WiiUPartition.h"
|
2022-07-26 08:16:27 +02:00
|
|
|
#include <coreinit/debug.h>
|
|
|
|
#include <memory>
|
2021-10-09 00:58:55 +02:00
|
|
|
|
2021-10-11 22:02:35 +02:00
|
|
|
uint64_t WiiUPartition::getSectionOffsetOnDefaultPartition() {
|
|
|
|
if (volumes.size() != 1) {
|
|
|
|
OSFatal("We have more or less than 1 volume header.");
|
2021-10-09 00:58:55 +02:00
|
|
|
}
|
2021-10-11 22:02:35 +02:00
|
|
|
return volumes.begin()->first.getAddressInBytes();
|
2021-10-09 00:58:55 +02:00
|
|
|
}
|
|
|
|
|
2021-10-11 22:02:35 +02:00
|
|
|
std::optional<std::shared_ptr<WiiUPartition>> WiiUPartition::make_shared(const std::shared_ptr<DiscReader> &discReader, uint32_t offset, const DiscBlockSize &blockSize) {
|
2021-10-09 00:58:55 +02:00
|
|
|
auto buffer = (uint8_t *) malloc(LENGTH);
|
|
|
|
if (buffer == nullptr) {
|
2021-10-11 22:02:35 +02:00
|
|
|
return {};
|
2021-10-09 00:58:55 +02:00
|
|
|
}
|
2021-10-11 22:02:35 +02:00
|
|
|
if (!discReader->hasDiscKey) {
|
|
|
|
if (!discReader->readEncrypted(buffer, offset, LENGTH)) {
|
|
|
|
return {};
|
2021-10-09 00:58:55 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
auto bufferBigger = (uint8_t *) malloc(LENGTH + 0x10);
|
|
|
|
if (bufferBigger == nullptr) {
|
2021-10-11 22:02:35 +02:00
|
|
|
return {};
|
2021-10-09 00:58:55 +02:00
|
|
|
}
|
2021-10-11 22:02:35 +02:00
|
|
|
if (!discReader->readDecrypted(bufferBigger, offset - 0x10, 0, LENGTH + 0x10, discReader->discKey, nullptr, true)) {
|
|
|
|
return {};
|
2021-10-09 00:58:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(buffer, bufferBigger + 0x10, LENGTH);
|
|
|
|
|
|
|
|
free(bufferBigger);
|
|
|
|
}
|
|
|
|
|
|
|
|
char name[32];
|
|
|
|
memset(name, 0, sizeof(name));
|
|
|
|
memcpy(name, buffer, 31);
|
2021-10-11 22:02:35 +02:00
|
|
|
auto volumeId = name;
|
2022-07-26 08:16:27 +02:00
|
|
|
uint8_t num = buffer[31];
|
2021-10-09 00:58:55 +02:00
|
|
|
|
2021-10-11 22:02:35 +02:00
|
|
|
std::map<AddressInDiscBlocks, std::shared_ptr<VolumeHeader>> volumes;
|
|
|
|
|
2021-10-09 00:58:55 +02:00
|
|
|
for (int i = 0; i < num; i++) {
|
2022-07-26 08:16:27 +02:00
|
|
|
auto address = *((uint32_t *) &buffer[32 + (i * 4)]);
|
2021-10-09 00:58:55 +02:00
|
|
|
AddressInDiscBlocks discLbaAddress = AddressInDiscBlocks(blockSize, address);
|
2022-07-26 08:16:27 +02:00
|
|
|
auto vh = VolumeHeader::make_shared(discReader, discLbaAddress.getAddressInBytes());
|
2021-10-11 22:02:35 +02:00
|
|
|
if (!vh.has_value()) {
|
|
|
|
free(buffer);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
volumes[discLbaAddress] = vh.value();
|
2021-10-09 00:58:55 +02:00
|
|
|
}
|
|
|
|
|
2021-10-11 22:02:35 +02:00
|
|
|
auto fileSystemDescriptor = ((uint16_t *) &buffer[64])[0];
|
2021-10-09 00:58:55 +02:00
|
|
|
|
|
|
|
free(buffer);
|
2021-10-11 22:02:35 +02:00
|
|
|
|
|
|
|
return std::unique_ptr<WiiUPartition>(new WiiUPartition(
|
|
|
|
volumeId,
|
|
|
|
volumes,
|
|
|
|
fileSystemDescriptor));
|
2021-10-09 00:58:55 +02:00
|
|
|
}
|
|
|
|
|
2022-07-26 08:16:27 +02:00
|
|
|
std::string WiiUPartition::getVolumeId() const & {
|
2021-10-11 22:02:35 +02:00
|
|
|
return volumeId;
|
|
|
|
}
|
|
|
|
|
2022-07-26 08:16:27 +02:00
|
|
|
std::map<AddressInDiscBlocks, std::shared_ptr<VolumeHeader>> WiiUPartition::getVolumes() const & {
|
2021-10-11 22:02:35 +02:00
|
|
|
return volumes;
|
2021-10-09 00:58:55 +02:00
|
|
|
}
|
|
|
|
|
2021-10-11 22:02:35 +02:00
|
|
|
uint16_t WiiUPartition::getFileSystemDescriptor() const {
|
|
|
|
return fileSystemDescriptor;
|
|
|
|
}
|
|
|
|
|
|
|
|
WiiUPartition::~WiiUPartition() = default;
|