ams_mitm: refactor for R_TRY

This commit is contained in:
Michael Scire
2019-06-20 04:04:33 -07:00
parent 4191dcee75
commit 60b831f369
32 changed files with 662 additions and 1246 deletions

View File

@@ -13,7 +13,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <switch.h>
#include <cstring>
@@ -22,7 +22,7 @@
#include "fs_istorage.hpp"
/* Represents a sectored storage. */
template<u64 SectorSize>
template<u64 SectorSize>
class SectoredProxyStorage : public ProxyStorage {
private:
u64 cur_seek = 0;
@@ -39,19 +39,16 @@ class SectoredProxyStorage : public ProxyStorage {
SectoredProxyStorage(FsStorage s) : ProxyStorage(s) { }
public:
virtual Result Read(void *_buffer, size_t size, u64 offset) override {
Result rc = ResultSuccess;
u8 *buffer = static_cast<u8 *>(_buffer);
this->Seek(offset);
if (this->cur_sector_ofs == 0 && size % SectorSize == 0) {
/* Fast case. */
return ProxyStorage::Read(buffer, size, offset);
}
if (R_FAILED((rc = ProxyStorage::Read(this->sector_buf, SectorSize, this->cur_seek)))) {
return rc;
}
R_TRY(ProxyStorage::Read(this->sector_buf, SectorSize, this->cur_seek));
if (size + this->cur_sector_ofs <= SectorSize) {
memcpy(buffer, sector_buf + this->cur_sector_ofs, size);
} else {
@@ -59,90 +56,77 @@ class SectoredProxyStorage : public ProxyStorage {
size_t ofs = SectorSize - this->cur_sector_ofs;
memcpy(buffer, sector_buf + this->cur_sector_ofs, ofs);
size -= ofs;
/* We're guaranteed alignment, here. */
const size_t aligned_remaining_size = size - (size % SectorSize);
if (aligned_remaining_size) {
if (R_FAILED((rc = ProxyStorage::Read(buffer + ofs, aligned_remaining_size, offset + ofs)))) {
return rc;
}
R_TRY(ProxyStorage::Read(buffer + ofs, aligned_remaining_size, offset + ofs));
ofs += aligned_remaining_size;
size -= aligned_remaining_size;
}
/* Read any leftover data. */
if (size) {
if (R_FAILED((rc = ProxyStorage::Read(this->sector_buf, SectorSize, offset + ofs)))) {
return rc;
}
R_TRY(ProxyStorage::Read(this->sector_buf, SectorSize, offset + ofs));
memcpy(buffer + ofs, sector_buf, size);
}
}
return rc;
};
return ResultSuccess;
}
virtual Result Write(void *_buffer, size_t size, u64 offset) override {
Result rc = ResultSuccess;
u8 *buffer = static_cast<u8 *>(_buffer);
this->Seek(offset);
if (this->cur_sector_ofs == 0 && size % SectorSize == 0) {
/* Fast case. */
return ProxyStorage::Write(buffer, size, offset);
}
if (R_FAILED((rc = ProxyStorage::Read(this->sector_buf, SectorSize, this->cur_seek)))) {
return rc;
}
R_TRY(ProxyStorage::Read(this->sector_buf, SectorSize, this->cur_seek));
if (size + this->cur_sector_ofs <= SectorSize) {
memcpy(this->sector_buf + this->cur_sector_ofs, buffer, size);
rc = ProxyStorage::Write(this->sector_buf, SectorSize, this->cur_seek);
R_TRY(ProxyStorage::Write(this->sector_buf, SectorSize, this->cur_seek));
} else {
/* Leaving the sector... */
size_t ofs = SectorSize - this->cur_sector_ofs;
memcpy(this->sector_buf + this->cur_sector_ofs, buffer, ofs);
if (R_FAILED((rc = ProxyStorage::Write(this->sector_buf, ofs, this->cur_seek)))) {
return rc;
}
R_TRY(ProxyStorage::Write(this->sector_buf, ofs, this->cur_seek));
size -= ofs;
/* We're guaranteed alignment, here. */
const size_t aligned_remaining_size = size - (size % SectorSize);
if (aligned_remaining_size) {
if (R_FAILED((rc = ProxyStorage::Write(buffer + ofs, aligned_remaining_size, offset + ofs)))) {
return rc;
}
R_TRY(ProxyStorage::Write(buffer + ofs, aligned_remaining_size, offset + ofs));
ofs += aligned_remaining_size;
size -= aligned_remaining_size;
}
/* Write any leftover data. */
if (size) {
if (R_FAILED((rc = ProxyStorage::Read(this->sector_buf, SectorSize, offset + ofs)))) {
return rc;
}
R_TRY(ProxyStorage::Read(this->sector_buf, SectorSize, offset + ofs));
memcpy(this->sector_buf, buffer + ofs, size);
rc = ProxyStorage::Write(this->sector_buf, SectorSize, this->cur_seek);
R_TRY(ProxyStorage::Write(this->sector_buf, SectorSize, this->cur_seek));
}
}
return rc;
};
}
return ResultSuccess;
}
};
/* Represents an RCM-preserving BOOT0 partition. */
class Boot0Storage : public SectoredProxyStorage<0x200> {
using Base = SectoredProxyStorage<0x200>;
public:
static constexpr u64 BctEndOffset = 0xFC000;
static constexpr u64 BctSize = 0x4000;
static constexpr u64 BctPubkStart = 0x210;
static constexpr u64 BctPubkSize = 0x100;
static constexpr u64 BctPubkEnd = BctPubkStart + BctPubkSize;
static constexpr u64 EksStart = 0x180000;
static constexpr u64 EksSize = 0x4000;
static constexpr u64 EksEnd = EksStart + EksSize;
@@ -154,6 +138,6 @@ class Boot0Storage : public SectoredProxyStorage<0x200> {
Boot0Storage(FsStorage *s, u64 t) : Base(s), title_id(t) { }
Boot0Storage(FsStorage s, u64 t) : Base(s), title_id(t) { }
public:
virtual Result Read(void *_buffer, size_t size, u64 offset) override;
virtual Result Read(void *_buffer, size_t size, u64 offset) override;
virtual Result Write(void *_buffer, size_t size, u64 offset) override;
};