-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnvvideo.c
108 lines (92 loc) · 2.1 KB
/
nvvideo.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.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
#define NVSTART 0x140f
#define NVSIZE 0x2
unsigned char nvbuf[NVSIZE];
int nvfd;
void
nvload( void )
{
if (lseek(nvfd, NVSTART, 0) < 0
|| read(nvfd, &nvbuf, NVSIZE) != NVSIZE) {
perror("Error reading /dev/nvram");
exit(EXIT_FAILURE);
}
}
void
nvstore( void )
{
if (lseek(nvfd, NVSTART, 0) < 0
|| write(nvfd, &nvbuf, NVSIZE) != NVSIZE) {
perror("Error writing /dev/nvram");
exit(EXIT_FAILURE);
}
}
struct nvvar {
char *name;
} nvvars[] = {
{"video-mode"},
{"color-mode"},
};
#define N_NVVARS (int) (sizeof(nvvars) / sizeof(nvvars[0]))
void
print_var(int i, /*@unused@*/ int indent)
{
(void) printf("%d", (int) nvbuf[i]);
(void) printf("\n");
}
int main(int ac, char **av)
{
int i = 0;
if (ac > 3 ||
(ac == 2 && (strcmp(av[1], "-?" ) == 0 || strcmp( av[1], "-h" ) == 0 || strcmp( av[1] , "--help" ) == 0)) ) {
(void) fprintf(stderr, "Usage: %s [variable [value]]\n", av[0]);
exit(EXIT_FAILURE);
}
if (ac >= 2) {
for (i = 0; i < N_NVVARS; ++i)
if (strcmp(av[1], nvvars[i].name) == 0)
break;
if (i >= N_NVVARS) {
(void) fprintf(stderr, "%s: no variable called '%s'\n",
av[1], av[1]);
exit(EXIT_FAILURE);
}
}
nvfd = open("/dev/nvram", ac <= 2? O_RDONLY: O_RDWR);
if (nvfd < 0) {
perror("Couldn't open /dev/nvram");
exit(EXIT_FAILURE);
}
nvload();
switch (ac) {
case 1:
for (i = 0; i < N_NVVARS; ++i) {
(void) printf("%-16s", nvvars[i].name);
print_var(i, 16);
}
break;
case 2:
print_var(i, 0);
break;
case 3:
if (strcmp(av[1],nvvars[0].name)==0)
nvbuf[0]=(unsigned char)strtoul(av[2], 0, 0);
if (strcmp(av[1],nvvars[1].name)==0)
nvbuf[1]=(unsigned char)strtoul(av[2], 0, 0);
nvstore();
break;
}
(void) ioctl(nvfd, IOC_NVRAM_SYNC);
(void) close(nvfd);
exit(EXIT_SUCCESS);
}