Atmosphere/stratosphere/ams_mitm/source/fs_mitm/fsmitm_boot0storage.cpp

111 lines
3.9 KiB
C++
Raw Normal View History

/*
2019-04-08 02:00:49 +00:00
* Copyright (c) 2018-2019 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope 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/>.
*/
2019-06-20 11:04:33 +00:00
#include <switch.h>
#include <cstring>
#include <stratosphere.hpp>
#include "fsmitm_boot0storage.hpp"
static ams::os::Mutex g_boot0_mutex;
static u8 g_boot0_bct_buffer[Boot0Storage::BctEndOffset];
bool Boot0Storage::CanModifyBctPubks() {
if (IsRcmBugPatched()) {
/* RCM bug patched. */
/* Only allow NS to update the BCT pubks. */
/* AutoRCM on a patched unit will cause a brick, so homebrew should NOT be allowed to write. */
return this->title_id == ams::ncm::TitleId::Ns;
} else {
/* RCM bug unpatched. */
/* Allow homebrew but not NS to update the BCT pubks. */
return this->title_id != ams::ncm::TitleId::Ns;
}
}
Result Boot0Storage::Read(void *_buffer, size_t size, u64 offset) {
2019-09-24 10:15:36 +00:00
std::scoped_lock lk{g_boot0_mutex};
2019-06-20 11:04:33 +00:00
return Base::Read(_buffer, size, offset);
}
Result Boot0Storage::Write(void *_buffer, size_t size, u64 offset) {
2019-09-24 10:15:36 +00:00
std::scoped_lock lk{g_boot0_mutex};
2019-06-20 11:04:33 +00:00
u8 *buffer = static_cast<u8 *>(_buffer);
2019-06-20 11:04:33 +00:00
/* Protect the keyblob region from writes. */
if (offset <= EksStart) {
if (offset + size < EksStart) {
/* Fall through, no need to do anything here. */
} else {
if (offset + size < EksEnd) {
/* Adjust size to avoid writing end of data. */
size = EksStart - offset;
} else {
/* Perform portion of write falling past end of keyblobs. */
const u64 diff = EksEnd - offset;
2019-06-20 11:04:33 +00:00
R_TRY(Base::Write(buffer + diff, size - diff, EksEnd));
/* Adjust size to avoid writing end of data. */
size = EksStart - offset;
}
}
} else {
if (offset < EksEnd) {
if (offset + size < EksEnd) {
/* Ignore writes falling strictly within the region. */
return ResultSuccess();
} else {
/* Only write past the end of the keyblob region. */
buffer = buffer + (EksEnd - offset);
size -= (EksEnd - offset);
offset = EksEnd;
}
} else {
/* Fall through, no need to do anything here. */
}
}
2019-06-20 11:04:33 +00:00
if (size == 0) {
return ResultSuccess();
}
2019-06-20 11:04:33 +00:00
/* We care about protecting autorcm from NS. */
if (CanModifyBctPubks() || offset >= BctEndOffset || (offset + BctSize >= BctEndOffset && offset % BctSize >= BctPubkEnd)) {
return Base::Write(buffer, size, offset);
}
2019-06-20 11:04:33 +00:00
/* First, let's deal with the data past the end. */
if (offset + size >= BctEndOffset) {
const u64 diff = BctEndOffset - offset;
2019-06-20 11:04:33 +00:00
R_TRY(ProxyStorage::Write(buffer + diff, size - diff, BctEndOffset));
size = diff;
}
2019-06-20 11:04:33 +00:00
/* Read in the current BCT region. */
2019-06-20 11:04:33 +00:00
R_TRY(ProxyStorage::Read(g_boot0_bct_buffer, BctEndOffset, 0));
/* Update the bct buffer. */
for (u64 cur_ofs = offset; cur_ofs < BctEndOffset && cur_ofs < offset + size; cur_ofs++) {
const u64 cur_bct_rel_ofs = cur_ofs % BctSize;
if (cur_bct_rel_ofs < BctPubkStart || BctPubkEnd <= cur_bct_rel_ofs) {
g_boot0_bct_buffer[cur_ofs] = buffer[cur_ofs - offset];
}
}
2019-06-20 11:04:33 +00:00
return ProxyStorage::Write(g_boot0_bct_buffer, BctEndOffset, 0);
}