## The Simple Code output format

Updated: Sep 15, 2004 [IJYB]
Archived: $Revision: 1.1 $

### Introduction

Simple-code is a generic flash loader format that has been developed by IAR Systems. It is a simple binary format with a few tags. The information in this document should be enough to write a flash loader.

### The file structure

The flash loader format file has a simple structure. The file starts with a file header that contains information about the entire program. After the file header records containing the bytes of the program and their location follow. All fields in the file are unsigned big endian values.

### The file header

The file header uses a magic number to identify the file as a simple-code file and it also contains the program's entry address and program flags. The program entry is a 4 byte address but because there might be several address spaces, a segment type field for the entry address is also needed. Finally, the file header contains a version information field.

The following table shows the order and sizes of the fields of the file header.

Name | Size in bytes
Magic number | 4
Program flags | 4
Number of Program Bytes | 4
Version Information | 2

#### Magic Number

The magic number for a simple-code file is 0x7F494152.

#### Program Flags

The program flags field supplies 32 bits. All are reserved for future use.

#### Number of program bytes

This is the number of program bytes in all data records in the file.

#### Version information

This is a plain 16-bit number, the current version number is 0.

### The records

The first byte in the record is the record tag, it is used to identify the different record types. Currently, (version 0) there are three record types, data records that contain the program bytes, entry records that contains the program's entry point and end record that marks the end of the file and the file's checksum. This might change in the future.

Note that there is no alignment since the size of the data record's program bytes not necessarily is a multiple of four.

#### Record tags

Currently (version 0) there are three record types. This might change in future versions. Note that tag 0 is invalid and not used by the format.

Name | Value
Invalid record | 0
Data record | 1
Entry record | 2
End record | 3

##### Data record

The data record contains the program bytes of a consecutive range of the program. The record contains the address of the first byte, record flags, the number of bytes and the actual bytes. The records are ordered, they are first ordered in increasing segment type order, then in increasing Record start address order.

The following table shows the order and size of the data record's fields.

Name | Size
Record tag | 1
Segment type | 1
Record flags | 2
Record start address | 4
Number of program bytes | 4
Program bytes | Number of program bytes

##### Entry record

The entry record contains the program's entry point. The record only contains an address. There should be at most one entry record in a file. It is up to each individual loader how to handle the case where there is no entry record.

The following table shows the order and size of the entry record's fields.

Name | Size
Record tag | 1
Entry address | 4
Segment type | 1

##### End record

The end record marks the end of the file. The file must contain exactly one such record and it must be the last record in the file. It also contains a checksum of the rest of the file.

The following table shows the order and size of the end record's fields.

Name | Size
Record tag | 1
Checksum | 4

#### Segment types

If there is more than one address space, this field is used to specify which address space the address in the file header or record uses. Note that segment type 0 is invalid and not used by the format.

Segment type | Value
INVALID | 0
CODE | 1
DATA | 2
XDATA | 3
IDATA | 4
BIT | 5

#### Record flags

There are currently no record flags. All these bits are set to 0 and are reserved for future use.

#### Record start address

This is the address of the first program byte in this record. The segment type field specifies which address space the record start address resides in.

#### Number of program bytes

This is the number of program bytes in the record.

#### Program bytes

These are the program bytes, the payload, of the data record. They form a consecutive range in memory, starting on the record's start address.

#### Entry address

This is the address of the program's entry point. The segment type specifies which address space the entry point resides in.

#### Checksum

All bytes in the file, except the four checksum bytes, are checksummed (the record tag for the checksum record is included in the checksum). To keep the flash loader simple the standard checksum is also simple; the sum of the file's individual program bytes and the checksum (as an unsigned 4-byte quantity, not the sum of the checksum's four bytes) must be zero. The checksum is ~(sum of the file's bytes) + 1. It is possible to allow more advanced check summing algorithms by using format modifiers, but there is currently no such modifier. A more advanced checksum algorithm will allow better error detection but will also require a more complex flash loader.

Copyright 2004 IAR Systems. All rights reserved.