

seti $printhelp = $printhelp
seti $help = $help
if $help = 1
  echo 'WD find cylinder count'

  if $printhelp = 1
    exit 0
  endif
  echo 'Hit ENTER to continue...'
  userinput $choicestring
  previousscript
endif
echo ""

include good_subroutines

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

# set this to itself for use with the menu system
# make sure not to set this anyplace else
# if it was not previously set it will = 0
seti $using_menu = $using_menu



# set these for later, and to make it easy to change if needed
seti $initial_read_size = 512
seti $cylinder_start = -1
seti $sector_start = 1
seti $head_start = 0


gosub find_cylinders



previousscript
end





subroutine find_cylinders
seti $command_failed = 0
seti $cylinder_status = 0
seti $cylinder = $cylinder_start
seti $last_good_cylinder = 0

while $cylinder > -1025
  gosub check_cylinder
  if $cylinder_status = 0
    #break
  endif
  seti $cylinder = $cylinder - 1
done
echo "last last_good_cylinder=" $last_good_cylinder

endsubroutine






subroutine check_cylinder
  gosub enable_vsc

  seti $sector = $sector_start
  seti $head = $head_start
  seti $read_size = $initial_read_size

    # vsc to prepare for chs read
    #echo "prepare to read chs"
    buffersize 0x200
    clearbuffer
    seti $offsetl = $cylinder & 0xff
    seti $offsetm1 = $cylinder > 8
    seti $offsetm1 = $offsetm1 & 0xff
    seti $offsetm2 = $cylinder > 16
    seti $offsetm2 = $offsetm2 & 0xff
    seti $offseth = $cylinder > 24
    seti $offseth = $offseth & 0xff

    seti $sectorl = $sector & 0xff
    seti $sectorh = $sector > 8
    seti $sectorh = $sectorh & 0xff

    seti $sizel = 0
    seti $sizem1 = 0
    seti $sizem2 = 0
    seti $sizeh = 0

    setbuffer 0
      0c 00  01 00  $offsetl $offsetm1  $offsetm2 $offseth  $head 00  $sectorl $sectorh  $sizel $sizem1 $sizem2 $sizeh
    endbuffer
    hex
    #echo "keysector= 0c 00  01 00  " $offsetl " " $offsetm1 " " $offsetm2 " " $offseth "  " $head " 00  " $sectorl " " $sectorh "  " $sizel " " $sizem1 " " $sizem2 " " $sizeh
    setwritepio
    ata28cmd 0xd6 0x01 0xbe 0x4f 0xc2 0xa0 0xb0
    #echo "command= d6 01 be 4f c2 a0 b0"
    decimal
    # check if command failed
    gosub status_check_no_stop_no_show

    # vsc to read chs
    seti $sectors = $read_size / 512
    buffersize $read_size
    clearbuffer
    setreadpio
    #echo "reading chs"
    ata28cmd 0xd5 $sectors 0xbf 0x4f 0xc2 0xa0 0xb0
    hex
    #echo "command= d5 " $sectors " bf 4f c2 a0 b0"
    decimal
    # check if command failed
    gosub status_check_no_stop_no_show
    if $command_failed != 0
      seti $cylinder_status = 0
      sets $cstatus = " fail"
    else
      seti $cylinder_status = 1
      seti $last_good_cylinder = $cylinder
      sets $cstatus = " ok"
    endif
    echo "cylinder=" $cylinder $cstatus

  gosub disable_vsc
endsubroutine






subroutine status_check
  seti $command_failed = 0
  gosub check_command_status
  if $command_failed = 1
    echo "Command failed!"
    gosub show_sense_data
    gosub show_ata_return_status
    previousscript
  endif
endsubroutine



subroutine status_check_no_stop
  seti $command_failed = 0
  gosub check_command_status
  if $command_failed = 1
    echo "Command failed!"
    gosub show_sense_data
    gosub show_ata_return_status
  endif
endsubroutine



subroutine status_check_no_stop_no_show
  seti $command_failed = 0
  gosub check_command_status
  if $command_failed = 1
    #echo "Command failed!"
    #gosub show_sense_data
    #gosub show_ata_return_status
  endif
endsubroutine



subroutine enable_vsc
  # enable vendor specific commands
  #echo "enable vsc"
  buffersize 0
  setreadpio
  ata28cmd 0x45 0x0b 0x00 0x44 0x57 0xa0 0x80
  # check if command failed
  gosub status_check
endsubroutine



subroutine disable_vsc
  # disable vendor specific commands
  #echo "disable vsc"
  buffersize 0
  setreadpio
  ata28cmd 0x44 0x0b 0x00 0x44 0x57 0xa0 0x80
  # check if command failed
  gosub status_check
endsubroutine



