wudd/source/WUD/DiscReaderDiscDrive.cpp

120 lines
4.4 KiB
C++
Raw Normal View History

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/>.
****************************************************************************/
2022-07-26 08:16:27 +02:00
#include "DiscReaderDiscDrive.h"
#include "utils/utils.h"
2021-10-09 00:58:55 +02:00
#include <WUD/content/WiiUDiscContentsHeader.h>
#include <common/common.h>
#include <malloc.h>
#include <mocha/fsa.h>
#include <mocha/mocha.h>
2021-10-09 00:58:55 +02:00
#include <utils/logger.h>
2022-07-26 08:16:27 +02:00
#include <utils/rijndael.h>
2021-10-09 00:58:55 +02:00
DiscReaderDiscDrive::DiscReaderDiscDrive() : DiscReader() {
auto sector_buf = (uint8_t *) memalign(0x40, (size_t) READ_SECTOR_SIZE);
if (!sector_buf) {
DEBUG_FUNCTION_LINE_ERR("Failed to allocate buffer");
2021-10-09 00:58:55 +02:00
return;
}
auto ret = FSAEx_RawOpen(__wut_devoptab_fs_client, "/dev/odd01", &device_handle);
2021-10-09 00:58:55 +02:00
if (ret < 0) {
free(sector_buf);
2021-10-09 00:58:55 +02:00
return;
}
auto res = FSAEx_RawRead(__wut_devoptab_fs_client, sector_buf, READ_SECTOR_SIZE, 1, 3, device_handle);
2021-10-09 00:58:55 +02:00
if (res >= 0) {
if (((uint32_t *) sector_buf)[0] != WiiUDiscContentsHeader::MAGIC) {
uint8_t iv[16];
memset(iv, 0, 16);
WUDDiscKey discKeyLocal;
auto discKeyRes = Mocha_ODMGetDiscKey(&discKeyLocal);
if (discKeyRes == MOCHA_RESULT_SUCCESS) {
hasDiscKey = true;
memcpy(this->discKey, discKeyLocal.key, 16);
auto sector_buf_decrypted = make_unique_nothrow<uint8_t[]>((size_t) READ_SECTOR_SIZE);
if (!sector_buf_decrypted) {
DEBUG_FUNCTION_LINE_ERR("Failed to allocate memory");
free(sector_buf);
return;
2021-10-09 00:58:55 +02:00
}
aes_set_key((uint8_t *) discKey);
aes_decrypt((uint8_t *) iv, (uint8_t *) &sector_buf[0], (uint8_t *) &sector_buf_decrypted[0], READ_SECTOR_SIZE);
if (((uint32_t *) sector_buf_decrypted.get())[0] == WiiUDiscContentsHeader::MAGIC) {
this->init_done = true;
} else {
DEBUG_FUNCTION_LINE_ERR("Invalid disc key");
}
} else {
DEBUG_FUNCTION_LINE_ERR("Failed to get the DiscKey");
this->init_done = false;
2021-10-09 00:58:55 +02:00
}
} else {
this->init_done = true;
}
} else {
DEBUG_FUNCTION_LINE_ERR("Raw read failed %d", ret);
2021-10-09 00:58:55 +02:00
}
}
bool DiscReaderDiscDrive::readEncryptedSector(uint8_t *buffer, uint32_t block_cnt, uint32_t block_offset) const {
if (FSAEx_RawRead(__wut_devoptab_fs_client, buffer, READ_SECTOR_SIZE, block_cnt, block_offset, device_handle) < 0) {
2021-10-09 00:58:55 +02:00
return false;
}
return true;
}
bool DiscReaderDiscDrive::IsReady() {
return init_done;
}
DiscReaderDiscDrive::~DiscReaderDiscDrive() {
if (device_handle != -1) {
FSAEx_RawClose(__wut_devoptab_fs_client, device_handle);
device_handle = -1;
2021-10-09 00:58:55 +02:00
}
}
bool DiscReaderDiscDrive::readEncrypted(uint8_t *buf, uint64_t offset, uint32_t size) {
if (size == 0) {
return true;
}
if ((offset & (SECTOR_SIZE - 0x1)) != 0 || (size & (SECTOR_SIZE - 0x1)) != 0) {
2021-10-09 00:58:55 +02:00
return DiscReader::readEncrypted(buf, offset, size);
}
2022-07-26 08:16:27 +02:00
uint32_t block_cnt = size >> 15;
2021-10-09 00:58:55 +02:00
uint32_t offset_in_sectors = offset >> 15;
if (FSAEx_RawRead(__wut_devoptab_fs_client, buf, 0x8000, block_cnt, offset_in_sectors, device_handle) < 0) {
DEBUG_FUNCTION_LINE_ERR("Failed to read from Disc");
2021-10-09 00:58:55 +02:00
return false;
}
return true;
}
std::optional<std::unique_ptr<DiscReaderDiscDrive>> DiscReaderDiscDrive::make_unique() {
auto discReader = make_unique_nothrow<DiscReaderDiscDrive>();
if (!discReader || !discReader->IsReady()) {
DEBUG_FUNCTION_LINE_ERR("Failed to init DiscReader %d %d", !discReader, discReader->IsReady());
return {};
}
return discReader;
}