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