;
; Salvator-X のフラッシュイメージを書き込みます。
; PROGRAM_ADDRESS_TABLE, FLASH_ADDRESS_TABLE, FILE_NAME_TABLE を適切に
; 設定してこのマクロを実行してください。
; 各ファイルは書き込み前にダイアログボックスで確認がでます。
; y と入力すると書き込みが実行され、それ以外ではスキップできます。
;

getdir BASE_PATH
strconcat BASE_PATH "\"

strdim PROGRAM_ADDRESS_TABLE 6
strdim FLASH_ADDRESS_TABLE 6
strdim FILE_NAME_TABLE 6

PROGRAM_ADDRESS_TABLE[0]="e6320000"
PROGRAM_ADDRESS_TABLE[1]="e6302000"
PROGRAM_ADDRESS_TABLE[2]="e6320000"
PROGRAM_ADDRESS_TABLE[3]="44000000"
PROGRAM_ADDRESS_TABLE[4]="44100000"
PROGRAM_ADDRESS_TABLE[5]="49000000"

FLASH_ADDRESS_TABLE[0] = "000000"
FLASH_ADDRESS_TABLE[1] = "040000"
FLASH_ADDRESS_TABLE[2] = "180000"
FLASH_ADDRESS_TABLE[3] = "1C0000"
FLASH_ADDRESS_TABLE[4] = "200000"
FLASH_ADDRESS_TABLE[5] = "640000"

; ##### FILE_NAME_TABLE[X]をコメント化すると書き込みスキップします
;;;;;;;;;;;
;FILE_NAME_TABLE[0] = "bootparam_sa0.srec"
;FILE_NAME_TABLE[1] = "bl2.srec"
;FILE_NAME_TABLE[2] = "cert_header_sa6.srec"
;FILE_NAME_TABLE[3] = "bl31.srec"
;FILE_NAME_TABLE[4] = "tee.srec"
;FILE_NAME_TABLE[5] = "u-boot.srec"

FILE_NAME_TABLE[0] = "bootparam_sa0.srec"
FILE_NAME_TABLE[1] = "bl2-salvator-x.srec"
FILE_NAME_TABLE[2] = "cert_header_sa6.srec"
FILE_NAME_TABLE[3] = "bl31-salvator-x.srec"
FILE_NAME_TABLE[4] = "tee-salvator-x.srec"
FILE_NAME_TABLE[5] = "u-boot-elf.srec"


; #####

setsync 1

for i 0 5
  strmatch FILE_NAME_TABLE[i] ".srec"
  if result then
    PROGRAM_ADDRESS=PROGRAM_ADDRESS_TABLE[i]
    FLASH_ADDRESS=FLASH_ADDRESS_TABLE[i]
    FILE_NAME=FILE_NAME_TABLE[i]

    sprintf '0x%s,0x%s,%s, Write OK?(y/n)' PROGRAM_ADDRESS FLASH_ADDRESS FILE_NAME
    inputbox inputstr "debug"
    strmatch inputstr "y"
    if result then
      call write_flash
    endif
  endif
next

exit

; #####
; Flash Writerを起動するルーチン
; #####

:launch_writer

sendln "xls2"

waitregex "Select \(1-3\)>"
sendln "3"

waitregex "SW1 SW2 All OFF!"
sendln "Y"

waitregex "SW3 ON!"
sendln "Y"

waitregex "Please Input"

return

; #####
; 書き込み処理を実行するルーチン
; 以下の引数に適切な設定をしておく必要がある。
;  PROGRAM_ADDRESS
;  BASE_PATH
;  FLASH_ADDRESS
;  FILE_NAME
; #####

:write_flash

call launch_writer

sendln PROGRAM_ADDRESS
waitregex "Please Input"

sendln FLASH_ADDRESS
waitregex "please send"

FILE_PATH=BASE_PATH
strconcat FILE_PATH FILE_NAME
sendfile FILE_PATH 1
waitregex "SPI Data Clear"

sendln "y"
waitregex ">"

return
