Implement the NCM sysmodule (closes #91)

* Implement NCM

* Modernize ncm_main

* Remove unnecessary smExit

* Give access to svcCallSecureMonitor

* Stack size bump

* Fix incorrect setup for NandUser's content storage entry

* Fix a potential data abort when flushing the placeholder accessor cache

* Fix HasFile and HasDirectory

* Use r+b, not w+b

* Misc fixes

* errno begone

* Fixed more stdio error handling

* More main fixes

* Various command improvements

* Make dispatch tables great again

* Fix logic inversion

* Fixed content path generation

* Bump heap size, fix CleanupAllPlaceHolder

* Various fixes. Note: This contains debug stuff which will be removed later. I was getting tired of having to cherrypick tiny changes

* Fixed placeholder/content deletion

* Fixed incorrect content manager destruction

* Prevent automatic placeholder creation on open

* Fixed List implementation. Also lots of debug logging.

* Removed debug code

* Added a scope guard for WritePlaceHolder

* Manually prevent placeholder/content appending

* Revert "Removed debug code"

This reverts commit d6ff261fcc.

* Always cache placeholder file. Switch to ftell for preventing appending

* Universally use EnsureEnabled

* Abstract away file writing logic

* Misc cleanup

* Refactor placeholder cacheing

* Remove debug code (again)

* Revert "Remove debug code (again)"

This reverts commit 168447d80e.

* Misc changes

* Fixed file modes

* Fixed ContentId/PlaceHolderId alignment

* Improved type safety

* Fixed reinitialization

* Fixed doubleup on path creation

* Remove debug code

* Fixed 1.0.0 booting

* Correct amount of add on content

* Correct main thread stack size

* lr: Introducing registered data

* Reorder stratosphere Makefile

* Move results to libstrat

* lr: Cleanup lr_redirection

* lr: lr_manager tweaks

* lr: Imrpoved path handling and adjust ResolveAddOnContentPath order

* lr: Organise types

* Add eof newlines

* lr: Eliminate unnecessary vars

* lr: Unnecessary vars 2 electric boogaloo

* lr: Various helpers

* lr: RegisteredLocationResolver helpers

* ncm: Move ncm_types to libstrat

* ncm: Misc cleanup

* Implement NCM

* Modernize ncm_main

* Remove unnecessary smExit

* Give access to svcCallSecureMonitor

* Stack size bump

* Fix incorrect setup for NandUser's content storage entry

* Fix a potential data abort when flushing the placeholder accessor cache

* Fix HasFile and HasDirectory

* Use r+b, not w+b

* Misc fixes

* errno begone

* Fixed more stdio error handling

* More main fixes

* Various command improvements

* Make dispatch tables great again

* Fix logic inversion

* Fixed content path generation

* Bump heap size, fix CleanupAllPlaceHolder

* Various fixes. Note: This contains debug stuff which will be removed later. I was getting tired of having to cherrypick tiny changes

* Fixed placeholder/content deletion

* Fixed incorrect content manager destruction

* Prevent automatic placeholder creation on open

* Fixed List implementation. Also lots of debug logging.

* Removed debug code

* Added a scope guard for WritePlaceHolder

* Manually prevent placeholder/content appending

* Revert "Removed debug code"

This reverts commit d6ff261fcc.

* Always cache placeholder file. Switch to ftell for preventing appending

* Universally use EnsureEnabled

* Abstract away file writing logic

* Misc cleanup

* Refactor placeholder cacheing

* Remove debug code (again)

* Revert "Remove debug code (again)"

This reverts commit 168447d80e.

* Misc changes

* Fixed file modes

* Fixed ContentId/PlaceHolderId alignment

* Improved type safety

* Fixed reinitialization

* Fixed doubleup on path creation

* Remove debug code

* Fixed 1.0.0 booting

* Correct amount of add on content

* Correct main thread stack size

* lr: Introducing registered data

* Reorder stratosphere Makefile

* Move results to libstrat

* lr: Cleanup lr_redirection

* lr: lr_manager tweaks

* lr: Imrpoved path handling and adjust ResolveAddOnContentPath order

* lr: Organise types

* Add eof newlines

* lr: Eliminate unnecessary vars

* lr: Unnecessary vars 2 electric boogaloo

* lr: Various helpers

* lr: RegisteredLocationResolver helpers

* ncm: Move ncm_types to libstrat

* ncm: Misc cleanup

* Updated AddOnContentLocationResolver and RegisteredLocationResolver to 9.0.0

* Finished updating lr to 9.0.0

* Updated NCM to 9.0.0

* Fix libstrat includes

* Fixed application launching

* title_id_2 -> owner_tid

* Updated to new-ipc

* Change to using pure virtuals

* Title Id -> Program Id

* Fixed compilation against master

* std::scoped_lock<> -> std::scoped_lock

* Adopted R_UNLESS and R_CONVERT

* Prefix namespace to Results

* Adopt std::numeric_limits

* Fixed incorrect error handling in ReadFile

* Adopted AMS_ABORT_UNLESS

* Adopt util::GenerateUuid()

* Syntax improvements

* ncm_types: Address review

* Address more review comments

* Updated copyrights

* Address more feedback

* More feedback addressed

* More changes

* Move dispatch tables out of interface files

* Addressed remaining comments

* lr: move into libstratosphere

* ncm: Fix logic inversion

* lr: Add comments

* lr: Remove whitespace

* ncm: Start addressing feedback

* ncm: Cleanup InitializeContentManager

* lr: support client-side usage

* lr_service -> lr_api

* ncm: Begin refactoring content manager

* ncm: More content manager improvements

* ncm: Content manager mount improvements

* ldr: use lr bindings

* lr bindings usage: minor fixes

* ncm/lr: Pointer placement

* ncm: placeholder accessor cleanup

* ncm: minor fixes

* ncm: refactor rights cache

* ncm: content meta database cleanup

* ncm: move content meta database impl out of interface file

* ncm: Use const ContentMetaKey &

* ncm: fix other non-const ContentMetaKey references

* ncm: content meta database cleanup

* ncm: content storage fixes for 2.0.0

* ncm: add missing end of file newlines

* ncm: implement ContentMetaReader

* ncm: client-side api

* ncm: trim trailing spaces

* ncm: FS_MAX_PATH-1 -> fs::EntryNameLengthMax

* ncm: Use PathString and Path

* fs: implement accessor wrappers for ncm

* fs: implement user fs wrappers

* fs: add MountSdCard

* ncm: move to content manager impl

* ncm: fix up main

* kvdb: use fs::

* fs: Add wrappers needed for ncm

* ncm: use fs bindings, other refactoring

* ncm: minor fixes

* fsa: fix ReadFile without size output

* fs: add substorage, rom path tool

* ncm: fix dangling fsdev usage

* fs: fix bug in Commit

* fs: fixed incorrect mode check

* fs: implement Mount(System)Data

* ncm: don't delete hos

* results: add R_SUCCEED_IF

* ams-except-ncm: use R_SUCCEED_IF

* ncm: added comments

* ncm: fix api definitions

* ncm: use R_SUCCEED_IF

* pm: think of the savings

* ncm: employ kernel strats

* ncm: Nintendo has 5 MiB of heap. Give ourselves 4 to be safe, pending analysis

* ncm: refactor IDs, split types header into many headers

* ams.mitm: use fs bindings instead of stdio

* fs: SystemData uses SystemDataId

* ncm: improve meta-db accuracy

* ncm: inline getlatestkey

* fs: improve UnsupportedOperation results

* fs: modernize mount utils

* ams: misc fixes for merge-errors

* fs: improve unsupportedoperation results

* git subrepo pull emummc

subrepo:
  subdir:   "emummc"
  merged:   "d12dd546"
upstream:
  origin:   "https://github.com/m4xw/emuMMC"
  branch:   "develop"
  commit:   "d12dd546"
git-subrepo:
  version:  "0.4.1"
  origin:   "???"
  commit:   "???"

* util: add boundedmap

* ncm: minor style fixes

* ncm: don't unmount if mounting fails

* lr: bug fixes

* ncm: implement ncm.for-initialize + ncm.for-safemode

* lr: ncm::ProgramId::Invalid -> ncm::InvalidProgramId

* ncm: fix open directory mode on 1.0.0

* ncm: fix fs use, implement more of < 4.0.0 for-initialize/safemode

* ncm: implement packagedcontent -> content for building metadb

* ncm: fix save data flag management

* ncm: address some review suggestions (thanks @leoetlino!)

* updater: use fs bindings

* fs: implement MountCode

* fs: prefer make_unique to operator new

* ncm: implement remaining ContentMetaDatabaseBuilder functionality

Co-authored-by: Michael Scire <SciresM@gmail.com>
This commit is contained in:
Adubbz 2020-03-08 19:06:23 +11:00 committed by GitHub
parent f9403201f0
commit c7026b9094
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
254 changed files with 16876 additions and 1274 deletions

View file

@ -13,7 +13,6 @@
* 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 <dirent.h>
#include <stratosphere.hpp>
namespace ams::boot2 {
@ -24,99 +23,99 @@ namespace ams::boot2 {
/* psc, bus, pcv is the minimal set of required programs to get SD card. */
/* bus depends on pcie, and pcv depends on settings. */
constexpr ncm::ProgramId PreSdCardLaunchPrograms[] = {
ncm::ProgramId::Psc, /* psc */
ncm::ProgramId::Pcie, /* pcie */
ncm::ProgramId::Bus, /* bus */
ncm::ProgramId::Settings, /* settings */
ncm::ProgramId::Pcv, /* pcv */
ncm::ProgramId::Usb, /* usb */
constexpr ncm::SystemProgramId PreSdCardLaunchPrograms[] = {
ncm::SystemProgramId::Psc, /* psc */
ncm::SystemProgramId::Pcie, /* pcie */
ncm::SystemProgramId::Bus, /* bus */
ncm::SystemProgramId::Settings, /* settings */
ncm::SystemProgramId::Pcv, /* pcv */
ncm::SystemProgramId::Usb, /* usb */
};
constexpr size_t NumPreSdCardLaunchPrograms = util::size(PreSdCardLaunchPrograms);
constexpr ncm::ProgramId AdditionalLaunchPrograms[] = {
ncm::ProgramId::Tma, /* tma */
ncm::ProgramId::Am, /* am */
ncm::ProgramId::NvServices, /* nvservices */
ncm::ProgramId::NvnFlinger, /* nvnflinger */
ncm::ProgramId::Vi, /* vi */
ncm::ProgramId::Ns, /* ns */
ncm::ProgramId::LogManager, /* lm */
ncm::ProgramId::Ppc, /* ppc */
ncm::ProgramId::Ptm, /* ptm */
ncm::ProgramId::Hid, /* hid */
ncm::ProgramId::Audio, /* audio */
ncm::ProgramId::Lbl, /* lbl */
ncm::ProgramId::Wlan, /* wlan */
ncm::ProgramId::Bluetooth, /* bluetooth */
ncm::ProgramId::BsdSockets, /* bsdsockets */
ncm::ProgramId::Nifm, /* nifm */
ncm::ProgramId::Ldn, /* ldn */
ncm::ProgramId::Account, /* account */
ncm::ProgramId::Friends, /* friends */
ncm::ProgramId::Nfc, /* nfc */
ncm::ProgramId::JpegDec, /* jpegdec */
ncm::ProgramId::CapSrv, /* capsrv */
ncm::ProgramId::Ssl, /* ssl */
ncm::ProgramId::Nim, /* nim */
ncm::ProgramId::Bcat, /* bcat */
ncm::ProgramId::Erpt, /* erpt */
ncm::ProgramId::Es, /* es */
ncm::ProgramId::Pctl, /* pctl */
ncm::ProgramId::Btm, /* btm */
ncm::ProgramId::Eupld, /* eupld */
ncm::ProgramId::Glue, /* glue */
/* ncm::ProgramId::Eclct, */ /* eclct */ /* Skip launching error collection in Atmosphere to lessen telemetry. */
ncm::ProgramId::Npns, /* npns */
ncm::ProgramId::Fatal, /* fatal */
ncm::ProgramId::Ro, /* ro */
ncm::ProgramId::Profiler, /* profiler */
ncm::ProgramId::Sdb, /* sdb */
ncm::ProgramId::Migration, /* migration */
ncm::ProgramId::Grc, /* grc */
ncm::ProgramId::Olsc, /* olsc */
ncm::ProgramId::Ngct, /* ngct */
constexpr ncm::SystemProgramId AdditionalLaunchPrograms[] = {
ncm::SystemProgramId::Tma, /* tma */
ncm::SystemProgramId::Am, /* am */
ncm::SystemProgramId::NvServices, /* nvservices */
ncm::SystemProgramId::NvnFlinger, /* nvnflinger */
ncm::SystemProgramId::Vi, /* vi */
ncm::SystemProgramId::Ns, /* ns */
ncm::SystemProgramId::LogManager, /* lm */
ncm::SystemProgramId::Ppc, /* ppc */
ncm::SystemProgramId::Ptm, /* ptm */
ncm::SystemProgramId::Hid, /* hid */
ncm::SystemProgramId::Audio, /* audio */
ncm::SystemProgramId::Lbl, /* lbl */
ncm::SystemProgramId::Wlan, /* wlan */
ncm::SystemProgramId::Bluetooth, /* bluetooth */
ncm::SystemProgramId::BsdSockets, /* bsdsockets */
ncm::SystemProgramId::Nifm, /* nifm */
ncm::SystemProgramId::Ldn, /* ldn */
ncm::SystemProgramId::Account, /* account */
ncm::SystemProgramId::Friends, /* friends */
ncm::SystemProgramId::Nfc, /* nfc */
ncm::SystemProgramId::JpegDec, /* jpegdec */
ncm::SystemProgramId::CapSrv, /* capsrv */
ncm::SystemProgramId::Ssl, /* ssl */
ncm::SystemProgramId::Nim, /* nim */
ncm::SystemProgramId::Bcat, /* bcat */
ncm::SystemProgramId::Erpt, /* erpt */
ncm::SystemProgramId::Es, /* es */
ncm::SystemProgramId::Pctl, /* pctl */
ncm::SystemProgramId::Btm, /* btm */
ncm::SystemProgramId::Eupld, /* eupld */
ncm::SystemProgramId::Glue, /* glue */
/* ncm::SystemProgramId::Eclct, */ /* eclct */ /* Skip launching error collection in Atmosphere to lessen telemetry. */
ncm::SystemProgramId::Npns, /* npns */
ncm::SystemProgramId::Fatal, /* fatal */
ncm::SystemProgramId::Ro, /* ro */
ncm::SystemProgramId::Profiler, /* profiler */
ncm::SystemProgramId::Sdb, /* sdb */
ncm::SystemProgramId::Migration, /* migration */
ncm::SystemProgramId::Grc, /* grc */
ncm::SystemProgramId::Olsc, /* olsc */
ncm::SystemProgramId::Ngct, /* ngct */
};
constexpr size_t NumAdditionalLaunchPrograms = util::size(AdditionalLaunchPrograms);
constexpr ncm::ProgramId AdditionalMaintenanceLaunchPrograms[] = {
ncm::ProgramId::Tma, /* tma */
ncm::ProgramId::Am, /* am */
ncm::ProgramId::NvServices, /* nvservices */
ncm::ProgramId::NvnFlinger, /* nvnflinger */
ncm::ProgramId::Vi, /* vi */
ncm::ProgramId::Ns, /* ns */
ncm::ProgramId::LogManager, /* lm */
ncm::ProgramId::Ppc, /* ppc */
ncm::ProgramId::Ptm, /* ptm */
ncm::ProgramId::Hid, /* hid */
ncm::ProgramId::Audio, /* audio */
ncm::ProgramId::Lbl, /* lbl */
ncm::ProgramId::Wlan, /* wlan */
ncm::ProgramId::Bluetooth, /* bluetooth */
ncm::ProgramId::BsdSockets, /* bsdsockets */
ncm::ProgramId::Nifm, /* nifm */
ncm::ProgramId::Ldn, /* ldn */
ncm::ProgramId::Account, /* account */
ncm::ProgramId::Nfc, /* nfc */
ncm::ProgramId::JpegDec, /* jpegdec */
ncm::ProgramId::CapSrv, /* capsrv */
ncm::ProgramId::Ssl, /* ssl */
ncm::ProgramId::Nim, /* nim */
ncm::ProgramId::Erpt, /* erpt */
ncm::ProgramId::Es, /* es */
ncm::ProgramId::Pctl, /* pctl */
ncm::ProgramId::Btm, /* btm */
ncm::ProgramId::Glue, /* glue */
/* ncm::ProgramId::Eclct, */ /* eclct */ /* Skip launching error collection in Atmosphere to lessen telemetry. */
ncm::ProgramId::Fatal, /* fatal */
ncm::ProgramId::Ro, /* ro */
ncm::ProgramId::Profiler, /* profiler */
ncm::ProgramId::Sdb, /* sdb */
ncm::ProgramId::Migration, /* migration */
ncm::ProgramId::Grc, /* grc */
ncm::ProgramId::Olsc, /* olsc */
ncm::ProgramId::Ngct, /* ngct */
constexpr ncm::SystemProgramId AdditionalMaintenanceLaunchPrograms[] = {
ncm::SystemProgramId::Tma, /* tma */
ncm::SystemProgramId::Am, /* am */
ncm::SystemProgramId::NvServices, /* nvservices */
ncm::SystemProgramId::NvnFlinger, /* nvnflinger */
ncm::SystemProgramId::Vi, /* vi */
ncm::SystemProgramId::Ns, /* ns */
ncm::SystemProgramId::LogManager, /* lm */
ncm::SystemProgramId::Ppc, /* ppc */
ncm::SystemProgramId::Ptm, /* ptm */
ncm::SystemProgramId::Hid, /* hid */
ncm::SystemProgramId::Audio, /* audio */
ncm::SystemProgramId::Lbl, /* lbl */
ncm::SystemProgramId::Wlan, /* wlan */
ncm::SystemProgramId::Bluetooth, /* bluetooth */
ncm::SystemProgramId::BsdSockets, /* bsdsockets */
ncm::SystemProgramId::Nifm, /* nifm */
ncm::SystemProgramId::Ldn, /* ldn */
ncm::SystemProgramId::Account, /* account */
ncm::SystemProgramId::Nfc, /* nfc */
ncm::SystemProgramId::JpegDec, /* jpegdec */
ncm::SystemProgramId::CapSrv, /* capsrv */
ncm::SystemProgramId::Ssl, /* ssl */
ncm::SystemProgramId::Nim, /* nim */
ncm::SystemProgramId::Erpt, /* erpt */
ncm::SystemProgramId::Es, /* es */
ncm::SystemProgramId::Pctl, /* pctl */
ncm::SystemProgramId::Btm, /* btm */
ncm::SystemProgramId::Glue, /* glue */
/* ncm::SystemProgramId::Eclct, */ /* eclct */ /* Skip launching error collection in Atmosphere to lessen telemetry. */
ncm::SystemProgramId::Fatal, /* fatal */
ncm::SystemProgramId::Ro, /* ro */
ncm::SystemProgramId::Profiler, /* profiler */
ncm::SystemProgramId::Sdb, /* sdb */
ncm::SystemProgramId::Migration, /* migration */
ncm::SystemProgramId::Grc, /* grc */
ncm::SystemProgramId::Olsc, /* olsc */
ncm::SystemProgramId::Ngct, /* ngct */
};
constexpr size_t NumAdditionalMaintenanceLaunchPrograms = util::size(AdditionalMaintenanceLaunchPrograms);
@ -150,7 +149,7 @@ namespace ams::boot2 {
}
}
void LaunchList(const ncm::ProgramId *launch_list, size_t num_entries) {
void LaunchList(const ncm::SystemProgramId *launch_list, size_t num_entries) {
for (size_t i = 0; i < num_entries; i++) {
LaunchProgram(nullptr, ncm::ProgramLocation::Make(launch_list[i], ncm::StorageId::BuiltInSystem), 0);
}
@ -191,19 +190,20 @@ namespace ams::boot2 {
template<typename F>
void IterateOverFlaggedProgramsOnSdCard(F f) {
/* Validate that the contents directory exists. */
DIR *contents_dir = opendir("sdmc:/atmosphere/contents");
if (contents_dir == nullptr) {
fs::DirectoryHandle contents_dir;
if (R_FAILED(fs::OpenDirectory(&contents_dir, "sdmc:/atmosphere/contents", fs::OpenDirectoryMode_Directory))) {
return;
}
ON_SCOPE_EXIT { closedir(contents_dir); };
ON_SCOPE_EXIT { fs::CloseDirectory(contents_dir); };
/* Iterate over entries in the contents directory */
struct dirent *ent;
while ((ent = readdir(contents_dir)) != nullptr) {
fs::DirectoryEntry entry;
s64 count;
while (R_SUCCEEDED(fs::ReadDirectory(&count, &entry, contents_dir, 1)) && count == 1) {
/* Check that the subdirectory can be converted to a program id. */
if (std::strlen(ent->d_name) == 2 * sizeof(ncm::ProgramId) && IsHexadecimal(ent->d_name)) {
if (std::strlen(entry.name) == 2 * sizeof(ncm::ProgramId) && IsHexadecimal(entry.name)) {
/* Check if we've already launched the program. */
ncm::ProgramId program_id{std::strtoul(ent->d_name, nullptr, 16)};
ncm::ProgramId program_id{std::strtoul(entry.name, nullptr, 16)};
/* Check if the program is flagged. */
if (!cfg::HasContentSpecificFlag(program_id, "boot2")) {
@ -224,14 +224,16 @@ namespace ams::boot2 {
/* Read the mitm list off the SD card. */
{
char path[FS_MAX_PATH];
std::snprintf(mitm_list, sizeof(mitm_list), "sdmc:/atmosphere/contents/%016lx/mitm.lst", static_cast<u64>(program_id));
FILE *f = fopen(path, "rb");
if (f == nullptr) {
char path[fs::EntryNameLengthMax];
std::snprintf(path, sizeof(path), "sdmc:/atmosphere/contents/%016lx/mitm.lst", static_cast<u64>(program_id));
fs::FileHandle f;
if (R_FAILED(fs::OpenFile(&f, path, fs::OpenMode_Read))) {
return;
}
mitm_list_size = static_cast<size_t>(fread(mitm_list, 1, sizeof(mitm_list), f));
fclose(f);
ON_SCOPE_EXIT { fs::CloseFile(f); };
R_ABORT_UNLESS(fs::ReadFile(&mitm_list_size, f, 0, mitm_list, sizeof(mitm_list)));
}
/* Validate read size. */
@ -313,7 +315,7 @@ namespace ams::boot2 {
}
/* Launch Atmosphere boot2, using NcmStorageId_None to force SD card boot. */
LaunchProgram(nullptr, ncm::ProgramLocation::Make(ncm::ProgramId::Boot2, ncm::StorageId::None), 0);
LaunchProgram(nullptr, ncm::ProgramLocation::Make(ncm::SystemProgramId::Boot2, ncm::StorageId::None), 0);
}
void LaunchPostSdCardBootPrograms() {
@ -326,7 +328,7 @@ namespace ams::boot2 {
}
/* Launch Atmosphere dmnt, using NcmStorageId_None to force SD card boot. */
LaunchProgram(nullptr, ncm::ProgramLocation::Make(ncm::ProgramId::Dmnt, ncm::StorageId::None), 0);
LaunchProgram(nullptr, ncm::ProgramLocation::Make(ncm::SystemProgramId::Dmnt, ncm::StorageId::None), 0);
/* Check for and forward declare non-atmosphere mitm modules. */
DetectAndDeclareFutureMitms();
@ -336,7 +338,7 @@ namespace ams::boot2 {
LaunchList(AdditionalMaintenanceLaunchPrograms, NumAdditionalMaintenanceLaunchPrograms);
/* Starting in 7.0.0, npns is launched during maintenance boot. */
if (hos::GetVersion() >= hos::Version_700) {
LaunchProgram(nullptr, ncm::ProgramLocation::Make(ncm::ProgramId::Npns, ncm::StorageId::BuiltInSystem), 0);
LaunchProgram(nullptr, ncm::ProgramLocation::Make(ncm::SystemProgramId::Npns, ncm::StorageId::BuiltInSystem), 0);
}
} else {
LaunchList(AdditionalLaunchPrograms, NumAdditionalLaunchPrograms);