@echo off
setlocal EnableExtensions

REM v114: sidecar-backed first Xbox auth packet probes after the bounded READ CAPACITY milestone.
REM Usage:
REM   run_gdr8050l_auth_packet_v114_sidecar_first_packets.cmd D:\path\game.xdvd.json [D:\path\game.iso]
REM
REM This is still runtime-only packet plumbing.  It does not claim auth success by itself.
REM It verifies whether the GDR firmware path can stage/submit captured MODE SENSE 3E
REM and READ DVD STRUCTURE Xbox auth payloads from the .xdvd sidecar.

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

REM ---------------------------------------------------------------------------
REM Probe 1: MODE SENSE(10), page 0x3E, locked state.
REM This should use drive_states.locked.mode_sense_3e_hex from the .xdvd sidecar.
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 v114 MODE SENSE 3E locked sidecar packet" ^
  --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 ^
  --auth-watch ^
  --trace-csv gdr8050l_auth_v114_mode_sense_3e_locked_trace.csv ^
  --trace-summary ^
  --config-out gdr8050l_auth_v114_mode_sense_3e_locked_config.json ^
  > gdr8050l_auth_v114_mode_sense_3e_locked.output.txt 2^>^&1

if errorlevel 1 goto fail_mode_sense

python analyze_gdr8050l_v109_readcap_route.py ^
  gdr8050l_auth_v114_mode_sense_3e_locked_trace.csv ^
  --out gdr8050l_auth_v114_mode_sense_3e_locked_summary.md

python extract_gdr_handshake.py ^
  gdr8050l_auth_v114_mode_sense_3e_locked_trace.csv ^
  --out-json gdr8050l_auth_v114_mode_sense_3e_locked_handshake.json ^
  --out-df70 gdr8050l_auth_v114_mode_sense_3e_locked_df70_all.bin ^
  --out-df70-after-packet gdr8050l_auth_v114_mode_sense_3e_locked_df70_after_packet.bin

REM ---------------------------------------------------------------------------
REM Probe 2: READ DVD STRUCTURE Xbox auth CDB (AD ... C0).
REM This should use the matching external response file referenced by the .xdvd sidecar.
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 v114 READ DVD STRUCTURE auth sidecar packet" ^
  --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 ^
  --auth-watch ^
  --trace-csv gdr8050l_auth_v114_read_dvd_structure_ad_trace.csv ^
  --trace-summary ^
  --config-out gdr8050l_auth_v114_read_dvd_structure_ad_config.json ^
  > gdr8050l_auth_v114_read_dvd_structure_ad.output.txt 2^>^&1

if errorlevel 1 goto fail_ad

python analyze_gdr8050l_v109_readcap_route.py ^
  gdr8050l_auth_v114_read_dvd_structure_ad_trace.csv ^
  --out gdr8050l_auth_v114_read_dvd_structure_ad_summary.md

python extract_gdr_handshake.py ^
  gdr8050l_auth_v114_read_dvd_structure_ad_trace.csv ^
  --out-json gdr8050l_auth_v114_read_dvd_structure_ad_handshake.json ^
  --out-df70 gdr8050l_auth_v114_read_dvd_structure_ad_df70_all.bin ^
  --out-df70-after-packet gdr8050l_auth_v114_read_dvd_structure_ad_df70_after_packet.bin

echo [OK] v114 first auth packet probes complete.
echo [NEXT] Send *_v114_*output.txt, *_v114_*summary.md, and *_v114_*handshake.json.
exit /b 0

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

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

