Build commands:

./tiheader ubl 1 $(UBL_BIN) $(UBL_BIN).hdr

./tiheader uboot 25 $(UBOOT_BIN) $(UBOOT_BIN).hdr

 

Source code:

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

int main (int argc, char *argv[]) {

    FILE * pFile;
    size_t lSize=0;
    size_t numPages=0;
    size_t tSize=0;
    size_t result=0;
    char * buffer;
     unsigned int *ubldesc;
     
    if(argc!=5 || (strcmp(argv[1], "ubl") && strcmp(argv[1], "uboot")) || atoi(argv[2]) <= 0 )
    {
        printf("%s [ubl/uboot] [start block] [file input] [file output]\n", argv[0]); 
        return 0;
    }
    const char *action = argv[1];
    int  start_block = atoi(argv[2]);
    const char *in = argv[3];
    const char *out = argv[4];
    pFile = fopen ( in , "rb" );
    if (pFile==NULL) 
    {
        printf("File(%s) open error\n", in); 
        printf("%s [ubl/uboot] [start block] [file input] [file output]\n", argv[0]); 
        exit (1);
    }
    // obtain file size:
     fseek (pFile , 0 , SEEK_END);
     lSize = ftell (pFile);
     tSize = lSize + 2048;
     rewind (pFile);
    numPages = 0;
    while ((numPages * 2048 ) < lSize) {
        numPages++;
    }

     // allocate memory to contain the whole file:
    buffer = (char*) malloc(sizeof(char) * tSize);
     if (buffer == NULL) 
     {
         printf("Memory malloc error(%lu)\n",lSize + 2048);
          exit (2);
    }

    memset(buffer,0xff,sizeof(buffer));
    
    ubldesc = (unsigned int *)buffer;
    if( strcmp(action, "ubl") == 0 )
    {
        ubldesc[0] = 0xA1ACED00; //ubl magic number
        ubldesc[1] = 0x0100;     //ubl fixed entry point;
        ubldesc[2] = numPages;   //ubl number of pages;
        ubldesc[3] = start_block;          //ubl search start block;
        ubldesc[4] = 1;          //ubl search start page;
        ubldesc[5] = 0;          //ubl loadaddress;
    }
    else
    {
        ubldesc[0] = 0xA1ACED66;    //uboot magic number
        ubldesc[1] = 0x81080000;    //uboot fixed entry point;
        ubldesc[2] = numPages;       //uboot number of pages;
        ubldesc[3] = start_block;            //uboot search start block;
        ubldesc[4] = 1;             //uboot search start page;
        ubldesc[5] = 0x81080000;    //uboot loadaddress;
    }    
    
     // copy the file into the buffer:
     result = fread (buffer + 2048 ,1,lSize,pFile);
     if (result != lSize) 
     {
        printf("%s Reading error\n",argv[1]); 
         exit (3);
     }

     printf("Read file size %lu\n",lSize);
     fclose (pFile);
     
     
    pFile = fopen ( out , "wb+" );
    if (pFile==NULL) 
    {
        printf("File(%s) create error\n", out); 
        exit (1);
    }
    
    result = fwrite (buffer , 1 , tSize , pFile );
     if (result != tSize) 
     {
         printf("Writing error-->result(%lu), tSize(%lu), lSize(%lu)\n", result, tSize, lSize); 
         exit (4);
     }
    fclose (pFile);
    free (buffer);
    printf("Writing File size %lu\n",result);
    return 0;
}
arrow
arrow
    全站熱搜

    Person 發表在 痞客邦 留言(0) 人氣()