注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

时间记录器

记录我的Linux、Android学习之路

 
 
 

日志

 
 

android 合并镜像之mtd.img镜像格式  

2011-08-18 19:47:26|  分类: Android |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

android 合并镜像之mtd.img镜像格式 - Neo - 时间记录器

 生成代码:

/* tools/mkbootimg/mkbootimg.c*/

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

#include "mincrypt/sha.h"
#include "bootimg.h"


//--------------------------------------------------------------------
// Image Common
//--------------------------------------------------------------------
#define TAG_AREA_IMAGE_HEADER     "[HEADER]"
//--------------------------------------------------------------------

//--------------------------------------------------------------------
// Raw Image
//--------------------------------------------------------------------
#define TAG_AREA_IMAGE_TYPE_RAW_IMAGE   "RAW_IMAGE"
//--------------------------------------------------------------------

typedef struct{
 unsigned char tagHeader[8];
 unsigned long ulHeaderSize;
 unsigned long ulCRC32;
 char   tagImageType[16];
 char   areaName[16];
    unsigned int    total_img;
} RAW_IMAGE_HEADER_T;

typedef struct{
 unsigned long long  ullPart;
 unsigned long long  ullLength;
} tagDiskImageBunchHeaderType;

#define MTD_PART_LK        0x0000
#define MTD_PART_MISC    0x0001
#define MTD_PART_RECOVERY   0x0002
#define MTD_PART_BOOT       0x0003
#define MTD_PART_SYSTEM    0x0004
#define MTD_PART_CACHE    0x0005
#define MTD_PART_USERDATA   0x0006
#define MTD_PART_RESERVED   0x0007
#define MTD_PART_FTL    0x0008

const unsigned int CRC32_TABLE[256] = {
 0x00000000, 0x90910101, 0x91210201, 0x01B00300,
 0x92410401, 0x02D00500, 0x03600600, 0x93F10701,
 0x94810801, 0x04100900, 0x05A00A00, 0x95310B01,
 0x06C00C00, 0x96510D01, 0x97E10E01, 0x07700F00,
 0x99011001, 0x09901100, 0x08201200, 0x98B11301,
 0x0B401400, 0x9BD11501, 0x9A611601, 0x0AF01700,
 0x0D801800, 0x9D111901, 0x9CA11A01, 0x0C301B00,
 0x9FC11C01, 0x0F501D00, 0x0EE01E00, 0x9E711F01,
 0x82012001, 0x12902100, 0x13202200, 0x83B12301,
 0x10402400, 0x80D12501, 0x81612601, 0x11F02700,
 0x16802800, 0x86112901, 0x87A12A01, 0x17302B00,
 0x84C12C01, 0x14502D00, 0x15E02E00, 0x85712F01,
 0x1B003000, 0x8B913101, 0x8A213201, 0x1AB03300,
 0x89413401, 0x19D03500, 0x18603600, 0x88F13701,
 0x8F813801, 0x1F103900, 0x1EA03A00, 0x8E313B01,
 0x1DC03C00, 0x8D513D01, 0x8CE13E01, 0x1C703F00,
 0xB4014001, 0x24904100, 0x25204200, 0xB5B14301,
 0x26404400, 0xB6D14501, 0xB7614601, 0x27F04700,
 0x20804800, 0xB0114901, 0xB1A14A01, 0x21304B00,
 0xB2C14C01, 0x22504D00, 0x23E04E00, 0xB3714F01,
 0x2D005000, 0xBD915101, 0xBC215201, 0x2CB05300,
 0xBF415401, 0x2FD05500, 0x2E605600, 0xBEF15701,
 0xB9815801, 0x29105900, 0x28A05A00, 0xB8315B01,
 0x2BC05C00, 0xBB515D01, 0xBAE15E01, 0x2A705F00,
 0x36006000, 0xA6916101, 0xA7216201, 0x37B06300,
 0xA4416401, 0x34D06500, 0x35606600, 0xA5F16701,
 0xA2816801, 0x32106900, 0x33A06A00, 0xA3316B01,
 0x30C06C00, 0xA0516D01, 0xA1E16E01, 0x31706F00,
 0xAF017001, 0x3F907100, 0x3E207200, 0xAEB17301,
 0x3D407400, 0xADD17501, 0xAC617601, 0x3CF07700,
 0x3B807800, 0xAB117901, 0xAAA17A01, 0x3A307B00,
 0xA9C17C01, 0x39507D00, 0x38E07E00, 0xA8717F01,
 0xD8018001, 0x48908100, 0x49208200, 0xD9B18301,
 0x4A408400, 0xDAD18501, 0xDB618601, 0x4BF08700,
 0x4C808800, 0xDC118901, 0xDDA18A01, 0x4D308B00,
 0xDEC18C01, 0x4E508D00, 0x4FE08E00, 0xDF718F01,
 0x41009000, 0xD1919101, 0xD0219201, 0x40B09300,
 0xD3419401, 0x43D09500, 0x42609600, 0xD2F19701,
 0xD5819801, 0x45109900, 0x44A09A00, 0xD4319B01,
 0x47C09C00, 0xD7519D01, 0xD6E19E01, 0x46709F00,
 0x5A00A000, 0xCA91A101, 0xCB21A201, 0x5BB0A300,
 0xC841A401, 0x58D0A500, 0x5960A600, 0xC9F1A701,
 0xCE81A801, 0x5E10A900, 0x5FA0AA00, 0xCF31AB01,
 0x5CC0AC00, 0xCC51AD01, 0xCDE1AE01, 0x5D70AF00,
 0xC301B001, 0x5390B100, 0x5220B200, 0xC2B1B301,
 0x5140B400, 0xC1D1B501, 0xC061B601, 0x50F0B700,
 0x5780B800, 0xC711B901, 0xC6A1BA01, 0x5630BB00,
 0xC5C1BC01, 0x5550BD00, 0x54E0BE00, 0xC471BF01,
 0x6C00C000, 0xFC91C101, 0xFD21C201, 0x6DB0C300,
 0xFE41C401, 0x6ED0C500, 0x6F60C600, 0xFFF1C701,
 0xF881C801, 0x6810C900, 0x69A0CA00, 0xF931CB01,
 0x6AC0CC00, 0xFA51CD01, 0xFBE1CE01, 0x6B70CF00,
 0xF501D001, 0x6590D100, 0x6420D200, 0xF4B1D301,
 0x6740D400, 0xF7D1D501, 0xF661D601, 0x66F0D700,
 0x6180D800, 0xF111D901, 0xF0A1DA01, 0x6030DB00,
 0xF3C1DC01, 0x6350DD00, 0x62E0DE00, 0xF271DF01,
 0xEE01E001, 0x7E90E100, 0x7F20E200, 0xEFB1E301,
 0x7C40E400, 0xECD1E501, 0xED61E601, 0x7DF0E700,
 0x7A80E800, 0xEA11E901, 0xEBA1EA01, 0x7B30EB00,
 0xE8C1EC01, 0x7850ED00, 0x79E0EE00, 0xE971EF01,
 0x7700F000, 0xE791F101, 0xE621F201, 0x76B0F300,
 0xE541F401, 0x75D0F500, 0x7460F600, 0xE4F1F701,
 0xE381F801, 0x7310F900, 0x72A0FA00, 0xE231FB01,
 0x71C0FC00, 0xE151FD01, 0xE0E1FE01, 0x7070FF00
};

unsigned int TC_CalcCRC(void *_base, unsigned int length, unsigned int crcIn)
{
 unsigned int cnt;
 unsigned char code;
 unsigned char *base = (unsigned char*)_base;

 for(cnt=0; cnt<length; cnt++)
 {
  code = (unsigned char)(base[cnt]^crcIn);
  crcIn = (crcIn>>8)^CRC32_TABLE[code&0xFF];
 }
 return crcIn;
}

static void *load_file(const char *fn, unsigned *_sz)
{
    char *data;
    int sz;
    int fd;

    data = 0;
    fd = open(fn, O_RDONLY);
    if(fd < 0) return 0;

    sz = lseek(fd, 0, SEEK_END);
    if(sz < 0) goto oops;

    if(lseek(fd, 0, SEEK_SET) != 0) goto oops;

    data = (char*) malloc(sz);
    if(data == 0) goto oops;

    if(read(fd, data, sz) != sz) goto oops;
    close(fd);

    if(_sz) *_sz = sz;
    return data;

oops:
    close(fd);
    if(data != 0) free(data);
    return 0;
}

int usage(void)
{
    fprintf(stderr,"usage: mkmtdimg\n"
            "       --lk <filename>\n"
            "       --misc <filename>\n"
            "       --recovery <filename>\n"
            "       --boot <filename>\n"
            "       --system <filename>\n"
            "       --cache <filename>\n"           
            "       --userdata <filename>\n"           
            "       --reserved <filename>\n"           
            "       --ftl <filename>\n"           
            "       -o|--output <filename>\n"
            );
    return 1;
}

int main(int argc, char **argv)
{
 RAW_IMAGE_HEADER_T stRawImageHeader;
 tagDiskImageBunchHeaderType stBunchHeader;

    unsigned int lk_size;
    unsigned int misc_size;
    unsigned int recovery_size;
    unsigned int boot_size;
    unsigned int system_size;
    unsigned int cache_size;
    unsigned int userdata_size;
    unsigned int reserved_size;
    unsigned int ftl_size;

    unsigned int img_cnt = 0;

    char *lk_fn = 0;
    void *lk_data = 0;
    char *misc_fn = 0;
    void *misc_data = 0;
    char *recovery_fn = 0;
    void *recovery_data = 0;
    char *boot_fn = 0;
    void *boot_data = 0;
    char *system_fn = 0;
    void *system_data = 0;
    char *cache_fn = 0;
    void *cache_data = 0;
    char *userdata_fn = 0;
    void *userdata_data = 0;
    char *reserved_fn = 0;
    void *reserved_data = 0;
    char *ftl_fn = 0;
    void *ftl_data = 0;
    char *bootimg = 0;
    int fd;

 unsigned int crc = 0;

    argc--;
    argv++;

    while(argc > 0)
    {
        char *arg = argv[0];
        char *val = argv[1];
        if(argc < 2) {
            return usage();
        }
        argc -= 2;
        argv += 2;
        if(!strcmp(arg, "--output") || !strcmp(arg, "-o")) {
            bootimg = val;
        } else if(!strcmp(arg, "--lk")) {
            lk_fn = val;
        } else if(!strcmp(arg, "--misc")) {
            misc_fn = val;
        } else if(!strcmp(arg, "--recovery")) {
            recovery_fn = val;
        } else if(!strcmp(arg, "--boot")) {
            boot_fn = val;
        } else if(!strcmp(arg, "--system")) {
            system_fn = val;
        } else if(!strcmp(arg, "--cache")) {
            cache_fn = val;
        } else if(!strcmp(arg, "--userdata")) {
            userdata_fn = val;
        } else if(!strcmp(arg, "--reserved")) {
            reserved_fn = val;
        } else if(!strcmp(arg, "--ftl")) {
            ftl_fn = val;
        } else {
            return usage();
        }
    }

    if(bootimg == 0) {
        fprintf(stderr,"error: no output filename specified\n");
        return usage();
    }
 
 if(boot_fn == 0) {
        fprintf(stderr,"error: no boot image specified\n");
        return usage();
    }

    if(lk_fn) {
     lk_data = load_file(lk_fn, &lk_size);
     if(lk_data == 0) {
         fprintf(stderr,"error: could not load lk '%s'\n", lk_fn);
         return 1;
     }
        img_cnt++;
    }

    if(misc_fn) {
     misc_data = load_file(misc_fn, &misc_size);
     if(misc_data == 0) {
         fprintf(stderr,"error: could not load misc '%s'\n", misc_fn);
         return 1;
     }
        img_cnt++;
    }

    if(recovery_fn) {
        recovery_data = load_file(recovery_fn, &recovery_size);
        if(recovery_data == 0) {
            fprintf(stderr,"error: could not load recovery '%s'\n", recovery_fn);
            return 1;
        }
        img_cnt++;
    }

    if(boot_fn) {
     boot_data = load_file(boot_fn, &boot_size);
     if(boot_data == 0) {
         fprintf(stderr,"error: could not load boot '%s'\n", boot_fn);
         return 1;
     }
        img_cnt++;
    }
 
 if(system_fn) {
     system_data = load_file(system_fn, &system_size);
     if(system_data == 0) {
         fprintf(stderr,"error: could not load system '%s'\n", system_fn);
         return 1;
     }
        img_cnt++;
 }
 
    if(cache_fn) {
        cache_data = load_file(cache_fn, &cache_size);
        if(cache_data == 0) {
            fprintf(stderr,"error: could not load cache '%s'\n", cache_fn);
            return 1;
        }
        img_cnt++;
    }

    if(userdata_fn) {
        userdata_data = load_file(userdata_fn, &userdata_size);
        if(userdata_data == 0) {
            fprintf(stderr,"error: could not load userdata'%s'\n", userdata_fn);
            return 1;
        }
        img_cnt++;
    }

    if(ftl_fn) {
        ftl_data = load_file(ftl_fn, &ftl_size);
        if(ftl_data == 0) {
            fprintf(stderr,"error: could not load ftl '%s'\n", ftl_fn);
            return 1;
        }
        img_cnt++;
    }
    printf("There are %d img(s) to be merged.\n", img_cnt);
    fd = open(bootimg, O_CREAT | O_TRUNC | O_WRONLY, 0644);
    if(fd < 0) {
        fprintf(stderr,"error: could not create '%s'\n", bootimg);
        return 1;
    }

 //4 Make DISK IMAGE HEADER
 memset(&stRawImageHeader,0,sizeof(stRawImageHeader));
 memcpy(stRawImageHeader.tagHeader,TAG_AREA_IMAGE_HEADER,8);
 stRawImageHeader.ulHeaderSize = (unsigned long)sizeof(stRawImageHeader);
 stRawImageHeader.ulCRC32 = 0; // CRC32 = 0 for skip
    stRawImageHeader.total_img = img_cnt; //number of img(s)
 strncpy(stRawImageHeader.tagImageType,TAG_AREA_IMAGE_TYPE_RAW_IMAGE,16);

 strncpy(stRawImageHeader.areaName,"KERNEL",16);

 if(write(fd, &stRawImageHeader, sizeof(stRawImageHeader)) != sizeof(stRawImageHeader))
        goto fail;
 
    crc = TC_CalcCRC(&stRawImageHeader, sizeof(stRawImageHeader), crc);

    if(lk_data) {
  stBunchHeader.ullPart = MTD_PART_LK;
  stBunchHeader.ullLength = lk_size;
  if(write(fd, &stBunchHeader, sizeof(stBunchHeader)) != sizeof(stBunchHeader)) goto fail;
  crc = TC_CalcCRC(&stBunchHeader, sizeof(stBunchHeader), crc);
     if(write(fd, lk_data, lk_size) != lk_size) goto fail;
  crc = TC_CalcCRC(lk_data, lk_size, crc);
        printf("lk.bin       is merged.\n");
    }
    if(misc_data) {
  stBunchHeader.ullPart = MTD_PART_MISC;
  stBunchHeader.ullLength = misc_size;
  if(write(fd, &stBunchHeader, sizeof(stBunchHeader)) != sizeof(stBunchHeader)) goto fail;
  crc = TC_CalcCRC(&stBunchHeader, sizeof(stBunchHeader), crc);
     if(write(fd, misc_data, misc_size) != misc_size) goto fail;
  crc = TC_CalcCRC(misc_data, misc_size, crc);
        printf("misc.img     is merged.\n");
    }
    if(recovery_data) {
  stBunchHeader.ullPart = MTD_PART_RECOVERY;
  stBunchHeader.ullLength = recovery_size;
  if(write(fd, &stBunchHeader, sizeof(stBunchHeader)) != sizeof(stBunchHeader)) goto fail;
  crc = TC_CalcCRC(&stBunchHeader, sizeof(stBunchHeader), crc);
        if(write(fd, recovery_data, recovery_size) != recovery_size) goto fail;
  crc = TC_CalcCRC(recovery_data, recovery_size, crc);
        printf("recovery.img is merged.\n");
    }
    if(boot_data) {
  stBunchHeader.ullPart = MTD_PART_BOOT;
  stBunchHeader.ullLength = boot_size;
  if(write(fd, &stBunchHeader, sizeof(stBunchHeader)) != sizeof(stBunchHeader)) goto fail;
  crc = TC_CalcCRC(&stBunchHeader, sizeof(stBunchHeader), crc);
     if(write(fd, boot_data, boot_size) != boot_size) goto fail;
  crc = TC_CalcCRC(boot_data, boot_size, crc);
        printf("boot.img     is merged.\n");
    }
    if(system_data) {
  stBunchHeader.ullPart = MTD_PART_SYSTEM;
  stBunchHeader.ullLength = system_size;
  if(write(fd, &stBunchHeader, sizeof(stBunchHeader)) != sizeof(stBunchHeader)) goto fail;
  crc = TC_CalcCRC(&stBunchHeader, sizeof(stBunchHeader), crc);
     if(write(fd, system_data, system_size) != system_size) goto fail;
  crc = TC_CalcCRC(system_data, system_size, crc);
        printf("system.img   is merged.\n");
    }
    if(cache_data) {
  stBunchHeader.ullPart = MTD_PART_CACHE;
  stBunchHeader.ullLength = cache_size;
  if(write(fd, &stBunchHeader, sizeof(stBunchHeader)) != sizeof(stBunchHeader)) goto fail;
  crc = TC_CalcCRC(&stBunchHeader, sizeof(stBunchHeader), crc);
     if(write(fd, cache_data, cache_size) != cache_size) goto fail;
  crc = TC_CalcCRC(cache_data, cache_size, crc);
        printf("cache.img    is merged.\n");
    }
    if(userdata_data) {
  stBunchHeader.ullPart = MTD_PART_USERDATA;
  stBunchHeader.ullLength = userdata_size;
  if(write(fd, &stBunchHeader, sizeof(stBunchHeader)) != sizeof(stBunchHeader)) goto fail;
  crc = TC_CalcCRC(&stBunchHeader, sizeof(stBunchHeader), crc);
        if(write(fd, userdata_data, userdata_size) != userdata_size) goto fail;
  crc = TC_CalcCRC(userdata_data, userdata_size, crc);
        printf("userdata.img is merged.\n");
    }
    if(reserved_data) {
  stBunchHeader.ullPart = MTD_PART_RESERVED;
  stBunchHeader.ullLength = reserved_size;
  if(write(fd, &stBunchHeader, sizeof(stBunchHeader)) != sizeof(stBunchHeader)) goto fail;
  crc = TC_CalcCRC(&stBunchHeader, sizeof(stBunchHeader), crc);
        if(write(fd, reserved_data, reserved_size) != reserved_size) goto fail;
  crc = TC_CalcCRC(reserved_data, reserved_size, crc);
        printf("reserved.img is merged.\n");
    }
    if(ftl_data) {
  stBunchHeader.ullPart = MTD_PART_FTL;
  stBunchHeader.ullLength = ftl_size;
  if(write(fd, &stBunchHeader, sizeof(stBunchHeader)) != sizeof(stBunchHeader)) goto fail;
  crc = TC_CalcCRC(&stBunchHeader, sizeof(stBunchHeader), crc);
        if(write(fd, ftl_data, ftl_size) != ftl_size) goto fail;
  crc = TC_CalcCRC(ftl_data, ftl_size, crc);
        printf("ftl.img      is merged.\n");
    }

 stRawImageHeader.ulCRC32 = crc;
    if(lseek(fd, 0, SEEK_SET) != 0) goto fail;
 if(write(fd, &stRawImageHeader, sizeof(stRawImageHeader)) != sizeof(stRawImageHeader)) goto fail;

    close(fd);

    return 0;

fail:
    unlink(bootimg);
    close(fd);
    fprintf(stderr,"error: failed writing '%s': %s\n", bootimg,
            strerror(errno));
    return 1;
}

如何将该文件添加进android编译,请参看下篇。

  评论这张
 
阅读(1099)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017