@echo off
setlocal EnableExtensions

REM v117: sidecar-backed first Xbox auth packet probes using the sequential
REM GDR packet source-stream model.  This continues past the first DF2A submit
REM and verifies the full configured data-in payload reconstructed from DF70
REM bursts.
REM
REM Usage:
REM   run_gdr8050l_auth_packet_v117_sidecar_stream_source.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 [V117] XDVD sidecar: %XDVD_JSON%
if not "%~2"=="" echo [V117] ISO metadata: %~2

REM ---------------------------------------------------------------------------
REM Probe 1: MODE SENSE(10), page 0x3E, locked state, full stream verification.
REM Expected success markers:
REM   gdr_data_in_ready=1, gdr_data_in_submit=1
REM   exact_complete_payload=true
REM ---------------------------------------------------------------------------
python run_direct_firmware.py ^
  --workspace "%WORKSPACE%" ^
  --bin "%FWBIN%" ^
  --base 0x90000000 ^
  --entry 0x90000020 ^
  --model GDR-8050L ^
  --name "GDR-8050L v117 MODE SENSE 3E locked sidecar stream source" ^
  --max-cycles 120000 ^
  --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 ^
  --auth-watch ^
  --trace-csv gdr8050l_auth_v117_mode_sense_3e_locked_stream_trace.csv ^
  --trace-summary ^
  --config-out gdr8050l_auth_v117_mode_sense_3e_locked_stream_config.json ^
  > gdr8050l_auth_v117_mode_sense_3e_locked_stream.output.txt 2^>^&1

if errorlevel 1 goto fail_mode_sense

python analyze_gdr8050l_v109_readcap_route.py ^
  gdr8050l_auth_v117_mode_sense_3e_locked_stream_trace.csv ^
  --out gdr8050l_auth_v117_mode_sense_3e_locked_stream_summary.md

python extract_gdr_handshake.py ^
  gdr8050l_auth_v117_mode_sense_3e_locked_stream_trace.csv ^
  --out-json gdr8050l_auth_v117_mode_sense_3e_locked_stream_handshake.json ^
  --config gdr8050l_auth_v117_mode_sense_3e_locked_stream_config.json ^
  --out-df70 gdr8050l_auth_v117_mode_sense_3e_locked_stream_df70_all.bin ^
  --out-df70-after-packet gdr8050l_auth_v117_mode_sense_3e_locked_stream_df70_after_packet.bin

python analyze_gdr_chunked_data_in.py ^
  gdr8050l_auth_v117_mode_sense_3e_locked_stream_trace.csv ^
  --config gdr8050l_auth_v117_mode_sense_3e_locked_stream_config.json ^
  --out gdr8050l_auth_v117_mode_sense_3e_locked_stream_verify.md ^
  --out-payload gdr8050l_auth_v117_mode_sense_3e_locked_stream_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 full stream verification.
REM ---------------------------------------------------------------------------
python run_direct_firmware.py ^
  --workspace "%WORKSPACE%" ^
  --bin "%FWBIN%" ^
  --base 0x90000000 ^
  --entry 0x90000020 ^
  --model GDR-8050L ^
  --name "GDR-8050L v117 READ DVD STRUCTURE auth sidecar stream source" ^
  --max-cycles 180000 ^
  --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 ^
  --auth-watch ^
  --trace-csv gdr8050l_auth_v117_read_dvd_structure_ad_stream_trace.csv ^
  --trace-summary ^
  --config-out gdr8050l_auth_v117_read_dvd_structure_ad_stream_config.json ^
  > gdr8050l_auth_v117_read_dvd_structure_ad_stream.output.txt 2^>^&1

if errorlevel 1 goto fail_ad

python analyze_gdr8050l_v109_readcap_route.py ^
  gdr8050l_auth_v117_read_dvd_structure_ad_stream_trace.csv ^
  --out gdr8050l_auth_v117_read_dvd_structure_ad_stream_summary.md

python extract_gdr_handshake.py ^
  gdr8050l_auth_v117_read_dvd_structure_ad_stream_trace.csv ^
  --out-json gdr8050l_auth_v117_read_dvd_structure_ad_stream_handshake.json ^
  --config gdr8050l_auth_v117_read_dvd_structure_ad_stream_config.json ^
  --out-df70 gdr8050l_auth_v117_read_dvd_structure_ad_stream_df70_all.bin ^
  --out-df70-after-packet gdr8050l_auth_v117_read_dvd_structure_ad_stream_df70_after_packet.bin

python analyze_gdr_chunked_data_in.py ^
  gdr8050l_auth_v117_read_dvd_structure_ad_stream_trace.csv ^
  --config gdr8050l_auth_v117_read_dvd_structure_ad_stream_config.json ^
  --out gdr8050l_auth_v117_read_dvd_structure_ad_stream_verify.md ^
  --out-payload gdr8050l_auth_v117_read_dvd_structure_ad_stream_payload_reconstructed.bin

if errorlevel 1 goto fail_ad_verify

echo [OK] v117 sidecar stream-source probes complete.
echo [NEXT] Send *_v117_*output.txt, *_v117_*summary.md, *_v117_*handshake.json, and *_v117_*verify.md.
exit /b 0

:fail_mode_sense
echo [FAIL] v117 MODE SENSE 3E stream probe failed. Send gdr8050l_auth_v117_mode_sense_3e_locked_stream.output.txt
exit /b 1

:fail_mode_verify
echo [FAIL] v117 MODE SENSE 3E stream verification failed. Send gdr8050l_auth_v117_mode_sense_3e_locked_stream_verify.md
exit /b 1

:fail_ad
echo [FAIL] v117 READ DVD STRUCTURE AD stream probe failed. Send gdr8050l_auth_v117_read_dvd_structure_ad_stream.output.txt
exit /b 1

:fail_ad_verify
echo [FAIL] v117 READ DVD STRUCTURE AD stream verification failed. Send gdr8050l_auth_v117_read_dvd_structure_ad_stream_verify.md
exit /b 1
