===============
Firmware Update
===============

The SRX-8 software consists of two firmware images and a single bootloader. Both firmware images
can be upgraded on-orbit using CSP while the bootloader can only be upgraded on ground.

The ``sys.boot.slot`` property shows which firmware image is currently running. The ``boot status``
command can be used to see additional information about the state of the two firmware images:

.. code-block:: none

    [satctl] srx8 boot status
    [slot 0] booted, preferred
    status:   stable
    priority: 1
    attempts: 2
    contents: valid

    [slot 1]
    status:   stable
    priority: 0
    attempts: 3
    contents: valid

By default, the SRX-8 will always boot the stable image with the highest priority, which is
indicated by the ``preferred`` keyword in the example above. Booting any of the
images for a number of boots can be configured using :ref:`BLIB<blib>`.
A firmware image can be marked as stable using :ref:`BLIB<blib>`, but it will also be marked stable
automatically after an uptime of 120 seconds.

Satlab supplies the ``blib-update`` script that simplifies the firmware update process by wrapping
the process outlined below into a single command.

.. code-block:: none

    satlab@satlab:~$ blib-update -d 28 --mark-stable -c <can_device> <path_to_.slim_file>

The ``blib-update`` tool is part of ``satctl`` which can be obtained here: https://resources.satlab.com/software/satctl/

Manual Firmware Update
======================

The ``boot flash`` command can be used to update a firmware image. Firmware images are stored
in SLIM (SatLab IMage) files. The SLIM file must already be transferred to the SRX-8 transmitter using :ref:`BTP<btp>`.
Firmware file uploads must use a block size of 128, which can be specified using the ``-s 128``
argument to the ``btp`` command. The name of the new SLIM file must be ``flash0``.

The example below shows how a ``.slim`` file can be moved to the SRX-8 transmitter and flashed to slot 1
from slot 0 using satctl:

.. code-block:: none

    [satctl] srx8 btp remove -t 60000 flash0
    [satctl] srx8 btp push -c100 -s128 -i60000 <path_to_.slim_file> flash0
    [satctl] srx8 boot flash flash0 1
    Flashing "flash0" to slot 1
    Flashed image OK
    [satctl] srx8 boot prefer 1
    [satctl] srx8 reboot

It is also possible to complete the flashing process using the serial interface instead:

.. code-block:: none

    [srx-8] boot flash flash0 other
    Flashing image from flash0 to slot 1
    Successfully flashed image
    [srx-8] boot prefer other
    [srx-8] reboot

.. NOTE::
    It is not possible to overwrite the booted image.

The SRX-8 automatically marks the boot as successful 120 seconds after a successful boot. It is
also possible to do this programmatically.

More information regarding boot options of the two firmware images can be found in the :ref:`blib`.

Bootloader Image
================

The bootloader can only be updated using the ``boot loader flash`` command. It is not possible to
update it remotely via CSP. A bootloader image is stored as a part of a firmware image. It is therefore
not required to specify a file name when updating the bootloader.

The example below shows how to update and verify the bootloader image:

.. code-block:: none

    [srx-8] boot loader flash
    Flash bootloader? [y/N] y
    [  509.854000] boot: flashing bootloader, 325104 bytes to 0
    [srx-8] boot loader verify
    Bootloader matches embedded
