Skip to content

Commit

Permalink
codebase: support HOS 19.0.0.
Browse files Browse the repository at this point in the history
Still missing NACP changes.
  • Loading branch information
DarkMatterCore committed Oct 10, 2024
1 parent b7718cb commit 94c396a
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 64 deletions.
2 changes: 1 addition & 1 deletion include/core/gamecard.h
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ typedef struct {
u32 valid_data_end_page; ///< Expressed in GAMECARD_PAGE_SIZE units.
u8 reserved_1;
u8 flags_2; ///< GameCardFlags2.
u8 reserved_2[0x2];
u16 application_id_list_entry_count; ///< Number of entries in the application ID list located right before valid_data_end_page (19.0.0+).
u8 card_info_iv[AES_128_KEY_SIZE]; ///< AES-128-CBC IV for the CardInfo area (reversed).
u64 partition_fs_header_address; ///< Root Hash File System header offset.
u64 partition_fs_header_size; ///< Root Hash File System header size.
Expand Down
8 changes: 5 additions & 3 deletions include/core/key_sources.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ static const u8 g_masterKeyVectorsProd[NcaKeyGeneration_Current][AES_128_KEY_SIZ
{ 0xAF, 0x11, 0x4C, 0x67, 0x17, 0x7A, 0x52, 0x43, 0xF7, 0x70, 0x2F, 0xC7, 0xEF, 0x81, 0x72, 0x16 }, ///< Master key 0E encrypted with master key 0F.
{ 0x25, 0x12, 0x8B, 0xCB, 0xB5, 0x46, 0xA1, 0xF8, 0xE0, 0x52, 0x15, 0xB7, 0x0B, 0x57, 0x00, 0xBD }, ///< Master key 0F encrypted with master key 10.
{ 0x58, 0x15, 0xD2, 0xF6, 0x8A, 0xE8, 0x19, 0xAB, 0xFB, 0x2D, 0x52, 0x9D, 0xE7, 0x55, 0xF3, 0x93 }, ///< Master key 10 encrypted with master key 11.
{ 0x4A, 0x01, 0x3B, 0xC7, 0x44, 0x6E, 0x45, 0xBD, 0xE6, 0x5E, 0x2B, 0xEC, 0x07, 0x37, 0x52, 0x86 }, ///< Master key 11 encrypted with master key 12.
};

/* Used to derive all previous master keys using the latest master key on development units. */
Expand All @@ -79,24 +80,25 @@ static const u8 g_masterKeyVectorsDev[NcaKeyGeneration_Current][AES_128_KEY_SIZE
{ 0x78, 0x66, 0x19, 0xBD, 0x86, 0xE7, 0xC1, 0x09, 0x9B, 0x6F, 0x92, 0xB2, 0x58, 0x7D, 0xCF, 0x26 }, ///< Master key 0E encrypted with master key 0F.
{ 0x39, 0x1E, 0x7E, 0xF8, 0x7E, 0x73, 0xEA, 0x6F, 0xAF, 0x00, 0x3A, 0xB4, 0xAA, 0xB8, 0xB7, 0x59 }, ///< Master key 0F encrypted with master key 10.
{ 0x0C, 0x75, 0x39, 0x15, 0x53, 0xEA, 0x81, 0x11, 0xA3, 0xE0, 0xDC, 0x3D, 0x0E, 0x76, 0xC6, 0xB8 }, ///< Master key 10 encrypted with master key 11.
{ 0x90, 0x64, 0xF9, 0x08, 0x29, 0x88, 0xD4, 0xDC, 0x73, 0xA4, 0xA1, 0x13, 0x9E, 0x59, 0x85, 0xA0 }, ///< Master key 11 encrypted with master key 12.
};

/* Used to derive a master KEK using the TSEC root key on Erista units. */
/* TODO: update on master key changes. */
static const u8 g_eristaMasterKekSource[AES_128_KEY_SIZE] = {
0x00, 0x04, 0x5D, 0xF0, 0x4D, 0xCD, 0x14, 0xA3, 0x1C, 0xBF, 0xDE, 0x48, 0x55, 0xBA, 0x35, 0xC1
0xD7, 0x63, 0x74, 0x46, 0x4E, 0xBA, 0x78, 0x0A, 0x7C, 0x9D, 0xB3, 0xE8, 0x7A, 0x3D, 0x71, 0xE3
};

/* Used to derive a master KEK on retail Mariko units. */
/* TODO: update on master key changes. */
static const u8 g_marikoMasterKekSourceProd[AES_128_KEY_SIZE] = {
0x4F, 0x41, 0x3C, 0x3B, 0xFB, 0x6A, 0x01, 0x2A, 0x68, 0x9F, 0x83, 0xE9, 0x53, 0xBD, 0x16, 0xD2
0x31, 0xBE, 0x25, 0xFB, 0xDB, 0xB4, 0xEE, 0x49, 0x5C, 0x77, 0x05, 0xC2, 0x36, 0x9F, 0x34, 0x80
};

/* Used to derive a master KEK on development Mariko units. */
/* TODO: update on master key changes. */
static const u8 g_marikoMasterKekSourceDev[AES_128_KEY_SIZE] = {
0xE4, 0x45, 0xD0, 0x14, 0xA0, 0xE5, 0xE9, 0x4B, 0xFE, 0x76, 0xF4, 0x29, 0x41, 0xBB, 0x64, 0xED
0x65, 0x7B, 0x11, 0x46, 0x0E, 0xC2, 0x22, 0x5D, 0xB9, 0xF1, 0xF5, 0x00, 0xF9, 0x3E, 0x1F, 0x70
};

/* Used to derive master keys from master KEKs. Found in TrustZone / Secure Monitor. */
Expand Down
5 changes: 3 additions & 2 deletions include/core/nca.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,9 @@ typedef enum {
NcaKeyGeneration_Since1500NUP = 15, ///< 15.0.0 - 15.0.1.
NcaKeyGeneration_Since1600NUP = 16, ///< 16.0.0 - 16.1.0.
NcaKeyGeneration_Since1700NUP = 17, ///< 17.0.0 - 17.0.1.
NcaKeyGeneration_Since1800NUP = 18, ///< 18.0.0+.
NcaKeyGeneration_Current = NcaKeyGeneration_Since1800NUP,
NcaKeyGeneration_Since1800NUP = 18, ///< 18.0.0 - 18.1.0.
NcaKeyGeneration_Since1900NUP = 19, ///< 19.0.0+.
NcaKeyGeneration_Current = NcaKeyGeneration_Since1900NUP,
NcaKeyGeneration_Max = 32
} NcaKeyGeneration;

Expand Down
106 changes: 48 additions & 58 deletions include/core/npdm.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ typedef struct {
u8 optimize_memory_allocation : 1;
u8 disable_device_address_space_merge : 1;
u8 enable_alias_region_extra_size : 1;
u8 reserved : 1;
u8 prevent_code_reads : 1;
} NpdmMetaFlags;

NXDT_ASSERT(NpdmMetaFlags, 0x1);
Expand Down Expand Up @@ -279,6 +279,15 @@ typedef struct {

NXDT_ASSERT(NpdmSrvAccessControlDescriptorEntry, 0x1);

/// KernelCapability descriptor. Part of the ACID and ACI0 section bodies.
/// This descriptor is composed of a variable number of u32 entries. Thus, the entry count can be calculated by dividing the KernelCapability descriptor size by 4.
/// The entry type is identified by a pattern of "01...11" (zero followed by ones) in the low u16, counting from the LSB. The variable number of ones must never exceed 16 (entirety of the low u16).
typedef struct {
u32 value;
} NpdmKernelCapabilityDescriptorEntry;

NXDT_ASSERT(NpdmKernelCapabilityDescriptorEntry, 0x4);

typedef enum {
NpdmKernelCapabilityEntryBitmaskSize_ThreadInfo = 3,
NpdmKernelCapabilityEntryBitmaskSize_EnableSystemCalls = 4,
Expand Down Expand Up @@ -307,8 +316,7 @@ typedef enum {

/// ThreadInfo entry for the KernelCapability descriptor.
typedef struct {
u32 bitmask : NpdmKernelCapabilityEntryBitmaskSize_ThreadInfo; ///< Always set to NpdmKernelCapabilityEntryBitmaskPattern_ThreadInfo.
u32 padding : 1; ///< Always set to zero.
u32 bitmask : NpdmKernelCapabilityEntryBitmaskSize_ThreadInfo + 1; ///< Always set to NpdmKernelCapabilityEntryBitmaskPattern_ThreadInfo.
u32 lowest_priority : 6;
u32 highest_priority : 6;
u32 min_core_number : 8;
Expand Down Expand Up @@ -534,10 +542,9 @@ typedef enum {

/// EnableSystemCalls entry for the KernelCapability descriptor.
typedef struct {
u32 bitmask : NpdmKernelCapabilityEntryBitmaskSize_EnableSystemCalls; ///< Always set to NpdmKernelCapabilityEntryBitmaskPattern_EnableSystemCalls.
u32 padding : 1; ///< Always set to zero.
u32 system_call_ids : 24; ///< NpdmSystemCallId.
u32 index : 3; ///< System calls index.
u32 bitmask : NpdmKernelCapabilityEntryBitmaskSize_EnableSystemCalls + 1; ///< Always set to NpdmKernelCapabilityEntryBitmaskPattern_EnableSystemCalls.
u32 system_call_ids : 24; ///< NpdmSystemCallId.
u32 index : 3; ///< System calls index.
} NpdmEnableSystemCalls;

NXDT_ASSERT(NpdmEnableSystemCalls, 0x4);
Expand All @@ -548,27 +555,25 @@ typedef enum {
NpdmPermissionType_Count = 2 ///< Total values supported by this enum.
} NpdmPermissionType;

typedef struct {
u32 bitmask : NpdmKernelCapabilityEntryBitmaskSize_MemoryMap + 1; ///< Always set to NpdmKernelCapabilityEntryBitmaskPattern_MemoryMap.
u32 begin_address : 24; ///< begin_address << 12.
u32 permission_type : 1; ///< NpdmPermissionType.
} NpdmMemoryMapType1;

NXDT_ASSERT(NpdmMemoryMapType1, 0x4);

typedef enum {
NpdmMappingType_Io = 0,
NpdmMappingType_Static = 1,
NpdmMappingType_Count = 2 ///< Total values supported by this enum.
} NpdmMappingType;

typedef struct {
u32 bitmask : NpdmKernelCapabilityEntryBitmaskSize_MemoryMap; ///< Always set to NpdmKernelCapabilityEntryBitmaskPattern_MemoryMap.
u32 padding : 1; ///< Always set to zero.
u32 begin_address : 24; ///< begin_address << 12.
u32 permission_type : 1; ///< NpdmPermissionType.
} NpdmMemoryMapType1;

NXDT_ASSERT(NpdmMemoryMapType1, 0x4);

typedef struct {
u32 bitmask : NpdmKernelCapabilityEntryBitmaskSize_MemoryMap; ///< Always set to NpdmKernelCapabilityEntryBitmaskPattern_MemoryMap.
u32 padding : 1; ///< Always set to zero.
u32 size : 20; ///< size << 12.
u32 bitmask : NpdmKernelCapabilityEntryBitmaskSize_MemoryMap + 1; ///< Always set to NpdmKernelCapabilityEntryBitmaskPattern_MemoryMap.
u32 size : 20; ///< size << 12.
u32 reserved : 4;
u32 mapping_type : 1; ///< NpdmMappingType.
u32 mapping_type : 1; ///< NpdmMappingType.
} NpdmMemoryMapType2;

NXDT_ASSERT(NpdmMemoryMapType2, 0x4);
Expand All @@ -586,9 +591,8 @@ NXDT_ASSERT(NpdmMemoryMap, 0x4);

/// IoMemoryMap entry for the KernelCapability descriptor.
typedef struct {
u32 bitmask : NpdmKernelCapabilityEntryBitmaskSize_IoMemoryMap; ///< Always set to NpdmKernelCapabilityEntryBitmaskPattern_IoMemoryMap.
u32 padding : 1; ///< Always set to zero.
u32 begin_address : 24; ///< begin_address << 12.
u32 bitmask : NpdmKernelCapabilityEntryBitmaskSize_IoMemoryMap + 1; ///< Always set to NpdmKernelCapabilityEntryBitmaskPattern_IoMemoryMap.
u32 begin_address : 24; ///< begin_address << 12.
} NpdmIoMemoryMap;

NXDT_ASSERT(NpdmIoMemoryMap, 0x4);
Expand All @@ -603,24 +607,22 @@ typedef enum {

/// MemoryRegionMap entry for the KernelCapability descriptor.
typedef struct {
u32 bitmask : NpdmKernelCapabilityEntryBitmaskSize_MemoryRegionMap; ///< Always set to NpdmKernelCapabilityEntryBitmaskPattern_MemoryRegionMap.
u32 padding : 1; ///< Always set to zero.
u32 region_type_0 : 6; ///< NpdmRegionType.
u32 permission_type_0 : 1; ///< NpdmPermissionType.
u32 region_type_1 : 6; ///< NpdmRegionType.
u32 permission_type_1 : 1; ///< NpdmPermissionType.
u32 region_type_2 : 6; ///< NpdmRegionType.
u32 permission_type_2 : 1; ///< NpdmPermissionType.
u32 bitmask : NpdmKernelCapabilityEntryBitmaskSize_MemoryRegionMap + 1; ///< Always set to NpdmKernelCapabilityEntryBitmaskPattern_MemoryRegionMap.
u32 region_type_0 : 6; ///< NpdmRegionType.
u32 permission_type_0 : 1; ///< NpdmPermissionType.
u32 region_type_1 : 6; ///< NpdmRegionType.
u32 permission_type_1 : 1; ///< NpdmPermissionType.
u32 region_type_2 : 6; ///< NpdmRegionType.
u32 permission_type_2 : 1; ///< NpdmPermissionType.
} NpdmMemoryRegionMap;

NXDT_ASSERT(NpdmMemoryRegionMap, 0x4);

/// EnableInterrupts entry for the KernelCapability descriptor.
typedef struct {
u32 bitmask : NpdmKernelCapabilityEntryBitmaskSize_EnableInterrupts; ///< Always set to NpdmKernelCapabilityEntryBitmaskPattern_EnableInterrupts.
u32 padding : 1; ///< Always set to zero.
u32 interrupt_number_0 : 10; ///< 0x3FF means empty.
u32 interrupt_number_1 : 10; ///< 0x3FF means empty.
u32 bitmask : NpdmKernelCapabilityEntryBitmaskSize_EnableInterrupts + 1; ///< Always set to NpdmKernelCapabilityEntryBitmaskPattern_EnableInterrupts.
u32 interrupt_number_0 : 10; ///< 0x3FF means empty.
u32 interrupt_number_1 : 10; ///< 0x3FF means empty.
} NpdmEnableInterrupts;

NXDT_ASSERT(NpdmEnableInterrupts, 0x4);
Expand All @@ -635,9 +637,8 @@ typedef enum {
/// MiscParams entry for the KernelCapability descriptor.
/// Defaults to 0 if this entry doesn't exist.
typedef struct {
u32 bitmask : NpdmKernelCapabilityEntryBitmaskSize_MiscParams; ///< Always set to NpdmKernelCapabilityEntryBitmaskPattern_MiscParams.
u32 padding : 1; ///< Always set to zero.
u32 program_type : 3; ///< NpdmProgramType.
u32 bitmask : NpdmKernelCapabilityEntryBitmaskSize_MiscParams + 1; ///< Always set to NpdmKernelCapabilityEntryBitmaskPattern_MiscParams.
u32 program_type : 3; ///< NpdmProgramType.
u32 reserved : 15;
} NpdmMiscParams;

Expand All @@ -646,18 +647,16 @@ NXDT_ASSERT(NpdmMiscParams, 0x4);
/// KernelVersion entry for the KernelCapability descriptor.
/// This is derived from/equivalent to SDK version.
typedef struct {
u32 bitmask : NpdmKernelCapabilityEntryBitmaskSize_KernelVersion; ///< Always set to NpdmKernelCapabilityEntryBitmaskPattern_KernelVersion.
u32 padding : 1; ///< Always set to zero.
u32 minor_version : 4; ///< SDK minor version.
u32 major_version : 13; ///< SDK major version + 4.
u32 bitmask : NpdmKernelCapabilityEntryBitmaskSize_KernelVersion + 1; ///< Always set to NpdmKernelCapabilityEntryBitmaskPattern_KernelVersion.
u32 minor_version : 4; ///< SDK minor version.
u32 major_version : 13; ///< SDK major version + 4.
} NpdmKernelVersion;

NXDT_ASSERT(NpdmKernelVersion, 0x4);

/// HandleTableSize entry for the KernelCapability descriptor.
typedef struct {
u32 bitmask : NpdmKernelCapabilityEntryBitmaskSize_HandleTableSize; ///< Always set to NpdmKernelCapabilityEntryBitmaskPattern_HandleTableSize.
u32 padding : 1; ///< Always set to zero.
u32 bitmask : NpdmKernelCapabilityEntryBitmaskSize_HandleTableSize + 1; ///< Always set to NpdmKernelCapabilityEntryBitmaskPattern_HandleTableSize.
u32 handle_table_size : 10;
u32 reserved : 6;
} NpdmHandleTableSize;
Expand All @@ -666,24 +665,15 @@ NXDT_ASSERT(NpdmHandleTableSize, 0x4);

/// MiscFlags entry for the KernelCapability descriptor.
typedef struct {
u32 bitmask : NpdmKernelCapabilityEntryBitmaskSize_MiscFlags; ///< Always set to NpdmKernelCapabilityEntryBitmaskPattern_MiscFlags.
u32 padding : 1; ///< Always set to zero.
u32 enable_debug : 1;
u32 force_debug : 1;
u32 reserved : 13;
u32 bitmask : NpdmKernelCapabilityEntryBitmaskSize_MiscFlags + 1; ///< Always set to NpdmKernelCapabilityEntryBitmaskPattern_MiscFlags.
u32 enable_debug : 1;
u32 force_debug_prod : 1;
u32 force_debug : 1;
u32 reserved : 12;
} NpdmMiscFlags;

NXDT_ASSERT(NpdmMiscFlags, 0x4);

/// KernelCapability descriptor. Part of the ACID and ACI0 section bodies.
/// This descriptor is composed of a variable number of u32 entries. Thus, the entry count can be calculated by dividing the KernelCapability descriptor size by 4.
/// The entry type is identified by a pattern of "01...11" (zero followed by ones) in the low u16, counting from the LSB. The variable number of ones must never exceed 16 (entirety of the low u16).
typedef struct {
u32 value;
} NpdmKernelCapabilityDescriptorEntry;

NXDT_ASSERT(NpdmKernelCapabilityDescriptorEntry, 0x4);

typedef struct {
u8 *raw_data; ///< Pointer to a dynamically allocated buffer that holds the raw NPDM.
u64 raw_data_size; ///< Raw NPDM size.
Expand Down

0 comments on commit 94c396a

Please sign in to comment.