1/*	$NetBSD: dk.c,v 1.10 2009/05/08 09:33:59 skrll Exp $	*/
2
3/*	$OpenBSD: dk.c,v 1.5 1999/04/20 20:01:01 mickey Exp $	*/
4
5/*
6 * Copyright 1996 1995 by Open Software Foundation, Inc.
7 *              All Rights Reserved
8 *
9 * Permission to use, copy, modify, and distribute this software and
10 * its documentation for any purpose and without fee is hereby granted,
11 * provided that the above copyright notice appears in all copies and
12 * that both the copyright notice and this permission notice appear in
13 * supporting documentation.
14 *
15 * OSF DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
16 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
17 * FOR A PARTICULAR PURPOSE.
18 *
19 * IN NO EVENT SHALL OSF BE LIABLE FOR ANY SPECIAL, INDIRECT, OR
20 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
21 * LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT,
22 * NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
23 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24 *
25 */
26
27#include "libsa.h"
28
29#include <sys/param.h>
30#include <sys/disklabel.h>
31#include <sys/reboot.h>
32#include <machine/pdc.h>
33#include <machine/iomod.h>
34
35#include "dev_hppa.h"
36
37const char *dk_disklabel(struct hppa_dev *, struct disklabel *);
38
39iodcio_t dkiodc;	/* boot IODC entry point */
40
41const char *
42dk_disklabel(struct hppa_dev *dp, struct disklabel *label)
43{
44	char buf[DEV_BSIZE];
45	size_t ret;
46
47	if (iodcstrategy(dp, F_READ, LABELSECTOR, DEV_BSIZE, buf, &ret) ||
48	    ret != DEV_BSIZE)
49		return "can't read disklabel";
50
51	return (getdisklabel(buf, label));
52}
53
54int
55dkopen(struct open_file *f, ...)
56{
57	struct disklabel dkl;
58	struct hppa_dev *dp = f->f_devdata;
59	const char *st;
60	u_int i;
61
62#ifdef	DEBUG
63	if (debug)
64		printf("dkopen(%p)\n", f);
65#endif
66
67	if (!(dp->pz_dev = pdc_findev(-1, PCL_RANDOM)))
68		return ENXIO;
69
70	dp->part_off = 0;
71	st = NULL;
72#ifdef DEBUG
73	if (debug)
74		printf ("disklabel\n");
75#endif
76	if ((st = dk_disklabel(dp, &dkl)) != NULL) {
77#ifdef DEBUG
78		if (debug)
79			printf ("dkopen: %s\n", st);
80#endif
81	/*
82	 * Ignore disklabel errors for this two reasons:
83	 * 1. It is possible to dd(1) a LIF image containing the bootloader
84	 * and a kernel with attached RAM disk to disk and boot it. That way
85	 * the netboot installation LIF image is also usable as disk boot
86	 * image.
87	 * 2. Some old 700 machines report a wrong device class in
88	 * PAGE0->mem_boot.pz_class when net booting. (PCL_RANDOM instead
89	 * PCL_NET_MASK|PCL_SEQU) So the bootloader thinks it is booting
90	 * from disk when it is actually net booting. The net boot LIF image
91	 * contains no disklabel so the test for the disklabel will fail.
92	 * If the device open fails if there is no disklabel we are not able
93	 * to netboot those machines.
94	 * Therefore the error is ignored. The bootloader will fall back to
95	 * LIF later when there is no disklabel / FFS partition.
96	 * At the moment it doesn't matter that the wrong device type ("dk"
97	 * instead "lf") is used, as all I/O is abstracted by the firmware.
98	 * To get the correct device type it would be necessary to add a
99	 * quirk table to the switch() in dev_hppa.c:devboot().
100	 */
101	} else {
102		i = B_PARTITION(dp->bootdev);
103#ifdef DEBUG
104		if (debug)
105			printf("bootdev 0x%x, partition %u\n", dp->bootdev, i);
106#endif
107		if (i >= dkl.d_npartitions || !dkl.d_partitions[i].p_size) {
108			return (EPART);
109		}
110		dp->part_off = dkl.d_partitions[i].p_offset * dkl.d_secsize;
111	}
112#ifdef DEBUGBUG
113	if (debug)
114		printf ("dkopen() ret\n");
115#endif
116	return (0);
117}
118
119int
120dkclose(struct open_file *f)
121{
122	dealloc(f->f_devdata, sizeof(struct hppa_dev));
123	f->f_devdata = NULL;
124	return 0;
125}
126