1#!/usr/bin/env bash
2
3###############################################################################
4#
5# Copyright (C) 2009-2013 Nintendo.  All rights reserved.
6#
7# These coded instructions, statements, and computer programs contain
8# proprietary information of Nintendo of America Inc. and/or Nintendo
9# Company Ltd., and are protected by Federal copyright law.  They may
10# not be disclosed to third parties or copied or duplicated in any form,
11# in whole or in part, without the prior written consent of Nintendo.
12#
13###############################################################################
14
15if [ "$USE_CAFEX" == "1" ]
16then
17    # Set IFS to something that will not be in the args, so that BASH will pass the entire un-parsed string to CafeX
18    saveIFS="$IFS"
19    IFS=$'\n'
20    echo "Using CafeControl"
21    cafex.exe headlessrun $@
22    rval=$?
23    IFS="$saveIFS"
24    exit $rval
25fi
26
27###########################################################################################
28#
29# Globals
30#
31###########################################################################################
32
33UPLOAD_BANK=10
34HLRUN_OUT=$CAFE_TEMP/headlessrun
35HLRUN_TMP=$CAFE_TEMP/headlessrun/tmp
36RUN_DEVMENU=1
37TITLE_WAIT=15
38DEBUG_FLAGS=
39BYPASS_SETUP=0
40RVAL=0
41
42source monitor_return_codes
43
44
45###########################################################################################
46#
47# Functions
48#
49###########################################################################################
50
51usage ()
52{
53    cat<<EOF
54    Usage: $(basename $0) [options] <app>.rpx "[arg1]" "[arg2]" ... "[argN]"
55    
56    options:
57      -b        : Use DEBUG version of the OS
58      -n        : Don't start the title after upload.
59      -m        : Bypass mastering setup.
60      -w <time> : Time (in seconds) to wait for title to boot during setup.
61                  Default time is 15 seconds.
62      -h <bank> : Specify bank to run from.  Default is bank 10.
63EOF
64}
65
66checkargs ()
67{
68    if [ -z "$1" ]
69    then
70        usage
71        exit 0
72    fi
73
74    while getopts :bnmh:w: OPT
75    do
76        case $OPT in
77            b ) DEBUG_FLAGS="-b";;      # Use the DEBUG version of the OS
78            h ) UPLOAD_BANK=$OPTARG;;   # Specify the upload bank
79            n ) RUN_DEVMENU=0;;         # Don't start devmenu after upload
80            m ) BYPASS_SETUP=1;;        # Bypass mastering setup
81            w ) TITLE_WAIT=$OPTARG;;    # Time to wait for title boot
82
83            \:) echo "$(basename $0): missing arguments on command line!"
84                usage
85                exit 1;;
86            \?) echo "$(basename $0): bad argument -$OPTARG on command line!"
87                usage
88                exit 1;;
89        esac
90    done
91    shift $(($OPTIND-1))
92
93    RPX_PATH=$1
94    if [ ! -f $RPX_PATH ]
95    then
96        echo "$(basename $0): RPX file '$RPX_PATH' does not exist!  Please check the path."
97        exit 1
98    fi
99
100    shift
101    RPX_ARGS=$*
102}
103
104update_bootloader()
105{
106    source hostcheckversion
107    hb_ver_flat=$(compute_flat_version $CAFERUN_HOSTBRIDGE_VERSION)
108
109    if [ $hb_ver_flat -lt $(compute_flat_version 3.2.4.8) ]
110    then
111        echo "Using BOOT mode $CAFE_BOOT_MODE"
112    else
113        echo "Checking for DUAL bootloader..."
114
115        cafestop -hard
116        sleep 1
117
118        BOOT_MODE=`FSEmul -ip $BRIDGE_CURRENT_IP_ADDRESS -modedetect`
119        rval=$?
120        if [ $rval -ne 0 ]
121        then
122            echo "Unable to determine boot mode!  rval=$rval"
123            exit $rval;
124        fi
125
126        echo "Detected BOOT Mode: $BOOT_MODE"
127    fi
128
129    if [ "${BOOT_MODE:0:4}" != "DUAL" ]
130    then
131        # Install the DUAL bootloader
132        caferecover
133    fi
134
135    export CAFE_BOOT_MODE=PCFS
136}
137
138prepare_rpx_for_mastering ()
139{
140    # Disable the built in capturing of output
141    OLD_CAFE_CONSOLE=$CAFE_CONSOLE
142    export CAFE_CONSOLE=notoucan
143
144    # Run the RPX to create the dependent files needed for mastering
145    echo "$(basename $0): calling cafediscrun -e em -e sata $DEBUG_FLAGS $RPX_PATH $RPX_ARGS"
146    cafediscrun -e em -e sata $DEBUG_FLAGS $RPX_PATH $RPX_ARGS
147
148    # Wait for MCP to come up
149    monitor -k "[+-* DK is ready for console input *-+]"
150    RVAL=$?
151    if [ $RVAL -eq $MON_RESULT_PASS ]
152    then
153        echo "$(basename $0): Master image prep for $RPX_PATH succeeded."
154    else
155        echo "$(basename $0): Master image prep for $RPX_PATH failed with code $RVAL!"
156        exit 3
157    fi
158
159    # Give the title time to boot up in case it writes files
160    # needed for mastering
161    echo "$(basename $0): waiting $TITLE_WAIT seconds for title to boot..."
162    sleep $TITLE_WAIT
163
164    export CAFE_CONSOLE=$OLD_CAFE_CONSOLE
165
166    # Unlock any files in use so we can master
167    cafestop
168    sleep 2
169}
170
171master_image ()
172{
173    # Create the working and temp folders
174    mkdir -p $HLRUN_TMP
175
176    # Create the master image for the last ran RPX
177    echo "$(basename $0): calling makecfmaster.sh -o $HLRUN_OUT/title -w $HLRUN_TMP"
178    makecfmaster.sh -o $HLRUN_OUT/title -w $HLRUN_TMP
179    RVAL=$?
180    if [ $RVAL -eq 0 ]
181    then
182        echo "$(basename $0): Mastering image for $RPX_PATH succeeded."
183    else
184        echo "$(basename $0): Mastering image for $RPX_PATH failed with code $RVAL!"
185        exit 4
186    fi
187}
188
189upload_image ()
190{
191    echo "$(basename $0): calling uploadimg.sh $UPLOAD_BANK $HLRUN_OUT/title.wumad"
192    uploadimg.sh $UPLOAD_BANK $HLRUN_OUT/title.wumad
193    RVAL=$?
194    if [ $RVAL -eq 0 ]
195    then
196        echo "$(basename $0): uploadimg.sh succeeded."
197    else
198        echo "$(basename $0): uploadimg.sh failed to upload with code $RVAL!"
199        exit 5
200    fi
201}
202
203run_image ()
204{
205    echo "$(basename $0): calling cafediscrun $DEBUG_FLAGS -e h:$UPLOAD_BANK -e nopcfs $RPX_PATH"
206    cafediscrun $DEBUG_FLAGS -e h:$UPLOAD_BANK -e nopcfs $RPX_PATH
207    RVAL=$?
208    if [ $RVAL -eq 0 ]
209    then
210        echo "$(basename $0): cafediscrun succeeded to launch title in bank $UPLOAD_BANK."
211    else
212        echo "$(basename $0): cafediscrun failed to launch title in bank $UPLOAD_BANK with code $RVAL!"
213        exit 6
214    fi
215}
216
217set_hreader_mode ()
218{
219    mionurl $BRIDGE_CURRENT_IP_ADDRESS /setup.cgi id_27=$UPLOAD_BANK id_32=1 op=0
220    RVAL=$?
221    if [ $RVAL -eq 0 ]
222    then
223        echo "$(basename $0): mionurl succeeded to set HREADER mode."
224    else
225        echo "$(basename $0): mionurl failed to set HREADER mode with code $RVAL!"
226        exit 7
227    fi
228
229    # Reboot MION and wait for completion so Bank adn HREADER mode can take effect
230    echo "$(basename $0): Rebooting MION..."
231    miontelnet -reboot
232}
233
234
235###########################################################################################
236#
237# Main
238#
239###########################################################################################
240
241OLD_CAFE_BOOT_MODE=$CAFE_BOOT_MODE
242
243checkargs $*
244
245update_bootloader
246
247if [ $BYPASS_SETUP -eq 0 ]
248then
249    prepare_rpx_for_mastering
250fi
251
252master_image
253
254upload_image
255
256set_hreader_mode
257
258if [ $RUN_DEVMENU -eq 1 ]
259then
260    export CAFE_BOOT_MODE=NAND
261    run_image
262fi
263