From 8177a27db946c3047f73e7800512dcff5f817069 Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Wed, 3 Jul 2019 20:01:53 -0700 Subject: [PATCH] pm: fix boot2 launch logic error --- stratosphere/pm/source/boot2/boot2_api.cpp | 43 +++++++++++++--------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/stratosphere/pm/source/boot2/boot2_api.cpp b/stratosphere/pm/source/boot2/boot2_api.cpp index 8a0862095..2d53f7fca 100644 --- a/stratosphere/pm/source/boot2/boot2_api.cpp +++ b/stratosphere/pm/source/boot2/boot2_api.cpp @@ -202,26 +202,35 @@ namespace sts::boot2 { } void LaunchFlaggedProgramsFromSdCard() { - /* Allow for user-customizable programs. */ + /* Validate that the titles directory exists. */ DIR *titles_dir = opendir("sdmc:/atmosphere/titles"); - struct dirent *ent; - if (titles_dir != nullptr) { - ON_SCOPE_EXIT { closedir(titles_dir); }; + if (titles_dir == nullptr) { + return; + } + ON_SCOPE_EXIT { closedir(titles_dir); }; - while ((ent = readdir(titles_dir)) != nullptr) { - if (strlen(ent->d_name) == 2 * sizeof(u64) && IsHexadecimal(ent->d_name)) { - ncm::TitleId title_id{strtoul(ent->d_name, nullptr, 16)}; - if (pm::info::HasLaunchedTitle(title_id)) { - return; - } - char title_path[FS_MAX_PATH]; - std::snprintf(title_path, sizeof(title_path), "sdmc:/atmosphere/titles/%s/flags/boot2.flag", ent->d_name); - FILE *f_flag = fopen(title_path, "rb"); - if (f_flag != nullptr) { - fclose(f_flag); - LaunchTitle(nullptr, ncm::TitleLocation::Make(title_id, ncm::StorageId::None), 0); - } + /* Iterate over entries in the titles directory */ + struct dirent *ent; + while ((ent = readdir(titles_dir)) != nullptr) { + /* Check that the subdirectory can be converted to a title id. */ + if (std::strlen(ent->d_name) == 2 * sizeof(ncm::TitleId) && IsHexadecimal(ent->d_name)) { + /* Check if we've already launched the title. */ + ncm::TitleId title_id{std::strtoul(ent->d_name, nullptr, 16)}; + if (pm::info::HasLaunchedTitle(title_id)) { + continue; } + + /* Check if the title is flagged. */ + char title_path[FS_MAX_PATH]; + std::snprintf(title_path, sizeof(title_path), "sdmc:/atmosphere/titles/%s/flags/boot2.flag", ent->d_name); + FILE *f_flag = fopen(title_path, "rb"); + if (f_flag == nullptr) { + continue; + } + fclose(f_flag); + + /* Actually launch the title. */ + LaunchTitle(nullptr, ncm::TitleLocation::Make(title_id, ncm::StorageId::None), 0); } } }