File: nvsetvol.c

package info (click to toggle)
powerpc-utils 1.1.3-19
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 276 kB
  • ctags: 381
  • sloc: ansic: 2,793; makefile: 166; sh: 58
file content (119 lines) | stat: -rw-r--r-- 3,208 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/* set volume of the startup chime in the PowerMac nvram. 

   Written by Matteo Frigo <athena@fftw.org>.
   $Id: nvsetvol.c,v 1.1 2003/01/11 11:26:30 athena Exp $

   This program is in the public domain.
*/

/* The volume is stored as a byte at address 8 in the parameter ram. */

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <asm/nvram.h>
#ifndef IOC_NVRAM_SYNC
#warning IOC_NVRAM_SYNC undefined -- update your headers
#define IOC_NVRAM_SYNC _IO('p', 0x43)
#endif

typedef struct {
     unsigned char sig;
     unsigned char cksum;
     unsigned short len;
     char name[12];
} header;

void die(const char *s) __attribute__((noreturn));
void die(const char *s)
{
     perror(s);
     exit(1);
}

void seek_pram(int fd, int offset)
{
     if (lseek(fd, offset, SEEK_SET) < 0)
	  die("lseek");
}

// MSch: only works for newworld - oldworld have XPRAM at offset 0x1300 fixed. 

int find_pram(int fd, int *size)
{
     header buf;
     int offset = 0;
     int rc = 0;
     while((rc = read(fd, &buf, sizeof(header))) == sizeof(header)) {
	  int sz = buf.len * 16;
	  // what's the sig in char?
	  // if (buf.sig == 0x1275) fprintf(stderr, "sig at offset %d rc %d buf.len %d buf.name %s \n", offset, rc, buf.len, buf.name);
	  // if (buf.sig == 0x5a) fprintf(stderr, "sig at offset %x rc %d buf.len %x buf.name %s \n", offset, rc, buf.len, buf.name);
	  // if (sz) fprintf(stderr, "offset %x rc %d sig %x buf.len %x buf.name %s \n", offset, rc, buf.sig, buf.len, buf.name);
	  if (!strncmp(buf.name, "APL,MacOS75", 11)) {
	       *size = sz - sizeof(header);
	       // fprintf(stderr, "XPRAM offset %x size %x\n", offset + sizeof(header), *size); 
	       return offset + sizeof(header);
	  }
	  if (sz)
	    offset += sz;
          else
            offset += sizeof(header);
	  seek_pram(fd, offset);
     }
     // no Core99 style PRAM found - just assume hard coded values as set in kernel:
     // XPRAM 0x1300
     // NR    0x1400
     // OF    0x1800
     // TODO: use ioctl to get PRAM offset
     // fprintf(stderr, "no NW PRAM found, assuming OW XPRAM offset 0x1300 size 0x100\n");
     *size = 0x100;
     return (0x1300);
     // NOTREACHED
     die("no PRAM found");
}

#define VOLADDR 8

int main(int argc, char *argv[])
{
     int fd, offset, size, i;
     char *buf;

     fd = open("/dev/nvram", O_RDWR);
     if (fd < 0) die("can't open /dev/nvram");
     
     offset = find_pram(fd, &size);
     buf = malloc(size);
     if (!buf) die("can't malloc()");

     seek_pram(fd, offset);
     if (read(fd, buf, size) != size)
	  die("error reading /dev/nvram");

     printf("current volume is %d\n", (unsigned char) buf[VOLADDR]);


#ifdef DEBUG_PRAM
     for (i=0; i < size; i++) {
          printf("cell %d : %d\n", i, (unsigned char) buf[i]);
     }
#endif
     
     if (argc > 1) {
	  buf[VOLADDR] = atoi(argv[1]);

	  seek_pram(fd, offset);
	  if (write(fd, buf, size) != size)
	       die("error writing /dev/nvram");
	  printf("new volume is %d\n", (unsigned char) buf[VOLADDR]);
     }
     ioctl(fd, IOC_NVRAM_SYNC);
     close(fd);
     return 0;
}