xref: /netbsd-src/sys/arch/arc/stand/boot/disk.c (revision e19dc66768b5647c460c8aba4b86b53337689ad7)
1*e19dc667Sdholland /*	$NetBSD: disk.c,v 1.7 2016/02/14 18:01:45 dholland Exp $	*/
2b28c4afbStsutsui 
3b28c4afbStsutsui /*
4b28c4afbStsutsui  * Copyright (c) 1992, 1993
5b28c4afbStsutsui  *	The Regents of the University of California.  All rights reserved.
6b28c4afbStsutsui  *
7b28c4afbStsutsui  * This code is derived from software contributed to Berkeley by
8b28c4afbStsutsui  * Van Jacobson of Lawrence Berkeley Laboratory and Ralph Campbell.
9b28c4afbStsutsui  *
10b28c4afbStsutsui  * Redistribution and use in source and binary forms, with or without
11b28c4afbStsutsui  * modification, are permitted provided that the following conditions
12b28c4afbStsutsui  * are met:
13b28c4afbStsutsui  * 1. Redistributions of source code must retain the above copyright
14b28c4afbStsutsui  *    notice, this list of conditions and the following disclaimer.
15b28c4afbStsutsui  * 2. Redistributions in binary form must reproduce the above copyright
16b28c4afbStsutsui  *    notice, this list of conditions and the following disclaimer in the
17b28c4afbStsutsui  *    documentation and/or other materials provided with the distribution.
18b28c4afbStsutsui  * 3. Neither the name of the University nor the names of its contributors
19b28c4afbStsutsui  *    may be used to endorse or promote products derived from this software
20b28c4afbStsutsui  *    without specific prior written permission.
21b28c4afbStsutsui  *
22b28c4afbStsutsui  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23b28c4afbStsutsui  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24b28c4afbStsutsui  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25b28c4afbStsutsui  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26b28c4afbStsutsui  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27b28c4afbStsutsui  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28b28c4afbStsutsui  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29b28c4afbStsutsui  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30b28c4afbStsutsui  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31b28c4afbStsutsui  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32b28c4afbStsutsui  * SUCH DAMAGE.
33b28c4afbStsutsui  *
34b28c4afbStsutsui  *	@(#)disk.c	8.1 (Berkeley) 6/10/93
35b28c4afbStsutsui  */
36b28c4afbStsutsui 
37b28c4afbStsutsui #include <lib/libsa/stand.h>
38b28c4afbStsutsui 
39b28c4afbStsutsui #include <sys/param.h>
40b28c4afbStsutsui #include <sys/disklabel.h>
41b28c4afbStsutsui 
42b28c4afbStsutsui #include <dev/arcbios/arcbios.h>
43b28c4afbStsutsui 
44b28c4afbStsutsui #include "common.h"
45b28c4afbStsutsui #include "disk.h"
46b28c4afbStsutsui 
47b28c4afbStsutsui #define	RF_PROTECTED_SECTORS	64	/* XXX refer to <.../rf_optnames.h> */
48b28c4afbStsutsui 
49b28c4afbStsutsui struct	disk_softc {
50b28c4afbStsutsui 	u_long	sc_fd;			/* ARCBIOS file id */
51b28c4afbStsutsui 	int	sc_part;		/* disk partition number */
52b28c4afbStsutsui 	struct	disklabel sc_label;	/* disk label for this disk */
53b28c4afbStsutsui };
54b28c4afbStsutsui 
55b28c4afbStsutsui int
diskstrategy(void * devdata,int rw,daddr_t bn,size_t reqcnt,void * addr,size_t * cnt)56b28c4afbStsutsui diskstrategy(void *devdata, int rw, daddr_t bn, size_t reqcnt, void *addr,
57b28c4afbStsutsui     size_t *cnt)
58b28c4afbStsutsui {
59b28c4afbStsutsui 	struct disk_softc *sc = (struct disk_softc *)devdata;
60b28c4afbStsutsui 	int part = sc->sc_part;
61b28c4afbStsutsui 	struct partition *pp = &sc->sc_label.d_partitions[part];
62b28c4afbStsutsui 	long error;
63b28c4afbStsutsui 	int64_t offset;
64b28c4afbStsutsui 	u_long count;
65b28c4afbStsutsui 
66b28c4afbStsutsui 	offset = bn;
67b28c4afbStsutsui 
68b28c4afbStsutsui 	/*
69b28c4afbStsutsui 	 * Partial-block transfers not handled.
70b28c4afbStsutsui 	 */
71b28c4afbStsutsui 	if (reqcnt & (DEV_BSIZE - 1)) {
72b28c4afbStsutsui 		*cnt = 0;
73b28c4afbStsutsui 		return EINVAL;
74b28c4afbStsutsui 	}
75b28c4afbStsutsui 
76b28c4afbStsutsui 	offset += pp->p_offset;
77b28c4afbStsutsui 
78b28c4afbStsutsui 	if (pp->p_fstype == FS_RAID)
79b28c4afbStsutsui 		offset += RF_PROTECTED_SECTORS;
80b28c4afbStsutsui 
81b28c4afbStsutsui 	/*
82b28c4afbStsutsui 	 * Convert from blocks to bytes.
83b28c4afbStsutsui 	 */
84b28c4afbStsutsui 	offset *= DEV_BSIZE;
85b28c4afbStsutsui 
867641af97Smatt 	error = arcbios_Seek(sc->sc_fd, &offset, 0);
87b28c4afbStsutsui 	if (error != ARCBIOS_ESUCCESS)
88b28c4afbStsutsui 		return EIO;
897641af97Smatt 	error = arcbios_Read(sc->sc_fd, addr, reqcnt, &count);
90b28c4afbStsutsui 	if (error != ARCBIOS_ESUCCESS)
91b28c4afbStsutsui 		return EIO;
92b28c4afbStsutsui 
93b28c4afbStsutsui 	*cnt = count;
94b28c4afbStsutsui 	return 0;
95b28c4afbStsutsui }
96b28c4afbStsutsui 
97b28c4afbStsutsui int
diskopen(struct open_file * f,...)98b28c4afbStsutsui diskopen(struct open_file *f, ...)
99b28c4afbStsutsui {
100b28c4afbStsutsui 	int part;
101b28c4afbStsutsui 
102b28c4afbStsutsui 	struct disk_softc *sc;
103b28c4afbStsutsui 	struct disklabel *lp;
104b28c4afbStsutsui #ifdef arc
105b28c4afbStsutsui 	char *msg, buf[DEV_BSIZE];
106b28c4afbStsutsui 	size_t cnt;
107b28c4afbStsutsui 	int mbrp_off, i;
108b28c4afbStsutsui #endif
109b28c4afbStsutsui 	int error;
110b28c4afbStsutsui 	u_long fd;
111b28c4afbStsutsui 	char *device;
112b28c4afbStsutsui 	va_list ap;
113b28c4afbStsutsui 
114b28c4afbStsutsui 	va_start(ap, f);
115b28c4afbStsutsui 	device = va_arg(ap, char *);
116*e19dc667Sdholland 	va_end(ap);
117b28c4afbStsutsui 
118b28c4afbStsutsui 	/*
119b28c4afbStsutsui 	 * For NetBSD/sgimips, since we use the SGI partition map directly,
120b28c4afbStsutsui 	 * we fake an in-core NetBSD disklabel with offset of 0.
121b28c4afbStsutsui 	 *
122b28c4afbStsutsui 	 * For NetBSD/arc, there is a MBR partition map on the disk, which we
123b28c4afbStsutsui 	 * then expect to find a NetBSD disklabel within the MBR partition.
124b28c4afbStsutsui 	 * We require that the kernel be located in first partition in the
125b28c4afbStsutsui 	 * NetBSD disklabel, because we have not other way to represent the
126b28c4afbStsutsui 	 * root partition.
127b28c4afbStsutsui 	 */
128b28c4afbStsutsui 	part = 0;
129b28c4afbStsutsui 
130b28c4afbStsutsui 	if (part >= MAXPARTITIONS)
131b28c4afbStsutsui 		return ENXIO;
132b28c4afbStsutsui 
1337641af97Smatt 	error = arcbios_Open(device, 0, &fd);
134b28c4afbStsutsui 	if (error) {
135b28c4afbStsutsui 		printf("diskopen: open failed, errno = %d\n", error);
136b28c4afbStsutsui 		return ENXIO;
137b28c4afbStsutsui 	}
138b28c4afbStsutsui 
139b28c4afbStsutsui 	sc = alloc(sizeof(struct disk_softc));
140b28c4afbStsutsui 	memset(sc, 0, sizeof(struct disk_softc));
141b28c4afbStsutsui 	f->f_devdata = (void *)sc;
142b28c4afbStsutsui 
143b28c4afbStsutsui 	sc->sc_fd = fd;
144b28c4afbStsutsui 	sc->sc_part = part;
145b28c4afbStsutsui 
146b28c4afbStsutsui 	/* try to read disk label and partition table information */
147b28c4afbStsutsui 	lp = &sc->sc_label;
148b28c4afbStsutsui 	lp->d_secsize = DEV_BSIZE;
149b28c4afbStsutsui 	lp->d_secpercyl = 1;
150b28c4afbStsutsui 	lp->d_npartitions = MAXPARTITIONS;
151b28c4afbStsutsui 	lp->d_partitions[part].p_offset = 0;
152b28c4afbStsutsui 	lp->d_partitions[part].p_size = 0x7fffffff;
153b28c4afbStsutsui 
154b28c4afbStsutsui #ifdef arc
155b28c4afbStsutsui 	error = diskstrategy(sc, F_READ, (daddr_t)LABELSECTOR, DEV_BSIZE,
156b28c4afbStsutsui 	    buf, &cnt);
157b28c4afbStsutsui 	if (error || cnt != DEV_BSIZE) {
158b28c4afbStsutsui 		printf("%s: can't read disklabel, errno = %d\n",
159b28c4afbStsutsui 		    device, error);
160606bb2caSchristos 		dealloc(sc, sizeof(struct disk_softc));
161b28c4afbStsutsui 		return ENXIO;
162b28c4afbStsutsui 	}
163b28c4afbStsutsui 	msg = getdisklabel(buf, lp);
164b28c4afbStsutsui 	if (msg) {
165b28c4afbStsutsui 		/* If no label, just assume 0 and return */
166b28c4afbStsutsui 		return 0;
167b28c4afbStsutsui 	}
168b28c4afbStsutsui 
169b28c4afbStsutsui 	/*
170b28c4afbStsutsui 	 * On arc, we can't open whole disk, but can open each partition with
171b28c4afbStsutsui 	 * OSLOADPARTITION like scsi(0)disk(0)rdisk()partition(1) etc.
172b28c4afbStsutsui 	 * Thus, we don't have to add offset of the MBR partition.
173b28c4afbStsutsui 	 */
174b28c4afbStsutsui 	/* XXX magic: partition 2 is whole NetBSD partition */
175b28c4afbStsutsui 	mbrp_off = lp->d_partitions[2].p_offset;
176b28c4afbStsutsui 	for (i = 0; i < MAXPARTITIONS; i++) {
177b28c4afbStsutsui 		if (lp->d_partitions[i].p_fstype != FS_UNUSED &&
178b28c4afbStsutsui 		    lp->d_partitions[i].p_offset >= mbrp_off)
179b28c4afbStsutsui 			lp->d_partitions[i].p_offset -= mbrp_off;
180b28c4afbStsutsui 	}
181b28c4afbStsutsui 
182b28c4afbStsutsui 	if (part >= lp->d_npartitions ||
183b28c4afbStsutsui 	    lp->d_partitions[part].p_fstype == FS_UNUSED ||
184b28c4afbStsutsui 	    lp->d_partitions[part].p_size == 0) {
185606bb2caSchristos 		dealloc(sc, sizeof(struct disk_softc));
186b28c4afbStsutsui 		return ENXIO;
187b28c4afbStsutsui 	}
188be6229fdStsutsui #endif
189b28c4afbStsutsui 	return 0;
190b28c4afbStsutsui }
191b28c4afbStsutsui 
192b28c4afbStsutsui #ifndef LIBSA_NO_DEV_CLOSE
193b28c4afbStsutsui int
diskclose(struct open_file * f)194b28c4afbStsutsui diskclose(struct open_file *f)
195b28c4afbStsutsui {
196b28c4afbStsutsui 
1977641af97Smatt 	arcbios_Close(((struct disk_softc *)(f->f_devdata))->sc_fd);
198606bb2caSchristos 	dealloc(f->f_devdata, sizeof(struct disk_softc));
199b28c4afbStsutsui 	f->f_devdata = NULL;
200b28c4afbStsutsui 	return 0;
201b28c4afbStsutsui }
202b28c4afbStsutsui #endif
203