bluez/tools/nokfw.c
Tedd Ho-Jeong An 0a259dd05b tools: Add SPDX License Identifier
This patch adds SPDX License Identifier and removes the license text.

-------------------------------------
       License            COUNT
-------------------------------------
 GPL-2.0-or-later     :     97
 LGPL-2.1-or-later    :     38
 GPL-2.0-only         :      2

License: GPL-2.0-or-later
   tools/l2cap-tester.c
   tools/hcisecfilter.c
   tools/ciptool.c
   tools/btsnoop.c
   tools/check-selftest.c
   tools/btpclientctl.c
   tools/hci-tester.c
   tools/hcitool.c
   tools/btiotest.c
   tools/oobtest.c
   tools/btinfo.c
   tools/hwdb.c
   tools/hciattach_bcm43xx.c
   tools/mgmt-tester.c
   tools/hex2hcd.c
   tools/hciattach_st.c
   tools/smp-tester.c
   tools/bluetooth-player.c
   tools/hciattach_tialt.c
   tools/gap-tester.c
   tools/bluemoon.c
   tools/bneptest.c
   tools/gatt-service.c
   tools/rctest.c
   tools/rfcomm-tester.c
   tools/hcieventmask.c
   tools/hciattach_ti.c
   tools/seq2bseq.c
   tools/scotest.c
   tools/bcmfw.c
   tools/hciconfig.c
   tools/btattach.c
   tools/l2ping.c
   tools/obexctl.c
   tools/l2test.c
   tools/hciattach_intel.c
   tools/hciattach.h
   tools/create-image.c
   tools/bnep-tester.c
   tools/userchan-tester.c
   tools/rfcomm.c
   tools/btmon-logger.c
   tools/hcidump.c
   tools/rtlfw.c
   tools/hciattach_qualcomm.c
   tools/btproxy.c
   tools/nokfw.c
   tools/hciattach_ath3k.c
   tools/3dsp.c
   tools/bdaddr.c
   tools/sco-tester.c
   tools/hciattach.c
   tools/amptest.c
   tools/btgatt-server.c
   tools/btgatt-client.c
   tools/cltest.c
   tools/ibeacon.c
   tools/mcaptest.c
   tools/hid2hci.c
   tools/btmgmt.c
   tools/advtest.c
   tools/eddystone.c
   tools/avtest.c
   tools/mpris-proxy.c
   tools/avinfo.c
   tools/sdptool.c
   tools/btconfig.c
   tools/update_compids.sh
   tools/parser/parser.h
   tools/parser/obex.c
   tools/parser/amp.c
   tools/parser/sdp.c
   tools/parser/tcpip.c
   tools/parser/sap.c
   tools/parser/cmtp.c
   tools/parser/avctp.c
   tools/parser/lmp.c
   tools/parser/ppp.c
   tools/parser/rfcomm.h
   tools/parser/hci.c
   tools/parser/sdp.h
   tools/parser/parser.c
   tools/parser/rfcomm.c
   tools/parser/avdtp.c
   tools/parser/avrcp.c
   tools/parser/ericsson.c
   tools/parser/hcrp.c
   tools/parser/bpa.c
   tools/parser/hidp.c
   tools/parser/bnep.c
   tools/parser/capi.c
   tools/parser/att.c
   tools/parser/l2cap.c
   tools/parser/smp.c
   tools/parser/csr.c
   tools/parser/l2cap.h
   tools/parse_companies.pl

License: LGPL-2.1-or-later
   tools/test-runner.c
   tools/btpclient.c
   tools/meshctl.c
   tools/mesh-cfgclient.c
   tools/mesh/model.h
   tools/mesh/util.h
   tools/mesh/config-model.h
   tools/mesh/cfgcli.h
   tools/mesh/mesh-db.c
   tools/mesh/mesh-db.h
   tools/mesh/keys.c
   tools/mesh/util.c
   tools/mesh/agent.h
   tools/mesh/remote.c
   tools/mesh/keys.h
   tools/mesh/agent.c
   tools/mesh/cfgcli.c
   tools/mesh/remote.h
   tools/mesh-gatt/prov.c
   tools/mesh-gatt/util.h
   tools/mesh-gatt/prov.h
   tools/mesh-gatt/net.c
   tools/mesh-gatt/util.c
   tools/mesh-gatt/prov-db.h
   tools/mesh-gatt/crypto.c
   tools/mesh-gatt/crypto.h
   tools/mesh-gatt/gatt.c
   tools/mesh-gatt/config-server.c
   tools/mesh-gatt/keys.h
   tools/mesh-gatt/onoff-model.c
   tools/mesh-gatt/net.h
   tools/mesh-gatt/gatt.h
   tools/mesh-gatt/node.c
   tools/mesh-gatt/config-client.c
   tools/mesh-gatt/mesh-net.h
   tools/mesh-gatt/node.h
   tools/mesh-gatt/onoff-model.h
   tools/mesh-gatt/prov-db.c

License: GPL-2.0-only
   tools/obex-server-tool.c
   tools/obex-client-tool.c
2020-09-29 14:37:23 -07:00

236 lines
4.7 KiB
C

// SPDX-License-Identifier: GPL-2.0-or-later
/*
*
* BlueZ - Bluetooth protocol stack for Linux
*
* Copyright (C) 2012-2013 Intel Corporation
*
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#define _GNU_SOURCE
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <getopt.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/stat.h>
#include <sys/mman.h>
struct neg_cmd {
uint8_t ack;
uint16_t baud;
uint16_t unused1;
uint8_t proto;
uint16_t sys_clk;
uint16_t unused2;
} __attribute__ ((packed));
struct alive_pkt {
uint8_t mid;
uint8_t unused;
} __attribute__ ((packed));
static void print_cmd(uint16_t opcode, const uint8_t *buf, uint8_t plen)
{
switch (opcode) {
case 0x0c43:
printf(" Write_Inquiry_Scan_Type [type=%u]", buf[0]);
break;
case 0x0c47:
printf(" Write_Page_Scan_Type [type=%u]", buf[0]);
break;
case 0xfc01:
printf(" Write_BD_ADDR [bdaddr=%02x:%02x:%02x:%02x:%02x:%02x]",
buf[5], buf[4], buf[3], buf[2], buf[1], buf[0]);
break;
case 0xfc0b:
printf(" Write_Local_Supported_Features");
printf(" [features=%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x]",
buf[0], buf[1], buf[2], buf[3],
buf[4], buf[5], buf[6], buf[7]);
break;
case 0xfc0a:
printf(" Super_Peek_Poke [type=%u]", buf[0]);
break;
case 0xfc15:
printf(" FM_RDS_Command [register=0x%02x,mode=%u]",
buf[0], buf[1]);
break;
case 0xfc18:
printf(" Update_UART_Baud_Rate");
break;
case 0xfc1c:
printf(" Write_SCO_PCM_Int_Param");
break;
case 0xfc1e:
printf(" Write_PCM_Data_Format_Param");
break;
case 0xfc22:
printf(" Write_SCO_Time_Slot [slot=%u]", buf[0]);
break;
case 0xfc41:
printf(" Write_Collaboration_Mode");
break;
case 0xfc4c:
printf(" Write_RAM [address=0x%08x]",
buf[0] | buf[1] << 8 | buf[2] << 16 | buf[3] << 24);
break;
case 0xfc4e:
printf(" Launch_RAM [address=0x%08x]",
buf[0] | buf[1] << 8 | buf[2] << 16 | buf[3] << 24);
break;
case 0xfc61:
printf(" Write_PCM_Pins");
break;
}
}
static void analyze_memory(const uint8_t *buf, size_t len)
{
const uint8_t *ptr = buf;
const struct neg_cmd *neg;
const struct alive_pkt *alive;
uint16_t pkt_len, opcode;
uint8_t pkt_type, plen;
while (ptr < buf + len) {
pkt_len = ptr[0] | ptr[1] << 8;
pkt_type = ptr[2];
printf("len=%-3u type=%u,", pkt_len, pkt_type);
switch (pkt_type) {
case 0x01:
opcode = ptr[3] | ptr[4] << 8;
plen = ptr[5];
printf("%-5s opcode=0x%04x plen=%-3u", "cmd",
opcode, plen);
print_cmd(opcode, ptr + 6, plen);
break;
case 0x06:
plen = ptr[3];
printf("%-5s plen=%-2u", "neg", plen);
neg = (void *) (ptr + 4);
printf(" [ack=%u baud=%u proto=0x%02x sys_clk=%u]",
neg->ack, neg->baud, neg->proto, neg->sys_clk);
break;
case 0x07:
plen = ptr[3];
printf("%-5s plen=%-2u", "alive", plen);
alive = (void *) (ptr + 4);
printf(" [mid=0x%02x]", alive->mid);
break;
case 0x08:
opcode = ptr[3] | ptr[4] << 8;
plen = ptr[5];
printf("%-5s opcode=0x%04x plen=%-3u", "radio",
opcode, plen);
print_cmd(opcode, ptr + 6, plen);
break;
default:
printf("unknown");
break;
}
printf("\n");
ptr += pkt_len + 2;
}
}
static void analyze_file(const char *pathname)
{
struct stat st;
void *map;
int fd;
printf("Analyzing %s\n", pathname);
fd = open(pathname, O_RDONLY | O_CLOEXEC);
if (fd < 0) {
perror("Failed to open file");
return;
}
if (fstat(fd, &st) < 0) {
fprintf(stderr, "Failed get file size\n");
close(fd);
return;
}
if (st.st_size == 0) {
fprintf(stderr, "Empty file\n");
close(fd);
return;
}
map = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
if (!map || map == MAP_FAILED) {
fprintf(stderr, "Failed to map file\n");
close(fd);
return;
}
analyze_memory(map, st.st_size);
munmap(map, st.st_size);
close(fd);
}
static void usage(void)
{
printf("Nokia Bluetooth firmware analyzer\n"
"Usage:\n");
printf("\tnokfw [options] <file>\n");
printf("Options:\n"
"\t-h, --help Show help options\n");
}
static const struct option main_options[] = {
{ "version", no_argument, NULL, 'v' },
{ "help", no_argument, NULL, 'h' },
{ }
};
int main(int argc, char *argv[])
{
int i;
for (;;) {
int opt;
opt = getopt_long(argc, argv, "vh", main_options, NULL);
if (opt < 0)
break;
switch (opt) {
case 'v':
printf("%s\n", VERSION);
return EXIT_SUCCESS;
case 'h':
usage();
return EXIT_SUCCESS;
default:
return EXIT_FAILURE;
}
}
if (argc - optind < 1) {
fprintf(stderr, "No input firmware files provided\n");
return EXIT_FAILURE;
}
for (i = optind; i < argc; i++)
analyze_file(argv[i]);
return EXIT_SUCCESS;
}