@echo off
setlocal EnableExtensions

REM v116: sidecar-backed first Xbox auth packet probes with prefix-length data-in submit markers.
REM Usage:
REM   run_gdr8050l_auth_packet_v116_sidecar_prefix_submit.cmd D:\path\game.xdvd.json [D:\path\game.iso]
REM
REM v114 proved the sidecar responses were being fed, but the data-in model waited
REM for the full response length in one DF70 window.  The GDR helper submits these
REM packets in small DF70 bursts, so v116 matches the first 8 staged bytes and stops
REM on the corresponding DF2A submit.

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 [V116] XDVD sidecar: %XDVD_JSON%
if not "%~2"=="" echo [V116] ISO metadata: %~2

REM ---------------------------------------------------------------------------
REM Probe 1: MODE SENSE(10), page 0x3E, locked state.
REM Expected success marker: gdr_data_in_ready=1 and gdr_data_in_submit=1.
REM ---------------------------------------------------------------------------
python run_direct_firmware.py ^
  --workspace "%WORKSPACE%" ^
  --bin "%FWBIN%" ^
  --base 0x90000000 ^
  --entry 0x90000020 ^
  --model GDR-8050L ^
  --name "GDR-8050L v116 MODE SENSE 3E locked sidecar prefix submit known-CDB known-CDB" ^
  --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-model-stop ^
  --packet-data-in-model-match-len 8 ^
  --auth-watch ^
  --trace-csv gdr8050l_auth_v116_mode_sense_3e_locked_prefix_trace.csv ^
  --trace-summary ^
  --config-out gdr8050l_auth_v116_mode_sense_3e_locked_prefix_config.json ^
  > gdr8050l_auth_v116_mode_sense_3e_locked_prefix.output.txt 2^>^&1

if errorlevel 1 goto fail_mode_sense

python analyze_gdr8050l_v109_readcap_route.py ^
  gdr8050l_auth_v116_mode_sense_3e_locked_prefix_trace.csv ^
  --out gdr8050l_auth_v116_mode_sense_3e_locked_prefix_summary.md

python extract_gdr_handshake.py ^
  gdr8050l_auth_v116_mode_sense_3e_locked_prefix_trace.csv ^
  --out-json gdr8050l_auth_v116_mode_sense_3e_locked_prefix_handshake.json ^
  --config gdr8050l_auth_v116_mode_sense_3e_locked_prefix_config.json ^
  --out-df70 gdr8050l_auth_v116_mode_sense_3e_locked_prefix_df70_all.bin ^
  --out-df70-after-packet gdr8050l_auth_v116_mode_sense_3e_locked_prefix_df70_after_packet.bin

REM ---------------------------------------------------------------------------
REM Probe 2: READ DVD STRUCTURE Xbox auth CDB (AD ... C0), locked state.
REM Expected success marker: gdr_data_in_ready=1 and gdr_data_in_submit=1.
REM ---------------------------------------------------------------------------
python run_direct_firmware.py ^
  --workspace "%WORKSPACE%" ^
  --bin "%FWBIN%" ^
  --base 0x90000000 ^
  --entry 0x90000020 ^
  --model GDR-8050L ^
  --name "GDR-8050L v116 READ DVD STRUCTURE auth sidecar prefix submit known-CDB known-CDB" ^
  --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-model-stop ^
  --packet-data-in-model-match-len 8 ^
  --auth-watch ^
  --trace-csv gdr8050l_auth_v116_read_dvd_structure_ad_prefix_trace.csv ^
  --trace-summary ^
  --config-out gdr8050l_auth_v116_read_dvd_structure_ad_prefix_config.json ^
  > gdr8050l_auth_v116_read_dvd_structure_ad_prefix.output.txt 2^>^&1

if errorlevel 1 goto fail_ad

python analyze_gdr8050l_v109_readcap_route.py ^
  gdr8050l_auth_v116_read_dvd_structure_ad_prefix_trace.csv ^
  --out gdr8050l_auth_v116_read_dvd_structure_ad_prefix_summary.md

python extract_gdr_handshake.py ^
  gdr8050l_auth_v116_read_dvd_structure_ad_prefix_trace.csv ^
  --out-json gdr8050l_auth_v116_read_dvd_structure_ad_prefix_handshake.json ^
  --config gdr8050l_auth_v116_read_dvd_structure_ad_prefix_config.json ^
  --out-df70 gdr8050l_auth_v116_read_dvd_structure_ad_prefix_df70_all.bin ^
  --out-df70-after-packet gdr8050l_auth_v116_read_dvd_structure_ad_prefix_df70_after_packet.bin

echo [OK] v116 first auth packet prefix-submit known-CDB probes complete.
echo [NEXT] Send *_v116_*output.txt, *_v116_*summary.md, and *_v116_*handshake.json.
exit /b 0

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

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