-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.c
1728 lines (1459 loc) · 45.1 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
/*
* Copyright (C) 2013 Neil McGill
*
* See the LICENSE file for license.
*/
#include "config.h"
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <libgen.h>
#include <fcntl.h>
#include "main.h"
#include "disk.h"
#include "command.h"
/*
* Global options
*/
boolean opt_verbose;
boolean opt_quiet;
boolean opt_debug;
boolean opt_debug2;
boolean opt_debug3;
boolean opt_debug4;
boolean opt_debug5;
/*
* Most common sector size.
*/
uint32_t opt_sector_size = DEFAULT_SECTOR_SIZE;
/*
* Sectors per cluster.
*/
uint32_t opt_sectors_per_cluster;
/*
* Die and print usage message.
*/
boolean die_with_usage;
/*
* Tool usage.
*/
void usage (void)
{
fprintf(stderr,
"fatdisk, version " VERSION "\n\n"
"fatdisk is a utility that allows you to perform various operations on files\n"
"on a DOS formatted disk image in FAT12,16,32 formats without needing to do any\n"
"mounting of the disk image, or needing root or sudo access.\n"
"\n"
"It can extract files from the DOS disk to the local harddrive, and likewise\n"
"can import files from the local disk back onto the DOS disk. Additionally\n"
"you can do basic operations like list, cat, hexdump etc...\n"
"\n"
"Lastly this tool can also format and partition a disk, setting up the FAT\n"
"filesystem and even copying in a bootloader like grub. This is a bit \n"
"experimental so use with care.\n"
"\n"
"You may specify the partition of the disk the tool is to look for, but it\n"
"will default to partition 0 if not. And if no partition info is found, it\n"
"will do a hunt of the disk to try and find it.\n"
"\n"
"Usage: fatdisk [OPTIONS] disk-image-file [COMMAND]\n"
"\n"
"COMMAND are things like extract, rm, ls, add, info, summary, format.\n"
);
fprintf(stderr, "Options:\n");
fprintf(stderr, " --verbose : print lots of disk info\n");
fprintf(stderr, " -verbose :\n");
fprintf(stderr, " -v :\n");
fprintf(stderr, "\n");
fprintf(stderr, " --quiet : print less info when adding/removing\n");
fprintf(stderr, " -quiet :\n");
fprintf(stderr, " -q :\n");
fprintf(stderr, "\n");
fprintf(stderr, " --debug : print internal debug info\n");
fprintf(stderr, " -debug :\n");
fprintf(stderr, " -d :\n");
fprintf(stderr, " -dd : more debugs\n");
fprintf(stderr, " -ddd : yet more debugs\n");
fprintf(stderr, " -dddd : and still more\n");
fprintf(stderr, " -ddddd : insane amount of debugs\n");
fprintf(stderr, "\n");
fprintf(stderr, " --offset : offset to start of disk\n");
fprintf(stderr, " -offset : e.g. -o 32256, -o 63s -o 0x7e00\n");
fprintf(stderr, " -o :\n");
fprintf(stderr, "\n");
fprintf(stderr, " --partition : partition to use\n");
fprintf(stderr, " -partition :\n");
fprintf(stderr, " -p :\n");
fprintf(stderr, "\n");
fprintf(stderr, " --sectors_per_cluster : sectors per cluster\n");
fprintf(stderr, " -sectors_per_cluster :\n");
fprintf(stderr, " -S :\n");
fprintf(stderr, "\n");
fprintf(stderr, " --sectorsize : default 512\n");
fprintf(stderr, " -sectorsize :\n");
fprintf(stderr, "\n");
fprintf(stderr, " --help : this help\n");
fprintf(stderr, " -help :\n");
fprintf(stderr, " -h :\n");
fprintf(stderr, "\n");
fprintf(stderr, " --version : tool version number\n");
fprintf(stderr, " -version :\n");
fprintf(stderr, " -ver :\n");
fprintf(stderr, "\n");
fprintf(stderr, "Commands:\n");
fprintf(stderr, "\n");
fprintf(stderr, " info : print disk info\n");
fprintf(stderr, " i :\n");
fprintf(stderr, "\n");
fprintf(stderr, " summary : print disk info summary\n");
fprintf(stderr, " sum :\n");
fprintf(stderr, " s :\n");
fprintf(stderr, "\n");
fprintf(stderr, " list <pat> : list a file or dir\n");
fprintf(stderr, " ls <pat> :\n");
fprintf(stderr, " l <pat> :\n");
fprintf(stderr, "\n");
fprintf(stderr, " find <pat> : find and raw list files\n");
fprintf(stderr, " fi <pat> :\n");
fprintf(stderr, " f <pat> :\n");
fprintf(stderr, "\n");
fprintf(stderr, " extract <pat> : extract a file or dir\n");
fprintf(stderr, " x <pat> :\n");
fprintf(stderr, "\n");
fprintf(stderr, " add <pat> : add a file or dir, keeping same\n");
fprintf(stderr, " a <pat> : full pathname on the disk image\n");
fprintf(stderr, "\n");
fprintf(stderr, " fileadd local-name [remote-name]\n");
fprintf(stderr, " : add a file with a different name from source\n");
fprintf(stderr, " f <pat> :\n");
fprintf(stderr, "\n");
fprintf(stderr, " remove <pat> : remove a file or dir\n");
fprintf(stderr, " rm <pat> :\n");
fprintf(stderr, " r <pat> :\n");
fprintf(stderr, "\n");
fprintf(stderr, " hexdump : include hex dump of files\n");
fprintf(stderr, " hex :\n");
fprintf(stderr, " h :\n");
fprintf(stderr, "\n");
fprintf(stderr, " cat : raw dump of file to console\n");
fprintf(stderr, " ca :\n");
fprintf(stderr, " c :\n");
fprintf(stderr, "\n");
fprintf(stderr, " format\n");
fprintf(stderr, " size xG/xM\n");
fprintf(stderr, " [part 0-3] select partiton\n");
fprintf(stderr, " [zero] zero sectors\n");
fprintf(stderr, " [bootloader <file>] install bootloader\n");
fprintf(stderr, " [<disktype>] select filesys type\n");
fprintf(stderr, " where <disktype> is: ");
uint32_t i;
for (i = 0; i < 0xff; i++) {
if (*msdos_get_systype(i)) {
fprintf(stderr, "%s ", msdos_get_systype(i));
}
}
fprintf(stderr, "\n");
fprintf(stderr, "\n");
fprintf(stderr, "Examples:\n");
fprintf(stderr, "\n");
fprintf(stderr, " $ fatdisk mybootdisk ls\n");
fprintf(stderr, " ----daD 0 2013 Jan 02 locale/ LOCALE\n");
fprintf(stderr, " -----aD 18573 2013 Jan 02 ast.mo AST.MO\n");
fprintf(stderr, " ...\n");
fprintf(stderr, "\n");
fprintf(stderr, " $ fatdisk mybootdisk info\n");
fprintf(stderr, "\n");
fprintf(stderr, " $ fatdisk mybootdisk summary\n");
fprintf(stderr, "\n");
fprintf(stderr, " $ fatdisk mybootdisk extract dir\n");
fprintf(stderr, "\t\t\t\t\t-- dumps dir to the local disk\n");
fprintf(stderr, " $ fatdisk mybootdisk rm dir\n");
fprintf(stderr, "\t\t\t\t\t-- recursively remove dir\n");
fprintf(stderr, " $ fatdisk mybootdisk rm dir/*/*.c\n");
fprintf(stderr, "\t\t\t\t\t-- selectively remove files\n");
fprintf(stderr, " $ fatdisk mybootdisk add dir\n");
fprintf(stderr, "\t\t\t\t\t-- recursively add dir to the disk\n");
fprintf(stderr, " $ fatdisk mybootdisk hexdump foo.c\n");
fprintf(stderr, "\t\t\t\t\t-- dump a file from the disk\n");
fprintf(stderr, "\n");
fprintf(stderr, " $ fatdisk mybootdisk format size 1G name MYDISK part 0 50%% \\\n");
fprintf(stderr, " bootloader grub_disk part 1 50%% fat32 bootloader grub_disk\n");
fprintf(stderr, "\n");
fprintf(stderr, "\t\t\t\t\t-- create and format a 1G disk\n");
fprintf(stderr, "\t\t\t\t\t with 2 FAT 32 partitions and grub\n");
fprintf(stderr, "\t\t\t\t\t installed in sector 0 of part 0\n");
fprintf(stderr, "\n");
fprintf(stderr, "Written by Neil McGill, goblinhack@gmail.com, with special thanks\n");
fprintf(stderr, "to Donald Sharp, Andy Dalton and Mike Woods\n");
fprintf(stderr, "\nfatdisk, version " VERSION "\n\n");
}
/*
* fatdisk_version
*
* Version number
*/
static void fatdisk_version (void)
{
fprintf(stderr, "fatdisk, version " VERSION "\n");
}
/*
* Cleanup operations on exit.
*/
void quit (void)
{
static boolean quitting;
if (quitting) {
return;
}
quitting = true;
ptrcheck_fini();
}
void die (void)
{
quit();
exit(1);
}
static disk_t *disk;
static void killed (int sig)
{
if (disk) {
disk_command_close(disk);
disk = 0;
}
exit(2);
}
/*
* command_format
*
* Format and partition a disk.
*/
static void command_format (int32_t argc,
int64_t opt_disk_start_offset,
boolean opt_disk_start_offset_set,
int32_t arg,
char *argv[],
const char *opt_filename)
{
uint64_t opt_disk_bootloader_size[MAX_PARTITON] = {0};
boolean opt_disk_partition_used[MAX_PARTITON] = {0};
uint64_t opt_disk_partition_size[MAX_PARTITON] = {0};
uint32_t opt_disk_sector_start[MAX_PARTITON] = {0};
uint64_t opt_disk_command_format_size = 0;
uint32_t opt_disk_sector_end[MAX_PARTITON] = {0};
char *opt_disk_bootloader[MAX_PARTITON] = {0};
uint8_t opt_disk_os_id_default = DISK_FAT32;
uint8_t opt_disk_os_id[MAX_PARTITON] = {0};
boolean opt_disk_zero[MAX_PARTITON] = {0};
char *opt_disk_command_format_name = 0;
uint32_t last_partition;
uint64_t needed_size;
uint64_t total_size;
uint32_t partition;
uint64_t size;
int32_t i;
partition = 0;
/*
* Parse format args
*/
for (i = arg; i < argc; i++) {
/*
* Size
*/
if (!strcmp(argv[i], "size")) {
if (i + 1 >= argc) {
DIE("no size value");
}
if (strcasestr(argv[i + 1], "0x")) {
opt_disk_command_format_size = strtol(argv[i + 1] + 2, 0, 16);
} else {
opt_disk_command_format_size = strtol(argv[i + 1], 0, 10);
}
if (strcasestr(argv[i + 1], "G")) {
opt_disk_command_format_size *= (uint64_t) 1024 * 1024 * 1024;
} else if (strcasestr(argv[i + 1], "M")) {
opt_disk_command_format_size *= ONE_MEG;
} else if (strcasestr(argv[i + 1], "K")) {
opt_disk_command_format_size *= ONE_K;
} else if (strcasestr(argv[i + 1], "s")) {
opt_disk_command_format_size *= opt_sector_size;
}
i++;
continue;
}
/*
* Name
*/
if (!strcmp(argv[i], "name")) {
if (i + 1 >= argc) {
DIE("no volume name");
}
opt_disk_command_format_name = dupstr(argv[i + 1], __FUNCTION__);
i++;
continue;
}
/*
* Partition option
*/
if (!strcmp(argv[i], "part")) {
if (i + 1 >= argc) {
DIE("no partition value");
}
i++;
if (strcasestr(argv[i], "0x")) {
partition = (uint32_t) strtol(argv[i] + 2, 0, 16);
} else {
partition = (uint32_t) strtol(argv[i], 0, 10);
}
if (partition >= MAX_PARTITON) {
DIE("partition value invalid");
}
opt_disk_partition_used[partition] = true;
continue;
}
/*
* Zero disk sectors
*/
if (!strcmp(argv[i], "zero")) {
opt_disk_zero[partition] = true;
continue;
}
/*
* Bootloader option
*/
if (!strcmp(argv[i], "bootloader") ||
!strcmp(argv[i], "boot") ||
!strcmp(argv[i], "b")) {
if (i + 1 >= argc) {
DIE("no bootloader value");
}
opt_disk_bootloader[partition] = argv[i + 1];
i++;
if (!file_exists(opt_disk_bootloader[partition])) {
DIE("bootloader boot file %s does not exisst",
opt_disk_bootloader[partition]);
}
opt_disk_bootloader_size[partition] =
file_size(opt_disk_bootloader[partition]);
opt_disk_bootloader_size[partition] += opt_sector_size - 1;
opt_disk_bootloader_size[partition] /= opt_sector_size;
if (opt_disk_bootloader_size[partition] < 63) {
opt_disk_bootloader_size[partition] = 63;
}
opt_disk_bootloader_size[partition] *= opt_sector_size;
continue;
}
/*
* Disk type
*/
uint8_t os_id = msdos_parse_systype(argv[i]);
if (os_id != 0xff) {
opt_disk_os_id_default = os_id;
opt_disk_os_id[partition] = opt_disk_os_id_default;
continue;
}
if (strcasestr(argv[i], "0x")) {
size = strtol(argv[i] + 2, 0, 16);
} else {
size = strtol(argv[i], 0, 10);
if (!strcasestr(argv[i], "%")) {
if (size <= 100) {
size = (uint32_t)
(((float)opt_disk_command_format_size / 100) *
(float)size);
}
}
}
if (strcasestr(argv[i], "G")) {
size *= ONE_GIG;
} else if (strcasestr(argv[i], "M")) {
size *= ONE_MEG;
} else if (strcasestr(argv[i], "K")) {
size *= ONE_K;
} else if (strcasestr(argv[i], "s")) {
size *= opt_sector_size;
} else if (strcasestr(argv[i], "%")) {
size = strtol(argv[i], 0, 10);
size = (uint32_t)
(((float)opt_disk_command_format_size / 100) * (float)size);
}
if (size) {
size += opt_sector_size - 1;
size /= opt_sector_size;
size *= opt_sector_size;
opt_disk_partition_size[partition] = size;
continue;
}
/*
* Bad argument.
*/
if (argv[i][0] == '-') {
die_with_usage = true;
DIE("unknown format argument, %s", argv[i]);
}
DIE("unknown format argument, %s", argv[i]);
}
/*
* If we have a device and no size given, get its size.
*/
if (!opt_disk_command_format_size) {
/*
* If the device exists?
*/
opt_disk_command_format_size = file_size(opt_filename);
if (opt_disk_command_format_size < DEFAULT_DISK_SIZE) {
opt_disk_command_format_size = DEFAULT_DISK_SIZE;
}
}
/*
* Still no size? Guess.
*/
if (!opt_disk_command_format_size) {
opt_disk_command_format_size = DEFAULT_DISK_SIZE;
}
DBG4("opt_disk_command_format_size %" PRIu64 " bytes "
"%" PRIu64 "G %" PRIu64 "M %" PRIu64 " sectors",
opt_disk_command_format_size,
opt_disk_command_format_size / ONE_GIG,
opt_disk_command_format_size / ONE_MEG,
opt_disk_command_format_size / opt_sector_size);
for (i = 0; i < MAX_PARTITON; i++) {
DBG3(" Partition size "
" %" PRIu64 " bytes %2.2fG %2.2fM %" PRIu64 " sectors",
opt_disk_partition_size[i],
(float)opt_disk_partition_size[i] / ONE_GIG,
(float)opt_disk_partition_size[i] / ONE_MEG,
opt_disk_partition_size[i] / opt_sector_size);
}
/*
* Sanity checks.
*/
total_size = opt_disk_command_format_size;
last_partition = 0;
needed_size = 0;
/*
* Have at least one partition? If not, make one.
*/
uint32_t total_partitions = 0;
for (i = 0; i < MAX_PARTITON; i++) {
if (opt_disk_partition_used[i]) {
total_partitions++;
if (!opt_disk_os_id[i]) {
opt_disk_os_id[i] = opt_disk_os_id_default;
}
}
}
if (!total_partitions) {
DBG4("Create one partition to span disk");
opt_disk_partition_used[0] = true;
opt_disk_partition_size[0] = opt_disk_command_format_size;
opt_disk_os_id[0] = opt_disk_os_id_default;
for (i = 0; i < MAX_PARTITON; i++) {
DBG3(" Partition size "
" %" PRIu64 " bytes %2.2fG %2.2fM %" PRIu64 " sectors",
opt_disk_partition_size[i],
(float)opt_disk_partition_size[i] / ONE_GIG,
(float)opt_disk_partition_size[i] / ONE_MEG,
opt_disk_partition_size[i] / opt_sector_size);
}
}
/*
* Any without a size? Give them a fraction of what remains.
*/
uint64_t allocated = total_size;
uint32_t unsized_partitions = 0;
DBG4("Check all clusters are allocated for size %" PRIu64 "", allocated);
for (i = 0; i < MAX_PARTITON; i++) {
if (opt_disk_partition_used[i]) {
if (!opt_disk_partition_size[i]) {
unsized_partitions++;
} else {
allocated -= opt_disk_partition_size[i];
}
}
}
DBG4("Remaining unallocated %" PRIu64 ", unsized_partitions %u",
allocated, unsized_partitions);
if (allocated && unsized_partitions) {
DBG4("%u partitions have no size", unsized_partitions);
for (i = 0; i < MAX_PARTITON; i++) {
if (opt_disk_partition_size[i]) {
continue;
}
if (opt_disk_partition_used[i]) {
DBG4("Set unsized partiton %u to size %" PRIu64 "",
i, allocated / unsized_partitions);
opt_disk_partition_size[i] = allocated / unsized_partitions;
}
}
for (i = 0; i < MAX_PARTITON; i++) {
DBG4(" Partition size "
" %" PRIu64 " bytes %2.2fG %2.2fM %" PRIu64 " sectors",
opt_disk_partition_size[i],
(float)opt_disk_partition_size[i] / ONE_GIG,
(float)opt_disk_partition_size[i] / ONE_MEG,
opt_disk_partition_size[i] / opt_sector_size);
}
}
/*
* How big a disk do we need?
*/
for (i = 0; i < MAX_PARTITON; i++) {
if (!opt_disk_partition_size[i]) {
continue;
}
needed_size += opt_disk_partition_size[i];
last_partition = 0;
}
/*
* Check the needed size is not beyond the disk size.
*/
if (needed_size > total_size) {
OUT("The total byte size asked for in partitions, %" PRIu64 ", "
"exceeds total on disk %" PRIu64 ", truncating "
"partition %" PRIu32 " to fit...",
needed_size, total_size, last_partition);
while (needed_size > total_size) {
opt_disk_partition_size[last_partition] -= opt_sector_size;
needed_size -= opt_sector_size;
}
}
uint32_t sector_start = 0;
for (i = 0; i < MAX_PARTITON; i++) {
if (!opt_disk_partition_size[i]) {
continue;
}
opt_disk_sector_start[i] = sector_start;
opt_disk_sector_end[i] =
opt_disk_sector_start[i] +
(uint32_t)
(opt_disk_partition_size[i] / opt_sector_size);
opt_disk_sector_end[i]--;
sector_start += opt_disk_partition_size[i] / opt_sector_size;
}
for (i = 0; i < MAX_PARTITON; i++) {
if (!opt_disk_partition_size[i]) {
continue;
}
if (opt_quiet) {
continue;
}
OUT("Partition %" PRIu32 ":", i);
OUT(" Partition size "
" %" PRIu64 " bytes %2.2fG %2.2fM %" PRIu64 " sectors",
opt_disk_partition_size[i],
(float)opt_disk_partition_size[i] / ONE_GIG,
(float)opt_disk_partition_size[i] / ONE_MEG,
opt_disk_partition_size[i] / opt_sector_size);
OUT(" Sector start %-10" PRIu32
" (0x%" PRIx32 ")",
opt_disk_sector_start[i],
opt_disk_sector_start[i]);
OUT(" Sector end %-10" PRIu32
" (0x%" PRIx32 ")",
opt_disk_sector_end[i],
opt_disk_sector_end[i]);
if (opt_disk_bootloader[i]) {
OUT(" Bootloader boot file %s",
opt_disk_bootloader[i]);
}
if (opt_disk_bootloader_size[i]) {
OUT(" Bootloader size "
" %" PRIu64 " bytes %2.2fG %2.2fM %" PRIu64 " sectors",
opt_disk_bootloader_size[i],
(float)opt_disk_bootloader_size[i] / ONE_GIG,
(float)opt_disk_bootloader_size[i] / ONE_MEG,
opt_disk_bootloader_size[i] / opt_sector_size);
}
OUT(" OS ID %" PRIu32 " (%s)",
opt_disk_os_id[i],
msdos_get_systype(opt_disk_os_id[i]));
}
if (!file_is_special(opt_filename)) {
unlink(opt_filename);
}
if (file_write(opt_filename, 0, 0) < 0) {
DIE("Cannot write to %s", opt_filename);
}
unsigned char tmp = '\0';
if (file_write_at(opt_filename,
opt_disk_command_format_size - 1,
&tmp,
(uint32_t) sizeof(tmp))) {
DIE("Cannot write to end of file of %s", opt_filename);
}
/*
* Format each partition.
*/
for (i = 0; i < MAX_PARTITON; i++) {
if (!opt_disk_partition_size[i]) {
continue;
}
disk = disk_command_format(opt_filename,
i,
opt_disk_start_offset,
opt_disk_start_offset_set,
opt_disk_command_format_size,
opt_disk_command_format_name,
opt_disk_sector_start[i],
opt_disk_sector_end[i],
opt_disk_os_id[i],
opt_disk_zero[i],
opt_disk_bootloader[i],
opt_disk_bootloader_size[i]);
if (!disk) {
DIE("format of partition %" PRIu32 " failed", i);
}
disk_command_close(disk);
disk = 0;
}
}
/*
* command_list
*
* Execute the list command
*/
static uint32_t command_list (int32_t argc, int32_t arg, char *argv[])
{
uint32_t count;
count = 0;
if (arg == argc - 1) {
/*
* List files.
*/
count += disk_command_list(disk, 0);
} else {
for (++arg; arg < argc; arg++) {
/*
* List files.
*/
count += disk_command_list(disk, argv[arg]);
}
}
if (!opt_quiet) {
if (count == 1) {
printf("Listed %" PRIu32 " entry\n", count);
} else {
printf("Listed %" PRIu32 " entries\n", count);
}
}
return (count);
}
/*
* command_find
*
* Execute the find command
*/
static uint32_t command_find (int32_t argc, int32_t arg, char *argv[])
{
uint32_t count;
count = 0;
if (arg == argc - 1) {
/*
* find files.
*/
count += disk_command_find(disk, 0);
} else {
for (++arg; arg < argc; arg++) {
/*
* find files.
*/
count += disk_command_find(disk, argv[arg]);
}
}
if (opt_verbose) {
if (count == 1) {
printf("Found %" PRIu32 " entry\n", count);
} else {
printf("Found %" PRIu32 " entries\n", count);
}
}
return (count);
}
/*
* command_hexdump
*
* Execute the hexdump command
*/
static uint32_t command_hexdump (int32_t argc, int32_t arg, char *argv[])
{
uint32_t count;
count = 0;
if (arg == argc - 1) {
/*
* Dump all files.
*/
count += disk_command_hex_dump(disk, 0);
} else {
for (++arg; arg < argc; arg++) {
/*
* Dump specific files.
*/
count += disk_command_hex_dump(disk, argv[arg]);
}
}
if (!opt_quiet) {
if (count == 1) {
printf("Dumped %" PRIu32 " entry\n", count);
} else {
printf("Dumped %" PRIu32 " entries\n", count);
}
}
return (count);
}
/*
* command_cat
*
* Execute the cat command
*/
static uint32_t command_cat (int32_t argc, int32_t arg, char *argv[])
{
uint32_t count;
count = 0;
if (arg == argc - 1) {
/*
* Dump all files.
*/
count += disk_command_cat(disk, 0);
} else {
for (++arg; arg < argc; arg++) {
/*
* Dump specific files.
*/
count += disk_command_cat(disk, argv[arg]);
}
}
return (count);
}
/*
* command_extract
*
* Execute the extract command
*/
static uint32_t command_extract (int32_t argc, int32_t arg, char *argv[])
{
uint32_t count;
count = 0;
if (arg == argc - 1) {
/*
* Extract all files.
*/
count += disk_command_extract(disk, 0);
} else {
for (++arg; arg < argc; arg++) {
/*
* Extract specific files.
*/
count += disk_command_extract(disk, argv[arg]);
}
}
if (!opt_quiet) {
if (count == 1) {
printf("Extracted %" PRIu32 " entry\n", count);
} else {
printf("Extracted %" PRIu32 " entries\n", count);
}
}
return (count);
}
/*
* command_add
*
* Execute the add command
*/
static uint32_t command_add (int32_t argc, int32_t arg, char *argv[])
{
uint32_t count;
count = 0;
if (arg == argc - 1) {
/*
* Add all files.
*/
count += disk_add(disk, 0, 0);
} else {
for (++arg; arg < argc; arg++) {
/*
* Add specific files.
*/
count += disk_add(disk,
argv[arg] /* source */,
argv[arg] /* target */);
}
}
if (!opt_quiet) {
if (count == 1) {
printf("Added %" PRIu32 " entry\n", count);
} else {
printf("Added %" PRIu32 " entries\n", count);
}
}
return (count);
}
/*
* command_remove
*
* Execute the remove command
*/
static uint32_t command_remove (int32_t argc, int32_t arg, char *argv[])
{
uint32_t count;
count = 0;
if (arg == argc - 1) {
/*
* remove all files.
*/
count += disk_command_remove(disk, 0);
} else {
for (++arg; arg < argc; arg++) {
/*
* remove specific files.
*/
count += disk_command_remove(disk, argv[arg]);
}
}
if (!opt_quiet) {
if (count == 1) {
printf("Removed %" PRIu32 " entry\n", count);
} else {
printf("Removed %" PRIu32 " entries\n", count);
}
}
return (count);
}
/*
* command_fileadd
*
* Execute the fileadd command
*/
static uint32_t command_fileadd (int32_t argc, int32_t arg, char *argv[])
{
uint32_t count;
count = 0;
if (arg == argc - 1) {
/*
* Add all files.
*/
count += disk_addfile(disk, 0, 0);
} else {
for (++arg; arg < argc; arg++) {
/*
* Add specific files.
*/
if (arg + 1 >= argc) {
/*
* Add the full path name source to the basename target if
* we are at the end of the args.
*/
char *b = mybasename(argv[arg], __FUNCTION__);