{# device_type: 'vexpress' #}
{% extends 'base.jinja2' %}

{% set vexpress_supports_vemsd = vexpress_supports_vemsd|default(true) %}
{% set vexpress_supports_uboot = vexpress_supports_uboot|default(true) %}
{% set vexpress_supports_uefi = vexpress_supports_uefi|default(true) %}
{% set vexpress_supports_uefi_menu = vexpress_supports_uefi_menu|default(true) %}
{% set vexpress_supports_grub = vexpress_supports_grub|default(false) %}

{% set base_high_limits = base_high_limits|default(true) %}
{% set base_uboot_commands = [
    "setenv bootdelay 1",
    "setenv ethact smc911x-0"]
-%}

{% set base_uboot_high_limits = [
    "setenv initrd_high " + vexpress_uboot_initrd_high|default('0xffffffff'),
    "setenv fdt_high " + vexpress_uboot_fdt_high|default('0xffffffff')]
-%}

{% set base_uboot_tftp_commands = uboot_tftp_commands|default([
    "tftp {KERNEL_ADDR} {KERNEL}",
    "tftp {RAMDISK_ADDR} {RAMDISK}",
    "setenv initrd_size ${filesize}",
    "tftp {DTB_ADDR} {DTB}"])
-%}

{% set base_uboot_bootcmd = uboot_bootcmd|default([
    uboot_bootx_cmd|default('{BOOTX}')])
-%}

{% set base_uboot_usb_commands = uboot_usb_commands|default(["usb start"]) -%}

{% set uboot_needs_usb = uboot_needs_usb|default(false) %}

{% set usb_uuid = usb_uuid|default('') %}
{% set usb_device_id = usb_device_id|default(0) %}

{% set uboot_mac_addr = uboot_mac_addr|default('') %}
{% set base_uboot_set_mac = ["setenv ethaddr " + uboot_mac_addr] -%}

{% set uboot_set_mac = uboot_set_mac|default(false) %}

{% set uboot_common_cmds = [] %}
{% if uboot_needs_usb %}
{% set uboot_common_cmds = uboot_common_cmds + base_uboot_usb_commands %}
{% endif %}
{% set uboot_common_cmds = uboot_common_cmds + base_uboot_commands %}
{% if base_high_limits %}
{% set uboot_common_cmds = uboot_common_cmds + base_uboot_high_limits %}
{% endif %}
{% if uboot_set_mac %}
{% set uboot_common_cmds = uboot_common_cmds + base_uboot_set_mac %}
{% endif %}
{% set uboot_common_cmds = uboot_common_cmds + base_uboot_tftp_commands %}

{% set deploy_character_delay = deploy_character_delay | default(30) %}
{% set boot_character_delay = boot_character_delay | default(30) %}

{# adds the parameters: top level block #}
{% block parameters %}
{{ super() -}}
{% if text_offset %}
  text_offset: '{{text_offset}}'
{% endif %}
{% if vexpress_booti_kernel_addr %}
  image:
    kernel: '{{ booti_kernel_addr|default(vexpress_booti_kernel_addr) }}'
    ramdisk: '{{ booti_ramdisk_addr|default(vexpress_booti_ramdisk_addr) }}'
    dtb: '{{ booti_dtb_addr|default(vexpress_booti_dtb_addr) }}'
  booti:
    kernel: '{{ booti_kernel_addr|default(vexpress_booti_kernel_addr) }}'
    ramdisk: '{{ booti_ramdisk_addr|default(vexpress_booti_ramdisk_addr) }}'
    dtb: '{{ booti_dtb_addr|default(vexpress_booti_dtb_addr) }}'
{% endif %}
{% if vexpress_bootm_kernel_addr %}
  uimage:
    kernel: '{{ bootm_kernel_addr|default(vexpress_bootm_kernel_addr) }}'
    ramdisk: '{{ bootm_ramdisk_addr|default(vexpress_bootm_ramdisk_addr) }}'
    dtb: '{{ bootm_dtb_addr|default(vexpress_bootm_dtb_addr) }}'
  bootm:
    kernel: '{{ bootm_kernel_addr|default(vexpress_bootm_kernel_addr) }}'
    ramdisk: '{{ bootm_ramdisk_addr|default(vexpress_bootm_ramdisk_addr) }}'
    dtb: '{{ bootm_dtb_addr|default(vexpress_bootm_dtb_addr) }}'
{% endif %}
{% if vexpress_bootz_kernel_addr %}
  zimage:
    kernel: '{{ bootz_kernel_addr|default(vexpress_bootz_kernel_addr) }}'
    ramdisk: '{{ bootz_ramdisk_addr|default(vexpress_bootz_ramdisk_addr) }}'
    dtb: '{{ bootz_dtb_addr|default(vexpress_bootz_dtb_addr) }}'
  bootz:
    kernel: '{{ bootz_kernel_addr|default(vexpress_bootz_kernel_addr) }}'
    ramdisk: '{{ bootz_ramdisk_addr|default(vexpress_bootz_ramdisk_addr) }}'
    dtb: '{{ bootz_dtb_addr|default(vexpress_bootz_dtb_addr) }}'
{% endif %}
{%- if sata_uuid or sd_uuid or usb_uuid %}
  media:
{% if sata_uuid %}
    sata:
      UUID-required: False
      {{ sata_label|default('ST160LM003') }}:
        uuid: "{{ sata_uuid }}"
        device_id: "{{ sata_id|default(0) }}"
        uboot_interface: "{{ sata_interface|default('scsi') }}"
{% endif %}
{% if sd_uuid %}
    sd:
      {{ sd_label }}:
        uuid: "{{ sd_uuid }}"
        device_id: "{{ sd_device_id }}"  # the bootloader device id for this media on the 'sd' interface
{% endif %}
{% if usb_uuid %}
    usb:
      {{ usb_label|default('SanDisk_Ultra') }}:
        uuid: "{{ usb_uuid }}"  # /dev/disk/by-id/
        device_id: "{{ usb_device_id }}"  # the bootloader device id for this media on the 'usb' interface
{% endif %}
{%- endif %}
{% endblock parameters %}

{% set action_timeout_auto_login = action_timeout_auto_login | default(5) %}
{% set action_timeout_bootloader_commands = action_timeout_bootloader_commands | default(3) %}
{% set action_timeout_bootloader_interrupt = action_timeout_bootloader_interrupt | default(180) %}
{% set action_timeout_u_boot_interrupt = action_timeout_u_boot_interrupt | default(120) %}
{% set action_timeout_uboot_commands = action_timeout_uboot_commands | default(4) %}
{% set action_timeout_uboot_action = action_timeout_uboot_action | default(4) %}

{% set connection_timeout_auto_login = connection_timeout_auto_login | default(5) %}
{% set connection_timeout_bootloader_commands = connection_timeout_bootloader_commands | default(180) %}
{% set connection_timeout_u_boot_interrupt = connection_timeout_u_boot_interrupt | default(120) %}
{% set connection_timeout_uboot_commands = connection_timeout_uboot_commands | default(4) %}


{% block body %}

usb_filesystem_label: '{{ usb_filesystem_label|default('VEMSD') }}'

{% set console_device = console_device | default('ttyAMA0') %}
{% set baud_rate = baud_rate | default(115200) %}
{% set base_kernel_args = extra_kernel_args | default(vexpress_kernel_args) %}
{% set action_timeout_bootloader_commands = action_timeout_bootloader_commands | default(3) %}
{% set connection_timeout_bootloader_commands = connection_timeout_bootloader_commands | default(30) %}

{% if vexpress_sky2_mac %}
{% set base_kernel_args = base_kernel_args + ' sky2.mac_address=' + vexpress_sky2_mac %}
{% endif %}

actions:
  deploy:
    parameters:
{% if vexpress_supports_uboot %}
      add_header: u-boot
      mkimage_arch: {{ uboot_mkimage_arch|default('arm64') }} # string to pass to mkimage -A when adding UBoot headers
{% endif %}
      append_dtb: {{ append_dtb|default(false) }}
      use_xip: {{ use_xip|default(false) }}
    connections:
      lxc:
      serial:
    methods:
{% if usbg_ms_commands %}
      usbg-ms:
        enable: {{ usbg_ms_commands["enable"] }}
        disable: {{ usbg_ms_commands["disable"] }}
{% endif %}
{% if flasher_deploy_commands %}
      flasher:
        commands: {{ flasher_deploy_commands }}
{% endif %}
      lxc:
      nfs:
      ssh:
        options:
{{ ssh_options }}
        host: '{{ ssh_host|default('') }}'
        port: {{ ssh_port|default(22) }}
        user: {{ ssh_user|default('root') }}
        identity_file: {{ ssh_identity_file }}
{% if usb_uuid %}
      usb:
{% endif %}
      tftp:
{% if vexpress_supports_vemsd %}
      vemsd:
        parameters:
          mcc_prompt: '{{ mcc_prompt|default('Cmd>') }}'
          msd_mount_cmd: '{{ msd_mount_cmd|default('usb_on') }}'
          flash_prompt: '{{ flash_prompt|default('Flash>') }}'
          flash_enter_cmd: '{{ flash_enter_cmd|default('flash') }}'
          flash_erase_cmd: '{{ flash_erase_cmd|default('eraseall') }}'
          flash_erase_msg: '{{ flash_erase_msg|default('Erasing Flash') }}'
          flash_exit_cmd: '{{ flash_exit_cmd|default('exit') }}'
          autorun_prompt: '{{ autorun_prompt|default('Press Enter to stop auto boot...') }}'
          mcc_reset_msg: '{{ mcc_reset_msg|default('Programming NOR Flash')}}'
{% endif %}
  boot:
    connections:
      lxc:
      serial:
    methods:
{% if vexpress_supports_grub %}
      grub:
        parameters:
          needs_interrupt: {{ grub_needs_interrupt|default(true) }}
          interrupt_prompt: '{{ grub_autoboot_prompt | default("for a command-line") }}'
          interrupt_character: '{{ grub_interrupt_character | default ("c") }}'
          bootloader_prompt: {{ grub_bootloader_prompt|default('grub>') }}
          boot_message: {{ boot_message|default('Linux version') }}
        ramdisk:
          commands:
          - insmod tftp
          - insmod efinet
          - net_bootp efinet0
          - set net_default_server={SERVER_IP}
          - linux (tftp,{SERVER_IP})/{KERNEL} console={{ console_device }} {{ base_kernel_args }} {{ base_ip_args }}
          - initrd (tftp,{SERVER_IP})/{RAMDISK}
{% if has_dtb %}
          - devicetree (tftp,{SERVER_IP})/{DTB}
{% endif %}
          - boot
        nfs:
          commands:
          - insmod tftp
          - insmod efinet
          - net_bootp efinet0
          - set net_default_server={SERVER_IP}
          - linux (tftp,{SERVER_IP})/{KERNEL} console={{ console_device }} {{ base_kernel_args }} {{ base_nfsroot_args }} {{ base_ip_args }} root=/dev/nfs rw
          - initrd (tftp,{SERVER_IP})/{RAMDISK}
{% if has_dtb %}
          - devicetree (tftp,{SERVER_IP})/{DTB}
{% endif %}
          - boot
{% endif %}
      minimal:
{% if vexpress_supports_uefi_menu %}
      uefi-menu:
        parameters:
          interrupt_prompt: '{{ interrupt_prompt|default('The default boot selection will start in') }}'
          interrupt_string: '{{ interrupt_string|default(' ') }}'
          item_markup:
            - "["
            - "]"
          item_class: '{{ item_class|default('0-9') }}'
          separator: '{{ separator|default(' ') }}'
          label_class: '{{ label_class|default('a-zA-Z0-9 \\s\\:') }}'
          bootloader_prompt: '{{ uefi_menu_bootloader_prompt|default('Start:') }}'
        nfs:
        - select:
            items:
            - 'Boot Manager'
            wait: "Choice:"
        - select:
            items:
               - 'Add Boot Device Entry'
            wait: "Select the Boot Device:"
        - select:
            enter: '3'  # Resolves to 1st NOR Flash device
            wait: "File path of the EFI Application or the kernel:"
        - select:
            enter: '{{ kernel_filename | default("zImage") }}'
            wait: 'Is an EFI Application?'
        - select:
            enter: n
            wait: 'Has FDT support?'
        - select:
            enter: y
            wait: "Add an initrd:"
        - select:
            enter: y
        - select:
            enter: '{{ ramdisk_filename | default("ramdisk.img") }}'
            wait: "Arguments to pass to the binary:"
        - select:
            enter: "console={{ console_device }},{{ baud_rate }}n8 root=/dev/nfs rw {{ base_nfsroot_args }} {{ base_kernel_args }} {{ base_ip_args }}"
            wait: "Description for this new Entry:"
        - select:
            enter: LAVA TEST NFS
            wait: "Choice:"
        - select:
            items:
              - "Return to main menu"
            wait: "Start:"
        - select:
            items:
              - LAVA TEST NFS
        tftp:
        - select:
            items:
             - 'Boot Manager'
            wait: "Choice:"
        - select:
            items:
               - 'Add Boot Device Entry'
            wait: "Select the Boot Device:"
        - select:
            items:
               - 'TFTP on MAC Address: {{ device_mac }}'
            wait: "Get the IP address from DHCP:"
        - select:
            enter: y
            wait: "Get the TFTP server IP address:"
        - select:
            enter: '{SERVER_IP}'
            wait: "File path of the EFI Application or the kernel:"
        - select:
            enter: '{KERNEL}'
            wait: 'Is an EFI Application?'
        - select:
            enter: n
            wait: 'Has FDT support?'
        - select:
            enter: y
            wait: "Add an initrd:"
        - select:
            enter: n
            wait: "Arguments to pass to the binary:"
        - select:
            enter: "console={{ console_device }},{{ baud_rate }}n8 root=/dev/nfs rw {{ base_nfsroot_args }} {{ base_kernel_args }} {{ base_ip_args }}"
            wait: "Description for this new Entry:"
        - select:
            enter: LAVA TEST TFTP
            wait: "Choice:"
        - select:
            items:
              - "Update FDT path"
            wait: "Select the Boot Device"
        - select:
            items:
               - 'TFTP on MAC Address: {{ device_mac }}'  # substitute the MAC in the template
            wait: "Get the IP address from DHCP:"
        - select:
            enter: "y"
            wait: "Get the TFTP server IP address"
        - select:
            enter: "{SERVER_IP}"
            wait: "File path of the FDT blob :"
        - select:
            enter: "{DTB}"
            wait: "Choice:"
        - select:
            items:
              - "Return to main menu"
            wait: "Start:"
        - select:
            items:
              - LAVA TEST TFTP
{% endif %}
{% if vexpress_supports_uefi %}
      uefi:
        parameters:
           bootloader_prompt: '{{ uefi_shell_prompt|default('Shell>') }}'
           shell_interrupt_prompt: '{{ shell_interrupt_prompt|default('any other key to continue') }}'
           shell_interrupt_string: '{{ shell_interrupt_string|default(' ') }}'
           boot_message: '{{ boot_message|default('Linux version') }}'
           shell_menu: shell
           # Menu options
           interrupt_prompt: The default boot selection will start in
           interrupt_string: '{{ interrupt_string|default(' ') }}'
           menu_boot_message: '{{ menu_boot_message|default('UEFI Interactive Shell') }}'
           menu_prompt: '{{ menu_prompt|default('Start:') }}'
           item_markup:
           - '['
           - ']'
           item_class: '{{ item_class|default('0-9') }}'
           separator: '{{ separator|default(' ') }}'
           line_separator: '{{ line_separator|default('dos') }}'
           label_class: '{{ label_class|default('a-zA-Z0-9\\s\\:') }}'
        shell:
        - select:
            items:
            - 'Shell'
        nfs:
          commands:
          - norkern console={{ console_device }},{{ baud_rate }}n8 root=/dev/nfs rw {{ base_nfsroot_args }} {{ base_kernel_args }} {{ base_ip_args }} dtb=board.dtb
{% if uefi_usb_id %}
        usb:
          # This assumes that an image has been dd'ed to the USB device
          # and that this device then contains two partitions, sda1 and sda2.
          # The first, is the boot device, which appears in UEFI as <uefi_usb_id>.
          # The second is the root filesystem.
          commands:
          # Go to usb boot partition
          - "{{ uefi_usb_id }}:"
          # Launch Image from boot partition, specifying sda2 as the root filesystem.
          - Image console={{ console_device }},{{ baud_rate }}n8 root=/dev/sda2 rw {{ base_nfsroot_args }} {{ base_kernel_args }} {{ base_ip_args }} dtb={{ device_type }}.dtb
{% endif %}
{% endif %}
      ssh:
{% if vexpress_supports_uboot %}
      u-boot:
        parameters:
          mkimage_arch: {{ uboot_mkimage_arch|default('arm') }}
          bootloader_prompt: {{ bootloader_prompt|default('VExpress64') }}
          interrupt_prompt: {{ interrupt_prompt|default('Hit any key to stop autoboot') }}
          interrupt_char: "{{ interrupt_char|default('') }}"
          boot_message: {{ boot_message|default('Booting Linux') }}
        defaults:
          commands:
          - "boot"
        nfs:
          commands:
          - "setenv serverip {SERVER_IP}"
          - "setenv autoload no"
          - "{{ uboot_ipaddr_cmd|default('dhcp') }}"
          - "{{ uboot_common_cmds|join('"\n          - "') }}"
          - "setenv bootargs console={{ console_device }},{{ baud_rate }}n8 root=/dev/nfs rw {{ base_nfsroot_args }} {{ base_kernel_args }} {{ base_ip_args }}"
          - "{{ uboot_bootx_cmd|default('{BOOTX}') }}"
        norflash:
          commands:
          - "setenv bootargs 'console={{ console_device }},{{ baud_rate }}n8 root=/dev/nfs rw {{ base_nfsroot_args }} {{ base_kernel_args }} {{ base_ip_args }}'"
          - "boot"
        ramdisk:
          commands:
          - "setenv serverip {SERVER_IP}"
          - "setenv autoload no"
          - "{{ uboot_ipaddr_cmd|default('dhcp') }}"
          - "{{ uboot_common_cmds|join('"\n          - "') }}"
          - "setenv bootargs 'console={{ console_device }},{{ baud_rate }}n8 root=/dev/ram0 {{ base_kernel_args }} {{ base_ip_args }}'"
          - "{{ uboot_bootx_cmd|default('{BOOTX}') }}"
        sata:
          commands:
          - scsi scan
          - "{{ base_uboot_commands|join('"\n          - "') }}"
          - "load scsi {ROOT_PART} {KERNEL_ADDR} {KERNEL}"
          - "load scsi {ROOT_PART} {RAMDISK_ADDR} {RAMDISK}"
          - "setenv initrd_size ${filesize}"
          - "load scsi {ROOT_PART} {DTB_ADDR} {DTB}"
          - "setenv bootargs 'console={{ console_device }},{{ baud_rate }}n8 root={ROOT} {{ base_kernel_args }} {{ base_ip_args }}'"
          - "{{ base_uboot_bootcmd|join('"\n          - "') }}"
        usb:
          commands:
          - "{{ (base_uboot_usb_commands + base_uboot_commands)|join('"\n          - "') }}"
          - "load usb {{ usb_device_id }}:{ROOT_PART} {KERNEL_ADDR} {KERNEL}"
          - "load usb {{ usb_device_id }}:{ROOT_PART} {RAMDISK_ADDR} {RAMDISK}"
          - "setenv initrd_size ${filesize}"
          - "load usb {{ usb_device_id }}:{ROOT_PART} {DTB_ADDR} {DTB}"
          - "setenv bootargs 'console={{ console_device }},{{ baud_rate }}n8 root={ROOT} {{ base_kernel_args }} {{ base_ip_args }}'"
          - "{{ base_uboot_bootcmd|join('"\n          - "') }}"
{% endif %}
{% endblock body %}
