1 /* $NetBSD: methods.c,v 1.5 2002/06/08 17:24:09 yamt Exp $ */
4 * Copyright (c) 1999 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
35 #include <sys/types.h>
37 #include "memswitch.h"
48 while (*p1
== ' ' || *p1
== '\t')
55 if (strlen (p1
) >= 2 && strncasecmp ("0x", p1
, 2) == 0) {
58 if (*p1
>= '0' && *p1
<= '9') {
62 } else if (*p1
>= 'A' && *p1
<= 'F') {
66 } else if (*p1
>= 'a' && *p1
<= 'f') {
77 if (*p1
>= '0' && *p1
<= '9') {
93 while (*p1
== ' ' || *p1
== '\t') p1
++;
100 struct property
*prop
;
102 if (current_values
== 0)
103 alloc_current_values ();
105 prop
->current_value
.byte
[0] = current_values
[prop
->offset
];
106 prop
->current_value
.byte
[1] = 0;
107 prop
->current_value
.byte
[2] = 0;
108 prop
->current_value
.byte
[3] = 0;
109 prop
->value_valid
= 1;
116 struct property
*prop
;
118 if (current_values
== 0)
119 alloc_current_values ();
121 prop
->current_value
.byte
[0] = current_values
[prop
->offset
];
122 prop
->current_value
.byte
[1] = current_values
[prop
->offset
+1];
123 prop
->current_value
.byte
[2] = 0;
124 prop
->current_value
.byte
[3] = 0;
125 prop
->value_valid
= 1;
132 struct property
*prop
;
134 if (current_values
== 0)
135 alloc_current_values ();
137 prop
->current_value
.byte
[0] = current_values
[prop
->offset
];
138 prop
->current_value
.byte
[1] = current_values
[prop
->offset
+1];
139 prop
->current_value
.byte
[2] = current_values
[prop
->offset
+2];
140 prop
->current_value
.byte
[3] = current_values
[prop
->offset
+3];
141 prop
->value_valid
= 1;
148 struct property
*prop
;
153 if (modified_values
== 0)
154 alloc_modified_values ();
156 modified_values
[prop
->offset
] = prop
->modified_value
.byte
[0];
163 struct property
*prop
;
168 if (modified_values
== 0)
169 alloc_modified_values ();
171 modified_values
[prop
->offset
] = prop
->modified_value
.byte
[0];
172 modified_values
[prop
->offset
+1] = prop
->modified_value
.byte
[1];
179 struct property
*prop
;
184 if (modified_values
== 0)
185 alloc_modified_values ();
187 modified_values
[prop
->offset
] = prop
->modified_value
.byte
[0];
188 modified_values
[prop
->offset
+1] = prop
->modified_value
.byte
[1];
189 modified_values
[prop
->offset
+2] = prop
->modified_value
.byte
[2];
190 modified_values
[prop
->offset
+3] = prop
->modified_value
.byte
[3];
197 struct property
*prop
;
203 parse_dummy (prop
, value
)
204 struct property
*prop
;
207 warnx ("Cannot modify %s.%s", prop
->class, prop
->node
);
213 parse_byte (prop
, value
)
214 struct property
*prop
;
217 const char *p
= value
;
222 warnx ("%s: Invalid value", value
);
226 if (strcasecmp ("MB", p
) == 0)
228 else if (strcasecmp ("KB", p
) == 0)
231 strcasecmp ("B", p
) != 0) {
232 warnx ("%s: Invalid value", value
);
237 warnx ("%s: Too small", value
);
239 } else if (v
> prop
->max
) {
240 warnx ("%s: Too large", value
);
245 prop
->modified_value
.longword
= v
;
251 parse_uchar (prop
, value
)
252 struct property
*prop
;
255 const char *p
= value
;
260 warnx ("%s: Invalid value", value
);
265 warnx ("%s: Too small", value
);
267 } else if (v
> prop
->max
) {
268 warnx ("%s: Too large", value
);
273 prop
->modified_value
.byte
[0] = v
;
279 parse_ulong (prop
, value
)
280 struct property
*prop
;
283 const char *p
= value
;
288 warnx ("%s: Invalid value", value
);
293 warnx ("%s: Too small", value
);
295 } else if (v
> prop
->max
) {
296 warnx ("%s: Too large", value
);
301 prop
->modified_value
.longword
= v
;
307 parse_ushort (prop
, value
)
308 struct property
*prop
;
311 const char *p
= value
;
316 warnx ("%s: Invalid value", value
);
321 warnx ("%s: Too small", value
);
323 } else if (v
> prop
->max
) {
324 warnx ("%s: Too large", value
);
329 prop
->modified_value
.word
[0] = v
;
335 parse_time (prop
, value
)
336 struct property
*prop
;
339 const char *p
= value
;
342 while (*p
== ' ' || *p
== '\t') p
++;
349 warnx ("%s: Invalid value", value
);
353 if (strcasecmp ("hours", p
) == 0 || strcasecmp ("hour", p
) == 0)
355 else if (strcasecmp ("minutes", p
) == 0 ||
356 strcasecmp ("minute", p
) == 0)
359 strcasecmp ("second", p
) != 0 &&
360 strcasecmp ("seconds", p
) != 0) {
361 warnx ("%s: Invalid value", value
);
366 warnx ("%s: Too small", value
);
368 } else if (v
> prop
->max
) {
369 warnx ("%s: Too large", value
);
374 prop
->modified_value
.longword
= v
;
380 parse_bootdev (prop
, value
)
381 struct property
*prop
;
384 const char *p
= value
;
388 while (*p
== ' ' || *p
== '\t') p
++;
390 if (strcasecmp ("STD", p
) == 0)
392 else if (strcasecmp ("ROM", p
) == 0)
394 else if (strcasecmp ("RAM", p
) == 0)
396 else if (strncasecmp ("HD", p
, 2) == 0) {
399 if (p
== 0 || v
< 0 || v
> 15) {
400 warnx ("%s: Invalid value", value
);
405 } else if (strncasecmp ("FD", p
, 2) == 0) {
408 if (p
== 0 || v
< 0 || v
> 3) {
409 warnx ("%s: Invalid value", value
);
414 } else if (strncasecmp ("INSCSI", p
, 6) == 0 ||
415 strncasecmp ("EXSCSI", p
, 6) == 0) {
416 int isin
= strncasecmp ("EXSCSI", p
, 6);
420 if (p
== 0 || v
< 0 || v
> 7) {
421 warnx ("%s: Invalid value", value
);
425 /* change boot.romaddr */
426 sprintf(expr_scsi
, "boot.romaddr=0x%06x",
427 (isin
? 0xfc0000 : 0xea0020) + v
* 4);
428 modify_single(expr_scsi
);
430 /* boot.device again */
433 warnx ("%s: Invalid value", value
);
438 prop
->modified_value
.word
[0] = v
;
444 parse_serial (prop
, value
)
445 struct property
*prop
;
447 #define NEXTSPEC while (*p == ' ' || *p == '\t') p++; \
449 warnx ("%s: Invalid value", value); \
452 while (*p == ' ' || *p == '\t') p++;
454 const char *p
= value
;
456 int baud
, bit
, parity
, stop
, flow
;
457 static const int bauds
[] = {75, 150, 300, 600, 1200, 2400, 4800, 9600,
459 static const char parities
[] = "noe";
462 while (*p
== ' ' || *p
== '\t') p
++;
467 warnx ("%s: Invalid value", value
);
470 for (i
= 0; bauds
[i
]; i
++)
471 if (baud
== bauds
[i
])
474 warnx ("%d: Invalid speed", baud
);
482 if (*p
< '5' || *p
> '8') {
483 warnx ("%c: Invalid bit size", *p
);
491 q
= strchr(parities
, *p
++);
493 warnx ("%c: Invalid parity spec", *p
);
496 parity
= q
- parities
;
501 if (strncmp (p
, "1.5", 3) == 0) {
504 } else if (strncmp (p
, "2", 1) == 0) {
507 } else if (strncmp (p
, "1", 1) == 0) {
511 warnx ("%s: Invalid value", value
);
523 warnx ("%s: Invalid value", value
);
528 while (*p
== ' ' || *p
== '\t') p
++;
530 warnx ("%s: Invalid value", value
);
535 prop
->modified_value
.word
[0] = ((stop
<< 14) +
546 parse_srammode (prop
, value
)
547 struct property
*prop
;
550 static const char *const sramstrs
[] = {"unused", "SRAMDISK", "program"};
553 for (i
= 0; i
<= 2; i
++) {
554 if (strcasecmp (value
, sramstrs
[i
]) == 0)
558 warnx ("%s: Invalid value", value
);
563 prop
->modified_value
.byte
[0] = i
;
569 print_uchar (prop
, str
)
570 struct property
*prop
;
574 snprintf (str
, MAXVALUELEN
,
575 "%d", prop
->modified_value
.byte
[0]);
577 if (!prop
->value_valid
)
579 snprintf (str
, MAXVALUELEN
, "%d",
580 prop
->current_value
.byte
[0]);
587 print_ucharh (prop
, str
)
588 struct property
*prop
;
592 snprintf (str
, MAXVALUELEN
,
593 "0x%4.4x", prop
->modified_value
.byte
[0]);
595 if (!prop
->value_valid
)
597 snprintf (str
, MAXVALUELEN
,
598 "0x%4.4x", prop
->current_value
.byte
[0]);
605 print_ushorth (prop
, str
)
606 struct property
*prop
;
610 snprintf (str
, MAXVALUELEN
,
611 "0x%4.4x", prop
->modified_value
.word
[0]);
613 if (!prop
->value_valid
)
615 snprintf (str
, MAXVALUELEN
,
616 "0x%4.4x", prop
->current_value
.word
[0]);
623 print_ulong (prop
, str
)
624 struct property
*prop
;
628 snprintf (str
, MAXVALUELEN
,
629 "%ld", prop
->modified_value
.longword
);
631 if (!prop
->value_valid
)
633 snprintf (str
, MAXVALUELEN
,
634 "%ld", prop
->current_value
.longword
);
641 print_ulongh (prop
, str
)
642 struct property
*prop
;
646 snprintf (str
, MAXVALUELEN
,
647 "0x%8.8lx", prop
->modified_value
.longword
);
649 if (!prop
->value_valid
)
651 snprintf (str
, MAXVALUELEN
,
652 "0x%8.8lx", prop
->current_value
.longword
);
659 print_magic (prop
, str
)
660 struct property
*prop
;
663 if (!prop
->value_valid
)
665 snprintf (str
, MAXVALUELEN
, "%c%c%c%c",
666 prop
->current_value
.byte
[0],
667 prop
->current_value
.byte
[1],
668 prop
->current_value
.byte
[2],
669 prop
->current_value
.byte
[3]);
675 print_timesec (prop
, str
)
676 struct property
*prop
;
680 snprintf (str
, MAXVALUELEN
,
681 "%ld second", prop
->modified_value
.longword
);
683 if (!prop
->value_valid
)
685 snprintf (str
, MAXVALUELEN
,
686 "%ld second", prop
->current_value
.longword
);
693 print_bootdev (prop
, str
)
694 struct property
*prop
;
700 v
= prop
->modified_value
.word
[0];
702 if (!prop
->value_valid
)
704 v
= prop
->current_value
.word
[0];
709 else if (v
== 0xa000)
711 else if (v
== 0xb000)
713 else if (v
>= 0x8000 && v
< 0x9000)
714 snprintf (str
, MAXVALUELEN
, "HD%d", (v
& 0x0f00) >> 8);
715 else if (v
>= 0x9000 && v
< 0xa000)
716 snprintf (str
, MAXVALUELEN
, "FD%d", (v
& 0x0f00) >> 8);
718 snprintf (str
, MAXVALUELEN
, "%8.8x", v
);
724 print_serial (prop
, str
)
725 struct property
*prop
;
729 const char *baud
, *stop
;
730 char bit
, parity
, flow
;
731 static const char *const bauds
[] = {"75", "150", "300", "600", "1200",
732 "2400", "4800", "9600", "17361"};
733 static const char bits
[] = "5678";
734 static const char parities
[] = "noen";
735 static const char *const stops
[] = {"2", "1", "1.5", "2"};
736 static const char flows
[] = "-s";
739 v
= prop
->modified_value
.word
[0];
741 if (!prop
->value_valid
)
743 v
= prop
->current_value
.word
[0];
746 baud
= bauds
[v
& 0x000f];
747 bit
= bits
[(v
& 0x0c00) >> 10];
748 parity
= parities
[(v
& 0x3000) >> 12];
749 stop
= stops
[(v
& 0xe000) >> 14];
750 flow
= flows
[(v
& 0x0200) >> 9];
751 sprintf (str
, "%s,%c,%c,%s,%c", baud
, bit
, parity
, stop
, flow
);
757 print_srammode (prop
, str
)
758 struct property
*prop
;
762 static const char *const sramstrs
[] = {"unused", "SRAMDISK", "program"};
765 v
= prop
->modified_value
.byte
[0];
767 if (!prop
->value_valid
)
769 v
= prop
->current_value
.byte
[0];
773 strcpy (str
, "INVALID");
775 strcpy (str
, sramstrs
[v
]);