Chinaunix首页 | 论坛 | 博客
  • 博客访问: 15497219
  • 博文数量: 2005
  • 博客积分: 11986
  • 博客等级: 上将
  • 技术积分: 22535
  • 用 户 组: 普通用户
  • 注册时间: 2007-05-17 13:56
文章分类

全部博文(2005)

文章存档

2014年(2)

2013年(2)

2012年(16)

2011年(66)

2010年(368)

2009年(743)

2008年(491)

2007年(317)

分类: LINUX

2008-03-04 16:21:59


typedef struct stPreTIMItem
{
    int iFromOffset; //Offset from the file position 0

    char bDownloadToFlash;//0 Not to flash,1 to Flash

    int iFileSize;
    int iFlashAddress;
    char bYaffsFile; //Only for User&System Yaffs

    int iYaffsSize; //For Yaffs Item use only

    char szFileName[256];
}STPreTIMItem;


struct stHead
{
    char magic[5];
    int items;
    #define BITMES_MAX_NUMS 50
    STPreTIMItem bitmes[BITMES_MAX_NUMS];
}__attribute__((packed));

typedef struct stHead STHead;

static STHead st_head;
static int bimtes_count = -1;
static STPreTIMItem *pbitmes = &st_head.bitmes[0];

int
main (int argc, char **argv)
{
    int ifd;
    uint32_t checksum;
    uint32_t addr;
    uint32_t ep;
    struct stat sbuf;
    unsigned char *ptr;
    char *name = "";
    char tmp_char;
    int i;

    cmdname = *argv;

    addr = ep = 0;

    memset(&st_head,0,sizeof(STHead));
    memcpy(st_head.magic,"magic",5);
//mkimage -s -addr 0x2000000 -fn gen_eth_addr.o

// -s -addr 0x10000 -fy img2srec.o

// -s -addr 0x4000000 -v 0x40000000 -yaffs bmp_logo.o uImage

    while (--argc > 0 && **++argv == '-') {
        while (*++*argv) {
            switch (**argv) {
            case 's':
                pbitmes = &st_head.bitmes[++bimtes_count];
                goto NXTARG;
            case 'a':
            case 'v':
                if (--argc <= 0)
                    usage ();
                tmp_char = **argv;
                addr = strtoul (*++argv, (char **)&ptr, 16);
                if (*ptr) {
                    fprintf (stderr,
                        "%s: invalid load address %s\n",
                        cmdname, *argv);
                    exit (EXIT_FAILURE);
                }
                
                if(tmp_char == 'a')pbitmes->iFlashAddress = addr;
                else if(tmp_char == 'v')pbitmes->iYaffsSize = addr;

                goto NXTARG;
            case 'f':
            case 'y':
                if (--argc <= 0)
                    usage ();
                if(**argv == 'y'){pbitmes->bYaffsFile = 1;pbitmes->bDownloadToFlash = 1;}
                if(*(*argv+1) == 'y')pbitmes->bDownloadToFlash = 1;
                datafile = *++argv;
                strncpy(pbitmes->szFileName,datafile,250);
                goto NXTARG;
            default:
                usage ();
            }
        }
NXTARG:        ;
    }

    {
        pbitmes = &st_head.bitmes[0];
        bimtes_count += 1;
        st_head.items = bimtes_count;

        pbitmes->iFromOffset = (uint32_t)pbitmes - (uint32_t)&st_head + bimtes_count*sizeof(STPreTIMItem);

        for(i = 0;i < bimtes_count;i++,pbitmes++)
        {
            if (stat (pbitmes->szFileName, &sbuf) < 0) {
                    fprintf (stderr, "Can't stat %s: %s\n",
                        pbitmes->szFileName, strerror(errno));
                    exit (EXIT_FAILURE);
                }
         pbitmes->iFileSize = sbuf.st_size;//htonl(sbuf.st_size);

            if(i != bimtes_count)(pbitmes+1)->iFromOffset = pbitmes->iFromOffset + sbuf.st_size;
            #if 1
            fprintf(stderr,
            "\niFromOffset=%d"\
            "\nbDownloadToFlash=%d"\
            "\niFileSize=%d"\
            "\niFlashAddress=0x%x"\
            "\nbYaffsFile=%d"\
            "\niYaffsSize=0x%x"\
            "\nszFileName=%s\n",
            pbitmes->iFromOffset,
            pbitmes->bDownloadToFlash,
            pbitmes->iFileSize,//ntohl(pbitmes->iFileSize),

            pbitmes->iFlashAddress,
            pbitmes->bYaffsFile,
            pbitmes->iYaffsSize,
            pbitmes->szFileName
            );
            #endif
        }
    }

    imagefile = *argv;
    fprintf(stderr,"\n=========%s============\n\n",imagefile);
    ifd = open(imagefile, O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
    
    if (ifd < 0) {
        fprintf (stderr, "%s: Can't open %s: %s\n",
            cmdname, imagefile, strerror(errno));
        exit (EXIT_FAILURE);
    }
    
    pbitmes = &st_head.bitmes[0];

    if (write(ifd, &st_head, pbitmes->iFromOffset) != pbitmes->iFromOffset) {
        fprintf (stderr, "%s: Write error on %s: %s\n",
            cmdname, imagefile, strerror(errno));
        exit (EXIT_FAILURE);
    }

    for(i = 0;i < bimtes_count;i++,pbitmes++)
     copy_file (ifd, pbitmes->szFileName);

    /* We're a bit of paranoid */
#if defined(_POSIX_SYNCHRONIZED_IO) && !defined(__sun__) && !defined(__FreeBSD__) && !defined(__APPLE__)
    (void) fdatasync (ifd);
#else
    (void) fsync (ifd);
#endif

    if (close(ifd)) {
        fprintf (stderr, "%s: Write error on %s: %s\n",
            cmdname, imagefile, strerror(errno));
        exit (EXIT_FAILURE);
    }

    exit (EXIT_SUCCESS);
}

static void
copy_file (int ifd, const char *datafile)
{
    int dfd;
    struct stat sbuf;
    unsigned char *ptr;
    int zero = 0;
    int size;


    if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
        fprintf (stderr, "Can't open %s: %s\n",
             datafile, strerror(errno));
        exit (EXIT_FAILURE);
    }

    if (fstat(dfd, &sbuf) < 0) {
        fprintf (stderr, "Can't stat %s: %s\n",
             datafile, strerror(errno));
        exit (EXIT_FAILURE);
    }

    ptr = (unsigned char *)mmap(0, sbuf.st_size,
                 PROT_READ, MAP_SHARED, dfd, 0);
    if (ptr == (unsigned char *)MAP_FAILED) {
        fprintf (stderr, "Can't read %s: %s\n",
             datafile, strerror(errno));
        exit (EXIT_FAILURE);
    }


    size = sbuf.st_size;
    if (write(ifd, ptr, size) != size) {
        fprintf (stderr, "Write error on %s: %s\n",
             imagefile, strerror(errno));
        exit (EXIT_FAILURE);
    }

    (void) munmap((void *)ptr, sbuf.st_size);
    (void) close (dfd);
}

阅读(1216) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~