Ask The Oracle

...A Tech blog for Engineers, Software Devs, and Computer Geeks

SRecord Utility Should be in Every Toolbox

Written by BobW on October 26th, 2010

SRecord is utility that should be in the toolbox of every embedded systems engineer. It is an amazingly useful, open source, cross platform tool. It is like a Swiss Army knife for reformatting programming files.

Among the things SRecord (not to be confused with the Motorola S-record format) can do:

1) Add CRCs to files.
2) Read a program file in most common programming formats (including Intel, Motorola, Binary, Ascii, Atmel, any many others) and output it into a different format
3) Join or Split files into multiple EPROMs.
4) Compare user selected portions of files.
5) Add embedded text including serial numbers or copyright notices.
6) Generate data files that can read by C files, Verilog, or VHDL

Here is an example to show you how I have used it. I was working on a new embedded system design for an infusion pump that is part of a  medical device. It is important to verify that the firmware on the infusion pump has not been corrupted before it is allowed to control the delivery of drugs to a patient. On power up, the unit does a CRC on the code and compares it against the CRC stored in the firmware. If the CRC matches, the code starts up and runs the pump. If the CRC fails, the pump issues an error warning that the pump has failed and needs repair.

Unfortunately, many compilers have no support for embedding a CRC into the code. Some of the ones that do support a CRC have no details on the CRC implementation they use. There are dozens of standards available. You want a widely used standard that you can implement in your microprocessor at boot time to check the compiled CRC (see “Why You Should Avoid Checksums and Use CRCs Instead“).  This is where SRecord saves the day.

SRecord can read a compiler output file (in almost any format), calculate a CRC16 (using the CCITT standard), embed it into the file, and output the file in a number of user selected formats. Code for writing a CCITT CRC is available on the web and can easily be converted to run on any microprocessor. Since SRecord is open source, you can download the source code for the CRC and adapt it’s CRC algorithm for your needs.

SRecord has user selectable options to handle the needs an embedded systems designer. For example, if you generate a CRC on the output of the compiler, it will probably not match the CRC you generate on the microprocessor. Most compilers generate an output file that contains only the compiled code, not the empty gaps between routines. Your embedded CRC calculation is going to CRC the entire program space. The CRC will not match unless you fill the empty space with a known value (usually 0xff). SRecord can read in the file, fill the empty spaces with 0xFF and calculate a CRC that will match your embedded code. It can skip areas where a bootloader, or system code resides.

The SRecord package contains three main tools. Srec_cat is main routine that performs all of the magic. Srec_cmp is used to compare files. Srec_info is used to display summary information about a file. The latest version of SRecord is available here.

Here is an example that will read and Intel Hex file and embed a CRC. This batch file for Windows assumes you are giving the filename with no extension. .HEX is assumed.  Of course, SRecord is available for Linux as well.


REM Created by Bob Weiman,
ECHO usage: AddCrc filename
ECHO AddCrc takes a Intel Hex16 file and embeds a 16 bit CCITT CRC at location 0xF001
srec_cat %1.hex -intel -crop 0x0 0xF000 -fill 0xff 0x00 0xF000 --Big_Endian_CRC16 0xF001 -Output %1_crc.hex -intel

The package includes detailed documentation that describes all the ways you can use SRecord.

Here is a description of the command line parameters that I used:

Parameter Description
%1.hex takes the specified filename and appends .hex to it
-intel read the file in Intel hex format
-crop 0x0 0xF000 include only the area from 0 to 0xF000
fill 0xff 0x00 0xF000 the unused gaps from 0 to 0xF000 with 0xFF
–Big_Endian_CRC16 0xF001 Generate a CCITT CRC16 in big endian format and place it at 0xF001
-Output %1_crc.hex Output the file as [Input filename]_crc.hex
-intel output the file in Intel hex format

You must be logged in to post a comment.