<linux> uimage头部信息详解

本文详细解析了U-Boot中U-Image头部信息的具体内容及其作用,包括魔术字、CRC校验、时间戳等关键字段,并通过实际例子进行了说明。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载:记得仰望星空

于 2022-03-31 22:31:13 发布

732
 收藏 5
文章标签: linux
版权
uimage头部信息详解
一、uboot中的源码展示
二、实例分析
1、ih_magic(魔术字)
2、ih_hcrc(头部crc32校验)
3、ih_time(时间戳)
4、ih_size(镜像大小)
5、ih_load(解压地址)
6、ih_ep (启动地址)
7、ih_dcrc(镜像crc32校验)
8、ih_os (镜像操作系统类型)
9、ih_arch(架构类型)
10、ih_type(镜像类型)
11、ih_comp(镜像压缩类型)
12、ih_name(镜像名称)
三、总结
一、uboot中的源码展示
随便找一个主线uboot,在uboot根目录下的include/image.h文件,定义了uboot头部信息的结构体。

/*
 * Legacy format image header,
 * all data in network byte order (aka natural aka bigendian).
 */
typedef struct image_header {
        __be32          ih_magic;       /* Image Header Magic Number  0x27051956  */
        __be32          ih_hcrc;        /* Image Header CRC Checksum    */
        __be32          ih_time;        /* Image Creation Timestamp     */
        __be32          ih_size;        /* Image Data Size              */
        __be32          ih_load;        /* Data  Load  Address          */
        __be32          ih_ep;          /* Entry Point Address          */
        __be32          ih_dcrc;        /* Image Data CRC Checksum      */
        uint8_t         ih_os;          /* Operating System             */
        uint8_t         ih_arch;        /* CPU architecture             */
        uint8_t         ih_type;        /* Image Type                   */
        uint8_t         ih_comp;        /* Compression Type             */
        uint8_t         ih_name[IH_NMLEN];      /* Image Name           */
} image_header_t;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
ih_magic:魔术字,固定为0x27051956,占4字节,方便uboot判断要启动的镜像是否为uimage格式。
ih_hcrc:整个头部的crc32校验,32位校验,所以占4字节。
ih_time:时间戳,占4字节,为16进制,表示uimage生成的时间。
ih_size:镜像数据大小,即除去头部信息外的数据大小,占4字节。
ih_load:镜像数据的解压地址,uboot启动过程中,需要将镜像数据解压到DDR的地址位置。占4字节。
ih_ep:镜像解压后,启动镜像的地址位置,占4字节。
ih_dcrc:除去头部信息外,所有镜像数据的crc32校验数值,占4字节。
ih_os:表示镜像是什么操作系统下的镜像。 占1字节。
ih_arch:表示镜像所属的cpu架构,如arm、mips等。
ih_type:镜像类型,占1字节。
ih_comp:镜像的压缩类型,如gzip、lzma等,占1字节。
ih_name[IHNMLEN]: 镜像的名称。占32字节。 即名称最长32个字符。
整个头部信息,占用64字节,即0x40长度。

二、实例分析
以AP136固件的uboot header为例分析


AP136的kernel分区在后,所以镜像位置是从0x00630000开始,头部信息占用0x40长度,所以到0x00630040结束。

1、ih_magic(魔术字)
首先可以看到27 05 19 56,即0x27051956,为uboot头部信息的魔术字,可以看到该镜像是个uimage格式的镜像。这里可以看到该镜像的存储方式是大端模式,即低地址,存放高位数据。

2、ih_hcrc(头部crc32校验)
接下来四字节DB 85 93 13,表示头部的crc32校验,使用win32将头部信息截取出来

同时将校验位设置为0,因为没有计算校验之前,是0。


使用linux的crc32工具,算出其校验值。

crc32 uboot_header.bin
db859313
1
2
可以看到计算出来的结果与uboot head中的DB 85 93 13一致。 这里要注意,计算crc32之前一定要将校验位设置为0.

3、ih_time(时间戳)
再往后读取4字节,60 3C E8 2A,即为时间戳。即0x603CE82A,转换为10进制为1614604330
使用时间戳转换工具,可以看到镜像生成的时间是2021-03-01 21:12:10。


4、ih_size(镜像大小)
再读取4字节,00 14 4F E8,0x00144FE8,转换为10进制为1331176字节。将除去uboot head以外的数据截取出来。

可以到大小为1131176字节。


5、ih_load(解压地址)
往后读取4字节, 80 06 00 00,即0x8060 0000这个地址,uboot需要将镜像解压到该地址。

6、ih_ep (启动地址)
往后读取4字节,80 06 00 00,即0x8060 0000,可以看到启动镜像的地址,即解压地址。

7、ih_dcrc(镜像crc32校验)
往后读取4字节,为镜像数据的crc32校验,6B BD 55 AF, 同样我们将提取出来的kernel进行crc32计算

crc32 kernel.bin
6bbd55af
1
2
没问题,与头部信息一致。

8、ih_os (镜像操作系统类型)
读取1字节,05, os类型, 5对应位linux, 在uboot根目录下的include/image.h可以看到。

/*
 * Operating System Codes
 */
#define IH_OS_INVALID           0       /* Invalid OS   */
#define IH_OS_OPENBSD           1       /* OpenBSD      */
#define IH_OS_NETBSD            2       /* NetBSD       */
#define IH_OS_FREEBSD           3       /* FreeBSD      */
#define IH_OS_4_4BSD            4       /* 4.4BSD       */
#define IH_OS_LINUX             5       /* Linux        */
#define IH_OS_SVR4              6       /* SVR4         */
#define IH_OS_ESIX              7       /* Esix         */
#define IH_OS_SOLARIS           8       /* Solaris      */
#define IH_OS_IRIX              9       /* Irix         */
#define IH_OS_SCO               10      /* SCO          */
#define IH_OS_DELL              11      /* Dell         */
#define IH_OS_NCR               12      /* NCR          */
#define IH_OS_LYNXOS            13      /* LynxOS       */
#define IH_OS_VXWORKS           14      /* VxWorks      */
#define IH_OS_PSOS              15      /* pSOS         */
#define IH_OS_QNX               16      /* QNX          */
#define IH_OS_U_BOOT            17      /* Firmware     */
#define IH_OS_RTEMS             18      /* RTEMS        */
#define IH_OS_ARTOS             19      /* ARTOS        */
#define IH_OS_UNITY             20      /* Unity OS     */
#define IH_OS_INTEGRITY         21      /* INTEGRITY    */
#define IH_OS_OSE               22      /* OSE          */
#define IH_OS_PLAN9             23      /* Plan 9       */
#define IH_OS_OPENRTOS          24      /* OpenRTOS     */

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
9、ih_arch(架构类型)
再读取1字节, 05, 架构类型,对应mips架构,在uboot根目录下的include/image.h可以看到。

/*
* CPU Architecture Codes (supported by Linux)
*/
#define IH_ARCH_INVALID         0       /* Invalid CPU  */
#define IH_ARCH_ALPHA           1       /* Alpha        */
#define IH_ARCH_ARM             2       /* ARM          */
#define IH_ARCH_I386            3       /* Intel x86    */
#define IH_ARCH_IA64            4       /* IA64         */
#define IH_ARCH_MIPS            5       /* MIPS         */
#define IH_ARCH_MIPS64          6       /* MIPS  64 Bit */
#define IH_ARCH_PPC             7       /* PowerPC      */
#define IH_ARCH_S390            8       /* IBM S390     */
#define IH_ARCH_SH              9       /* SuperH       */
#define IH_ARCH_SPARC           10      /* Sparc        */
#define IH_ARCH_SPARC64         11      /* Sparc 64 Bit */
#define IH_ARCH_M68K            12      /* M68K         */
#define IH_ARCH_MICROBLAZE      14      /* MicroBlaze   */
#define IH_ARCH_NIOS2           15      /* Nios-II      */
#define IH_ARCH_BLACKFIN        16      /* Blackfin     */
#define IH_ARCH_AVR32           17      /* AVR32        */
#define IH_ARCH_ST200           18      /* STMicroelectronics ST200  */
#define IH_ARCH_SANDBOX         19      /* Sandbox architecture (test only) */
#define IH_ARCH_NDS32           20      /* ANDES Technology - NDS32  */
#define IH_ARCH_OPENRISC        21      /* OpenRISC 1000  */
#define IH_ARCH_ARM64           22      /* ARM64        */
#define IH_ARCH_ARC             23      /* Synopsys DesignWare ARC */
#define IH_ARCH_X86_64          24      /* AMD x86_64, Intel and Via */
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
10、ih_type(镜像类型)
再读取1字节, 02,表示镜像类型,对应为os kernel image,操作系统镜像。同样在uboot根目录下的include/image.h可以看到。

#define IH_TYPE_INVALID         0       /* Invalid Image                */
#define IH_TYPE_STANDALONE      1       /* Standalone Program           */
#define IH_TYPE_KERNEL          2       /* OS Kernel Image              */
#define IH_TYPE_RAMDISK         3       /* RAMDisk Image                */
#define IH_TYPE_MULTI           4       /* Multi-File Image             */
#define IH_TYPE_FIRMWARE        5       /* Firmware Image               */
#define IH_TYPE_SCRIPT          6       /* Script file                  */
#define IH_TYPE_FILESYSTEM      7       /* Filesystem Image (any type)  */
#define IH_TYPE_FLATDT          8       /* Binary Flat Device Tree Blob */
#define IH_TYPE_KWBIMAGE        9       /* Kirkwood Boot Image          */
#define IH_TYPE_IMXIMAGE        10      /* Freescale IMXBoot Image      */
#define IH_TYPE_UBLIMAGE        11      /* Davinci UBL Image            */
#define IH_TYPE_OMAPIMAGE       12      /* TI OMAP Config Header Image  */
#define IH_TYPE_AISIMAGE        13      /* TI Davinci AIS Image         */
#define IH_TYPE_KERNEL_NOLOAD   14      /* OS Kernel Image, can run from any load address */
#define IH_TYPE_PBLIMAGE        15      /* Freescale PBL Boot Image     */
#define IH_TYPE_MXSIMAGE        16      /* Freescale MXSBoot Image      */
#define IH_TYPE_GPIMAGE         17      /* TI Keystone GPHeader Image   */
#define IH_TYPE_ATMELIMAGE      18      /* ATMEL ROM bootable Image     */
#define IH_TYPE_SOCFPGAIMAGE    19      /* Altera SOCFPGA Preloader     */
#define IH_TYPE_X86_SETUP       20      /* x86 setup.bin Image          */
#define IH_TYPE_LPC32XXIMAGE    21      /* x86 setup.bin Image          */
#define IH_TYPE_LOADABLE        22      /* A list of typeless images    */
#define IH_TYPE_RKIMAGE         23      /* Rockchip Boot Image          */
#define IH_TYPE_RKSD            24      /* Rockchip SD card             */
#define IH_TYPE_RKSPI           25      /* Rockchip SPI image           */
#define IH_TYPE_ZYNQIMAGE       26      /* Xilinx Zynq Boot Image */

#define IH_TYPE_COUNT           27      /* Number of image types */
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
11、ih_comp(镜像压缩类型)
再读取1字节, 03, 镜像的压缩算法类型,对应lzma压缩类型。同样在uboot根目录下的include/image.h可以看到。

/*
 * Compression Types
 */
#define IH_COMP_NONE            0       /*  No   Compression Used       */
#define IH_COMP_GZIP            1       /* gzip  Compression Used       */
#define IH_COMP_BZIP2           2       /* bzip2 Compression Used       */
#define IH_COMP_LZMA            3       /* lzma  Compression Used       */
#define IH_COMP_LZO             4       /* lzo   Compression Used       */
#define IH_COMP_LZ4             5       /* lz4   Compression Used       */
1
2
3
4
5
6
7
8
9
12、ih_name(镜像名称)
读取剩余的32字节。名称为MIPS OpenWrt Linux-4.4.194。


三、总结
uboot头部占64字节。 为了方便uboot启动镜像,即bootm命令。
头部信息包括,魔术字(4)、头部crc32校验(4)、生成uimage的时间戳(4)、镜像数据大小(4)、解压地址(4)、启动地址(4)、镜像数据crc32校验(4)、镜像操作系统类型(1)、镜像架构类型(1)、镜像类型(1)、镜像压缩算法类型(1)、镜像名称(32).
计算头部crc32时,需要将校验位设置为0.
————————————————
版权声明:本文为优快云博主「记得仰望星空」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.youkuaiyun.com/weixin_43580872/article/details/123885462

### 代码功能与实现细节 以下是对包含 `swiper`、`v-for` 和 `item.iurl` 的 Vue 代码的功能及实现细节的解释: #### 功能描述 代码的主要功能是通过 Vue.js 框架实现一个轮播图(swiper)组件,用于展示一组图片或链接。每个轮播项的数据来源于一个数组,数组中的每一项包含一个 URL 地址(即 `item.iurl`),用于指定轮播项的来源或跳转目标[^1]。 #### 实现细节 以下是代码中涉及的关键部分及其作用: 1. **`swiper` 组件** `swiper` 是一个常见的轮播图组件,通常由第三方库(如 Swiper.js 或 Vue-awesome-swiper)提供。它允许用户在多个滑块之间进行水平或垂直切换。通过配置参数,可以实现自动播放、分页器、导航按钮等功能[^1]。 2. **`v-for` 指令** Vue.js 提供的 `v-for` 指令用于遍历数组或对象,并为每个元素生成对应的 DOM 结构。在这段代码中,`v-for="item in items"` 表示遍历 `items` 数组,为数组中的每一项生成一个轮播项。每个轮播项的 URL 地址由 `item.iurl` 提供[^1]。 3. **动态绑定属性** 在 Vue 中,可以通过 `:` 或 `v-bind` 动态绑定 HTML 属性。例如,`<web-view :src="item.iurl"></web-view>` 将 `item.iurl` 的值动态绑定到 `web-view` 组件的 `src` 属性上,从而指定每个轮播项的外部链接地址[^1]。 4. **事件监听** `@message="bindGetMsg"` 表示监听 `web-view` 组件发出的 `message` 事件,并将事件数据传递给 `bindGetMsg` 方法处理。在 `bindGetMsg` 方法中,可以从事件对象 `e` 中提取数据并存储到 `this.shareObj` 中,以便后续使用。 5. **代码结构** - `<view>`:Vue 中的普通容器标签,用于包裹子组件。 - `<web-view>`:用于加载外部网页的组件,支持跨平台应用开发(如微信小程序)。 - `bindGetMsg(e)`:事件处理函数,接收 `web-view` 组件发送的消息,并提取最后一条数据存储到 `shareObj` 中。 #### 示例代码 以下是一个完整的代码示例,展示如何实现上述功能: ```vue <template> <view> <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000"> <swiper-item v-for="(item, index) in items" :key="index"> <web-view :src="item.iurl" @message="bindGetMsg"></web-view> </swiper-item> </swiper> </view> </template> <script> export default { data() { return { items: [ { iurl: "https://example.com/image1.jpg" }, { iurl: "https://example.com/image2.jpg" }, { iurl: "https://example.com/image3.jpg" } ], shareObj: null }; }, methods: { bindGetMsg(e) { this.shareObj = e.detail.data[e.detail.data.length - 1]; } } }; </script> ``` ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值