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
15################################################################################
16# caferecover
17# This script updates a DEV-KIT to boot using the DUAL bootloader
18################################################################################
19if [ "$USE_CAFEX" == "1" ]
20then
21    # Set IFS to something that will not be in the args, so that BASH will pass the entire un-parsed string to CafeX
22    saveIFS="$IFS"
23    IFS=$'\n'
24    echo "Using CafeControl"
25    cafex.exe cleardata $@
26    rval=$?
27    IFS="$saveIFS"
28    exit $rval
29fi
30
31#------------------------------------------------------------------------------
32#---- initialization
33#------------------------------------------------------------------------------
34source cafe_utils
35
36SCRIPT_EXE_TIMESTAMP=`date +%m_%d_%Y_%H%M%S`
37LOGDIR="$CAFE_TEMP/`basename "$0"`/${SCRIPT_EXE_TIMESTAMP}"
38echo `basename "$0"`
39echo log directory is $LOGDIR
40mkdir -p "$LOGDIR"
41CAFE_CONSOLE=toucan
42
43#------------------------------------------------------------------------------
44#---- cafeon
45#------------------------------------------------------------------------------
46source hostcheckversion
47hb_ver_flat=$(compute_flat_version $CAFERUN_HOSTBRIDGE_VERSION)
48
49echo Executing cafeon"..."
50set -e
51if [ $hb_ver_flat -lt $(compute_flat_version 3.2.4.1) ]
52then
53    cafeon 1> "$LOGDIR"/cafeon.txt 2>&1
54else
55    cafeon -e nomodecheck 1> "$LOGDIR"/cafeon.txt 2>&1
56fi
57if [ -e "$TMP/${SESSION_PATH_PREFIX}cafestop_mion.log" ]; then
58    mv "$TMP/${SESSION_PATH_PREFIX}cafestop_mion.log" "${LOGDIR}/cafeon_mion.log"
59fi
60set +e
61Monitor_For_Output -k "source -p -q /vol/content/" $MONITOR_DEFAULT_TIMEOUT "$LOGDIR"
62if [ $? -ne 0 ]; then
63    echo `basename "$0"`: monitor failed waiting for cafeon success message.
64    exit 1
65fi
66
67#------------------------------------------------------------------------------
68#---- recover command
69#------------------------------------------------------------------------------
70echo Executing devkitmsg recover -v"..."
71set -e
72devkitmsg recover -v -p $SESSION_LAUNCH_CTRL_PORT
73set +e
74Monitor_For_Output -K "WUD_BCMFWCheck" $MONITOR_DEFAULT_TIMEOUT "$LOGDIR" #note: need -K here, not -k
75if [ $? -ne 0 ]; then
76    echo `basename "$0"`: monitor failed waiting for devkitmsg recover success message.
77    exit 1
78fi
79
80#------------------------------------------------------------------------------
81#---- clr_usrdata command
82#------------------------------------------------------------------------------
83echo Executing devkitmsg clr_usrdata -v"..."
84set -e
85devkitmsg clr_usrdata -v -p $SESSION_LAUNCH_CTRL_PORT
86set +e
87Monitor_For_Output -k "Done, err 0" $MONITOR_DEFAULT_TIMEOUT "$LOGDIR"
88if [ $? -ne 0 ]; then
89    echo `basename "$0"`: monitor failed waiting for devkitmsg clr_usrdata success message.
90    exit 1
91fi
92
93#------------------------------------------------------------------------------
94#---- cafestop
95#------------------------------------------------------------------------------
96echo Executing cafestop"..."
97source cafestop 1> "$LOGDIR"/cafestop1.txt 2>&1
98if [ -e "$TMP/${SESSION_PATH_PREFIX}cafestop_mion.log" ]; then
99    mv "$TMP/${SESSION_PATH_PREFIX}cafestop_mion.log" "${LOGDIR}/cafestop1_mion.log"
100fi
101if [ $CAFESTOP_STATUS -ne 0 ]; then
102    echo `basename "$0"`: cafestop failure.
103    exit 1
104fi
105
106#------------------------------------------------------------------------------
107#---- success!
108#------------------------------------------------------------------------------
109echo Removing log directory $LOGDIR"..."
110rm -rf "$LOGDIR"
111echo `basename "$0"` complete. Exiting with code 0.
112exit $rval
113