#include <sys/types.h>
#include <sys/ioctl.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <machine/eeprom.h>
#include <machine/openpromio.h>
#include "defs.h"
extern char *path_openprom;
extern int eval;
extern int verbose;
static char err_str[BUFSIZE];
static void op_notsupp (const struct extabent *, struct opiocdesc *, char *);
static const struct extabent opextab[] = {
{ "security-password", op_notsupp },
{ "security-mode", op_notsupp },
{ "oem-logo", op_notsupp },
{ NULL, op_notsupp },
};
#define BARF(str1, str2) { \
snprintf(err_str, sizeof err_str, "%s: %s", (str1), (str2)); \
++eval; \
return (err_str); \
};
void
op_action(char *keyword, char *arg)
{
char *cp;
if ((cp = op_handler(keyword, arg)) != NULL)
warnx("%s", cp);
return;
}
int
check_for_openprom(void)
{
int fd, rv, optnode;
if ((fd = open(path_openprom, O_RDONLY)) < 0)
return (0);
rv = ioctl(fd, OPIOCGETOPTNODE, (char *)&optnode);
close (fd);
return (rv == 0);
}
char *
op_handler(char *keyword, char *arg)
{
struct opiocdesc opio;
const struct extabent *ex;
char opio_buf[BUFSIZE];
int fd, optnode;
if ((fd = open(path_openprom, arg ? O_RDWR : O_RDONLY, 0640)) < 0)
BARF(path_openprom, strerror(errno));
for (ex = opextab; ex->ex_keyword != NULL; ++ex)
if (strcmp(ex->ex_keyword, keyword) == 0)
break;
if (ioctl(fd, OPIOCGETOPTNODE, (char *)&optnode) < 0) {
(void)close(fd);
BARF("OPIOCGETOPTNODE", strerror(errno));
}
memset(&opio_buf[0], 0, sizeof(opio_buf));
memset(&opio, 0, sizeof(opio));
opio.op_nodeid = optnode;
opio.op_name = keyword;
opio.op_namelen = strlen(opio.op_name);
if (arg) {
if (verbose) {
printf("old: ");
opio.op_buf = &opio_buf[0];
opio.op_buflen = sizeof(opio_buf);
if (ioctl(fd, OPIOCGET, (char *)&opio) < 0) {
(void)close(fd);
BARF("OPIOCGET", strerror(errno));
}
if (opio.op_buflen <= 0) {
printf("nothing available for %s\n", keyword);
goto out;
}
if (ex->ex_keyword != NULL)
(*ex->ex_handler)(ex, &opio, NULL);
else
printf("%s\n", opio.op_buf);
}
out:
if (ex->ex_keyword != NULL)
(*ex->ex_handler)(ex, &opio, arg);
else {
opio.op_buf = arg;
opio.op_buflen = strlen(arg);
}
if (ioctl(fd, OPIOCSET, (char *)&opio) < 0) {
(void)close(fd);
BARF("invalid keyword", keyword);
}
if (verbose) {
printf("new: ");
if (ex->ex_keyword != NULL)
(*ex->ex_handler)(ex, &opio, NULL);
else
printf("%s\n", opio.op_buf);
}
} else {
opio.op_buf = &opio_buf[0];
opio.op_buflen = sizeof(opio_buf);
if (ioctl(fd, OPIOCGET, (char *)&opio) < 0) {
(void)close(fd);
BARF("OPIOCGET", strerror(errno));
}
if (opio.op_buflen <= 0) {
(void)snprintf(err_str, sizeof err_str,
"nothing available for %s", keyword);
return (err_str);
}
if (ex->ex_keyword != NULL)
(*ex->ex_handler)(ex, &opio, NULL);
else
printf("%s=%s\n", keyword, opio.op_buf);
}
(void)close(fd);
return (NULL);
}
static void
op_notsupp(const struct extabent *exent, struct opiocdesc *opiop, char *arg)
{
warnx("property `%s' not yet supported", exent->ex_keyword);
}
void
op_dump(void)
{
struct opiocdesc opio1, opio2;
const struct extabent *ex;
char buf1[BUFSIZE], buf2[BUFSIZE], buf3[BUFSIZE], buf4[BUFSIZE];
int fd, optnode;
if ((fd = open(path_openprom, O_RDONLY, 0640)) < 0)
err(1, "open: %s", path_openprom);
if (ioctl(fd, OPIOCGETOPTNODE, (char *)&optnode) < 0)
err(1, "OPIOCGETOPTNODE");
memset(&opio1, 0, sizeof(opio1));
memset(buf1, 0, sizeof(buf1));
memset(buf2, 0, sizeof(buf2));
opio1.op_nodeid = opio2.op_nodeid = optnode;
opio1.op_name = buf1;
opio1.op_buf = buf2;
opio2.op_name = buf3;
opio2.op_buf = buf4;
for (;;) {
opio1.op_namelen = strlen(opio1.op_name);
opio1.op_buflen = sizeof(buf2);
if (ioctl(fd, OPIOCNEXTPROP, (char *)&opio1) < 0)
err(1, "ioctl: OPIOCNEXTPROP");
strcpy(opio2.op_name, opio1.op_buf);
opio2.op_namelen = strlen(opio2.op_name);
if (opio2.op_namelen == 0) {
(void)close(fd);
return;
}
memset(opio2.op_buf, 0, sizeof(buf4));
opio2.op_buflen = sizeof(buf4);
if (ioctl(fd, OPIOCGET, (char *)&opio2) < 0)
err(1, "ioctl: OPIOCGET");
for (ex = opextab; ex->ex_keyword != NULL; ++ex)
if (strcmp(ex->ex_keyword, opio2.op_name) == 0)
break;
if (ex->ex_keyword != NULL)
(*ex->ex_handler)(ex, &opio2, NULL);
else
printf("%s=%s\n", opio2.op_name, opio2.op_buf);
memset(opio1.op_name, 0, sizeof(buf1));
memset(opio1.op_buf, 0, sizeof(buf2));
strcpy(opio1.op_name, opio2.op_name);
}
}