seti $printhelp = $printhelp
seti $help = $help
if $help = 1
  echo 'Unlock the disk using 28 bit security unlock pio data-out command.'
  echo 'This requires either the string variable "password" or "hex" to be set.'
  echo 'Password is the password in plain text.'
  echo 'Hex is the password in hex. It does not require 0x to proceed numbers.'
  echo 'If neither password or hex is set then the password will be blank (all 0s).'
  echo 'DO NOT SET BOTH or you could end up with undesired results!'
  echo 'This requires the number variable "type" to be set on the command line.'
  echo 'Type is either 0 for user password or 1 for master password.'
  echo 'If type is not set it will default to 0.'
  echo 'Example: hddsupertool -t /dev/sdb -f ata_security_unlock password=="abcd" type=0'
  echo 'Example: hddsupertool -t /dev/sdb -f ata_security_unlock hex=="0a 0b 0c 0d" type=0'
  if $printhelp = 1
    exit 0
  endif
  echo 'Hit ENTER to continue...'
  userinput $choicestring
  previousscript
endif

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


# find out if using menus or if variables were set on the command line, and ask for input if they were not set

seti $ask = 0
while 1 = 1
  variablecheck $password
  if $error_level < 16
    seti $ask = 1
  elseif $using_menu = 1
    seti $ask = 1
  endif
  if $ask = 1
    echo "Enter password in ascii, or leave blank to set hex:"
    userinput $choicestring
    if $choicestring != $null
      sets $password = $choicestring
      break
    else
      sets $password = ""
      break
    endif
  else
    break
  endif
done

seti $ask = 0
while 1 = 1
  variablecheck $hex
  if $error_level < 16
    seti $ask = 1
  elseif $using_menu = 1
    seti $ask = 1
  endif
  if $ask = 1
    echo "Enter password in hex, or leave blank if ascii:"
    userinput $choicestring
    if $choicestring != $null
      sets $hex = $choicestring
      break
    else
      sets $hex = ""
      break
    endif
  else
    break
  endif
done

seti $ask = 0
while 1 = 1
  variablecheck $type
  if $error_level < 16
    seti $ask = 1
  elseif $using_menu = 1
    seti $ask = 1
  endif
  if $ask = 1
    echo "Enter type (0 for user, 1 for master):"
    userinput $choicestring
    if $choicestring != $null
      seti $type = $choicestring 0
      break
    else
      break
    endif
  else
    break
  endif
done

# check if both regular and hex passwords were set
if $password != $null
  if $hex != $null
    echo "Error! Both password and hex were set!"
    previousscript
  endif
endif




# set the buffer size
buffersize 512
# clear the buffer to 0's
clearbuffer


# put the type in the buffer
seti $byte0 = $type & 0x01
setbuffer 0
  $byte0
endbuffer

# put the hex (up to 32 characters) in the buffer
# force reading has hex so it does not require 0x
hex
seti $b0 = $hex 0
seti $b1 = $hex 1
seti $b2 = $hex 2
seti $b3 = $hex 3
seti $b4 = $hex 4
seti $b5 = $hex 5
seti $b6 = $hex 6
seti $b7 = $hex 7
seti $b8 = $hex 8
seti $b9 = $hex 9
seti $b10 = $hex 10
seti $b11 = $hex 11
seti $b12 = $hex 12
seti $b13 = $hex 13
seti $b14 = $hex 14
seti $b15 = $hex 15
seti $b16 = $hex 16
seti $b17 = $hex 17
seti $b18 = $hex 18
seti $b19 = $hex 19
seti $b20 = $hex 20
seti $b21 = $hex 21
seti $b22 = $hex 22
seti $b23 = $hex 23
seti $b24 = $hex 24
seti $b25 = $hex 25
seti $b26 = $hex 26
seti $b27 = $hex 27
seti $b28 = $hex 28
seti $b29 = $hex 29
seti $b30 = $hex 30
seti $b31 = $hex 31
decimal

setbuffer 2
  $b0  $b1  $b2  $b3  $b4  $b5  $b6  $b7  $b8  $b9  $b10 $b11 $b12 $b13 $b14 $b15
  $b16 $b17 $b18 $b19 $b20 $b21 $b22 $b23 $b24 $b25 $b26 $b27 $b28 $b29 $b30 $b31
endbuffer

# put the password (up to 32 characters) in the buffer
# if set this will overwrite part of all of the hex password
stringtobuffer 2 32 $password

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

# set some things for the ata command
setwritepio
# set features to 0
seti $features = 0
# set device bits 7(compatibility) 5(compatibility)
seti $device = 0xa0
# set the command for security unlock pio data-out
seti $command = 0xf2

# perform the command
ata28cmd $features $count $LBAlow $LBAmid $LBAhigh $device $command

# check if command failed
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

echo "Command completed"

previousscript
end