seti $printhelp = $printhelp
seti $help = $help
if $help = 1
  echo 'Make a bad sector using the old 28 bit writelong command.'
  echo 'This command is obsolete and not supported on all drives.'
  echo 'This requires number variable "sector" to be set.'
  echo '"sector" is the starting sector to write.'
  echo 'Example: hddsupertool -t /dev/sda -f ata28_makebad sector=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 $sector
  if $error_level < 16
    seti $ask = 1
  elseif $using_menu = 1
    seti $ask = 1
  endif
  if $ask = 1
    echo "Enter starting sector:"
    userinput $choicestring
    if $choicestring != $null
      seti $sector = $choicestring 0
      break
    else
      echo "Choice cannot be blank!"
    endif
  else
    break
  endif
done

seti $ask = 0
variablecheck $yes
if $error_level < 16
  seti $ask = 1
elseif $using_menu = 1
  seti $ask = 1
endif
if $ask = 1
  echo "You are about to perform a data changing command,"
  echo 'type "YES" to continue:'
  userinput $choicestring
  if $choicestring != yes
    if $choicestring != YES
      echo "You chose not to continue."
      previousscript
    endif
  endif
endif



# set the buffer size to one sector plus 8 ecc bytes
seti $buffersize = 512 + 8
buffersize $buffersize

# fill the first 512 buffer bytes with data
setbuffer 0
  a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 a5
  a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 a5
  a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 a5
  a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 a5
  a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 a5
  a5 a5 a5 a5 a5 a5 a5 a5 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5 a5 a5 a5 a5 a5 a5 a5 a5 a5
  a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 a5
  a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 a5
  a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 a5
  a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 a5
  a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 a5
endbuffer
# fill the ecc part of the buffer with data
setbuffer 0x200
  a5 a5 a5 a5 a5 a5 a5 a5
endbuffer


# set the LBAs to the proper sector start
seti $LBAlow = $sector & 0xff
seti $temp = $sector > 8
seti $LBAmid = $temp & 0xff
seti $temp = $sector > 16
seti $LBAhigh = $temp & 0xff
seti $temp = $sector > 24
seti $LBAdevice = $temp & 0x0f

# set some things for the ata write command
setwritepio
# set features to 0
seti $features = 0
# set the count to 1
seti $count = 1
# set device bits 7(compatibility) 6(LBA mode) 5(compatibility)
seti $device = 0xe0
# set device bits 0-3 to high 4 bits of LBA
seti $device = $device | $LBAdevice
# set the command for writelong
seti $command = 0x32

# 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




