2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
25 /* $NetBSD: cd9660_rrip.c,v 1.11 1994/12/24 15:30:10 cgd Exp $ */
28 * Copyright (c) 1993, 1994
29 * The Regents of the University of California. All rights reserved.
31 * This code is derived from software contributed to Berkeley
32 * by Pace Willisson (pace@blitz.com). The Rock Ridge Extension
33 * Support code is derived from software contributed to Berkeley
34 * by Atsushi Murai (amurai@spec.co.jp).
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. All advertising materials mentioning features or use of this software
45 * must display the following acknowledgement:
46 * This product includes software developed by the University of
47 * California, Berkeley and its contributors.
48 * 4. Neither the name of the University nor the names of its contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * @(#)cd9660_rrip.c 8.6 (Berkeley) 12/5/94
69 * 22-Jan-98 radar 1669467 - ISO 9660 CD support - jwc
73 #include <sys/param.h>
74 #include <sys/systm.h>
75 #include <sys/vnode.h>
76 #include <sys/mount.h>
77 #include <sys/namei.h>
80 #include <sys/kernel.h>
82 #include <sys/types.h>
86 #include <isofs/cd9660/iso.h>
87 #include <isofs/cd9660/cd9660_node.h>
88 #include <isofs/cd9660/cd9660_rrip.h>
89 #include <isofs/cd9660/iso_rrip.h>
92 * POSIX file attribute
95 cd9660_rrip_attr(p
,ana
)
97 ISO_RRIP_ANALYZE
*ana
;
99 ana
->inop
->inode
.iso_mode
= isonum_733(p
->mode
);
100 ana
->inop
->inode
.iso_uid
= isonum_733(p
->uid
);
101 ana
->inop
->inode
.iso_gid
= isonum_733(p
->gid
);
102 ana
->inop
->inode
.iso_links
= isonum_733(p
->links
);
103 ana
->fields
&= ~ISO_SUSP_ATTR
;
104 return ISO_SUSP_ATTR
;
108 cd9660_rrip_defattr(isodir
,ana
)
109 struct iso_directory_record
*isodir
;
110 ISO_RRIP_ANALYZE
*ana
;
112 /* But this is a required field! */
113 printf("RRIP without PX field?\n");
114 cd9660_defattr(isodir
,ana
->inop
,NULL
);
121 cd9660_rrip_slink(p
,ana
)
123 ISO_RRIP_ANALYZE
*ana
;
125 register ISO_RRIP_SLINK_COMPONENT
*pcomp
;
126 register ISO_RRIP_SLINK_COMPONENT
*pcompe
;
128 char *outbuf
, *inbuf
;
130 pcomp
= (ISO_RRIP_SLINK_COMPONENT
*)p
->component
;
131 pcompe
= (ISO_RRIP_SLINK_COMPONENT
*)((char *)p
+ isonum_711(p
->h
.length
));
133 outbuf
= ana
->outbuf
;
137 * Gathering a Symbolic name from each component with path
141 pcomp
= (ISO_RRIP_SLINK_COMPONENT
*)((char *)pcomp
+ ISO_RRIP_SLSIZ
142 + isonum_711(pcomp
->clen
))) {
145 if (len
< ana
->maxlen
) {
155 switch (*pcomp
->cflag
) {
157 case ISO_SUSP_CFLAG_CURRENT
:
158 /* Inserting Current */
162 case ISO_SUSP_CFLAG_PARENT
:
163 /* Inserting Parent */
167 case ISO_SUSP_CFLAG_ROOT
:
168 /* Inserting slash for ROOT */
169 /* start over from beginning(?) */
174 case ISO_SUSP_CFLAG_VOLROOT
:
175 /* Inserting a mount point i.e. "/cdrom" */
179 inbuf
= ana
->imp
->im_mountp
->mnt_stat
.f_mntonname
;
180 wlen
= strlen(inbuf
);
183 case ISO_SUSP_CFLAG_HOST
:
184 /* Inserting hostname i.e. "kurt.tools.de" */
189 case ISO_SUSP_CFLAG_CONTINUE
:
193 /* Inserting component */
194 wlen
= isonum_711(pcomp
->clen
);
198 printf("RRIP with incorrect flags?");
199 wlen
= ana
->maxlen
+ 1;
203 if (len
+ wlen
> ana
->maxlen
) {
204 /* indicate error to caller */
207 ana
->outbuf
-= *ana
->outlen
;
212 bcopy(inbuf
,outbuf
,wlen
);
217 ana
->outbuf
= outbuf
;
221 if (!isonum_711(p
->flags
)) {
222 ana
->fields
&= ~ISO_SUSP_SLINK
;
223 return ISO_SUSP_SLINK
;
232 cd9660_rrip_altname(p
,ana
)
234 ISO_RRIP_ANALYZE
*ana
;
245 case ISO_SUSP_CFLAG_CURRENT
:
246 /* Inserting Current */
250 case ISO_SUSP_CFLAG_PARENT
:
251 /* Inserting Parent */
255 case ISO_SUSP_CFLAG_HOST
:
256 /* Inserting hostname i.e. "kurt.tools.de" */
261 case ISO_SUSP_CFLAG_CONTINUE
:
265 /* Inserting component */
266 wlen
= isonum_711(p
->h
.length
) - 5;
267 inbuf
= (char *)p
+ 5;
271 printf("RRIP with incorrect NM flags?\n");
272 wlen
= ana
->maxlen
+ 1;
276 if ((*ana
->outlen
+= wlen
) > ana
->maxlen
) {
277 /* treat as no name field */
278 ana
->fields
&= ~ISO_SUSP_ALTNAME
;
279 ana
->outbuf
-= *ana
->outlen
- wlen
;
284 bcopy(inbuf
,ana
->outbuf
,wlen
);
288 ana
->fields
&= ~ISO_SUSP_ALTNAME
;
289 return ISO_SUSP_ALTNAME
;
295 cd9660_rrip_defname(isodir
,ana
)
296 struct iso_directory_record
*isodir
;
297 ISO_RRIP_ANALYZE
*ana
;
299 strcpy(ana
->outbuf
,"..");
300 switch (*isodir
->name
) {
302 isofntrans(isodir
->name
, isonum_711(isodir
->name_len
),
303 ana
->outbuf
, ana
->outlen
, 1);
315 * Parent or Child Link
318 cd9660_rrip_pclink(p
,ana
)
320 ISO_RRIP_ANALYZE
*ana
;
322 *ana
->inump
= isonum_733(p
->dir_loc
) << ana
->imp
->im_bshift
;
323 ana
->fields
&= ~(ISO_SUSP_CLINK
|ISO_SUSP_PLINK
);
324 return *p
->h
.type
== 'C' ? ISO_SUSP_CLINK
: ISO_SUSP_PLINK
;
328 * Relocated directory
331 cd9660_rrip_reldir(p
,ana
)
333 ISO_RRIP_ANALYZE
*ana
;
335 /* special hack to make caller aware of RE field */
338 return ISO_SUSP_RELDIR
|ISO_SUSP_ALTNAME
|ISO_SUSP_CLINK
|ISO_SUSP_PLINK
;
342 cd9660_rrip_tstamp(p
,ana
)
344 ISO_RRIP_ANALYZE
*ana
;
350 /* Check a format of time stamp (7bytes/17bytes) */
351 if (!(*p
->flags
&ISO_SUSP_TSTAMP_FORM17
)) {
352 if (*p
->flags
&ISO_SUSP_TSTAMP_CREAT
)
355 if (*p
->flags
&ISO_SUSP_TSTAMP_MODIFY
) {
356 cd9660_tstamp_conv7(ptime
,&ana
->inop
->inode
.iso_mtime
);
359 bzero(&ana
->inop
->inode
.iso_mtime
,sizeof(struct timespec
));
361 if (*p
->flags
&ISO_SUSP_TSTAMP_ACCESS
) {
362 cd9660_tstamp_conv7(ptime
,&ana
->inop
->inode
.iso_atime
);
365 ana
->inop
->inode
.iso_atime
= ana
->inop
->inode
.iso_mtime
;
367 if (*p
->flags
&ISO_SUSP_TSTAMP_ATTR
)
368 cd9660_tstamp_conv7(ptime
,&ana
->inop
->inode
.iso_ctime
);
370 ana
->inop
->inode
.iso_ctime
= ana
->inop
->inode
.iso_mtime
;
373 if (*p
->flags
&ISO_SUSP_TSTAMP_CREAT
)
376 if (*p
->flags
&ISO_SUSP_TSTAMP_MODIFY
) {
377 cd9660_tstamp_conv17(ptime
,&ana
->inop
->inode
.iso_mtime
);
380 bzero(&ana
->inop
->inode
.iso_mtime
,sizeof(struct timespec
));
382 if (*p
->flags
&ISO_SUSP_TSTAMP_ACCESS
) {
383 cd9660_tstamp_conv17(ptime
,&ana
->inop
->inode
.iso_atime
);
386 ana
->inop
->inode
.iso_atime
= ana
->inop
->inode
.iso_mtime
;
388 if (*p
->flags
&ISO_SUSP_TSTAMP_ATTR
)
389 cd9660_tstamp_conv17(ptime
,&ana
->inop
->inode
.iso_ctime
);
391 ana
->inop
->inode
.iso_ctime
= ana
->inop
->inode
.iso_mtime
;
394 ana
->fields
&= ~ISO_SUSP_TSTAMP
;
395 return ISO_SUSP_TSTAMP
;
399 cd9660_rrip_deftstamp(isodir
,ana
)
400 struct iso_directory_record
*isodir
;
401 ISO_RRIP_ANALYZE
*ana
;
403 cd9660_deftstamp(isodir
,ana
->inop
,NULL
);
410 cd9660_rrip_device(p
,ana
)
412 ISO_RRIP_ANALYZE
*ana
;
416 high
= isonum_733(p
->dev_t_high
);
417 low
= isonum_733(p
->dev_t_low
);
420 ana
->inop
->inode
.iso_rdev
= makedev(major(low
), minor(low
));
422 ana
->inop
->inode
.iso_rdev
= makedev(high
, minor(low
));
423 ana
->fields
&= ~ISO_SUSP_DEVICE
;
424 return ISO_SUSP_DEVICE
;
431 cd9660_rrip_idflag(p
,ana
)
433 ISO_RRIP_ANALYZE
*ana
;
435 ana
->fields
&= isonum_711(p
->flags
)|~0xff; /* don't touch high bits */
436 /* special handling of RE field */
437 if (ana
->fields
&ISO_SUSP_RELDIR
)
438 return cd9660_rrip_reldir(p
,ana
);
440 return ISO_SUSP_IDFLAG
;
444 * Continuation pointer
447 cd9660_rrip_cont(p
,ana
)
449 ISO_RRIP_ANALYZE
*ana
;
451 ana
->iso_ce_blk
= isonum_733(p
->location
);
452 ana
->iso_ce_off
= isonum_733(p
->offset
);
453 ana
->iso_ce_len
= isonum_733(p
->length
);
454 return ISO_SUSP_CONT
;
461 cd9660_rrip_stop(p
,ana
)
463 ISO_RRIP_ANALYZE
*ana
;
465 return ISO_SUSP_STOP
;
469 * Extension reference
472 cd9660_rrip_extref(p
,ana
)
474 ISO_RRIP_ANALYZE
*ana
;
476 if (isonum_711(p
->len_id
) != 10
477 || bcmp((char *)p
+ 8,"RRIP_1991A",10)
478 || isonum_711(p
->version
) != 1)
480 ana
->fields
&= ~ISO_SUSP_EXTREF
;
481 return ISO_SUSP_EXTREF
;
492 cd9660_rrip_loop(isodir
,ana
,table
)
493 struct iso_directory_record
*isodir
;
494 ISO_RRIP_ANALYZE
*ana
;
497 register RRIP_TABLE
*ptable
;
498 register ISO_SUSP_HEADER
*phead
;
499 register ISO_SUSP_HEADER
*pend
;
500 struct buf
*bp
= NULL
;
505 * Note: If name length is odd,
506 * it will be padding 1 byte after the name
508 pwhead
= isodir
->name
+ isonum_711(isodir
->name_len
);
509 if (!(isonum_711(isodir
->name_len
)&1))
512 /* If it's not the '.' entry of the root dir obey SP field */
513 if (*isodir
->name
!= 0
514 || isonum_733(isodir
->extent
) != ana
->imp
->root_extent
)
515 pwhead
+= ana
->imp
->rr_skip
;
517 pwhead
+= ana
->imp
->rr_skip0
;
519 phead
= (ISO_SUSP_HEADER
*)pwhead
;
520 pend
= (ISO_SUSP_HEADER
*)((char *)isodir
+ isonum_711(isodir
->length
));
526 * Note: "pend" should be more than one SUSP header
528 while (pend
>= phead
+ 1) {
529 if (isonum_711(phead
->version
) == 1) {
530 for (ptable
= table
; ptable
->func
; ptable
++) {
531 if (*phead
->type
== *ptable
->type
532 && phead
->type
[1] == ptable
->type
[1]) {
533 result
|= ptable
->func(phead
,ana
);
540 if (result
&ISO_SUSP_STOP
) {
541 result
&= ~ISO_SUSP_STOP
;
544 /* plausibility check */
545 if (isonum_711(phead
->length
) < sizeof(*phead
))
549 * Hopefully this works with newer versions, too
551 phead
= (ISO_SUSP_HEADER
*)((char *)phead
+ isonum_711(phead
->length
));
554 if (ana
->fields
&& ana
->iso_ce_len
) {
555 if (ana
->iso_ce_blk
>= ana
->imp
->volume_space_size
556 || ana
->iso_ce_off
+ ana
->iso_ce_len
> ana
->imp
->logical_block_size
557 || bread(ana
->imp
->im_devvp
,
558 #if 1 // radar 1669467 - logical and physical blocksize are the same
561 ana
->iso_ce_blk
<< (ana
->imp
->im_bshift
- DEV_BSHIFT
),
562 #endif // radar 1669467
563 ana
->imp
->logical_block_size
, NOCRED
, &bp
))
564 /* what to do now? */
566 phead
= (ISO_SUSP_HEADER
*)(bp
->b_data
+ ana
->iso_ce_off
);
567 pend
= (ISO_SUSP_HEADER
*) ((char *)phead
+ ana
->iso_ce_len
);
574 * If we don't find the Basic SUSP stuffs, just set default value
575 * (attribute/time stamp)
577 for (ptable
= table
; ptable
->func2
; ptable
++)
578 if (!(ptable
->result
&result
))
579 ptable
->func2(isodir
,ana
);
587 static RRIP_TABLE rrip_table_analyze
[] = {
588 { "PX", cd9660_rrip_attr
, cd9660_rrip_defattr
, ISO_SUSP_ATTR
},
589 { "TF", cd9660_rrip_tstamp
, cd9660_rrip_deftstamp
, ISO_SUSP_TSTAMP
},
590 { "PN", cd9660_rrip_device
, 0, ISO_SUSP_DEVICE
},
591 { "RR", cd9660_rrip_idflag
, 0, ISO_SUSP_IDFLAG
},
592 { "CE", cd9660_rrip_cont
, 0, ISO_SUSP_CONT
},
593 { "ST", cd9660_rrip_stop
, 0, ISO_SUSP_STOP
},
598 cd9660_rrip_analyze(isodir
,inop
,imp
)
599 struct iso_directory_record
*isodir
;
600 struct iso_node
*inop
;
603 ISO_RRIP_ANALYZE analyze
;
607 analyze
.fields
= ISO_SUSP_ATTR
|ISO_SUSP_TSTAMP
|ISO_SUSP_DEVICE
;
609 return cd9660_rrip_loop(isodir
,&analyze
,rrip_table_analyze
);
613 * Get Alternate Name.
615 static RRIP_TABLE rrip_table_getname
[] = {
616 { "NM", cd9660_rrip_altname
, cd9660_rrip_defname
, ISO_SUSP_ALTNAME
},
617 { "CL", cd9660_rrip_pclink
, 0, ISO_SUSP_CLINK
|ISO_SUSP_PLINK
},
618 { "PL", cd9660_rrip_pclink
, 0, ISO_SUSP_CLINK
|ISO_SUSP_PLINK
},
619 { "RE", cd9660_rrip_reldir
, 0, ISO_SUSP_RELDIR
},
620 { "RR", cd9660_rrip_idflag
, 0, ISO_SUSP_IDFLAG
},
621 { "CE", cd9660_rrip_cont
, 0, ISO_SUSP_CONT
},
622 { "ST", cd9660_rrip_stop
, 0, ISO_SUSP_STOP
},
627 cd9660_rrip_getname(isodir
,outbuf
,outlen
,inump
,imp
)
628 struct iso_directory_record
*isodir
;
634 ISO_RRIP_ANALYZE analyze
;
637 analyze
.outbuf
= outbuf
;
638 analyze
.outlen
= outlen
;
639 analyze
.maxlen
= ISO_RRIP_NAMEMAX
;
640 analyze
.inump
= inump
;
642 analyze
.fields
= ISO_SUSP_ALTNAME
|ISO_SUSP_RELDIR
|ISO_SUSP_CLINK
|ISO_SUSP_PLINK
;
645 tab
= rrip_table_getname
;
646 if (*isodir
->name
== 0
647 || *isodir
->name
== 1) {
648 cd9660_rrip_defname(isodir
,&analyze
);
650 analyze
.fields
&= ~ISO_SUSP_ALTNAME
;
654 return cd9660_rrip_loop(isodir
,&analyze
,tab
);
660 static RRIP_TABLE rrip_table_getsymname
[] = {
661 { "SL", cd9660_rrip_slink
, 0, ISO_SUSP_SLINK
},
662 { "RR", cd9660_rrip_idflag
, 0, ISO_SUSP_IDFLAG
},
663 { "CE", cd9660_rrip_cont
, 0, ISO_SUSP_CONT
},
664 { "ST", cd9660_rrip_stop
, 0, ISO_SUSP_STOP
},
669 cd9660_rrip_getsymname(isodir
,outbuf
,outlen
,imp
)
670 struct iso_directory_record
*isodir
;
675 ISO_RRIP_ANALYZE analyze
;
677 analyze
.outbuf
= outbuf
;
678 analyze
.outlen
= outlen
;
680 analyze
.maxlen
= MAXPATHLEN
;
681 analyze
.cont
= 1; /* don't start with a slash */
683 analyze
.fields
= ISO_SUSP_SLINK
;
685 return (cd9660_rrip_loop(isodir
,&analyze
,rrip_table_getsymname
)&ISO_SUSP_SLINK
);
688 static RRIP_TABLE rrip_table_extref
[] = {
689 { "ER", cd9660_rrip_extref
, 0, ISO_SUSP_EXTREF
},
690 { "CE", cd9660_rrip_cont
, 0, ISO_SUSP_CONT
},
691 { "ST", cd9660_rrip_stop
, 0, ISO_SUSP_STOP
},
696 * Check for Rock Ridge Extension and return offset of its fields.
697 * Note: We insist on the ER field.
700 cd9660_rrip_offset(isodir
,imp
)
701 struct iso_directory_record
*isodir
;
705 ISO_RRIP_ANALYZE analyze
;
708 p
= (ISO_RRIP_OFFSET
*)(isodir
->name
+ 1);
709 if (bcmp(p
,"SP\7\1\276\357",6)) {
710 /* Maybe, it's a CDROM XA disc? */
712 p
= (ISO_RRIP_OFFSET
*)((char *)p
+ 15);
713 if (bcmp(p
,"SP\7\1\276\357",6))
718 analyze
.fields
= ISO_SUSP_EXTREF
;
719 if (!(cd9660_rrip_loop(isodir
,&analyze
,rrip_table_extref
)&ISO_SUSP_EXTREF
))
722 return isonum_711(p
->skip
);