1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
|
// SPDX-License-Identifier: GPL-2.0-or-later
/*
*
* BlueZ - Bluetooth protocol stack for Linux
*
* Copyright (C) 2005-2010 Marcel Holtmann <marcel@holtmann.org>
* Copyright (c) 2010, Code Aurora Forum. All rights reserved.
*
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#define _GNU_SOURCE
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <syslog.h>
#include <termios.h>
#include <time.h>
#include <poll.h>
#include <sys/time.h>
#include <sys/param.h>
#include <sys/ioctl.h>
#include <sys/uio.h>
#include "lib/bluetooth.h"
#include "lib/hci.h"
#include "lib/hci_lib.h"
#include "hciattach.h"
#define FAILIF(x, args...) do { \
if (x) { \
fprintf(stderr, ##args); \
return -1; \
} \
} while (0)
typedef struct {
uint8_t uart_prefix;
hci_event_hdr hci_hdr;
evt_cmd_complete cmd_complete;
uint8_t status;
uint8_t data[16];
} __attribute__((packed)) command_complete_t;
static int read_command_complete(int fd,
unsigned short opcode,
unsigned char len)
{
command_complete_t resp;
unsigned char vsevent[512];
int n;
/* Read reply. */
n = read_hci_event(fd, vsevent, sizeof(vsevent));
FAILIF(n < 0, "Failed to read response");
FAILIF(vsevent[1] != 0xFF, "Failed to read response");
n = read_hci_event(fd, (unsigned char *)&resp, sizeof(resp));
FAILIF(n < 0, "Failed to read response");
/* event must be event-complete */
FAILIF(resp.hci_hdr.evt != EVT_CMD_COMPLETE,
"Error in response: not a cmd-complete event, "
"but 0x%02x!\n", resp.hci_hdr.evt);
FAILIF(resp.hci_hdr.plen < 4, /* plen >= 4 for EVT_CMD_COMPLETE */
"Error in response: plen is not >= 4, but 0x%02x!\n",
resp.hci_hdr.plen);
/* cmd-complete event: opcode */
FAILIF(resp.cmd_complete.opcode != 0,
"Error in response: opcode is 0x%04x, not 0!",
resp.cmd_complete.opcode);
return resp.status == 0 ? 0 : -1;
}
static int qualcomm_load_firmware(int fd, const char *firmware, const char *bdaddr_s)
{
int fw = open(firmware, O_RDONLY);
fprintf(stdout, "Opening firmware file: %s\n", firmware);
FAILIF(fw < 0,
"Could not open firmware file %s: %s (%d).\n",
firmware, strerror(errno), errno);
fprintf(stdout, "Uploading firmware...\n");
do {
/* Read each command and wait for a response. */
unsigned char data[1024];
unsigned char cmdp[1 + sizeof(hci_command_hdr)];
hci_command_hdr *cmd = (hci_command_hdr *) (cmdp + 1);
int nr;
nr = read(fw, cmdp, sizeof(cmdp));
if (!nr)
break;
FAILIF(nr != sizeof(cmdp),
"Could not read H4 + HCI header!\n");
FAILIF(*cmdp != HCI_COMMAND_PKT,
"Command is not an H4 command packet!\n");
FAILIF(read(fw, data, cmd->plen) != cmd->plen,
"Could not read %d bytes of data \
for command with opcode %04x!\n",
cmd->plen, cmd->opcode);
if ((data[0] == 1) && (data[1] == 2) && (data[2] == 6)) {
bdaddr_t bdaddr;
if (bdaddr_s != NULL) {
str2ba(bdaddr_s, &bdaddr);
memcpy(&data[3], &bdaddr, sizeof(bdaddr_t));
}
}
{
int nw;
struct iovec iov_cmd[2];
iov_cmd[0].iov_base = cmdp;
iov_cmd[0].iov_len = sizeof(cmdp);
iov_cmd[1].iov_base = data;
iov_cmd[1].iov_len = cmd->plen;
nw = writev(fd, iov_cmd, 2);
FAILIF(nw != (int) sizeof(cmdp) + cmd->plen,
"Could not send entire command \
(sent only %d bytes)!\n",
nw);
}
/* Wait for response */
if (read_command_complete(fd, cmd->opcode, cmd->plen) < 0)
return -1;
} while (1);
fprintf(stdout, "Firmware upload successful.\n");
close(fw);
return 0;
}
int qualcomm_init(int fd, int speed, struct termios *ti, const char *bdaddr)
{
struct timespec tm = {0, 50000};
char cmd[5];
unsigned char resp[100]; /* Response */
char fw[100];
int n;
memset(resp, 0, 100);
/* Get Manufacturer and LMP version */
cmd[0] = HCI_COMMAND_PKT;
cmd[1] = 0x01;
cmd[2] = 0x10;
cmd[3] = 0x00;
do {
n = write(fd, cmd, 4);
if (n < 4) {
perror("Failed to write init command");
return -1;
}
/* Read reply. */
n = read_hci_event(fd, resp, 100);
FAILIF(n < 0, "Failed to read init response");
/* Wait for command complete event for our Opcode */
} while (resp[4] != cmd[1] && resp[5] != cmd[2]);
/* Verify manufacturer */
if ((resp[11] & 0xFF) != 0x1d)
fprintf(stderr,
"WARNING : module's manufacturer is not Qualcomm\n");
/* Print LMP version */
fprintf(stderr,
"Qualcomm module LMP version : 0x%02x\n", resp[10] & 0xFF);
/* Print LMP subversion */
{
unsigned short lmp_subv = resp[13] | (resp[14] << 8);
fprintf(stderr, "Qualcomm module LMP sub-version : 0x%04x\n",
lmp_subv);
}
/* Get SoC type */
cmd[0] = HCI_COMMAND_PKT;
cmd[1] = 0x00;
cmd[2] = 0xFC;
cmd[3] = 0x01;
cmd[4] = 0x06;
do {
n = write(fd, cmd, 5);
if (n < 5) {
perror("Failed to write vendor init command");
return -1;
}
/* Read reply. */
n = read_hci_event(fd, resp, 100);
FAILIF(n < 0, "Failed to read vendor init response");
} while (resp[3] != 0 && resp[4] != 2);
snprintf(fw, sizeof(fw), "%s/%c%c%c%c%c%c_%c%c%c%c.bin",
HCI_TO_STR(FIRMWARE_DIR),
resp[18], resp[19], resp[20], resp[21],
resp[22], resp[23],
resp[32], resp[33], resp[34], resp[35]);
/* Wait for command complete event for our Opcode */
n = read_hci_event(fd, resp, 100);
FAILIF(n < 0, "Failed to read init response");
qualcomm_load_firmware(fd, fw, bdaddr);
/* Reset */
cmd[0] = HCI_COMMAND_PKT;
cmd[1] = 0x03;
cmd[2] = 0x0C;
cmd[3] = 0x00;
do {
n = write(fd, cmd, 4);
if (n < 4) {
perror("Failed to write reset command");
return -1;
}
/* Read reply. */
n = read_hci_event(fd, resp, 100);
FAILIF(n < 0, "Failed to read reset response");
} while (resp[4] != cmd[1] && resp[5] != cmd[2]);
nanosleep(&tm, NULL);
return 0;
}
|