2 * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* $NetBSD: cd9660_rrip.c,v 1.11 1994/12/24 15:30:10 cgd Exp $ */
31 * Copyright (c) 1993, 1994
32 * The Regents of the University of California. All rights reserved.
34 * This code is derived from software contributed to Berkeley
35 * by Pace Willisson (pace@blitz.com). The Rock Ridge Extension
36 * Support code is derived from software contributed to Berkeley
37 * by Atsushi Murai (amurai@spec.co.jp).
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
42 * 1. Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
47 * 3. All advertising materials mentioning features or use of this software
48 * must display the following acknowledgement:
49 * This product includes software developed by the University of
50 * California, Berkeley and its contributors.
51 * 4. Neither the name of the University nor the names of its contributors
52 * may be used to endorse or promote products derived from this software
53 * without specific prior written permission.
55 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
56 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
59 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
67 * @(#)cd9660_rrip.c 8.6 (Berkeley) 12/5/94
72 * 22-Jan-98 radar 1669467 - ISO 9660 CD support - jwc
76 #include <sys/param.h>
77 #include <sys/systm.h>
78 #include <sys/vnode.h>
79 #include <sys/mount.h>
80 #include <sys/namei.h>
83 #include <sys/kernel.h>
85 #include <sys/types.h>
89 #include <isofs/cd9660/iso.h>
90 #include <isofs/cd9660/cd9660_node.h>
91 #include <isofs/cd9660/cd9660_rrip.h>
92 #include <isofs/cd9660/iso_rrip.h>
95 * POSIX file attribute
98 cd9660_rrip_attr(ISO_RRIP_ATTR
*p
, ISO_RRIP_ANALYZE
*ana
)
100 ana
->inop
->inode
.iso_mode
= isonum_733(p
->mode
);
101 ana
->inop
->inode
.iso_uid
= isonum_733(p
->uid
);
102 ana
->inop
->inode
.iso_gid
= isonum_733(p
->gid
);
103 ana
->inop
->inode
.iso_links
= isonum_733(p
->links
);
104 ana
->fields
&= ~ISO_SUSP_ATTR
;
105 return ISO_SUSP_ATTR
;
109 cd9660_rrip_defattr(struct iso_directory_record
*isodir
, ISO_RRIP_ANALYZE
*ana
)
111 /* But this is a required field! */
112 printf("RRIP without PX field?\n");
113 cd9660_defattr(isodir
,ana
->inop
,NULL
);
120 cd9660_rrip_slink(ISO_RRIP_SLINK
*p
, ISO_RRIP_ANALYZE
*ana
)
122 register ISO_RRIP_SLINK_COMPONENT
*pcomp
;
123 register ISO_RRIP_SLINK_COMPONENT
*pcompe
;
128 pcomp
= (ISO_RRIP_SLINK_COMPONENT
*)p
->component
;
129 pcompe
= (ISO_RRIP_SLINK_COMPONENT
*)((char *)p
+ isonum_711(p
->h
.length
));
131 outbuf
= ana
->outbuf
;
135 * Gathering a Symbolic name from each component with path
139 pcomp
= (ISO_RRIP_SLINK_COMPONENT
*)((char *)pcomp
+ ISO_RRIP_SLSIZ
140 + isonum_711(pcomp
->clen
))) {
143 if (len
< ana
->maxlen
) {
153 switch (*pcomp
->cflag
) {
155 case ISO_SUSP_CFLAG_CURRENT
:
156 /* Inserting Current */
160 case ISO_SUSP_CFLAG_PARENT
:
161 /* Inserting Parent */
165 case ISO_SUSP_CFLAG_ROOT
:
166 /* Inserting slash for ROOT */
167 /* start over from beginning(?) */
172 case ISO_SUSP_CFLAG_VOLROOT
:
173 /* Inserting a mount point i.e. "/cdrom" */
177 inbuf
= vfs_statfs(ana
->imp
->im_mountp
)->f_mntonname
;
178 wlen
= strlen(inbuf
);
181 case ISO_SUSP_CFLAG_HOST
:
182 /* Inserting hostname i.e. "kurt.tools.de" */
187 case ISO_SUSP_CFLAG_CONTINUE
:
191 /* Inserting component */
192 wlen
= isonum_711(pcomp
->clen
);
196 printf("RRIP with incorrect flags?");
197 wlen
= ana
->maxlen
+ 1;
201 if (len
+ wlen
> ana
->maxlen
) {
202 /* indicate error to caller */
205 ana
->outbuf
-= *ana
->outlen
;
210 bcopy(inbuf
,outbuf
,wlen
);
215 ana
->outbuf
= outbuf
;
219 if (!isonum_711(p
->flags
)) {
220 ana
->fields
&= ~ISO_SUSP_SLINK
;
221 return ISO_SUSP_SLINK
;
230 cd9660_rrip_altname(ISO_RRIP_ALTNAME
*p
, ISO_RRIP_ANALYZE
*ana
)
241 case ISO_SUSP_CFLAG_CURRENT
:
242 /* Inserting Current */
246 case ISO_SUSP_CFLAG_PARENT
:
247 /* Inserting Parent */
251 case ISO_SUSP_CFLAG_HOST
:
252 /* Inserting hostname i.e. "kurt.tools.de" */
257 case ISO_SUSP_CFLAG_CONTINUE
:
261 /* Inserting component */
262 wlen
= isonum_711(p
->h
.length
) - 5;
263 inbuf
= (char *)p
+ 5;
267 printf("RRIP with incorrect NM flags?\n");
268 wlen
= ana
->maxlen
+ 1;
272 if ((*ana
->outlen
+= wlen
) > ana
->maxlen
) {
273 /* treat as no name field */
274 ana
->fields
&= ~ISO_SUSP_ALTNAME
;
275 ana
->outbuf
-= *ana
->outlen
- wlen
;
280 bcopy(inbuf
,ana
->outbuf
,wlen
);
284 ana
->fields
&= ~ISO_SUSP_ALTNAME
;
285 return ISO_SUSP_ALTNAME
;
291 cd9660_rrip_defname(struct iso_directory_record
*isodir
, ISO_RRIP_ANALYZE
*ana
)
293 strlcpy(ana
->outbuf
, "..", ana
->outlen
);
294 switch (*isodir
->name
) {
296 isofntrans(isodir
->name
, isonum_711(isodir
->name_len
),
297 ana
->outbuf
, ana
->outlen
, 1,
298 isonum_711(isodir
->flags
) & associatedBit
);
310 * Parent or Child Link
313 cd9660_rrip_pclink(ISO_RRIP_CLINK
*p
, ISO_RRIP_ANALYZE
*ana
)
315 *ana
->inump
= isonum_733(p
->dir_loc
) << ana
->imp
->im_bshift
;
316 ana
->fields
&= ~(ISO_SUSP_CLINK
|ISO_SUSP_PLINK
);
317 return *p
->h
.type
== 'C' ? ISO_SUSP_CLINK
: ISO_SUSP_PLINK
;
321 * Relocated directory
325 cd9660_rrip_reldir(__unused ISO_RRIP_RELDIR
*p
, ISO_RRIP_ANALYZE
*ana
)
327 /* special hack to make caller aware of RE field */
330 return ISO_SUSP_RELDIR
|ISO_SUSP_ALTNAME
|ISO_SUSP_CLINK
|ISO_SUSP_PLINK
;
334 cd9660_rrip_tstamp(ISO_RRIP_TSTAMP
*p
, ISO_RRIP_ANALYZE
*ana
)
340 /* Check a format of time stamp (7bytes/17bytes) */
341 if (!(*p
->flags
&ISO_SUSP_TSTAMP_FORM17
)) {
342 if (*p
->flags
&ISO_SUSP_TSTAMP_CREAT
)
345 if (*p
->flags
&ISO_SUSP_TSTAMP_MODIFY
) {
346 cd9660_tstamp_conv7(ptime
,&ana
->inop
->inode
.iso_mtime
);
349 bzero(&ana
->inop
->inode
.iso_mtime
, sizeof(struct timespec
));
351 if (*p
->flags
&ISO_SUSP_TSTAMP_ACCESS
) {
352 cd9660_tstamp_conv7(ptime
,&ana
->inop
->inode
.iso_atime
);
355 ana
->inop
->inode
.iso_atime
= ana
->inop
->inode
.iso_mtime
;
357 if (*p
->flags
&ISO_SUSP_TSTAMP_ATTR
)
358 cd9660_tstamp_conv7(ptime
,&ana
->inop
->inode
.iso_ctime
);
360 ana
->inop
->inode
.iso_ctime
= ana
->inop
->inode
.iso_mtime
;
363 if (*p
->flags
&ISO_SUSP_TSTAMP_CREAT
)
366 if (*p
->flags
&ISO_SUSP_TSTAMP_MODIFY
) {
367 cd9660_tstamp_conv17(ptime
,&ana
->inop
->inode
.iso_mtime
);
370 bzero(&ana
->inop
->inode
.iso_mtime
, sizeof(struct timespec
));
372 if (*p
->flags
&ISO_SUSP_TSTAMP_ACCESS
) {
373 cd9660_tstamp_conv17(ptime
,&ana
->inop
->inode
.iso_atime
);
376 ana
->inop
->inode
.iso_atime
= ana
->inop
->inode
.iso_mtime
;
378 if (*p
->flags
&ISO_SUSP_TSTAMP_ATTR
)
379 cd9660_tstamp_conv17(ptime
,&ana
->inop
->inode
.iso_ctime
);
381 ana
->inop
->inode
.iso_ctime
= ana
->inop
->inode
.iso_mtime
;
384 ana
->fields
&= ~ISO_SUSP_TSTAMP
;
385 return ISO_SUSP_TSTAMP
;
389 cd9660_rrip_deftstamp(struct iso_directory_record
*isodir
,
390 ISO_RRIP_ANALYZE
*ana
)
392 cd9660_deftstamp(isodir
,ana
->inop
,NULL
);
399 cd9660_rrip_device(ISO_RRIP_DEVICE
*p
, ISO_RRIP_ANALYZE
*ana
)
403 high
= isonum_733(p
->dev_t_high
);
404 low
= isonum_733(p
->dev_t_low
);
407 ana
->inop
->inode
.iso_rdev
= makedev(major(low
), minor(low
));
409 ana
->inop
->inode
.iso_rdev
= makedev(high
, minor(low
));
410 ana
->fields
&= ~ISO_SUSP_DEVICE
;
411 return ISO_SUSP_DEVICE
;
418 cd9660_rrip_idflag(ISO_RRIP_IDFLAG
*p
, ISO_RRIP_ANALYZE
*ana
)
420 ana
->fields
&= isonum_711(p
->flags
)|~0xff; /* don't touch high bits */
421 /* special handling of RE field */
422 if (ana
->fields
&ISO_SUSP_RELDIR
)
423 return cd9660_rrip_reldir(p
,ana
);
425 return ISO_SUSP_IDFLAG
;
429 * Continuation pointer
432 cd9660_rrip_cont(ISO_RRIP_CONT
*p
, ISO_RRIP_ANALYZE
*ana
)
434 ana
->iso_ce_blk
= isonum_733(p
->location
);
435 ana
->iso_ce_off
= isonum_733(p
->offset
);
436 ana
->iso_ce_len
= isonum_733(p
->length
);
437 return ISO_SUSP_CONT
;
445 cd9660_rrip_stop(__unused ISO_SUSP_HEADER
*p
, __unused ISO_RRIP_ANALYZE
*ana
)
447 return ISO_SUSP_STOP
;
451 * Extension reference
454 cd9660_rrip_extref(ISO_RRIP_EXTREF
*p
, ISO_RRIP_ANALYZE
*ana
)
456 if (isonum_711(p
->len_id
) != 10
457 || bcmp((char *)p
+ 8,"RRIP_1991A",10)
458 || isonum_711(p
->version
) != 1)
460 ana
->fields
&= ~ISO_SUSP_EXTREF
;
461 return ISO_SUSP_EXTREF
;
464 typedef int (*rrip_table_func
)(ISO_SUSP_HEADER
*phead
, ISO_RRIP_ANALYZE
*ana
);
465 typedef int (*rrip_table_func2
)(struct iso_directory_record
*isodir
,
466 ISO_RRIP_ANALYZE
*ana
);
469 rrip_table_func func
;
470 rrip_table_func2 func2
;
475 cd9660_rrip_loop(struct iso_directory_record
*isodir
, ISO_RRIP_ANALYZE
*ana
,
478 register RRIP_TABLE
*ptable
;
479 register ISO_SUSP_HEADER
*phead
;
480 register ISO_SUSP_HEADER
*pend
;
481 struct buf
*bp
= NULL
;
486 * Note: If name length is odd,
487 * it will be padding 1 byte after the name
489 pwhead
= isodir
->name
+ isonum_711(isodir
->name_len
);
490 if (!(isonum_711(isodir
->name_len
)&1))
493 /* If it's not the '.' entry of the root dir obey SP field */
494 if (*isodir
->name
!= 0
495 || isonum_733(isodir
->extent
) != ana
->imp
->root_extent
)
496 pwhead
+= ana
->imp
->rr_skip
;
498 pwhead
+= ana
->imp
->rr_skip0
;
500 phead
= (ISO_SUSP_HEADER
*)pwhead
;
501 pend
= (ISO_SUSP_HEADER
*)((char *)isodir
+ isonum_711(isodir
->length
));
507 * Note: "pend" should be more than one SUSP header
509 while (pend
>= phead
+ 1) {
510 if (isonum_711(phead
->version
) == 1) {
511 for (ptable
= table
; ptable
->func
; ptable
++) {
512 if (*phead
->type
== *ptable
->type
513 && phead
->type
[1] == ptable
->type
[1]) {
514 result
|= (ptable
->func(phead
,ana
));
521 if (result
&ISO_SUSP_STOP
) {
522 result
&= ~ISO_SUSP_STOP
;
525 /* plausibility check */
526 if (isonum_711(phead
->length
) < sizeof(*phead
))
530 * Hopefully this works with newer versions, too
532 phead
= (ISO_SUSP_HEADER
*)((char *)phead
+ isonum_711(phead
->length
));
535 if (ana
->fields
&& ana
->iso_ce_len
) {
536 if (ana
->iso_ce_blk
>= ana
->imp
->volume_space_size
537 || ana
->iso_ce_off
+ ana
->iso_ce_len
> ana
->imp
->logical_block_size
538 || buf_bread(ana
->imp
->im_devvp
,
539 #if 1 // radar 1669467 - logical and physical blocksize are the same
540 (daddr64_t
)((unsigned)ana
->iso_ce_blk
),
542 (daddr64_t
)((unsigned)(ana
->iso_ce_blk
<< (ana
->imp
->im_bshift
- DEV_BSHIFT
))),
543 #endif // radar 1669467
544 ana
->imp
->logical_block_size
, NOCRED
, &bp
))
545 /* what to do now? */
547 phead
= (ISO_SUSP_HEADER
*)((char *)buf_dataptr(bp
) + ana
->iso_ce_off
);
548 pend
= (ISO_SUSP_HEADER
*) ((char *)phead
+ ana
->iso_ce_len
);
555 * If we don't find the Basic SUSP stuffs, just set default value
556 * (attribute/time stamp)
558 for (ptable
= table
; ptable
->func2
; ptable
++)
559 if (!(ptable
->result
&result
))
560 ptable
->func2(isodir
,ana
);
568 static RRIP_TABLE rrip_table_analyze
[] = {
569 { "PX", (rrip_table_func
)cd9660_rrip_attr
,
570 (rrip_table_func2
)cd9660_rrip_defattr
,
572 { "TF", (rrip_table_func
)cd9660_rrip_tstamp
,
573 (rrip_table_func2
)cd9660_rrip_deftstamp
,
575 { "PN", (rrip_table_func
)cd9660_rrip_device
,
576 0, ISO_SUSP_DEVICE
},
577 { "RR", (rrip_table_func
)cd9660_rrip_idflag
,
578 0, ISO_SUSP_IDFLAG
},
579 { "CE", (rrip_table_func
)cd9660_rrip_cont
,
581 { "ST", cd9660_rrip_stop
, 0, ISO_SUSP_STOP
},
586 cd9660_rrip_analyze(struct iso_directory_record
*isodir
, struct iso_node
*inop
,
589 ISO_RRIP_ANALYZE analyze
;
593 analyze
.fields
= ISO_SUSP_ATTR
|ISO_SUSP_TSTAMP
|ISO_SUSP_DEVICE
;
595 return cd9660_rrip_loop(isodir
,&analyze
,rrip_table_analyze
);
599 * Get Alternate Name.
601 static RRIP_TABLE rrip_table_getname
[] = {
602 { "NM", (rrip_table_func
)cd9660_rrip_altname
,
603 (rrip_table_func2
)cd9660_rrip_defname
,
605 { "CL", (rrip_table_func
)cd9660_rrip_pclink
,
606 0, ISO_SUSP_CLINK
|ISO_SUSP_PLINK
},
607 { "PL", (rrip_table_func
)cd9660_rrip_pclink
,
608 0, ISO_SUSP_CLINK
|ISO_SUSP_PLINK
},
609 { "RE", (rrip_table_func
)cd9660_rrip_reldir
,
610 0, ISO_SUSP_RELDIR
},
611 { "RR", (rrip_table_func
)cd9660_rrip_idflag
,
612 0, ISO_SUSP_IDFLAG
},
613 { "CE", (rrip_table_func
)cd9660_rrip_cont
,
615 { "ST", cd9660_rrip_stop
, 0, ISO_SUSP_STOP
},
620 cd9660_rrip_getname(struct iso_directory_record
*isodir
, char *outbuf
,
621 u_short
*outlen
, ino_t
*inump
, struct iso_mnt
*imp
)
623 ISO_RRIP_ANALYZE analyze
;
626 analyze
.outbuf
= outbuf
;
627 analyze
.outlen
= outlen
;
628 analyze
.maxlen
= ISO_RRIP_NAMEMAX
;
629 analyze
.inump
= inump
;
631 analyze
.fields
= ISO_SUSP_ALTNAME
|ISO_SUSP_RELDIR
|ISO_SUSP_CLINK
|ISO_SUSP_PLINK
;
634 tab
= rrip_table_getname
;
635 if (*isodir
->name
== 0
636 || *isodir
->name
== 1) {
637 cd9660_rrip_defname(isodir
,&analyze
);
639 analyze
.fields
&= ~ISO_SUSP_ALTNAME
;
643 return cd9660_rrip_loop(isodir
,&analyze
,tab
);
649 static RRIP_TABLE rrip_table_getsymname
[] = {
650 { "SL", (rrip_table_func
)cd9660_rrip_slink
,
652 { "RR", (rrip_table_func
)cd9660_rrip_idflag
,
653 0, ISO_SUSP_IDFLAG
},
654 { "CE", (rrip_table_func
)cd9660_rrip_cont
,
656 { "ST", cd9660_rrip_stop
, 0, ISO_SUSP_STOP
},
661 cd9660_rrip_getsymname(struct iso_directory_record
*isodir
, char *outbuf
,
662 u_short
*outlen
, struct iso_mnt
*imp
)
664 ISO_RRIP_ANALYZE analyze
;
666 analyze
.outbuf
= outbuf
;
667 analyze
.outlen
= outlen
;
669 analyze
.maxlen
= MAXPATHLEN
;
670 analyze
.cont
= 1; /* don't start with a slash */
672 analyze
.fields
= ISO_SUSP_SLINK
;
674 return (cd9660_rrip_loop(isodir
,&analyze
,rrip_table_getsymname
)&ISO_SUSP_SLINK
);
677 static RRIP_TABLE rrip_table_extref
[] = {
678 { "ER", (rrip_table_func
)cd9660_rrip_extref
,
679 0, ISO_SUSP_EXTREF
},
680 { "CE", (rrip_table_func
)cd9660_rrip_cont
,
682 { "ST", cd9660_rrip_stop
, 0, ISO_SUSP_STOP
},
687 * Check for Rock Ridge Extension and return offset of its fields.
688 * Note: We insist on the ER field.
691 cd9660_rrip_offset(struct iso_directory_record
*isodir
, struct iso_mnt
*imp
)
694 ISO_RRIP_ANALYZE analyze
;
697 p
= (ISO_RRIP_OFFSET
*)(isodir
->name
+ 1);
698 if (bcmp(p
,"SP\7\1\276\357",6)) {
699 /* Maybe, it's a CDROM XA disc? */
701 p
= (ISO_RRIP_OFFSET
*)((char *)p
+ 15);
702 if (bcmp(p
,"SP\7\1\276\357",6))
707 analyze
.fields
= ISO_SUSP_EXTREF
;
708 if (!(cd9660_rrip_loop(isodir
,&analyze
,rrip_table_extref
)&ISO_SUSP_EXTREF
))
711 return isonum_711(p
->skip
);