1@echo off
2
3:: ###############################################################################
4:: #
5:: # Copyright (C) 2009-2014 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
15set MULTI_ARG=core
16set MAKECORE_DIR=.
17set DBG_SRC_ROOT=%CAFE_ROOT%\system
18
19set DO_MAKECORE=1
20set LAUNCH_MULTI=1
21set DISPLAY_HELP=0
22
23set MIN_VER0=5
24set MIN_VER1=3
25set MIN_VER2=21
26set MIN_VER="%MIN_VER0.%MIN_VER1.%MIN_VER"
27set PATCH="Core0 BETA"
28set VER_TOO_OLD=0
29
30if "%GHS_ROOT%" EQU "" (
31    echo Error: GHS_ROOT not defined. Exiting viewcore
32)
33
34::::: Check GHS version; should be newer than 5.3.21 Core0 BETA.
35::::: If it's 5.3.22 or newer, doesn't have to check the patch name.
36
37for /f "usebackq tokens=7,8,9,11,12 delims=. " %%a in (`%GHS_ROOT%\gversion -quiet %GHS_ROOT%\multi.exe`) do (
38    if "%%a" EQU "v5" (
39        set VER0=%%a
40        set VER1=%%b
41        set VER2=%%c
42        set CORE=%%d
43        set BETA=%%e
44    )
45)
46
47if %VER0% NEQ "v5" (
48    set VER_TOO_OLD=1
49)
50if %VER1% LSS 3 (
51    set VER_TOO_OLD=1
52)
53if %VER2% LSS 21 (
54    set VER_TOO_OLD=1
55)
56if %VER2% EQU 21 (
57
58    if "%CORE%" NEQ "Core0" (
59        set VER_TOO_OLD=1
60        set CORE=""
61    )
62    if "%BETA%" NEQ "BETA" (
63        set VER_TOO_OLD=1
64        set BETA=""
65    )
66)
67if %VER2% GEQ 22 (
68    if "%CORE%" NEQ "Core0" (
69        set CORE=""
70    )
71    if "%BETA%" NEQ "BETA" (
72        set BETA=""
73    )
74)
75
76if %CORE% EQU "" (
77    set CURRENT_VERSION=%VER0%.%VER1%.%VER2%
78)
79if %CORE% NEQ "" (
80    if %BETA% EQU "" (
81        set CURRENT_VERSION=%VER0%.%VER1%.%VER2% %CORE%
82    )
83    if %BETA% NEQ "" (
84        set CURRENT_VERSION=%VER0%.%VER1%.%VER2% %CORE% %BETA%
85    )
86)
87
88if %VER_TOO_OLD EQU 1 (
89    echo Error: MULTI version %MIN_VER% %PATCH% or later is needed. Current Version is %CURRENT_VERSION%
90)
91
92::::: Get viewcore options
93
94:LOOP
95if "%~1" EQU "" goto CONTINUE
96
97if "%~1" EQU "-o" (
98    shift
99    set MULTI_ARG="%~2"
100    shift
101    goto LOOP
102)
103if "%~1" EQU "-i" (
104    shift
105    set DO_MAKECORE=0
106    set MULTI_ARG="%~2"
107    shift
108    goto LOOP
109)
110if "%~1" EQU "-d" (
111    shift
112    set DBG_SRC_ROOT="%~2"
113    shift
114    goto LOOP
115)
116if "%~1" EQU "-a" (
117    set DBG_SRC_ROOT=""
118    shift
119    goto LOOP
120)
121if "%~1" EQU "-x" (
122    set AGE_OLD_DUMPS=1
123    shift
124    goto LOOP
125)
126if "%~1" EQU "-h" (
127    set DISPLAY_HELP=1
128    set DO_MAKECORE=0
129    set LAUNCH_MULTI=0
130    shift
131    goto LOOP
132)
133if "%~1" EQU "-help" (
134    set DISPLAY_HELP=1
135    set DO_MAKECORE=0
136    set LAUNCH_MULTI=0
137    shift
138    goto LOOP
139)
140if "%~1" EQU "-*" (
141    echo Error: $1 unrecognized options
142    set DISPLAY_HELP=1
143    set DO_MAKECORE=0
144    set LAUNCH_MULTI=0
145    shift
146    goto LOOP
147)
148set MAKECORE_DIR="%~1"
149shift
150goto LOOP
151
152:CONTINUE
153
154::::: Get all the memory blocks in current directory, or user specified directory
155
156:::: Automatically age old dump logs
157if "%AGE_OLD_DUMPS%" EQU "" goto :age_done
158if "%CAFE_CRASH_DUMP_LIMIT%" == "" set CAFE_CRASH_DUMP_LIMIT=5
159set /a CAFE_CRASH_DUMP_LIMIT=CAFE_CRASH_DUMP_LIMIT + 0
160if %CAFE_CRASH_DUMP_LIMIT% LEQ 0 goto :age_done
161    set /a NUM_OLD_DUMPS = 0
162    for /f %%i in ('dir /o-d/b %MAKECORE_DIR%\..') do call :age_dump %%i
163    goto :age_done
164        :age_dump
165        set /a NUM_OLD_DUMPS = NUM_OLD_DUMPS + 1
166        if %NUM_OLD_DUMPS% GTR %CAFE_CRASH_DUMP_LIMIT% rd /s/q %MAKECORE_DIR%\..\%1
167        goto :eof
168:age_done
169
170if %DO_MAKECORE% EQU 0 goto :nomulti_done
171if "%CAFE_CRASH_DUMP_NOMULTI%" == "" goto :nomulti_done
172if %CAFE_CRASH_DUMP_NOMULTI% EQU 0 goto :nomulti_done
173    echo Making a core dump file, but NOT automatically launching GHS MULTI!
174    set LAUNCH_MULTI=0
175:nomulti_done
176
177if "%DBG_SRC_ROOT%" EQU "" (
178    echo Message: Use absolute path for all rpl
179)
180if "%DBG_SRC_ROOT%" NEQ "" (
181    set DBG_SRC_ROOT=-d %DBG_SRC_ROOT%
182)
183
184set "MAKECORE_ARG="
185setLocal Enabledelayedexpansion
186
187for /f "usebackq" %%i in (`dir %MAKECORE_DIR%\BLK* /B 2^>nul`) do (
188    set "MAKECORE_ARG=%MAKECORE_DIR%\%%i !MAKECORE_ARG!"
189)
190
191setlocal disabledelayedexpansion
192
193::::: Remove "free " string in %MAKECORE_ARG%
194
195set MAKECORE_ARG=%MAKECORE_ARG:free =%
196
197::::: Call makecore
198
199if %DO_MAKECORE% EQU 1 (
200::::: Specified directory doesn't have memory blocks
201    if "%MAKECORE_ARG%" EQU "" (
202	    echo Error: No memory blocks found in %MAKECORE_DIR. Exiting viewcore. 1>&2
203	    set DISPLAY_HELP=1
204	)
205	if "%MAKECORE_ARG%" NEQ "" (
206::::: -o option is used without specified core dump name
207	    if %MULTI_ARG% EQU "" (
208            echo Error: missing argument with option -o. Exiting viewcore. 1>&2
209            set DISPLAY_HELP=1
210        )
211::::: makecore processes the memory blocks
212	    if %MULTI_ARG% NEQ "" (
213            echo Running makecore to process the following memory blocks:
214            for %%x in (%MAKECORE_ARG%) do echo %%x
215            makecore %MAKECORE_ARG% -o %MULTI_ARG% %DBG_SRC_ROOT%
216            if ERRORLEVEL 0 (
217                if %LAUNCH_MULTI% NEQ 0 (
218                    echo MULTI %CURRENT_VERSION% is launching %MULTI_ARG% 1>&2
219                    %GHS_ROOT%\multi.exe %MULTI_ARG%
220                )
221	            goto END
222	        )
223::::: makecore returns non-zero
224            echo Error: MULTI %CURRENT_VERSION% is not launched due to makecore error. 1>&2
225            set DISPLAY_HELP=1
226	    )
227    )
228)
229
230::::: Only view core dump file, does not call makecore
231if %DO_MAKECORE% NEQ 1 (
232    if %LAUNCH_MULTI%  EQU 1 (
233::::: -i option is used without specified core dump file
234        if %MULTI_ARG% EQU "" (
235            echo Error: missing argument with option -i. Exiting viewcore.
236            set DISPLAY_HELP=1
237            goto HELP_MENU
238        )
239::::: Test if the specified core dump file exist
240        if %MULTI_ARG% NEQ ""  (
241            for /f "usebackq" %%i in (`dir /b /a-d %MULTI_ARG%`) do (
242               echo.%MULTI_ARG% | findstr /C:"%%i"
243::::: Specified core dump file is a directory or does not exist
244               if ERRORLEVEL 1 (
245                   echo Error: %MULTI_ARG% not found, or is a directory. Exiting viewcore.
246                   set DISPLAY_HELP=1
247                   goto HELP_MENU
248               )
249::::: Specified core dump file exists, call MULTI
250               if ERRORLEVEL 0 (
251                   echo MULTI %CURRENT_VERSION% is launching %MULTI_ARG% 1>&2
252                   %GHS_ROOT%\multi.exe %MULTI_ARG%
253               )
254           )
255       )
256    )
257)
258
259:HELP_MENU
260
261if %DISPLAY_HELP% EQU 1 (
262    echo.
263    echo viewcore.bat
264    echo     Usage: viewcore.bat [-o ^<file^>] [-i ^<file^>] [-d ^<dir^>] [-a] [-h^<elp^>] [^<directory^>]
265    echo     Options:
266    echo       -o ^<file^>    Specify output core dump file name. If unspecified,
267    echo                    "core" is used.
268    echo       -i ^<file^>    View core dump file "file". No memory
269    echo                    block files will be processed
270    echo       -d ^<dir^>     Debug source root "dir". If unspecified,
271    echo                    "%CAFE_ROOT%\system" is used
272    echo       -a           Treat path info stored in RPLs as absolute path.
273    echo                    Use this option to process and view RPLs built
274    echo                    before SDK2.11.02
275    echo       -h^<elp^>      Print this menu
276    echo       ^<directory^>  Look for memory block files in ^<directory^> to be parsed
277    echo                    If unspecified, current working directory is used.
278)
279
280:END