seti $printhelp = $printhelp
seti $help = $help
if $help = 1
  echo 'usb test 3.'
  echo 'Example: hddsupertool -f usb_test3'
  if $printhelp = 1
    exit 0
  endif
  echo 'Hit ENTER to continue...'
  userinput $choicestring
  previousscript
endif

# set a blank string variable to compare with user input
# make sure not to set this anyplace else
sets $null = ""



# requesttype
seti $USB_DIR_OUT = 0
seti $USB_ENDPOINT_OUT = 0x02
seti $USB_DIR_IN = 0x80
seti $USB_ENDPOINT_IN = 0x81
seti $USB_TYPE_CLASS = 0x21

#seti $USB_ENDPOINT_OUT = 0x0a
#seti $USB_ENDPOINT_IN = 0x8b


# cbw csw signature
seti $cbwsig1 = 0x55
seti $cbwsig2 = 0x53
seti $cbwsig3 = 0x42
seti $cbwsig4 = 0x43

# cbw csw tag (remember to increment for each command)
seti $cbwtag = 1
gosub setcbwtag

# cbw data transfer length (remember to set before each command)
seti $cbwdatalen = 0
gosub setcbwdatalen

# cbw transfer direction (remember to set before each command)
seti $cbwflags = $USB_DIR_IN

# cbw command length (remember to set before each command)
seti $cbwcommandlen = 0

usbtimeout 6000
seti $sectorsize = 512



seti $usbrequesttype = 0
seti $usbrequest = 0
seti $usbvalue = 0
seti $usbindex = 0
usbbuffersize 8

while 1 = 1
  echo " "
  echo "usb mass storage test functions"
  echo "q) Quit"
  echo "p) Previous menu"
  echo "h) Toggle script help
  echo "1) test identify"
  echo "2) read sectors"
  echo "3) bulk only reset"
  echo "4) test control message"
  echo "5) endpoint in reset"
  echo "6) endpoint out reset"
  echo "7) reset recovery"
  echo "8) get endpoint status"
  echo "9) read good bad good"
  echo "10) soft reset 12"
  echo "11) hard reset 12"
  echo "12) WD enable vsc"
  echo "13) read big"
  echo "14) read log ext"
  echo "15) read black bad 3"
  echo "16) read big pio"
  echo "17) read sectors pio"
  echo "18) read sectors dma"
  echo "19) request sense"
  echo "20) usb reset"
  echo "21) test inquiry"
  echo "22) test read capacity 10"
  echo "23) test read capacity 16"

  if $printhelp = 1
    exit 0
  endif

  echo "Enter your choice:
  userinput $choicestring

  if $choicestring = q
    break

  elseif $choicestring = p
    previousscript

  elseif $choicestring = h
    if $help = 0
      seti $help = 1
      echo "Help is now turned on"
    else
      seti $help = 0
      echo "Help is now turned off"
    endif

  elseif $choicestring = 1
    gosub testidentify

  elseif $choicestring = 2
    gosub readsectors

  elseif $choicestring = 3
    gosub bulkreset

  elseif $choicestring = 4
    gosub testctrlmsg

  elseif $choicestring = 5
    gosub endpointinreset

  elseif $choicestring = 6
    gosub endpointoutreset

  elseif $choicestring = 7
    gosub resetrecovery

  elseif $choicestring = 8
    gosub getendpointstatus

  elseif $choicestring = 9
    gosub readgoodbadgood

  elseif $choicestring = 10
    gosub softreset12

  elseif $choicestring = 11
    gosub hardreset12

  elseif $choicestring = 12
    gosub enablevsc

  elseif $choicestring = 13
    gosub readbig

  elseif $choicestring = 14
    gosub readlog

  elseif $choicestring = 15
    gosub readblack3bad

  elseif $choicestring = 16
    gosub readbigpio

  elseif $choicestring = 17
    gosub readsectorspio

  elseif $choicestring = 18
    gosub readsectorsdma

  elseif $choicestring = 19
    gosub requestsense

  elseif $choicestring = 20
    gosub dousbreset

  elseif $choicestring = 21
    gosub testinquiry

  elseif $choicestring = 22
    gosub readcapacity10

  elseif $choicestring = 23
    gosub readcapacity16


  else
    echo "bad choice= " $choicestring
  endif

done
end


subroutine setcbwtag
  seti $cbwtag1 = $cbwtag & 0xff
  seti $cbwtag2 = $cbwtag > 8
  seti $cbwtag2 = $cbwtag2 & 0xff
  seti $cbwtag3 = $cbwtag > 16
  seti $cbwtag3 = $cbwtag3 & 0xff
  seti $cbwtag4 = $cbwtag > 24
  seti $cbwtag4 = $cbwtag4 & 0xff
endsubroutine

subroutine setcbwdatalen
  seti $cbwdatalen1 = $cbwdatalen & 0xff
  seti $cbwdatalen2 = $cbwdatalen > 8
  seti $cbwdatalen2 = $cbwdatalen2 & 0xff
  seti $cbwdatalen3 = $cbwdatalen > 16
  seti $cbwdatalen3 = $cbwdatalen3 & 0xff
  seti $cbwdatalen4 = $cbwdatalen > 24
  seti $cbwdatalen4 = $cbwdatalen4 & 0xff
endsubroutine

subroutine bulkreset
  seti $usbrequesttype = 0x21
  seti $usbrequest = 0xff
  seti $usbvalue = 0
  seti $usbindex = 0
  usbbuffersize 0
  sendusbcontrolmsg $usbrequesttype $usbrequest $usbvalue $usbindex
  echo "bulk reset return=" $usb_return_status
endsubroutine

subroutine endpointinreset
  seti $usbrequesttype = 0x02
  seti $usbrequest = 0x01
  seti $usbvalue = 0
  seti $usbindex = $USB_ENDPOINT_IN
  usbbuffersize 0
  sendusbcontrolmsg $usbrequesttype $usbrequest $usbvalue $usbindex
  echo "reset in return=" $usb_return_status
endsubroutine

subroutine endpointoutreset
  seti $usbrequesttype = 0x02
  seti $usbrequest = 0x01
  seti $usbvalue = 0
  seti $usbindex = $USB_ENDPOINT_OUT
  usbbuffersize 0
  sendusbcontrolmsg $usbrequesttype $usbrequest $usbvalue $usbindex
  echo "reset out return=" $usb_return_status
endsubroutine

subroutine resetrecovery
  gosub bulkreset
  gosub endpointinreset
  gosub endpointoutreset
endsubroutine


subroutine getendpointstatus
  seti $usbrequesttype = 0x82
  seti $usbrequest = 0
  seti $usbvalue = 0
  seti $usbindex = 0
  usbbuffersize 2
  #sendusbcontrolmsg $usbrequesttype $usbrequest $usbvalue $usbindex
  #printusbbuffer 0 2
  #echo "endpoint 0 status return=" $usb_return_status
  seti $usbindex = $USB_ENDPOINT_IN
  sendusbcontrolmsg $usbrequesttype $usbrequest $usbvalue $usbindex
  seti $endpointinstatus = usbbuffer 0 w
  printusbbuffer 0 2
  echo "endpoint in status return=" $usb_return_status "
  seti $usbindex = $USB_ENDPOINT_OUT
  sendusbcontrolmsg $usbrequesttype $usbrequest $usbvalue $usbindex
  seti $endpointoutstatus = usbbuffer 0 w
  printusbbuffer 0 2
  echo "endpoint out status return=" $usb_return_status
endsubroutine

subroutine autoreset
  gosub getendpointstatus
  if $endpointinstatus != 0
    gosub endpointinreset
  endif
  if $endpointoutstatus != 0
    gosub endpointoutreset
  endif
endsubroutine

subroutine forcereset
  gosub bulkreset
  gosub endpointinreset
endsubroutine

subroutine testctrlmsg
  seti $usbrequesttype = 0x11
  seti $usbrequest = 0x22
  seti $usbvalue = 0x33
  seti $usbindex = 0x44
  usbbuffersize 0x55
  sendusbcontrolmsg $usbrequesttype $usbrequest $usbvalue $usbindex
  echo "reset return=" $usb_return_status
endsubroutine

subroutine testidentify
  usbbuffersize 31
  setusbbuffer 0
    0x55 0x53 0x42 0x43 0x2f 1 0 0
    0 2 0 0 0x80 0 0x0c 0xa1
    8 0x2e 0 0 0 0 0 0xa0
    0xec 0 0 0 0 0 0
  endusbbuffer
  usbrawwrite
  echo "write cbw return=" $usb_return_status
  usbbuffersize 512
  usbrawread
  echo "read data return=" $usb_return_status
  printusbbuffer 0 512
  usbbuffersize 13
  usbrawread
  echo "read csw return=" $usb_return_status
  printusbbuffer 0 13
endsubroutine


subroutine testinquiry
  seti $datasize = 0x2c
  usbbuffersize 31
  setusbbuffer 0
    0x55 0x53 0x42 0x43 0x2f 1 0 0
    $datasize 0 0 0 0x80 0 0x06 0x12
    0 0 0 $datasize 0 0 0 0
    0 0 0 0 0 0 0
  endusbbuffer
  usbrawwrite
  echo "write cbw return=" $usb_return_status
  usbbuffersize $datasize
  usbrawread
  echo "read data return=" $usb_return_status
  printusbbuffer 0 $datasize
  if $datasize != $usb_return_status
    gosub autoreset
  endif
  usbbuffersize 13
  usbrawread
  echo "read csw return=" $usb_return_status
endsubroutine


subroutine readcapacity10
  seti $datasize = 8
  usbbuffersize 31
  setusbbuffer 0
    0x55 0x53 0x42 0x43 8 6 4 2
    $datasize 0 0 0 0x80 0 0x0a 0x25
    0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0
  endusbbuffer
  usbrawwrite
  echo "write cbw return=" $usb_return_status
  usbbuffersize $datasize
  usbrawread
  echo "read data return=" $usb_return_status
  printusbbuffer 0 $datasize
  if $datasize != $usb_return_status
    gosub autoreset
  endif
  usbbuffersize 13
  usbrawread
  echo "read csw return=" $usb_return_status
  printusbbuffer 0 13
endsubroutine


subroutine readcapacity16
  seti $datasize = 32
  usbbuffersize 31
  setusbbuffer 0
    0x55 0x53 0x42 0x43 8 6 4 2
    $datasize 0 0 0 0x80 0 0x10 0x9e
    0x10 0 0 0 0 0 0 0
    0 0 0 0 $datasize 0 0
  endusbbuffer
  usbrawwrite
  echo "write cbw return=" $usb_return_status
  usbbuffersize $datasize
  usbrawread
  echo "read data return=" $usb_return_status
  printusbbuffer 0 $datasize
  if $datasize != $usb_return_status
    gosub autoreset
  endif
  usbbuffersize 13
  usbrawread
  echo "read csw return=" $usb_return_status
  printusbbuffer 0 13
endsubroutine


subroutine readsectors
  while 1 = 1
    echo "Enter starting sector:"
    userinput $choicestring
    if $choicestring != $null
      seti $sector = $choicestring 0
      break
    else
      echo "Choice cannot be blank!"
    endif
  done

  while 1 = 1
    echo "Enter count:"
    userinput $choicestring
    if $choicestring != $null
      seti $count = $choicestring 0
      break
    else
      echo "Choice cannot be blank!"
    endif
  done
  gosub readsectors10
endsubroutine


subroutine readsectorspio
  while 1 = 1
    echo "Enter starting sector:"
    userinput $choicestring
    if $choicestring != $null
      seti $sector = $choicestring 0
      break
    else
      echo "Choice cannot be blank!"
    endif
  done

  while 1 = 1
    echo "Enter count:"
    userinput $choicestring
    if $choicestring != $null
      seti $count = $choicestring 0
      break
    else
      echo "Choice cannot be blank!"
    endif
  done
  gosub atareadpio16
endsubroutine


subroutine readsectorsdma
  while 1 = 1
    echo "Enter starting sector:"
    userinput $choicestring
    if $choicestring != $null
      seti $sector = $choicestring 0
      break
    else
      echo "Choice cannot be blank!"
    endif
  done

  while 1 = 1
    echo "Enter count:"
    userinput $choicestring
    if $choicestring != $null
      seti $count = $choicestring 0
      break
    else
      echo "Choice cannot be blank!"
    endif
  done
  gosub atareaddma16
endsubroutine


subroutine readgoodbadgood
  while 1 = 1
    echo "Enter good sector1:"
    userinput $choicestring
    if $choicestring != $null
      seti $goodsector1 = $choicestring 0
      break
    else
      echo "Choice cannot be blank!"
    endif
  done

  while 1 = 1
    echo "Enter bad sector:"
    userinput $choicestring
    if $choicestring != $null
      seti $badsector = $choicestring 0
      break
    else
      echo "Choice cannot be blank!"
    endif
  done

  while 1 = 1
    echo "Enter good sector2:"
    userinput $choicestring
    if $choicestring != $null
      seti $goodsector2 = $choicestring 0
      break
    else
      echo "Choice cannot be blank!"
    endif
  done

  while 1 = 1
    echo "Enter count:"
    userinput $choicestring
    if $choicestring != $null
      seti $count = $choicestring 0
      break
    else
      echo "Choice cannot be blank!"
    endif
  done

  seti $sector = $goodsector1
  gosub readsectors10

  seti $sector = $badsector
  gosub readsectors10

  seti $sector = $goodsector2
  gosub readsectors10
endsubroutine


subroutine readbig
  seti $sector = 0
  seti $clustersize = 0xfefd
  seti $count = $clustersize
  while 1 = 1
    gosub readsectors10
    seti $sector = $sector + $clustersize
  done
endsubroutine


subroutine readbigpio
  seti $sector = 0
  seti $clustersize = 0xfefd
  seti $count = $clustersize
  while 1 = 1
    gosub atareadpio16
    seti $sector = $sector + $clustersize
  done
endsubroutine


subroutine readblack3bad
  seti $clustersize = 0xfefd
  seti $count = $clustersize
  while 1 = 1
    gettime
    seti $temp = $time & 0xfefd
    seti $sector = 0x69701e18 - $temp
    seti $badread = 0
    gosub readsectors10
    seti $sector = 0x6d71bb38 - $temp
    gosub readsectors10
    seti $sector = 0x6ee0a0e8 - $temp
    gosub readsectors10
    if $badread = 1
      sets $line1 = "There was a bad write " $date
      writelog logfile.txt $line1
    endif
  done
endsubroutine


subroutine readsectors10
  seti $lba1 = $sector & 0xff
  seti $lba2 = $sector > 8
  seti $lba2 = $lba2 & 0xff
  seti $lba3 = $sector > 16
  seti $lba3 = $lba3 & 0xff
  seti $lba4 = $sector > 24
  seti $lba4 = $lba4 & 0xff
  seti $lba5 = $sector > 32
  seti $lba5 = $lba5 & 0xff
  seti $lba6 = $sector > 40
  seti $lba6 = $lba6 & 0xff
  seti $lba7 = $sector > 48
  seti $lba7 = $lba7 & 0xff
  seti $lba8 = $sector > 56
  seti $lba8 = $lba8 & 0xff

  seti $cnt1 = $count & 0xff
  seti $cnt2 = $count > 8
  seti $cnt2 = $cnt2 & 0xff
  seti $cnt3 = $count > 16
  seti $cnt3 = $cnt3 & 0xff
  seti $cnt4 = $count > 24
  seti $cnt4 = $cnt4 & 0xff

  gosub read10
endsubroutine

subroutine read10
  seti $lba1 = $sector & 0xff
  seti $lba2 = $sector > 8
  seti $lba2 = $lba2 & 0xff
  seti $lba3 = $sector > 16
  seti $lba3 = $lba3 & 0xff
  seti $lba4 = $sector > 24
  seti $lba4 = $lba4 & 0xff
  seti $lba5 = $sector > 32
  seti $lba5 = $lba5 & 0xff
  seti $lba6 = $sector > 40
  seti $lba6 = $lba6 & 0xff
  seti $lba7 = $sector > 48
  seti $lba7 = $lba7 & 0xff
  seti $lba8 = $sector > 56
  seti $lba8 = $lba8 & 0xff

  seti $cnt1 = $count & 0xff
  seti $cnt2 = $count > 8
  seti $cnt2 = $cnt2 & 0xff
  seti $cnt3 = $count > 16
  seti $cnt3 = $cnt3 & 0xff
  seti $cnt4 = $count > 24
  seti $cnt4 = $cnt4 & 0xff

  seti $cbwtag = $cbwtag + 1
  gosub setcbwtag
  seti $cbwdatalen = $count * $sectorsize
  gosub setcbwdatalen
  seti $cbwflags = $USB_DIR_IN
  seti $cbwcommandlen = 10
  seti $cbwcommand1 = 0x28
  seti $cbwcommand2 = 0
  seti $cbwcommand3 = $lba4
  seti $cbwcommand4 = $lba3
  seti $cbwcommand5 = $lba2
  seti $cbwcommand6 = $lba1
  seti $cbwcommand7 = 0
  seti $cbwcommand8 = $cnt2
  seti $cbwcommand9 = $cnt1
  seti $cbwcommand10 = 0
  seti $cbwcommand11 = 0
  seti $cbwcommand12 = 0
  seti $cbwcommand13 = 0
  seti $cbwcommand14 = 0
  seti $cbwcommand15 = 0
  seti $cbwcommand16 = 0

  gosub setcbwbuffer
  gettime
  echo $date
  printusbbuffer 0 31
  usbrawwrite
  echo "write cbw return=" $usb_return_status
  usbbuffersize $cbwdatalen
  gettime
  echo $date
  usbrawread
  echo "read data return=" $usb_return_status
  if $usb_return_status > 0
    if $usb_return_status > $cbwdatalen
      echo "error: return is greater than expected size"
    else
      #printusbbuffer 0 $cbwdatalen
      seti $cswsuccess = 0
      seti $cswgood = 0
      gosub getcsw
      if $cswgood = 0
        echo "failed to get valid csw"
      elseif $cswsuccess = 0
        echo "csw reports error"
        seti $badread = 1
      endif
    endif
  elseif $usb_return_status < 0
    echo "read error, performing force reset"
    gosub forcereset
  else
    echo "read error, performing in reset"
    gosub endpointinreset
  endif
  gettime
  echo $date
endsubroutine

subroutine getcsw
  gosub autoreset
  usbbuffersize 13
  usbrawread
  echo "read csw return=" $usb_return_status
  if $usb_return_status = 13
    printusbbuffer 0 13
    seti $cswsig = usbbuffer 0 dw
    if $cswsig = 0x53425355
      echo "good cswsig"
      seti $cswgood = 1
      seti $cswstatus = usbbuffer 12 b
      if $cswstatus = 0
        echo "csw success"
        seti $cswsuccess = 1
      else
        echo "csw fail"
      endif
    else
      echo "bad cswsig"
    endif
  else
    echo "bad cswsig"
  endif
endsubroutine

subroutine softreset12
  seti $cbwcommand2 = 0x02
  gosub reset12
endsubroutine

subroutine hardreset12
  seti $cbwcommand2 = 0x00
  gosub reset12
endsubroutine

subroutine reset12
  seti $cbwtag = $cbwtag + 1
  gosub setcbwtag
  seti $cbwdatalen = 0
  gosub setcbwdatalen
  seti $cbwflags = $USB_DIR_IN
  seti $cbwcommandlen = 12
  seti $cbwcommand1 = 0xa1

  seti $cbwcommand3 = 0
  seti $cbwcommand4 = 0
  seti $cbwcommand5 = 0
  seti $cbwcommand6 = 0
  seti $cbwcommand7 = 0
  seti $cbwcommand8 = 0
  seti $cbwcommand9 = 0
  seti $cbwcommand10 = 0
  seti $cbwcommand11 = 0
  seti $cbwcommand12 = 0
  seti $cbwcommand13 = 0
  seti $cbwcommand14 = 0
  seti $cbwcommand15 = 0
  seti $cbwcommand16 = 0
  gosub setcbwbuffer

  gettime
  echo $date
  printusbbuffer 0 31
  usbrawwrite
  echo "write cbw return=" $usb_return_status

  seti $cswsuccess = 0
  seti $cswgood = 0
  gosub getcsw
  if $cswgood = 0
    echo "failed to get valid csw"
  elseif $cswsuccess = 0
    echo "csw reports error"
  endif

  gettime
  echo $date
endsubroutine

subroutine enablevsc
  seti $cbwtag = $cbwtag + 1
  gosub setcbwtag
  seti $cbwdatalen = 0
  gosub setcbwdatalen
  seti $cbwflags = $USB_DIR_IN
  seti $cbwcommandlen = 12
  seti $cbwcommand1 = 0xa1
  seti $cbwcommand2 = 0x08
  seti $cbwcommand3 = 0x0e
  seti $cbwcommand4 = 0x45
  seti $cbwcommand5 = 0x0b
  seti $cbwcommand6 = 0x00
  seti $cbwcommand7 = 0x44
  seti $cbwcommand8 = 0x57
  seti $cbwcommand9 = 0xa0
  seti $cbwcommand10 = 0x80
  seti $cbwcommand11 = 0
  seti $cbwcommand12 = 0
  seti $cbwcommand13 = 0
  seti $cbwcommand14 = 0
  seti $cbwcommand15 = 0
  seti $cbwcommand16 = 0
  gosub setcbwbuffer

  gettime
  echo $date
  printusbbuffer 0 31
  usbrawwrite
  echo "write cbw return=" $usb_return_status

  seti $cswsuccess = 0
  seti $cswgood = 0
  gosub getcsw
  if $cswgood = 0
    echo "failed to get valid csw"
  elseif $cswsuccess = 0
    echo "csw reports error"
  endif

  gettime
  echo $date
endsubroutine


subroutine setcbwbuffer
  usbbuffersize 31
  setusbbuffer 0
    $cbwsig1 $cbwsig2 $cbwsig3 $cbwsig4
    $cbwtag1 $cbwtag2 $cbwtag3 $cbwtag4
    $cbwdatalen1 $cbwdatalen2 $cbwdatalen3 $cbwdatalen4
    $cbwflags
    0
    $cbwcommandlen
    $cbwcommand1 $cbwcommand2 $cbwcommand3 $cbwcommand4
    $cbwcommand5 $cbwcommand6 $cbwcommand7 $cbwcommand8
    $cbwcommand9 $cbwcommand10 $cbwcommand11 $cbwcommand12
    $cbwcommand13 $cbwcommand14 $cbwcommand15 $cbwcommand16
  endusbbuffer
endsubroutine


subroutine readlog
  seti $ask = 0
  while 1 = 1
    variablecheck $log
    if $error_level < 16
      seti $ask = 1
    elseif $using_menu = 1
      seti $ask = 1
    endif
    if $ask = 1
      echo "Enter log number in decimal (0x for hex):"
      userinput $choicestring
      if $choicestring != $null
        seti $log = $choicestring 0
        if $log < 256
          break
        else
          echo "Value cannot be above 255 (0xff)"
        endif
      else
        echo "Choice cannot be blank!"
      endif
    else
      break
    endif
  done

  # set the buffer size
  seti $buffersize = 512
  buffersize $buffersize

  # set the LBAs to 0
  seti $LBAlow = 0
  seti $LBAmid = 0
  seti $LBAhigh = 0

  # set some things to read log 0
  gosub setmodereadpio
  # set features to 0
  seti $features = 0
  # set count to 1
  seti $count = 1
  # set device to 0
  seti $device = 0
  # set lba to log page 0
  seti $LBAlow = 0x0
  # set the command for read log extended
  seti $command = 0x2f

  # perform the command
  #ata48cmd $features $count $LBAhigh $LBAmid $LBAlow $device $command
  gosub atapass16


  #echo "Raw buffer:"
  #printbuffer 0 512

  if $log > 0
    seti $temp = $log * 2
    seti $logsize = buffer $temp w
  else
    seti $logsize = 1
  endif
  if $logsize > 0
    seti $buffersize = $logsize * 512
    buffersize $buffersize
    # set lba to log page
    seti $LBAlow = $log
    # set count to log size
    seti $count = $logsize
    # perform the command
    #ata48cmd $features $count $LBAhigh $LBAmid $LBAlow $device $command
    gosub atapass16
    echo "Raw buffer:"
    printbuffer 0 $buffersize
  endif

  if $log = 0
    seti $log = 1
    while $log < 256
      seti $temp = $log * 2
      seti $logsize = buffer $temp w
      if $logsize > 0
        hex
        echo "log 0x" $log " = 0x" $logsize
        decimal
      endif
    seti $log = $log + 1
    done
  endif

  if $log = 3
    seti $logpage = 0
    seti $logerrcount = 1
    while $logpage < $logsize
      seti $logposition = $logpage * 512
      if $logpage = 0
        seti $logindex = buffer 2 w
        echo "log index = " $logindex
      endif
      seti $errstructcount = 0
      while $errstructcount < 4
        seti $errstructoffset = $logposition + 4
        seti $tempoffset = $errstructcount * 124
        seti $errstructoffset = $errstructoffset + $tempoffset
        echo "error structure " $logerrcount
        seti $datastructcount = 0
        while $datastructcount < 6
          seti $tempoffset = $datastructcount * 18
          seti $datastructoffset = $errstructoffset + $tempoffset
          printbuffer $datastructoffset 16
          seti $datastructcount = $datastructcount + 1
        done
        seti $errstructcount = $errstructcount + 1
        seti $logerrcount = $logerrcount + 1
      done
      seti $logpage = $logpage + 1
    done
  endif
endsubroutine


subroutine atareadpio16
  gosub setmodereadpio
  seti $buffersize = $count * 512
  seti $features = 0
  seti $LBAlow = $sector & 0xffff
  seti $temp = $sector > 16
  seti $LBAmid = $temp & 0xffff
  seti $temp = $sector > 32
  seti $LBAhigh = $temp & 0xffff
  seti $device = 0xe0
  seti $command = 0x24
  gosub atapass16
endsubroutine


subroutine atareaddma16
  gosub setmodereaddma
  seti $buffersize = $count * 512
  seti $features = 0
  seti $LBAlow = $sector & 0xffff
  seti $temp = $sector > 16
  seti $LBAmid = $temp & 0xffff
  seti $temp = $sector > 32
  seti $LBAhigh = $temp & 0xffff
  seti $device = 0xe0
  seti $command = 0x25
  gosub atapass16
endsubroutine


subroutine setmodereadpio
  seti $passprotocol = 4
  seti $passprotocol = $passprotocol < 1
  seti $passflags = 0x0e
endsubroutine


subroutine setmodereaddma
  seti $passprotocol = 10
  seti $passprotocol = $passprotocol < 1
  seti $passflags = 0x0e
endsubroutine


subroutine atapass12
  seti $cbwtag = $cbwtag + 1
  gosub setcbwtag
  seti $cbwdatalen = $buffersize
  gosub setcbwdatalen
  seti $cbwflags = $USB_DIR_IN
  seti $cbwcommandlen = 12
  seti $cbwcommand1 = 0xa1
  seti $cbwcommand2 = $passprotocol
  seti $cbwcommand3 = $passflags
  seti $cbwcommand4 = $features
  seti $cbwcommand5 = $count
  seti $cbwcommand6 = $LBAlow
  seti $cbwcommand7 = $LBAmid
  seti $cbwcommand8 = $LBAhigh
  seti $cbwcommand9 = $device
  seti $cbwcommand10 = $command
  seti $cbwcommand11 = 0
  seti $cbwcommand12 = 0
  seti $cbwcommand13 = 0
  seti $cbwcommand14 = 0
  seti $cbwcommand15 = 0
  seti $cbwcommand16 = 0
  gosub setcbwbuffer
  printusbbuffer 0 31
  usbrawwrite
  echo "write cbw return=" $usb_return_status

  usbbuffersize $cbwdatalen
  usbrawread
  echo "read data return=" $usb_return_status
  copyusbtobuffer 0 0 $buffersize

  seti $cswsuccess = 0
  seti $cswgood = 0
  gosub getcsw
  if $cswgood = 0
    echo "failed to get valid csw"
  elseif $cswsuccess = 0
    echo "csw reports error"
  endif

endsubroutine


subroutine atapass16
  seti $cbwtag = $cbwtag + 1
  gosub setcbwtag
  seti $cbwdatalen = $buffersize
  gosub setcbwdatalen
  seti $cbwflags = $USB_DIR_IN
  seti $cbwcommandlen = 16
  seti $cbwcommand1 = 0x85
  seti $cbwcommand2 = $passprotocol | 1
  seti $cbwcommand3 = $passflags
  seti $cbwcommand4 = $features > 8
  seti $cbwcommand5 = $features & 0xff
  seti $cbwcommand6 = $count > 8
  seti $cbwcommand7 = $count & 0xff
  seti $cbwcommand8 = $LBAlow > 8
  seti $cbwcommand9 = $LBAlow & 0xff
  seti $cbwcommand10 = $LBAmid > 8
  seti $cbwcommand11 = $LBAmid & 0xff
  seti $cbwcommand12 = $LBAhigh > 8
  seti $cbwcommand13 = $LBAhigh & 0xff
  seti $cbwcommand14 = $device
  seti $cbwcommand15 = $command
  seti $cbwcommand16 = 0
  gosub setcbwbuffer
  printusbbuffer 0 31
  usbrawwrite
  echo "write cbw return=" $usb_return_status

  usbbuffersize $cbwdatalen
  usbrawread
  echo "read data return=" $usb_return_status
  #copyusbtobuffer 0 0 $buffersize

  seti $cswsuccess = 0
  seti $cswgood = 0
  gosub getcsw
  if $cswgood = 0
    echo "failed to get valid csw"
  elseif $cswsuccess = 0
    echo "csw reports error"
  endif
endsubroutine


subroutine requestsense
  seti $cbwtag = $cbwtag + 1
  gosub setcbwtag
  seti $cbwdatalen = 18
  gosub setcbwdatalen
  seti $cbwflags = $USB_DIR_IN
  seti $cbwcommandlen = 6
  seti $cbwcommand1 = 0x03
  seti $cbwcommand2 = 0
  seti $cbwcommand3 = 0
  seti $cbwcommand4 = 0
  seti $cbwcommand5 = 18
  seti $cbwcommand6 = 0

  gosub setcbwbuffer
  printusbbuffer 0 31
  usbrawwrite
  echo "write cbw return=" $usb_return_status
  usbbuffersize $cbwdatalen
  usbrawread
  echo "read data return=" $usb_return_status
  if $usb_return_status > 0
    if $usb_return_status > $cbwdatalen
      echo "error: return is greater than expected size"
    else
      printusbbuffer 0 $cbwdatalen
      seti $cswsuccess = 0
      seti $cswgood = 0
      gosub getcsw
      if $cswgood = 0
        echo "failed to get valid csw"
      elseif $cswsuccess = 0
        echo "csw reports error"
        seti $badread = 1
      endif
    endif
  endif
endsubroutine


subroutine dousbreset
  usbreset
  
endsubroutine





previousscript
end












