Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | /* |
2 | * ========== Copyright Header Begin ========================================== | |
3 | * | |
4 | * Hypervisor Software File: common.c | |
5 | * | |
6 | * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved. | |
7 | * | |
8 | * - Do no alter or remove copyright notices | |
9 | * | |
10 | * - Redistribution and use of this software in source and binary forms, with | |
11 | * or without modification, are permitted provided that the following | |
12 | * conditions are met: | |
13 | * | |
14 | * - Redistribution of source code must retain the above copyright notice, | |
15 | * this list of conditions and the following disclaimer. | |
16 | * | |
17 | * - Redistribution in binary form must reproduce the above copyright notice, | |
18 | * this list of conditions and the following disclaimer in the | |
19 | * documentation and/or other materials provided with the distribution. | |
20 | * | |
21 | * Neither the name of Sun Microsystems, Inc. or the names of contributors | |
22 | * may be used to endorse or promote products derived from this software | |
23 | * without specific prior written permission. | |
24 | * | |
25 | * This software is provided "AS IS," without a warranty of any kind. | |
26 | * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, | |
27 | * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A | |
28 | * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN | |
29 | * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR | |
30 | * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR | |
31 | * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN | |
32 | * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR | |
33 | * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE | |
34 | * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, | |
35 | * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF | |
36 | * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. | |
37 | * | |
38 | * You acknowledge that this software is not designed, licensed or | |
39 | * intended for use in the design, construction, operation or maintenance of | |
40 | * any nuclear facility. | |
41 | * | |
42 | * ========== Copyright Header End ============================================ | |
43 | */ | |
44 | /* | |
45 | * id: @(#)common.c 1.2 02/01/24 | |
46 | * purpose: | |
47 | * copyright: Copyright 2000-2002 Sun Microsystems, Inc. All Rights Reserved | |
48 | * copyright: Use is subject to license terms. | |
49 | */ | |
50 | ||
51 | #include <stdio.h> | |
52 | #include <stdlib.h> | |
53 | #include <strings.h> | |
54 | #include "gen-seeprom.h" | |
55 | #include "prototypes.h" | |
56 | ||
57 | union data_format { | |
58 | unsigned char str[8]; | |
59 | unsigned long long value; | |
60 | }; | |
61 | ||
62 | /* | |
63 | * scan decimal or hex value in from a line of input | |
64 | */ | |
65 | ||
66 | ||
67 | void | |
68 | strip_val(char *input_str, unsigned int *input_val) | |
69 | { | |
70 | if ((strncmp(input_str, "0x", 2) == 0) || | |
71 | (strncmp(input_str, "0X", 2) == 0)) { | |
72 | sscanf(&input_str[2], "%x", input_val); | |
73 | } else { | |
74 | sscanf(&input_str[0], "%d", input_val); | |
75 | } | |
76 | } | |
77 | ||
78 | int | |
79 | scan_line(char *line, unsigned int *input_val) | |
80 | { | |
81 | char temp[MAXNAMESIZE]; | |
82 | char input_str[MAXNAMESIZE]; | |
83 | ||
84 | if (sscanf(line, "%32s %32s", temp, input_str) != 2) { | |
85 | return (ERROR); | |
86 | } | |
87 | strip_val(input_str, input_val); | |
88 | return (NO_ERROR); | |
89 | } | |
90 | ||
91 | ||
92 | /* | |
93 | * convert an integer value into a byte string and assign it to data_ptr | |
94 | * at the given offset | |
95 | */ | |
96 | ||
97 | void | |
98 | write_bytes(unsigned int input_val, int size, int offset, | |
99 | unsigned char *data_ptr) | |
100 | { | |
101 | unsigned char byte; | |
102 | ||
103 | while (size) { | |
104 | byte = input_val >> ((size-1)*8); | |
105 | byte &= 0xff; | |
106 | data_ptr[offset++] = byte; | |
107 | size--; | |
108 | } | |
109 | } | |
110 | ||
111 | void | |
112 | store_chars(int len, unsigned char *string, unsigned char **ptr) | |
113 | { | |
114 | union data_format data; | |
115 | ||
116 | while (len > 0) { | |
117 | if (len > 8) { | |
118 | memcpy(data.str, string, 8); | |
119 | store_bytes(8, data.value, ptr); | |
120 | len -= 8; | |
121 | string += 8; | |
122 | } else { | |
123 | memcpy(data.str, string, len); | |
124 | data.value >>= (8-len)*8; | |
125 | store_bytes(len, data.value, ptr); | |
126 | len = 0; | |
127 | } | |
128 | } | |
129 | } | |
130 | ||
131 | /* Internet checksum */ | |
132 | unsigned short | |
133 | checksum(unsigned short *addr, int count) | |
134 | { | |
135 | int sum = 0; | |
136 | while (count > 1) { | |
137 | sum += *addr++; | |
138 | count -= 2; | |
139 | } | |
140 | ||
141 | if (count > 0) | |
142 | sum += *(unsigned char *) addr; | |
143 | ||
144 | while (sum >> 16) { | |
145 | sum = (sum & 0xffff) + (sum >> 16); | |
146 | } | |
147 | ||
148 | sum = (~sum) & 0xffff; | |
149 | if (sum == 0) | |
150 | sum = 0xffff; | |
151 | return (sum); | |
152 | } |