-
Notifications
You must be signed in to change notification settings - Fork 1
/
updatefw.cpp
1469 lines (1195 loc) · 24.8 KB
/
updatefw.cpp
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Anderson Luiz Alves
*
* 2017-07-13
*
*
* license: public domain
*
* require: libssl-dev
* compile: g++ -o updatefw updatefw.cpp -lcrypto
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/stat.h>
#include <openssl/md5.h> // libssl-dev
#include <netdb.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#define SZ_VERSION "v0.3"
#define ERR -1
#define ERR_HEADER -2
#define ERR_CHECKSUM -3
#define ERR_PARTITION -4
#define ERR_ARGS -50
#define ERR_MEM -100
#define ERR_FILEOPEN -101
#define ERR_FILECREATE -102
#define ERR_FILEREAD -103
#define ERR_FILEWRITE -104
#define ERR_FILESEEK -105
#define ERR_FILEEXISTS -106
#define ERR_SOCKET_ADDRESS -200
#define ERR_SOCKET_CONNECT -201
#define ERR_SOCKET_SEND -202
#define ERR_SOCKET_READ -203
#define BUFSIZE 4096
#define SENDSIZE 1280
#define SEND_WAIT_MSEC 2
#define PRODCODE 0xB14103
#define RECOVERY_ADDRESS "239.255.255.240"
#define RECOVERY_PORT "53428"
bool FORCE = false;
typedef int INT32;
typedef short INT16;
/** bigendian int */
struct INT32B
{
INT32 value;
INT32 invert(INT32 v) const
{
INT32 ret;
unsigned char *pret = (unsigned char *)&ret;
unsigned char *pv = (unsigned char *)&v;
for(size_t n=0; n<sizeof(v); ++n)
pret[n] = pv[sizeof(v)-n-1];
return ret;
}
INT32 get() const
{
return invert(value);
}
void set(INT32 v)
{
value = invert(v);
}
operator INT32 () const
{
return get();
}
INT32B& operator = (const INT32 v)
{
set(v);
return *this;
}
INT32B& operator += (const INT32 v)
{
set(get() + v);
return *this;
}
INT32B& operator -= (const INT32 v)
{
set(get() - v);
return *this;
}
};
// https://github.com/torvalds/linux/blob/master/include/uapi/linux/magic.h
// https://github.com/torvalds/linux/blob/master/fs/squashfs/squashfs_fs.h
#define SQUASHFS_MAGIC 0x73717368 // hsqs
#define SQUASHFS_MAGIC_SWAP 0x71736873 // shsq
#define ZLIB_COMPRESSION 1
#define LZMA_COMPRESSION 2
#define LZO_COMPRESSION 3
#define XZ_COMPRESSION 4
#define LZ4_COMPRESSION 5
struct SQUASHFS_SUPERBLOCK
{
INT32 s_magic;
INT32 inodes;
INT32 mkfs_time;
INT32 block_size;
INT32 fragments;
INT16 compression;
INT16 block_log;
INT16 flags;
INT16 no_ids;
INT16 s_major;
INT16 s_minor;
};
#define MAX_PARTITION 3
#define CHECKSUM_SIZE 16
struct PARTITION // 284
{
char name[64]; // size 64?
INT32B offset;
INT32B size;
char unk[212]; // x0
};
struct HEADER // 4096
{
char copyright[80]; // x0 // Copyright (c) 2006-2010 BeWAN Systems
char platform[80]; // x50 // V5471 iBoxNG GVT
char sprodcode[8]; // xa0 // B14103
char filename[80]; // xa8 // B14103-GVT-RC2-98509.bin
char date[24]; // xf8 // 2015-03-04-15:55:18
char releasename[8]; // x110 // GVT-RC2 // size 8?
char unk0[748]; // x118 // 00
INT32B bootstatus; // x404 // 0x4E415742 = will boot, ffffffff on checksum
INT32B check; // x408 // 0x4E415742
INT32B size; // x40c // firmware size
INT32B bootorder; // x410 // 00000000 on checksum
INT32B prodcode; // x414 // 0xB14103
INT32B svn_version; // x418
PARTITION partition[MAX_PARTITION]; // x41c
char unk1[2176];
unsigned char checksum[CHECKSUM_SIZE]; // xff0 // md5(0-xff0,x1000-) ^ magic
};
// magics for B14103
const unsigned char CHECKSUM_MAGIC[CHECKSUM_SIZE] = {
0x0A, 0x20, 0x20, 0x42,
0x7C, 0x22, 0x61, 0xDB,
0x12, 0x78, 0xB2, 0x9B,
0x27, 0xB2, 0xCE, 0x31
};
// firmware recovery fragment
struct FRAGMENT
{
INT32B seq;
INT32B unk0;
INT32B prodcode; // 0x00B14103
INT32B address; // 0x80800000
INT32B fwsize;
INT32B fragsize;
};
int err_mem()
{
printf("ERROR: memory\n");
return ERR_MEM;
}
int err_fileopen()
{
printf("ERROR: file open failed\n");
return ERR_FILEOPEN;
}
int err_filecreate()
{
printf("ERROR: file create failed\n");
return ERR_FILECREATE;
}
int err_fileread()
{
printf("ERROR: file read failed\n");
return ERR_FILEREAD;
}
int err_filewrite()
{
printf("ERROR: file write failed\n");
return ERR_FILEWRITE;
}
int err_fileseek(int offset)
{
printf("ERROR: seek offset %X failed\n", offset);
return ERR_FILESEEK;
}
int err_fileexists()
{
printf("ERROR: file already exists\n");
return ERR_FILEEXISTS;
}
int err_header()
{
printf("ERROR: invalid header\n");
return ERR_HEADER;
}
int err_socket_address()
{
printf("ERROR: get socket address failed\n");
return ERR_SOCKET_ADDRESS;
}
int err_socket_connect()
{
printf("ERROR: create socket failed\n");
return ERR_SOCKET_CONNECT;
}
int err_socket_send()
{
printf("ERROR: socket send failed\n");
return ERR_SOCKET_SEND;
}
void stripext(char *fname)
{
char *s = fname;
while(0 != *s) ++s;
for(; s > fname; --s)
{
if('.' == *s)
{
*s = 0;
break;
}
if(('\\' == *s) || ('/' == *s)) break;
}
}
const char* basename(const char *fname)
{
const char *s = fname;
while(0 != *s) ++s;
for(; s >= fname; --s)
if(('\\' == *s) || ('/' == *s))
return s+1;
return fname;
}
char* changeext(const char *fname, const char *newext)
{
char *s = (char*)malloc(
strlen(fname) + sizeof(char) +
strlen(newext) + sizeof(char));
if(0 == s) return 0;
strcpy(s, fname);
stripext(s);
strcat(s, ".");
strcat(s, newext);
return s;
}
bool exists(const char *fname)
{
struct stat t;
return (0 == stat(fname, &t));
}
int getchecksum(FILE *f, unsigned char *checksum, HEADER *header=0)
{
unsigned char buf[BUFSIZE];
// read header from file
if(0 == header)
{
if(0 != fseek(f, 0, SEEK_SET))
return err_fileseek(0);
clearerr(f);
if(fread(buf, 1, sizeof(HEADER), f) < sizeof(HEADER))
{
if(ferror(f)) return err_fileread();
return err_header();
}
}
else
// use arg header
{
if(0 != fseek(f, sizeof(HEADER), SEEK_SET))
return err_fileseek(sizeof(HEADER));
memcpy(buf, header, sizeof(HEADER));
}
// required for checksum
header = (HEADER*)buf;
header->bootorder = 0;
header->bootstatus = 0xffffffff;
// md5
MD5_CTX ctx;
MD5_Init(&ctx);
MD5_Update(&ctx, header, sizeof(HEADER)-CHECKSUM_SIZE);
// read data
clearerr(f);
for(;;)
{
int r = fread(buf, 1, sizeof(buf), f);
if(r <= 0)
{
if(ferror(f)) return err_fileread();
break;
}
MD5_Update(&ctx, buf, r);
}
MD5_Final(checksum, &ctx);
for(int n=0; n<CHECKSUM_SIZE; ++n)
checksum[n] ^= CHECKSUM_MAGIC[n];
return 0;
}
int fwinfo(const char *fname)
{
printf("file: %s\n", fname);
FILE *f = fopen(fname, "rb");
if(0 == f) return err_fileopen();
// load header
HEADER header;
clearerr(f);
if(fread(&header, 1, sizeof(header), f) < sizeof(header))
{
if(ferror(f))
{
fclose(f);
return err_fileread();
}
fclose(f);
return err_header();
}
// display info
printf("copyright: %s\n", header.copyright);
printf("platform: %s\n", header.platform);
printf("sprodcode: %s\n", header.sprodcode);
printf("filename: %s\n", header.filename);
printf("date: %s\n", header.date);
printf("releasename: %s\n", header.releasename);
printf("prodcode: 0x%X\n", header.prodcode.get());
printf("svn version: %d\n", header.svn_version.get());
printf("somecheck: 0x%X\n", header.check.get());
printf("boot status: 0x%X\n", header.bootstatus.get());
printf("boot order: %d\n", header.bootorder.get());
printf("firmware size: %d", header.size.get());
printf(
"\n"
"partitions:\n"
);
PARTITION *part = &(header.partition[0]);
for(int idx=0; idx<MAX_PARTITION; ++idx, ++part)
{
// empty?
if((0 == part->name[0]) && (0 == part->offset) && (0 == part->size)) continue;
printf(" %d\n", idx);
printf(" name: %s\n", part->name);
int n;
n = part->offset;
printf(" offset: %d (0x%X)\n", n, n);
n = part->size;
printf(" size: %d (0x%X)\n", n, n);
}
printf(
"checksum: "
"%.2x%.2x%.2x%.2x"
"%.2x%.2x%.2x%.2x"
"%.2x%.2x%.2x%.2x"
"%.2x%.2x%.2x%.2x"
"\n",
header.checksum[0], header.checksum[1], header.checksum[2], header.checksum[3],
header.checksum[4], header.checksum[5], header.checksum[6], header.checksum[7],
header.checksum[8], header.checksum[9], header.checksum[10], header.checksum[11],
header.checksum[12], header.checksum[13], header.checksum[14], header.checksum[15]
);
// verify checksum
unsigned char verify[CHECKSUM_SIZE];
int ret = getchecksum(f, verify, &header);
if(0 != ret)
{
fclose(f);
return ret;
}
if(0 == memcmp(header.checksum, verify, sizeof(verify)))
{
printf("checksum ok!\n");
}
else
{
printf(
"mismatch: "
"%.2x%.2x%.2x%.2x"
"%.2x%.2x%.2x%.2x"
"%.2x%.2x%.2x%.2x"
"%.2x%.2x%.2x%.2x"
"\n",
verify[0], verify[1], verify[2], verify[3],
verify[4], verify[5], verify[6], verify[7],
verify[8], verify[9], verify[10], verify[11],
verify[12], verify[13], verify[14], verify[15]
);
}
// file size
if(0 != fseek(f, 0, SEEK_END))
{
fclose(f);
return err_fileseek(0);
}
int fsize = ftell(f);
if(fsize != header.size)
printf(
"WARNING: firmware size %d %s file size %d\n",
header.size.get(),
((header.size < fsize)? "<" : ">"),
fsize
);
fclose(f);
return 0;
}
int fwextractheader(const char *fname, const HEADER *header)
{
char *dname = changeext(fname, "header");
if(0 == dname) return err_mem();
printf("saving %s\n", dname);
if(exists(dname))
{
free(dname);
return err_fileexists();
}
FILE *d = fopen(dname, "w+b");
if(0 == d)
{
free(dname);
return err_filecreate();
}
if(fwrite(header, 1, sizeof(HEADER), d) < sizeof(HEADER))
{
fclose(d);
free(dname);
return err_filewrite();
}
fclose(d);
free(dname);
return 0;
}
int fwextractpart(const char *fname, FILE *f, const PARTITION *part, const char *pname)
{
char *dname = changeext(fname, pname);
if(0 == dname) return err_mem();
printf("saving %s\n", dname);
if(0 != strcmp(pname, part->name))
printf("WARNING: partition name is not %s\n", pname);
int size = part->size;
if(size <= 0)
{
free(dname);
printf("WARNING: empty partition\n");
return ERR_PARTITION;
}
if(0 != fseek(f, part->offset, SEEK_SET))
{
free(dname);
return err_fileseek(part->offset);
}
if(exists(dname))
{
free(dname);
return err_fileexists();
}
FILE *d = fopen(dname, "w+b");
if(0 == d)
{
free(dname);
return err_filecreate();
}
unsigned char buf[BUFSIZE];
clearerr(f);
while(size > 0)
{
int r = (size > sizeof(buf))? sizeof(buf) : size;
size -= r;
if(fread(buf, 1, r, f) < r)
{
fclose(d);
free(dname);
return err_fileread();
}
if(fwrite(buf, 1, r, d) < r)
{
fclose(d);
free(dname);
return err_filewrite();
}
}
fclose(d);
free(dname);
return 0;
}
int fwextract(const char *fname)
{
printf("extracting %s\n", fname);
FILE *f = fopen(fname, "rb");
if(0 == f) return err_fileopen();
// load header
HEADER header;
clearerr(f);
if(fread(&header, 1, sizeof(header), f) < sizeof(header))
{
if(ferror(f))
{
fclose(f);
return err_fileread();
}
fclose(f);
return err_header();
}
// verify checksum
unsigned char verify[CHECKSUM_SIZE];
if(
(0 != getchecksum(f, verify, &header)) ||
(0 != memcmp(header.checksum, verify, sizeof(verify)))
)
{
printf("WARNING: checksum mismatch!\n");
if(!FORCE)
{
fclose(f);
printf("use -F to ignore.\n");
return ERR_CHECKSUM;
}
}
int ret = 0, r;
// header
r = fwextractheader(fname, &header);
if(0 != r) ret = r;
// partitions
const char *pnames[] = {
"kernel",
"fs",
};
PARTITION *part = &(header.partition[0]);
for(int n=0; n<2; ++n)
{
r = fwextractpart(fname, f, part++, pnames[n]);
if(0 != r) ret = r;
}
fclose(f);
if(0 == ret) printf("done.\n");
return ret;
}
int partcopy(PARTITION *part, FILE *src, FILE *dst)
{
if(0 != fseek(src, part->offset, SEEK_SET))
return err_fileseek(part->offset);
unsigned char buf[BUFSIZE];
int size = part->size;
while(size > 0)
{
int r = (size > sizeof(buf))? sizeof(buf) : size;
size -= r;
if(fread(buf, 1, r, src) < r)
return err_fileread();
if(fwrite(buf, 1, r, dst) < r)
return err_filewrite();
}
return 0;
}
int partimport(PARTITION *part, const char *fpart, FILE *dst)
{
FILE *f = fopen(fpart, "rb");
if(0 == f) return err_fileopen();
unsigned char buf[BUFSIZE];
int size = 0;
clearerr(f);
while(!feof(f))
{
int r = fread(buf, 1, sizeof(buf), f);
if((r < sizeof(buf)) && ferror(f))
{
fclose(f);
return err_fileread();
}
if(fwrite(buf, 1, r, dst) < r)
{
fclose(f);
return err_filewrite();
}
size += r;
}
fclose(f);
part->size = size;
return 0;
}
int updatesquashfs(FILE *f, int offset)
{
SQUASHFS_SUPERBLOCK buf;
if(0 != fseek(f, offset, SEEK_SET))
return err_fileseek(offset);
if(fread(&buf, 1, sizeof(buf), f) < sizeof(buf))
return err_fileread();
if((SQUASHFS_MAGIC_SWAP == buf.s_magic) &&
(ZLIB_COMPRESSION == buf.compression))
return 0;
if((SQUASHFS_MAGIC_SWAP != buf.s_magic) &&
(SQUASHFS_MAGIC != buf.s_magic))
{
printf("ERROR: bad squashfs magic\n");
return ERR_PARTITION;
}
buf.s_magic = SQUASHFS_MAGIC_SWAP;
if((ZLIB_COMPRESSION != buf.compression) &&
(LZMA_COMPRESSION != buf.compression))
{
printf("ERROR: bad squashfs compression\n");
return ERR_PARTITION;
}
buf.compression = ZLIB_COMPRESSION;
if(0 != fseek(f, offset, SEEK_SET))
return err_fileseek(offset);
if(fwrite(&buf, 1, sizeof(buf), f) < sizeof(buf))
return err_filewrite();
return 0;
}
int fwupdate(
const char *fname,
const char *kernel,
const char *fs,
const char *releasename,
const char *out,
bool keep
)
{
FILE *f = fopen(fname, "rb");
if(0 == f) return err_fileopen();
// load header
HEADER header;
clearerr(f);
if(fread(&header, 1, sizeof(header), f) < sizeof(header))
{
if(ferror(f))
{
fclose(f);
return err_fileread();
}
fclose(f);
return err_header();
}
// verify checksum
unsigned char verify[CHECKSUM_SIZE];
if(
(0 != getchecksum(f, verify, &header)) ||
(0 != memcmp(header.checksum, verify, sizeof(verify)))
)
{
printf("WARNING: checksum mismatch!\n");
if(!FORCE)
{
fclose(f);
printf("use -F to ignore.\n");
return ERR_CHECKSUM;
}
}
// prodcode
if(header.prodcode != PRODCODE)
{
printf("WARNING: prodcode is not 0x%X\n", PRODCODE);
if(!FORCE)
{
fclose(f);
printf("use -F to ignore.\n");
return ERR_CHECKSUM;
}
}
// check partitions
const char *pnames[] = {
"kernel",
"fs",
};
const char *pname;
PARTITION *part = &(header.partition[0]);
for(int n=0; n<2; ++n)
{
pname = pnames[n];
if(0 != strcmp(pname, part->name))
{
printf("WARNING: partition %d name is not %s\n", n, pname);
if(!FORCE)
{
fclose(f);
printf("use -F to ignore.\n");
return ERR_PARTITION;
}
}
++part;
}
if((0 != part->name[0]) || (0 != part->size))
{
printf("ERROR: unknown partition\n");
fclose(f);
return ERR_PARTITION;
}
// out
printf("file: %s\n", out);
// header
memset(header.checksum, 0, sizeof(header.checksum));
if(!keep)
{
header.bootorder = 0;
header.bootstatus = 0xffffffff;
// date
time_t rawtime;
tm *ti;
time(&rawtime);
ti = localtime(&rawtime);
sprintf(
header.date,
"%.4d-%.2d-%.2d-%.2d:%.2d:%.2d",
ti->tm_year+1900,
ti->tm_mon+1,
ti->tm_mday,
ti->tm_hour,
ti->tm_min,
ti->tm_sec
);
// fname
memset(header.filename, 0, sizeof(header.filename));
strncpy(header.filename, basename(out), sizeof(header.filename));
}
header.size = sizeof(HEADER);
// releasename
if((0 != releasename) && (0 != *releasename))
{
memset(header.releasename, 0, sizeof(header.releasename));
strncpy(header.releasename, releasename, sizeof(header.releasename)-1);
if(strlen(header.releasename) < strlen(releasename))
printf("WARNING: releasename truncated: %s\n", header.releasename);
}
if(exists(out))
{
fclose(f);
return err_fileexists();
}
// create dest
FILE *d = fopen(out, "w+b");
if(0 == d)
{
fclose(f);
return err_filecreate();
}
// write header
if(fwrite(&header, 1, sizeof(HEADER), d) < sizeof(HEADER))
{
fclose(f);
fclose(d);
return err_filewrite();
}
int r;
// partitions
const char *pfiles[] = {
kernel,
fs
};
const char *pfile;
part = &(header.partition[0]);
for(int n=0; n<2; ++n, ++part)
{
pname = pnames[n];
pfile = pfiles[n];
if(0 != pfile)
{
printf("importing %s\n", pname);
r = partimport(part, pfile, d);
}
else
{
printf("copying %s\n", pname);
r = partcopy(part, f, d);
}
if(0 != r)
{
fclose(f);
fclose(d);
return r;
}
if(part->size <= 0)
{
printf("ERROR: %s partition empty\n", pname);
fclose(f);
fclose(d);
return ERR_PARTITION;
}
part->offset = header.size;
header.size += part->size;
}
// update squashfs
part = &(header.partition[1]);
r = updatesquashfs(d, part->offset);
if(0 != r)
{
fclose(f);
fclose(d);
return r;
}
// update checksum
r = getchecksum(d, verify, &header);
if(0 != r)
{