1 /* $NetBSD: bootpref.c,v 1.5 2009/03/14 21:04:07 dsl Exp $ */
3 * Copyright (c) 1998 The NetBSD Foundation, Inc.
6 * This code is derived from software contributed to The NetBSD Foundation
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/types.h>
41 static void usage(void);
42 static int openNVRAM(void);
43 static void closeNVRAM(int);
44 static u_char
readNVRAM(int, int);
45 static void writeNVRAM(int, int, u_char
);
46 static void getNVpref(int, u_char
[]);
47 static void setNVpref(int, u_char
[], int, int);
48 static void showOS(u_char
);
49 static void showLang(u_char
);
50 static void showKbdLang(u_char
);
51 static void showDateFmt(u_char
);
52 static void showDateSep(u_char
);
53 static void showVideo2(u_char
);
54 static void showVideo1(u_char
, u_char
);
55 static int checkOS(u_char
*, char *);
56 static int checkLang(u_char
*, char *);
57 static int checkKbdLang(u_char
*, char *);
58 static int checkInt(u_char
*, char *, int, int);
59 static int checkDateFmt(u_char
*, char *);
60 static void checkDateSep(u_char
*, char *);
61 static int checkColours(u_char
*, char *);
64 #define SET_LANG 0x002
65 #define SET_KBDLANG 0x004
66 #define SET_HOSTID 0x008
67 #define SET_DATIME 0x010
68 #define SET_DATESEP 0x020
69 #define SET_BOOTDLY 0x040
70 #define SET_VID1 0x080
71 #define SET_VID2 0x100
75 #define ARRAY_KBDLANG 2
76 #define ARRAY_HOSTID 3
77 #define ARRAY_DATIME 4
78 #define ARRAY_DATESEP 5
79 #define ARRAY_BOOTDLY 6
83 static const char nvrdev
[] = PATH_NVRAM
;
86 main (int argc
, char *argv
[])
88 int c
, set
= 0, verbose
= 0;
90 u_char bootpref
[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
91 u_char check_hour
= 0, check_video1
= 0, check_video2
= 0;
94 bootpref
[ARRAY_VID2
] = readNVRAM (fd
, NVRAM_VID2
);
95 bootpref
[ARRAY_VID1
] = readNVRAM (fd
, NVRAM_VID1
);
97 while ((c
= getopt (argc
, argv
, "Vb:d:k:l:s:f:12e:c:nptv48oOxXiI")) != -1) {
103 if (checkOS (&bootpref
[ARRAY_OS
], optarg
))
109 if (checkInt (&bootpref
[ARRAY_BOOTDLY
], optarg
,
116 if (checkKbdLang (&bootpref
[ARRAY_KBDLANG
], optarg
))
122 if (checkLang (&bootpref
[ARRAY_LANG
], optarg
))
128 if (checkInt (&bootpref
[ARRAY_HOSTID
], optarg
,
135 if (checkDateFmt (&bootpref
[ARRAY_DATIME
], optarg
))
141 if (check_hour
& DATIME_24H
) {
144 bootpref
[ARRAY_DATIME
] &= ~DATIME_24H
;
146 check_hour
|= DATIME_24H
;
150 if (check_hour
& DATIME_24H
) {
153 bootpref
[ARRAY_DATIME
] |= DATIME_24H
;
155 check_hour
|= DATIME_24H
;
159 checkDateSep (&bootpref
[ARRAY_DATESEP
], optarg
);
163 if (checkColours (&bootpref
[ARRAY_VID2
], optarg
))
169 if (check_video2
& VID2_PAL
) {
172 bootpref
[ARRAY_VID2
] &= ~VID2_PAL
;
174 check_video2
|= VID2_PAL
;
178 if (check_video2
& VID2_PAL
) {
181 bootpref
[ARRAY_VID2
] |= VID2_PAL
;
183 check_video2
|= VID2_PAL
;
187 if (check_video2
& VID2_VGA
) {
190 bootpref
[ARRAY_VID2
] &= ~VID2_VGA
;
192 check_video2
|= VID2_VGA
;
196 if (check_video2
& VID2_VGA
) {
199 bootpref
[ARRAY_VID2
] |= VID2_VGA
;
201 check_video2
|= VID2_VGA
;
205 if (check_video2
& VID2_80CLM
) {
208 bootpref
[ARRAY_VID2
] &= ~VID2_80CLM
;
210 check_video2
|= VID2_80CLM
;
214 if (check_video2
& VID2_80CLM
) {
217 bootpref
[ARRAY_VID2
] |= VID2_80CLM
;
219 check_video2
|= VID2_80CLM
;
223 if (check_video2
& VID2_OVERSCAN
) {
226 bootpref
[ARRAY_VID2
] |= VID2_OVERSCAN
;
228 check_video2
|= VID2_OVERSCAN
;
232 if (check_video2
& VID2_OVERSCAN
) {
235 bootpref
[ARRAY_VID2
] &= ~VID2_OVERSCAN
;
237 check_video2
|= VID2_OVERSCAN
;
241 if (check_video2
& VID2_COMPAT
) {
244 bootpref
[ARRAY_VID2
] |= VID2_COMPAT
;
246 check_video2
|= VID2_COMPAT
;
250 if (check_video2
& VID2_COMPAT
) {
253 bootpref
[ARRAY_VID2
] &= ~VID2_COMPAT
;
255 check_video2
|= VID2_COMPAT
;
259 if (check_video1
& VID1_INTERLACE
) {
262 bootpref
[ARRAY_VID1
] |= VID1_INTERLACE
;
264 check_video1
|= VID1_INTERLACE
;
268 if (check_video1
& VID1_INTERLACE
) {
271 bootpref
[ARRAY_VID1
] &= ~VID1_INTERLACE
;
273 check_video1
|= VID1_INTERLACE
;
280 if (optind
!= argc
) {
284 setNVpref (fd
, bootpref
, set
, verbose
);
286 getNVpref (fd
, bootpref
);
289 return (EXIT_SUCCESS
);
296 "usage: bootpref [-V] [-b os] [-d delay] [-k kbd] [-l lang] "
298 "\t[-f fmt] [-1] [-2] [-e sep]\n"
299 "\t[-c colours] [-n] [-p] [-t] [-v] [-4] [-8]\n"
300 "\t[-o] [-O] [-x] [-X] [-i] [-I]\n");
309 if ((fd
= open (nvrdev
, O_RDWR
)) < 0) {
310 err (EXIT_FAILURE
, "%s", nvrdev
);
318 if (close (fd
) < 0) {
319 err (EXIT_FAILURE
, "%s", nvrdev
);
324 readNVRAM (int fd
, int pos
)
328 if (lseek(fd
, (off_t
)pos
, SEEK_SET
) != pos
) {
329 err(EXIT_FAILURE
, "%s", nvrdev
);
331 if (read (fd
, &val
, (size_t)1) != 1) {
332 err(EXIT_FAILURE
, "%s", nvrdev
);
338 writeNVRAM (int fd
, int pos
, u_char val
)
340 if (lseek(fd
, (off_t
)pos
, SEEK_SET
) != pos
) {
341 err(EXIT_FAILURE
, "%s", nvrdev
);
343 if (write (fd
, &val
, (size_t)1) != 1) {
344 err(EXIT_FAILURE
, "%s", nvrdev
);
349 getNVpref (int fd
, u_char bootpref
[])
352 printf ("Boot OS is ");
353 showOS (readNVRAM (fd
, NVRAM_BOOTPREF
));
355 printf ("Boot delay is %d seconds\n", readNVRAM (fd
, NVRAM_BOOTDLY
));
357 printf ("Language is ");
358 showLang (readNVRAM (fd
, NVRAM_LANG
));
359 /* Keyboard Language */
360 printf ("Keyboard is ");
361 showKbdLang (readNVRAM (fd
, NVRAM_KBDLANG
));
363 printf ("SCSI host ID is ");
364 if (readNVRAM (fd
, NVRAM_HOSTID
) & HOSTID_VALID
) {
365 printf ("%d\n", readNVRAM (fd
, NVRAM_HOSTID
) ^ HOSTID_VALID
);
369 /* Date format/separator */
370 printf ("Date format is ");
371 showDateFmt (readNVRAM (fd
, NVRAM_DATIME
));
372 printf ("Date separator is ");
373 showDateSep (readNVRAM (fd
, NVRAM_DATESEP
));
375 printf ("Video is (0x%02x, 0x%02x) :\n", readNVRAM (fd
, NVRAM_VID2
),
376 readNVRAM (fd
, NVRAM_VID1
));
377 showVideo2 (readNVRAM (fd
, NVRAM_VID2
));
378 showVideo1 (readNVRAM (fd
, NVRAM_VID1
), readNVRAM (fd
, NVRAM_VID2
));
382 setNVpref (int fd
, u_char bootpref
[], int set
, int verbose
)
386 writeNVRAM (fd
, NVRAM_BOOTPREF
, bootpref
[ARRAY_OS
]);
388 printf ("Boot OS set to ");
389 showOS (readNVRAM (fd
, NVRAM_BOOTPREF
));
393 if (set
& SET_BOOTDLY
) {
394 writeNVRAM (fd
, NVRAM_BOOTDLY
, bootpref
[ARRAY_BOOTDLY
]);
396 printf ("Boot delay set to %d seconds\n", readNVRAM (fd
,
401 if (set
& SET_LANG
) {
402 writeNVRAM (fd
, NVRAM_LANG
, bootpref
[ARRAY_LANG
]);
404 printf ("Language set to ");
405 showLang (readNVRAM (fd
, NVRAM_LANG
));
408 /* Keyboard Language */
409 if (set
& SET_KBDLANG
) {
410 writeNVRAM (fd
, NVRAM_KBDLANG
, bootpref
[ARRAY_KBDLANG
]);
412 printf ("Keyboard set to ");
413 showKbdLang (readNVRAM (fd
, NVRAM_KBDLANG
));
417 if (set
& SET_HOSTID
) {
418 writeNVRAM (fd
, NVRAM_HOSTID
, bootpref
[ARRAY_HOSTID
] |
421 printf ("SCSI host ID set to ");
422 printf ("%d\n", readNVRAM (fd
, NVRAM_HOSTID
) ^
426 /* Date format/separator */
427 if (set
& SET_DATIME
) {
428 writeNVRAM (fd
, NVRAM_DATIME
, bootpref
[ARRAY_DATIME
]);
430 printf ("Date format set to ");
431 showDateFmt (readNVRAM (fd
, NVRAM_DATIME
));
435 if (set
& SET_DATESEP
) {
436 writeNVRAM (fd
, NVRAM_DATESEP
, bootpref
[ARRAY_DATESEP
]);
438 printf ("Date separator set to ");
439 showDateSep (readNVRAM (fd
, NVRAM_DATESEP
));
443 if ((set
& SET_VID2
) || (set
& SET_VID1
)) {
444 if (set
& SET_VID2
) {
445 writeNVRAM (fd
, NVRAM_VID2
, bootpref
[ARRAY_VID2
]);
447 if (set
& SET_VID1
) {
448 writeNVRAM (fd
, NVRAM_VID1
, bootpref
[ARRAY_VID1
]);
451 printf ("Video set to (0x%02x, 0x%02x) :\n",
452 readNVRAM (fd
, NVRAM_VID2
),
453 readNVRAM (fd
, NVRAM_VID1
));
454 showVideo2 (readNVRAM (fd
, NVRAM_VID2
));
455 showVideo1 (readNVRAM (fd
, NVRAM_VID1
),
456 readNVRAM (fd
, NVRAM_VID2
));
462 showOS (u_char bootos
)
465 case BOOTPREF_NETBSD
:
487 printf (" (0x%x).\n", bootos
);
491 showLang (u_char lang
)
514 printf (" (0x%x).\n", lang
);
518 showKbdLang (u_char lang
)
540 printf ("Swiss (French)");
543 printf ("Swiss (German)");
549 printf (" (0x%x).\n", lang
);
553 showDateFmt (u_char fmt
)
555 if (fmt
& DATIME_24H
) {
556 printf ("24 hour clock, ");
558 printf ("12 hour clock, ");
560 switch (fmt
& ~DATIME_24H
) {
577 printf (" (0x%02x)\n", fmt
);
581 showDateSep (u_char sep
)
585 printf ("\"%c\" ", sep
);
590 printf ("(0x%02x)\n", sep
);
594 showVideo2 (u_char vid2
)
598 colours
= vid2
& 0x07;
617 printf (" colours, ");
618 if (vid2
& VID2_80CLM
) {
623 printf (" column, ");
624 if (vid2
& VID2_VGA
) {
630 if (vid2
& VID2_PAL
) {
635 printf ("\tOverscan ");
636 if (vid2
& VID2_OVERSCAN
) {
641 printf ("\tST compatibility ");
642 if (vid2
& VID2_COMPAT
) {
650 showVideo1 (u_char vid1
, u_char vid2
)
652 if (vid2
& VID2_VGA
) {
653 printf ("\tDouble line ");
654 if (vid1
& VID1_INTERLACE
) {
660 printf ("\tInterlace ");
661 if (vid1
& VID1_INTERLACE
) {
671 checkOS (u_char
*val
, char *str
)
673 if (!strncasecmp (str
, "ne", 2)) {
674 *val
= BOOTPREF_NETBSD
;
677 if (!strncasecmp (str
, "t", 1)) {
681 if (!strncasecmp (str
, "m", 1)) {
682 *val
= BOOTPREF_MAGIC
;
685 if (!strncasecmp (str
, "l", 1)) {
686 *val
= BOOTPREF_LINUX
;
689 if (!strncasecmp (str
, "s", 1)) {
690 *val
= BOOTPREF_SYSV
;
693 if (!strncasecmp (str
, "no", 2)) {
694 *val
= BOOTPREF_NONE
;
701 checkLang (u_char
*val
, char *str
)
703 if (!strncasecmp (str
, "e", 1)) {
707 if (!strncasecmp (str
, "g", 1)) {
711 if (!strncasecmp (str
, "f", 1)) {
715 if (!strncasecmp (str
, "s", 1)) {
719 if (!strncasecmp (str
, "i", 1)) {
727 checkKbdLang (u_char
*val
, char *str
)
729 if (!strncasecmp (str
, "a", 1)) {
733 if (!strncasecmp (str
, "g", 1)) {
737 if (!strncasecmp (str
, "f", 1)) {
741 if (!strncasecmp (str
, "b", 1)) {
745 if (!strncasecmp (str
, "sp", 2)) {
749 if (!strncasecmp (str
, "i", 1)) {
753 if (!strncasecmp (str
, "swiss f", 7) || !strncasecmp (str
, "sw f", 4)) {
757 if (!strncasecmp (str
, "swiss g", 7) || !strncasecmp (str
, "sw g", 4)) {
765 checkInt (u_char
*val
, char *str
, int min
, int max
)
768 if (1 == sscanf (str
, "%d", &num
) && num
>= min
&& num
<= max
) {
776 checkDateFmt (u_char
*val
, char *str
)
778 if (!strncasecmp (str
, "m", 1)) {
779 *val
|= DATIME_MMDDYY
;
782 if (!strncasecmp (str
, "d", 1)) {
783 *val
|= DATIME_DDMMYY
;
786 if (!strncasecmp (str
, "yym", 3)) {
787 *val
|= DATIME_YYMMDD
;
790 if (!strncasecmp (str
, "yyd", 3)) {
791 *val
|= DATIME_YYDDMM
;
798 checkDateSep (u_char
*val
, char *str
)
808 checkColours (u_char
*val
, char *str
)
811 if (!strncasecmp (str
, "6", 1)) {
812 *val
|= VID2_65535COL
;
815 if (!strncasecmp (str
, "25", 2)) {
819 if (!strncasecmp (str
, "1", 1)) {
823 if (!strncasecmp (str
, "4", 1)) {
827 if (!strncasecmp (str
, "2", 1)) {