13446Smrj /* 23446Smrj * CDDL HEADER START 33446Smrj * 43446Smrj * The contents of this file are subject to the terms of the 53446Smrj * Common Development and Distribution License (the "License"). 63446Smrj * You may not use this file except in compliance with the License. 73446Smrj * 83446Smrj * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 93446Smrj * or http://www.opensolaris.org/os/licensing. 103446Smrj * See the License for the specific language governing permissions 113446Smrj * and limitations under the License. 123446Smrj * 133446Smrj * When distributing Covered Code, include this CDDL HEADER in each 143446Smrj * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 153446Smrj * If applicable, add the following below this CDDL HEADER, with the 163446Smrj * fields enclosed by brackets "[]" replaced with your own identifying 173446Smrj * information: Portions Copyright [yyyy] [name of copyright owner] 183446Smrj * 193446Smrj * CDDL HEADER END 203446Smrj */ 213446Smrj /* 223446Smrj * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 233446Smrj * Use is subject to license terms. 243446Smrj */ 253446Smrj 263446Smrj #pragma ident "%Z%%M% %I% %E% SMI" 273446Smrj 283446Smrj #include "benv.h" 293446Smrj #include "message.h" 303446Smrj #include <ctype.h> 313446Smrj #include <stdarg.h> 323446Smrj #include <sys/mman.h> 333446Smrj #include <unistd.h> 343446Smrj #include <signal.h> 353446Smrj #include <sys/wait.h> 363446Smrj 373446Smrj /* 383446Smrj * Usage: % eeprom [-v] [-f prom_dev] [-] 393446Smrj * % eeprom [-v] [-f prom_dev] field[=value] ... 403446Smrj */ 413446Smrj 423446Smrj extern void get_kbenv(void); 433446Smrj extern void close_kbenv(void); 443446Smrj extern caddr_t get_propval(char *name, char *node); 453446Smrj extern void setprogname(char *prog); 463446Smrj 473446Smrj char *boottree; 483446Smrj struct utsname uts_buf; 493446Smrj 503446Smrj static int test; 513446Smrj int verbose; 523446Smrj 533446Smrj /* 543446Smrj * Concatenate a NULL terminated list of strings into 553446Smrj * a single string. 563446Smrj */ 573446Smrj char * 583446Smrj strcats(char *s, ...) 593446Smrj { 603446Smrj char *cp, *ret; 613446Smrj size_t len; 623446Smrj va_list ap; 633446Smrj 643446Smrj va_start(ap, s); 653446Smrj for (ret = NULL, cp = s; cp; cp = va_arg(ap, char *)) { 663446Smrj if (ret == NULL) { 673446Smrj ret = strdup(s); 683446Smrj len = strlen(ret) + 1; 693446Smrj } else { 703446Smrj len += strlen(cp); 713446Smrj ret = realloc(ret, len); 723446Smrj (void) strcat(ret, cp); 733446Smrj } 743446Smrj } 753446Smrj va_end(ap); 763446Smrj 773446Smrj return (ret); 783446Smrj } 793446Smrj 803446Smrj eplist_t * 813446Smrj new_list(void) 823446Smrj { 833446Smrj eplist_t *list; 843446Smrj 853446Smrj list = (eplist_t *)malloc(sizeof (eplist_t)); 863446Smrj (void) memset(list, 0, sizeof (eplist_t)); 873446Smrj 883446Smrj list->next = list; 893446Smrj list->prev = list; 903446Smrj list->item = NULL; 913446Smrj 923446Smrj return (list); 933446Smrj } 943446Smrj 953446Smrj void 963446Smrj add_item(void *item, eplist_t *list) 973446Smrj { 983446Smrj eplist_t *entry; 993446Smrj 1003446Smrj entry = (eplist_t *)malloc(sizeof (eplist_t)); 1013446Smrj (void) memset(entry, 0, sizeof (eplist_t)); 1023446Smrj entry->item = item; 1033446Smrj 1043446Smrj entry->next = list; 1053446Smrj entry->prev = list->prev; 1063446Smrj list->prev->next = entry; 1073446Smrj list->prev = entry; 1083446Smrj } 1093446Smrj 1103446Smrj typedef struct benv_ent { 1113446Smrj char *cmd; 1123446Smrj char *name; 1133446Smrj char *val; 1143446Smrj } benv_ent_t; 1153446Smrj 1163446Smrj typedef struct benv_des { 1173446Smrj char *name; 1183446Smrj int fd; 1193446Smrj caddr_t adr; 1203446Smrj size_t len; 1213446Smrj eplist_t *elist; 1223446Smrj } benv_des_t; 1233446Smrj 1243446Smrj static benv_des_t * 1253446Smrj new_bd(void) 1263446Smrj { 1273446Smrj 1283446Smrj benv_des_t *bd; 1293446Smrj 1303446Smrj bd = (benv_des_t *)malloc(sizeof (benv_des_t)); 1313446Smrj (void) memset(bd, 0, sizeof (benv_des_t)); 1323446Smrj 1333446Smrj bd->elist = new_list(); 1343446Smrj 1353446Smrj return (bd); 1363446Smrj } 1373446Smrj 1383446Smrj /* 1393446Smrj * Create a new entry. Comment entries have NULL names. 1403446Smrj */ 1413446Smrj static benv_ent_t * 1423446Smrj new_bent(char *comm, char *cmd, char *name, char *val) 1433446Smrj { 1443446Smrj benv_ent_t *bent; 1453446Smrj 1463446Smrj bent = (benv_ent_t *)malloc(sizeof (benv_ent_t)); 1473446Smrj (void) memset(bent, 0, sizeof (benv_ent_t)); 1483446Smrj 1493446Smrj if (comm) { 1503446Smrj bent->cmd = strdup(comm); 1513446Smrj comm = NULL; 1523446Smrj } else { 1533446Smrj bent->cmd = strdup(cmd); 1543446Smrj bent->name = strdup(name); 1553446Smrj if (val) 1563446Smrj bent->val = strdup(val); 1573446Smrj } 1583446Smrj 1593446Smrj return (bent); 1603446Smrj } 1613446Smrj 1623446Smrj /* 1633446Smrj * Add a new entry to the benv entry list. Entries can be 1643446Smrj * comments or commands. 1653446Smrj */ 1663446Smrj static void 1673446Smrj add_bent(eplist_t *list, char *comm, char *cmd, char *name, char *val) 1683446Smrj { 1693446Smrj benv_ent_t *bent; 1703446Smrj 1713446Smrj bent = new_bent(comm, cmd, name, val); 1723446Smrj add_item((void *)bent, list); 1733446Smrj } 1743446Smrj 1753446Smrj static benv_ent_t * 1763446Smrj get_var(char *name, eplist_t *list) 1773446Smrj { 1783446Smrj eplist_t *e; 1793446Smrj benv_ent_t *p; 1803446Smrj 1813446Smrj for (e = list->next; e != list; e = e->next) { 1823446Smrj p = (benv_ent_t *)e->item; 1833446Smrj if (p->name != NULL && strcmp(p->name, name) == 0) 1843446Smrj return (p); 1853446Smrj } 1863446Smrj 1873446Smrj return (NULL); 1883446Smrj } 1893446Smrj 1903446Smrj /*PRINTFLIKE1*/ 1913446Smrj static void 1923446Smrj eeprom_error(const char *format, ...) 1933446Smrj { 1943446Smrj va_list ap; 1953446Smrj 1963446Smrj va_start(ap, format); 1973446Smrj (void) fprintf(stderr, "eeprom: "); 1983446Smrj (void) vfprintf(stderr, format, ap); 1993446Smrj va_end(ap); 2003446Smrj } 2013446Smrj 2023446Smrj static int 2033446Smrj exec_cmd(char *cmdline, char *output, int64_t osize) 2043446Smrj { 2053446Smrj char buf[BUFSIZ]; 2063446Smrj int ret; 2073446Smrj size_t len; 2083446Smrj FILE *ptr; 2093446Smrj sigset_t set; 2103446Smrj void (*disp)(int); 2113446Smrj 2123446Smrj if (output) 2133446Smrj output[0] = '\0'; 2143446Smrj 2153446Smrj /* 2163446Smrj * For security 2173446Smrj * - only absolute paths are allowed 2183446Smrj * - set IFS to space and tab 2193446Smrj */ 2203446Smrj if (*cmdline != '/') { 2213446Smrj eeprom_error(ABS_PATH_REQ, cmdline); 2223446Smrj return (-1); 2233446Smrj } 2243446Smrj (void) putenv("IFS= \t"); 2253446Smrj 2263446Smrj /* 2273446Smrj * We may have been exec'ed with SIGCHLD blocked 2283446Smrj * unblock it here 2293446Smrj */ 2303446Smrj (void) sigemptyset(&set); 2313446Smrj (void) sigaddset(&set, SIGCHLD); 2323446Smrj if (sigprocmask(SIG_UNBLOCK, &set, NULL) != 0) { 2333446Smrj eeprom_error(FAILED_SIG, strerror(errno)); 2343446Smrj return (-1); 2353446Smrj } 2363446Smrj 2373446Smrj /* 2383446Smrj * Set SIGCHLD disposition to SIG_DFL for popen/pclose 2393446Smrj */ 2403446Smrj disp = sigset(SIGCHLD, SIG_DFL); 2413446Smrj if (disp == SIG_ERR) { 2423446Smrj eeprom_error(FAILED_SIG, strerror(errno)); 2433446Smrj return (-1); 2443446Smrj } 2453446Smrj if (disp == SIG_HOLD) { 2463446Smrj eeprom_error(BLOCKED_SIG, cmdline); 2473446Smrj return (-1); 2483446Smrj } 2493446Smrj 2503446Smrj ptr = popen(cmdline, "r"); 2513446Smrj if (ptr == NULL) { 2523446Smrj eeprom_error(POPEN_FAIL, cmdline, strerror(errno)); 2533446Smrj return (-1); 2543446Smrj } 2553446Smrj 2563446Smrj /* 2573446Smrj * If we simply do a pclose() following a popen(), pclose() 2583446Smrj * will close the reader end of the pipe immediately even 2593446Smrj * if the child process has not started/exited. pclose() 2603446Smrj * does wait for cmd to terminate before returning though. 2613446Smrj * When the executed command writes its output to the pipe 2623446Smrj * there is no reader process and the command dies with 2633446Smrj * SIGPIPE. To avoid this we read repeatedly until read 2643446Smrj * terminates with EOF. This indicates that the command 2653446Smrj * (writer) has closed the pipe and we can safely do a 2663446Smrj * pclose(). 2673446Smrj * 2683446Smrj * Since pclose() does wait for the command to exit, 2693446Smrj * we can safely reap the exit status of the command 2703446Smrj * from the value returned by pclose() 2713446Smrj */ 2723446Smrj while (fgets(buf, sizeof (buf), ptr) != NULL) { 2733446Smrj if (output && osize > 0) { 2743446Smrj (void) snprintf(output, osize, "%s", buf); 2753446Smrj len = strlen(buf); 2763446Smrj output += len; 2773446Smrj osize -= len; 2783446Smrj } 2793446Smrj } 2803446Smrj 2813446Smrj /* 2823446Smrj * If there's a "\n" at the end, we want to chop it off 2833446Smrj */ 2843446Smrj if (output) { 2853446Smrj len = strlen(output) - 1; 2863446Smrj if (output[len] == '\n') 2873446Smrj output[len] = '\0'; 2883446Smrj } 2893446Smrj 2903446Smrj ret = pclose(ptr); 2913446Smrj if (ret == -1) { 2923446Smrj eeprom_error(PCLOSE_FAIL, cmdline, strerror(errno)); 2933446Smrj return (-1); 2943446Smrj } 2953446Smrj 2963446Smrj if (WIFEXITED(ret)) { 2973446Smrj return (WEXITSTATUS(ret)); 2983446Smrj } else { 2993446Smrj eeprom_error(EXEC_FAIL, cmdline, ret); 3003446Smrj return (-1); 3013446Smrj } 3023446Smrj } 3033446Smrj 3043446Smrj #define BOOTADM_STR "bootadm: " 3053446Smrj 3063446Smrj /* 3073446Smrj * bootadm starts all error messages with "bootadm: ". 3083446Smrj * Add a note so users don't get confused on how they ran bootadm. 3093446Smrj */ 3103446Smrj static void 3113446Smrj output_error_msg(const char *msg) 3123446Smrj { 3133446Smrj size_t len = sizeof (BOOTADM_STR) - 1; 3143446Smrj 3153446Smrj if (strncmp(msg, BOOTADM_STR, len) == 0) { 3163446Smrj eeprom_error("error returned from %s\n", msg); 3173446Smrj } else if (msg[0] != '\0') { 3183446Smrj eeprom_error("%s\n", msg); 3193446Smrj } 3203446Smrj } 3213446Smrj 3223446Smrj static char * 3233481Srscott get_bootadm_value(char *name, const int quiet) 3243446Smrj { 3253446Smrj char *ptr, *ret_str, *end_ptr, *orig_ptr; 3263446Smrj char output[BUFSIZ]; 3273446Smrj int is_console, is_kernel = 0; 3283446Smrj size_t len; 3293446Smrj 3303446Smrj is_console = (strcmp(name, "console") == 0); 3313446Smrj 3323446Smrj if (strcmp(name, "boot-file") == 0) { 3333446Smrj is_kernel = 1; 3343446Smrj ptr = "/sbin/bootadm set-menu kernel 2>&1"; 3353446Smrj } else if (is_console || (strcmp(name, "boot-args") == 0)) { 3363446Smrj ptr = "/sbin/bootadm set-menu args 2>&1"; 3373446Smrj } else { 3383446Smrj eeprom_error("Unknown value in get_bootadm_value: %s\n", name); 3393446Smrj return (NULL); 3403446Smrj } 3413446Smrj 3423446Smrj if (exec_cmd(ptr, output, BUFSIZ) != 0) { 3433481Srscott if (quiet == 0) { 3443481Srscott output_error_msg(output); 3453481Srscott } 3463446Smrj return (NULL); 3473446Smrj } 3483446Smrj 3493446Smrj if (is_console) { 3503446Smrj if ((ptr = strstr(output, "console=")) == NULL) { 3513446Smrj return (NULL); 3523446Smrj } 3533446Smrj ptr += strlen("console="); 3543446Smrj 3553446Smrj /* 3563446Smrj * -B may have comma-separated values. It may also be 3573446Smrj * followed by other flags. 3583446Smrj */ 3593446Smrj len = strcspn(ptr, " \t,"); 3603446Smrj ret_str = calloc(len + 1, 1); 3613446Smrj if (ret_str == NULL) { 3623446Smrj eeprom_error(NO_MEM, len + 1); 3633446Smrj return (NULL); 3643446Smrj } 3653446Smrj (void) strncpy(ret_str, ptr, len); 3663446Smrj return (ret_str); 3673446Smrj } else if (is_kernel) { 3683446Smrj ret_str = strdup(output); 3693446Smrj if (ret_str == NULL) 3703446Smrj eeprom_error(NO_MEM, strlen(output) + 1); 3713446Smrj return (ret_str); 3723446Smrj } else { 3733446Smrj /* If there's no console setting, we can return */ 3743446Smrj if ((orig_ptr = strstr(output, "console=")) == NULL) { 3753446Smrj return (strdup(output)); 3763446Smrj } 3773446Smrj len = strcspn(orig_ptr, " \t,"); 3783446Smrj ptr = orig_ptr; 3793446Smrj end_ptr = orig_ptr + len + 1; 3803446Smrj 3813446Smrj /* Eat up any white space */ 3823446Smrj while ((*end_ptr == ' ') || (*end_ptr == '\t')) 3833446Smrj end_ptr++; 3843446Smrj 3853446Smrj /* 3863446Smrj * If there's data following the console string, copy it. 3873446Smrj * If not, cut off the new string. 3883446Smrj */ 3893446Smrj if (*end_ptr == '\0') 3903446Smrj *ptr = '\0'; 3913446Smrj 3923446Smrj while (*end_ptr != '\0') { 3933446Smrj *ptr = *end_ptr; 3943446Smrj ptr++; 3953446Smrj end_ptr++; 3963446Smrj } 3973446Smrj *ptr = '\0'; 3983446Smrj if ((strchr(output, '=') == NULL) && 3993446Smrj (strncmp(output, "-B ", 3) == 0)) { 4003446Smrj /* 4013446Smrj * Since we removed the console setting, we no 4023446Smrj * longer need the initial "-B " 4033446Smrj */ 4043446Smrj orig_ptr = output + 3; 4053446Smrj } else { 4063446Smrj orig_ptr = output; 4073446Smrj } 4083446Smrj 4093446Smrj ret_str = strdup(orig_ptr); 4103446Smrj if (ret_str == NULL) 4113446Smrj eeprom_error(NO_MEM, strlen(orig_ptr) + 1); 4123446Smrj return (ret_str); 4133446Smrj } 4143446Smrj } 4153446Smrj 4163446Smrj /* 4173446Smrj * If quiet is 1, print nothing if there is no value. If quiet is 0, print 4184088Srscott * a message. Return 1 if the value is printed, 0 otherwise. 4193446Smrj */ 4204088Srscott static int 4213481Srscott print_bootadm_value(char *name, const int quiet) 4223446Smrj { 4234088Srscott int rv = 0; 4243481Srscott char *value = get_bootadm_value(name, quiet); 4253446Smrj 4263446Smrj if ((value != NULL) && (value[0] != '\0')) { 4273446Smrj (void) printf("%s=%s\n", name, value); 4284088Srscott rv = 1; 4293446Smrj } else if (quiet == 0) { 4303446Smrj (void) printf("%s: data not available.\n", name); 4313446Smrj } 4323446Smrj 4333446Smrj if (value != NULL) 4343446Smrj free(value); 4354088Srscott return (rv); 4363446Smrj } 4373446Smrj 4383446Smrj static void 4393446Smrj print_var(char *name, eplist_t *list) 4403446Smrj { 4413446Smrj benv_ent_t *p; 4423446Smrj 4434088Srscott /* 4444088Srscott * The console property is kept in both menu.lst and bootenv.rc. The 4454088Srscott * menu.lst value takes precedence. 4464088Srscott */ 4474088Srscott if (strcmp(name, "console") == 0) { 4484088Srscott if (print_bootadm_value(name, 1) == 0) { 4494088Srscott if ((p = get_var(name, list)) != NULL) { 4504088Srscott (void) printf("%s=%s\n", name, p->val ? 4514088Srscott p->val : ""); 4524088Srscott } else { 4534088Srscott (void) printf("%s: data not available.\n", 4544088Srscott name); 4554088Srscott } 4564088Srscott } 4574088Srscott } else if ((strcmp(name, "boot-file") == 0) || 4584088Srscott (strcmp(name, "boot-args") == 0)) { 4594088Srscott (void) print_bootadm_value(name, 0); 4603446Smrj } else if ((p = get_var(name, list)) == NULL) 4613446Smrj (void) printf("%s: data not available.\n", name); 4623446Smrj else 4633446Smrj (void) printf("%s=%s\n", name, p->val ? p->val : ""); 4643446Smrj } 4653446Smrj 4663446Smrj static void 4673446Smrj print_vars(eplist_t *list) 4683446Smrj { 4693446Smrj eplist_t *e; 4703446Smrj benv_ent_t *p; 4714088Srscott int console_printed = 0; 4724088Srscott 4734088Srscott /* 4744088Srscott * The console property is kept both in menu.lst and bootenv.rc. 4754088Srscott * The menu.lst value takes precedence, so try printing that one 4764088Srscott * first. 4774088Srscott */ 4784088Srscott console_printed = print_bootadm_value("console", 1); 4793446Smrj 4803446Smrj for (e = list->next; e != list; e = e->next) { 4813446Smrj p = (benv_ent_t *)e->item; 4823446Smrj if (p->name != NULL) { 4834088Srscott if (((strcmp(p->name, "console") == 0) && 4844088Srscott (console_printed == 1)) || 4854088Srscott ((strcmp(p->name, "boot-file") == 0) || 4864088Srscott (strcmp(p->name, "boot-args") == 0))) { 4873446Smrj /* handle these separately */ 4883446Smrj continue; 4893446Smrj } 4903446Smrj (void) printf("%s=%s\n", p->name, p->val ? p->val : ""); 4913446Smrj } 4923446Smrj } 4934088Srscott (void) print_bootadm_value("boot-file", 1); 4944088Srscott (void) print_bootadm_value("boot-args", 1); 4953446Smrj } 4963446Smrj 4973446Smrj /* 4983446Smrj * Write a string to a file, quoted appropriately. We use single 4993446Smrj * quotes to prevent any variable expansion. Of course, we backslash-quote 5003446Smrj * any single quotes or backslashes. 5013446Smrj */ 5023446Smrj static void 5033446Smrj put_quoted(FILE *fp, char *val) 5043446Smrj { 5053446Smrj (void) putc('\'', fp); 5063446Smrj while (*val) { 5073446Smrj switch (*val) { 5083446Smrj case '\'': 5093446Smrj case '\\': 5103446Smrj (void) putc('\\', fp); 5113446Smrj /* FALLTHROUGH */ 5123446Smrj default: 5133446Smrj (void) putc(*val, fp); 5143446Smrj break; 5153446Smrj } 5163446Smrj val++; 5173446Smrj } 5183446Smrj (void) putc('\'', fp); 5193446Smrj } 5203446Smrj 5213446Smrj static void 5223446Smrj set_bootadm_var(char *name, char *value) 5233446Smrj { 5243446Smrj char buf[BUFSIZ]; 5253446Smrj char output[BUFSIZ] = ""; 5263481Srscott char *console, *args; 5273446Smrj int is_console; 5283446Smrj 5293446Smrj if (verbose) { 5303446Smrj (void) printf("old:"); 5314088Srscott (void) print_bootadm_value(name, 0); 5323446Smrj } 5333446Smrj 5343446Smrj /* 5353446Smrj * For security, we single-quote whatever we run on the command line, 5363446Smrj * and we don't allow single quotes in the string. 5373446Smrj */ 5383481Srscott if (strchr(value, '\'') != NULL) { 5393446Smrj eeprom_error("Single quotes are not allowed " 5403446Smrj "in the %s property.\n", name); 5413446Smrj return; 5423446Smrj } 5433446Smrj 5443446Smrj is_console = (strcmp(name, "console") == 0); 5453446Smrj if (strcmp(name, "boot-file") == 0) { 5463446Smrj (void) snprintf(buf, BUFSIZ, "/sbin/bootadm set-menu " 5473446Smrj "kernel='%s' 2>&1", value); 5483446Smrj } else if (is_console || (strcmp(name, "boot-args") == 0)) { 5493446Smrj if (is_console) { 5503481Srscott args = get_bootadm_value("boot-args", 1); 5513446Smrj console = value; 5523446Smrj } else { 5533446Smrj args = value; 5543481Srscott console = get_bootadm_value("console", 1); 5553446Smrj } 5563446Smrj if (((args == NULL) || (args[0] == '\0')) && 5573446Smrj ((console == NULL) || (console[0] == '\0'))) { 5583446Smrj (void) snprintf(buf, BUFSIZ, "/sbin/bootadm set-menu " 5593446Smrj "args= 2>&1"); 5603446Smrj } else if ((args == NULL) || (args[0] == '\0')) { 5613446Smrj (void) snprintf(buf, BUFSIZ, "/sbin/bootadm " 5623446Smrj "set-menu args='-B console=%s' 2>&1", 5633446Smrj console); 5643446Smrj } else if ((console == NULL) || (console[0] == '\0')) { 5653446Smrj (void) snprintf(buf, BUFSIZ, "/sbin/bootadm " 5663446Smrj "set-menu args='%s' 2>&1", args); 5673446Smrj } else if (strncmp(args, "-B ", 3) != 0) { 5683446Smrj (void) snprintf(buf, BUFSIZ, "/sbin/bootadm " 5693446Smrj "set-menu args='-B console=%s %s' 2>&1", 5703446Smrj console, args); 5713446Smrj } else { 5723446Smrj (void) snprintf(buf, BUFSIZ, "/sbin/bootadm " 5733446Smrj "set-menu args='-B console=%s,%s' 2>&1", 5743446Smrj console, args + 3); 5753446Smrj } 5763446Smrj } else { 5773446Smrj eeprom_error("Unknown value in set_bootadm_value: %s\n", name); 5783446Smrj return; 5793446Smrj } 5803446Smrj 5813446Smrj if (exec_cmd(buf, output, BUFSIZ) != 0) { 5823446Smrj output_error_msg(output); 5833446Smrj return; 5843446Smrj } 5853446Smrj 5863446Smrj if (verbose) { 5873446Smrj (void) printf("new:"); 5884088Srscott (void) print_bootadm_value(name, 0); 5893446Smrj } 5903446Smrj } 5913446Smrj 592*4346Srscott /* 593*4346Srscott * Returns 1 if bootenv.rc was modified, 0 otherwise. 594*4346Srscott */ 595*4346Srscott static int 5963446Smrj set_var(char *name, char *val, eplist_t *list) 5973446Smrj { 5983446Smrj benv_ent_t *p; 5994088Srscott int old_verbose; 6003446Smrj 6013446Smrj if ((strcmp(name, "boot-file") == 0) || 6024088Srscott (strcmp(name, "boot-args") == 0)) { 6033446Smrj set_bootadm_var(name, val); 604*4346Srscott return (0); 6053446Smrj } 6063446Smrj 6074088Srscott /* 6084088Srscott * The console property is kept in two places: menu.lst and bootenv.rc. 6094088Srscott * Update them both. We clear verbose to prevent duplicate messages. 6104088Srscott */ 6114088Srscott if (strcmp(name, "console") == 0) { 6124088Srscott old_verbose = verbose; 6134088Srscott verbose = 0; 6144088Srscott set_bootadm_var(name, val); 6154088Srscott verbose = old_verbose; 6164088Srscott } 6174088Srscott 6183446Smrj if (verbose) { 6193446Smrj (void) printf("old:"); 6203446Smrj print_var(name, list); 6213446Smrj } 6223446Smrj 6233446Smrj if ((p = get_var(name, list)) != NULL) { 6243446Smrj free(p->val); 6253446Smrj p->val = strdup(val); 6263446Smrj } else 6273446Smrj add_bent(list, NULL, "setprop", name, val); 6283446Smrj 6293446Smrj if (verbose) { 6303446Smrj (void) printf("new:"); 6313446Smrj print_var(name, list); 6323446Smrj } 633*4346Srscott return (1); 6343446Smrj } 6353446Smrj 6363446Smrj /* 637*4346Srscott * Returns 1 if bootenv.rc is modified or 0 if no modification was 638*4346Srscott * necessary. This allows us to implement non super-user look-up of 639*4346Srscott * variables by name without the user being yelled at for trying to 640*4346Srscott * modify the bootenv.rc file. 6413446Smrj */ 6423446Smrj static int 6433446Smrj proc_var(char *name, eplist_t *list) 6443446Smrj { 6453446Smrj register char *val; 6463446Smrj 6473446Smrj if ((val = strchr(name, '=')) == NULL) { 6483446Smrj print_var(name, list); 6493446Smrj return (0); 6503446Smrj } else { 6513446Smrj *val++ = '\0'; 652*4346Srscott return (set_var(name, val, list)); 6533446Smrj } 6543446Smrj } 6553446Smrj 6563446Smrj static void 6573446Smrj init_benv(benv_des_t *bd, char *file) 6583446Smrj { 6593446Smrj get_kbenv(); 6603446Smrj 6613446Smrj if (test) 6623446Smrj boottree = "/tmp"; 6633446Smrj else if ((boottree = (char *)get_propval("boottree", "chosen")) == NULL) 6643446Smrj boottree = strcats("/boot", NULL); 6653446Smrj 6663446Smrj if (file != NULL) 6673446Smrj bd->name = file; 6683446Smrj else 6693446Smrj bd->name = strcats(boottree, "/solaris/bootenv.rc", NULL); 6703446Smrj } 6713446Smrj 6723446Smrj static void 6733446Smrj map_benv(benv_des_t *bd) 6743446Smrj { 6753446Smrj if ((bd->fd = open(bd->name, O_RDONLY)) == -1) 6763446Smrj if (errno == ENOENT) 6773446Smrj return; 6783446Smrj else 6793446Smrj exit(_error(PERROR, "cannot open %s", bd->name)); 6803446Smrj 6813446Smrj if ((bd->len = (size_t)lseek(bd->fd, 0, SEEK_END)) == 0) { 6823446Smrj if (close(bd->fd) == -1) 6833446Smrj exit(_error(PERROR, "close error on %s", bd->name)); 6843446Smrj return; 6853446Smrj } 6863446Smrj 6873446Smrj (void) lseek(bd->fd, 0, SEEK_SET); 6883446Smrj 6893446Smrj if ((bd->adr = mmap((caddr_t)0, bd->len, (PROT_READ | PROT_WRITE), 6903446Smrj MAP_PRIVATE, bd->fd, 0)) == MAP_FAILED) 6913446Smrj exit(_error(PERROR, "cannot map %s", bd->name)); 6923446Smrj } 6933446Smrj 6943446Smrj static void 6953446Smrj unmap_benv(benv_des_t *bd) 6963446Smrj { 6973446Smrj if (munmap(bd->adr, bd->len) == -1) 6983446Smrj exit(_error(PERROR, "unmap error on %s", bd->name)); 6993446Smrj 7003446Smrj if (close(bd->fd) == -1) 7013446Smrj exit(_error(PERROR, "close error on %s", bd->name)); 7023446Smrj } 7033446Smrj 7043446Smrj #define NL '\n' 7053446Smrj #define COMM '#' 7063446Smrj 7073446Smrj /* 7083446Smrj * Add a comment block to the benv list. 7093446Smrj */ 7103446Smrj static void 7113446Smrj add_comm(benv_des_t *bd, char *base, char *last, char **next, int *line) 7123446Smrj { 7133446Smrj int nl, lines; 7143446Smrj char *p; 7153446Smrj 7163446Smrj nl = 0; 7173446Smrj for (p = base, lines = 0; p < last; p++) { 7183446Smrj if (*p == NL) { 7193446Smrj nl++; 7203446Smrj lines++; 7213446Smrj } else if (nl) { 7223446Smrj if (*p != COMM) 7233446Smrj break; 7243446Smrj nl = 0; 7253446Smrj } 7263446Smrj } 7273446Smrj *(p - 1) = NULL; 7283446Smrj add_bent(bd->elist, base, NULL, NULL, NULL); 7293446Smrj *next = p; 7303446Smrj *line += lines; 7313446Smrj } 7323446Smrj 7333446Smrj /* 7343446Smrj * Parse out an operator (setprop) from the boot environment 7353446Smrj */ 7363446Smrj static char * 7373446Smrj parse_cmd(benv_des_t *bd, char **next, int *line) 7383446Smrj { 7393446Smrj char *strbegin; 7403446Smrj char *badeof = "unexpected EOF in %s line %d"; 7413446Smrj char *syntax = "syntax error in %s line %d"; 7423446Smrj char *c = *next; 7433446Smrj 7443446Smrj /* 7453446Smrj * Skip spaces or tabs. New lines increase the line count. 7463446Smrj */ 7473446Smrj while (isspace(*c)) { 7483446Smrj if (*c++ == '\n') 7493446Smrj (*line)++; 7503446Smrj } 7513446Smrj 7523446Smrj /* 7533446Smrj * Check for a the setprop command. Currently that's all we 7543446Smrj * seem to support. 7553446Smrj * 7563446Smrj * XXX need support for setbinprop? 7573446Smrj */ 7583446Smrj 7593446Smrj /* 7603446Smrj * Check first for end of file. Finding one now would be okay. 7613446Smrj * We should also bail if we are at the start of a comment. 7623446Smrj */ 7633446Smrj if (*c == '\0' || *c == COMM) { 7643446Smrj *next = c; 7653446Smrj return (NULL); 7663446Smrj } 7673446Smrj 7683446Smrj strbegin = c; 7693446Smrj while (*c && !isspace(*c)) 7703446Smrj c++; 7713446Smrj 7723446Smrj /* 7733446Smrj * Check again for end of file. Finding one now would NOT be okay. 7743446Smrj */ 7753446Smrj if (*c == '\0') { 7763446Smrj exit(_error(NO_PERROR, badeof, bd->name, *line)); 7773446Smrj } 7783446Smrj 7793446Smrj *c++ = '\0'; 7803446Smrj *next = c; 7813446Smrj 7823446Smrj /* 7833446Smrj * Last check is to make sure the command is a setprop! 7843446Smrj */ 7853446Smrj if (strcmp(strbegin, "setprop") != 0) { 7863446Smrj exit(_error(NO_PERROR, syntax, bd->name, *line)); 7873446Smrj /* NOTREACHED */ 7883446Smrj } 7893446Smrj return (strbegin); 7903446Smrj } 7913446Smrj 7923446Smrj /* 7933446Smrj * Parse out the name (LHS) of a setprop from the boot environment 7943446Smrj */ 7953446Smrj static char * 7963446Smrj parse_name(benv_des_t *bd, char **next, int *line) 7973446Smrj { 7983446Smrj char *strbegin; 7993446Smrj char *badeof = "unexpected EOF in %s line %d"; 8003446Smrj char *syntax = "syntax error in %s line %d"; 8013446Smrj char *c = *next; 8023446Smrj 8033446Smrj /* 8043446Smrj * Skip spaces or tabs. No tolerance for new lines now. 8053446Smrj */ 8063446Smrj while (isspace(*c)) { 8073446Smrj if (*c++ == '\n') 8083446Smrj exit(_error(NO_PERROR, syntax, bd->name, *line)); 8093446Smrj } 8103446Smrj 8113446Smrj /* 8123446Smrj * Grab a name for the property to set. 8133446Smrj */ 8143446Smrj 8153446Smrj /* 8163446Smrj * Check first for end of file. Finding one now would NOT be okay. 8173446Smrj */ 8183446Smrj if (*c == '\0') { 8193446Smrj exit(_error(NO_PERROR, badeof, bd->name, *line)); 8203446Smrj } 8213446Smrj 8223446Smrj strbegin = c; 8233446Smrj while (*c && !isspace(*c)) 8243446Smrj c++; 8253446Smrj 8263446Smrj /* 8273446Smrj * At this point in parsing we have 'setprop name'. What follows 8283446Smrj * is a newline, other whitespace, or EOF. Most of the time we 8293446Smrj * want to replace a white space character with a NULL to terminate 8303446Smrj * the name, and then continue on processing. A newline here provides 8313446Smrj * the most grief. If we just replace it with a null we'll 8323446Smrj * potentially get the setprop on the next line as the value of this 8333446Smrj * setprop! So, if the last thing we see is a newline we'll have to 8343446Smrj * dup the string. 8353446Smrj */ 8363446Smrj if (isspace(*c)) { 8373446Smrj if (*c == '\n') { 8383446Smrj *c = '\0'; 8393446Smrj strbegin = strdup(strbegin); 8403446Smrj *c = '\n'; 8413446Smrj } else { 8423446Smrj *c++ = '\0'; 8433446Smrj } 8443446Smrj } 8453446Smrj 8463446Smrj *next = c; 8473446Smrj return (strbegin); 8483446Smrj } 8493446Smrj 8503446Smrj /* 8513446Smrj * Parse out the value (RHS) of a setprop line from the boot environment 8523446Smrj */ 8533446Smrj static char * 8543446Smrj parse_value(benv_des_t *bd, char **next, int *line) 8553446Smrj { 8563446Smrj char *strbegin; 8573446Smrj char *badeof = "unexpected EOF in %s line %d"; 8583446Smrj char *result; 8593446Smrj char *c = *next; 8603446Smrj char quote; 8613446Smrj 8623446Smrj /* 8633446Smrj * Skip spaces or tabs. A newline here would indicate a 8643446Smrj * NULL property value. 8653446Smrj */ 8663446Smrj while (isspace(*c)) { 8673446Smrj if (*c++ == '\n') { 8683446Smrj (*line)++; 8693446Smrj *next = c; 8703446Smrj return (NULL); 8713446Smrj } 8723446Smrj } 8733446Smrj 8743446Smrj /* 8753446Smrj * Grab the value of the property to set. 8763446Smrj */ 8773446Smrj 8783446Smrj /* 8793446Smrj * Check first for end of file. Finding one now would 8803446Smrj * also indicate a NULL property. 8813446Smrj */ 8823446Smrj if (*c == '\0') { 8833446Smrj *next = c; 8843446Smrj return (NULL); 8853446Smrj } 8863446Smrj 8873446Smrj /* 8883446Smrj * Value may be quoted, in which case we assume the end of the value 8893446Smrj * comes with a closing quote. 8903446Smrj * 8913446Smrj * We also allow escaped quote characters inside the quoted value. 8923446Smrj * 8933446Smrj * For obvious reasons we do not attempt to parse variable references. 8943446Smrj */ 8953446Smrj if (*c == '"' || *c == '\'') { 8963446Smrj quote = *c; 8973446Smrj c++; 8983446Smrj strbegin = c; 8993446Smrj result = c; 9003446Smrj while (*c != quote) { 9013446Smrj if (*c == '\\') { 9023446Smrj c++; 9033446Smrj } 9043446Smrj if (*c == '\0' || *c == '\n') { 9053446Smrj break; 9063446Smrj } 9073446Smrj *result++ = *c++; 9083446Smrj } 9093446Smrj 9103446Smrj /* 9113446Smrj * Throw fatal exception if no end quote found. 9123446Smrj */ 9133446Smrj if (*c != quote) { 9143446Smrj exit(_error(NO_PERROR, badeof, bd->name, *line)); 9153446Smrj } 9163446Smrj 9173446Smrj *result = '\0'; /* Terminate the result */ 9183446Smrj c++; /* and step past the close quote */ 9193446Smrj } else { 9203446Smrj strbegin = c; 9213446Smrj while (*c && !isspace(*c)) 9223446Smrj c++; 9233446Smrj } 9243446Smrj 9253446Smrj /* 9263446Smrj * Check again for end of file. Finding one now is okay. 9273446Smrj */ 9283446Smrj if (*c == '\0') { 9293446Smrj *next = c; 9303446Smrj return (strbegin); 9313446Smrj } 9323446Smrj 9333446Smrj *c++ = '\0'; 9343446Smrj *next = c; 9353446Smrj return (strbegin); 9363446Smrj } 9373446Smrj 9383446Smrj /* 9393446Smrj * Add a command to the benv list. 9403446Smrj */ 9413446Smrj static void 9423446Smrj add_cmd(benv_des_t *bd, char *last, char **next, int *line) 9433446Smrj { 9443446Smrj char *cmd, *name, *val; 9453446Smrj 9463446Smrj while (*next <= last && **next != COMM) { 9473446Smrj if ((cmd = parse_cmd(bd, next, line)) == NULL) 9483446Smrj break; 9493446Smrj name = parse_name(bd, next, line); 9503446Smrj val = parse_value(bd, next, line); 9513446Smrj add_bent(bd->elist, NULL, cmd, name, val); 9523446Smrj (*line)++; 9533446Smrj }; 9543446Smrj } 9553446Smrj 9563446Smrj /* 9573446Smrj * Parse the benv (bootenv.rc) file and break it into a benv 9583446Smrj * list. List entries may be comment blocks or commands. 9593446Smrj */ 9603446Smrj static void 9613446Smrj parse_benv(benv_des_t *bd) 9623446Smrj { 9633446Smrj int line; 9643446Smrj char *pbase, *pend; 9653446Smrj char *tok, *tnext; 9663446Smrj 9673446Smrj line = 1; 9683446Smrj pbase = (char *)bd->adr; 9693446Smrj pend = pbase + bd->len; 9703446Smrj 9713446Smrj for (tok = tnext = pbase; tnext < pend; tok = tnext) 9723446Smrj if (*tok == COMM) 9733446Smrj add_comm(bd, tok, pend, &tnext, &line); 9743446Smrj else 9753446Smrj add_cmd(bd, pend, &tnext, &line); 9763446Smrj } 9773446Smrj 9783446Smrj static void 9793446Smrj write_benv(benv_des_t *bd) 9803446Smrj { 9813446Smrj FILE *fp; 9823446Smrj eplist_t *list, *e; 9833446Smrj benv_ent_t *bent; 9843446Smrj char *name; 9853446Smrj 9863446Smrj list = bd->elist; 9873446Smrj 9883446Smrj if (list->next == list) 9893446Smrj return; 9903446Smrj 9913446Smrj if ((fp = fopen(bd->name, "w")) == NULL) 9923446Smrj exit(_error(PERROR, "cannot open %s", bd->name)); 9933446Smrj 9943446Smrj for (e = list->next; e != list; e = e->next) { 9953446Smrj bent = (benv_ent_t *)e->item; 9963446Smrj name = bent->name; 9973446Smrj if (name) { 9983446Smrj if (bent->val) { 9993446Smrj (void) fprintf(fp, "%s %s ", 1000*4346Srscott bent->cmd, bent->name); 10013446Smrj put_quoted(fp, bent->val); 10023446Smrj (void) fprintf(fp, "\n"); 10033446Smrj } else { 10043446Smrj (void) fprintf(fp, "%s %s\n", 1005*4346Srscott bent->cmd, bent->name); 10063446Smrj } 10073446Smrj } else { 10083446Smrj (void) fprintf(fp, "%s\n", bent->cmd); 10093446Smrj } 10103446Smrj } 10113446Smrj 10123446Smrj (void) fclose(fp); 10133446Smrj } 10143446Smrj 10153446Smrj static char * 10163446Smrj get_line(void) 10173446Smrj { 10183446Smrj int c; 10193446Smrj char *nl; 10203446Smrj static char line[256]; 10213446Smrj 10223446Smrj if (fgets(line, sizeof (line), stdin) != NULL) { 10233446Smrj /* 10243446Smrj * Remove newline if present, 10253446Smrj * otherwise discard rest of line. 10263446Smrj */ 10273446Smrj if (nl = strchr(line, '\n')) 10283446Smrj *nl = 0; 10293446Smrj else 10303446Smrj while ((c = getchar()) != '\n' && c != EOF) 10313446Smrj ; 10323446Smrj return (line); 10333446Smrj } else 10343446Smrj return (NULL); 10353446Smrj } 10363446Smrj 10373446Smrj int 10383446Smrj main(int argc, char **argv) 10393446Smrj { 10403446Smrj int c; 10413446Smrj int updates = 0; 10423446Smrj char *usage = "Usage: %s [-v] [-f prom-device]" 10433446Smrj " [variable[=value] ...]"; 10443446Smrj eplist_t *elist; 10453446Smrj benv_des_t *bd; 10463446Smrj char *file = NULL; 10473446Smrj 10483446Smrj setprogname(argv[0]); 10493446Smrj 10503446Smrj while ((c = getopt(argc, argv, "f:Itv")) != -1) 10513446Smrj switch (c) { 10523446Smrj case 'v': 10533446Smrj verbose++; 10543446Smrj break; 10553446Smrj case 'f': 10563446Smrj file = optarg; 10573446Smrj break; 10583446Smrj case 't': 10593446Smrj test++; 10603446Smrj break; 10613446Smrj default: 10623446Smrj exit(_error(NO_PERROR, usage, argv[0])); 10633446Smrj } 10643446Smrj 10653446Smrj (void) uname(&uts_buf); 10663446Smrj bd = new_bd(); 10673446Smrj init_benv(bd, file); 10683446Smrj 10693446Smrj map_benv(bd); 10703446Smrj if (bd->len) { 10713446Smrj parse_benv(bd); 10723446Smrj unmap_benv(bd); 10733446Smrj } 10743446Smrj 10753446Smrj elist = bd->elist; 10763446Smrj 10773446Smrj if (optind >= argc) { 10783446Smrj print_vars(elist); 10793446Smrj return (0); 10803446Smrj } else 10813446Smrj while (optind < argc) { 10823446Smrj /* 10833446Smrj * If "-" specified, read variables from stdin; 10843446Smrj * otherwise, process each argument as a variable 10853446Smrj * print or set request. 10863446Smrj */ 10873446Smrj if (strcmp(argv[optind], "-") == 0) { 10883446Smrj char *line; 10893446Smrj 10903446Smrj while ((line = get_line()) != NULL) 10913446Smrj updates += proc_var(line, elist); 10923446Smrj clearerr(stdin); 10933446Smrj } else 10943446Smrj updates += proc_var(argv[optind], elist); 10953446Smrj 10963446Smrj optind++; 10973446Smrj } 10983446Smrj 10993446Smrj /* 11003446Smrj * don't write benv if we are processing delayed writes since 11013446Smrj * it is likely that the delayed writes changes bootenv.rc anyway... 11023446Smrj */ 11033446Smrj if (updates) 11043446Smrj write_benv(bd); 11053446Smrj close_kbenv(); 11063446Smrj 11073446Smrj return (0); 11083446Smrj } 1109