promlib.c revision 1.1.78.1 1 /* $NetBSD: promlib.c,v 1.1.78.1 2008/05/18 12:32:51 yamt Exp $ */
2
3 /*-
4 * Copyright (c) 2005 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Paul Kranenburg.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * OPENPROM functions. These are here mainly to hide the OPENPROM interface
34 * from the rest of the kernel.
35 */
36
37 #include <sys/types.h>
38 #include <machine/promlib.h>
39
40 #include "openfirm.h"
41
42
43 void *romp;
44 struct promops promops;
45
46
47 static void
48 openfirmware_fatal(void)
49 {
50 printf("Invalid Openfirmware environment\n");
51 exit(0);
52 }
53
54 static int
55 openfirmware_chosen(void)
56 {
57 static int phandle = -1;
58
59 if (phandle == -1) {
60 if ( (phandle = OF_finddevice("/chosen")) == -1) {
61 exit(0);
62 }
63 }
64
65 return (phandle);
66 }
67
68 static const char*
69 openfirmware_bootpath(void)
70 {
71 static char bootpath[PROM_MAX_PATH];
72
73 if (_prom_getprop(openfirmware_chosen(), "bootpath", bootpath,
74 sizeof(bootpath)) < 0) {
75 openfirmware_fatal();
76 }
77
78 return bootpath;
79 }
80
81 static const char*
82 openfirmware_bootfile(void)
83 {
84 /* Default image name */
85 return "netbsd";
86 }
87
88 static const char*
89 openfirmware_bootargs(void)
90 {
91 static char bootargs[PROM_MAX_PATH * 2];
92
93 if (_prom_getprop(openfirmware_chosen(), "bootargs", bootargs,
94 sizeof(bootargs)) < 0) {
95 openfirmware_fatal();
96 }
97
98 return bootargs;
99 }
100
101 static int
102 openfirmware_getchar(void)
103 {
104 unsigned char ch = '\0';
105 int l;
106
107 while ((l = OF_read(prom_stdin(), &ch, 1)) != 1)
108 if (l != -2 && l != 0)
109 return -1;
110 return ch;
111 }
112
113 static void
114 openfirmware_putchar(int c)
115 {
116 char ch = c;
117
118 if (c == '\n')
119 putchar('\r');
120 OF_write(prom_stdout(), &ch, 1);
121 }
122
123 void
124 prom_halt(void)
125 {
126 _prom_halt();
127 }
128
129 int
130 prom_findroot(void)
131 {
132 return OF_peer(0);
133 }
134
135 void
136 prom_init(void)
137 {
138 int phandle, size;
139
140 OF_initialize();
141
142 memset(promops, 0, sizeof(promops));
143
144 /* Access to boot arguments */
145 promops.po_bootpath = openfirmware_bootpath;
146 promops.po_bootfile = openfirmware_bootfile;
147 promops.po_bootargs = openfirmware_bootargs;
148
149 /* I/O functions */
150 promops.po_getchar = openfirmware_getchar;
151 promops.po_putchar = openfirmware_putchar;
152 promops.po_open = OF_open;
153 promops.po_close = OF_close;
154 promops.po_read = OF_read;
155 promops.po_write = OF_write;
156 promops.po_seek = OF_seek;
157
158 promops.po_instance_to_package = OF_instance_to_package;
159
160 /* Program termination control */
161 promops.po_halt = OF_exit;
162 promops.po_abort = OF_enter;
163 promops.po_ticks = OF_milliseconds;
164
165 /* Device node traversal */
166 promops.po_firstchild = OF_child;
167 promops.po_nextsibling = OF_peer;
168
169 /* Device node properties */
170 promops.po_getprop = OF_getprop;
171
172 /* Device discovery */
173 promops.po_finddevice = OF_finddevice;
174
175 /* Console I/O */
176 phandle = openfirmware_chosen();
177 size = _prom_getprop(phandle, "stdin", &promops.po_stdin,
178 sizeof(promops.po_stdin));
179 size += _prom_getprop(phandle, "stdout", &promops.po_stdout,
180 sizeof(promops.po_stdout));
181 if (size != (sizeof(promops.po_stdin) + sizeof(promops.po_stdout))) {
182 prom_halt();
183 }
184 }
185