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 [ ! -n "$SDIO_BRIDGE_TOOLS" ]
16then
17	if [ ! -n "$MION_BRIDGE_TOOLS" ]
18	then
19		echo "hoststop failed: Please install HostBridgeSetup and restart cafe.bat."
20		exit 1
21	fi
22fi
23
24HOSTSTOP_RVAL=0
25HOSTSTOP_RETRIES=0
26
27source getbridgetype
28
29HOSTSTOP_RESET_OPTION=-SMCResetHold
30if [ "${CAFE_HARDWARE:0:2}" == "ev" ]
31then
32	HOSTSTOP_RESET_OPTION=-hold
33	export MION_PWR_SEQ=FOFF1
34fi
35
36# make FOFF2 the default power sequence
37if [ "$BRIDGE_TYPE" == "Mion" ]
38then
39	checkbridgename $BRIDGE_CURRENT_NAME -ip $BRIDGE_CURRENT_IP_ADDRESS
40	if [ "$1" == "-makemine" ]
41	then
42		ToucanReset -stop
43		"$MION_BRIDGE_TOOLS/FSEmul.exe" -ip $BRIDGE_CURRENT_IP_ADDRESS -makemine
44	fi
45    if [ "$MION_PWR_SEQ" == "smcreset" ]
46    then
47	    "$MION_BRIDGE_TOOLS/MionButton.exe" -ip $BRIDGE_CURRENT_IP_ADDRESS -rsthold "$@"
48	    HOSTSTOP_RVAL=$?
49    else
50	    if [ "$MION_PWR_SEQ" == "FOFF1" ]
51	    then
52		    "$MION_BRIDGE_TOOLS/MionButton.exe" -ip $BRIDGE_CURRENT_IP_ADDRESS -forceOff1 "$@"
53		    HOSTSTOP_RVAL=$?
54		else
55			if [ "$MION_PWR_SEQ" == "G_SHUTDOWN" ]
56			then
57				"$MION_BRIDGE_TOOLS/MionButton.exe" -ip $BRIDGE_CURRENT_IP_ADDRESS -init_shutdown_w_FOFF2 60000 "$@"
58				HOSTSTOP_RVAL=$?
59			else
60				# Poor network conditions sometimes prevent MionButton from succeeding -
61				# so adding retry logic
62				HOSTSTOP_RVAL=1
63				HOSTSTOP_RETRIES=0
64				while [ $HOSTSTOP_RVAL -ne 0 -a $HOSTSTOP_RETRIES -lt 4 ]
65				do
66					if [ $HOSTSTOP_RETRIES -gt 0 ]
67					then
68						echo "WARNING: MionButton powerdown failed with err=$HOSTSTOP_RVAL, will retry shortly, count=$HOSTSTOP_RETRIES"
69						sleep 30
70					fi
71					"$MION_BRIDGE_TOOLS/MionButton.exe" -ip $BRIDGE_CURRENT_IP_ADDRESS -forceOff2_off_single_cgi "$@"
72					HOSTSTOP_RVAL=$?
73					((HOSTSTOP_RETRIES=HOSTSTOP_RETRIES+1))
74				done
75			fi
76		fi
77    fi
78
79
80	if [ $HOSTSTOP_RVAL -gt 2 ]
81    then
82        #
83        # We have a return from mionbutton that indicates the bridge may be in a bad state.
84        #
85        echo "Gathering MION logs via telnet for the mionbutton return of $HOSTSTOP_RVAL."
86        echo "----------------------------------------------------------------" >> "$TMP/${SESSION_PATH_PREFIX}cafestop_mion.log"
87        echo "----- MION telnet debug log @ `date` -----" >> "$TMP/${SESSION_PATH_PREFIX}cafestop_mion.log"
88        echo "----------------------------------------------------------------" >> "$TMP/${SESSION_PATH_PREFIX}cafestop_mion.log"
89        miontelnet.exe $BRIDGE_CURRENT_IP_ADDRESS >> "$TMP/${SESSION_PATH_PREFIX}cafestop_mion.log"
90        echo "    See $TMP/${SESSION_PATH_PREFIX}cafestop_mion.log for details."
91    fi
92else
93	nohup ToucanReset $HOSTSTOP_RESET_OPTION < /dev/null &> /dev/null
94	HOSTSTOP_RVAL=$?
95fi
96
97# stop FSEmul.exe
98ToucanReset -stop
99
100if [ $HOSTSTOP_RVAL -ne 0 ]
101then
102	if [ "$BRIDGE_TYPE" == "Mion" ]
103	then
104        if [ "$MION_PWR_SEQ" == "G_SHUTDOWN" -a $HOSTSTOP_RVAL -eq 1 ]
105        then
106		    echo "hoststop warning: Software shutdown failed."
107            HOSTSTOP_RVAL=0
108        else
109		    echo "hoststop failed: Could not stop cafe through '$BRIDGE_CURRENT_NAME'."
110			checkbridgename -s -ip $BRIDGE_CURRENT_IP_ADDRESS
111        fi
112	else
113		echo "hoststop failed: Could not stop cafe through Toucan."
114	fi
115fi
116
117