Initial commit.
[b43-tools.git] / ssb_sprom / utils.c
1 /*
2
3   Broadcom Sonics Silicon Backplane bus SPROM data modification tool
4
5   Copyright (c) 2006-2007 Michael Buesch <mb@bu3sch.de>
6
7   This program is free software; you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation; either version 2 of the License, or
10   (at your option) any later version.
11
12   This program is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16
17   You should have received a copy of the GNU General Public License
18   along with this program; see the file COPYING.  If not, write to
19   the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
20   Boston, MA 02110-1301, USA.
21
22 */
23
24 #include "utils.h"
25 #include "ssb_sprom.h"
26
27 #include <string.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <stdarg.h>
31
32
33 int prinfo(const char *fmt, ...)
34 {
35         int ret;
36         va_list va;
37
38         if (!cmdargs.verbose)
39                 return 0;
40
41         va_start(va, fmt);
42         ret = vfprintf(stderr, fmt, va);
43         va_end(va);
44
45         return ret;
46 }
47
48 int prerror(const char *fmt, ...)
49 {
50         int ret;
51         va_list va;
52
53         va_start(va, fmt);
54         ret = vfprintf(stderr, fmt, va);
55         va_end(va);
56
57         return ret;
58 }
59
60 int prdata(const char *fmt, ...)
61 {
62         int ret;
63         va_list va;
64
65         va_start(va, fmt);
66         ret = vfprintf(stderr, fmt, va);
67         va_end(va);
68
69         return ret;
70 }
71
72 void internal_error(const char *message)
73 {
74         prerror("Internal programming error: %s\n", message);
75         exit(1);
76 }
77
78 static void oom(void)
79 {
80         prerror("ERROR: Out of memory!\n"
81                 "Virtual memory exhausted. "
82                 "Please close some applications, "
83                 "add more RAM or SWAP space.\n");
84         exit(1);
85 }
86
87 void * malloce(size_t size)
88 {
89         void *ret;
90
91         ret = malloc(size);
92         if (!ret)
93                 oom();
94
95         return ret;
96 }
97
98 void * realloce(void *ptr, size_t newsize)
99 {
100         void *ret;
101
102         ret = realloc(ptr, newsize);
103         if (!ret)
104                 oom();
105
106         return ret;
107 }
108
109 uint8_t crc8(uint8_t crc, uint8_t data)
110 {
111         static const uint8_t t[] = {
112                 0x00, 0xF7, 0xB9, 0x4E, 0x25, 0xD2, 0x9C, 0x6B,
113                 0x4A, 0xBD, 0xF3, 0x04, 0x6F, 0x98, 0xD6, 0x21,
114                 0x94, 0x63, 0x2D, 0xDA, 0xB1, 0x46, 0x08, 0xFF,
115                 0xDE, 0x29, 0x67, 0x90, 0xFB, 0x0C, 0x42, 0xB5,
116                 0x7F, 0x88, 0xC6, 0x31, 0x5A, 0xAD, 0xE3, 0x14,
117                 0x35, 0xC2, 0x8C, 0x7B, 0x10, 0xE7, 0xA9, 0x5E,
118                 0xEB, 0x1C, 0x52, 0xA5, 0xCE, 0x39, 0x77, 0x80,
119                 0xA1, 0x56, 0x18, 0xEF, 0x84, 0x73, 0x3D, 0xCA,
120                 0xFE, 0x09, 0x47, 0xB0, 0xDB, 0x2C, 0x62, 0x95,
121                 0xB4, 0x43, 0x0D, 0xFA, 0x91, 0x66, 0x28, 0xDF,
122                 0x6A, 0x9D, 0xD3, 0x24, 0x4F, 0xB8, 0xF6, 0x01,
123                 0x20, 0xD7, 0x99, 0x6E, 0x05, 0xF2, 0xBC, 0x4B,
124                 0x81, 0x76, 0x38, 0xCF, 0xA4, 0x53, 0x1D, 0xEA,
125                 0xCB, 0x3C, 0x72, 0x85, 0xEE, 0x19, 0x57, 0xA0,
126                 0x15, 0xE2, 0xAC, 0x5B, 0x30, 0xC7, 0x89, 0x7E,
127                 0x5F, 0xA8, 0xE6, 0x11, 0x7A, 0x8D, 0xC3, 0x34,
128                 0xAB, 0x5C, 0x12, 0xE5, 0x8E, 0x79, 0x37, 0xC0,
129                 0xE1, 0x16, 0x58, 0xAF, 0xC4, 0x33, 0x7D, 0x8A,
130                 0x3F, 0xC8, 0x86, 0x71, 0x1A, 0xED, 0xA3, 0x54,
131                 0x75, 0x82, 0xCC, 0x3B, 0x50, 0xA7, 0xE9, 0x1E,
132                 0xD4, 0x23, 0x6D, 0x9A, 0xF1, 0x06, 0x48, 0xBF,
133                 0x9E, 0x69, 0x27, 0xD0, 0xBB, 0x4C, 0x02, 0xF5,
134                 0x40, 0xB7, 0xF9, 0x0E, 0x65, 0x92, 0xDC, 0x2B,
135                 0x0A, 0xFD, 0xB3, 0x44, 0x2F, 0xD8, 0x96, 0x61,
136                 0x55, 0xA2, 0xEC, 0x1B, 0x70, 0x87, 0xC9, 0x3E,
137                 0x1F, 0xE8, 0xA6, 0x51, 0x3A, 0xCD, 0x83, 0x74,
138                 0xC1, 0x36, 0x78, 0x8F, 0xE4, 0x13, 0x5D, 0xAA,
139                 0x8B, 0x7C, 0x32, 0xC5, 0xAE, 0x59, 0x17, 0xE0,
140                 0x2A, 0xDD, 0x93, 0x64, 0x0F, 0xF8, 0xB6, 0x41,
141                 0x60, 0x97, 0xD9, 0x2E, 0x45, 0xB2, 0xFC, 0x0B,
142                 0xBE, 0x49, 0x07, 0xF0, 0x9B, 0x6C, 0x22, 0xD5,
143                 0xF4, 0x03, 0x4D, 0xBA, 0xD1, 0x26, 0x68, 0x9F,
144         };
145         return t[crc ^ data];
146 }