freebsd-nq/usr.sbin/eeprom/eeprom.c
Marius Strobl 96818a07a7 Add eeprom(8), a utility to display and modify system configurations
stored in EEPROM or NVRAM. It's inspired by the NetBSD eeprom(8) and
the SunOS/Solaris eeprom(1M) utilities. Currently, this eeprom(8)
only supports systems equipped with Open Firmware and is only tested
on Sun machines but should work on any platform using Open Firmware.
A bit more specific, eeprom(8) can be used on these systems to do the
same under FreeBSD as can be done using the printenv and setenv
commandos in the boot monitor. One thing that only hardly can be done
using the boot monitor but easily with eeprom(8) is to write a logo
to the "oem-logo" property. eeprom(8) may also be useful to recover
the boot monitor password (in the default configuration only as root,
of course), i.e. when the boot monitor allows you to boot but you
can't alter the configuration because the password is unknown. The
man page may also be a useful reference of the various configuration
variables.

The idea of eeprom(8) is that handlers can be written to add support
for any firmware that stores such configuration in EEPROM or NVRAM;
sort of e.g. eeprom(1M) on Solaris/x86 is used to turn PAE-support
on and off (stored in a file then, not hardware). In FreeBSD, a
candidate for this would be a handler for the EFI boot environment
for FreeBSD/ia64.

eeprom(8) uses some code from NetBSD (eeprom.c and the base for
eeprom.8), the handler for the Open Firmware /options node
(ofw_options.[c,h]) was written using ofw_util.[c,h] from ofwdump(8).

Reviewed by:	ru (slightly earlier version of the man page)
2004-05-22 16:56:04 +00:00

154 lines
3.7 KiB
C

/*-
* Copyright (c) 1996 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Jason R. Thorpe.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the NetBSD
* Foundation, Inc. and its contributors.
* 4. Neither the name of The NetBSD Foundation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* from: NetBSD: main.c,v 1.15 2001/02/19 23:22:42 cgd Exp
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
#include <unistd.h>
#include "ofw_options.h"
static int action(char *);
static void dump_config(void);
static void usage(void);
static void
usage(void)
{
fprintf(stderr,
"usage: eeprom -a\n"
" eeprom [-] name[=value] ...\n");
exit(EX_USAGE);
}
int
main(int argc, char *argv[])
{
int do_stdin, opt;
int aflag, rv;
char *cp;
char line[BUFSIZ];
aflag = do_stdin = 0;
rv = EX_OK;
while ((opt = getopt(argc, argv, "-a")) != -1) {
switch (opt) {
case '-':
if (aflag)
usage();
do_stdin = 1;
break;
case 'a':
if (do_stdin)
usage();
aflag = 1;
break;
case '?':
default:
usage();
/* NOTREACHED */
}
}
argc -= optind;
argv += optind;
if (aflag) {
if (argc != 0)
usage();
dump_config();
} else {
if (do_stdin) {
while (fgets(line, BUFSIZ, stdin) != NULL &&
rv == EX_OK) {
if (line[0] == '\n')
continue;
if ((cp = strrchr(line, '\n')) != NULL)
*cp = '\0';
rv = action(line);
}
if (ferror(stdin))
err(EX_NOINPUT, "stdin");
} else {
if (argc == 0)
usage();
while (argc && rv == EX_OK) {
rv = action(*argv);
++argv;
--argc;
}
}
}
return (rv);
}
static int
action(char *line)
{
int rv;
char *keyword, *arg;
keyword = strdup(line);
if (keyword == NULL)
err(EX_OSERR, "malloc() failed");
if ((arg = strrchr(keyword, '=')) != NULL)
*arg++ = '\0';
switch (rv = ofwo_action(keyword, arg)) {
case EX_UNAVAILABLE:
warnx("nothing available for '%s'.", keyword);
break;
case EX_DATAERR:
warnx("invalid value '%s' for '%s'.", arg, keyword);
break;
}
free(keyword);
return(rv);
}
static void
dump_config()
{
ofwo_dump();
}