diff --git a/stratosphere/loader/source/ldr_launch_record.cpp b/stratosphere/loader/source/ldr_launch_record.cpp index aca54a1ea..4135d4f6e 100644 --- a/stratosphere/loader/source/ldr_launch_record.cpp +++ b/stratosphere/loader/source/ldr_launch_record.cpp @@ -22,15 +22,43 @@ namespace ams::ldr { /* Global cache. */ std::set g_launched_programs; + constexpr size_t NumSystemProgramIds = ncm::SystemProgramId::End.value - ncm::SystemProgramId::Start.value + 1; + static_assert(util::IsPowerOfTwo(NumSystemProgramIds)); + static_assert(util::IsAligned(NumSystemProgramIds, BITSIZEOF(u64))); + + bool IsTrackableSystemProgramId(ncm::ProgramId program_id) { + return ncm::SystemProgramId::Start <= program_id && program_id <= ncm::SystemProgramId::End; + } + + u64 g_system_launch_records[NumSystemProgramIds / BITSIZEOF(u64)]; + + void SetLaunchedSystemProgram(ncm::SystemProgramId program_id) { + const u64 val = program_id.value - ncm::SystemProgramId::Start.value; + g_system_launch_records[val / BITSIZEOF(u64)] |= (1ul << (val % BITSIZEOF(u64))); + } + + bool HasLaunchedSystemProgram(ncm::SystemProgramId program_id) { + const u64 val = program_id.value - ncm::SystemProgramId::Start.value; + return (g_system_launch_records[val / BITSIZEOF(u64)] & (1ul << (val % BITSIZEOF(u64)))) != 0; + } + } /* Launch Record API. */ bool HasLaunchedProgram(ncm::ProgramId program_id) { - return g_launched_programs.find(static_cast(program_id)) != g_launched_programs.end(); + if (IsTrackableSystemProgramId(program_id)) { + return HasLaunchedSystemProgram(ncm::SystemProgramId{program_id.value}); + } else { + return g_launched_programs.find(static_cast(program_id)) != g_launched_programs.end(); + } } void SetLaunchedProgram(ncm::ProgramId program_id) { - g_launched_programs.insert(static_cast(program_id)); + if (IsTrackableSystemProgramId(program_id)) { + SetLaunchedSystemProgram(ncm::SystemProgramId{program_id.value}); + } else { + g_launched_programs.insert(static_cast(program_id)); + } } }