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.
35 * Stand-alone ISO9660 file reading package.
37 * Note: This doesn't support Rock Ridge extensions, extended attributes,
38 * blocksizes other than 2048 bytes, multi-extent files, etc.
40 #include <sys/param.h>
43 #include <sys/dirent.h>
44 #include <fs/cd9660/iso.h>
45 #include <fs/cd9660/cd9660_rrip.h>
49 #define SUSP_CONTINUATION "CE"
50 #define SUSP_PRESENT "SP"
51 #define SUSP_STOP "ST"
52 #define SUSP_EXTREF "ER"
53 #define RRIP_NAME "NM"
57 u_char signature [ISODCL ( 5, 6)];
58 u_char len_skp [ISODCL ( 7, 7)]; /* 711 */
61 static int buf_read_file(struct open_file *f, char **buf_p,
63 static int cd9660_open(const char *path, struct open_file *f);
64 static int cd9660_close(struct open_file *f);
65 static int cd9660_read(struct open_file *f, void *buf, size_t size,
67 static off_t cd9660_seek(struct open_file *f, off_t offset, int where);
68 static int cd9660_stat(struct open_file *f, struct stat *sb);
69 static int cd9660_readdir(struct open_file *f, struct dirent *d);
70 static int cd9660_mount(const char *, const char *, void **);
71 static int cd9660_unmount(const char *, void *);
72 static int dirmatch(struct open_file *f, const char *path,
73 struct iso_directory_record *dp, int use_rrip, int lenskip);
74 static int rrip_check(struct open_file *f, struct iso_directory_record *dp,
76 static char *rrip_lookup_name(struct open_file *f,
77 struct iso_directory_record *dp, int lenskip, size_t *len);
78 static ISO_SUSP_HEADER *susp_lookup_record(struct open_file *f,
79 const char *identifier, struct iso_directory_record *dp,
82 struct fs_ops cd9660_fsops = {
84 .fo_open = cd9660_open,
85 .fo_close = cd9660_close,
86 .fo_read = cd9660_read,
87 .fo_write = null_write,
88 .fo_seek = cd9660_seek,
89 .fo_stat = cd9660_stat,
90 .fo_readdir = cd9660_readdir,
91 .fo_mount = cd9660_mount,
92 .fo_unmount = cd9660_unmount
95 typedef struct cd9660_mnt {
96 struct devdesc *cd_dev;
98 struct iso_directory_record cd_rec;
99 STAILQ_ENTRY(cd9660_mnt) cd_link;
102 typedef STAILQ_HEAD(cd9660_mnt_list, cd9660_mnt) cd9660_mnt_list_t;
103 static cd9660_mnt_list_t mnt_list = STAILQ_HEAD_INITIALIZER(mnt_list);
105 #define F_ISDIR 0x0001 /* Directory */
106 #define F_ROOTDIR 0x0002 /* Root directory */
107 #define F_RR 0x0004 /* Rock Ridge on this volume */
110 int f_flags; /* file flags */
111 off_t f_off; /* Current offset within file */
112 daddr_t f_bno; /* Starting block number */
113 off_t f_size; /* Size of file */
114 daddr_t f_buf_blkno; /* block number of data block */
115 char *f_buf; /* buffer for data block */
116 int f_susp_skip; /* len_skip for SUSP records */
120 char namlen [ISODCL( 1, 1)]; /* 711 */
121 char extlen [ISODCL( 2, 2)]; /* 711 */
122 char block [ISODCL( 3, 6)]; /* 732 */
123 char parent [ISODCL( 7, 8)]; /* 722 */
127 #define PTSIZE(pp) roundup(PTFIXSZ + isonum_711((pp)->namlen), 2)
129 #define cdb2devb(bno) ((bno) * ISO_DEFAULT_BLOCK_SIZE / DEV_BSIZE)
131 static ISO_SUSP_HEADER *
132 susp_lookup_record(struct open_file *f, const char *identifier,
133 struct iso_directory_record *dp, int lenskip)
135 static char susp_buffer[ISO_DEFAULT_BLOCK_SIZE];
142 p = dp->name + isonum_711(dp->name_len) + lenskip;
143 /* Names of even length have a padding byte after the name. */
144 if ((isonum_711(dp->name_len) & 1) == 0)
146 end = (char *)dp + isonum_711(dp->length);
147 while (p + 3 < end) {
148 sh = (ISO_SUSP_HEADER *)p;
149 if (bcmp(sh->type, identifier, 2) == 0)
151 if (bcmp(sh->type, SUSP_STOP, 2) == 0)
153 if (bcmp(sh->type, SUSP_CONTINUATION, 2) == 0) {
154 shc = (ISO_RRIP_CONT *)sh;
155 error = f->f_dev->dv_strategy(f->f_devdata, F_READ,
156 cdb2devb(isonum_733(shc->location)),
157 ISO_DEFAULT_BLOCK_SIZE, susp_buffer, &read);
159 /* Bail if it fails. */
160 if (error != 0 || read != ISO_DEFAULT_BLOCK_SIZE)
162 p = susp_buffer + isonum_733(shc->offset);
163 end = p + isonum_733(shc->length);
165 /* Ignore this record and skip to the next. */
166 p += isonum_711(sh->length);
168 /* Avoid infinite loops with corrupted file systems */
169 if (isonum_711(sh->length) == 0)
177 rrip_lookup_name(struct open_file *f, struct iso_directory_record *dp,
178 int lenskip, size_t *len)
185 p = (ISO_RRIP_ALTNAME *)susp_lookup_record(f, RRIP_NAME, dp, lenskip);
189 case ISO_SUSP_CFLAG_CURRENT:
192 case ISO_SUSP_CFLAG_PARENT:
196 *len = isonum_711(p->h.length) - 5;
197 return ((char *)p + 5);
200 * We don't handle hostnames or continued names as they are
201 * too hard, so just bail and use the default name.
208 rrip_check(struct open_file *f, struct iso_directory_record *dp, int *lenskip)
210 ISO_SUSP_PRESENT *sp;
214 /* First, see if we can find a SP field. */
215 p = dp->name + isonum_711(dp->name_len);
216 if (p > (char *)dp + isonum_711(dp->length))
218 sp = (ISO_SUSP_PRESENT *)p;
219 if (bcmp(sp->h.type, SUSP_PRESENT, 2) != 0)
221 if (isonum_711(sp->h.length) != sizeof(ISO_SUSP_PRESENT))
223 if (sp->signature[0] != 0xbe || sp->signature[1] != 0xef)
225 *lenskip = isonum_711(sp->len_skp);
228 * Now look for an ER field. If RRIP is present, then there must
229 * be at least one of these. It would be more pedantic to walk
230 * through the list of fields looking for a Rock Ridge ER field.
232 er = (ISO_RRIP_EXTREF *)susp_lookup_record(f, SUSP_EXTREF, dp, 0);
239 dirmatch(struct open_file *f, const char *path, struct iso_directory_record *dp,
240 int use_rrip, int lenskip)
247 cp = rrip_lookup_name(f, dp, lenskip, &len);
251 len = isonum_711(dp->name_len);
257 sep = strchr(path, '/');
267 for (i = len; --i >= 0; path++, cp++) {
268 if (!*path || *path == '/')
272 if (!icase && toupper(*path) == *cp)
276 if (*path && *path != '/')
279 * Allow stripping of trailing dots and the version number.
280 * Note that this will find the first instead of the last version
283 if (i >= 0 && (*cp == ';' || *cp == '.')) {
284 /* This is to prevent matching of numeric extensions */
285 if (*cp == '.' && cp[1] != ';')
288 if (*++cp != ';' && (*cp < '0' || *cp > '9'))
295 cd9660_read_dr(struct open_file *f, struct iso_directory_record *rec)
297 struct iso_primary_descriptor *vd;
303 vd = malloc(MAX(ISO_DEFAULT_BLOCK_SIZE,
304 sizeof(struct iso_primary_descriptor)));
308 for (bno = 16;; bno++) {
310 rc = f->f_dev->dv_strategy(f->f_devdata, F_READ, cdb2devb(bno),
311 ISO_DEFAULT_BLOCK_SIZE, (char *)vd, &read);
314 if (read != ISO_DEFAULT_BLOCK_SIZE) {
319 if (bcmp(vd->id, ISO_STANDARD_ID, sizeof(vd->id)) != 0)
321 if (isonum_711(vd->type) == ISO_VD_END)
323 if (isonum_711(vd->type) == ISO_VD_PRIMARY)
326 if (isonum_723(vd->logical_block_size) == ISO_DEFAULT_BLOCK_SIZE) {
327 bcopy(vd->root_directory_record, rec, sizeof(*rec));
336 cd9660_open(const char *path, struct open_file *f)
338 struct file *fp = NULL;
340 size_t read, dsize, off;
342 struct iso_directory_record rec;
343 struct iso_directory_record *dp = NULL;
344 int rc, first, use_rrip, lenskip;
349 /* First find the volume descriptor */
351 buf = malloc(MAX(ISO_DEFAULT_BLOCK_SIZE,
352 sizeof(struct iso_primary_descriptor)));
357 STAILQ_FOREACH(mnt, &mnt_list, cd_link) {
358 if (dev->d_dev->dv_type == mnt->cd_dev->d_dev->dv_type &&
359 dev->d_unit == mnt->cd_dev->d_unit)
365 rc = cd9660_read_dr(f, &rec);
373 path++; /* eat leading '/' */
379 bno = isonum_733(rec.extent) + isonum_711(rec.ext_attr_length);
380 dsize = isonum_733(rec.size);
384 while (off < dsize) {
385 if ((off % ISO_DEFAULT_BLOCK_SIZE) == 0) {
387 rc = f->f_dev->dv_strategy
388 (f->f_devdata, F_READ,
389 cdb2devb(bno + boff),
390 ISO_DEFAULT_BLOCK_SIZE,
394 if (read != ISO_DEFAULT_BLOCK_SIZE) {
399 dp = (struct iso_directory_record *) buf;
401 if (isonum_711(dp->length) == 0) {
402 /* skip to next block, if any */
403 off = boff * ISO_DEFAULT_BLOCK_SIZE;
407 /* See if RRIP is in use. */
409 use_rrip = rrip_check(f, dp, &lenskip);
411 if (dirmatch(f, path, dp, use_rrip,
412 first ? 0 : lenskip)) {
418 dp = (struct iso_directory_record *)
419 ((char *) dp + isonum_711(dp->length));
420 /* If the new block has zero length, it is padding. */
421 if (isonum_711(dp->length) == 0) {
422 /* Skip to next block, if any. */
423 off = boff * ISO_DEFAULT_BLOCK_SIZE;
426 off += isonum_711(dp->length);
434 while (*path && *path != '/') /* look for next component */
437 if (*path) /* this component was directory */
441 path++; /* skip '/' */
443 if (*path) /* We do have next component. */
448 * if the path had trailing / but the path does point to file,
449 * report the error ENOTDIR.
451 if (isdir == true && (isonum_711(rec.flags) & 2) == 0) {
456 /* allocate file system specific data structure */
457 fp = malloc(sizeof(struct file));
458 bzero(fp, sizeof(struct file));
459 f->f_fsdata = (void *)fp;
461 if ((isonum_711(rec.flags) & 2) != 0) {
462 fp->f_flags = F_ISDIR;
465 fp->f_flags |= F_ROOTDIR;
467 /* Check for Rock Ridge since we didn't in the loop above. */
468 bno = isonum_733(rec.extent) + isonum_711(rec.ext_attr_length);
470 rc = f->f_dev->dv_strategy(f->f_devdata, F_READ, cdb2devb(bno),
471 ISO_DEFAULT_BLOCK_SIZE, buf, &read);
474 if (read != ISO_DEFAULT_BLOCK_SIZE) {
478 dp = (struct iso_directory_record *)buf;
479 use_rrip = rrip_check(f, dp, &lenskip);
483 fp->f_susp_skip = lenskip;
486 fp->f_bno = isonum_733(rec.extent) + isonum_711(rec.ext_attr_length);
487 fp->f_size = isonum_733(rec.size);
500 cd9660_close(struct open_file *f)
502 struct file *fp = (struct file *)f->f_fsdata;
511 buf_read_file(struct open_file *f, char **buf_p, size_t *size_p)
513 struct file *fp = (struct file *)f->f_fsdata;
514 daddr_t blkno, blkoff;
518 blkno = fp->f_off / ISO_DEFAULT_BLOCK_SIZE + fp->f_bno;
519 blkoff = fp->f_off % ISO_DEFAULT_BLOCK_SIZE;
521 if (blkno != fp->f_buf_blkno) {
522 if (fp->f_buf == (char *)0)
523 fp->f_buf = malloc(ISO_DEFAULT_BLOCK_SIZE);
526 rc = f->f_dev->dv_strategy(f->f_devdata, F_READ,
527 cdb2devb(blkno), ISO_DEFAULT_BLOCK_SIZE,
531 if (read != ISO_DEFAULT_BLOCK_SIZE)
534 fp->f_buf_blkno = blkno;
537 *buf_p = fp->f_buf + blkoff;
538 *size_p = ISO_DEFAULT_BLOCK_SIZE - blkoff;
540 if (*size_p > fp->f_size - fp->f_off)
541 *size_p = fp->f_size - fp->f_off;
546 cd9660_read(struct open_file *f, void *start, size_t size, size_t *resid)
548 struct file *fp = (struct file *)f->f_fsdata;
550 size_t buf_size, csize;
555 if (fp->f_off < 0 || fp->f_off >= fp->f_size)
558 rc = buf_read_file(f, &buf, &buf_size);
562 csize = size > buf_size ? buf_size : size;
563 bcopy(buf, addr, csize);
575 cd9660_readdir(struct open_file *f, struct dirent *d)
577 struct file *fp = (struct file *)f->f_fsdata;
578 struct iso_directory_record *ep;
579 size_t buf_size, reclen, namelen;
585 if (fp->f_off >= fp->f_size)
587 error = buf_read_file(f, &buf, &buf_size);
590 ep = (struct iso_directory_record *)buf;
592 if (isonum_711(ep->length) == 0) {
595 /* skip to next block, if any */
596 blkno = fp->f_off / ISO_DEFAULT_BLOCK_SIZE;
597 fp->f_off = (blkno + 1) * ISO_DEFAULT_BLOCK_SIZE;
601 if (fp->f_flags & F_RR) {
602 if (fp->f_flags & F_ROOTDIR && fp->f_off == 0)
605 lenskip = fp->f_susp_skip;
606 name = rrip_lookup_name(f, ep, lenskip, &namelen);
610 namelen = isonum_711(ep->name_len);
613 if (ep->name[0] == 0)
615 else if (ep->name[0] == 1) {
621 reclen = sizeof(struct dirent) - (MAXNAMLEN+1) + namelen + 1;
622 reclen = (reclen + 3) & ~3;
624 d->d_fileno = isonum_733(ep->extent);
625 d->d_reclen = reclen;
626 if (isonum_711(ep->flags) & 2)
630 d->d_namlen = namelen;
632 bcopy(name, d->d_name, d->d_namlen);
633 d->d_name[d->d_namlen] = 0;
635 fp->f_off += isonum_711(ep->length);
640 cd9660_seek(struct open_file *f, off_t offset, int where)
642 struct file *fp = (struct file *)f->f_fsdata;
652 fp->f_off = fp->f_size - offset;
661 cd9660_stat(struct open_file *f, struct stat *sb)
663 struct file *fp = (struct file *)f->f_fsdata;
665 /* only important stuff */
666 sb->st_mode = S_IRUSR | S_IRGRP | S_IROTH;
667 if (fp->f_flags & F_ISDIR)
668 sb->st_mode |= S_IFDIR;
670 sb->st_mode |= S_IFREG;
671 sb->st_uid = sb->st_gid = 0;
672 sb->st_size = fp->f_size;
677 cd9660_mount(const char *dev, const char *path, void **data)
684 mnt = calloc(1, sizeof(*mnt));
689 if (asprintf(&fs, "%s%s", dev, path) < 0)
692 mnt->cd_fd = open(fs, O_RDONLY);
694 if (mnt->cd_fd == -1)
697 f = fd2open_file(mnt->cd_fd);
698 /* Is it cd9660 file system? */
699 if (strcmp(f->f_ops->fs_name, "cd9660") == 0) {
700 mnt->cd_dev = f->f_devdata;
701 errno = cd9660_read_dr(f, &mnt->cd_rec);
702 STAILQ_INSERT_TAIL(&mnt_list, mnt, cd_link);
720 cd9660_unmount(const char *dev __unused, void *data)
722 cd9660_mnt_t *mnt = data;
724 STAILQ_REMOVE(&mnt_list, mnt, cd9660_mnt, cd_link);