@@ -404,7 +404,7 @@ fn parse_ncch(cia: &mut CiaReader, offs: u64, mut titleid: [u8; 8]) {
404404 }
405405}
406406
407- fn parse_cia ( mut romfile : File , filename : String ) {
407+ fn parse_cia ( mut romfile : File , filename : String , partition : Option < u8 > ) {
408408 romfile. seek ( SeekFrom :: Start ( 0 ) ) . unwrap ( ) ;
409409 let mut tmp: [ u8 ; 32 ] = [ 0 ; 32 ] ;
410410 romfile. read_exact ( & mut tmp) . unwrap ( ) ;
@@ -472,7 +472,13 @@ fn parse_cia(mut romfile: File, filename: String) {
472472 romfile. seek ( SeekFrom :: Start ( contentoffs + next_content_offs) ) . unwrap ( ) ;
473473 let mut cia_handle = CiaReader :: new ( romfile. try_clone ( ) . unwrap ( ) , cenc, filename. clone ( ) , titkey, content. cid , content. cidx , contentoffs + next_content_offs, false , false ) ;
474474 next_content_offs += align ( content. csize , 64 ) ;
475+
476+ match partition {
477+ Some ( number) => if ( i as u8 ) != number { continue ; } ,
478+ None => ( ) ,
479+ }
475480 parse_ncch ( & mut cia_handle, 0 , tid[ 0 ..8 ] . try_into ( ) . unwrap ( ) ) ;
481+
476482 } else { println ! ( "CIA content can't be parsed, skipping partition" ) }
477483 Err ( _) => println ! ( "CIA content can't be parsed, skipping partition" )
478484 }
@@ -481,12 +487,25 @@ fn parse_cia(mut romfile: File, filename: String) {
481487
482488fn main ( ) {
483489 let args: Vec < String > = std:: env:: args ( ) . collect ( ) ;
484- if args. len ( ) < 2 {
485- println ! ( "Usage: ctrdecrypt <ROMFILE>" ) ;
490+ let mut partition: Option < u8 > = None ;
491+ if args. len ( ) < 2 || args. len ( ) == 3 {
492+ // Decrypting a specific NCCH container is supported only for .cia backups
493+ println ! ( "Usage: ctrdecrypt <ROMFILE> [--ncch <partion-number>]" ) ;
486494 return ;
487495 } else if !Path :: exists ( Path :: new ( & args[ 1 ] ) ) {
488496 println ! ( "ROM does not exist" ) ;
489497 return ;
498+ } else if args. len ( ) == 4 {
499+ if args[ 2 ] != "--ncch" {
500+ println ! ( "Invalid argument: {}" , args[ 2 ] ) ;
501+ return ;
502+ }
503+ if args[ 3 ] . parse :: < u8 > ( ) . is_err ( ) {
504+ println ! ( "Invalid partition number: {}" , args[ 3 ] ) ;
505+ return ;
506+ } else {
507+ partition = Some ( args[ 3 ] . parse :: < u8 > ( ) . unwrap ( ) ) ;
508+ }
490509 }
491510
492511 let mut rom = File :: open ( & args[ 1 ] ) . unwrap ( ) ;
@@ -513,6 +532,6 @@ fn main() {
513532 let mut check: [ u8 ; 4 ] = [ 0 ; 4 ] ;
514533 rom. read_exact ( & mut check) . unwrap ( ) ;
515534
516- if check[ 2 ..4 ] == [ 0 , 0 ] { parse_cia ( rom, args[ 1 ] . to_string ( ) ) }
535+ if check[ 2 ..4 ] == [ 0 , 0 ] { parse_cia ( rom, args[ 1 ] . to_string ( ) , partition ) }
517536 }
518537}
0 commit comments