Add another one constructor to KIP1Provider
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Dmitry Isaenko 2023-02-05 23:04:17 +03:00
parent 1f25e917c3
commit 2b5e08c60a
3 changed files with 146 additions and 15 deletions

View file

@ -92,7 +92,7 @@ public class NCAProvider {
//------------------------------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------------------------------
byte[] decryptedHeader = new byte[0xC00]; byte[] decryptedHeader = new byte[0xC00];
RandomAccessFile raf = new RandomAccessFile(file, "r"); try (RandomAccessFile raf = new RandomAccessFile(file, "r")) {
byte[] encryptedSequence = new byte[0x200]; byte[] encryptedSequence = new byte[0x200];
byte[] decryptedSequence; byte[] decryptedSequence;
@ -107,8 +107,7 @@ public class NCAProvider {
} }
setupHeader(decryptedHeader); setupHeader(decryptedHeader);
}
raf.close();
setupNCAContent(); setupNCAContent();
/*//--------------------------------------------------------------------- /*//---------------------------------------------------------------------

View file

@ -34,14 +34,20 @@ public class KIP1Provider extends ExportAble {
private long size; private long size;
public KIP1Provider(String fileLocation) throws Exception{ public KIP1Provider(String fileLocation) throws Exception{
this(fileLocation, 0);
}
public KIP1Provider(String fileLocation, long kip1StartOffset) throws Exception{
this.producer = new InFileStreamClassicProducer(Paths.get(fileLocation)); this.producer = new InFileStreamClassicProducer(Paths.get(fileLocation));
this.stream = producer.produce(); this.stream = producer.produce();
if (kip1StartOffset != stream.skip(kip1StartOffset))
throw new Exception("Failed to skip declared starting offset "+kip1StartOffset);
byte[] kip1HeaderBytes = new byte[HEADER_SIZE]; byte[] kip1HeaderBytes = new byte[HEADER_SIZE];
if (HEADER_SIZE != stream.read(kip1HeaderBytes)) if (HEADER_SIZE != stream.read(kip1HeaderBytes))
throw new Exception("Unable to read KIP1 file header"); throw new Exception("Unable to read KIP1 file header");
makeHeader(kip1HeaderBytes); makeHeader(kip1HeaderBytes);
calculateOffsets(0); calculateOffsets(kip1StartOffset);
} }
public KIP1Provider(byte[] kip1HeaderBytes, long kip1StartOffset, InFileStreamClassicProducer producer) throws Exception{ public KIP1Provider(byte[] kip1HeaderBytes, long kip1StartOffset, InFileStreamClassicProducer producer) throws Exception{

View file

@ -0,0 +1,126 @@
package libKonogonka.examples;
import libKonogonka.Converter;
import libKonogonka.KeyChainHolder;
import libKonogonka.fs.NCA.NCAProvider;
import libKonogonka.fs.RomFs.FileSystemEntry;
import libKonogonka.fs.RomFs.RomFsProvider;
import libKonogonka.fs.other.System2.System2Provider;
import libKonogonka.fs.other.System2.ini1.KIP1Provider;
import libKonogonka.aesctr.InFileStreamProducer;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import java.io.File;
import java.nio.file.*;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class ExtractDecompressedKip1Test {
final String KEYS_FILE_LOCATION = "FilesForTests"+ File.separator+"prod.keys";
final String XCI_HEADER_KEYS_FILE_LOCATION = "FilesForTests"+File.separator+"xci_header_key.txt";
final String pathToFirmwares = ". . . nintendo-switch-global-firmwares";
private static KeyChainHolder keyChainHolder;
@DisplayName("Extract FS.kip1")
@Test
void testSystem2() throws Exception{
makeKeys();
File firmwaresDir = new File(pathToFirmwares);
Assertions.assertNotNull(firmwaresDir);
//File[] fwDirs = new File(pathToFirmwares).listFiles((file, s) -> (s.matches("^Firmware (9\\.|[0-9][0-9]\\.).*") && ! s.endsWith(".zip")));
File[] fwDirs = new File(pathToFirmwares).listFiles((file, s) -> s.equals("Firmware 14.1.2"));
for (File fw : fwDirs) {
if (fw.isFile())
continue;
System.out.println("\t\t\t==== "+fw.getName()+" ====");
String fwAbsolutePath = fw.getAbsolutePath();
iterate(fwAbsolutePath,
System.getProperty("java.io.tmpdir")+File.separator+fw.getName()+File.separator+"FAT",
System.getProperty("java.io.tmpdir")+File.separator+fw.getName()+File.separator+"ExFAT");
}
}
void iterate(String pathToFirmware, String exportFat, String exportExFat){
try {
String[] ncaFileNames = collectNcaFileNames(pathToFirmware);
List<NCAProvider> ncaProviders = makeNcaProviders(ncaFileNames, pathToFirmware);
NCAProvider system2FatNcaProvider = null;
NCAProvider system2ExFatNcaProvider = null;
for (NCAProvider ncaProvider : ncaProviders) {
String titleId = Converter.byteArrToHexStringAsLE(ncaProvider.getTitleId());
if (titleId.equals("0100000000000819"))
system2FatNcaProvider = ncaProvider;
else if (titleId.equals("010000000000081b"))
system2ExFatNcaProvider = ncaProvider;
}
Assertions.assertNotNull(system2FatNcaProvider);
Assertions.assertNotNull(system2ExFatNcaProvider);
System.out.println("FAT " + system2FatNcaProvider.getFile().getName() + "\t" + exportFat);
System.out.println("ExFAT " + system2ExFatNcaProvider.getFile().getName() + "\t" + exportExFat);
testExportedFiles(system2FatNcaProvider, exportFat);
testExportedFiles(system2ExFatNcaProvider, exportExFat);
}
catch (Exception e){
e.printStackTrace();
}
}
void makeKeys() throws Exception{
String keyValue = new String(Files.readAllBytes(Paths.get(XCI_HEADER_KEYS_FILE_LOCATION))).trim();
Assertions.assertNotEquals(0, keyValue.length());
keyChainHolder = new KeyChainHolder(KEYS_FILE_LOCATION, keyValue);
}
String[] collectNcaFileNames(String pathToFirmware){
File firmware = new File(pathToFirmware);
Assertions.assertTrue(firmware.exists());
String[] ncaFileNames = firmware.list((File directory, String file) -> ( ! file.endsWith(".cnmt.nca") && file.endsWith(".nca")));
Assertions.assertNotNull(ncaFileNames);
return ncaFileNames;
}
List<NCAProvider> makeNcaProviders(String[] ncaFileNames, String pathToFirmware) throws Exception{
List<NCAProvider> ncaProviders = new ArrayList<>();
for (String ncaFileName : ncaFileNames){
File nca = new File(pathToFirmware+File.separator+ncaFileName);
NCAProvider provider = new NCAProvider(nca, keyChainHolder.getRawKeySet());
ncaProviders.add(provider);
}
Assertions.assertNotEquals(0, ncaProviders.size());
return ncaProviders;
}
void testExportedFiles(NCAProvider system2NcaProvider, String exportIntoFolder) throws Exception{
RomFsProvider romFsProvider = system2NcaProvider.getNCAContentProvider(0).getRomfs();
FileSystemEntry package2FileSystemEntry = romFsProvider.getRootEntry().getContent()
.stream()
.filter(e -> e.getName().equals("nx"))
.collect(Collectors.toList())
.get(0)
.getContent()
.stream()
.filter(e -> e.getName().equals("package2"))
.collect(Collectors.toList())
.get(0);
InFileStreamProducer producer = romFsProvider.getStreamProducer(package2FileSystemEntry);
System2Provider providerStream = new System2Provider(producer, keyChainHolder);
for (KIP1Provider kip1Provider : providerStream.getIni1Provider().getKip1List()){
String kip1Name = kip1Provider.getHeader().getName();
if (kip1Name.equals("FS")) {
System.out.println("Exported: "+kip1Provider.exportAsDecompressed(exportIntoFolder));
break;
}
}
}
}