From fc217fc53f13b58ff09b2262682c406df24aa54b Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Sat, 11 Jan 2014 00:50:41 -0800 Subject: [PATCH] tools: Add firmware conversion utility --- .gitignore | 1 + Makefile.tools | 4 +- tools/seq2bseq.c | 202 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 206 insertions(+), 1 deletion(-) create mode 100644 tools/seq2bseq.c diff --git a/.gitignore b/.gitignore index 3e0641dfb..175828583 100644 --- a/.gitignore +++ b/.gitignore @@ -59,6 +59,7 @@ tools/amptest tools/sdptool tools/avtest tools/bdaddr +tools/seq2bseq tools/btiotest tools/mpris-player tools/bluetooth-player diff --git a/Makefile.tools b/Makefile.tools index ec1499a14..02798ae02 100644 --- a/Makefile.tools +++ b/Makefile.tools @@ -205,7 +205,7 @@ noinst_PROGRAMS += tools/bdaddr tools/avinfo tools/avtest \ tools/hcieventmask tools/hcisecfilter \ tools/btmgmt tools/btinfo tools/btattach \ tools/btsnoop tools/btproxy tools/btiotest \ - tools/mpris-player tools/cltest + tools/mpris-player tools/cltest tools/seq2bseq tools_bdaddr_SOURCES = tools/bdaddr.c src/oui.h src/oui.c tools_bdaddr_LDADD = lib/libbluetooth-internal.la @UDEV_LIBS@ @@ -252,6 +252,8 @@ tools_mpris_player_LDADD = gdbus/libgdbus-internal.la @GLIB_LIBS@ @DBUS_LIBS@ tools_cltest_SOURCES = tools/cltest.c monitor/mainloop.h monitor/mainloop.c tools_cltest_LDADD = lib/libbluetooth-internal.la +tools_seq2bseq_SOURCES = tools/seq2bseq.c + EXTRA_DIST += tools/bdaddr.1 endif diff --git a/tools/seq2bseq.c b/tools/seq2bseq.c new file mode 100644 index 000000000..6d7077799 --- /dev/null +++ b/tools/seq2bseq.c @@ -0,0 +1,202 @@ +/* + * + * BlueZ - Bluetooth protocol stack for Linux + * + * Copyright (C) 2012-2013 Intel Corporation + * + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include +#include +#include +#include +#include +#include + +static void convert_line(int fd, const char *line) +{ + const char *ptr = line; + char str[3]; + unsigned char val; + + if (line[0] == '*' || line[0] == '\r' || line[0] == '\r') + return; + + while (1) { + str[0] = *ptr++; + str[1] = *ptr++; + str[2] = '\0'; + + val = strtol(str, NULL, 16); + + write(fd, &val, 1); + + if (*ptr == '\r' || *ptr == '\n') + break; + + while (*ptr == ' ') + ptr++; + } +} + +static void convert_file(const char *input_path, const char *output_path) +{ + size_t line_size = 1024; + char line_buffer[line_size]; + char *path; + const char *ptr; + FILE *fp; + struct stat st; + off_t cur = 0; + int fd; + + if (output_path) { + path = strdup(output_path); + if (!path) { + perror("Failed to allocate string"); + return; + } + } else { + ptr = strrchr(input_path, '.'); + if (ptr) { + path = malloc(ptr - input_path + 6); + if (!path) { + perror("Failed to allocate string"); + return; + } + strncpy(path, input_path, ptr - input_path); + strcpy(path + (ptr - input_path), ".bseq"); + } else { + if (asprintf(&path, "%s.bseq", input_path) < 0) { + perror("Failed to allocate string"); + return; + } + } + } + + printf("Converting %s to %s\n", input_path, path); + + fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644); + + free(path); + + if (fd < 0) { + perror("Failed to create output file"); + return; + } + + if (stat(input_path, &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; + } + + fp = fopen(input_path, "r"); + if (!fp) { + fprintf(stderr, "Failed to open input file\n"); + close(fd); + return; + } + + while (1) { + char *str; + + str = fgets(line_buffer, line_size - 1, fp); + if (!str) + break; + + cur += strlen(str); + + convert_line(fd, str); + } + + fclose(fp); + + close(fd); +} + +static void usage(void) +{ + printf("Intel Bluetooth firmware converter\n" + "Usage:\n"); + printf("\tseq2bseq [options] \n"); + printf("Options:\n" + "\t-o, --output Provide firmware output file\n" + "\t-h, --help Show help options\n"); +} + +static const struct option main_options[] = { + { "output", required_argument, NULL, 'o' }, + { "version", no_argument, NULL, 'v' }, + { "help", no_argument, NULL, 'h' }, + { } +}; + +int main(int argc, char *argv[]) +{ + const char *output_path = NULL; + int i; + + for (;;) { + int opt; + + opt = getopt_long(argc, argv, "o:vh", main_options, NULL); + if (opt < 0) + break; + + switch (opt) { + case 'o': + output_path = optarg; + break; + 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; + } + + if (output_path && argc - optind > 1) { + fprintf(stderr, "Only single input firmware supported\n"); + return EXIT_FAILURE; + } + + for (i = optind; i < argc; i++) + convert_file(argv[i], output_path); + + return EXIT_SUCCESS; +} -- 2.47.3