1 /* $NetBSD: promlib.c,v 1.12 2024/02/07 04:02:36 msaitoh 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 *
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 * Specially crafted scaled-down version of promlib for the first-stage
34 * boot program.
35 *
36 * bootxx needs the following PROM functions:
37 * prom_version()
38 * prom_getbootpath()
39 * prom_putchar()
40 * prom_halt()
41 * prom_open()
42 * prom_close()
43 * prom_seek()
44 * prom_read()
45 */
46
47 #include <sys/errno.h>
48 #include <sys/param.h>
49
50 #include <machine/oldmon.h>
51 #include <machine/bsd_openprom.h>
52 #include <machine/promlib.h>
53
54 #include <lib/libsa/stand.h>
55
56 #define obpvec ((struct promvec *)romp)
57
58 static void obp_v2_putchar(int);
59 static int obp_v2_seek(int, u_quad_t);
60 static const char *obp_v0_getbootpath(void);
61 static const char *obp_v2_getbootpath(void);
62
63 /*
64 * PROM entry points.
65 */
66 struct promops promops;
67
68 /*
69 * Determine whether a node has the given property.
70 */
71 void
prom_halt(void)72 prom_halt(void)
73 {
74
75 _prom_halt();
76 }
77
78
79 void
obp_v2_putchar(int c)80 obp_v2_putchar(int c)
81 {
82 char c0;
83
84 c0 = (c & 0x7f);
85 (*promops.po_write)(promops.po_stdout, &c0, 1);
86 }
87
88 int
obp_v2_seek(int handle,u_quad_t offset)89 obp_v2_seek(int handle, u_quad_t offset)
90 {
91 uint32_t hi, lo;
92
93 lo = offset & ((uint32_t)-1);
94 hi = (offset >> 32) & ((uint32_t)-1);
95 (*obpvec->pv_v2devops.v2_seek)(handle, hi, lo);
96 return (0);
97 }
98
99 /*
100 * On SS1s (and also IPCs, SLCs), `promvec->pv_v0bootargs->ba_argv[1]'
101 * contains the flags that were given after the boot command. On SS2s
102 * (and ELCs, IPXs, etc. and any sun4m class machine), `pv_v0bootargs'
103 * is NULL but `*promvec->pv_v2bootargs.v2_bootargs' points to
104 * "netbsd -s" or whatever.
105 */
106 const char *
obp_v0_getbootpath(void)107 obp_v0_getbootpath(void)
108 {
109 struct v0bootargs *ba = promops.po_bootcookie;
110 return (ba->ba_argv[0]);
111 }
112
113 const char *
obp_v2_getbootpath(void)114 obp_v2_getbootpath(void)
115 {
116 struct v2bootargs *ba = promops.po_bootcookie;
117 return (*ba->v2_bootpath);
118 }
119
120
121 static void prom_init_oldmon(void);
122 static void prom_init_obp(void);
123
124 static inline void
prom_init_oldmon(void)125 prom_init_oldmon(void)
126 {
127 struct om_vector *oldpvec = (struct om_vector *)PROM_BASE;
128
129 promops.po_version = PROM_OLDMON;
130 promops.po_revision = oldpvec->monId[0]; /*XXX*/
131
132 promops.po_stdout = *oldpvec->outSink;
133 promops.po_putchar = oldpvec->putChar;
134 promops.po_halt = oldpvec->exitToMon;
135 promops.po_bootcookie = *oldpvec->bootParam; /* deref 1 lvl */
136 promops.po_bootpath = obp_v0_getbootpath;
137 }
138
139 static inline void
prom_init_obp(void)140 prom_init_obp(void)
141 {
142 /*
143 * OBP v0, v2 & v3
144 */
145 switch (obpvec->pv_romvec_vers) {
146 case 0:
147 promops.po_version = PROM_OBP_V0;
148 break;
149 case 2:
150 promops.po_version = PROM_OBP_V2;
151 break;
152 case 3:
153 promops.po_version = PROM_OBP_V3;
154 break;
155 default:
156 obpvec->pv_halt(); /* What else? */
157 }
158
159 promops.po_revision = obpvec->pv_printrev;
160 promops.po_halt = obpvec->pv_halt;
161
162 /*
163 * Next, deal with prom vector differences between versions.
164 */
165 switch (promops.po_version) {
166 case PROM_OBP_V0:
167 promops.po_stdout = *obpvec->pv_stdout;
168 promops.po_putchar = obpvec->pv_putchar;
169 promops.po_open = obpvec->pv_v0devops.v0_open;
170 promops.po_close = (void *)obpvec->pv_v0devops.v0_close;
171 promops.po_bootcookie = *obpvec->pv_v0bootargs; /* deref 1 lvl */
172 promops.po_bootpath = obp_v0_getbootpath;
173 break;
174 case PROM_OBP_V3:
175 case PROM_OBP_V2:
176 /* Deref stdio handles one level */
177 promops.po_stdout = *obpvec->pv_v2bootargs.v2_fd1;
178 promops.po_putchar = obp_v2_putchar;
179 promops.po_open = obpvec->pv_v2devops.v2_open;
180 promops.po_close = (void *)obpvec->pv_v2devops.v2_close;
181 promops.po_read = obpvec->pv_v2devops.v2_read;
182 promops.po_write = obpvec->pv_v2devops.v2_write;
183 promops.po_seek = obp_v2_seek;
184 promops.po_bootcookie = &obpvec->pv_v2bootargs;
185 promops.po_bootpath = obp_v2_getbootpath;
186 break;
187 }
188 }
189
190 /*
191 * Initialize our PROM operations vector.
192 */
193 void
prom_init(void)194 prom_init(void)
195 {
196
197 if (CPU_ISSUN4) {
198 prom_init_oldmon();
199 romp = (void *)PROM_LOADADDR; /* Used in main() */
200 } else if (obpvec->pv_magic == OBP_MAGIC) {
201 prom_init_obp();
202 } else {
203 /* No Openfirmware support */
204 }
205 }
206