全志V5平台升级uboot分区Demo

最近接到领导需求,需要将sys_config.fex的修改透过网络,单独烧写分区的方式将修改升级到板上。研究了下全志的开发文档,sys_config.fex转成2进制文件后打包到uboot升级文件中,所以我们需要升级uboot分区。之前有过升级bootlogo分区的经验,接下来和之前差不多,不过需要注意的是,uboot分区所在emmc上的地址是有偏移的,不多说,直接上代码

#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define     INT8    char
#define         INT32   int


#define     FILE_NAME          "/mnt/nfs/boot_package.fex"
//#define     FILE_NAME          "/mnt/nfs/u-boot.fex"
#define     PARTITION_NAME          "/dev/mmcblk0"
#define UBOOT_START_SECTOR_IN_SDMMC             (32800)
#define SECTOR_SIZE	            				512

int mywrite(char * FileName,char * DevName)
{
	FILE	*fp = NULL;
	int  flash_handle = 0;
	INT8 *ptBuff = NULL;
	INT32 niReadLen = 0;
	INT32 niWriteLen = 0;

	if(FileName == NULL || DevName == NULL)
	{
		printf("UpdateExt4File  input  invalid!!!\n");
		return -1;
	}
	ptBuff = (INT8  *)malloc(4*1024*1024);
	if(ptBuff)
	{
		fp = fopen(FileName, "r");
		if (NULL != fp)
		{

			flash_handle = open(DevName,O_RDWR);
			 if(flash_handle < 0)
			 {
			 	printf("open  %s failed!!!\n",DevName);
			}

			niReadLen =fread(ptBuff,1,4*1024*1024,fp);
#if 1
	if (lseek(flash_handle, 0, SEEK_SET) == -1) {
		printf("Failed to SEEK_SET 0 mmcblk0 device\n");
		return -1;
	}

	if (lseek(flash_handle, UBOOT_START_SECTOR_IN_SDMMC * SECTOR_SIZE, SEEK_CUR) == -1) {
		printf("Failed to SEEK_SET %d mmcblk0 device\n", UBOOT_START_SECTOR_IN_SDMMC * SECTOR_SIZE);
		return -1;
	}
#endif				
				if(niReadLen > 0)
				{
					niWriteLen = write(flash_handle,ptBuff,niReadLen);
					if(niWriteLen != niReadLen)
					{
						printf("Write %s failed  niWriteLen: %d,niReadLen: %d !!!!\n",DevName,niWriteLen,niReadLen);
					}
				}

			fdatasync(flash_handle);
			close(flash_handle);
			fclose(fp);
		}
		else
		{
			printf("UpdateExt4File  fopen %s failed!!!\n",FileName);
			return -3;
		}
		free(ptBuff);
		ptBuff = NULL;
		printf("write uboot Partition OK !! \n");
	}
	else
	{
		printf("UpdateExt4File  malloc failed!!!\n");
		return -2;
	}
	return 0;
}

int main(int argc, char **argv)
{
	printf("Main start !!\n");
	mywrite(FILE_NAME,PARTITION_NAME);
	 
    return 0;
}

/dev/mmcblk0 是指emmc设备,而全志uboot的起始地址就是37800*512

以上

你可能感兴趣的:(全志V5平台升级uboot分区Demo)