2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 /* $NetBSD: cd9660_rrip.c,v 1.11 1994/12/24 15:30:10 cgd Exp $ */
25 * Copyright (c) 1993, 1994
26 * The Regents of the University of California. All rights reserved.
28 * This code is derived from software contributed to Berkeley
29 * by Pace Willisson (pace@blitz.com). The Rock Ridge Extension
30 * Support code is derived from software contributed to Berkeley
31 * by Atsushi Murai (amurai@spec.co.jp).
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. All advertising materials mentioning features or use of this software
42 * must display the following acknowledgement:
43 * This product includes software developed by the University of
44 * California, Berkeley and its contributors.
45 * 4. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * @(#)cd9660_rrip.c 8.6 (Berkeley) 12/5/94
66 * 22-Jan-98 radar 1669467 - ISO 9660 CD support - jwc
70 #include <sys/param.h>
71 #include <sys/systm.h>
72 #include <sys/vnode.h>
73 #include <sys/mount.h>
74 #include <sys/namei.h>
77 #include <sys/kernel.h>
79 #include <sys/types.h>
83 #include <isofs/cd9660/iso.h>
84 #include <isofs/cd9660/cd9660_node.h>
85 #include <isofs/cd9660/cd9660_rrip.h>
86 #include <isofs/cd9660/iso_rrip.h>
89 * POSIX file attribute
92 cd9660_rrip_attr(ISO_RRIP_ATTR
*p
, ISO_RRIP_ANALYZE
*ana
)
94 ana
->inop
->inode
.iso_mode
= isonum_733(p
->mode
);
95 ana
->inop
->inode
.iso_uid
= isonum_733(p
->uid
);
96 ana
->inop
->inode
.iso_gid
= isonum_733(p
->gid
);
97 ana
->inop
->inode
.iso_links
= isonum_733(p
->links
);
98 ana
->fields
&= ~ISO_SUSP_ATTR
;
103 cd9660_rrip_defattr(struct iso_directory_record
*isodir
, ISO_RRIP_ANALYZE
*ana
)
105 /* But this is a required field! */
106 printf("RRIP without PX field?\n");
107 cd9660_defattr(isodir
,ana
->inop
,NULL
);
114 cd9660_rrip_slink(ISO_RRIP_SLINK
*p
, ISO_RRIP_ANALYZE
*ana
)
116 register ISO_RRIP_SLINK_COMPONENT
*pcomp
;
117 register ISO_RRIP_SLINK_COMPONENT
*pcompe
;
119 char *outbuf
, *inbuf
;
121 pcomp
= (ISO_RRIP_SLINK_COMPONENT
*)p
->component
;
122 pcompe
= (ISO_RRIP_SLINK_COMPONENT
*)((char *)p
+ isonum_711(p
->h
.length
));
124 outbuf
= ana
->outbuf
;
128 * Gathering a Symbolic name from each component with path
132 pcomp
= (ISO_RRIP_SLINK_COMPONENT
*)((char *)pcomp
+ ISO_RRIP_SLSIZ
133 + isonum_711(pcomp
->clen
))) {
136 if (len
< ana
->maxlen
) {
146 switch (*pcomp
->cflag
) {
148 case ISO_SUSP_CFLAG_CURRENT
:
149 /* Inserting Current */
153 case ISO_SUSP_CFLAG_PARENT
:
154 /* Inserting Parent */
158 case ISO_SUSP_CFLAG_ROOT
:
159 /* Inserting slash for ROOT */
160 /* start over from beginning(?) */
165 case ISO_SUSP_CFLAG_VOLROOT
:
166 /* Inserting a mount point i.e. "/cdrom" */
170 inbuf
= &(vfs_statfs(ana
->imp
->im_mountp
)->f_mntonname
);
171 wlen
= strlen(inbuf
);
174 case ISO_SUSP_CFLAG_HOST
:
175 /* Inserting hostname i.e. "kurt.tools.de" */
180 case ISO_SUSP_CFLAG_CONTINUE
:
184 /* Inserting component */
185 wlen
= isonum_711(pcomp
->clen
);
189 printf("RRIP with incorrect flags?");
190 wlen
= ana
->maxlen
+ 1;
194 if (len
+ wlen
> ana
->maxlen
) {
195 /* indicate error to caller */
198 ana
->outbuf
-= *ana
->outlen
;
203 bcopy(inbuf
,outbuf
,wlen
);
208 ana
->outbuf
= outbuf
;
212 if (!isonum_711(p
->flags
)) {
213 ana
->fields
&= ~ISO_SUSP_SLINK
;
214 return ISO_SUSP_SLINK
;
223 cd9660_rrip_altname(ISO_RRIP_ALTNAME
*p
, ISO_RRIP_ANALYZE
*ana
)
234 case ISO_SUSP_CFLAG_CURRENT
:
235 /* Inserting Current */
239 case ISO_SUSP_CFLAG_PARENT
:
240 /* Inserting Parent */
244 case ISO_SUSP_CFLAG_HOST
:
245 /* Inserting hostname i.e. "kurt.tools.de" */
250 case ISO_SUSP_CFLAG_CONTINUE
:
254 /* Inserting component */
255 wlen
= isonum_711(p
->h
.length
) - 5;
256 inbuf
= (char *)p
+ 5;
260 printf("RRIP with incorrect NM flags?\n");
261 wlen
= ana
->maxlen
+ 1;
265 if ((*ana
->outlen
+= wlen
) > ana
->maxlen
) {
266 /* treat as no name field */
267 ana
->fields
&= ~ISO_SUSP_ALTNAME
;
268 ana
->outbuf
-= *ana
->outlen
- wlen
;
273 bcopy(inbuf
,ana
->outbuf
,wlen
);
277 ana
->fields
&= ~ISO_SUSP_ALTNAME
;
278 return ISO_SUSP_ALTNAME
;
284 cd9660_rrip_defname(struct iso_directory_record
*isodir
, ISO_RRIP_ANALYZE
*ana
)
286 strcpy(ana
->outbuf
,"..");
287 switch (*isodir
->name
) {
289 isofntrans(isodir
->name
, isonum_711(isodir
->name_len
),
290 ana
->outbuf
, ana
->outlen
, 1,
291 isonum_711(isodir
->flags
) & associatedBit
);
303 * Parent or Child Link
306 cd9660_rrip_pclink(ISO_RRIP_CLINK
*p
, ISO_RRIP_ANALYZE
*ana
)
308 *ana
->inump
= isonum_733(p
->dir_loc
) << ana
->imp
->im_bshift
;
309 ana
->fields
&= ~(ISO_SUSP_CLINK
|ISO_SUSP_PLINK
);
310 return *p
->h
.type
== 'C' ? ISO_SUSP_CLINK
: ISO_SUSP_PLINK
;
314 * Relocated directory
318 cd9660_rrip_reldir(__unused ISO_RRIP_RELDIR
*p
, ISO_RRIP_ANALYZE
*ana
)
320 /* special hack to make caller aware of RE field */
323 return ISO_SUSP_RELDIR
|ISO_SUSP_ALTNAME
|ISO_SUSP_CLINK
|ISO_SUSP_PLINK
;
327 cd9660_rrip_tstamp(ISO_RRIP_TSTAMP
*p
, ISO_RRIP_ANALYZE
*ana
)
333 /* Check a format of time stamp (7bytes/17bytes) */
334 if (!(*p
->flags
&ISO_SUSP_TSTAMP_FORM17
)) {
335 if (*p
->flags
&ISO_SUSP_TSTAMP_CREAT
)
338 if (*p
->flags
&ISO_SUSP_TSTAMP_MODIFY
) {
339 cd9660_tstamp_conv7(ptime
,&ana
->inop
->inode
.iso_mtime
);
342 bzero(&ana
->inop
->inode
.iso_mtime
,sizeof(struct timespec
));
344 if (*p
->flags
&ISO_SUSP_TSTAMP_ACCESS
) {
345 cd9660_tstamp_conv7(ptime
,&ana
->inop
->inode
.iso_atime
);
348 ana
->inop
->inode
.iso_atime
= ana
->inop
->inode
.iso_mtime
;
350 if (*p
->flags
&ISO_SUSP_TSTAMP_ATTR
)
351 cd9660_tstamp_conv7(ptime
,&ana
->inop
->inode
.iso_ctime
);
353 ana
->inop
->inode
.iso_ctime
= ana
->inop
->inode
.iso_mtime
;
356 if (*p
->flags
&ISO_SUSP_TSTAMP_CREAT
)
359 if (*p
->flags
&ISO_SUSP_TSTAMP_MODIFY
) {
360 cd9660_tstamp_conv17(ptime
,&ana
->inop
->inode
.iso_mtime
);
363 bzero(&ana
->inop
->inode
.iso_mtime
,sizeof(struct timespec
));
365 if (*p
->flags
&ISO_SUSP_TSTAMP_ACCESS
) {
366 cd9660_tstamp_conv17(ptime
,&ana
->inop
->inode
.iso_atime
);
369 ana
->inop
->inode
.iso_atime
= ana
->inop
->inode
.iso_mtime
;
371 if (*p
->flags
&ISO_SUSP_TSTAMP_ATTR
)
372 cd9660_tstamp_conv17(ptime
,&ana
->inop
->inode
.iso_ctime
);
374 ana
->inop
->inode
.iso_ctime
= ana
->inop
->inode
.iso_mtime
;
377 ana
->fields
&= ~ISO_SUSP_TSTAMP
;
378 return ISO_SUSP_TSTAMP
;
382 cd9660_rrip_deftstamp(struct iso_directory_record
*isodir
,
383 ISO_RRIP_ANALYZE
*ana
)
385 cd9660_deftstamp(isodir
,ana
->inop
,NULL
);
392 cd9660_rrip_device(ISO_RRIP_DEVICE
*p
, ISO_RRIP_ANALYZE
*ana
)
396 high
= isonum_733(p
->dev_t_high
);
397 low
= isonum_733(p
->dev_t_low
);
400 ana
->inop
->inode
.iso_rdev
= makedev(major(low
), minor(low
));
402 ana
->inop
->inode
.iso_rdev
= makedev(high
, minor(low
));
403 ana
->fields
&= ~ISO_SUSP_DEVICE
;
404 return ISO_SUSP_DEVICE
;
411 cd9660_rrip_idflag(ISO_RRIP_IDFLAG
*p
, ISO_RRIP_ANALYZE
*ana
)
413 ana
->fields
&= isonum_711(p
->flags
)|~0xff; /* don't touch high bits */
414 /* special handling of RE field */
415 if (ana
->fields
&ISO_SUSP_RELDIR
)
416 return cd9660_rrip_reldir(p
,ana
);
418 return ISO_SUSP_IDFLAG
;
422 * Continuation pointer
425 cd9660_rrip_cont(ISO_RRIP_CONT
*p
, ISO_RRIP_ANALYZE
*ana
)
427 ana
->iso_ce_blk
= isonum_733(p
->location
);
428 ana
->iso_ce_off
= isonum_733(p
->offset
);
429 ana
->iso_ce_len
= isonum_733(p
->length
);
430 return ISO_SUSP_CONT
;
438 cd9660_rrip_stop(__unused ISO_SUSP_HEADER
*p
, __unused ISO_RRIP_ANALYZE
*ana
)
440 return ISO_SUSP_STOP
;
444 * Extension reference
447 cd9660_rrip_extref(ISO_RRIP_EXTREF
*p
, ISO_RRIP_ANALYZE
*ana
)
449 if (isonum_711(p
->len_id
) != 10
450 || bcmp((char *)p
+ 8,"RRIP_1991A",10)
451 || isonum_711(p
->version
) != 1)
453 ana
->fields
&= ~ISO_SUSP_EXTREF
;
454 return ISO_SUSP_EXTREF
;
457 typedef int (*rrip_table_func
)(ISO_SUSP_HEADER
*phead
, ISO_RRIP_ANALYZE
*ana
);
458 typedef int (*rrip_table_func2
)(struct iso_directory_record
*isodir
,
459 ISO_RRIP_ANALYZE
*ana
);
462 rrip_table_func func
;
463 rrip_table_func2 func2
;
468 cd9660_rrip_loop(struct iso_directory_record
*isodir
, ISO_RRIP_ANALYZE
*ana
,
471 register RRIP_TABLE
*ptable
;
472 register ISO_SUSP_HEADER
*phead
;
473 register ISO_SUSP_HEADER
*pend
;
474 struct buf
*bp
= NULL
;
479 * Note: If name length is odd,
480 * it will be padding 1 byte after the name
482 pwhead
= isodir
->name
+ isonum_711(isodir
->name_len
);
483 if (!(isonum_711(isodir
->name_len
)&1))
486 /* If it's not the '.' entry of the root dir obey SP field */
487 if (*isodir
->name
!= 0
488 || isonum_733(isodir
->extent
) != ana
->imp
->root_extent
)
489 pwhead
+= ana
->imp
->rr_skip
;
491 pwhead
+= ana
->imp
->rr_skip0
;
493 phead
= (ISO_SUSP_HEADER
*)pwhead
;
494 pend
= (ISO_SUSP_HEADER
*)((char *)isodir
+ isonum_711(isodir
->length
));
500 * Note: "pend" should be more than one SUSP header
502 while (pend
>= phead
+ 1) {
503 if (isonum_711(phead
->version
) == 1) {
504 for (ptable
= table
; ptable
->func
; ptable
++) {
505 if (*phead
->type
== *ptable
->type
506 && phead
->type
[1] == ptable
->type
[1]) {
507 result
|= (ptable
->func(phead
,ana
));
514 if (result
&ISO_SUSP_STOP
) {
515 result
&= ~ISO_SUSP_STOP
;
518 /* plausibility check */
519 if (isonum_711(phead
->length
) < sizeof(*phead
))
523 * Hopefully this works with newer versions, too
525 phead
= (ISO_SUSP_HEADER
*)((char *)phead
+ isonum_711(phead
->length
));
528 if (ana
->fields
&& ana
->iso_ce_len
) {
529 if (ana
->iso_ce_blk
>= ana
->imp
->volume_space_size
530 || ana
->iso_ce_off
+ ana
->iso_ce_len
> ana
->imp
->logical_block_size
531 || buf_bread(ana
->imp
->im_devvp
,
532 #if 1 // radar 1669467 - logical and physical blocksize are the same
533 (daddr64_t
)((unsigned)ana
->iso_ce_blk
),
535 (daddr64_t
)((unsigned)(ana
->iso_ce_blk
<< (ana
->imp
->im_bshift
- DEV_BSHIFT
))),
536 #endif // radar 1669467
537 ana
->imp
->logical_block_size
, NOCRED
, &bp
))
538 /* what to do now? */
540 phead
= (ISO_SUSP_HEADER
*)((char *)buf_dataptr(bp
) + ana
->iso_ce_off
);
541 pend
= (ISO_SUSP_HEADER
*) ((char *)phead
+ ana
->iso_ce_len
);
548 * If we don't find the Basic SUSP stuffs, just set default value
549 * (attribute/time stamp)
551 for (ptable
= table
; ptable
->func2
; ptable
++)
552 if (!(ptable
->result
&result
))
553 ptable
->func2(isodir
,ana
);
561 static RRIP_TABLE rrip_table_analyze
[] = {
562 { "PX", (rrip_table_func
)cd9660_rrip_attr
,
563 (rrip_table_func2
)cd9660_rrip_defattr
,
565 { "TF", (rrip_table_func
)cd9660_rrip_tstamp
,
566 (rrip_table_func2
)cd9660_rrip_deftstamp
,
568 { "PN", (rrip_table_func
)cd9660_rrip_device
,
569 0, ISO_SUSP_DEVICE
},
570 { "RR", (rrip_table_func
)cd9660_rrip_idflag
,
571 0, ISO_SUSP_IDFLAG
},
572 { "CE", (rrip_table_func
)cd9660_rrip_cont
,
574 { "ST", cd9660_rrip_stop
, 0, ISO_SUSP_STOP
},
579 cd9660_rrip_analyze(struct iso_directory_record
*isodir
, struct iso_node
*inop
,
582 ISO_RRIP_ANALYZE analyze
;
586 analyze
.fields
= ISO_SUSP_ATTR
|ISO_SUSP_TSTAMP
|ISO_SUSP_DEVICE
;
588 return cd9660_rrip_loop(isodir
,&analyze
,rrip_table_analyze
);
592 * Get Alternate Name.
594 static RRIP_TABLE rrip_table_getname
[] = {
595 { "NM", (rrip_table_func
)cd9660_rrip_altname
,
596 (rrip_table_func2
)cd9660_rrip_defname
,
598 { "CL", (rrip_table_func
)cd9660_rrip_pclink
,
599 0, ISO_SUSP_CLINK
|ISO_SUSP_PLINK
},
600 { "PL", (rrip_table_func
)cd9660_rrip_pclink
,
601 0, ISO_SUSP_CLINK
|ISO_SUSP_PLINK
},
602 { "RE", (rrip_table_func
)cd9660_rrip_reldir
,
603 0, ISO_SUSP_RELDIR
},
604 { "RR", (rrip_table_func
)cd9660_rrip_idflag
,
605 0, ISO_SUSP_IDFLAG
},
606 { "CE", (rrip_table_func
)cd9660_rrip_cont
,
608 { "ST", cd9660_rrip_stop
, 0, ISO_SUSP_STOP
},
613 cd9660_rrip_getname(struct iso_directory_record
*isodir
, char *outbuf
,
614 u_short
*outlen
, ino_t
*inump
, struct iso_mnt
*imp
)
616 ISO_RRIP_ANALYZE analyze
;
619 analyze
.outbuf
= outbuf
;
620 analyze
.outlen
= outlen
;
621 analyze
.maxlen
= ISO_RRIP_NAMEMAX
;
622 analyze
.inump
= inump
;
624 analyze
.fields
= ISO_SUSP_ALTNAME
|ISO_SUSP_RELDIR
|ISO_SUSP_CLINK
|ISO_SUSP_PLINK
;
627 tab
= rrip_table_getname
;
628 if (*isodir
->name
== 0
629 || *isodir
->name
== 1) {
630 cd9660_rrip_defname(isodir
,&analyze
);
632 analyze
.fields
&= ~ISO_SUSP_ALTNAME
;
636 return cd9660_rrip_loop(isodir
,&analyze
,tab
);
642 static RRIP_TABLE rrip_table_getsymname
[] = {
643 { "SL", (rrip_table_func
)cd9660_rrip_slink
,
645 { "RR", (rrip_table_func
)cd9660_rrip_idflag
,
646 0, ISO_SUSP_IDFLAG
},
647 { "CE", (rrip_table_func
)cd9660_rrip_cont
,
649 { "ST", cd9660_rrip_stop
, 0, ISO_SUSP_STOP
},
654 cd9660_rrip_getsymname(struct iso_directory_record
*isodir
, char *outbuf
,
655 u_short
*outlen
, struct iso_mnt
*imp
)
657 ISO_RRIP_ANALYZE analyze
;
659 analyze
.outbuf
= outbuf
;
660 analyze
.outlen
= outlen
;
662 analyze
.maxlen
= MAXPATHLEN
;
663 analyze
.cont
= 1; /* don't start with a slash */
665 analyze
.fields
= ISO_SUSP_SLINK
;
667 return (cd9660_rrip_loop(isodir
,&analyze
,rrip_table_getsymname
)&ISO_SUSP_SLINK
);
670 static RRIP_TABLE rrip_table_extref
[] = {
671 { "ER", (rrip_table_func
)cd9660_rrip_extref
,
672 0, ISO_SUSP_EXTREF
},
673 { "CE", (rrip_table_func
)cd9660_rrip_cont
,
675 { "ST", cd9660_rrip_stop
, 0, ISO_SUSP_STOP
},
680 * Check for Rock Ridge Extension and return offset of its fields.
681 * Note: We insist on the ER field.
684 cd9660_rrip_offset(struct iso_directory_record
*isodir
, struct iso_mnt
*imp
)
687 ISO_RRIP_ANALYZE analyze
;
690 p
= (ISO_RRIP_OFFSET
*)(isodir
->name
+ 1);
691 if (bcmp(p
,"SP\7\1\276\357",6)) {
692 /* Maybe, it's a CDROM XA disc? */
694 p
= (ISO_RRIP_OFFSET
*)((char *)p
+ 15);
695 if (bcmp(p
,"SP\7\1\276\357",6))
700 analyze
.fields
= ISO_SUSP_EXTREF
;
701 if (!(cd9660_rrip_loop(isodir
,&analyze
,rrip_table_extref
)&ISO_SUSP_EXTREF
))
704 return isonum_711(p
->skip
);