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# setbootmode
17# This script enables a CAT_DEV to boot from PCFS or NAND. It must not be
18# executed directly from the bash command prompt. Instead, invoke this using:
19#   source setbootmode PCFS|NAND
20################################################################################
21reflash=0
22reflash_arg=
23mixed=1
24production_arg=
25quick=0
26if [ $# -gt 0 ]
27then
28	_MORE_OPTIONS=1
29	while [ $_MORE_OPTIONS -eq 1 ]
30	do
31		if [ "$1" = "-noreflash" ]
32		then
33			reflash=0;
34			reflash_arg="$1"
35			shift
36		elif [ "$1" = "-reflash" ]
37		then
38			reflash=1;
39			reflash_arg="$1"
40			shift
41		elif [ "$1" = "-production" -o "$1" = "-P" ]
42		then
43			mixed=0;
44			production_arg="$1"
45			shift
46		elif [ "$1" = "-quick" -o "$1" = "-Q" ]
47		then
48			quick=1;
49			shift
50		else
51			_MORE_OPTIONS=0
52		fi
53	done
54fi
55target=$1
56
57
58status_str="FAIL"
59source cafe_utils
60
61if [ "${SDK_MAJ_VER}.${SDK_MIN_VER}.${SDK_MIC_VER}" != "${SDK_VER//0/}" ]; then
62    "$CAFE_ROOT\system\bin\tool\mionps" $BRIDGE_CURRENT_IP_ADDRESS 3 -s ${SDK_VER:0:1}
63    "$CAFE_ROOT\system\bin\tool\mionps" $BRIDGE_CURRENT_IP_ADDRESS 4 -s ${SDK_VER:2:2}
64    "$CAFE_ROOT\system\bin\tool\mionps" $BRIDGE_CURRENT_IP_ADDRESS 5 -s ${SDK_VER:5:2}
65fi
66
67
68Bash_sourced_check 1 `basename "$0"` "`basename "$0"` PCFS|NAND"
69rval=$?
70
71if [ $rval -eq 0 ]; then
72    if [ $quick -eq 1 ]
73    then
74        if [ "$USE_CAFEX" = "1" ]; then
75            cafex.exe setbootmode -quick $target
76            rval=$?
77        else
78            if [ "$target" != "PCFS" -a "$target" != "NAND" ]; then
79                echo "Error: $target is not a valid boot mode!  Please use NAND or PCFS."
80                return 1
81            fi
82
83            if [ ! -z "$production_arg" ]; then
84                echo "Error: Can't specify both -quick and -production options!  Please choose one or the other."
85                return 1
86            fi
87
88            source hostcheckversion
89            hb_ver_flat=$(compute_flat_version $CAFERUN_HOSTBRIDGE_VERSION)
90
91            if [ $hb_ver_flat -lt $(compute_flat_version 3.2.4.8) ]
92            then
93                echo "Error: This version of the Host Bridge doesn't support boot mode detection!"
94                echo "       Please upgrade it to at least version 3.2.4.8 and try again."
95
96                return 1
97            else
98                # See if the DUAL bootloader is installed
99                cafestop -hard
100                sleep 1
101
102                CAFE_BOOT_MODE=`FSEmul -ip $BRIDGE_CURRENT_IP_ADDRESS -modedetect`
103                rval=$?
104                if [ $rval -ne 0 ]
105                then
106                    echo "Error: Unable to determine boot mode!  rval=$rval"
107                    return 1;
108                fi
109
110                cafestop -hard
111
112                echo "Detected BOOT Mode: $CAFE_BOOT_MODE"
113                if [ "${CAFE_BOOT_MODE:0:4}" != "DUAL" ]
114                then
115                    echo "Error: DUAL bootloader is not installed!.  Please re-run ${0} not using the quick option."
116                    return 1
117                fi
118
119                # Call mionps
120                if [ "$target" = "NAND" ]; then
121                    "$CAFE_ROOT\system\bin\tool\mionps" $BRIDGE_CURRENT_IP_ADDRESS 2 -s 1
122                else
123                    "$CAFE_ROOT\system\bin\tool\mionps" $BRIDGE_CURRENT_IP_ADDRESS 2 -s 2
124                fi
125
126                rval=0
127            fi
128        fi
129
130        if [ $rval -eq 0 ]; then
131            export CAFE_BOOT_MODE=$target
132            status_str="PASS"
133        fi
134    else
135        case "$target" in
136            "PCFS")
137                if [ "$USE_CAFEX" == "1" ]; then
138                    cafex.exe setbootmode $reflash_arg PCFS
139                else
140                    if [ $mixed -eq 1 ]; then
141                        echo Executing caferecover for mixed PCFS"..."
142                        caferecover $reflash_arg
143                    else
144                        echo "Executing cafe_nand2pcfs ..."
145                        cafe_nand2pcfs 0
146                    fi
147                fi
148                rval=$?
149                if [ $rval == 0 ]; then
150                    "$CAFE_ROOT\system\bin\tool\mionps" $BRIDGE_CURRENT_IP_ADDRESS 2 -s 2
151                    export CAFE_BOOT_MODE=PCFS
152                    echo CAFE_BOOT_MODE=$CAFE_BOOT_MODE
153                    status_str="PASS"
154                fi
155                ;;
156            "NAND")
157                if [ "$USE_CAFEX" = "1" ]; then
158                        cafex.exe setbootmode $reflash_arg $production_arg NAND
159                else
160                    if [ $mixed -eq 1 ]; then
161                        echo Executing caferecover for mixed NAND"..."
162                        caferecover $reflash_arg
163                    else
164                        echo Executing cafe_pcfs2nand"..."
165                        cafe_pcfs2nand $reflash 0
166                    fi
167                fi
168                rval=$?
169                if [ $rval == 0 ]; then
170                    "$CAFE_ROOT\system\bin\tool\mionps" $BRIDGE_CURRENT_IP_ADDRESS 2 -s 1
171                    export CAFE_BOOT_MODE=NAND
172                    echo CAFE_BOOT_MODE=$CAFE_BOOT_MODE
173                    status_str="PASS"
174                fi
175                ;;
176            *)
177                rval=1
178                echo `basename "$BASH_SOURCE"` error: must be executed using \"source `basename "$BASH_SOURCE"` PCFS\" or \"source `basename "$BASH_SOURCE"` [-production] NAND\". >/dev/stderr
179                ;;
180        esac
181    fi
182else
183        echo `basename "$BASH_SOURCE"` error: must be executed using \"source `basename "$BASH_SOURCE"` PCFS\" or \"source `basename "$BASH_SOURCE"` [-production] NAND\". >/dev/stderr
184fi
185
186echo `basename "$BASH_SOURCE"`: $status_str.
187unset SCRIPT_IS_SOURCED
188return $rval
189