17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*986fd29aSsetje * Common Development and Distribution License (the "License").
6*986fd29aSsetje * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate /*
22*986fd29aSsetje * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
237c478bd9Sstevel@tonic-gate * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
277c478bd9Sstevel@tonic-gate
287c478bd9Sstevel@tonic-gate #include <sys/types.h>
297c478bd9Sstevel@tonic-gate #include <sys/bootconf.h>
307c478bd9Sstevel@tonic-gate #include <sys/reboot.h>
317c478bd9Sstevel@tonic-gate #include <sys/param.h>
327c478bd9Sstevel@tonic-gate #include <sys/salib.h>
337c478bd9Sstevel@tonic-gate #include <sys/debug.h>
347c478bd9Sstevel@tonic-gate #include <sys/promif.h>
357c478bd9Sstevel@tonic-gate #include <sys/boot.h>
367c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
377c478bd9Sstevel@tonic-gate #include <util/getoptstr.h>
387c478bd9Sstevel@tonic-gate #include "boot_plat.h"
397c478bd9Sstevel@tonic-gate
407c478bd9Sstevel@tonic-gate static char default_path_buf[MAXPATHLEN];
417c478bd9Sstevel@tonic-gate
427c478bd9Sstevel@tonic-gate char wanboot_arguments[OBP_MAXPATHLEN]; /* args following "-o" */
437c478bd9Sstevel@tonic-gate
44*986fd29aSsetje char cmd_line_boot_archive[MAXPATHLEN];
45*986fd29aSsetje
46*986fd29aSsetje boolean_t halt;
47*986fd29aSsetje
487c478bd9Sstevel@tonic-gate /*
497c478bd9Sstevel@tonic-gate * Parse the boot arguments, adding the options found to the existing boothowto
507c478bd9Sstevel@tonic-gate * value (if any) or other state. Then rewrite the buffer with arguments for
517c478bd9Sstevel@tonic-gate * the standalone.
527c478bd9Sstevel@tonic-gate *
537c478bd9Sstevel@tonic-gate * NOTE: boothowto may already have bits set when this function is called
547c478bd9Sstevel@tonic-gate */
557c478bd9Sstevel@tonic-gate void
bootflags(char * args,size_t argsz)567c478bd9Sstevel@tonic-gate bootflags(char *args, size_t argsz)
577c478bd9Sstevel@tonic-gate {
587c478bd9Sstevel@tonic-gate static char newargs[OBP_MAXPATHLEN];
597c478bd9Sstevel@tonic-gate struct gos_params params;
607c478bd9Sstevel@tonic-gate const char *cp;
617c478bd9Sstevel@tonic-gate char *np;
627c478bd9Sstevel@tonic-gate size_t npres;
637c478bd9Sstevel@tonic-gate int c;
64*986fd29aSsetje char *cmd_line_default_path;
657c478bd9Sstevel@tonic-gate
667c478bd9Sstevel@tonic-gate cmd_line_default_path = NULL;
677c478bd9Sstevel@tonic-gate
687c478bd9Sstevel@tonic-gate params.gos_opts = "HXF:VnI:D:advhko:";
697c478bd9Sstevel@tonic-gate params.gos_strp = args;
707c478bd9Sstevel@tonic-gate getoptstr_init(¶ms);
717c478bd9Sstevel@tonic-gate while ((c = getoptstr(¶ms)) != -1) {
727c478bd9Sstevel@tonic-gate switch (c) {
737c478bd9Sstevel@tonic-gate /*
74*986fd29aSsetje * Bootblock flags.
757c478bd9Sstevel@tonic-gate */
767c478bd9Sstevel@tonic-gate case 'H':
77*986fd29aSsetje halt = B_TRUE;
78*986fd29aSsetje /*FALLTHRU*/
797c478bd9Sstevel@tonic-gate case 'X':
80*986fd29aSsetje break;
81*986fd29aSsetje
827c478bd9Sstevel@tonic-gate case 'F':
83*986fd29aSsetje if (params.gos_optarglen >=
84*986fd29aSsetje sizeof (cmd_line_boot_archive)) {
85*986fd29aSsetje printf("boot: -F argument too long. "
86*986fd29aSsetje "Ignoring.\n");
87*986fd29aSsetje break;
88*986fd29aSsetje }
89*986fd29aSsetje (void) strncpy(cmd_line_boot_archive,
90*986fd29aSsetje params.gos_optargp, params.gos_optarglen);
91*986fd29aSsetje cmd_line_boot_archive[params.gos_optarglen] = '\0';
927c478bd9Sstevel@tonic-gate break;
937c478bd9Sstevel@tonic-gate
947c478bd9Sstevel@tonic-gate /*
957c478bd9Sstevel@tonic-gate * Boot flags.
967c478bd9Sstevel@tonic-gate */
977c478bd9Sstevel@tonic-gate case 'V':
987c478bd9Sstevel@tonic-gate verbosemode = 1;
997c478bd9Sstevel@tonic-gate break;
1007c478bd9Sstevel@tonic-gate case 'n':
1017c478bd9Sstevel@tonic-gate cache_state = 0;
1027c478bd9Sstevel@tonic-gate printf("Warning: boot will not enable cache\n");
1037c478bd9Sstevel@tonic-gate break;
1047c478bd9Sstevel@tonic-gate
1057c478bd9Sstevel@tonic-gate case 'D':
1067c478bd9Sstevel@tonic-gate if (params.gos_optarglen >= sizeof (default_path_buf)) {
1077c478bd9Sstevel@tonic-gate printf("boot: -D argument too long. "
1087c478bd9Sstevel@tonic-gate "Ignoring.\n");
1097c478bd9Sstevel@tonic-gate break;
1107c478bd9Sstevel@tonic-gate }
1117c478bd9Sstevel@tonic-gate (void) strncpy(default_path_buf, params.gos_optargp,
1127c478bd9Sstevel@tonic-gate params.gos_optarglen);
1137c478bd9Sstevel@tonic-gate default_path_buf[params.gos_optarglen] = '\0';
1147c478bd9Sstevel@tonic-gate cmd_line_default_path = default_path_buf;
1157c478bd9Sstevel@tonic-gate break;
1167c478bd9Sstevel@tonic-gate
1177c478bd9Sstevel@tonic-gate case 'o':
1187c478bd9Sstevel@tonic-gate if (params.gos_optarglen >=
1197c478bd9Sstevel@tonic-gate sizeof (wanboot_arguments)) {
1207c478bd9Sstevel@tonic-gate printf("boot: -o argument too long. "
1217c478bd9Sstevel@tonic-gate "Ignoring.\n");
1227c478bd9Sstevel@tonic-gate break;
1237c478bd9Sstevel@tonic-gate }
1247c478bd9Sstevel@tonic-gate (void) strncpy(wanboot_arguments, params.gos_optargp,
1257c478bd9Sstevel@tonic-gate params.gos_optarglen);
1267c478bd9Sstevel@tonic-gate wanboot_arguments[params.gos_optarglen] = '\0';
1277c478bd9Sstevel@tonic-gate break;
1287c478bd9Sstevel@tonic-gate
1297c478bd9Sstevel@tonic-gate case 'a':
1307c478bd9Sstevel@tonic-gate boothowto |= RB_ASKNAME;
1317c478bd9Sstevel@tonic-gate break;
1327c478bd9Sstevel@tonic-gate
1337c478bd9Sstevel@tonic-gate case 'd':
1347c478bd9Sstevel@tonic-gate boothowto |= RB_DEBUGENTER;
1357c478bd9Sstevel@tonic-gate break;
1367c478bd9Sstevel@tonic-gate case 'v':
1377c478bd9Sstevel@tonic-gate boothowto |= RB_VERBOSE;
1387c478bd9Sstevel@tonic-gate break;
1397c478bd9Sstevel@tonic-gate case 'h':
1407c478bd9Sstevel@tonic-gate boothowto |= RB_HALT;
1417c478bd9Sstevel@tonic-gate break;
1427c478bd9Sstevel@tonic-gate
1437c478bd9Sstevel@tonic-gate /* Consumed by the kernel */
1447c478bd9Sstevel@tonic-gate case 'k':
1457c478bd9Sstevel@tonic-gate boothowto |= RB_KMDB;
1467c478bd9Sstevel@tonic-gate break;
1477c478bd9Sstevel@tonic-gate
1487c478bd9Sstevel@tonic-gate /*
1497c478bd9Sstevel@tonic-gate * Unrecognized flags: stop.
1507c478bd9Sstevel@tonic-gate */
1517c478bd9Sstevel@tonic-gate case '?':
1527c478bd9Sstevel@tonic-gate /*
1537c478bd9Sstevel@tonic-gate * Error. Either an unrecognized option, or an option
1547c478bd9Sstevel@tonic-gate * without an argument. Check for the latter.
1557c478bd9Sstevel@tonic-gate */
1567c478bd9Sstevel@tonic-gate switch (params.gos_last_opt) {
1577c478bd9Sstevel@tonic-gate case 'F':
1587c478bd9Sstevel@tonic-gate case 'I':
1597c478bd9Sstevel@tonic-gate case 'D':
1607c478bd9Sstevel@tonic-gate case 'o':
1617c478bd9Sstevel@tonic-gate printf("boot: -%c flag missing required "
1627c478bd9Sstevel@tonic-gate "argument. Ignoring.\n",
1637c478bd9Sstevel@tonic-gate params.gos_last_opt);
1647c478bd9Sstevel@tonic-gate break;
1657c478bd9Sstevel@tonic-gate default:
1667c478bd9Sstevel@tonic-gate /* Unrecognized option. Stop. */
1677c478bd9Sstevel@tonic-gate goto done;
1687c478bd9Sstevel@tonic-gate }
1697c478bd9Sstevel@tonic-gate break;
1707c478bd9Sstevel@tonic-gate
1717c478bd9Sstevel@tonic-gate default:
1727c478bd9Sstevel@tonic-gate printf("boot: Ignoring unimplemented option -%c.\n", c);
1737c478bd9Sstevel@tonic-gate }
1747c478bd9Sstevel@tonic-gate }
1757c478bd9Sstevel@tonic-gate done:
1767c478bd9Sstevel@tonic-gate
1777c478bd9Sstevel@tonic-gate /*
1787c478bd9Sstevel@tonic-gate * Construct the arguments for the standalone.
1797c478bd9Sstevel@tonic-gate */
1807c478bd9Sstevel@tonic-gate
1817c478bd9Sstevel@tonic-gate *newargs = '\0';
1827c478bd9Sstevel@tonic-gate np = newargs;
1837c478bd9Sstevel@tonic-gate
1847c478bd9Sstevel@tonic-gate /*
1857c478bd9Sstevel@tonic-gate * We need a dash if we encountered an unrecognized option or if we
1867c478bd9Sstevel@tonic-gate * need to pass flags on.
1877c478bd9Sstevel@tonic-gate */
1887c478bd9Sstevel@tonic-gate if (c == '?' || (boothowto &
1897c478bd9Sstevel@tonic-gate /* These flags are to be passed to the standalone. */
1907c478bd9Sstevel@tonic-gate (RB_ASKNAME | RB_DEBUGENTER | RB_VERBOSE | RB_HALT | RB_KMDB))) {
1917c478bd9Sstevel@tonic-gate *np++ = '-';
1927c478bd9Sstevel@tonic-gate
1937c478bd9Sstevel@tonic-gate /*
1947c478bd9Sstevel@tonic-gate * boot(1M) says to pass these on.
1957c478bd9Sstevel@tonic-gate */
1967c478bd9Sstevel@tonic-gate if (boothowto & RB_ASKNAME)
1977c478bd9Sstevel@tonic-gate *np++ = 'a';
1987c478bd9Sstevel@tonic-gate
1997c478bd9Sstevel@tonic-gate /*
2007c478bd9Sstevel@tonic-gate * boot isn't documented as consuming these flags, so pass
2017c478bd9Sstevel@tonic-gate * them on.
2027c478bd9Sstevel@tonic-gate */
2037c478bd9Sstevel@tonic-gate if (boothowto & RB_DEBUGENTER)
2047c478bd9Sstevel@tonic-gate *np++ = 'd';
2057c478bd9Sstevel@tonic-gate if (boothowto & RB_KMDB)
2067c478bd9Sstevel@tonic-gate *np++ = 'k';
2077c478bd9Sstevel@tonic-gate if (boothowto & RB_VERBOSE)
2087c478bd9Sstevel@tonic-gate *np++ = 'v';
2097c478bd9Sstevel@tonic-gate if (boothowto & RB_HALT)
2107c478bd9Sstevel@tonic-gate *np++ = 'h';
2117c478bd9Sstevel@tonic-gate
2127c478bd9Sstevel@tonic-gate /*
2137c478bd9Sstevel@tonic-gate * If we didn't encounter an unrecognized flag and there's
2147c478bd9Sstevel@tonic-gate * more to copy, add a space to separate these flags.
2157c478bd9Sstevel@tonic-gate * (Otherwise, unrecognized flags can be appended since we
2167c478bd9Sstevel@tonic-gate * started this word with a dash.)
2177c478bd9Sstevel@tonic-gate */
2187c478bd9Sstevel@tonic-gate if (c == -1 && params.gos_strp[0] != '\0')
2197c478bd9Sstevel@tonic-gate *np++ = ' ';
2207c478bd9Sstevel@tonic-gate }
2217c478bd9Sstevel@tonic-gate
2227c478bd9Sstevel@tonic-gate npres = sizeof (newargs) - (size_t)(np - newargs);
2237c478bd9Sstevel@tonic-gate
2247c478bd9Sstevel@tonic-gate if (c == '?') {
2257c478bd9Sstevel@tonic-gate /*
2267c478bd9Sstevel@tonic-gate * Unrecognized flag: Copy gos_errp to end of line or a "--"
2277c478bd9Sstevel@tonic-gate * word.
2287c478bd9Sstevel@tonic-gate */
2297c478bd9Sstevel@tonic-gate cp = params.gos_errp;
2307c478bd9Sstevel@tonic-gate while (*cp && npres > 0) {
2317c478bd9Sstevel@tonic-gate if (cp[0] == '-' && cp[1] == '-' &&
2327c478bd9Sstevel@tonic-gate (cp[2] == '\0' || ISSPACE(cp[2]))) {
2337c478bd9Sstevel@tonic-gate cp += 2;
2347c478bd9Sstevel@tonic-gate SKIP_SPC(cp);
2357c478bd9Sstevel@tonic-gate break;
2367c478bd9Sstevel@tonic-gate } else {
2377c478bd9Sstevel@tonic-gate const char *sp = cp;
2387c478bd9Sstevel@tonic-gate size_t sz;
2397c478bd9Sstevel@tonic-gate
2407c478bd9Sstevel@tonic-gate /* Copy until the next word. */
2417c478bd9Sstevel@tonic-gate while (*cp && !ISSPACE(*cp))
2427c478bd9Sstevel@tonic-gate cp++;
2437c478bd9Sstevel@tonic-gate while (ISSPACE(*cp))
2447c478bd9Sstevel@tonic-gate cp++;
2457c478bd9Sstevel@tonic-gate
2467c478bd9Sstevel@tonic-gate sz = MIN(npres, (size_t)(cp - sp));
2477c478bd9Sstevel@tonic-gate npres -= sz;
2487c478bd9Sstevel@tonic-gate bcopy(sp, np, sz);
2497c478bd9Sstevel@tonic-gate np += sz;
2507c478bd9Sstevel@tonic-gate }
2517c478bd9Sstevel@tonic-gate }
2527c478bd9Sstevel@tonic-gate } else {
2537c478bd9Sstevel@tonic-gate cp = params.gos_strp;
2547c478bd9Sstevel@tonic-gate }
2557c478bd9Sstevel@tonic-gate
2567c478bd9Sstevel@tonic-gate while (npres > 0 && (*np++ = *cp++) != '\0')
2577c478bd9Sstevel@tonic-gate npres--;
2587c478bd9Sstevel@tonic-gate
2597c478bd9Sstevel@tonic-gate newargs[sizeof (newargs) - 1] = '\0';
2607c478bd9Sstevel@tonic-gate (void) strlcpy(args, newargs, argsz);
2617c478bd9Sstevel@tonic-gate
2627c478bd9Sstevel@tonic-gate /*
2637c478bd9Sstevel@tonic-gate * If a default filename was specified in the args, set it.
2647c478bd9Sstevel@tonic-gate */
2657c478bd9Sstevel@tonic-gate if (cmd_line_default_path)
2667c478bd9Sstevel@tonic-gate set_default_filename(cmd_line_default_path);
267*986fd29aSsetje
268*986fd29aSsetje /*
269*986fd29aSsetje * See if user wants to examine things
270*986fd29aSsetje */
271*986fd29aSsetje if (halt == B_TRUE)
272*986fd29aSsetje prom_enter_mon();
2737c478bd9Sstevel@tonic-gate }
274