Exemplo n.º 1
0
 public NcchPartition(RomFs romfs = null, ExeFs exefs = null, NcchHeader header = null, NcchExtendedHeader exheader = null, string plainRegion = null, byte[] logo = null)
 {
     RomFs       = romfs;
     ExeFs       = exefs;
     Header      = header;
     ExHeader    = exheader;
     PlainRegion = plainRegion;
     Logo        = logo;
 }
Exemplo n.º 2
0
        public static async Task <NcchPartition> Load(IReadOnlyBinaryDataAccessor data)
        {
            NcchHeader?header = null;

            if (data.Length > 0)
            {
                header = new NcchHeader(await data.ReadArrayAsync(0, 0x200));
            }

            var partition = new NcchPartition(header);
            await partition.Initialize(data);

            return(partition);
        }
Exemplo n.º 3
0
            /// <summary>
            /// Creates a new <see cref="NcchHeader"/> based on the given header
            /// </summary>
            /// <remarks>
            /// The following properties need to be updated manually:
            /// - <see cref="Signature" />
            /// - <see cref="ContentSize" />
            /// - <see cref="ContentLockSeedHash" />
            /// - <see cref="LogoRegionHash" />
            /// - <see cref="ExHeaderHash" />
            /// - <see cref="ExHeaderSize" />
            /// - <see cref="PlainRegionOffset" />
            /// - <see cref="PlainRegionSize" />
            /// - <see cref="LogoRegionOffset" />
            /// - <see cref="LogoRegionSize" />
            /// - <see cref="ExeFsOffset" />
            /// - <see cref="ExeFsSize" />
            /// - <see cref="ExeFsHashRegionSize" />
            /// - <see cref="RomFsOffset" />
            /// - <see cref="RomFsSize" />
            /// - <see cref="RomFsHashRegionSize" />
            /// - <see cref="ExeFsSuperblockHash" />
            /// - <see cref="RomFsSuperblockHash" />
            /// </remarks>
            public static NcchHeader Copy(NcchHeader other)
            {
                if (other == null)
                {
                    throw new ArgumentNullException(nameof(other));
                }

                return(new NcchHeader
                {
                    Magic = "NCCH",
                    PartitionId = other.PartitionId,
                    MakerCode = other.MakerCode,
                    Version = other.Version,
                    ProgramId = other.ProgramId,
                    Reserved1 = other.Reserved1,
                    ProductCode = other.ProductCode,
                    Reserved2 = other.Reserved2,
                    Flags = other.Flags,
                    Reserved3 = other.Reserved3,
                    Reserved4 = other.Reserved4
                });
            }
Exemplo n.º 4
0
        /// <summary>
        /// Builds a new NCCH partition from the given directory
        /// </summary>
        /// <param name="fileSystem">File system from which to load the files</param>
        /// <returns>A newly built NCCH partition</returns>
        public static async Task <NcchPartition> Build(string headerFilename, string exHeaderFilename, string?exeFsDirectory, string?romFsDiretory, string?plainRegionFilename, string?logoFilename, IFileSystem fileSystem, ProcessingProgressedToken?progressToken = null)
        {
            ProcessingProgressedToken?exefsToken = null;
            ProcessingProgressedToken?romfsToken = null;

            void ReportProgress()
            {
                if (progressToken != null)
                {
                    progressToken.TotalFileCount     = (exefsToken?.TotalFileCount + romfsToken?.TotalFileCount).GetValueOrDefault();
                    progressToken.ProcessedFileCount = (exefsToken?.ProcessedFileCount + romfsToken?.ProcessedFileCount).GetValueOrDefault();
                }
            };

            Task <ExeFs?> exeFsTask;

            if (!string.IsNullOrEmpty(exeFsDirectory))
            {
                if (progressToken != null)
                {
                    exefsToken = new ProcessingProgressedToken();
                    exefsToken.FileCountChanged += (sender, e) => ReportProgress();
                }
                exeFsTask = Task.Run <ExeFs?>(async() => await ExeFs.Build(exeFsDirectory, fileSystem, exefsToken).ConfigureAwait(false));
            }
            else
            {
                exeFsTask = Task.FromResult <ExeFs?>(null);
            }

            Task <RomFs?> romFsTask;

            if (!string.IsNullOrEmpty(romFsDiretory))
            {
                if (progressToken != null)
                {
                    romfsToken = new ProcessingProgressedToken();
                    romfsToken.FileCountChanged += (sender, e) => ReportProgress();
                }
                romFsTask = Task.Run <RomFs?>(async() => await RomFs.Build(romFsDiretory, fileSystem, romfsToken).ConfigureAwait(false));
            }
            else
            {
                romFsTask = Task.FromResult <RomFs?>(null);
            }

            var header = new NcchHeader(fileSystem.ReadAllBytes(headerFilename));

            NcchExtendedHeader?exHeader = null;

            if (!string.IsNullOrEmpty(exHeaderFilename))
            {
                using var exHeaderData = new BinaryFile(fileSystem.ReadAllBytes(exHeaderFilename));
                exHeader = await NcchExtendedHeader.Load(exHeaderData);
            }

            string?plainRegion = null;

            if (!string.IsNullOrEmpty(plainRegionFilename))
            {
                plainRegion = fileSystem.ReadAllText(plainRegionFilename);
            }

            byte[]? logo = null;
            if (!string.IsNullOrEmpty(logoFilename))
            {
                logo = fileSystem.ReadAllBytes(logoFilename);
            }

            return(new NcchPartition(await romFsTask, await exeFsTask, header, exHeader, plainRegion, logo));
        }
Exemplo n.º 5
0
        /// <summary>
        /// Writes the current state of the NCCH partition to the given binary data accessor
        /// </summary>
        /// <param name="data">Data accessor to receive the binary data</param>
        /// <returns>A long representing the total length of data written</returns>
        public async Task <long> WriteBinary(IWriteOnlyBinaryDataAccessor data)
        {
            // Get the data
            var exheader = ExHeader?.ToByteArray();

            var plainRegion       = !string.IsNullOrEmpty(PlainRegion) ? Encoding.ASCII.GetBytes(PlainRegion) : null;
            var plainRegionOffset = 0;
            var logoRegionOffset  = 0;

            var exeFs       = ExeFs?.ToByteArray();
            var exeFsOffset = 0;

            var romFs       = RomFs?.Data;
            var romFsOffset = 0;

            // Write the data
            var offset = 0x200; // Skip the header, write it last

            if (exheader != null)
            {
                await data.WriteAsync(offset, exheader);

                offset += exheader.Length;
            }
            if (plainRegion != null)
            {
                plainRegionOffset = offset;
                await data.WriteAsync(offset, plainRegion);

                offset += plainRegion.Length;

                var padding = new byte[0x200 - plainRegion.Length % 0x200];
                await data.WriteAsync(offset, padding);

                offset += padding.Length;
            }
            if (Logo != null)
            {
                logoRegionOffset = offset;
                await data.WriteAsync(offset, Logo);

                offset += Logo.Length;

                var padding = new byte[0x200 - Logo.Length % 0x200];
                await data.WriteAsync(offset, padding);

                offset += padding.Length;
            }
            if (exeFs != null)
            {
                exeFsOffset = offset;
                await data.WriteAsync(offset, exeFs);

                offset += exeFs.Length;

                var padding = new byte[0x200 - exeFs.Length % 0x200];
                await data.WriteAsync(offset, padding);

                offset += padding.Length;
            }
            if (romFs != null)
            {
                romFsOffset = offset;
                const int bufferSize = 1024 * 1024;
                for (int i = 0; i < romFs.Length; i += bufferSize)
                {
                    int length = (int)Math.Min(bufferSize, romFs.Length - i);
                    var block  = await romFs.ReadArrayAsync(i, length);

                    await data.WriteAsync(offset, block);

                    offset += length;
                }

                var padding = new byte[0x200 - romFs.Length % 0x200];
                await data.WriteAsync(offset, padding);

                offset += padding.Length;
            }

            // Create a new header
            using var sha = SHA256.Create();

            var header = NcchHeader.Copy(this.Header);

            header.Signature           = new byte[0x100];                              // We lack the 3DS's private key, so leave out the signature
            header.ContentSize         = (offset + MediaUnitSize - 1) / MediaUnitSize; // offset/MediaUnitSize, but rounding up
            header.ContentLockSeedHash = 0;                                            // Unknown, left blank by SciresM's 3DS Builder
            if (Logo != null)
            {
                header.LogoRegionHash = sha.ComputeHash(Logo);
            }
            else
            {
                header.LogoRegionHash = new byte[0x20];
            }

            if (exheader != null)
            {
                header.ExHeaderHash = NcchExtendedHeader.GetSuperblockHash(sha, exheader);
                header.ExHeaderSize = NcchExtendedHeader.ExHeaderDataSize;
            }
            else
            {
                header.ExHeaderHash = new byte[0x20];
                header.ExHeaderSize = 0;
            }

            header.PlainRegionOffset   = (plainRegionOffset + MediaUnitSize - 1) / MediaUnitSize;
            header.PlainRegionSize     = ((plainRegion?.Length ?? 0) + MediaUnitSize - 1) / MediaUnitSize;
            header.LogoRegionOffset    = (logoRegionOffset + MediaUnitSize - 1) / MediaUnitSize;
            header.LogoRegionSize      = ((Logo?.Length ?? 0) + MediaUnitSize - 1) / MediaUnitSize;
            header.ExeFsOffset         = (exeFsOffset + MediaUnitSize - 1) / MediaUnitSize;
            header.ExeFsSize           = ((exeFs?.Length ?? 0) + MediaUnitSize - 1) / MediaUnitSize;
            header.ExeFsHashRegionSize = 1; // Static 0x200 for exefs superblock
            header.RomFsOffset         = (romFsOffset + MediaUnitSize - 1) / MediaUnitSize;
            header.RomFsSize           = ((int)(romFs?.Length ?? 0) + MediaUnitSize - 1) / MediaUnitSize;
            header.RomFsHashRegionSize = ((RomFs?.Header?.MasterHashSize ?? 0) + MediaUnitSize - 1) / MediaUnitSize;
            header.ExeFsSuperblockHash = ExeFs?.GetSuperblockHash() ?? new byte[0x20];
            header.RomFsSuperblockHash = RomFs != null ? await RomFs.GetSuperblockHash(sha, romFs, RomFs.Header) : new byte[0x20];

            var headerData = await header.ToBinary().ReadArrayAsync();

            await data.WriteAsync(0, headerData);

            return(offset);
        }
Exemplo n.º 6
0
 public NcchPartition(NcchHeader header)
 {
     Header = header;
 }