forked from reticulatedpines/magiclantern_hg_02
-
Notifications
You must be signed in to change notification settings - Fork 6
/
pack-fir.c
156 lines (130 loc) · 4.1 KB
/
pack-fir.c
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/* original source:
* http://chdk.setepontos.com/index.php/topic,111.msg10849.html#msg10849
*/
#include <stdio.h>
#include <unistd.h>
#define CK(X) -1-(X)
typedef unsigned long t_dword;
typedef unsigned char t_byte;
typedef struct
{ t_dword modelID;
t_byte unknown[12];
char version[16];
t_dword cksum;
t_dword firstOffset;
t_dword secondOffset;
t_dword unknown_0;
} s_main_head;
typedef struct
{ t_dword cksum;
t_dword rec_num;
t_dword tableOffset;
t_dword packOffset;
t_dword tableLenght;
t_dword packLenght;
} s_pack_head;
typedef struct
{ t_dword flags;
t_dword Offset;
t_dword lenght;
char name[28];
} s_file_rec;
void f_clear(void *p, t_dword s)
{ t_byte *cp; cp=p;
while (s--) *(cp++)=0;
}
t_dword f_cksum(void *p, t_dword s)
{ t_byte *cp; t_dword sum=0; cp=p;
while (s--) sum += *(cp++);
return sum;
}
t_dword f_filecpy(s_file_rec *rec, FILE* out, FILE* in)
{ t_byte buf[256];
t_dword red,sum=0,len=0;
while (! feof(in))
{ red = fread(buf, sizeof(t_byte), 256, in);
fwrite(buf, sizeof(t_byte), red, out);
sum += f_cksum(buf, red);
len += red;
// printf("red:%d ", red);
}
rec->lenght=len;
return sum;
}
int f_add(t_dword* cksum, s_file_rec *rec, FILE* out, t_dword flags, char* infile, char* name)
{ FILE* in;
printf("Adding %s->%s\n",infile, name);
if ((in = fopen(infile, "rb")) == NULL)
{ printf("Can't open file name %s\n", infile);
return 0;
}
*cksum += f_filecpy(rec, out, in);
fclose(in);
strcpy(rec->name, name);
return rec->lenght;
}
main(int argc, char *argv[])
{
FILE* out;
FILE* pack;
s_main_head mhead;
s_pack_head phead;
s_file_rec rec[20];
s_file_rec frec;
t_dword len, cksum=0, i=0,j;
f_clear(&mhead,sizeof(s_main_head));
f_clear(&phead,sizeof(s_pack_head));
f_clear(rec,sizeof(s_file_rec)*20);
if (argc < 2)
printf("eospack outfile [firm version]");
if (argc > 2)
strcpy(mhead.version, argv[2]);
else
strcpy(mhead.version, "eos test firm1");
if ((out = fopen(argv[1], "wb")) == NULL) {
printf("Cant't open file name %s\n", argv[1]);
exit(-1);
}
if ((pack = fopen("tmp.pack", "wb+")) == NULL) {
printf("Cant't open file name tmp.pack\n");
exit(-1);
}
printf("File to write: %s\n", argv[1]);
printf("Writing pack\n");
cksum = 0;
if (f_add(&cksum, &(rec[i]), pack, 8, "firm", "MAIN_FIRMWARE")) i++;
if (f_add(&cksum, &(rec[i]), pack, 8, "ver", "FirmwareVersion")) i++;
fseek(pack, 0, SEEK_SET);
phead.tableOffset = sizeof(s_pack_head);
phead.rec_num = i;
phead.tableLenght = i*sizeof(s_file_rec);
phead.packOffset = phead.tableLenght+phead.tableOffset;
for (j=0; j<i; j++)
{
rec[j].Offset = phead.packLenght + phead.packOffset;
phead.packLenght += rec[j].lenght;
cksum += f_cksum(&(rec[j]), sizeof(s_file_rec));
}
phead.cksum = CK(cksum + f_cksum(&phead,sizeof(s_pack_head)));
printf("Writing main\n");
cksum=0;
len=0;
mhead.modelID= 0x80000250;
mhead.cksum= 0x00000000;
mhead.firstOffset = sizeof(s_main_head);
fwrite(&mhead, sizeof(s_main_head), 1, out);
len =f_add(&cksum, &frec, out, 0, "loader", "LOADER");
mhead.secondOffset = mhead.firstOffset + len;
fwrite (&phead, sizeof(s_pack_head), 1, out);
fwrite (rec, sizeof(s_file_rec), phead.rec_num, out);
cksum += f_filecpy(&frec, out, pack);
mhead.cksum = CK(cksum +
f_cksum(&mhead, sizeof(s_main_head)) +
f_cksum(&phead, sizeof(s_pack_head)) +
f_cksum(rec, sizeof(s_file_rec)*phead.rec_num));
fseek(out, 0, SEEK_SET);
fwrite(&mhead, sizeof(s_main_head), 1, out);
fclose(pack);
fclose(out);
exit(0);
}