mkNonSecCmdh.c 2.72 KB
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <netinet/in.h>

#include <getopt.h>

#include <PR/bbmetadata.h>

#define PAGE_SIZE	512

/*********************************************************************

 COMMAND SYNTAX:

   mkNonSecCmdh [-r rights] appInFile cmdhOutFile

 INPUT 
 
   appInFile     - binary file, determine size from file.
  
   [-r rights]   - future additions to enable hw/sw runtime rights
                   Form is TBD, but default to all rights.

 OUTPUT:
   
   cmdhOutFile  R - content metadata binary struct, BbContentMetaData.
                          
*********************************************************************/



void printUsage()
{
    fprintf(stderr, "\nUsage:\n\n"
            " mkNonSecCmdh [-r rights] [-e] appInFile cmdhOutFile\n\n");
}


int asciiToBin(char *str,int bytes,u8 *buf)
{
    int i,j;
    char t;

    for(i=0;i<bytes;i++){
        t = str[2*i];
        if ((t >= '0') && (t <= '9')) 
            j = (t - '0') << 4;
        else if ((t >= 'a') && (t <= 'f')) 
            j = (t - 'a' + 10) << 4;
        else if ((t >= 'A') && (t <= 'F')) 
            j = (t - 'A' + 10) << 4;
        else return -1;
        
        t = str[2*i+1];
        if ((t >= '0') && (t <= '9')) 
            j ^= (t - '0');
        else if ((t >= 'a') && (t <= 'f')) 
            j ^= (t - 'a' + 10);
        else if ((t >= 'A') && (t <= 'F')) 
            j ^= (t - 'A' + 10);
        else return -1;

        buf[i]=j;
    }
    
    return 0;
}

int main(int argc, char* argv[]) 
{
    FILE *appInFile,*cmdOutFile;
    char c;
    char ibuf[PAGE_SIZE];
    int inBytes, contentBytes;
    BbContentMetaDataHead cmd;
    u32 rightsFlags;

    opterr = 0;
    while((c = getopt(argc, argv, "r:")) != (char)-1) {
        switch(c) {
        case 'r': 
            rightsFlags = atoi(optarg);
            break;
	default:
            printUsage();
	    return 1;
	}
    }

    if((argc-optind)!=2){
        fprintf(stderr,"\nError: too few required args (2)\n\n");
        printUsage();
        return 1;
    }

    if((appInFile = fopen(argv[optind], "r"))==NULL){
        fprintf(stderr,"failed to open file %s\n",argv[optind]);
        return 1;
    }

    optind++;
    if((cmdOutFile = fopen(argv[optind], "w"))==NULL){
        fprintf(stderr,"failed to open file %s\n",argv[optind]);
        return 1;
    }

    memset(&cmd, 0, sizeof(BbContentMetaDataHead) );

    cmd.descFlags|=BB_CMD_DESC_COMMON_KEY;

    contentBytes=0;
    while((inBytes = fread(ibuf, sizeof(ibuf[0]), PAGE_SIZE, appInFile)) > 0) {
        contentBytes+=inBytes;
    }

    cmd.size = htonl(contentBytes); 

    if (fwrite((void *)&cmd, 1, sizeof(cmd), cmdOutFile) < 0) {
        perror("fwrite");
        return 1;
    }

    return 0;
}