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
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <unistd.h>
#include <strings.h>
#include <errno.h>
#include <libintl.h>
#include <libgen.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/int_types.h>
#include <sys/dkio.h>
#include <sys/cdio.h>
#include <sys/vtoc.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <sys/fs/udf_volume.h>
#include "ud_lib.h"
extern char *getfullrawname(char *);
static int32_t ud_get_ecma_ver(ud_handle_t, uint32_t);
static int32_t ud_get_fs_bsize(ud_handle_t, uint32_t, uint32_t *);
static int32_t ud_parse_fill_vds(ud_handle_t, struct vds *, uint32_t, uint32_t);
static int32_t ud_read_and_translate_lvd(ud_handle_t, uint32_t, uint32_t);
static int32_t ud_get_latest_lvid(ud_handle_t, uint32_t, uint32_t);
static int32_t ud_get_latest_fsd(ud_handle_t, uint16_t, uint32_t, uint32_t);
static uint16_t ud_crc(uint8_t *, int32_t);
static int32_t UdfTxName(uint16_t *, int32_t);
static int32_t UncompressUnicode(int32_t, uint8_t *, uint16_t *);
static int32_t ud_compressunicode(int32_t, int32_t, uint16_t *, uint8_t *);
static int32_t ud_convert2utf8(uint8_t *, uint8_t *, int32_t);
static int32_t ud_convert2utf16(uint8_t *, uint8_t *, int32_t);
int
ud_init(int fd, ud_handle_t *hp)
{
struct ud_handle *h;
if ((h = calloc(1, sizeof (struct ud_handle))) == NULL) {
return (ENOMEM);
}
h->fd = fd;
*hp = h;
return (0);
}
void
ud_fini(ud_handle_t h)
{
free(h);
}
/* ARGSUSED */
int32_t
ud_open_dev(ud_handle_t h, char *special, uint32_t flags)
{
char *temp;
struct stat i_stat, r_stat;
(void) bzero(&i_stat, sizeof (struct stat));
(void) bzero(&r_stat, sizeof (struct stat));
temp = special;
/*
* Get the stat structure
*/
if (stat(special, &i_stat) == 0) {
if ((i_stat.st_mode & S_IFMT) == S_IFBLK) {
/*
* Block device try to convert to raw device
*/
temp = getfullrawname(special);
/*
* Stat the converted device name and verify
* both the raw and block device belong to
* the same device
*/
if (stat(temp, &r_stat) < 0) {
temp = special;
} else {
if (((r_stat.st_mode & S_IFMT) == S_IFBLK) ||
(r_stat.st_rdev != i_stat.st_rdev)) {
temp = special;
}
}
}
}
/*
* Now finally open the device
*/
h->fd = open(temp, flags);
return (h->fd);
}
/* ARGSUSED */
void
ud_close_dev(ud_handle_t h)
{
/*
* Too simple Just close it
*/
(void) close(h->fd);
}
int32_t
ud_read_dev(ud_handle_t h, uint64_t offset, uint8_t *buf, uint32_t count)
{
/*
* Seek to the given offset
*/
if (lseek(h->fd, offset, SEEK_SET) == -1) {
return (1);
}
/*
* Read the required number of bytes
*/
if (read(h->fd, buf, count) != count) {
return (1);
}
return (0);
}
int32_t
ud_write_dev(ud_handle_t h, uint64_t offset, uint8_t *buf, uint32_t count)
{
/*
* Seek to the given offset
*/
if (lseek(h->fd, offset, SEEK_SET) == -1) {
return (1);
}
/*
* Read the appropriate number of bytes
*/
if (write(h->fd, buf, count) != count) {
return (1);
}
return (0);
}
/* ----- BEGIN Read and translate the on disk VDS to IN CORE format -------- */
int32_t
ud_fill_udfs_info(ud_handle_t h)
{
struct anch_vol_desc_ptr *avdp = NULL;
uint32_t offset = 0;
if (ioctl(h->fd, CDROMREADOFFSET, &offset) == -1) {
offset = 0;
}
h->udfs.flags = INVALID_UDFS;
h->udfs.ecma_version = ud_get_ecma_ver(h, offset);
if (h->udfs.ecma_version == UD_ECMA_UNKN) {
return (1);
}
h->udfs.lbsize = ud_get_fs_bsize(h, offset, &h->udfs.avdp_loc);
if (h->udfs.lbsize == 0) {
return (2);
}
h->udfs.avdp_len = lb_roundup(512, h->udfs.lbsize);
if ((avdp = (struct anch_vol_desc_ptr *)
malloc(h->udfs.lbsize)) == NULL) {
return (3);
}
if (ud_read_dev(h, h->udfs.avdp_loc * h->udfs.lbsize,
(uint8_t *)avdp, h->udfs.lbsize) != 0) {
free(avdp);
return (4);
}
if (ud_verify_tag(h, &avdp->avd_tag, UD_ANCH_VOL_DESC,
h->udfs.avdp_loc, 1, 0) != 0) {
free(avdp);
return (5);
}
h->udfs.mvds_loc = SWAP_32(avdp->avd_main_vdse.ext_loc);
h->udfs.mvds_len = SWAP_32(avdp->avd_main_vdse.ext_len);
h->udfs.rvds_loc = SWAP_32(avdp->avd_res_vdse.ext_loc);
h->udfs.rvds_len = SWAP_32(avdp->avd_res_vdse.ext_len);
free(avdp);
/*
* get information from mvds and rvds
*/
if (ud_parse_fill_vds(h, &h->udfs.mvds,
h->udfs.mvds_loc, h->udfs.mvds_len) == 0) {
h->udfs.flags |= VALID_MVDS;
}
if (ud_parse_fill_vds(h, &h->udfs.rvds,
h->udfs.rvds_loc, h->udfs.rvds_len) == 0) {
h->udfs.flags |= VALID_RVDS;
}
if ((h->udfs.flags & (VALID_MVDS | VALID_RVDS)) == 0) {
return (6);
}
/*
* If we are here we have
* a valid Volume Descriptor Seqence
* Read and understand lvd
*/
if (h->udfs.flags & VALID_MVDS) {
if (ud_read_and_translate_lvd(h, h->udfs.mvds.lvd_loc,
h->udfs.mvds.lvd_len) != 0) {
return (7);
}
} else {
if (ud_read_and_translate_lvd(h, h->udfs.rvds.lvd_loc,
h->udfs.rvds.lvd_len) != 0) {
return (8);
}
}
h->udfs.flags |= VALID_UDFS;
return (0);
}
static int32_t
ud_get_ecma_ver(ud_handle_t h, uint32_t offset)
{
uint8_t *buf;
uint64_t off;
uint64_t end_off;
struct nsr_desc *ndsc;
uint32_t ecma_ver = UD_ECMA_UNKN;
/*
* Allocate a buffer of size UD_VOL_REC_BSZ
*/
if ((buf = (uint8_t *)malloc(UD_VOL_REC_BSZ)) == NULL) {
/*
* Uh could not even allocate this much
*/
goto end;
}
/*
* Start from 32k and keep reading 2k blocks we
* should be able to find NSR if we have one by 256 * 2k bytes
*/
off = offset * 2048 + UD_VOL_REC_START;
end_off = offset * 2048 + UD_VOL_REC_END;
for (; off < end_off; off += UD_VOL_REC_BSZ) {
if (ud_read_dev(h, off, buf, UD_VOL_REC_BSZ) == 0) {
ndsc = (struct nsr_desc *)buf;
/*
* Is this either NSR02 or NSR03
*/
if ((ndsc->nsr_str_type == 0) &&
(ndsc->nsr_ver == 1) &&
(ndsc->nsr_id[0] == 'N') &&
(ndsc->nsr_id[1] == 'S') &&
(ndsc->nsr_id[2] == 'R') &&
(ndsc->nsr_id[3] == '0') &&
((ndsc->nsr_id[4] == '2') ||
(ndsc->nsr_id[4] == '3'))) {
(void) strncpy((char *)h->udfs.ecma_id,
(char *)ndsc->nsr_id, 5);
switch (ndsc->nsr_id[4]) {
case '2' :
/*
* ECMA 167/2
*/
ecma_ver = UD_ECMA_VER2;
goto end;
case '3' :
/*
* ECMA 167/3
*/
ecma_ver = UD_ECMA_VER3;
goto end;
}
}
}
}
end:
/*
* Cleanup
*/
free(buf);
return (ecma_ver);
}
static uint32_t last_block_index[] = {0, 0, 256, 2, 2 + 256,
150, 150 + 256, 152, 152 + 256};
static int32_t
ud_get_fs_bsize(ud_handle_t h, uint32_t offset, uint32_t *avd_loc)
{
uint64_t off;
int32_t index, bsize, shift, end_index;
uint32_t num_blocks, sub_blk;
uint8_t *buf = NULL;
struct anch_vol_desc_ptr *avdp;
if ((buf = (uint8_t *)malloc(MAXBSIZE)) == NULL) {
return (0);
}
/*
* If we could figure out the last block
* search at 256, N, N - 256 blocks
* otherwise just check at 256
*/
if (ud_get_num_blks(h, &num_blocks) != 0) {
end_index = 1;
num_blocks = 0;
} else {
end_index = sizeof (last_block_index) / 4;
}
for (index = 0; index < end_index; index++) {
sub_blk = last_block_index[index];
/*
* Start guessing from DEV_BSIZE to MAXBSIZE
*/
for (bsize = DEV_BSIZE, shift = 0;
bsize <= MAXBSIZE; bsize <<= 1, shift++) {
if (index == 0) {
/*
* Check if we atleast have 256 of bsize
* blocks on the device
*/
if ((end_index == 0) ||
(num_blocks > (256 << shift))) {
*avd_loc = 256;
if (bsize <= 2048) {
*avd_loc +=
offset * 2048 / bsize;
} else {
*avd_loc +=
offset / (bsize / 2048);
}
} else {
continue;
}
} else {
/*
* Calculate the bsize avd block
*/
if ((num_blocks) &&
(num_blocks > (sub_blk << shift))) {
*avd_loc = (num_blocks >> shift) -
sub_blk;
} else {
continue;
}
}
off = (uint64_t)*avd_loc * bsize;
/*
* Read bsize bytes at off
*/
if (ud_read_dev(h, off, buf, bsize) != 0) {
continue;
}
/*
* Check if we have a Anchor Volume Descriptor here
*/
/* LINTED */
avdp = (struct anch_vol_desc_ptr *)buf;
if (ud_verify_tag(h, &avdp->avd_tag,
UD_ANCH_VOL_DESC, *avd_loc, 1, 0) != 0) {
continue;
}
goto end;
}
}
end:
if (bsize > MAXBSIZE) {
bsize = 0;
*avd_loc = 0;
}
free(buf);
return (bsize);
}
static int32_t
ud_parse_fill_vds(ud_handle_t h, struct vds *v,
uint32_t vds_loc, uint32_t vds_len)
{
uint8_t *addr, *taddr, *eaddr;
uint16_t id;
int32_t i;
uint64_t off;
struct tag *tag;
struct pri_vol_desc *pvd;
struct log_vol_desc *lvd;
struct vol_desc_ptr *vds;
struct unall_spc_desc *usd;
begin:
if ((addr = (uint8_t *)malloc(vds_len)) == NULL) {
return (1);
}
off = vds_loc * h->udfs.lbsize;
if (ud_read_dev(h, off, addr, vds_len) != 0) {
goto end;
}
for (taddr = addr, eaddr = addr + h->udfs.mvds_len; taddr < eaddr;
taddr += h->udfs.lbsize, vds_loc ++) {
/* LINTED */
tag = (struct tag *)taddr;
id = SWAP_16(tag->tag_id);
/*
* If you cannot verify the tag just skip it
* This is not a fatal error
*/
if (ud_verify_tag(h, tag, id, vds_loc, 1, 0) != 0) {
continue;
}
switch (id) {
case UD_PRI_VOL_DESC :
/*
* Primary Volume Descriptor
*/
/* LINTED */
pvd = (struct pri_vol_desc *)taddr;
if ((v->pvd_len == 0) ||
(SWAP_32(pvd->pvd_vdsn) > v->pvd_vdsn)) {
v->pvd_vdsn = SWAP_32(pvd->pvd_vdsn);
v->pvd_loc = vds_loc;
v->pvd_len = h->udfs.lbsize;
}
break;
case UD_VOL_DESC_PTR :
/*
* Curent sequence is continued from
* the location pointed by vdp
*/
/* LINTED */
vds = (struct vol_desc_ptr *)taddr;
if (SWAP_32(vds->vdp_nvdse.ext_len) != 0) {
vds_loc = SWAP_32(vds->vdp_nvdse.ext_loc);
vds_len = SWAP_32(vds->vdp_nvdse.ext_len);
free(addr);
goto begin;
}
break;
case UD_IMPL_USE_DESC :
/*
* Implementation Use Volume Descriptor
*/
v->iud_loc = vds_loc;
v->iud_len = lb_roundup(512, h->udfs.lbsize);
break;
case UD_PART_DESC :
{
struct ud_part *p;
struct phdr_desc *ph;
struct part_desc *pd;
/*
* Partition Descriptor
*/
/* LINTED */
pd = (struct part_desc *)taddr;
for (i = 0; i < h->n_parts; i++) {
p = &h->part[i];
if ((SWAP_16(pd->pd_pnum) ==
p->udp_number) &&
(SWAP_32(pd->pd_vdsn) >
p->udp_seqno)) {
break;
}
}
v->part_loc[i] = vds_loc;
v->part_len[i] =
lb_roundup(512, h->udfs.lbsize);
p = &h->part[i];
p->udp_number = SWAP_16(pd->pd_pnum);
p->udp_seqno = SWAP_32(pd->pd_vdsn);
p->udp_access = SWAP_32(pd->pd_acc_type);
p->udp_start = SWAP_32(pd->pd_part_start);
p->udp_length = SWAP_32(pd->pd_part_length);
/* LINTED */
ph = (struct phdr_desc *)pd->pd_pc_use;
if (ph->phdr_ust.sad_ext_len) {
p->udp_flags = UDP_SPACETBLS;
p->udp_unall_loc = SWAP_32(ph->phdr_ust.sad_ext_loc);
p->udp_unall_len = SWAP_32(ph->phdr_ust.sad_ext_len);
p->udp_freed_loc = SWAP_32(ph->phdr_fst.sad_ext_loc);
p->udp_freed_len = SWAP_32(ph->phdr_fst.sad_ext_len);
} else {
p->udp_flags = UDP_BITMAPS;
p->udp_unall_loc = SWAP_32(ph->phdr_usb.sad_ext_loc);
p->udp_unall_len = SWAP_32(ph->phdr_usb.sad_ext_len);
p->udp_freed_loc = SWAP_32(ph->phdr_fsb.sad_ext_loc);
p->udp_freed_len = SWAP_32(ph->phdr_fsb.sad_ext_len);
}
if (i == h->n_parts) {
h->n_parts ++;
}
}
break;
case UD_LOG_VOL_DESC :
/*
* Logical Volume Descriptor
*/
/* LINTED */
lvd = (struct log_vol_desc *)taddr;
if ((v->lvd_len == 0) ||
(SWAP_32(lvd->lvd_vdsn) > v->lvd_vdsn)) {
v->lvd_vdsn = SWAP_32(lvd->lvd_vdsn);
v->lvd_loc = vds_loc;
v->lvd_len = ((uint32_t)
&((struct log_vol_desc *)0)->lvd_pmaps);
v->lvd_len =
lb_roundup(v->lvd_len, h->udfs.lbsize);
}
break;
case UD_UNALL_SPA_DESC :
/*
* Unallocated Space Descriptor
*/
/* LINTED */
usd = (struct unall_spc_desc *)taddr;
v->usd_loc = vds_loc;
v->usd_len = ((uint32_t)
&((unall_spc_desc_t *)0)->ua_al_dsc) +
SWAP_32(usd->ua_nad) *
sizeof (struct extent_ad);
v->usd_len = lb_roundup(v->usd_len, h->udfs.lbsize);
break;
case UD_TERM_DESC :
/*
* Success fully completed
*/
goto end;
default :
/*
* If you donot undetstand any tag just skip
* it. This is not a fatal error
*/
break;
}
}
end:
free(addr);
if ((v->pvd_len == 0) ||
(v->part_len[0] == 0) ||
(v->lvd_len == 0)) {
return (1);
}
return (0);
}
static int32_t
ud_read_and_translate_lvd(ud_handle_t h, uint32_t lvd_loc, uint32_t lvd_len)
{
caddr_t addr;
uint16_t fsd_prn;
uint32_t fsd_loc, fsd_len;
uint32_t lvds_loc, lvds_len;
uint64_t off;
struct log_vol_desc *lvd = NULL;
int32_t max_maps, i, mp_sz, index;
struct ud_map *m;
struct pmap_hdr *ph;
struct pmap_typ1 *typ1;
struct pmap_typ2 *typ2;
if (lvd_len == 0) {
return (1);
}
if ((lvd = (struct log_vol_desc *)
malloc(lvd_len)) == NULL) {
return (1);
}
off = lvd_loc * h->udfs.lbsize;
if (ud_read_dev(h, off, (uint8_t *)lvd, lvd_len) != 0) {
free(lvd);
return (1);
}
if (ud_verify_tag(h, &lvd->lvd_tag, UD_LOG_VOL_DESC,
lvd_loc, 1, 0) != 0) {
free(lvd);
return (1);
}
/*
* Take care of maps
*/
max_maps = SWAP_32(lvd->lvd_num_pmaps);
ph = (struct pmap_hdr *)lvd->lvd_pmaps;
for (h->n_maps = index = 0; index < max_maps; index++) {
m = &h->maps[h->n_maps];
switch (ph->maph_type) {
case MAP_TYPE1 :
/* LINTED */
typ1 = (struct pmap_typ1 *)ph;
m->udm_flags = UDM_MAP_NORM;
m->udm_vsn = SWAP_16(typ1->map1_vsn);
m->udm_pn = SWAP_16(typ1->map1_pn);
h->n_maps++;
break;
case MAP_TYPE2 :
/* LINTED */
typ2 = (struct pmap_typ2 *)ph;
if (strncmp(typ2->map2_pti.reg_id,
UDF_VIRT_PART, 23) == 0) {
m->udm_flags = UDM_MAP_VPM;
m->udm_vsn = SWAP_16(typ2->map2_vsn);
m->udm_pn = SWAP_16(typ2->map2_pn);
} else if (strncmp(typ2->map2_pti.reg_id,
UDF_SPAR_PART, 23) == 0) {
if ((SWAP_16(typ2->map2_pl) != 32) ||
(typ2->map2_nst < 1) ||
(typ2->map2_nst > 4)) {
break;
}
m->udm_flags = UDM_MAP_SPM;
m->udm_vsn = SWAP_16(typ2->map2_vsn);
m->udm_pn = SWAP_16(typ2->map2_pn);
m->udm_plen = SWAP_16(typ2->map2_pl);
m->udm_nspm = typ2->map2_nst;
m->udm_spsz = SWAP_32(typ2->map2_sest);
mp_sz = lb_roundup(m->udm_spsz, h->udfs.lbsize);
if ((addr = malloc(mp_sz * m->udm_nspm)) ==
NULL) {
break;
}
for (i = 0; i < m->udm_nspm; i++) {
m->udm_loc[i] =
SWAP_32(typ2->map2_st[index]);
m->udm_spaddr[i] = addr + i * mp_sz;
off = m->udm_loc[i] * h->udfs.lbsize;
if (ud_read_dev(h, off,
(uint8_t *)m->udm_spaddr[i],
mp_sz) != 0) {
m->udm_spaddr[i] = NULL;
continue;
}
}
}
h->n_maps++;
default :
break;
}
ph = (struct pmap_hdr *)(((uint8_t *)h) + ph->maph_length);
}
lvds_loc = SWAP_32(lvd->lvd_int_seq_ext.ext_loc);
lvds_len = SWAP_32(lvd->lvd_int_seq_ext.ext_len);
fsd_prn = SWAP_16(lvd->lvd_lvcu.lad_ext_prn);
fsd_loc = SWAP_32(lvd->lvd_lvcu.lad_ext_loc);
fsd_len = SWAP_32(lvd->lvd_lvcu.lad_ext_len);
free(lvd);
/*
* Get the latest LVID
*/
if (ud_get_latest_lvid(h, lvds_loc, lvds_len) != 0) {
return (1);
}
if (ud_get_latest_fsd(h, fsd_prn, fsd_loc, fsd_len) != 0) {
return (1);
}
return (0);
}
static int32_t
ud_get_latest_lvid(ud_handle_t h, uint32_t lvds_loc, uint32_t lvds_len)
{
uint8_t *addr, *taddr, *eaddr;
uint16_t id;
uint64_t off;
struct tag *tag;
struct log_vol_int_desc *lvid;
begin:
if ((addr = (uint8_t *)malloc(lvds_len)) == NULL) {
return (1);
}
off = lvds_loc * h->udfs.lbsize;
if (ud_read_dev(h, off, addr, lvds_len) != 0) {
goto end;
}
for (taddr = addr, eaddr = addr + h->udfs.mvds_len; taddr < eaddr;
taddr += h->udfs.lbsize, lvds_loc ++) {
/* LINTED */
tag = (struct tag *)taddr;
id = SWAP_16(tag->tag_id);
/*
* If you cannot verify the tag just skip it
* This is not a fatal error
*/
if (ud_verify_tag(h, tag, id, lvds_loc, 1, 0) != 0) {
continue;
}
switch (id) {
case UD_LOG_VOL_INT :
/*
* Logical Volume Integrity Descriptor
*/
/* LINTED */
lvid = (struct log_vol_int_desc *)taddr;
h->udfs.lvid_loc = lvds_loc;
h->udfs.lvid_len = ((uint32_t)
&((struct log_vol_int_desc *)0)->lvid_fst) +
SWAP_32(lvid->lvid_npart) * 8 +
SWAP_32(lvid->lvid_liu);
h->udfs.lvid_len = lb_roundup(h->udfs.lvid_len,
h->udfs.lbsize);
/*
* It seems we have a next integrity
* sequence
*/
if (SWAP_32(lvid->lvid_nie.ext_len) != 0) {
free(addr);
lvds_loc = SWAP_32(lvid->lvid_nie.ext_loc);
lvds_len = SWAP_32(lvid->lvid_nie.ext_len);
goto begin;
}
goto end;
case UD_TERM_DESC :
/*
* Success fully completed
*/
goto end;
default :
/*
* If you donot undetstand any tag just skip
* it. This is not a fatal error
*/
break;
}
}
end:
free(addr);
if (h->udfs.lvid_len == 0) {
return (1);
}
return (0);
}
static int32_t
ud_get_latest_fsd(ud_handle_t h, uint16_t fsd_prn,
uint32_t fsd_loc, uint32_t fsd_len)
{
uint8_t *addr, *taddr, *eaddr;
uint16_t id;
uint64_t off;
uint32_t fsds_loc, fsds_len;
struct tag *tag;
struct file_set_desc *fsd;
uint32_t old_fsn = 0;
begin:
h->udfs.fsds_prn = fsd_prn;
h->udfs.fsds_loc = fsd_loc;
h->udfs.fsds_len = fsd_len;
fsds_loc = ud_xlate_to_daddr(h, fsd_prn, fsd_loc);
fsds_len = lb_roundup(fsd_len, h->udfs.lbsize);
if ((addr = (uint8_t *)malloc(fsds_len)) == NULL) {
return (1);
}
off = fsds_loc * h->udfs.lbsize;
if (ud_read_dev(h, off, addr, fsds_len) != 0) {
goto end;
}
for (taddr = addr, eaddr = addr + h->udfs.mvds_len; taddr < eaddr;
taddr += h->udfs.lbsize, fsds_loc ++) {
/* LINTED */
tag = (struct tag *)taddr;
id = SWAP_16(tag->tag_id);
/*
* If you cannot verify the tag just skip it
* This is not a fatal error
*/
if (ud_verify_tag(h, tag, id, fsds_loc, 1, 0) != 0) {
continue;
}
switch (id) {
case UD_FILE_SET_DESC :
/* LINTED */
fsd = (struct file_set_desc *)taddr;
if ((h->udfs.fsd_len == 0) ||
(SWAP_32(fsd->fsd_fs_no) > old_fsn)) {
old_fsn = SWAP_32(fsd->fsd_fs_no);
h->udfs.fsd_loc = fsds_loc;
h->udfs.fsd_len = lb_roundup(512,
h->udfs.lbsize);
h->udfs.ricb_prn =
SWAP_16(fsd->fsd_root_icb.lad_ext_prn);
h->udfs.ricb_loc =
SWAP_32(fsd->fsd_root_icb.lad_ext_loc);
h->udfs.ricb_len =
SWAP_32(fsd->fsd_root_icb.lad_ext_len);
}
if (SWAP_32(fsd->fsd_next.lad_ext_len) != 0) {
fsd_prn = SWAP_16(fsd->fsd_next.lad_ext_prn);
fsd_loc = SWAP_32(fsd->fsd_next.lad_ext_loc);
fsd_len = SWAP_32(fsd->fsd_next.lad_ext_len);
goto begin;
}
break;
case UD_TERM_DESC :
/*
* Success fully completed
*/
goto end;
default :
/*
* If you donot undetstand any tag just skip
* it. This is not a fatal error
*/
break;
}
}
end:
free(addr);
if (h->udfs.fsd_len == 0) {
return (1);
}
return (0);
}
int32_t
ud_get_num_blks(ud_handle_t h, uint32_t *blkno)
{
struct vtoc vtoc;
struct dk_cinfo dki_info;
int32_t error;
/*
* Get VTOC from driver
*/
if ((error = ioctl(h->fd, DKIOCGVTOC, (intptr_t)&vtoc)) != 0) {
return (error);
}
/*
* Verify if is proper
*/
if (vtoc.v_sanity != VTOC_SANE) {
return (EINVAL);
}
/*
* Get dk_cinfo from driver
*/
if ((error = ioctl(h->fd, DKIOCINFO, (intptr_t)&dki_info)) != 0) {
return (error);
}
if (dki_info.dki_partition >= V_NUMPAR) {
return (EINVAL);
}
/*
* Return the size of the partition
*/
*blkno = vtoc.v_part[dki_info.dki_partition].p_size;
return (0);
}
uint32_t
ud_xlate_to_daddr(ud_handle_t h, uint16_t prn, uint32_t blkno)
{
int32_t i;
struct ud_map *m;
struct ud_part *p;
if (prn < h->n_maps) {
m = &h->maps[prn];
for (i = 0; i < h->n_parts; i++) {
p = &h->part[i];
if (m->udm_pn == p->udp_number) {
return (p->udp_start + blkno);
}
}
}
return (0);
}
/* ------ END Read and translate the on disk VDS to IN CORE format -------- */
int32_t
ud_verify_tag(ud_handle_t h, struct tag *tag, uint16_t id,
uint32_t blockno, int32_t do_crc, int32_t print_msg)
{
int32_t i;
uint8_t *addr, cksum = 0;
uint16_t crc;
/*
* Verify Tag Identifier
*/
if (tag->tag_id != SWAP_16(id)) {
if (print_msg != 0) {
(void) fprintf(stderr,
gettext("tag does not verify tag %x req %x\n"),
SWAP_16(tag->tag_id), id);
}
return (1);
}
/*
* Verify Tag Descriptor Version
*/
if (SWAP_16(tag->tag_desc_ver) != h->udfs.ecma_version) {
if (print_msg != 0) {
(void) fprintf(stderr,
gettext("tag version does not match with "
"NSR descriptor version TAG %x NSR %x\n"),
SWAP_16(tag->tag_desc_ver),
h->udfs.ecma_version);
}
return (1);
}
/*
* Caliculate Tag Checksum
*/
addr = (uint8_t *)tag;
for (i = 0; i <= 15; i++) {
if (i != 4) {
cksum += addr[i];
}
}
/*
* Verify Tag Checksum
*/
if (cksum != tag->tag_cksum) {
if (print_msg != 0) {
(void) fprintf(stderr,
gettext("Checksum Does not Verify TAG"
" %x CALC %x\n"), tag->tag_cksum, cksum);
}
return (1);
}
/*
* Do we want to do crc
*/
if (do_crc) {
if (tag->tag_crc_len) {
/*
* Caliculate CRC for the descriptor
*/
crc = ud_crc(addr + 0x10, SWAP_16(tag->tag_crc_len));
/*
* Verify CRC
*/
if (crc != SWAP_16(tag->tag_crc)) {
if (print_msg != 0) {
(void) fprintf(stderr,
gettext("CRC Does not verify"
" TAG %x CALC %x %x\n"),
SWAP_16(tag->tag_crc),
crc, addr);
}
}
}
/*
* Verify Tag Location
*/
if (SWAP_32(blockno) != tag->tag_loc) {
if (print_msg != 0) {
(void) fprintf(stderr,
gettext("Tag Location Does not verify"
" blockno %x tag_blockno %x\n"),
blockno, SWAP_32(tag->tag_loc));
}
}
}
return (0);
}
/* ARGSUSED1 */
void
ud_make_tag(ud_handle_t h, struct tag *tag, uint16_t tag_id,
uint32_t blkno, uint16_t crc_len)
{
int32_t i;
uint16_t crc;
uint8_t *addr, cksum = 0;
tag->tag_id = SWAP_16(tag_id);
tag->tag_desc_ver = SWAP_16(h->udfs.ecma_version);
tag->tag_cksum = 0;
tag->tag_res = 0;
/*
* Calicualte and assign CRC, CRC_LEN
*/
addr = (uint8_t *)tag;
crc = ud_crc(addr + 0x10, crc_len);
tag->tag_crc = SWAP_16(crc);
tag->tag_crc_len = SWAP_16(crc_len);
tag->tag_loc = SWAP_32(blkno);
/*
* Caliculate Checksum
*/
for (i = 0; i <= 15; i++) {
cksum += addr[i];
}
/*
* Assign Checksum
*/
tag->tag_cksum = cksum;
}
/* **************** udf specific subroutines *********************** */
static uint16_t ud_crc_table[256] = {
0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7,
0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF,
0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6,
0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE,
0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 0x5485,
0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE, 0xF5CF, 0xC5AC, 0xD58D,
0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4,
0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC,
0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823,
0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B,
0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12,
0xDBFD, 0xCBDC, 0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A,
0x6CA6, 0x7C87, 0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41,
0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD, 0xAD2A, 0xBD0B, 0x8D68, 0x9D49,
0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70,
0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A, 0x9F59, 0x8F78,
0x9188, 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E, 0xE16F,
0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067,
0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E,
0x02B1, 0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256,
0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D,
0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 0xF77E, 0xC71D, 0xD73C,
0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634,
0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9, 0xB98A, 0xA9AB,
0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3,
0xCB7D, 0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A,
0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92,
0xFD2E, 0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9,
0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1,
0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8,
0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0
};
static uint16_t
ud_crc(uint8_t *addr, int32_t len)
{
uint16_t crc = 0;
while (len-- > 0) {
crc = ud_crc_table[(crc >> 8 ^ *addr++) & 0xff] ^ (crc<<8);
}
return (crc);
}
#define MAXNAMLEN 0x200
#define POUND 0x0023
#define DOT 0x002E
#define SLASH 0x002F
#define UNDERBAR 0x005F
static uint16_t htoc[16] = {'0', '1', '2', '3',
'4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
/*
* unicode is the string of 16-bot characters
* length is the number of 16-bit characters
*/
static int32_t
UdfTxName(uint16_t *unicode, int32_t count)
{
int32_t i, j, k, lic, make_crc, dot_loc;
uint16_t crc;
if ((unicode[0] == DOT) &&
((count == 1) || ((count == 2) && (unicode[1] == DOT)))) {
crc = DOT;
if (count == 2) {
crc += DOT;
}
unicode[0] = UNDERBAR;
unicode[1] = POUND;
unicode[2] = htoc[(uint16_t)(crc & 0xf000) >> 12];
unicode[3] = htoc[(uint16_t)(crc & 0xf00) >> 8];
unicode[4] = htoc[(uint16_t)(crc & 0xf0) >> 4];
unicode[5] = htoc[crc & 0xf];
return (6);
}
crc = 0;
j = make_crc = 0;
lic = dot_loc = -1;
for (i = 0; i < count; i++) {
if (make_crc) {
crc += unicode[i];
}
if (unicode[i] == DOT) {
dot_loc = j;
}
if ((unicode[i] == SLASH) ||
(unicode[i] == 0)) {
if (make_crc == 0) {
for (k = 0; k <= i; k++) {
crc += unicode[k];
}
make_crc = 1;
}
if (lic != (i - 1)) {
unicode[j++] = UNDERBAR;
}
lic = i;
} else {
unicode[j++] = unicode[i];
}
}
if (make_crc) {
if (dot_loc != -1) {
if ((j + 5) > MAXNAMLEN) {
if ((j - dot_loc + 5) > MAXNAMLEN) {
j = MAXNAMLEN - 5 + dot_loc;
for (k = MAXNAMLEN;
j >= dot_loc; k --, j--) {
unicode[k] = unicode[j];
}
k = 0;
} else {
for (k = MAXNAMLEN;
j >= dot_loc; k--, j--) {
unicode[k] = unicode[j];
}
k -= 4;
}
j = MAXNAMLEN;
} else {
for (k = j; k >= dot_loc; k--) {
unicode[k + 5] = unicode[k];
}
k = dot_loc;
j += 5;
}
} else {
if ((j + 5) > MAXNAMLEN) {
j = MAXNAMLEN;
k = MAXNAMLEN - 5;
} else {
k = j;
j += 5;
}
}
unicode[k++] = POUND;
unicode[k++] = htoc[(uint16_t)(crc & 0xf000) >> 12];
unicode[k++] = htoc[(uint16_t)(crc & 0xf00) >> 8];
unicode[k++] = htoc[(uint16_t)(crc & 0xf0) >> 4];
unicode[k++] = htoc[crc & 0xf];
}
return (j);
}
/*
* Assumes the output buffer is large
* enough to hold the uncompressed
* code
*/
static int32_t
UncompressUnicode(
int32_t numberOfBytes, /* (Input) number of bytes read from media. */
uint8_t *UDFCompressed, /* (Input) bytes read from media. */
uint16_t *unicode) /* (Output) uncompressed unicode characters. */
{
int32_t compID;
int32_t returnValue, unicodeIndex, byteIndex;
/*
* Use UDFCompressed to store current byte being read.
*/
compID = UDFCompressed[0];
/* First check for valid compID. */
if (compID != 8 && compID != 16) {
returnValue = -1;
} else {
unicodeIndex = 0;
byteIndex = 1;
/* Loop through all the bytes. */
while (byteIndex < numberOfBytes) {
if (compID == 16) {
/*
* Move the first byte to the
* high bits of the unicode char.
*/
unicode[unicodeIndex] =
UDFCompressed[byteIndex++] << 8;
} else {
unicode[unicodeIndex] = 0;
}
if (byteIndex < numberOfBytes) {
/*
* Then the next byte to the low bits.
*/
unicode[unicodeIndex] |=
UDFCompressed[byteIndex++];
}
unicodeIndex++;
}
returnValue = unicodeIndex;
}
return (returnValue);
}
static int32_t
ud_compressunicode(
int32_t numberOfChars, /* (Input) number of unicode characters. */
int32_t compID, /* (Input) compression ID to be used. */
uint16_t *unicode, /* (Input) unicode characters to compress. */
uint8_t *UDFCompressed) /* (Output) compressed string, as bytes. */
{
int32_t byteIndex;
if (compID != 8 && compID != 16) {
/*
* Unsupported compression ID !
*/
byteIndex = -1;
} else {
/*
* Place compression code in first byte.
*/
UDFCompressed[0] = (uint8_t)compID;
(void) strncpy((caddr_t)&UDFCompressed[1],
(caddr_t)unicode, numberOfChars);
byteIndex = numberOfChars + 1;
}
return (byteIndex);
}
static int32_t
ud_convert2utf8(uint8_t *ibuf, uint8_t *obuf, int32_t length)
{
int i, size;
uint16_t *buf;
/* LINTED */
buf = (uint16_t *)obuf;
size = UncompressUnicode(length, ibuf, buf);
size = UdfTxName(buf, size);
for (i = 0; i < size; i++) {
obuf[i] = (uint8_t)buf[i];
}
obuf[i] = '\0';
return (size);
}
static int32_t
ud_convert2utf16(uint8_t *ibuf, uint8_t *obuf, int32_t length)
{
int32_t comp_len;
uint16_t *ptr;
/* LINTED */
ptr = (uint16_t *)ibuf;
comp_len = ud_compressunicode(length, 8, ptr, obuf);
return (comp_len);
}
/*
* Assumption code set is zero in udfs
*/
void
ud_convert2local(int8_t *ibuf, int8_t *obuf, int32_t length)
{
wchar_t buf4c[128];
int32_t i, comp, index;
/*
* Special uncompress code
* written to accomodate solaris wchar_t
*/
comp = ibuf[0];
for (i = 0, index = 1; i < length; i++) {
if (comp == 16) {
buf4c[i] = ibuf[index++] << 8;
} else {
buf4c[i] = 0;
}
if (index < length) {
buf4c[i] |= ibuf[index++];
}
}
(void) wcstombs((char *)obuf, buf4c, 128);
}
/* ------------ Routines to print basic structures Part 1 ---------------- */
void
print_charspec(FILE *fout, char *name, struct charspec *cspec)
{
int i = 0;
(void) fprintf(fout,
"%s : %x - \"", name, cspec->cs_type);
for (i = 0; i < 63; i++) {
(void) fprintf(fout,
"%c", cspec->cs_info[i]);
}
(void) fprintf(fout, "\n");
}
/* ARGSUSED */
void
print_dstring(FILE *fout, char *name, uint16_t cset, char *bufc, uint8_t length)
{
int8_t bufmb[1024];
ud_convert2local(bufc, bufmb, length);
(void) fprintf(fout,
"%s %s\n", name, bufmb);
}
void
set_dstring(dstring_t *dp, char *cp, int32_t len)
{
int32_t length;
bzero(dp, len);
length = strlen(cp);
if (length > len - 1) {
length = len - 1;
}
(void) strncpy(dp, cp, length);
dp[len - 1] = length;
}
void
print_tstamp(FILE *fout, char *name, tstamp_t *ts)
{
(void) fprintf(fout, "%s tz : %d yr : %d mo : %d da : %d "
"Time : %d : %d : %d : %d : %d : %d\n", name,
SWAP_16(ts->ts_tzone), SWAP_16(ts->ts_year), ts->ts_month,
ts->ts_day, ts->ts_hour, ts->ts_min, ts->ts_sec, ts->ts_csec,
ts->ts_husec, ts->ts_usec);
}
void
make_regid(ud_handle_t h, struct regid *reg, char *id, int32_t type)
{
reg->reg_flags = 0;
(void) strncpy(reg->reg_id, id, 23);
if (type == REG_DOM_ID) {
struct dom_id_suffix *dis;
/* LINTED */
dis = (struct dom_id_suffix *)reg->reg_ids;
dis->dis_udf_revison = SWAP_16(h->udfs.ma_write);
dis->dis_domain_flags = 0;
} else if (type == REG_UDF_ID) {
struct udf_id_suffix *uis;
/* LINTED */
uis = (struct udf_id_suffix *)reg->reg_ids;
uis->uis_udf_revision = SWAP_16(h->udfs.ma_write);
uis->uis_os_class = OS_CLASS_UNIX;
uis->uis_os_identifier = OS_IDENTIFIER_SOLARIS;
} else if (type == REG_UDF_II) {
struct impl_id_suffix *iis;
iis = (struct impl_id_suffix *)reg->reg_ids;
iis->iis_os_class = OS_CLASS_UNIX;
iis->iis_os_identifier = OS_IDENTIFIER_SOLARIS;
}
}
void
print_regid(FILE *fout, char *name, struct regid *reg, int32_t type)
{
(void) fprintf(fout, "%s : 0x%x : \"%s\" :",
name, reg->reg_flags, reg->reg_id);
if (type == REG_DOM_ID) {
struct dom_id_suffix *dis;
/* LINTED */
dis = (struct dom_id_suffix *)reg->reg_ids;
(void) fprintf(fout, " 0x%x : %s : %s\n",
SWAP_16(dis->dis_udf_revison),
(dis->dis_domain_flags & PROTECT_SOFT_WRITE) ?
"HW Protect" : "No HW Write Protect",
(dis->dis_domain_flags & PROTECT_HARD_WRITE) ?
"SW Protect" : "No SW Protect");
} else if (type == REG_UDF_ID) {
struct udf_id_suffix *uis;
/* LINTED */
uis = (struct udf_id_suffix *)reg->reg_ids;
(void) fprintf(fout,
" 0x%x : OS Class 0x%x : OS Identifier 0x%x\n",
SWAP_16(uis->uis_udf_revision),
uis->uis_os_class, uis->uis_os_identifier);
} else {
struct impl_id_suffix *iis;
iis = (struct impl_id_suffix *)reg->reg_ids;
(void) fprintf(fout,
" OS Class 0x%x : OS Identifier 0x%x\n",
iis->iis_os_class, iis->iis_os_identifier);
}
}
#ifdef OLD
void
print_regid(FILE *fout, char *name, struct regid *reg)
{
(void) fprintf(fout, "%s : 0x%x : \"%s\" :",
name, reg->reg_flags, reg->reg_id);
if (strncmp(reg->reg_id, "*OSTA UDF Compliant", 19) == 0) {
(void) fprintf(fout, " 0x%x : %s : %s\n",
reg->reg_ids[0] | (reg->reg_ids[1] << 8),
(reg->reg_ids[2] & 1) ?
"HW Protect" : "No HW Write Protect",
(reg->reg_ids[2] & 2) ?
"SW Protect" : "No SW Protect");
} else if ((strncmp(reg->reg_id, "*UDF Virtual Partition", 22) == 0) ||
(strncmp(reg->reg_id, "*UDF Sparable Partition", 23) == 0) ||
(strncmp(reg->reg_id, "*UDF Virtual Alloc Tbl", 22) == 0) ||
(strncmp(reg->reg_id, "*UDF Sparing Table", 18) == 0)) {
(void) fprintf(fout,
" 0x%x : OS Class 0x%x : OS Identifier 0x%x\n",
reg->reg_ids[0] | (reg->reg_ids[1] << 8),
reg->reg_ids[2], reg->reg_ids[3]);
} else {
(void) fprintf(fout,
" OS Class 0x%x : OS Identifier 0x%x\n",
reg->reg_ids[0], reg->reg_ids[1]);
}
}
#endif
/* ------------ Routines to print basic structures Part 2 ---------------- */
/*
* Part 2
* This part is OS specific and is currently
* not supported
*/
/* ------------ Routines to print basic structures Part 3 ---------------- */
void
print_ext_ad(FILE *fout, char *name, struct extent_ad *ead)
{
(void) fprintf(fout,
"%s EAD Len %x Loc %x\n",
name, SWAP_32(ead->ext_len), SWAP_32(ead->ext_loc));
}
void
print_tag(FILE *fout, struct tag *tag)
{
(void) fprintf(fout,
"tag_id : %x ver : %x cksum : %x "
"sno : %x crc : %x crc_len : %x loc : %x\n",
SWAP_16(tag->tag_id), SWAP_16(tag->tag_desc_ver),
tag->tag_cksum, SWAP_16(tag->tag_sno),
SWAP_16(tag->tag_crc), SWAP_16(tag->tag_crc_len),
SWAP_32(tag->tag_loc));
}
void
print_pvd(FILE *fout, struct pri_vol_desc *pvd)
{
(void) fprintf(fout,
"\n\t\t\tPrimary Volume Descriptor\n");
print_tag(fout, &pvd->pvd_tag);
(void) fprintf(fout, "vdsn : %x vdn : %x\n",
SWAP_32(pvd->pvd_vdsn), SWAP_32(pvd->pvd_pvdn));
print_dstring(fout, "volid : ", pvd->pvd_desc_cs.cs_type,
pvd->pvd_vol_id, 32);
(void) fprintf(fout,
"vsn : %x mvsn : %x il : %x mil :"
" %x csl : %x mcsl %x\n",
SWAP_16(pvd->pvd_vsn), SWAP_16(pvd->pvd_mvsn),
SWAP_16(pvd->pvd_il), SWAP_16(pvd->pvd_mil),
SWAP_32(pvd->pvd_csl), SWAP_32(pvd->pvd_mcsl));
print_dstring(fout, "vsid :", pvd->pvd_desc_cs.cs_type,
pvd->pvd_vsi, 128);
print_charspec(fout, "desc_cs", &pvd->pvd_desc_cs);
print_charspec(fout, "exp_cs", &pvd->pvd_exp_cs);
print_ext_ad(fout, "val ", &pvd->pvd_vol_abs);
print_ext_ad(fout, "vcnl ", &pvd->pvd_vcn);
print_regid(fout, "ai", &pvd->pvd_appl_id, REG_UDF_II);
print_regid(fout, "ii", &pvd->pvd_ii, REG_UDF_II);
(void) fprintf(fout, "pvdsl : %x flags : %x\n",
SWAP_32(pvd->pvd_pvdsl),
SWAP_16(pvd->pvd_flags));
}
void
print_avd(FILE *fout, struct anch_vol_desc_ptr *avdp)
{
(void) fprintf(fout,
"\n\t\t\tAnchor Volume Descriptor\n");
print_tag(fout, &avdp->avd_tag);
print_ext_ad(fout, "Main Volume Descriptor Sequence : ",
&avdp->avd_main_vdse);
print_ext_ad(fout, "Reserve Volume Descriptor Sequence : ",
&avdp->avd_res_vdse);
}
void
print_vdp(FILE *fout, struct vol_desc_ptr *vdp)
{
(void) fprintf(fout,
"\n\t\t\tVolume Descriptor Pointer\n");
print_tag(fout, &vdp->vdp_tag);
(void) fprintf(fout, "vdsn : %x ",
SWAP_32(vdp->vdp_vdsn));
print_ext_ad(fout, "vdse ", &vdp->vdp_nvdse);
}
void
print_iuvd(FILE *fout, struct iuvd_desc *iuvd)
{
(void) fprintf(fout,
"\n\t\t\tImplementation Use Volume Descriptor\n");
print_tag(fout, &iuvd->iuvd_tag);
(void) fprintf(fout,
"vdsn : %x ", SWAP_32(iuvd->iuvd_vdsn));
print_regid(fout, "Impl Id : ", &iuvd->iuvd_ii, REG_UDF_ID);
print_charspec(fout, "cset ", &iuvd->iuvd_cset);
print_dstring(fout, "lvi : ", iuvd->iuvd_cset.cs_type,
iuvd->iuvd_lvi, 128);
print_dstring(fout, "ifo1 : ", iuvd->iuvd_cset.cs_type,
iuvd->iuvd_ifo1, 36);
print_dstring(fout, "ifo2 : ", iuvd->iuvd_cset.cs_type,
iuvd->iuvd_ifo2, 36);
print_dstring(fout, "ifo3 : ", iuvd->iuvd_cset.cs_type,
iuvd->iuvd_ifo3, 36);
print_regid(fout, "iid ", &iuvd->iuvd_iid, REG_UDF_II);
}
void
print_part(FILE *fout, struct part_desc *pd)
{
(void) fprintf(fout,
"\n\t\t\tPartition Descriptor\n");
print_tag(fout, &pd->pd_tag);
(void) fprintf(fout,
"vdsn : %x flags : %x num : %x ",
SWAP_32(pd->pd_vdsn),
SWAP_16(pd->pd_pflags),
SWAP_16(pd->pd_pnum));
print_regid(fout, "contents ", &pd->pd_pcontents, REG_UDF_II);
/* LINTED */
print_phdr(fout, (struct phdr_desc *)(&pd->pd_pc_use));
(void) fprintf(fout,
"acc : %x start : %x length : %x ",
SWAP_32(pd->pd_acc_type),
SWAP_32(pd->pd_part_start),
SWAP_32(pd->pd_part_length));
print_regid(fout, "Impl Id : ", &pd->pd_ii, REG_UDF_II);
}
void
print_lvd(FILE *fout, struct log_vol_desc *lvd)
{
(void) fprintf(fout,
"\n\t\t\tLogical Volume Descriptor\n");
print_tag(fout, &lvd->lvd_tag);
(void) fprintf(fout,
"vdsn : %x ", SWAP_32(lvd->lvd_vdsn));
print_charspec(fout, "Desc Char Set ", &lvd->lvd_desc_cs);
print_dstring(fout, "lvid : ", lvd->lvd_desc_cs.cs_type,
lvd->lvd_lvid, 28);
(void) fprintf(fout,
"lbsize : %x ",
SWAP_32(lvd->lvd_log_bsize));
print_regid(fout, "Dom Id", &lvd->lvd_dom_id, REG_DOM_ID);
print_long_ad(fout, "lvcu", &lvd->lvd_lvcu);
(void) fprintf(fout,
"mtlen : %x nmaps : %x ",
SWAP_32(lvd->lvd_mtbl_len),
SWAP_32(lvd->lvd_num_pmaps));
print_regid(fout, "Impl Id : ", &lvd->lvd_ii, REG_UDF_II);
print_ext_ad(fout, "Int Seq", &lvd->lvd_int_seq_ext);
print_pmaps(fout, lvd->lvd_pmaps, SWAP_32(lvd->lvd_num_pmaps));
}
void
print_usd(FILE *fout, struct unall_spc_desc *ua)
{
int32_t i, count;
(void) fprintf(fout,
"\n\t\t\tUnallocated Space Descriptor\n");
print_tag(fout, &ua->ua_tag);
count = SWAP_32(ua->ua_nad);
(void) fprintf(fout,
"vdsn : %x nad : %x\n",
SWAP_32(ua->ua_vdsn), count);
for (i = 0; i < count; i++) {
(void) fprintf(fout,
"loc : %x len : %x\n",
SWAP_32(ua->ua_al_dsc[i * 2]),
SWAP_32(ua->ua_al_dsc[i * 2 + 1]));
}
}
void
print_lvid(FILE *fout, struct log_vol_int_desc *lvid)
{
int32_t i, count;
caddr_t addr;
struct lvid_iu *liu;
(void) fprintf(fout,
"\n\t\t\tLogical Volume Integrity Descriptor\n");
print_tag(fout, &lvid->lvid_tag);
print_tstamp(fout, "Rec TM ", &lvid->lvid_tstamp);
if (SWAP_32(lvid->lvid_int_type) == 0) {
(void) fprintf(fout,
"int_typ : Open\n");
} else if (SWAP_32(lvid->lvid_int_type) == 1) {
(void) fprintf(fout, "int_typ : Closed\n");
} else {
(void) fprintf(fout, "int_typ : Unknown\n");
}
print_ext_ad(fout, "Nie ", &lvid->lvid_nie);
count = SWAP_32(lvid->lvid_npart);
(void) fprintf(fout,
"Uniq : %llx npart : %x liu : %x\n",
SWAP_64(lvid->lvid_lvcu.lvhd_uniqid),
count, SWAP_32(lvid->lvid_liu));
for (i = 0; i < count; i++) {
(void) fprintf(fout,
"Part : %x Free : %x Size : %x\n",
i, SWAP_32(lvid->lvid_fst[i]),
SWAP_32(lvid->lvid_fst[count + i]));
}
addr = (caddr_t)lvid->lvid_fst;
/* LINTED */
liu = (struct lvid_iu *)(addr + 2 * count * 4);
print_regid(fout, "Impl Id :", &liu->lvidiu_regid, REG_UDF_II);
(void) fprintf(fout,
"nfiles : %x ndirs : %x miread : %x"
" miwrite : %x mawrite : %x\n",
SWAP_32(liu->lvidiu_nfiles), SWAP_32(liu->lvidiu_ndirs),
SWAP_16(liu->lvidiu_mread), SWAP_16(liu->lvidiu_mwrite),
SWAP_16(liu->lvidiu_maxwr));
}
/* ------------ Routines to print basic structures Part 4 ---------------- */
void
print_fsd(FILE *fout, ud_handle_t h, struct file_set_desc *fsd)
{
(void) fprintf(fout,
"\n\t\t\tFile Set Descriptor\n");
print_tag(fout, &fsd->fsd_tag);
print_tstamp(fout, "Rec TM ", &fsd->fsd_time);
(void) fprintf(fout,
"ilvl : %x milvl : %x csl : %x"
" mcsl : %x fsn : %x fsdn : %x\n",
SWAP_16(fsd->fsd_ilevel), SWAP_16(fsd->fsd_mi_level),
SWAP_32(fsd->fsd_cs_list), SWAP_32(fsd->fsd_mcs_list),
SWAP_32(fsd->fsd_fs_no), SWAP_32(fsd->fsd_fsd_no));
print_charspec(fout, "ID CS ", &fsd->fsd_lvidcs);
print_dstring(fout, "lvi : ", fsd->fsd_lvidcs.cs_type,
fsd->fsd_lvid, 128);
print_charspec(fout, "ID CS ", &fsd->fsd_fscs);
print_dstring(fout, "fsi : ", fsd->fsd_lvidcs.cs_type,
fsd->fsd_fsi, 32);
print_dstring(fout, "cfi : ", fsd->fsd_lvidcs.cs_type,
fsd->fsd_cfi, 32);
print_dstring(fout, "afi : ", fsd->fsd_lvidcs.cs_type,
fsd->fsd_afi, 32);
print_long_ad(fout, "Ricb ", &fsd->fsd_root_icb);
print_regid(fout, "DI ", &fsd->fsd_did, REG_DOM_ID);
print_long_ad(fout, "Next Fsd ", &fsd->fsd_next);
if (h->udfs.ecma_version == UD_ECMA_VER3) {
print_long_ad(fout, "System Stream Directory ICB ",
&fsd->fsd_next);
}
}
void
print_phdr(FILE *fout, struct phdr_desc *ph)
{
print_short_ad(fout, "ust ", &ph->phdr_ust);
print_short_ad(fout, "usb ", &ph->phdr_usb);
print_short_ad(fout, "int ", &ph->phdr_it);
print_short_ad(fout, "fst ", &ph->phdr_fst);
print_short_ad(fout, "fsh ", &ph->phdr_fsb);
}
void
print_fid(FILE *fout, struct file_id *fid)
{
int32_t i;
uint8_t *addr;
(void) fprintf(fout,
"File Identifier Descriptor\n");
print_tag(fout, &fid->fid_tag);
(void) fprintf(fout, "fvn : %x fc : %x length : %x ",
fid->fid_ver, fid->fid_flags, fid->fid_idlen);
print_long_ad(fout, "ICB", &fid->fid_icb);
addr = &fid->fid_spec[SWAP_16(fid->fid_iulen)];
(void) fprintf(fout, "iulen : %x comp : %x name : ",
SWAP_16(fid->fid_iulen), *addr);
addr++;
for (i = 0; i < fid->fid_idlen; i++) {
(void) fprintf(fout, "%c", *addr++);
}
(void) fprintf(fout, "\n");
}
void
print_aed(FILE *fout, struct alloc_ext_desc *aed)
{
(void) fprintf(fout,
"Allocation Extent Descriptor\n");
print_tag(fout, &aed->aed_tag);
(void) fprintf(fout, "prev ael loc : %x laed : %x\n",
SWAP_32(aed->aed_rev_ael), SWAP_32(aed->aed_len_aed));
}
static char *ftype[] = {
"NON", "USE", "PIE", "IE",
"DIR", "REG", "BDEV", "CDEV",
"EATT", "FIFO", "SOCK", "TERM",
"SYML", "SDIR"
};
void
print_icb_tag(FILE *fout, struct icb_tag *itag)
{
(void) fprintf(fout,
"prnde : %x strat : %x param : %x max_ent %x\n",
SWAP_32(itag->itag_prnde), SWAP_16(itag->itag_strategy),
SWAP_16(itag->itag_param), SWAP_16(itag->itag_max_ent));
(void) fprintf(fout,
"ftype : %s prn : %x loc : %x flags : %x\n",
(itag->itag_ftype >= 14) ? ftype[0] : ftype[itag->itag_ftype],
SWAP_16(itag->itag_lb_prn),
SWAP_32(itag->itag_lb_loc), SWAP_16(itag->itag_flags));
}
void
print_ie(FILE *fout, struct indirect_entry *ie)
{
(void) fprintf(fout,
"Indirect Entry\n");
print_tag(fout, &ie->ie_tag);
print_icb_tag(fout, &ie->ie_icb_tag);
print_long_ad(fout, "ICB", &ie->ie_indirecticb);
}
void
print_td(FILE *fout, struct term_desc *td)
{
(void) fprintf(fout,
"Terminating Descriptor\n");
print_tag(fout, &td->td_tag);
}
void
print_fe(FILE *fout, struct file_entry *fe)
{
(void) fprintf(fout,
"File Entry\n");
print_tag(fout, &fe->fe_tag);
print_icb_tag(fout, &fe->fe_icb_tag);
(void) fprintf(fout,
"uid : %x gid : %x perms : %x nlnk : %x\n",
SWAP_32(fe->fe_uid), SWAP_32(fe->fe_gid),
SWAP_32(fe->fe_perms), SWAP_16(fe->fe_lcount));
(void) fprintf(fout,
"rec_for : %x rec_dis : %x rec_len : %x "
"sz : %llx blks : %llx\n",
fe->fe_rec_for, fe->fe_rec_dis, SWAP_32(fe->fe_rec_len),
SWAP_64(fe->fe_info_len), SWAP_64(fe->fe_lbr));
print_tstamp(fout, "ctime ", &fe->fe_acc_time);
print_tstamp(fout, "mtime ", &fe->fe_mod_time);
print_tstamp(fout, "atime ", &fe->fe_attr_time);
(void) fprintf(fout,
"ckpoint : %x ", SWAP_32(fe->fe_ckpoint));
print_long_ad(fout, "ICB", &fe->fe_ea_icb);
print_regid(fout, "impl", &fe->fe_impl_id, REG_UDF_II);
(void) fprintf(fout,
"uniq_id : %llx len_ear : %x len_adesc %x\n",
SWAP_64(fe->fe_uniq_id), SWAP_32(fe->fe_len_ear),
SWAP_32(fe->fe_len_adesc));
}
void
print_pmaps(FILE *fout, uint8_t *addr, int32_t count)
{
struct pmap_hdr *hdr;
struct pmap_typ1 *map1;
struct pmap_typ2 *map2;
while (count--) {
hdr = (struct pmap_hdr *)addr;
switch (hdr->maph_type) {
case 1 :
/* LINTED */
map1 = (struct pmap_typ1 *)hdr;
(void) fprintf(fout, "Map type 1 ");
(void) fprintf(fout, "VSN %x prn %x\n",
SWAP_16(map1->map1_vsn),
SWAP_16(map1->map1_pn));
break;
case 2 :
/* LINTED */
map2 = (struct pmap_typ2 *)hdr;
(void) fprintf(fout, "Map type 2 ");
(void) fprintf(fout, "VSN %x prn %x\n",
SWAP_16(map2->map2_vsn),
SWAP_16(map2->map2_pn));
print_regid(fout, "Partition Type Identifier",
&map2->map2_pti, REG_UDF_ID);
break;
default :
(void) fprintf(fout, "unknown map type\n");
}
addr += hdr->maph_length;
}
}
void
print_short_ad(FILE *fout, char *name, struct short_ad *sad)
{
(void) fprintf(fout,
"%s loc : %x len : %x\n", name,
SWAP_32(sad->sad_ext_loc), SWAP_32(sad->sad_ext_len));
}
void
print_long_ad(FILE *fout, char *name, struct long_ad *lad)
{
(void) fprintf(fout,
"%s prn : %x loc : %x len : %x\n", name,
SWAP_16(lad->lad_ext_prn), SWAP_32(lad->lad_ext_loc),
SWAP_32(lad->lad_ext_len));
}
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#ifndef _UD_LIB_H
#define _UD_LIB_H
#ifdef __cplusplus
extern "C" {
#endif
#define UD_VOL_REC_START (32 * 1024)
#define UD_VOL_REC_BSZ 2048
#define UD_VOL_REC_END (256 * UD_VOL_REC_BSZ)
#define UD_ECMA_VER2 0x00000002
#define UD_ECMA_VER3 0x00000003
#define UD_ECMA_UNKN 0xFFFFFFFF
#define MAX_PARTS 10
#define MAX_MAPS 10
#define MAX_SPM 4
#define lb_roundup(sz, lbsz) \
(((sz) + (lbsz - 1)) & (~(lbsz - 1)))
struct vds {
uint32_t pvd_loc;
uint32_t pvd_len;
uint32_t pvd_vdsn;
uint32_t iud_loc;
uint32_t iud_len;
uint32_t part_loc[MAX_PARTS];
uint32_t part_len[MAX_PARTS];
uint32_t lvd_loc;
uint32_t lvd_len;
uint32_t lvd_vdsn;
uint32_t usd_loc;
uint32_t usd_len;
};
/*
* All addresses are the lbsize block numbers
* offseted into the partition
*/
struct udf {
uint32_t flags;
#define INVALID_UDFS 0x0000
#define VALID_UDFS 0x0001
#define VALID_MVDS 0x0002
#define VALID_RVDS 0x0004
uint32_t ecma_version;
uint8_t ecma_id[5];
uint16_t mi_read;
uint16_t ma_read;
uint16_t ma_write;
uint32_t lbsize;
uint32_t avdp_loc; /* First Readable avdp */
uint32_t avdp_len;
uint32_t mvds_loc;
uint32_t mvds_len;
uint32_t rvds_len;
uint32_t rvds_loc;
struct vds mvds;
struct vds rvds;
/*
* location of the latest lvid
*/
uint32_t lvid_loc;
uint32_t lvid_len;
uint16_t fsds_prn;
uint32_t fsds_loc;
uint32_t fsds_len;
/*
* Location of the most usable fsd
* on WORM we have to follow till the
* end of the chain
* FSD location is absolute disk location
* after translating using maps and partitions
*/
uint32_t fsd_loc;
uint32_t fsd_len;
uint16_t ricb_prn;
uint32_t ricb_loc;
uint32_t ricb_len;
uint32_t vat_icb_loc;
uint32_t vat_icb_len;
};
struct ud_part {
uint16_t udp_flags; /* See below */
#define UDP_BITMAPS 0x00
#define UDP_SPACETBLS 0x01
uint16_t udp_number; /* partition Number */
uint32_t udp_seqno; /* to find the prevailaing desc */
uint32_t udp_access; /* access type */
uint32_t udp_start; /* Starting block no of partition */
uint32_t udp_length; /* Lenght of the partition */
uint32_t udp_unall_loc; /* unall space tbl or bitmap loc */
uint32_t udp_unall_len; /* unall space tbl or bitmap length */
uint32_t udp_freed_loc; /* freed space tbl or bitmap loc */
uint32_t udp_freed_len; /* freed space tbl or bitmap length */
/* From part desc */
uint32_t udp_nfree; /* No of free blocks in the partition */
uint32_t udp_nblocks; /* Total no of blks in the partition */
/* From lvid */
};
struct ud_map {
uint16_t udm_flags;
#define UDM_MAP_NORM 0x00
#define UDM_MAP_VPM 0x01
#define UDM_MAP_SPM 0x02
uint16_t udm_vsn;
uint16_t udm_pn;
uint32_t udm_vat_icb_loc;
uint32_t udm_nent;
uint32_t *udm_count;
struct buf **udm_bp;
uint32_t **udm_addr;
int32_t udm_plen;
int32_t udm_nspm;
uint32_t udm_spsz;
uint32_t udm_loc[MAX_SPM];
struct buf *udm_sbp[MAX_SPM];
caddr_t udm_spaddr[MAX_SPM];
};
#define REG_DOM_ID 0x1
#define REG_UDF_ID 0x2
#define REG_UDF_II 0x4
#define EI_FLG_DIRTY 0x01
#define EI_FLG_PROT 0x02
struct dom_id_suffix {
uint16_t dis_udf_revison;
uint8_t dis_domain_flags;
uint8_t dis_pad[5];
};
#define PROTECT_SOFT_WRITE 0x01
#define PROTECT_HARD_WRITE 0x02
struct udf_id_suffix {
uint16_t uis_udf_revision;
uint8_t uis_os_class;
uint8_t uis_os_identifier;
uint8_t uis_pad[4];
};
struct impl_id_suffix {
uint8_t iis_os_class;
uint8_t iis_os_identifier;
uint8_t iis_pad[6];
};
#define OS_CLASS_UNDEFINED 0x00
#define OS_CLASS_DOS_WIN3x 0x01
#define OS_CLASS_OS_2 0x02
#define OS_CLASS_MAC_OS_7 0x02
#define OS_CLASS_UNIX 0x04
#define OS_CLASS_WIN_95 0x05
#define OS_CLASS_WIN_NT 0x06
#define OS_IDENTIFIER_GENERIC 0x00
#define OS_IDENTIFIER_IBM_AIX 0x01
#define OS_IDENTIFIER_SOLARIS 0x02
#define OS_IDENTIFIER_HP_UX 0x03
#define OS_IDENTIFIER_SG_IRIX 0x04
#define OS_IDENTIFIER_LINUX 0x05
#define OS_IDENTIFIER_MK_LINUX 0x06
#define OS_IDENTIFIER_FREE_BSD 0x07
struct ud_handle {
int fd;
struct udf udfs;
struct ud_part part[MAX_PARTS];
int32_t n_parts;
struct ud_map maps[MAX_MAPS];
int32_t n_maps;
};
typedef struct ud_handle *ud_handle_t;
int ud_init(int, ud_handle_t *);
void ud_fini(ud_handle_t);
int32_t ud_open_dev(ud_handle_t, char *, uint32_t);
void ud_close_dev(ud_handle_t);
int32_t ud_read_dev(ud_handle_t, uint64_t, uint8_t *, uint32_t);
int32_t ud_write_dev(ud_handle_t, uint64_t, uint8_t *, uint32_t);
int32_t ud_fill_udfs_info(ud_handle_t);
int32_t ud_get_num_blks(ud_handle_t, uint32_t *);
int32_t ud_verify_tag(ud_handle_t, struct tag *,
uint16_t, uint32_t, int32_t, int32_t);
void ud_make_tag(ud_handle_t, struct tag *, uint16_t, uint32_t, uint16_t);
uint32_t ud_xlate_to_daddr(ud_handle_t, uint16_t, uint32_t);
void ud_convert2local(int8_t *, int8_t *, int32_t);
void print_charspec(FILE *, char *, struct charspec *);
void print_dstring(FILE *, char *, uint16_t, char *, uint8_t);
void set_dstring(dstring_t *, char *, int32_t);
void print_tstamp(FILE *, char *, tstamp_t *);
void print_regid(FILE *, char *, struct regid *, int32_t);
void print_ext_ad(FILE *, char *, struct extent_ad *);
void print_tag(FILE *, struct tag *);
void print_pvd(FILE *, struct pri_vol_desc *);
void print_avd(FILE *, struct anch_vol_desc_ptr *);
void print_vdp(FILE *, struct vol_desc_ptr *);
void print_iuvd(FILE *, struct iuvd_desc *);
void print_part(FILE *, struct part_desc *);
void print_lvd(FILE *, struct log_vol_desc *);
void print_usd(FILE *, struct unall_spc_desc *);
void print_lvid(FILE *, struct log_vol_int_desc *);
void print_part(FILE *, struct part_desc *);
void print_fsd(FILE *, ud_handle_t h, struct file_set_desc *);
void print_phdr(FILE *, struct phdr_desc *);
void print_fid(FILE *, struct file_id *);
void print_aed(FILE *, struct alloc_ext_desc *);
void print_icb_tag(FILE *, struct icb_tag *);
void print_ie(FILE *, struct indirect_entry *);
void print_td(FILE *, struct term_desc *);
void print_fe(FILE *, struct file_entry *);
void print_pmaps(FILE *, uint8_t *, int32_t);
void print_short_ad(FILE *, char *, struct short_ad *);
void print_long_ad(FILE *, char *, struct long_ad *);
#ifdef __cplusplus
}
#endif
#endif /* _UD_LIB_H */
|