Ota Firmware
Ota Firmware
2024
Lassi Lahti
2024 | 43 pages
Lassi Lahti
Keywords:
Turun ammattikorkeakoulu
2024 | 43 sivua
Lassi Lahti
Langattomien ohjelmistopäivitysjärjestelmän
toteuttaminen sulautetuissa laitteissa
Tämä opinnäytetyö käsittelee sulautettujen laitteiden
ohjelmistopäivitysjärjestelmän suunnittelua ja toteutusta, keskittyen
käynnistyksenlataajan ja langattoman päivitystoiminnon kehittämiseen.
Opinnäytetyö kuvailee työnantajan projektia, jossa tavoitteena oli toteuttaa
käynnistyksenlataaja ja päivitysominaisuus mahdollistamaan
ohjelmistopäivitykset Bluetoothin yli.
Asiasanat:
1 Introduction 8
2 Bootloader 9
2.1 Bootloader fundamentals 9
2.2 Types of bootloaders 9
2.2.1 Primary bootloaders 9
2.2.2 Secondary bootloaders 10
2.3 Choosing the right bootloader implementation 10
3 Firmware 12
3.1 The importance of firmware updates 12
3.2 Firmware update methods and requirements 13
4 Cybersecurity 14
4.1 EU Cyber Resilience Act 14
4.2 Bootloader security measures 15
4.3 OTA update security 15
5 Project requirements 17
5.1 Bootloader requirements 17
5.2 Cybersecurity requirements 17
6 ATSAMC21N18A 18
9 Testing 32
9.1 Test setup 32
9.2 Bootloader tests 33
9.2.1 Test case 1.1 34
9.2.2 Test case 1.2 37
9.3 OTA update tests 37
9.3.1 Test case 2.1 38
9.3.2 Test case 2.2 40
9.3.3 Test case 3.1 40
9.3.4 Test case 3.2 40
10 Conclusion 41
References 42
Pictures
Code
OTA Over-The-Air
EU European Union
KB Kilobyte
XOR Exclusive OR
1 Introduction
This thesis was written based on an employer project. The request was to
implement a bootloader and update functionality to enable firmware updates
over Bluetooth in the field.
2 Bootloader
As embedded devices have evolved, the need for updatability and upgradability
has risen. As a result, bootloaders have become more common even in the
simplest of devices.
There are two main types of bootloaders in an embedded system, primary and
secondary bootloaders.
remote update mechanisms and secure update protocols helps streamline the
update process while ensuring security and integrity of the device’s firmware.
3 Firmware
the first time the device has been used for its intended purpose on a large
scale. Due to this, problems often arise as oversights in testing lead to
unforeseen issues in the firmware.
For this purpose, devices developed with the intention of an extended lifetime
are made updatable. These updates allow the developer to fix bugs and
improve the functionality after the launch of the product. In addition, firmware
updates often provide upgrades and critical fixes that improve the products
quality and user friendliness.
Embedded devices can be updated using multiple different methods, each with
their own advantages and disadvantages. The main methods employed by
embedded devices however are external storage devices and OTA updates. In
rare cases the update process requires the device to be returned to the
manufacturer to be updated by an engineer.
For devices that operate in places where a dedicated team of engineers can
access and manipulate the device, the updates are often provided by the
developers and installed by an engineer using external storage devices or
dedicated update devices. Examples of such devices are industrial automation
devices, where the update process requires an engineer to be present.
On the other hand, devices operating in remote locations that do not require or
have access to a dedicated team of engineers, the developer may opt to use
OTA updates to provide firmware packages to the user. These devices often
require a mobile application, or a similar method provided by the manufacturer.
OTA updates allow more flexibility to the device’s operator, as they can decide
when and where to update the device. While these types of devices provide an
easy and efficient way to update firmware, they carry more risk as providing a
faulty firmware package may cause a device to become inoperable. In such
cases, the device must be sent back to the manufacturer, where it will be reset
and reprogrammed.
14
4 Cybersecurity
Devices produced for and sold in EU member states are required to follow
regulations set in the CRA, which details device types and software
requirements as well as risk assessment guidelines that determine the device
type.
At its core, CRA addresses two major problems according to the Council of the
European Union:
For embedded devices, the ability for the system to autonomously verify
firmware to detect unauthorized modifications is critical in ensuring device
security and integrity. This can be achieved through hardware-based security
methods such as TPMs to establish a hardware-based root of trust for the
device or storing the bootloader in ROM. ‘A programmable hardware-based root
of trust can be continuously updated to contend with an ever-increasing range
of threats’ (Rambus Press, 2023). Another option is software-based security,
where measures such as digital signatures using cryptographic keys, and
runtime integrity checks can be used.
5 Project requirements
The subject device of this paper will be implemented with a simple bootloader
and update function. In this project, there is no need for a secure bootloader or
advanced cybersecurity functions. The requirements are for the device to be
updated remotely via an OTA update provided by a mobile application over a
Bluetooth connection using an XMODEM implementation.
The bootloader itself will reside in the first 8KB of memory, leaving enough free
memory for future modifications and upgrades. The bootloader must initialize a
Bluetooth module and establish a connection to a mobile device. It must also be
able to verify the integrity of a firmware file and revert to an earlier version in
case of faulty firmware.
Considering the devices relation to the regulations set by the CRA and the
project requirements, the device does not directly relate to a classification
defined in the regulations. Therefore, the implementation will ignore more
advanced features such as bootloader updatability. The regulations set in the
CRA will be followed where applicable, however no verification of accordance
will be performed.
Firmware verification methods such as file headers with digital signatures will be
used, and the device calculates a CRC for the provided file to verify its integrity.
18
6 ATSAMC21N18A
For the bootloader implementation, the most important feature is the Watchdog
timer. The timer allows for boot and update process interruptions in case of
faults.
19
As defined in the project requirements for the bootloader, the bootloader resides
in the first 8 KB of memory on the device. Since the bootloader is compact and
does not require the entire 8KB of memory, it leaves a significant portion of the
reserved space free for future expansion.
In addition to the space reserved for the bootloader, the device utilizes a shared
memory section in external flash memory. The shared memory section includes
utilities such as a reset counter and flag to force the device into bootloader
mode in case too many reboots without successful boot are detected.
21
The bootloader also contains the address where the firmware’s reset vector is
located. The reset vector is a 4-byte address located at the beginning of the
firmware memory section and points to the initial address of the code to be
executed after a reset.
The bootloader executes code to jump to the memory address of the reset
vector.
22
void go_to_app(void) {
uint32_t jump_address;
start_app jump_to_app;
__set_MSP(*(uint32_t*)APP_ADDRESS);
jump_to_app();
}
else {
led_set_pin_level(LED_IO_ORANGE, 0);
}
}
All valid firmware packages for the device include a header portion in the first
512-byte section of the file. The bootloader verifies the existence of the header
portion and performs checks to determine the validity of the header. It checks
the firmware version and a hard-coded identification number. After the checks
have been performed, the bootloader executes a function to jump to the
application address.
23
uint8_t version_str[5];
uint8_t hw_version_compatability[6];
uint32_t bin_length;
uint32_t crc32;
uint32_t app_offset; // From binary header = header size
};
-------------------------------------SNIP---------------------------
-------------------------------------SNIP-------------------------
// Checks if file header and command header is not the same or header
has 0xFF in it or there is no header
if ((check_for_update || file_header.header_version == 0xFF ||
(file_header.header_id[0] != xxx && file_header.header_id[1] != xxx))
&& !get_force_bootloader_flag()) {
go_to_app();
}
-------------------------------------SNIP---------------------------
24
struct shared_data_struct {
uint8_t reboot_counter;
uint8_t copy_counter;
uint8_t force_bootloader_flag;
};
-------------------------------------SNIP---------------------------
int main(void) {
atmel_start_init();
increment_reboot_counter();
get_shared_data(&shared_data);
if(shared_data.reboot_counter >= 3) {
if(shared_data.copy_counter == 0) {
-------------------------------------SNIP---------------------------
}
else {
// Force bootloader
set_force_bootloader();
reset_reboot_counter();
reset_copy_counter();
-------------------------------------SNIP---------------------------
To secure the bootloader and prevent tampering, the bootloader memory region
will be defined as a write protected region using the MPU included in the
ATSAMC21N18A. As of the current version this feature is not enabled.
27
The update functionality is intended to be used when the device has booted into
the application. However, as a failsafe feature, the update can also be
performed while in the bootloader.
In case the firmware does not function as expected, the Watchdog timer is
initialized and will revert to the bootloader.
8.1 Bluetooth
The communication between the user and the device will be handled over
Bluetooth, including the file upload and custom communication protocol related
to the update functionality.
Bluetooth communication was chosen over other methods as the device will be
used in areas where a wireless network may not be available. In addition, the
application firmware requirements define the need for Bluetooth communication
with a mobile device for normal operation. Therefore, the basis for Bluetooth
communication already exists and does not require significant changes to be
able to transfer files over Bluetooth.
28
A typical XMODEM packet consists of 133 bytes, of which one byte is reserved
for a defined protocol symbol such as SOH or EOT to determine the type of
packet being sent. Bytes two and three are reserved for packet numbers, with
byte three being a negated version of byte twos packet number. Bytes 4-131
include the data contained in the packet, while the remaining two bytes contain
the calculated checksum of the packet.
Valid XMODEM packets include data packets starting with a start of header
symbol, end of transmission symbol, or cancel symbol.
The protocol works by receiver initiation when device A sends the ASCII
character ‘C’. Device B constructs a packet and passes it to device A. Device A
handles the received packet appropriately and in response, sends an ACK
message back to device B signaling a completed transfer. The packet exchange
continues until device B sends an EOT packet to device A, to which device A
responds once more with an ACK message, finalizing the file transfer.
combines the current CRC value with the data using a bitwise XOR operation. It
then looks up the result in the polynomial table to determine the next CRC value
and applies a bitwise shift right to process each byte, continuing until all bytes in
the buffer have been processed.
// Send ACK
reply = ACK;
io_write(bt_uart_io, (uint8_t *)&reply, 1);
delay_ms(1);
// Cleanup
external_flash_buffer_write_ptr = external_flash_buffer;
external_flash_buffer_overflow_counter = 0;
xmodem_status = X_DONE;
// Reset mcu so bootloader can update app
_reset_mcu();
31
In normal operation the mobile device receives GPS data from the system in
NMEA format. The mobile device is able to send custom commands to the
system, which responds accordingly. When the mobile device sends the
command “$FWUPLOAD”, the system stops forwarding GPS data to the mobile
device and initiates the file transfer by repeatedly sending the ASCII character
‘C’ until it receives a valid XMODEM packet or a cancel command.
change_module_mode_variable(FILE_UPLOAD);
delay_ms(1);
}
32
9 Testing
This chapter describes the testing process, starting with the bootloader, and
finally testing the update capability.
Preparations for testing include flashing the device with a functioning bootloader
and building the necessary binaries, one valid version and one invalid modified
version. The invalid binary has been modified by assigning random values to 6
bytes of the binary file, picture 5.
The hardware used for testing is a custom PCB with an ATSAMC21N18A and a
BM70 Bluetooth module.
The software used in testing includes Microchip studio version 7.0.2594 and a
Serial Bluetooth Terminal application version 1.47.
A successful boot is then verified by connecting to the device using the mobile
application, where multiple NMEA messages containing GPS data are received.
36
Replicating test case 1.1, the device lights an orange indicator LED to indicate
the lack of existing firmware. When programming the invalid firmware file to the
device, the device reboots and reverts to the bootloader indicated again by the
orange LED. The lack of firmware can be verified by connecting to the device
using the mobile application. As seen in picture 8, there are no NMEA
messages containing GPS data, and the device sends the ASCII character ‘C’
indicating the device is in bootloader mode and is attempting to initiate an
update.
Test cases 2.1 and 2.2 are performed using the same valid firmware file used in
test case 1.1. A “$FWUPLOAD” command is sent to the device from the mobile
application and the ASCII character ‘C’ is sent to the application by the device.
Once a valid binary file is selected, the mobile application begins sending
XMODEM packets to the device. Once finished, the device reboots and begins
transmitting GPS data. The firmware version can be verified by sending a
“$GETVER” command, prompting the device to send the version number to the
mobile application over Bluetooth.
38
Test cases 3.1 and 3.2 are performed by programming the bootloader to the
device with no valid firmware file. Bluetooth connection is then established, and
firmware binary file is transmitted to the device. Successful updates are verified
by a green indicator LED and GPS data transmission. Failed updates are
indicated by an orange indicator LED, indicating the device remains in
bootloader.
The same steps as in test case 2.1 are repeated, but a modified binary file is
sent to the device. Upon reboot of the device, the “$GETVER” command is sent
to the device and the version number returned to the application has not
changed.
Test case 3.1 is repeated, but a modified binary file is uploaded to the device.
After the upload is complete, the device reboots and an orange indicator LED
can be observed, indicating the device remained in the bootloader and the
update was not successful.
41
10 Conclusion
Future research will focus on the regulations set by the European Union’s Cyber
Resilience Act. Continuous development is required to provide security updates
and patches mandated by the legislation.
References
1 Amos, Z. (2024). How secure are Over-the-Air updates for IoT devices?
Available at: https://www.iotinsider.com/iot-insights/how-secure-are-over-
the-air-updates-for-iot-devices/ [Accessed 2 April 2024]
9 Vilcocq, G. (2023). What does the EU Cyber Resilience Act (CRA) mean
for connected device manufacturers? Available at:
https://eshard.com/posts/CRA-connected-device-manufacturers
[Accessed 25 March 2024]
43