@echo off
setlocal EnableExtensions

REM v119: same sidecar-backed auth packet probes as v118, but continue past
REM the packet timer-done checkpoint at 0x90008824 and stop only after the
REM firmware has returned to the post-timer main loop at 0x90006608.
REM
REM Requires v118-or-newer emulator files: --gdr-hw-model, stream source model,
REM and --clear-preset-stops.
REM
REM Usage:
REM   run_gdr8050l_auth_packet_v119_post_timer_main_loop.cmd D:\path\game.xdvd.json [D:\path\game.iso]

if "%~1"=="" (
  echo Usage: %~nx0 D:\path\game.xdvd.json [D:\path\game.iso]
  exit /b 2
)

set "XDVD_JSON=%~1"
set "ISO_FLAGS="
if not "%~2"=="" set "ISO_FLAGS=--iso "%~2""

set "WORKSPACE=D:\vscode-workspace\xbox-dvd-tools-v2"
set "FWBIN=D:\vscode-workspace\xbox-dvd-tools-v2\firmware\GDR-8050L\8050L_fw_plaintext_256k.bin"

echo [V119] XDVD sidecar: %XDVD_JSON%
if not "%~2"=="" echo [V119] ISO metadata: %~2
echo [V119] Continuing past packet timer-done stop; target stop is post-timer main loop 0x90006608.

REM ---------------------------------------------------------------------------
REM Probe 1: MODE SENSE(10), page 0x3E, locked state.
REM ---------------------------------------------------------------------------
python run_direct_firmware.py ^
  --workspace "%WORKSPACE%" ^
  --bin "%FWBIN%" ^
  --base 0x90000000 ^
  --entry 0x90000020 ^
  --model GDR-8050L ^
  --name "GDR-8050L v119 MODE SENSE 3E post-timer main-loop" ^
  --max-cycles 90000 ^
  --sp 0x003FF000 ^
  --preset gdr8050l-packet-full-cdb ^
  --packet-cdb mode-sense-auth-xbox ^
  --xdvd-sidecar "%XDVD_JSON%" ^
  --xdvd-state locked ^
  %ISO_FLAGS% ^
  --packet-data-in-source-mode stream ^
  --packet-data-in-model-match-len 8 ^
  --gdr-hw-model ^
  --clear-preset-stops ^
  --stop-pc 0x90006608 ^
  --stop-pc-hit-count 4 ^
  --stop-pc-min-cycle 51200 ^
  --auth-watch ^
  --trace-csv gdr8050l_auth_v119_mode_sense_3e_locked_post_timer_trace.csv ^
  --trace-summary ^
  --config-out gdr8050l_auth_v119_mode_sense_3e_locked_post_timer_config.json ^
  > gdr8050l_auth_v119_mode_sense_3e_locked_post_timer.output.txt 2^>^&1

if errorlevel 1 goto fail_mode_sense

python analyze_gdr8050l_v109_readcap_route.py ^
  gdr8050l_auth_v119_mode_sense_3e_locked_post_timer_trace.csv ^
  --out gdr8050l_auth_v119_mode_sense_3e_locked_post_timer_summary.md

python extract_gdr_handshake.py ^
  gdr8050l_auth_v119_mode_sense_3e_locked_post_timer_trace.csv ^
  --out-json gdr8050l_auth_v119_mode_sense_3e_locked_post_timer_handshake.json ^
  --config gdr8050l_auth_v119_mode_sense_3e_locked_post_timer_config.json ^
  --out-df70 gdr8050l_auth_v119_mode_sense_3e_locked_post_timer_df70_all.bin ^
  --out-df70-after-packet gdr8050l_auth_v119_mode_sense_3e_locked_post_timer_df70_after_packet.bin

python analyze_gdr_chunked_data_in.py ^
  gdr8050l_auth_v119_mode_sense_3e_locked_post_timer_trace.csv ^
  --config gdr8050l_auth_v119_mode_sense_3e_locked_post_timer_config.json ^
  --out gdr8050l_auth_v119_mode_sense_3e_locked_post_timer_verify.md ^
  --out-payload gdr8050l_auth_v119_mode_sense_3e_locked_post_timer_payload_reconstructed.bin

if errorlevel 1 goto fail_mode_verify

REM ---------------------------------------------------------------------------
REM Probe 2: READ DVD STRUCTURE Xbox auth CDB (AD ... C0), locked state.
REM ---------------------------------------------------------------------------
python run_direct_firmware.py ^
  --workspace "%WORKSPACE%" ^
  --bin "%FWBIN%" ^
  --base 0x90000000 ^
  --entry 0x90000020 ^
  --model GDR-8050L ^
  --name "GDR-8050L v119 READ DVD STRUCTURE auth post-timer main-loop" ^
  --max-cycles 120000 ^
  --sp 0x003FF000 ^
  --preset gdr8050l-packet-full-cdb ^
  --packet-cdb read-dvd-structure-xbox ^
  --xdvd-sidecar "%XDVD_JSON%" ^
  --xdvd-state locked ^
  %ISO_FLAGS% ^
  --packet-data-in-source-mode stream ^
  --packet-data-in-model-match-len 8 ^
  --gdr-hw-model ^
  --clear-preset-stops ^
  --stop-pc 0x90006608 ^
  --stop-pc-hit-count 4 ^
  --stop-pc-min-cycle 51200 ^
  --auth-watch ^
  --trace-csv gdr8050l_auth_v119_read_dvd_structure_ad_post_timer_trace.csv ^
  --trace-summary ^
  --config-out gdr8050l_auth_v119_read_dvd_structure_ad_post_timer_config.json ^
  > gdr8050l_auth_v119_read_dvd_structure_ad_post_timer.output.txt 2^>^&1

if errorlevel 1 goto fail_ad

python analyze_gdr8050l_v109_readcap_route.py ^
  gdr8050l_auth_v119_read_dvd_structure_ad_post_timer_trace.csv ^
  --out gdr8050l_auth_v119_read_dvd_structure_ad_post_timer_summary.md

python extract_gdr_handshake.py ^
  gdr8050l_auth_v119_read_dvd_structure_ad_post_timer_trace.csv ^
  --out-json gdr8050l_auth_v119_read_dvd_structure_ad_post_timer_handshake.json ^
  --config gdr8050l_auth_v119_read_dvd_structure_ad_post_timer_config.json ^
  --out-df70 gdr8050l_auth_v119_read_dvd_structure_ad_post_timer_df70_all.bin ^
  --out-df70-after-packet gdr8050l_auth_v119_read_dvd_structure_ad_post_timer_df70_after_packet.bin

python analyze_gdr_chunked_data_in.py ^
  gdr8050l_auth_v119_read_dvd_structure_ad_post_timer_trace.csv ^
  --config gdr8050l_auth_v119_read_dvd_structure_ad_post_timer_config.json ^
  --out gdr8050l_auth_v119_read_dvd_structure_ad_post_timer_verify.md ^
  --out-payload gdr8050l_auth_v119_read_dvd_structure_ad_post_timer_payload_reconstructed.bin

if errorlevel 1 goto fail_ad_verify

echo [OK] v119 post-timer main-loop probes complete.
echo [NEXT] Send *_v119_*output.txt, *_v119_*summary.md, *_v119_*handshake.json, and *_v119_*verify.md.
exit /b 0

:fail_mode_sense
echo [FAIL] v119 MODE SENSE 3E post-timer probe failed. Send gdr8050l_auth_v119_mode_sense_3e_locked_post_timer.output.txt
exit /b 1

:fail_mode_verify
echo [FAIL] v119 MODE SENSE 3E verification failed. Send gdr8050l_auth_v119_mode_sense_3e_locked_post_timer_verify.md
exit /b 1

:fail_ad
echo [FAIL] v119 READ DVD STRUCTURE AD post-timer probe failed. Send gdr8050l_auth_v119_read_dvd_structure_ad_post_timer.output.txt
exit /b 1

:fail_ad_verify
echo [FAIL] v119 READ DVD STRUCTURE AD verification failed. Send gdr8050l_auth_v119_read_dvd_structure_ad_post_timer_verify.md
exit /b 1
