forked from handsthatstrike/EpsLinNeo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EpsLin_v1.51.c
5300 lines (4386 loc) · 150 KB
/
EpsLin_v1.51.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
/////////////////////////////////////////////////////
//
// EpsLin Neo v1.51
// based on EpsLin (c) Juha Forsten 5 Aug / 2006
// https://sites.google.com/site/epslin4free/
// -------------------------------------------------
// * File utility for Ensoniq samplers (EPS/ASR)
//
// ------------------------------------------------------------------------
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// -------------------------------------------------------------------------
//
// TO COMPILE:
// ===========
//
// Should compile in Linux, macOS, and Windows (Cygwin) just by typing:
//
// gcc EpsLin_v1.51.c -o epslin
//
// In Windows you have to install "fdrawcmd.sys" from
// http://simonowen.com/fdrawcmd/
//
// and get the header file from
// http://simonowen.com/fdrawcmd/fdrawcmd.h
//
// and put it in the same directory as the EpsLin source.
//
// HISTORY
// =======
//
// v9.99: [FUTURE]
// - Optional "allowed" devices list to minimize
// unintentional writes to devices like system
// hard disk etc.. for Format and ImageCopy
// THIS COULD SAVE YOUR DAY (or even two)..!
// - Check mode and only EFE erase/put use the
// 'I' write to check if 's' media type..
// - Use 's' media type in Get and in GetFat/SetFat
//
// vX.XX: [FUTURE]
// - TODO: allow a sector offset to be supplied; needed to
// handle direct access of SCSI2SD-ready SD cards
// - TODO: check for Ensoniq volume signatures prior to any
// non-Format related routines
// - TODO: add recursive mode for GetEFE
// - TODO: create named sub-directories when GetEFE is used
// such as 'root', etc. to hold EFEs on export
//
// v1.51:
// - Fixed error with additional IsEFE checks from v1.47.
// - Entries which can't be exported are now skipped by GetEFE.
//
// v1.50:
// - Added preliminary macOS support, although issues may exist.
//
// v1.47:
// - Added handling for EFEs with "/" in Ensoniq filename.
// - Fixed crash in PutEFE when directory full (39th entry).
// - Improved wildcard/globbing usage, although still not
// 100% case-insensitive under Windows, as is ideal.
// - Null sector no longer used for vanity message, etc.
// - Improved EFE/EFA/INS checking before PutEFE is allowed.
// - Added INS extension support as an alias for EFE.
// - Added Directory and Disk Information mode (read-only).
// - Show usage help when '?' or unknown arguments used.
// - Show usage help when run without arguments rather than
// than showing potential "fdrawcmd.sys" error.
//
// v1.46:
// - [Windows] Cygwin component in non-contiguous portion
// of the GetEFEs routine caused *some* EFEs not to extract
// properly; routine was given an overhaul.
// - Corrected SuperDisk support in PutEFE/GetMedia/EraseEFEs.
// - Removed path from filename when printing report.
//
// v1.45: (forked by Thomas Arashikage)
// - [Windows] Tweaks to help compile under Cygwin.
// - [Windows] Fixed FAT block calculation in GetEFE and GetInfo,
// as values could sometimes be off by one!
// - [Windows] ReadBlock function altered to avoid abort on error.
// - [Windows] Too restrictive permissions on newly created
// files (IMG, EFE, EDE, et al) were lessened.
// - Corrected disk label size and default string.
// - Added ASR-Macro to identified file types.
// - Added SuperDisk (255 block) support for EPS16 and ASR which
// is only applicable with HFEs under floppy emulators.
//
// v1.42:
// - [Windows] Had to optimize the I/O functions involved
// when EFEs are GET/ERASED/PUT from/to media. This increased
// the run time memory consumption, as the whole FAT
// table are loaded to memory in order to avoid
// seeks in CD-ROM (/dev/scd) and to write Iomega
// Zip drives which don't support single byte writes.
// As the load/save time of the FAT table depends on the size
// of the media, it _might_ take quite a long time for some media,
// thus making the get/put/erase slow. As the Zip drive is the only
// device to test this, it happens to take only 2 seconds to
// read+write the table, so it's quite reasonable.
// If you send me a SCSI device, that is very slow for this reason,
// I'll try to optimize the functions more - until that,
// for me it's fast enough.. ;-)
// - Options that accept parameter 'a' (= all), now starts
// at idx 1 (not 0 as previous versions).
// - Added progress info when EFEs are put/get.
//
// v1.41:
// - [Windows] Added support for /dev/scd layer in Cygwin..
// Since the seeks are possible only in 2048 byte multiples
// had to make some mods to ReadBlock etc. functions.
// Minor penalties in performance..
//
// v1.40: NOW PORTED TO WINDOWS!!
// - Uses Simon Owens "fdrawcmd.sys" (http://simonowen.com/fdrawcmd/)
// driver for low-level floppy access. Thanks Simon!
// - Minor differences between Cygwin (Win32) and Linux in command
// line arguments (for ex. Cygwin requires allways parameter for -p)
// - Should support also SCSI devices through Cygwin layer
// - Some minor fixes
//
// v1.37:
// - Fixed nasty bug in dir-argument (-d) parsing, that
// sometimes leads an error "ERROR: Index '0' is not a directory!".
// Hopefully now solved..
// - More investigation had to be done to find out how EPS knows, when to
// load Song (instruments has the mask,that tells what location to load -
// for song there are no such info). It seems that if index is 0, it is
// not counted as a valid song, so no song data is loaded.
// The BankInfo function is modified based on that knowledge.
//
// BTW: You can make deeper dir structures with EPS than banks can handle, so..
// don't do it if you want to use files in those dirs with banks.. :-)
//
// - Minor changes to BankInfo output. Paths starts with '/' and ends with no '/'
//
//
// v1.36:
// - Option to join multifile EFEs to single EFE file
//
// v1.35:
// - Added missing CR,LF,EOF bytes to EFE header (cosmetic)
// - Option to show info from Ensoniq Bank EFE
// - Option to split EFE to multifile parts added
// - Option to print directory listing in compact mode to make the
// parsing for external programs easier.
// - Some code clean-ups...
//
// v1.34:
// - Support for Multi Files (ie. instruments, that don't fit in one floppy)
// - Fixed (new) gcc errors
//
// NOTES!
// ======
//
// * Some _commercial_ EPS/ASR tools corrupts the FAT.. You should avoid
// those programs.. IF you have to use them, please at least check
// the disks and image-files before use with EpsLin 'check' option (-C).
// If you use those corrupted disk/images you may lose data!!
//
// TODO:
// =====
//
// [CYGWIN]:
// !! PutEFE doesn't work without idx number.. It's the Optget that
// doesn't support optional arguments, so we'll wait for proper version...
//
// [GENERAL]
//
// !! Format -fi optimize (now redundancy in allocated fat_block write)
//
// !! MkDir max level.. (Bank can load files only max 11-level deep dirs)
//
// !! Fix MKDIR Kludge in PutEFE...
//
// !! Using Floppy with ReadImage sometimes mess up the FD controller !?
//
// !! Add 'image copy' -feature (ie. make it possible to transfer
// data in SCSI HD <-> ZIP, Zip requires '0x6d' at first byte in disk)
//
// ! WAV to EFE & EFE to WAV conversions.
//
// ! In EPS, the OS has to be in one contiguous part
// * ASR doesn't mind...
//
// - Dir-transfer function (copy files recursively..)
// Useful to transfer whole dir structures from medium to an another.
// (Must take care of changing SCSI-ID in Bank-files ?)
//
// - Erase: Recurse Dirs !!
//
// - Format_n_Write -support (?!)
//
// Uncomment to enter debug state...
// #define DEBUG
#include <ctype.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <libgen.h>
#include <dirent.h>
#ifdef __APPLE__
#include <sys/uio.h> // equivalent of <sys/io.h>
#elif __CYGWIN__
#include <sys/uio.h> // equivalent of <sys/io.h>
#include <windows.h>
#include "fdrawcmd.h" // floppy drive support -- from http://simonowen.com/fdrawcmd/fdrawcmd.h
#else // Linux
#include <sys/io.h>
#include <linux/fd.h> // floppy drive support
#include <linux/fdreg.h> // floppy drive support
#endif
#ifdef __CYGWIN__ // Windows
// do nothing
#else // Linux and macOS
#define O_BINARY 0 // In Windows, open is set to binary mode.
// In Linux, this is not defined so set it to 0
// (this value is ORed, so no harm is done..)
#endif
#ifdef __CYGWIN__
#define VERSION "v1.51 (Beta)"
#elif __APPLE__
#define VERSION "v1.51 (Alpha)"
#else // Linux
#define VERSION "v1.51"
#endif
// This idea is too close to commercial "vanity" spam, so has been reverted to classic 0x6D/0xB6 fill pattern.
// Code only left for illustration purposes.
#ifdef __CYGWIN__
#define FIRST_BLOCK_MESSAGE "Created with EpsLin for Windows"
#else
#define FIRST_BLOCK_MESSAGE "Created with EpsLin for Linux"
#endif
#define BLOCK_SIZE 512 // amount of bytes per block
#define ID_BLOCK 1 // ID block is 1 because block 0 is null (0x6D/0xB6 filler)
#define DISK_LABEL_SIZE 7 // disk label is contained within ID block
#define OS_BLOCK 2 // OS block comes directly after ID block
#define DIR_START_BLOCK 3 // DR area comes directly after OS block
#define DIR_BLOCKS 2 // 1024 bytes for entire directory (DR)
#define DIR_END_BLOCK 4 // DR area consists of blocks 3 and 4
#define EFE_SIZE 26 // each directory entry is 26 bytes
#define MAX_NUM_OF_DIR_ENTRIES 39 // 1024 bytes in directory, so only 39 entries possible...
// ...but zero indexed, so 0~38
#define MAX_DIR_DEPTH 10 // maximum amount of nesting
#define FAT_START_BLOCK 5 // FAT comes directly after DR area, but there is a variable amount
// of FAT blocks; amount depends on capacity of specific volume
#define FAT_ENTRIES_PER_BLK 170 // 170 FAT entries allowed in each FAT block
#define MAX_DISK_SECT 20 // ASR uses 20 sectors per track, EPS/EPS16 uses 10 sectors
#define EPS_IMAGE_SIZE 819200 // size in bytes of EPS/EPS16 800K DD diskettes
#define E16_SD_IMAGE_SIZE 2611200 // size in bytes of EPS16 2550K "SuperDisk" images
#define ASR_IMAGE_SIZE 1638400 // size in bytes of ASR 1600K HD diskettes
#define ASR_SD_IMAGE_SIZE 5222400 // size in bytes of ASR 5100K "SuperDisk" images
// Buffer size for image copy
#define IMAGE_COPY_BUFFER_BLOCKS 100
#define DEFAULT_DISK_LABEL "DISK000" // seven characters max
#define EDE_LABEL "EPS-16 Disk"
#define EDE_SKIP_START 0xA0
#define EDE_SKIP_SIZE 200
#define EDE_ID 0x03
#define EDA_LABEL "ASR-10 Disk"
#define EDA_SKIP_START 0x60
#define EDA_SKIP_SIZE 400
#define EDA_ID 0xCB
// Imagefile types
#define EPS_TYPE 'e'
#define E16_SD_TYPE 's'
#define ASR_SD_TYPE 'S'
#define ASR_TYPE 'a'
#define GKH_TYPE 'g'
#define EDE_TYPE 'E'
#define EDA_TYPE 'A'
#define OTHER_TYPE 'o'
//Modes for Bank Info
#define MODE_EPS 4
#define MODE_E16 23
#define MODE_ASR 30
// OS version info offsets (in EFE)
#define EPS_OS_POS 0x3A8
#define E16_OS_POS 0x390
#define ASR_OS_POS 0x6F2
// Modes
#define NONE 0
#define READ 1
#define WRITE 2
#define GET 3
#define PUT 4
#define ERASE 5
#define MKDIR 6
#define FORMAT 7
#define DIRLIST 8
#define TEST 99
// Print modes
#define HUMAN_READABLE 0
#define COMPUTER_READABLE 1
// Return values
#define ERR -1
#define OK 0
// Error print-out & exit
#ifdef DEBUG
#define EEXIT(args) (fprintf(stderr,"(line: %d) ",__LINE__), fprintf args, exit(ERR))
#else
#define EEXIT(args) (fprintf args, exit(ERR))
#endif
// File permissions
#ifdef __CYGWIN__ // Windows
#define FILE_RIGHTS 0777 // all can read, write and execute
#else // Linux and macOS
#define FILE_RIGHTS 0700 // owner can read, write and execute
#endif
// For 'getopt'
// Next two lines generate "redeclared without dllimport" warnings under CYGWIN, but apparently harmless.
extern char *optarg;
extern int optind, opterr, optopt;
// Datatype for floppy disk access
#ifdef __CYGWIN__
typedef HANDLE FD_HANDLE;
#else // Linux and macOS
typedef int FD_HANDLE;
#endif
#ifdef __CYGWIN__
char* LastError ()
{
static char sz[256];
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), sz, sizeof(sz), NULL);
return GetLastError() ? sz : "no error";
}
#endif
// Temp-file name
static char tmp_file[13] = "EpsLinXXXXXX";
//static char tmp_file[64];
// Declaration of ReadBlocks
int ReadBlocks(char media_type, FD_HANDLE fd, int file, unsigned int start_block,
unsigned int length, unsigned char *buffer);
// Temp-file cleanup -function (called by 'atexit')
static void CleanTmpFile(void) {
unlink(tmp_file);
}
// EPS/ASR-file types
static char *EpsTypes[40]={
// 0 1 2 3 4 5 6 7 8 9
"(empty)","EPS-OS ","DIR/ ","Instr ","EPS-Bnk","EPS-Seq","EPS-Sng","EPS-Sys",".. / ","EPS-Mac",
// 10 11 12 13 14 15 16 17 18 19
" "," "," "," "," "," "," "," "," "," ",
// 20 21 22 23 24 25 26 27 28 29
" "," "," ","E16-Bnk","E16-Eff","E16-Seq","E16-Sng","E16-OS ","ASR-Seq","ASR-Sng",
// 30 31 32 33 34 35 36 37 38 39
"ASR-Bnk","ASR-Trk","ASR-OS ","ASR-Eff","ASR-Mac"," "," "," "," "," ??? ",
};
// 00 (0x00) = Unused/Blank/Empty
// 01 (0x01) = EPS (original/classic) Operating System
// 02 (0x02) = Sub-Directory
// 03 (0x03) = EPS Individual Instrument File
// 04 (0x04) = EPS Bank of Sounds
// 05 (0x05) = EPS Sequence File
// 06 (0x06) = EPS Song File
// 07 (0x07) = EPS System Exclusive File
// 08 (0x08) = Pointer to Parent Directory
// 09 (0x09) = EPS Macro File
// ...missing entries are SD-1/VFX-SD only, so not applicable to ASR/EPS...
// 23 (0x17) = EPS-16 Plus Bank File
// 24 (0x18) = EPS-16 Plus Effect File
// 25 (0x19) = EPS-16 Plus Sequence File
// 26 (0x1A) = EPS-16 Plus Song File
// 27 (0x1B) = EPS-16 Plus Operating System
// 28 (0x1C) = ASR-10 Sequence File
// 29 (0x1D) = ASR-10 Song File
// 30 (0x1E) = ASR-10 Bank File
// 31 (0x1F) = ASR-10 Audio Track
// 32 (0x20) = ASR-10 Operating System
// 33 (0x21) = ASR-10 Effect File
// 34 (0x22) = ASR-10 Macro File
//////////////
// ShowUsage
void ShowUsage()
{
printf("\nEpsLin Neo %s -- open source Ensoniq EPS/ASR file and disk utility \n", VERSION);
printf("==============================================================================\n");
printf("\nUsage: epslin [options] [imagefile or device] [EFE #0] [EFE #1] ... [EFE #N]\n\n");
printf("Options:\n-------- \n\n");
printf(" -r Read image from disk. (Disk type (EPS/ASR) is autodetected!)\n");
printf(" File extension (IMG/EDE/EDA) selects the format.\n\n");
printf(" -w Write image to disk. (Disk type (EPS/ASR) is autodetected!)\n");
printf(" File extension (IMG/GKH/EDE/EDA) selects the format.\n\n");
printf(" -fe,-fa,-fi Format disk/image/device (a=ASR, e=EPS, i=image/device)\n");
printf(" Use '-l' option to define disk label.\n");
printf(" If imagefile doesn't exist, it will be created. \n");
printf(" Image size is then needed as an last argument.\n");
printf(" Size is in bytes ('K' and 'M' can be used as a suffix)\n");
printf(" If EPS/ASR disk image is wanted, use size 'eps' or 'asr'\n");
printf(" instead of numeric values!! See examples..\n\n");
printf(" Examples: 'epslin -fe' : Format EPS floppy\n");
printf(" 'epslin -fa' : Format ASR floppy\n");
printf(" 'epslin -fi epsdisk.img eps' : Create EPS disk image\n");
printf(" 'epslin -fi asrdisk.img asr' : Create ASR disk image\n");
printf(" 'epslin -fi e16supr.img e16sd' : Create EPS16 SuperDisk image\n");
printf(" 'epslin -fi asrsupr.img asrsd' : Create ASR SuperDisk image\n");
printf(" 'epslin -fi MyImage.img' : Format MyImage.img\n");
printf(" NOTE: File exists!\n");
printf(" 'epslin -fi MyCDROM.img 650M' : Create and format\n");
printf(" file size 650MB\n");
printf(" 'epslin -l EpsHD -fi /dev/sda' : Format SCSI disk\n");
printf(" : with label 'EpsHD'\n\n");
printf(" -c Convert imagefile from one format to another.\n");
printf(" Supported conversions:\n");
printf(" .img -> ede (EPS)\n");
printf(" .img -> eda (ASR)\n");
printf(" .gkh -> img (EPS)\n");
printf(" .ede -> img (EPS)\n");
printf(" .eda -> img (ASR)\n");
printf(" Examples: 'epslin -c my_disk.img my_disk.ede'\n\n");
printf(" -g index_list \n");
printf(" Get EFE(s) from image/dev.\n");
printf(" Examples: -g1,2,4 : EFEs 1,2 and 4.\n");
printf(" -g10-20 : from 10 to 20.\n");
printf(" -g10- : from 10 to end.\n");
printf(" -g1,3-5,8 : 1,3 to 5, and 8.\n");
printf(" -ga : All from that dir.\n\n");
#ifdef __CYGWIN__
printf(" -p start_index \n");
printf(" Put EFE(s) to image/dev. The EFE is saved to the\n");
printf(" first empty index found. The search of empty index\n");
printf(" can be set to start at start_index.\n");
printf(" The EFE files must be defined after image file.\n");
printf(" Examples: -p1 EFE_files.efe : Put EFE in the idx 1 or\n");
printf(" first empty index.\n");
printf(" -p1 all : Put all EFEs found in\n");
printf(" the current dir.\n");
printf(" -p0 os_file.efe : Put Operating System\n");
printf(" to index 0.\n\n");
#else // Linux and macOS
printf(" -p [start_index] \n");
printf(" Put EFE(s) to image/dev. The EFE is saved to the\n");
printf(" first empty index found. The search of empty index\n");
printf(" can be set to start at start_index (default=1).\n");
printf(" The EFE files must be defined after image file.\n");
printf(" Examples: -p EFE_files.efe : Put EFE in the first \n");
printf(" empty index.\n");
printf(" -p all : Put all EFEs found in\n");
printf(" the current dir.\n");
printf(" -p0 os_file.efe : Put Operating System\n");
printf(" to index 0.\n\n");
#endif
printf(" -e index_list \n");
printf(" Erase EFE(s) from image/dev.\n\n");
printf(" -D Shows directory and disk information, such as EFE listing and \n");
printf(" amount of used and free space in blocks and bytes. \n");
printf(" Example: -D FMsynths.img \n");
printf(" Example: -D drums.gkh \n\n");
printf(" -d path Directory definition for most operations. \n");
printf(" Use index number for sub-directory. \n");
printf(" Example: 'epslin -d2 -ga DX7stabs.img' \n\n");
printf(" For nested sub-folders, use forward-slashes. \n");
printf(" Example: -d12/4/7 \n\n");
printf(" -m dir_name Make directory.\n\n");
printf(" -C level Check the disk/image. Gives detailed info about the \n");
printf(" low-level technical structure of the disk/image.\n");
printf(" Level can be either 0 or 1.\n\n");
printf(" -s EFE_to_split slice_type\n");
printf(" Split a big EFE into smaller pieces.\n");
printf(" Example: -s big.efe eps : Slices to fit EPS DD size \n");
printf(" Example: -s big.efe asr : Slices to fit ASR HD size \n\n");
printf(" -b bank.efe Bank info. Prints useful(?) inside info about bank EFE \n\n");
printf(" -P Parse-friendly output. Use with GUI/frontend software \n\n");
printf("image_file = Ensoniq EPS/EPS16/ASR-type disk image file \n\n");
}
///////////////
// ParseRange
void ParseRange(char *optarg, char process_EFE[MAX_NUM_OF_DIR_ENTRIES])
{
size_t ssize;
char tmp_str[80];
int from_EFE,to_EFE,i;
strcpy(tmp_str,optarg);
ssize = strspn(tmp_str,"0123456789");
tmp_str[ssize]='\0';
from_EFE= atoi(tmp_str);
if(strlen(optarg)== ssize+1) {
to_EFE=MAX_NUM_OF_DIR_ENTRIES-1;
} else {
to_EFE= atoi(tmp_str+ssize+1);
}
// Mark the range of EFEs
for(i=from_EFE;i<=to_EFE;i++) process_EFE[i]=1;
}
////////////////
// ParseList
void ParseList(char *optarg, char process_EFE[MAX_NUM_OF_DIR_ENTRIES])
{
size_t ssize;
char tmp_str[80];
int j;
strcpy(tmp_str,optarg);
j=0;
// Slice the argument using ',' as a separator.
while((ssize = strspn(tmp_str+j,"0123456789-")) != 0) {
tmp_str[ssize+j]='\0';
// List element is 'range'
if((index(tmp_str+j,'-')) != NULL) {
ParseRange(tmp_str+j,process_EFE);
} else {
process_EFE[atoi(tmp_str+j)] = 1;
}
j=j+(ssize+1);
}
}
/////////////////////////
// ParseEntry
void ParseEntry(char *optarg, char process_EFE[MAX_NUM_OF_DIR_ENTRIES])
{
char *idx_new;
int i;
// All?
if(strcmp(optarg,"a")==0) {
//Mark all
for(i=1;i < MAX_NUM_OF_DIR_ENTRIES; i++) process_EFE[i]=1;
} else {
// List ?
if((idx_new=index(optarg,',')) == NULL) {
// Range ?
if(index(optarg,'-')) {
ParseRange(optarg,process_EFE);
} else {
// One number - Mark it
process_EFE[atoi(optarg)]=1;
}
} else {
ParseList(optarg,process_EFE);
}
}
}
//////////////////////
// ParseDir
int ParseDir(char *dirpath_str, unsigned int *DirPath, unsigned int *subdir_cnt)
{
char *idx_new, *idx, dir_str[80];
// Validity check for 'dirpath_str' ie. can't
// be over 70 characters (even that's too much!)
if(strlen(dirpath_str) > 70) {
EEXIT((stderr,"ERROR: Invalid directory path '%s'.\n",dirpath_str));
}
// Clear the 'dir_str'-buffer so that
// parsing doesn't fail if buffer contains
// character '/' at the "right" place.. (ie. bug fix :-)
dir_str[strlen(dirpath_str)+1]= 0;
// Make a copy of path string
strcpy(dir_str,dirpath_str);
// Check if path starts with '/'
if(dir_str[0] == '/') {
if(dir_str[1] == '\0') return(OK); else dir_str[0]=' ';
}
*subdir_cnt=0;
// Parse 'path'
if(dir_str[strlen(dir_str)-1] != '/') {
dir_str[strlen(dir_str)]='/';
dir_str[strlen(dir_str)+1]='\0';
}
idx= dir_str;
while((idx_new= (char *) index(idx,'/')) != NULL) {
*idx_new= '\0';
DirPath[*subdir_cnt]=atoi(idx);
idx= idx_new+1;
(*subdir_cnt)++;
}
return(OK);
}
////////////////////
// Name to DosName
// ---------------
// Some characters allowed by Ensoniq are not allowed by PC operating systems
// to exist in a filename (such as asterisk, as it is used as a wildcard).
void DosName(register char *dosname, char *name)
{
register char *p;
// Correct "invalid" character(s) within Ensoniq filename using substitution.
strcpy(dosname,name);
while((p=(char *) strchr(dosname,'*'))!=NULL) {
// substitute '#' when '*' was used in Ensoniq file
*p='#';
}
while((p=(char *) strchr(dosname,'/'))!=NULL) {
// substitute '^' when '/' was used in Ensoniq file
*p='^';
}
// Uncomment if 'space' to '_' -conversion needed
// Not generally needed as spaces can exist in Windows filenames when quotes are in use.
/*
while((p=strchr(dosname,' '))!=NULL) {
*p='_';
}
*/
// Add .efe file extension to corrected filename.
strcat(dosname,".efe");
}
///////////////////////////
// Print the Directory List
// ------------------------
// Shows all EFEs stored on disk and how much space is used and free.
// The parameters passed to this function are
void PrintDir(unsigned char EFE[MAX_NUM_OF_DIR_ENTRIES][EFE_SIZE], unsigned int mode,
char process_EFE[MAX_NUM_OF_DIR_ENTRIES], char in_file[FILENAME_MAX],
char media_type, char *DiskLabel, unsigned int free_blks, unsigned int used_blks,
int printmode)
{
unsigned int size, cont, start;
unsigned int type, real_type, j, k;
char name[13],dosname[17];
char media[FILENAME_MAX];
char type_text[9];
// Create variable to hold just the filename without path.
char *bname;
// Use 'basename' to return just the filename of the input file.
bname = basename(in_file);
switch (media_type)
{
case 'f':
strcpy(media,"FILE: ");
strcat(media,bname);
break;
case 'e':
strcpy(media,"EPS DISK ");
//strcat(media,DiskLabel);
break;
case 'a':
strcpy(media,"ASR DISK ");
//strcat(media,DiskLabel);
break;
}
// Print DirectoryList
// ===================
if(printmode== HUMAN_READABLE) {
printf("\n------------------------------------------+---------------------------------+\n");
printf(" Disk Label: %-28s |", DiskLabel);
printf(" %-31s |\n", media);
printf("------+----------+--------------+---------+------------------+--------------+\n");
printf(" Idx |Type | Name |Blocks | FileName | FileSize |\n");
printf("------+----------+--------------+---------+------------------+--------------+\n");
} else {
//Computer readable: Label,media, used block, used bytes, free blocks, free bytes
printf("%s,%s,%d,%ld,%d,%ld\n",DiskLabel,media, used_blks, (unsigned long) (used_blks)*512, free_blks, (unsigned long) (free_blks)*512);
}
for(j=0;j<MAX_NUM_OF_DIR_ENTRIES;j++){
//Name
for(k=0;k<12;k++) {
name[k]=EFE[j][k+2];
}
name[12]=0;
DosName(dosname,name);
size =(unsigned int) ((EFE[j][14] << 8) + EFE[j][15]);
cont =(unsigned int) ((EFE[j][16] << 8) + EFE[j][17]);
start=(unsigned long) ((EFE[j][18] << 24)+ (EFE[j][19] << 16) +
(EFE[j][20] << 8 ) + EFE[j][21]);
type=EFE[j][1];
real_type=type;
// Check If type in unknown
if(type>39) {
type=39;
}
//if(1) {
if(type!=0) {
if(printmode == HUMAN_READABLE) {
if(process_EFE[j] == 1) {
switch(mode)
{
case GET:
printf("<-");
break;
case PUT:
case MKDIR:
printf("->");
break;
case ERASE:
printf("!!");
break;
default:
break;
}
} else {
printf(" ");
}
}
if(printmode == HUMAN_READABLE) {
if((type==2) || (type==8)) {
printf(" %02d | %s | %-12s | | | |\n",j,EpsTypes[type],name);
} else {
if(type!=0) {
strcpy(type_text,EpsTypes[type]);
if( EFE[j][22] != 0) {
type_text[5]='\0';
sprintf(type_text,"%s(%2d)",type_text,EFE[j][22]);
printf(" %02d | %s| %-12s | %7d | %s | %12ld |\n",j,type_text,name ,size,dosname, (unsigned long) (size+1)*512);
} else {
printf(" %02d | %s | %-12s | %7d | %s | %12ld |\n",j,type_text,name ,size,dosname, (unsigned long) (size+1)*512);
}
}
} // else
} else {
// Computer readable
printf("%d,%s,%d,%s,%d,%d,%s,%ld\n",j, EpsTypes[type], real_type, name,EFE[j][22], size, dosname, (unsigned long) (size+1)*512);
}
// This line is for debugging. Uncomment it if needed
//printf(" %02d | %s(%d) | %-12s | %4d | start=%d(%x) , cont=%d(%x) |\n",j,EpsTypes[type],real_type,name ,size, start,start,cont,cont);
}
//else if (printmode == COMPUTER_READABLE) {
// If empty dir entry in this index, print "null" line for easier computer parsing...
// printf("%d,%s,%d,%s,%d,%d,%s,%ld\n",j, "(empty)",0 ,"", 0,0,"",0);
//}
}
if(printmode == HUMAN_READABLE) {
printf("------+----------+--------------+---------+---------------------------------+\n");
printf(" Used: %23d Blocks | Used: %16ld Bytes |\n", used_blks, (unsigned long) (used_blks)*512);
printf(" Free: %23d Blocks | Free: %16ld Bytes |\n", free_blks, (unsigned long) (free_blks)*512);
printf("----------------------------------------------------------------------------+\n");
//printf(" Total: %23d Blocks | Total:%16ld Bytes |\n", free_blks+used_blks, (unsigned long) (free_blks+used_blks)*512);
//printf("----------------------------------------------------------------------------+\n\n");
}
}
/////////////////////////////////////
// PrintBankInfo
// =============
// - Prints the structure of bank including
// instruments and songs.
int PrintBankInfo(char filename[FILENAME_MAX], int printmode)
{
int mode,count;
int instValid[8];
int effectFound=0;
int fd;
int i,j,k;
unsigned char data[1024];
if(printmode == COMPUTER_READABLE) {
// COMPUTER READABLE
//Open & Read
if( (fd=open(filename, O_RDONLY | O_BINARY, 0)) <= 0) {
printf("1\n");
return(1);
}
if((count=read(fd,data,0x222)) < 0x222) {
printf("1\n");
return(1);
}
// Get Bank type
if((data[0x32] != 4) && (data[0x32] != 23) && (data[0x32] != 30)) {
printf("2\n");
return(1);
}
// OK
printf("0\n");
// Bank Type
mode=data[0x32];
printf("%d\n",mode);
// Check Num of Blocks
if(data[0x35] > 3) {
effectFound = 1;
}
// Bank Name
for(i=0x208;i<0x220;i=i+2) {
printf("%c",data[i]);
}
//Mask
printf("\n%d\n",data[0x220]);
instValid[7] = (data[0x220] >> 7) & 0x01;
instValid[6] = (data[0x220] >> 6) & 0x01;
instValid[5] = (data[0x220] >> 5) & 0x01;
instValid[4] = (data[0x220] >> 4) & 0x01;
instValid[3] = (data[0x220] >> 3) & 0x01;
instValid[2] = (data[0x220] >> 2) & 0x01;
instValid[1] = (data[0x220] >> 1) & 0x01;
instValid[0] = data[0x220] & 0x01;
for(j=0;j<9;j++) {
//eps
if(mode==MODE_EPS) {
read(fd,data,16);
} else if(mode==MODE_E16) {
read(fd,data,16);
} else {
read(fd,data,28);
}
// Valid instrument?
if(j<8) {
if(!instValid[j]) {
printf("0\n");
continue;
}
} else {
// Valid song ?
if(data[4] == 0) {
printf("0\n");
continue;
}
}
// Inst/Song info
if(data[0] > 0x7f) {
//Song?
if(j==8) {
printf("0\n");
} else {
// "Copy of" + Inst num
printf("2,%d\n",(data[0] & 0x0f)+1);
}
} else {
//"Valid" + Path Depth
printf("1,%d,",data[0]);
// Device
printf("%d,",data[2]);
//Media Name (if any)
if(mode==MODE_EPS) {
printf("<NONE>,");
} else {
printf("%c%c%c%c%c%c%c,",
data[ 3],
data[ 5],
data[ 7],
data[ 9],
data[11],
data[13],
data[15]);
}
if(data[0] > 0) {
for(k=0;k<data[0];k++) {
printf("/%d",data[ 4+2*k]);
}
printf(",%d\n",data[4+2*k]);
} else {
printf("/,%d\n",data[4]);
}
}
}
if(effectFound) {
lseek(fd,0x800,0);
read(fd,data,64);
printf("1,%c%c%c%c%c%c%c%c%c%c%c%c\n",
data[10],
data[12],
data[14],
data[16],
data[18],
data[20],
data[22],
data[24],
data[26],
data[28],
data[30],
data[32]);
} else {
printf("0\n");
}
} else {
// HUMAN READABLE
if( (fd=open(filename, O_RDONLY | O_BINARY, 0)) <= 0) {
printf("ERROR: File not found!\n");
return(1);
}
if( (count=read(fd,data,0x222)) < 0x222) {
printf("ERROR: File read error!\n");
return(1);
}