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/>.
*/
#include <switch.h>
#include <cstring>
#include <stratosphere.hpp>
@@ -38,16 +38,15 @@ bool Boot0Storage::CanModifyBctPubks() {
Result Boot0Storage::Read(void *_buffer, size_t size, u64 offset) {
std::scoped_lock<HosMutex> lk{g_boot0_mutex};
return Base::Read(_buffer, size, offset);
}
Result Boot0Storage::Write(void *_buffer, size_t size, u64 offset) {
std::scoped_lock<HosMutex> lk{g_boot0_mutex};
Result rc = ResultSuccess;
u8 *buffer = static_cast<u8 *>(_buffer);
/* Protect the keyblob region from writes. */
if (offset <= EksStart) {
if (offset + size < EksStart) {
@@ -59,9 +58,7 @@ Result Boot0Storage::Write(void *_buffer, size_t size, u64 offset) {
} else {
/* Perform portion of write falling past end of keyblobs. */
const u64 diff = EksEnd - offset;
if (R_FAILED((rc = Base::Write(buffer + diff, size - diff, EksEnd)))) {
return rc;
}
R_TRY(Base::Write(buffer + diff, size - diff, EksEnd));
/* Adjust size to avoid writing end of data. */
size = EksStart - offset;
}
@@ -81,30 +78,26 @@ Result Boot0Storage::Write(void *_buffer, size_t size, u64 offset) {
/* Fall through, no need to do anything here. */
}
}
if (size == 0) {
return ResultSuccess;
}
/* We care about protecting autorcm from NS. */
if (CanModifyBctPubks() || offset >= BctEndOffset || (offset + BctSize >= BctEndOffset && offset % BctSize >= BctPubkEnd)) {
return Base::Write(buffer, size, offset);
}
/* First, let's deal with the data past the end. */
if (offset + size >= BctEndOffset) {
const u64 diff = BctEndOffset - offset;
if (R_FAILED((rc = ProxyStorage::Write(buffer + diff, size - diff, BctEndOffset)))) {
return rc;
}
R_TRY(ProxyStorage::Write(buffer + diff, size - diff, BctEndOffset));
size = diff;
}
/* Read in the current BCT region. */
if (R_FAILED((rc = ProxyStorage::Read(g_boot0_bct_buffer, BctEndOffset, 0)))) {
return rc;
}
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;
@@ -112,6 +105,6 @@ Result Boot0Storage::Write(void *_buffer, size_t size, u64 offset) {
g_boot0_bct_buffer[cur_ofs] = buffer[cur_ofs - offset];
}
}
return ProxyStorage::Write(g_boot0_bct_buffer, BctEndOffset, 0);
}