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# this script prepares the hard drive to run a HDD-based test,
16# only in the event that the environment is set up with a HDD number.
17# The script will create a HD image and burn that to disk
18
19# arguments to this script:
20#  $1:  hard disk slot - empty means no hard disk
21#  $2:  name of the RPX file to generate image from
22#  $3:  IP address of MION to upload image to. Defaults to configured bridge
23#  $4:  temporary work directory to use - optional, will use $TEMP if not specified
24
25
26IMG_UPLOAD_WINDOWS_PATH=
27USE_ARCHIVE_IMAGE=0
28IMG_UPLOAD_HDD_BANK=$1
29if [ "$IMG_UPLOAD_HDD_BANK" == "-a" ]; then
30    shift
31    USE_ARCHIVE_IMAGE=1
32    IMG_UPLOAD_HDD_BANK=$1
33fi
34
35IMG_UPLOAD_MASTER_FILE=$2
36IMG_UPLOAD_IP_ADDR=$3
37IMG_UPLOAD_WORK_DIR=$4
38
39#NOTE: Autotest is looking for this to determine the
40#      condition of a test that's skipped because of no HDD available
41IMG_UPLOAD_FORCE_TEST_SKIP=25
42DEFAULT_TIMEOUT_SECS=3600
43
44#Versions of the uploader above this one are able to auto-concatenate the header file
45MIN_AUTOCONCAT_UPLOADER=702
46MIN_WUMAD_SUPPORT_UPLOADER=705
47
48#Local variables used in detecting version
49IMGUPLOADERV=
50IMGUPLOADFLAT=0
51vpart=0
52IMGVF1=
53
54
55
56###############################################################################
57# create_directory_if_noexist
58###############################################################################
59create_directory_if_noexist()
60{
61    if [ ! -d "$1" ]
62    then
63        echo "$(basename "$0"): Creating dir: $1"
64        mkdir -m 0777 -p "$1"
65    fi
66}
67
68
69###############################################################################
70# usage
71###############################################################################
72usage ()
73{
74    cat <<EOF
75    Usage: $(basename $0) [-a] <hdd_bank_num> <master_image_file> [mion_ip_address] [temp_folder]
76          -a = Treat <master_image_file> file as master archive
77
78EOF
79
80}
81
82
83###############################################################################
84# main
85###############################################################################
86
87source getbridgetype
88
89if [ -z "$IMG_UPLOAD_HDD_BANK" ]; then
90   usage
91   exit 1
92fi
93
94
95if [ -z "$IMG_UPLOAD_MASTER_FILE" ]; then
96   usage
97   exit 1
98fi
99
100if [ -z "$IMG_UPLOAD_IP_ADDR" ]; then
101    IMG_UPLOAD_IP_ADDR=$BRIDGE_CURRENT_IP_ADDRESS
102fi
103
104if [ -z "$IMG_UPLOAD_WORK_DIR" ]; then
105    if [ -z "$CAFE_TEMP" ]; then
106       echo "ERROR: unable to find temporary directory for upload"
107       exit 1
108    fi
109    IMG_UPLOAD_WORK_DIR=$CAFE_TEMP
110fi
111
112if [ "$BRIDGE_TYPE" == "Toucan" ]
113then
114    echo "Skipping SATA HDD test with non-MION image"
115    echo "PASS or FAIL"
116    exit $IMG_UPLOAD_FORCE_TEST_SKIP
117fi
118
119if [ ! "$BRIDGE_TYPE" == "Mion" ]
120then
121    echo "Skipping SATA HDD test with non-MION image"
122    echo "PASS or FAIL"
123    exit $IMG_UPLOAD_FORCE_TEST_SKIP
124fi
125
126create_directory_if_noexist "$IMG_UPLOAD_WORK_DIR/${SESSION_PATH_PREFIX}hddimage"
127
128IMGUPLOADERV=`ImageUploader.exe | grep "Image Uploader by"`
129
130if [ -z "$IMGUPLOADERV" ]; then
131    echo "Working around cygwin backtick output limitation"
132    # we really didn't want to create any files here -
133    # but certain versions of Cygwin don't capture the text that
134    # comes out of ImageUploader.exe for some strange unknown reason -
135    # but they are happy to capture the results of 'cat'
136    ImageUploader.exe>"$IMG_UPLOAD_WORK_DIR/${SESSION_PATH_PREFIX}hddimage/imguploaderv.txt"
137    IMGUPLOADERV=`cat "$IMG_UPLOAD_WORK_DIR/${SESSION_PATH_PREFIX}hddimage/imguploaderv.txt" | grep "Image Uploader by"`
138fi
139
140if [[ $IMGUPLOADERV == *ver.* ]]
141then
142    IMGUPLOADERV=${IMGUPLOADERV#*.}
143    IMGUPLOADFLAT=0
144    vpart=0
145    while [ $vpart -lt 4 ]
146    do
147        # delete all after the first dot
148        IMGVF1=${IMGUPLOADERV%%.*}
149        IMGUPLOADERV=${IMGUPLOADERV#*.}
150        IMGVF1=${IMGVF1//[^0-9]/}
151        ((IMGUPLOADFLAT=IMGUPLOADFLAT*100+IMGVF1))
152        ((vpart=vpart+1))
153    done
154    echo Detected ImageUploader flatversion to be $IMGUPLOADFLAT
155else
156    set IMGUPLOADFLAT=$MIN_WUMAD_SUPPORT_UPLOADER
157    echo "WARNING: Unable to query ImageUploader version"
158fi
159
160
161
162
163
164
165############
166# all checks done, prepare for action
167
168
169if [[ ! $IMGUPLOADFLAT -lt $MIN_AUTOCONCAT_UPLOADER ]]
170then
171    IMG_FILE_WINDOWS_PATH=`cygpath --windows $IMG_UPLOAD_MASTER_FILE`
172    if [ $USE_ARCHIVE_IMAGE = 0 ]; then
173        echo "Performing direct upload of header"
174        IMG_HEADER_WINDOWS_PATH=`cygpath --windows ${0%/*}/mh_cafe.bin`
175
176        ImageUploader.exe -ip $IMG_UPLOAD_IP_ADDR -upload $IMG_UPLOAD_HDD_BANK "-h:$IMG_HEADER_WINDOWS_PATH" "$IMG_FILE_WINDOWS_PATH" -timeout $DEFAULT_TIMEOUT_SECS
177        ret=$?
178        if [ ! $ret = 0 ]; then
179           echo "Unable to upload image to disk"
180           exit 1
181        fi
182    else
183
184        if [[ $IMGUPLOADFLAT -lt $MIN_WUMAD_SUPPORT_UPLOADER ]]; then
185           echo "ERROR: The current version of ImageUploader does not support Master archives. Need $MIN_WUMAD_SUPPORT_UPLOADER flat version."
186           exit 1
187        fi
188
189        rm -rf "$IMG_UPLOAD_WORK_DIR/${SESSION_PATH_PREFIX}hddimage"
190        create_directory_if_noexist "$IMG_UPLOAD_WORK_DIR/${SESSION_PATH_PREFIX}hddimage"
191        IMG_WINDOWS_TMP_ARCHIVE_DIR=`cygpath --windows $IMG_UPLOAD_WORK_DIR/${SESSION_PATH_PREFIX}hddimage`
192
193        echo  ImageUploader.exe -ip $IMG_UPLOAD_IP_ADDR -upload $IMG_UPLOAD_HDD_BANK -w "$IMG_FILE_WINDOWS_PATH" -timeout $DEFAULT_TIMEOUT_SECS -tmp "$IMG_WINDOWS_TMP_ARCHIVE_DIR"
194        ImageUploader.exe -ip $IMG_UPLOAD_IP_ADDR -upload $IMG_UPLOAD_HDD_BANK -w "$IMG_FILE_WINDOWS_PATH" -timeout $DEFAULT_TIMEOUT_SECS -tmp "$IMG_WINDOWS_TMP_ARCHIVE_DIR"
195        ret=$?
196        if [ ! $ret = 0 ]; then
197           echo "Unable to upload image to disk"
198           exit 1
199        fi
200
201    fi
202
203else
204    # this is the older uploader, which can only handle one file
205    # so we must create a temp dir and build a full image with header
206    # there
207
208    create_directory_if_noexist "$IMG_UPLOAD_WORK_DIR/${SESSION_PATH_PREFIX}hddimage"
209    echo "About to prepare image for MION..."
210    cat "${0%/*}/mh_cafe.bin" > "$IMG_UPLOAD_WORK_DIR/${SESSION_PATH_PREFIX}hddimage/mion.bin"
211    ret=$?
212    if [ ! $ret = 0 ]; then
213       echo "Unable to copy header file ${0%/*}/mh_cafe.bin"
214       exit 1
215    fi
216
217    cat "$IMG_UPLOAD_MASTER_FILE" >> "$IMG_UPLOAD_WORK_DIR/${SESSION_PATH_PREFIX}hddimage/mion.bin"
218    ret=$?
219    if [ ! $ret = 0 ]; then
220       echo "Unable to concatenate header file and image file"
221       exit 1
222    fi
223
224    echo "About to upload HDD image to bank #$IMG_UPLOAD_HDD_BANK"
225    IMG_UPLOAD_WINDOWS_PATH=`cygpath --windows $IMG_UPLOAD_WORK_DIR/${SESSION_PATH_PREFIX}hddimage/mion.bin`
226    ImageUploader.exe -ip $IMG_UPLOAD_IP_ADDR -upload $IMG_UPLOAD_HDD_BANK "$IMG_UPLOAD_WINDOWS_PATH" -timeout $DEFAULT_TIMEOUT_SECS
227    ret=$?
228    if [ ! $ret = 0 ]; then
229       echo "Unable to upload image to disk"
230       exit 1
231    fi
232
233    rm -Rf "$IMG_UPLOAD_WORK_DIR/${SESSION_PATH_PREFIX}hddimage"
234
235fi
236
237
238
239
240exit 0
241