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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239
|
/* DASDUTIL.C (c) Copyright Roger Bowler, 1999-2010 */
/* Hercules DASD Utilities: Common subroutines */
/*-------------------------------------------------------------------*/
/* This module contains common subroutines used by DASD utilities */
/*-------------------------------------------------------------------*/
#include "hstdinc.h"
#define _DASDUTIL_C_
#define _HDASD_DLL_
#include "hercules.h"
#include "dasdblks.h"
#include "devtype.h"
#include "opcode.h"
/*-------------------------------------------------------------------*/
/* External references (defined in ckddasd.c) */
/*-------------------------------------------------------------------*/
extern DEVHND ckddasd_device_hndinfo;
/*-------------------------------------------------------------------*/
/* Internal macro definitions */
/*-------------------------------------------------------------------*/
/*-------------------------------------------------------------------*/
/* Static data areas */
/*-------------------------------------------------------------------*/
static int verbose = 0; /* Be chatty about reads etc. */
static BYTE eighthexFF[] = {0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
static BYTE iplpsw[8] = {0x00,0x06,0x00,0x00,0x00,0x00,0x00,0x0F};
static BYTE iplccw1[8] = {0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x01};
static BYTE iplccw2[8] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
static int nextnum = 0;
#if 0
SYSBLK sysblk; /* Currently required for shared.c */
#endif
/*-------------------------------------------------------------------*/
/* Subroutine to convert a null-terminated string to upper case */
/*-------------------------------------------------------------------*/
DLL_EXPORT void string_to_upper (char *source)
{
int i; /* Array subscript */
for (i = 0; source[i] != '\0'; i++)
source[i] = toupper(source[i]);
} /* end function string_to_upper */
/*-------------------------------------------------------------------*/
/* Subroutine to convert a null-terminated string to lower case */
/*-------------------------------------------------------------------*/
DLL_EXPORT void string_to_lower (char *source)
{
int i; /* Array subscript */
for (i = 0; source[i] != '\0'; i++)
source[i] = tolower(source[i]);
} /* end function string_to_lower */
/*-------------------------------------------------------------------*/
/* Subroutine to convert a string to EBCDIC and pad with blanks */
/*-------------------------------------------------------------------*/
DLL_EXPORT void convert_to_ebcdic (BYTE *dest, int len, char *source)
{
int i; /* Array subscript */
set_codepage(NULL);
for (i = 0; i < len && source[i] != '\0'; i++)
dest[i] = host_to_guest(source[i]);
while (i < len)
dest[i++] = 0x40;
} /* end function convert_to_ebcdic */
/*-------------------------------------------------------------------*/
/* Subroutine to convert an EBCDIC string to an ASCIIZ string. */
/* Removes trailing blanks and adds a terminating null. */
/* Returns the length of the ASCII string excluding terminating null */
/*-------------------------------------------------------------------*/
DLL_EXPORT int make_asciiz (char *dest, int destlen, BYTE *src, int srclen)
{
int len; /* Result length */
set_codepage(NULL);
for (len=0; len < srclen && len < destlen-1; len++)
dest[len] = guest_to_host(src[len]);
while (len > 0 && dest[len-1] == SPACE) len--;
dest[len] = '\0';
return len;
} /* end function make_asciiz */
/*-------------------------------------------------------------------*/
/* Subroutine to print a data block in hex and character format. */
/*-------------------------------------------------------------------*/
DLL_EXPORT void data_dump ( void *addr, int len )
{
unsigned int maxlen = 2048;
unsigned int i, xi, offset, startoff = 0;
BYTE c;
BYTE *pchar;
char print_chars[17];
char hex_chars[64];
char prev_hex[64] = "";
int firstsame = 0;
int lastsame = 0;
set_codepage(NULL);
pchar = (BYTE *)addr;
for (offset=0; ; )
{
if (offset >= maxlen && offset <= len - maxlen)
{
offset += 16;
pchar += 16;
prev_hex[0] = '\0';
continue;
}
if ( offset > 0 )
{
if ( strcmp ( hex_chars, prev_hex ) == 0 )
{
if ( firstsame == 0 ) firstsame = startoff;
lastsame = startoff;
}
else
{
if ( firstsame != 0 )
{
if ( lastsame == firstsame )
printf ("Line %4.4X same as above\n",
firstsame );
else
printf ("Lines %4.4X to %4.4X same as above\n",
firstsame, lastsame );
firstsame = lastsame = 0;
}
printf ("+%4.4X %s %s\n",
startoff, hex_chars, print_chars);
strcpy ( prev_hex, hex_chars );
}
}
if ( offset >= (U32)len ) break;
memset ( print_chars, 0, sizeof(print_chars) );
memset ( hex_chars, SPACE, sizeof(hex_chars) );
startoff = offset;
for (xi=0, i=0; i < 16; i++)
{
c = *pchar++;
if (offset < (U32)len) {
sprintf(hex_chars+xi, "%2.2X", c);
print_chars[i] = '.';
if (isprint(c)) print_chars[i] = c;
c = guest_to_host(c);
if (isprint(c)) print_chars[i] = c;
}
offset++;
xi += 2;
hex_chars[xi] = SPACE;
if ((offset & 3) == 0) xi++;
} /* end for(i) */
hex_chars[xi] = '\0';
} /* end for(offset) */
} /* end function data_dump */
/*-------------------------------------------------------------------*/
/* Subroutine to read a track from the CKD DASD image */
/* Input: */
/* cif -> CKD image file descriptor structure */
/* cyl Cylinder number */
/* head Head number */
/* Output: */
/* The track is read into trkbuf, and curcyl and curhead */
/* are set to the cylinder and head number. */
/* */
/* Return value is 0 if successful, -1 if error */
/*-------------------------------------------------------------------*/
DLL_EXPORT int read_track (CIFBLK *cif, int cyl, int head)
{
int rc; /* Return code */
int trk; /* Track number */
DEVBLK *dev; /* -> CKD device block */
BYTE unitstat; /* Unit status */
/* Exit if required track is already in buffer */
if (cif->curcyl == cyl && cif->curhead == head)
return 0;
dev = &cif->devblk;
if (cif->trkmodif)
{
cif->trkmodif = 0;
if (verbose) /* Issue progress message */
fprintf (stdout, _("HHCDU001I Updating cyl %d head %d\n"),
cif->curcyl, cif->curhead);
trk = (cif->curcyl * cif->heads) + cif->curhead;
rc = (dev->hnd->write)(dev, trk, 0, NULL, cif->trksz, &unitstat);
if (rc < 0)
{
fprintf (stderr, _("HHCDU002E %s write track error: stat=%2.2X\n"),
cif->fname, unitstat);
return -1;
}
}
if (verbose) /* Issue progress message */
fprintf (stdout, _("HHCDU003I Reading cyl %d head %d\n"), cyl, head);
trk = (cyl * cif->heads) + head;
rc = (dev->hnd->read)(dev, trk, &unitstat);
if (rc < 0)
{
fprintf (stderr, _("HHCDU004E %s read track error: stat=%2.2X\n"),
cif->fname, unitstat);
return -1;
}
/* Set current buf, cylinder and head */
cif->trkbuf = dev->buf;
cif->curcyl = cyl;
cif->curhead = head;
return 0;
} /* end function read_track */
/*-------------------------------------------------------------------*/
/* Subroutine to read a block from the CKD DASD image */
/* Input: */
/* cif -> CKD image file descriptor structure */
/* cyl Cylinder number of requested block */
/* head Head number of requested block */
/* rec Record number of requested block */
/* Output: */
/* keyptr Pointer to record key */
/* keylen Actual key length */
/* dataptr Pointer to record data */
/* datalen Actual data length */
/* */
/* Return value is 0 if successful, +1 if end of track, -1 if error */
/*-------------------------------------------------------------------*/
DLL_EXPORT int read_block (CIFBLK *cif, int cyl, int head, int rec, BYTE **keyptr,
int *keylen, BYTE **dataptr, int *datalen)
{
int rc; /* Return code */
BYTE *ptr; /* -> byte in track buffer */
CKDDASD_RECHDR *rechdr; /* -> Record header */
int kl; /* Key length */
int dl; /* Data length */
/* Read the required track into the track buffer if necessary */
rc = read_track (cif, cyl, head);
if (rc < 0) return -1;
/* Search for the requested record in the track buffer */
ptr = cif->trkbuf;
ptr += CKDDASD_TRKHDR_SIZE;
while (1)
{
/* Exit with record not found if end of track */
if (memcmp(ptr, eighthexFF, 8) == 0)
return +1;
/* Extract key length and data length from count field */
rechdr = (CKDDASD_RECHDR*)ptr;
kl = rechdr->klen;
dl = (rechdr->dlen[0] << 8) | rechdr->dlen[1];
/* Exit if requested record number found */
if (rechdr->rec == rec)
break;
/* Issue progress message */
// fprintf (stdout,
// "Skipping CCHHR=%2.2X%2.2X%2.2X%2.2X"
// "%2.2X KL=%2.2X DL=%2.2X%2.2X\n",
// rechdr->cyl[0], rechdr->cyl[1],
// rechdr->head[0], rechdr->head[1],
// rechdr->rec, rechdr->klen,
// rechdr->dlen[0], rechdr->dlen[1]);
/* Point past count key and data to next block */
ptr += CKDDASD_RECHDR_SIZE + kl + dl;
}
/* Return key and data pointers and lengths */
if (keyptr != NULL) *keyptr = ptr + CKDDASD_RECHDR_SIZE;
if (keylen != NULL) *keylen = kl;
if (dataptr != NULL) *dataptr = ptr + CKDDASD_RECHDR_SIZE + kl;
if (datalen != NULL) *datalen = dl;
return 0;
} /* end function read_block */
/*-------------------------------------------------------------------*/
/* Subroutine to search a dataset for a specified key */
/* Input: */
/* cif -> CKD image file descriptor structure */
/* key Key value */
/* keylen Key length */
/* noext Number of extents */
/* extent Dataset extent array */
/* Output: */
/* cyl Cylinder number of requested block */
/* head Head number of requested block */
/* rec Record number of requested block */
/* */
/* Return value is 0 if successful, +1 if key not found, -1 if error */
/*-------------------------------------------------------------------*/
DLL_EXPORT int search_key_equal (CIFBLK *cif, BYTE *key, int keylen, int noext,
DSXTENT extent[], int *cyl, int *head, int *rec)
{
int rc; /* Return code */
int ccyl; /* Cylinder number */
int chead; /* Head number */
int cext; /* Extent sequence number */
int ecyl; /* Extent end cylinder */
int ehead; /* Extent end head */
BYTE *ptr; /* -> byte in track buffer */
CKDDASD_RECHDR *rechdr; /* -> Record header */
int kl; /* Key length */
int dl; /* Data length */
/* Start at first track of first extent */
cext = 0;
ccyl = (extent[cext].xtbcyl[0] << 8) | extent[cext].xtbcyl[1];
chead = (extent[cext].xtbtrk[0] << 8) | extent[cext].xtbtrk[1];
ecyl = (extent[cext].xtecyl[0] << 8) | extent[cext].xtecyl[1];
ehead = (extent[cext].xtetrk[0] << 8) | extent[cext].xtetrk[1];
if (verbose)
{
fprintf (stdout,
_("HHCDU005I Searching extent %d begin (%d,%d) end (%d,%d)\n"),
cext, ccyl, chead, ecyl, ehead);
}
while (1)
{
/* Read the required track into the track buffer */
rc = read_track (cif, ccyl, chead);
if (rc < 0) return -1;
/* Search for the requested record in the track buffer */
ptr = cif->trkbuf;
ptr += CKDDASD_TRKHDR_SIZE;
while (1)
{
/* Exit loop at end of track */
if (memcmp(ptr, eighthexFF, 8) == 0)
break;
/* Extract key length and data length from count field */
rechdr = (CKDDASD_RECHDR*)ptr;
kl = rechdr->klen;
dl = (rechdr->dlen[0] << 8) | rechdr->dlen[1];
/* Return if requested record key found */
if (kl == keylen
&& memcmp(ptr + CKDDASD_RECHDR_SIZE, key, 44) == 0)
{
*cyl = ccyl;
*head = chead;
*rec = rechdr->rec;
return 0;
}
/* Issue progress message */
// fprintf (stdout,
// "Skipping CCHHR=%2.2X%2.2X%2.2X%2.2X"
// "%2.2X KL=%2.2X DL=%2.2X%2.2X\n",
// rechdr->cyl[0], rechdr->cyl[1],
// rechdr->head[0], rechdr->head[1],
// rechdr->rec, rechdr->klen,
// rechdr->dlen[0], rechdr->dlen[1]);
/* Point past count key and data to next block */
ptr += CKDDASD_RECHDR_SIZE + kl + dl;
} /* end while */
/* Point to the next track */
chead++;
if (chead >= cif->heads)
{
ccyl++;
chead = 0;
}
/* Loop if next track is within current extent */
if (ccyl < ecyl || (ccyl == ecyl && chead <= ehead))
continue;
/* Move to next extent */
cext++;
if (cext >= noext) break;
ccyl = (extent[cext].xtbcyl[0] << 8) | extent[cext].xtbcyl[1];
chead = (extent[cext].xtbtrk[0] << 8) | extent[cext].xtbtrk[1];
ecyl = (extent[cext].xtecyl[0] << 8) | extent[cext].xtecyl[1];
ehead = (extent[cext].xtetrk[0] << 8) | extent[cext].xtetrk[1];
if (verbose)
{
fprintf (stdout,
_("HHCDU006I Searching extent %d begin (%d,%d) end (%d,%d)\n"),
cext, ccyl, chead, ecyl, ehead);
}
} /* end while */
/* Return record not found at end of extents */
return +1;
} /* end function search_key_equal */
/*-------------------------------------------------------------------*/
/* Subroutine to convert relative track to cylinder and head */
/* Input: */
/* tt Relative track number */
/* noext Number of extents in dataset */
/* extent Dataset extent array */
/* heads Number of tracks per cylinder */
/* Output: */
/* cyl Cylinder number */
/* head Head number */
/* */
/* Return value is 0 if successful, or -1 if error */
/*-------------------------------------------------------------------*/
DLL_EXPORT int convert_tt (int tt, int noext, DSXTENT extent[], int heads,
int *cyl, int *head)
{
int i; /* Extent sequence number */
int trk; /* Relative track number */
int bcyl; /* Extent begin cylinder */
int btrk; /* Extent begin head */
int ecyl; /* Extent end cylinder */
int etrk; /* Extent end head */
int start; /* Extent begin track */
int end; /* Extent end track */
int extsize; /* Extent size in tracks */
for (i = 0, trk = tt; i < noext; i++)
{
bcyl = (extent[i].xtbcyl[0] << 8) | extent[i].xtbcyl[1];
btrk = (extent[i].xtbtrk[0] << 8) | extent[i].xtbtrk[1];
ecyl = (extent[i].xtecyl[0] << 8) | extent[i].xtecyl[1];
etrk = (extent[i].xtetrk[0] << 8) | extent[i].xtetrk[1];
start = (bcyl * heads) + btrk;
end = (ecyl * heads) + etrk;
extsize = end - start + 1;
if (trk < extsize)
{
trk += start;
*cyl = trk / heads;
*head = trk % heads;
return 0;
}
trk -= extsize;
} /* end for(i) */
fprintf (stderr,
_("HHCDU007E Track %d not found in extent table\n"),
tt);
return -1;
} /* end function convert_tt */
/*-------------------------------------------------------------------*/
/* Subroutine to open a CKD image file */
/* Input: */
/* fname CKD image file name */
/* sfname xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx */
/* omode Open mode: O_RDONLY or O_RDWR */
/* dasdcopy xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx */
/* */
/* The CKD image file is opened, a track buffer is obtained, */
/* and a CKD image file descriptor structure is built. */
/* Return value is a pointer to the CKD image file descriptor */
/* structure if successful, or NULL if unsuccessful. */
/*-------------------------------------------------------------------*/
DLL_EXPORT CIFBLK* open_ckd_image (char *fname, char *sfname, int omode,
int dasdcopy)
{
int fd; /* File descriptor */
int rc; /* Return code */
int len; /* Record length */
CKDDASD_DEVHDR devhdr; /* CKD device header */
CIFBLK *cif; /* CKD image file descriptor */
DEVBLK *dev; /* CKD device block */
CKDDEV *ckd; /* CKD DASD table entry */
char *rmtdev; /* Possible remote device */
char *argv[2]; /* Arguments to */
int argc=0; /* */
char sfxname[FILENAME_MAX*2];/* Suffixed file name */
char typname[64];
char pathname[MAX_PATH]; /* file path in host format */
/* Obtain storage for the file descriptor structure */
cif = (CIFBLK*) calloc (sizeof(CIFBLK), 1);
if (cif == NULL)
{
fprintf (stderr,
_("HHCDU008E Cannot obtain storage for device descriptor "
"buffer: %s\n"),
strerror(errno));
return NULL;
}
/* Initialize the devblk */
dev = &cif->devblk;
if ((omode & O_RDWR) == 0) dev->ckdrdonly = 1;
dev->fd = -1;
dev->batch = 1;
dev->dasdcopy = dasdcopy;
/* If the filename has a `:' then it may be a remote device */
rmtdev = strchr(fname, ':');
/* Read the device header so we can determine the device type */
strcpy (sfxname, fname);
hostpath(pathname, sfxname, sizeof(pathname));
fd = hopen(pathname, omode);
if (fd < 0)
{
/* If no shadow file name was specified, then try opening the
file with the file sequence number in the name */
if (sfname == NULL)
{
int i;
char *s,*suffix;
/* Look for last slash marking end of directory name */
s = strrchr (fname, '/');
if (s == NULL) s = fname;
/* Insert suffix before first dot in file name, or
append suffix to file name if there is no dot.
If the filename already has a place for the suffix
then use that. */
s = strchr (s, '.');
if (s != NULL)
{
i = s - fname;
if (i > 2 && fname[i-2] == '_')
suffix = sfxname + i - 1;
else
{
strcpy (sfxname + i, "_1");
strcat (sfxname, fname + i);
suffix = sfxname + i + 1;
}
}
else
{
if (strlen(sfxname) < 2 || sfxname[strlen(sfxname)-2] != '_')
strcat (sfxname, "_1");
suffix = sfxname + strlen(sfxname) - 1;
}
*suffix = '1';
hostpath(pathname, sfxname, sizeof(pathname));
fd = hopen(pathname, omode);
}
if (fd < 0 && rmtdev == NULL)
{
fprintf (stderr, _("HHCDU009E Cannot open %s: %s\n"),
fname, strerror(errno));
free (cif);
return NULL;
}
else if (fd < 0) strcpy (sfxname, fname);
}
/* If not a possible remote devic, check the dasd header
and set the device type */
if (fd >= 0)
{
len = read (fd, &devhdr, CKDDASD_DEVHDR_SIZE);
if (len < 0)
{
fprintf (stderr, _("HHCDU010E %s read error: %s\n"),
fname, strerror(errno));
close (fd);
free (cif);
return NULL;
}
close (fd);
if (len < (int)CKDDASD_DEVHDR_SIZE
|| (memcmp(devhdr.devid, "CKD_P370", 8)
&& memcmp(devhdr.devid, "CKD_C370", 8)))
{
fprintf (stderr, _("HHCDU011E %s CKD header invalid\n"), fname);
free (cif);
return NULL;
}
/* Set the device type */
ckd = dasd_lookup (DASD_CKDDEV, NULL, devhdr.devtype, 0);
if (ckd == NULL)
{
fprintf(stderr, _("HHCDU012E DASD table entry not found for "
"devtype 0x%2.2X\n"),
devhdr.devtype);
free (cif);
return NULL;
}
dev->devtype = ckd->devt;
snprintf(typname,64,"%4.4X",dev->devtype);
dev->typname=typname; /* Makes HDL Happy */
}
/* Set the device handlers */
dev->hnd = &ckddasd_device_hndinfo;
/* Set the device number */
dev->devnum = ++nextnum;
/* Build arguments for ckddasd_init_handler */
argv[0] = sfxname;
argc++;
if (sfname != NULL)
{
argv[1] = sfname;
argc++;
}
/* Call the device handler initialization function */
rc = (dev->hnd->init)(dev, argc, argv);
if (rc < 0)
{
fprintf (stderr, _("HHCDU013E CKD initialization failed for %s\n"),
fname);
free (cif);
return NULL;
}
/* Call the device start exit */
if (dev->hnd->start) (dev->hnd->start) (dev);
/* Set CIF fields */
cif->fname = fname;
cif->fd = dev->fd;
/* Extract the number of heads and the track size */
cif->heads = dev->ckdheads;
cif->trksz = ((U32)(devhdr.trksize[3]) << 24)
| ((U32)(devhdr.trksize[2]) << 16)
| ((U32)(devhdr.trksize[1]) << 8)
| (U32)(devhdr.trksize[0]);
if (verbose)
{
fprintf (stderr,
_("HHCDU014I %s heads=%d trklen=%d\n"),
cif->fname, cif->heads, cif->trksz);
}
/* Indicate that the track buffer is empty */
cif->curcyl = -1;
cif->curhead = -1;
cif->trkmodif = 0;
return cif;
} /* end function open_ckd_image */
/*-------------------------------------------------------------------*/
/* Subroutine to close a CKD image file */
/* Input: */
/* cif -> CKD image file descriptor structure */
/* */
/* The track buffer is flushed and released, the CKD image file */
/* is closed, and the file descriptor structure is released. */
/* Return value is 0 if successful, -1 if error */
/*-------------------------------------------------------------------*/
DLL_EXPORT int close_ckd_image (CIFBLK *cif)
{
int rc; /* Return code */
int trk; /* Track number */
DEVBLK *dev; /* -> CKD device block */
BYTE unitstat; /* Unit status */
dev = &cif->devblk;
/* Write the last track if modified */
if (cif->trkmodif)
{
if (verbose) /* Issue progress message */
fprintf (stdout, _("HHCDU015I Updating cyl %d head %d\n"),
cif->curcyl, cif->curhead);
trk = (cif->curcyl * cif->heads) + cif->curhead;
rc = (dev->hnd->write)(dev, trk, 0, NULL, cif->trksz, &unitstat);
if (rc < 0)
{
fprintf (stderr, _("HHCDU016E %s write track error: stat=%2.2X\n"),
cif->fname, unitstat);
}
}
/* Call the END exit */
if (dev->hnd->end) (dev->hnd->end) (dev);
/* Close the CKD image file */
(dev->hnd->close)(dev);
/* Release the file descriptor structure */
free (cif);
return 0;
} /* end function close_ckd_image */
/*-------------------------------------------------------------------*/
/* Subroutine to open a FBA image file */
/* Input: */
/* fname FBA image file name */
/* sfname xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx */
/* omode Open mode: O_RDONLY or O_RDWR */
/* dasdcopy xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx */
/* */
/* The FBA image file is opened, a track buffer is obtained, */
/* and a FBA image file descriptor structure is built. */
/* Return value is a pointer to the FBA image file descriptor */
/* structure if successful, or NULL if unsuccessful. */
/*-------------------------------------------------------------------*/
DLL_EXPORT CIFBLK* open_fba_image (char *fname, char *sfname, int omode,
int dasdcopy)
{
int rc; /* Return code */
CIFBLK *cif; /* FBA image file descriptor */
DEVBLK *dev; /* FBA device block */
FBADEV *fba; /* FBA DASD table entry */
char *argv[2]; /* Arguments to */
int argc=0; /* device open */
/* Obtain storage for the file descriptor structure */
cif = (CIFBLK*) calloc (sizeof(CIFBLK), 1);
if (cif == NULL)
{
fprintf (stderr,
_("HHCDU017E Cannot obtain storage for device descriptor "
"buffer: %s\n"),
strerror(errno));
return NULL;
}
/* Initialize the devblk */
dev = &cif->devblk;
if ((omode & O_RDWR) == 0) dev->ckdrdonly = 1;
dev->batch = 1;
dev->dasdcopy = dasdcopy;
/* Set the device type */
fba = dasd_lookup (DASD_FBADEV, NULL, DEFAULT_FBA_TYPE, 0);
if (fba == NULL)
{
fprintf(stderr, _("HHCDU018E DASD table entry not found for "
"devtype 0x%2.2X\n"),
DEFAULT_FBA_TYPE);
free (cif);
return NULL;
}
dev->devtype = fba->devt;
/* Set the device handlers */
dev->hnd = &fbadasd_device_hndinfo;
/* Set the device number */
dev->devnum = ++nextnum;
/* Build arguments for fbadasd_init_handler */
argv[0] = fname;
argc++;
if (sfname != NULL)
{
argv[1] = sfname;
argc++;
}
/* Call the device handler initialization function */
rc = (dev->hnd->init)(dev, argc, argv);
if (rc < 0)
{
fprintf (stderr, _("HHCDU019E FBA initialization failed for %s\n"),
fname);
free (cif);
return NULL;
}
/* Set CIF fields */
cif->fname = fname;
cif->fd = dev->fd;
/* Extract the number of sectors and the sector size */
cif->heads = dev->fbanumblk;
cif->trksz = dev->fbablksiz;
if (verbose)
{
fprintf (stderr,
_("HHCDU020I %s sectors=%d size=%d\n"),
cif->fname, cif->heads, cif->trksz);
}
/* Indicate that the track buffer is empty */
cif->curcyl = -1;
cif->curhead = -1;
cif->trkmodif = 0;
return cif;
} /* end function open_fba_image */
/*-------------------------------------------------------------------*/
/* Subroutine to build extent array for specified dataset */
/* Input: */
/* cif -> CKD image file descriptor structure */
/* dsnama -> Dataset name (ASCIIZ) */
/* Output: */
/* extent Extent array (up to 16 entries) */
/* noext Number of extents */
/* */
/* Return value is 0 if successful, or -1 if error */
/*-------------------------------------------------------------------*/
DLL_EXPORT int build_extent_array (CIFBLK *cif, char *dsnama, DSXTENT extent[],
int *noext)
{
int rc; /* Return code */
int len; /* Record length */
int cyl; /* Cylinder number */
int head; /* Head number */
int rec; /* Record number */
BYTE *vol1data; /* -> Volume label */
FORMAT1_DSCB *f1dscb; /* -> Format 1 DSCB */
FORMAT3_DSCB *f3dscb; /* -> Format 3 DSCB */
FORMAT4_DSCB *f4dscb; /* -> Format 4 DSCB */
BYTE dsname[44]; /* Dataset name (EBCDIC) */
char volser[7]; /* Volume serial (ASCIIZ) */
/* Convert the dataset name to EBCDIC */
convert_to_ebcdic (dsname, sizeof(dsname), dsnama);
/* Read the volume label */
rc = read_block (cif, 0, 0, 3, NULL, NULL, &vol1data, &len);
if (rc < 0) return -1;
if (rc > 0)
{
fprintf (stderr, _("HHCDU021E VOL1 record not found\n"));
return -1;
}
/* Extract the volume serial and the CCHHR of the format 4 DSCB */
make_asciiz (volser, sizeof(volser), vol1data+4, 6);
cyl = (vol1data[11] << 8) | vol1data[12];
head = (vol1data[13] << 8) | vol1data[14];
rec = vol1data[15];
if (verbose)
{
fprintf (stdout,
_("HHCDU022I VOLSER=%s VTOC=%4.4X%4.4X%2.2X\n"),
volser, cyl, head, rec);
}
/* Read the format 4 DSCB */
rc = read_block (cif, cyl, head, rec,
(void *)&f4dscb, &len, NULL, NULL);
if (rc < 0) return -1;
if (rc > 0)
{
fprintf (stderr, _("HHCDU023E F4DSCB record not found\n"));
return -1;
}
if (verbose)
{
fprintf (stdout,
_("HHCDU023I VTOC start %2.2X%2.2X%2.2X%2.2X "
"end %2.2X%2.2X%2.2X%2.2X\n"),
f4dscb->ds4vtoce.xtbcyl[0], f4dscb->ds4vtoce.xtbcyl[1],
f4dscb->ds4vtoce.xtbtrk[0], f4dscb->ds4vtoce.xtbtrk[1],
f4dscb->ds4vtoce.xtecyl[0], f4dscb->ds4vtoce.xtecyl[1],
f4dscb->ds4vtoce.xtetrk[0], f4dscb->ds4vtoce.xtetrk[1]);
}
/* Search for the requested dataset in the VTOC */
rc = search_key_equal (cif, dsname, sizeof(dsname),
1, &(f4dscb->ds4vtoce),
&cyl, &head, &rec);
if (rc < 0) return -1;
if (rc > 0)
{
fprintf (stderr,
_("HHCDU024E Dataset %s not found in VTOC\n"),
dsnama);
return -1;
}
if (verbose)
{
fprintf (stdout,
_("HHCDU025I DSNAME=%s F1DSCB CCHHR=%4.4X%4.4X%2.2X\n"),
dsnama, cyl, head, rec);
}
/* Read the format 1 DSCB */
rc = read_block (cif, cyl, head, rec,
(void *)&f1dscb, &len, NULL, NULL);
if (rc < 0) return -1;
if (rc > 0)
{
fprintf (stderr, _("HHCDU026E F1DSCB record not found\n"));
return -1;
}
/* Extract number of extents and first 3 extent descriptors */
*noext = f1dscb->ds1noepv;
extent[0] = f1dscb->ds1ext1;
extent[1] = f1dscb->ds1ext2;
extent[2] = f1dscb->ds1ext3;
/* Obtain additional extent descriptors */
if (f1dscb->ds1noepv > 3)
{
/* Read the format 3 DSCB */
cyl = (f1dscb->ds1ptrds[0] << 8) | f1dscb->ds1ptrds[1];
head = (f1dscb->ds1ptrds[2] << 8) | f1dscb->ds1ptrds[3];
rec = f1dscb->ds1ptrds[4];
rc = read_block (cif, cyl, head, rec,
(void *)&f3dscb, &len, NULL, NULL);
if (rc < 0) return -1;
if (rc > 0)
{
fprintf (stderr, _("HHCDU027E F3DSCB record not found\n"));
return -1;
}
/* Extract the next 13 extent descriptors */
extent[3] = f3dscb->ds3extnt[0];
extent[4] = f3dscb->ds3extnt[1];
extent[5] = f3dscb->ds3extnt[2];
extent[6] = f3dscb->ds3extnt[3];
extent[7] = f3dscb->ds3adext[0];
extent[8] = f3dscb->ds3adext[1];
extent[9] = f3dscb->ds3adext[2];
extent[10] = f3dscb->ds3adext[3];
extent[11] = f3dscb->ds3adext[4];
extent[12] = f3dscb->ds3adext[5];
extent[13] = f3dscb->ds3adext[6];
extent[14] = f3dscb->ds3adext[7];
extent[15] = f3dscb->ds3adext[8];
}
return 0;
} /* end function build_extent_array */
/*-------------------------------------------------------------------*/
/* Subroutine to calculate physical device track capacities */
/* Input: */
/* cif -> CKD image file descriptor structure */
/* used Number of bytes used so far on track, */
/* excluding home address and record 0 */
/* keylen Key length of proposed new record */
/* datalen Data length of proposed new record */
/* Output: */
/* newused Number of bytes used including proposed new record */
/* trkbaln Number of bytes remaining on track */
/* physlen Number of bytes on physical track (=ds4devtk) */
/* kbconst Overhead for non-last keyed block (=ds4devi) */
/* lbconst Overhead for last keyed block (=ds4devl) */
/* nkconst Overhead difference for non-keyed block (=ds4devk) */
/* devflag Device flag byte for VTOC (=ds4devfg) */
/* tolfact Device tolerance factor (=ds4devtl) */
/* maxdlen Maximum data length for non-keyed record 1 */
/* numrecs Number of records of specified length per track */
/* numhead Number of tracks per cylinder */
/* numcyls Number of cylinders per volume */
/* Note: */
/* A NULL address may be specified for any of the output */
/* fields if the output value is not required. */
/* The return value is 0 if the record will fit on the track, */
/* +1 if record will not fit on track, or -1 if unknown devtype */
/* Note: */
/* Although the virtual DASD image file contains no interrecord */
/* gaps, this subroutine performs its calculations taking into */
/* account the gaps that would exist on a real device, so that */
/* the track capacities of the real device are not exceeded. */
/*-------------------------------------------------------------------*/
DLL_EXPORT int capacity_calc (CIFBLK *cif, int used, int keylen, int datalen,
int *newused, int *trkbaln, int *physlen, int *kbconst,
int *lbconst, int *nkconst, BYTE*devflag, int *tolfact,
int *maxdlen, int *numrecs, int *numhead, int *numcyls)
{
CKDDEV *ckd; /* -> CKD device table entry */
int heads; /* Number of tracks/cylinder */
int cyls; /* Number of cyls/volume */
int trklen; /* Physical track length */
int maxlen; /* Maximum data length */
int devi, devl, devk; /* Overhead fields for VTOC */
BYTE devfg; /* Flag field for VTOC */
int devtl; /* Tolerance field for VTOC */
int b1; /* Bytes used by new record
when last record on track */
int b2; /* Bytes used by new record
when not last on track */
int nrecs; /* Number of record/track */
int c, d1, d2, x; /* 23xx/3330/3350 factors */
int f1, f2, f3, f4, f5, f6; /* 3380/3390/9345 factors */
int fl1, fl2, int1, int2; /* 3380/3390/9345 calculation*/
ckd = cif->devblk.ckdtab;
trklen = ckd->len;
maxlen = ckd->r1;
heads = ckd->heads;
cyls = ckd->cyls;
switch (ckd->formula) {
case -2: /* 2311, 2314 */
c = ckd->f1; x = ckd->f2; d1 = ckd->f3; d2 = ckd->f4;
b1 = keylen + datalen + (keylen == 0 ? 0 : c);
b2 = ((keylen + datalen) * d1 / d2)
+ (keylen == 0 ? 0 : c) + x;
nrecs = (trklen - b1)/b2 + 1;
devi = c + x; devl = c; devk = c; devtl = d1 / (d2/512);
devfg = 0x01;
break;
case -1: /* 3330, 3340, 3350 */
c = ckd->f1; x = ckd->f2;
b1 = b2 = keylen + datalen + (keylen == 0 ? 0 : c) + x;
nrecs = trklen / b2;
devi = c + x; devl = c + x; devk = c; devtl = 512;
devfg = 0x01;
break;
case 1: /* 3375, 3380 */
f1 = ckd->f1; f2 = ckd->f2; f3 = ckd->f3;
fl1 = datalen + f2;
fl2 = (keylen == 0 ? 0 : keylen + f3);
fl1 = ((fl1 + f1 - 1) / f1) * f1;
fl2 = ((fl2 + f1 - 1) / f1) * f1;
b1 = b2 = fl1 + fl2;
nrecs = trklen / b2;
devi = 0; devl = 0; devk = 0; devtl = 0; devfg = 0x30;
break;
case 2: /* 3390, 9345 */
f1 = ckd->f1; f2 = ckd->f2; f3 = ckd->f3;
f4 = ckd->f4; f5 = ckd->f5; f6 = ckd->f6;
int1 = ((datalen + f6) + (f5*2-1)) / (f5*2);
int2 = ((keylen + f6) + (f5*2-1)) / (f5*2);
fl1 = (f1 * f2) + datalen + f6 + f4*int1;
fl2 = (keylen == 0 ? 0 : (f1 * f3) + keylen + f6 + f4*int2);
fl1 = ((fl1 + f1 - 1) / f1) * f1;
fl2 = ((fl2 + f1 - 1) / f1) * f1;
b1 = b2 = fl1 + fl2;
nrecs = trklen / b2;
devi = 0; devl = 0; devk = 0; devtl = 0; devfg = 0x30;
break;
default:
return -1;
} /* end switch(ckd->formula) */
/* Return VTOC fields and maximum data length */
if (physlen != NULL) *physlen = trklen;
if (kbconst != NULL) *kbconst = devi;
if (lbconst != NULL) *lbconst = devl;
if (nkconst != NULL) *nkconst = devk;
if (devflag != NULL) *devflag = devfg;
if (tolfact != NULL) *tolfact = devtl;
if (maxdlen != NULL) *maxdlen = maxlen;
/* Return number of records per track */
if (numrecs != NULL) *numrecs = nrecs;
/* Return number of tracks per cylinder
and usual number of cylinders per volume */
if (numhead != NULL) *numhead = heads;
if (numcyls != NULL) *numcyls = cyls;
/* Return if record will not fit on the track */
if (used + b1 > trklen)
return +1;
/* Calculate number of bytes used and track balance */
if (newused != NULL)
*newused = used + b2;
if (trkbaln != NULL)
*trkbaln = (used + b2 > trklen) ? 0 : trklen - used - b2;
return 0;
} /* end function capacity_calc */
/*-------------------------------------------------------------------*/
/* Subroutine to create a CKD DASD image file */
/* Input: */
/* fname DASD image file name */
/* fseqn Sequence number of this file (1=first) */
/* devtype Device type */
/* heads Number of heads per cylinder */
/* trksize DADS image track length */
/* buf -> Track image buffer */
/* start Starting cylinder number for this file */
/* end Ending cylinder number for this file */
/* volcyls Total number of cylinders on volume */
/* volser Volume serial number */
/* comp Compression algorithm for a compressed device. */
/* Will be 0xff if device is not to be compressed. */
/* dasdcopy xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx */
/* nullfmt xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx */
/* rawflag create raw image (skip special track 0 handling) */
/*-------------------------------------------------------------------*/
static int
create_ckd_file (char *fname, int fseqn, U16 devtype, U32 heads,
U32 trksize, BYTE *buf, U32 start, U32 end,
U32 volcyls, char *volser, BYTE comp, int dasdcopy,
int nullfmt, int rawflag)
{
int rc; /* Return code */
off_t rcoff; /* Return value from lseek() */
int fd; /* File descriptor */
int i; /* Loop counter */
int n; /* Loop delimiter */
CKDDASD_DEVHDR devhdr; /* Device header */
CCKDDASD_DEVHDR cdevhdr; /* Compressed device header */
CCKD_L1ENT *l1=NULL; /* -> Primary lookup table */
CCKD_L2ENT l2[256]; /* Secondary lookup table */
CKDDASD_TRKHDR *trkhdr; /* -> Track header */
CKDDASD_RECHDR *rechdr; /* -> Record header */
U32 cyl; /* Cylinder number */
U32 head; /* Head number */
int trk = 0; /* Track number */
int trks; /* Total number tracks */
BYTE r; /* Record number */
BYTE *pos; /* -> Next position in buffer*/
U32 cpos = 0; /* Offset into cckd file */
int len = 0; /* Length used in track */
int keylen = 4; /* Length of keys */
int ipl1len = 24; /* Length of IPL1 data */
int ipl2len = 144; /* Length of IPL2 data */
int vol1len = 80; /* Length of VOL1 data */
int rec0len = 8; /* Length of R0 data */
int fileseq; /* CKD header sequence number*/
int highcyl; /* CKD header high cyl number*/
int x=O_EXCL; /* Open option */
CKDDEV *ckdtab; /* -> CKD table entry */
char pathname[MAX_PATH]; /* file path in host format */
/* Locate the CKD dasd table entry */
ckdtab = dasd_lookup (DASD_CKDDEV, NULL, devtype, volcyls);
if (ckdtab == NULL)
{
fprintf (stderr,
_("HHCDU028E device type %4.4X not found in dasd table\n"),
devtype);
return -1;
}
/* Set file sequence number to zero if this is the only file */
if (fseqn == 1 && end + 1 == volcyls)
fileseq = 0;
else
fileseq = fseqn;
/* Set high cylinder number to zero if this is the last file */
if (end + 1 == volcyls)
highcyl = 0;
else
highcyl = end;
cyl = end - start + 1;
/* Special processing for ckd and dasdcopy */
if (comp == 0xFF && dasdcopy)
{
highcyl = end;
if (end + 1 == volcyls)
fileseq = 0xff;
}
trks = volcyls * heads;
/* if `dasdcopy' > 1 then we can replace the existing file */
if (dasdcopy > 1) x = 0;
/* Create the DASD image file */
hostpath(pathname, fname, sizeof(pathname));
fd = hopen(pathname, O_WRONLY | O_CREAT | x | O_BINARY,
S_IRUSR | S_IWUSR | S_IRGRP);
if (fd < 0)
{
fprintf (stderr, _("HHCDU028E %s open error: %s\n"),
fname, strerror(errno));
return -1;
}
/* Create the device header */
memset(&devhdr, 0, CKDDASD_DEVHDR_SIZE);
if (comp == 0xff)
memcpy(devhdr.devid, "CKD_P370", 8);
else
memcpy(devhdr.devid, "CKD_C370", 8);
devhdr.heads[3] = (heads >> 24) & 0xFF;
devhdr.heads[2] = (heads >> 16) & 0xFF;
devhdr.heads[1] = (heads >> 8) & 0xFF;
devhdr.heads[0] = heads & 0xFF;
devhdr.trksize[3] = (trksize >> 24) & 0xFF;
devhdr.trksize[2] = (trksize >> 16) & 0xFF;
devhdr.trksize[1] = (trksize >> 8) & 0xFF;
devhdr.trksize[0] = trksize & 0xFF;
devhdr.devtype = devtype & 0xFF;
devhdr.fileseq = fileseq;
devhdr.highcyl[1] = (highcyl >> 8) & 0xFF;
devhdr.highcyl[0] = highcyl & 0xFF;
/* Write the device header */
rc = write (fd, &devhdr, CKDDASD_DEVHDR_SIZE);
if (rc < (int)CKDDASD_DEVHDR_SIZE)
{
fprintf (stderr, _("HHCDU029E %s device header write error: %s\n"),
fname, errno ? strerror(errno) : "incomplete");
return -1;
}
/* Build a compressed CKD file */
if (comp != 0xff)
{
/* Create the compressed device header */
memset(&cdevhdr, 0, CCKDDASD_DEVHDR_SIZE);
cdevhdr.vrm[0] = CCKD_VERSION;
cdevhdr.vrm[1] = CCKD_RELEASE;
cdevhdr.vrm[2] = CCKD_MODLVL;
if (cckd_endian()) cdevhdr.options |= CCKD_BIGENDIAN;
cdevhdr.options |= (CCKD_ORDWR | CCKD_NOFUDGE);
cdevhdr.numl1tab = (volcyls * heads + 255) / 256;
cdevhdr.numl2tab = 256;
cdevhdr.cyls[3] = (volcyls >> 24) & 0xFF;
cdevhdr.cyls[2] = (volcyls >> 16) & 0xFF;
cdevhdr.cyls[1] = (volcyls >> 8) & 0xFF;
cdevhdr.cyls[0] = volcyls & 0xFF;
cdevhdr.compress = comp;
cdevhdr.compress_parm = -1;
cdevhdr.nullfmt = nullfmt;
/* Write the compressed device header */
rc = write (fd, &cdevhdr, CCKDDASD_DEVHDR_SIZE);
if (rc < (int)CCKDDASD_DEVHDR_SIZE)
{
fprintf (stderr, _("HHCDU030E %s compressed device header "
"write error: %s\n"),
fname, errno ? strerror(errno) : "incomplete");
return -1;
}
/* Create the primary lookup table */
l1 = calloc (cdevhdr.numl1tab, CCKD_L1ENT_SIZE);
if (l1 == NULL)
{
fprintf (stderr, _("HHCDU031E Cannot obtain l1tab buffer: %s\n"),
strerror(errno));
return -1;
}
l1[0] = CCKD_L1TAB_POS + cdevhdr.numl1tab * CCKD_L1ENT_SIZE;
/* Write the primary lookup table */
rc = write (fd, l1, cdevhdr.numl1tab * CCKD_L1ENT_SIZE);
if (rc < (int)(cdevhdr.numl1tab * CCKD_L1ENT_SIZE))
{
fprintf (stderr, _("HHCDU032E %s primary lookup table "
"write error: %s\n"),
fname, errno ? strerror(errno) : "incomplete");
return -1;
}
/* Create the secondary lookup table */
memset (&l2, 0, CCKD_L2TAB_SIZE);
/* Write the seondary lookup table */
rc = write (fd, &l2, CCKD_L2TAB_SIZE);
if (rc < (int)CCKD_L2TAB_SIZE)
{
fprintf (stderr, _("HHCDU033E %s secondary lookup table "
"write error: %s\n"),
fname, errno ? strerror(errno) : "incomplete");
return -1;
}
cpos = l1[0] + CCKD_L2TAB_SIZE;
}
if (!dasdcopy)
{
/* Write each cylinder */
for (cyl = start; cyl <= end; cyl++)
{
/* Display progress message every 10 cylinders */
if (cyl && !(cyl % 10))
{
#ifdef EXTERNALGUI
if (extgui)
fprintf (stderr, "CYL=%u\n", cyl);
else
#endif /*EXTERNALGUI*/
fprintf (stderr, "Writing cylinder %u\r", cyl);
}
for (head = 0; head < heads; head++)
{
/* Clear the track to zeroes */
memset (buf, 0, trksize);
/* Build the track header */
trkhdr = (CKDDASD_TRKHDR*)buf;
trkhdr->bin = 0;
store_hw(&trkhdr->cyl, cyl);
store_hw(&trkhdr->head, head);
pos = buf + CKDDASD_TRKHDR_SIZE;
/* Build record zero */
r = 0;
rechdr = (CKDDASD_RECHDR*)pos;
pos += CKDDASD_RECHDR_SIZE;
store_hw(&rechdr->cyl, cyl);
store_hw(&rechdr->head, head);
rechdr->rec = r;
rechdr->klen = 0;
store_hw(&rechdr->dlen, rec0len);
pos += rec0len;
r++;
/* Track 0 contains IPL records and volume label */
if (!rawflag && fseqn == 1 && trk == 0)
{
/* Build the IPL1 record */
rechdr = (CKDDASD_RECHDR*)pos;
pos += CKDDASD_RECHDR_SIZE;
store_hw(&rechdr->cyl, cyl);
store_hw(&rechdr->head, head);
rechdr->rec = r;
rechdr->klen = keylen;
store_hw(&rechdr->dlen, ipl1len);
r++;
convert_to_ebcdic (pos, keylen, "IPL1");
pos += keylen;
memcpy (pos, iplpsw, 8);
memcpy (pos+8, iplccw1, 8);
memcpy (pos+16, iplccw2, 8);
pos += ipl1len;
/* Build the IPL2 record */
rechdr = (CKDDASD_RECHDR*)pos;
pos += CKDDASD_RECHDR_SIZE;
store_hw(&rechdr->cyl, cyl);
store_hw(&rechdr->head, head);
rechdr->rec = r;
rechdr->klen = keylen;
store_hw(&rechdr->dlen, ipl2len);
r++;
convert_to_ebcdic (pos, keylen, "IPL2");
pos += keylen;
pos += ipl2len;
/* Build the VOL1 record */
rechdr = (CKDDASD_RECHDR*)pos;
pos += CKDDASD_RECHDR_SIZE;
store_hw(&rechdr->cyl, cyl);
store_hw(&rechdr->head, head);
rechdr->rec = r;
rechdr->klen = keylen;
store_hw(&rechdr->dlen, vol1len);
r++;
convert_to_ebcdic (pos, keylen, "VOL1");
pos += keylen;
convert_to_ebcdic (pos, 4, "VOL1"); //VOL1
convert_to_ebcdic (pos+4, 6, volser); //volser
pos[10] = 0x40; //security
store_hw(pos+11,0); //vtoc CC
store_hw(pos+13,1); //vtoc HH
pos[15] = 0x01; //vtoc R
memset(pos+16, 0x40, 21); //reserved
convert_to_ebcdic (pos+37, 14, " HERCULES"); //ownerid
memset(pos+51, 0x40, 29); //reserved
pos += vol1len;
/* 9 4096 data blocks for linux volume */
if (nullfmt == CKDDASD_NULLTRK_FMT2)
{
for (i = 0; i < 9; i++)
{
rechdr = (CKDDASD_RECHDR*)pos;
pos += CKDDASD_RECHDR_SIZE;
store_hw(&rechdr->cyl, cyl);
store_hw(&rechdr->head, head);
rechdr->rec = r;
rechdr->klen = 0;
store_hw(&rechdr->dlen, 4096);
pos += 4096;
r++;
}
}
} /* end if(trk == 0) */
/* Track 1 for linux contains an empty VTOC */
else if (fseqn == 1 && trk == 1 && nullfmt == CKDDASD_NULLTRK_FMT2)
{
/* build format 4 dscb */
rechdr = (CKDDASD_RECHDR*)pos;
pos += CKDDASD_RECHDR_SIZE;
/* track 1 record 1 count */
store_hw(&rechdr->cyl, cyl);
store_hw(&rechdr->head, head);
rechdr->rec = r;
rechdr->klen = 44;
store_hw(&rechdr->dlen, 96);
r++;
/* track 1 record 1 key */
memset (pos, 0x04, 44);
pos += 44;
/* track 1 record 1 data */
memset (pos, 0, 96);
pos[0] = 0xf4; // DS4IDFMT
store_hw(pos + 6, 10); // DS4DSREC
pos[14] = trks > 65535 ? 0xa0 : 0; // DS4VTOCI
pos[15] = 1; // DS4NOEXT
store_hw(pos+18, volcyls); // DS4DSCYL
store_hw(pos+20, heads); // DS4DSTRK
store_hw(pos+22, ckdtab->len); // DS4DEVTK
pos[27] = 0x30; // DS4DEVFG
pos[30] = 0x0c; // DS4DEVDT
pos[61] = 0x01; // DS4VTOCE + 00
pos[66] = 0x01; // DS4VTOCE + 05
pos[70] = 0x01; // DS4VTOCE + 09
pos[81] = trks > 65535 ? 7 : 0; // DS4EFLVL
pos[85] = trks > 65535 ? 1 : 0; // DS4EFPTR + 03
pos[86] = trks > 65535 ? 3 : 0; // DS4EFPTR + 04
pos += 96;
/* build format 5 dscb */
rechdr = (CKDDASD_RECHDR*)pos;
pos += CKDDASD_RECHDR_SIZE;
/* track 1 record 1 count */
store_hw(&rechdr->cyl, cyl);
store_hw(&rechdr->head, head);
rechdr->rec = r;
rechdr->klen = 44;
store_hw(&rechdr->dlen, 96);
r++;
/* track 1 record 2 key */
memset (pos, 0x05, 4); // DS5KEYID
memset (pos+4, 0, 40);
if (trks <= 65535)
{
store_hw(pos+4, 2); // DS5AVEXT + 00
store_hw(pos+6, volcyls - 1); // DS5AVEXT + 02
pos[8] = heads - 2; // DS5AVEXT + 04
}
pos += 44;
/* track 1 record 2 data */
memset (pos, 0, 96);
pos[0] = 0xf5; // DS5FMTID
pos += 96;
/* build format 7 dscb */
if (trks > 65535)
{
rechdr = (CKDDASD_RECHDR*)pos;
pos += CKDDASD_RECHDR_SIZE;
/* track 1 record 3 count */
store_hw(&rechdr->cyl, cyl);
store_hw(&rechdr->head, head);
rechdr->rec = r;
rechdr->klen = 44;
store_hw(&rechdr->dlen, 96);
r++;
/* track 1 record 2 key */
memset (pos, 0x07, 4); // DS7KEYID
memset (pos+4, 0, 40);
store_fw(pos+4, 2); // DS7EXTNT + 00
store_fw(pos+8, trks - 1); // DS7EXTNT + 04
pos += 44;
/* track 1 record 2 data */
memset (pos, 0, 96);
pos[0] = 0xf7; // DS7FMTID
pos += 96;
}
n = 12 - r + 1;
for (i = 0; i < n; i++)
{
rechdr = (CKDDASD_RECHDR*)pos;
pos += CKDDASD_RECHDR_SIZE;
store_hw(&rechdr->cyl, cyl);
store_hw(&rechdr->head, head);
rechdr->rec = r;
rechdr->klen = 44;
store_hw(&rechdr->dlen, 96);
pos += 140;
r++;
}
}
/* Specific null track formatting */
else if (nullfmt == CKDDASD_NULLTRK_FMT0)
{
rechdr = (CKDDASD_RECHDR*)pos;
pos += CKDDASD_RECHDR_SIZE;
store_hw(&rechdr->cyl, cyl);
store_hw(&rechdr->head, head);
rechdr->rec = r;
rechdr->klen = 0;
store_hw(&rechdr->dlen, 0);
r++;
}
else if (nullfmt == CKDDASD_NULLTRK_FMT2)
{
/* Other linux tracks have 12 4096 data records */
for (i = 0; i < 12; i++)
{
rechdr = (CKDDASD_RECHDR*)pos;
pos += CKDDASD_RECHDR_SIZE;
store_hw(&rechdr->cyl, cyl);
store_hw(&rechdr->head, head);
rechdr->rec = r;
rechdr->klen = 0;
store_hw(&rechdr->dlen, 4096);
pos += 4096;
r++;
}
}
/* End-of-track marker */
memcpy (pos, eighthexFF, 8);
pos += 8;
/* Calculate length to write */
if (comp == 0xff)
len = (int)trksize;
else
{
len = (int)(pos - buf);
l2[trk].pos = cpos;
l2[trk].len = l2[trk].size = len;
cpos += len;
}
/* Write the track to the file */
rc = write (fd, buf, len);
if (rc != len)
{
fprintf (stderr,
_("HHCDU035E %s cylinder %u head %u "
"write error: %s\n"),
fname, cyl, head,
errno ? strerror(errno) : "incomplete");
return -1;
}
/* Exit if compressed disk and current track is 1 */
if (comp != 0xff && trk == 1) break;
trk++;
} /* end for(head) */
/* Exit if compressed disk */
if (comp != 0xff) break;
} /* end for(cyl) */
} /* `dasdcopy' bit is off */
else
cyl = end + 1;
/* Complete building the compressed file */
if (comp != 0xff)
{
cdevhdr.size = cdevhdr.used = cpos;
/* Rewrite the compressed device header */
rcoff = lseek (fd, CKDDASD_DEVHDR_SIZE, SEEK_SET);
if (rcoff == -1)
{
fprintf (stderr, _("HHCDU036E %s compressed device header "
"lseek error: %s\n"),
fname, strerror(errno));
return -1;
}
rc = write (fd, &cdevhdr, CCKDDASD_DEVHDR_SIZE);
if (rc < (int)CCKDDASD_DEVHDR_SIZE)
{
fprintf (stderr, _("HHCDU037E %s compressed device header "
"write error: %s\n"),
fname, errno ? strerror(errno) : "incomplete");
return -1;
}
/* Rewrite the secondary lookup table */
rcoff = lseek (fd, (off_t)l1[0], SEEK_SET);
if (rcoff == -1)
{
fprintf (stderr, _("HHCDU038E %s secondary lookup table "
"lseek error: %s\n"),
fname, strerror(errno));
return -1;
}
rc = write (fd, &l2, CCKD_L2TAB_SIZE);
if (rc < (int)CCKD_L2TAB_SIZE)
{
fprintf (stderr, _("HHCDU039E %s secondary lookup table "
"write error: %s\n"),
fname, errno ? strerror(errno) : "incomplete");
return -1;
}
rc = ftruncate(fd, (off_t)cdevhdr.size);
free (l1);
cyl = volcyls;
}
/* Close the DASD image file */
rc = close (fd);
if (rc < 0)
{
fprintf (stderr, _("HHCDU040E %s close error: %s\n"),
fname, strerror(errno));
return -1;
}
/* Display completion message */
fprintf (stderr,
_("HHCDU041I %u cylinders successfully written to file %s\n"),
cyl - start, fname);
return 0;
} /* end function create_ckd_file */
/*-------------------------------------------------------------------*/
/* Subroutine to create a CKD DASD image */
/* Input: */
/* fname DASD image file name */
/* devtype Device type */
/* heads Number of heads per cylinder */
/* maxdlen Maximum R1 record data length */
/* volcyls Total number of cylinders on volume */
/* volser Volume serial number */
/* comp Compression algorithm for a compressed device. */
/* Will be 0xff if device is not to be compressed. */
/* lfs build large (uncompressed) file (if supported) */
/* dasdcopy xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx */
/* nullfmt xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx */
/* rawflag create raw image (skip special track 0 handling) */
/* */
/* If the total number of cylinders exceeds the capacity of a 2GB */
/* file, then multiple CKD image files will be created, with the */
/* suffix _1, _2, etc suffixed to the specified file name. */
/* Otherwise a single file is created without a suffix. */
/*-------------------------------------------------------------------*/
DLL_EXPORT int
create_ckd (char *fname, U16 devtype, U32 heads, U32 maxdlen,
U32 volcyls, char *volser, BYTE comp, int lfs, int dasdcopy,
int nullfmt, int rawflag)
{
int i; /* Array subscript */
int rc; /* Return code */
char *s; /* String pointer */
int fileseq; /* File sequence number */
char sfname[FILENAME_MAX]; /* Suffixed name of this file*/
char *suffix; /* -> Suffix character */
U32 endcyl; /* Last cylinder of this file*/
U32 cyl; /* Cylinder number */
U32 cylsize; /* Cylinder size in bytes */
BYTE *buf; /* -> Track data buffer */
U32 mincyls; /* Minimum cylinder count */
U32 maxcyls; /* Maximum cylinder count */
U32 maxcpif; /* Maximum number of cylinders
in each CKD image file */
int rec0len = 8; /* Length of R0 data */
U32 trksize; /* DASD image track length */
/* Compute the DASD image track length */
trksize = sizeof(CKDDASD_TRKHDR)
+ sizeof(CKDDASD_RECHDR) + rec0len
+ sizeof(CKDDASD_RECHDR) + maxdlen
+ sizeof(eighthexFF);
trksize = ROUND_UP(trksize,512);
/* Compute minimum and maximum number of cylinders */
cylsize = trksize * heads;
mincyls = 1;
if (comp == 0xff && !lfs)
{
maxcpif = (0x7fffffff - CKDDASD_DEVHDR_SIZE + 1) / cylsize;
maxcyls = maxcpif * CKD_MAXFILES;
}
else
maxcpif = maxcyls = volcyls;
if (maxcyls > 65536) maxcyls = 65536;
/* Check for valid number of cylinders */
if (volcyls < mincyls || volcyls > maxcyls)
{
fprintf (stderr,
_("HHCDU042E Cylinder count %u is outside range %u-%u\n"),
volcyls, mincyls, maxcyls);
return -1;
}
/* Obtain track data buffer */
buf = malloc(trksize);
if (buf == NULL)
{
fprintf (stderr, _("HHCDU043E Cannot obtain track buffer: %s\n"),
strerror(errno));
return -1;
}
/* Display progress message */
fprintf (stderr,
_("HHCDU044I Creating %4.4X volume %s: %u cyls, "
"%u trks/cyl, %u bytes/track\n"),
devtype, rawflag ? "" : volser, volcyls, heads, trksize);
/* Copy the unsuffixed DASD image file name */
strcpy (sfname, fname);
suffix = NULL;
/* Create the suffixed file name if volume will exceed 2GB */
if (volcyls > maxcpif)
{
/* Look for last slash marking end of directory name */
s = strrchr (fname, '/');
if (s == NULL) s = fname;
/* Insert suffix before first dot in file name, or
append suffix to file name if there is no dot.
If the filename already has a place for the suffix
then use that. */
s = strchr (s, '.');
if (s != NULL)
{
i = s - fname;
if (i > 2 && fname[i-2] == '_')
suffix = sfname + i - 1;
else
{
strcpy (sfname + i, "_1");
strcat (sfname, fname + i);
suffix = sfname + i + 1;
}
}
else
{
if (strlen(sfname) < 2 || sfname[strlen(sfname)-2] == '_')
strcat (sfname, "_1");
suffix = sfname + strlen(sfname) - 1;
}
}
/* Create the DASD image files */
for (cyl = 0, fileseq = 1; cyl < volcyls;
cyl += maxcpif, fileseq++)
{
/* Insert the file sequence number in the file name */
if (suffix)
{
if (fileseq <= 9)
*suffix = '0' + fileseq;
else
*suffix = 'A' - 10 + fileseq;
}
/* Calculate the ending cylinder for this file */
if (cyl + maxcpif < volcyls)
endcyl = cyl + maxcpif - 1;
else
endcyl = volcyls - 1;
/* Create a CKD DASD image file */
rc = create_ckd_file (sfname, fileseq, devtype, heads,
trksize, buf, cyl, endcyl, volcyls, volser,
comp, dasdcopy, nullfmt, rawflag);
if (rc < 0) return -1;
}
/* Release data buffer */
free (buf);
return 0;
} /* end function create_ckd */
/*-------------------------------------------------------------------*/
/* Subroutine to create an FBA DASD image file */
/* Input: */
/* fname DASD image file name */
/* devtype Device type */
/* sectsz Sector size */
/* sectors Number of sectors */
/* volser Volume serial number */
/* comp Compression algorithm for a compressed device. */
/* Will be 0xff if device is not to be compressed. */
/* lfs build large (uncompressed) file (if supported) */
/* dasdcopy xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx */
/* rawflag create raw image (skip sector 1 VOL1 processing) */
/*-------------------------------------------------------------------*/
DLL_EXPORT int
create_fba (char *fname, U16 devtype, U32 sectsz, U32 sectors,
char *volser, BYTE comp, int lfs, int dasdcopy, int rawflag)
{
int rc; /* Return code */
int fd; /* File descriptor */
U32 sectnum; /* Sector number */
BYTE *buf; /* -> Sector data buffer */
U32 minsect; /* Minimum sector count */
U32 maxsect; /* Maximum sector count */
int x=O_EXCL; /* Open option */
char pathname[MAX_PATH]; /* file path in host format */
/* Special processing for compressed fba */
if (comp != 0xff)
{
rc = create_compressed_fba (fname, devtype, sectsz, sectors,
volser, comp, lfs, dasdcopy, rawflag);
return rc;
}
/* Compute minimum and maximum number of sectors */
minsect = 64;
maxsect = 0x80000000 / sectsz;
/* Check for valid number of sectors */
if (sectors < minsect || (!lfs && sectors > maxsect))
{
fprintf (stderr,
_("HHCDU045E Sector count %u is outside range %u-%u\n"),
sectors, minsect, maxsect);
return -1;
}
/* Obtain sector data buffer */
buf = malloc(sectsz);
if (buf == NULL)
{
fprintf (stderr, _("HHCDU046E Cannot obtain sector buffer: %s\n"),
strerror(errno));
return -1;
}
/* Display progress message */
fprintf (stderr,
_("HHCDU047I Creating %4.4X volume %s: "
"%u sectors, %u bytes/sector\n"),
devtype, rawflag ? "" : volser, sectors, sectsz);
/* if `dasdcopy' > 1 then we can replace the existing file */
if (dasdcopy > 1) x = 0;
/* Create the DASD image file */
hostpath(pathname, fname, sizeof(pathname));
fd = hopen(pathname, O_WRONLY | O_CREAT | x | O_BINARY,
S_IRUSR | S_IWUSR | S_IRGRP);
if (fd < 0)
{
fprintf (stderr, _("HHCDU048I %s open error: %s\n"),
fname, strerror(errno));
return -1;
}
/* If the `dasdcopy' bit is on then simply allocate the space */
if (dasdcopy)
{
off_t sz = sectors * sectsz;
rc = ftruncate (fd, sz);
if (rc < 0)
{
fprintf (stderr, _("HHCDU049E %s dasdcopy ftruncate error: %s\n"),
fname, strerror(errno));
return -1;
}
}
/* Write each sector */
else
{
for (sectnum = 0; sectnum < sectors; sectnum++)
{
/* Clear the sector to zeroes */
memset (buf, 0, sectsz);
/* Sector 1 contains the volume label */
if (!rawflag && sectnum == 1)
{
convert_to_ebcdic (buf, 4, "VOL1");
convert_to_ebcdic (buf+4, 6, volser);
} /* end if(sectnum==1) */
/* Display progress message every 100 sectors */
if ((sectnum % 100) == 0)
#ifdef EXTERNALGUI
{
if (extgui) fprintf (stderr, "BLK=%u\n", sectnum);
else fprintf (stderr, "Writing sector %u\r", sectnum);
}
#else /*!EXTERNALGUI*/
fprintf (stderr, "Writing sector %u\r", sectnum);
#endif /*EXTERNALGUI*/
/* Write the sector to the file */
rc = write (fd, buf, sectsz);
if (rc < (int)sectsz)
{
fprintf (stderr, _("HHCDU050E %s sector %u write error: %s\n"),
fname, sectnum,
errno ? strerror(errno) : "incomplete");
return -1;
}
} /* end for(sectnum) */
} /* `dasdcopy' bit is off */
/* Close the DASD image file */
rc = close (fd);
if (rc < 0)
{
fprintf (stderr, _("HHCDU051E %s close error: %s\n"),
fname, strerror(errno));
return -1;
}
/* Release data buffer */
free (buf);
/* Display completion message */
fprintf (stderr,
_("HHCDU052I %u sectors successfully written to file %s\n"),
sectors, fname);
return 0;
} /* end function create_fba */
/*-------------------------------------------------------------------*/
/* Subroutine to create a compressed FBA DASD image file */
/* Input: */
/* fname DASD image file name */
/* devtype Device type */
/* sectsz Sector size */
/* sectors Number of sectors */
/* volser Volume serial number */
/* comp Compression algorithm for a compressed device. */
/* lfs build large (uncompressed) file (if supported) */
/* dasdcopy xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx */
/* rawflag create raw image (skip sector 1 VOL1 processing) */
/*-------------------------------------------------------------------*/
int
create_compressed_fba (char *fname, U16 devtype, U32 sectsz,
U32 sectors, char *volser, BYTE comp, int lfs, int dasdcopy,
int rawflag)
{
int rc; /* Return code */
off_t rcoff; /* Return value from lseek() */
int fd; /* File descriptor */
CKDDASD_DEVHDR devhdr; /* Device header */
CCKDDASD_DEVHDR cdevhdr; /* Compressed device header */
int blkgrps; /* Number block groups */
int numl1tab, l1tabsz; /* Level 1 entries, size */
CCKD_L1ENT *l1; /* Level 1 table pointer */
CCKD_L2ENT l2[256]; /* Level 2 table */
unsigned long len2; /* Compressed buffer length */
BYTE buf2[256]; /* Compressed buffer */
BYTE buf[65536]; /* Buffer */
int x=O_EXCL; /* Open option */
char pathname[MAX_PATH]; /* file path in host format */
UNREFERENCED(lfs);
/* Calculate the size of the level 1 table */
blkgrps = (sectors / CFBA_BLOCK_NUM) + 1;
numl1tab = (blkgrps + 255) / 256;
l1tabsz = numl1tab * CCKD_L1ENT_SIZE;
if (l1tabsz > 65536)
{
fprintf (stderr, _("HHCDU053E File size too large: %" I64_FMT "ud [%d]\n"),
(U64)(sectors * sectsz), numl1tab);
return -1;
}
/* if `dasdcopy' > 1 then we can replace the existing file */
if (dasdcopy > 1) x = 0;
/* Create the DASD image file */
hostpath(pathname, fname, sizeof(pathname));
fd = hopen(pathname, O_WRONLY | O_CREAT | x | O_BINARY,
S_IRUSR | S_IWUSR | S_IRGRP);
if (fd < 0)
{
fprintf (stderr, _("HHCDU054E %s open error: %s\n"),
fname, strerror(errno));
return -1;
}
/* Display progress message */
fprintf (stderr,
_("HHCDU055I Creating %4.4X compressed volume %s: "
"%u sectors, %u bytes/sector\n"),
devtype, rawflag ? "" : volser, sectors, sectsz);
/* Write the device header */
memset (&devhdr, 0, CKDDASD_DEVHDR_SIZE);
memcpy (&devhdr.devid, "FBA_C370", 8);
rc = write (fd, &devhdr, CKDDASD_DEVHDR_SIZE);
if (rc < (int)CKDDASD_DEVHDR_SIZE)
{
fprintf (stderr, _("HHCDU056E %s devhdr write error: %s\n"),
fname, errno ? strerror(errno) : "incomplete");
return -1;
}
/* Write the compressed device header */
memset (&cdevhdr, 0, CCKDDASD_DEVHDR_SIZE);
cdevhdr.vrm[0] = CCKD_VERSION;
cdevhdr.vrm[1] = CCKD_RELEASE;
cdevhdr.vrm[2] = CCKD_MODLVL;
if (cckd_endian()) cdevhdr.options |= CCKD_BIGENDIAN;
cdevhdr.options |= (CCKD_ORDWR | CCKD_NOFUDGE);
cdevhdr.numl1tab = numl1tab;
cdevhdr.numl2tab = 256;
cdevhdr.cyls[3] = (sectors >> 24) & 0xFF;
cdevhdr.cyls[2] = (sectors >> 16) & 0xFF;
cdevhdr.cyls[1] = (sectors >> 8) & 0xFF;
cdevhdr.cyls[0] = sectors & 0xFF;
cdevhdr.compress = comp;
cdevhdr.compress_parm = -1;
rc = write (fd, &cdevhdr, CCKDDASD_DEVHDR_SIZE);
if (rc < (int)CCKDDASD_DEVHDR_SIZE)
{
fprintf (stderr, _("HHCDU057E %s cdevhdr write error: %s\n"),
fname, errno ? strerror(errno) : "incomplete");
return -1;
}
/* Write the level 1 table */
l1 = (CCKD_L1ENT *)&buf;
memset (l1, 0, l1tabsz);
l1[0] = CKDDASD_DEVHDR_SIZE + CCKDDASD_DEVHDR_SIZE + l1tabsz;
rc = write (fd, l1, l1tabsz);
if (rc < l1tabsz)
{
fprintf (stderr, _("HHCDU058E %s l1tab write error: %s\n"),
fname, errno ? strerror(errno) : "incomplete");
return -1;
}
/* Write the 1st level 2 table */
memset (&l2, 0, CCKD_L2TAB_SIZE);
l2[0].pos = CKDDASD_DEVHDR_SIZE + CCKDDASD_DEVHDR_SIZE + l1tabsz +
CCKD_L2TAB_SIZE;
rc = write (fd, &l2, CCKD_L2TAB_SIZE);
if (rc < (int)CCKD_L2TAB_SIZE)
{
fprintf (stderr, _("HHCDU059E %s l2tab write error: %s\n"),
fname, errno ? strerror(errno) : "incomplete");
return -1;
}
/* Write the 1st block group */
memset (&buf, 0, CKDDASD_DEVHDR_SIZE + CFBA_BLOCK_SIZE);
if (!rawflag)
{
convert_to_ebcdic (&buf[CKDDASD_TRKHDR_SIZE+sectsz], 4, "VOL1");
convert_to_ebcdic (&buf[CKDDASD_TRKHDR_SIZE+sectsz+4], 6, volser);
}
len2 = sizeof(buf2);
#ifdef HAVE_LIBZ
rc = compress2 (&buf2[0], &len2, &buf[CKDDASD_TRKHDR_SIZE],
CFBA_BLOCK_SIZE, -1);
if (comp && rc == Z_OK)
{
buf[0] = CCKD_COMPRESS_ZLIB;
rc = write (fd, &buf, CKDDASD_TRKHDR_SIZE);
if (rc < (int)CKDDASD_TRKHDR_SIZE)
{
fprintf (stderr, _("HHCDU060E %s block header write error: %s\n"),
fname, errno ? strerror(errno) : "incomplete");
return -1;
}
rc = write (fd, &buf2, len2);
if (rc < (int)len2)
{
fprintf (stderr, _("HHCDU061E %s block write error: %s\n"),
fname, errno ? strerror(errno) : "incomplete");
return -1;
}
l2[0].len = l2[0].size = CKDDASD_TRKHDR_SIZE + len2;
cdevhdr.size = cdevhdr.used = CKDDASD_DEVHDR_SIZE +
CCKDDASD_DEVHDR_SIZE + l1tabsz + CCKD_L2TAB_SIZE +
CKDDASD_TRKHDR_SIZE + len2;
}
else
#endif // defined(HAVE_LIBZ)
{
rc = write (fd, &buf, CKDDASD_TRKHDR_SIZE + CFBA_BLOCK_SIZE);
if (rc < (int)(CKDDASD_TRKHDR_SIZE + CFBA_BLOCK_SIZE))
{
fprintf (stderr, _("HHCDU062E %s block write error: %s\n"),
fname, errno ? strerror(errno) : "incomplete");
return -1;
}
l2[0].len = l2[0].size = CKDDASD_TRKHDR_SIZE + CFBA_BLOCK_SIZE;
cdevhdr.size = cdevhdr.used = CKDDASD_DEVHDR_SIZE +
CCKDDASD_DEVHDR_SIZE + l1tabsz + CCKD_L2TAB_SIZE +
CKDDASD_TRKHDR_SIZE + CFBA_BLOCK_SIZE;
}
/* Re-write the compressed device header */
rcoff = lseek (fd, CKDDASD_DEVHDR_SIZE, SEEK_SET);
if (rcoff < 0)
{
fprintf (stderr, _("HHCDU063E %s cdevhdr lseek error: %s\n"),
fname, strerror(errno));
return -1;
}
rc = write (fd, &cdevhdr, CCKDDASD_DEVHDR_SIZE);
if (rc < (int)CCKDDASD_DEVHDR_SIZE)
{
fprintf (stderr, _("HHCDU064E %s cdevhdr rewrite error: %s\n"),
fname, errno ? strerror(errno) : "incomplete");
return -1;
}
/* Re-write the 1st level 2 table */
rcoff = lseek (fd, CKDDASD_DEVHDR_SIZE + CCKDDASD_DEVHDR_SIZE + l1tabsz, SEEK_SET);
if (rcoff < 0)
{
fprintf (stderr, _("HHCDU065E %s l2tab lseek error: %s\n"),
fname, strerror(errno));
return -1;
}
rc = write (fd, &l2, CCKD_L2TAB_SIZE);
if (rc < (int)CCKD_L2TAB_SIZE)
{
fprintf (stderr, _("HHCDU066E %s l2tab rewrite error: %s\n"),
fname, errno ? strerror(errno) : "incomplete");
return -1;
}
/* Close the DASD image file */
rc = close (fd);
if (rc < 0)
{
fprintf (stderr, _("HHCDU067E %s close error: %s\n"),
fname, strerror(errno));
return -1;
}
/* Display completion message */
fprintf (stderr,
_("HHCDU068I %u sectors successfully written to file %s\n"),
sectors, fname);
return 0;
} /* end function create_compressed_fba */
int get_verbose_util(void)
{
return verbose;
}
DLL_EXPORT void set_verbose_util(int v)
{
verbose = v;
}
DLL_EXPORT int valid_dsname( const char *pszdsname )
{
int i;
int iLen = (int)strlen(pszdsname);
if ( iLen > 44 || iLen == 0 ) return FALSE;
for ( i = 0; i < iLen; i++ )
{
BYTE c = pszdsname[i];
if ( isalnum( c ) )
continue;
else if ( c == '$' )
continue;
else if ( c == '@' )
continue;
else if ( c == '#' )
continue;
else if ( c == '-' )
continue;
else if ( c == '.' )
continue;
else if ( c == '{' )
continue;
else if ( i > 1 && c == '\0' )
break;
else
return FALSE;
}
return TRUE;
}
|