xref: /netbsd-src/sys/arch/atari/stand/libsa/diskio.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: diskio.c,v 1.8 2009/03/17 00:18:40 he Exp $	*/
2 
3 /*
4  * Copyright (c) 1995 Waldi Ravens.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *        This product includes software developed by Waldi Ravens.
18  * 4. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <lib/libsa/stand.h>
34 #include "atari_stand.h"
35 #include <sys/disklabel.h>
36 
37 typedef int (*rdsec_f)(void *buffer, u_int offset, u_int count);
38 typedef	struct { rdsec_f rds; u_int rst; u_int rend; } bdevd_t;
39 
40 static int rootstrategy(void *, int, daddr_t, size_t, void *, size_t *);
41 static int rootopen(struct open_file *, ...);
42 static int rootclose(struct open_file *);
43 static int rootioctl(struct open_file *, u_long, void *);
44 
45 struct devsw devsw[] = {
46 	{ "root", rootstrategy, rootopen, rootclose, rootioctl }
47 };
48 static bdevd_t	bootdev;
49 
50 /*
51  * Initialise boot device info.
52  */
53 int
54 init_dskio (void *func, void *label, int root)
55 {
56 	struct disklabel *dl = label;
57 	struct partition *pd = &dl->d_partitions[root];
58 
59 	if (dl->d_magic != DISKMAGIC || dl->d_magic2 != DISKMAGIC
60 	    || dl->d_npartitions > MAXPARTITIONS || dkcksum(dl) != 0) {
61 		printf("Invalid disk label.\n");
62 		return(-1);
63 	}
64 	if (root >= 0) {
65 		if (root >= dl->d_npartitions
66 		    || pd->p_fstype != FS_BSDFFS || pd->p_size == 0) {
67 			printf("No suitable root.\n");
68 			return(-1);
69 		}
70 		bootdev.rds  = func;
71 		bootdev.rst  = pd->p_offset;
72 		bootdev.rend = pd->p_offset + pd->p_size - 1;
73 	}
74 	return(0);
75 }
76 
77 /*
78  * No choice, the kernel is loaded from the
79  * same device as the bootstrap.
80  */
81 int
82 devopen (struct open_file *f, const char *fname, char **file)
83 {
84 	f->f_devdata = &bootdev;
85 	f->f_dev = &devsw[0];
86 	*file = (char *)fname;
87 	return(0);
88 }
89 
90 static int
91 rootstrategy (void *devd, int flag, daddr_t dblk, size_t size, void *buf, size_t *rsize)
92 {
93 	bdevd_t	*dd = devd;
94 	daddr_t stb = dd->rst + dblk;
95 	size_t	nb  = size >> 9;
96 
97 	if ((flag == F_READ) && !(size & 511) && (stb + nb <= dd->rend)) {
98 		if (!dd->rds(buf, stb, nb)) {
99 			*rsize = size;
100 			return(0);
101 		}
102 	}
103 	*rsize = 0;
104 	return(EIO);
105 }
106 
107 static int
108 rootopen (struct open_file *f, ...)
109 {
110 	return(0);
111 }
112 
113 static int
114 rootclose (struct open_file *f)
115 {
116 	return(EIO);
117 }
118 
119 static int
120 rootioctl (struct open_file *f, u_long cmd, void *data)
121 {
122 	return(EIO);
123 }
124