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