dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / boot / bootadm / bootadm_loader.c
blob3e424f98f9bd4cb832d65a0bde1acc989ebc6128
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright 2012 Milan Jurik. All rights reserved.
27 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
28 * Copyright 2016 Toomas Soome <tsoome@me.com>
32 * Loader menu management.
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <wchar.h>
39 #include <errno.h>
40 #include <limits.h>
41 #include <alloca.h>
42 #include <unistd.h>
43 #include <sys/types.h>
44 #include <sys/stat.h>
45 #include <sys/queue.h>
46 #include <libbe.h>
47 #include <ficl.h>
48 #include <ficlplatform/emu.h>
49 #include <ofmt.h>
51 #include "bootadm.h"
53 extern int bam_rootlen;
54 extern int bam_alt_root;
55 extern char *rootbuf;
56 extern char *bam_root;
58 #define BOOT_DIR "/boot"
59 #define CONF_DIR BOOT_DIR "/conf.d"
60 #define MENU BOOT_DIR "/menu.lst"
61 #define TRANSIENT BOOT_DIR "/transient.conf"
62 #define XEN_CONFIG CONF_DIR "/xen"
64 typedef struct menu_entry {
65 int me_idx;
66 boolean_t me_active;
67 char *me_title;
68 char *me_type;
69 char *me_bootfs;
70 STAILQ_ENTRY(menu_entry) me_next;
71 } menu_entry_t;
72 STAILQ_HEAD(menu_lst, menu_entry);
74 static error_t set_option(struct menu_lst *, char *, char *);
75 static error_t list_entry(struct menu_lst *, char *, char *);
76 static error_t update_entry(struct menu_lst *, char *, char *);
77 static error_t update_temp(struct menu_lst *, char *, char *);
78 static error_t list_setting(struct menu_lst *menu, char *, char *);
79 static error_t disable_hyper(struct menu_lst *, char *, char *);
80 static error_t enable_hyper(struct menu_lst *, char *, char *);
82 /* Menu related sub commands */
83 static subcmd_defn_t menu_subcmds[] = {
84 { "set_option", OPT_ABSENT, set_option, 0 }, /* PUB */
85 { "list_entry", OPT_OPTIONAL, list_entry, 1 }, /* PUB */
86 { "update_entry", OPT_REQ, update_entry, 0 }, /* menu */
87 { "update_temp", OPT_OPTIONAL, update_temp, 0 }, /* reboot */
88 { "list_setting", OPT_OPTIONAL, list_setting, 1 }, /* menu */
89 { "disable_hypervisor", OPT_ABSENT, disable_hyper, 0 }, /* menu */
90 { "enable_hypervisor", OPT_ABSENT, enable_hyper, 0 }, /* menu */
91 { NULL, 0, NULL, 0 } /* must be last */
94 #define NUM_COLS (5)
96 static boolean_t
97 print_menu_cb(ofmt_arg_t *ofarg, char *buf, uint_t bufsize)
99 menu_entry_t *entry = ofarg->ofmt_cbarg;
101 switch (ofarg->ofmt_id) {
102 case 0:
103 (void) snprintf(buf, bufsize, "%d", entry->me_idx);
104 break;
105 case 1:
106 (void) snprintf(buf, bufsize, "%s", entry->me_title);
107 break;
108 case 2:
109 (void) snprintf(buf, bufsize, "%s", entry->me_bootfs);
110 break;
111 case 3:
112 (void) snprintf(buf, bufsize, "%s", entry->me_type);
113 break;
114 case 4:
115 if (entry->me_active == B_TRUE)
116 (void) snprintf(buf, bufsize, " *");
117 else
118 (void) snprintf(buf, bufsize, " -");
119 break;
120 default:
121 return (B_FALSE);
123 return (B_TRUE);
126 static void
127 init_hdr_cols(ofmt_field_t *hdr)
129 uint_t i;
131 for (i = 0; i < NUM_COLS; i++) {
132 char *name = NULL;
134 switch (i) {
135 case 0:
136 name = _("INDEX");
137 break;
138 case 1:
139 name = _("NAME");
140 break;
141 case 2:
142 name = _("DEVICE");
143 break;
144 case 3:
145 name = _("TYPE");
146 break;
147 case 4:
148 name = _("DEFAULT");
149 break;
152 hdr[i].of_name = name;
153 hdr[i].of_id = i;
154 hdr[i].of_cb = print_menu_cb;
156 if (name != NULL) {
157 wchar_t wname[128];
158 size_t sz = mbstowcs(wname, name, sizeof (wname) /
159 sizeof (wchar_t));
160 if (sz > 0) {
161 int wcsw = wcswidth(wname, sz);
162 if (wcsw > 0)
163 hdr[i].of_width = wcsw;
164 else
165 hdr[i].of_width = sz;
166 } else {
167 hdr[i].of_width = strlen(name);
173 static void
174 menu_update_widths(ofmt_field_t *hdr, struct menu_lst *menu)
176 size_t len[NUM_COLS];
177 menu_entry_t *entry;
178 int i;
180 for (i = 0; i < NUM_COLS; i++)
181 len[i] = hdr[i].of_width + 1;
183 STAILQ_FOREACH(entry, menu, me_next) {
184 size_t entry_len;
186 entry_len = strlen(entry->me_title) + 1;
187 if (entry_len > len[1])
188 len[1] = entry_len;
190 entry_len = strlen(entry->me_bootfs) + 1;
191 if (entry_len > len[2])
192 len[2] = entry_len;
194 entry_len = strlen(entry->me_type) + 1;
195 if (entry_len > len[3])
196 len[3] = entry_len;
199 for (i = 0; i < NUM_COLS; i++)
200 hdr[i].of_width = len[i];
203 static ofmt_field_t *
204 init_menu_template(struct menu_lst *menu)
206 ofmt_field_t *temp;
208 if ((temp = calloc(NUM_COLS + 1, sizeof (ofmt_field_t))) == NULL)
209 return (temp);
211 init_hdr_cols(temp);
212 menu_update_widths(temp, menu);
213 return (temp);
216 static void
217 print_nodes(boolean_t parsable, struct menu_lst *menu)
219 ofmt_status_t oferr;
220 ofmt_handle_t ofmt;
221 uint_t ofmtflags = 0;
222 ofmt_field_t *menu_template;
223 menu_entry_t *entry;
225 if (parsable == B_TRUE)
226 ofmtflags = OFMT_PARSABLE;
228 menu_template = init_menu_template(menu);
229 oferr = ofmt_open(NULL, menu_template, ofmtflags, 0, &ofmt);
231 if (oferr != OFMT_SUCCESS) {
232 char buf[OFMT_BUFSIZE];
234 (void) ofmt_strerror(ofmt, oferr, buf, sizeof (buf));
235 (void) printf("bootadm: %s\n", buf);
236 free(menu_template);
237 return;
240 STAILQ_FOREACH(entry, menu, me_next)
241 ofmt_print(ofmt, entry);
243 ofmt_close(ofmt);
244 free(menu_template);
248 * Get the be_active_on_boot for bootfs.
250 static boolean_t
251 menu_active_on_boot(be_node_list_t *be_nodes, const char *bootfs)
253 be_node_list_t *be_node;
254 boolean_t rv = B_FALSE;
256 for (be_node = be_nodes; be_node != NULL;
257 be_node = be_node->be_next_node) {
258 if (strcmp(be_node->be_root_ds, bootfs) == 0) {
259 rv = be_node->be_active_on_boot;
260 break;
264 return (rv);
267 error_t
268 menu_read(struct menu_lst *menu, char *menu_path)
270 FILE *fp;
271 be_node_list_t *be_nodes;
272 menu_entry_t *mp;
273 char buf[PATH_MAX];
274 char *title;
275 char *bootfs;
276 char *type;
277 char *key, *value;
278 int i = 0;
279 int ret = BAM_SUCCESS;
281 fp = fopen(menu_path, "r");
282 if (fp == NULL)
283 return (BAM_ERROR);
285 if (be_list(NULL, &be_nodes) != BE_SUCCESS)
286 be_nodes = NULL;
289 * menu.lst entry is on two lines, one for title, one for bootfs
290 * so we process both lines in succession.
292 title = NULL;
293 type = NULL;
294 bootfs = NULL;
295 do {
296 if (fgets(buf, PATH_MAX, fp) == NULL) {
297 if (!feof(fp))
298 ret = BAM_ERROR;
299 goto done;
301 key = strtok(buf, " \n");
302 if (strcmp(key, "title") != 0) {
303 ret = BAM_ERROR;
304 goto done;
306 value = strtok(NULL, " \n");
307 if ((title = strdup(value)) == NULL) {
308 ret = BAM_ERROR;
309 goto done;
312 if (fgets(buf, PATH_MAX, fp) == NULL) {
313 ret = BAM_ERROR;
314 goto done;
317 key = strtok(buf, " \n");
318 if ((type = strdup(key)) == NULL) {
319 ret = BAM_ERROR;
320 goto done;
322 value = strtok(NULL, " \n");
323 if ((bootfs = strdup(value)) == NULL) {
324 ret = BAM_ERROR;
325 goto done;
327 if ((mp = malloc(sizeof (menu_entry_t))) == NULL) {
328 ret = BAM_ERROR;
329 goto done;
331 mp->me_idx = i++;
332 mp->me_title = title;
333 mp->me_type = type;
334 mp->me_bootfs = bootfs;
335 mp->me_active = menu_active_on_boot(be_nodes, bootfs);
336 STAILQ_INSERT_TAIL(menu, mp, me_next);
338 title = NULL;
339 type = NULL;
340 bootfs = NULL;
341 } while (feof(fp) == 0);
343 done:
344 free(title);
345 free(type);
346 free(bootfs);
347 (void) fclose(fp);
348 be_free_list(be_nodes);
349 return (ret);
352 void
353 menu_free(struct menu_lst *menu)
355 menu_entry_t *entry;
356 STAILQ_FOREACH(entry, menu, me_next) {
357 STAILQ_REMOVE_HEAD(menu, me_next);
358 free(entry->me_title);
359 free(entry->me_type);
360 free(entry->me_bootfs);
361 free(entry);
365 error_t
366 bam_loader_menu(char *subcmd, char *opt, int largc, char *largv[])
368 error_t ret;
369 char menu_path[PATH_MAX];
370 char clean_menu_root[PATH_MAX];
371 char menu_root[PATH_MAX];
372 struct stat sb;
373 error_t (*f)(struct menu_lst *, char *, char *);
374 char *special;
375 char *pool = NULL;
376 zfs_mnted_t zmnted;
377 char *zmntpt;
378 char *osdev;
379 char *osroot;
380 const char *fcn = "bam_loader_menu()";
381 struct menu_lst menu = {0};
383 STAILQ_INIT(&menu);
386 * Check arguments
388 ret = check_subcmd_and_options(subcmd, opt, menu_subcmds, &f);
389 if (ret == BAM_ERROR) {
390 return (BAM_ERROR);
393 assert(bam_root);
395 (void) strlcpy(menu_root, bam_root, sizeof (menu_root));
396 osdev = osroot = NULL;
398 if (strcmp(subcmd, "update_entry") == 0) {
399 assert(opt);
401 osdev = strtok(opt, ",");
402 assert(osdev);
403 osroot = strtok(NULL, ",");
404 if (osroot) {
405 /* fixup bam_root so that it points at osroot */
406 if (realpath(osroot, rootbuf) == NULL) {
407 bam_error(_("cannot resolve path %s: %s\n"),
408 osroot, strerror(errno));
409 return (BAM_ERROR);
411 bam_alt_root = 1;
412 bam_root = rootbuf;
413 bam_rootlen = strlen(rootbuf);
417 if (stat(menu_root, &sb) == -1) {
418 bam_error(_("cannot find menu\n"));
419 return (BAM_ERROR);
422 if (!is_zfs(menu_root)) {
423 bam_error(_("only ZFS root is supported\n"));
424 return (BAM_ERROR);
427 assert(strcmp(menu_root, bam_root) == 0);
428 special = get_special(menu_root);
429 INJECT_ERROR1("Z_MENU_GET_SPECIAL", special = NULL);
430 if (special == NULL) {
431 bam_error(_("cant find special file for mount-point %s\n"),
432 menu_root);
433 return (BAM_ERROR);
435 pool = strtok(special, "/");
436 INJECT_ERROR1("Z_MENU_GET_POOL", pool = NULL);
437 if (pool == NULL) {
438 free(special);
439 bam_error(_("cant find pool for mount-point %s\n"), menu_root);
440 return (BAM_ERROR);
442 BAM_DPRINTF(("%s: derived pool=%s from special\n", fcn, pool));
444 zmntpt = mount_top_dataset(pool, &zmnted);
445 INJECT_ERROR1("Z_MENU_MOUNT_TOP_DATASET", zmntpt = NULL);
446 if (zmntpt == NULL) {
447 bam_error(_("cannot mount pool dataset for pool: %s\n"), pool);
448 free(special);
449 return (BAM_ERROR);
451 BAM_DPRINTF(("%s: top dataset mountpoint=%s\n", fcn, zmntpt));
453 (void) strlcpy(menu_root, zmntpt, sizeof (menu_root));
454 BAM_DPRINTF(("%s: zfs menu_root=%s\n", fcn, menu_root));
456 elide_trailing_slash(menu_root, clean_menu_root,
457 sizeof (clean_menu_root));
459 BAM_DPRINTF(("%s: cleaned menu root is <%s>\n", fcn, clean_menu_root));
461 (void) strlcpy(menu_path, clean_menu_root, sizeof (menu_path));
462 (void) strlcat(menu_path, MENU, sizeof (menu_path));
464 BAM_DPRINTF(("%s: menu path is: %s\n", fcn, menu_path));
467 * update_entry is special case, its used by installer
468 * and needs to create menu.lst file for loader
470 if (menu_read(&menu, menu_path) == BAM_ERROR &&
471 strcmp(subcmd, "update_entry") != 0) {
472 bam_error(_("cannot find menu file: %s\n"), menu_path);
473 if (special != NULL)
474 free(special);
475 return (BAM_ERROR);
479 * If listing the menu, display the menu location
481 if (strcmp(subcmd, "list_entry") == 0)
482 bam_print(_("the location for the active menu is: %s\n"),
483 menu_path);
486 * We already checked the following case in
487 * check_subcmd_and_suboptions() above. Complete the
488 * final step now.
490 if (strcmp(subcmd, "set_option") == 0) {
491 assert(largc == 1 && largv[0] && largv[1] == NULL);
492 opt = largv[0];
493 } else if ((strcmp(subcmd, "enable_hypervisor") != 0) &&
494 (strcmp(subcmd, "list_setting") != 0)) {
495 assert(largc == 0 && largv == NULL);
499 * Once the sub-cmd handler has run
500 * only the line field is guaranteed to have valid values
502 if (strcmp(subcmd, "update_entry") == 0) {
503 ret = f(&menu, menu_root, osdev);
504 } else if (strcmp(subcmd, "upgrade") == 0) {
505 ret = f(&menu, bam_root, menu_root);
506 } else if (strcmp(subcmd, "list_entry") == 0) {
507 ret = f(&menu, menu_path, opt);
508 } else if (strcmp(subcmd, "list_setting") == 0) {
509 ret = f(&menu, ((largc > 0) ? largv[0] : ""),
510 ((largc > 1) ? largv[1] : ""));
511 } else if (strcmp(subcmd, "disable_hypervisor") == 0) {
512 ret = f(&menu, bam_root, NULL);
513 } else if (strcmp(subcmd, "enable_hypervisor") == 0) {
514 char *extra_args = NULL;
517 * Compress all arguments passed in the largv[] array
518 * into one string that can then be appended to the
519 * end of the kernel$ string the routine to enable the
520 * hypervisor will build.
522 * This allows the caller to supply arbitrary unparsed
523 * arguments, such as dom0 memory settings or APIC
524 * options.
526 * This concatenation will be done without ANY syntax
527 * checking whatsoever, so it's the responsibility of
528 * the caller to make sure the arguments are valid and
529 * do not duplicate arguments the conversion routines
530 * may create.
532 if (largc > 0) {
533 int extra_len, i;
535 for (extra_len = 0, i = 0; i < largc; i++)
536 extra_len += strlen(largv[i]);
539 * Allocate space for argument strings,
540 * intervening spaces and terminating NULL.
542 extra_args = alloca(extra_len + largc);
544 (void) strcpy(extra_args, largv[0]);
546 for (i = 1; i < largc; i++) {
547 (void) strcat(extra_args, " ");
548 (void) strcat(extra_args, largv[i]);
552 ret = f(&menu, bam_root, extra_args);
553 } else
554 ret = f(&menu, NULL, opt);
556 if (ret == BAM_WRITE) {
557 BAM_DPRINTF(("%s: writing menu to clean-menu-root: <%s>\n",
558 fcn, clean_menu_root));
559 /* ret = menu_write(clean_menu_root, menu); */
562 INJECT_ERROR1("POOL_SET", pool = "/pooldata");
563 assert((is_zfs(menu_root)) ^ (pool == NULL));
564 if (pool) {
565 (void) umount_top_dataset(pool, zmnted, zmntpt);
566 free(special);
569 menu_free(&menu);
570 return (ret);
574 * To suppress output from ficl. We do not want to see messages
575 * from interpreting loader config.
578 /*ARGSUSED*/
579 static void
580 ficlTextOutSilent(ficlCallback *cb, char *text)
584 /*ARGSUSED*/
585 static error_t
586 set_option(struct menu_lst *menu, char *dummy, char *opt)
588 char path[PATH_MAX];
589 char *val;
590 char *rest;
591 int optval;
592 menu_entry_t *entry;
593 nvlist_t *be_attrs;
594 FILE *fp;
595 int rv, ret = BAM_SUCCESS;
597 assert(menu);
598 assert(opt);
599 assert(dummy == NULL);
601 val = strchr(opt, '=');
602 if (val != NULL) {
603 *val++ = '\0';
606 if (strcmp(opt, "default") == 0) {
607 errno = 0;
608 optval = strtol(val, &rest, 10);
609 if (errno != 0 || *rest != '\0') {
610 bam_error(_("invalid boot entry number: %s\n"), val);
611 return (BAM_ERROR);
613 STAILQ_FOREACH(entry, menu, me_next) {
614 if (entry->me_idx == optval)
615 break;
617 if (entry == NULL) {
618 bam_error(_("invalid boot entry number: %s\n"), val);
619 return (BAM_ERROR);
621 if (nvlist_alloc(&be_attrs, NV_UNIQUE_NAME, 0) != 0) {
622 bam_error(_("out of memory\n"));
623 return (BAM_ERROR);
625 if (nvlist_add_string(be_attrs, BE_ATTR_ORIG_BE_NAME,
626 entry->me_title) != 0) {
627 bam_error(_("out of memory\n"));
628 nvlist_free(be_attrs);
629 return (BAM_ERROR);
631 ret = be_activate(be_attrs);
632 nvlist_free(be_attrs);
633 if (ret != 0)
634 ret = BAM_ERROR;
635 return (ret);
636 } else if (strcmp(opt, "timeout") == 0) {
637 errno = 0;
638 optval = strtol(val, &rest, 10);
639 if (errno != 0 || *rest != '\0') {
640 bam_error(_("invalid timeout: %s\n"), val);
641 return (BAM_ERROR);
644 (void) snprintf(path, PATH_MAX, "%s" CONF_DIR "/timeout",
645 bam_root);
647 fp = fopen(path, "w");
648 if (fp == NULL) {
649 bam_error(_("failed to open file: %s: %s\n"),
650 path, strerror(errno));
651 return (BAM_ERROR);
654 * timeout=-1 is to disable auto boot in illumos, but
655 * loader needs "NO" to disable auto boot.
657 if (optval == -1)
658 rv = fprintf(fp, "autoboot_delay=\"NO\"\n");
659 else
660 rv = fprintf(fp, "autoboot_delay=\"%d\"\n", optval);
662 if (rv < 0) {
663 bam_error(_("write to file failed: %s: %s\n"),
664 path, strerror(errno));
665 (void) fclose(fp);
666 ret = BAM_ERROR;
667 } else
668 rv = fclose(fp);
670 if (rv < 0) {
671 bam_error(_("failed to close file: %s: %s\n"),
672 path, strerror(errno));
673 ret = BAM_ERROR;
675 if (ret == BAM_ERROR)
676 (void) unlink(path);
678 return (BAM_SUCCESS);
681 bam_error(_("invalid option: %s\n"), opt);
682 return (BAM_ERROR);
685 static int
686 bam_mount_be(menu_entry_t *entry, char **dir)
688 nvlist_t *be_attrs = NULL;
689 const char *tmpdir = getenv("TMPDIR");
690 const char *tmpname = "bam.XXXXXX";
691 be_node_list_t *be_node, *be_nodes = NULL;
692 int ret;
694 *dir = NULL;
695 if (tmpdir == NULL)
696 tmpdir = "/tmp";
698 ret = asprintf(dir, "%s/%s", tmpdir, tmpname);
699 if (ret < 0) {
700 return (BE_ERR_NOMEM);
702 *dir = mkdtemp(*dir);
704 if (nvlist_alloc(&be_attrs, NV_UNIQUE_NAME, 0) != 0) {
705 ret = BE_ERR_NOMEM;
706 goto out;
709 ret = be_list(NULL, &be_nodes);
710 if (ret != BE_SUCCESS) {
711 goto out;
714 for (be_node = be_nodes; be_node;
715 be_node = be_node->be_next_node)
716 if (strcmp(be_node->be_root_ds, entry->me_bootfs) == 0)
717 break;
719 if (nvlist_add_string(be_attrs, BE_ATTR_ORIG_BE_NAME,
720 be_node->be_node_name) != 0) {
721 ret = BE_ERR_NOMEM;
722 goto out;
725 if (nvlist_add_string(be_attrs, BE_ATTR_MOUNTPOINT, *dir) != 0) {
726 ret = BE_ERR_NOMEM;
727 goto out;
730 ret = be_mount(be_attrs);
731 if (ret == BE_ERR_MOUNTED) {
733 * if BE is mounted, dir does not point to correct directory
735 (void) rmdir(*dir);
736 free(*dir);
737 *dir = NULL;
739 out:
740 if (be_nodes != NULL)
741 be_free_list(be_nodes);
742 nvlist_free(be_attrs);
743 return (ret);
746 static int
747 bam_umount_be(char *dir)
749 nvlist_t *be_attrs;
750 int ret;
752 if (dir == NULL) /* nothing to do */
753 return (BE_SUCCESS);
755 if (nvlist_alloc(&be_attrs, NV_UNIQUE_NAME, 0) != 0)
756 return (BE_ERR_NOMEM);
758 if (nvlist_add_string(be_attrs, BE_ATTR_ORIG_BE_NAME, dir) != 0) {
759 ret = BE_ERR_NOMEM;
760 goto out;
763 ret = be_unmount(be_attrs);
764 out:
765 nvlist_free(be_attrs);
766 return (ret);
770 * display details of menu entry or single property
772 static error_t
773 list_menu_entry(menu_entry_t *entry, char *setting)
775 int ret = BAM_SUCCESS;
776 char *ptr, *dir;
777 char buf[MAX_INPUT];
778 ficlVm *vm;
779 int mounted;
781 if (strcmp(entry->me_type, "bootfs") != 0 ||
782 strchr(entry->me_bootfs, ':') != NULL) {
783 (void) printf("\nTitle: %s\n", entry->me_title);
784 (void) printf("Type: %s\n", entry->me_type);
785 (void) printf("Device: %s\n", entry->me_bootfs);
786 return (ret);
789 mounted = bam_mount_be(entry, &dir);
790 if (mounted != BE_SUCCESS && mounted != BE_ERR_MOUNTED) {
791 if (dir != NULL) {
792 (void) rmdir(dir);
793 free(dir);
795 bam_error(_("%s is not mounted\n"), entry->me_title);
796 return (BAM_ERROR);
799 vm = bf_init("", ficlTextOutSilent);
800 if (vm == NULL) {
801 bam_error(_("error setting up forth interpreter\n"));
802 ret = BAM_ERROR;
803 goto done;
806 /* should only get FICL_VM_STATUS_OUT_OF_TEXT */
807 (void) snprintf(buf, MAX_INPUT, "set currdev=zfs:%s:",
808 entry->me_bootfs);
809 ret = ficlVmEvaluate(vm, buf);
810 if (ret != FICL_VM_STATUS_OUT_OF_TEXT) {
811 bam_error(_("error interpreting boot config\n"));
812 ret = BAM_ERROR;
813 goto done;
815 (void) snprintf(buf, MAX_INPUT, "include /boot/forth/loader.4th");
816 ret = ficlVmEvaluate(vm, buf);
817 if (ret != FICL_VM_STATUS_OUT_OF_TEXT) {
818 bam_error(_("error interpreting boot config\n"));
819 ret = BAM_ERROR;
820 goto done;
822 (void) snprintf(buf, MAX_INPUT, "start");
823 ret = ficlVmEvaluate(vm, buf);
824 if (ret != FICL_VM_STATUS_OUT_OF_TEXT) {
825 bam_error(_("error interpreting boot config\n"));
826 ret = BAM_ERROR;
827 goto done;
829 (void) snprintf(buf, MAX_INPUT, "boot");
830 ret = ficlVmEvaluate(vm, buf);
831 if (ret != FICL_VM_STATUS_OUT_OF_TEXT) {
832 bam_error(_("error interpreting boot config\n"));
833 ret = BAM_ERROR;
834 goto done;
837 ret = BAM_SUCCESS;
838 if (*setting == '\0')
839 (void) printf("\nTitle: %s\n", entry->me_title);
840 else if (strcasecmp(setting, "title") == 0) {
841 (void) printf("%s\n", entry->me_title);
842 goto done;
845 ptr = getenv("autoboot_delay");
846 if (ptr != NULL) {
847 char *timeout = "-1";
849 if (strcasecmp(ptr, "NO") != 0)
850 timeout = ptr;
852 if (*setting == '\0')
853 (void) printf("Timeout: %s\n", timeout);
854 else if (strcasecmp(setting, "timeout") == 0) {
855 (void) printf("%s\n", timeout);
856 goto done;
860 ptr = getenv("console");
861 if (ptr != NULL) {
862 if (*setting == '\0')
863 (void) printf("Console: %s\n", ptr);
864 else if (strcasecmp(setting, "console") == 0) {
865 (void) printf("%s\n", ptr);
866 goto done;
870 if (*setting == '\0')
871 (void) printf("Bootfs: %s\n", entry->me_bootfs);
872 else if (strcasecmp(setting, "bootfs") == 0) {
873 (void) printf("%s\n", entry->me_bootfs);
874 goto done;
877 ptr = getenv("xen_kernel");
878 if (ptr != NULL) {
879 if (*setting == '\0') {
880 (void) printf("Xen kernel: %s\n", ptr);
881 } else if (strcasecmp(setting, "xen_kernel") == 0) {
882 (void) printf("%s\n", ptr);
883 goto done;
886 if (*setting == '\0') {
887 (void) printf("Xen args: \"%s\"\n",
888 getenv("xen_cmdline"));
889 } else if (strcasecmp(setting, "xen_cmdline") == 0) {
890 (void) printf("%s\n", getenv("xen_cmdline"));
891 goto done;
894 if (*setting == '\0') {
895 (void) printf("Kernel: %s\n",
896 getenv("bootfile"));
897 } if (strcasecmp(setting, "kernel") == 0) {
898 (void) printf("%s\n", getenv("bootfile"));
899 goto done;
901 } else {
902 ptr = getenv("kernelname");
903 if (ptr != NULL) {
904 if (*setting == '\0') {
905 (void) printf("Kernel: %s\n", ptr);
906 } else if (strcasecmp(setting, "kernel") == 0) {
907 (void) printf("%s\n", ptr);
908 goto done;
913 ptr = getenv("boot-args");
914 if (ptr != NULL) {
915 if (*setting == '\0') {
916 (void) printf("Boot-args: \"%s\"\n", ptr);
917 } else if (strcasecmp(setting, "boot-args") == 0) {
918 (void) printf("%s\n", ptr);
919 goto done;
923 if (*setting == '\0' || strcasecmp(setting, "modules") == 0) {
924 (void) printf("\nModules:\n");
925 ficlVmSetTextOut(vm, ficlCallbackDefaultTextOut);
926 (void) snprintf(buf, MAX_INPUT, "show-module-options");
927 ret = ficlVmEvaluate(vm, buf);
928 if (ret != FICL_VM_STATUS_OUT_OF_TEXT) {
929 bam_error(_("error interpreting boot config\n"));
930 ret = BAM_ERROR;
931 goto done;
933 ret = BAM_SUCCESS;
934 goto done;
937 /* if we got here with setting string, its unknown property */
938 if (*setting != '\0') {
939 bam_error(_("unknown property: %s\n"), setting);
940 ret = BAM_ERROR;
941 } else
942 ret = BAM_SUCCESS;
943 done:
944 bf_fini();
945 if (mounted != BE_ERR_MOUNTED) {
946 (void) bam_umount_be(dir);
949 if (dir != NULL) {
950 (void) rmdir(dir);
951 free(dir);
954 return (ret);
957 /*ARGSUSED*/
958 static error_t
959 list_entry(struct menu_lst *menu, char *menu_root, char *opt)
961 error_t ret = BAM_SUCCESS;
962 menu_entry_t *entry;
963 char *ptr, *title = NULL;
964 int i, e = -1;
966 if (opt == NULL) {
967 print_nodes(B_FALSE, menu);
968 return (ret);
971 if ((ptr = strchr(opt, '=')) == NULL) {
972 bam_error(_("invalid option: %s\n"), opt);
973 return (BAM_ERROR);
976 i = ptr - opt;
977 if (strncmp(opt, "entry", i) == 0) {
978 e = atoi(ptr+1);
979 } else if (strncmp(opt, "title", i) == 0) {
980 title = ptr+1;
981 } else {
982 bam_error(_("invalid option: %s\n"), opt);
983 return (BAM_ERROR);
986 STAILQ_FOREACH(entry, menu, me_next) {
987 if (title != NULL) {
988 if (strcmp(title, entry->me_title) == 0)
989 break;
990 } else if (entry->me_idx == e)
991 break;
994 if (entry == NULL) {
995 bam_error(_("no matching entry found\n"));
996 return (BAM_ERROR);
999 return (list_menu_entry(entry, ""));
1003 * For now this is just stub entry to support grub interface, the
1004 * known consumer is installer ict.py code, calling as:
1005 * bootadm update-menu -R /a -Z -o rdisk
1006 * Later this can be converted to do something useful.
1008 /*ARGSUSED*/
1009 static error_t
1010 update_entry(struct menu_lst *menu, char *menu_root, char *osdev)
1012 char path[PATH_MAX];
1013 char *pool = menu_root + 1;
1014 be_node_list_t *be_nodes, *be_node;
1015 int rv;
1016 FILE *fp;
1018 (void) snprintf(path, PATH_MAX, "%s%s", menu_root, MENU);
1019 rv = be_list(NULL, &be_nodes);
1021 if (rv != BE_SUCCESS)
1022 return (BAM_ERROR);
1024 fp = fopen(path, "w");
1025 if (fp == NULL) {
1026 be_free_list(be_nodes);
1027 return (BAM_ERROR);
1030 for (be_node = be_nodes; be_node; be_node = be_node->be_next_node) {
1031 if (strcmp(be_node->be_rpool, pool) == 0) {
1032 (void) fprintf(fp, "title %s\n", be_node->be_node_name);
1033 (void) fprintf(fp, "bootfs %s\n", be_node->be_root_ds);
1037 be_free_list(be_nodes);
1038 (void) fclose(fp);
1039 return (BAM_SUCCESS);
1042 /*ARGSUSED*/
1043 static error_t
1044 update_temp(struct menu_lst *menu, char *dummy, char *opt)
1046 error_t ret = BAM_ERROR;
1047 char path[PATH_MAX];
1048 char buf[MAX_INPUT];
1049 struct mnttab mpref = { 0 };
1050 struct mnttab mp = { 0 };
1051 ficlVm *vm;
1052 char *env, *o;
1053 FILE *fp;
1055 (void) snprintf(path, PATH_MAX, "%s" TRANSIENT, bam_root);
1057 * if opt == NULL, remove transient config
1059 if (opt == NULL) {
1060 (void) unlink(path);
1061 return (BAM_SUCCESS);
1064 fp = fopen(MNTTAB, "r");
1065 if (fp == NULL)
1066 return (BAM_ERROR);
1068 mpref.mnt_mountp = "/";
1069 if (getmntany(fp, &mp, &mpref) != 0) {
1070 (void) fclose(fp);
1071 return (BAM_ERROR);
1073 (void) fclose(fp);
1075 vm = bf_init("", ficlTextOutSilent);
1076 if (vm == NULL) {
1077 bam_error(_("Error setting up forth interpreter\n"));
1078 return (ret);
1082 * need to check current boot config, so fire up the ficl
1083 * if its xen setup, we add option to boot-args list, not replacing it.
1085 (void) snprintf(buf, MAX_INPUT, "set currdev=zfs:%s:", mp.mnt_special);
1086 ret = ficlVmEvaluate(vm, buf);
1087 if (ret != FICL_VM_STATUS_OUT_OF_TEXT) {
1088 bam_error(_("Error interpreting boot config\n"));
1089 bf_fini();
1090 return (BAM_ERROR);
1092 (void) snprintf(buf, MAX_INPUT, "include /boot/forth/loader.4th");
1093 ret = ficlVmEvaluate(vm, buf);
1094 if (ret != FICL_VM_STATUS_OUT_OF_TEXT) {
1095 bam_error(_("Error interpreting boot config\n"));
1096 bf_fini();
1097 return (BAM_ERROR);
1099 (void) snprintf(buf, MAX_INPUT, "start");
1100 ret = ficlVmEvaluate(vm, buf);
1101 if (ret != FICL_VM_STATUS_OUT_OF_TEXT) {
1102 bam_error(_("Error interpreting boot config\n"));
1103 bf_fini();
1104 return (BAM_ERROR);
1106 (void) snprintf(buf, MAX_INPUT, "boot");
1107 ret = ficlVmEvaluate(vm, buf);
1108 if (ret != FICL_VM_STATUS_OUT_OF_TEXT) {
1109 bam_error(_("Error interpreting boot config\n"));
1110 bf_fini();
1111 return (BAM_ERROR);
1113 bf_fini();
1115 if (opt[0] == '-') {
1116 env = getenv("xen_kernel");
1117 fp = fopen(path, "w");
1118 if (fp == NULL)
1119 return (BAM_ERROR);
1121 if (env != NULL) {
1122 env = getenv("boot-args");
1123 (void) fprintf(fp, "boot-args=\"%s %s\"\n", env, opt);
1124 } else
1125 (void) fprintf(fp, "boot-args=\"%s\"\n", opt);
1126 (void) fclose(fp);
1127 return (BAM_SUCCESS);
1131 * it should be the case with "kernel args"
1132 * so, we split the opt at first space
1133 * and store bootfile= and boot-args=
1135 env = getenv("xen_kernel");
1137 o = strchr(opt, ' ');
1138 if (o == NULL) {
1139 fp = fopen(path, "w");
1140 if (fp == NULL)
1141 return (BAM_ERROR);
1142 (void) fprintf(fp, "bootfile=\"%s\"\n", opt);
1143 (void) fclose(fp);
1144 return (BAM_SUCCESS);
1146 *o++ = '\0';
1147 fp = fopen(path, "w");
1148 if (fp == NULL)
1149 return (BAM_ERROR);
1150 (void) fprintf(fp, "bootfile=\"%s\"\n", opt);
1152 if (env != NULL) {
1153 env = getenv("boot-args");
1154 (void) fprintf(fp, "boot-args=\"%s %s\"\n", env, opt);
1155 } else
1156 (void) fprintf(fp, "boot-args=\"%s\"\n", o);
1158 (void) fflush(fp);
1159 (void) fclose(fp);
1160 return (ret);
1163 static error_t
1164 list_setting(struct menu_lst *menu, char *which, char *setting)
1166 int entry = -1;
1167 menu_entry_t *m;
1168 be_node_list_t *be_nodes, *be_node = NULL;
1169 int ret;
1171 assert(which);
1172 assert(setting);
1175 * which can be:
1176 * "" - list default entry
1177 * number - use for entry number
1178 * property name
1180 if (*which != '\0') {
1181 if (isdigit(*which)) {
1182 char *rest;
1183 errno = 0;
1184 entry = strtol(which, &rest, 10);
1185 if (errno != 0 || *rest != '\0') {
1186 bam_error(_("invalid boot entry number: %s\n"),
1187 which);
1188 return (BAM_ERROR);
1190 } else
1191 setting = which;
1194 /* find default entry */
1195 if (entry == -1) {
1196 ret = be_list(NULL, &be_nodes);
1197 if (ret != BE_SUCCESS) {
1198 bam_error(_("No BE's found\n"));
1199 return (BAM_ERROR);
1201 STAILQ_FOREACH(m, menu, me_next) {
1202 entry++;
1203 for (be_node = be_nodes; be_node;
1204 be_node = be_node->be_next_node) {
1205 if (strcmp(be_node->be_root_ds,
1206 m->me_bootfs) == 0)
1207 break;
1209 if (be_node != NULL &&
1210 be_node->be_active_on_boot == B_TRUE)
1211 break; /* found active node */
1213 be_free_list(be_nodes);
1214 if (be_node == NULL) {
1215 bam_error(_("None of BE nodes is marked active\n"));
1216 return (BAM_ERROR);
1218 } else {
1219 STAILQ_FOREACH(m, menu, me_next)
1220 if (m->me_idx == entry)
1221 break;
1223 if (m == NULL) {
1224 bam_error(_("no matching entry found\n"));
1225 return (BAM_ERROR);
1229 return (list_menu_entry(m, setting));
1232 /*ARGSUSED*/
1233 static error_t
1234 disable_hyper(struct menu_lst *menu, char *osroot, char *opt)
1236 char path[PATH_MAX];
1238 (void) snprintf(path, PATH_MAX, "%s" XEN_CONFIG, bam_root);
1239 (void) unlink(path);
1240 return (BAM_SUCCESS);
1243 /*ARGSUSED*/
1244 static error_t
1245 enable_hyper(struct menu_lst *menu, char *osroot, char *opt)
1247 ficlVm *vm;
1248 char path[PATH_MAX];
1249 char buf[MAX_INPUT];
1250 char *env;
1251 FILE *fp;
1252 struct mnttab mpref = { 0 };
1253 struct mnttab mp = { 0 };
1254 int ret;
1256 fp = fopen(MNTTAB, "r");
1257 if (fp == NULL)
1258 return (BAM_ERROR);
1260 mpref.mnt_mountp = "/";
1261 if (getmntany(fp, &mp, &mpref) != 0) {
1262 (void) fclose(fp);
1263 return (BAM_ERROR);
1265 (void) fclose(fp);
1267 vm = bf_init("", ficlTextOutSilent);
1268 if (vm == NULL) {
1269 bam_error(_("Error setting up forth interpreter\n"));
1270 return (BAM_ERROR);
1274 * need to check current boot config, so fire up the ficl
1275 * if its xen setup, we add option to boot-args list, not replacing it.
1277 (void) snprintf(buf, MAX_INPUT, "set currdev=zfs:%s:", mp.mnt_special);
1278 ret = ficlVmEvaluate(vm, buf);
1279 if (ret != FICL_VM_STATUS_OUT_OF_TEXT) {
1280 bam_error(_("Error interpreting boot config\n"));
1281 bf_fini();
1282 return (BAM_ERROR);
1284 (void) snprintf(buf, MAX_INPUT, "include /boot/forth/loader.4th");
1285 ret = ficlVmEvaluate(vm, buf);
1286 if (ret != FICL_VM_STATUS_OUT_OF_TEXT) {
1287 bam_error(_("Error interpreting boot config\n"));
1288 bf_fini();
1289 return (BAM_ERROR);
1291 (void) snprintf(buf, MAX_INPUT, "start");
1292 ret = ficlVmEvaluate(vm, buf);
1293 if (ret != FICL_VM_STATUS_OUT_OF_TEXT) {
1294 bam_error(_("Error interpreting boot config\n"));
1295 bf_fini();
1296 return (BAM_ERROR);
1298 (void) snprintf(buf, MAX_INPUT, "boot");
1299 ret = ficlVmEvaluate(vm, buf);
1300 if (ret != FICL_VM_STATUS_OUT_OF_TEXT) {
1301 bam_error(_("Error interpreting boot config\n"));
1302 bf_fini();
1303 return (BAM_ERROR);
1305 bf_fini();
1307 (void) mkdir(CONF_DIR, 0755);
1308 (void) snprintf(path, PATH_MAX, "%s" XEN_CONFIG, bam_root);
1309 fp = fopen(path, "w");
1310 if (fp == NULL) {
1311 return (BAM_ERROR); /* error, cant write config */
1314 errno = 0;
1316 * on write error, remove file to ensure we have bootable config.
1317 * note we dont mind if config exists, it will get updated
1319 (void) fprintf(fp, "xen_kernel=\"/boot/${ISADIR}/xen\"\n");
1320 if (errno != 0)
1321 goto error;
1324 * really simple and stupid console conversion.
1325 * it really has to be gone, it belongs to milestone/xvm properties.
1327 env = getenv("console");
1328 if (env != NULL) {
1329 if (strcmp(env, "ttya") == 0)
1330 (void) fprintf(fp, "xen_cmdline=\"console=com1 %s\"\n",
1331 opt);
1332 else if (strcmp(env, "ttyb") == 0)
1333 (void) fprintf(fp, "xen_cmdline=\"console=com2 %s\"\n",
1334 opt);
1335 else
1336 (void) fprintf(fp, "xen_cmdline=\"console=vga %s\"\n",
1337 opt);
1338 } else
1339 (void) fprintf(fp, "xen_cmdline=\"%s\"\n", opt);
1340 if (errno != 0)
1341 goto error;
1343 (void) fprintf(fp,
1344 "bootfile=\"/platform/i86xpv/kernel/${ISADIR}/unix\"\n");
1345 if (errno != 0)
1346 goto error;
1348 (void) fprintf(fp,
1349 "boot-args=\"/platform/i86xpv/kernel/${ISADIR}/unix\"\n");
1350 if (errno != 0)
1351 goto error;
1353 (void) fclose(fp);
1354 if (errno != 0) {
1355 (void) unlink(path);
1356 return (BAM_ERROR);
1358 return (BAM_SUCCESS);
1359 error:
1360 (void) fclose(fp);
1361 (void) unlink(path);
1362 return (BAM_ERROR);