You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
7.3 KiB

5 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
4 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
4 years ago
5 years ago
  1. \subsection{The update mechanism}
  2. The update mechanism is split into four main phases: checking for updates, reprogramming the device, calculating and verifying the cryptographic signature of the updated firmware, and - assuming that the update was successful - reconfiguring the boot process to use the new firmware.
  3. \subsubsection{Checking for updates}
  4. In order to inform the IoT devices of the availability of a new firmware version, the update server provides a file for each device type containing meta-information about the latest available firmware version.
  5. The meta-information file has a simple line oriented ASCII format, which is easy to generate and efficient to parse within the limited constraints of the embedded device.
  6. It consists of the version identifier and the cryptographic signatures of both of the firmware binaries.
  7. The version identifier can be an arbitrary string as the content is not interpreted semantically but only compared to the version identifier used during build time.
  8. The other two lines in the meta-information file provide the hexadecimal representation of the cryptographic signatures, one line for each firmware binary file.
  9. These meta-information files are provided by the update server using \textit{HTTP 1.1} \cite{HTTP_1.1} under the following path pattern: \texttt{\$\{DEVICE\}.version} (whereas \texttt{\$\{DEVICE\}} gets replaced by the device type name).
  10. Each device queries the update server regularly (initially when the boot process is finished and periodically once an hour) for the currently available firmware version.
  11. It uses the \texttt{UPDATER\_URL} option to identify the update server.
  12. After the meta-information file has been downloaded successfully, the version identifier is extracted and compared to the version identifier of the running firmware.
  13. If the version identifiers differ, the update process is initialized.
  14. In cases where the download fails, the update server or network connection is not available, or any other error occurres, another attempt will be made automatically at the next regular interval.
  15. In addition to the interval, a special \textit{MQTT} topic shared by all devices is subscribed on device startup: \texttt{\$\{MQTT\_REALM\}/update}.
  16. Every time a message is received on this topic, a fetch attempt for the meta-information file is triggered and the process restarts.
  17. This allows faster roll-outs of updates and finer control for manual maintenance.
  18. \subsubsection{Reprogramming the device}
  19. As the \textit{ESP-01s} is only equipped with 1 MB of flash, this means that the whole memory is mapped to a contiguous address space (refer to Section \ref{flashlayout}).
  20. Therefore, the second ROM slot can not be re-mapped to have the same start address as the first ROM slot.
  21. While the firmware is executed without any dynamic linking mechanism and the chip does not support position independent code, the addresses used in the ROM slots are dependent to the offset at which the firmware is stored.
  22. This arises the need for building two firmware images, one for each target location.
  23. To do so, a linker script for each of the two ROM slots was created, which is used to create two variations of the same firmware, only differing in ROM placement.
  24. The two resulting firmware image files are both provided for download via \textit{HTTP 1.1} - which one to download depends on the target ROM slot and is selected by the device during the update process.
  25. Listing~\ref{lst:linker_script} shows the only difference between the two linker scripts, where \texttt{\$\{SLOT\}} must be replaced with the slot number according to the current build.
  26. \begin{lstlisting}[language=,
  27. caption={Linker script to build firmware for two ROM slots.},
  28. label=lst:linker_script, basicstyle=\ttfamily\scriptsize]
  29. irom0_0_seg :
  30. org = ( 0x40200000 // The memory mapping address
  31. + 0x2000 // Bootloader code and config
  32. + 0x10 // Data offset after header
  33. + 1M / 2 * ${SLOT} // Offset for the ROM slot
  34. ),
  35. len = ( 1M / 2 - 0x2010 )// Half ROM size excl. bootloader
  36. \end{lstlisting}
  37. For installing a firmware update, the new firmware image file is downloaded using an HTTP GET request.
  38. \subsubsection{Verifying the cryptographic signature}
  39. While the image is being downloaded, each chunk received in the download stream is used to update the \textit{SHA256} hash before it is written to the flash.
  40. When the write has been finished, the next chunk is received and the process continues until all chunks have been received.
  41. After the download of a new ROM has been finished successfully, the calculated hash is checked against the cryptographically signed hash provided in the meta-information file triggering the update using the \textit{curve25519} public key provided by the running firmware.
  42. Only if the firmware is considered valid, the bootloader configuration is altered to boot into the new ROM slot and the device is rebooted.
  43. \subsubsection{Reconfiguring the boot process}
  44. For the bootloader, \textit{rBoot}\cite{rBoot} has been choosen as it is integrated within the \textit{Sming} framework and allows to boot to multiple ROM slots.
  45. For configuration, an \textit{rBoot} specific structure is placed in the flash at a well-known location directly after the space reserved for the bootloader code.
  46. This structure contains, among other things, the target offsets for all known ROMs and the number of the ROM to boot from on next reboot.
  47. \begin{figure}[htbp]
  48. \centering\includegraphics[width=.98\linewidth]{flash_layout.pdf}
  49. \caption{The flash layout used for two ROM slots.}
  50. \label{fig:memory_layout}
  51. \end{figure}
  52. To calculate the origin of application data for each ROM slot, the available memory of 1 MB is split in half and an offset of the size of the bootloader code and its configuration (0x2000 bytes) is added.
  53. For alignment and easy debugging, the second block is shifted by the same amount of bytes as the first block.
  54. The gap of 8192 bytes is available to applications to store data, which can persist over application updates.
  55. \begin{lstlisting}[caption={The flash layout used for two ROMs.},
  56. label=lst:choosing_rom, basicstyle=\ttfamily\scriptsize]
  57. #define URL_ROM(slot) (( URL "/" DEVICE ".rom" slot ))
  58. // Select rom slot to flash
  59. const auto& bootconf = rboot_get_config();
  60. // Add items to flash
  61. if (bootconf.current_rom == 0) {
  62. updater.addItem(bootconf.roms[1], URL_ROM("1"));
  63. updater.switchToRom(1);
  64. } else {
  65. updater.addItem(bootconf.roms[0], URL_ROM("0"));
  66. updater.switchToRom(0);
  67. }
  68. \end{lstlisting}
  69. The firmware files provided on the update server are the exact same ones as used to initially flash the chip for the according version.
  70. Using the same files for flashing and updating allows better debugging by eliminating errors related to the update process itself and eases development and initial installation.
  71. Listing~\ref{lst:choosing_rom} shows the algorithm used to determine the download address and reconfigure the bootloader.
  72. The update server provides these files in the exact same way as it provides the meta-information files, but the path pattern differs: the suffixes \texttt{.rom0} and \texttt{.rom1} are used to provide the firmware image files for the first and second slot respectively.
  73. For installing a firmware update, the new firmware image file is downloaded using an \textit{HTTP 1.1} \texttt{GET} request.