| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466 |
- // SPDX-License-Identifier: GPL-2.0-or-later
- /*
- *
- * BlueZ - Bluetooth protocol stack for Linux
- *
- * Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
- *
- *
- */
- #ifdef HAVE_CONFIG_H
- #include <config.h>
- #endif
- #include <stdio.h>
- #include <errno.h>
- #include <stdlib.h>
- #include <getopt.h>
- #include <unistd.h>
- #include <sys/ioctl.h>
- #include <sys/socket.h>
- #include "lib/bluetooth.h"
- #include "lib/hci.h"
- #include "lib/hci_lib.h"
- #include "src/oui.h"
- static int transient = 0;
- static int generic_reset_device(int dd)
- {
- bdaddr_t bdaddr;
- int err;
- err = hci_send_cmd(dd, 0x03, 0x0003, 0, NULL);
- if (err < 0)
- return err;
- return hci_read_bd_addr(dd, &bdaddr, 10000);
- }
- #define OCF_ERICSSON_WRITE_BD_ADDR 0x000d
- typedef struct {
- bdaddr_t bdaddr;
- } __attribute__ ((packed)) ericsson_write_bd_addr_cp;
- static int ericsson_write_bd_addr(int dd, bdaddr_t *bdaddr)
- {
- struct hci_request rq;
- ericsson_write_bd_addr_cp cp;
- memset(&cp, 0, sizeof(cp));
- bacpy(&cp.bdaddr, bdaddr);
- memset(&rq, 0, sizeof(rq));
- rq.ogf = OGF_VENDOR_CMD;
- rq.ocf = OCF_ERICSSON_WRITE_BD_ADDR;
- rq.cparam = &cp;
- rq.clen = sizeof(cp);
- rq.rparam = NULL;
- rq.rlen = 0;
- if (hci_send_req(dd, &rq, 1000) < 0)
- return -1;
- return 0;
- }
- #define OCF_ERICSSON_STORE_IN_FLASH 0x0022
- typedef struct {
- uint8_t user_id;
- uint8_t flash_length;
- uint8_t flash_data[253];
- } __attribute__ ((packed)) ericsson_store_in_flash_cp;
- static int ericsson_store_in_flash(int dd, uint8_t user_id, uint8_t flash_length, uint8_t *flash_data)
- {
- struct hci_request rq;
- ericsson_store_in_flash_cp cp;
- memset(&cp, 0, sizeof(cp));
- cp.user_id = user_id;
- cp.flash_length = flash_length;
- if (flash_length > 0)
- memcpy(cp.flash_data, flash_data, flash_length);
- memset(&rq, 0, sizeof(rq));
- rq.ogf = OGF_VENDOR_CMD;
- rq.ocf = OCF_ERICSSON_STORE_IN_FLASH;
- rq.cparam = &cp;
- rq.clen = sizeof(cp);
- rq.rparam = NULL;
- rq.rlen = 0;
- if (hci_send_req(dd, &rq, 1000) < 0)
- return -1;
- return 0;
- }
- static int csr_write_bd_addr(int dd, bdaddr_t *bdaddr)
- {
- unsigned char cmd[] = { 0x02, 0x00, 0x0c, 0x00, 0x11, 0x47, 0x03, 0x70,
- 0x00, 0x00, 0x01, 0x00, 0x04, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
- unsigned char cp[254], rp[254];
- struct hci_request rq;
- if (transient)
- cmd[14] = 0x08;
- cmd[16] = bdaddr->b[2];
- cmd[17] = 0x00;
- cmd[18] = bdaddr->b[0];
- cmd[19] = bdaddr->b[1];
- cmd[20] = bdaddr->b[3];
- cmd[21] = 0x00;
- cmd[22] = bdaddr->b[4];
- cmd[23] = bdaddr->b[5];
- memset(&cp, 0, sizeof(cp));
- cp[0] = 0xc2;
- memcpy(cp + 1, cmd, sizeof(cmd));
- memset(&rq, 0, sizeof(rq));
- rq.ogf = OGF_VENDOR_CMD;
- rq.ocf = 0x00;
- rq.event = EVT_VENDOR;
- rq.cparam = cp;
- rq.clen = sizeof(cmd) + 1;
- rq.rparam = rp;
- rq.rlen = sizeof(rp);
- if (hci_send_req(dd, &rq, 2000) < 0)
- return -1;
- if (rp[0] != 0xc2) {
- errno = EIO;
- return -1;
- }
- if ((rp[9] + (rp[10] << 8)) != 0) {
- errno = ENXIO;
- return -1;
- }
- return 0;
- }
- static int csr_reset_device(int dd)
- {
- unsigned char cmd[] = { 0x02, 0x00, 0x09, 0x00,
- 0x00, 0x00, 0x01, 0x40, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
- unsigned char cp[254], rp[254];
- struct hci_request rq;
- if (transient)
- cmd[6] = 0x02;
- memset(&cp, 0, sizeof(cp));
- cp[0] = 0xc2;
- memcpy(cp + 1, cmd, sizeof(cmd));
- memset(&rq, 0, sizeof(rq));
- rq.ogf = OGF_VENDOR_CMD;
- rq.ocf = 0x00;
- rq.event = EVT_VENDOR;
- rq.cparam = cp;
- rq.clen = sizeof(cmd) + 1;
- rq.rparam = rp;
- rq.rlen = sizeof(rp);
- if (hci_send_req(dd, &rq, 2000) < 0)
- return -1;
- return 0;
- }
- #define OCF_TI_WRITE_BD_ADDR 0x0006
- typedef struct {
- bdaddr_t bdaddr;
- } __attribute__ ((packed)) ti_write_bd_addr_cp;
- static int ti_write_bd_addr(int dd, bdaddr_t *bdaddr)
- {
- struct hci_request rq;
- ti_write_bd_addr_cp cp;
- memset(&cp, 0, sizeof(cp));
- bacpy(&cp.bdaddr, bdaddr);
- memset(&rq, 0, sizeof(rq));
- rq.ogf = OGF_VENDOR_CMD;
- rq.ocf = OCF_TI_WRITE_BD_ADDR;
- rq.cparam = &cp;
- rq.clen = sizeof(cp);
- rq.rparam = NULL;
- rq.rlen = 0;
- if (hci_send_req(dd, &rq, 1000) < 0)
- return -1;
- return 0;
- }
- #define OCF_BCM_WRITE_BD_ADDR 0x0001
- typedef struct {
- bdaddr_t bdaddr;
- } __attribute__ ((packed)) bcm_write_bd_addr_cp;
- static int bcm_write_bd_addr(int dd, bdaddr_t *bdaddr)
- {
- struct hci_request rq;
- bcm_write_bd_addr_cp cp;
- memset(&cp, 0, sizeof(cp));
- bacpy(&cp.bdaddr, bdaddr);
- memset(&rq, 0, sizeof(rq));
- rq.ogf = OGF_VENDOR_CMD;
- rq.ocf = OCF_BCM_WRITE_BD_ADDR;
- rq.cparam = &cp;
- rq.clen = sizeof(cp);
- rq.rparam = NULL;
- rq.rlen = 0;
- if (hci_send_req(dd, &rq, 1000) < 0)
- return -1;
- return 0;
- }
- #define OCF_ZEEVO_WRITE_BD_ADDR 0x0001
- typedef struct {
- bdaddr_t bdaddr;
- } __attribute__ ((packed)) zeevo_write_bd_addr_cp;
- static int zeevo_write_bd_addr(int dd, bdaddr_t *bdaddr)
- {
- struct hci_request rq;
- zeevo_write_bd_addr_cp cp;
- memset(&cp, 0, sizeof(cp));
- bacpy(&cp.bdaddr, bdaddr);
- memset(&rq, 0, sizeof(rq));
- rq.ogf = OGF_VENDOR_CMD;
- rq.ocf = OCF_ZEEVO_WRITE_BD_ADDR;
- rq.cparam = &cp;
- rq.clen = sizeof(cp);
- rq.rparam = NULL;
- rq.rlen = 0;
- if (hci_send_req(dd, &rq, 1000) < 0)
- return -1;
- return 0;
- }
- #define OCF_MRVL_WRITE_BD_ADDR 0x0022
- typedef struct {
- uint8_t parameter_id;
- uint8_t bdaddr_len;
- bdaddr_t bdaddr;
- } __attribute__ ((packed)) mrvl_write_bd_addr_cp;
- static int mrvl_write_bd_addr(int dd, bdaddr_t *bdaddr)
- {
- mrvl_write_bd_addr_cp cp;
- memset(&cp, 0, sizeof(cp));
- cp.parameter_id = 0xFE;
- cp.bdaddr_len = 6;
- bacpy(&cp.bdaddr, bdaddr);
- if (hci_send_cmd(dd, OGF_VENDOR_CMD, OCF_MRVL_WRITE_BD_ADDR,
- sizeof(cp), &cp) < 0)
- return -1;
- sleep(1);
- return 0;
- }
- static int st_write_bd_addr(int dd, bdaddr_t *bdaddr)
- {
- return ericsson_store_in_flash(dd, 0xfe, 6, (uint8_t *) bdaddr);
- }
- static struct {
- uint16_t compid;
- int (*write_bd_addr)(int dd, bdaddr_t *bdaddr);
- int (*reset_device)(int dd);
- } vendor[] = {
- { 0, ericsson_write_bd_addr, NULL },
- { 10, csr_write_bd_addr, csr_reset_device },
- { 13, ti_write_bd_addr, NULL },
- { 15, bcm_write_bd_addr, generic_reset_device },
- { 18, zeevo_write_bd_addr, NULL },
- { 48, st_write_bd_addr, generic_reset_device },
- { 57, ericsson_write_bd_addr, generic_reset_device },
- { 72, mrvl_write_bd_addr, generic_reset_device },
- { 65535, NULL, NULL },
- };
- static void usage(void)
- {
- printf("bdaddr - Utility for changing the Bluetooth device address\n\n");
- printf("Usage:\n"
- "\tbdaddr [-i <dev>] [-r] [-t] [new bdaddr]\n");
- }
- static struct option main_options[] = {
- { "device", 1, 0, 'i' },
- { "reset", 0, 0, 'r' },
- { "transient", 0, 0, 't' },
- { "help", 0, 0, 'h' },
- { 0, 0, 0, 0 }
- };
- int main(int argc, char *argv[])
- {
- struct hci_dev_info di;
- struct hci_version ver;
- bdaddr_t bdaddr;
- char addr[18], *comp;
- int i, dd, opt, dev = 0, reset = 0;
- bacpy(&bdaddr, BDADDR_ANY);
- while ((opt=getopt_long(argc, argv, "+i:rth", main_options, NULL)) != -1) {
- switch (opt) {
- case 'i':
- dev = hci_devid(optarg);
- if (dev < 0) {
- perror("Invalid device");
- exit(1);
- }
- break;
- case 'r':
- reset = 1;
- break;
- case 't':
- transient = 1;
- break;
- case 'h':
- default:
- usage();
- exit(0);
- }
- }
- argc -= optind;
- argv += optind;
- optind = 0;
- dd = hci_open_dev(dev);
- if (dd < 0) {
- fprintf(stderr, "Can't open device hci%d: %s (%d)\n",
- dev, strerror(errno), errno);
- exit(1);
- }
- if (hci_devinfo(dev, &di) < 0) {
- fprintf(stderr, "Can't get device info for hci%d: %s (%d)\n",
- dev, strerror(errno), errno);
- hci_close_dev(dd);
- exit(1);
- }
- if (hci_read_local_version(dd, &ver, 1000) < 0) {
- fprintf(stderr, "Can't read version info for hci%d: %s (%d)\n",
- dev, strerror(errno), errno);
- hci_close_dev(dd);
- exit(1);
- }
- if (!bacmp(&di.bdaddr, BDADDR_ANY)) {
- if (hci_read_bd_addr(dd, &bdaddr, 1000) < 0) {
- fprintf(stderr, "Can't read address for hci%d: %s (%d)\n",
- dev, strerror(errno), errno);
- hci_close_dev(dd);
- exit(1);
- }
- } else
- bacpy(&bdaddr, &di.bdaddr);
- printf("Manufacturer: %s (%d)\n",
- bt_compidtostr(ver.manufacturer), ver.manufacturer);
- comp = batocomp(&bdaddr);
- ba2str(&bdaddr, addr);
- printf("Device address: %s", addr);
- if (comp) {
- printf(" (%s)\n", comp);
- free(comp);
- } else
- printf("\n");
- if (argc < 1) {
- hci_close_dev(dd);
- exit(0);
- }
- str2ba(argv[0], &bdaddr);
- if (!bacmp(&bdaddr, BDADDR_ANY)) {
- hci_close_dev(dd);
- exit(0);
- }
- for (i = 0; vendor[i].compid != 65535; i++)
- if (ver.manufacturer == vendor[i].compid) {
- comp = batocomp(&bdaddr);
- ba2str(&bdaddr, addr);
- printf("New BD address: %s", addr);
- if (comp) {
- printf(" (%s)\n\n", comp);
- free(comp);
- } else
- printf("\n\n");
- if (vendor[i].write_bd_addr(dd, &bdaddr) < 0) {
- fprintf(stderr, "Can't write new address\n");
- hci_close_dev(dd);
- exit(1);
- }
- printf("Address changed - ");
- if (reset && vendor[i].reset_device) {
- if (vendor[i].reset_device(dd) < 0) {
- printf("Reset device manually\n");
- } else {
- ioctl(dd, HCIDEVRESET, dev);
- printf("Device reset successfully\n");
- }
- } else {
- printf("Reset device now\n");
- }
- //ioctl(dd, HCIDEVRESET, dev);
- //ioctl(dd, HCIDEVDOWN, dev);
- //ioctl(dd, HCIDEVUP, dev);
- hci_close_dev(dd);
- exit(0);
- }
- hci_close_dev(dd);
- printf("\n");
- fprintf(stderr, "Unsupported manufacturer\n");
- exit(1);
- }
|