1 /* $NetBSD: cd9660.c,v 1.5 1997/06/26 19:11:33 drochner Exp $ */
4 * Copyright (C) 1996 Wolfgang Solfrank.
5 * Copyright (C) 1996 TooLs GmbH.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by TooLs GmbH.
19 * 4. The name of TooLs GmbH may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
38 * Stand-alone ISO9660 file reading package.
40 * Note: This doesn't support Rock Ridge extensions, extended attributes,
41 * blocksizes other than 2048 bytes, multi-extent files, etc.
43 #include <sys/param.h>
46 #include <sys/dirent.h>
47 #include <fs/cd9660/iso.h>
48 #include <fs/cd9660/cd9660_rrip.h>
52 #define SUSP_CONTINUATION "CE"
53 #define SUSP_PRESENT "SP"
54 #define SUSP_STOP "ST"
55 #define SUSP_EXTREF "ER"
56 #define RRIP_NAME "NM"
60 u_char signature [ISODCL ( 5, 6)];
61 u_char len_skp [ISODCL ( 7, 7)]; /* 711 */
64 static int buf_read_file(struct open_file *f, char **buf_p,
66 static int cd9660_open(const char *path, struct open_file *f);
67 static int cd9660_close(struct open_file *f);
68 static int cd9660_read(struct open_file *f, void *buf, size_t size,
70 static off_t cd9660_seek(struct open_file *f, off_t offset, int where);
71 static int cd9660_stat(struct open_file *f, struct stat *sb);
72 static int cd9660_readdir(struct open_file *f, struct dirent *d);
73 static int cd9660_mount(const char *, const char *, void **);
74 static int cd9660_unmount(const char *, void *);
75 static int dirmatch(struct open_file *f, const char *path,
76 struct iso_directory_record *dp, int use_rrip, int lenskip);
77 static int rrip_check(struct open_file *f, struct iso_directory_record *dp,
79 static char *rrip_lookup_name(struct open_file *f,
80 struct iso_directory_record *dp, int lenskip, size_t *len);
81 static ISO_SUSP_HEADER *susp_lookup_record(struct open_file *f,
82 const char *identifier, struct iso_directory_record *dp,
85 struct fs_ops cd9660_fsops = {
87 .fo_open = cd9660_open,
88 .fo_close = cd9660_close,
89 .fo_read = cd9660_read,
90 .fo_write = null_write,
91 .fo_seek = cd9660_seek,
92 .fo_stat = cd9660_stat,
93 .fo_readdir = cd9660_readdir,
94 .fo_mount = cd9660_mount,
95 .fo_unmount = cd9660_unmount
98 typedef struct cd9660_mnt {
99 struct devdesc *cd_dev;
101 struct iso_directory_record cd_rec;
102 STAILQ_ENTRY(cd9660_mnt) cd_link;
105 typedef STAILQ_HEAD(cd9660_mnt_list, cd9660_mnt) cd9660_mnt_list_t;
106 static cd9660_mnt_list_t mnt_list = STAILQ_HEAD_INITIALIZER(mnt_list);
108 #define F_ISDIR 0x0001 /* Directory */
109 #define F_ROOTDIR 0x0002 /* Root directory */
110 #define F_RR 0x0004 /* Rock Ridge on this volume */
113 int f_flags; /* file flags */
114 off_t f_off; /* Current offset within file */
115 daddr_t f_bno; /* Starting block number */
116 off_t f_size; /* Size of file */
117 daddr_t f_buf_blkno; /* block number of data block */
118 char *f_buf; /* buffer for data block */
119 int f_susp_skip; /* len_skip for SUSP records */
123 char namlen [ISODCL( 1, 1)]; /* 711 */
124 char extlen [ISODCL( 2, 2)]; /* 711 */
125 char block [ISODCL( 3, 6)]; /* 732 */
126 char parent [ISODCL( 7, 8)]; /* 722 */
130 #define PTSIZE(pp) roundup(PTFIXSZ + isonum_711((pp)->namlen), 2)
132 #define cdb2devb(bno) ((bno) * ISO_DEFAULT_BLOCK_SIZE / DEV_BSIZE)
134 static ISO_SUSP_HEADER *
135 susp_lookup_record(struct open_file *f, const char *identifier,
136 struct iso_directory_record *dp, int lenskip)
138 static char susp_buffer[ISO_DEFAULT_BLOCK_SIZE];
145 p = dp->name + isonum_711(dp->name_len) + lenskip;
146 /* Names of even length have a padding byte after the name. */
147 if ((isonum_711(dp->name_len) & 1) == 0)
149 end = (char *)dp + isonum_711(dp->length);
150 while (p + 3 < end) {
151 sh = (ISO_SUSP_HEADER *)p;
152 if (bcmp(sh->type, identifier, 2) == 0)
154 if (bcmp(sh->type, SUSP_STOP, 2) == 0)
156 if (bcmp(sh->type, SUSP_CONTINUATION, 2) == 0) {
157 shc = (ISO_RRIP_CONT *)sh;
158 error = f->f_dev->dv_strategy(f->f_devdata, F_READ,
159 cdb2devb(isonum_733(shc->location)),
160 ISO_DEFAULT_BLOCK_SIZE, susp_buffer, &read);
162 /* Bail if it fails. */
163 if (error != 0 || read != ISO_DEFAULT_BLOCK_SIZE)
165 p = susp_buffer + isonum_733(shc->offset);
166 end = p + isonum_733(shc->length);
168 /* Ignore this record and skip to the next. */
169 p += isonum_711(sh->length);
171 /* Avoid infinite loops with corrupted file systems */
172 if (isonum_711(sh->length) == 0)
180 rrip_lookup_name(struct open_file *f, struct iso_directory_record *dp,
181 int lenskip, size_t *len)
188 p = (ISO_RRIP_ALTNAME *)susp_lookup_record(f, RRIP_NAME, dp, lenskip);
192 case ISO_SUSP_CFLAG_CURRENT:
195 case ISO_SUSP_CFLAG_PARENT:
199 *len = isonum_711(p->h.length) - 5;
200 return ((char *)p + 5);
203 * We don't handle hostnames or continued names as they are
204 * too hard, so just bail and use the default name.
211 rrip_check(struct open_file *f, struct iso_directory_record *dp, int *lenskip)
213 ISO_SUSP_PRESENT *sp;
217 /* First, see if we can find a SP field. */
218 p = dp->name + isonum_711(dp->name_len);
219 if (p > (char *)dp + isonum_711(dp->length))
221 sp = (ISO_SUSP_PRESENT *)p;
222 if (bcmp(sp->h.type, SUSP_PRESENT, 2) != 0)
224 if (isonum_711(sp->h.length) != sizeof(ISO_SUSP_PRESENT))
226 if (sp->signature[0] != 0xbe || sp->signature[1] != 0xef)
228 *lenskip = isonum_711(sp->len_skp);
231 * Now look for an ER field. If RRIP is present, then there must
232 * be at least one of these. It would be more pedantic to walk
233 * through the list of fields looking for a Rock Ridge ER field.
235 er = (ISO_RRIP_EXTREF *)susp_lookup_record(f, SUSP_EXTREF, dp, 0);
242 dirmatch(struct open_file *f, const char *path, struct iso_directory_record *dp,
243 int use_rrip, int lenskip)
250 cp = rrip_lookup_name(f, dp, lenskip, &len);
254 len = isonum_711(dp->name_len);
260 sep = strchr(path, '/');
270 for (i = len; --i >= 0; path++, cp++) {
271 if (!*path || *path == '/')
275 if (!icase && toupper(*path) == *cp)
279 if (*path && *path != '/')
282 * Allow stripping of trailing dots and the version number.
283 * Note that this will find the first instead of the last version
286 if (i >= 0 && (*cp == ';' || *cp == '.')) {
287 /* This is to prevent matching of numeric extensions */
288 if (*cp == '.' && cp[1] != ';')
291 if (*++cp != ';' && (*cp < '0' || *cp > '9'))
298 cd9660_read_dr(struct open_file *f, struct iso_directory_record *rec)
300 struct iso_primary_descriptor *vd;
306 vd = malloc(MAX(ISO_DEFAULT_BLOCK_SIZE,
307 sizeof(struct iso_primary_descriptor)));
311 for (bno = 16;; bno++) {
313 rc = f->f_dev->dv_strategy(f->f_devdata, F_READ, cdb2devb(bno),
314 ISO_DEFAULT_BLOCK_SIZE, (char *)vd, &read);
317 if (read != ISO_DEFAULT_BLOCK_SIZE) {
322 if (bcmp(vd->id, ISO_STANDARD_ID, sizeof(vd->id)) != 0)
324 if (isonum_711(vd->type) == ISO_VD_END)
326 if (isonum_711(vd->type) == ISO_VD_PRIMARY)
329 if (isonum_723(vd->logical_block_size) == ISO_DEFAULT_BLOCK_SIZE) {
330 bcopy(vd->root_directory_record, rec, sizeof(*rec));
339 cd9660_open(const char *path, struct open_file *f)
341 struct file *fp = NULL;
343 size_t read, dsize, off;
345 struct iso_directory_record rec;
346 struct iso_directory_record *dp = NULL;
347 int rc, first, use_rrip, lenskip;
352 /* First find the volume descriptor */
354 buf = malloc(MAX(ISO_DEFAULT_BLOCK_SIZE,
355 sizeof(struct iso_primary_descriptor)));
360 STAILQ_FOREACH(mnt, &mnt_list, cd_link) {
361 if (dev->d_dev->dv_type == mnt->cd_dev->d_dev->dv_type &&
362 dev->d_unit == mnt->cd_dev->d_unit)
368 rc = cd9660_read_dr(f, &rec);
376 path++; /* eat leading '/' */
382 bno = isonum_733(rec.extent) + isonum_711(rec.ext_attr_length);
383 dsize = isonum_733(rec.size);
387 while (off < dsize) {
388 if ((off % ISO_DEFAULT_BLOCK_SIZE) == 0) {
390 rc = f->f_dev->dv_strategy
391 (f->f_devdata, F_READ,
392 cdb2devb(bno + boff),
393 ISO_DEFAULT_BLOCK_SIZE,
397 if (read != ISO_DEFAULT_BLOCK_SIZE) {
402 dp = (struct iso_directory_record *) buf;
404 if (isonum_711(dp->length) == 0) {
405 /* skip to next block, if any */
406 off = boff * ISO_DEFAULT_BLOCK_SIZE;
410 /* See if RRIP is in use. */
412 use_rrip = rrip_check(f, dp, &lenskip);
414 if (dirmatch(f, path, dp, use_rrip,
415 first ? 0 : lenskip)) {
421 dp = (struct iso_directory_record *)
422 ((char *) dp + isonum_711(dp->length));
423 /* If the new block has zero length, it is padding. */
424 if (isonum_711(dp->length) == 0) {
425 /* Skip to next block, if any. */
426 off = boff * ISO_DEFAULT_BLOCK_SIZE;
429 off += isonum_711(dp->length);
437 while (*path && *path != '/') /* look for next component */
440 if (*path) /* this component was directory */
444 path++; /* skip '/' */
446 if (*path) /* We do have next component. */
451 * if the path had trailing / but the path does point to file,
452 * report the error ENOTDIR.
454 if (isdir == true && (isonum_711(rec.flags) & 2) == 0) {
459 /* allocate file system specific data structure */
460 fp = malloc(sizeof(struct file));
461 bzero(fp, sizeof(struct file));
462 f->f_fsdata = (void *)fp;
464 if ((isonum_711(rec.flags) & 2) != 0) {
465 fp->f_flags = F_ISDIR;
468 fp->f_flags |= F_ROOTDIR;
470 /* Check for Rock Ridge since we didn't in the loop above. */
471 bno = isonum_733(rec.extent) + isonum_711(rec.ext_attr_length);
473 rc = f->f_dev->dv_strategy(f->f_devdata, F_READ, cdb2devb(bno),
474 ISO_DEFAULT_BLOCK_SIZE, buf, &read);
477 if (read != ISO_DEFAULT_BLOCK_SIZE) {
481 dp = (struct iso_directory_record *)buf;
482 use_rrip = rrip_check(f, dp, &lenskip);
486 fp->f_susp_skip = lenskip;
489 fp->f_bno = isonum_733(rec.extent) + isonum_711(rec.ext_attr_length);
490 fp->f_size = isonum_733(rec.size);
503 cd9660_close(struct open_file *f)
505 struct file *fp = (struct file *)f->f_fsdata;
514 buf_read_file(struct open_file *f, char **buf_p, size_t *size_p)
516 struct file *fp = (struct file *)f->f_fsdata;
517 daddr_t blkno, blkoff;
521 blkno = fp->f_off / ISO_DEFAULT_BLOCK_SIZE + fp->f_bno;
522 blkoff = fp->f_off % ISO_DEFAULT_BLOCK_SIZE;
524 if (blkno != fp->f_buf_blkno) {
525 if (fp->f_buf == (char *)0)
526 fp->f_buf = malloc(ISO_DEFAULT_BLOCK_SIZE);
529 rc = f->f_dev->dv_strategy(f->f_devdata, F_READ,
530 cdb2devb(blkno), ISO_DEFAULT_BLOCK_SIZE,
534 if (read != ISO_DEFAULT_BLOCK_SIZE)
537 fp->f_buf_blkno = blkno;
540 *buf_p = fp->f_buf + blkoff;
541 *size_p = ISO_DEFAULT_BLOCK_SIZE - blkoff;
543 if (*size_p > fp->f_size - fp->f_off)
544 *size_p = fp->f_size - fp->f_off;
549 cd9660_read(struct open_file *f, void *start, size_t size, size_t *resid)
551 struct file *fp = (struct file *)f->f_fsdata;
553 size_t buf_size, csize;
558 if (fp->f_off < 0 || fp->f_off >= fp->f_size)
561 rc = buf_read_file(f, &buf, &buf_size);
565 csize = size > buf_size ? buf_size : size;
566 bcopy(buf, addr, csize);
578 cd9660_readdir(struct open_file *f, struct dirent *d)
580 struct file *fp = (struct file *)f->f_fsdata;
581 struct iso_directory_record *ep;
582 size_t buf_size, reclen, namelen;
588 if (fp->f_off >= fp->f_size)
590 error = buf_read_file(f, &buf, &buf_size);
593 ep = (struct iso_directory_record *)buf;
595 if (isonum_711(ep->length) == 0) {
598 /* skip to next block, if any */
599 blkno = fp->f_off / ISO_DEFAULT_BLOCK_SIZE;
600 fp->f_off = (blkno + 1) * ISO_DEFAULT_BLOCK_SIZE;
604 if (fp->f_flags & F_RR) {
605 if (fp->f_flags & F_ROOTDIR && fp->f_off == 0)
608 lenskip = fp->f_susp_skip;
609 name = rrip_lookup_name(f, ep, lenskip, &namelen);
613 namelen = isonum_711(ep->name_len);
616 if (ep->name[0] == 0)
618 else if (ep->name[0] == 1) {
624 reclen = sizeof(struct dirent) - (MAXNAMLEN+1) + namelen + 1;
625 reclen = (reclen + 3) & ~3;
627 d->d_fileno = isonum_733(ep->extent);
628 d->d_reclen = reclen;
629 if (isonum_711(ep->flags) & 2)
633 d->d_namlen = namelen;
635 bcopy(name, d->d_name, d->d_namlen);
636 d->d_name[d->d_namlen] = 0;
638 fp->f_off += isonum_711(ep->length);
643 cd9660_seek(struct open_file *f, off_t offset, int where)
645 struct file *fp = (struct file *)f->f_fsdata;
655 fp->f_off = fp->f_size - offset;
664 cd9660_stat(struct open_file *f, struct stat *sb)
666 struct file *fp = (struct file *)f->f_fsdata;
668 /* only important stuff */
669 sb->st_mode = S_IRUSR | S_IRGRP | S_IROTH;
670 if (fp->f_flags & F_ISDIR)
671 sb->st_mode |= S_IFDIR;
673 sb->st_mode |= S_IFREG;
674 sb->st_uid = sb->st_gid = 0;
675 sb->st_size = fp->f_size;
680 cd9660_mount(const char *dev, const char *path, void **data)
687 mnt = calloc(1, sizeof(*mnt));
692 if (asprintf(&fs, "%s%s", dev, path) < 0)
695 mnt->cd_fd = open(fs, O_RDONLY);
697 if (mnt->cd_fd == -1)
700 f = fd2open_file(mnt->cd_fd);
701 /* Is it cd9660 file system? */
702 if (strcmp(f->f_ops->fs_name, "cd9660") == 0) {
703 mnt->cd_dev = f->f_devdata;
704 errno = cd9660_read_dr(f, &mnt->cd_rec);
705 STAILQ_INSERT_TAIL(&mnt_list, mnt, cd_link);
723 cd9660_unmount(const char *dev __unused, void *data)
725 cd9660_mnt_t *mnt = data;
727 STAILQ_REMOVE(&mnt_list, mnt, cd9660_mnt, cd_link);