Home | History | Annotate | Line # | Download | only in dosboot
dosfile.c revision 1.1
      1  1.1  rin /*	$NetBSD: dosfile.c,v 1.1 2024/06/29 13:45:14 rin Exp $	 */
      2  1.1  rin 
      3  1.1  rin /*
      4  1.1  rin  * Copyright (c) 1996
      5  1.1  rin  *	Matthias Drochner.  All rights reserved.
      6  1.1  rin  *
      7  1.1  rin  * Redistribution and use in source and binary forms, with or without
      8  1.1  rin  * modification, are permitted provided that the following conditions
      9  1.1  rin  * are met:
     10  1.1  rin  * 1. Redistributions of source code must retain the above copyright
     11  1.1  rin  *    notice, this list of conditions and the following disclaimer.
     12  1.1  rin  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  rin  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  rin  *    documentation and/or other materials provided with the distribution.
     15  1.1  rin  *
     16  1.1  rin  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  1.1  rin  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  1.1  rin  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  1.1  rin  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  1.1  rin  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21  1.1  rin  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22  1.1  rin  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23  1.1  rin  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  1.1  rin  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25  1.1  rin  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  1.1  rin  *
     27  1.1  rin  */
     28  1.1  rin 
     29  1.1  rin /*
     30  1.1  rin  * DOS filesystem for libsa
     31  1.1  rin  * standalone - uses no device, works only with DOS running
     32  1.1  rin  * needs lowlevel parts from dos_file.S
     33  1.1  rin  */
     34  1.1  rin 
     35  1.1  rin #include <lib/libsa/stand.h>
     36  1.1  rin 
     37  1.1  rin #include "diskbuf.h"
     38  1.1  rin #include "dosfile.h"
     39  1.1  rin 
     40  1.1  rin extern int dosopen(const char *);
     41  1.1  rin extern void dosclose(int);
     42  1.1  rin extern int dosread(int, char *, int);
     43  1.1  rin extern int dosseek(int, int, int);
     44  1.1  rin 
     45  1.1  rin struct dosfile {
     46  1.1  rin 	int doshandle, off;
     47  1.1  rin };
     48  1.1  rin 
     49  1.1  rin extern int doserrno;	/* in dos_file.S */
     50  1.1  rin 
     51  1.1  rin static int dos2errno(void);
     52  1.1  rin 
     53  1.1  rin static int
     54  1.1  rin dos2errno(void)
     55  1.1  rin {
     56  1.1  rin 	int err;
     57  1.1  rin 
     58  1.1  rin 	switch (doserrno) {
     59  1.1  rin 	    case 1: /* invalid function number */
     60  1.1  rin 	    case 4: /* too many open files */
     61  1.1  rin 	    case 12: /* invalid access mode */
     62  1.1  rin 	    default:
     63  1.1  rin 		err = EIO;
     64  1.1  rin 		break;
     65  1.1  rin 	    case 2: /* file not found */
     66  1.1  rin 	    case 3: /* path not found */
     67  1.1  rin 		err = ENOENT;
     68  1.1  rin 		break;
     69  1.1  rin 	    case 5: /* access denied */
     70  1.1  rin 		err = EPERM;
     71  1.1  rin 		break;
     72  1.1  rin 	    case 6: /* invalid handle */
     73  1.1  rin 		err = EINVAL;
     74  1.1  rin 		break;
     75  1.1  rin 	}
     76  1.1  rin 	return err;
     77  1.1  rin }
     78  1.1  rin 
     79  1.1  rin __compactcall int
     80  1.1  rin dos_open(const char *path, struct open_file *f)
     81  1.1  rin {
     82  1.1  rin 	struct dosfile *df;
     83  1.1  rin 
     84  1.1  rin 	df = (struct dosfile *) alloc(sizeof(*df));
     85  1.1  rin 	if (!df)
     86  1.1  rin 		return -1;
     87  1.1  rin 
     88  1.1  rin 	df->off = 0;
     89  1.1  rin 	df->doshandle = dosopen(path);
     90  1.1  rin 	if (df->doshandle < 0) {
     91  1.1  rin #ifdef DEBUG
     92  1.1  rin 		printf("DOS error %d\n", doserrno);
     93  1.1  rin #endif
     94  1.1  rin 		dealloc(df, sizeof(*df));
     95  1.1  rin 		return dos2errno();
     96  1.1  rin 	}
     97  1.1  rin 	f->f_fsdata = (void *) df;
     98  1.1  rin 	return 0;
     99  1.1  rin }
    100  1.1  rin 
    101  1.1  rin __compactcall int
    102  1.1  rin dos_read(struct open_file *f, void *addr, size_t size, size_t *resid)
    103  1.1  rin {
    104  1.1  rin 	struct dosfile *df;
    105  1.1  rin 	int             got;
    106  1.1  rin 	static int      tc = 0;
    107  1.1  rin 
    108  1.1  rin 	df = (struct dosfile *) f->f_fsdata;
    109  1.1  rin 
    110  1.1  rin 	if (!(tc++ % 4))
    111  1.1  rin 		twiddle();
    112  1.1  rin 
    113  1.1  rin 	if ((unsigned long) addr >= 0x10000) {
    114  1.1  rin 		u_int           lsize = size;
    115  1.1  rin 
    116  1.1  rin 		while (lsize > 0) {
    117  1.1  rin 			u_int           tsize;
    118  1.1  rin 			size_t          tgot;
    119  1.1  rin 			char		*p = addr;
    120  1.1  rin 
    121  1.1  rin 			tsize = lsize;
    122  1.1  rin 
    123  1.1  rin 			if (tsize > DISKBUFSIZE)
    124  1.1  rin 				tsize = DISKBUFSIZE;
    125  1.1  rin 
    126  1.1  rin 			alloc_diskbuf(dos_read);
    127  1.1  rin 
    128  1.1  rin 			tgot = dosread(df->doshandle, diskbufp, tsize);
    129  1.1  rin 			if (tgot < 0) {
    130  1.1  rin #ifdef DEBUG
    131  1.1  rin 				printf("DOS error %d\n", doserrno);
    132  1.1  rin #endif
    133  1.1  rin 				return dos2errno();
    134  1.1  rin 			}
    135  1.1  rin 			memcpy(p, diskbufp, tgot);
    136  1.1  rin 
    137  1.1  rin 			p += tgot;
    138  1.1  rin 			lsize -= tgot;
    139  1.1  rin 
    140  1.1  rin 			if (tgot != tsize)
    141  1.1  rin 				break;	/* EOF */
    142  1.1  rin 		}
    143  1.1  rin 		got = size - lsize;
    144  1.1  rin 	} else {
    145  1.1  rin 		got = dosread(df->doshandle, addr, size);
    146  1.1  rin 
    147  1.1  rin 		if (got < 0) {
    148  1.1  rin #ifdef DEBUG
    149  1.1  rin 			printf("DOS error %d\n", doserrno);
    150  1.1  rin #endif
    151  1.1  rin 			return dos2errno();
    152  1.1  rin 		}
    153  1.1  rin 	}
    154  1.1  rin 
    155  1.1  rin 	df->off += got;
    156  1.1  rin 	size -= got;
    157  1.1  rin 
    158  1.1  rin 	if (resid)
    159  1.1  rin 		*resid = size;
    160  1.1  rin 	return 0;
    161  1.1  rin }
    162  1.1  rin 
    163  1.1  rin __compactcall int
    164  1.1  rin dos_close(struct open_file *f)
    165  1.1  rin {
    166  1.1  rin 	struct dosfile *df;
    167  1.1  rin 	df = (struct dosfile *) f->f_fsdata;
    168  1.1  rin 
    169  1.1  rin 	dosclose(df->doshandle);
    170  1.1  rin 
    171  1.1  rin 	if (df)
    172  1.1  rin 		dealloc(df, sizeof(*df));
    173  1.1  rin 	return 0;
    174  1.1  rin }
    175  1.1  rin 
    176  1.1  rin __compactcall int
    177  1.1  rin dos_write(struct open_file *f, void *start, size_t size, size_t *resid)
    178  1.1  rin {
    179  1.1  rin 	return EROFS;
    180  1.1  rin }
    181  1.1  rin 
    182  1.1  rin __compactcall int
    183  1.1  rin dos_stat(struct open_file *f, struct stat *sb)
    184  1.1  rin {
    185  1.1  rin 	sb->st_mode = 0444;
    186  1.1  rin 	sb->st_nlink = 1;
    187  1.1  rin 	sb->st_uid = 0;
    188  1.1  rin 	sb->st_gid = 0;
    189  1.1  rin 	sb->st_size = -1;
    190  1.1  rin 	return 0;
    191  1.1  rin }
    192  1.1  rin 
    193  1.1  rin __compactcall off_t
    194  1.1  rin dos_seek(struct open_file *f, off_t offset, int where)
    195  1.1  rin {
    196  1.1  rin 	struct dosfile *df;
    197  1.1  rin 	int             doswhence, res;
    198  1.1  rin #ifdef DOS_CHECK
    199  1.1  rin 	int             checkoffs;
    200  1.1  rin #endif
    201  1.1  rin 	df = (struct dosfile *) f->f_fsdata;
    202  1.1  rin 
    203  1.1  rin 	switch (where) {
    204  1.1  rin 	case SEEK_SET:
    205  1.1  rin 		doswhence = 0;
    206  1.1  rin #ifdef DOS_CHECK
    207  1.1  rin 		checkoffs = offset;	/* don't trust DOS */
    208  1.1  rin #endif
    209  1.1  rin 		break;
    210  1.1  rin 	case SEEK_CUR:
    211  1.1  rin 		doswhence = 1;
    212  1.1  rin #ifdef DOS_CHECK
    213  1.1  rin 		checkoffs = df->off + offset;
    214  1.1  rin #endif
    215  1.1  rin 		break;
    216  1.1  rin 	case SEEK_END:
    217  1.1  rin 		doswhence = 2;
    218  1.1  rin #ifdef DOS_CHECK
    219  1.1  rin 		checkoffs = -1;	/* we dont know len */
    220  1.1  rin #endif
    221  1.1  rin 		break;
    222  1.1  rin 	default:
    223  1.1  rin 		errno = EOFFSET;
    224  1.1  rin 		return -1;
    225  1.1  rin 	}
    226  1.1  rin 	res = dosseek(df->doshandle, offset, doswhence);
    227  1.1  rin 	if (res == -1) {
    228  1.1  rin 		errno = dos2errno();
    229  1.1  rin 		return -1;
    230  1.1  rin 	}
    231  1.1  rin #ifdef DOS_CHECK
    232  1.1  rin 	if ((checkoffs != -1) && (res != checkoffs)) {
    233  1.1  rin 		printf("dosfile: unexpected seek result (%d+%d(%d)=%d)\n",
    234  1.1  rin 		       df->off, offset, where, res);
    235  1.1  rin 		errno = EOFFSET;
    236  1.1  rin 		return -1;
    237  1.1  rin 	}
    238  1.1  rin #endif
    239  1.1  rin 	df->off = res;
    240  1.1  rin 	return res;
    241  1.1  rin }
    242