Print this page
NEX-16819 loader UEFI support
Includes work by Toomas Soome <tsoome@me.com>
Upstream commits:
loader: pxe receive cleanup
9475 libefi: Do not return only if ReceiveFilter
installboot: should support efi system partition
8931 boot1.efi: scan all display modes rather than
loader: spinconsole updates
loader: gfx experiment to try GOP Blt() function.
sha1 build test
loader: add sha1 hash calculation
common/sha1: update for loader build
loader: biosdisk rework
uts: 32-bit kernel FB needs mapping in low memory
uts: add diag-device
uts: boot console mirror with diag-device
uts: enable very early console on ttya
kmdb: add diag-device as input/output device
uts: test VGA memory exclusion from mapping
uts: clear boot mapping and protect boot pages test
uts: add dboot map debug printf
uts: need to release FB pages in release_bootstrap()
uts: add screenmap ioctl
uts: update sys/queue.h
loader: add illumos uts/common to include path
loader: tem/gfx font cleanup
loader: vbe checks
uts: gfx_private set KD_TEXT when KD_RESETTEXT is
uts: gfx 8-bit update
loader: gfx 8-bit fix
loader: always set media size from partition.
uts: MB2 support for 32-bit kernel
loader: x86 should have tem 80x25
uts: x86 should have tem 80x25
uts: font update
loader: font update
uts: tem attributes
loader: tem.c comment added
uts: use font module
loader: add font module
loader: build rules for new font setup
uts: gfx_private update for new font structure
uts: early boot update for new font structure
uts: font update
uts: font build rules update for new fonts
uts: tem update to new font structure
loader: module.c needs to include tem_impl.h
uts: gfx_private 8x16 font rework
uts: make font_lookup public
loader: font rework
uts: font rework
9259 libefi: efi_alloc_and_read should check for PMBR
uts: tem utf-8 support
loader: implement tem utf-8 support
loader: tem should be able to display UTF-8
7784 uts: console input should support utf-8
7796 uts: ldterm default to utf-8
uts: do not reset serial console
uts: set up colors even if tem is not console
uts: add type for early boot properties
uts: gfx_private experiment with drm and vga
uts: gfx_private should use setmode drm callback.
uts: identify FB types and set up gfx_private based
loader: replace gop and vesa with framebuffer
uts: boot needs simple tem to support mdb
uts: boot_keyboard should emit esc sequences for
uts: gfx_private FB showuld be written by line
kmdb: set terminal window size
uts: gfx_private needs to keep track of early boot FB
pnglite: move pnglite to usr/src/common
loader: gfx_fb
ficl-sys: add gfx primitives
loader: add illumos.png logo
ficl: add fb-putimage
loader: add png support
loader: add alpha blending for gfx_fb
loader: use term-drawrect for menu frame
ficl: add simple gfx words
uts: provide fb_info via fbgattr dev_specific array.
uts: gfx_private add alpha blending
uts: update sys/ascii.h
uts: tem OSC support (incomplete)
uts: implement env module support and use data from
uts: tem get colors from early boot data
loader: use crc32 from libstand (libz)
loader: optimize for size
loader: pass tem info to the environment
loader: import tem for loader console
loader: UEFI loader needs to set ISADIR based on
loader: need UEFI32 support
8918 loader.efi: add vesa edid support
uts: tem_safe_pix_clear_prom_output() should only
uts: tem_safe_pix_clear_entire_screen() should use
uts: tem_safe_check_first_time() should query cursor
uts: tem implement cls callback & visual_io v4
uts: gfx_vgatext use block cursor for vgatext
uts: gfx_private implement cls callback & visual_io
uts: gfx_private bitmap framebuffer implementation
uts: early start frame buffer console support
uts: font functions should check the input char
uts: font rendering should support 16/24/32bit depths
uts: use smallest font as fallback default.
uts: update terminal dimensions based on selected
7834 uts: vgatext should use gfx_private
uts: add spacing property to 8859-1.bdf
terminfo: add underline for sun-color
terminfo: sun-color has 16 colors
uts: add font load callback type
loader: do not repeat int13 calls with error 0x20 and
8905 loader: add skein/edonr support
8904 common/crypto: make skein and edonr loader
Reviewed by: Yuri Pankov <yuri.pankov@nexenta.com>
Reviewed by: Sanjay Nadkarni <sanjay.nadkarni@nexenta.com>
Reviewed by: Evan Layton <evan.layton@nexenta.com>
Revert "NEX-16819 loader UEFI support"
This reverts commit ec06b9fc617b99234e538bf2e7e4d02a24993e0c.
Reverting due to failures in the zfs-tests and the sharefs-tests
NEX-16819 loader UEFI support
Includes work by Toomas Soome <tsoome@me.com>
Upstream commits:
loader: pxe receive cleanup
9475 libefi: Do not return only if ReceiveFilter
installboot: should support efi system partition
8931 boot1.efi: scan all display modes rather than
loader: spinconsole updates
loader: gfx experiment to try GOP Blt() function.
sha1 build test
loader: add sha1 hash calculation
common/sha1: update for loader build
loader: biosdisk rework
uts: 32-bit kernel FB needs mapping in low memory
uts: add diag-device
uts: boot console mirror with diag-device
uts: enable very early console on ttya
kmdb: add diag-device as input/output device
uts: test VGA memory exclusion from mapping
uts: clear boot mapping and protect boot pages test
uts: add dboot map debug printf
uts: need to release FB pages in release_bootstrap()
uts: add screenmap ioctl
uts: update sys/queue.h
loader: add illumos uts/common to include path
loader: tem/gfx font cleanup
loader: vbe checks
uts: gfx_private set KD_TEXT when KD_RESETTEXT is
uts: gfx 8-bit update
loader: gfx 8-bit fix
loader: always set media size from partition.
uts: MB2 support for 32-bit kernel
loader: x86 should have tem 80x25
uts: x86 should have tem 80x25
uts: font update
loader: font update
uts: tem attributes
loader: tem.c comment added
uts: use font module
loader: add font module
loader: build rules for new font setup
uts: gfx_private update for new font structure
uts: early boot update for new font structure
uts: font update
uts: font build rules update for new fonts
uts: tem update to new font structure
loader: module.c needs to include tem_impl.h
uts: gfx_private 8x16 font rework
uts: make font_lookup public
loader: font rework
uts: font rework
libefi: efi_alloc_and_read should check for PMBR
uts: tem utf-8 support
loader: implement tem utf-8 support
loader: tem should be able to display UTF-8
7784 uts: console input should support utf-8
7796 uts: ldterm default to utf-8
uts: do not reset serial console
uts: set up colors even if tem is not console
uts: add type for early boot properties
uts: gfx_private experiment with drm and vga
uts: gfx_private should use setmode drm callback.
uts: identify FB types and set up gfx_private based
loader: replace gop and vesa with framebuffer
uts: boot needs simple tem to support mdb
uts: boot_keyboard should emit esc sequences for
uts: gfx_private FB showuld be written by line
kmdb: set terminal window size
uts: gfx_private needs to keep track of early boot FB
pnglite: move pnglite to usr/src/common
loader: gfx_fb
ficl-sys: add gfx primitives
loader: add illumos.png logo
ficl: add fb-putimage
loader: add png support
loader: add alpha blending for gfx_fb
loader: use term-drawrect for menu frame
ficl: add simple gfx words
uts: provide fb_info via fbgattr dev_specific array.
uts: gfx_private add alpha blending
uts: update sys/ascii.h
uts: tem OSC support (incomplete)
uts: implement env module support and use data from
uts: tem get colors from early boot data
loader: use crc32 from libstand (libz)
loader: optimize for size
loader: pass tem info to the environment
loader: import tem for loader console
loader: UEFI loader needs to set ISADIR based on
loader: need UEFI32 support
8918 loader.efi: add vesa edid support
uts: tem_safe_pix_clear_prom_output() should only
uts: tem_safe_pix_clear_entire_screen() should use
uts: tem_safe_check_first_time() should query cursor
uts: tem implement cls callback & visual_io v4
uts: gfx_vgatext use block cursor for vgatext
uts: gfx_private implement cls callback & visual_io
uts: gfx_private bitmap framebuffer implementation
uts: early start frame buffer console support
uts: font functions should check the input char
uts: font rendering should support 16/24/32bit depths
uts: use smallest font as fallback default.
uts: update terminal dimensions based on selected
7834 uts: vgatext should use gfx_private
uts: add spacing property to 8859-1.bdf
terminfo: add underline for sun-color
terminfo: sun-color has 16 colors
uts: add font load callback type
loader: do not repeat int13 calls with error 0x20 and
8905 loader: add skein/edonr support
8904 common/crypto: make skein and edonr loader
Reviewed by: Yuri Pankov <yuri.pankov@nexenta.com>
Reviewed by: Sanjay Nadkarni <sanjay.nadkarni@nexenta.com>
Reviewed by: Evan Layton <evan.layton@nexenta.com>
| Split |
Close |
| Expand all |
| Collapse all |
--- old/usr/src/common/ficl/emu/loader_emu.c
+++ new/usr/src/common/ficl/emu/loader_emu.c
1 1 /*
2 2 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3 3 * All rights reserved.
4 4 *
5 5 * Redistribution and use in source and binary forms, with or without
6 6 * modification, are permitted provided that the following conditions
7 7 * are met:
8 8 * 1. Redistributions of source code must retain the above copyright
9 9 * notice, this list of conditions and the following disclaimer.
10 10 * 2. Redistributions in binary form must reproduce the above copyright
11 11 * notice, this list of conditions and the following disclaimer in the
12 12 * documentation and/or other materials provided with the distribution.
13 13 *
14 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 24 * SUCH DAMAGE.
25 25 */
26 26
27 27 #include <sys/types.h>
28 28 #include <sys/stat.h>
29 29 #include <fcntl.h>
30 30 #include <errno.h>
31 31 #include <stdlib.h>
|
↓ open down ↓ |
31 lines elided |
↑ open up ↑ |
32 32 #include <stdio.h>
33 33 #include <string.h>
34 34 #include <strings.h>
35 35 #include <limits.h>
36 36 #include <unistd.h>
37 37 #include <dirent.h>
38 38 #include <macros.h>
39 39 #include <sys/systeminfo.h>
40 40 #include <sys/queue.h>
41 41 #include <sys/mnttab.h>
42 +#include "gfx_fb.h"
42 43 #include "ficl.h"
43 44
44 45 /* Commands and return values; nonzero return sets command_errmsg != NULL */
45 46 typedef int (bootblk_cmd_t)(int argc, char *argv[]);
46 47 #define CMD_OK 0
47 48 #define CMD_ERROR 1
48 49
49 50 /*
50 51 * Support for commands
51 52 */
52 53 struct bootblk_command
53 54 {
54 55 const char *c_name;
55 56 const char *c_desc;
56 57 bootblk_cmd_t *c_fn;
57 58 STAILQ_ENTRY(bootblk_command) next;
58 59 };
59 60
60 61 #define MDIR_REMOVED 0x0001
61 62 #define MDIR_NOHINTS 0x0002
62 63
63 64 struct moduledir {
64 65 char *d_path; /* path of modules directory */
65 66 uchar_t *d_hints; /* content of linker.hints file */
66 67 int d_hintsz; /* size of hints data */
67 68 int d_flags;
68 69 STAILQ_ENTRY(moduledir) d_link;
69 70 };
70 71 static STAILQ_HEAD(, moduledir) moduledir_list =
71 72 STAILQ_HEAD_INITIALIZER(moduledir_list);
72 73
73 74 static const char *default_searchpath = "/platform/i86pc";
74 75
75 76 static char typestr[] = "?fc?d?b? ?l?s?w";
76 77 static int ls_getdir(char **pathp);
77 78 extern char **_environ;
78 79
79 80 char *command_errmsg;
80 81 char command_errbuf[256];
81 82
82 83 extern void pager_open(void);
83 84 extern void pager_close(void);
84 85 extern int pager_output(const char *);
85 86 extern int pager_file(const char *);
86 87 static int page_file(char *);
87 88 static int include(const char *);
88 89
89 90 static int command_help(int argc, char *argv[]);
90 91 static int command_commandlist(int argc, char *argv[]);
91 92 static int command_show(int argc, char *argv[]);
92 93 static int command_set(int argc, char *argv[]);
93 94 static int command_setprop(int argc, char *argv[]);
94 95 static int command_unset(int argc, char *argv[]);
|
↓ open down ↓ |
43 lines elided |
↑ open up ↑ |
95 96 static int command_echo(int argc, char *argv[]);
96 97 static int command_read(int argc, char *argv[]);
97 98 static int command_more(int argc, char *argv[]);
98 99 static int command_ls(int argc, char *argv[]);
99 100 static int command_include(int argc, char *argv[]);
100 101 static int command_autoboot(int argc, char *argv[]);
101 102 static int command_boot(int argc, char *argv[]);
102 103 static int command_unload(int argc, char *argv[]);
103 104 static int command_load(int argc, char *argv[]);
104 105 static int command_reboot(int argc, char *argv[]);
106 +static int command_framebuffer(int argc, char *argv[]);
105 107
106 108 #define BF_PARSE 100
107 109 #define BF_DICTSIZE 30000
108 110
109 111 /* update when loader version will change */
110 112 static const char bootprog_rev[] = "1.1";
111 113 STAILQ_HEAD(cmdh, bootblk_command) commands;
112 114
113 115 /*
114 116 * BootForth Interface to Ficl Forth interpreter.
115 117 */
116 118
117 119 ficlSystem *bf_sys;
118 120 ficlVm *bf_vm;
119 121
120 122 /*
121 123 * Redistribution and use in source and binary forms, with or without
122 124 * modification, are permitted provided that the following conditions
123 125 * are met:
124 126 * 1. Redistributions of source code must retain the above copyright
125 127 * notice, this list of conditions and the following disclaimer.
126 128 * 2. Redistributions in binary form must reproduce the above copyright
127 129 * notice, this list of conditions and the following disclaimer in the
128 130 * documentation and/or other materials provided with the distribution.
129 131 *
130 132 * Jordan K. Hubbard
131 133 * 29 August 1998
132 134 *
133 135 * The meat of the simple parser.
134 136 */
135 137
136 138 static void clean(void);
137 139 static int insert(int *argcp, char *buf);
138 140
139 141 #define PARSE_BUFSIZE 1024 /* maximum size of one element */
140 142 #define MAXARGS 20 /* maximum number of elements */
141 143 static char *args[MAXARGS];
142 144
143 145 #define DIGIT(x) \
144 146 (isdigit(x) ? (x) - '0' : islower(x) ? (x) + 10 - 'a' : (x) + 10 - 'A')
145 147
146 148 /*
147 149 * backslash: Return malloc'd copy of str with all standard "backslash
148 150 * processing" done on it. Original can be free'd if desired.
149 151 */
150 152 char *
151 153 backslash(char *str)
152 154 {
153 155 /*
154 156 * Remove backslashes from the strings. Turn \040 etc. into a single
155 157 * character (we allow eight bit values). Currently NUL is not
156 158 * allowed.
157 159 *
158 160 * Turn "\n" and "\t" into '\n' and '\t' characters. Etc.
159 161 */
160 162 char *new_str;
161 163 int seenbs = 0;
162 164 int i = 0;
163 165
164 166 if ((new_str = strdup(str)) == NULL)
165 167 return (NULL);
166 168
167 169 while (*str) {
168 170 if (seenbs) {
169 171 seenbs = 0;
170 172 switch (*str) {
171 173 case '\\':
172 174 new_str[i++] = '\\';
173 175 str++;
174 176 break;
175 177
176 178 /* preserve backslashed quotes, dollar signs */
177 179 case '\'':
178 180 case '"':
179 181 case '$':
180 182 new_str[i++] = '\\';
181 183 new_str[i++] = *str++;
182 184 break;
183 185
184 186 case 'b':
185 187 new_str[i++] = '\b';
186 188 str++;
187 189 break;
188 190
189 191 case 'f':
190 192 new_str[i++] = '\f';
191 193 str++;
192 194 break;
193 195
194 196 case 'r':
195 197 new_str[i++] = '\r';
196 198 str++;
197 199 break;
198 200
199 201 case 'n':
200 202 new_str[i++] = '\n';
201 203 str++;
202 204 break;
203 205
204 206 case 's':
205 207 new_str[i++] = ' ';
206 208 str++;
207 209 break;
208 210
209 211 case 't':
210 212 new_str[i++] = '\t';
211 213 str++;
212 214 break;
213 215
214 216 case 'v':
215 217 new_str[i++] = '\13';
216 218 str++;
217 219 break;
218 220
219 221 case 'z':
220 222 str++;
221 223 break;
222 224
223 225 case '0': case '1': case '2': case '3': case '4':
224 226 case '5': case '6': case '7': case '8': case '9': {
225 227 char val;
226 228
227 229 /* Three digit octal constant? */
228 230 if (*str >= '0' && *str <= '3' &&
229 231 *(str + 1) >= '0' && *(str + 1) <= '7' &&
230 232 *(str + 2) >= '0' && *(str + 2) <= '7') {
231 233
232 234 val = (DIGIT(*str) << 6) +
233 235 (DIGIT(*(str + 1)) << 3) +
234 236 DIGIT(*(str + 2));
235 237
236 238 /*
237 239 * Allow null value if user really
238 240 * wants to shoot at feet, but beware!
239 241 */
240 242 new_str[i++] = val;
241 243 str += 3;
242 244 break;
243 245 }
244 246
245 247 /*
246 248 * One or two digit hex constant?
247 249 * If two are there they will both be taken.
248 250 * Use \z to split them up if this is not
249 251 * wanted.
250 252 */
251 253 if (*str == '0' &&
252 254 (*(str + 1) == 'x' || *(str + 1) == 'X') &&
253 255 isxdigit(*(str + 2))) {
254 256 val = DIGIT(*(str + 2));
255 257 if (isxdigit(*(str + 3))) {
256 258 val = (val << 4) +
257 259 DIGIT(*(str + 3));
258 260 str += 4;
259 261 } else
260 262 str += 3;
261 263 /* Yep, allow null value here too */
262 264 new_str[i++] = val;
263 265 break;
264 266 }
265 267 }
266 268 break;
267 269
268 270 default:
269 271 new_str[i++] = *str++;
270 272 break;
271 273 }
272 274 } else {
273 275 if (*str == '\\') {
274 276 seenbs = 1;
275 277 str++;
276 278 } else
277 279 new_str[i++] = *str++;
278 280 }
279 281 }
280 282
281 283 if (seenbs) {
282 284 /*
283 285 * The final character was a '\'.
284 286 * Put it in as a single backslash.
285 287 */
286 288 new_str[i++] = '\\';
287 289 }
288 290 new_str[i] = '\0';
289 291 return (new_str);
290 292 }
291 293
292 294 /*
293 295 * parse: accept a string of input and "parse" it for backslash
294 296 * substitutions and environment variable expansions (${var}),
295 297 * returning an argc/argv style vector of whitespace separated
296 298 * arguments. Returns 0 on success, 1 on failure (ok, ok, so I
297 299 * wimped-out on the error codes! :).
298 300 *
299 301 * Note that the argv array returned must be freed by the caller, but
300 302 * we own the space allocated for arguments and will free that on next
301 303 * invocation. This allows argv consumers to modify the array if
302 304 * required.
303 305 *
304 306 * NB: environment variables that expand to more than one whitespace
305 307 * separated token will be returned as a single argv[] element, not
306 308 * split in turn. Expanded text is also immune to further backslash
307 309 * elimination or expansion since this is a one-pass, non-recursive
308 310 * parser. You didn't specify more than this so if you want more, ask
309 311 * me. - jkh
310 312 */
311 313
312 314 #define PARSE_FAIL(expr) \
313 315 if (expr) { \
314 316 printf("fail at line %d\n", __LINE__); \
315 317 clean(); \
316 318 free(copy); \
317 319 free(buf); \
318 320 return (1); \
319 321 }
320 322
321 323 /* Accept the usual delimiters for a variable, returning counterpart */
322 324 static char
323 325 isdelim(int ch)
324 326 {
325 327 if (ch == '{')
326 328 return ('}');
327 329 else if (ch == '(')
328 330 return (')');
329 331 return ('\0');
330 332 }
331 333
332 334 static int
333 335 isquote(int ch)
334 336 {
335 337 return (ch == '\'');
336 338 }
337 339
338 340 static int
339 341 isdquote(int ch)
340 342 {
341 343 return (ch == '"');
342 344 }
343 345
344 346 int
345 347 parse(int *argc, char ***argv, char *str)
346 348 {
347 349 int ac;
348 350 char *val, *p, *q, *copy = NULL;
349 351 size_t i = 0;
350 352 char token, tmp, quote, dquote, *buf;
351 353 enum { STR, VAR, WHITE } state;
352 354
353 355 ac = *argc = 0;
354 356 dquote = quote = 0;
355 357 if (!str || (p = copy = backslash(str)) == NULL)
356 358 return (1);
357 359
358 360 /* Initialize vector and state */
359 361 clean();
360 362 state = STR;
361 363 buf = (char *)malloc(PARSE_BUFSIZE);
362 364 token = 0;
363 365
364 366 /* And awaaaaaaaaay we go! */
365 367 while (*p) {
366 368 switch (state) {
367 369 case STR:
368 370 if ((*p == '\\') && p[1]) {
369 371 p++;
370 372 PARSE_FAIL(i == (PARSE_BUFSIZE - 1));
371 373 buf[i++] = *p++;
372 374 } else if (isquote(*p)) {
373 375 quote = quote ? 0 : *p;
374 376 if (dquote) { /* keep quote */
375 377 PARSE_FAIL(i == (PARSE_BUFSIZE - 1));
376 378 buf[i++] = *p++;
377 379 } else
378 380 ++p;
379 381 } else if (isdquote(*p)) {
380 382 dquote = dquote ? 0 : *p;
381 383 if (quote) { /* keep dquote */
382 384 PARSE_FAIL(i == (PARSE_BUFSIZE - 1));
383 385 buf[i++] = *p++;
384 386 } else
385 387 ++p;
386 388 } else if (isspace(*p) && !quote && !dquote) {
387 389 state = WHITE;
388 390 if (i) {
389 391 buf[i] = '\0';
390 392 PARSE_FAIL(insert(&ac, buf));
391 393 i = 0;
392 394 }
393 395 ++p;
394 396 } else if (*p == '$' && !quote) {
395 397 token = isdelim(*(p + 1));
396 398 if (token)
397 399 p += 2;
398 400 else
399 401 ++p;
400 402 state = VAR;
401 403 } else {
402 404 PARSE_FAIL(i == (PARSE_BUFSIZE - 1));
403 405 buf[i++] = *p++;
404 406 }
405 407 break;
406 408
407 409 case WHITE:
408 410 if (isspace(*p))
409 411 ++p;
410 412 else
411 413 state = STR;
412 414 break;
413 415
414 416 case VAR:
415 417 if (token) {
416 418 PARSE_FAIL((q = strchr(p, token)) == NULL);
417 419 } else {
418 420 q = p;
419 421 while (*q && !isspace(*q))
420 422 ++q;
421 423 }
422 424 tmp = *q;
423 425 *q = '\0';
424 426 if ((val = getenv(p)) != NULL) {
425 427 size_t len = strlen(val);
426 428
427 429 strncpy(buf + i, val, PARSE_BUFSIZE - (i + 1));
428 430 i += min(len, PARSE_BUFSIZE - 1);
429 431 }
430 432 *q = tmp; /* restore value */
431 433 p = q + (token ? 1 : 0);
432 434 state = STR;
433 435 break;
434 436 }
435 437 }
436 438 /* missing terminating ' or " */
437 439 PARSE_FAIL(quote || dquote);
438 440 /* If at end of token, add it */
439 441 if (i && state == STR) {
440 442 buf[i] = '\0';
441 443 PARSE_FAIL(insert(&ac, buf));
442 444 }
443 445 args[ac] = NULL;
444 446 *argc = ac;
445 447 *argv = (char **)malloc((sizeof (char *) * ac + 1));
446 448 bcopy(args, *argv, sizeof (char *) * ac + 1);
447 449 free(buf);
448 450 free(copy);
449 451 return (0);
450 452 }
451 453
452 454 #define MAXARGS 20
453 455
454 456 /* Clean vector space */
455 457 static void
456 458 clean(void)
457 459 {
458 460 int i;
459 461
460 462 for (i = 0; i < MAXARGS; i++) {
461 463 if (args[i] != NULL) {
462 464 free(args[i]);
463 465 args[i] = NULL;
464 466 }
465 467 }
466 468 }
467 469
468 470 static int
469 471 insert(int *argcp, char *buf)
470 472 {
471 473 if (*argcp >= MAXARGS)
472 474 return (1);
473 475 args[(*argcp)++] = strdup(buf);
474 476 return (0);
475 477 }
476 478
477 479 static char *
478 480 isadir(void)
479 481 {
480 482 char *buf;
481 483 size_t bufsize = 20;
482 484 int ret;
483 485
484 486 if ((buf = malloc(bufsize)) == NULL)
485 487 return (NULL);
486 488 ret = sysinfo(SI_ARCHITECTURE_K, buf, bufsize);
487 489 if (ret == -1) {
488 490 free(buf);
489 491 return (NULL);
490 492 }
491 493 return (buf);
492 494 }
493 495
494 496 /*
495 497 * Shim for taking commands from BF and passing them out to 'standard'
496 498 * argv/argc command functions.
497 499 */
498 500 static void
499 501 bf_command(ficlVm *vm)
500 502 {
501 503 char *name, *line, *tail, *cp;
502 504 size_t len;
503 505 struct bootblk_command *cmdp;
504 506 bootblk_cmd_t *cmd;
505 507 int nstrings, i;
506 508 int argc, result;
507 509 char **argv;
508 510
509 511 /* Get the name of the current word */
510 512 name = vm->runningWord->name;
511 513
512 514 /* Find our command structure */
513 515 cmd = NULL;
514 516 STAILQ_FOREACH(cmdp, &commands, next) {
515 517 if ((cmdp->c_name != NULL) && strcmp(name, cmdp->c_name) == 0)
516 518 cmd = cmdp->c_fn;
517 519 }
518 520 if (cmd == NULL)
519 521 printf("callout for unknown command '%s'\n", name);
520 522
521 523 /* Check whether we have been compiled or are being interpreted */
522 524 if (ficlStackPopInteger(ficlVmGetDataStack(vm))) {
523 525 /*
524 526 * Get parameters from stack, in the format:
525 527 * an un ... a2 u2 a1 u1 n --
526 528 * Where n is the number of strings, a/u are pairs of
527 529 * address/size for strings, and they will be concatenated
528 530 * in LIFO order.
529 531 */
530 532 nstrings = ficlStackPopInteger(ficlVmGetDataStack(vm));
531 533 for (i = 0, len = 0; i < nstrings; i++)
532 534 len += ficlStackFetch(ficlVmGetDataStack(vm), i * 2).i + 1;
533 535 line = malloc(strlen(name) + len + 1);
534 536 strcpy(line, name);
535 537
536 538 if (nstrings)
537 539 for (i = 0; i < nstrings; i++) {
538 540 len = ficlStackPopInteger(
539 541 ficlVmGetDataStack(vm));
540 542 cp = ficlStackPopPointer(
541 543 ficlVmGetDataStack(vm));
542 544 strcat(line, " ");
543 545 strncat(line, cp, len);
544 546 }
545 547 } else {
546 548 /* Get remainder of invocation */
547 549 tail = ficlVmGetInBuf(vm);
548 550 for (cp = tail, len = 0;
549 551 cp != vm->tib.end && *cp != 0 && *cp != '\n'; cp++, len++)
550 552 ;
551 553
552 554 line = malloc(strlen(name) + len + 2);
553 555 strcpy(line, name);
554 556 if (len > 0) {
555 557 strcat(line, " ");
556 558 strncat(line, tail, len);
557 559 ficlVmUpdateTib(vm, tail + len);
558 560 }
559 561 }
560 562
561 563 command_errmsg = command_errbuf;
562 564 command_errbuf[0] = 0;
563 565 if (!parse(&argc, &argv, line)) {
564 566 result = (cmd)(argc, argv);
565 567 free(argv);
566 568 } else {
567 569 result = BF_PARSE;
568 570 }
569 571 free(line);
570 572 /*
571 573 * If there was error during nested ficlExec(), we may no longer have
572 574 * valid environment to return. Throw all exceptions from here.
573 575 */
574 576 if (result != 0)
575 577 ficlVmThrow(vm, result);
576 578 /* This is going to be thrown!!! */
577 579 ficlStackPushInteger(ficlVmGetDataStack(vm), result);
578 580 }
579 581
580 582 static char *
581 583 get_currdev(void)
582 584 {
583 585 int ret;
584 586 char *currdev;
585 587 FILE *fp;
586 588 struct mnttab mpref = {0};
587 589 struct mnttab mp = {0};
588 590
589 591 mpref.mnt_mountp = "/";
590 592 fp = fopen(MNTTAB, "r");
591 593
592 594 /* do the best we can to return something... */
593 595 if (fp == NULL)
594 596 return (strdup(":"));
595 597
596 598 ret = getmntany(fp, &mp, &mpref);
597 599 (void) fclose(fp);
598 600 if (ret == 0)
599 601 (void) asprintf(&currdev, "zfs:%s:", mp.mnt_special);
600 602 else
601 603 return (strdup(":"));
602 604
603 605 return (currdev);
604 606 }
605 607
606 608 /*
607 609 * Replace a word definition (a builtin command) with another
608 610 * one that:
609 611 *
610 612 * - Throw error results instead of returning them on the stack
611 613 * - Pass a flag indicating whether the word was compiled or is
612 614 * being interpreted.
613 615 *
614 616 * There is one major problem with builtins that cannot be overcome
615 617 * in anyway, except by outlawing it. We want builtins to behave
616 618 * differently depending on whether they have been compiled or they
617 619 * are being interpreted. Notice that this is *not* the interpreter's
618 620 * current state. For example:
619 621 *
620 622 * : example ls ; immediate
621 623 * : problem example ; \ "ls" gets executed while compiling
622 624 * example \ "ls" gets executed while interpreting
623 625 *
624 626 * Notice that, though the current state is different in the two
625 627 * invocations of "example", in both cases "ls" has been
626 628 * *compiled in*, which is what we really want.
627 629 *
628 630 * The problem arises when you tick the builtin. For example:
629 631 *
630 632 * : example-1 ['] ls postpone literal ; immediate
631 633 * : example-2 example-1 execute ; immediate
632 634 * : problem example-2 ;
633 635 * example-2
634 636 *
635 637 * We have no way, when we get EXECUTEd, of knowing what our behavior
636 638 * should be. Thus, our only alternative is to "outlaw" this. See RFI
637 639 * 0007, and ANS Forth Standard's appendix D, item 6.7 for a related
638 640 * problem, concerning compile semantics.
639 641 *
640 642 * The problem is compounded by the fact that "' builtin CATCH" is valid
641 643 * and desirable. The only solution is to create an intermediary word.
642 644 * For example:
643 645 *
644 646 * : my-ls ls ;
645 647 * : example ['] my-ls catch ;
646 648 *
647 649 * So, with the below implementation, here is a summary of the behavior
648 650 * of builtins:
649 651 *
650 652 * ls -l \ "interpret" behavior, ie,
651 653 * \ takes parameters from TIB
652 654 * : ex-1 s" -l" 1 ls ; \ "compile" behavior, ie,
653 655 * \ takes parameters from the stack
654 656 * : ex-2 ['] ls catch ; immediate \ undefined behavior
655 657 * : ex-3 ['] ls catch ; \ undefined behavior
656 658 * ex-2 ex-3 \ "interpret" behavior,
657 659 * \ catch works
658 660 * : ex-4 ex-2 ; \ "compile" behavior,
659 661 * \ catch does not work
660 662 * : ex-5 ex-3 ; immediate \ same as ex-2
661 663 * : ex-6 ex-3 ; \ same as ex-3
662 664 * : ex-7 ['] ex-1 catch ; \ "compile" behavior,
663 665 * \ catch works
664 666 * : ex-8 postpone ls ; immediate \ same as ex-2
665 667 * : ex-9 postpone ls ; \ same as ex-3
666 668 *
667 669 * As the definition below is particularly tricky, and it's side effects
668 670 * must be well understood by those playing with it, I'll be heavy on
669 671 * the comments.
670 672 *
671 673 * (if you edit this definition, pay attention to trailing spaces after
672 674 * each word -- I warned you! :-) )
673 675 */
674 676 #define BUILTIN_CONSTRUCTOR \
675 677 ": builtin: " \
676 678 ">in @ " /* save the tib index pointer */ \
677 679 "' " /* get next word's xt */ \
678 680 "swap >in ! " /* point again to next word */ \
679 681 "create " /* create a new definition of the next word */ \
680 682 ", " /* save previous definition's xt */ \
681 683 "immediate " /* make the new definition an immediate word */ \
682 684 \
683 685 "does> " /* Now, the *new* definition will: */ \
684 686 "state @ if " /* if in compiling state: */ \
685 687 "1 postpone literal " /* pass 1 flag to indicate compile */ \
686 688 "@ compile, " /* compile in previous definition */ \
687 689 "postpone throw " /* throw stack-returned result */ \
688 690 "else " /* if in interpreting state: */ \
689 691 "0 swap " /* pass 0 flag to indicate interpret */ \
690 692 "@ execute " /* call previous definition */ \
691 693 "throw " /* throw stack-returned result */ \
692 694 "then ; "
693 695
694 696 extern int ficlExecFD(ficlVm *, int);
695 697 #define COMMAND_SET(ptr, name, desc, fn) \
696 698 ptr = malloc(sizeof (struct bootblk_command)); \
697 699 ptr->c_name = (name); \
698 700 ptr->c_desc = (desc); \
699 701 ptr->c_fn = (fn);
700 702
701 703 /*
702 704 * Initialise the Forth interpreter, create all our commands as words.
703 705 */
704 706 ficlVm *
705 707 bf_init(const char *rc, ficlOutputFunction out)
706 708 {
707 709 struct bootblk_command *cmdp;
708 710 char create_buf[41]; /* 31 characters-long builtins */
709 711 char *buf;
710 712 int fd, rv;
711 713 ficlSystemInformation *fsi;
712 714 ficlDictionary *dict;
713 715 ficlDictionary *env;
714 716
715 717 /* set up commands list */
716 718 STAILQ_INIT(&commands);
717 719 COMMAND_SET(cmdp, "help", "detailed help", command_help);
718 720 STAILQ_INSERT_TAIL(&commands, cmdp, next);
719 721 COMMAND_SET(cmdp, "?", "list commands", command_commandlist);
720 722 STAILQ_INSERT_TAIL(&commands, cmdp, next);
721 723 COMMAND_SET(cmdp, "show", "show variable(s)", command_show);
722 724 STAILQ_INSERT_TAIL(&commands, cmdp, next);
723 725 COMMAND_SET(cmdp, "printenv", "show variable(s)", command_show);
724 726 STAILQ_INSERT_TAIL(&commands, cmdp, next);
725 727 COMMAND_SET(cmdp, "set", "set a variable", command_set);
726 728 STAILQ_INSERT_TAIL(&commands, cmdp, next);
727 729 COMMAND_SET(cmdp, "setprop", "set a variable", command_setprop);
728 730 STAILQ_INSERT_TAIL(&commands, cmdp, next);
729 731 COMMAND_SET(cmdp, "unset", "unset a variable", command_unset);
730 732 STAILQ_INSERT_TAIL(&commands, cmdp, next);
731 733 COMMAND_SET(cmdp, "echo", "echo arguments", command_echo);
732 734 STAILQ_INSERT_TAIL(&commands, cmdp, next);
733 735 COMMAND_SET(cmdp, "read", "read input from the terminal", command_read);
734 736 STAILQ_INSERT_TAIL(&commands, cmdp, next);
735 737 COMMAND_SET(cmdp, "more", "show contents of a file", command_more);
736 738 STAILQ_INSERT_TAIL(&commands, cmdp, next);
737 739 COMMAND_SET(cmdp, "ls", "list files", command_ls);
738 740 STAILQ_INSERT_TAIL(&commands, cmdp, next);
739 741 COMMAND_SET(cmdp, "include", "read commands from a file",
740 742 command_include);
741 743 STAILQ_INSERT_TAIL(&commands, cmdp, next);
742 744 COMMAND_SET(cmdp, "boot", "boot a file or loaded kernel", command_boot);
|
↓ open down ↓ |
628 lines elided |
↑ open up ↑ |
743 745 STAILQ_INSERT_TAIL(&commands, cmdp, next);
744 746 COMMAND_SET(cmdp, "autoboot", "boot automatically after a delay",
745 747 command_autoboot);
746 748 STAILQ_INSERT_TAIL(&commands, cmdp, next);
747 749 COMMAND_SET(cmdp, "load", "load a kernel or module", command_load);
748 750 STAILQ_INSERT_TAIL(&commands, cmdp, next);
749 751 COMMAND_SET(cmdp, "unload", "unload all modules", command_unload);
750 752 STAILQ_INSERT_TAIL(&commands, cmdp, next);
751 753 COMMAND_SET(cmdp, "reboot", "reboot the system", command_reboot);
752 754 STAILQ_INSERT_TAIL(&commands, cmdp, next);
755 + COMMAND_SET(cmdp, "framebuffer", "framebuffer mode management",
756 + command_framebuffer);
757 + STAILQ_INSERT_TAIL(&commands, cmdp, next);
753 758
754 759 fsi = malloc(sizeof (ficlSystemInformation));
755 760 ficlSystemInformationInitialize(fsi);
756 761 fsi->textOut = out;
757 762 fsi->dictionarySize = BF_DICTSIZE;
758 763
759 764 bf_sys = ficlSystemCreate(fsi);
760 765 free(fsi);
761 766 ficlSystemCompileExtras(bf_sys);
762 767 bf_vm = ficlSystemCreateVm(bf_sys);
763 768
764 769 buf = isadir();
765 770 if (buf == NULL || strcmp(buf, "amd64") != 0) {
766 771 (void) setenv("ISADIR", "", 1);
767 772 } else {
768 773 (void) setenv("ISADIR", buf, 1);
769 774 }
770 775 if (buf != NULL)
771 776 free(buf);
772 777 buf = get_currdev();
773 778 (void) setenv("currdev", buf, 1);
774 779 free(buf);
775 780
776 781 /* Put all private definitions in a "builtins" vocabulary */
777 782 rv = ficlVmEvaluate(bf_vm,
778 783 "vocabulary builtins also builtins definitions");
779 784 if (rv != FICL_VM_STATUS_OUT_OF_TEXT) {
780 785 printf("error interpreting forth: %d\n", rv);
781 786 exit(1);
782 787 }
783 788
784 789 /* Builtin constructor word */
785 790 rv = ficlVmEvaluate(bf_vm, BUILTIN_CONSTRUCTOR);
786 791 if (rv != FICL_VM_STATUS_OUT_OF_TEXT) {
787 792 printf("error interpreting forth: %d\n", rv);
788 793 exit(1);
789 794 }
790 795
791 796 /* make all commands appear as Forth words */
792 797 dict = ficlSystemGetDictionary(bf_sys);
793 798 cmdp = NULL;
794 799 STAILQ_FOREACH(cmdp, &commands, next) {
795 800 ficlDictionaryAppendPrimitive(dict, (char *)cmdp->c_name,
796 801 bf_command, FICL_WORD_DEFAULT);
797 802 rv = ficlVmEvaluate(bf_vm, "forth definitions builtins");
798 803 if (rv != FICL_VM_STATUS_OUT_OF_TEXT) {
799 804 printf("error interpreting forth: %d\n", rv);
800 805 exit(1);
801 806 }
802 807 sprintf(create_buf, "builtin: %s", cmdp->c_name);
803 808 rv = ficlVmEvaluate(bf_vm, create_buf);
804 809 if (rv != FICL_VM_STATUS_OUT_OF_TEXT) {
805 810 printf("error interpreting forth: %d\n", rv);
806 811 exit(1);
807 812 }
808 813 rv = ficlVmEvaluate(bf_vm, "builtins definitions");
809 814 if (rv != FICL_VM_STATUS_OUT_OF_TEXT) {
810 815 printf("error interpreting forth: %d\n", rv);
811 816 exit(1);
812 817 }
813 818 }
814 819 rv = ficlVmEvaluate(bf_vm, "only forth definitions");
815 820 if (rv != FICL_VM_STATUS_OUT_OF_TEXT) {
816 821 printf("error interpreting forth: %d\n", rv);
817 822 exit(1);
818 823 }
819 824
820 825 /*
821 826 * Export some version numbers so that code can detect the
822 827 * loader/host version
823 828 */
824 829 env = ficlSystemGetEnvironment(bf_sys);
825 830 ficlDictionarySetConstant(env, "loader_version",
826 831 (bootprog_rev[0] - '0') * 10 + (bootprog_rev[2] - '0'));
827 832
828 833 /* try to load and run init file if present */
|
↓ open down ↓ |
66 lines elided |
↑ open up ↑ |
829 834 if (rc == NULL)
830 835 rc = "/boot/forth/boot.4th";
831 836 if (*rc != '\0') {
832 837 fd = open(rc, O_RDONLY);
833 838 if (fd != -1) {
834 839 (void) ficlExecFD(bf_vm, fd);
835 840 close(fd);
836 841 }
837 842 }
838 843
844 + gfx_framework_init();
839 845 return (bf_vm);
840 846 }
841 847
842 848 void
843 849 bf_fini(void)
844 850 {
845 851 ficlSystemDestroy(bf_sys);
852 + gfx_framework_fini();
846 853 }
847 854
848 855 /*
849 856 * Feed a line of user input to the Forth interpreter
850 857 */
851 858 int
852 859 bf_run(char *line)
853 860 {
854 861 int result;
855 862 ficlString s;
856 863
857 864 FICL_STRING_SET_FROM_CSTRING(s, line);
858 865 result = ficlVmExecuteString(bf_vm, s);
859 866
860 867 switch (result) {
861 868 case FICL_VM_STATUS_OUT_OF_TEXT:
862 869 case FICL_VM_STATUS_ABORTQ:
863 870 case FICL_VM_STATUS_QUIT:
864 871 case FICL_VM_STATUS_ERROR_EXIT:
865 872 break;
866 873 case FICL_VM_STATUS_USER_EXIT:
867 874 break;
868 875 case FICL_VM_STATUS_ABORT:
869 876 printf("Aborted!\n");
870 877 break;
871 878 case BF_PARSE:
872 879 printf("Parse error!\n");
873 880 break;
874 881 default:
875 882 if (command_errmsg != NULL) {
876 883 printf("%s\n", command_errmsg);
877 884 command_errmsg = NULL;
878 885 }
879 886 }
880 887
881 888 setenv("interpret", bf_vm->state ? "" : "ok", 1);
882 889
883 890 return (result);
884 891 }
885 892
886 893 char *
887 894 get_dev(const char *path)
888 895 {
889 896 FILE *fp;
890 897 struct mnttab mpref = {0};
891 898 struct mnttab mp = {0};
892 899 char *currdev;
893 900 int ret;
894 901 char *buf;
895 902 char *tmppath;
896 903 char *tmpdev;
897 904 char *cwd = NULL;
898 905
899 906 fp = fopen(MNTTAB, "r");
900 907
901 908 /* do the best we can to return something... */
902 909 if (fp == NULL)
903 910 return (strdup(path));
904 911
905 912 /*
906 913 * the path can have device provided, check for it
907 914 * and extract it.
908 915 */
909 916 buf = strrchr(path, ':');
910 917 if (buf != NULL) {
911 918 tmppath = buf+1; /* real path */
912 919 buf = strchr(path, ':'); /* skip zfs: */
913 920 buf++;
914 921 tmpdev = strdup(buf);
915 922 buf = strchr(tmpdev, ':'); /* get ending : */
916 923 *buf = '\0';
917 924 } else {
918 925 tmppath = (char *)path;
919 926 if (tmppath[0] != '/')
920 927 if ((cwd = getcwd(NULL, PATH_MAX)) == NULL) {
921 928 (void) fclose(fp);
922 929 return (strdup(path));
923 930 }
924 931
925 932 currdev = getenv("currdev");
926 933 buf = strchr(currdev, ':'); /* skip zfs: */
927 934 if (buf == NULL) {
928 935 (void) fclose(fp);
929 936 return (strdup(path));
930 937 }
931 938 buf++;
932 939 tmpdev = strdup(buf);
933 940 buf = strchr(tmpdev, ':'); /* get ending : */
934 941 *buf = '\0';
935 942 }
936 943
937 944 mpref.mnt_special = tmpdev;
938 945 ret = getmntany(fp, &mp, &mpref);
939 946 (void) fclose(fp);
940 947 free(tmpdev);
941 948
942 949 if (cwd == NULL)
943 950 (void) asprintf(&buf, "%s/%s", ret? "":mp.mnt_mountp, tmppath);
944 951 else {
945 952 (void) asprintf(&buf, "%s/%s/%s", ret? "":mp.mnt_mountp, cwd,
946 953 tmppath);
947 954 free(cwd);
948 955 }
949 956 return (buf);
950 957 }
951 958
952 959 static void
953 960 ngets(char *buf, int n)
954 961 {
955 962 int c;
956 963 char *lp;
957 964
958 965 for (lp = buf; ; )
959 966 switch (c = getchar() & 0177) {
960 967 case '\n':
961 968 case '\r':
962 969 *lp = '\0';
963 970 putchar('\n');
964 971 return;
965 972 case '\b':
966 973 case '\177':
967 974 if (lp > buf) {
968 975 lp--;
969 976 putchar('\b');
970 977 putchar(' ');
971 978 putchar('\b');
972 979 }
973 980 break;
974 981 case 'r'&037: {
975 982 char *p;
976 983
977 984 putchar('\n');
978 985 for (p = buf; p < lp; ++p)
979 986 putchar(*p);
980 987 break;
981 988 }
982 989 case 'u'&037:
983 990 case 'w'&037:
984 991 lp = buf;
985 992 putchar('\n');
986 993 break;
987 994 default:
988 995 if ((n < 1) || ((lp - buf) < n - 1)) {
989 996 *lp++ = c;
990 997 putchar(c);
991 998 }
992 999 }
993 1000 /*NOTREACHED*/
994 1001 }
995 1002
996 1003 static int
997 1004 fgetstr(char *buf, int size, int fd)
998 1005 {
999 1006 char c;
1000 1007 int err, len;
1001 1008
1002 1009 size--; /* leave space for terminator */
1003 1010 len = 0;
1004 1011 while (size != 0) {
1005 1012 err = read(fd, &c, sizeof (c));
1006 1013 if (err < 0) /* read error */
1007 1014 return (-1);
1008 1015
1009 1016 if (err == 0) { /* EOF */
1010 1017 if (len == 0)
1011 1018 return (-1); /* nothing to read */
1012 1019 break;
1013 1020 }
1014 1021 if ((c == '\r') || (c == '\n')) /* line terminators */
1015 1022 break;
1016 1023 *buf++ = c; /* keep char */
1017 1024 size--;
1018 1025 len++;
1019 1026 }
1020 1027 *buf = 0;
1021 1028 return (len);
1022 1029 }
1023 1030
1024 1031 static char *
1025 1032 unargv(int argc, char *argv[])
1026 1033 {
1027 1034 size_t hlong;
1028 1035 int i;
1029 1036 char *cp;
1030 1037
1031 1038 for (i = 0, hlong = 0; i < argc; i++)
1032 1039 hlong += strlen(argv[i]) + 2;
1033 1040
1034 1041 if (hlong == 0)
1035 1042 return (NULL);
1036 1043
1037 1044 cp = malloc(hlong);
1038 1045 cp[0] = 0;
1039 1046 for (i = 0; i < argc; i++) {
1040 1047 strcat(cp, argv[i]);
1041 1048 if (i < (argc - 1))
1042 1049 strcat(cp, " ");
1043 1050 }
1044 1051
1045 1052 return (cp);
1046 1053 }
1047 1054
1048 1055 /*
1049 1056 * Help is read from a formatted text file.
1050 1057 *
1051 1058 * Entries in the file are formatted as:
1052 1059 * # Ttopic [Ssubtopic] Ddescription
1053 1060 * help
1054 1061 * text
1055 1062 * here
1056 1063 * #
1057 1064 *
1058 1065 * Note that for code simplicity's sake, the above format must be followed
1059 1066 * exactly.
1060 1067 *
1061 1068 * Subtopic entries must immediately follow the topic (this is used to
1062 1069 * produce the listing of subtopics).
1063 1070 *
1064 1071 * If no argument(s) are supplied by the user, the help for 'help' is displayed.
1065 1072 */
1066 1073 static int
1067 1074 help_getnext(int fd, char **topic, char **subtopic, char **desc)
1068 1075 {
1069 1076 char line[81], *cp, *ep;
1070 1077
1071 1078 for (;;) {
1072 1079 if (fgetstr(line, 80, fd) < 0)
1073 1080 return (0);
1074 1081
1075 1082 if ((strlen(line) < 3) || (line[0] != '#') || (line[1] != ' '))
1076 1083 continue;
1077 1084
1078 1085 *topic = *subtopic = *desc = NULL;
1079 1086 cp = line + 2;
1080 1087 while ((cp != NULL) && (*cp != 0)) {
1081 1088 ep = strchr(cp, ' ');
1082 1089 if ((*cp == 'T') && (*topic == NULL)) {
1083 1090 if (ep != NULL)
1084 1091 *ep++ = 0;
1085 1092 *topic = strdup(cp + 1);
1086 1093 } else if ((*cp == 'S') && (*subtopic == NULL)) {
1087 1094 if (ep != NULL)
1088 1095 *ep++ = 0;
1089 1096 *subtopic = strdup(cp + 1);
1090 1097 } else if (*cp == 'D') {
1091 1098 *desc = strdup(cp + 1);
1092 1099 ep = NULL;
1093 1100 }
1094 1101 cp = ep;
1095 1102 }
1096 1103 if (*topic == NULL) {
1097 1104 if (*subtopic != NULL)
1098 1105 free(*subtopic);
1099 1106 if (*desc != NULL)
1100 1107 free(*desc);
1101 1108 continue;
1102 1109 }
1103 1110 return (1);
1104 1111 }
1105 1112 }
1106 1113
1107 1114 static int
1108 1115 help_emitsummary(char *topic, char *subtopic, char *desc)
1109 1116 {
1110 1117 int i;
1111 1118
1112 1119 pager_output(" ");
1113 1120 pager_output(topic);
1114 1121 i = strlen(topic);
1115 1122 if (subtopic != NULL) {
1116 1123 pager_output(" ");
1117 1124 pager_output(subtopic);
1118 1125 i += strlen(subtopic) + 1;
1119 1126 }
1120 1127 if (desc != NULL) {
1121 1128 do {
1122 1129 pager_output(" ");
1123 1130 } while (i++ < 30);
1124 1131 pager_output(desc);
1125 1132 }
1126 1133 return (pager_output("\n"));
1127 1134 }
1128 1135
1129 1136 static int
1130 1137 command_help(int argc, char *argv[])
1131 1138 {
1132 1139 char buf[81]; /* XXX buffer size? */
1133 1140 int hfd, matched, doindex;
1134 1141 char *topic, *subtopic, *t, *s, *d;
1135 1142
1136 1143 /* page the help text from our load path */
1137 1144 sprintf(buf, "/boot/loader.help");
1138 1145 if ((hfd = open(buf, O_RDONLY)) < 0) {
1139 1146 printf("Verbose help not available, "
1140 1147 "use '?' to list commands\n");
1141 1148 return (CMD_OK);
1142 1149 }
1143 1150
1144 1151 /* pick up request from arguments */
1145 1152 topic = subtopic = NULL;
1146 1153 switch (argc) {
1147 1154 case 3:
1148 1155 subtopic = strdup(argv[2]);
1149 1156 /* FALLTHROUGH */
1150 1157 case 2:
1151 1158 topic = strdup(argv[1]);
1152 1159 break;
1153 1160 case 1:
1154 1161 topic = strdup("help");
1155 1162 break;
1156 1163 default:
1157 1164 command_errmsg = "usage is 'help <topic> [<subtopic>]";
1158 1165 close(hfd);
1159 1166 return (CMD_ERROR);
1160 1167 }
1161 1168
1162 1169 /* magic "index" keyword */
1163 1170 doindex = strcmp(topic, "index") == 0;
1164 1171 matched = doindex;
1165 1172
1166 1173 /* Scan the helpfile looking for help matching the request */
1167 1174 pager_open();
1168 1175 while (help_getnext(hfd, &t, &s, &d)) {
1169 1176 if (doindex) { /* dink around formatting */
1170 1177 if (help_emitsummary(t, s, d))
1171 1178 break;
1172 1179
1173 1180 } else if (strcmp(topic, t)) {
1174 1181 /* topic mismatch */
1175 1182 /* nothing more on this topic, stop scanning */
1176 1183 if (matched)
1177 1184 break;
1178 1185 } else {
1179 1186 /* topic matched */
1180 1187 matched = 1;
1181 1188 if (((subtopic == NULL) && (s == NULL)) ||
1182 1189 ((subtopic != NULL) && (s != NULL) &&
1183 1190 strcmp(subtopic, s) == 0)) {
1184 1191 /* exact match, print text */
1185 1192 while ((fgetstr(buf, 80, hfd) >= 0) &&
1186 1193 (buf[0] != '#')) {
1187 1194 if (pager_output(buf))
1188 1195 break;
1189 1196 if (pager_output("\n"))
1190 1197 break;
1191 1198 }
1192 1199 } else if ((subtopic == NULL) && (s != NULL)) {
1193 1200 /* topic match, list subtopics */
1194 1201 if (help_emitsummary(t, s, d))
1195 1202 break;
1196 1203 }
1197 1204 }
1198 1205 free(t);
1199 1206 free(s);
1200 1207 free(d);
1201 1208 }
1202 1209 pager_close();
1203 1210 close(hfd);
1204 1211 if (!matched) {
1205 1212 snprintf(command_errbuf, sizeof (command_errbuf),
1206 1213 "no help available for '%s'", topic);
1207 1214 free(topic);
1208 1215 if (subtopic)
1209 1216 free(subtopic);
1210 1217 return (CMD_ERROR);
1211 1218 }
1212 1219 free(topic);
1213 1220 if (subtopic)
1214 1221 free(subtopic);
1215 1222 return (CMD_OK);
1216 1223 }
1217 1224
1218 1225 static int
1219 1226 command_commandlist(int argc, char *argv[])
1220 1227 {
1221 1228 struct bootblk_command *cmdp;
1222 1229 int res;
1223 1230 char name[20];
1224 1231
1225 1232 res = 0;
1226 1233 pager_open();
1227 1234 res = pager_output("Available commands:\n");
1228 1235 cmdp = NULL;
1229 1236 STAILQ_FOREACH(cmdp, &commands, next) {
1230 1237 if (res)
1231 1238 break;
1232 1239 if ((cmdp->c_name != NULL) && (cmdp->c_desc != NULL)) {
1233 1240 sprintf(name, " %-15s ", cmdp->c_name);
1234 1241 pager_output(name);
1235 1242 pager_output(cmdp->c_desc);
1236 1243 res = pager_output("\n");
1237 1244 }
1238 1245 }
1239 1246 pager_close();
1240 1247 return (CMD_OK);
1241 1248 }
1242 1249
1243 1250 /*
1244 1251 * XXX set/show should become set/echo if we have variable
1245 1252 * substitution happening.
1246 1253 */
1247 1254 static int
1248 1255 command_show(int argc, char *argv[])
1249 1256 {
1250 1257 char **ev;
1251 1258 char *cp;
1252 1259
1253 1260 if (argc < 2) {
1254 1261 /*
1255 1262 * With no arguments, print everything.
1256 1263 */
1257 1264 pager_open();
1258 1265 for (ev = _environ; *ev != NULL; ev++) {
1259 1266 pager_output(*ev);
1260 1267 cp = getenv(*ev);
1261 1268 if (cp != NULL) {
1262 1269 pager_output("=");
1263 1270 pager_output(cp);
1264 1271 }
1265 1272 if (pager_output("\n"))
1266 1273 break;
1267 1274 }
1268 1275 pager_close();
1269 1276 } else {
1270 1277 if ((cp = getenv(argv[1])) != NULL) {
1271 1278 printf("%s\n", cp);
1272 1279 } else {
1273 1280 snprintf(command_errbuf, sizeof (command_errbuf),
1274 1281 "variable '%s' not found", argv[1]);
1275 1282 return (CMD_ERROR);
1276 1283 }
1277 1284 }
1278 1285 return (CMD_OK);
1279 1286 }
1280 1287
1281 1288 static int
1282 1289 command_set(int argc, char *argv[])
1283 1290 {
1284 1291 int err;
1285 1292 char *value, *copy;
1286 1293
1287 1294 if (argc != 2) {
1288 1295 command_errmsg = "wrong number of arguments";
1289 1296 return (CMD_ERROR);
1290 1297 } else {
1291 1298 copy = strdup(argv[1]);
1292 1299 if (copy == NULL) {
1293 1300 command_errmsg = strerror(errno);
1294 1301 return (CMD_ERROR);
1295 1302 }
1296 1303 if ((value = strchr(copy, '=')) != NULL)
1297 1304 *(value++) = 0;
1298 1305 else
1299 1306 value = "";
1300 1307 if ((err = setenv(copy, value, 1)) != 0) {
1301 1308 free(copy);
1302 1309 command_errmsg = strerror(errno);
1303 1310 return (CMD_ERROR);
1304 1311 }
1305 1312 free(copy);
1306 1313 }
1307 1314 return (CMD_OK);
1308 1315 }
1309 1316
1310 1317 static int
1311 1318 command_setprop(int argc, char *argv[])
1312 1319 {
1313 1320 int err;
1314 1321
1315 1322 if (argc != 3) {
1316 1323 command_errmsg = "wrong number of arguments";
1317 1324 return (CMD_ERROR);
1318 1325 } else {
1319 1326 if ((err = setenv(argv[1], argv[2], 1)) != 0) {
1320 1327 command_errmsg = strerror(err);
1321 1328 return (CMD_ERROR);
1322 1329 }
1323 1330 }
1324 1331 return (CMD_OK);
1325 1332 }
1326 1333
1327 1334 static int
1328 1335 command_unset(int argc, char *argv[])
1329 1336 {
1330 1337 int err;
1331 1338
1332 1339 if (argc != 2) {
1333 1340 command_errmsg = "wrong number of arguments";
1334 1341 return (CMD_ERROR);
1335 1342 } else {
1336 1343 if ((err = unsetenv(argv[1])) != 0) {
1337 1344 command_errmsg = strerror(err);
1338 1345 return (CMD_ERROR);
1339 1346 }
1340 1347 }
1341 1348 return (CMD_OK);
1342 1349 }
1343 1350
1344 1351 static int
1345 1352 command_echo(int argc, char *argv[])
1346 1353 {
1347 1354 char *s;
1348 1355 int nl, ch;
1349 1356
1350 1357 nl = 0;
1351 1358 optind = 1;
1352 1359 opterr = 1;
1353 1360 while ((ch = getopt(argc, argv, "n")) != -1) {
1354 1361 switch (ch) {
1355 1362 case 'n':
1356 1363 nl = 1;
1357 1364 break;
1358 1365 case '?':
1359 1366 default:
1360 1367 /* getopt has already reported an error */
1361 1368 return (CMD_OK);
1362 1369 }
1363 1370 }
1364 1371 argv += (optind);
1365 1372 argc -= (optind);
1366 1373
1367 1374 s = unargv(argc, argv);
1368 1375 if (s != NULL) {
1369 1376 printf("%s", s);
1370 1377 free(s);
1371 1378 }
1372 1379 if (!nl)
1373 1380 printf("\n");
1374 1381 return (CMD_OK);
1375 1382 }
1376 1383
1377 1384 /*
1378 1385 * A passable emulation of the sh(1) command of the same name.
1379 1386 */
1380 1387 static int
1381 1388 ischar(void)
1382 1389 {
1383 1390 return (1);
1384 1391 }
1385 1392
1386 1393 static int
1387 1394 command_read(int argc, char *argv[])
1388 1395 {
1389 1396 char *prompt;
1390 1397 int timeout;
1391 1398 time_t when;
1392 1399 char *cp;
1393 1400 char *name;
1394 1401 char buf[256]; /* XXX size? */
1395 1402 int c;
1396 1403
1397 1404 timeout = -1;
1398 1405 prompt = NULL;
1399 1406 optind = 1;
1400 1407 opterr = 1;
1401 1408 while ((c = getopt(argc, argv, "p:t:")) != -1) {
1402 1409 switch (c) {
1403 1410 case 'p':
1404 1411 prompt = optarg;
1405 1412 break;
1406 1413 case 't':
1407 1414 timeout = strtol(optarg, &cp, 0);
1408 1415 if (cp == optarg) {
1409 1416 snprintf(command_errbuf,
1410 1417 sizeof (command_errbuf),
1411 1418 "bad timeout '%s'", optarg);
1412 1419 return (CMD_ERROR);
1413 1420 }
1414 1421 break;
1415 1422 default:
1416 1423 return (CMD_OK);
1417 1424 }
1418 1425 }
1419 1426
1420 1427 argv += (optind);
1421 1428 argc -= (optind);
1422 1429 name = (argc > 0) ? argv[0]: NULL;
1423 1430
1424 1431 if (prompt != NULL)
1425 1432 printf("%s", prompt);
1426 1433 if (timeout >= 0) {
1427 1434 when = time(NULL) + timeout;
1428 1435 while (!ischar())
1429 1436 if (time(NULL) >= when)
1430 1437 return (CMD_OK); /* is timeout an error? */
1431 1438 }
1432 1439
1433 1440 ngets(buf, sizeof (buf));
1434 1441
1435 1442 if (name != NULL)
1436 1443 setenv(name, buf, 1);
1437 1444 return (CMD_OK);
1438 1445 }
1439 1446
1440 1447 /*
1441 1448 * File pager
1442 1449 */
1443 1450 static int
1444 1451 command_more(int argc, char *argv[])
1445 1452 {
1446 1453 int i;
1447 1454 int res;
1448 1455 char line[80];
1449 1456 char *name;
1450 1457
1451 1458 res = 0;
1452 1459 pager_open();
1453 1460 for (i = 1; (i < argc) && (res == 0); i++) {
1454 1461 sprintf(line, "*** FILE %s BEGIN ***\n", argv[i]);
1455 1462 if (pager_output(line))
1456 1463 break;
1457 1464 name = get_dev(argv[i]);
1458 1465 res = page_file(name);
1459 1466 free(name);
1460 1467 if (!res) {
1461 1468 sprintf(line, "*** FILE %s END ***\n", argv[i]);
1462 1469 res = pager_output(line);
1463 1470 }
1464 1471 }
1465 1472 pager_close();
1466 1473
1467 1474 if (res == 0)
1468 1475 return (CMD_OK);
1469 1476 return (CMD_ERROR);
1470 1477 }
1471 1478
1472 1479 static int
1473 1480 page_file(char *filename)
1474 1481 {
1475 1482 int result;
1476 1483
1477 1484 result = pager_file(filename);
1478 1485
1479 1486 if (result == -1) {
1480 1487 snprintf(command_errbuf, sizeof (command_errbuf),
1481 1488 "error showing %s", filename);
1482 1489 }
1483 1490
1484 1491 return (result);
1485 1492 }
1486 1493
1487 1494 static int
1488 1495 command_ls(int argc, char *argv[])
1489 1496 {
1490 1497 DIR *dir;
1491 1498 int fd;
1492 1499 struct stat sb;
1493 1500 struct dirent *d;
1494 1501 char *buf, *path;
1495 1502 char lbuf[128]; /* one line */
1496 1503 int result, ch;
1497 1504 int verbose;
1498 1505
1499 1506 result = CMD_OK;
1500 1507 fd = -1;
1501 1508 verbose = 0;
1502 1509 optind = 1;
1503 1510 opterr = 1;
1504 1511 while ((ch = getopt(argc, argv, "l")) != -1) {
1505 1512 switch (ch) {
1506 1513 case 'l':
1507 1514 verbose = 1;
1508 1515 break;
1509 1516 case '?':
1510 1517 default:
1511 1518 /* getopt has already reported an error */
1512 1519 return (CMD_OK);
1513 1520 }
1514 1521 }
1515 1522 argv += (optind - 1);
1516 1523 argc -= (optind - 1);
1517 1524
1518 1525 if (argc < 2) {
1519 1526 path = "";
1520 1527 } else {
1521 1528 path = argv[1];
1522 1529 }
1523 1530
1524 1531 fd = ls_getdir(&path);
1525 1532 if (fd == -1) {
1526 1533 result = CMD_ERROR;
1527 1534 goto out;
1528 1535 }
1529 1536 dir = fdopendir(fd);
1530 1537 pager_open();
1531 1538 pager_output(path);
1532 1539 pager_output("\n");
1533 1540
1534 1541 while ((d = readdir(dir)) != NULL) {
1535 1542 if (strcmp(d->d_name, ".") && strcmp(d->d_name, "..")) {
1536 1543 /* stat the file, if possible */
1537 1544 sb.st_size = 0;
1538 1545 sb.st_mode = 0;
1539 1546 buf = malloc(strlen(path) + strlen(d->d_name) + 2);
1540 1547 if (path[0] == '\0')
1541 1548 sprintf(buf, "%s", d->d_name);
1542 1549 else
1543 1550 sprintf(buf, "%s/%s", path, d->d_name);
1544 1551 /* ignore return, could be symlink, etc. */
1545 1552 if (stat(buf, &sb))
1546 1553 sb.st_size = 0;
1547 1554 free(buf);
1548 1555 if (verbose) {
1549 1556 sprintf(lbuf, " %c %8d %s\n",
1550 1557 typestr[sb.st_mode >> 12],
1551 1558 (int)sb.st_size, d->d_name);
1552 1559 } else {
1553 1560 sprintf(lbuf, " %c %s\n",
1554 1561 typestr[sb.st_mode >> 12], d->d_name);
1555 1562 }
1556 1563 if (pager_output(lbuf))
1557 1564 goto out;
1558 1565 }
1559 1566 }
1560 1567 out:
1561 1568 pager_close();
1562 1569 if (fd != -1)
1563 1570 closedir(dir);
1564 1571 if (path != NULL)
1565 1572 free(path);
1566 1573 return (result);
1567 1574 }
1568 1575
1569 1576 /*
1570 1577 * Given (path) containing a vaguely reasonable path specification, return an fd
1571 1578 * on the directory, and an allocated copy of the path to the directory.
1572 1579 */
1573 1580 static int
1574 1581 ls_getdir(char **pathp)
1575 1582 {
1576 1583 struct stat sb;
1577 1584 int fd;
1578 1585 char *cp, *path;
1579 1586
1580 1587 fd = -1;
1581 1588
1582 1589 /* one extra byte for a possible trailing slash required */
1583 1590 path = malloc(strlen(*pathp) + 2);
1584 1591 strcpy(path, *pathp);
1585 1592
1586 1593 /* Make sure the path is respectable to begin with */
1587 1594 if ((cp = get_dev(path)) == NULL) {
1588 1595 snprintf(command_errbuf, sizeof (command_errbuf),
1589 1596 "bad path '%s'", path);
1590 1597 goto out;
1591 1598 }
1592 1599
1593 1600 /* If there's no path on the device, assume '/' */
1594 1601 if (*cp == 0)
1595 1602 strcat(path, "/");
1596 1603
1597 1604 fd = open(cp, O_RDONLY);
1598 1605 if (fd < 0) {
1599 1606 snprintf(command_errbuf, sizeof (command_errbuf),
1600 1607 "open '%s' failed: %s", path, strerror(errno));
1601 1608 goto out;
1602 1609 }
1603 1610 if (fstat(fd, &sb) < 0) {
1604 1611 snprintf(command_errbuf, sizeof (command_errbuf),
1605 1612 "stat failed: %s", strerror(errno));
1606 1613 goto out;
1607 1614 }
1608 1615 if (!S_ISDIR(sb.st_mode)) {
1609 1616 snprintf(command_errbuf, sizeof (command_errbuf),
1610 1617 "%s: %s", path, strerror(ENOTDIR));
1611 1618 goto out;
1612 1619 }
1613 1620
1614 1621 free(cp);
1615 1622 *pathp = path;
1616 1623 return (fd);
1617 1624
1618 1625 out:
1619 1626 free(cp);
1620 1627 free(path);
1621 1628 *pathp = NULL;
1622 1629 if (fd != -1)
1623 1630 close(fd);
1624 1631 return (-1);
1625 1632 }
1626 1633
1627 1634 static int
1628 1635 command_include(int argc, char *argv[])
1629 1636 {
1630 1637 int i;
1631 1638 int res;
1632 1639 char **argvbuf;
1633 1640
1634 1641 /*
1635 1642 * Since argv is static, we need to save it here.
1636 1643 */
1637 1644 argvbuf = (char **)calloc(argc, sizeof (char *));
1638 1645 for (i = 0; i < argc; i++)
1639 1646 argvbuf[i] = strdup(argv[i]);
1640 1647
1641 1648 res = CMD_OK;
1642 1649 for (i = 1; (i < argc) && (res == CMD_OK); i++)
1643 1650 res = include(argvbuf[i]);
1644 1651
1645 1652 for (i = 0; i < argc; i++)
1646 1653 free(argvbuf[i]);
1647 1654 free(argvbuf);
1648 1655
1649 1656 return (res);
1650 1657 }
1651 1658
1652 1659 /*
1653 1660 * Header prepended to each line. The text immediately follows the header.
1654 1661 * We try to make this short in order to save memory -- the loader has
1655 1662 * limited memory available, and some of the forth files are very long.
1656 1663 */
1657 1664 struct includeline
1658 1665 {
1659 1666 struct includeline *next;
1660 1667 int line;
1661 1668 char text[];
1662 1669 };
1663 1670
1664 1671 int
1665 1672 include(const char *filename)
1666 1673 {
1667 1674 struct includeline *script, *se, *sp;
1668 1675 int res = CMD_OK;
1669 1676 int prevsrcid, fd, line;
1670 1677 char *cp, input[256]; /* big enough? */
1671 1678 char *path;
1672 1679
1673 1680 path = get_dev(filename);
1674 1681 if (((fd = open(path, O_RDONLY)) == -1)) {
1675 1682 snprintf(command_errbuf, sizeof (command_errbuf),
1676 1683 "can't open '%s': %s", filename,
1677 1684 strerror(errno));
1678 1685 free(path);
1679 1686 return (CMD_ERROR);
1680 1687 }
1681 1688
1682 1689 free(path);
1683 1690 /*
1684 1691 * Read the script into memory.
1685 1692 */
1686 1693 script = se = NULL;
1687 1694 line = 0;
1688 1695
1689 1696 while (fgetstr(input, sizeof (input), fd) >= 0) {
1690 1697 line++;
1691 1698 cp = input;
1692 1699 /* Allocate script line structure and copy line, flags */
1693 1700 if (*cp == '\0')
1694 1701 continue; /* ignore empty line, save memory */
1695 1702 if (cp[0] == '\\' && cp[1] == ' ')
1696 1703 continue; /* ignore comment */
1697 1704
1698 1705 sp = malloc(sizeof (struct includeline) + strlen(cp) + 1);
1699 1706 /*
1700 1707 * On malloc failure (it happens!), free as much as possible
1701 1708 * and exit
1702 1709 */
1703 1710 if (sp == NULL) {
1704 1711 while (script != NULL) {
1705 1712 se = script;
1706 1713 script = script->next;
1707 1714 free(se);
1708 1715 }
1709 1716 snprintf(command_errbuf, sizeof (command_errbuf),
1710 1717 "file '%s' line %d: memory allocation "
1711 1718 "failure - aborting", filename, line);
1712 1719 return (CMD_ERROR);
1713 1720 }
1714 1721 strcpy(sp->text, cp);
1715 1722 sp->line = line;
1716 1723 sp->next = NULL;
1717 1724
1718 1725 if (script == NULL) {
1719 1726 script = sp;
1720 1727 } else {
1721 1728 se->next = sp;
1722 1729 }
1723 1730 se = sp;
1724 1731 }
1725 1732 close(fd);
1726 1733
1727 1734 /*
1728 1735 * Execute the script
1729 1736 */
1730 1737
1731 1738 prevsrcid = bf_vm->sourceId.i;
1732 1739 bf_vm->sourceId.i = fd+1; /* 0 is user input device */
1733 1740
1734 1741 res = CMD_OK;
1735 1742
1736 1743 for (sp = script; sp != NULL; sp = sp->next) {
1737 1744 res = bf_run(sp->text);
1738 1745 if (res != FICL_VM_STATUS_OUT_OF_TEXT) {
1739 1746 snprintf(command_errbuf, sizeof (command_errbuf),
1740 1747 "Error while including %s, in the line %d:\n%s",
1741 1748 filename, sp->line, sp->text);
1742 1749 res = CMD_ERROR;
1743 1750 break;
1744 1751 } else
1745 1752 res = CMD_OK;
1746 1753 }
1747 1754
1748 1755 bf_vm->sourceId.i = -1;
1749 1756 (void) bf_run("");
1750 1757 bf_vm->sourceId.i = prevsrcid;
1751 1758
1752 1759 while (script != NULL) {
1753 1760 se = script;
1754 1761 script = script->next;
1755 1762 free(se);
1756 1763 }
1757 1764
1758 1765 return (res);
1759 1766 }
1760 1767
1761 1768 static int
1762 1769 command_boot(int argc, char *argv[])
1763 1770 {
1764 1771 return (CMD_OK);
1765 1772 }
1766 1773
1767 1774 static int
1768 1775 command_autoboot(int argc, char *argv[])
1769 1776 {
1770 1777 return (CMD_OK);
1771 1778 }
1772 1779
1773 1780 static void
1774 1781 moduledir_rebuild(void)
1775 1782 {
1776 1783 struct moduledir *mdp, *mtmp;
1777 1784 const char *path, *cp, *ep;
1778 1785 int cplen;
1779 1786
1780 1787 path = getenv("module_path");
1781 1788 if (path == NULL)
1782 1789 path = default_searchpath;
1783 1790 /*
1784 1791 * Rebuild list of module directories if it changed
1785 1792 */
1786 1793 STAILQ_FOREACH(mdp, &moduledir_list, d_link)
1787 1794 mdp->d_flags |= MDIR_REMOVED;
1788 1795
1789 1796 for (ep = path; *ep != 0; ep++) {
1790 1797 cp = ep;
1791 1798 for (; *ep != 0 && *ep != ';'; ep++)
1792 1799 ;
1793 1800 /*
1794 1801 * Ignore trailing slashes
1795 1802 */
1796 1803 for (cplen = ep - cp; cplen > 1 && cp[cplen - 1] == '/';
1797 1804 cplen--)
1798 1805 ;
1799 1806 STAILQ_FOREACH(mdp, &moduledir_list, d_link) {
1800 1807 if (strlen(mdp->d_path) != cplen ||
1801 1808 bcmp(cp, mdp->d_path, cplen) != 0)
1802 1809 continue;
1803 1810 mdp->d_flags &= ~MDIR_REMOVED;
1804 1811 break;
1805 1812 }
1806 1813 if (mdp == NULL) {
1807 1814 mdp = malloc(sizeof (*mdp) + cplen + 1);
1808 1815 if (mdp == NULL)
1809 1816 return;
1810 1817 mdp->d_path = (char *)(mdp + 1);
1811 1818 bcopy(cp, mdp->d_path, cplen);
1812 1819 mdp->d_path[cplen] = 0;
1813 1820 mdp->d_hints = NULL;
1814 1821 mdp->d_flags = 0;
1815 1822 STAILQ_INSERT_TAIL(&moduledir_list, mdp, d_link);
1816 1823 }
1817 1824 if (*ep == 0)
1818 1825 break;
1819 1826 }
1820 1827 /*
1821 1828 * Delete unused directories if any
1822 1829 */
1823 1830 mdp = STAILQ_FIRST(&moduledir_list);
1824 1831 while (mdp) {
1825 1832 if ((mdp->d_flags & MDIR_REMOVED) == 0) {
1826 1833 mdp = STAILQ_NEXT(mdp, d_link);
1827 1834 } else {
1828 1835 if (mdp->d_hints)
1829 1836 free(mdp->d_hints);
1830 1837 mtmp = mdp;
1831 1838 mdp = STAILQ_NEXT(mdp, d_link);
1832 1839 STAILQ_REMOVE(&moduledir_list, mtmp, moduledir, d_link);
1833 1840 free(mtmp);
1834 1841 }
1835 1842 }
1836 1843 }
1837 1844
1838 1845 static char *
1839 1846 file_lookup(const char *path, const char *name, int namelen)
1840 1847 {
1841 1848 struct stat st;
1842 1849 char *result, *cp, *gz;
1843 1850 int pathlen;
1844 1851
1845 1852 pathlen = strlen(path);
1846 1853 result = malloc(pathlen + namelen + 2);
1847 1854 if (result == NULL)
1848 1855 return (NULL);
1849 1856 bcopy(path, result, pathlen);
1850 1857 if (pathlen > 0 && result[pathlen - 1] != '/')
1851 1858 result[pathlen++] = '/';
1852 1859 cp = result + pathlen;
1853 1860 bcopy(name, cp, namelen);
1854 1861 cp += namelen;
1855 1862 *cp = '\0';
1856 1863 if (stat(result, &st) == 0 && S_ISREG(st.st_mode))
1857 1864 return (result);
1858 1865 /* also check for gz file */
1859 1866 (void) asprintf(&gz, "%s.gz", result);
1860 1867 if (gz != NULL) {
1861 1868 int res = stat(gz, &st);
1862 1869 free(gz);
1863 1870 if (res == 0)
1864 1871 return (result);
1865 1872 }
1866 1873 free(result);
1867 1874 return (NULL);
1868 1875 }
1869 1876
1870 1877 static char *
1871 1878 file_search(const char *name)
1872 1879 {
1873 1880 struct moduledir *mdp;
1874 1881 struct stat sb;
1875 1882 char *result;
1876 1883 int namelen;
1877 1884
1878 1885 if (name == NULL)
1879 1886 return (NULL);
1880 1887 if (*name == 0)
1881 1888 return (strdup(name));
1882 1889
1883 1890 if (strchr(name, '/') != NULL) {
1884 1891 char *gz;
1885 1892 if (stat(name, &sb) == 0)
1886 1893 return (strdup(name));
1887 1894 /* also check for gz file */
1888 1895 (void) asprintf(&gz, "%s.gz", name);
1889 1896 if (gz != NULL) {
1890 1897 int res = stat(gz, &sb);
1891 1898 free(gz);
1892 1899 if (res == 0)
1893 1900 return (strdup(name));
1894 1901 }
1895 1902 return (NULL);
1896 1903 }
1897 1904
1898 1905 moduledir_rebuild();
1899 1906 result = NULL;
1900 1907 namelen = strlen(name);
1901 1908 STAILQ_FOREACH(mdp, &moduledir_list, d_link) {
1902 1909 result = file_lookup(mdp->d_path, name, namelen);
1903 1910 if (result)
1904 1911 break;
1905 1912 }
1906 1913 return (result);
1907 1914 }
1908 1915
1909 1916 static int
1910 1917 command_load(int argc, char *argv[])
1911 1918 {
1912 1919 int dofile, ch;
1913 1920 char *typestr = NULL;
1914 1921 char *filename;
1915 1922 dofile = 0;
1916 1923 optind = 1;
1917 1924
1918 1925 if (argc == 1) {
1919 1926 command_errmsg = "no filename specified";
1920 1927 return (CMD_ERROR);
1921 1928 }
1922 1929
1923 1930 while ((ch = getopt(argc, argv, "kt:")) != -1) {
1924 1931 switch (ch) {
1925 1932 case 'k':
1926 1933 break;
1927 1934 case 't':
1928 1935 typestr = optarg;
1929 1936 dofile = 1;
1930 1937 break;
1931 1938 case '?':
1932 1939 default:
1933 1940 return (CMD_OK);
1934 1941 }
1935 1942 }
1936 1943 argv += (optind - 1);
1937 1944 argc -= (optind - 1);
1938 1945 if (dofile) {
1939 1946 if ((typestr == NULL) || (*typestr == 0)) {
1940 1947 command_errmsg = "invalid load type";
1941 1948 return (CMD_ERROR);
1942 1949 }
1943 1950 #if 0
1944 1951 return (file_loadraw(argv[1], typestr, argc - 2, argv + 2, 1)
1945 1952 ? CMD_OK : CMD_ERROR);
1946 1953 #endif
1947 1954 return (CMD_OK);
1948 1955 }
1949 1956
1950 1957 filename = file_search(argv[1]);
1951 1958 if (filename == NULL) {
1952 1959 snprintf(command_errbuf, sizeof (command_errbuf),
1953 1960 "can't find '%s'", argv[1]);
1954 1961 return (CMD_ERROR);
1955 1962 }
1956 1963 setenv("kernelname", filename, 1);
1957 1964
1958 1965 return (CMD_OK);
1959 1966 }
1960 1967
1961 1968 static int
1962 1969 command_unload(int argc, char *argv[])
1963 1970 {
|
↓ open down ↓ |
1108 lines elided |
↑ open up ↑ |
1964 1971 unsetenv("kernelname");
1965 1972 return (CMD_OK);
1966 1973 }
1967 1974
1968 1975 static int
1969 1976 command_reboot(int argc, char *argv[])
1970 1977 {
1971 1978 exit(0);
1972 1979 return (CMD_OK);
1973 1980 }
1981 +
1982 +/* Only implement get, ignore other arguments */
1983 +static int
1984 +command_framebuffer(int argc, char *argv[])
1985 +{
1986 + if (fb.fd < 0) {
1987 + printf("Framebuffer is not available.\n");
1988 + return (CMD_OK);
1989 + }
1990 +
1991 + if (argc == 2 && strcmp(argv[1], "get") == 0) {
1992 + printf("\nSystem frame buffer: %s\n", fb.ident.name);
1993 + printf("%dx%dx%d, stride=%d\n", fb.fb_width, fb.fb_height,
1994 + fb.fb_depth, (fb.fb_pitch << 3) / fb.fb_depth);
1995 + return (CMD_OK);
1996 + }
1997 + if (argc == 2 && strcmp(argv[1], "list") == 0) {
1998 + printf("0: %dx%dx%d\n", fb.fb_width, fb.fb_height, fb.fb_depth);
1999 + return (CMD_OK);
2000 + }
2001 + return (CMD_OK);
2002 +}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX