mkNonSecCmdh.c
2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#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;
}