4 * Copyright (c) 2001 Maksim Yevmenkin <m_evmenkin@yahoo.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * $Id: bt3cfw.c,v 1.2 2003/05/21 22:40:29 max Exp $
32 #include <sys/types.h>
35 #include <netgraph/bluetooth/include/ng_bt3c.h>
43 #define BT3CFW_IDENT "bt3cfw"
44 #define BT3CFW_MAX_FIRMWARE_SIZE 0xffff
46 /* Convert hex ASCII to int4 */
48 hexa2int4(const char *a)
50 if ('0' <= *a && *a <= '9')
53 if ('A' <= *a && *a <= 'F')
54 return (*a - 'A' + 0xa);
56 if ('a' <= *a && *a <= 'f')
57 return (*a - 'a' + 0xa);
59 syslog(LOG_ERR, "Invalid hex character: '%c' (%#x)", *a, *a);
63 /* Convert hex ASCII to int8 */
65 hexa2int8(const char *a)
67 return ((hexa2int4(a) << 4) | hexa2int4(a + 1));
70 /* Convert hex ASCII to int16 */
72 hexa2int16(const char *a)
74 return ((hexa2int8(a) << 8) | hexa2int8(a + 2));
77 /* Convert hex ASCII to int32 */
79 hexa2int32(const char *a)
81 return ((hexa2int16(a) << 16) | hexa2int16(a + 4));
84 /* Display usage() and exit */
88 syslog(LOG_ERR, "Usage: %s -f FirmwareFile -n NodeName", BT3CFW_IDENT);
94 main(int argc, char *argv[])
96 FILE *firmware_file = NULL;
97 char buffer[80], path[NG_PATHSIZ],
98 *firmware_filename = NULL;
99 uint8_t *firmware = NULL;
100 int firmware_size, opt, cs, ds;
102 memset(path, 0, sizeof(path));
103 openlog(BT3CFW_IDENT, LOG_NDELAY|LOG_PID|LOG_PERROR, LOG_USER);
105 while ((opt = getopt(argc, argv, "f:hn:")) != -1) {
108 firmware_filename = optarg;
112 snprintf(path, sizeof(path), "%s:", optarg);
122 if (firmware_filename == NULL || path[0] == 0)
126 firmware = (uint8_t *) calloc(BT3CFW_MAX_FIRMWARE_SIZE,
128 if (firmware == NULL) {
129 syslog(LOG_ERR, "Could not allocate firmware buffer");
133 if ((firmware_file = fopen(firmware_filename, "r")) == NULL) {
134 syslog(LOG_ERR, "Could not open BT3C firmware file %s. %s (%d)",
135 firmware_filename, strerror(errno), errno);
141 while (fgets(buffer, sizeof(buffer), firmware_file)) {
142 int i, size, address, cs, fcs;
144 size = hexa2int8(buffer + 2);
145 address = hexa2int32(buffer + 4);
146 fcs = hexa2int8(buffer + 2 + size * 2);
148 if (buffer[1] == '3') {
149 ng_bt3c_firmware_block_ep *block = NULL;
150 uint16_t *data = NULL;
152 block = (ng_bt3c_firmware_block_ep *)
153 (firmware + firmware_size);
155 firmware_size += sizeof(*block);
156 if (firmware_size >= BT3CFW_MAX_FIRMWARE_SIZE) {
157 syslog(LOG_ERR, "Could not add new firmware " \
158 "block. Firmware file %s is " \
159 "too big, firmware_size=%d",
165 block->block_address = address;
166 block->block_size = (size - 4) / 2;
167 block->block_alignment = (block->block_size * 2) % 3;
168 if (block->block_alignment != 0)
169 block->block_alignment = 3 - block->block_alignment;
171 firmware_size += (block->block_size * 2);
172 firmware_size += block->block_alignment;
173 if (firmware_size >= BT3CFW_MAX_FIRMWARE_SIZE) {
174 syslog(LOG_ERR, "Could not add new firmware " \
175 "data. Firmware file %s is " \
176 "too big, firmware_size=%d",
182 /* First part of the cheksum: size and address */
184 for (i = 0; i < 5; i++)
185 cs += hexa2int8(buffer + 2 + i * 2);
187 /* Data + second part of the cheksum: data */
188 data = (uint16_t *)(block + 1);
189 for (i = 0; i < block->block_size; i++) {
190 data[i] = hexa2int16(buffer + (i * 4) + 12);
191 cs += (((data[i] & 0xff00) >> 8) & 0xff);
192 cs += (data[i] & 0x00ff);
195 for (cs = 0, i = 0; i < size; i++)
196 cs += hexa2int8(buffer + 2 + i * 2);
198 if (((cs + fcs) & 0xff) != 0xff) {
199 syslog(LOG_ERR, "Invalid firmware file %s. Checksum " \
200 "error, cs=%#x, fcs=%#x, checksum=%#x",
201 firmware_filename, (cs & 0xff), fcs,
202 ((cs + fcs) & 0xff));
207 /* Send firmware to the card */
208 if (NgMkSockNode(NULL, &cs, &ds) < 0) {
209 syslog(LOG_ERR, "Could not create Netgraph socket. %s (%d)",
210 strerror(errno), errno);
214 if (NgSendMsg(cs, path, NGM_BT3C_COOKIE,
215 NGM_BT3C_NODE_DOWNLOAD_FIRMWARE,
216 (void const *) firmware, firmware_size) < 0) {
217 syslog(LOG_ERR, "Could not send Netgraph message. %s (%d)",
218 strerror(errno), errno);
224 fclose(firmware_file);