-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.c
1047 lines (861 loc) · 23.2 KB
/
main.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
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
/*
* imgeditor tools
*
* qianfan Zhao 2022-09-22
*/
#define _GNU_SOURCE /* for memmem */
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <getopt.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <dirent.h>
#include <dlfcn.h>
#include "imgeditor.h"
#include "string_helper.h"
#include "structure.h"
#include "minilzo.h"
#include "gd_private.h"
static struct imgeditor *get_imgeditor_by_private_data(void *p);
static LIST_HEAD(registed_plugins);
static int imgeditor_filter_plugin(const char *name)
{
size_t len = strlen(name);
if (len > strlen(".so") && !strcmp(name + len - strlen(".so"), ".so"))
return 0;
return -1;
}
static int imgeditor_load_plugin(const char *path)
{
DIR *dirp = opendir(path);
if (!dirp)
return 0;
while (1) {
struct imgeditor_plugin *plugin = NULL;
struct dirent *d = readdir(dirp);
char filename[512];
void *dl;
if (!d)
break;
snprintf(filename, sizeof(filename), "%s/%s", path, d->d_name);
if (d->d_type == DT_DIR) {
int ret;
if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
continue;
ret = imgeditor_load_plugin(filename);
if (ret < 0)
return ret;
}
if (d->d_type != DT_REG || imgeditor_filter_plugin(d->d_name) < 0)
continue;
/* dlopen will run the construct function defined in so */
dl = dlopen(filename, RTLD_LAZY);
if (!dl) {
fprintf(stderr, "Error: dlopen %s failed(%s)\n",
filename, dlerror());
continue;
}
plugin = dlsym(dl, "imgeditor_plugin");
if (!plugin) {
fprintf(stderr, "Error: no imgeditor_plugin defined\n");
continue;
}
if (plugin->plugin_version != IMGEDITOR_PLUGIN_STRUCT_VERSION) {
fprintf(stderr, "Error: plugin %s version doesn't match\n",
filename);
continue;
}
plugin->dl = dl;
list_init(&plugin->head);
snprintf(plugin->path, sizeof(plugin->path), "%s", filename);
for (int i = 0; plugin->editors[i]; i++) {
struct imgeditor *editor = plugin->editors[i];
register_imgeditor(editor);
}
list_add_tail(&plugin->head, ®isted_plugins);
}
closedir(dirp);
return 0;
}
static LIST_HEAD(registed_imgeditor_lists);
static int editor_prepare_private_data(struct imgeditor *editor)
{
if (!editor->private_data) {
/* used as an ID,
* to search the imgeditor by this private data in
* @get_imgeditor_by_private_data.
*/
size_t pdata_size = 4;
if (editor->private_data_size)
pdata_size = editor->private_data_size;
/* alloc private data first */
editor->private_data = malloc(pdata_size);
if (!editor->private_data) {
fprintf(stderr, "Error: alloc %zu bytes private data for %s failed\n",
editor->private_data_size, editor->name);
return -1;
}
memset(editor->private_data, 0, editor->private_data_size);
}
return 0;
}
static int editor_init(struct imgeditor *editor)
{
int ret = 0;
if (editor->init) {
ret = editor->init(editor->private_data);
if (ret < 0)
fprintf(stderr, "Error: init %s failed\n", editor->name);
}
return ret;
}
static int empty_rw_file(char *name, size_t namesz)
{
snprintf(name, namesz, "/tmp/imagewtyXXXXXX");
return mkstemp(name);
}
static int imgeditor_unpack2fd_helper(struct imgeditor *editor, int fd,
int fd_out, int argc, char **argv)
{
char tmpfile[128];
int ret, fd_tmp;
fd_tmp = empty_rw_file(tmpfile, sizeof(tmpfile));
if (fd_tmp < 0) {
fprintf(stderr, "Error: create tmp file failed\n");
return fd_tmp;
}
/* unpack and copy to the new file */
ret = editor->unpack(editor->private_data, fd, tmpfile, argc, argv);
if (ret == 0)
dd64(fd_tmp, fd_out, 0, 0, filelength(fd_tmp), NULL, NULL);
close(fd_tmp);
unlink(tmpfile);
return ret;
}
static int imgeditor_unpack_helper(struct imgeditor *editor, int fd,
const char *fileout, int argc, char **argv)
{
int fd_out = fileopen(fileout, O_RDWR | O_CREAT | O_TRUNC, 0664);
int ret;
if (fd_out < 0)
return fd_out;
ret = editor->unpack2fd(editor->private_data, fd, fd_out, argc, argv);
close(fd_out);
return ret;
}
static int imgeditor_default_unpack2fd(void *private_data, int fd, int fd_out,
int argc, char **argv)
{
struct imgeditor *editor = get_imgeditor_by_private_data(private_data);
if (!editor) {
fprintf(stderr, "Error: %s can't find matched editor\n",
__func__);
abort();
}
return imgeditor_unpack2fd_helper(editor, fd, fd_out, argc, argv);
}
static int imgeditor_default_unpack(void *private_data, int fd,
const char *outfile, int argc, char **argv)
{
struct imgeditor *editor = get_imgeditor_by_private_data(private_data);
if (!editor) {
fprintf(stderr, "Error: %s can't find matched editor\n",
__func__);
abort();
}
return imgeditor_unpack_helper(editor, fd, outfile, argc, argv);
}
void register_imgeditor(struct imgeditor *editor)
{
list_init(&editor->head);
if (!editor_prepare_private_data(editor)) {
int has_unpack = !!editor->unpack, has_unpack2fd = !!editor->unpack2fd;
if (!(editor->flags & IMGEDITOR_FLAG_CONTAIN_MULTI_BIN)) {
if (!editor->unpack2fd && has_unpack)
editor->unpack2fd = imgeditor_default_unpack2fd;
if (!editor->unpack && has_unpack2fd)
editor->unpack = imgeditor_default_unpack;
}
if (!editor_init(editor))
list_add_tail(&editor->head, ®isted_imgeditor_lists);
else
free(editor->private_data);
}
}
static void editor_exit(struct imgeditor *editor)
{
if (editor->private_data) {
if (editor->exit)
editor->exit(editor->private_data);
free(editor->private_data);
editor->private_data = NULL;
}
}
static struct imgeditor *get_imgeditor_byname(const char *name)
{
struct imgeditor *editor;
list_for_each_entry(editor, ®isted_imgeditor_lists, head,
struct imgeditor) {
if (!strcmp(editor->name, name))
return editor;
}
return NULL;
}
static struct imgeditor *get_imgeditor_by_private_data(void *p)
{
struct imgeditor *editor;
list_for_each_entry(editor, ®isted_imgeditor_lists, head,
struct imgeditor) {
if (editor->private_data && editor->private_data == p)
return editor;
}
return NULL;
}
static void imgeditor_export_registed(void)
{
struct imgeditor *editor;
list_for_each_entry(editor, ®isted_imgeditor_lists, head,
struct imgeditor) {
gd_export_imgeditor(editor);
}
}
struct img_location {
const char *name;
int64_t offset;
char summary[1024];
};
static int img_location_compare(const void *p1, const void *p2)
{
const struct img_location *img1 = p1, *img2 = p2;
return img1->offset - img2->offset;
}
static void print_img_location(struct img_location *img)
{
const char *part_type = NULL;
const struct disk_partition *part;
char s_offset[128], s_sector[128];
int has_part = 0;
snprintf(s_offset, sizeof(s_offset),
"0x%08" PRIx64 "(%" PRIu64 ")",
img->offset, img->offset);
snprintf(s_sector, sizeof(s_sector),
"0x%08" PRIx64 "(%" PRIu64 ")",
img->offset / 512,
img->offset / 512);
printf("%-20s %-25s %-25s", img->name, s_offset, s_sector);
part = find_registed_partition(img->offset, &part_type);
if (part) {
char part_info[64];
snprintf(part_info, sizeof(part_info), "%s.%s",
part_type, part->name);
printf(" %-25s", part_info);
has_part = 1;
}
if (img->summary[0] != '\0') {
if (!has_part)
printf(" %-25s", " ");
printf(" %s", img->summary);
}
putchar('\n');
}
static struct img_location *imgeditor_search_buf(int fd, off64_t file_offset,
const void *buf, size_t bufsz,
int *count)
{
struct img_location *imgs = NULL;
int offset0_searched = 0;
int found = 0;
int ready;
do {
struct imgeditor *editor;
ready = 0;
list_for_each_entry(editor, ®isted_imgeditor_lists, head,
struct imgeditor) {
struct imgmagic *sm = &editor->search_magic;
int64_t img_offset, magic_file_offset;
int buf4m_search_offset;
const void *p_magic;
int detect;
int vfd;
/* MBR doesn't has any signature, but we want load it
* to register disk partitions
*/
if (!offset0_searched && file_offset == 0) {
if (!strcmp(editor->name, "mbr")) {
offset0_searched++;
img_offset = 0;
goto detect_it;
}
}
if (sm->magic_sz == 0)
continue;
if (sm->next_search_offset == 0) {
buf4m_search_offset = 0;
} else {
buf4m_search_offset =
sm->next_search_offset - file_offset;
}
if (buf4m_search_offset < 0 ||
buf4m_search_offset >= (int)bufsz)
continue;
p_magic = memmem(buf + buf4m_search_offset,
bufsz - buf4m_search_offset,
sm->magic, sm->magic_sz);
if (!p_magic) {
sm->next_search_offset = file_offset + bufsz;
continue;
}
/* the magic is matched */
++ready;
magic_file_offset = file_offset + (p_magic - buf);
sm->next_search_offset = magic_file_offset + 1;
img_offset = magic_file_offset - sm->magic_offset;
if (get_verbose_level() > 2) {
/* print the raw address of this magic */
printf("? %-18s 0x%" PRIx64 "\n",
editor->name,
img_offset + sm->magic_offset);
}
if (img_offset + (int)editor->header_size > filelength(fd))
continue;
detect_it:
structure_force_endian(STRUCTURE_ENDIAN_FORCE_NONE);
memset(editor->private_data, 0,
editor->private_data_size);
if (editor->init)
editor->init(editor->private_data);
vfd = virtual_file_dup(fd, img_offset);
if (vfd < 0)
continue;
detect = editor->detect(editor->private_data, 0, vfd);
if (detect == 0) {
struct img_location *img;
int r;
imgs = realloc(imgs,
sizeof(*imgs) * (found + 1));
if (!imgs) {
fprintf(stderr, "Error: alloc %d imgs"
" failed\n", found);
found = 0;
break;
}
img = &imgs[found];
memset(img, 0, sizeof(*img));
img->name = editor->name;
img->offset = img_offset;
found++;
if (editor->summary) {
r = editor->summary(editor->private_data,
vfd,
img->summary,
sizeof(img->summary));
if (r != 0)
memset(img->summary, 0,
sizeof(img->summary));
}
/* some driver such as sunxi_package will
* alloc data when @detect,
* we should free those
*/
if (editor->exit)
editor->exit(editor->private_data);
}
/* reset the private data to pervent editor_exit@main
* double free again
*/
memset(editor->private_data, 0, editor->private_data_size);
if (editor->init)
editor->init(editor->private_data);
virtual_file_close(vfd);
}
} while (ready);
*count = found;
return imgs;
}
static int imgeditor_search_foreach(int fd)
{
#define BUF4M_SZ (4 << 20)
uint8_t *buf4m = malloc(BUF4M_SZ);
off64_t offset = filestart(fd);
int loaded, count = 0;
if (!buf4m) {
fprintf(stderr, "Error: alloc buf4m failed\n");
return -1;
}
do {
struct img_location *imgs;
int found = 0;
/* read back: 1024 is OK for all magics */
if (offset > 1024)
offset -= 1024;
lseek64(fd, offset, SEEK_SET);
loaded = read(fd, buf4m, BUF4M_SZ);
if (loaded < 0) {
fprintf(stderr, "Error: read from offset #%" PRIu64
"failed\n", offset);
break;
} else if (loaded == 0) {
break;
}
imgs = imgeditor_search_buf(fd, offset, buf4m, loaded, &found);
offset += loaded;
if (!found)
continue;
if (count == 0 && found) {
/* print the title */
printf("%-20s %-25s %-25s\n",
"NAME", "OFFSET", "SECTOR");
}
qsort(imgs, found, sizeof(*imgs), img_location_compare);
for (int i = 0; i < found; i++)
print_img_location(&imgs[i]);
free(imgs);
count += found;
} while (loaded == BUF4M_SZ);
free(buf4m);
return count;
}
static int imgeditor_search(const char *name, off64_t offset)
{
int fd = virtual_file_open(name, O_RDONLY, 0, offset);
int search_count;
if (fd < 0)
return fd;
if (filelength(fd) <= 0) {
virtual_file_close(fd);
return -1;
}
search_count = imgeditor_search_foreach(fd);
if (search_count <= 0) /* noting is found */
return -1;
return 0;
}
static int imgeditor_list_plugin(void)
{
struct imgeditor_plugin *plugin;
list_for_each_entry(plugin, ®isted_plugins, head,
struct imgeditor_plugin) {
printf("%-20s %-20s %s\n",
plugin->name,
plugin->version,
plugin->path);
}
return 0;
}
static void imgeditor_close_plugin(void)
{
struct imgeditor_plugin *plugin, *next;
/* the memory address of @plugin is from the dl library,
* it will be unaccessable after dlclose it.
* so we use @list_for_each_entry_safe here
*/
list_for_each_entry_safe(plugin, next, ®isted_plugins, head,
struct imgeditor_plugin) {
dlclose(plugin->dl);
}
}
#ifndef CONFIG_IMGEDITOR_PLUGIN_PATH
#define CONFIG_IMGEDITOR_PLUGIN_PATH "/usr/local/bin/imgeditor-plugin"
#endif
static void usage(void)
{
const struct imgeditor *editor;
fprintf(stderr, "imgeditor %s: firmware image edit tools\n", IMGEDITOR_VERSION);
fprintf(stderr, "Usage: imgeditor [OPTIONS] [outfile] [-- SUBOPTIONS]\n");
fprintf(stderr, "\n");
fprintf(stderr, " --offset offset set the offset location\n");
fprintf(stderr, " --sector sector set the offset location in sectors\n");
fprintf(stderr, " --sector-size sz set the sector's size, default is 512\n");
fprintf(stderr, " --peek image peek the binary location in image's @offset/@sector and save it\n");
fprintf(stderr, " --unpack image unpack all\n");
fprintf(stderr, " --pack firmware-dir pack firmwares to a image file\n");
fprintf(stderr, " --type type select the image type\n");
fprintf(stderr, "-s --search search supported images\n");
fprintf(stderr, "-v --verbose: set the verbose mode\n");
fprintf(stderr, " --plugin path set the plugin library's path. Default %s\n", CONFIG_IMGEDITOR_PLUGIN_PATH);
fprintf(stderr, " --list-plugin show all registed plugins\n");
fprintf(stderr, " --disable-plugin disable all plugins\n");
fprintf(stderr, "-h --help show this messages\n");
fprintf(stderr, "\n");
fprintf(stderr, "Available image types:\n");
list_for_each_entry(editor, ®isted_imgeditor_lists, head,
const struct imgeditor) {
fprintf(stderr, " %-20s %s\n",
editor->name, editor->descriptor);
}
}
enum {
ARG_TYPE,
ARG_VERBOSE,
ARG_OFFSET,
ARG_SECTOR,
ARG_SECTOR_SIZE,
ARG_PLUGIN,
ARG_DISABLE_PLUGIN,
ARG_VERSION,
ACTION_LIST_PLUGIN,
ACTION_MAIN,
ACTION_UNPACK,
ACTION_PACK,
ACTION_PEEK,
ACTION_SEARCH = 's',
ACTION_LIST = 'l',
ACTION_HELP = 'h',
};
static struct option imgeditor_options[] = {
/* name has_arg, *flag, val */
{ "type", required_argument, NULL, ARG_TYPE },
{ "offset", required_argument, NULL, ARG_OFFSET },
{ "sector", required_argument, NULL, ARG_SECTOR },
{ "sector-size", required_argument, NULL, ARG_SECTOR_SIZE },
{ "plugin", required_argument, NULL, ARG_PLUGIN },
{ "disable-plugin", no_argument, NULL, ARG_DISABLE_PLUGIN},
{ "list-plugin", no_argument, NULL, ACTION_LIST_PLUGIN},
{ "unpack", required_argument, NULL, ACTION_UNPACK },
{ "pack", required_argument, NULL, ACTION_PACK },
{ "peek", required_argument, NULL, ACTION_PEEK },
{ "search", no_argument, NULL, ACTION_SEARCH },
{ "verbose", no_argument, NULL, ARG_VERBOSE },
{ "help", no_argument, NULL, ACTION_HELP },
{ "version", no_argument, NULL, ARG_VERSION },
{ NULL, 0, NULL, 0 },
};
static int editor_detect(const struct imgeditor *editor, int force_type, int fd)
{
int64_t filesize = filelength(fd);
if (filesize < (long)editor->header_size)
return -1;
/* this image editor can't auto detect, it only be used with '--type' */
if (!editor->detect)
return -1;
return editor->detect(editor->private_data, force_type, fd);
}
static int arg_to_ull(const char *arg, const char *value,
unsigned long long *ull)
{
unsigned long long n;
int err;
n = strict_strtoull(value, 0, &err, NULL);
if (err) {
fprintf(stderr, "Error: bad %s %s\n", arg, value);
return -1;
}
*ull = n;
return 0;
}
static int arg_to_ul(const char *arg, const char *value, unsigned long *ul)
{
unsigned long long n;
int ret;
ret = arg_to_ull(arg, value, &n);
if (ret < 0)
return ret;
*ul = (unsigned long)n;
return 0;
}
static int editor_peek(struct imgeditor *editor, int fd, int64_t total_size,
const char *out_file)
{
int fd_out = fileopen(out_file, O_RDWR | O_TRUNC | O_CREAT, 0664);
if (fd_out < 0)
return fd_out;
dd64(fd, fd_out, 0, 0, total_size, NULL, NULL);
close(fd_out);
return 0;
}
int main(int argc, char *argv[])
{
struct imgeditor *editor = NULL;
struct global_data *gd = NULL;
const char *origin_file = NULL, *out_file = NULL, *type = NULL;
const char *plugin_path = CONFIG_IMGEDITOR_PLUGIN_PATH;
char tmpbuf[1024];
unsigned long long offset = 0;
unsigned long offset_sector = 0, sector_size = 512;
int main_argc = 0, sub_argc = argc;
int search_mode = 0, action = ACTION_LIST; /* default action */
int disable_plugin = 0;
int64_t peek_size = 0;
int fd = -1;
int ret = -1;
lzo_init();
if (imgeditor_core_setup_gd() < 0)
return ret;
gd = imgeditor_get_gd();
/* args after '--' will passed to the subcommand */
for (main_argc = 0; main_argc < argc; main_argc++, sub_argc--) {
if (!strcmp(argv[main_argc], "--")) {
sub_argc--; /* skip '--' */
argv[main_argc] = NULL;
break;
}
}
while (1) {
int option_index = 0;
int c;
c = getopt_long(main_argc, argv, "hvs", imgeditor_options, &option_index);
if (c == -1)
break;
switch (c) {
case ARG_TYPE:
type = optarg;
break;
case ARG_OFFSET:
ret = arg_to_ull("--offset", optarg, &offset);
if (ret < 0)
return ret;
break;
case ARG_SECTOR:
ret = arg_to_ul("--sector", optarg, &offset_sector);
if (ret < 0)
return ret;
break;
case ARG_SECTOR_SIZE:
ret = arg_to_ul("--sector-size", optarg, §or_size);
if (ret < 0)
return ret;
break;
case 'v':
case ARG_VERBOSE:
gd->verbose_level++;
break;
case ARG_PLUGIN:
plugin_path = optarg;
break;
case ARG_DISABLE_PLUGIN:
disable_plugin = 1;
break;
case ACTION_LIST:
case ACTION_PACK:
case ACTION_UNPACK:
case ACTION_PEEK:
origin_file = optarg;
action = c;
break;
case ACTION_SEARCH:
search_mode = 1;
break;
case ACTION_HELP:
usage();
exit(EXIT_SUCCESS);
case ARG_VERSION:
printf("%s\n", IMGEDITOR_VERSION);
exit(EXIT_SUCCESS);
case ACTION_LIST_PLUGIN:
action = c;
break;
default:
usage();
return -1;
}
}
if (!disable_plugin)
imgeditor_load_plugin(plugin_path);
if (main_argc == 1) {
usage();
goto free_gd;
}
if (action == ACTION_LIST_PLUGIN) {
ret = imgeditor_list_plugin();
goto free_gd;
}
if (optind < argc)
out_file = argv[optind];
if (offset_sector)
offset = offset_sector * sector_size;
if (search_mode) {
ret = imgeditor_search(out_file, offset);
goto done;
}
imgeditor_export_registed();
/*
* `imgeditor --type gpt -- xxx` run in main mode
* `imgeditor 1.bin` run in list mode
*/
if (action == ACTION_LIST) {
if (!out_file) {
if (type) {
action = ACTION_MAIN;
} else {
usage();
return -1;
}
} else {
origin_file = out_file;
}
}
switch (action) {
case ACTION_MAIN:
if (sub_argc == 0) {
fprintf(stderr, "Error: no SUBOPTIONS\n");
return -1;
}
editor = get_imgeditor_byname(type);
if (!editor) {
fprintf(stderr, "Error: image type %s is unsupported\n",
type);
return -1;
}
break;
case ACTION_LIST:
case ACTION_UNPACK:
case ACTION_PEEK:
fd = virtual_file_open(origin_file, O_RDONLY, 0, offset);
if (fd < 0)
return fd;
if (type) {
editor = get_imgeditor_byname(type);
if (!editor) {
fprintf(stderr, "Error: image type %s is unsupported\n",
type);
return -1;
}
if (editor_detect(editor, 1, fd) < 0)
goto done;
} else {
int matched = 0;
list_for_each_entry(editor, ®isted_imgeditor_lists,
head, struct imgeditor) {
lseek64(fd, filestart(fd), SEEK_SET);
if (!editor_detect(editor, 0, fd)) {
matched = 1;
break;
}
}
if (!matched) {
fprintf(stderr, "Error: can't detect the file type of %s\n",
origin_file);
goto done;
}
}
if (action == ACTION_PEEK) {
if (!out_file) {
fprintf(stderr, "Error: the output file is not selected\n");
goto done;
}
if (!editor->total_size) {
fprintf(stderr, "Error: can't detect total size with image type %s\n",
editor->name);
goto done;
}
lseek64(fd, filestart(fd), SEEK_SET);
peek_size = editor->total_size(editor->private_data, fd);
if (peek_size < 0) {
fprintf(stderr, "Error: get total size with the image type %s failed\n",
editor->name);
goto done;
}
}
/* recovery fd after detect */
lseek64(fd, filestart(fd), SEEK_SET);
break;
case ACTION_PACK:
if (!out_file) {
fprintf(stderr, "Error: the output file is not selected\n");
goto done;
}
if (!type) {
/* reading the .imgeditor marker from firmware_dir */
snprintf(tmpbuf, sizeof(tmpbuf), "%s/.imgeditor",
origin_file);
fd = open(tmpbuf, O_RDONLY);
if (fd < 0)
return fd;
memset(tmpbuf, 0, sizeof(tmpbuf));
read(fd, tmpbuf, sizeof(tmpbuf));
type = tmpbuf;
close(fd);
}
editor = get_imgeditor_byname(type);
if (!editor) {
fprintf(stderr, "Error: image type %s is unsupported\n",
type);
return -1;
}
/* fd point to the output image now */
fd = virtual_file_open(out_file, O_RDWR | O_CREAT, 0664, 0);
if (fd < 0)
return fd;
/* clear it */
ftruncate(fd, 0);
break;
}
/* A program that scans multiple argument vectors, or rescans the same vector
* more than once, must reinitialize getopt() by resetting optind to 0,
* rather than the traditional value of 1.
*/
optind = 0;
switch (action) {
case ACTION_MAIN:
if (!editor->main) {
fprintf(stderr, "Error: %s doesn't support this mode\n",
type);
} else {
ret = editor->main(editor->private_data,
sub_argc,
&argv[main_argc + 1]);
}
break;
case ACTION_LIST:
if ((editor->flags & IMGEDITOR_FLAG_HIDE_INFO_WHEN_LIST) == 0)
printf("%s: %s\n", editor->name, editor->descriptor);
if (editor->list)
ret = editor->list(editor->private_data, fd,
sub_argc, &argv[main_argc + 1]);
break;
case ACTION_UNPACK:
umask(0);
if (editor->flags & IMGEDITOR_FLAG_CONTAIN_MULTI_BIN) {
/* create a dump directory for saving multi binary */
snprintf(tmpbuf, sizeof(tmpbuf), "%s.dump", origin_file);
mkdir(tmpbuf, 0744);
} else {
if (!out_file) {
fprintf(stderr, "Error: outfile is requested for image type %s\n",
editor->name);
goto done;
}
snprintf(tmpbuf, sizeof(tmpbuf), "%s", out_file);