add pynrfbintool

This commit is contained in:
hathach committed 2017-01-19 19:41:58 +07:00
1 parent 2a730585ea
commit 71fe451fa1
3 files changed
+370

No files matched your search

+210
View File
@@ -0,0 +1,210 @@
/**************************************************************************/
/*!
@file at_cmd_hwpin.c
@author hathach
@section LICENSE
Software License Agreement (BSD License)
Copyright (c) 2014, Adafruit Industries (adafruit.com)
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holders nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**************************************************************************/
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#define SIGNATURE_EXTENSION "_signature.bin"
#define DFU_INIT_EXTENSION "_init.dat"
typedef enum
{
BANK_VALID_APP = 0x01,
BANK_VALID_SD = 0xA5,
BANK_VALID_BOOT = 0xAA,
BANK_ERASED = 0xFE,
BANK_INVALID_APP = 0xFF,
} bootloader_bank_code_t;
/**@brief Structure holding bootloader settings for application and bank data.
*/
typedef struct
{
uint32_t bank_0; /**< Variable to store if bank 0 contains a valid application. */
uint32_t bank_0_crc; /**< If bank is valid, this field will contain a valid CRC of the total image. */
uint32_t bank_1; /**< Variable to store if bank 1 has been erased/prepared for new image. Bank 1 is only used in Banked Update scenario. */
uint32_t bank_0_size; /**< Size of active image in bank0 if present, otherwise 0. */
uint32_t sd_image_size; /**< Size of SoftDevice image in bank0 if bank_0 code is BANK_VALID_SD. */
uint32_t bl_image_size; /**< Size of Bootloader image in bank0 if bank_0 code is BANK_VALID_SD. */
uint32_t app_image_size; /**< Size of Application image in bank0 if bank_0 code is BANK_VALID_SD. */
uint32_t sd_image_start; /**< Location in flash where SoftDevice image is stored for SoftDevice update. */
} bootloader_settings_t;
/**
* crc16 - compute the CRC-CCITT (0xFFFF) for the data buffer
* @buffer: data pointer
* @len: number of bytes in the buffer
*
* Returns the updated CRC value.
*/
uint16_t crc16(uint8_t const *buffer, size_t len)
{
uint16_t crc = 0xffff;
int i;
for (i = 0; i < len; i++)
{
crc = (uint8_t)(crc >> 8) | (crc << 8);
crc ^= buffer[i];
crc ^= (unsigned char)(crc & 0xff) >> 4;
crc ^= (crc << 8) << 4;
crc ^= ((crc & 0xff) << 4) << 1;
}
return crc;
}
uint32_t get_filesize(char* filename)
{
uint32_t filesize;
FILE* fin = fopen(filename, "rb");
assert(fin != NULL);
// get file size
fseek(fin, 0, SEEK_END);
filesize = ftell(fin);
fseek(fin, 0, SEEK_SET);
fclose(fin);
return filesize;
}
uint16_t crc16_of_file(char *filename)
{
uint32_t filesize = get_filesize(filename);
assert(filesize > 0);
// open input binary file
FILE* fin = fopen(filename, "rb");
assert(fin != NULL);
//malloc and compute crc
uint8_t* buf = (uint8_t*) malloc(filesize);
(void) fread(buf, 1, filesize, fin);
uint16_t crc = crc16(buf, filesize);
free(buf);
fclose(fin);
return crc;
}
void create_app_signature(char *filename)
{
// this signature structure depends on bootloader_settings_t
bootloader_settings_t signature =
{
.bank_0 = BANK_VALID_APP,
.bank_0_crc = crc16_of_file(filename),
.bank_1 = BANK_ERASED,
.bank_0_size = get_filesize(filename)
} ;
// output name = input_name (no extension) + _signature .bin
char output_name[256] = { 0 };
memcpy(output_name, filename, strlen(filename) - 4); // dont copy extension
strcat(output_name, SIGNATURE_EXTENSION);
// Create output binary file
FILE* fout = fopen(output_name, "wb");
assert(fout != NULL );
// fwrite(&signature, 4, sizeof(signature)/4, fout);
fwrite(&signature, 1, sizeof(signature), fout);
fclose(fout);
}
#define U32_FROM_U16(high, low) ((uint32_t) (((high) << 16) | (low)))
#define DFU_SOFTDEVICE_ANY ((uint16_t)0xFFFE)
void create_app_dfu_init_packet(char* filename)
{
typedef struct
{
uint16_t device_type; /**< Device type (2 bytes), for example Heart Rate. This number must be defined by the customer before production. It can be located in UICR or FICR. */
uint16_t device_rev; /**< Device revision (2 bytes), for example major revision 1, minor revision 0. This number must be defined by the customer before production. It can be located in UICR or FICR. */
uint32_t app_version; /**< Application version for the image software. This field allows for additional checking, for example ensuring that a downgrade is not allowed. */
uint16_t softdevice_len; /**< Number of different SoftDevice revisions compatible with this application. The list of SoftDevice firmware IDs is defined in @ref softdevice. */
uint16_t softdevice[1]; /**< Variable length array of SoftDevices compatible with this application. The length of the array is specified in the length field. SoftDevice firmware id 0xFFFE indicates any SoftDevice. */
uint16_t app_crc16;
} dfu_init_packet_t;
dfu_init_packet_t init_packet =
{
.device_type = 0xffff,
.device_rev = 0xffff,
.app_version = U32_FROM_U16(0, 0),
.softdevice_len = 1,
.softdevice = { DFU_SOFTDEVICE_ANY },
.app_crc16 = crc16_of_file(filename)
};
// output name = input_name (no extension) + _signature .bin
char output_name[256] = { 0 };
memcpy(output_name, filename, strlen(filename) - 4); // dont copy extension
strcat(output_name, DFU_INIT_EXTENSION);
// Create output binary file
FILE* fout = fopen(output_name, "wb");
assert(fout != NULL );
fwrite(&init_packet, 1, sizeof(init_packet), fout);
fclose(fout);
}
int main(int argc, char *argv[])
{
if (argc != 2)
{
printf("%s <filename>", argv[0]);
return 0;
}
char *filename = argv[1];
create_app_signature(filename);
create_app_dfu_init_packet(filename);
return 0;
}
+112
View File
@@ -0,0 +1,112 @@
# Python nRF Binary Tool
# Generate a signature and DFU init packet for an nRF51822 binary.
# Note, can run unit tests by executing inside the same directory as this file:
# python -m unittest pynrfbintool
# Author: Tony DiCola
import argparse
import os
import struct
import unittest
# Constants and other config values.
SIGNATURE_EXTENSION = '_signature.bin'
BANK_VALID_APP = 0x01
BANK_VALID_SD = 0xA5
BANK_VALID_BOOT = 0xAA
BANK_ERASED = 0xFE
BANK_INVALID_APP = 0xFF
DFU_SOFTDEVICE_ANY = 0xFFFE
# Define the C structures used for the signature file format.
# typedef struct
# {
# uint32_t bank_0; /**< Variable to store if bank 0 contains a valid application. */
# uint32_t bank_0_crc; /**< If bank is valid, this field will contain a valid CRC of the total image. */
# uint32_t bank_1; /**< Variable to store if bank 1 has been erased/prepared for new image. Bank 1 is only used in Banked Update scenario. */
# uint32_t bank_0_size; /**< Size of active image in bank0 if present, otherwise 0. */
# uint32_t sd_image_size; /**< Size of SoftDevice image in bank0 if bank_0 code is BANK_VALID_SD. */
# uint32_t bl_image_size; /**< Size of Bootloader image in bank0 if bank_0 code is BANK_VALID_SD. */
# uint32_t app_image_size; /**< Size of Application image in bank0 if bank_0 code is BANK_VALID_SD. */
# uint32_t sd_image_start; /**< Location in flash where SoftDevice image is stored for SoftDevice update. */
# } bootloader_settings_t;
bootloader_settings = struct.Struct('<IIIIIIII')
def crc16(data):
"""Generate the 16-bit CRC of the provided input string and return it."""
crc = 0xFFFF
for c in data:
crc = (((crc >> 8) & 0xFF) | (crc << 8)) & 0xFFFF
crc = (crc ^ ord(c)) & 0xFFFF
crc = (crc ^ ((crc & 0xFF) >> 4)) & 0xFFFF
crc = (crc ^ ((crc << 8) << 4)) & 0xFFFF
crc = (crc ^ (((crc & 0xFF) << 4) << 1)) & 0xFFFF
return crc
def create_app_signature(data):
"""Generate a signature for the provided input data and return it. Input
should be a string of bytes with the input data.
"""
settings = bootloader_settings.pack(BANK_VALID_APP, # Bank 0
crc16(data), # Bank 0 CRC16
BANK_ERASED, # Bank 1
len(data), # Bank 0 length
0, # SD image size
0, # BL image size
0, # App image size
0) # SD image start
return settings
if __name__ == '__main__':
# Parse command line arguments.
parser = argparse.ArgumentParser(description='nRF51822 binary signature tool')
parser.add_argument('input',
action='store',
help='input binary filename')
parser.add_argument('--signature',
action='store',
help='filename for the generated signature. Defaults to <input file>_signature.bin',
metavar='FILENAME')
parser.add_argument('-q', '--quiet',
action='store_true',
help='disable all console output unless an error occurs')
args = parser.parse_args()
# Make sure input filename exists.
if not os.path.isfile(args.input):
raise RuntimeError('Input file does not exist!')
# Set default output filenames if not set.
prefix = os.path.splitext(args.input)[0]
signature = args.signature
if signature is None:
signature = prefix + SIGNATURE_EXTENSION
# Read in all the input data.
with open(args.input, 'rb') as in_file:
data = in_file.read()
if data is None or len(data) == 0:
raise RuntimeError('Input file has no data!')
# Generate and write signature.
if not args.quiet:
print 'Writing signature to: {0}'.format(signature)
with open(signature, 'wb') as out_file:
out_file.write(create_app_signature(data))
# Done!
if not args.quiet:
print 'Done!'
# Unit Tests.
class UnitTests(unittest.TestCase):
def test_crc16(self):
self.assertEqual(crc16('\x00'), 0xE1F0)
self.assertEqual(crc16('Hello'), 0xDADA)
def test_signature(self):
expected = '\x01\x00\x00\x00\xDA\xDA\x00\x00\xFE\x00\x00\x00\x05\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
self.assertEqual(create_app_signature('Hello'), expected)
+48
View File
@@ -0,0 +1,48 @@
# pynrfbintool
pynrfbintool.py takes a binary (.bin) application file and generates two main outputs:
### 1. Bootloader Application Config Settings (for manual updates)
The bootloader uses the last page of flash memory (0x3FC00) to store various bootloader config settings, including the user application size and a CRC to to verify user application integrity at start up. The config setting structure is show below (based on the `bootloader_settings_t` struct):
[0x7f000] = Application state (1 is valid)
[0x7f004] = Application CRC16
[0x7f008] = Bank 1 state (should be set to 0xFE)
[0x7f00c] = Application size
When we flash a new user application to an nRF51822 module that uses the DFU bootloader, we also have to update the bootloader config settings with these values. If these values aren't present, the bootloader will reject the user application code as invalid.
This script takes the application .bin file, calculates the CRC16 of it, then writes all the above info to a new .bin file with the `_signature.bin` suffix. The `_signature.bin` file can then be converted to a `_signature.hex` (in Intel Hex format) with an offset of 0x3FC00, which can be flashed to the nRF51822 at the appropriate address, causing the bootloader to treat the firmware as a valid image.
### 2. Init File (for OTA updates)
If you wish to use the over the air (OTA) features of the bootloader to update your nRF51822 via an Android or iOS device, you need a specially formatted `_init.dat` file. This file provides meta-data about the firmware you are trying to transfer to the target device, such as the target chipset, SD version, etc. The main goal of this file is to prevent flashing an inappropriate image for your device.
In addition to calculating the application config settings when manually flashing application code (as described above), pynrfbintool will also generate an appropriate `_init.dat` file for OTA updates.
# Syntax
The pynrfbintool script has the following syntax, where the `input` .bin file is the only mandatory parameter:
```
usage: pynrfbintool.py [-h] [--signature FILENAME] [--dfu-init FILENAME] [-q]
input
nRF51822 binary signature tool
positional arguments:
input input binary filename
optional arguments:
-h, --help show this help message and exit
--signature FILENAME filename for the generated signature. Defaults to
<input file>_signature.bin
--dfu-init FILENAME filename for the generated DFU init data. Defaults to
<input file>_init.dat
-q, --quiet disable all console output unless an error occurs
```
# nrfbintool.c
nrfbintool.c is provided for reference only if a binary implementation or pynrfbintool is ever required. pynrfbintool.py should be used in any production environment, though, as it is cross-platform and doesn't require any additional build steps to function.