ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

HEX 文件格式详解

2022-03-01 15:32:25  阅读:1064  来源: 互联网

标签:记录 HEX record 地址 详解 文件格式 address data


写HEX转BIN的工具,找了一下中文关于HEX介绍的文本,下面这个是最全的,但是仍然少了一个03 05的记录类型没有介绍,干脆就去看了一下最讨厌的英文网站:

在80X86系统里面,03指明初始化寄存器CS: IP的值,05指明80386以上CPU的EIP寄存器的值;

在ARM系统里面:03是没有的,05是MDK-ARM里面独有的,指明 __main地址用,实际在转换到BIN文件的时候是没有用的。这下才放心去写工具,不然总觉得那个05会出问题。

文章和链接都在下面了(注意下面三篇文字都是转载的。)

 

原文地址:http://blog.csdn.net/a1037488611/article/details/43340055

Intel HEX 文件是由一行行符合Intel HEX 文件格式的文本所 构 成的ASCII 文本文件。在Intel HEX 文件中,每一行包含一 个 HEX 记录 。 这 些 记录 由 对应 机器 语 言 码 和/ 或常量 数 据的十六 进 制 编码数 字 组 成。Intel HEX 文件通常用于 传输将 被存于ROM 或者EPROM 中的程序和 数 据。大多 数 EPROM 编 程器或模 拟器使用Intel HEX 文件。

Hex文件是可以烧写到单片机中,被单片机执行的一种文件格式,生成Hex文件的方式由很多种,可以通过不同的编译器将C程序或者汇编程序编译生成hex。

一般Hex文件通过记事本就可以打开。可以发现一般Hex文件的记录格式如下:

Intel HEX 由任意数量的十六 进 制 记录组 成。每 个记录 包含5 个 域, 它们按以下格式排列:

 

每一组字母 对应 一 个 不同的域,每一 个 字母 对应 一 个 十六 进 制 编码 的 数 字。每一 个 域由至少 两个 十六 进制 编码数 字 组 成, 它们构 成一 个 字 节 ,就像以下描述的那 样:

:(冒号)每个Intel HEX 记录 都由冒 号开头 ;
LL 是 数 据 长 度域, 它 代表 记录当 中 数 据字 节 (dd) 的 数量 ;
aaaa 是地址域, 它代表 记录当 中 数据的起始地址;
TT是代表HEX 记录类 型的域 , 它 可能是以下 数 据 当 中的一 个:
     00 – 数 据 记录(Data Record)
     01 – 文件结 束 记录(End of FileRecord)
     02 – 扩展段地址 记录(ExtendedSegment Address Record)

03 – 开始段地址 记录(Start Segment Address Record)
     04 – 扩展 线 性地址 记录(Extended Linear Address Record)

05 – 开始线性地址 记录(Extended Segment Address Record)
dd 是数 据域 , 它 代表一 个 字 节 的 数 据. 一 个记录 可以有 许 多 数 据字 节 . 记录当 中 数 据字 节 的 数 量必 须 和数 据 长 度域(ll) 中指定的 数字相符.
cc 是校验 和域 , 它 表示 这个记录 的校 验 和. 校 验 和的 计 算是通 过将记录当 中所有十六 进 制 编码数 字 对 的 值相加, 以256 为 模 进 行以下 补 足.

表示为:“:[1字节长度][2字节地址][1字节记录类型][n字节数据段][1字节校验和] ”

 

具体根据记录类型分析如下:

(1)数据记录”00”

Intel HEX文件由任意数 量以回车换行符结束的数据记录组成数据记录外观如下:
    :10246200464C5549442050524F46494C4500464C33
其中:10 是这个记录当中 数 据字 节 的 数量.即0x10 ;
     2462 是数据 将 被下 载 到存 储 器 当中的地址.即0x2462 ;

00 是记录类型( 数 据 记录).即0x00 ;
464C…464C是 数据.分别代表0x46,0x4C... ;
33 是这个记录的校 验和即0x33;计算方法如下:256D-(10H+24H+62H+00H+46H+4CH+55H+49H+44H+20H+50H+52H+4FH+46H+49H+4CH+45H+00H+46H+4CH)/100H=33H;

 

(2)文件结束(EOF)”01”

Intel HEX文件必须以文件结束(EOF) 记录结束这个记录的记录类的值必须是01.EOF 记录 外 观总是如下:
   :00000001FF
其中:00 是记录当中 数 据字 节 的 数量.
   0000 是数据被下载到存储器当中的地址. 在文件结束记录当中地址是没有意义,被忽略的.0000h 是典型的地址;
   01 是记录类型 01( 文件 结 束 记录)
   FF 是 这个记录 的校 验 和, 计算方法如下: 256D-(00H+00H+00H+01H)=FFH;

 

(3)扩展线性地址记录(HEX386) ”04”

由于每行标识数据地址的只有2Byte,所以最大只能到64K,为了可以保存高地址的数据,就有了Extended Linear AddressRecord。如果这行的数据类型是0x04,那么,这行的数据就是随后数据的基地址。

扩展线性地址记录也叫作32位地址记录或HEX386记录.这些记录含数据的高16位扩展线性地址记录总是有两个数据字节,外观如下:
    :02000004FFFFFC

其中:02 是这个记录当中 数 据字 节 的 数量.
0000 是地址域, 对于 扩 展 线 性地址 记录 , 这个 域 总是0000.
04 是记录类型 04( 扩 展 线 性地址 记录)
FFFF 是地址的高16 位.
FC 是这个记录的校 验 和, 计算如下: 256D-(02H+00H+00H+04H+FFH+FFH)/100H=FFH;

当一 个扩展 线 性地址记录被读 取, 存 储于数据域的扩展线性地址被保存,它被应于

从 Intel HEX 文件 读取 来 的 随 后的 记录 . 线 性地址保持有效, 到 它 被另外一 个扩址记录 所改 变。

通 过 把 记录当 中的地址域 与 被移位的 来 自 扩 展 线 性地址 记录 的地址 数 据相加

 获 得 数 据 记录 的 绝对 存 储器地址。

 

以下的例子演示了这个过 程:

:0200000480007A    //数据记录的绝对存储器地址高16位为0x8000               

:100000001D000A00000000000000000000000000C9

:100010000000000085F170706F0104005D00BD00FC

第一行,是Extended Linear Address Record,里面的数据,也就是基地址是0x8000,第二行是DataRecord,里面的地址值是0x0000。那么数据1D000A00000000000000000000000000(共16个字节)要写入FLASH中的地址为 (0x8000<< 16)| 0x0000,也就是写入FLASH的0x80000000这个地址;第三行的数据写入地址为0x80000010.当一个HEX文件的数据超过64k的时候,文件中就会出现多个Extended Linear Address Record。

 

(4)扩展段地址记录(HEX86)“02“

扩展段地址记录也叫HEX86 记录 , 它包括4-19 位数据地址段. 扩展段地址记总是有两

个数 据字节 , 外观如下:
:020000021200EA
其中:02 是记录当中 数 据字 节 的 数量;
0000 是地址域. 对于 扩 展段地址 记录 , 这个 域 总是0000;
02 是记录类型 02( 扩 展段地址 记录);
1200 是地址段;
EA 是这个记录的校 验 和;

当一 个扩 展段地址 记录 被 读 取, 存 储 于 数 据域的 扩 展段地址被保存, 它 被 应 用于 从 Intel HEX 文件 读 取 来的 随 后的 记录 . 段地址保持有效, 直到 它 被另外一 个扩 展地址 记录 所改 变。

通 过 把 记录当 中的地址域 与 被移位的 来 自 扩 展段地址 记录 的地址 数 据相加 获 得 数 据 记录 的 绝对 存 储器地址。
     以下的例子演示了这个过 程..
来自 数 据 记录地址域的地址          2462
扩展段地址 记录数据域             +  1200(注意4-19位————转载的人加的)
                               ---------
绝对存 储 器地址                   00014462


Intel HEX 文件例子:
下面是一个 完整的Intel HEX 文件的例子:
:10001300AC12AD13AE10AF1112002F8E0E8F0F2244
:10000300E50B250DF509E50A350CF5081200132259
:03000000020023D8
:0C002300787FE4F6D8FD7581130200031D
:10002F00EFF88DF0A4FFEDC5F0CEA42EFEEC88F016
:04003F00A42EFE22CB
:00000001FF

 

 

 

以下是Keil官网的解析:http://www.keil.com/support/docs/1584/

GENERAL: INTEL HEX FILE FORMAT


Information in this article applies to:

  • MDK-ARM All Versions
  • C166 All Versions
  • C251 All Versions
  • C51 All Versions

QUESTION

What is the Intel HEX file format?

ANSWER

The Intel HEX file is an ASCII text file with lines of text that follow the Intel HEX file format. Each line in an Intel HEX file contains one HEX record. These records are made up of hexadecimal numbers that represent machine language code and/or constant data. Intel HEX files are often used to transfer the program and data that would be stored in a ROM or EPROM. Most EPROM programmers or emulators can use Intel HEX files.

Record Format

An Intel HEX file is composed of any number of HEX records. Each record is made up of five fields that are arranged in the following format:

:llaaaatt[dd...]cc

Each group of letters corresponds to a different field, and each letter represents a single hexadecimal digit. Each field is composed of at least two hexadecimal digits-which make up a byte-as described below:

  • : is the colon that starts every Intel HEX record.
  • ll is the record-length field that represents the number of data bytes (dd) in the record.
  • aaaa is the address field that represents the starting address for subsequent data in the record.
  • tt is the field that represents the HEX record type, which may be one of the following:
    00 - data record
    01 - end-of-file record
    02 - extended segment address record
    04 - extended linear address record
    05 - start linear address record (MDK-ARM only)
  • dd is a data field that represents one byte of data. A record may have multiple data bytes. The number of data bytes in the record must match the number specified by the ll field.
  • cc is the checksum field that represents the checksum of the record. The checksum is calculated by summing the values of all hexadecimal digit pairs in the record modulo 256 and taking the two's complement.

Data Records

The Intel HEX file is made up of any number of data records that are terminated with a carriage return and a linefeed. Data records appear as follows:

:10246200464C5549442050524F46494C4500464C33

This record is decoded as follows:

:10246200464C5549442050524F46494C4500464C33
|||||||||||                              CC->Checksum
|||||||||DD->Data
|||||||TT->Record Type
|||AAAA->Address
|LL->Record Length
:->Colon

where:

  • 10 is the number of data bytes in the record.
  • 2462 is the address where the data are to be located in memory.
  • 00 is the record type 00 (a data record).
  • 464C...464C is the data.
  • 33 is the checksum of the record.

Extended Linear Address Records (HEX386)

Extended linear address records are also known as 32-bit address records and HEX386 records. These records contain the upper 16 bits (bits 16-31) of the data address. The extended linear address record always has two data bytes and appears as follows:

:02000004FFFFFC

where:

  • 02 is the number of data bytes in the record.
  • 0000 is the address field. For the extended linear address record, this field is always 0000.
  • 04 is the record type 04 (an extended linear address record).
  • FFFF is the upper 16 bits of the address.
  • FC is the checksum of the record and is calculated as
    01h + NOT(02h + 00h + 00h + 04h + FFh + FFh).

When an extended linear address record is read, the extended linear address stored in the data field is saved and is applied to subsequent records read from the Intel HEX file. The linear address remains effective until changed by another extended address record.

The absolute-memory address of a data record is obtained by adding the address field in the record to the shifted address data from the extended linear address record. The following example illustrates this process..

Address from the data record's address field      2462
Extended linear address record data field     FFFF
                                              --------
Absolute-memory address                       FFFF2462

Extended Segment Address Records (HEX86)

Extended segment address records-also known as HEX86 records-contain bits 4-19 of the data address segment. The extended segment address record always has two data bytes and appears as follows:

:020000021200EA

where:

  • 02 is the number of data bytes in the record.
  • 0000 is the address field. For the extended segment address record, this field is always 0000.
  • 02 is the record type 02 (an extended segment address record).
  • 1200 is the segment of the address.
  • EA is the checksum of the record and is calculated as
    01h + NOT(02h + 00h + 00h + 02h + 12h + 00h).

When an extended segment address record is read, the extended segment address stored in the data field is saved and is applied to subsequent records read from the Intel HEX file. The segment address remains effective until changed by another extended address record.

The absolute-memory address of a data record is obtained by adding the address field in the record to the shifted-address data from the extended segment address record. The following example illustrates this process.

Address from the data record's address field     2462
Extended segment address record data field      1200
                                             --------
Absolute memory address                      00014462

Start Linear Address Records (MDK-ARM only)

Start linear address records specify the start address of the application. These records contain the full linear 32 bit address. The start linear address record always has four data bytes and appears as follows:

:04000005000000CD2A

where:

  • 04 is the number of data bytes in the record.
  • 0000 is the address field. For the start linear address record, this field is always 0000.
  • 05 is the record type 05 (a start linear address record).
  • 000000CD is the 4 byte linear start address of the application.
  • 2A is the checksum of the record and is calculated as
    01h + NOT(04h + 00h + 00h + 05h + 00h + 00h + 00h + CDh).

The Start Linear Address specifies the address of the __main (pre-main) function but not the address of the startup code which usually calls __main after calling SystemInit(). An odd linear start address specifies that __main is compiled for the Thumb instruction set.

The Start Linear Address Record can appear anywhere in hex file. In most cases this record can be ignored because it does not contain information which is needed to program flash memory.

End-of-File (EOF) Records

An Intel HEX file must end with an end-of-file (EOF) record. This record must have the value 01 in the record type field. An EOF record always appears as follows:

:00000001FF

where:

  • 00 is the number of data bytes in the record.
  • 0000 is the address where the data are to be located in memory. The address in end-of-file records is meaningless and is ignored. An address of 0000h is typical.
  • 01 is the record type 01 (an end-of-file record).
  • FF is the checksum of the record and is calculated as
    01h + NOT(00h + 00h + 00h + 01h).

Example Intel HEX File

Following is an example of a complete Intel HEX file:

:10001300AC12AD13AE10AF1112002F8E0E8F0F2244
:10000300E50B250DF509E50A350CF5081200132259
:03000000020023D8
:0C002300787FE4F6D8FD7581130200031D
:10002F00EFF88DF0A4FFEDC5F0CEA42EFEEC88F016
:04003F00A42EFE22CB
:00000001FF

 

以下是维基百科的解析:https://en.wikipedia.org/wiki/Intel_HEX

Intel HEX

From Wikipedia, the free encyclopedia    

Intel HEX is a file format that conveys binary information in ASCII text form. It is commonly used for programming microcontrollersEPROMs, and other types of programmable logic devices. In a typical application, a compiler or assemblerconverts a program's source code (such as in C or assembly language) to machine code and outputs it into a HEX file. The HEX file is then imported by a programmer to "burn" the machine code into a ROM, or is transferred to the target system for loading and execution.[1]

 

Contents

  [hide

 

Format[edit]

Intel HEX consists of lines of ASCII text that are separated by line feed or carriage return characters or both. Each text line contains hexadecimal characters that encode multiple binary numbers. The binary numbers may represent data,memory addresses, or other values, depending on their position in the line and the type and length of the line. Each text line is called a record.

Record structure[edit]

record (line of text) consists of six fields (parts) that appear in order from left to right:

  1. Start code, one character, an ASCII colon ':'.
  2. Byte count, two hex digits, indicating the number of bytes (hex digit pairs) in the data field. The maximum byte count is 255 (0xFF). 16 (0x10) and 32 (0x20) are commonly used byte counts.
  3. Address, four hex digits, representing the 16-bit beginning memory address offset of the data. The physical address of the data is computed by adding this offset to a previously established base address, thus allowing memory addressing beyond the 64 kilobyte limit of 16-bit addresses. The base address, which defaults to zero, can be changed by various types of records. Base addresses and address offsets are always expressed as big endian values.
  4. Record type (see record types below), two hex digits, 00 to 05, defining the meaning of the data field.
  5. Data, a sequence of n bytes of data, represented by 2n hex digits. Some records omit this field (n equals zero). The meaning and interpretation of data bytes depends on the application.
  6. Checksum, two hex digits, a computed value that can be used to verify the record has no errors.

Color legend[edit]

As a visual aid, the fields of Intel HEX records are colored throughout this article as follows:

  Start code   Byte count   Address   Record type   Data   Checksum

Checksum calculation[edit]

A record's checksum byte is the two's complement (negative) of the least significant byte (LSB) of the sum of all decoded byte values in the record preceding the checksum. It is computed by summing the decoded byte values and extracting the LSB of the sum (i.e., the data checksum), and then calculating the two's complement of the LSB (e.g., by inverting its bits and adding one).

For example, in the case of the record :0300300002337A1E, the sum of the decoded byte values is 03 + 00 + 30 + 00 + 02 + 33 + 7A = E2. The two's complement of E2 is 1E, which is the checksum byte appearing at the end of the record.

The validity of a record can be checked by computing its checksum and verifying that the computed checksum equals the checksum appearing in the record; an error is indicated if the checksums differ. Since the record's checksum byte is the negative of the data checksum, this process can be reduced to summing all decoded byte values — including the record's checksum — and verifying that the LSB of the sum is zero.

Text line terminators[edit]

Intel HEX records are separated by one or more ASCII line termination characters so that each record appears alone on a text line. This enhances legibility by visually delimiting the records and it also provides padding between records that can be used to improve machine parsing efficiency.

Programs that create HEX records typically use line termination characters that conform to the conventions of their operating systems. For example, Linux programs use a single LF (line feed, hex value 0A) character to terminate lines, whereas Windows programs use a CR (carriage return, hex value 0D) followed by a LF.

Record types[edit]

Intel HEX has six standard record types:

Hex codeRecord typeDescriptionExample
00 Data Contains data and a 16-bit starting address for the data. The byte count specifies number of data bytes in the record. The example shown to the right has 0B (decimal 11) data bytes (61, 64, 64, 72, 65, 73, 73, 20, 67, 61, 70) located at consecutive addresses beginning at address 0010. :0B0010006164647265737320676170A7
01 End Of File Must occur exactly once per file in the last line of the file. The data field is empty (thus byte count is 00) and the address field is typically 0000. :00000001FF
02 Extended Segment Address The data field contains a 16-bit segment base address (thus byte count is 02) compatible with 80x86 real mode addressing. The address field (typically 0000) is ignored. The segment address from the most recent 02 record is multiplied by 16 and added to each subsequent data record address to form the physical starting address for the data. This allows addressing up to one megabyte of address space. :020000021200EA
03 Start Segment Address For 80x86 processors, specifies the initial content of the CS:IP registers. The address field is 0000, the byte count is 04, the first two bytes are the CS value, the latter two are the IP value. :0400000300003800C1
04 Extended Linear Address Allows for 32 bit addressing (up to 4GiB). The address field is ignored (typically 0000) and the byte count is always 02. The two encoded, big endian data bytes specify the upper 16 bits of the 32 bit absolute address for all subsequent type 00 records; these upper address bits apply until the next 04 record. If no type 04 record precedes a 00record, the upper 16 address bits default to 0000. The absolute address for a type 00 record is formed by combining the upper 16 address bits of the most recent 04 record with the low 16 address bits of the 00 record. :02000004FFFFFC
05 Start Linear Address The address field is 0000 (not used) and the byte count is 04. The four data bytes represent the 32-bit value loaded into the EIP register of the 80386 and higher CPU. :04000005000000CD2A

Named formats[edit]

Special names are sometimes used to denote the formats of HEX files that employ specific subsets of record types. For example:

  • I8HEX files use only record types 00 and 01 (16 bit addresses)
  • I16HEX files use only record types 00 through 03 (20 bit addresses)
  • I32HEX files use only record types 00, 01, 04, and 05 (32 bit addresses)

File example[edit]

This example shows a file that has four data records followed by an end-of-file record:

:10010000214601360121470136007EFE09D2190140
:100110002146017E17C20001FF5F16002148011928
:10012000194E79234623965778239EDA3F01B2CAA7
:100130003F0156702B5E712B722B732146013421C7
:00000001FF

标签:记录,HEX,record,地址,详解,文件格式,address,data
来源: https://www.cnblogs.com/shoule/p/15950388.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有