1 /* $NetBSD: promlib.c,v 1.3 2011/05/21 15:50:42 tsutsui 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 <lib/libsa/stand.h>
41 #include <lib/libkern/libkern.h>
42
43 #include "openfirm.h"
44
45
46 void *romp;
47 struct promops promops;
48
49
50 static void
openfirmware_fatal(void)51 openfirmware_fatal(void)
52 {
53 printf("Invalid Openfirmware environment\n");
54 exit(0);
55 }
56
57 static int
openfirmware_chosen(void)58 openfirmware_chosen(void)
59 {
60 static int phandle = -1;
61
62 if (phandle == -1) {
63 if ( (phandle = OF_finddevice("/chosen")) == -1) {
64 exit(0);
65 }
66 }
67
68 return (phandle);
69 }
70
71 static const char*
openfirmware_bootpath(void)72 openfirmware_bootpath(void)
73 {
74 static char bootpath[PROM_MAX_PATH];
75
76 if (_prom_getprop(openfirmware_chosen(), "bootpath", bootpath,
77 sizeof(bootpath)) < 0) {
78 openfirmware_fatal();
79 }
80
81 return bootpath;
82 }
83
84 static const char*
openfirmware_bootfile(void)85 openfirmware_bootfile(void)
86 {
87 /* Default image name */
88 return "netbsd";
89 }
90
91 static const char*
openfirmware_bootargs(void)92 openfirmware_bootargs(void)
93 {
94 static char bootargs[PROM_MAX_PATH * 2];
95
96 if (_prom_getprop(openfirmware_chosen(), "bootargs", bootargs,
97 sizeof(bootargs)) < 0) {
98 openfirmware_fatal();
99 }
100
101 return bootargs;
102 }
103
104 static int
openfirmware_getchar(void)105 openfirmware_getchar(void)
106 {
107 unsigned char ch = '\0';
108 int l;
109
110 while ((l = OF_read(prom_stdin(), &ch, 1)) != 1)
111 if (l != -2 && l != 0)
112 return -1;
113 return ch;
114 }
115
116 static void
openfirmware_putchar(int c)117 openfirmware_putchar(int c)
118 {
119 char ch = c;
120
121 if (c == '\n')
122 putchar('\r');
123 OF_write(prom_stdout(), &ch, 1);
124 }
125
126 void
prom_halt(void)127 prom_halt(void)
128 {
129 _prom_halt();
130 }
131
132 int
prom_findroot(void)133 prom_findroot(void)
134 {
135 return OF_peer(0);
136 }
137
138 void
prom_init(void)139 prom_init(void)
140 {
141 int phandle, size;
142
143 OF_initialize();
144
145 memset(&promops, 0, sizeof(promops));
146
147 /* Access to boot arguments */
148 promops.po_bootpath = openfirmware_bootpath;
149 promops.po_bootfile = openfirmware_bootfile;
150 promops.po_bootargs = openfirmware_bootargs;
151
152 /* I/O functions */
153 promops.po_getchar = openfirmware_getchar;
154 promops.po_putchar = openfirmware_putchar;
155 promops.po_open = OF_open;
156 promops.po_close = OF_close;
157 promops.po_read = OF_read;
158 promops.po_write = OF_write;
159 promops.po_seek = OF_seek;
160
161 promops.po_instance_to_package = OF_instance_to_package;
162
163 /* Program termination control */
164 promops.po_halt = OF_exit;
165 promops.po_abort = OF_enter;
166 promops.po_ticks = OF_milliseconds;
167
168 /* Device node traversal */
169 promops.po_firstchild = OF_child;
170 promops.po_nextsibling = OF_peer;
171
172 /* Device node properties */
173 promops.po_getprop = OF_getprop;
174
175 /* Device discovery */
176 promops.po_finddevice = OF_finddevice;
177
178 /* Console I/O */
179 phandle = openfirmware_chosen();
180 size = _prom_getprop(phandle, "stdin", &promops.po_stdin,
181 sizeof(promops.po_stdin));
182 size += _prom_getprop(phandle, "stdout", &promops.po_stdout,
183 sizeof(promops.po_stdout));
184 if (size != (sizeof(promops.po_stdin) + sizeof(promops.po_stdout))) {
185 prom_halt();
186 }
187 }
188