{% extends 'base.jinja2' %}

{% set base_high_limits = base_high_limits|default(true) %}
{% set base_uboot_commands = ["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 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_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"] -%}
{% 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_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 %}

{# adds the parameters: top level block #}
{% block vland %}
{{ 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') }}'
  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') }}'
{% 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') }}'
  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') }}'
{% 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') }}'
  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') }}'
{% endif %}
{% endblock vland %}

{% 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:
      add_header: 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:
      lxc:
      fastboot:
      serial:
    methods:
{% if uboot_ums_flash %}
      u-boot-ums:
{% endif %}
{% if flasher_deploy_commands %}
      flasher:
        commands: {{ flasher_deploy_commands }}
{% endif %}
      image:
      lxc:
      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:
      lxc:
      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 uboot_ums_flash %}
          uboot_ums_flash: {{ uboot_ums_flash }}
          uboot_mass_storage_device: {{ uboot_mass_storage_device|default(None) }}
{% 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}"
          - "setenv bootargs '{{ console_device }}{{ baud_rate }} rootfstype=ramfs {{ base_kernel_args }}'"
          - "bootm {KERNEL_ADDR}#conf@{{ bootm_device_tree_config }}"
{% endif %}
        nfs:
          commands:
          - "{{ uboot_common_cmds|join('"\n          - "') }}"
          # Always quote the entire string if the command includes a colon to support correct YAML.
          - "setenv bootargs '{{ console_device }}{{ baud_rate }} root=/dev/nfs rw {{ base_nfsroot_args }} {{ base_kernel_args }} {{ base_ip_args }}'"
          - "{{ base_uboot_bootcmd }}"
        nbd:
          commands:
          - "{{ uboot_common_cmds|join('"\n          - "') }}"
          # Always quote the entire string if the command includes a colon to support correct YAML.
          - "setenv bootargs '{{ console_device }}{{ baud_rate }} rw {{ base_nbdroot_args }} {{ base_kernel_args }} {{ base_ip_args }} verbose earlyprintk systemd.log_color=false ${extraargs} rw'"
          - "{{ base_uboot_bootcmd }}"
        ramdisk:
          commands:
          - "{{ uboot_common_cmds|join('"\n          - "') }}"
          - "setenv bootargs '{{ console_device }}{{ baud_rate }} root=/dev/ram0 {{ base_kernel_args }} {{ base_ip_args }}'"
          - "{{ 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}"
          - "{{ console_device }}{{ baud_rate }} root={ROOT} {{ base_kernel_args }} {{ base_ip_args }}"
          - "{{ 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}"
          - "setenv bootargs '{{ console_device }}{{ baud_rate }} root={ROOT} {{ base_kernel_args }} {{ base_ip_args }}'"
          - "{{ base_uboot_bootcmd }}"
      uuu:
        options:
          usb_otg_path: "{{ uuu_usb_otg_path }}"
          corrupt_boot_media_command: {{ uuu_corrupt_boot_media_command }}
          docker_image: "{{ uuu_docker_image }}"
          remote_options: "{{ uuu_remote_options }}"


{% endblock body %}
