seti $printhelp = $printhelp
seti $help = $help
if $help = 1
  echo 'Western Digital ROYL dump module 02 to file using vendor specific commands.'
  echo 'By default this will dump the sectors of the module to the file "mod02.bin".'
  echo 'It will also display the data on the screen.'
  echo 'Note that some USB drives do not support these vendor specific commands.'
  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

seti $ask = 0
variablecheck $file
if $error_level < 16
  seti $ask = 1
elseif $using_menu = 1
  seti $ask = 1
endif
if $ask = 1
  echo "Enter file name, or leave blank to show data on screen:"
  userinput $choicestring
  sets $file = $choicestring
endif

getfilesize $file
if $error_level < 0
  echo "File '" $file "' not found."
  previousscript
else
  seti $buffersize = $error_level
  buffersize $buffersize
  seti $scratchpad_size = $buffersize
  scratchpadsize $scratchpad_size
  clearbuffer
  readbuffer $file 0 0 $buffersize
  echo "Data read from file: " $file
endif

copybuffertoscratchpad 0 0 $buffersize

seti $mod_length_sectors = $buffersize / 512


# process the module
echo "Header:"
printscratchpad 0 4
seti $mod_id = scratchpad 8 w
echo "Module ID = " $mod_id
echo "Size in sectors = " $mod_length_sectors
seti $checksum = scratchpad 0xc dw
hex
echo "32 bit checksum = 0x" $checksum
decimal
echo "Mod version:"
printscratchpad 0x10 8
seti $total_records = scratchpad 0x30 w
echo "Total records = " $total_records

# process the data records
seti $count = 0
while $count < $total_records
  seti $offset = $count * 4
  seti $count = $count + 1
  seti $offset = $offset + 0x32
  seti $record_offset = scratchpad $offset w
  seti $offset = $offset + 2
  seti $record_size = scratchpad $offset w
  echo ""
  echo "Data record #" $count ":"
  printscratchpad $record_offset $record_size
done

# show the user and master passwords
seti $offset = 18 * 4
seti $offset = $offset + 0x32
seti $record_offset = scratchpad $offset w
seti $record_offset = $record_offset + 4
echo ""
echo "User password:"
printscratchpad $record_offset 32
seti $record_offset = $record_offset + 32
echo "Master password:"
printscratchpad $record_offset 32

# calculate checksum
seti $calculated_checksum = 0
seti $count = 0
while $count < $scratchpad_size
  seti $dword = scratchpad $count dw
  seti $calculated_checksum = $calculated_checksum + $dword
  seti $count = $count + 4
  # skip the actual checksum bytes
  if $count = 0xc
    seti $count = $count + 4
  endif
done
seti $calculated_checksum = $calculated_checksum & 0xffffffff
seti $calculated_checksum = 0xffffffff - $calculated_checksum
seti $calculated_checksum = $calculated_checksum + 1
if $checksum = $calculated_checksum
  sets $message = " (good)"
else
  sets $message = " (BAD)"
endif
hex
echo
echo "Calculated Checksum = 0x" $calculated_checksum $message
decimal



previousscript
end

