Boot loader fixes - fix recursive malloc()/free() errors, NULL freed fields
[dragonfly.git] / lib / libstand / cd9660.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (C) 1996 Wolfgang Solfrank.
3 * Copyright (C) 1996 TooLs GmbH.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by TooLs GmbH.
17 * 4. The name of TooLs GmbH may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38a690d7
MD
30 *
31 * $NetBSD: cd9660.c,v 1.5 1997/06/26 19:11:33 drochner Exp $
32 * $FreeBSD: src/lib/libstand/cd9660.c,v 1.4.2.4 2001/12/21 22:17:44 jhb Exp $
ff2aef30 33 * $DragonFly: src/lib/libstand/cd9660.c,v 1.6 2005/12/11 02:27:26 swildner Exp $
984263bc
MD
34 */
35
36/*
37 * Stand-alone ISO9660 file reading package.
38 *
39 * Note: This doesn't support Rock Ridge extensions, extended attributes,
40 * blocksizes other than 2048 bytes, multi-extent files, etc.
41 */
42#include <sys/param.h>
43#include <string.h>
44#include <sys/dirent.h>
45#include <isofs/cd9660/iso.h>
46#include <isofs/cd9660/cd9660_rrip.h>
47
48#include "stand.h"
49
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"
55
56typedef struct {
57 ISO_SUSP_HEADER h;
58 u_char signature [ISODCL ( 5, 6)];
59 u_char len_skp [ISODCL ( 7, 7)]; /* 711 */
60} ISO_SUSP_PRESENT;
61
62static int buf_read_file(struct open_file *f, char **buf_p,
63 size_t *size_p);
64static int cd9660_open(const char *path, struct open_file *f);
65static int cd9660_close(struct open_file *f);
66static int cd9660_read(struct open_file *f, void *buf, size_t size,
67 size_t *resid);
68static int cd9660_write(struct open_file *f, void *buf, size_t size,
69 size_t *resid);
70static off_t cd9660_seek(struct open_file *f, off_t offset, int where);
71static int cd9660_stat(struct open_file *f, struct stat *sb);
72static int cd9660_readdir(struct open_file *f, struct dirent *d);
73static int dirmatch(struct open_file *f, const char *path,
74 struct iso_directory_record *dp, int use_rrip, int lenskip);
75static int rrip_check(struct open_file *f, struct iso_directory_record *dp,
76 int *lenskip);
77static char *rrip_lookup_name(struct open_file *f,
78 struct iso_directory_record *dp, int lenskip, size_t *len);
79static ISO_SUSP_HEADER *susp_lookup_record(struct open_file *f,
80 const char *identifier, struct iso_directory_record *dp,
81 int lenskip);
82
83struct fs_ops cd9660_fsops = {
84 "cd9660",
85 cd9660_open,
86 cd9660_close,
87 cd9660_read,
88 cd9660_write,
89 cd9660_seek,
90 cd9660_stat,
91 cd9660_readdir
92};
93
94#define F_ISDIR 0x0001 /* Directory */
95#define F_ROOTDIR 0x0002 /* Root directory */
96#define F_RR 0x0004 /* Rock Ridge on this volume */
97
98struct file {
99 int f_flags; /* file flags */
100 off_t f_off; /* Current offset within file */
101 daddr_t f_bno; /* Starting block number */
102 off_t f_size; /* Size of file */
103 daddr_t f_buf_blkno; /* block number of data block */
104 char *f_buf; /* buffer for data block */
105 int f_susp_skip; /* len_skip for SUSP records */
106};
107
108struct ptable_ent {
109 char namlen [ISODCL( 1, 1)]; /* 711 */
110 char extlen [ISODCL( 2, 2)]; /* 711 */
111 char block [ISODCL( 3, 6)]; /* 732 */
112 char parent [ISODCL( 7, 8)]; /* 722 */
113 char name [1];
114};
115#define PTFIXSZ 8
116#define PTSIZE(pp) roundup(PTFIXSZ + isonum_711((pp)->namlen), 2)
117
118#define cdb2devb(bno) ((bno) * ISO_DEFAULT_BLOCK_SIZE / DEV_BSIZE)
119
120/* XXX these should be in the system headers */
121static __inline int
ff2aef30 122isonum_722(u_char *p)
984263bc
MD
123{
124 return (*p << 8)|p[1];
125}
126
127static __inline int
ff2aef30 128isonum_732(u_char *p)
984263bc
MD
129{
130 return (*p << 24)|(p[1] << 16)|(p[2] << 8)|p[3];
131}
132
133static ISO_SUSP_HEADER *
134susp_lookup_record(struct open_file *f, const char *identifier,
135 struct iso_directory_record *dp, int lenskip)
136{
137 static char susp_buffer[ISO_DEFAULT_BLOCK_SIZE];
138 ISO_SUSP_HEADER *sh;
139 ISO_RRIP_CONT *shc;
140 char *p, *end;
141 int error;
142 size_t read;
143
144 p = dp->name + isonum_711(dp->name_len) + lenskip;
145 /* Names of even length have a padding byte after the name. */
146 if ((isonum_711(dp->name_len) & 1) == 0)
147 p++;
148 end = (char *)dp + isonum_711(dp->length);
149 while (p + 3 < end) {
150 sh = (ISO_SUSP_HEADER *)p;
151 if (bcmp(sh->type, identifier, 2) == 0)
152 return (sh);
153 if (bcmp(sh->type, SUSP_STOP, 2) == 0)
154 return (NULL);
155 if (bcmp(sh->type, SUSP_CONTINUATION, 2) == 0) {
156 shc = (ISO_RRIP_CONT *)sh;
157 error = f->f_dev->dv_strategy(f->f_devdata, F_READ,
158 cdb2devb(isonum_733(shc->location)),
159 ISO_DEFAULT_BLOCK_SIZE, susp_buffer, &read);
160
161 /* Bail if it fails. */
162 if (error != 0 || read != ISO_DEFAULT_BLOCK_SIZE)
163 return (NULL);
164 p = susp_buffer + isonum_733(shc->offset);
165 end = p + isonum_733(shc->length);
166 } else
167 /* Ignore this record and skip to the next. */
168 p += isonum_711(sh->length);
169 }
170 return (NULL);
171}
172
173static char *
174rrip_lookup_name(struct open_file *f, struct iso_directory_record *dp,
175 int lenskip, size_t *len)
176{
177 ISO_RRIP_ALTNAME *p;
178
179 if (len == NULL)
180 return (NULL);
181
182 p = (ISO_RRIP_ALTNAME *)susp_lookup_record(f, RRIP_NAME, dp, lenskip);
183 if (p == NULL)
184 return (NULL);
185 switch (*p->flags) {
186 case ISO_SUSP_CFLAG_CURRENT:
187 *len = 1;
188 return (".");
189 case ISO_SUSP_CFLAG_PARENT:
190 *len = 2;
191 return ("..");
192 case 0:
193 *len = isonum_711(p->h.length) - 5;
194 return ((char *)p + 5);
195 default:
196 /*
197 * We don't handle hostnames or continued names as they are
198 * too hard, so just bail and use the default name.
199 */
200 return (NULL);
201 }
202}
203
204static int
205rrip_check(struct open_file *f, struct iso_directory_record *dp, int *lenskip)
206{
207 ISO_SUSP_PRESENT *sp;
208 ISO_RRIP_EXTREF *er;
209 char *p;
210
211 /* First, see if we can find a SP field. */
212 p = dp->name + isonum_711(dp->name_len);
213 if (p > (char *)dp + isonum_711(dp->length))
214 return (0);
215 sp = (ISO_SUSP_PRESENT *)p;
216 if (bcmp(sp->h.type, SUSP_PRESENT, 2) != 0)
217 return (0);
218 if (isonum_711(sp->h.length) != sizeof(ISO_SUSP_PRESENT))
219 return (0);
220 if (sp->signature[0] != 0xbe || sp->signature[1] != 0xef)
221 return (0);
222 *lenskip = isonum_711(sp->len_skp);
223
224 /*
225 * Now look for an ER field. If RRIP is present, then there must
226 * be at least one of these. It would be more pedantic to walk
227 * through the list of fields looking for a Rock Ridge ER field.
228 */
229 er = (ISO_RRIP_EXTREF *)susp_lookup_record(f, SUSP_EXTREF, dp, 0);
230 if (er == NULL)
231 return (0);
232 return (1);
233}
234
235static int
236dirmatch(struct open_file *f, const char *path, struct iso_directory_record *dp,
237 int use_rrip, int lenskip)
238{
239 size_t len;
240 char *cp;
241 int i, icase;
242
243 if (use_rrip)
244 cp = rrip_lookup_name(f, dp, lenskip, &len);
245 else
246 cp = NULL;
247 if (cp == NULL) {
248 len = isonum_711(dp->name_len);
249 cp = dp->name;
250 icase = 1;
251 } else
252 icase = 0;
253 for (i = len; --i >= 0; path++, cp++) {
254 if (!*path || *path == '/')
255 break;
256 if (*path == *cp)
257 continue;
258 if (!icase && toupper(*path) == *cp)
259 continue;
260 return 0;
261 }
262 if (*path && *path != '/')
263 return 0;
264 /*
265 * Allow stripping of trailing dots and the version number.
266 * Note that this will find the first instead of the last version
267 * of a file.
268 */
269 if (i >= 0 && (*cp == ';' || *cp == '.')) {
270 /* This is to prevent matching of numeric extensions */
271 if (*cp == '.' && cp[1] != ';')
272 return 0;
273 while (--i >= 0)
274 if (*++cp != ';' && (*cp < '0' || *cp > '9'))
275 return 0;
276 }
277 return 1;
278}
279
280static int
281cd9660_open(const char *path, struct open_file *f)
282{
283 struct file *fp = 0;
284 void *buf;
285 struct iso_primary_descriptor *vd;
286 size_t buf_size, read, dsize, off;
287 daddr_t bno, boff;
288 struct iso_directory_record rec;
289 struct iso_directory_record *dp = 0;
290 int rc, first, use_rrip, lenskip;
291
292 /* First find the volume descriptor */
293 buf = malloc(buf_size = ISO_DEFAULT_BLOCK_SIZE);
294 vd = buf;
295 for (bno = 16;; bno++) {
296 twiddle();
297 rc = f->f_dev->dv_strategy(f->f_devdata, F_READ, cdb2devb(bno),
298 ISO_DEFAULT_BLOCK_SIZE, buf, &read);
299 if (rc)
300 goto out;
301 if (read != ISO_DEFAULT_BLOCK_SIZE) {
302 rc = EIO;
303 goto out;
304 }
305 rc = EINVAL;
306 if (bcmp(vd->id, ISO_STANDARD_ID, sizeof vd->id) != 0)
307 goto out;
308 if (isonum_711(vd->type) == ISO_VD_END)
309 goto out;
310 if (isonum_711(vd->type) == ISO_VD_PRIMARY)
311 break;
312 }
313 if (isonum_723(vd->logical_block_size) != ISO_DEFAULT_BLOCK_SIZE)
314 goto out;
315
316 rec = *(struct iso_directory_record *) vd->root_directory_record;
317 if (*path == '/') path++; /* eat leading '/' */
318
319 first = 1;
320 use_rrip = 0;
321 while (*path) {
322 bno = isonum_733(rec.extent) + isonum_711(rec.ext_attr_length);
323 dsize = isonum_733(rec.size);
324 off = 0;
325 boff = 0;
326
327 while (off < dsize) {
328 if ((off % ISO_DEFAULT_BLOCK_SIZE) == 0) {
329 twiddle();
330 rc = f->f_dev->dv_strategy
331 (f->f_devdata, F_READ,
332 cdb2devb(bno + boff),
333 ISO_DEFAULT_BLOCK_SIZE,
334 buf, &read);
335 if (rc)
336 goto out;
337 if (read != ISO_DEFAULT_BLOCK_SIZE) {
338 rc = EIO;
339 goto out;
340 }
341 boff++;
342 dp = (struct iso_directory_record *) buf;
343 }
344 if (isonum_711(dp->length) == 0) {
345 /* skip to next block, if any */
346 off = boff * ISO_DEFAULT_BLOCK_SIZE;
347 continue;
348 }
349
350 /* See if RRIP is in use. */
351 if (first)
352 use_rrip = rrip_check(f, dp, &lenskip);
353
354 if (dirmatch(f, path, dp, use_rrip,
355 first ? 0 : lenskip)) {
356 first = 0;
357 break;
358 } else
359 first = 0;
360
361 dp = (struct iso_directory_record *)
362 ((char *) dp + isonum_711(dp->length));
363 off += isonum_711(dp->length);
364 }
365 if (off >= dsize) {
366 rc = ENOENT;
367 goto out;
368 }
369
370 rec = *dp;
371 while (*path && *path != '/') /* look for next component */
372 path++;
b0e98229
MD
373 if (*path == '/') { /* skip /, make sure is dir */
374 path++;
375 if (*path && (isonum_711(dp->flags) & 2) == 0) {
376 rc = ENOENT; /* not directory */
377 goto out;
378 }
379 }
984263bc
MD
380 }
381
382 /* allocate file system specific data structure */
383 fp = malloc(sizeof(struct file));
384 bzero(fp, sizeof(struct file));
385 f->f_fsdata = (void *)fp;
386
387 if ((isonum_711(rec.flags) & 2) != 0) {
388 fp->f_flags = F_ISDIR;
389 }
390 if (first) {
391 fp->f_flags |= F_ROOTDIR;
392
393 /* Check for Rock Ridge since we didn't in the loop above. */
394 bno = isonum_733(rec.extent) + isonum_711(rec.ext_attr_length);
395 twiddle();
396 rc = f->f_dev->dv_strategy(f->f_devdata, F_READ, cdb2devb(bno),
397 ISO_DEFAULT_BLOCK_SIZE, buf, &read);
398 if (rc)
399 goto out;
400 if (read != ISO_DEFAULT_BLOCK_SIZE) {
401 rc = EIO;
402 goto out;
403 }
404 dp = (struct iso_directory_record *)buf;
405 use_rrip = rrip_check(f, dp, &lenskip);
406 }
407 if (use_rrip) {
408 fp->f_flags |= F_RR;
409 fp->f_susp_skip = lenskip;
410 }
411 fp->f_off = 0;
412 fp->f_bno = isonum_733(rec.extent) + isonum_711(rec.ext_attr_length);
413 fp->f_size = isonum_733(rec.size);
414 free(buf);
415
416 return 0;
417
418out:
6825e71f 419 f->f_fsdata = NULL;
984263bc
MD
420 if (fp)
421 free(fp);
422 free(buf);
423
424 return rc;
425}
426
427static int
428cd9660_close(struct open_file *f)
429{
430 struct file *fp = (struct file *)f->f_fsdata;
431
6825e71f
MD
432 f->f_fsdata = NULL;
433 if (fp)
434 free(fp);
984263bc
MD
435
436 return 0;
437}
438
439static int
440buf_read_file(struct open_file *f, char **buf_p, size_t *size_p)
441{
442 struct file *fp = (struct file *)f->f_fsdata;
443 daddr_t blkno, blkoff;
444 int rc = 0;
445 size_t read;
446
447 blkno = fp->f_off / ISO_DEFAULT_BLOCK_SIZE + fp->f_bno;
448 blkoff = fp->f_off % ISO_DEFAULT_BLOCK_SIZE;
449
450 if (blkno != fp->f_buf_blkno) {
451 if (fp->f_buf == (char *)0)
452 fp->f_buf = malloc(ISO_DEFAULT_BLOCK_SIZE);
453
454 twiddle();
455 rc = f->f_dev->dv_strategy(f->f_devdata, F_READ,
456 cdb2devb(blkno), ISO_DEFAULT_BLOCK_SIZE, fp->f_buf, &read);
457 if (rc)
458 return (rc);
459 if (read != ISO_DEFAULT_BLOCK_SIZE)
460 return (EIO);
461
462 fp->f_buf_blkno = blkno;
463 }
464
465 *buf_p = fp->f_buf + blkoff;
466 *size_p = ISO_DEFAULT_BLOCK_SIZE - blkoff;
467
468 if (*size_p > fp->f_size - fp->f_off)
469 *size_p = fp->f_size - fp->f_off;
470 return (rc);
471}
472
473static int
474cd9660_read(struct open_file *f, void *start, size_t size, size_t *resid)
475{
476 struct file *fp = (struct file *)f->f_fsdata;
477 char *buf, *addr;
478 size_t buf_size, csize;
479 int rc = 0;
480
481 addr = start;
482 while (size) {
483 if (fp->f_off < 0 || fp->f_off >= fp->f_size)
484 break;
485
486 rc = buf_read_file(f, &buf, &buf_size);
487 if (rc)
488 break;
489
490 csize = size > buf_size ? buf_size : size;
491 bcopy(buf, addr, csize);
492
493 fp->f_off += csize;
494 addr += csize;
495 size -= csize;
496 }
497 if (resid)
498 *resid = size;
499 return (rc);
500}
501
502static int
503cd9660_readdir(struct open_file *f, struct dirent *d)
504{
505 struct file *fp = (struct file *)f->f_fsdata;
506 struct iso_directory_record *ep;
507 size_t buf_size, reclen, namelen;
508 int error = 0;
509 int lenskip;
510 char *buf, *name;
511
512again:
513 if (fp->f_off >= fp->f_size)
514 return (ENOENT);
515 error = buf_read_file(f, &buf, &buf_size);
516 if (error)
517 return (error);
518 ep = (struct iso_directory_record *)buf;
519
520 if (isonum_711(ep->length) == 0) {
521 daddr_t blkno;
522
523 /* skip to next block, if any */
524 blkno = fp->f_off / ISO_DEFAULT_BLOCK_SIZE;
525 fp->f_off = (blkno + 1) * ISO_DEFAULT_BLOCK_SIZE;
526 goto again;
527 }
528
529 if (fp->f_flags & F_RR) {
530 if (fp->f_flags & F_ROOTDIR && fp->f_off == 0)
531 lenskip = 0;
532 else
533 lenskip = fp->f_susp_skip;
534 name = rrip_lookup_name(f, ep, lenskip, &namelen);
535 } else
536 name = NULL;
537 if (name == NULL) {
538 namelen = isonum_711(ep->name_len);
539 name = ep->name;
540 if (namelen == 1) {
541 if (ep->name[0] == 0)
542 name = ".";
543 else if (ep->name[0] == 1) {
544 namelen = 2;
545 name = "..";
546 }
547 }
548 }
01f31ab3 549 reclen = _DIRENT_RECLEN(namelen);
984263bc 550
01f31ab3 551 d->d_ino = isonum_733(ep->extent);
984263bc
MD
552 if (isonum_711(ep->flags) & 2)
553 d->d_type = DT_DIR;
554 else
555 d->d_type = DT_REG;
556 d->d_namlen = namelen;
557
558 bcopy(name, d->d_name, d->d_namlen);
559 d->d_name[d->d_namlen] = 0;
560
561 fp->f_off += isonum_711(ep->length);
562 return (0);
563}
564
565static int
566cd9660_write(struct open_file *f, void *start, size_t size, size_t *resid)
567{
568 return EROFS;
569}
570
571static off_t
572cd9660_seek(struct open_file *f, off_t offset, int where)
573{
574 struct file *fp = (struct file *)f->f_fsdata;
575
576 switch (where) {
577 case SEEK_SET:
578 fp->f_off = offset;
579 break;
580 case SEEK_CUR:
581 fp->f_off += offset;
582 break;
583 case SEEK_END:
584 fp->f_off = fp->f_size - offset;
585 break;
586 default:
587 return -1;
588 }
589 return fp->f_off;
590}
591
592static int
593cd9660_stat(struct open_file *f, struct stat *sb)
594{
595 struct file *fp = (struct file *)f->f_fsdata;
596
597 /* only important stuff */
598 sb->st_mode = S_IRUSR | S_IRGRP | S_IROTH;
599 if (fp->f_flags & F_ISDIR)
600 sb->st_mode |= S_IFDIR;
601 else
602 sb->st_mode |= S_IFREG;
603 sb->st_uid = sb->st_gid = 0;
604 sb->st_size = fp->f_size;
605 return 0;
606}