2018-09-07 16:00:13 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018 Atmosphère-NX
|
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2018-05-01 17:58:19 +01:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstdint>
|
2018-07-28 03:34:22 +01:00
|
|
|
#include <cstdio>
|
2018-05-01 17:58:19 +01:00
|
|
|
#include <cstring>
|
2018-07-28 03:34:22 +01:00
|
|
|
#include <dirent.h>
|
2018-05-01 17:58:19 +01:00
|
|
|
#include <malloc.h>
|
|
|
|
|
|
|
|
#include <switch.h>
|
|
|
|
#include <stratosphere.hpp>
|
2018-07-27 10:23:53 +01:00
|
|
|
#include "pm_boot2.hpp"
|
|
|
|
#include "pm_registration.hpp"
|
2018-05-01 17:58:19 +01:00
|
|
|
|
2018-07-28 03:34:22 +01:00
|
|
|
static bool IsHexadecimal(const char *str) {
|
|
|
|
while (*str) {
|
2018-07-30 01:27:30 +01:00
|
|
|
if (isxdigit(*str)) {
|
|
|
|
str++;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2018-07-28 03:34:22 +01:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-27 10:37:45 +01:00
|
|
|
static void LaunchTitle(Boot2KnownTitleId title_id, FsStorageId storage_id, u32 launch_flags, u64 *pid) {
|
2018-05-01 17:58:19 +01:00
|
|
|
u64 local_pid;
|
2018-07-27 10:23:53 +01:00
|
|
|
|
2018-07-27 10:37:45 +01:00
|
|
|
Result rc = Registration::LaunchProcessByTidSid(Registration::TidSid{(u64)title_id, storage_id}, launch_flags, &local_pid);
|
2018-05-01 17:58:19 +01:00
|
|
|
switch (rc) {
|
|
|
|
case 0xCE01:
|
|
|
|
/* Out of resource! */
|
|
|
|
/* TODO: Panic(). */
|
|
|
|
break;
|
|
|
|
case 0xDE01:
|
|
|
|
/* Out of memory! */
|
|
|
|
/* TODO: Panic(). */
|
|
|
|
break;
|
|
|
|
case 0xD001:
|
2018-05-01 18:04:39 +01:00
|
|
|
/* Limit Reached! */
|
2018-05-01 17:58:19 +01:00
|
|
|
/* TODO: Panic(). */
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* We don't care about other issues. */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (pid) {
|
|
|
|
*pid = local_pid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-27 10:23:53 +01:00
|
|
|
static bool ShouldForceMaintenanceMode() {
|
2018-05-01 17:58:19 +01:00
|
|
|
/* TODO: Contact set:sys, retrieve boot!force_maintenance, read plus/minus buttons. */
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-07-27 10:37:45 +01:00
|
|
|
static const std::tuple<Boot2KnownTitleId, bool> g_additional_launch_programs[] = {
|
|
|
|
{Boot2KnownTitleId::am, true}, /* am */
|
|
|
|
{Boot2KnownTitleId::nvservices, true}, /* nvservices */
|
|
|
|
{Boot2KnownTitleId::nvnflinger, true}, /* nvnflinger */
|
|
|
|
{Boot2KnownTitleId::vi, true}, /* vi */
|
|
|
|
{Boot2KnownTitleId::ns, true}, /* ns */
|
|
|
|
{Boot2KnownTitleId::lm, true}, /* lm */
|
|
|
|
{Boot2KnownTitleId::ppc, true}, /* ppc */
|
|
|
|
{Boot2KnownTitleId::ptm, true}, /* ptm */
|
|
|
|
{Boot2KnownTitleId::hid, true}, /* hid */
|
|
|
|
{Boot2KnownTitleId::audio, true}, /* audio */
|
|
|
|
{Boot2KnownTitleId::lbl, true}, /* lbl */
|
|
|
|
{Boot2KnownTitleId::wlan, true}, /* wlan */
|
|
|
|
{Boot2KnownTitleId::bluetooth, true}, /* bluetooth */
|
|
|
|
{Boot2KnownTitleId::bsdsockets, true}, /* bsdsockets */
|
|
|
|
{Boot2KnownTitleId::nifm, true}, /* nifm */
|
|
|
|
{Boot2KnownTitleId::ldn, true}, /* ldn */
|
|
|
|
{Boot2KnownTitleId::account, true}, /* account */
|
|
|
|
{Boot2KnownTitleId::friends, false}, /* friends */
|
|
|
|
{Boot2KnownTitleId::nfc, true}, /* nfc */
|
|
|
|
{Boot2KnownTitleId::jpegdec, true}, /* jpegdec */
|
|
|
|
{Boot2KnownTitleId::capsrv, true}, /* capsrv */
|
|
|
|
{Boot2KnownTitleId::ssl, true}, /* ssl */
|
|
|
|
{Boot2KnownTitleId::nim, true}, /* nim */
|
|
|
|
{Boot2KnownTitleId::bcat, false}, /* bcat */
|
|
|
|
{Boot2KnownTitleId::erpt, true}, /* erpt */
|
|
|
|
{Boot2KnownTitleId::es, true}, /* es */
|
|
|
|
{Boot2KnownTitleId::pctl, true}, /* pctl */
|
|
|
|
{Boot2KnownTitleId::btm, true}, /* btm */
|
|
|
|
{Boot2KnownTitleId::eupld, false}, /* eupld */
|
|
|
|
{Boot2KnownTitleId::glue, true}, /* glue */
|
2018-08-02 07:15:18 +01:00
|
|
|
/* {Boot2KnownTitleId::eclct, true}, */ /* eclct */ /* Skip launching error collection in Atmosphere to lessen telemetry. */
|
2018-07-27 10:37:45 +01:00
|
|
|
{Boot2KnownTitleId::npns, false}, /* npns */
|
|
|
|
{Boot2KnownTitleId::fatal, true}, /* fatal */
|
|
|
|
{Boot2KnownTitleId::ro, true}, /* ro */
|
|
|
|
{Boot2KnownTitleId::profiler, true}, /* profiler */
|
|
|
|
{Boot2KnownTitleId::sdb, true}, /* sdb */
|
|
|
|
{Boot2KnownTitleId::migration, true}, /* migration */
|
|
|
|
{Boot2KnownTitleId::grc, true}, /* grc */
|
2018-09-09 07:47:15 +01:00
|
|
|
{Boot2KnownTitleId::olsc, true}, /* olsc */
|
2018-05-01 17:58:19 +01:00
|
|
|
};
|
|
|
|
|
2018-07-27 11:22:06 +01:00
|
|
|
static void MountSdCard() {
|
|
|
|
Handle tmp_hnd = 0;
|
|
|
|
static const char * const required_active_services[] = {"pcv", "gpio", "pinmux", "psc:c"};
|
|
|
|
for (unsigned int i = 0; i < sizeof(required_active_services) / sizeof(required_active_services[0]); i++) {
|
|
|
|
if (R_FAILED(smGetServiceOriginal(&tmp_hnd, smEncodeName(required_active_services[i])))) {
|
|
|
|
/* TODO: Panic */
|
|
|
|
} else {
|
|
|
|
svcCloseHandle(tmp_hnd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fsdevMountSdmc();
|
|
|
|
}
|
|
|
|
|
2018-11-30 10:42:48 +00:00
|
|
|
void EmbeddedBoot2::Main() {
|
|
|
|
/* Wait until fs.mitm has installed itself. We want this to happen as early as possible. */
|
|
|
|
bool fs_mitm_installed = false;
|
|
|
|
|
|
|
|
Result rc = smManagerAmsInitialize();
|
|
|
|
if (R_FAILED(rc)) {
|
|
|
|
std::abort();
|
|
|
|
}
|
|
|
|
while (R_FAILED((rc = smManagerAmsHasMitm(&fs_mitm_installed, "fsp-srv"))) || !fs_mitm_installed) {
|
|
|
|
if (R_FAILED(rc)) {
|
|
|
|
std::abort();
|
|
|
|
}
|
|
|
|
svcSleepThread(1000ull);
|
|
|
|
}
|
|
|
|
smManagerAmsExit();
|
|
|
|
|
2018-07-27 11:22:06 +01:00
|
|
|
/* psc, bus, pcv is the minimal set of required titles to get SD card. */
|
|
|
|
/* bus depends on pcie, and pcv depends on settings. */
|
2018-05-01 17:58:19 +01:00
|
|
|
/* Launch psc. */
|
2018-07-27 10:37:45 +01:00
|
|
|
LaunchTitle(Boot2KnownTitleId::psc, FsStorageId_NandSystem, 0, NULL);
|
2018-05-01 17:58:19 +01:00
|
|
|
/* Launch pcie. */
|
2018-07-27 10:37:45 +01:00
|
|
|
LaunchTitle(Boot2KnownTitleId::pcie, FsStorageId_NandSystem, 0, NULL);
|
2018-07-27 10:23:53 +01:00
|
|
|
/* Launch bus. */
|
2018-07-27 10:37:45 +01:00
|
|
|
LaunchTitle(Boot2KnownTitleId::bus, FsStorageId_NandSystem, 0, NULL);
|
2018-07-27 11:22:06 +01:00
|
|
|
/* Launch settings. */
|
|
|
|
LaunchTitle(Boot2KnownTitleId::settings, FsStorageId_NandSystem, 0, NULL);
|
2018-07-27 10:23:53 +01:00
|
|
|
/* Launch pcv. */
|
2018-07-27 10:37:45 +01:00
|
|
|
LaunchTitle(Boot2KnownTitleId::pcv, FsStorageId_NandSystem, 0, NULL);
|
2018-05-01 17:58:19 +01:00
|
|
|
|
2018-07-27 11:22:06 +01:00
|
|
|
/* At this point, the SD card can be mounted. */
|
|
|
|
MountSdCard();
|
|
|
|
|
|
|
|
/* Launch usb. */
|
|
|
|
LaunchTitle(Boot2KnownTitleId::usb, FsStorageId_NandSystem, 0, NULL);
|
2018-11-03 18:52:04 +00:00
|
|
|
|
2018-12-06 07:36:12 +00:00
|
|
|
/* Launch Atmosphere tma, using FsStorageId_None to force SD card boot. */
|
|
|
|
LaunchTitle(Boot2KnownTitleId::tma, FsStorageId_None, 0, NULL);
|
|
|
|
|
|
|
|
/* Launch Atmosphere dmnt, using FsStorageId_None to force SD card boot. */
|
|
|
|
LaunchTitle(Boot2KnownTitleId::dmnt, FsStorageId_None, 0, NULL);
|
2018-07-27 11:22:06 +01:00
|
|
|
|
|
|
|
/* Launch default programs. */
|
2018-05-01 17:58:19 +01:00
|
|
|
bool maintenance = ShouldForceMaintenanceMode();
|
2018-05-04 00:24:34 +01:00
|
|
|
for (auto &launch_program : g_additional_launch_programs) {
|
|
|
|
if (!maintenance || std::get<bool>(launch_program)) {
|
2018-07-27 10:37:45 +01:00
|
|
|
LaunchTitle(std::get<Boot2KnownTitleId>(launch_program), FsStorageId_NandSystem, 0, NULL);
|
2018-05-01 17:58:19 +01:00
|
|
|
}
|
|
|
|
}
|
2018-07-27 11:22:06 +01:00
|
|
|
|
|
|
|
/* Allow for user-customizable programs. */
|
2018-07-28 03:34:22 +01:00
|
|
|
DIR *titles_dir = opendir("sdmc:/atmosphere/titles");
|
|
|
|
struct dirent *ent;
|
|
|
|
if (titles_dir != NULL) {
|
|
|
|
while ((ent = readdir(titles_dir)) != NULL) {
|
|
|
|
if (strlen(ent->d_name) == 0x10 && IsHexadecimal(ent->d_name)) {
|
|
|
|
u64 title_id = strtoul(ent->d_name, NULL, 16);
|
|
|
|
char title_path[FS_MAX_PATH] = {0};
|
|
|
|
strcpy(title_path, "sdmc:/atmosphere/titles/");
|
|
|
|
strcat(title_path, ent->d_name);
|
2018-11-15 12:26:40 +00:00
|
|
|
strcat(title_path, "/flags/boot2.flag");
|
2018-07-28 03:34:22 +01:00
|
|
|
FILE *f_flag = fopen(title_path, "rb");
|
|
|
|
if (f_flag != NULL) {
|
|
|
|
fclose(f_flag);
|
|
|
|
LaunchTitle((Boot2KnownTitleId)title_id, FsStorageId_None, 0, NULL);
|
2018-11-15 12:26:40 +00:00
|
|
|
} else {
|
|
|
|
/* TODO: Deprecate this in the future. */
|
|
|
|
memset(title_path, 0, FS_MAX_PATH);
|
|
|
|
strcpy(title_path, "sdmc:/atmosphere/titles/");
|
|
|
|
strcat(title_path, ent->d_name);
|
|
|
|
strcat(title_path, "/boot2.flag");
|
|
|
|
f_flag = fopen(title_path, "rb");
|
|
|
|
if (f_flag != NULL) {
|
|
|
|
fclose(f_flag);
|
|
|
|
LaunchTitle((Boot2KnownTitleId)title_id, FsStorageId_None, 0, NULL);
|
|
|
|
}
|
2018-07-28 03:34:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(titles_dir);
|
2018-07-27 11:22:06 +01:00
|
|
|
}
|
2018-10-30 00:29:35 +00:00
|
|
|
|
2018-07-27 11:22:06 +01:00
|
|
|
/* We no longer need the SD card. */
|
|
|
|
fsdevUnmountAll();
|
2018-05-04 00:24:34 +01:00
|
|
|
}
|