promlib.c revision 1.8 1 /* $NetBSD: promlib.c,v 1.8 2006/07/13 20:03:34 uwe Exp $ */
2
3 /*-
4 * Copyright (c) 1999 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 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Specially crafted scaled-down version of promlib for the first-stage
41 * boot program.
42 *
43 * bootxx needs the follwoing PROM functions:
44 * prom_version()
45 * prom_getbootpath()
46 * prom_putchar()
47 * prom_halt()
48 * prom_open()
49 * prom_close()
50 * prom_seek()
51 * prom_read()
52 */
53
54 #include <sys/errno.h>
55 #include <sys/param.h>
56
57 #include <machine/stdarg.h>
58 #include <machine/oldmon.h>
59 #include <machine/bsd_openprom.h>
60 #include <machine/promlib.h>
61
62 #include <lib/libsa/stand.h>
63
64 #define obpvec ((struct promvec *)romp)
65
66 static void obp_v2_putchar(int);
67 static int obp_v2_seek(int, u_quad_t);
68 static const char *obp_v0_getbootpath(void);
69 static const char *obp_v2_getbootpath(void);
70
71 /*
72 * PROM entry points.
73 */
74 struct promops promops;
75
76 /*
77 * Determine whether a node has the given property.
78 */
79 void
80 prom_halt(void)
81 {
82
83 _prom_halt();
84 }
85
86
87 void
88 obp_v2_putchar(int c)
89 {
90 char c0;
91
92 c0 = (c & 0x7f);
93 (*promops.po_write)(promops.po_stdout, &c0, 1);
94 }
95
96 int
97 obp_v2_seek(int handle, u_quad_t offset)
98 {
99 uint32_t hi, lo;
100
101 lo = offset & ((uint32_t)-1);
102 hi = (offset >> 32) & ((uint32_t)-1);
103 (*obpvec->pv_v2devops.v2_seek)(handle, hi, lo);
104 return (0);
105 }
106
107 /*
108 * On SS1s (and also IPCs, SLCs), `promvec->pv_v0bootargs->ba_argv[1]'
109 * contains the flags that were given after the boot command. On SS2s
110 * (and ELCs, IPXs, etc. and any sun4m class machine), `pv_v0bootargs'
111 * is NULL but `*promvec->pv_v2bootargs.v2_bootargs' points to
112 * "netbsd -s" or whatever.
113 */
114 const char *
115 obp_v0_getbootpath(void)
116 {
117 struct v0bootargs *ba = promops.po_bootcookie;
118 return (ba->ba_argv[0]);
119 }
120
121 const char *
122 obp_v2_getbootpath(void)
123 {
124 struct v2bootargs *ba = promops.po_bootcookie;
125 return (*ba->v2_bootpath);
126 }
127
128
129 static void prom_init_oldmon(void);
130 static void prom_init_obp(void);
131
132 static inline void
133 prom_init_oldmon(void)
134 {
135 struct om_vector *oldpvec = (struct om_vector *)PROM_BASE;
136
137 promops.po_version = PROM_OLDMON;
138 promops.po_revision = oldpvec->monId[0]; /*XXX*/
139
140 promops.po_stdout = *oldpvec->outSink;
141 promops.po_putchar = oldpvec->putChar;
142 promops.po_halt = oldpvec->exitToMon;
143 promops.po_bootcookie = *oldpvec->bootParam; /* deref 1 lvl */
144 promops.po_bootpath = obp_v0_getbootpath;
145 }
146
147 static inline void
148 prom_init_obp(void)
149 {
150 /*
151 * OBP v0, v2 & v3
152 */
153 switch (obpvec->pv_romvec_vers) {
154 case 0:
155 promops.po_version = PROM_OBP_V0;
156 break;
157 case 2:
158 promops.po_version = PROM_OBP_V2;
159 break;
160 case 3:
161 promops.po_version = PROM_OBP_V3;
162 break;
163 default:
164 obpvec->pv_halt(); /* What else? */
165 }
166
167 promops.po_revision = obpvec->pv_printrev;
168 promops.po_halt = obpvec->pv_halt;
169
170 /*
171 * Next, deal with prom vector differences between versions.
172 */
173 switch (promops.po_version) {
174 case PROM_OBP_V0:
175 promops.po_stdout = *obpvec->pv_stdout;
176 promops.po_putchar = obpvec->pv_putchar;
177 promops.po_open = obpvec->pv_v0devops.v0_open;
178 promops.po_close = (void *)obpvec->pv_v0devops.v0_close;
179 promops.po_bootcookie = *obpvec->pv_v0bootargs; /* deref 1 lvl */
180 promops.po_bootpath = obp_v0_getbootpath;
181 break;
182 case PROM_OBP_V3:
183 case PROM_OBP_V2:
184 /* Deref stdio handles one level */
185 promops.po_stdout = *obpvec->pv_v2bootargs.v2_fd1;
186 promops.po_putchar = obp_v2_putchar;
187 promops.po_open = obpvec->pv_v2devops.v2_open;
188 promops.po_close = (void *)obpvec->pv_v2devops.v2_close;
189 promops.po_read = obpvec->pv_v2devops.v2_read;
190 promops.po_write = obpvec->pv_v2devops.v2_write;
191 promops.po_seek = obp_v2_seek;
192 promops.po_bootcookie = &obpvec->pv_v2bootargs;
193 promops.po_bootpath = obp_v2_getbootpath;
194 break;
195 }
196 }
197
198 /*
199 * Initialize our PROM operations vector.
200 */
201 void
202 prom_init(void)
203 {
204
205 if (CPU_ISSUN4) {
206 prom_init_oldmon();
207 romp = (caddr_t)PROM_LOADADDR; /* Used in main() */
208 } else if (obpvec->pv_magic == OBP_MAGIC) {
209 prom_init_obp();
210 } else {
211 /* No Openfirmware support */
212 }
213 }
214