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>
36 * Stand-alone ISO9660 file reading package.
38 * Note: This doesn't support Rock Ridge extensions, extended attributes,
39 * blocksizes other than 2048 bytes, multi-extent files, etc.
41 #include <sys/param.h>
44 #include <sys/dirent.h>
45 #include <fs/cd9660/iso.h>
46 #include <fs/cd9660/cd9660_rrip.h>
50 #define SUSP_CONTINUATION "CE"
51 #define SUSP_PRESENT "SP"
52 #define SUSP_STOP "ST"
53 #define SUSP_EXTREF "ER"
54 #define RRIP_NAME "NM"
58 u_char signature [ISODCL ( 5, 6)];
59 u_char len_skp [ISODCL ( 7, 7)]; /* 711 */
62 static int buf_read_file(struct open_file *f, char **buf_p,
64 static int cd9660_open(const char *path, struct open_file *f);
65 static int cd9660_close(struct open_file *f);
66 static int cd9660_read(struct open_file *f, void *buf, size_t size,
68 static off_t cd9660_seek(struct open_file *f, off_t offset, int where);
69 static int cd9660_stat(struct open_file *f, struct stat *sb);
70 static int cd9660_readdir(struct open_file *f, struct dirent *d);
71 static int cd9660_mount(const char *, const char *, void **);
72 static int cd9660_unmount(const char *, void *);
73 static int dirmatch(struct open_file *f, const char *path,
74 struct iso_directory_record *dp, int use_rrip, int lenskip);
75 static int rrip_check(struct open_file *f, struct iso_directory_record *dp,
77 static char *rrip_lookup_name(struct open_file *f,
78 struct iso_directory_record *dp, int lenskip, size_t *len);
79 static ISO_SUSP_HEADER *susp_lookup_record(struct open_file *f,
80 const char *identifier, struct iso_directory_record *dp,
83 struct fs_ops cd9660_fsops = {
85 .fo_open = cd9660_open,
86 .fo_close = cd9660_close,
87 .fo_read = cd9660_read,
88 .fo_write = null_write,
89 .fo_seek = cd9660_seek,
90 .fo_stat = cd9660_stat,
91 .fo_readdir = cd9660_readdir,
92 .fo_mount = cd9660_mount,
93 .fo_unmount = cd9660_unmount
96 typedef struct cd9660_mnt {
97 struct devdesc *cd_dev;
99 struct iso_directory_record cd_rec;
100 STAILQ_ENTRY(cd9660_mnt) cd_link;
103 typedef STAILQ_HEAD(cd9660_mnt_list, cd9660_mnt) cd9660_mnt_list_t;
104 static cd9660_mnt_list_t mnt_list = STAILQ_HEAD_INITIALIZER(mnt_list);
106 #define F_ISDIR 0x0001 /* Directory */
107 #define F_ROOTDIR 0x0002 /* Root directory */
108 #define F_RR 0x0004 /* Rock Ridge on this volume */
111 int f_flags; /* file flags */
112 off_t f_off; /* Current offset within file */
113 daddr_t f_bno; /* Starting block number */
114 off_t f_size; /* Size of file */
115 daddr_t f_buf_blkno; /* block number of data block */
116 char *f_buf; /* buffer for data block */
117 int f_susp_skip; /* len_skip for SUSP records */
121 char namlen [ISODCL( 1, 1)]; /* 711 */
122 char extlen [ISODCL( 2, 2)]; /* 711 */
123 char block [ISODCL( 3, 6)]; /* 732 */
124 char parent [ISODCL( 7, 8)]; /* 722 */
128 #define PTSIZE(pp) roundup(PTFIXSZ + isonum_711((pp)->namlen), 2)
130 #define cdb2devb(bno) ((bno) * ISO_DEFAULT_BLOCK_SIZE / DEV_BSIZE)
132 static ISO_SUSP_HEADER *
133 susp_lookup_record(struct open_file *f, const char *identifier,
134 struct iso_directory_record *dp, int lenskip)
136 static char susp_buffer[ISO_DEFAULT_BLOCK_SIZE];
143 p = dp->name + isonum_711(dp->name_len) + lenskip;
144 /* Names of even length have a padding byte after the name. */
145 if ((isonum_711(dp->name_len) & 1) == 0)
147 end = (char *)dp + isonum_711(dp->length);
148 while (p + 3 < end) {
149 sh = (ISO_SUSP_HEADER *)p;
150 if (bcmp(sh->type, identifier, 2) == 0)
152 if (bcmp(sh->type, SUSP_STOP, 2) == 0)
154 if (bcmp(sh->type, SUSP_CONTINUATION, 2) == 0) {
155 shc = (ISO_RRIP_CONT *)sh;
156 error = f->f_dev->dv_strategy(f->f_devdata, F_READ,
157 cdb2devb(isonum_733(shc->location)),
158 ISO_DEFAULT_BLOCK_SIZE, susp_buffer, &read);
160 /* Bail if it fails. */
161 if (error != 0 || read != ISO_DEFAULT_BLOCK_SIZE)
163 p = susp_buffer + isonum_733(shc->offset);
164 end = p + isonum_733(shc->length);
166 /* Ignore this record and skip to the next. */
167 p += isonum_711(sh->length);
169 /* Avoid infinite loops with corrupted file systems */
170 if (isonum_711(sh->length) == 0)
178 rrip_lookup_name(struct open_file *f, struct iso_directory_record *dp,
179 int lenskip, size_t *len)
186 p = (ISO_RRIP_ALTNAME *)susp_lookup_record(f, RRIP_NAME, dp, lenskip);
190 case ISO_SUSP_CFLAG_CURRENT:
193 case ISO_SUSP_CFLAG_PARENT:
197 *len = isonum_711(p->h.length) - 5;
198 return ((char *)p + 5);
201 * We don't handle hostnames or continued names as they are
202 * too hard, so just bail and use the default name.
209 rrip_check(struct open_file *f, struct iso_directory_record *dp, int *lenskip)
211 ISO_SUSP_PRESENT *sp;
215 /* First, see if we can find a SP field. */
216 p = dp->name + isonum_711(dp->name_len);
217 if (p > (char *)dp + isonum_711(dp->length))
219 sp = (ISO_SUSP_PRESENT *)p;
220 if (bcmp(sp->h.type, SUSP_PRESENT, 2) != 0)
222 if (isonum_711(sp->h.length) != sizeof(ISO_SUSP_PRESENT))
224 if (sp->signature[0] != 0xbe || sp->signature[1] != 0xef)
226 *lenskip = isonum_711(sp->len_skp);
229 * Now look for an ER field. If RRIP is present, then there must
230 * be at least one of these. It would be more pedantic to walk
231 * through the list of fields looking for a Rock Ridge ER field.
233 er = (ISO_RRIP_EXTREF *)susp_lookup_record(f, SUSP_EXTREF, dp, 0);
240 dirmatch(struct open_file *f, const char *path, struct iso_directory_record *dp,
241 int use_rrip, int lenskip)
248 cp = rrip_lookup_name(f, dp, lenskip, &len);
252 len = isonum_711(dp->name_len);
258 sep = strchr(path, '/');
268 for (i = len; --i >= 0; path++, cp++) {
269 if (!*path || *path == '/')
273 if (!icase && toupper(*path) == *cp)
277 if (*path && *path != '/')
280 * Allow stripping of trailing dots and the version number.
281 * Note that this will find the first instead of the last version
284 if (i >= 0 && (*cp == ';' || *cp == '.')) {
285 /* This is to prevent matching of numeric extensions */
286 if (*cp == '.' && cp[1] != ';')
289 if (*++cp != ';' && (*cp < '0' || *cp > '9'))
296 cd9660_read_dr(struct open_file *f, struct iso_directory_record *rec)
298 struct iso_primary_descriptor *vd;
304 vd = malloc(MAX(ISO_DEFAULT_BLOCK_SIZE,
305 sizeof(struct iso_primary_descriptor)));
309 for (bno = 16;; bno++) {
311 rc = f->f_dev->dv_strategy(f->f_devdata, F_READ, cdb2devb(bno),
312 ISO_DEFAULT_BLOCK_SIZE, (char *)vd, &read);
315 if (read != ISO_DEFAULT_BLOCK_SIZE) {
320 if (bcmp(vd->id, ISO_STANDARD_ID, sizeof(vd->id)) != 0)
322 if (isonum_711(vd->type) == ISO_VD_END)
324 if (isonum_711(vd->type) == ISO_VD_PRIMARY)
327 if (isonum_723(vd->logical_block_size) == ISO_DEFAULT_BLOCK_SIZE) {
328 bcopy(vd->root_directory_record, rec, sizeof(*rec));
337 cd9660_open(const char *path, struct open_file *f)
339 struct file *fp = NULL;
341 size_t read, dsize, off;
343 struct iso_directory_record rec;
344 struct iso_directory_record *dp = NULL;
345 int rc, first, use_rrip, lenskip;
350 /* First find the volume descriptor */
352 buf = malloc(MAX(ISO_DEFAULT_BLOCK_SIZE,
353 sizeof(struct iso_primary_descriptor)));
358 STAILQ_FOREACH(mnt, &mnt_list, cd_link) {
359 if (dev->d_dev->dv_type == mnt->cd_dev->d_dev->dv_type &&
360 dev->d_unit == mnt->cd_dev->d_unit)
366 rc = cd9660_read_dr(f, &rec);
374 path++; /* eat leading '/' */
380 bno = isonum_733(rec.extent) + isonum_711(rec.ext_attr_length);
381 dsize = isonum_733(rec.size);
385 while (off < dsize) {
386 if ((off % ISO_DEFAULT_BLOCK_SIZE) == 0) {
388 rc = f->f_dev->dv_strategy
389 (f->f_devdata, F_READ,
390 cdb2devb(bno + boff),
391 ISO_DEFAULT_BLOCK_SIZE,
395 if (read != ISO_DEFAULT_BLOCK_SIZE) {
400 dp = (struct iso_directory_record *) buf;
402 if (isonum_711(dp->length) == 0) {
403 /* skip to next block, if any */
404 off = boff * ISO_DEFAULT_BLOCK_SIZE;
408 /* See if RRIP is in use. */
410 use_rrip = rrip_check(f, dp, &lenskip);
412 if (dirmatch(f, path, dp, use_rrip,
413 first ? 0 : lenskip)) {
419 dp = (struct iso_directory_record *)
420 ((char *) dp + isonum_711(dp->length));
421 /* If the new block has zero length, it is padding. */
422 if (isonum_711(dp->length) == 0) {
423 /* Skip to next block, if any. */
424 off = boff * ISO_DEFAULT_BLOCK_SIZE;
427 off += isonum_711(dp->length);
435 while (*path && *path != '/') /* look for next component */
438 if (*path) /* this component was directory */
442 path++; /* skip '/' */
444 if (*path) /* We do have next component. */
449 * if the path had trailing / but the path does point to file,
450 * report the error ENOTDIR.
452 if (isdir == true && (isonum_711(rec.flags) & 2) == 0) {
457 /* allocate file system specific data structure */
458 fp = malloc(sizeof(struct file));
459 bzero(fp, sizeof(struct file));
460 f->f_fsdata = (void *)fp;
462 if ((isonum_711(rec.flags) & 2) != 0) {
463 fp->f_flags = F_ISDIR;
466 fp->f_flags |= F_ROOTDIR;
468 /* Check for Rock Ridge since we didn't in the loop above. */
469 bno = isonum_733(rec.extent) + isonum_711(rec.ext_attr_length);
471 rc = f->f_dev->dv_strategy(f->f_devdata, F_READ, cdb2devb(bno),
472 ISO_DEFAULT_BLOCK_SIZE, buf, &read);
475 if (read != ISO_DEFAULT_BLOCK_SIZE) {
479 dp = (struct iso_directory_record *)buf;
480 use_rrip = rrip_check(f, dp, &lenskip);
484 fp->f_susp_skip = lenskip;
487 fp->f_bno = isonum_733(rec.extent) + isonum_711(rec.ext_attr_length);
488 fp->f_size = isonum_733(rec.size);
501 cd9660_close(struct open_file *f)
503 struct file *fp = (struct file *)f->f_fsdata;
512 buf_read_file(struct open_file *f, char **buf_p, size_t *size_p)
514 struct file *fp = (struct file *)f->f_fsdata;
515 daddr_t blkno, blkoff;
519 blkno = fp->f_off / ISO_DEFAULT_BLOCK_SIZE + fp->f_bno;
520 blkoff = fp->f_off % ISO_DEFAULT_BLOCK_SIZE;
522 if (blkno != fp->f_buf_blkno) {
523 if (fp->f_buf == (char *)0)
524 fp->f_buf = malloc(ISO_DEFAULT_BLOCK_SIZE);
527 rc = f->f_dev->dv_strategy(f->f_devdata, F_READ,
528 cdb2devb(blkno), ISO_DEFAULT_BLOCK_SIZE,
532 if (read != ISO_DEFAULT_BLOCK_SIZE)
535 fp->f_buf_blkno = blkno;
538 *buf_p = fp->f_buf + blkoff;
539 *size_p = ISO_DEFAULT_BLOCK_SIZE - blkoff;
541 if (*size_p > fp->f_size - fp->f_off)
542 *size_p = fp->f_size - fp->f_off;
547 cd9660_read(struct open_file *f, void *start, size_t size, size_t *resid)
549 struct file *fp = (struct file *)f->f_fsdata;
551 size_t buf_size, csize;
556 if (fp->f_off < 0 || fp->f_off >= fp->f_size)
559 rc = buf_read_file(f, &buf, &buf_size);
563 csize = size > buf_size ? buf_size : size;
564 bcopy(buf, addr, csize);
576 cd9660_readdir(struct open_file *f, struct dirent *d)
578 struct file *fp = (struct file *)f->f_fsdata;
579 struct iso_directory_record *ep;
580 size_t buf_size, reclen, namelen;
586 if (fp->f_off >= fp->f_size)
588 error = buf_read_file(f, &buf, &buf_size);
591 ep = (struct iso_directory_record *)buf;
593 if (isonum_711(ep->length) == 0) {
596 /* skip to next block, if any */
597 blkno = fp->f_off / ISO_DEFAULT_BLOCK_SIZE;
598 fp->f_off = (blkno + 1) * ISO_DEFAULT_BLOCK_SIZE;
602 if (fp->f_flags & F_RR) {
603 if (fp->f_flags & F_ROOTDIR && fp->f_off == 0)
606 lenskip = fp->f_susp_skip;
607 name = rrip_lookup_name(f, ep, lenskip, &namelen);
611 namelen = isonum_711(ep->name_len);
614 if (ep->name[0] == 0)
616 else if (ep->name[0] == 1) {
622 reclen = sizeof(struct dirent) - (MAXNAMLEN+1) + namelen + 1;
623 reclen = (reclen + 3) & ~3;
625 d->d_fileno = isonum_733(ep->extent);
626 d->d_reclen = reclen;
627 if (isonum_711(ep->flags) & 2)
631 d->d_namlen = namelen;
633 bcopy(name, d->d_name, d->d_namlen);
634 d->d_name[d->d_namlen] = 0;
636 fp->f_off += isonum_711(ep->length);
641 cd9660_seek(struct open_file *f, off_t offset, int where)
643 struct file *fp = (struct file *)f->f_fsdata;
653 fp->f_off = fp->f_size - offset;
662 cd9660_stat(struct open_file *f, struct stat *sb)
664 struct file *fp = (struct file *)f->f_fsdata;
666 /* only important stuff */
667 sb->st_mode = S_IRUSR | S_IRGRP | S_IROTH;
668 if (fp->f_flags & F_ISDIR)
669 sb->st_mode |= S_IFDIR;
671 sb->st_mode |= S_IFREG;
672 sb->st_uid = sb->st_gid = 0;
673 sb->st_size = fp->f_size;
678 cd9660_mount(const char *dev, const char *path, void **data)
685 mnt = calloc(1, sizeof(*mnt));
690 if (asprintf(&fs, "%s%s", dev, path) < 0)
693 mnt->cd_fd = open(fs, O_RDONLY);
695 if (mnt->cd_fd == -1)
698 f = fd2open_file(mnt->cd_fd);
699 /* Is it cd9660 file system? */
700 if (strcmp(f->f_ops->fs_name, "cd9660") == 0) {
701 mnt->cd_dev = f->f_devdata;
702 errno = cd9660_read_dr(f, &mnt->cd_rec);
703 STAILQ_INSERT_TAIL(&mnt_list, mnt, cd_link);
721 cd9660_unmount(const char *dev __unused, void *data)
723 cd9660_mnt_t *mnt = data;
725 STAILQ_REMOVE(&mnt_list, mnt, cd9660_mnt, cd_link);