2020-04-14 01:07:37 +01:00
|
|
|
/*
|
2021-10-04 20:59:10 +01:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2020-04-14 01:07:37 +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 <stratosphere.hpp>
|
|
|
|
#include "erpt_srv_allocator.hpp"
|
|
|
|
#include "erpt_srv_context_record.hpp"
|
|
|
|
#include "erpt_srv_context.hpp"
|
|
|
|
#include "erpt_srv_reporter.hpp"
|
|
|
|
#include "erpt_srv_journal.hpp"
|
|
|
|
#include "erpt_srv_service.hpp"
|
2021-04-30 12:21:03 +01:00
|
|
|
#include "erpt_srv_forced_shutdown.hpp"
|
2020-04-14 01:07:37 +01:00
|
|
|
|
|
|
|
namespace ams::erpt::srv {
|
|
|
|
|
2021-10-10 08:14:06 +01:00
|
|
|
constinit lmem::HeapHandle g_heap_handle;
|
2022-03-06 20:08:20 +00:00
|
|
|
constinit ams::sf::ExpHeapAllocator g_sf_allocator = {};
|
2020-04-14 01:07:37 +01:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
constexpr fs::SystemSaveDataId SystemSaveDataId = 0x80000000000000D1;
|
|
|
|
constexpr u32 SystemSaveDataFlags = fs::SaveDataFlags_KeepAfterResettingSystemSaveDataWithoutUserSaveData;
|
|
|
|
constexpr s64 SystemSaveDataSize = 11_MB;
|
|
|
|
constexpr s64 SystemSaveDataJournalSize = 2720_KB;
|
|
|
|
|
2022-03-17 07:31:30 +00:00
|
|
|
constinit bool g_automatic_report_cleanup_enabled = true;
|
|
|
|
|
2020-04-14 01:07:37 +01:00
|
|
|
Result ExtendSystemSaveData() {
|
|
|
|
s64 cur_journal_size;
|
|
|
|
s64 cur_savedata_size;
|
|
|
|
|
|
|
|
R_TRY(fs::GetSaveDataJournalSize(std::addressof(cur_journal_size), SystemSaveDataId));
|
|
|
|
R_TRY(fs::GetSaveDataAvailableSize(std::addressof(cur_savedata_size), SystemSaveDataId));
|
|
|
|
|
|
|
|
if (cur_journal_size < SystemSaveDataJournalSize || cur_savedata_size < SystemSaveDataSize) {
|
2020-04-14 06:19:44 +01:00
|
|
|
if (hos::GetVersion() >= hos::Version_3_0_0) {
|
2020-04-14 01:07:37 +01:00
|
|
|
R_TRY(fs::ExtendSaveData(fs::SaveDataSpaceId::System, SystemSaveDataId, SystemSaveDataSize, SystemSaveDataJournalSize));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-26 07:14:36 +00:00
|
|
|
R_SUCCEED();
|
2020-04-14 01:07:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result MountSystemSaveData() {
|
|
|
|
fs::DisableAutoSaveDataCreation();
|
|
|
|
|
|
|
|
/* Extend the system save data. */
|
|
|
|
/* NOTE: Nintendo does not check result of this. */
|
|
|
|
ExtendSystemSaveData();
|
|
|
|
|
|
|
|
R_TRY_CATCH(fs::MountSystemSaveData(ReportStoragePath, SystemSaveDataId)) {
|
|
|
|
R_CATCH(fs::ResultTargetNotFound) {
|
|
|
|
R_TRY(fs::CreateSystemSaveData(SystemSaveDataId, SystemSaveDataSize, SystemSaveDataJournalSize, SystemSaveDataFlags));
|
|
|
|
R_TRY(fs::MountSystemSaveData(ReportStoragePath, SystemSaveDataId));
|
|
|
|
}
|
|
|
|
} R_END_TRY_CATCH;
|
|
|
|
|
2022-03-26 07:14:36 +00:00
|
|
|
R_SUCCEED();
|
2020-04-14 01:07:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Result Initialize(u8 *mem, size_t mem_size) {
|
|
|
|
R_ABORT_UNLESS(time::Initialize());
|
|
|
|
|
|
|
|
g_heap_handle = lmem::CreateExpHeap(mem, mem_size, lmem::CreateOption_ThreadSafe);
|
|
|
|
AMS_ABORT_UNLESS(g_heap_handle != nullptr);
|
|
|
|
|
2021-10-08 01:44:54 +01:00
|
|
|
fs::InitializeForSystem();
|
2020-04-14 01:07:37 +01:00
|
|
|
fs::SetAllocator(Allocate, DeallocateWithSize);
|
2021-10-08 01:44:54 +01:00
|
|
|
fs::SetEnabledAutoAbort(false);
|
2020-04-14 01:07:37 +01:00
|
|
|
|
|
|
|
R_ABORT_UNLESS(fs::MountSdCardErrorReportDirectoryForAtmosphere(ReportOnSdStoragePath));
|
|
|
|
|
2022-03-17 07:31:30 +00:00
|
|
|
if (g_automatic_report_cleanup_enabled) {
|
|
|
|
constexpr s64 MinimumReportCountForCleanup = 1000;
|
|
|
|
s64 report_count = MinimumReportCountForCleanup;
|
|
|
|
|
|
|
|
fs::DirectoryHandle dir;
|
2022-12-13 08:08:13 +00:00
|
|
|
if (R_SUCCEEDED(fs::OpenDirectory(std::addressof(dir), ReportOnSdStorageRootDirectoryPath, fs::OpenDirectoryMode_All))) {
|
2022-03-17 07:31:30 +00:00
|
|
|
ON_SCOPE_EXIT { fs::CloseDirectory(dir); };
|
|
|
|
|
|
|
|
if (R_FAILED(fs::GetDirectoryEntryCount(std::addressof(report_count), dir))) {
|
|
|
|
report_count = MinimumReportCountForCleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (report_count >= MinimumReportCountForCleanup) {
|
2022-12-13 08:08:13 +00:00
|
|
|
fs::CleanDirectoryRecursively(ReportOnSdStorageRootDirectoryPath);
|
2022-03-17 07:31:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-14 01:07:37 +01:00
|
|
|
R_ABORT_UNLESS(MountSystemSaveData());
|
|
|
|
|
2021-01-17 15:55:32 +00:00
|
|
|
g_sf_allocator.Attach(g_heap_handle);
|
|
|
|
|
2024-03-28 11:36:14 +00:00
|
|
|
for (const auto category_id : CategoryIndexToCategoryIdMap) {
|
|
|
|
Context *ctx = new Context(category_id);
|
2020-04-14 01:07:37 +01:00
|
|
|
AMS_ABORT_UNLESS(ctx != nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
Journal::Restore();
|
|
|
|
|
2021-04-30 05:48:47 +01:00
|
|
|
Reporter::UpdatePowerOnTime();
|
|
|
|
Reporter::UpdateAwakeTime();
|
2021-04-16 08:55:22 +01:00
|
|
|
|
2022-03-26 07:14:36 +00:00
|
|
|
R_SUCCEED();
|
2020-04-14 01:07:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result InitializeAndStartService() {
|
2021-04-30 12:21:03 +01:00
|
|
|
/* Initialize forced shutdown detection. */
|
|
|
|
/* NOTE: Nintendo does not check error code here. */
|
|
|
|
InitializeForcedShutdownDetection();
|
|
|
|
|
2022-03-26 21:48:33 +00:00
|
|
|
R_RETURN(InitializeService());
|
2020-04-14 01:07:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result SetSerialNumberAndOsVersion(const char *sn, u32 sn_len, const char *os, u32 os_len, const char *os_priv, u32 os_priv_len) {
|
2022-03-26 21:48:33 +00:00
|
|
|
R_RETURN(Reporter::SetSerialNumberAndOsVersion(sn, sn_len, os, os_len, os_priv, os_priv_len));
|
2020-04-14 01:07:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result SetProductModel(const char *model, u32 model_len) {
|
|
|
|
/* NOTE: Nintendo does not check that this allocation succeeds. */
|
2020-12-03 19:13:35 +00:00
|
|
|
auto record = std::make_unique<ContextRecord>(CategoryId_ProductModelInfo);
|
2020-04-14 01:07:37 +01:00
|
|
|
R_UNLESS(record != nullptr, erpt::ResultOutOfMemory());
|
|
|
|
|
2023-10-12 01:48:40 +01:00
|
|
|
R_TRY(record->Add(FieldId_ProductModel, model, model_len));
|
2020-12-03 19:13:35 +00:00
|
|
|
R_TRY(Context::SubmitContextRecord(std::move(record)));
|
2020-04-14 01:07:37 +01:00
|
|
|
|
2022-03-26 07:14:36 +00:00
|
|
|
R_SUCCEED();
|
2020-04-14 01:07:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result SetRegionSetting(const char *region, u32 region_len) {
|
|
|
|
/* NOTE: Nintendo does not check that this allocation succeeds. */
|
2020-12-03 19:13:35 +00:00
|
|
|
auto record = std::make_unique<ContextRecord>(CategoryId_RegionSettingInfo);
|
2020-04-14 01:07:37 +01:00
|
|
|
R_UNLESS(record != nullptr, erpt::ResultOutOfMemory());
|
|
|
|
|
2023-10-12 01:48:40 +01:00
|
|
|
R_TRY(record->Add(FieldId_RegionSetting, region, region_len));
|
2020-12-03 19:13:35 +00:00
|
|
|
R_TRY(Context::SubmitContextRecord(std::move(record)));
|
2020-04-14 01:07:37 +01:00
|
|
|
|
2022-03-26 07:14:36 +00:00
|
|
|
R_SUCCEED();
|
2020-04-14 01:07:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result SetRedirectNewReportsToSdCard(bool redirect) {
|
|
|
|
Reporter::SetRedirectNewReportsToSdCard(redirect);
|
2022-03-26 07:14:36 +00:00
|
|
|
R_SUCCEED();
|
2020-04-14 01:07:37 +01:00
|
|
|
}
|
|
|
|
|
2022-03-17 07:31:30 +00:00
|
|
|
Result SetEnabledAutomaticReportCleanup(bool en) {
|
|
|
|
g_automatic_report_cleanup_enabled = en;
|
2022-03-26 07:14:36 +00:00
|
|
|
R_SUCCEED();
|
2022-03-17 07:31:30 +00:00
|
|
|
}
|
|
|
|
|
2020-04-14 01:07:37 +01:00
|
|
|
void Wait() {
|
2021-04-30 12:21:03 +01:00
|
|
|
/* Get the update event. */
|
|
|
|
os::Event *event = GetForcedShutdownUpdateEvent();
|
|
|
|
|
|
|
|
/* Forever wait, saving any updates. */
|
|
|
|
while (true) {
|
|
|
|
event->Wait();
|
|
|
|
event->Clear();
|
|
|
|
SaveForcedShutdownContext();
|
|
|
|
}
|
2020-04-14 01:07:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|