{% 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') ) -%}

{% if append_dtb %}
    {% set run_load_fdt = "" %}
    {% set load_fdt = "" %}
{% else %}
    {% set run_load_fdt = "run loadfdt; " %}
    {% set load_fdt = uboot_load_ftd|default("          - tftp {DTB_ADDR} {DTB}") %}
{% endif %}

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

{% set base_uboot_tftp_commands = uboot_tftp_commands|default(
"          - tftp {KERNEL_ADDR} {KERNEL}
          - tftp {RAMDISK_ADDR} {RAMDISK}
          - setenv initrd_size ${filesize}
" + 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 " + uboot_mac_addr) -%}

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

{# 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') }}'
  booti:
    kernel: '{{ booti_kernel_addr|default('0x80000000') }}'
    ramdisk: '{{ booti_ramdisk_addr|default('0x82000000') }}'
    dtb: '{{ booti_dtb_addr|default('0x81000000') }}'
{% endif %}
{% if bootm_kernel_addr %}
  uimage:
    kernel: '{{ bootm_kernel_addr|default('0x80000000') }}'
    ramdisk: '{{ bootm_ramdisk_addr|default('0x82000000') }}'
    dtb: '{{ bootm_dtb_addr|default('0x81000000') }}'
  bootm:
    kernel: '{{ bootm_kernel_addr|default('0x80000000') }}'
    ramdisk: '{{ bootm_ramdisk_addr|default('0x82000000') }}'
    dtb: '{{ bootm_dtb_addr|default('0x81000000') }}'
{% endif %}
{% if bootz_kernel_addr %}
  zimage:
    kernel: '{{ bootz_kernel_addr|default('0x80000000') }}'
    ramdisk: '{{ bootz_ramdisk_addr|default('0x82000000') }}'
    dtb: '{{ bootz_dtb_addr|default('0x81000000') }}'
  bootz:
    kernel: '{{ bootz_kernel_addr|default('0x80000000') }}'
    ramdisk: '{{ bootz_ramdisk_addr|default('0x82000000') }}'
    dtb: '{{ bootz_dtb_addr|default('0x81000000') }}'
{% 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 %}
      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 }}"
          send_char: False
          fastboot:
            commands:
{% for command in fastboot_deploy_uboot_commands %}              - {{ command }}{% endfor %}
{{- fastboot_interrupt_params }}
{% endif %}

  boot:
    connections:
      lxc:
      fastboot:
      serial:
    methods:
      minimal:
      ssh:
{% if fastboot_boot_uboot %}
      fastboot: {{ fastboot_sequence }}
{% endif %}
      u-boot:
        parameters:
          mkimage_arch: {{ uboot_mkimage_arch|default('arm') }}
          bootloader_prompt: {{ bootloader_prompt|default('=>') }}
          interrupt_prompt: {{ interrupt_prompt|default('Hit any key to stop autoboot') }}
          # character needed to interrupt u-boot, single whitespace by default
          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')}}
        nfs:
          commands:
{% if uboot_needs_usb %}
{{ base_uboot_usb_commands }}
{% endif -%}
{{ base_uboot_commands }}
{% if base_high_limits %}
{{ base_uboot_high_limits }}
{% endif %}
{% if uboot_set_mac %}
{{ base_uboot_set_mac }}
{% endif -%}
{{ base_uboot_dhcp_command }}
{{ base_uboot_tftp_commands }}
          # 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:
{% if uboot_needs_usb %}
{{ base_uboot_usb_commands }}
{% endif -%}
{{ base_uboot_commands }}
{% if base_high_limits %}
{{ base_uboot_high_limits }}
{% endif %}
{% if uboot_set_mac %}
{{ base_uboot_set_mac }}
{% endif -%}
{{ base_uboot_dhcp_command }}
{{ base_uboot_tftp_commands }}
          # 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:
{% if uboot_needs_usb %}
{{ base_uboot_usb_commands }}
{% endif -%}
{{ base_uboot_commands }}
{% if base_high_limits %}
{{ base_uboot_high_limits }}
{% endif %}
{% if uboot_set_mac %}
{{ base_uboot_set_mac }}
{% endif -%}
{{ base_uboot_dhcp_command }}
{{ base_uboot_tftp_commands }}
          - "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 }}
          - "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 }}
          - "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 }}
{% endblock body %}
