1 /* $NetBSD: diskio.c,v 1.9 2021/05/17 19:31:38 mrg 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 #include <lib/libkern/libkern.h>
38
39 typedef int (*rdsec_f)(void *buffer, u_int offset, u_int count);
40 typedef struct { rdsec_f rds; u_int rst; u_int rend; } bdevd_t;
41
42 static int rootstrategy(void *, int, daddr_t, size_t, void *, size_t *);
43 static int rootopen(struct open_file *, ...);
44 static int rootclose(struct open_file *);
45 static int rootioctl(struct open_file *, u_long, void *);
46
47 struct devsw devsw[] = {
48 { "root", rootstrategy, rootopen, rootclose, rootioctl }
49 };
50 static bdevd_t bootdev;
51
52 /*
53 * Initialise boot device info.
54 */
55 int
init_dskio(void * func,void * label,int root)56 init_dskio (void *func, void *label, int root)
57 {
58 struct disklabel *dl = label;
59 struct partition *pd = &dl->d_partitions[root];
60
61 if (dl->d_magic != DISKMAGIC || dl->d_magic2 != DISKMAGIC
62 || dl->d_npartitions > MAXPARTITIONS || dkcksum(dl) != 0) {
63 printf("Invalid disk label.\n");
64 return(-1);
65 }
66 if (root >= 0) {
67 if (root >= dl->d_npartitions
68 || pd->p_fstype != FS_BSDFFS || pd->p_size == 0) {
69 printf("No suitable root.\n");
70 return(-1);
71 }
72 bootdev.rds = func;
73 bootdev.rst = pd->p_offset;
74 bootdev.rend = pd->p_offset + pd->p_size - 1;
75 }
76 return(0);
77 }
78
79 /*
80 * No choice, the kernel is loaded from the
81 * same device as the bootstrap.
82 */
83 int
devopen(struct open_file * f,const char * fname,char ** file)84 devopen (struct open_file *f, const char *fname, char **file)
85 {
86 f->f_devdata = &bootdev;
87 f->f_dev = &devsw[0];
88 *file = (char *)fname;
89 return(0);
90 }
91
92 static int
rootstrategy(void * devd,int flag,daddr_t dblk,size_t size,void * buf,size_t * rsize)93 rootstrategy (void *devd, int flag, daddr_t dblk, size_t size, void *buf, size_t *rsize)
94 {
95 bdevd_t *dd = devd;
96 daddr_t stb = dd->rst + dblk;
97 size_t nb = size >> 9;
98
99 if ((flag == F_READ) && !(size & 511) && (stb + nb <= dd->rend)) {
100 if (!dd->rds(buf, stb, nb)) {
101 *rsize = size;
102 return(0);
103 }
104 }
105 *rsize = 0;
106 return(EIO);
107 }
108
109 static int
rootopen(struct open_file * f,...)110 rootopen (struct open_file *f, ...)
111 {
112 return(0);
113 }
114
115 static int
rootclose(struct open_file * f)116 rootclose (struct open_file *f)
117 {
118 return(EIO);
119 }
120
121 static int
rootioctl(struct open_file * f,u_long cmd,void * data)122 rootioctl (struct open_file *f, u_long cmd, void *data)
123 {
124 return(EIO);
125 }
126