1#!/usr/bin/env bash
2###############################################################################
3#
4# Copyright (C) 2009-2014 Nintendo.  All rights reserved.
5#
6# These coded instructions, statements, and computer programs contain
7# proprietary information of Nintendo of America Inc. and/or Nintendo
8# Company Ltd., and are protected by Federal copyright law.  They may
9# not be disclosed to third parties or copied or duplicated in any form,
10# in whole or in part, without the prior written consent of Nintendo.::::
11#
12###############################################################################
13################################################################################
14# cafe_utils
15################################################################################
16source cafe_utils
17
18################################################################################
19# internal helper functions
20################################################################################
21check_mlc_sys_update_mixed()
22{
23    NAND_SRC_DIR=$CAFE_MLC_DIR/sys/update/nand
24    MIXED_DEST_DIR=$CAFE_MLC_DIR/sys/update/mixed
25    BL_DEST_DIR=$CAFE_MLC_DIR/sys/update/bootloader
26
27    if [ ! -e "$MIXED_DEST_DIR" ]
28    then
29        mkdir "$MIXED_DEST_DIR"
30    fi
31
32    if [ ! -e "$BL_DEST_DIR" ]
33    then
34        mkdir "$BL_DEST_DIR"
35    fi
36
37    # Copy the DUAL bootloader to it own directory
38    if [ ! -e "$BL_DEST_DIR/aaa-boot1-dual" ]; then
39        cp -r "$MIXED_DEST_DIR/aaa-boot1-dual" "$BL_DEST_DIR"
40    fi
41
42    # Copy the nand content(not hte bootloader) to mixed
43    for entry in "$NAND_SRC_DIR"/*
44    do
45        if [ -f "$entry" ]
46        then
47            if [ ! -e "$MIXED_DEST_DIR/$(basename "$entry")" ]
48            then
49                cp "$entry" "$MIXED_DEST_DIR/$(basename "$entry")"
50            fi
51        elif [ -d "$entry" ]
52        then
53            if [ ! -e "$MIXED_DEST_DIR/$(basename "$entry")" ]
54            then
55                if [ $(expr "$entry" : ".*zzz-boot1-.*") -eq 0 ]
56                then
57                    cp -R "$entry" "$MIXED_DEST_DIR/$(basename "$entry")"
58                fi
59            fi
60        fi
61    done
62}
63
64################################################################################
65# caferecover
66# This script attemps to recover a CAT_DEV to boot from PCFS.
67# It must not be ran from the bash command prompt.
68# Instead, invoke this using:
69#   source caferecover
70################################################################################
71if [ "$USE_CAFEX" == "1" ]
72then
73    # Set IFS to something that will not be in the args, so that BASH will pass the entire un-parsed string to CafeX
74    saveIFS="$IFS"
75    IFS=$'\n'
76    echo "Using CafeControl"
77    cafex.exe recover $@
78    rval=$?
79    IFS="$saveIFS"
80    exit $rval
81fi
82
83#------------------------------------------------------------------------------
84#---- initialization
85#------------------------------------------------------------------------------
86SCRIPT_EXE_TIMESTAMP=`date +%m_%d_%Y_%H%M%S`
87LOGDIR="$CAFE_TEMP/`basename "$0"`/${SCRIPT_EXE_TIMESTAMP}"
88echo `basename "$0"`
89echo log directory is $LOGDIR
90mkdir -p "$LOGDIR"
91OLDCONSOLE=$CAFE_CONSOLE
92CAFE_CONSOLE=toucan
93old_boot_mode=$CAFE_BOOT_MODE
94production_bootloader=0
95bootloader_only=0
96skip_update=0
97
98#Initialize_serial_port_and_directory cafestop
99
100
101#------------------------------------------------------------------------------
102#---- command line processing
103#------------------------------------------------------------------------------
104if [ $# -gt 0 ]
105then
106	CAFERECOVER_MORE_OPTIONS=1
107	while [ $CAFERECOVER_MORE_OPTIONS -eq 1 ]
108	do
109		if [ "$1" = "-production" ]
110		then
111            production_bootloader=1
112			shift
113		elif [ "$1" = "-bootloader" ]
114		then
115            bootloader_only=1
116			shift
117		else
118			CAFERECOVER_MORE_OPTIONS=0
119		fi
120	done
121fi
122
123if [ $bootloader_only -eq 1 -a $production_bootloader -eq 1 ]; then
124    echo `basename "$0"`": Invalid arguments: Can't specify both -production and -bootloader options!"
125    exit 1
126fi
127
128
129#------------------------------------------------------------------------------
130#---- boot mode detection
131#------------------------------------------------------------------------------
132source hostcheckversion
133hb_ver_flat=$(compute_flat_version $CAFERUN_HOSTBRIDGE_VERSION)
134
135if [ $hb_ver_flat -lt $(compute_flat_version 3.2.4.8) ]
136then
137    echo "Using BOOT mode $CAFE_BOOT_MODE"
138else
139    cafestop -hard
140    sleep 1
141
142    CAFE_BOOT_MODE=`FSEmul -ip $BRIDGE_CURRENT_IP_ADDRESS -modedetect`
143    rval=$?
144    if [ $rval -ne 0 ]
145    then
146        echo "Unable to determine boot mode!  rval=$rval"
147        exit $rval;
148    fi
149
150    echo "Detected BOOT Mode: $CAFE_BOOT_MODE"
151    if [ "$CAFE_BOOT_MODE" != "NAND" ]
152    then
153        CAFE_BOOT_MODE=PCFS
154    fi
155fi
156
157export CAFE_BOOT_MODE
158
159#------------------------------------------------------------------------------
160#---- Boot the DEVKIT into recovery mode
161#------------------------------------------------------------------------------
162if [ "$CAFE_BOOT_MODE" == "NAND" ]
163then
164    #------------------------------------------------------------------------------
165    #---- cafeon
166    #------------------------------------------------------------------------------
167    echo Executing cafeon"..."
168    set -e
169    if [ $hb_ver_flat -lt $(compute_flat_version 3.2.4.1) ]
170    then
171        cafeon 1> "$LOGDIR"/cafeon.txt 2>&1
172    else
173        cafeon -e nomodecheck 1> "$LOGDIR"/cafeon.txt 2>&1
174    fi
175    if [ -e "$TMP/${SESSION_PATH_PREFIX}cafestop_mion.log" ]; then
176        mv "$TMP/${SESSION_PATH_PREFIX}cafestop_mion.log" "${LOGDIR}/cafeon_mion.log"
177    fi
178    set +e
179    Monitor_For_Output -k "source -p -q /vol/content" $MONITOR_DEFAULT_TIMEOUT "$LOGDIR"
180    if [ $? -ne 0 ]; then
181        echo `basename $0`: monitor failed waiting for cafeon success message.
182        exit 1
183    fi
184
185    #------------------------------------------------------------------------------
186    #---- devkitmsg -recover (enter recovery mode)
187    #------------------------------------------------------------------------------
188    echo Executing devkitmsg recover -v"..."
189    set -e
190    devkitmsg recover -v -p $SESSION_LAUNCH_CTRL_PORT
191    set +e
192    Monitor_For_Output -K "WUD_BCMFWCheck" $MONITOR_DEFAULT_TIMEOUT "$LOGDIR" #note: need -K here, not -k
193    if [ $? -ne 0 ]; then
194        echo `basename "$0"`: monitor failed waiting for devkitmsg recover success message.
195        exit 1
196    fi
197
198    # Check the $CAFE_ROOT/data/mlc/sys/update/mixed folder
199    check_mlc_sys_update_mixed
200
201    # Install the DUAL bootloader as fail-safe
202    echo Executing devkitmsg \'update /vol/storage_hfiomlc01/sys/update/bootloader\' -v"..."
203    set -e
204    devkitmsg 'update /vol/storage_hfiomlc01/sys/update/bootloader' -v -p $SESSION_LAUNCH_CTRL_PORT
205    set +e
206    Monitor_For_Output -k "Update Done, err 0" $MONITOR_DEFAULT_TIMEOUT "$LOGDIR"
207    if [ $? -ne 0 ]; then
208        echo `basename "$0"`: monitor failed waiting for devkitmsg 1st update success message.
209        CAFE_CONSOLE=$OLDCONSOLE
210        exit 1
211    fi
212
213    # Rest of the update from PCFS boot mode with the DUAL bootlaoder
214    CAFE_BOOT_MODE="PCFS"
215
216    if [ $bootloader_only -eq 1 ]; then
217        # We have already updated the bootloader so suppress doing it again if it was
218        #   the only thing we are requested to do.
219        skip_update=1
220    fi
221
222    cafestop -hard
223    # export CAFE_RUN_RUNNING=0
224fi
225
226if [ $skip_update -eq 0 ]
227then
228    #------------------------------------------------------------------------------
229    #---- cafeon (with recovery image)
230    #------------------------------------------------------------------------------
231    echo Executing cafeon"..."
232    set -e
233    if [ $hb_ver_flat -lt $(compute_flat_version 3.2.4.1) ]
234    then
235        cafeon -recover 1> "$LOGDIR"/caferecover.txt 2>&1
236    else
237        cafeon -recover -e nomodecheck 1> "$LOGDIR"/caferecover.txt 2>&1
238    fi
239    if [ -e "$TMP/${SESSION_PATH_PREFIX}cafestop_mion.log" ]; then
240        mv "$TMP/${SESSION_PATH_PREFIX}cafestop_mion.log" "${LOGDIR}/cafeon_mion.log"
241    fi
242    set +e
243    Monitor_For_Output -K "WUD_BCMFWCheck" $MONITOR_DEFAULT_TIMEOUT "$LOGDIR" #note: need -K here, not -k
244    if [ $? -ne 0 ]; then
245        echo `basename "$0"`: monitor failed waiting for recovery boot success message.
246        CAFE_CONSOLE=$OLDCONSOLE
247        exit 1
248    fi
249
250    #------------------------------------------------------------------------------
251    #---- Update/Reflash the DEVKIT
252    #------------------------------------------------------------------------------
253    # Check the $CAFE_ROOT/data/mlc/sys/update/mixed folder
254    check_mlc_sys_update_mixed
255
256    if [ $bootloader_only -eq 1 ]
257    then
258        echo Executing devkitmsg \'update /vol/storage_hfiomlc01/sys/update/bootloader\' -v"..."
259        set -e
260        devkitmsg 'update /vol/storage_hfiomlc01/sys/update/bootloader' -v -p $SESSION_LAUNCH_CTRL_PORT
261        set +e
262        Monitor_For_Output -k "Update Done, err 0" $MONITOR_DEFAULT_TIMEOUT "$LOGDIR"
263        if [ $? -ne 0 ]; then
264            echo `basename "$0"`: monitor failed waiting for devkitmsg 1st update success message.
265            CAFE_CONSOLE=$OLDCONSOLE
266            exit 1
267        fi
268    else
269        if [ $production_bootloader -eq 1 ]
270        then
271            echo Executing devkitmsg \'reflash /vol/storage_hfiomlc01/sys/update/nand\' -v"..."
272            set -e
273            devkitmsg 'reflash /vol/storage_hfiomlc01/sys/update/nand' -v -p $SESSION_LAUNCH_CTRL_PORT
274            set +e
275        else
276            echo Executing devkitmsg \'reflash /vol/storage_hfiomlc01/sys/update/mixed\' -v"..."
277            set -e
278            devkitmsg 'reflash /vol/storage_hfiomlc01/sys/update/mixed' -v -p $SESSION_LAUNCH_CTRL_PORT
279            set +e
280        fi
281        Monitor_For_Output -k "System update was successful" $MONITOR_DEFAULT_TIMEOUT "$LOGDIR"
282        if [ $? -ne 0 ]; then
283            echo `basename "$0"`: monitor failed waiting for devkitmsg 1st update success message.
284            CAFE_CONSOLE=$OLDCONSOLE
285            exit 1
286        fi
287    fi
288
289    #------------------------------------------------------------------------------
290    #---- enable self refresh
291    #------------------------------------------------------------------------------
292    if [ "$CAFE_TEST_SELF_REFRESH" == "1" ]
293    then
294        devkitmsg 'standby_en 1' -v -p $SESSION_LAUNCH_CTRL_PORT
295    fi
296
297    #------------------------------------------------------------------------------
298    #---- cafestop
299    #------------------------------------------------------------------------------
300    echo Executing cafestop"..."
301    source cafestop 1> "$LOGDIR"/cafestop1.txt 2>&1
302    if [ -e "$TMP/${SESSION_PATH_PREFIX}cafestop_mion.log" ]; then
303        mv "$TMP/${SESSION_PATH_PREFIX}cafestop_mion.log" "${LOGDIR}/cafestop1_mion.log"
304    fi
305    if [ $CAFESTOP_STATUS -ne 0 ]; then
306        echo `basename "$0"`: cafestop failure.
307        CAFE_CONSOLE=$OLDCONSOLE
308        exit 1
309    fi
310fi
311
312# Update the MION parameter space for the boot mode
313if [ $production_bootloader -eq 1 -o "$old_boot_mode" == "NAND" ]
314then
315    # NAND boot mode
316    "$CAFE_ROOT\system\bin\tool\mionps" $BRIDGE_CURRENT_IP_ADDRESS 2 -s 1
317else
318    # PCFS boot mode
319    "$CAFE_ROOT\system\bin\tool\mionps" $BRIDGE_CURRENT_IP_ADDRESS 2 -s 2
320fi
321
322
323#------------------------------------------------------------------------------
324#---- success!
325#------------------------------------------------------------------------------
326echo Removing log directory $LOGDIR"..."
327rm -rf "$LOGDIR"
328echo `basename "$0"` complete. Exiting with code 0.
329
330if [ $production_bootloader -eq 1 -a "$old_boot_mode" != "NAND" ]
331then
332    echo
333    echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
334    echo "!!                                                           !!"
335    echo "!!  Boot Loader/Boot Mode mis-match detected!                !!"
336    echo "!!                                                           !!"
337    echo "!!  Please export CAFE_BOOT_MODE=NAND for proper operation!  !!"
338    echo "!!                                                           !!"
339    echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
340fi
341
342CAFE_CONSOLE=$OLDCONSOLE
343exit $rval
344