Hi, "recent" macppc machines, power{mac,book}2.1 and onward, also known as "Core99" powermac and after, have an onboard nvram of 8kB (actually 2 banks of 8kB).
This nvram is used, at least by the Open Firmware, to store its variables. The following diff is a driver to access the nvram without calling the openfirmware. Its interest may be not that important considering that we can already access the nvram through the "options" node of the ofw. But here it is, if someone is interested ;) I've tested it with an amd flash, it's basically a port of apple's xnu driver so the commands should be correct. I also include at the end of the mail a slightly modified version of FreeBSD's nvram(8) utility, if you want to test it. Comments are welcome. Have fun, Martin Index: conf/GENERIC =================================================================== RCS file: /cvs/src/sys/arch/macppc/conf/GENERIC,v retrieving revision 1.207 diff -u -p -r1.207 GENERIC --- conf/GENERIC 19 Apr 2011 23:07:54 -0000 1.207 +++ conf/GENERIC 5 May 2011 14:43:23 -0000 @@ -32,6 +32,7 @@ config bsd swap generic mainbus0 at root cpu* at mainbus0 mem* at mainbus0 +nvram0 at mainbus0 # nvram mpcpcibr* at mainbus0 # MPC106 PCI Bridge. memc* at mainbus0 @@ -146,7 +147,6 @@ bm* at macobio? # BMAC ethernet mc* at macobio? # MACE ethernet #esp* at macobio? flags 0x0000ffff # 53c9x SCSI mesh* at macobio? flags 0xffff # MESH SCSI -#nvram* at macobio? # nvram adb* at macobio? # Apple Desktop Bus apm0 at adb? # APM emulation piic0 at adb? # PMU I2C Index: conf/files.macppc =================================================================== RCS file: /cvs/src/sys/arch/macppc/conf/files.macppc,v retrieving revision 1.63 diff -u -p -r1.63 files.macppc --- conf/files.macppc 6 Dec 2010 20:10:18 -0000 1.63 +++ conf/files.macppc 5 May 2011 14:43:23 -0000 @@ -51,6 +51,10 @@ device memc {} attach memc at mainbus file arch/macppc/dev/uni_n.c memc +device nvram +attach nvram at mainbus +file arch/macppc/dev/nvram.c nvram needs-flag + major {rd = 17} major {wd = 0} major {sd = 2} @@ -111,7 +115,6 @@ device macobio {} attach macobio at pci file arch/macppc/pci/macobio.c macobio - # kauai ATA glue device kauaiata {} attach kauaiata at pci @@ -219,10 +222,6 @@ file arch/macppc/dev/aoa.c aoa device daca: audio, auconv, mulaw, i2s attach daca at macobio file arch/macppc/dev/daca.c daca - -#device nvram -#attach nvram at macobio -#file arch/macppc/dev/nvram.c nvram needs-flag device macgpio {} attach macgpio at macobio with macgpio Index: dev/nvram.c =================================================================== RCS file: dev/nvram.c diff -N dev/nvram.c --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ dev/nvram.c 5 May 2011 14:43:23 -0000 @@ -0,0 +1,518 @@ +/* $OpenBSD$ */ +/* + * Copyright (c) 2011 Martin Pieuchot <m...@nolizard.org> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include <sys/param.h> +#include <sys/systm.h> +#include <sys/malloc.h> +#include <sys/uio.h> +#include <sys/fcntl.h> + +#include <machine/pio.h> +#include <machine/autoconf.h> + +#include <dev/ofw/openfirm.h> + +#define NVRAM_DEBUG 1 + +#ifdef NVRAM_DEBUG +#define DPRINTF(x...) printf(x) +#else +#define DPRINTF(x...) +#endif + +#define DEVNAME(sc) ((sc)->sc_dev.dv_xname) + +#define NVRAM_SIZE 0x2000 /* 8kB for each bank. */ +#define NVRAM_SIGN 0x5a + +struct nvram_header { + uint8_t sign; + uint8_t csum; /* checksum sign, len & name */ + uint16_t len; + char name[12]; + uint32_t adler; /* adler32 bank checksum */ + uint32_t gen; + uint32_t reserved[2]; +}; + +#define CHRP_SIZE 14 /* size of len + name */ +#define ADLER_OFFSET 20 /* offset of hdr->gen */ +#define ADLER_SIZE (NVRAM_SIZE - ADLER_OFFSET) + +struct nvram_softc { + struct device sc_dev; + int sc_node; + uint8_t *sc_map; + uint8_t *sc_bank0; /* current bank */ + uint8_t *sc_bank1; /* next bank */ + uint8_t *sc_data; /* buffer */ +#define NVRAM_OPEN 0x01 + char sc_open; +#define NVRAM_TYPE_AMD 0 +#define NVRAM_TYPE_SHARP 1 + char sc_type; +}; + +int nvram_match(struct device *, void *, void *); +void nvram_attach(struct device *, struct device *, void *); +int nvram_detach(struct device *, int); + +int nvramopen(dev_t, int, int, struct proc *); +int nvramclose(dev_t, int, int, struct proc *); +int nvramrw(dev_t, struct uio *, int); +int nvramioctl(dev_t, u_long, caddr_t, int, struct proc *); +paddr_t nvrammmap(dev_t, off_t, int); + +int nvram_sync(struct nvram_softc *); +int nvram_erase_next_bank(struct nvram_softc *); +int nvram_write_next_bank(struct nvram_softc *); + +int nvram_csum_valid(uint8_t *); +uint8_t nvram_chrp_csum(struct nvram_header *); +uint32_t nvram_adler_csum(struct nvram_header *); + +int nvram_erase_amd_bank(uint8_t *); +int nvram_write_amd_bank(uint8_t *, uint8_t *); +int nvram_wait_amd_command(uint8_t *); +int nvram_erase_sharp_bank(uint8_t *); +int nvram_write_sharp_bank(uint8_t *, uint8_t *); +int nvram_wait_sharp_command(uint8_t *); + +struct cfattach nvram_ca = { + sizeof(struct nvram_softc), nvram_match, nvram_attach, nvram_detach +}; + +struct cfdriver nvram_cd = { + NULL, "nvram", DV_DULL +}; + +int +nvram_match(struct device *parent, void *arg, void *aux) +{ + struct confargs *ca = aux; + char compatible[32]; + + if (strcmp(ca->ca_name, "nvram") != 0) + return (0); + + OF_getprop(ca->ca_node, "compatible", compatible, sizeof(compatible)); + DPRINTF("%s: compatible = %s\n", __func__, compatible); + + /* Only support Power{Mac,Book}2,1 and higher */ + if (strcmp(compatible, "amd-0137") != 0 && + strcmp(compatible, "nvram,flash") != 0) + return (0); + + return (1); +} + +void +nvram_attach(struct device *parent, struct device *self, void *aux) +{ + struct nvram_softc *sc = (struct nvram_softc *)self; + struct confargs *ca = aux; + int nreg, gen0, gen1; + char compatible[32]; + uint32_t reg[4]; + + sc->sc_node = ca->ca_node; + + OF_getprop(sc->sc_node, "compatible", compatible, sizeof(compatible)); + if (strcmp(compatible, "amd-0137") == 0) { + sc->sc_type = NVRAM_TYPE_AMD; + printf(": AMD BootROM flash\n"); + } else { + sc->sc_type = NVRAM_TYPE_SHARP; + printf(": Micron/Sharp BootROM Flash\n"); + } + + nreg = OF_getprop(sc->sc_node, "reg", reg, sizeof(reg)); + DPRINTF("%s: nreg = %d\n", DEVNAME(sc), nreg); + + sc->sc_map = mapiodev(reg[0], NVRAM_SIZE * 2); + + gen0 = nvram_csum_valid(sc->sc_map); + gen1 = nvram_csum_valid(sc->sc_map + NVRAM_SIZE); + DPRINTF("%s: gen0 = %d, gen1 = %d, ", DEVNAME(sc), gen0, gen1); + + if (gen0 > gen1) { + sc->sc_bank0 = sc->sc_map; + sc->sc_bank1 = sc->sc_map + NVRAM_SIZE; + } else { + sc->sc_bank0 = sc->sc_map + NVRAM_SIZE; + sc->sc_bank1 = sc->sc_map; + } + DPRINTF("bank0 = 0x%x, bank1 = 0x%x\n", sc->sc_bank0, sc->sc_bank1); + + if ((sc->sc_data = malloc(NVRAM_SIZE, M_DEVBUF, M_NOWAIT)) == NULL) { + printf("%s: could not allocate buffer\n", DEVNAME(sc)); + return; + } + + bcopy(sc->sc_bank0, sc->sc_data, NVRAM_SIZE); +} + +int +nvram_detach(struct device *self, int flags) +{ + struct nvram_softc *sc = (struct nvram_softc *)self; + int err = 0; + + err = nvram_sync(sc); + + unmapiodev(sc->sc_map, NVRAM_SIZE * 2); + + if (sc->sc_data != NULL) + free(sc->sc_data, M_DEVBUF); + + return (err); +} + +int +nvramopen(dev_t dev, int flag, int mode, struct proc *p) +{ + struct nvram_softc *sc; + + /* nvram0 only */ + if (!nvram_cd.cd_ndevs || minor(dev) != 0) + return (ENXIO); + + if ((sc = nvram_cd.cd_devs[0]) == NULL) + return (ENXIO); + + if (sc->sc_data == NULL) + return (ENXIO); + + if (sc->sc_open & NVRAM_OPEN) + return (EBUSY); + sc->sc_open |= NVRAM_OPEN; + + return (0); +} + +int +nvramclose(dev_t dev, int flags, int fmt, struct proc *p) +{ + struct nvram_softc *sc = nvram_cd.cd_devs[0]; + int err = 0; + + err = nvram_sync(sc); + + sc->sc_open &= ~NVRAM_OPEN; + + return (err); +} + +int +nvramrw(dev_t dev, struct uio *uio, int flags) +{ + struct nvram_softc *sc = nvram_cd.cd_devs[0]; + size_t cnt; + off_t off; + + + DPRINTF("%s: resid=%d\n", DEVNAME(sc), uio->uio_resid); + + if ((off = uio->uio_offset) > NVRAM_SIZE) + return (EFAULT); + + cnt = min(uio->uio_resid, NVRAM_SIZE - off); + + return (uiomove(sc->sc_data + off, cnt, uio)); +} + +int +nvramioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p) +{ + return (EINVAL); +} + +paddr_t +nvrammmap(dev_t dev, off_t off, int prot) +{ + return (EINVAL); +} + +int +nvram_sync(struct nvram_softc *sc) +{ + struct nvram_header *hdr; + uint8_t *tmp; + + if (bcmp(sc->sc_data, sc->sc_bank0, NVRAM_SIZE) == 0) + return (0); + + hdr = (struct nvram_header *)sc->sc_data; + hdr->gen++; + hdr->csum = nvram_chrp_csum(hdr); + hdr->adler = nvram_adler_csum(hdr); + + if (nvram_erase_next_bank(sc)) + return (1); + + if (nvram_write_next_bank(sc)) + return (1); + + tmp = sc->sc_bank0; + sc->sc_bank0 = sc->sc_bank1; + sc->sc_bank1 = tmp; + + return (0); +} + +int +nvram_erase_next_bank(struct nvram_softc *sc) +{ + if (sc->sc_type == NVRAM_TYPE_AMD) + return (nvram_erase_amd_bank(sc->sc_bank1)); + else + return (nvram_erase_sharp_bank(sc->sc_bank1)); +} + +int +nvram_write_next_bank(struct nvram_softc *sc) +{ + if (sc->sc_type == NVRAM_TYPE_AMD) + return (nvram_write_amd_bank(sc->sc_bank1, sc->sc_data)); + else + return (nvram_write_sharp_bank(sc->sc_bank1, sc->sc_data)); +} + +int +nvram_csum_valid(uint8_t *bank) +{ + struct nvram_header *hdr = (struct nvram_header *)bank; + + if (hdr->sign != NVRAM_SIGN) + return (0); + + if (hdr->csum != nvram_chrp_csum(hdr)) + return (0); + + if (hdr->adler != nvram_adler_csum(hdr)) + return (0); + + return (hdr->gen); +} + +uint8_t +nvram_chrp_csum(struct nvram_header *hdr) +{ + uint8_t *data = (uint8_t*) &hdr->len; + uint8_t csum, sum; + int i; + + csum = hdr->sign; + for (i = 0; i < CHRP_SIZE; i++) { + sum = csum + data[i]; + if (sum < csum) + sum++; + csum = sum; + } + + return (csum); +} + +uint32_t +nvram_adler_csum(struct nvram_header *hdr) +{ + uint8_t *data = (uint8_t*) &hdr->gen; + uint32_t low, high; + int i; + + low = 1; + high = 0; + + for (i = 0; i < ADLER_SIZE; i++) { + if (i % 5000 == 0) { + low %= 65521L; + high %= 65521L; + } + low += data[i]; + high += low; + } + + low %= 65521L; + high %= 65521L; + + return ((high << 16) | low); +} + +int +nvram_wait_amd_command(uint8_t *bank) +{ + uint8_t a, b; + +#define AMD_STATUS_DONE 0x40 +#define AMD_STATUS_ERROR 0x20 + + while (1) { + a = inb(bank); + b = inb(bank); + + /* A command is complete when the toggling is over. */ + if (((a ^ b) & AMD_STATUS_DONE) == 0) + break; + + if (b & AMD_STATUS_ERROR) { + a = inb(bank); + b = inb(bank); + + if (((a ^ b) & AMD_STATUS_DONE) != 0) { + DPRINTF("%s: timeout, reseting.\n", __func__); + outb(bank, 0xf0); /* reset */ + return (1); + } + + break; + } + } + + return (0); +} + +int +nvram_erase_amd_bank(uint8_t *bank) +{ + int i; + + outb(bank + 0x555, 0xaa); /* unlock bypass 1 */ + outb(bank + 0x2aa, 0x55); /* unlock bypass 2 */ + outb(bank + 0x555, 0x80); /* erase setup */ + + outb(bank + 0x555, 0xaa); /* unlock bypass 1 */ + outb(bank + 0x2aa, 0x55); /* unlock bypass 2 */ + outb(bank, 0x30); /* erase confirm */ + + /* + * Wait for the sector erase timeout before checking for toggling. + * Timeout should be 50 microseconds. + */ + delay(55); + + if (nvram_wait_amd_command(bank)) + return (1); + + for (i = 0; i < NVRAM_SIZE; i++) { + if (bank[i] != 0xff) { + DPRINTF("%s: erase failed.\n", __func__); + return (1); + } + } + + return (0); +} + +int +nvram_write_amd_bank(uint8_t *bank, uint8_t *data) +{ + int i; + + /* + * The unlocked mode allows to write the data without + * unlocking each individual byte, thus, faster. + */ + outb(bank + 0x555, 0xaa); /* unlock bypass 1 */ + outb(bank + 0x2aa, 0x55); /* unlock bypass 2 */ + outb(bank + 0x555, 0x20); /* unlocked mode */ + + for (i = 0; i < NVRAM_SIZE; i++) { + outb(bank + 0x555, 0xa0); /* write confirm */ + outb(bank + i, data[i]); + + if (nvram_wait_amd_command(bank)) { + /* XXX needed? */ + outb(bank, 0x90); /* unlock reset 1 */ + outb(bank, 0x00); /* unlock reset 2 */ + return (1); + } + } + + outb(bank, 0x90); /* unlock reset 1 */ + outb(bank, 0x00); /* unlock reset 2 */ + + if (bcmp(bank, data, NVRAM_SIZE) != 0) { + DPRINTF("%s: write failed.\n", __func__); + return (1); + } + + return (0); +} + +int +nvram_wait_sharp_command(uint8_t *bank) +{ + uint8_t status; + +#define SHARP_STATUS_DONE 0x80 +#define SHARP_STATUS_ERROR 0x38 + + do { + status = inb(bank); + } while ((status & SHARP_STATUS_DONE) == 0); + + if (status & SHARP_STATUS_ERROR) + return (1); + + return (0); +} + +int +nvram_erase_sharp_bank(uint8_t *bank) +{ + int i; + + outb(bank, 0x20); /* erase setup */ + outb(bank, 0xd0); /* erase confirm */ + + if (nvram_wait_sharp_command(bank)) + return (1); + + outb(bank, 0xff); /* reset */ + + for (i = 0; i < NVRAM_SIZE; i++) { + if (bank[i] != 0xff) { + DPRINTF("%s: erase failed.\n", __func__); + return (1); + } + } + + return (0); +} + +int +nvram_write_sharp_bank(uint8_t *bank, uint8_t *data) +{ + int i; + + for (i = 0; i < NVRAM_SIZE; i++) { + outb(bank + i, 0x40); /* write setup */ + outb(bank + i, data[i]); + + if (nvram_wait_sharp_command(bank)) + return (1); + } + + outb(bank, 0xff); /* reset */ + + if (bcmp(bank, data, NVRAM_SIZE) != 0) { + DPRINTF("%s: write failed.\n", __func__); + return (1); + } + + return (0); +} Index: include/conf.h =================================================================== RCS file: /cvs/src/sys/arch/macppc/include/conf.h,v retrieving revision 1.13 diff -u -p -r1.13 conf.h --- include/conf.h 21 Jul 2010 15:40:04 -0000 1.13 +++ include/conf.h 5 May 2011 14:43:23 -0000 @@ -54,3 +54,7 @@ cdev_decl(zs); (dev_type_mmap((*))) enodev } cdev_decl(openprom); + +#define nvramread nvramrw +#define nvramwrite nvramrw +cdev_decl(nvram); Index: macppc/conf.c =================================================================== RCS file: /cvs/src/sys/arch/macppc/macppc/conf.c,v retrieving revision 1.45 diff -u -p -r1.45 conf.c --- macppc/conf.c 19 Apr 2011 23:07:54 -0000 1.45 +++ macppc/conf.c 5 May 2011 14:43:23 -0000 @@ -121,6 +121,7 @@ cdev_decl(nnpfs_dev); #include "apm.h" #include "bthub.h" +#include "nvram.h" #include "wsmux.h" @@ -159,7 +160,7 @@ struct cdevsw cdevsw[] = { cdev_disk_init(NCD,cd), /* 9: SCSI CD-ROM */ cdev_notdef(), /* 10: SCSI changer */ cdev_disk_init(NWD,wd), /* 11: ST506/ESDI/IDE disk */ - cdev_notdef(), /* 12 */ + cdev_mm_init(NNVRAM,nvram), /* 12: /dev/nvram */ cdev_notdef(), /* 13 */ cdev_notdef(), /* 14 */ cdev_notdef(), /* 15 */ Index: macppc/mainbus.c =================================================================== RCS file: /cvs/src/sys/arch/macppc/macppc/mainbus.c,v retrieving revision 1.21 diff -u -p -r1.21 mainbus.c --- macppc/mainbus.c 19 Apr 2009 17:53:39 -0000 1.21 +++ macppc/mainbus.c 5 May 2011 14:43:23 -0000 @@ -36,6 +36,8 @@ #include <machine/autoconf.h> #include <dev/ofw/openfirm.h> +#include "nvram.h" + struct mainbus_softc { struct device sc_dv; struct bushook sc_bus; @@ -198,6 +200,14 @@ mbattach(struct device *parent, struct d nca.ca_bus = &sc->sc_bus; config_found(self, &nca, mbprint); } +#if NNVRAM > 0 + if (strcmp(name, "nvram") == 0) { + nca.ca_name = "nvram"; + nca.ca_node = node; + nca.ca_bus = &sc->sc_bus; + config_found(self, &nca, mbprint); + } +#endif if (strcmp(name, "pci") == 0) { nca.ca_name = "mpcpcibr"; nca.ca_node = node; ----------- DIFF END ------------------------------------------------- /* * Copyright (c) 2006 Maxim Sobolev <sobo...@freebsd.org> * All rights reserved. * * 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. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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. * * $FreeBSD$ */ #include <sys/types.h> #include <sys/uio.h> #include <err.h> #include <errno.h> #include <fcntl.h> #include <paths.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #define NVRAM_SIZE 0x2000 #define DEVICE_NAME _PATH_DEV "nvram" struct chrp_header { uint8_t sign; uint8_t csum; uint16_t len; char name[12]; }; static void usage(void); static int remove_var(uint8_t *, int, const char *); static int append_var(uint8_t *, int, const char *, const char *); struct deletelist { char *name; struct deletelist *next; struct deletelist *last; }; int main(int argc, char **argv) { int opt, dump, fd, res, i, size; uint8_t buf[NVRAM_SIZE], *cp, *common; struct chrp_header *header; struct deletelist *dl; dump = 0; dl = NULL; while((opt = getopt(argc, argv, "d:p")) != -1) { switch(opt) { case 'p': dump = 1; break; case 'd': if (dl == NULL) { dl = malloc(sizeof(*dl)); if (dl == NULL) err(1, "malloc"); bzero(dl, sizeof(*dl)); dl->last = dl; } else { dl->last->next = malloc(sizeof(*dl)); if (dl->last->next == NULL) err(1, "malloc"); dl->last = dl->last->next; bzero(dl->last, sizeof(*dl)); } dl->last->name = optarg; break; default: usage(); /* Not reached */ } } argc -= optind; argv += optind; if (argc == 0 && dump == 0 && dl == NULL) { usage(); /* Not reached */ } fd = open(DEVICE_NAME, O_RDWR); if (fd == -1) err(1, DEVICE_NAME); for (i = 0; i < (int)sizeof(buf);) { res = read(fd, buf + i, sizeof(buf) - i); if (res == -1 && errno != EINTR) err(1, DEVICE_NAME); if (res == 0) break; if (res > 0) i += res; } if (i != sizeof(buf)) errx(1, "%s: short read", DEVICE_NAME); /* Locate common block */ size = 0; for (cp = buf; cp < buf + sizeof(buf); cp += size) { header = (struct chrp_header *)cp; size = header->len * 0x10; if (strncmp(header->name, "common", 7) == 0) break; } if (cp >= buf + sizeof(buf) || size <= (int)sizeof(struct chrp_header)) errx(1, "%s: no common block", DEVICE_NAME); common = cp + sizeof(struct chrp_header); size -= sizeof(struct chrp_header); if (dump != 0) { while (size > 0) { i = strlen(common) + 1; if (i == 1) break; printf("%s\n", common); size -= i; common += i; } exit(0); } for (;dl != NULL; dl = dl->next) { if (remove_var(common, size, dl->name) == 0) warnx("%s: no such variable", dl->name); } for (; argc > 0; argc--, argv++) { cp = strchr(*argv, '='); if (cp == NULL) errx(1, "%s: invalid argument", *argv); cp[0] = '\0'; cp++; remove_var(common, size, *argv); if (append_var(common, size, *argv, cp) == -1) errx(1, "%s: error setting variable", *argv); } lseek(fd, 0, SEEK_SET); for (i = 0; i < (int)sizeof(buf);) { res = write(fd, buf + i, sizeof(buf) - i); if (res == -1 && errno != EINTR) err(1, DEVICE_NAME); if (res == 0) break; if (res > 0) i += res; } if (i != sizeof(buf)) errx(1, "%s: short write", DEVICE_NAME); if (close(fd) == -1) err(1, DEVICE_NAME); exit(0); } static void usage(void) { fprintf(stderr, "usage: nvram [-p] | [-d name ...] [name=value ...]\n"); exit(1); } static int remove_var(uint8_t *buf, int len, const char *var_name) { int nremoved, i, name_len; nremoved = 0; name_len = strlen(var_name); while (len > 0) { i = strlen(buf) + 1; if (i == 1) break; if (strncmp(buf, var_name, name_len) == 0 && buf[name_len] == '=') { memmove(buf, buf + i, len - i); memset(buf + len - i, '\0', i); nremoved += 1; continue; } len -= i; buf += i; } return nremoved; } static int append_var(uint8_t *buf, int len, const char *var_name, const char *var_value) { int i, append_len; while (len > 0) { i = strlen(buf) + 1; if (i == 1) break; len -= i; buf += i; } append_len = strlen(var_name) + strlen(var_value) + 2; if (len < append_len) return -1; snprintf(buf, append_len, "%s=%s", var_name, var_value); return 0; }