mkbl2:Usage: unsupported size

本文介绍如何解决使用mkbl2工具时遇到的问题,详细分析了错误原因并提供了修改后的源码,确保生成的BL2.bin文件符合要求。

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

  在前面的文章 tiny4412 led 裸机程序中遇到一个问题,使用 mkbl2 来将裸机程序制作成 BL2.bin 时报错。
  ./mkbl2 led.bin bl2.bin 14336 
  Usage: unsupported size
  我尝试把 14336 改成 led.bin 文件的实际大小后,烧写到 emmc 程序无法按照预期执行。那么问题出在哪?
  

  BL2 位于设备偏移地址(512 +8K)字节处,BL1从这个位置读入 14K 字节的数据,存在iRAM 地址 0x02023400 处。 BL2不能大于(14K – 4)字节,最后 4字节用于存放较验码。

  那么我猜测,当我们的 led.bin 小于 14K 的时候,我们生成的 bl2.bin 也得是14K大小,并且最后4字节是校验码。

来看看 mkbl2 的源码:

/*
 * Copyright (c) 2010 Samsung Electronics Co., Ltd.
 *              http://www.samsung.com/
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main (int argc, char *argv[])
{
	FILE		*fp;
	unsigned char	src;
	char		*Buf, *a;
	int		BufLen;
	int		nbytes, fileLen;
	unsigned int	checksum = 0;
	int		i;

	if (argc != 4)
	{
		printf("Usage: mkbl1 <source file> <destination file> <size> \n");
		return -1;
	}

	BufLen = atoi(argv[3]);
	Buf = (char *)malloc(BufLen);
	memset(Buf, 0x00, BufLen);

	fp = fopen(argv[1], "rb");
	if( fp == NULL)
	{
		printf("source file open error\n");
		free(Buf);
		return -1;
	}

	fseek(fp, 0L, SEEK_END);
	fileLen = ftell(fp);
	fseek(fp, 0L, SEEK_SET);

	if ( BufLen > fileLen )
	{
		printf("Usage: unsupported size\n");
		free(Buf);
		fclose(fp);
		return -1;
	}

	nbytes = fread(Buf, 1, BufLen, fp);

	if ( nbytes != BufLen )
	{
		printf("source file read error\n");
		free(Buf);
		fclose(fp);
		return -1;
	}

	fclose(fp);

	for(i = 0;i < (14 * 1024) - 4;i++)
	{
		checksum += (unsigned char)(Buf[i]);
	}
	*(unsigned int*)(Buf+i) = checksum;

	fp = fopen(argv[2], "wb");
	if (fp == NULL)
	{
		printf("destination file open error\n");
		free(Buf);
		return -1;
	}

	a	= Buf;
	nbytes	= fwrite( a, 1, BufLen, fp);

	if ( nbytes != BufLen )
	{
		printf("destination file write error\n");
		free(Buf);
		fclose(fp);
		return -1;
	}

	free(Buf);
	fclose(fp);

	return 0;
}
  代码很简单,问题很明显,当我们的 Led.bin 小于14K时,我们输入文件实际大小,Bufsize == filesize ,但是它在计算校验的时候居然是按照 14k 的大小来计算的,数组都溢出了!!!

修改:

/*
 * Copyright (c) 2010 Samsung Electronics Co., Ltd.
 *              http://www.samsung.com/
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main (int argc, char *argv[])
{
	FILE		*fp;
	unsigned char	src;
	char		*Buf, *a;
	int		BufLen;
	int		nbytes, fileLen;
	unsigned int	checksum = 0;
	int		i;

	if (argc != 4)
	{
		printf("Usage: mkbl1 <source file> <destination file> <size> \n");
		return -1;
	}

	BufLen = atoi(argv[3]);
	Buf = (char *)malloc(BufLen);
	memset(Buf, 0x00, BufLen);

	fp = fopen(argv[1], "rb");
	if( fp == NULL)
	{
		printf("source file open error\n");
		free(Buf);
		return -1;
	}

	fseek(fp, 0L, SEEK_END);
	fileLen = ftell(fp);
	fseek(fp, 0L, SEEK_SET);
/*
	if ( BufLen > fileLen )
	{
		printf("Usage: unsupported size\n");
		free(Buf);
		fclose(fp);
		return -1;
	}
*/
	//nbytes = fread(Buf, 1, BufLen, fp);
	if(BufLen > fileLen)
		nbytes = fread(Buf, 1, fileLen, fp);
	else
		nbytes = fread(Buf, 1, BufLen, fp);
/*
	if ( nbytes != BufLen )
	{
		printf("source file read error\n");
		free(Buf);
		fclose(fp);
		return -1;
	}
*/
	fclose(fp);

	for(i = 0;i < (14 * 1024) - 4;i++)
	{
		checksum += (unsigned char)(Buf[i]);
	}
	*(unsigned int*)(Buf+i) = checksum;

	fp = fopen(argv[2], "wb");
	if (fp == NULL)
	{
		printf("destination file open error\n");
		free(Buf);
		return -1;
	}

	a	= Buf;
	nbytes	= fwrite( a, 1, BufLen, fp);

	if ( nbytes != BufLen )
	{
		printf("destination file write error\n");
		free(Buf);
		fclose(fp);
		return -1;
	}

	free(Buf);
	fclose(fp);

	return 0;
}
  

  1. gcc -o mkbl2 V310-EVT1-mkbl2.c  
  以后我们在使用 mkbl2 的时候无论文件大小是多少,都写死为 14336 就好了,例如:

  mkbl2 led.bin bl2.bin 14336

  实验验证上述做法是正确的,对4412的启动流程的了解又加深了一步。

总结:

  mkbl2 原理:读取源文件14k-4字节,计算校验和,写入14k-4至14k的地方,生成bl2.bin。

  无论是sd卡,还是emmc,BL2存放在第17个扇区,大小14K,校验和必须存放在14k-4的位置,估计BL1会到这个位置取出校验和进行校验文件是否损坏。

  

### IPOP 烧录指令及相关技术信息 IPOP 是一种用于嵌入式设备固件烧录的技术工具,通常应用于开发板、路由器或其他硬件平台的初始化配置阶段。以下是关于 **IPOP 烧录指令** 的使用方法和技术细节: #### 1. 基本概念与环境搭建 在执行 IPOP 烧录前,需确保目标设备已连接至主机并完成必要的驱动安装。对于树莓派等开发板,可以参考以下步骤准备环境[^1]: - 下载适合的目标操作系统镜像文件(如 Raspbian 老版本 `2018-11-13-raspbian-stretch.img`)。 - 使用专用软件(如 Etcher 或 Win32DiskImager)将镜像写入 SD 卡。 #### 2. IPOP 烧录命令格式 IPOP 工具支持多种模式下的固件更新或烧录操作。其基本命令结构如下: ```bash ipop_burn -p <port> -f <firmware_file> [-o offset] [-v] ``` | 参数 | 描述 | |------------|----------------------------------------------------------------------| | `-p <port>` | 指定串口通信端口号(例如 `/dev/ttyUSB0`)。 | | `-f <firmware_file>` | 提供待烧录的固件文件路径。 | | `-o offset` | 设置固件写入偏移量,默认为 0。 | | `-v` | 启用详细日志输出以便调试。 | 示例命令: ```bash ipop_burn -p /dev/ttyUSB0 -f u-boot.bin -o 0x400 -v ``` 此命令表示通过指定串口 `/dev/ttyUSB0` 将 `u-boot.bin` 文件烧录到目标设备,并设置起始偏移地址为 `0x400`。 #### 3. 配置 BIOS 类型的注意事项 如果涉及 BIOS 更新,则需要注意芯片兼容性和具体流程[^2]。例如,在 FlyPRO 中选择合适的芯片型号以及验证各步骤的成功状态。虽然该部分主要针对传统 PC 平台,但在某些情况下也可能适用于嵌入式系统中的 SPI Flash 设备。 #### 4. 解决缺失文件问题 当遇到缺少必要组件(如 `u-boot.bin` 和 `mkbl2`)的情况时,可以通过源码编译生成所需文件[^3]。假设当前目录存在 Makefile 文件,则可通过以下方式构建这些依赖项: ```makefile cd ../ make mkbl2 sd_fdisk ``` 完成后即可获得所需的二进制文件以继续后续烧录过程。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值