2019-06-24 10:05:51 +01:00
|
|
|
/*
|
2020-01-24 10:10:40 +00:00
|
|
|
* Copyright (c) 2018-2020 Atmosphère-NX
|
2019-06-24 10:05:51 +01:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
#include "ro_nro_utils.hpp"
|
|
|
|
|
2019-10-24 10:30:10 +01:00
|
|
|
namespace ams::ro::impl {
|
2019-06-24 10:05:51 +01:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
constexpr size_t MaxMapRetries = 0x200;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Result MapNro(u64 *out_base_address, Handle process_handle, u64 nro_heap_address, u64 nro_heap_size, u64 bss_heap_address, u64 bss_heap_size) {
|
2019-10-24 09:40:44 +01:00
|
|
|
map::MappedCodeMemory nro_mcm(ResultInternalError{});
|
|
|
|
map::MappedCodeMemory bss_mcm(ResultInternalError{});
|
2019-06-24 10:05:51 +01:00
|
|
|
u64 base_address;
|
|
|
|
|
|
|
|
/* Map the NRO, and map the BSS immediately after it. */
|
|
|
|
size_t i;
|
|
|
|
for (i = 0; i < MaxMapRetries; i++) {
|
2019-10-24 09:40:44 +01:00
|
|
|
map::MappedCodeMemory tmp_nro_mcm(ResultInternalError{});
|
2019-06-24 10:05:51 +01:00
|
|
|
R_TRY(map::MapCodeMemoryInProcess(tmp_nro_mcm, process_handle, nro_heap_address, nro_heap_size));
|
|
|
|
base_address = tmp_nro_mcm.GetDstAddress();
|
|
|
|
|
|
|
|
if (bss_heap_size > 0) {
|
|
|
|
map::MappedCodeMemory tmp_bss_mcm(process_handle, base_address + nro_heap_size, bss_heap_address, bss_heap_size);
|
|
|
|
R_TRY_CATCH(tmp_bss_mcm.GetResult()) {
|
2020-02-14 06:05:20 +00:00
|
|
|
R_CATCH(svc::ResultInvalidCurrentMemory) {
|
2019-06-24 10:05:51 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} R_END_TRY_CATCH;
|
|
|
|
|
|
|
|
if (!map::CanAddGuardRegionsInProcess(process_handle, base_address, nro_heap_size + bss_heap_size)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
bss_mcm = std::move(tmp_bss_mcm);
|
|
|
|
} else {
|
|
|
|
if (!map::CanAddGuardRegionsInProcess(process_handle, base_address, nro_heap_size)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
nro_mcm = std::move(tmp_nro_mcm);
|
|
|
|
break;
|
|
|
|
}
|
2019-10-24 09:40:44 +01:00
|
|
|
R_UNLESS(i < MaxMapRetries, ResultOutOfAddressSpace());
|
2019-06-24 10:05:51 +01:00
|
|
|
|
|
|
|
/* Invalidation here actually prevents them from unmapping at scope exit. */
|
|
|
|
nro_mcm.Invalidate();
|
|
|
|
bss_mcm.Invalidate();
|
|
|
|
|
|
|
|
*out_base_address = base_address;
|
2019-10-24 09:40:44 +01:00
|
|
|
return ResultSuccess();
|
2019-06-24 10:05:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result SetNroPerms(Handle process_handle, u64 base_address, u64 rx_size, u64 ro_size, u64 rw_size) {
|
|
|
|
const u64 rx_offset = 0;
|
|
|
|
const u64 ro_offset = rx_offset + rx_size;
|
|
|
|
const u64 rw_offset = ro_offset + ro_size;
|
|
|
|
|
|
|
|
R_TRY(svcSetProcessMemoryPermission(process_handle, base_address + rx_offset, rx_size, Perm_Rx));
|
|
|
|
R_TRY(svcSetProcessMemoryPermission(process_handle, base_address + ro_offset, ro_size, Perm_R ));
|
|
|
|
R_TRY(svcSetProcessMemoryPermission(process_handle, base_address + rw_offset, rw_size, Perm_Rw));
|
|
|
|
|
2019-10-24 09:40:44 +01:00
|
|
|
return ResultSuccess();
|
2019-06-24 10:05:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result UnmapNro(Handle process_handle, u64 base_address, u64 nro_heap_address, u64 bss_heap_address, u64 bss_heap_size, u64 code_size, u64 rw_size) {
|
|
|
|
/* First, unmap bss. */
|
|
|
|
if (bss_heap_size > 0) {
|
|
|
|
R_TRY(svcUnmapProcessCodeMemory(process_handle, base_address + code_size + rw_size, bss_heap_address, bss_heap_size));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Next, unmap .rwdata */
|
|
|
|
if (rw_size > 0) {
|
|
|
|
R_TRY(svcUnmapProcessCodeMemory(process_handle, base_address + code_size, nro_heap_address + code_size, rw_size));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Finally, unmap .text + .rodata. */
|
|
|
|
R_TRY(svcUnmapProcessCodeMemory(process_handle, base_address, nro_heap_address, code_size));
|
|
|
|
|
2019-10-24 09:40:44 +01:00
|
|
|
return ResultSuccess();
|
2019-06-24 10:05:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|