合并文件功能代码

//本程序用于将fpga.pack和embedded.pack融合为一个.pack文件,用于备份程序下载或者发送给客户升级使用。
//本程序可用于ML30S B1与MLX A1的.pack文件合成。

#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>

using namespace std;

#define BOOTFILE    "./ml30sp_b1_fpga_debug_0.0.26.pack" 
#define IMAGEFILE   "./ML30S_Plus_B1_Embeded_Debug_V0.0.27.pack"
#define OUTFILE     "./ML30Sp_B1_Debug_Fpga_V0.0.26_Embeded_V0.0.27.pack"

//#define BOOTFILE    "./ML30Sp_B1_Debug_Fpga_bak.pack"
//#define IMAGEFILE   "./ML30Sp_B1_Debug_Embeded_V0.0.3.pack" 
//#define OUTFILE     "./ML30Sp_B1_Bak_Fpga_V0.0.2_Embeded_V0.0.3.pack"

typedef unsigned int        u32;
typedef unsigned short      u16;
typedef unsigned char       u8;

#define UPDATE_TYPE_OFF     6
#define BOOT_VERSION_OFF    12
#define IMAGE_VERSION_OFF   16
#define BOOT_SIZE_OFF       20
#define IMAGE_SIZE_OFF      24
#define CHECKSUM_OFF        28

#define HEAD_SIZE           36

int main(void)
{
    FILE * f1 = fopen(BOOTFILE,"rb");
    FILE * f2 = fopen(IMAGEFILE,"rb"); 
    FILE * f3 = fopen(OUTFILE,"wb+");

    u8 filebuff1[1024],filebuff2[1024],filebuff3[1024]; 
    u8 boot_version[4],image_version[4];
    u8 boot_size[4],image_size[4];
    u8 check_sum1[2],check_sum2[2];
    u32 sum1,sum2,sum3;
    u32 size1,size2;
    u32 tmp_size,read_size;
    int ret;

    if((f1 == NULL)||(f2 == NULL)||(f3 == NULL))
    {
        cout << "open file error" << endl;
        return 1;
    }

    //read head
    ret = fread(filebuff1,1,HEAD_SIZE,f1);
    if(ret != HEAD_SIZE)
    {
        cout << "read data1 error = " << ret << endl;
        return 1;
    }
    ret = fread(filebuff2,1,HEAD_SIZE,f2);
    if(ret != HEAD_SIZE)
    {
        cout << "read data2 error" << endl;
        return 1;
    }
    ret= memcmp(filebuff1,filebuff2,6);     //compare the flag+zero+mid+sub_mid.
    if(ret != 0)
    {
        cout << "two files compare error" << endl;
        return 1;
    }  

    if(filebuff1[3] != 0)
    {
        cout << "old firmware head information,not support" << endl;    //旧的固件头信息,改程序不支持,请使用ML30S A1版本的融合程序
        return 1;
    }

    //check the update type
    if(((filebuff1[UPDATE_TYPE_OFF]&0x0f) != 0x01)||((filebuff2[UPDATE_TYPE_OFF]&0x0f) != 0x02))    //first file should update boot.bin,second file should update image.ub
    {
        cout << "update flag error,please choose right file" << endl;
        return 1;
    }

    //get useful information
    memcpy(boot_version,filebuff1+BOOT_VERSION_OFF,4);
    memcpy(image_version,filebuff2+IMAGE_VERSION_OFF,4);
    memcpy(boot_size,filebuff1+BOOT_SIZE_OFF,4);
    memcpy(image_size,filebuff2+IMAGE_SIZE_OFF,4);
    memcpy(check_sum1,filebuff1+CHECKSUM_OFF,2);
    memcpy(check_sum2,filebuff2+CHECKSUM_OFF,2);
    size1 = (boot_size[0]<<24)|(boot_size[1]<<16)|(boot_size[2]<<8)|(boot_size[3]<<0);
    size2 = (image_size[0]<<24)|(image_size[1]<<16)|(image_size[2]<<8)|(image_size[3]<<0);
    sum1 = (check_sum1[0]<<8)|(check_sum1[1]);
    sum2 = (check_sum2[0]<<8)|(check_sum2[1]);   
    sum3 = (sum1+sum2)&0xffff;

    //fill the output buffer.
    memset(filebuff3,0,HEAD_SIZE);
    memcpy(filebuff3,filebuff1,7);  //copy the first 7 bytes useful information to output file buffer.
    filebuff3[UPDATE_TYPE_OFF] |= 0x03;   //set the update type to 0x03
    memcpy(filebuff3+BOOT_VERSION_OFF,boot_version,4);
    memcpy(filebuff3+IMAGE_VERSION_OFF,image_version,4);
    memcpy(filebuff3+BOOT_SIZE_OFF,boot_size,4);
    memcpy(filebuff3+IMAGE_SIZE_OFF,image_size,4);
    filebuff3[CHECKSUM_OFF] = (sum3>>8)&0xff;
    filebuff3[CHECKSUM_OFF+1] = sum3&0xff;

    ret = fwrite(filebuff3,1,HEAD_SIZE,f3);    //write the head to file
    if(ret != HEAD_SIZE)
    {
        cout << "write output file error" << endl;
    }

    //copy the real firmware data,BOOT.BIN to output file 
    tmp_size = size1;
    read_size = 1024;
    while(tmp_size != 0)
    {
        if(tmp_size < 1024)
        {
            read_size = tmp_size;
        }
        fread(filebuff3,1,read_size,f1);
        fwrite(filebuff3,1,read_size,f3);
        tmp_size -= read_size;
    }

    //copy the real firmware data,image.ub to output file 
    tmp_size = size2;
    read_size = 1024;
    while(tmp_size != 0)
    {
        if(tmp_size < 1024)
        {
            read_size = tmp_size;
        }
        fread(filebuff3,1,read_size,f2);
        fwrite(filebuff3,1,read_size,f3);
        tmp_size -= read_size;
    }

    cout << "combine firmware succeed" << endl;

    fclose(f1);
    fclose(f2);
    fclose(f3);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值