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"
|
2019-01-22 07:31:59 +00:00
|
|
|
#include "pm_boot_mode.hpp"
|
2018-05-01 17:58:19 +01:00
|
|
|
|
2019-03-29 04:36:08 +00:00
|
|
|
static std::vector<u64> g_launched_titles;
|
2018-12-06 07:44:11 +00: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;
|
|
|
|
}
|
|
|
|
|
2019-03-29 04:36:08 +00:00
|
|
|
static bool HasLaunchedTitle(u64 title_id) {
|
2018-12-06 07:44:11 +00:00
|
|
|
return std::find(g_launched_titles.begin(), g_launched_titles.end(), title_id) != g_launched_titles.end();
|
|
|
|
}
|
|
|
|
|
2019-03-29 04:36:08 +00:00
|
|
|
static void SetLaunchedTitle(u64 title_id) {
|
2018-12-06 07:44:11 +00:00
|
|
|
g_launched_titles.push_back(title_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ClearLaunchedTitles() {
|
|
|
|
g_launched_titles.clear();
|
|
|
|
}
|
|
|
|
|
2019-03-29 04:36:08 +00:00
|
|
|
static void LaunchTitle(u64 title_id, FsStorageId storage_id, u32 launch_flags, u64 *pid) {
|
2018-12-06 07:44:11 +00:00
|
|
|
u64 local_pid = 0;
|
2018-07-27 10:23:53 +01:00
|
|
|
|
2019-01-21 06:21:49 +00:00
|
|
|
/* Don't launch a title twice during boot2. */
|
|
|
|
if (HasLaunchedTitle(title_id)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-29 04:36:08 +00:00
|
|
|
Result rc = Registration::LaunchProcessByTidSid(Registration::TidSid{title_id, storage_id}, launch_flags, &local_pid);
|
2018-05-01 17:58:19 +01:00
|
|
|
switch (rc) {
|
2019-03-28 23:57:18 +00:00
|
|
|
case ResultKernelResourceExhausted:
|
2018-05-01 17:58:19 +01:00
|
|
|
/* Out of resource! */
|
2018-12-06 07:44:11 +00:00
|
|
|
std::abort();
|
2018-05-01 17:58:19 +01:00
|
|
|
break;
|
2019-03-28 23:57:18 +00:00
|
|
|
case ResultKernelOutOfMemory:
|
2018-05-01 17:58:19 +01:00
|
|
|
/* Out of memory! */
|
2018-12-06 07:44:11 +00:00
|
|
|
std::abort();
|
2018-05-01 17:58:19 +01:00
|
|
|
break;
|
2019-03-28 23:57:18 +00:00
|
|
|
case ResultKernelLimitReached:
|
2018-05-01 18:04:39 +01:00
|
|
|
/* Limit Reached! */
|
2018-12-06 07:44:11 +00:00
|
|
|
std::abort();
|
2018-05-01 17:58:19 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* We don't care about other issues. */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (pid) {
|
|
|
|
*pid = local_pid;
|
|
|
|
}
|
2019-01-21 06:21:49 +00:00
|
|
|
|
|
|
|
if (R_SUCCEEDED(rc)) {
|
|
|
|
SetLaunchedTitle(title_id);
|
|
|
|
}
|
2018-05-01 17:58:19 +01:00
|
|
|
}
|
|
|
|
|
2019-01-22 07:31:59 +00:00
|
|
|
static bool GetGpioPadLow(GpioPadName pad) {
|
|
|
|
GpioPadSession button;
|
|
|
|
if (R_FAILED(gpioOpenSession(&button, pad))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Ensure we close even on early return. */
|
|
|
|
ON_SCOPE_EXIT { gpioPadClose(&button); };
|
|
|
|
|
|
|
|
/* Set direction input. */
|
|
|
|
gpioPadSetDirection(&button, GpioDirection_Input);
|
|
|
|
|
|
|
|
GpioValue val;
|
|
|
|
return R_SUCCEEDED(gpioPadGetValue(&button, &val)) && val == GpioValue_Low;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool IsMaintenanceMode() {
|
|
|
|
/* Contact set:sys, retrieve boot!force_maintenance. */
|
|
|
|
if (R_SUCCEEDED(setsysInitialize())) {
|
|
|
|
ON_SCOPE_EXIT { setsysExit(); };
|
|
|
|
|
|
|
|
u8 force_maintenance = 1;
|
|
|
|
setsysGetSettingsItemValue("boot", "force_maintenance", &force_maintenance, sizeof(force_maintenance));
|
|
|
|
if (force_maintenance != 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Contact GPIO, read plus/minus buttons. */
|
|
|
|
if (R_SUCCEEDED(gpioInitialize())) {
|
|
|
|
ON_SCOPE_EXIT { gpioExit(); };
|
|
|
|
|
|
|
|
return GetGpioPadLow(GpioPadName_ButtonVolUp) && GetGpioPadLow(GpioPadName_ButtonVolDown);
|
|
|
|
}
|
|
|
|
|
2018-05-01 17:58:19 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-03-29 04:36:08 +00:00
|
|
|
static const std::tuple<u64, bool> g_additional_launch_programs[] = {
|
|
|
|
{TitleId_Am, true}, /* am */
|
|
|
|
{TitleId_NvServices, true}, /* nvservices */
|
|
|
|
{TitleId_NvnFlinger, true}, /* nvnflinger */
|
|
|
|
{TitleId_Vi, true}, /* vi */
|
|
|
|
{TitleId_Ns, true}, /* ns */
|
|
|
|
{TitleId_LogManager, true}, /* lm */
|
|
|
|
{TitleId_Ppc, true}, /* ppc */
|
|
|
|
{TitleId_Ptm, true}, /* ptm */
|
|
|
|
{TitleId_Hid, true}, /* hid */
|
|
|
|
{TitleId_Audio, true}, /* audio */
|
|
|
|
{TitleId_Lbl, true}, /* lbl */
|
|
|
|
{TitleId_Wlan, true}, /* wlan */
|
|
|
|
{TitleId_Bluetooth, true}, /* bluetooth */
|
|
|
|
{TitleId_BsdSockets, true}, /* bsdsockets */
|
|
|
|
{TitleId_Nifm, true}, /* nifm */
|
|
|
|
{TitleId_Ldn, true}, /* ldn */
|
|
|
|
{TitleId_Account, true}, /* account */
|
|
|
|
{TitleId_Friends, false}, /* friends */
|
|
|
|
{TitleId_Nfc, true}, /* nfc */
|
|
|
|
{TitleId_JpegDec, true}, /* jpegdec */
|
|
|
|
{TitleId_CapSrv, true}, /* capsrv */
|
|
|
|
{TitleId_Ssl, true}, /* ssl */
|
|
|
|
{TitleId_Nim, true}, /* nim */
|
|
|
|
{TitleId_Bcat, false}, /* bcat */
|
|
|
|
{TitleId_Erpt, true}, /* erpt */
|
|
|
|
{TitleId_Es, true}, /* es */
|
|
|
|
{TitleId_Pctl, true}, /* pctl */
|
|
|
|
{TitleId_Btm, true}, /* btm */
|
|
|
|
{TitleId_Eupld, false}, /* eupld */
|
|
|
|
{TitleId_Glue, true}, /* glue */
|
|
|
|
/* {TitleId_Eclct, true}, */ /* eclct */ /* Skip launching error collection in Atmosphere to lessen telemetry. */
|
|
|
|
{TitleId_Npns, false}, /* npns */
|
|
|
|
{TitleId_Fatal, true}, /* fatal */
|
|
|
|
{TitleId_Ro, true}, /* ro */
|
|
|
|
{TitleId_Profiler, true}, /* profiler */
|
|
|
|
{TitleId_Sdb, true}, /* sdb */
|
|
|
|
{TitleId_Migration, true}, /* migration */
|
|
|
|
{TitleId_Grc, true}, /* grc */
|
|
|
|
{TitleId_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();
|
|
|
|
}
|
|
|
|
|
2019-01-21 06:21:49 +00:00
|
|
|
static void WaitForMitm(const char *service) {
|
|
|
|
bool mitm_installed = false;
|
2018-11-30 10:42:48 +00:00
|
|
|
|
|
|
|
Result rc = smManagerAmsInitialize();
|
|
|
|
if (R_FAILED(rc)) {
|
|
|
|
std::abort();
|
|
|
|
}
|
2019-01-21 06:21:49 +00:00
|
|
|
while (R_FAILED((rc = smManagerAmsHasMitm(&mitm_installed, service))) || !mitm_installed) {
|
2018-11-30 10:42:48 +00:00
|
|
|
if (R_FAILED(rc)) {
|
|
|
|
std::abort();
|
|
|
|
}
|
2019-01-21 06:21:49 +00:00
|
|
|
svcSleepThread(1000000ull);
|
2018-11-30 10:42:48 +00:00
|
|
|
}
|
|
|
|
smManagerAmsExit();
|
2019-01-21 01:00:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void EmbeddedBoot2::Main() {
|
|
|
|
/* Wait until fs.mitm has installed itself. We want this to happen as early as possible. */
|
2019-01-21 06:21:49 +00:00
|
|
|
WaitForMitm("fsp-srv");
|
|
|
|
|
|
|
|
/* Clear titles. */
|
|
|
|
ClearLaunchedTitles();
|
2018-11-30 10:42:48 +00:00
|
|
|
|
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. */
|
2019-03-29 04:36:08 +00:00
|
|
|
LaunchTitle(TitleId_Psc, FsStorageId_NandSystem, 0, NULL);
|
2018-05-01 17:58:19 +01:00
|
|
|
/* Launch pcie. */
|
2019-03-29 04:36:08 +00:00
|
|
|
LaunchTitle(TitleId_Pcie, FsStorageId_NandSystem, 0, NULL);
|
2018-07-27 10:23:53 +01:00
|
|
|
/* Launch bus. */
|
2019-03-29 04:36:08 +00:00
|
|
|
LaunchTitle(TitleId_Bus, FsStorageId_NandSystem, 0, NULL);
|
2018-07-27 11:22:06 +01:00
|
|
|
/* Launch settings. */
|
2019-03-29 04:36:08 +00:00
|
|
|
LaunchTitle(TitleId_Settings, FsStorageId_NandSystem, 0, NULL);
|
2018-07-27 10:23:53 +01:00
|
|
|
/* Launch pcv. */
|
2019-03-29 04:36:08 +00:00
|
|
|
LaunchTitle(TitleId_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();
|
|
|
|
|
2019-01-22 07:31:59 +00:00
|
|
|
/* Find out whether we are maintenance mode. */
|
|
|
|
bool maintenance = IsMaintenanceMode();
|
|
|
|
if (maintenance) {
|
|
|
|
BootModeService::SetMaintenanceBootForEmbeddedBoot2();
|
|
|
|
}
|
2019-01-29 10:29:29 +00:00
|
|
|
|
2019-02-05 05:17:05 +00:00
|
|
|
/* Wait for other atmosphere mitm modules to initialize. */
|
2019-01-21 06:21:49 +00:00
|
|
|
WaitForMitm("set:sys");
|
2019-02-05 05:17:05 +00:00
|
|
|
if (GetRuntimeFirmwareVersion() >= FirmwareVersion_200) {
|
|
|
|
WaitForMitm("bpc");
|
|
|
|
} else {
|
|
|
|
WaitForMitm("bpc:c");
|
|
|
|
}
|
2019-01-21 06:21:49 +00:00
|
|
|
|
2018-07-27 11:22:06 +01:00
|
|
|
/* Launch usb. */
|
2019-03-29 04:36:08 +00:00
|
|
|
LaunchTitle(TitleId_Usb, FsStorageId_NandSystem, 0, NULL);
|
2019-03-04 07:53:53 +00:00
|
|
|
|
|
|
|
/* Launch tma. */
|
2019-03-29 04:36:08 +00:00
|
|
|
LaunchTitle(TitleId_Tma, FsStorageId_NandSystem, 0, NULL);
|
2019-02-27 09:44:47 +00:00
|
|
|
|
2018-12-06 07:36:12 +00:00
|
|
|
/* Launch Atmosphere dmnt, using FsStorageId_None to force SD card boot. */
|
2019-03-29 04:36:08 +00:00
|
|
|
LaunchTitle(TitleId_Dmnt, FsStorageId_None, 0, NULL);
|
2018-07-27 11:22:06 +01:00
|
|
|
|
|
|
|
/* Launch default programs. */
|
2018-05-04 00:24:34 +01:00
|
|
|
for (auto &launch_program : g_additional_launch_programs) {
|
|
|
|
if (!maintenance || std::get<bool>(launch_program)) {
|
2019-03-29 04:36:08 +00:00
|
|
|
LaunchTitle(std::get<u64>(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)) {
|
2019-03-29 04:36:08 +00:00
|
|
|
u64 title_id = (u64)strtoul(ent->d_name, NULL, 16);
|
2018-12-06 07:44:11 +00:00
|
|
|
if (HasLaunchedTitle(title_id)) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-07-28 03:34:22 +01:00
|
|
|
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);
|
2018-12-06 07:44:11 +00:00
|
|
|
LaunchTitle(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);
|
2018-12-06 07:44:11 +00:00
|
|
|
LaunchTitle(title_id, FsStorageId_None, 0, NULL);
|
2018-11-15 12:26:40 +00:00
|
|
|
}
|
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();
|
2019-01-21 06:21:49 +00:00
|
|
|
|
2019-03-26 00:12:19 +00:00
|
|
|
/* Free the memory used to track what boot2 launches. */
|
2019-01-21 06:21:49 +00:00
|
|
|
ClearLaunchedTitles();
|
2018-05-04 00:24:34 +01:00
|
|
|
}
|