Home | History | Annotate | Line # | Download | only in dosboot
dosfile.c revision 1.2
      1  1.2  rin /*	$NetBSD: dosfile.c,v 1.2 2024/06/29 13:46:40 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 struct dosfile {
     41  1.1  rin 	int doshandle, off;
     42  1.1  rin };
     43  1.1  rin 
     44  1.1  rin extern int doserrno;	/* in dos_file.S */
     45  1.1  rin 
     46  1.1  rin static int dos2errno(void);
     47  1.1  rin 
     48  1.1  rin static int
     49  1.1  rin dos2errno(void)
     50  1.1  rin {
     51  1.1  rin 	int err;
     52  1.1  rin 
     53  1.1  rin 	switch (doserrno) {
     54  1.1  rin 	    case 1: /* invalid function number */
     55  1.1  rin 	    case 4: /* too many open files */
     56  1.1  rin 	    case 12: /* invalid access mode */
     57  1.1  rin 	    default:
     58  1.1  rin 		err = EIO;
     59  1.1  rin 		break;
     60  1.1  rin 	    case 2: /* file not found */
     61  1.1  rin 	    case 3: /* path not found */
     62  1.1  rin 		err = ENOENT;
     63  1.1  rin 		break;
     64  1.1  rin 	    case 5: /* access denied */
     65  1.1  rin 		err = EPERM;
     66  1.1  rin 		break;
     67  1.1  rin 	    case 6: /* invalid handle */
     68  1.1  rin 		err = EINVAL;
     69  1.1  rin 		break;
     70  1.1  rin 	}
     71  1.1  rin 	return err;
     72  1.1  rin }
     73  1.1  rin 
     74  1.1  rin __compactcall int
     75  1.1  rin dos_open(const char *path, struct open_file *f)
     76  1.1  rin {
     77  1.1  rin 	struct dosfile *df;
     78  1.1  rin 
     79  1.1  rin 	df = (struct dosfile *) alloc(sizeof(*df));
     80  1.1  rin 	if (!df)
     81  1.1  rin 		return -1;
     82  1.1  rin 
     83  1.1  rin 	df->off = 0;
     84  1.1  rin 	df->doshandle = dosopen(path);
     85  1.1  rin 	if (df->doshandle < 0) {
     86  1.1  rin #ifdef DEBUG
     87  1.1  rin 		printf("DOS error %d\n", doserrno);
     88  1.1  rin #endif
     89  1.1  rin 		dealloc(df, sizeof(*df));
     90  1.1  rin 		return dos2errno();
     91  1.1  rin 	}
     92  1.1  rin 	f->f_fsdata = (void *) df;
     93  1.1  rin 	return 0;
     94  1.1  rin }
     95  1.1  rin 
     96  1.1  rin __compactcall int
     97  1.1  rin dos_read(struct open_file *f, void *addr, size_t size, size_t *resid)
     98  1.1  rin {
     99  1.1  rin 	struct dosfile *df;
    100  1.1  rin 	int             got;
    101  1.1  rin 	static int      tc = 0;
    102  1.1  rin 
    103  1.1  rin 	df = (struct dosfile *) f->f_fsdata;
    104  1.1  rin 
    105  1.1  rin 	if (!(tc++ % 4))
    106  1.1  rin 		twiddle();
    107  1.1  rin 
    108  1.1  rin 	if ((unsigned long) addr >= 0x10000) {
    109  1.1  rin 		u_int           lsize = size;
    110  1.1  rin 
    111  1.1  rin 		while (lsize > 0) {
    112  1.1  rin 			u_int           tsize;
    113  1.1  rin 			size_t          tgot;
    114  1.1  rin 			char		*p = addr;
    115  1.1  rin 
    116  1.1  rin 			tsize = lsize;
    117  1.1  rin 
    118  1.1  rin 			if (tsize > DISKBUFSIZE)
    119  1.1  rin 				tsize = DISKBUFSIZE;
    120  1.1  rin 
    121  1.1  rin 			alloc_diskbuf(dos_read);
    122  1.1  rin 
    123  1.1  rin 			tgot = dosread(df->doshandle, diskbufp, tsize);
    124  1.1  rin 			if (tgot < 0) {
    125  1.1  rin #ifdef DEBUG
    126  1.1  rin 				printf("DOS error %d\n", doserrno);
    127  1.1  rin #endif
    128  1.1  rin 				return dos2errno();
    129  1.1  rin 			}
    130  1.1  rin 			memcpy(p, diskbufp, tgot);
    131  1.1  rin 
    132  1.1  rin 			p += tgot;
    133  1.1  rin 			lsize -= tgot;
    134  1.1  rin 
    135  1.1  rin 			if (tgot != tsize)
    136  1.1  rin 				break;	/* EOF */
    137  1.1  rin 		}
    138  1.1  rin 		got = size - lsize;
    139  1.1  rin 	} else {
    140  1.1  rin 		got = dosread(df->doshandle, addr, size);
    141  1.1  rin 
    142  1.1  rin 		if (got < 0) {
    143  1.1  rin #ifdef DEBUG
    144  1.1  rin 			printf("DOS error %d\n", doserrno);
    145  1.1  rin #endif
    146  1.1  rin 			return dos2errno();
    147  1.1  rin 		}
    148  1.1  rin 	}
    149  1.1  rin 
    150  1.1  rin 	df->off += got;
    151  1.1  rin 	size -= got;
    152  1.1  rin 
    153  1.1  rin 	if (resid)
    154  1.1  rin 		*resid = size;
    155  1.1  rin 	return 0;
    156  1.1  rin }
    157  1.1  rin 
    158  1.1  rin __compactcall int
    159  1.1  rin dos_close(struct open_file *f)
    160  1.1  rin {
    161  1.1  rin 	struct dosfile *df;
    162  1.1  rin 	df = (struct dosfile *) f->f_fsdata;
    163  1.1  rin 
    164  1.1  rin 	dosclose(df->doshandle);
    165  1.1  rin 
    166  1.1  rin 	if (df)
    167  1.1  rin 		dealloc(df, sizeof(*df));
    168  1.1  rin 	return 0;
    169  1.1  rin }
    170  1.1  rin 
    171  1.1  rin __compactcall int
    172  1.1  rin dos_write(struct open_file *f, void *start, size_t size, size_t *resid)
    173  1.1  rin {
    174  1.1  rin 	return EROFS;
    175  1.1  rin }
    176  1.1  rin 
    177  1.1  rin __compactcall int
    178  1.1  rin dos_stat(struct open_file *f, struct stat *sb)
    179  1.1  rin {
    180  1.1  rin 	sb->st_mode = 0444;
    181  1.1  rin 	sb->st_nlink = 1;
    182  1.1  rin 	sb->st_uid = 0;
    183  1.1  rin 	sb->st_gid = 0;
    184  1.1  rin 	sb->st_size = -1;
    185  1.1  rin 	return 0;
    186  1.1  rin }
    187  1.1  rin 
    188  1.1  rin __compactcall off_t
    189  1.1  rin dos_seek(struct open_file *f, off_t offset, int where)
    190  1.1  rin {
    191  1.1  rin 	struct dosfile *df;
    192  1.1  rin 	int             doswhence, res;
    193  1.1  rin #ifdef DOS_CHECK
    194  1.1  rin 	int             checkoffs;
    195  1.1  rin #endif
    196  1.1  rin 	df = (struct dosfile *) f->f_fsdata;
    197  1.1  rin 
    198  1.1  rin 	switch (where) {
    199  1.1  rin 	case SEEK_SET:
    200  1.1  rin 		doswhence = 0;
    201  1.1  rin #ifdef DOS_CHECK
    202  1.1  rin 		checkoffs = offset;	/* don't trust DOS */
    203  1.1  rin #endif
    204  1.1  rin 		break;
    205  1.1  rin 	case SEEK_CUR:
    206  1.1  rin 		doswhence = 1;
    207  1.1  rin #ifdef DOS_CHECK
    208  1.1  rin 		checkoffs = df->off + offset;
    209  1.1  rin #endif
    210  1.1  rin 		break;
    211  1.1  rin 	case SEEK_END:
    212  1.1  rin 		doswhence = 2;
    213  1.1  rin #ifdef DOS_CHECK
    214  1.1  rin 		checkoffs = -1;	/* we dont know len */
    215  1.1  rin #endif
    216  1.1  rin 		break;
    217  1.1  rin 	default:
    218  1.1  rin 		errno = EOFFSET;
    219  1.1  rin 		return -1;
    220  1.1  rin 	}
    221  1.1  rin 	res = dosseek(df->doshandle, offset, doswhence);
    222  1.1  rin 	if (res == -1) {
    223  1.1  rin 		errno = dos2errno();
    224  1.1  rin 		return -1;
    225  1.1  rin 	}
    226  1.1  rin #ifdef DOS_CHECK
    227  1.1  rin 	if ((checkoffs != -1) && (res != checkoffs)) {
    228  1.1  rin 		printf("dosfile: unexpected seek result (%d+%d(%d)=%d)\n",
    229  1.1  rin 		       df->off, offset, where, res);
    230  1.1  rin 		errno = EOFFSET;
    231  1.1  rin 		return -1;
    232  1.1  rin 	}
    233  1.1  rin #endif
    234  1.1  rin 	df->off = res;
    235  1.1  rin 	return res;
    236  1.1  rin }
    237