#!/usr/bin/perl -w

# $Id: myMakemaster,v 1.11 2007/05/01 18:38:59 craig Exp $

#               Copyright (C) 2007, BroadOn Communications Corp.
#
#  These coded instructions, statements, and computer programs contain
#  unpublished  proprietary information of BroadOn Communications Corp.,
#  and  are protected by Federal copyright law. They may not be disclosed
#  to  third  parties or copied or duplicated in any form, in whole or in
#  part, without the prior written consent of BroadOn Communications Corp.


# This program generates a developer encrypted disk image
# given the output of executing 'ndrun'.  It does what the
# Nintendo tool 'makemaster' does, but optionally allows
# keeping intermediate files around for debugging.


use strict;
use Getopt::Long;
use File::Basename;

my $ProgName;

# ********************************************************************
sub
usage()
{
    print "Usage: $0 [options] <RVL program name>\n";
    print " Options:\n";
    print "           -a               - Keep all intermediate files\n";
    print "           -b               - Use Broadon (not IRD) tools -\n";
    print "                              allows keeping of tmd, ticket files\n";
    print "           -h               - Print help message\n";
    print "           -k               - Keep some intermediate files\n";
    print "                              like the partitions, TMD, etc.\n";
    print "           -N               - don't invoke ndstop - it hangs\n";
    print "                              on remote logged in shells\n";
    print "           -t [file.tmd]    - input TMD (for testing only)\n";
    print "           -v               - Print status messages\n";
}

# ********************************************************************
sub
readOneLineFileIntoString($)
{
    my ($fileName) = @_;
    my $fileIntoString;

    unless(open(INFILE, $fileName)) {
        print STDERR "$ProgName: open of file $fileName failed ($!)\n";
        return(undef);
    }
    $fileIntoString = <INFILE>;
    close(INFILE);

    chomp($fileIntoString);

    return($fileIntoString);
}

# ********************************************************************

sub
main()
{
    my ($help, $keep, $keepAll, $verbose, $inName, $optReturn, $NoNdstop);
    my ($useBroadonTools, $tmdInput);
    my ($base, $path, $suffix);
    my ($command, $rvlDevelArgs);
    my $exitVal = 1;            # Default to exit code for failure

    #print "$0: arguments are: @ARGV\n";

    $optReturn = GetOptions (
                                 "allKeep"              => \$keepAll,
                                 "broadon"              => \$useBroadonTools,
                                 "help"    	        => \$help,
                                 "keep"    	        => \$keep,
                                 "NoNdstop"             => \$NoNdstop,
                                 "tmdInput:s"           => \$tmdInput,
                                 "verbose" 	        => \$verbose,
                             );
    unless ($optReturn) {
        print STDERR "$0: Parsing command line options failed\n";
        usage();
        exit(1);
    }
    if (defined ($help)) {
        usage();
        exit(0);
    }

    if ($#ARGV == -1) {
	print STDERR "$0: Need name of input file\n";
	usage();
	exit(1);
    }
    elsif ($#ARGV > 0) {
	print STDERR "$0: Too many input file names\n";
	usage();
	exit(1);
    }
    else {
        $inName = $ARGV[0];
    }

    unless (defined ($verbose)) {
        $verbose = 0;
    }

    unless (defined ($keep)) {
        $keep = 0;
    }
    unless (defined ($keepAll)) {
        $keepAll = 0;
    }

    # Make the program name more readable when printed
    ($ProgName) = fileparse($0);

    ($base, $path, $suffix) = fileparse($inName, '\.dlf|\.elf');

    if (! defined ($NoNdstop)) {
        # Need to run ndstop, otherwise makervm (done below) doesn't work right.
        if (system ("ndstop") != 0) {
            print STDERR "$ProgName: Running 'ndstop' failed - exiting\n";
            goto end;
        }
    }

    # This command generates the new .rcf (image beginning) file,
    # and the input .dlf files
    $command = "makeRVTR -d $base.dlf -o $base.rvm -p $ENV{REVOLUTION_SDK_ROOT}";

    if ($verbose) {
        print "$ProgName: Generating file for the beginning of the image...\n";
        print "$ProgName: Command is: $command\n";
    }
    if (system($command) != 0) {
        print STDERR "$ProgName: Command '$command' failed - exiting\n";
        goto end;
    }

    $command = "makervm _0$base.dlf $base.part -x > /dev/null 2>&1";
    if ($verbose) {
        print
            "$ProgName: Now generating the unencrypted data for partition...\n";
        print "$ProgName: Command is: $command\n";
    }

    if (system($command) != 0) {
        print STDERR
            "$ProgName: Generation of partition's unencrypted data failed -exit\n";
        print STDERR "$ProgName: Command was: '$command'\n";
        goto end;
    }
    $rvlDevelArgs = readOneLineFileIntoString("arg1");
    if (! defined($rvlDevelArgs)) {
        print STDERR "$ProgName: reading contents of file 'arg1' failed\n";
        goto end;
    }

    if (defined($useBroadonTools)) {
        $command = "rvl_devel_partition.exe";
        if ($keep) {
            $command .= " -k";
        }
    }
    else {
        $command = "rvl_devel_partitionIRD.exe";
    }
    if (defined($tmdInput)) {
        $command .= " -T $tmdInput";
    }
    $command .= " $rvlDevelArgs";
    if ($verbose) {
        print "$ProgName: Now generating the encrypted partition...\n";
        print "$ProgName: Command is: $command\n";
    }
    if (system($command) != 0) {
        print STDERR "$ProgName: Generation of encrypted partition failed -exit\n";
        print STDERR "$ProgName: Command was: '$command'\n";
        goto end;
    }

    # Remove the unecrypted partition here if it is not being kept
    # in order to keep total disc usage down.
    unless ($keep || $keepAll) {
        unlink($base . ".part");
    }

    if ($verbose) {
        print "$ProgName: Now generating the disk image...\n";
    }
    $command = "makervm -gc1 _1" . $base . ".dlf $base.rvm > /dev/null 2>&1";
    if (system($command) != 0) {
        print STDERR "$ProgName: Generation of disk image failed -exit\n";
        print STDERR "$ProgName: Command was: '$command'\n";
        goto end;
    }
    unless ($keepAll) {
        unlink("arg0");
        unlink("arg1");
        unlink("arg2");
        unlink("_" . $base . ".rcf");
        unlink("_0" . $base . ".dlf");
        unlink("_1" . $base . ".dlf");
    }
    unless ($keep || $keepAll) {
        unlink($base . ".dev_part");
    }
    if ($verbose) {
        print "$ProgName: Disk image created.\n";
    }

    $exitVal = 0;

end:
    exit($exitVal);
} # main

main;
