{% extends 'base.jinja2' %}

{% set base_high_limits = base_high_limits|default(true) %}
{% set base_uboot_commands = base_uboot_commands|default([]) + ["setenv autoload no"] -%}
{% set base_uboot_high_limits = [
    "setenv initrd_high " + uboot_initrd_high|default('0xffffffff'),
    "setenv fdt_high "    + uboot_fdt_high|default('0xffffffff')]
-%}

{% set base_uboot_dhcp_command = [
    uboot_ipaddr_cmd|default('dhcp'),
    "setenv serverip {SERVER_IP}" ]
-%}

{% if append_dtb %}
  {% set uboot_load_fdt = [] %}
{% else %}
  {% set uboot_load_fdt = [uboot_load_fdt|default("tftp {DTB_ADDR} {DTB}")] %}
{% endif %}

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

{% set apply_dtbo_commands = apply_dtbo_commands|default([
    "tftp {DTBO_ADDR} {DTBO}",
    "fdt apply {DTBO_ADDR}"])
-%}
{% set uboot_load_dtbo = [
    "fdt addr {DTB_BASE_ADDR}",
    "fdt resize {DTB_BASE_RESIZE}",
    "{APPLY_DTBO_COMMANDS}"]
-%}
{% if enable_dtbo_support %}
  {% set base_uboot_tftp_commands = base_uboot_tftp_commands + uboot_load_dtbo %}
{% endif %}

{% set uboot_fdt_extra_commands = uboot_fdt_extra_commands|default([]) %}
{% if uboot_fdt_extra_commands %}
  {% set base_uboot_tftp_commands = base_uboot_tftp_commands +
    ["fdt addr {DTB_ADDR}"] + uboot_fdt_extra_commands
  %}
{% endif %}

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

{% set base_uboot_nbd_bootcmd = [
    "setenv loadnbd 'setenv serverip {SERVER_IP}; run loadkernel; run loadfdt; run loadinitrd';",
    "setenv verify no"
    "setenv bootcmd 'dhcp; run loadnbd; printenv; {BOOTX}'"
    "run bootcmd"]
-%}

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

{% set uboot_needs_pci = uboot_needs_pci|default(false) %}
{% 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"] -%}
{% if uboot_mac_addr %}
  {% set base_uboot_set_mac = ["setenv ethaddr " + uboot_mac_addr] -%}
{% endif %}

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

{% set uboot_common_cmds = [] %}
{% if uboot_needs_pci %}
  {% set uboot_common_cmds = uboot_common_cmds + base_uboot_pci_commands %}
{% endif %}
{% 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_dhcp_command  + base_uboot_tftp_commands %}

{% set uboot_allow_chain_load_grub = uboot_allow_chain_load_grub|default(true) %}

{% set additional_bcu_cleanup_commands = additional_bcu_cleanup_commands|default([]) -%}

{# adds the parameters: top level block #}
{% block parameters %}
{{ super() }}
{% if text_offset %}
  text_offset: '{{text_offset}}'
{% endif %}
{% if booti_kernel_addr %}
  image:
    kernel: '{{ booti_kernel_addr|default('0x80000000') }}'
    ramdisk: '{{ booti_ramdisk_addr|default('0x82000000') }}'
    dtb: '{{ booti_dtb_addr|default('0x81000000') }}'
    tee: '{{ booti_tee_addr|default('0x83000000') }}'
    dtbo: '{{ booti_dtbo_addr|default('0x93c00000') }}'
  booti:
    kernel: '{{ booti_kernel_addr|default('0x80000000') }}'
    ramdisk: '{{ booti_ramdisk_addr|default('0x82000000') }}'
    dtb: '{{ booti_dtb_addr|default('0x81000000') }}'
    tee: '{{ booti_tee_addr|default('0x83000000') }}'
    dtbo: '{{ booti_dtbo_addr|default('0x93c00000') }}'
{% endif %}
{% if bootm_kernel_addr %}
  uimage:
    kernel: '{{ bootm_kernel_addr|default('0x80000000') }}'
    ramdisk: '{{ bootm_ramdisk_addr|default('0x82000000') }}'
    dtb: '{{ bootm_dtb_addr|default('0x81000000') }}'
    tee: '{{ bootm_tee_addr|default('0x83000000') }}'
    dtbo: '{{ bootm_dtbo_addr|default('0x93c00000') }}'
  bootm:
    kernel: '{{ bootm_kernel_addr|default('0x80000000') }}'
    ramdisk: '{{ bootm_ramdisk_addr|default('0x82000000') }}'
    dtb: '{{ bootm_dtb_addr|default('0x81000000') }}'
    tee: '{{ bootm_tee_addr|default('0x83000000') }}'
    dtbo: '{{ bootm_dtbo_addr|default('0x93c00000') }}'
{% endif %}
{% if bootz_kernel_addr %}
  zimage:
    kernel: '{{ bootz_kernel_addr|default('0x80000000') }}'
    ramdisk: '{{ bootz_ramdisk_addr|default('0x82000000') }}'
    dtb: '{{ bootz_dtb_addr|default('0x81000000') }}'
    tee: '{{ bootz_tee_addr|default('0x83000000') }}'
    dtbo: '{{ bootz_dtbo_addr|default('0x93c00000') }}'
  bootz:
    kernel: '{{ bootz_kernel_addr|default('0x80000000') }}'
    ramdisk: '{{ bootz_ramdisk_addr|default('0x82000000') }}'
    dtb: '{{ bootz_dtb_addr|default('0x81000000') }}'
    tee: '{{ bootz_tee_addr|default('0x83000000') }}'
    dtbo: '{{ bootz_dtbo_addr|default('0x93c00000') }}'
{% endif %}
  dtb_base_resize: {{ dtb_base_resize|default(1048576) }}
{% endblock parameters %}

{% block body %}

{% if console_device %}
  {% set console_device = "console=" + console_device + "," %}
  {% set parity = console_parity|default('n') %}
  {% set databits = console_databits|default('8') %}
  {% set baud_rate = baud_rate | default(115200)|string + parity + databits %}
{% endif %}
{% set base_kernel_args = extra_kernel_args | default(base_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 fastboot_deploy_uboot_commands %}
adb_serial_number: "{{ adb_serial_number|default('0000000000') }}"
fastboot_serial_number: "{{ fastboot_serial_number|default('0000000000') }}"
fastboot_options: {{ fastboot_options|default([]) }}
# This attribute identifies whether a device should get into fastboot mode by
# interrupting uboot and issuing commands at the bootloader prompt.
fastboot_via_uboot: True
{% endif %}

actions:
  deploy:
    parameters:
{#
 # uboot_ramdisk_format defaults to the u-boot legacy image format. This is not
 # a good default but it keeps us backwards compatible with old device-type
 # templates that predate its introduction. It should almost always be set to
 # "raw" for new device-type templates.
 #
 # Note that the u-boot legacy format requires CONFIG_LEGACY_IMAGE_FORMAT be
 # set and support for raw images is controlled by CONFIG_SUPPORT_RAW_INITRD.
 #}
      add_header: {{ uboot_ramdisk_format|default("u-boot") }}
      mkimage_arch: {{ uboot_mkimage_arch|default('arm64') }} # string to pass to mkimage -A when adding UBoot headers
      append_dtb: {{ append_dtb|default(false) }}
      use_xip: {{ use_xip|default(false) }}
    connections:
      fastboot:
      serial:
    methods:
{% if uboot_ums_flash %}
      u-boot-ums:
{% endif %}
{% 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 %}
      image:
      overlay:
      usb:
      tftp:
      nbd:
      ssh:
        options:
{{ ssh_options }}
        host: "{{ ssh_host|default('') }}"
        port: {{ ssh_port|default(22) }}
        user: "{{ ssh_user|default('root') }}"
        identity_file: "{{ ssh_identity_file }}"
{% if recovery_mode %}
{{ recovery_mode }}
          recovery_mode:
{% for url in recovery_mode_command %}
          - {{ url }}
{% endfor %}
          recovery_exit:
{% for url in recovery_exit_command %}
          - {{ url }}
{% endfor %}
{% endif %}
{% if fastboot_deploy_uboot_commands %}
      fastboot:
      u-boot:
        parameters:
          bootloader_prompt: "{{ bootloader_prompt }}"
          interrupt_prompt: "{{ interrupt_prompt }}"
          interrupt_char: "{{ interrupt_char }}"
          fastboot:
            commands:
{% for command in fastboot_deploy_uboot_commands %}
              - {{ command }}
{% endfor %}
{{- fastboot_interrupt_params }}
{% endif %}

  boot:
    connections:
      fastboot:
      serial:
    methods:
      minimal:
      ssh:
      dfu:
        implementation: u-boot
        reset_works: False
        parameters:
          enter-commands:
{% for command in deploy_dfu_commands %}
            - {{ command }}
{% endfor %}
          command: dfu-util
{% if fastboot_boot_uboot %}
      fastboot: {{ fastboot_sequence }}
{% endif %}
      u-boot:
        parameters:
          bootloader_prompt: {{ bootloader_prompt|default('=>') }}
          interrupt_prompt: {{ interrupt_prompt|default('Hit any key to stop autoboot') }}
          interrupt_char: "{{ interrupt_char|default('') }}"
          needs_interrupt: {{ uboot_needs_interrupt|default(True) }}
{% if enable_dtbo_support %}
          apply_dtbo_commands: {{ apply_dtbo_commands }}
{% endif %}
{% if uboot_altbank %}
          uboot_altbank: {{ uboot_altbank }}
          uboot_altbank_cmd: "{{ uboot_altbank_cmd|default('reset')}}"
{% endif %}
{% if uboot_ums_flash %}
          uboot_ums_flash: {{ uboot_ums_flash }}
          uboot_mass_storage_device: {{ uboot_mass_storage_device|default(None) }}
{% endif %}
{% if uboot_set_mac %}
          lava_mac: {{ uboot_mac_addr }}
{% endif %}
{% if fastboot_boot_uboot %}
{{- fastboot_boot_uboot }}
{{- fastboot_boot_grub }}
{% endif %}
        # method specific stanza
        ums:
          commands:
          - "{{ ums_command|default('ums 0 mmc 0')}}"
{% if uboot_method_fit %}
        fit:
          commands:
          - "{{ (base_uboot_commands + base_uboot_dhcp_command)|join('"\n          - "') }}"
          - "tftp {KERNEL_ADDR} {KERNEL}"
{% set uboot_fit_bootargs %}{{ console_device }}{{ baud_rate }} rootfstype=ramfs {{ base_kernel_args }}{% endset %}
{% set uboot_fit_setenv %}setenv bootargs {{ uboot_fit_bootargs|shlex_quote }}{% endset %}
          - {{ uboot_fit_setenv|yaml_quote }}
          - "bootm {KERNEL_ADDR}{% if bootm_device_tree_config %}#conf@{{ bootm_device_tree_config }}{% endif %}"
{% endif %}
        nfs:
          commands:
          - "{{ uboot_common_cmds|join('"\n          - "') }}"
{% set uboot_nfs_bootargs %}{{ console_device }}{{ baud_rate }} root=/dev/nfs rw {{ base_nfsroot_args }} {{ base_kernel_args }} {{ base_ip_args }}{% endset %}
{% set uboot_nfs_setenv %}setenv bootargs {{ uboot_nfs_bootargs|shlex_quote }}{% endset %}
          - {{ uboot_nfs_setenv|yaml_quote }}
          - "{{ base_uboot_bootcmd }}"
        nbd:
          commands:
          - "{{ uboot_common_cmds|join('"\n          - "') }}"
{% set uboot_nbd_bootargs %}{{ console_device }}{{ baud_rate }} rw {{ base_nbdroot_args }} {{ base_kernel_args }} {{ base_ip_args }} verbose earlyprintk systemd.log_color=false ${extraargs} rw{% endset %}
{% set uboot_nbd_setenv %}setenv bootargs {{ uboot_nbd_bootargs|shlex_quote }}{% endset %}
          - {{ uboot_nbd_setenv|yaml_quote }}
          - "{{ base_uboot_bootcmd }}"
        ramdisk:
          commands:
          - "{{ uboot_common_cmds|join('"\n          - "') }}"
{% set uboot_ramdisk_bootargs %}{{ console_device }}{{ baud_rate }} root=/dev/ram0 {{ base_kernel_args }} {{ base_ip_args }}{% endset %}
{% set uboot_ramdisk_setenv %}setenv bootargs {{ uboot_ramdisk_bootargs|shlex_quote }}{% endset %}
          - {{ uboot_ramdisk_setenv|yaml_quote }}
          - "{{ base_uboot_bootcmd }}"
        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}"
{% set uboot_usb_bootargs %}{{ console_device }}{{ baud_rate }} root={ROOT} {{ base_kernel_args }} {{ base_ip_args }}{% endset %}
          - {{ uboot_usb_bootargs|yaml_quote }}
          - "{{ base_uboot_bootcmd }}"
        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}"
{% set uboot_sata_bootargs %}{{ console_device }}{{ baud_rate }} root={ROOT} {{ base_kernel_args }} {{ base_ip_args }}{% endset %}
{% set uboot_sata_setenv %}setenv bootargs {{ uboot_sata_bootargs|shlex_quote }}{% endset %}
          - {{ uboot_sata_setenv|yaml_quote }}
          - "{{ base_uboot_bootcmd }}"
      uuu:
        options:
{% if uuu_usb_otg_path is iterable and uuu_usb_otg_path is not string %}
          usb_otg_path: {{ uuu_usb_otg_path }}
{% else %}
          usb_otg_path: "{{ uuu_usb_otg_path }}"
{% endif %}
          usb_otg_path_command: {{ uuu_usb_otg_path_command }}
          corrupt_boot_media_command: {{ uuu_corrupt_boot_media_command }}
          power_off_before_corrupt_boot_media: {{ uuu_power_off_before_corrupt_boot_media }}
          docker_image: "{{ uuu_docker_image }}"
          remote_options: "{{ uuu_remote_options }}"
          bcu_board_name: "{{ bcu_board_name }}"
          bcu_board_id: "{{ bcu_board_id }}"
          bcu_board_id_command: {{ bcu_board_id_command }}
          has_bcu_commands: "{{ has_bcu_commands }}"
          additional_bcu_cleanup_commands: {{ additional_bcu_cleanup_commands }}
{% if uboot_allow_chain_load_grub %}
      grub:
        parameters:
          bootloader_prompt: 'grub>'
          needs_interrupt: true
      grub-efi:
        parameters:
          bootloader_prompt: 'grub>'
          needs_interrupt: true
{% endif %}
{% if extra_boot_methods %}
{{ extra_boot_methods|indent(first=True,width=6) }}
{% endif %}

  test:
    methods:
      docker:
        global_options:
        - {{ docker_shell_global_arguments }}
        options:
        - {{ docker_shell_extra_arguments }}

{% endblock body %}
