forked from SDL-Hercules-390/hyperion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcckdutil.c
2550 lines (2258 loc) · 90.5 KB
/
cckdutil.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
/* CCKDUTIL.C (c) Copyright Roger Bowler, 1999-2006 */
/* ESA/390 Compressed CKD Common routines */
/*-------------------------------------------------------------------*/
/* This module contains functions for compressed CKD devices */
/* used by more than 1 main program. */
/*-------------------------------------------------------------------*/
#include "hstdinc.h"
#define _CCKDUTIL_C_
#define _HDASD_DLL_
#include "hercules.h"
#include "opcode.h"
typedef struct _SPCTAB { /* Space table */
OFF_T pos; /* Space offset */
long long len; /* Space length */
long long siz; /* Space size */
int val; /* Value for space */
void *ptr; /* Pointer to recovered space*/
int typ; /* Type of space */
} SPCTAB;
#define SPCTAB_NONE 0 /* Ignore this space entry */
#define SPCTAB_DEVHDR 1 /* Space is device header */
#define SPCTAB_CDEVHDR 2 /* Space is compressed hdr */
#define SPCTAB_L1TAB 3 /* Space is level 1 table */
#define SPCTAB_L2TAB 4 /* Space is level 2 table */
#define SPCTAB_TRKIMG 5 /* Space is track image */
#define SPCTAB_FREE 6 /* Space is free block */
#define SPCTAB_END 7 /* Space is end-of-file */
/*-------------------------------------------------------------------*/
/* Internal functions */
/*-------------------------------------------------------------------*/
int cdsk_spctab_comp(const void *, const void *);
int cdsk_rcvtab_comp(const void *, const void *);
int cdsk_valid_trk (int, BYTE *, int, int, int, char *);
int cdsk_recover_trk (int, BYTE *, int, int, int, int, int, int *);
int cdsk_build_gap (SPCTAB *, int *, SPCTAB *);
int cdsk_build_gap_long (SPCTAB *, int *, SPCTAB *);
static __inline__ void ENDMSG(FILE *,char *,...);
static __inline__ void COMPMSG(FILE *,char *,...);
static __inline__ void CDSKMSG(FILE *,char *,...);
/*-------------------------------------------------------------------*/
/* Static data areas */
/*-------------------------------------------------------------------*/
static BYTE eighthexFF[] = {0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
/*-------------------------------------------------------------------*/
/* Change the endianess of a compressed file */
/*-------------------------------------------------------------------*/
DLL_EXPORT int cckd_swapend (int fd, FILE *m)
{
int rc; /* Return code */
OFF_T rcoff; /* Return value from lseek() */
int i; /* Index */
CCKDDASD_DEVHDR cdevhdr; /* Compressed ckd header */
CCKD_L1ENT *l1; /* Level 1 table */
CCKD_L2ENT l2[256]; /* Level 2 table */
CCKD_FREEBLK fb; /* Free block */
int swapend; /* 1 = New endianess doesn't
match machine endianess */
int n; /* Level 1 table entries */
U32 o; /* Level 2 table offset */
/* fix the compressed ckd header */
rcoff = LSEEK (fd, CKDDASD_DEVHDR_SIZE, SEEK_SET);
if (rcoff == -1)
{
ENDMSG (m, "lseek error fd %d offset %" I64_FMT "d: %s\n",
fd, (long long)CKDDASD_DEVHDR_SIZE, strerror(errno));
return -1;
}
rc = read (fd, &cdevhdr, CCKDDASD_DEVHDR_SIZE);
if (rc != CCKDDASD_DEVHDR_SIZE)
{
ENDMSG (m, "cdevhdr read error fd %d: %s\n",
fd, strerror(errno));
return -1;
}
cckd_swapend_chdr (&cdevhdr);
rcoff = LSEEK (fd, CKDDASD_DEVHDR_SIZE, SEEK_SET);
if (rcoff == -1)
{
ENDMSG (m, "lseek error fd %d offset %" I64_FMT "d: %s\n",
fd, (long long)CKDDASD_DEVHDR_SIZE, strerror(errno));
return -1;
}
cdevhdr.options |= CCKD_ORDWR;
rc = write (fd, &cdevhdr, CCKDDASD_DEVHDR_SIZE);
if (rc != CCKDDASD_DEVHDR_SIZE)
{
ENDMSG (m, "cdevhdr write error fd %d: %s\n",
fd, strerror(errno));
return -1;
}
/* Determine need to swap */
swapend = (cckd_endian() !=
((cdevhdr.options & CCKD_BIGENDIAN) != 0));
/* fix the level 1 table */
n = cdevhdr.numl1tab;
if (swapend) cckd_swapend4((char *)&n);
l1 = malloc (n * CCKD_L1ENT_SIZE);
if (l1 == NULL)
{
ENDMSG (m, "l1tab malloc error fd %d size %ud: %s\n",
fd, (unsigned int)(n * CCKD_L1ENT_SIZE), strerror(errno));
return -1;
}
rcoff = LSEEK (fd, CCKD_L1TAB_POS, SEEK_SET);
if (rcoff == -1)
{
ENDMSG (m, "lseek error fd %d offset %"I64_FMT"d: %s\n",
fd, (U64)CCKD_L1TAB_POS, strerror(errno));
free (l1);
return -1;
}
rc = read (fd, l1, n * CCKD_L1ENT_SIZE);
if (rc != (int)(n * CCKD_L1ENT_SIZE))
{
ENDMSG (m, "l1tab read error fd %d: %s\n",
fd, strerror(errno));
free (l1);
return -1;
}
cckd_swapend_l1 (l1, n);
rcoff = LSEEK (fd, CCKD_L1TAB_POS, SEEK_SET);
if (rcoff == -1)
{
ENDMSG (m, "lseek error fd %d offset %"I64_FMT"d: %s\n",
fd, (U64)CCKD_L1TAB_POS, strerror(errno));
free (l1);
return -1;
}
rc = write (fd, l1, n * CCKD_L1ENT_SIZE);
if (rc != (int)(n * CCKD_L1ENT_SIZE))
{
ENDMSG (m, "l1tab write error fd %d: %s\n",
fd, strerror(errno));
free (l1);
return -1;
}
/* fix the level 2 tables */
for (i=0; i < n; i++)
{
o = l1[i];
if (swapend) cckd_swapend4((char *)&o);
if (o != 0 && o != 0xffffffff)
{
rcoff = LSEEK (fd, (OFF_T)o, SEEK_SET);
if (rcoff == -1)
{
ENDMSG (m, "lseek error fd %d offset %" I64_FMT "d: %s\n",
fd, (long long)o, strerror(errno));
free (l1);
return -1;
}
rc = read (fd, &l2, CCKD_L2TAB_SIZE);
if (rc != CCKD_L2TAB_SIZE)
{
ENDMSG (m, "l2[%d] read error, offset %" I64_FMT "d bytes read %d : %s\n",
i, (long long)o, rc, strerror(errno));
free (l1);
return -1;
}
cckd_swapend_l2 ((CCKD_L2ENT *)&l2);
rcoff = LSEEK (fd, (OFF_T)o, SEEK_SET);
if (rcoff == -1)
{
ENDMSG (m, "lseek error fd %d offset %" I64_FMT "d: %s\n",
fd, (long long)o, strerror(errno));
free (l1);
return -1;
}
rc = write (fd, &l2, CCKD_L2TAB_SIZE);
if (rc != CCKD_L2TAB_SIZE)
{
ENDMSG (m, "l2[%d] write error fd %d (%d): %s\n",
i, fd, rc, strerror(errno));
free (l1);
return -1;
}
}
}
free (l1);
/* fix the free chain */
for (o = cdevhdr.free; o != 0; o = fb.pos)
{
if (swapend) cckd_swapend4 ((char *)&o);
rcoff = LSEEK (fd, o, SEEK_SET);
if (rcoff == -1)
{
ENDMSG (m, "lseek error fd %d offset %" I64_FMT "d: %s\n",
fd, (long long)o, strerror(errno));
return -1;
}
rc = read (fd, &fb, CCKD_FREEBLK_SIZE);
if (rc != CCKD_FREEBLK_SIZE)
{
ENDMSG (m, "free block read error fd %d: %s\n",
fd, strerror(errno));
return -1;
}
cckd_swapend_free (&fb);
rcoff = LSEEK (fd, (OFF_T)o, SEEK_SET);
if (rcoff == -1)
{
ENDMSG (m, "lseek error fd %d offset %" I64_FMT "d: %s\n",
fd, (long long)o, strerror(errno));
return -1;
}
rc = write (fd, &fb, CCKD_FREEBLK_SIZE);
if (rc != CCKD_FREEBLK_SIZE)
{
ENDMSG (m, "free block write error fd %d: %s\n",
fd, strerror(errno));
return -1;
}
}
return 0;
}
/*-------------------------------------------------------------------*/
/* Swap endian - compressed device header */
/*-------------------------------------------------------------------*/
DLL_EXPORT void cckd_swapend_chdr (CCKDDASD_DEVHDR *cdevhdr)
{
/* fix the compressed ckd header */
cdevhdr->options ^= CCKD_BIGENDIAN;
cckd_swapend4 ((char *) &cdevhdr->numl1tab);
cckd_swapend4 ((char *) &cdevhdr->numl2tab);
cckd_swapend4 ((char *) &cdevhdr->size);
cckd_swapend4 ((char *) &cdevhdr->used);
cckd_swapend4 ((char *) &cdevhdr->free);
cckd_swapend4 ((char *) &cdevhdr->free_total);
cckd_swapend4 ((char *) &cdevhdr->free_largest);
cckd_swapend4 ((char *) &cdevhdr->free_number);
cckd_swapend4 ((char *) &cdevhdr->free_imbed);
cckd_swapend2 ((char *) &cdevhdr->compress_parm);
}
/*-------------------------------------------------------------------*/
/* Swap endian - level 1 table */
/*-------------------------------------------------------------------*/
DLL_EXPORT void cckd_swapend_l1 (CCKD_L1ENT *l1, int n)
{
int i; /* Index */
for (i=0; i < n; i++)
cckd_swapend4 ((char *) &l1[i]);
}
/*-------------------------------------------------------------------*/
/* Swap endian - level 2 table */
/*-------------------------------------------------------------------*/
DLL_EXPORT void cckd_swapend_l2 (CCKD_L2ENT *l2)
{
int i; /* Index */
for (i=0; i < 256; i++)
{
cckd_swapend4 ((char *) &l2[i].pos);
cckd_swapend2 ((char *) &l2[i].len);
cckd_swapend2 ((char *) &l2[i].size);
}
}
/*-------------------------------------------------------------------*/
/* Swap endian - free space entry */
/*-------------------------------------------------------------------*/
DLL_EXPORT void cckd_swapend_free (CCKD_FREEBLK *fb)
{
cckd_swapend4 ((char *) &fb->pos);
cckd_swapend4 ((char *) &fb->len);
}
/*-------------------------------------------------------------------*/
/* Swap endian - 4 bytes */
/*-------------------------------------------------------------------*/
DLL_EXPORT void cckd_swapend4 (char *c)
{
char temp[4];
memcpy (&temp, c, 4);
c[0] = temp[3];
c[1] = temp[2];
c[2] = temp[1];
c[3] = temp[0];
}
/*-------------------------------------------------------------------*/
/* Swap endian - 2 bytes */
/*-------------------------------------------------------------------*/
DLL_EXPORT void cckd_swapend2 (char *c)
{
char temp[2];
memcpy (&temp, c, 2);
c[0] = temp[1];
c[1] = temp[0];
}
/*-------------------------------------------------------------------*/
/* Are we little or big endian? From Harbison&Steele. */
/*-------------------------------------------------------------------*/
DLL_EXPORT int cckd_endian()
{
union
{
long l;
char c[sizeof (long)];
} u;
u.l = 1;
return (u.c[sizeof (long) - 1] == 1);
}
/*-------------------------------------------------------------------
* Remove all free space from a compressed ckd file
*-------------------------------------------------------------------*/
int comp_spctab_sort(const void *a, const void *b);
DLL_EXPORT int cckd_comp (int fd, FILE *m)
{
int rc; /* Return code */
OFF_T rc_off; /* Return value from lseek() */
int comp_rc=0; /* Routine return code */
U32 pos; /* Current file position */
int i, j, n; /* Some integers */
int l1size; /* Primary lookup table size */
int errs=0; /* Number of errors */
int moves=0; /* Number of moves */
int moved=0; /* Number bytes moved */
int freed=0; /* Number bytes freed */
int ckddasd; /* 1=CKD dasd 0=FBA dasd */
CKDDASD_DEVHDR devhdr; /* CKD device header */
CCKDDASD_DEVHDR cdevhdr; /* Compressed CKD device hdr */
CCKD_L1ENT *l1=NULL; /* -> Primary lookup table */
CCKD_L2ENT **l2=NULL; /* -> Secondary lookup tables*/
struct {
U32 pos; /* Space offset */
U16 len; /* Space length */
int i, j; /* Space index */
} *spc=NULL; /* -> Space table */
BYTE buf[65536]; /* Buffer */
#ifdef EXTERNALGUI
int extgui2 = (m!= NULL && fileno(m) == fileno(stderr) && extgui);
#endif
comp_restart:
/*-------------------------------------------------------------------
* Read device header
*-------------------------------------------------------------------*/
rc_off = LSEEK (fd, 0, SEEK_SET);
if (rc_off < 0)
{
COMPMSG (m, "lseek error offset 0: %s\n",strerror(errno));
goto comp_err_exit;
}
rc = read (fd, &devhdr, CKDDASD_DEVHDR_SIZE);
if (rc < 0 || rc < (int)CKDDASD_DEVHDR_SIZE)
{
COMPMSG (m, "devhdr read error: %s\n",strerror(errno));
goto comp_err_exit;
}
if (memcmp (devhdr.devid, "CKD_C370", 8) == 0
|| memcmp (devhdr.devid, "CKD_S370", 8) == 0)
ckddasd = 1;
else if (memcmp (devhdr.devid, "FBA_C370", 8) == 0
|| memcmp (devhdr.devid, "FBA_S370", 8) == 0)
ckddasd = 0;
else
{
COMPMSG (m, "incorrect header id\n");
goto comp_err_exit;
}
/*-------------------------------------------------------------------
* Read compressed device header
*-------------------------------------------------------------------*/
rc = read (fd, &cdevhdr, CCKDDASD_DEVHDR_SIZE);
if (rc < 0 || rc < CCKDDASD_DEVHDR_SIZE)
{
COMPMSG (m, "cdevhdr read error: %s\n",strerror(errno));
goto comp_err_exit;
}
cdevhdr.options |= CCKD_ORDWR;
/* Check the endianess of the file */
if (((cdevhdr.options & CCKD_BIGENDIAN) != 0 && cckd_endian() == 0) ||
((cdevhdr.options & CCKD_BIGENDIAN) == 0 && cckd_endian() != 0))
{
rc = cckd_swapend (fd, m);
if (rc < 0) goto comp_err_exit;
goto comp_restart;
}
/*-------------------------------------------------------------------
* Read level 1 table
*-------------------------------------------------------------------*/
l1size = cdevhdr.numl1tab * CCKD_L1ENT_SIZE;
l1 = malloc (l1size);
if (l1 == NULL)
{
COMPMSG (m, "l1 table malloc error: %s\n",strerror(errno));
goto comp_err_exit;
}
rc = read (fd, l1, l1size);
if (rc < 0 || rc < l1size)
{
COMPMSG (m, "l1 table read error: %s\n",strerror(errno));
goto comp_err_exit;
}
/*-------------------------------------------------------------------
* Read level 2 tables
* l2 tables will be relocated immediately after the l1 table
*-------------------------------------------------------------------*/
l2 = calloc (cdevhdr.numl1tab, sizeof (void *));
if (l2 == NULL)
{
COMPMSG (m, "l2 table calloc error: %s\n",strerror(errno));
goto comp_err_exit;
}
pos = CCKD_L1TAB_POS + l1size;
for (i = 0; i < cdevhdr.numl1tab; i++)
{
if (l1[i] == 0 || l1[i] == 0xffffffff)
continue;
l2[i] = malloc (CCKD_L2TAB_SIZE);
if (l2[i] == NULL)
{
COMPMSG (m, "l2[%d] table malloc error: %s\n",
i, strerror(errno));
goto comp_err_exit;
}
rc_off = LSEEK (fd, (OFF_T)l1[i], SEEK_SET);
if (rc_off < 0)
{
COMPMSG (m, "l2[%d] lseek error offset %8.8x: %s\n",
i, l1[i], strerror(errno));
goto comp_err_exit;
}
rc = read (fd, l2[i], CCKD_L2TAB_SIZE);
if (rc < 0 || rc < CCKD_L2TAB_SIZE)
{
COMPMSG (m, "l2[%d] read error %d: %s\n",
i, rc, rc < 0 ? strerror(errno) : "incomplete");
goto comp_err_exit;
}
l1[i] = pos;
pos += CCKD_L2TAB_SIZE;
}
/*-------------------------------------------------------------------
* Relocate any tracks within the area of the relocated l2 tables to
* the end of the file. Also count the number of non-null tracks.
*-------------------------------------------------------------------*/
for (i = n = 0; i < cdevhdr.numl1tab; i++)
{
if (l2[i] == NULL)
continue;
for (j = 0; j < 256; j++)
{
if (l2[i][j].pos == 0 || l2[i][j].pos == 0xffffffff)
continue;
n++;
if (l2[i][j].pos >= pos)
continue;
rc_off = LSEEK (fd, (OFF_T)l2[i][j].pos, SEEK_SET);
if (rc_off < 0)
{
COMPMSG (m, "%s %d lseek error offset %8.8x: %s\n",
ckddasd ? "track" : "blkgrp", (i*256)+j,
l2[i][j].pos, strerror(errno));
goto comp_err_exit;
}
rc = read (fd, buf, l2[i][j].len);
if (rc < 0 || rc < l2[i][j].len)
{
COMPMSG (m, "l2[i] read error %d: %s\n",
i, rc, rc < 0 ? strerror(errno) : "incomplete");
goto comp_err_exit;
}
rc_off = LSEEK (fd, (OFF_T)cdevhdr.size, SEEK_SET);
if (rc_off < 0)
{
COMPMSG (m, "%s %d lseek error offset %8.8x: %s\n",
ckddasd ? "track" : "blkgrp", (i*256)+j,
cdevhdr.size, strerror(errno));
goto comp_err_exit;
}
rc = write (fd, buf, l2[i][j].len);
if (rc < 0 || rc < l2[i][j].len)
{
COMPMSG (m, "l2[i] write error %d: %s\n",
i, rc, rc < 0 ? strerror(errno) : "incomplete");
goto comp_err_exit;
}
l2[i][j].pos = cdevhdr.size;
l2[i][j].size = l2[i][j].len;
cdevhdr.size += l2[i][j].len;
}
}
if (n == 0)
{
COMPMSG (m, "file has no non-null %s\n",
ckddasd ? "tracks" : "blkgrps");
goto comp_exit;
}
/*-------------------------------------------------------------------
* Build space table
*-------------------------------------------------------------------*/
spc = calloc (n, sizeof(spc[0]));
if (spc == NULL)
{
COMPMSG (m, "space table calloc error: %s\n",strerror(errno));
goto comp_err_exit;
}
for (i = n = 0; i < cdevhdr.numl1tab; i++)
{
if (l2[i] == NULL)
continue;
for (j = 0; j < 256; j++)
{
if (l2[i][j].pos == 0 || l2[i][j].pos == 0xffffffff)
continue;
spc[n].pos = l2[i][j].pos;
spc[n].len = l2[i][j].len;
spc[n].i = i;
spc[n++].j = j;
}
}
qsort(spc, n, sizeof(spc[0]), comp_spctab_sort);
/*-------------------------------------------------------------------
* Relocate tracks (or blkgrps)
*-------------------------------------------------------------------*/
for (i = 0; i < n; i++)
{
if (spc[i].pos != pos)
{
rc_off = LSEEK (fd, (OFF_T)spc[i].pos, SEEK_SET);
if (rc_off < 0)
{
COMPMSG (m, "%s %d lseek error offset %8.8x: %s\n",
ckddasd ? "track" : "blkgrp",
(spc[i].i*256)+spc[i].j,
spc[i].pos, strerror(errno));
l2[spc[i].i][spc[i].j].pos = l2[spc[i].i][spc[i].j].len =
l2[spc[i].i][spc[i].j].size = 0;
errs++;
continue;
}
rc = read (fd, buf, spc[i].len);
if (rc < 0 || rc < spc[i].len)
{
COMPMSG (m, "%s %d read error %d: %s\n",
ckddasd ? "track" : "blkgrp",
(spc[i].i*256)+spc[i].j, rc,
rc < 0 ? strerror(errno) : "incomplete");
l2[spc[i].i][spc[i].j].pos = l2[spc[i].i][spc[i].j].len =
l2[spc[i].i][spc[i].j].size = 0;
errs++;
continue;
}
rc_off = LSEEK (fd, (OFF_T)pos, SEEK_SET);
if (rc_off < 0)
{
COMPMSG (m, "%s %d lseek error offset %8.8x: %s\n",
ckddasd ? "track" : "blkgrp",
(spc[i].i*256)+spc[i].j,
pos, strerror(errno));
l2[spc[i].i][spc[i].j].pos = l2[spc[i].i][spc[i].j].len =
l2[spc[i].i][spc[i].j].size = 0;
errs++;
continue;
}
rc = write (fd, buf, spc[i].len);
if (rc < 0 || rc < spc[i].len)
{
COMPMSG (m, "%s %d write error %d: %s\n",
ckddasd ? "track" : "blkgrp",
(spc[i].i*256)+spc[i].j, rc,
rc < 0 ? strerror(errno) : "incomplete");
l2[spc[i].i][spc[i].j].pos = l2[spc[i].i][spc[i].j].len =
l2[spc[i].i][spc[i].j].size = 0;
errs++;
continue;
}
l2[spc[i].i][spc[i].j].pos = pos;
moves++;
moved += spc[i].len;
}
l2[spc[i].i][spc[i].j].size = spc[i].len;
pos += spc[i].len;
}
cdevhdr.size = cdevhdr.used = pos;
FTRUNCATE (fd, (OFF_T)pos);
freed = spc[n-1].pos + spc[n-1].len - pos;
/*-------------------------------------------------------------------
* Write headers and tables
*-------------------------------------------------------------------*/
cdevhdr.free = cdevhdr.free_total = cdevhdr.free_largest =
cdevhdr.free_number = cdevhdr.free_imbed = 0;
rc_off = LSEEK (fd, (OFF_T)CKDDASD_DEVHDR_SIZE, SEEK_SET);
if (rc_off < 0)
{
COMPMSG (m, "cdevhdr lseek error offset %8.8x: %s\n",
CKDDASD_DEVHDR_SIZE, strerror(errno));
goto comp_err_exit;
}
rc = write (fd, &cdevhdr, CCKDDASD_DEVHDR_SIZE);
if (rc < 0 || rc < CCKDDASD_DEVHDR_SIZE)
{
COMPMSG (m, "cdevhdr write error %d: %s\n",
rc, rc < 0 ? strerror(errno) : "incomplete");
goto comp_err_exit;
}
rc = write (fd, l1, l1size);
if (rc < 0 || rc < l1size)
{
COMPMSG (m, "l1 table write error %d: %s\n",
rc, rc < 0 ? strerror(errno) : "incomplete");
goto comp_err_exit;
}
for (i = 0; i < cdevhdr.numl1tab; i++)
{
if (l2[i] == NULL)
continue;
rc = write (fd, l2[i], CCKD_L2TAB_SIZE);
if (rc < 0 || rc < CCKD_L2TAB_SIZE)
{
COMPMSG (m, "l2[%d] table write error %d: %s\n",
i, rc, rc < 0 ? strerror(errno) : "incomplete");
goto comp_err_exit;
}
}
/*-------------------------------------------------------------------
* Free storage and exit
*-------------------------------------------------------------------*/
comp_exit:
if (spc)
free (spc);
if (l2)
{
for (i = 0; i < cdevhdr.numl1tab; i++)
if (l2[i])
free (l2[i]);
free (l2);
}
if (l1)
free (l1);
if (comp_rc == 0)
COMPMSG (m, "completed%s: moves %d moved %d freed %d\n",
errs ? " with errors" : "", moves, moved, freed);
return comp_rc;
/*-------------------------------------------------------------------
* Error exit
*-------------------------------------------------------------------*/
comp_err_exit:
comp_rc = -1;
goto comp_exit;
} /* function cckd_comp */
/*-------------------------------------------------------------------
* Sort space table
*-------------------------------------------------------------------*/
int comp_spctab_sort(const void *a, const void *b)
{
const struct {
U32 pos; /* Space offset */
U16 len; /* Space length */
int i, j; /* Space index */
} *x = a, *y = b; /* Entries to be sorted */
return x->pos < y->pos ? -1 : 1;
} /* end function cckd_comp_sort */
/*-------------------------------------------------------------------*/
/* Perform check function on a compressed ckd file */
/*-------------------------------------------------------------------*/
DLL_EXPORT int cckd_chkdsk(int fd, FILE *m, int level)
{
CKDDASD_DEVHDR devhdr; /* CKD device header */
CCKDDASD_DEVHDR cdevhdr; /* Compressed CKD device hdr */
CCKDDASD_DEVHDR cdevhdr2; /* CCKD header 2 */
CCKD_L1ENT *l1=NULL; /* -> Primary lookup table */
int l1tabsz; /* Primary lookup table size */
CCKD_L2ENT l2[256], *l2p=NULL; /* Secondary lookup table */
CKDDEV *ckd=NULL; /* -> CKD DASD table entry */
BYTE *buf=NULL; /* Buffer for track image */
int readlen; /* Length for buffer read */
long trklen=0; /* Length of track image */
int rc; /* Return code */
OFF_T rcoff; /* Return value from lseek() */
int crc=-1; /* Chdsk return code */
int i,j,k; /* Indexes */
struct STAT fst; /* File status information */
int cyls=-1, hdrcyls=-1; /* Total cylinders */
int trks; /* Total tracks */
int heads=-1, hdrheads=-1; /* Heads per cylinder */
int rec0len=8; /* R0 length */
int maxdlen=-1; /* Max data length for device*/
int trksz=-1, hdrtrksz=-1; /* Track size */
OFF_T hipos, lopos; /* Valid high/low offsets */
OFF_T pos; /* File offset */
int hdrerr=0, fsperr=0, l1errs=0, l2errs=0, trkerrs=0;
/* Error indicators */
OFF_T fsp; /* Free space offset */
CCKD_FREEBLK fb; /* Free space block */
int n; /* Size of space tables */
SPCTAB *spc=NULL, *rcv=NULL, /* Space/Recovery tables */
*gap=NULL; /* Gaps in the file */
int s=0, r=0; /* Space/Recovery indices */
int trk; /* Current track number */
int maxlen; /* Max length of track image */
int gaps=0, gapsize=0; /* Gaps and size in file */
BYTE *gapbuf=NULL; /* Buffer containing gap data*/
int x,y; /* Lookup table indices */
int rcvtrks; /* Nbr trks to be recovered */
int fdflags; /* File flags */
int shadow=0; /* 1=Shadow file */
int swapend=0; /* 1=Wrong endianess */
int fend,mend; /* Byte order indicators */
int ckddasd; /* 1=CKD dasd 0=FBA dasd */
U32 trkavg=0, trksum=0, trknum=0;
/* Used to compute avg trk sz*/
int trys; /* Nbr recovery trys for trk */
int comps = 0; /* Supported compressions */
int badcomps[] = {0, 0, 0}; /* Bad compression counts */
char msg[256]; /* Message */
char *space[] = {"none", "devhdr", "cdevhdr", "l1tab", "l2tab",
"trkimg", "free_blk", "file_end"};
char *compression[] = {"none", "zlib", "bzip2"};
#if defined(HAVE_LIBZ)
comps |= CCKD_COMPRESS_ZLIB;
#endif
#if defined(CCKD_BZIP2)
comps |= CCKD_COMPRESS_BZIP2;
#endif
#ifdef EXTERNALGUI
/* The number of "steps" (checks) we'll be doing... */
if (extgui) fprintf (stderr,"STEPS=10\n");
#endif /*EXTERNALGUI*/
#ifdef EXTERNALGUI
/* Beginning first step... */
if (extgui) fprintf (stderr,"STEP=1\n");
#endif /*EXTERNALGUI*/
/*-------------------------------------------------------------------*/
/* Read the device header. */
/*-------------------------------------------------------------------*/
rc = FSTAT (fd, &fst);
if (rc < 0)
{
CDSKMSG (m, "fstat(%d) error: %s\n",
fd, strerror(errno));
return -1;
}
fdflags = get_file_accmode_flags(fd);
rcoff = LSEEK (fd, 0, SEEK_SET);
if (rcoff < 0)
{
CDSKMSG (m, "devhdr lseek error: %s\n",
strerror(errno));
}
rc = read (fd, &devhdr, CKDDASD_DEVHDR_SIZE);
if (rc != CKDDASD_DEVHDR_SIZE)
{
CDSKMSG (m, "devhdr read error: %s\n",
strerror(errno));
return -1;
}
/*-------------------------------------------------------------------*/
/* Perform checks on the device header. The following fields */
/* *must* be correct or the chkdsk function terminates: */
/* devid */
/* In this circumstance, the header will need to be */
/* manually repaired -- see cckdfix.c for a sample */
/*-------------------------------------------------------------------*/
/* Check the identifier */
if (memcmp(devhdr.devid, "CKD_C370", 8) == 0
|| memcmp(devhdr.devid, "CKD_S370", 8) == 0)
ckddasd = 1;
else if (memcmp(devhdr.devid, "FBA_C370", 8) == 0
|| memcmp(devhdr.devid, "FBA_S370", 8) == 0)
ckddasd = 0;
else
{
CDSKMSG (m, "file is not a compressed dasd file\n");
goto cdsk_return;
}
if (memcmp(devhdr.devid, "CKD_S370", 8) == 0
|| memcmp(devhdr.devid, "FBA_S370", 8) == 0)
shadow = 1;
/*-------------------------------------------------------------------*/
/* read the compressed CKD device header */
/*-------------------------------------------------------------------*/
rc = read (fd, &cdevhdr, CCKDDASD_DEVHDR_SIZE);
if (rc != CCKDDASD_DEVHDR_SIZE)
{
CDSKMSG (m, "cdevhdr read error: %s\n",
strerror(errno));
goto cdsk_return;
}
/*-------------------------------------------------------------------*/
/* Check the endianess of the compressed CKD file */
/*-------------------------------------------------------------------*/
fend = ((cdevhdr.options & CCKD_BIGENDIAN) != 0);
mend = cckd_endian ();
if (fend != mend)
{
if (fdflags & O_RDWR)
{
CDSKMSG (m, "converting file(%d) to %s\n",
fd, mend ? "big-endian" : "little-endian");
rc = cckd_swapend (fd, m);
if (rc == -1) goto cdsk_return;
rcoff = LSEEK (fd, CKDDASD_DEVHDR_SIZE, SEEK_SET);
rc = read (fd, &cdevhdr, CCKDDASD_DEVHDR_SIZE);
cdevhdr.options |= CCKD_ORDWR;
}
else
{
swapend = 1;
cckd_swapend_chdr (&cdevhdr);
}
}
/*-------------------------------------------------------------------*/
/* Lookup the DASD table entry for the device */
/*-------------------------------------------------------------------*/
if (ckddasd)
{
hdrcyls = ((U32)(cdevhdr.cyls[3]) << 24)
| ((U32)(cdevhdr.cyls[2]) << 16)
| ((U32)(cdevhdr.cyls[1]) << 8)
| (U32)(cdevhdr.cyls[0]);
ckd = dasd_lookup (DASD_CKDDEV, NULL, devhdr.devtype, hdrcyls);
if (ckd == NULL)
{
CDSKMSG (m, "DASD table entry not found for 0x%2.2X cyls %d\n",
devhdr.devtype, hdrcyls);
goto cdsk_return;
}
maxdlen = ckd->r1;
heads = ckd->heads;
}
/*-------------------------------------------------------------------*/
/* Perform checks on the headers. The following fields must be */
/* correct or the chkdsk function terminates: */
/* devhdr: */
/* trksz */
/* heads */
/* cdevhdr: */
/* cyls */
/* numl1tab */
/* In this circumstance, the headers will need to be manually */
/* repaired -- see cckdfix.c for a sample */
/*-------------------------------------------------------------------*/
if (ckddasd)
{
/* Check track size */
trksz = sizeof(CKDDASD_TRKHDR)
+ sizeof(CKDDASD_RECHDR) + rec0len
+ sizeof(CKDDASD_RECHDR) + maxdlen
+ sizeof(eighthexFF);
trksz = ((trksz+511)/512)*512;
hdrtrksz = ((U32)(devhdr.trksize[3]) << 24)
| ((U32)(devhdr.trksize[2]) << 16)
| ((U32)(devhdr.trksize[1]) << 8)
| (U32)(devhdr.trksize[0]);
if (trksz != hdrtrksz)
{
CDSKMSG (m, "Invalid track size in header: "
"0x%x, expecting 0x%x\n",
hdrtrksz, trksz);
goto cdsk_return;
}
/* Check number of heads */
hdrheads = ((U32)(devhdr.heads[3]) << 24)
| ((U32)(devhdr.heads[2]) << 16)
| ((U32)(devhdr.heads[1]) << 8)
| (U32)(devhdr.heads[0]);
if (heads != hdrheads)
{
CDSKMSG (m, "Invalid number of heads in header: "
"%d, expecting %d\n",
hdrheads, heads);
goto cdsk_return;
}
if (cdevhdr.numl1tab < ((hdrcyls * heads) + 255) / 256
|| cdevhdr.numl1tab > ((hdrcyls * heads) + 255) / 256 + 1)
{
CDSKMSG (m, "Invalid number of l1 table entries in header: "
"%d, expecting %d\n",
cdevhdr.numl1tab, ((hdrcyls * heads) + 255) / 256);
goto cdsk_return;
}
cyls = hdrcyls;
trks = cyls * heads;
}
else
{
trks = ((U32)(cdevhdr.cyls[3]) << 24)
| ((U32)(cdevhdr.cyls[2]) << 16)
| ((U32)(cdevhdr.cyls[1]) << 8)
| (U32)(cdevhdr.cyls[0]);
trks = (trks / CFBA_BLOCK_NUM) + 1;
trksz = CFBA_BLOCK_SIZE + CKDDASD_TRKHDR_SIZE;
cyls = heads = -1;
if (cdevhdr.numl1tab < (trks + 255) / 256
|| cdevhdr.numl1tab > (trks + 255) / 256 + 1)
{
CDSKMSG (m, "Invalid number of l1 table entries in header: "
"%d, expecting %d\n", cdevhdr.numl1tab, (trks + 255) / 256);
goto cdsk_return;
}
}
l1tabsz = cdevhdr.numl1tab * CCKD_L1ENT_SIZE;
/* Perform space checks */
if ((OFF_T)cdevhdr.size != fst.st_size
|| (OFF_T)(cdevhdr.used + cdevhdr.free_total) != fst.st_size
|| (cdevhdr.free_largest > cdevhdr.free_total - cdevhdr.free_imbed)
|| (cdevhdr.free == 0