├── .gitignore ├── LICENSE ├── README.md ├── UartSecureDFU.sln ├── UartSecureDFU ├── LICENSE ├── Makefile ├── Makefile.win32 ├── ReadMe.txt ├── UartSecureDFU.c ├── UartSecureDFU.vcxproj ├── UartSecureDFU.vcxproj.filters ├── crc32.c ├── crc32.h ├── delay_connect.c ├── delay_connect.h ├── dfu.c ├── dfu.h ├── dfu_serial.c ├── dfu_serial.h ├── jsmn.c ├── jsmn.h ├── logging.c ├── logging.h ├── miniz.h ├── slip_enc.c ├── slip_enc.h ├── uart_drv.h ├── uart_linux.c ├── uart_slip.c ├── uart_slip.h ├── uart_win32.c ├── zip.c └── zip.h └── testing_package_sdk15.2 ├── ble_app_uart_fw_1 ├── main.c └── pca10040 │ └── s132 │ ├── config │ └── sdk_config.h │ └── ses │ ├── ble_app_uart_pca10040_s132.emProject │ ├── ble_app_uart_pca10040_s132.emSession │ └── flash_placement.xml ├── ble_app_uart_fw_2 ├── main.c └── pca10040 │ └── s132 │ ├── config │ └── sdk_config.h │ └── ses │ ├── ble_app_uart_pca10040_s132.emProject │ ├── ble_app_uart_pca10040_s132.emSession │ └── flash_placement.xml ├── dfu_public_key.c ├── key_serial_dfu ├── app_uart_fw1.zip ├── app_uart_fw2.zip ├── demo_private.key ├── demo_public_key.c ├── generate_private_key.bat ├── generate_zip_fw1.bat ├── generate_zip_fw2.bat ├── nrfutil_script_dfu_fw1.bat └── nrfutil_script_dfu_fw2.bat └── secure_bootloader ├── main.c ├── pca10040_uart ├── config │ └── sdk_config.h └── ses │ ├── flash_placement.xml │ ├── secure_bootloader_uart_mbr_pca10040.emProject │ └── secure_bootloader_uart_mbr_pca10040.emSession └── pca10040_uart_debug ├── config └── sdk_config.h └── ses ├── flash_placement.xml ├── secure_bootloader_uart_mbr_pca10040_debug.emProject └── secure_bootloader_uart_mbr_pca10040_debug.emSession /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Object files 5 | *.o 6 | *.ko 7 | *.obj 8 | *.elf 9 | 10 | # Linker output 11 | *.ilk 12 | *.map 13 | *.exp 14 | 15 | # Precompiled Headers 16 | *.gch 17 | *.pch 18 | 19 | # Libraries 20 | *.lib 21 | *.a 22 | *.la 23 | *.lo 24 | 25 | # Shared objects (inc. Windows DLLs) 26 | *.dll 27 | *.so 28 | *.so.* 29 | *.dylib 30 | 31 | # Executables 32 | *.exe 33 | *.out 34 | *.app 35 | *.i*86 36 | *.x86_64 37 | *.hex 38 | 39 | # Debug files 40 | *.dSYM/ 41 | *.su 42 | *.idb 43 | *.pdb 44 | 45 | # Kernel Module Compile Results 46 | *.mod* 47 | *.cmd 48 | .tmp_versions/ 49 | modules.order 50 | Module.symvers 51 | Mkfile.old 52 | dkms.conf 53 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Serial DFU Host Application (By using C programming language) 2 | 3 | This demo is to implement the DFU host application by using C code instead of using nRFutil. 4 | 5 | It is based on the nRFUtil 3.5.0 protocol to port on corresponding C code. 6 | 7 | * Secure DFU UART transport is supported. 8 | * UART bit rate is 115200bps with 8-N-1 data format. RTS/CTS flow control is enabled. It is the same as nRF5 SDK DFU bootloader. 9 | * The program takes the DFU package ZIP file generated by Python nrfutil. 10 | * nrfutil v3.5.0 is referred to. 11 | 12 | ## Example Package 13 | 14 | For the detail testing procedures, you can refer to the blog as 15 | 16 | https://jimmywongbluetooth.wordpress.com/2019/03/06/serial-uart-dfu-on-nrf5-sdk-by-using-host-tool-instead-of-nrfutil-in-c-code-application/ 17 | 18 | ## Requirement 19 | 20 | Nordic nRF5 SDK 15.2 21 | NRF52832 DK Board 22 | nrfutil 3.5 / 4.0 23 | 24 | To compile it, clone the repository in the \nRF5_SDK_15.2.0_9412b96\ folder. If you download the zip, place each of the project folders of this repository into the \nRF5_SDK_15.2.0_9412b96\ folder. 25 | 26 | This example is tested and worked with Nordic SDK 15.2. 27 | 28 | -------------------------------------------------------------------------------- /UartSecureDFU.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Express 14 for Windows Desktop 4 | VisualStudioVersion = 14.0.25420.1 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "UartSecureDFU", "UartSecureDFU\UartSecureDFU.vcxproj", "{7A738474-A62F-4AF8-B8C7-ACBF9DD678A5}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|x64 = Debug|x64 11 | Debug|x86 = Debug|x86 12 | Release|x64 = Release|x64 13 | Release|x86 = Release|x86 14 | EndGlobalSection 15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 16 | {7A738474-A62F-4AF8-B8C7-ACBF9DD678A5}.Debug|x64.ActiveCfg = Debug|x64 17 | {7A738474-A62F-4AF8-B8C7-ACBF9DD678A5}.Debug|x64.Build.0 = Debug|x64 18 | {7A738474-A62F-4AF8-B8C7-ACBF9DD678A5}.Debug|x86.ActiveCfg = Debug|Win32 19 | {7A738474-A62F-4AF8-B8C7-ACBF9DD678A5}.Debug|x86.Build.0 = Debug|Win32 20 | {7A738474-A62F-4AF8-B8C7-ACBF9DD678A5}.Release|x64.ActiveCfg = Release|x64 21 | {7A738474-A62F-4AF8-B8C7-ACBF9DD678A5}.Release|x64.Build.0 = Release|x64 22 | {7A738474-A62F-4AF8-B8C7-ACBF9DD678A5}.Release|x86.ActiveCfg = Release|Win32 23 | {7A738474-A62F-4AF8-B8C7-ACBF9DD678A5}.Release|x86.Build.0 = Release|Win32 24 | EndGlobalSection 25 | GlobalSection(SolutionProperties) = preSolution 26 | HideSolutionNode = FALSE 27 | EndGlobalSection 28 | EndGlobal 29 | -------------------------------------------------------------------------------- /UartSecureDFU/LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2010 Serge A. Zaitsev 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. 20 | 21 | -------------------------------------------------------------------------------- /UartSecureDFU/Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | CFLAGS = -Wall -O2 -I. 3 | BIN = UartSecureDFU 4 | 5 | DEPS = crc32.h \ 6 | delay_connect.h \ 7 | dfu.h \ 8 | dfu_serial.h \ 9 | logging.h \ 10 | slip_enc.h \ 11 | uart_drv.h \ 12 | uart_slip.h \ 13 | zip.h \ 14 | miniz.h \ 15 | jsmn.h \ 16 | Makefile 17 | 18 | OBJS = crc32.o \ 19 | delay_connect.o \ 20 | dfu.o \ 21 | dfu_serial.o \ 22 | jsmn.o \ 23 | logging.o \ 24 | slip_enc.o \ 25 | uart_linux.o \ 26 | UartSecureDFU.o \ 27 | uart_slip.o \ 28 | zip.o 29 | 30 | %.o: %.c $(DEPS) 31 | $(CC) $(CFLAGS) -c $< -o $@ 32 | 33 | $(BIN): $(OBJS) 34 | $(CC) $(OBJS) -o $(BIN) 35 | 36 | clean: 37 | rm -f $(BIN) $(OBJS) 38 | -------------------------------------------------------------------------------- /UartSecureDFU/Makefile.win32: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | CFLAGS = -Wall -O2 -I. -DWIN32 3 | BIN = UartSecureDFU 4 | 5 | DEPS = crc32.h \ 6 | delay_connect.h \ 7 | dfu.h \ 8 | dfu_serial.h \ 9 | logging.h \ 10 | slip_enc.h \ 11 | uart_drv.h \ 12 | uart_slip.h \ 13 | zip.h \ 14 | miniz.h \ 15 | jsmn.h \ 16 | Makefile 17 | 18 | OBJS = crc32.o \ 19 | delay_connect.o \ 20 | dfu.o \ 21 | dfu_serial.o \ 22 | jsmn.o \ 23 | logging.o \ 24 | slip_enc.o \ 25 | uart_win32.o \ 26 | UartSecureDFU.o \ 27 | uart_slip.o \ 28 | zip.o 29 | 30 | %.o: %.c $(DEPS) 31 | $(CC) $(CFLAGS) -c $< -o $@ 32 | 33 | $(BIN): $(OBJS) 34 | $(CC) $(OBJS) -o $(BIN) 35 | 36 | clean: 37 | rm -f $(BIN) $(OBJS) 38 | -------------------------------------------------------------------------------- /UartSecureDFU/ReadMe.txt: -------------------------------------------------------------------------------- 1 | ======================================================================== 2 | CONSOLE APPLICATION : UartSecureDFU Project Overview 3 | ======================================================================== 4 | 5 | AppWizard has created this UartSecureDFU application for you. 6 | 7 | This file contains a summary of what you will find in each of the files that 8 | make up your UartSecureDFU application. 9 | 10 | 11 | UartSecureDFU.vcxproj 12 | This is the main project file for VC++ projects generated using an Application Wizard. 13 | It contains information about the version of Visual C++ that generated the file, and 14 | information about the platforms, configurations, and project features selected with the 15 | Application Wizard. 16 | 17 | UartSecureDFU.vcxproj.filters 18 | This is the filters file for VC++ projects generated using an Application Wizard. 19 | It contains information about the association between the files in your project 20 | and the filters. This association is used in the IDE to show grouping of files with 21 | similar extensions under a specific node (for e.g. ".cpp" files are associated with the 22 | "Source Files" filter). 23 | 24 | UartSecureDFU.cpp 25 | This is the main application source file. 26 | 27 | ///////////////////////////////////////////////////////////////////////////// 28 | Other standard files: 29 | 30 | StdAfx.h, StdAfx.cpp 31 | These files are used to build a precompiled header (PCH) file 32 | named UartSecureDFU.pch and a precompiled types file named StdAfx.obj. 33 | 34 | ///////////////////////////////////////////////////////////////////////////// 35 | Other notes: 36 | 37 | AppWizard uses "TODO:" comments to indicate parts of the source code you 38 | should add to or customize. 39 | 40 | ///////////////////////////////////////////////////////////////////////////// 41 | -------------------------------------------------------------------------------- /UartSecureDFU/UartSecureDFU.c: -------------------------------------------------------------------------------- 1 | // UartSecureDFU.c : Defines the entry point for the console application. 2 | // 3 | 4 | #include 5 | #include 6 | #include "uart_drv.h" 7 | #include "uart_slip.h" 8 | #include "dfu.h" 9 | #include "logging.h" 10 | 11 | 12 | static int is_argv_verbose(char *p_argv) 13 | { 14 | int err_code; 15 | 16 | if (!strcmp(p_argv, "-v") || !strcmp(p_argv, "-V")) 17 | err_code = 0; 18 | else 19 | err_code = 1; 20 | 21 | return err_code; 22 | } 23 | 24 | int main(int argc, char *argv[]) 25 | { 26 | int err_code = 0; 27 | int show_usage = 0; 28 | char *portName = NULL; 29 | uart_drv_t uart_drv; 30 | char *zipName = NULL; 31 | int argn; 32 | int info_lvl = LOGGER_INFO_LVL_0; 33 | 34 | if (argc >= 2 && strlen(argv[1]) > 0) 35 | portName = argv[1]; 36 | else 37 | { 38 | show_usage = 1; 39 | err_code = 1; 40 | } 41 | 42 | if (argc >= 3 && strlen(argv[2]) > 0) 43 | zipName = argv[2]; 44 | else 45 | { 46 | show_usage = 1; 47 | err_code = 1; 48 | } 49 | 50 | for (argn = 3; argn < argc && !err_code; argn++) 51 | { 52 | err_code = is_argv_verbose(argv[argn]); 53 | if (!err_code) 54 | { 55 | info_lvl++; 56 | 57 | logger_set_info_level(info_lvl); 58 | 59 | if (info_lvl >= LOGGER_INFO_LVL_3) 60 | break; 61 | } 62 | else 63 | { 64 | show_usage = 1; 65 | err_code = 1; 66 | 67 | break; 68 | } 69 | } 70 | 71 | if (show_usage) 72 | { 73 | printf("Usage: UartSecureDFU serial_port package_name [-v] [-v] [-v]\n"); 74 | } 75 | 76 | uart_drv.p_PortName = portName; 77 | 78 | if (!err_code) 79 | { 80 | err_code = uart_slip_open(&uart_drv); 81 | } 82 | 83 | if (!err_code) 84 | { 85 | dfu_param_t dfu_param; 86 | 87 | dfu_param.p_uart = &uart_drv; 88 | dfu_param.p_pkg_file = zipName; 89 | err_code = dfu_send_package(&dfu_param); 90 | } 91 | 92 | if (!show_usage) 93 | { 94 | int err_code2 = uart_slip_close(&uart_drv); 95 | 96 | if (!err_code) 97 | err_code = err_code2; 98 | } 99 | 100 | return err_code; 101 | } 102 | -------------------------------------------------------------------------------- /UartSecureDFU/UartSecureDFU.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | Debug 14 | x64 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | {7A738474-A62F-4AF8-B8C7-ACBF9DD678A5} 23 | Win32Proj 24 | UartSecureDFU 25 | 8.1 26 | 27 | 28 | 29 | Application 30 | true 31 | v140 32 | NotSet 33 | 34 | 35 | Application 36 | false 37 | v140 38 | true 39 | NotSet 40 | 41 | 42 | Application 43 | true 44 | v140 45 | Unicode 46 | 47 | 48 | Application 49 | false 50 | v140 51 | true 52 | Unicode 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | true 74 | 75 | 76 | true 77 | 78 | 79 | false 80 | 81 | 82 | false 83 | 84 | 85 | 86 | 87 | 88 | Level3 89 | Disabled 90 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) 91 | 92 | 93 | Console 94 | true 95 | 96 | 97 | 98 | 99 | 100 | 101 | Level3 102 | Disabled 103 | _DEBUG;_CONSOLE;%(PreprocessorDefinitions) 104 | 105 | 106 | Console 107 | true 108 | 109 | 110 | 111 | 112 | Level3 113 | 114 | 115 | MaxSpeed 116 | true 117 | true 118 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 119 | 120 | 121 | Console 122 | true 123 | true 124 | true 125 | 126 | 127 | 128 | 129 | Level3 130 | 131 | 132 | MaxSpeed 133 | true 134 | true 135 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 136 | 137 | 138 | Console 139 | true 140 | true 141 | true 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | -------------------------------------------------------------------------------- /UartSecureDFU/UartSecureDFU.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | Source Files 23 | 24 | 25 | Source Files 26 | 27 | 28 | Source Files 29 | 30 | 31 | Source Files 32 | 33 | 34 | Source Files 35 | 36 | 37 | Source Files 38 | 39 | 40 | Source Files 41 | 42 | 43 | Source Files 44 | 45 | 46 | Source Files 47 | 48 | 49 | Source Files 50 | 51 | 52 | Source Files 53 | 54 | 55 | -------------------------------------------------------------------------------- /UartSecureDFU/crc32.c: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2013 - 2018, Nordic Semiconductor ASA 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without modification, 7 | * are permitted provided that the following conditions are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright notice, this 10 | * list of conditions and the following disclaimer. 11 | * 12 | * 2. Redistributions in binary form, except as embedded into a Nordic 13 | * Semiconductor ASA integrated circuit in a product or a software update for 14 | * such product, must reproduce the above copyright notice, this list of 15 | * conditions and the following disclaimer in the documentation and/or other 16 | * materials provided with the distribution. 17 | * 18 | * 3. Neither the name of Nordic Semiconductor ASA nor the names of its 19 | * contributors may be used to endorse or promote products derived from this 20 | * software without specific prior written permission. 21 | * 22 | * 4. This software, with or without modification, must only be used with a 23 | * Nordic Semiconductor ASA integrated circuit. 24 | * 25 | * 5. Any software provided in binary form under this license must not be reverse 26 | * engineered, decompiled, modified and/or disassembled. 27 | * 28 | * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS 29 | * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 30 | * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE 31 | * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE 32 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 33 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 34 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 37 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 38 | * 39 | */ 40 | #include "crc32.h" 41 | 42 | #include 43 | 44 | uint32_t crc32_compute(uint8_t const * p_data, uint32_t size, uint32_t const * p_crc) 45 | { 46 | uint32_t crc; 47 | uint32_t i, j; 48 | 49 | crc = (p_crc == NULL) ? 0xFFFFFFFF : ~(*p_crc); 50 | for (i = 0; i < size; i++) 51 | { 52 | crc = crc ^ p_data[i]; 53 | for (j = 8; j > 0; j--) 54 | { 55 | crc = (crc >> 1) ^ (0xEDB88320U & ((crc & 1) ? 0xFFFFFFFF : 0)); 56 | } 57 | } 58 | return ~crc; 59 | } 60 | -------------------------------------------------------------------------------- /UartSecureDFU/crc32.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2015 - 2018, Nordic Semiconductor ASA 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without modification, 7 | * are permitted provided that the following conditions are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright notice, this 10 | * list of conditions and the following disclaimer. 11 | * 12 | * 2. Redistributions in binary form, except as embedded into a Nordic 13 | * Semiconductor ASA integrated circuit in a product or a software update for 14 | * such product, must reproduce the above copyright notice, this list of 15 | * conditions and the following disclaimer in the documentation and/or other 16 | * materials provided with the distribution. 17 | * 18 | * 3. Neither the name of Nordic Semiconductor ASA nor the names of its 19 | * contributors may be used to endorse or promote products derived from this 20 | * software without specific prior written permission. 21 | * 22 | * 4. This software, with or without modification, must only be used with a 23 | * Nordic Semiconductor ASA integrated circuit. 24 | * 25 | * 5. Any software provided in binary form under this license must not be reverse 26 | * engineered, decompiled, modified and/or disassembled. 27 | * 28 | * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS 29 | * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 30 | * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE 31 | * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE 32 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 33 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 34 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 37 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 38 | * 39 | */ 40 | /** @file 41 | * 42 | * @defgroup crc32 CRC32 compute 43 | * @{ 44 | * @ingroup hci_transport 45 | * 46 | * @brief This module implements the CRC-32 calculation in the blocks. 47 | */ 48 | 49 | #ifndef CRC32_H__ 50 | #define CRC32_H__ 51 | 52 | #include 53 | 54 | #ifdef __cplusplus 55 | extern "C" { 56 | #endif 57 | 58 | /**@brief Function for calculating CRC-32 in blocks. 59 | * 60 | * Feed each consecutive data block into this function, along with the current value of p_crc as 61 | * returned by the previous call of this function. The first call of this function should pass NULL 62 | * as the initial value of the crc in p_crc. 63 | * 64 | * @param[in] p_data The input data block for computation. 65 | * @param[in] size The size of the input data block in bytes. 66 | * @param[in] p_crc The previous calculated CRC-32 value or NULL if first call. 67 | * 68 | * @return The updated CRC-32 value, based on the input supplied. 69 | */ 70 | uint32_t crc32_compute(uint8_t const * p_data, uint32_t size, uint32_t const * p_crc); 71 | 72 | 73 | #ifdef __cplusplus 74 | } 75 | #endif 76 | 77 | #endif // CRC32_H__ 78 | 79 | /** @} */ 80 | -------------------------------------------------------------------------------- /UartSecureDFU/delay_connect.c: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2018, Nordic Semiconductor ASA 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without modification, 7 | * are permitted provided that the following conditions are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright notice, this 10 | * list of conditions and the following disclaimer. 11 | * 12 | * 2. Redistributions in binary form, except as embedded into a Nordic 13 | * Semiconductor ASA integrated circuit in a product or a software update for 14 | * such product, must reproduce the above copyright notice, this list of 15 | * conditions and the following disclaimer in the documentation and/or other 16 | * materials provided with the distribution. 17 | * 18 | * 3. Neither the name of Nordic Semiconductor ASA nor the names of its 19 | * contributors may be used to endorse or promote products derived from this 20 | * software without specific prior written permission. 21 | * 22 | * 4. This software, with or without modification, must only be used with a 23 | * Nordic Semiconductor ASA integrated circuit. 24 | * 25 | * 5. Any software provided in binary form under this license must not be reverse 26 | * engineered, decompiled, modified and/or disassembled. 27 | * 28 | * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS 29 | * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 30 | * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE 31 | * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE 32 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 33 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 34 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 37 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 38 | * 39 | */ 40 | 41 | #ifdef WIN32 42 | #include 43 | #else 44 | #include 45 | #endif 46 | #include "delay_connect.h" 47 | 48 | // number of seconds to delay before connect 49 | #define DELAY_CONNECT_SEC 5 50 | 51 | int delay_connect(void) 52 | { 53 | #ifdef WIN32 54 | Sleep(DELAY_CONNECT_SEC * 1000); 55 | #else 56 | sleep(DELAY_CONNECT_SEC); 57 | #endif 58 | 59 | return 0; 60 | } 61 | -------------------------------------------------------------------------------- /UartSecureDFU/delay_connect.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2018, Nordic Semiconductor ASA 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without modification, 7 | * are permitted provided that the following conditions are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright notice, this 10 | * list of conditions and the following disclaimer. 11 | * 12 | * 2. Redistributions in binary form, except as embedded into a Nordic 13 | * Semiconductor ASA integrated circuit in a product or a software update for 14 | * such product, must reproduce the above copyright notice, this list of 15 | * conditions and the following disclaimer in the documentation and/or other 16 | * materials provided with the distribution. 17 | * 18 | * 3. Neither the name of Nordic Semiconductor ASA nor the names of its 19 | * contributors may be used to endorse or promote products derived from this 20 | * software without specific prior written permission. 21 | * 22 | * 4. This software, with or without modification, must only be used with a 23 | * Nordic Semiconductor ASA integrated circuit. 24 | * 25 | * 5. Any software provided in binary form under this license must not be reverse 26 | * engineered, decompiled, modified and/or disassembled. 27 | * 28 | * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS 29 | * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 30 | * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE 31 | * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE 32 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 33 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 34 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 37 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 38 | * 39 | */ 40 | 41 | #pragma once 42 | 43 | #ifndef _INC_DELAY_CONNECT 44 | #define _INC_DELAY_CONNECT 45 | 46 | 47 | #ifdef __cplusplus 48 | extern "C" { 49 | #endif /* __cplusplus */ 50 | 51 | int delay_connect(void); 52 | 53 | #ifdef __cplusplus 54 | } /* ... extern "C" */ 55 | #endif /* __cplusplus */ 56 | 57 | 58 | #endif // _INC_DELAY_CONNECT 59 | -------------------------------------------------------------------------------- /UartSecureDFU/dfu.c: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2018, Nordic Semiconductor ASA 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without modification, 7 | * are permitted provided that the following conditions are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright notice, this 10 | * list of conditions and the following disclaimer. 11 | * 12 | * 2. Redistributions in binary form, except as embedded into a Nordic 13 | * Semiconductor ASA integrated circuit in a product or a software update for 14 | * such product, must reproduce the above copyright notice, this list of 15 | * conditions and the following disclaimer in the documentation and/or other 16 | * materials provided with the distribution. 17 | * 18 | * 3. Neither the name of Nordic Semiconductor ASA nor the names of its 19 | * contributors may be used to endorse or promote products derived from this 20 | * software without specific prior written permission. 21 | * 22 | * 4. This software, with or without modification, must only be used with a 23 | * Nordic Semiconductor ASA integrated circuit. 24 | * 25 | * 5. Any software provided in binary form under this license must not be reverse 26 | * engineered, decompiled, modified and/or disassembled. 27 | * 28 | * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS 29 | * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 30 | * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE 31 | * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE 32 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 33 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 34 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 37 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 38 | * 39 | */ 40 | 41 | #include 42 | #include 43 | #include 44 | #include "dfu.h" 45 | #include "dfu_serial.h" 46 | #include "delay_connect.h" 47 | #include "logging.h" 48 | #include "zip.h" 49 | #include "jsmn.h" 50 | 51 | // maximum number of JSON tokens to process 52 | #define JSON_TOKEN_NUM_MAX 30 53 | 54 | // maximum number of DFU objects to process 55 | #define DFU_OBJECT_NUM_MAX 3 56 | 57 | typedef enum { 58 | DFU_IMG_NIL = 0, //!< DFU image invalid 59 | DFU_IMG_APP = 1, //!< DFU application image 60 | DFU_IMG_BL = 2, //!< DFU bootloader image 61 | DFU_IMG_SD = 3, //!< DFU SoftDevice image 62 | DFU_IMG_SD_BL = 4 //!< DFU SoftDevice & bootloader image 63 | } dfu_image_type_t; 64 | 65 | typedef enum { 66 | STR_UNDEFINED = 0, 67 | STR_FILE_BIN = 1, 68 | STR_FILE_DAT = 2 69 | } dfu_json_str_t; 70 | 71 | typedef struct { 72 | jsmntype_t type; //!< Token type. 73 | int size; //!< Number of child tokens. 74 | char *str; //!< String value. 75 | dfu_json_str_t str_type; //!< String usage type. 76 | } jsmn_entity_t; 77 | 78 | typedef struct { 79 | dfu_image_type_t img_type; //!< DFU image type. 80 | const jsmn_entity_t *p_pattern; //!< JSMN token pattern. 81 | } dfu_image_jsmn_pattern; 82 | 83 | typedef struct 84 | { 85 | dfu_image_type_t img_type; //!< DFU image type. 86 | 87 | char *file_bin; //!< BIN file name. 88 | char *file_dat; //!< DAT file name. 89 | } dfu_json_object_t; 90 | 91 | typedef struct 92 | { 93 | uart_drv_t *p_uart; 94 | 95 | uint8_t *p_img_dat; //!< Image DAT pointer. 96 | uint32_t n_dat_size; //!< Image DAT size. 97 | uint8_t *p_img_bin; //!< Image BIN pointer. 98 | uint32_t n_bin_size; //!< Image BIN size. 99 | } dfu_img_param_t; 100 | 101 | // JSON tokens 102 | static jsmntok_t json_tokens[JSON_TOKEN_NUM_MAX]; 103 | 104 | // DFU objects 105 | static dfu_json_object_t dfu_objects[DFU_OBJECT_NUM_MAX]; 106 | 107 | // JSMN token pattern for Manifest 108 | static const jsmn_entity_t dfu_mft_pattern[] = 109 | { 110 | { JSMN_OBJECT, 1, NULL, STR_UNDEFINED }, 111 | { JSMN_STRING, 1, "manifest", STR_UNDEFINED }, 112 | { JSMN_UNDEFINED, 0, NULL, STR_UNDEFINED } 113 | }; 114 | 115 | // JSMN token pattern for 1 DFU image 116 | static const jsmn_entity_t dfu_img_1_pattern[] = 117 | { 118 | { JSMN_OBJECT, 1, NULL, STR_UNDEFINED }, 119 | { JSMN_UNDEFINED, 0, NULL, STR_UNDEFINED } 120 | }; 121 | 122 | // JSMN token pattern for 2 DFU images 123 | static const jsmn_entity_t dfu_img_2_pattern[] = 124 | { 125 | { JSMN_OBJECT, 2, NULL, STR_UNDEFINED }, 126 | { JSMN_UNDEFINED, 0, NULL, STR_UNDEFINED } 127 | }; 128 | 129 | // JSMN token pattern for DFU application 130 | static const jsmn_entity_t dfu_app_pattern[] = 131 | { 132 | { JSMN_STRING, 1, "application", STR_UNDEFINED }, 133 | { JSMN_OBJECT, 2, NULL, STR_UNDEFINED }, 134 | { JSMN_STRING, 1, "bin_file", STR_UNDEFINED }, 135 | { JSMN_STRING, 0, NULL, STR_FILE_BIN }, 136 | { JSMN_STRING, 1, "dat_file", STR_UNDEFINED }, 137 | { JSMN_STRING, 0, NULL, STR_FILE_DAT }, 138 | { JSMN_UNDEFINED, 0, NULL, STR_UNDEFINED } 139 | }; 140 | 141 | // JSMN token pattern for DFU bootloader 142 | static const jsmn_entity_t dfu_bl_pattern[] = 143 | { 144 | { JSMN_STRING, 1, "bootloader", STR_UNDEFINED }, 145 | { JSMN_OBJECT, 2, NULL, STR_UNDEFINED }, 146 | { JSMN_STRING, 1, "bin_file", STR_UNDEFINED }, 147 | { JSMN_STRING, 0, NULL, STR_FILE_BIN }, 148 | { JSMN_STRING, 1, "dat_file", STR_UNDEFINED }, 149 | { JSMN_STRING, 0, NULL, STR_FILE_DAT }, 150 | { JSMN_UNDEFINED, 0, NULL, STR_UNDEFINED } 151 | }; 152 | 153 | // JSMN token pattern for DFU SoftDevice 154 | static const jsmn_entity_t dfu_sd_pattern[] = 155 | { 156 | { JSMN_STRING, 1, "softdevice", STR_UNDEFINED }, 157 | { JSMN_OBJECT, 2, NULL, STR_UNDEFINED }, 158 | { JSMN_STRING, 1, "bin_file", STR_UNDEFINED }, 159 | { JSMN_STRING, 0, NULL, STR_FILE_BIN }, 160 | { JSMN_STRING, 1, "dat_file", STR_UNDEFINED }, 161 | { JSMN_STRING, 0, NULL, STR_FILE_DAT }, 162 | { JSMN_UNDEFINED, 0, NULL, STR_UNDEFINED } 163 | }; 164 | 165 | // JSMN token pattern for DFU SoftDevice & bootloader 166 | static const jsmn_entity_t dfu_sd_bl_pattern[] = 167 | { 168 | { JSMN_STRING, 1, "softdevice_bootloader", STR_UNDEFINED }, 169 | { JSMN_OBJECT, 3, NULL, STR_UNDEFINED }, 170 | { JSMN_STRING, 1, "bin_file", STR_UNDEFINED }, 171 | { JSMN_STRING, 0, NULL, STR_FILE_BIN }, 172 | { JSMN_STRING, 1, "dat_file", STR_UNDEFINED }, 173 | { JSMN_STRING, 0, NULL, STR_FILE_DAT }, 174 | { JSMN_STRING, 1, "info_read_only_metadata", STR_UNDEFINED }, 175 | { JSMN_OBJECT, 2, NULL, STR_UNDEFINED }, 176 | { JSMN_STRING, 1, "bl_size", STR_UNDEFINED }, 177 | { JSMN_PRIMITIVE, 0, NULL, STR_UNDEFINED }, 178 | { JSMN_STRING, 1, "sd_size", STR_UNDEFINED }, 179 | { JSMN_PRIMITIVE, 0, NULL, STR_UNDEFINED }, 180 | { JSMN_UNDEFINED, 0, NULL, STR_UNDEFINED } 181 | }; 182 | 183 | // JSMN token pattern table 184 | static const dfu_image_jsmn_pattern dfu_pattern_tbl[] = 185 | { 186 | { DFU_IMG_APP, dfu_app_pattern }, 187 | { DFU_IMG_BL, dfu_bl_pattern }, 188 | { DFU_IMG_SD, dfu_sd_pattern }, 189 | { DFU_IMG_SD_BL, dfu_sd_bl_pattern }, 190 | { DFU_IMG_NIL, NULL } 191 | }; 192 | 193 | static void free_dfu_json_obj(dfu_json_object_t *p_dfu_obj); 194 | 195 | // allocate memory to store a JSON string 196 | static char *put_json_to_string(const uint8_t *p_data, int len) 197 | { 198 | char *p_str; 199 | 200 | if (len >= 0) 201 | p_str = (char *)malloc(len + 1); 202 | else 203 | p_str = NULL; 204 | 205 | if (p_str != NULL) 206 | { 207 | memcpy(p_str, p_data, len); 208 | *(p_str + len) = '\0'; 209 | } 210 | 211 | return p_str; 212 | } 213 | 214 | // map a JSMN token to a DFU JSON pattern 215 | static int map_jsmn_token_to_pattern(dfu_json_object_t *p_dfu_obj, const jsmn_entity_t *p_pattern, const jsmntok_t *p_tokens, int num_tokens, const uint8_t *p_data) 216 | { 217 | int i, j = 0; 218 | jsmntype_t jsmn_type; 219 | 220 | // compare the JSMN tokens one by one 221 | for (i = 0; (jsmn_type = (p_pattern + i)->type) != JSMN_UNDEFINED; i++) 222 | { 223 | if (i >= num_tokens || 224 | jsmn_type != (p_tokens + i)->type || 225 | (p_pattern + i)->size != (p_tokens + i)->size) 226 | { 227 | // type or size mismatch... 228 | j = -1; 229 | break; 230 | } 231 | 232 | if (jsmn_type == JSMN_STRING) 233 | { 234 | int len = (p_tokens + i)->end - (p_tokens + i)->start; 235 | char *p_str = put_json_to_string(p_data + (p_tokens + i)->start, len); 236 | 237 | if (p_str == NULL) 238 | { 239 | // cannot allocate memory... 240 | j = -1; 241 | break; 242 | } 243 | 244 | // check the pattern string 245 | if ((p_pattern + i)->str != NULL && strcmp(p_str, (p_pattern + i)->str)) 246 | { 247 | free(p_str); 248 | 249 | // mismatch... 250 | j = -1; 251 | break; 252 | } 253 | 254 | // store string target, if any 255 | switch ((p_pattern + i)->str_type) 256 | { 257 | case STR_FILE_DAT: 258 | if (p_dfu_obj != NULL) 259 | p_dfu_obj->file_dat = p_str; 260 | break; 261 | case STR_FILE_BIN: 262 | if (p_dfu_obj != NULL) 263 | p_dfu_obj->file_bin = p_str; 264 | break; 265 | default: 266 | free(p_str); 267 | break; 268 | } 269 | } 270 | } 271 | 272 | if (j < 0) 273 | { 274 | i = j; 275 | 276 | if (p_dfu_obj != NULL) 277 | free_dfu_json_obj(p_dfu_obj); 278 | } 279 | 280 | return i; 281 | } 282 | 283 | // free allocated strings 284 | static void free_dfu_json_obj(dfu_json_object_t *p_dfu_obj) 285 | { 286 | if (p_dfu_obj->file_dat != NULL) 287 | { 288 | free(p_dfu_obj->file_dat); 289 | p_dfu_obj->file_dat = NULL; 290 | } 291 | if (p_dfu_obj->file_bin != NULL) 292 | { 293 | free(p_dfu_obj->file_bin); 294 | p_dfu_obj->file_bin = NULL; 295 | } 296 | } 297 | 298 | static int dfu_send_image(dfu_img_param_t *p_dfu_img) 299 | { 300 | int err_code; 301 | 302 | err_code = dfu_serial_open(p_dfu_img->p_uart); 303 | 304 | if (!err_code) 305 | { 306 | err_code = dfu_serial_send_init_packet(p_dfu_img->p_uart, p_dfu_img->p_img_dat, p_dfu_img->n_dat_size); 307 | } 308 | 309 | if (!err_code) 310 | { 311 | err_code = dfu_serial_send_firmware(p_dfu_img->p_uart, p_dfu_img->p_img_bin, p_dfu_img->n_bin_size); 312 | } 313 | 314 | if (!err_code) 315 | { 316 | err_code = dfu_serial_close(p_dfu_img->p_uart); 317 | } 318 | 319 | return err_code; 320 | } 321 | 322 | static int dfu_send_object(uart_drv_t *p_uart, dfu_json_object_t *p_dfu_obj, struct zip_t *p_zip_pkg) 323 | { 324 | int err_code = 0; 325 | uint8_t *buf_dat = NULL; 326 | size_t buf_dat_size; 327 | uint8_t *buf_bin = NULL; 328 | size_t buf_bin_size; 329 | dfu_img_param_t dfu_img; 330 | 331 | if (zip_entry_open(p_zip_pkg, p_dfu_obj->file_dat)) 332 | { 333 | logger_error("Cannot open package DAT file!"); 334 | 335 | err_code = 1; 336 | } 337 | else 338 | { 339 | if (zip_entry_read(p_zip_pkg, (void **)&buf_dat, &buf_dat_size)) 340 | { 341 | logger_error("Cannot read package DAT file!"); 342 | 343 | err_code = 1; 344 | } 345 | 346 | zip_entry_close(p_zip_pkg); 347 | } 348 | 349 | if (!err_code) 350 | { 351 | if (zip_entry_open(p_zip_pkg, p_dfu_obj->file_bin)) 352 | { 353 | logger_error("Cannot open package BIN file!"); 354 | 355 | err_code = 1; 356 | } 357 | else 358 | { 359 | if (zip_entry_read(p_zip_pkg, (void **)&buf_bin, &buf_bin_size)) 360 | { 361 | logger_error("Cannot read package BIN file!"); 362 | 363 | err_code = 1; 364 | } 365 | 366 | zip_entry_close(p_zip_pkg); 367 | } 368 | } 369 | 370 | if (!err_code) 371 | { 372 | dfu_img.p_uart = p_uart; 373 | dfu_img.p_img_dat = buf_dat; 374 | dfu_img.n_dat_size = buf_dat_size; 375 | dfu_img.p_img_bin = buf_bin; 376 | dfu_img.n_bin_size = buf_bin_size; 377 | err_code = dfu_send_image(&dfu_img); 378 | } 379 | 380 | if (buf_dat != NULL) 381 | free(buf_dat); 382 | 383 | if (buf_bin != NULL) 384 | free(buf_bin); 385 | 386 | return err_code; 387 | } 388 | 389 | static dfu_json_object_t *find_dfu_object(dfu_json_object_t *p_dfu_obj, int num_obj, dfu_image_type_t img_type) 390 | { 391 | dfu_json_object_t *p_obj = NULL; 392 | int i; 393 | 394 | for (i = 0; i < num_obj; i++) 395 | { 396 | if ((p_dfu_obj + i)->img_type == img_type) 397 | { 398 | p_obj = p_dfu_obj + i; 399 | break; 400 | } 401 | } 402 | 403 | return p_obj; 404 | } 405 | 406 | int dfu_send_package(dfu_param_t *p_dfu) 407 | { 408 | int err_code = 0; 409 | struct zip_t *zip_pkg; 410 | uint8_t *buf_json = NULL; 411 | size_t bufsize; 412 | jsmn_parser parser; 413 | int num_tokens; 414 | int num_images, img_n = 0; 415 | dfu_json_object_t *p_dfu_object; 416 | int i, n; 417 | 418 | zip_pkg = zip_open(p_dfu->p_pkg_file, 0, 'r'); 419 | if (zip_pkg == NULL) 420 | { 421 | logger_error("Cannot open ZIP package file!"); 422 | 423 | err_code = 1; 424 | } 425 | else 426 | { 427 | if (zip_entry_open(zip_pkg, "manifest.json")) 428 | { 429 | logger_error("Cannot open package manifest file!"); 430 | 431 | err_code = 1; 432 | } 433 | else 434 | { 435 | if (zip_entry_read(zip_pkg, (void **)&buf_json, &bufsize)) 436 | { 437 | logger_error("Cannot read package manifest file!"); 438 | 439 | err_code = 1; 440 | } 441 | else 442 | { 443 | zip_entry_close(zip_pkg); 444 | } 445 | } 446 | } 447 | 448 | if (!err_code) 449 | { 450 | jsmn_init(&parser); 451 | 452 | num_tokens = jsmn_parse(&parser, (char *)buf_json, bufsize, json_tokens, JSON_TOKEN_NUM_MAX); 453 | 454 | if (num_tokens < 0) 455 | { 456 | logger_error("Cannot parse package manifest json (%d)!", num_tokens); 457 | 458 | err_code = 1; 459 | } 460 | } 461 | 462 | for (i = 0; i < DFU_OBJECT_NUM_MAX; i++) 463 | { 464 | dfu_objects[i].file_bin = NULL; 465 | dfu_objects[i].file_dat = NULL; 466 | } 467 | 468 | if (!err_code) 469 | { 470 | // check that JSON starts with a manifest object 471 | i = map_jsmn_token_to_pattern(NULL, dfu_mft_pattern, json_tokens, num_tokens, buf_json); 472 | if (i < 0) 473 | { 474 | logger_error("Cannot get json manifest object!"); 475 | 476 | err_code = 1; 477 | } 478 | n = i; 479 | 480 | if (!err_code) 481 | { 482 | // check whether there are 1 or 2 DFU images 483 | i = map_jsmn_token_to_pattern(NULL, dfu_img_1_pattern, json_tokens + n, num_tokens - n, buf_json); 484 | if (i > 0) 485 | { 486 | num_images = 1; 487 | } 488 | else 489 | { 490 | i = map_jsmn_token_to_pattern(NULL, dfu_img_2_pattern, json_tokens + n, num_tokens - n, buf_json); 491 | if (i > 0) 492 | { 493 | num_images = 2; 494 | } 495 | } 496 | 497 | if (i <= 0) 498 | { 499 | logger_error("Cannot get json number of DFU images!"); 500 | 501 | err_code = 1; 502 | } 503 | } 504 | n += i; 505 | 506 | while (!err_code && n < num_tokens) 507 | { 508 | int t; 509 | 510 | if (img_n >= DFU_OBJECT_NUM_MAX) 511 | { 512 | logger_error("No data storage for json DFU image object!"); 513 | 514 | err_code = 1; 515 | break; 516 | } 517 | 518 | // determine the DFU image type 519 | for (t = 0; dfu_pattern_tbl[t].img_type != DFU_IMG_NIL; t++) 520 | { 521 | i = map_jsmn_token_to_pattern(dfu_objects + img_n, dfu_pattern_tbl[t].p_pattern, json_tokens + n, num_tokens - n, buf_json); 522 | 523 | if (i > 0) 524 | { 525 | dfu_objects[img_n].img_type = dfu_pattern_tbl[t].img_type; 526 | break; 527 | } 528 | } 529 | 530 | if (i <= 0) 531 | { 532 | logger_error("Cannot find json DFU image object!"); 533 | 534 | err_code = 1; 535 | break; 536 | } 537 | else 538 | { 539 | n += i; 540 | 541 | img_n++; 542 | } 543 | } 544 | 545 | if (!err_code && (n != num_tokens || img_n != num_images)) 546 | { 547 | logger_error("Incoherent json object structure detected!"); 548 | 549 | err_code = 1; 550 | } 551 | } 552 | 553 | if (!err_code) 554 | { 555 | // send SoftDevice & bootloader image, if any 556 | p_dfu_object = find_dfu_object(dfu_objects, num_images, DFU_IMG_SD_BL); 557 | if (p_dfu_object != NULL) 558 | { 559 | logger_info_1("Sending SoftDevice+Bootloader image."); 560 | 561 | err_code = dfu_send_object(p_dfu->p_uart, p_dfu_object, zip_pkg); 562 | 563 | if (!err_code && num_images > 1) 564 | err_code = delay_connect(); 565 | } 566 | } 567 | 568 | if (!err_code) 569 | { 570 | // send SoftDevice image, if any 571 | p_dfu_object = find_dfu_object(dfu_objects, num_images, DFU_IMG_SD); 572 | if (p_dfu_object != NULL) 573 | { 574 | logger_info_1("Sending SoftDevice image."); 575 | 576 | err_code = dfu_send_object(p_dfu->p_uart, p_dfu_object, zip_pkg); 577 | 578 | if (!err_code && num_images > 1) 579 | err_code = delay_connect(); 580 | } 581 | } 582 | 583 | if (!err_code) 584 | { 585 | // send bootloader image, if any 586 | p_dfu_object = find_dfu_object(dfu_objects, num_images, DFU_IMG_BL); 587 | if (p_dfu_object != NULL) 588 | { 589 | logger_info_1("Sending Bootloader image."); 590 | 591 | err_code = dfu_send_object(p_dfu->p_uart, p_dfu_object, zip_pkg); 592 | 593 | if (!err_code && num_images > 1) 594 | err_code = delay_connect(); 595 | } 596 | } 597 | 598 | if (!err_code) 599 | { 600 | // send application image, if any 601 | p_dfu_object = find_dfu_object(dfu_objects, num_images, DFU_IMG_APP); 602 | if (p_dfu_object != NULL) 603 | { 604 | logger_info_1("Sending Application image."); 605 | 606 | err_code = dfu_send_object(p_dfu->p_uart, p_dfu_object, zip_pkg); 607 | } 608 | } 609 | 610 | for (i = 0; i < DFU_OBJECT_NUM_MAX; i++) 611 | free_dfu_json_obj(dfu_objects + i); 612 | 613 | if (buf_json != NULL) 614 | free(buf_json); 615 | 616 | if (zip_pkg != NULL) 617 | zip_close(zip_pkg); 618 | 619 | return err_code; 620 | } 621 | -------------------------------------------------------------------------------- /UartSecureDFU/dfu.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2018, Nordic Semiconductor ASA 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without modification, 7 | * are permitted provided that the following conditions are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright notice, this 10 | * list of conditions and the following disclaimer. 11 | * 12 | * 2. Redistributions in binary form, except as embedded into a Nordic 13 | * Semiconductor ASA integrated circuit in a product or a software update for 14 | * such product, must reproduce the above copyright notice, this list of 15 | * conditions and the following disclaimer in the documentation and/or other 16 | * materials provided with the distribution. 17 | * 18 | * 3. Neither the name of Nordic Semiconductor ASA nor the names of its 19 | * contributors may be used to endorse or promote products derived from this 20 | * software without specific prior written permission. 21 | * 22 | * 4. This software, with or without modification, must only be used with a 23 | * Nordic Semiconductor ASA integrated circuit. 24 | * 25 | * 5. Any software provided in binary form under this license must not be reverse 26 | * engineered, decompiled, modified and/or disassembled. 27 | * 28 | * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS 29 | * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 30 | * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE 31 | * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE 32 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 33 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 34 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 37 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 38 | * 39 | */ 40 | 41 | #pragma once 42 | 43 | #ifndef _INC_DFU 44 | #define _INC_DFU 45 | 46 | #include "uart_drv.h" 47 | #include "uart_slip.h" 48 | 49 | 50 | #ifdef __cplusplus 51 | extern "C" { 52 | #endif /* __cplusplus */ 53 | 54 | 55 | typedef struct 56 | { 57 | uart_drv_t *p_uart; 58 | 59 | char *p_pkg_file; 60 | } dfu_param_t; 61 | 62 | int dfu_send_package(dfu_param_t *p_dfu); 63 | 64 | #ifdef __cplusplus 65 | } /* ... extern "C" */ 66 | #endif /* __cplusplus */ 67 | 68 | 69 | #endif // _INC_DFU 70 | -------------------------------------------------------------------------------- /UartSecureDFU/dfu_serial.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2018, Nordic Semiconductor ASA 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without modification, 7 | * are permitted provided that the following conditions are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright notice, this 10 | * list of conditions and the following disclaimer. 11 | * 12 | * 2. Redistributions in binary form, except as embedded into a Nordic 13 | * Semiconductor ASA integrated circuit in a product or a software update for 14 | * such product, must reproduce the above copyright notice, this list of 15 | * conditions and the following disclaimer in the documentation and/or other 16 | * materials provided with the distribution. 17 | * 18 | * 3. Neither the name of Nordic Semiconductor ASA nor the names of its 19 | * contributors may be used to endorse or promote products derived from this 20 | * software without specific prior written permission. 21 | * 22 | * 4. This software, with or without modification, must only be used with a 23 | * Nordic Semiconductor ASA integrated circuit. 24 | * 25 | * 5. Any software provided in binary form under this license must not be reverse 26 | * engineered, decompiled, modified and/or disassembled. 27 | * 28 | * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS 29 | * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 30 | * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE 31 | * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE 32 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 33 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 34 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 37 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 38 | * 39 | */ 40 | 41 | #pragma once 42 | 43 | #ifndef _INC_DFU_SERIAL 44 | #define _INC_DFU_SERIAL 45 | 46 | #include "uart_drv.h" 47 | #include "uart_slip.h" 48 | 49 | 50 | #ifdef __cplusplus 51 | extern "C" { 52 | #endif /* __cplusplus */ 53 | 54 | int dfu_serial_open(uart_drv_t *p_uart); 55 | 56 | int dfu_serial_close(uart_drv_t *p_uart); 57 | 58 | int dfu_serial_send_init_packet(uart_drv_t *p_uart, const uint8_t *p_data, uint32_t data_size); 59 | 60 | int dfu_serial_send_firmware(uart_drv_t *p_uart, const uint8_t *p_data, uint32_t data_size); 61 | 62 | #ifdef __cplusplus 63 | } /* ... extern "C" */ 64 | #endif /* __cplusplus */ 65 | 66 | 67 | #endif // _INC_DFU_SERIAL 68 | -------------------------------------------------------------------------------- /UartSecureDFU/jsmn.c: -------------------------------------------------------------------------------- 1 | #include "jsmn.h" 2 | 3 | /** 4 | * Allocates a fresh unused token from the token pull. 5 | */ 6 | static jsmntok_t *jsmn_alloc_token(jsmn_parser *parser, 7 | jsmntok_t *tokens, size_t num_tokens) { 8 | jsmntok_t *tok; 9 | if (parser->toknext >= num_tokens) { 10 | return NULL; 11 | } 12 | tok = &tokens[parser->toknext++]; 13 | tok->start = tok->end = -1; 14 | tok->size = 0; 15 | #ifdef JSMN_PARENT_LINKS 16 | tok->parent = -1; 17 | #endif 18 | return tok; 19 | } 20 | 21 | /** 22 | * Fills token type and boundaries. 23 | */ 24 | static void jsmn_fill_token(jsmntok_t *token, jsmntype_t type, 25 | int start, int end) { 26 | token->type = type; 27 | token->start = start; 28 | token->end = end; 29 | token->size = 0; 30 | } 31 | 32 | /** 33 | * Fills next available token with JSON primitive. 34 | */ 35 | static int jsmn_parse_primitive(jsmn_parser *parser, const char *js, 36 | size_t len, jsmntok_t *tokens, size_t num_tokens) { 37 | jsmntok_t *token; 38 | int start; 39 | 40 | start = parser->pos; 41 | 42 | for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) { 43 | switch (js[parser->pos]) { 44 | #ifndef JSMN_STRICT 45 | /* In strict mode primitive must be followed by "," or "}" or "]" */ 46 | case ':': 47 | #endif 48 | case '\t' : case '\r' : case '\n' : case ' ' : 49 | case ',' : case ']' : case '}' : 50 | goto found; 51 | } 52 | if (js[parser->pos] < 32 || js[parser->pos] >= 127) { 53 | parser->pos = start; 54 | return JSMN_ERROR_INVAL; 55 | } 56 | } 57 | #ifdef JSMN_STRICT 58 | /* In strict mode primitive must be followed by a comma/object/array */ 59 | parser->pos = start; 60 | return JSMN_ERROR_PART; 61 | #endif 62 | 63 | found: 64 | if (tokens == NULL) { 65 | parser->pos--; 66 | return 0; 67 | } 68 | token = jsmn_alloc_token(parser, tokens, num_tokens); 69 | if (token == NULL) { 70 | parser->pos = start; 71 | return JSMN_ERROR_NOMEM; 72 | } 73 | jsmn_fill_token(token, JSMN_PRIMITIVE, start, parser->pos); 74 | #ifdef JSMN_PARENT_LINKS 75 | token->parent = parser->toksuper; 76 | #endif 77 | parser->pos--; 78 | return 0; 79 | } 80 | 81 | /** 82 | * Fills next token with JSON string. 83 | */ 84 | static int jsmn_parse_string(jsmn_parser *parser, const char *js, 85 | size_t len, jsmntok_t *tokens, size_t num_tokens) { 86 | jsmntok_t *token; 87 | 88 | int start = parser->pos; 89 | 90 | parser->pos++; 91 | 92 | /* Skip starting quote */ 93 | for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) { 94 | char c = js[parser->pos]; 95 | 96 | /* Quote: end of string */ 97 | if (c == '\"') { 98 | if (tokens == NULL) { 99 | return 0; 100 | } 101 | token = jsmn_alloc_token(parser, tokens, num_tokens); 102 | if (token == NULL) { 103 | parser->pos = start; 104 | return JSMN_ERROR_NOMEM; 105 | } 106 | jsmn_fill_token(token, JSMN_STRING, start+1, parser->pos); 107 | #ifdef JSMN_PARENT_LINKS 108 | token->parent = parser->toksuper; 109 | #endif 110 | return 0; 111 | } 112 | 113 | /* Backslash: Quoted symbol expected */ 114 | if (c == '\\' && parser->pos + 1 < len) { 115 | int i; 116 | parser->pos++; 117 | switch (js[parser->pos]) { 118 | /* Allowed escaped symbols */ 119 | case '\"': case '/' : case '\\' : case 'b' : 120 | case 'f' : case 'r' : case 'n' : case 't' : 121 | break; 122 | /* Allows escaped symbol \uXXXX */ 123 | case 'u': 124 | parser->pos++; 125 | for(i = 0; i < 4 && parser->pos < len && js[parser->pos] != '\0'; i++) { 126 | /* If it isn't a hex character we have an error */ 127 | if(!((js[parser->pos] >= 48 && js[parser->pos] <= 57) || /* 0-9 */ 128 | (js[parser->pos] >= 65 && js[parser->pos] <= 70) || /* A-F */ 129 | (js[parser->pos] >= 97 && js[parser->pos] <= 102))) { /* a-f */ 130 | parser->pos = start; 131 | return JSMN_ERROR_INVAL; 132 | } 133 | parser->pos++; 134 | } 135 | parser->pos--; 136 | break; 137 | /* Unexpected symbol */ 138 | default: 139 | parser->pos = start; 140 | return JSMN_ERROR_INVAL; 141 | } 142 | } 143 | } 144 | parser->pos = start; 145 | return JSMN_ERROR_PART; 146 | } 147 | 148 | /** 149 | * Parse JSON string and fill tokens. 150 | */ 151 | int jsmn_parse(jsmn_parser *parser, const char *js, size_t len, 152 | jsmntok_t *tokens, unsigned int num_tokens) { 153 | int r; 154 | int i; 155 | jsmntok_t *token; 156 | int count = parser->toknext; 157 | 158 | for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) { 159 | char c; 160 | jsmntype_t type; 161 | 162 | c = js[parser->pos]; 163 | switch (c) { 164 | case '{': case '[': 165 | count++; 166 | if (tokens == NULL) { 167 | break; 168 | } 169 | token = jsmn_alloc_token(parser, tokens, num_tokens); 170 | if (token == NULL) 171 | return JSMN_ERROR_NOMEM; 172 | if (parser->toksuper != -1) { 173 | tokens[parser->toksuper].size++; 174 | #ifdef JSMN_PARENT_LINKS 175 | token->parent = parser->toksuper; 176 | #endif 177 | } 178 | token->type = (c == '{' ? JSMN_OBJECT : JSMN_ARRAY); 179 | token->start = parser->pos; 180 | parser->toksuper = parser->toknext - 1; 181 | break; 182 | case '}': case ']': 183 | if (tokens == NULL) 184 | break; 185 | type = (c == '}' ? JSMN_OBJECT : JSMN_ARRAY); 186 | #ifdef JSMN_PARENT_LINKS 187 | if (parser->toknext < 1) { 188 | return JSMN_ERROR_INVAL; 189 | } 190 | token = &tokens[parser->toknext - 1]; 191 | for (;;) { 192 | if (token->start != -1 && token->end == -1) { 193 | if (token->type != type) { 194 | return JSMN_ERROR_INVAL; 195 | } 196 | token->end = parser->pos + 1; 197 | parser->toksuper = token->parent; 198 | break; 199 | } 200 | if (token->parent == -1) { 201 | if(token->type != type || parser->toksuper == -1) { 202 | return JSMN_ERROR_INVAL; 203 | } 204 | break; 205 | } 206 | token = &tokens[token->parent]; 207 | } 208 | #else 209 | for (i = parser->toknext - 1; i >= 0; i--) { 210 | token = &tokens[i]; 211 | if (token->start != -1 && token->end == -1) { 212 | if (token->type != type) { 213 | return JSMN_ERROR_INVAL; 214 | } 215 | parser->toksuper = -1; 216 | token->end = parser->pos + 1; 217 | break; 218 | } 219 | } 220 | /* Error if unmatched closing bracket */ 221 | if (i == -1) return JSMN_ERROR_INVAL; 222 | for (; i >= 0; i--) { 223 | token = &tokens[i]; 224 | if (token->start != -1 && token->end == -1) { 225 | parser->toksuper = i; 226 | break; 227 | } 228 | } 229 | #endif 230 | break; 231 | case '\"': 232 | r = jsmn_parse_string(parser, js, len, tokens, num_tokens); 233 | if (r < 0) return r; 234 | count++; 235 | if (parser->toksuper != -1 && tokens != NULL) 236 | tokens[parser->toksuper].size++; 237 | break; 238 | case '\t' : case '\r' : case '\n' : case ' ': 239 | break; 240 | case ':': 241 | parser->toksuper = parser->toknext - 1; 242 | break; 243 | case ',': 244 | if (tokens != NULL && parser->toksuper != -1 && 245 | tokens[parser->toksuper].type != JSMN_ARRAY && 246 | tokens[parser->toksuper].type != JSMN_OBJECT) { 247 | #ifdef JSMN_PARENT_LINKS 248 | parser->toksuper = tokens[parser->toksuper].parent; 249 | #else 250 | for (i = parser->toknext - 1; i >= 0; i--) { 251 | if (tokens[i].type == JSMN_ARRAY || tokens[i].type == JSMN_OBJECT) { 252 | if (tokens[i].start != -1 && tokens[i].end == -1) { 253 | parser->toksuper = i; 254 | break; 255 | } 256 | } 257 | } 258 | #endif 259 | } 260 | break; 261 | #ifdef JSMN_STRICT 262 | /* In strict mode primitives are: numbers and booleans */ 263 | case '-': case '0': case '1' : case '2': case '3' : case '4': 264 | case '5': case '6': case '7' : case '8': case '9': 265 | case 't': case 'f': case 'n' : 266 | /* And they must not be keys of the object */ 267 | if (tokens != NULL && parser->toksuper != -1) { 268 | jsmntok_t *t = &tokens[parser->toksuper]; 269 | if (t->type == JSMN_OBJECT || 270 | (t->type == JSMN_STRING && t->size != 0)) { 271 | return JSMN_ERROR_INVAL; 272 | } 273 | } 274 | #else 275 | /* In non-strict mode every unquoted value is a primitive */ 276 | default: 277 | #endif 278 | r = jsmn_parse_primitive(parser, js, len, tokens, num_tokens); 279 | if (r < 0) return r; 280 | count++; 281 | if (parser->toksuper != -1 && tokens != NULL) 282 | tokens[parser->toksuper].size++; 283 | break; 284 | 285 | #ifdef JSMN_STRICT 286 | /* Unexpected char in strict mode */ 287 | default: 288 | return JSMN_ERROR_INVAL; 289 | #endif 290 | } 291 | } 292 | 293 | if (tokens != NULL) { 294 | for (i = parser->toknext - 1; i >= 0; i--) { 295 | /* Unmatched opened object or array */ 296 | if (tokens[i].start != -1 && tokens[i].end == -1) { 297 | return JSMN_ERROR_PART; 298 | } 299 | } 300 | } 301 | 302 | return count; 303 | } 304 | 305 | /** 306 | * Creates a new parser based over a given buffer with an array of tokens 307 | * available. 308 | */ 309 | void jsmn_init(jsmn_parser *parser) { 310 | parser->pos = 0; 311 | parser->toknext = 0; 312 | parser->toksuper = -1; 313 | } 314 | 315 | -------------------------------------------------------------------------------- /UartSecureDFU/jsmn.h: -------------------------------------------------------------------------------- 1 | #ifndef __JSMN_H_ 2 | #define __JSMN_H_ 3 | 4 | #include 5 | 6 | #ifdef __cplusplus 7 | extern "C" { 8 | #endif 9 | 10 | /** 11 | * JSON type identifier. Basic types are: 12 | * o Object 13 | * o Array 14 | * o String 15 | * o Other primitive: number, boolean (true/false) or null 16 | */ 17 | typedef enum { 18 | JSMN_UNDEFINED = 0, 19 | JSMN_OBJECT = 1, 20 | JSMN_ARRAY = 2, 21 | JSMN_STRING = 3, 22 | JSMN_PRIMITIVE = 4 23 | } jsmntype_t; 24 | 25 | enum jsmnerr { 26 | /* Not enough tokens were provided */ 27 | JSMN_ERROR_NOMEM = -1, 28 | /* Invalid character inside JSON string */ 29 | JSMN_ERROR_INVAL = -2, 30 | /* The string is not a full JSON packet, more bytes expected */ 31 | JSMN_ERROR_PART = -3 32 | }; 33 | 34 | /** 35 | * JSON token description. 36 | * type type (object, array, string etc.) 37 | * start start position in JSON data string 38 | * end end position in JSON data string 39 | */ 40 | typedef struct { 41 | jsmntype_t type; 42 | int start; 43 | int end; 44 | int size; 45 | #ifdef JSMN_PARENT_LINKS 46 | int parent; 47 | #endif 48 | } jsmntok_t; 49 | 50 | /** 51 | * JSON parser. Contains an array of token blocks available. Also stores 52 | * the string being parsed now and current position in that string 53 | */ 54 | typedef struct { 55 | unsigned int pos; /* offset in the JSON string */ 56 | unsigned int toknext; /* next token to allocate */ 57 | int toksuper; /* superior token node, e.g parent object or array */ 58 | } jsmn_parser; 59 | 60 | /** 61 | * Create JSON parser over an array of tokens 62 | */ 63 | void jsmn_init(jsmn_parser *parser); 64 | 65 | /** 66 | * Run JSON parser. It parses a JSON data string into and array of tokens, each describing 67 | * a single JSON object. 68 | */ 69 | int jsmn_parse(jsmn_parser *parser, const char *js, size_t len, 70 | jsmntok_t *tokens, unsigned int num_tokens); 71 | 72 | #ifdef __cplusplus 73 | } 74 | #endif 75 | 76 | #endif /* __JSMN_H_ */ 77 | -------------------------------------------------------------------------------- /UartSecureDFU/logging.c: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2018, Nordic Semiconductor ASA 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without modification, 7 | * are permitted provided that the following conditions are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright notice, this 10 | * list of conditions and the following disclaimer. 11 | * 12 | * 2. Redistributions in binary form, except as embedded into a Nordic 13 | * Semiconductor ASA integrated circuit in a product or a software update for 14 | * such product, must reproduce the above copyright notice, this list of 15 | * conditions and the following disclaimer in the documentation and/or other 16 | * materials provided with the distribution. 17 | * 18 | * 3. Neither the name of Nordic Semiconductor ASA nor the names of its 19 | * contributors may be used to endorse or promote products derived from this 20 | * software without specific prior written permission. 21 | * 22 | * 4. This software, with or without modification, must only be used with a 23 | * Nordic Semiconductor ASA integrated circuit. 24 | * 25 | * 5. Any software provided in binary form under this license must not be reverse 26 | * engineered, decompiled, modified and/or disassembled. 27 | * 28 | * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS 29 | * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 30 | * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE 31 | * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE 32 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 33 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 34 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 37 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 38 | * 39 | */ 40 | 41 | #include 42 | #include 43 | #include "logging.h" 44 | 45 | int m_level = LOGGER_INFO_LVL_0; 46 | 47 | void logger_error(const char* format, ...) 48 | { 49 | va_list argptr; 50 | va_start(argptr, format); 51 | vfprintf(stderr, format, argptr); 52 | va_end(argptr); 53 | putchar('\n'); 54 | } 55 | 56 | void logger_info(const char* format, va_list arg_list) 57 | { 58 | vfprintf(stdout, format, arg_list); 59 | putchar('\n'); 60 | } 61 | 62 | void logger_info_1(const char* format, ...) 63 | { 64 | if (m_level >= LOGGER_INFO_LVL_1) 65 | { 66 | va_list argptr; 67 | va_start(argptr, format); 68 | logger_info(format, argptr); 69 | va_end(argptr); 70 | } 71 | } 72 | 73 | void logger_info_2(const char* format, ...) 74 | { 75 | if (m_level >= LOGGER_INFO_LVL_2) 76 | { 77 | va_list argptr; 78 | va_start(argptr, format); 79 | logger_info(format, argptr); 80 | va_end(argptr); 81 | } 82 | } 83 | 84 | void logger_info_3(const char* format, ...) 85 | { 86 | if (m_level >= LOGGER_INFO_LVL_3) 87 | { 88 | va_list argptr; 89 | va_start(argptr, format); 90 | logger_info(format, argptr); 91 | va_end(argptr); 92 | } 93 | } 94 | 95 | void logger_set_info_level(int level) 96 | { 97 | m_level = level; 98 | } 99 | 100 | int logger_get_info_level(void) 101 | { 102 | return m_level; 103 | } 104 | -------------------------------------------------------------------------------- /UartSecureDFU/logging.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2018, Nordic Semiconductor ASA 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without modification, 7 | * are permitted provided that the following conditions are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright notice, this 10 | * list of conditions and the following disclaimer. 11 | * 12 | * 2. Redistributions in binary form, except as embedded into a Nordic 13 | * Semiconductor ASA integrated circuit in a product or a software update for 14 | * such product, must reproduce the above copyright notice, this list of 15 | * conditions and the following disclaimer in the documentation and/or other 16 | * materials provided with the distribution. 17 | * 18 | * 3. Neither the name of Nordic Semiconductor ASA nor the names of its 19 | * contributors may be used to endorse or promote products derived from this 20 | * software without specific prior written permission. 21 | * 22 | * 4. This software, with or without modification, must only be used with a 23 | * Nordic Semiconductor ASA integrated circuit. 24 | * 25 | * 5. Any software provided in binary form under this license must not be reverse 26 | * engineered, decompiled, modified and/or disassembled. 27 | * 28 | * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS 29 | * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 30 | * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE 31 | * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE 32 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 33 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 34 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 37 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 38 | * 39 | */ 40 | 41 | #pragma once 42 | 43 | #ifndef _INC_LOGGING 44 | #define _INC_LOGGING 45 | 46 | 47 | #ifdef __cplusplus 48 | extern "C" { 49 | #endif /* __cplusplus */ 50 | 51 | 52 | #define LOGGER_INFO_LVL_0 0 53 | #define LOGGER_INFO_LVL_1 1 54 | #define LOGGER_INFO_LVL_2 2 55 | #define LOGGER_INFO_LVL_3 3 56 | 57 | 58 | void logger_error(const char* format, ...); 59 | 60 | void logger_info_1(const char* format, ...); 61 | void logger_info_2(const char* format, ...); 62 | void logger_info_3(const char* format, ...); 63 | 64 | void logger_set_info_level(int level); 65 | int logger_get_info_level(void); 66 | 67 | 68 | #ifdef __cplusplus 69 | } /* ... extern "C" */ 70 | #endif /* __cplusplus */ 71 | 72 | 73 | #endif // _INC_LOGGING 74 | -------------------------------------------------------------------------------- /UartSecureDFU/slip_enc.c: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017, Nordic Semiconductor ASA 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without modification, 7 | * are permitted provided that the following conditions are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright notice, this 10 | * list of conditions and the following disclaimer. 11 | * 12 | * 2. Redistributions in binary form, except as embedded into a Nordic 13 | * Semiconductor ASA integrated circuit in a product or a software update for 14 | * such product, must reproduce the above copyright notice, this list of 15 | * conditions and the following disclaimer in the documentation and/or other 16 | * materials provided with the distribution. 17 | * 18 | * 3. Neither the name of Nordic Semiconductor ASA nor the names of its 19 | * contributors may be used to endorse or promote products derived from this 20 | * software without specific prior written permission. 21 | * 22 | * 4. This software, with or without modification, must only be used with a 23 | * Nordic Semiconductor ASA integrated circuit. 24 | * 25 | * 5. Any software provided in binary form under this license must not be reverse 26 | * engineered, decompiled, modified and/or disassembled. 27 | * 28 | * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS 29 | * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 30 | * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE 31 | * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE 32 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 33 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 34 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 37 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 38 | * 39 | */ 40 | 41 | #include 42 | #include "slip_enc.h" 43 | 44 | #define SLIP_END 0300 45 | #define SLIP_ESC 0333 46 | #define SLIP_ESC_END 0334 47 | #define SLIP_ESC_ESC 0335 48 | 49 | void encode_slip(uint8_t *pDestData, uint32_t *pDestSize, const uint8_t *pSrcData, uint32_t nSrcSize) 50 | { 51 | uint32_t n, nDestSize; 52 | 53 | nDestSize = 0; 54 | 55 | for (n = 0; n < nSrcSize; n++) 56 | { 57 | uint8_t nSrcByte = *(pSrcData + n); 58 | 59 | if (nSrcByte == SLIP_END) 60 | { 61 | *pDestData++ = SLIP_ESC; 62 | *pDestData++ = SLIP_ESC_END; 63 | nDestSize += 2; 64 | } 65 | else if (nSrcByte == SLIP_ESC) 66 | { 67 | *pDestData++ = SLIP_ESC; 68 | *pDestData++ = SLIP_ESC_ESC; 69 | nDestSize += 2; 70 | } 71 | else 72 | { 73 | *pDestData++ = nSrcByte; 74 | nDestSize++; 75 | } 76 | } 77 | 78 | *pDestData = SLIP_END; 79 | nDestSize++; 80 | 81 | *pDestSize = nDestSize; 82 | } 83 | 84 | int decode_slip(uint8_t *pDestData, uint32_t *pDestSize, const uint8_t *pSrcData, uint32_t nSrcSize) 85 | { 86 | int err_code = 1; 87 | uint32_t n, nDestSize = 0; 88 | bool is_escaped = false; 89 | 90 | for (n = 0; n < nSrcSize; n++) 91 | { 92 | uint8_t nSrcByte = *(pSrcData + n); 93 | 94 | if (nSrcByte == SLIP_END) 95 | { 96 | if (!is_escaped) 97 | err_code = 0; // Done. OK 98 | 99 | break; 100 | } 101 | else if (nSrcByte == SLIP_ESC) 102 | { 103 | if (is_escaped) 104 | { 105 | // should not get SLIP_ESC twice... 106 | err_code = 1; 107 | break; 108 | } 109 | else 110 | is_escaped = true; 111 | } 112 | else if (nSrcByte == SLIP_ESC_END) 113 | { 114 | if (is_escaped) 115 | { 116 | is_escaped = false; 117 | 118 | *pDestData++ = SLIP_END; 119 | } 120 | else 121 | *pDestData++ = nSrcByte; 122 | 123 | nDestSize++; 124 | } 125 | else if (nSrcByte == SLIP_ESC_ESC) 126 | { 127 | if (is_escaped) 128 | { 129 | is_escaped = false; 130 | 131 | *pDestData++ = SLIP_ESC; 132 | } 133 | else 134 | *pDestData++ = nSrcByte; 135 | 136 | nDestSize++; 137 | } 138 | else 139 | { 140 | *pDestData++ = nSrcByte; 141 | nDestSize++; 142 | } 143 | } 144 | 145 | *pDestSize = nDestSize; 146 | 147 | return err_code; 148 | } 149 | -------------------------------------------------------------------------------- /UartSecureDFU/slip_enc.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017, Nordic Semiconductor ASA 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without modification, 7 | * are permitted provided that the following conditions are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright notice, this 10 | * list of conditions and the following disclaimer. 11 | * 12 | * 2. Redistributions in binary form, except as embedded into a Nordic 13 | * Semiconductor ASA integrated circuit in a product or a software update for 14 | * such product, must reproduce the above copyright notice, this list of 15 | * conditions and the following disclaimer in the documentation and/or other 16 | * materials provided with the distribution. 17 | * 18 | * 3. Neither the name of Nordic Semiconductor ASA nor the names of its 19 | * contributors may be used to endorse or promote products derived from this 20 | * software without specific prior written permission. 21 | * 22 | * 4. This software, with or without modification, must only be used with a 23 | * Nordic Semiconductor ASA integrated circuit. 24 | * 25 | * 5. Any software provided in binary form under this license must not be reverse 26 | * engineered, decompiled, modified and/or disassembled. 27 | * 28 | * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS 29 | * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 30 | * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE 31 | * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE 32 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 33 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 34 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 37 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 38 | * 39 | */ 40 | 41 | #pragma once 42 | 43 | #ifndef _INC_SLIP_ENC 44 | #define _INC_SLIP_ENC 45 | 46 | #include 47 | 48 | 49 | #ifdef __cplusplus 50 | extern "C" { 51 | #endif /* __cplusplus */ 52 | 53 | 54 | void encode_slip(uint8_t *pDestData, uint32_t *pDestSize, const uint8_t *pSrcData, uint32_t nSrcSize); 55 | 56 | int decode_slip(uint8_t *pDestData, uint32_t *pDestSize, const uint8_t *pSrcData, uint32_t nSrcSize); 57 | 58 | 59 | #ifdef __cplusplus 60 | } /* ... extern "C" */ 61 | #endif /* __cplusplus */ 62 | 63 | 64 | #endif // _INC_SLIP_ENC 65 | -------------------------------------------------------------------------------- /UartSecureDFU/uart_drv.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2018, Nordic Semiconductor ASA 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without modification, 7 | * are permitted provided that the following conditions are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright notice, this 10 | * list of conditions and the following disclaimer. 11 | * 12 | * 2. Redistributions in binary form, except as embedded into a Nordic 13 | * Semiconductor ASA integrated circuit in a product or a software update for 14 | * such product, must reproduce the above copyright notice, this list of 15 | * conditions and the following disclaimer in the documentation and/or other 16 | * materials provided with the distribution. 17 | * 18 | * 3. Neither the name of Nordic Semiconductor ASA nor the names of its 19 | * contributors may be used to endorse or promote products derived from this 20 | * software without specific prior written permission. 21 | * 22 | * 4. This software, with or without modification, must only be used with a 23 | * Nordic Semiconductor ASA integrated circuit. 24 | * 25 | * 5. Any software provided in binary form under this license must not be reverse 26 | * engineered, decompiled, modified and/or disassembled. 27 | * 28 | * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS 29 | * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 30 | * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE 31 | * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE 32 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 33 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 34 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 37 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 38 | * 39 | */ 40 | 41 | #pragma once 42 | 43 | #ifndef _INC_UART_DRV 44 | #define _INC_UART_DRV 45 | 46 | #include 47 | #ifdef WIN32 48 | #include 49 | #endif 50 | 51 | #ifdef __cplusplus 52 | extern "C" { 53 | #endif /* __cplusplus */ 54 | 55 | 56 | typedef struct { 57 | const char *p_PortName; 58 | 59 | #ifdef WIN32 60 | HANDLE portHandle; 61 | #else 62 | int tty_fd; 63 | #endif 64 | } uart_drv_t; 65 | 66 | 67 | int uart_drv_open(uart_drv_t *p_uart); 68 | 69 | int uart_drv_close(uart_drv_t *p_uart); 70 | 71 | int uart_drv_send(uart_drv_t *p_uart, const uint8_t *pData, uint32_t nSize); 72 | 73 | int uart_drv_receive(uart_drv_t *p_uart, uint8_t *pData, uint32_t nSize, uint32_t *pSize); 74 | 75 | 76 | #ifdef __cplusplus 77 | } /* ... extern "C" */ 78 | #endif /* __cplusplus */ 79 | 80 | 81 | #endif // _INC_UART_DRV 82 | -------------------------------------------------------------------------------- /UartSecureDFU/uart_linux.c: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2018, Nordic Semiconductor ASA 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without modification, 7 | * are permitted provided that the following conditions are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright notice, this 10 | * list of conditions and the following disclaimer. 11 | * 12 | * 2. Redistributions in binary form, except as embedded into a Nordic 13 | * Semiconductor ASA integrated circuit in a product or a software update for 14 | * such product, must reproduce the above copyright notice, this list of 15 | * conditions and the following disclaimer in the documentation and/or other 16 | * materials provided with the distribution. 17 | * 18 | * 3. Neither the name of Nordic Semiconductor ASA nor the names of its 19 | * contributors may be used to endorse or promote products derived from this 20 | * software without specific prior written permission. 21 | * 22 | * 4. This software, with or without modification, must only be used with a 23 | * Nordic Semiconductor ASA integrated circuit. 24 | * 25 | * 5. Any software provided in binary form under this license must not be reverse 26 | * engineered, decompiled, modified and/or disassembled. 27 | * 28 | * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS 29 | * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 30 | * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE 31 | * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE 32 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 33 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 34 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 37 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 38 | * 39 | */ 40 | 41 | #include 42 | #include 43 | #include 44 | #include 45 | #include "uart_drv.h" 46 | #include "logging.h" 47 | 48 | int uart_drv_open(uart_drv_t *p_uart) 49 | { 50 | int err_code = 0; 51 | int fd = -1; 52 | const char *tty_name = p_uart->p_PortName; 53 | char tty_path[20]; 54 | struct termios options; 55 | 56 | strcpy(tty_path, "/dev/"); 57 | if (strlen(tty_name) <= 14) 58 | strcat(tty_path, tty_name); 59 | else 60 | { 61 | logger_error("Invalid TTY port!"); 62 | 63 | err_code = 1; 64 | } 65 | 66 | if (!err_code) 67 | { 68 | fd = open(tty_path, O_RDWR | O_NOCTTY); 69 | 70 | if (fd < 0) 71 | { 72 | logger_error("Cannot open TTY port!"); 73 | 74 | err_code = 1; 75 | } 76 | } 77 | 78 | if (!err_code) 79 | { 80 | // clear all flags 81 | memset(&options, 0, sizeof(options)); 82 | 83 | // 115200bps 84 | cfsetispeed(&options, B115200); 85 | cfsetospeed(&options, B115200); 86 | // 8N1 87 | options.c_cflag &= ~PARENB; 88 | options.c_cflag &= ~CSTOPB; 89 | options.c_cflag &= ~CSIZE; 90 | options.c_cflag |= CS8; 91 | // ignore DCD line 92 | options.c_cflag |= (CLOCAL | CREAD); 93 | // enable RTS/CTS handshake 94 | options.c_cflag |= CRTSCTS; 95 | // disabe XON/XOFF handshake 96 | options.c_iflag &= ~(IXON | IXOFF | IXANY); 97 | // disabe input mapping options 98 | options.c_iflag &= ~(ISTRIP | INLCR | IGNCR | ICRNL | IUCLC); 99 | // select RAW input 100 | options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); 101 | // select RAW output 102 | options.c_oflag &= ~OPOST; 103 | // disabe output mapping options 104 | options.c_oflag &= ~(OLCUC | ONLCR | OCRNL | ONOCR | ONLRET); 105 | // set read timeout 106 | options.c_cc[VMIN] = 0; 107 | options.c_cc[VTIME] = 5; // 0.5 seconds 108 | 109 | if (tcsetattr(fd, TCSANOW, &options)) 110 | { 111 | logger_error("Cannot set TTY options!"); 112 | 113 | err_code = 1; 114 | } 115 | } 116 | 117 | if (!err_code) 118 | { 119 | if (tcflush(fd, TCIFLUSH)) 120 | { 121 | logger_error("Cannot flush TTY RX buffer!"); 122 | 123 | err_code = 1; 124 | } 125 | } 126 | 127 | if (err_code && fd >= 0) 128 | { 129 | p_uart->tty_fd = fd; 130 | uart_drv_close(p_uart); 131 | 132 | fd = -1; 133 | } 134 | 135 | p_uart->tty_fd = fd; 136 | 137 | return err_code; 138 | } 139 | 140 | int uart_drv_close(uart_drv_t *p_uart) 141 | { 142 | int err_code = 0; 143 | int fd = p_uart->tty_fd; 144 | 145 | if (fd >= 0) 146 | { 147 | if (close(fd)) 148 | { 149 | logger_error("Cannot close TTY port!"); 150 | 151 | err_code = 1; 152 | } 153 | } 154 | else 155 | err_code = 1; 156 | 157 | return err_code; 158 | } 159 | 160 | int uart_drv_send(uart_drv_t *p_uart, const uint8_t *pData, uint32_t nSize) 161 | { 162 | int err_code = 0; 163 | int32_t length; 164 | 165 | length = write(p_uart->tty_fd, pData, nSize); 166 | if (length != nSize) 167 | { 168 | logger_error("Cannot write TTY port!"); 169 | 170 | err_code = 1; 171 | } 172 | else 173 | { 174 | if (tcdrain(p_uart->tty_fd)) 175 | { 176 | logger_error("Cannot drain TTY TX buffer!"); 177 | 178 | err_code = 1; 179 | } 180 | } 181 | 182 | return err_code; 183 | } 184 | 185 | int uart_drv_receive(uart_drv_t *p_uart, uint8_t *pData, uint32_t nSize, uint32_t *pSize) 186 | { 187 | int err_code = 0; 188 | int32_t length; 189 | 190 | length = read(p_uart->tty_fd, pData, nSize); 191 | if (length < 0) 192 | { 193 | logger_error("Cannot read TTY port!"); 194 | 195 | err_code = 1; 196 | } 197 | else 198 | *pSize = length; 199 | 200 | return err_code; 201 | } 202 | -------------------------------------------------------------------------------- /UartSecureDFU/uart_slip.c: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017, Nordic Semiconductor ASA 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without modification, 7 | * are permitted provided that the following conditions are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright notice, this 10 | * list of conditions and the following disclaimer. 11 | * 12 | * 2. Redistributions in binary form, except as embedded into a Nordic 13 | * Semiconductor ASA integrated circuit in a product or a software update for 14 | * such product, must reproduce the above copyright notice, this list of 15 | * conditions and the following disclaimer in the documentation and/or other 16 | * materials provided with the distribution. 17 | * 18 | * 3. Neither the name of Nordic Semiconductor ASA nor the names of its 19 | * contributors may be used to endorse or promote products derived from this 20 | * software without specific prior written permission. 21 | * 22 | * 4. This software, with or without modification, must only be used with a 23 | * Nordic Semiconductor ASA integrated circuit. 24 | * 25 | * 5. Any software provided in binary form under this license must not be reverse 26 | * engineered, decompiled, modified and/or disassembled. 27 | * 28 | * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS 29 | * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 30 | * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE 31 | * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE 32 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 33 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 34 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 37 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 38 | * 39 | */ 40 | 41 | #include 42 | #include "uart_slip.h" 43 | #include "slip_enc.h" 44 | #include "logging.h" 45 | 46 | #define UART_SLIP_BUFF_SIZE (UART_SLIP_SIZE_MAX * 2 + 1) 47 | 48 | static uint8_t uart_slip_buff[UART_SLIP_BUFF_SIZE]; 49 | 50 | int uart_slip_open(uart_drv_t *p_uart) 51 | { 52 | return uart_drv_open(p_uart); 53 | } 54 | 55 | int uart_slip_close(uart_drv_t *p_uart) 56 | { 57 | return uart_drv_close(p_uart); 58 | } 59 | 60 | int uart_slip_send(uart_drv_t *p_uart, const uint8_t *pData, uint32_t nSize) 61 | { 62 | int err_code = 0; 63 | uint32_t nSlipSize; 64 | 65 | if (nSize > UART_SLIP_SIZE_MAX) 66 | { 67 | logger_error("Cannot encode SLIP!"); 68 | 69 | err_code = 1; 70 | } 71 | else 72 | { 73 | encode_slip(uart_slip_buff, &nSlipSize, pData, nSize); 74 | 75 | err_code = uart_drv_send(p_uart, uart_slip_buff, nSlipSize); 76 | } 77 | 78 | return err_code; 79 | } 80 | 81 | int uart_slip_receive(uart_drv_t *p_uart, uint8_t *pData, uint32_t nSize, uint32_t *pSize) 82 | { 83 | int err_code = 0; 84 | uint32_t sizeBuffer; 85 | uint32_t length, slip_len = 0; 86 | 87 | do 88 | { 89 | sizeBuffer = sizeof(uart_slip_buff) - slip_len; 90 | if (!sizeBuffer) 91 | { 92 | logger_error("UART buffer overflow!"); 93 | 94 | err_code = 1; 95 | 96 | break; 97 | } 98 | 99 | length = 0; 100 | err_code = uart_drv_receive(p_uart, uart_slip_buff + slip_len, sizeBuffer, &length); 101 | if (err_code) 102 | break; 103 | 104 | if (!length) 105 | { 106 | logger_error("Read no data from UART!"); 107 | 108 | err_code = 1; 109 | 110 | break; 111 | } 112 | 113 | slip_len += length; 114 | 115 | if (!decode_slip(pData, pSize, uart_slip_buff, slip_len)) 116 | { 117 | break; 118 | } 119 | } while (!err_code); 120 | 121 | return err_code; 122 | } 123 | -------------------------------------------------------------------------------- /UartSecureDFU/uart_slip.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017, Nordic Semiconductor ASA 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without modification, 7 | * are permitted provided that the following conditions are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright notice, this 10 | * list of conditions and the following disclaimer. 11 | * 12 | * 2. Redistributions in binary form, except as embedded into a Nordic 13 | * Semiconductor ASA integrated circuit in a product or a software update for 14 | * such product, must reproduce the above copyright notice, this list of 15 | * conditions and the following disclaimer in the documentation and/or other 16 | * materials provided with the distribution. 17 | * 18 | * 3. Neither the name of Nordic Semiconductor ASA nor the names of its 19 | * contributors may be used to endorse or promote products derived from this 20 | * software without specific prior written permission. 21 | * 22 | * 4. This software, with or without modification, must only be used with a 23 | * Nordic Semiconductor ASA integrated circuit. 24 | * 25 | * 5. Any software provided in binary form under this license must not be reverse 26 | * engineered, decompiled, modified and/or disassembled. 27 | * 28 | * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS 29 | * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 30 | * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE 31 | * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE 32 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 33 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 34 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 37 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 38 | * 39 | */ 40 | 41 | #pragma once 42 | 43 | #ifndef _INC_UART_SLIP 44 | #define _INC_UART_SLIP 45 | 46 | #include 47 | #include "uart_drv.h" 48 | 49 | 50 | #ifdef __cplusplus 51 | extern "C" { 52 | #endif /* __cplusplus */ 53 | 54 | 55 | #define UART_SLIP_SIZE_MAX 128 56 | 57 | 58 | int uart_slip_open(uart_drv_t *p_uart); 59 | 60 | int uart_slip_close(uart_drv_t *p_uart); 61 | 62 | int uart_slip_send(uart_drv_t *p_uart, const uint8_t *pData, uint32_t nSize); 63 | 64 | int uart_slip_receive(uart_drv_t *p_uart, uint8_t *pData, uint32_t nSize, uint32_t *pSize); 65 | 66 | 67 | #ifdef __cplusplus 68 | } /* ... extern "C" */ 69 | #endif /* __cplusplus */ 70 | 71 | 72 | #endif // _INC_UART_SLIP 73 | -------------------------------------------------------------------------------- /UartSecureDFU/uart_win32.c: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2018, Nordic Semiconductor ASA 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without modification, 7 | * are permitted provided that the following conditions are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright notice, this 10 | * list of conditions and the following disclaimer. 11 | * 12 | * 2. Redistributions in binary form, except as embedded into a Nordic 13 | * Semiconductor ASA integrated circuit in a product or a software update for 14 | * such product, must reproduce the above copyright notice, this list of 15 | * conditions and the following disclaimer in the documentation and/or other 16 | * materials provided with the distribution. 17 | * 18 | * 3. Neither the name of Nordic Semiconductor ASA nor the names of its 19 | * contributors may be used to endorse or promote products derived from this 20 | * software without specific prior written permission. 21 | * 22 | * 4. This software, with or without modification, must only be used with a 23 | * Nordic Semiconductor ASA integrated circuit. 24 | * 25 | * 5. Any software provided in binary form under this license must not be reverse 26 | * engineered, decompiled, modified and/or disassembled. 27 | * 28 | * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS 29 | * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 30 | * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE 31 | * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE 32 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 33 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 34 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 37 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 38 | * 39 | */ 40 | 41 | #include 42 | #include "uart_drv.h" 43 | #include "logging.h" 44 | 45 | int uart_drv_open(uart_drv_t *p_uart) 46 | { 47 | int err_code = 0; 48 | const char *portName = p_uart->p_PortName; 49 | CHAR portFileName[14]; 50 | HANDLE handlePort_ = INVALID_HANDLE_VALUE; 51 | 52 | strcpy(portFileName, "\\\\.\\"); 53 | if (strlen(portName) <= 6) 54 | strcat(portFileName, portName); 55 | else 56 | { 57 | logger_error("Invalid COM port!"); 58 | 59 | err_code = 1; 60 | } 61 | 62 | if (!err_code) 63 | { 64 | handlePort_ = CreateFile(portFileName, // Specify port device: default "COM1" 65 | GENERIC_READ | GENERIC_WRITE, // Specify mode that open device. 66 | 0, // the devide isn't shared. 67 | NULL, // the object gets a default security. 68 | OPEN_EXISTING, // Specify which action to take on file. 69 | 0, // default. 70 | NULL); // default. 71 | 72 | if (handlePort_ == INVALID_HANDLE_VALUE) 73 | { 74 | logger_error("Cannot open COM port!"); 75 | 76 | err_code = 1; 77 | } 78 | } 79 | 80 | if (!err_code) 81 | { 82 | DCB config_; 83 | 84 | // Get current configuration of serial communication port. 85 | if (GetCommState(handlePort_, &config_) == FALSE) 86 | { 87 | logger_error("Cannot get COM configuration!"); 88 | 89 | err_code = 1; 90 | } 91 | 92 | if (!err_code) 93 | { 94 | config_.BaudRate = 115200; // Specify buad rate of communicaiton. 95 | config_.StopBits = 0; // Specify stopbit of communication. 96 | config_.Parity = 0; // Specify parity of communication. 97 | config_.ByteSize = 8; // Specify byte of size of communication. 98 | config_.fDtrControl = DTR_CONTROL_DISABLE; 99 | config_.fDsrSensitivity = 0; 100 | config_.fRtsControl = RTS_CONTROL_HANDSHAKE; 101 | config_.fInX = 0; 102 | config_.fOutX = 0; 103 | config_.fBinary = 1; 104 | if (SetCommState(handlePort_, &config_) == FALSE) 105 | { 106 | logger_error("Cannot set COM configuration!"); 107 | 108 | err_code = 1; 109 | } 110 | } 111 | } 112 | 113 | if (!err_code) 114 | { 115 | // instance an object of COMMTIMEOUTS. 116 | COMMTIMEOUTS comTimeOut; 117 | // Specify value is added to the product of the 118 | // ReadTotalTimeoutMultiplier member 119 | comTimeOut.ReadTotalTimeoutConstant = 500; 120 | // Specify value that is multiplied 121 | // by the requested number of bytes to be read. 122 | comTimeOut.ReadTotalTimeoutMultiplier = 10; 123 | // Specify time-out between charactor for receiving. 124 | comTimeOut.ReadIntervalTimeout = 100; 125 | // Specify value that is multiplied 126 | // by the requested number of bytes to be sent. 127 | comTimeOut.WriteTotalTimeoutMultiplier = 15; 128 | // Specify value is added to the product of the 129 | // WriteTotalTimeoutMultiplier member 130 | comTimeOut.WriteTotalTimeoutConstant = 300; 131 | // set the time-out parameter into device control. 132 | SetCommTimeouts(handlePort_, &comTimeOut); 133 | } 134 | 135 | if (!err_code) 136 | { 137 | if (PurgeComm(handlePort_, PURGE_RXCLEAR) == FALSE) 138 | { 139 | logger_error("Cannot purge COM RX buffer!"); 140 | 141 | err_code = 1; 142 | } 143 | } 144 | 145 | if (err_code && handlePort_ != INVALID_HANDLE_VALUE) 146 | { 147 | p_uart->portHandle = handlePort_; 148 | uart_drv_close(p_uart); 149 | 150 | handlePort_ = INVALID_HANDLE_VALUE; 151 | } 152 | 153 | p_uart->portHandle = handlePort_; 154 | 155 | return err_code; 156 | } 157 | 158 | int uart_drv_close(uart_drv_t *p_uart) 159 | { 160 | int err_code = 0; 161 | HANDLE portHandle = p_uart->portHandle; 162 | 163 | if (portHandle != INVALID_HANDLE_VALUE) 164 | { 165 | if (CloseHandle(portHandle) == FALSE) // Call this function to close port. 166 | { 167 | logger_error("Cannot close COM port!"); 168 | 169 | err_code = 1; 170 | } 171 | } 172 | else 173 | err_code = 1; 174 | 175 | return err_code; 176 | } 177 | 178 | int uart_drv_send(uart_drv_t *p_uart, const uint8_t *pData, uint32_t nSize) 179 | { 180 | int err_code = 0; 181 | HANDLE portHandle = p_uart->portHandle; 182 | DWORD length; 183 | 184 | if (WriteFile(portHandle, // handle to file to write to 185 | pData, // pointer to data to write to file 186 | nSize, // number of bytes to write 187 | &length, NULL) == FALSE || // pointer to number of bytes written 188 | length < nSize) 189 | { 190 | logger_error("Cannot write COM port!"); 191 | 192 | err_code = 1; 193 | } 194 | 195 | return err_code; 196 | } 197 | 198 | int uart_drv_receive(uart_drv_t *p_uart, uint8_t *pData, uint32_t nSize, uint32_t *pSize) 199 | { 200 | int err_code = 0; 201 | HANDLE portHandle = p_uart->portHandle; 202 | DWORD length = 0; 203 | 204 | if (ReadFile(portHandle, // handle of file to read 205 | pData, // pointer to data to read from file 206 | nSize, // number of bytes to read 207 | &length, // pointer to number of bytes read 208 | NULL) == FALSE) // pointer to structure for data 209 | { 210 | logger_error("Cannot read COM port!"); 211 | 212 | err_code = 1; 213 | } 214 | 215 | *pSize = length; 216 | 217 | return err_code; 218 | } 219 | -------------------------------------------------------------------------------- /UartSecureDFU/zip.h: -------------------------------------------------------------------------------- 1 | /* 2 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 3 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 4 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 5 | * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 6 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 7 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 8 | * OTHER DEALINGS IN THE SOFTWARE. 9 | */ 10 | 11 | #pragma once 12 | #ifndef ZIP_H 13 | #define ZIP_H 14 | 15 | #include 16 | 17 | #ifdef __cplusplus 18 | extern "C" { 19 | #endif 20 | 21 | #ifndef MAX_PATH 22 | #define MAX_PATH 32767 /* # chars in a path name including NULL */ 23 | #endif 24 | 25 | #define ZIP_DEFAULT_COMPRESSION_LEVEL 6 26 | 27 | /* 28 | This data structure is used throughout the library to represent zip archive 29 | - forward declaration. 30 | */ 31 | struct zip_t; 32 | 33 | /* 34 | Opens zip archive with compression level using the given mode. 35 | 36 | Args: 37 | zipname: zip archive file name. 38 | level: compression level (0-9 are the standard zlib-style levels). 39 | mode: file access mode. 40 | 'r': opens a file for reading/extracting (the file must exists). 41 | 'w': creates an empty file for writing. 42 | 'a': appends to an existing archive. 43 | 44 | Returns: 45 | The zip archive handler or NULL on error 46 | */ 47 | extern struct zip_t *zip_open(const char *zipname, int level, char mode); 48 | 49 | /* 50 | Closes the zip archive, releases resources - always finalize. 51 | 52 | Args: 53 | zip: zip archive handler. 54 | */ 55 | extern void zip_close(struct zip_t *zip); 56 | 57 | /* 58 | Opens an entry by name in the zip archive. 59 | For zip archive opened in 'w' or 'a' mode the function will append 60 | a new entry. In readonly mode the function tries to locate the entry 61 | in global dictionary. 62 | 63 | Args: 64 | zip: zip archive handler. 65 | entryname: an entry name in local dictionary. 66 | 67 | Returns: 68 | The return code - 0 on success, negative number (< 0) on error. 69 | */ 70 | extern int zip_entry_open(struct zip_t *zip, const char *entryname); 71 | 72 | /* 73 | Opens a new entry by index in the zip archive. 74 | This function is only valid if zip archive was opened in 'r' (readonly) mode. 75 | 76 | Args: 77 | zip: zip archive handler. 78 | index: index in local dictionary. 79 | 80 | Returns: 81 | The return code - 0 on success, negative number (< 0) on error. 82 | */ 83 | extern int zip_entry_openbyindex(struct zip_t *zip, int index); 84 | 85 | /* 86 | Closes a zip entry, flushes buffer and releases resources. 87 | 88 | Args: 89 | zip: zip archive handler. 90 | 91 | Returns: 92 | The return code - 0 on success, negative number (< 0) on error. 93 | */ 94 | extern int zip_entry_close(struct zip_t *zip); 95 | 96 | /* 97 | Returns a local name of the current zip entry. 98 | The main difference between user's entry name and local entry name 99 | is optional relative path. 100 | Following .ZIP File Format Specification - the path stored MUST not contain 101 | a drive or device letter, or a leading slash. 102 | All slashes MUST be forward slashes '/' as opposed to backwards slashes '\' 103 | for compatibility with Amiga and UNIX file systems etc. 104 | 105 | Args: 106 | zip: zip archive handler. 107 | 108 | Returns: 109 | The pointer to the current zip entry name, or NULL on error. 110 | */ 111 | extern const char *zip_entry_name(struct zip_t *zip); 112 | 113 | /* 114 | Returns an index of the current zip entry. 115 | 116 | Args: 117 | zip: zip archive handler. 118 | 119 | Returns: 120 | The index on success, negative number (< 0) on error. 121 | */ 122 | extern int zip_entry_index(struct zip_t *zip); 123 | 124 | /* 125 | Determines if the current zip entry is a directory entry. 126 | 127 | Args: 128 | zip: zip archive handler. 129 | 130 | Returns: 131 | The return code - 1 (true), 0 (false), negative number (< 0) on error. 132 | */ 133 | extern int zip_entry_isdir(struct zip_t *zip); 134 | 135 | /* 136 | Returns an uncompressed size of the current zip entry. 137 | 138 | Args: 139 | zip: zip archive handler. 140 | 141 | Returns: 142 | The uncompressed size in bytes. 143 | */ 144 | extern unsigned long long zip_entry_size(struct zip_t *zip); 145 | 146 | /* 147 | Returns CRC-32 checksum of the current zip entry. 148 | 149 | Args: 150 | zip: zip archive handler. 151 | 152 | Returns: 153 | The CRC-32 checksum. 154 | */ 155 | extern unsigned int zip_entry_crc32(struct zip_t *zip); 156 | 157 | /* 158 | Compresses an input buffer for the current zip entry. 159 | 160 | Args: 161 | zip: zip archive handler. 162 | buf: input buffer. 163 | bufsize: input buffer size (in bytes). 164 | 165 | Returns: 166 | The return code - 0 on success, negative number (< 0) on error. 167 | */ 168 | extern int zip_entry_write(struct zip_t *zip, const void *buf, size_t bufsize); 169 | 170 | /* 171 | Compresses a file for the current zip entry. 172 | 173 | Args: 174 | zip: zip archive handler. 175 | filename: input file. 176 | 177 | Returns: 178 | The return code - 0 on success, negative number (< 0) on error. 179 | */ 180 | extern int zip_entry_fwrite(struct zip_t *zip, const char *filename); 181 | 182 | /* 183 | Extracts the current zip entry into output buffer. 184 | The function allocates sufficient memory for a output buffer. 185 | 186 | Args: 187 | zip: zip archive handler. 188 | buf: output buffer. 189 | bufsize: output buffer size (in bytes). 190 | 191 | Note: 192 | - remember to release memory allocated for a output buffer. 193 | - for large entries, please take a look at zip_entry_extract function. 194 | 195 | Returns: 196 | The return code - 0 on success, negative number (< 0) on error. 197 | */ 198 | extern int zip_entry_read(struct zip_t *zip, void **buf, size_t *bufsize); 199 | 200 | /* 201 | Extracts the current zip entry into a memory buffer using no memory allocation. 202 | 203 | Args: 204 | zip: zip archive handler. 205 | buf: preallocated output buffer. 206 | bufsize: output buffer size (in bytes). 207 | 208 | Note: 209 | - ensure supplied output buffer is large enough. 210 | - zip_entry_size function (returns uncompressed size for the current entry) 211 | can be handy to estimate how big buffer is needed. 212 | - for large entries, please take a look at zip_entry_extract function. 213 | 214 | Returns: 215 | The return code - 0 on success, negative number (< 0) on error (e.g. bufsize 216 | is not large enough). 217 | */ 218 | extern int zip_entry_noallocread(struct zip_t *zip, void *buf, size_t bufsize); 219 | 220 | /* 221 | Extracts the current zip entry into output file. 222 | 223 | Args: 224 | zip: zip archive handler. 225 | filename: output file. 226 | 227 | Returns: 228 | The return code - 0 on success, negative number (< 0) on error. 229 | */ 230 | extern int zip_entry_fread(struct zip_t *zip, const char *filename); 231 | 232 | /* 233 | Extracts the current zip entry using a callback function (on_extract). 234 | 235 | Args: 236 | zip: zip archive handler. 237 | on_extract: callback function. 238 | arg: opaque pointer (optional argument, 239 | which you can pass to the on_extract callback) 240 | 241 | Returns: 242 | The return code - 0 on success, negative number (< 0) on error. 243 | */ 244 | extern int zip_entry_extract(struct zip_t *zip, 245 | size_t (*on_extract)(void *arg, 246 | unsigned long long offset, 247 | const void *data, 248 | size_t size), 249 | void *arg); 250 | 251 | /* 252 | Returns the number of all entries (files and directories) in the zip archive. 253 | 254 | Args: 255 | zip: zip archive handler. 256 | 257 | Returns: 258 | The return code - the number of entries on success, 259 | negative number (< 0) on error. 260 | */ 261 | extern int zip_total_entries(struct zip_t *zip); 262 | 263 | /* 264 | Creates a new archive and puts files into a single zip archive. 265 | 266 | Args: 267 | zipname: zip archive file. 268 | filenames: input files. 269 | len: number of input files. 270 | 271 | Returns: 272 | The return code - 0 on success, negative number (< 0) on error. 273 | */ 274 | extern int zip_create(const char *zipname, const char *filenames[], size_t len); 275 | 276 | /* 277 | Extracts a zip archive file into directory. 278 | 279 | If on_extract_entry is not NULL, the callback will be called after 280 | successfully extracted each zip entry. 281 | Returning a negative value from the callback will cause abort and return an 282 | error. The last argument (void *arg) is optional, which you can use to pass 283 | data to the on_extract_entry callback. 284 | 285 | Args: 286 | zipname: zip archive file. 287 | dir: output directory. 288 | on_extract_entry: on extract callback. 289 | arg: opaque pointer. 290 | 291 | Returns: 292 | The return code - 0 on success, negative number (< 0) on error. 293 | */ 294 | extern int zip_extract(const char *zipname, const char *dir, 295 | int (*on_extract_entry)(const char *filename, void *arg), 296 | void *arg); 297 | 298 | #ifdef __cplusplus 299 | } 300 | #endif 301 | 302 | #endif 303 | -------------------------------------------------------------------------------- /testing_package_sdk15.2/ble_app_uart_fw_1/pca10040/s132/ses/ble_app_uart_pca10040_s132.emProject: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 125 | 128 | 129 | -------------------------------------------------------------------------------- /testing_package_sdk15.2/ble_app_uart_fw_1/pca10040/s132/ses/ble_app_uart_pca10040_s132.emSession: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | -------------------------------------------------------------------------------- /testing_package_sdk15.2/ble_app_uart_fw_1/pca10040/s132/ses/flash_placement.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /testing_package_sdk15.2/ble_app_uart_fw_2/pca10040/s132/ses/ble_app_uart_pca10040_s132.emProject: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 125 | 128 | 129 | -------------------------------------------------------------------------------- /testing_package_sdk15.2/ble_app_uart_fw_2/pca10040/s132/ses/ble_app_uart_pca10040_s132.emSession: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | -------------------------------------------------------------------------------- /testing_package_sdk15.2/ble_app_uart_fw_2/pca10040/s132/ses/flash_placement.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /testing_package_sdk15.2/dfu_public_key.c: -------------------------------------------------------------------------------- 1 | 2 | /* This file was automatically generated by nrfutil on 2018-09-08 (YY-MM-DD) at 06:07:33 */ 3 | 4 | #include "sdk_config.h" 5 | #include "stdint.h" 6 | #include "compiler_abstraction.h" 7 | 8 | #if NRF_CRYPTO_BACKEND_OBERON_ENABLED 9 | /* Oberon backend is changing endianness thus public key must be kept in RAM. */ 10 | #define _PK_CONST 11 | #else 12 | #define _PK_CONST const 13 | #endif 14 | 15 | 16 | /* This file was generated with a throwaway private key, that is only inteded for a debug version of the DFU project. 17 | Please see https://github.com/NordicSemiconductor/pc-nrfutil/blob/master/README.md to generate a valid public key. */ 18 | 19 | #ifdef NRF_DFU_DEBUG_VERSION 20 | 21 | /** @brief Public key used to verify DFU images */ 22 | __ALIGN(4) _PK_CONST uint8_t pk[64] = 23 | { 24 | 0x59, 0x4c, 0xcb, 0x48, 0xdb, 0xb3, 0x0a, 0x84, 0x6e, 0x54, 0xe5, 0x9b, 0x16, 0x61, 0x9a, 0x45, 0x4c, 0x0a, 0x23, 0x19, 0x0b, 0x0e, 0x9f, 0x7e, 0x23, 0x9b, 0x02, 0xc4, 0x59, 0xad, 0xf5, 0x74, 25 | 0x9b, 0x3b, 0x61, 0x2f, 0xf2, 0xe7, 0x55, 0x1a, 0xf8, 0x72, 0x52, 0x14, 0x56, 0x10, 0x28, 0x83, 0x27, 0x1f, 0xef, 0xc1, 0xa8, 0x95, 0x80, 0x63, 0x43, 0x6f, 0x8d, 0x00, 0x72, 0x93, 0x53, 0x7b 26 | }; 27 | 28 | 29 | #else 30 | #error "Debug public key not valid for production. Please see https://github.com/NordicSemiconductor/pc-nrfutil/blob/master/README.md to generate it" 31 | #endif 32 | -------------------------------------------------------------------------------- /testing_package_sdk15.2/key_serial_dfu/app_uart_fw1.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jimmywong2003/nrf-slim-serial-uart-dfu-host-c-code/155f233c10d3a38c6e637215d44e7d5f67611e2b/testing_package_sdk15.2/key_serial_dfu/app_uart_fw1.zip -------------------------------------------------------------------------------- /testing_package_sdk15.2/key_serial_dfu/app_uart_fw2.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jimmywong2003/nrf-slim-serial-uart-dfu-host-c-code/155f233c10d3a38c6e637215d44e7d5f67611e2b/testing_package_sdk15.2/key_serial_dfu/app_uart_fw2.zip -------------------------------------------------------------------------------- /testing_package_sdk15.2/key_serial_dfu/demo_private.key: -------------------------------------------------------------------------------- 1 | -----BEGIN EC PRIVATE KEY----- 2 | MHcCAQEEILBLWRRr8XF0bHdBuD4O9VIX9xNew/GI50zUYunhkPPloAoGCCqGSM49 3 | AwEHoUQDQgAEdPWtWcQCmyN+nw4LGSMKTEWaYRab5VRuhAqz20jLTFl7U5NyAI1v 4 | Q2OAlajB7x8ngygQVhRScvgaVefyL2E7mw== 5 | -----END EC PRIVATE KEY----- 6 | -------------------------------------------------------------------------------- /testing_package_sdk15.2/key_serial_dfu/demo_public_key.c: -------------------------------------------------------------------------------- 1 | 2 | /* This file was automatically generated by nrfutil on 2019-03-04 (YY-MM-DD) at 10:59:52 */ 3 | 4 | #include "sdk_config.h" 5 | #include "stdint.h" 6 | #include "compiler_abstraction.h" 7 | 8 | #if NRF_CRYPTO_BACKEND_OBERON_ENABLED 9 | /* Oberon backend is changing endianness thus public key must be kept in RAM. */ 10 | #define _PK_CONST 11 | #else 12 | #define _PK_CONST const 13 | #endif 14 | 15 | 16 | /** @brief Public key used to verify DFU images */ 17 | __ALIGN(4) _PK_CONST uint8_t pk[64] = 18 | { 19 | 0x59, 0x4c, 0xcb, 0x48, 0xdb, 0xb3, 0x0a, 0x84, 0x6e, 0x54, 0xe5, 0x9b, 0x16, 0x61, 0x9a, 0x45, 0x4c, 0x0a, 0x23, 0x19, 0x0b, 0x0e, 0x9f, 0x7e, 0x23, 0x9b, 0x02, 0xc4, 0x59, 0xad, 0xf5, 0x74, 20 | 0x9b, 0x3b, 0x61, 0x2f, 0xf2, 0xe7, 0x55, 0x1a, 0xf8, 0x72, 0x52, 0x14, 0x56, 0x10, 0x28, 0x83, 0x27, 0x1f, 0xef, 0xc1, 0xa8, 0x95, 0x80, 0x63, 0x43, 0x6f, 0x8d, 0x00, 0x72, 0x93, 0x53, 0x7b 21 | }; 22 | -------------------------------------------------------------------------------- /testing_package_sdk15.2/key_serial_dfu/generate_private_key.bat: -------------------------------------------------------------------------------- 1 | @echo generate the private key 2 | nrfutil keys generate demo_private.key 3 | 4 | pause 5 | 6 | @echo generate the public_key from private key 7 | nrfutil keys display --key pk --format code demo_private.key --out_file demo_public_key.c 8 | -------------------------------------------------------------------------------- /testing_package_sdk15.2/key_serial_dfu/generate_zip_fw1.bat: -------------------------------------------------------------------------------- 1 | nrfutil pkg generate --hw-version 52 --application-version 1 --application ble_app_uart_pca10040_s132_FW1.hex --sd-req 0xB7 --key-file demo_private.key app_uart_fw1.zip 2 | -------------------------------------------------------------------------------- /testing_package_sdk15.2/key_serial_dfu/generate_zip_fw2.bat: -------------------------------------------------------------------------------- 1 | nrfutil pkg generate --hw-version 52 --application-version 1 --application ble_app_uart_pca10040_s132_FW2.hex --sd-req 0xB7 --key-file demo_private.key app_uart_fw2.zip 2 | -------------------------------------------------------------------------------- /testing_package_sdk15.2/key_serial_dfu/nrfutil_script_dfu_fw1.bat: -------------------------------------------------------------------------------- 1 | .\nrfutil -v -v -v -v dfu serial -pkg app_uart_fw1.zip -p com8 2 | -------------------------------------------------------------------------------- /testing_package_sdk15.2/key_serial_dfu/nrfutil_script_dfu_fw2.bat: -------------------------------------------------------------------------------- 1 | nrfutil -v -v -v -v dfu serial -pkg app_uart_fw2.zip -p com8 2 | -------------------------------------------------------------------------------- /testing_package_sdk15.2/secure_bootloader/main.c: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016 - 2018, Nordic Semiconductor ASA 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without modification, 7 | * are permitted provided that the following conditions are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright notice, this 10 | * list of conditions and the following disclaimer. 11 | * 12 | * 2. Redistributions in binary form, except as embedded into a Nordic 13 | * Semiconductor ASA integrated circuit in a product or a software update for 14 | * such product, must reproduce the above copyright notice, this list of 15 | * conditions and the following disclaimer in the documentation and/or other 16 | * materials provided with the distribution. 17 | * 18 | * 3. Neither the name of Nordic Semiconductor ASA nor the names of its 19 | * contributors may be used to endorse or promote products derived from this 20 | * software without specific prior written permission. 21 | * 22 | * 4. This software, with or without modification, must only be used with a 23 | * Nordic Semiconductor ASA integrated circuit. 24 | * 25 | * 5. Any software provided in binary form under this license must not be reverse 26 | * engineered, decompiled, modified and/or disassembled. 27 | * 28 | * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS 29 | * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 30 | * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE 31 | * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE 32 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 33 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 34 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 37 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 38 | * 39 | */ 40 | /** @file 41 | * 42 | * @defgroup bootloader_secure_ble main.c 43 | * @{ 44 | * @ingroup dfu_bootloader_api 45 | * @brief Bootloader project main file for secure DFU. 46 | * 47 | */ 48 | 49 | #include 50 | #include "boards.h" 51 | #include "nrf_mbr.h" 52 | #include "nrf_bootloader.h" 53 | #include "nrf_bootloader_app_start.h" 54 | #include "nrf_bootloader_dfu_timers.h" 55 | #include "nrf_dfu.h" 56 | #include "nrf_log.h" 57 | #include "nrf_log_ctrl.h" 58 | #include "nrf_log_default_backends.h" 59 | #include "app_error.h" 60 | #include "app_error_weak.h" 61 | #include "nrf_bootloader_info.h" 62 | #include "nrf_delay.h" 63 | 64 | static void on_error(void) 65 | { 66 | NRF_LOG_FINAL_FLUSH(); 67 | 68 | #if NRF_MODULE_ENABLED(NRF_LOG_BACKEND_RTT) 69 | // To allow the buffer to be flushed by the host. 70 | nrf_delay_ms(100); 71 | #endif 72 | #ifdef NRF_DFU_DEBUG_VERSION 73 | NRF_BREAKPOINT_COND; 74 | #endif 75 | NVIC_SystemReset(); 76 | } 77 | 78 | 79 | void app_error_handler(uint32_t error_code, uint32_t line_num, const uint8_t * p_file_name) 80 | { 81 | NRF_LOG_ERROR("%s:%d", p_file_name, line_num); 82 | on_error(); 83 | } 84 | 85 | 86 | void app_error_fault_handler(uint32_t id, uint32_t pc, uint32_t info) 87 | { 88 | NRF_LOG_ERROR("Received a fault! id: 0x%08x, pc: 0x%08x, info: 0x%08x", id, pc, info); 89 | on_error(); 90 | } 91 | 92 | 93 | void app_error_handler_bare(uint32_t error_code) 94 | { 95 | NRF_LOG_ERROR("Received an error: 0x%08x!", error_code); 96 | on_error(); 97 | } 98 | 99 | /** 100 | * @brief Function notifies certain events in DFU process. 101 | */ 102 | static void dfu_observer(nrf_dfu_evt_type_t evt_type) 103 | { 104 | switch (evt_type) 105 | { 106 | case NRF_DFU_EVT_DFU_FAILED: 107 | case NRF_DFU_EVT_DFU_ABORTED: 108 | case NRF_DFU_EVT_DFU_INITIALIZED: 109 | bsp_board_init(BSP_INIT_LEDS); 110 | bsp_board_led_on(BSP_BOARD_LED_0); 111 | bsp_board_led_on(BSP_BOARD_LED_1); 112 | bsp_board_led_off(BSP_BOARD_LED_2); 113 | break; 114 | case NRF_DFU_EVT_TRANSPORT_ACTIVATED: 115 | bsp_board_led_off(BSP_BOARD_LED_1); 116 | bsp_board_led_on(BSP_BOARD_LED_2); 117 | break; 118 | case NRF_DFU_EVT_DFU_STARTED: 119 | break; 120 | default: 121 | break; 122 | } 123 | } 124 | 125 | 126 | /**@brief Function for application main entry. */ 127 | int main(void) 128 | { 129 | uint32_t ret_val; 130 | 131 | // Protect MBR and bootloader code from being overwritten. 132 | ret_val = nrf_bootloader_flash_protect(0, MBR_SIZE, false); 133 | APP_ERROR_CHECK(ret_val); 134 | ret_val = nrf_bootloader_flash_protect(BOOTLOADER_START_ADDR, BOOTLOADER_SIZE, false); 135 | APP_ERROR_CHECK(ret_val); 136 | 137 | (void) NRF_LOG_INIT(nrf_bootloader_dfu_timer_counter_get); 138 | NRF_LOG_DEFAULT_BACKENDS_INIT(); 139 | 140 | NRF_LOG_INFO("Inside main"); 141 | 142 | ret_val = nrf_bootloader_init(dfu_observer); 143 | APP_ERROR_CHECK(ret_val); 144 | 145 | // Either there was no DFU functionality enabled in this project or the DFU module detected 146 | // no ongoing DFU operation and found a valid main application. 147 | // Boot the main application. 148 | nrf_bootloader_app_start(); 149 | 150 | // Should never be reached. 151 | NRF_LOG_INFO("After main"); 152 | } 153 | 154 | /** 155 | * @} 156 | */ 157 | -------------------------------------------------------------------------------- /testing_package_sdk15.2/secure_bootloader/pca10040_uart/ses/flash_placement.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | -------------------------------------------------------------------------------- /testing_package_sdk15.2/secure_bootloader/pca10040_uart/ses/secure_bootloader_uart_mbr_pca10040.emProject: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 147 | 148 | -------------------------------------------------------------------------------- /testing_package_sdk15.2/secure_bootloader/pca10040_uart/ses/secure_bootloader_uart_mbr_pca10040.emSession: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /testing_package_sdk15.2/secure_bootloader/pca10040_uart_debug/ses/flash_placement.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | -------------------------------------------------------------------------------- /testing_package_sdk15.2/secure_bootloader/pca10040_uart_debug/ses/secure_bootloader_uart_mbr_pca10040_debug.emProject: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 156 | 157 | -------------------------------------------------------------------------------- /testing_package_sdk15.2/secure_bootloader/pca10040_uart_debug/ses/secure_bootloader_uart_mbr_pca10040_debug.emSession: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | --------------------------------------------------------------------------------