pyctr.type.ncch module

Module for interacting with NCCH files.

exception pyctr.type.ncch.NCCHError[source]

Bases: PyCTRError

Generic exception for NCCH operations.

exception pyctr.type.ncch.InvalidNCCHError[source]

Bases: NCCHError

Invalid NCCH header exception.

exception pyctr.type.ncch.NCCHSeedError[source]

Bases: NCCHError

NCCH seed is not set up, or attempted to set up seed when seed crypto is not used.

exception pyctr.type.ncch.MissingSeedError[source]

Bases: NCCHSeedError

Seed could not be found.

class pyctr.type.ncch.NCCHSection(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)[source]

Bases: IntEnum

ExtendedHeader = 1
ExeFS = 2
RomFS = 3
Header = 4
Plain = 6
FullDecrypted = 7
Raw = 8
class pyctr.type.ncch.NCCHRegion(section, offset, size, end, iv)[source]

Bases: NamedTuple

Parameters:
section: NCCHSection

Alias for field number 0

offset: int

Alias for field number 1

size: int

Alias for field number 2

end: int

Alias for field number 3

iv: int

Alias for field number 4

class pyctr.type.ncch.NCCHFlags(crypto_method, executable, fixed_crypto_key, no_romfs, no_crypto, uses_seed)[source]

Bases: NamedTuple

Flags for an NCCH. This is not a complete set. See: https://3dbrew.org/wiki/NCCH#NCCH_Flags

Parameters:
  • crypto_method (int) –

  • executable (bool) –

  • fixed_crypto_key (bool) –

  • no_romfs (bool) –

  • no_crypto (bool) –

  • uses_seed (bool) –

crypto_method: int

Determines the extra keyslot used for RomFS and parts of ExeFS. 0x00 = NCCH, 0x01 = NCCH70, 0x0A = NCCH93, 0x0B = NCCH96

executable: bool

If this content is a CXI (CTR Executable Image) or CFA (CTR File Archive). In the raw flags, “Data” needs to be set with “Executable” unset for this to be a CFA.

fixed_crypto_key: bool

If a fixed normal key is used to encrypt the contents. This is often a zero-key, with a different “fixed system key” used in specfic situations.

no_romfs: bool

Determines if there is no RomFS.

no_crypto: bool

If no encryption is used at all. This takes precedence over other encryption flags.

uses_seed: bool

If a seed is used in conjunction with the extra keyslot.

classmethod from_bytes(flag_bytes)[source]
Parameters:

flag_bytes (bytes) –

Return type:

NCCHFlags

class pyctr.type.ncch.NCCHReader(file, *, fs=None, closefd=None, case_insensitive=True, crypto=None, dev=False, seed=None, load_sections=True, assume_decrypted=False)[source]

Bases: TypeReaderCryptoBase

Reads the contents of NCCH containers.

The NCCH header contains information such as Title ID, Product Code, flags, and section info.

NCCH containers can be classified as a CTR Executable Image (CXI) if it has executable code, or a CTR File Archive (CFA) if it doesn’t.

A CXI can contain:

  • an Extended Header (extheader) with executable info and access permissions

  • a logo region (for titles released before System Menu 5.0.0-11, this is in the ExeFS)

  • a plain region with SDK library strings

  • an Executable Filesystem (ExeFS) with .code, icon, and banner

  • a Read-only Filesystem (RomFS)

A CFA can contain:

  • an ExeFS with icon and banner

  • a RomFS

Parameters:
  • file (FilePathOrObject) – A file path or a file-like object with the NCCH data.

  • case_insensitive (bool) – Use case-insensitive paths for the RomFS.

  • crypto (CryptoEngine) – A custom CryptoEngine object to be used. Defaults to None, which causes a new one to be created.

  • dev (bool) – Use devunit keys.

  • seed (bytes) – Seed to use. This is a quick way to add a seed using add_seed().

  • load_sections (bool) – Load the ExeFS and RomFS as ExeFSReader and RomFSReader objects.

  • assume_decrypted (bool) – Assume each NCCH content is decrypted. Needed if the image was decrypted without fixing the NCCH flags.

  • fs (Optional[FS]) –

  • closefd (bool) –

closed: bool

True if the reader is closed.

exefs: ExeFSReader | None

The ExeFSReader of the NCCH, if it has one.

romfs: RomFSReader | None

The RomFSReader of the NCCH, if it has one.

version: int

NCCH version. Not to be confused with the title version.

content_size: int

Expected size of the NCCH container in bytes.

partition_id: str

Partition ID as an integer. Usually this is different for NCCH in a title, incrementing the platform section (e.g. 0004 for Application, 0005 for Manual, 0006 for Download Play Child). DLC does not follow this, all contents use 0004 for the platform.

product_code: str

Product code of the content.

program_id: str

Title ID of the application.

sections: Dict[NCCHSection, NCCHRegion]

Contains all the sections the NCCH has.

flags: NCCHFlags

NCCH flags of the container.

main_keyslot: Keyslot

Keyslot to use for decrypting the Extended Header and ExeFS header. In most cases this is Original NCCH (0x2C). Some titles may use a fixed crypto key though, either all zeros, or a special key for system titles. PyCTR uses the fake keyslots 0x41 and 0x42 for these respectively.

extra_keyslot: Keyslot

Second keyslot to use for the ExeFS contents and RomFS. This is determined by the crypto method in the NCCH flags. This is set to the same as main_keyslot for titles without an extra crypto method, or with a fixed crypto key.

close()[source]

Close the reader. If closefd is True, the underlying file is also closed.

load_sections()[source]

Load the sections of the NCCH (Extended Header, ExeFS, and RomFS).

open_raw_section(section)[source]

Open a raw NCCH section for reading with on-the-fly decryption.

Parameters:

section (NCCHSection) – The section to open.

Returns:

A file-like object that reads from the section.

get_key_y(original=False)[source]
Parameters:

original (bool) –

Return type:

bytes

check_for_extheader()[source]

Checks if the NCCH has an Extended Header.

Returns:

True if it has an ExtHeader.

Return type:

bool

setup_seed(seed)[source]
Parameters:

seed (bytes) –

get_data(section, offset, size)[source]

Get data from an NCCH section.

Parameters:
  • section (Union[NCCHRegion, NCCHSection]) – A region or section to read from.

  • offset (int) – Offset from the section start.

  • size (int) – Data size.

Returns:

Decrypted data from the region.

Return type:

bytes