Skip to content

Commit

Permalink
title: init gc title storage by HFS as fallback
Browse files Browse the repository at this point in the history
Makes it possible to generate TitleInfo and TitleApplicationMetadata elements for gamecards that can't be used with ncm (e.g. Kiosk / Quest gamecards under retail, non-Quest units). This is achieved by:

1. Retrieving a Hash FS context for the gamecard's secure partition.
2. Initializing NCA and CNMT contexts for all of the Meta NCAs within the secure partition.
3. Manually generating NcmContentMetaKey and NcmContentInfo elements per each CNMT context.
4. Creating TitleInfo elements using the generated NcmContentMetaKey and NcmContentInfo elements.
5. Initializing NCA and NACP contexts for the base (or update) Control NCAs within the secure partition.
6. Manually generating a NsApplicationControlData element per each NACP context.
7. Creating TitleApplicationMetadata elements using the generated NsApplicationControlData elements.

Afterwards, gamecard title/content enumeration and all other features that rely on it (e.g. NSP, NCA, NCA FS) work as expected. Please note that this process is only carried out if regular title storage initialization fails.

Other changes include:

* title: reorder code.
* title: add TitleGameCardContentMetaContext struct.
* title: rename titleGetInfoFromStorageByTitleId() -> titleGetTitleInfoEntryFromStorageByTitleId().
* title: add titleInitializeGameCardTitleStorageByHashFileSystem().
* title: rename titleGenerateDummySystemMetadataEntry() -> titleGetSystemMetadataEntry().
* title: rename titleRetrieveUserApplicationMetadataByTitleId() -> titleGenerateUserMetadataEntryFromNs().
* title: move ns logic from titleRetrieveUserApplicationMetadataByTitleId() into a new function: titleGetApplicationControlDataFromNs().
* title: add titleGenerateUserMetadataEntryFromControlNca().
* title: add titleGetApplicationControlDataFromControlNca().
* title: add titleInitializeUserMetadataEntryFromControlData().
* title: add titleGenerateTitleInfoEntriesByHashFileSystemForGameCardTitleStorage().
* title: move TitleInfo generation logic from titleGenerateTitleInfoEntriesForTitleStorage() into a new function: titleGenerateTitleInfoEntry().
* title: add titleInitializeTitleInfoApplicationMetadataFromControlNca().
* title: add titleGetGameCardContentMetaContexts().
* title: add titleFreeGameCardContentMetaContexts().
* title: add titleGetContentInfosByGameCardContentMetaContext().
* title: rename _titleGetInfoFromStorageByTitleId() -> _titleGetTitleInfoEntryFromStorageByTitleId().
* title: rename titleSystemTitleMetadataEntrySortFunction() -> titleSystemMetadataSortFunction().
* title: rename titleUserApplicationMetadataEntrySortFunction() -> titleUserMetadataSortFunction().
* title: rename titleInfoEntrySortFunction() -> titleInfoSortFunction().
* title: add titleGameCardContentMetaContextSortFunction().

* codebase: update to reflect the rest of the changes.
  • Loading branch information
DarkMatterCore committed Aug 18, 2024
1 parent 89b211c commit 9c7a57e
Show file tree
Hide file tree
Showing 6 changed files with 1,208 additions and 544 deletions.
2 changes: 1 addition & 1 deletion code_templates/nxdt_rw_poc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1264,7 +1264,7 @@ int main(int argc, char *argv[])
break;
default:
/* Get TitleInfo element on demand. */
title_info = titleGetInfoFromStorageByTitleId(NcmStorageId_BuiltInSystem, app_metadata->title_id);
title_info = titleGetTitleInfoEntryFromStorageByTitleId(NcmStorageId_BuiltInSystem, app_metadata->title_id);
break;
}

Expand Down
4 changes: 2 additions & 2 deletions include/core/title.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ TitleGameCardApplicationMetadata *titleGetGameCardApplicationMetadataEntries(u32
/// Returns a pointer to a dynamically allocated TitleInfo element with a matching storage ID and title ID. Returns NULL if an error occurs.
/// If NcmStorageId_Any is used, the first entry with a matching title ID is returned.
/// Use titleFreeTitleInfo() to free the returned data.
TitleInfo *titleGetInfoFromStorageByTitleId(u8 storage_id, u64 title_id);
TitleInfo *titleGetTitleInfoEntryFromStorageByTitleId(u8 storage_id, u64 title_id);

/// Frees a dynamically allocated TitleInfo element.
void titleFreeTitleInfo(TitleInfo **info);
Expand Down Expand Up @@ -152,7 +152,7 @@ TitleInfo **titleGetOrphanTitles(u32 *out_count);
void titleFreeOrphanTitles(TitleInfo ***orphan_info);

/// Checks if a gamecard status update has been detected by the background gamecard title info thread (e.g. after a new gamecard has been inserted, of after the current one has been taken out).
/// If this function returns true and functions such as titleGetInfoFromStorageByTitleId(), titleGetUserApplicationData() or titleGetInfoFromOrphanTitles() have been previously called:
/// If this function returns true and functions such as titleGetTitleInfoEntryFromStorageByTitleId(), titleGetUserApplicationData() or titleGetInfoFromOrphanTitles() have been previously called:
/// 1. Their returned data must be freed.
/// 2. They must be called again.
bool titleIsGameCardInfoUpdated(void);
Expand Down
2 changes: 1 addition & 1 deletion source/core/bfttf.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ bool bfttfInitialize(void)
TitleInfo *title_info = NULL;

/* Get title info. */
if (!(title_info = titleGetInfoFromStorageByTitleId(NcmStorageId_BuiltInSystem, font_info->title_id)))
if (!(title_info = titleGetTitleInfoEntryFromStorageByTitleId(NcmStorageId_BuiltInSystem, font_info->title_id)))
{
LOG_MSG_ERROR("Failed to get title info for %016lX!", font_info->title_id);
continue;
Expand Down
2 changes: 1 addition & 1 deletion source/core/nxdt_bfsar.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ bool bfsarInitialize(void)
}

/* Get title info. */
if (!(title_info = titleGetInfoFromStorageByTitleId(NcmStorageId_BuiltInSystem, QLAUNCH_TID)))
if (!(title_info = titleGetTitleInfoEntryFromStorageByTitleId(NcmStorageId_BuiltInSystem, QLAUNCH_TID)))
{
LOG_MSG_ERROR("Failed to get title info for qlaunch!");
break;
Expand Down
Loading

0 comments on commit 9c7a57e

Please sign in to comment.