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,
304 isonum_711(isodir
->flags
) & associatedBit
);
316 * Parent or Child Link
319 cd9660_rrip_pclink(p
,ana
)
321 ISO_RRIP_ANALYZE
*ana
;
323 *ana
->inump
= isonum_733(p
->dir_loc
) << ana
->imp
->im_bshift
;
324 ana
->fields
&= ~(ISO_SUSP_CLINK
|ISO_SUSP_PLINK
);
325 return *p
->h
.type
== 'C' ? ISO_SUSP_CLINK
: ISO_SUSP_PLINK
;
329 * Relocated directory
332 cd9660_rrip_reldir(p
,ana
)
334 ISO_RRIP_ANALYZE
*ana
;
336 /* special hack to make caller aware of RE field */
339 return ISO_SUSP_RELDIR
|ISO_SUSP_ALTNAME
|ISO_SUSP_CLINK
|ISO_SUSP_PLINK
;
343 cd9660_rrip_tstamp(p
,ana
)
345 ISO_RRIP_ANALYZE
*ana
;
351 /* Check a format of time stamp (7bytes/17bytes) */
352 if (!(*p
->flags
&ISO_SUSP_TSTAMP_FORM17
)) {
353 if (*p
->flags
&ISO_SUSP_TSTAMP_CREAT
)
356 if (*p
->flags
&ISO_SUSP_TSTAMP_MODIFY
) {
357 cd9660_tstamp_conv7(ptime
,&ana
->inop
->inode
.iso_mtime
);
360 bzero(&ana
->inop
->inode
.iso_mtime
,sizeof(struct timespec
));
362 if (*p
->flags
&ISO_SUSP_TSTAMP_ACCESS
) {
363 cd9660_tstamp_conv7(ptime
,&ana
->inop
->inode
.iso_atime
);
366 ana
->inop
->inode
.iso_atime
= ana
->inop
->inode
.iso_mtime
;
368 if (*p
->flags
&ISO_SUSP_TSTAMP_ATTR
)
369 cd9660_tstamp_conv7(ptime
,&ana
->inop
->inode
.iso_ctime
);
371 ana
->inop
->inode
.iso_ctime
= ana
->inop
->inode
.iso_mtime
;
374 if (*p
->flags
&ISO_SUSP_TSTAMP_CREAT
)
377 if (*p
->flags
&ISO_SUSP_TSTAMP_MODIFY
) {
378 cd9660_tstamp_conv17(ptime
,&ana
->inop
->inode
.iso_mtime
);
381 bzero(&ana
->inop
->inode
.iso_mtime
,sizeof(struct timespec
));
383 if (*p
->flags
&ISO_SUSP_TSTAMP_ACCESS
) {
384 cd9660_tstamp_conv17(ptime
,&ana
->inop
->inode
.iso_atime
);
387 ana
->inop
->inode
.iso_atime
= ana
->inop
->inode
.iso_mtime
;
389 if (*p
->flags
&ISO_SUSP_TSTAMP_ATTR
)
390 cd9660_tstamp_conv17(ptime
,&ana
->inop
->inode
.iso_ctime
);
392 ana
->inop
->inode
.iso_ctime
= ana
->inop
->inode
.iso_mtime
;
395 ana
->fields
&= ~ISO_SUSP_TSTAMP
;
396 return ISO_SUSP_TSTAMP
;
400 cd9660_rrip_deftstamp(isodir
,ana
)
401 struct iso_directory_record
*isodir
;
402 ISO_RRIP_ANALYZE
*ana
;
404 cd9660_deftstamp(isodir
,ana
->inop
,NULL
);
411 cd9660_rrip_device(p
,ana
)
413 ISO_RRIP_ANALYZE
*ana
;
417 high
= isonum_733(p
->dev_t_high
);
418 low
= isonum_733(p
->dev_t_low
);
421 ana
->inop
->inode
.iso_rdev
= makedev(major(low
), minor(low
));
423 ana
->inop
->inode
.iso_rdev
= makedev(high
, minor(low
));
424 ana
->fields
&= ~ISO_SUSP_DEVICE
;
425 return ISO_SUSP_DEVICE
;
432 cd9660_rrip_idflag(p
,ana
)
434 ISO_RRIP_ANALYZE
*ana
;
436 ana
->fields
&= isonum_711(p
->flags
)|~0xff; /* don't touch high bits */
437 /* special handling of RE field */
438 if (ana
->fields
&ISO_SUSP_RELDIR
)
439 return cd9660_rrip_reldir(p
,ana
);
441 return ISO_SUSP_IDFLAG
;
445 * Continuation pointer
448 cd9660_rrip_cont(p
,ana
)
450 ISO_RRIP_ANALYZE
*ana
;
452 ana
->iso_ce_blk
= isonum_733(p
->location
);
453 ana
->iso_ce_off
= isonum_733(p
->offset
);
454 ana
->iso_ce_len
= isonum_733(p
->length
);
455 return ISO_SUSP_CONT
;
462 cd9660_rrip_stop(p
,ana
)
464 ISO_RRIP_ANALYZE
*ana
;
466 return ISO_SUSP_STOP
;
470 * Extension reference
473 cd9660_rrip_extref(p
,ana
)
475 ISO_RRIP_ANALYZE
*ana
;
477 if (isonum_711(p
->len_id
) != 10
478 || bcmp((char *)p
+ 8,"RRIP_1991A",10)
479 || isonum_711(p
->version
) != 1)
481 ana
->fields
&= ~ISO_SUSP_EXTREF
;
482 return ISO_SUSP_EXTREF
;
493 cd9660_rrip_loop(isodir
,ana
,table
)
494 struct iso_directory_record
*isodir
;
495 ISO_RRIP_ANALYZE
*ana
;
498 register RRIP_TABLE
*ptable
;
499 register ISO_SUSP_HEADER
*phead
;
500 register ISO_SUSP_HEADER
*pend
;
501 struct buf
*bp
= NULL
;
506 * Note: If name length is odd,
507 * it will be padding 1 byte after the name
509 pwhead
= isodir
->name
+ isonum_711(isodir
->name_len
);
510 if (!(isonum_711(isodir
->name_len
)&1))
513 /* If it's not the '.' entry of the root dir obey SP field */
514 if (*isodir
->name
!= 0
515 || isonum_733(isodir
->extent
) != ana
->imp
->root_extent
)
516 pwhead
+= ana
->imp
->rr_skip
;
518 pwhead
+= ana
->imp
->rr_skip0
;
520 phead
= (ISO_SUSP_HEADER
*)pwhead
;
521 pend
= (ISO_SUSP_HEADER
*)((char *)isodir
+ isonum_711(isodir
->length
));
527 * Note: "pend" should be more than one SUSP header
529 while (pend
>= phead
+ 1) {
530 if (isonum_711(phead
->version
) == 1) {
531 for (ptable
= table
; ptable
->func
; ptable
++) {
532 if (*phead
->type
== *ptable
->type
533 && phead
->type
[1] == ptable
->type
[1]) {
534 result
|= ptable
->func(phead
,ana
);
541 if (result
&ISO_SUSP_STOP
) {
542 result
&= ~ISO_SUSP_STOP
;
545 /* plausibility check */
546 if (isonum_711(phead
->length
) < sizeof(*phead
))
550 * Hopefully this works with newer versions, too
552 phead
= (ISO_SUSP_HEADER
*)((char *)phead
+ isonum_711(phead
->length
));
555 if (ana
->fields
&& ana
->iso_ce_len
) {
556 if (ana
->iso_ce_blk
>= ana
->imp
->volume_space_size
557 || ana
->iso_ce_off
+ ana
->iso_ce_len
> ana
->imp
->logical_block_size
558 || bread(ana
->imp
->im_devvp
,
559 #if 1 // radar 1669467 - logical and physical blocksize are the same
562 ana
->iso_ce_blk
<< (ana
->imp
->im_bshift
- DEV_BSHIFT
),
563 #endif // radar 1669467
564 ana
->imp
->logical_block_size
, NOCRED
, &bp
))
565 /* what to do now? */
567 phead
= (ISO_SUSP_HEADER
*)(bp
->b_data
+ ana
->iso_ce_off
);
568 pend
= (ISO_SUSP_HEADER
*) ((char *)phead
+ ana
->iso_ce_len
);
575 * If we don't find the Basic SUSP stuffs, just set default value
576 * (attribute/time stamp)
578 for (ptable
= table
; ptable
->func2
; ptable
++)
579 if (!(ptable
->result
&result
))
580 ptable
->func2(isodir
,ana
);
588 static RRIP_TABLE rrip_table_analyze
[] = {
589 { "PX", cd9660_rrip_attr
, cd9660_rrip_defattr
, ISO_SUSP_ATTR
},
590 { "TF", cd9660_rrip_tstamp
, cd9660_rrip_deftstamp
, ISO_SUSP_TSTAMP
},
591 { "PN", cd9660_rrip_device
, 0, ISO_SUSP_DEVICE
},
592 { "RR", cd9660_rrip_idflag
, 0, ISO_SUSP_IDFLAG
},
593 { "CE", cd9660_rrip_cont
, 0, ISO_SUSP_CONT
},
594 { "ST", cd9660_rrip_stop
, 0, ISO_SUSP_STOP
},
599 cd9660_rrip_analyze(isodir
,inop
,imp
)
600 struct iso_directory_record
*isodir
;
601 struct iso_node
*inop
;
604 ISO_RRIP_ANALYZE analyze
;
608 analyze
.fields
= ISO_SUSP_ATTR
|ISO_SUSP_TSTAMP
|ISO_SUSP_DEVICE
;
610 return cd9660_rrip_loop(isodir
,&analyze
,rrip_table_analyze
);
614 * Get Alternate Name.
616 static RRIP_TABLE rrip_table_getname
[] = {
617 { "NM", cd9660_rrip_altname
, cd9660_rrip_defname
, ISO_SUSP_ALTNAME
},
618 { "CL", cd9660_rrip_pclink
, 0, ISO_SUSP_CLINK
|ISO_SUSP_PLINK
},
619 { "PL", cd9660_rrip_pclink
, 0, ISO_SUSP_CLINK
|ISO_SUSP_PLINK
},
620 { "RE", cd9660_rrip_reldir
, 0, ISO_SUSP_RELDIR
},
621 { "RR", cd9660_rrip_idflag
, 0, ISO_SUSP_IDFLAG
},
622 { "CE", cd9660_rrip_cont
, 0, ISO_SUSP_CONT
},
623 { "ST", cd9660_rrip_stop
, 0, ISO_SUSP_STOP
},
628 cd9660_rrip_getname(isodir
,outbuf
,outlen
,inump
,imp
)
629 struct iso_directory_record
*isodir
;
635 ISO_RRIP_ANALYZE analyze
;
638 analyze
.outbuf
= outbuf
;
639 analyze
.outlen
= outlen
;
640 analyze
.maxlen
= ISO_RRIP_NAMEMAX
;
641 analyze
.inump
= inump
;
643 analyze
.fields
= ISO_SUSP_ALTNAME
|ISO_SUSP_RELDIR
|ISO_SUSP_CLINK
|ISO_SUSP_PLINK
;
646 tab
= rrip_table_getname
;
647 if (*isodir
->name
== 0
648 || *isodir
->name
== 1) {
649 cd9660_rrip_defname(isodir
,&analyze
);
651 analyze
.fields
&= ~ISO_SUSP_ALTNAME
;
655 return cd9660_rrip_loop(isodir
,&analyze
,tab
);
661 static RRIP_TABLE rrip_table_getsymname
[] = {
662 { "SL", cd9660_rrip_slink
, 0, ISO_SUSP_SLINK
},
663 { "RR", cd9660_rrip_idflag
, 0, ISO_SUSP_IDFLAG
},
664 { "CE", cd9660_rrip_cont
, 0, ISO_SUSP_CONT
},
665 { "ST", cd9660_rrip_stop
, 0, ISO_SUSP_STOP
},
670 cd9660_rrip_getsymname(isodir
,outbuf
,outlen
,imp
)
671 struct iso_directory_record
*isodir
;
676 ISO_RRIP_ANALYZE analyze
;
678 analyze
.outbuf
= outbuf
;
679 analyze
.outlen
= outlen
;
681 analyze
.maxlen
= MAXPATHLEN
;
682 analyze
.cont
= 1; /* don't start with a slash */
684 analyze
.fields
= ISO_SUSP_SLINK
;
686 return (cd9660_rrip_loop(isodir
,&analyze
,rrip_table_getsymname
)&ISO_SUSP_SLINK
);
689 static RRIP_TABLE rrip_table_extref
[] = {
690 { "ER", cd9660_rrip_extref
, 0, ISO_SUSP_EXTREF
},
691 { "CE", cd9660_rrip_cont
, 0, ISO_SUSP_CONT
},
692 { "ST", cd9660_rrip_stop
, 0, ISO_SUSP_STOP
},
697 * Check for Rock Ridge Extension and return offset of its fields.
698 * Note: We insist on the ER field.
701 cd9660_rrip_offset(isodir
,imp
)
702 struct iso_directory_record
*isodir
;
706 ISO_RRIP_ANALYZE analyze
;
709 p
= (ISO_RRIP_OFFSET
*)(isodir
->name
+ 1);
710 if (bcmp(p
,"SP\7\1\276\357",6)) {
711 /* Maybe, it's a CDROM XA disc? */
713 p
= (ISO_RRIP_OFFSET
*)((char *)p
+ 15);
714 if (bcmp(p
,"SP\7\1\276\357",6))
719 analyze
.fields
= ISO_SUSP_EXTREF
;
720 if (!(cd9660_rrip_loop(isodir
,&analyze
,rrip_table_extref
)&ISO_SUSP_EXTREF
))
723 return isonum_711(p
->skip
);