1/* ********************************************************************* 2 * Broadcom Common Firmware Environment (CFE) 3 * 4 * NVRAM Environment commands File: ui_nvramcmds.c 5 * 6 * User interface for environment variables 7 * 8 * Author: Mitch Lichtenberg 9 * 10 ********************************************************************* 11 * 12 * Copyright 2000,2001,2002,2003 13 * Broadcom Corporation. All rights reserved. 14 * 15 * This software is furnished under license and may be used and 16 * copied only in accordance with the following terms and 17 * conditions. Subject to these conditions, you may download, 18 * copy, install, use, modify and distribute modified or unmodified 19 * copies of this software in source and/or binary form. No title 20 * or ownership is transferred hereby. 21 * 22 * 1) Any source code used, modified or distributed must reproduce 23 * and retain this copyright notice and list of conditions 24 * as they appear in the source file. 25 * 26 * 2) No right is granted to use any trade name, trademark, or 27 * logo of Broadcom Corporation. The "Broadcom Corporation" 28 * name may not be used to endorse or promote products derived 29 * from this software without the prior written permission of 30 * Broadcom Corporation. 31 * 32 * 3) THIS SOFTWARE IS PROVIDED "AS-IS" AND ANY EXPRESS OR 33 * IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, ANY IMPLIED 34 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 35 * PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT 36 * SHALL BROADCOM BE LIABLE FOR ANY DAMAGES WHATSOEVER, AND IN 37 * PARTICULAR, BROADCOM SHALL NOT BE LIABLE FOR DIRECT, INDIRECT, 38 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 39 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 40 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 41 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 42 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 43 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE), EVEN IF ADVISED OF 44 * THE POSSIBILITY OF SUCH DAMAGE. 45 ********************************************************************* */ 46 47 48#include "cfe.h" 49#include "env_subr.h" 50#include "bcmnvram.h" 51#include "ui_command.h" 52 53static nvram_import_t nvram_import_default[] = { 54 { "LINUX_CMDLINE", "kernel_args", NULL }, 55 { NULL, NULL } 56}; 57 58nvram_import_t *nvram_import_data = nvram_import_default; 59 60int ui_init_nvramcmds(void); 61static int ui_cmd_nvram_get(ui_cmdline_t *cmd, int argc, char *argv[]); 62static int ui_cmd_nvram_set(ui_cmdline_t *cmd,int argc,char *argv[]); 63static int ui_cmd_nvram_unset(ui_cmdline_t *cmd,int argc,char *argv[]); 64static int ui_cmd_nvram_show(ui_cmdline_t *cmd,int argc,char *argv[]); 65static int ui_cmd_nvram_commit(ui_cmdline_t *cmd,int argc,char *argv[]); 66static int ui_cmd_nvram_erase(ui_cmdline_t *cmd,int argc,char *argv[]); 67static int ui_cmd_nvram_import(ui_cmdline_t *cmd,int argc,char *argv[]); 68 69static int ui_cmd_nvram_get(ui_cmdline_t *cmd, int argc, char *argv[]) 70{ 71 const char *name; 72 const char *value; 73 74 name = cmd_getarg(cmd, 0); 75 76 if (!name) { 77 return ui_showusage(cmd); 78 } 79 80 value = nvram_get(name); 81 if (value) { 82 printf("%s\n", value); 83 } 84 else { 85 return ui_showerror(-1, "Could not get nvram variable '%s'", name); 86 } 87 88 return 0; 89} 90 91 92static int ui_cmd_nvram_set(ui_cmdline_t *cmd, int argc, char *argv[]) 93{ 94 char *name, *eq, *value; 95 int res; 96 97 if (argc == 2) { 98 name = cmd_getarg(cmd, 0); 99 eq = "="; 100 value = cmd_getarg(cmd, 1); 101 } 102 else if (argc == 3) { 103 name = cmd_getarg(cmd, 0); 104 eq = cmd_getarg(cmd, 1); 105 value = cmd_getarg(cmd, 2); 106 } 107 else { 108 return ui_showusage(cmd); 109 } 110 if (!name || !value || !eq || strcmp(eq, "=") != 0) { 111 return ui_showusage(cmd); 112 } 113 114 res = nvram_set(name, value); 115 if (res != 0) { 116 return ui_showerror(res, "Could not set nvram variable '%s'", name); 117 } 118 119 return 0; 120} 121 122 123static int ui_cmd_nvram_unset(ui_cmdline_t *cmd, int argc, char *argv[]) 124{ 125 char *name; 126 int res; 127 128 name = cmd_getarg(cmd,0); 129 130 if (!name) { 131 return ui_showusage(cmd); 132 } 133 134 res = nvram_unset(name); 135 if (res != 0) { 136 return ui_showerror(res, "Could not delete nvram variable '%s'", name); 137 } 138 139 return 0; 140} 141 142 143static int nvram_show_one(const char *tuple) 144{ 145 xprintf(" %s\n", tuple); 146 return 1; 147} 148 149static int ui_cmd_nvram_show(ui_cmdline_t *cmd, int argc, char *argv[]) 150{ 151 nvram_enum(&nvram_show_one); 152 return 0; 153} 154 155 156static int ui_cmd_nvram_commit(ui_cmdline_t *cmd, int argc, char *argv[]) 157{ 158 int nvram_handle; 159 int res; 160 161 nvram_handle = cfe_open("flash1.nvram"); 162 if (nvram_handle < 0) 163 return CFE_ERR_DEVNOTFOUND; 164 165 res = nvram_commit(nvram_handle); 166 cfe_close(nvram_handle); 167 168 return res; 169} 170 171 172static int ui_cmd_nvram_erase(ui_cmdline_t *cmd, int argc, char *argv[]) 173{ 174 int nvram_handle; 175 int res; 176 177 nvram_handle = cfe_open("flash0.nvram"); 178 if (nvram_handle < 0) 179 return CFE_ERR_DEVNOTFOUND; 180 181 /* XXX Write NVRAM_SPACE of 0xFF ? */ 182 res = -1; 183 184 return res; 185} 186 187 188static int ui_cmd_nvram_import(ui_cmdline_t *cmd, int argc, char *argv[]) 189{ 190 char *value; 191 nvram_import_t *e2n = nvram_import_data; 192 193 while (e2n && e2n->envname) { 194 if ((value = env_getenv(e2n->envname)) != NULL) { 195 nvram_set(e2n->nvname, value); 196 } 197 else if (nvram_get(e2n->nvname) == NULL && e2n->def != NULL) { 198 nvram_set(e2n->nvname, e2n->def); 199 } 200 e2n++; 201 } 202 203 return 0; 204} 205 206 207int ui_init_nvramcmds(void) 208{ 209 /* Note: HND code has a single command "nvram" with subcommand 210 selected by the first argument. Better? */ 211 212 cmd_addcmd("nvram get", 213 ui_cmd_nvram_get, 214 NULL, 215 "Get the value of an nvram variable.", 216 "nvram get <variable>\n\n" 217 "Get the current value of an nvram variable.", 218 ""); 219 220 cmd_addcmd("nvram set", 221 ui_cmd_nvram_set, 222 NULL, 223 "Set the value of an nvram variable.", 224 "nvram set <variable>=<value>\n\n" 225 "Set the value of an nvram variable.\n" 226 "(This session only until commit)", 227 ""); 228 229 cmd_addcmd("nvram unset", 230 ui_cmd_nvram_unset, 231 NULL, 232 "Delete an nvram variable.", 233 "nvram unsetenv <variable>\n\n" 234 "Delete an nvram variable and its value from memory.\n" 235 "(This session only until commit)", 236 ""); 237 238 cmd_addcmd("nvram show", 239 ui_cmd_nvram_show, 240 NULL, 241 "Show all nvram variables.", 242 "nvram show\n\n" 243 "Display the names and values of all defined nvram variables.", 244 ""); 245 246 cmd_addcmd("nvram commit", 247 ui_cmd_nvram_commit, 248 NULL, 249 "Commit nvram variable bindings.", 250 "nvram commit\n\n" 251 "Commit the current nvram variables and values to " 252 "non-volatile memory.", 253 ""); 254 255 cmd_addcmd("nvram erase", 256 ui_cmd_nvram_erase, 257 NULL, 258 "Delete all nvram variables.", 259 "nvram erase\n\n" 260 "This command deletes all nvram variables from both memory " 261 "and non-volatile memory.", 262 ""); 263 264 cmd_addcmd("nvram import", 265 ui_cmd_nvram_import, 266 NULL, 267 "Import nvram variables from standard environment.", 268 "nvram import\n\n" 269 "This command converts standard environment variables " 270 "into the corresponding nvram variables.", 271 ""); 272 273 return 0; 274} 275