seq2bseq.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2012-2013 Intel Corporation
  7. *
  8. *
  9. */
  10. #ifdef HAVE_CONFIG_H
  11. #include <config.h>
  12. #endif
  13. #define _GNU_SOURCE
  14. #include <stdio.h>
  15. #include <fcntl.h>
  16. #include <unistd.h>
  17. #include <string.h>
  18. #include <stdlib.h>
  19. #include <getopt.h>
  20. #include <errno.h>
  21. #include <sys/stat.h>
  22. static int convert_line(int fd, const char *line)
  23. {
  24. const char *ptr = line;
  25. char str[3];
  26. unsigned char val;
  27. if (line[0] == '*' || line[0] == '\r' || line[0] == '\n')
  28. return 0;
  29. while (1) {
  30. str[0] = *ptr++;
  31. str[1] = *ptr++;
  32. str[2] = '\0';
  33. val = strtol(str, NULL, 16);
  34. if (write(fd, &val, 1) < 0)
  35. return -errno;
  36. if (*ptr == '\r' || *ptr == '\n')
  37. break;
  38. while (*ptr == ' ')
  39. ptr++;
  40. }
  41. return 0;
  42. }
  43. static void convert_file(const char *input_path, const char *output_path)
  44. {
  45. size_t line_size = 1024;
  46. char line_buffer[line_size];
  47. char *path;
  48. const char *ptr;
  49. FILE *fp;
  50. struct stat st;
  51. off_t cur = 0;
  52. int fd;
  53. if (output_path) {
  54. path = strdup(output_path);
  55. if (!path) {
  56. perror("Failed to allocate string");
  57. return;
  58. }
  59. } else {
  60. ptr = strrchr(input_path, '.');
  61. if (ptr) {
  62. path = malloc(ptr - input_path + 6);
  63. if (!path) {
  64. perror("Failed to allocate string");
  65. return;
  66. }
  67. strncpy(path, input_path, ptr - input_path);
  68. strcpy(path + (ptr - input_path), ".bseq");
  69. } else {
  70. if (asprintf(&path, "%s.bseq", input_path) < 0) {
  71. perror("Failed to allocate string");
  72. return;
  73. }
  74. }
  75. }
  76. printf("Converting %s to %s\n", input_path, path);
  77. fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
  78. free(path);
  79. if (fd < 0) {
  80. perror("Failed to create output file");
  81. return;
  82. }
  83. if (stat(input_path, &st) < 0) {
  84. fprintf(stderr, "Failed get file size\n");
  85. close(fd);
  86. return;
  87. }
  88. if (st.st_size == 0) {
  89. fprintf(stderr, "Empty file\n");
  90. close(fd);
  91. return;
  92. }
  93. fp = fopen(input_path, "r");
  94. if (!fp) {
  95. fprintf(stderr, "Failed to open input file\n");
  96. close(fd);
  97. return;
  98. }
  99. while (1) {
  100. char *str;
  101. int err;
  102. str = fgets(line_buffer, line_size - 1, fp);
  103. if (!str)
  104. break;
  105. cur += strlen(str);
  106. err = convert_line(fd, str);
  107. if (err < 0) {
  108. fprintf(stderr, "Failed to convert file (%s)\n",
  109. strerror(-err));
  110. break;
  111. }
  112. }
  113. fclose(fp);
  114. close(fd);
  115. }
  116. static void usage(void)
  117. {
  118. printf("Intel Bluetooth firmware converter\n"
  119. "Usage:\n");
  120. printf("\tseq2bseq [options] <file>\n");
  121. printf("Options:\n"
  122. "\t-o, --output <file> Provide firmware output file\n"
  123. "\t-h, --help Show help options\n");
  124. }
  125. static const struct option main_options[] = {
  126. { "output", required_argument, NULL, 'o' },
  127. { "version", no_argument, NULL, 'v' },
  128. { "help", no_argument, NULL, 'h' },
  129. { }
  130. };
  131. int main(int argc, char *argv[])
  132. {
  133. const char *output_path = NULL;
  134. int i;
  135. for (;;) {
  136. int opt;
  137. opt = getopt_long(argc, argv, "o:vh", main_options, NULL);
  138. if (opt < 0)
  139. break;
  140. switch (opt) {
  141. case 'o':
  142. output_path = optarg;
  143. break;
  144. case 'v':
  145. printf("%s\n", VERSION);
  146. return EXIT_SUCCESS;
  147. case 'h':
  148. usage();
  149. return EXIT_SUCCESS;
  150. default:
  151. return EXIT_FAILURE;
  152. }
  153. }
  154. if (argc - optind < 1) {
  155. fprintf(stderr, "No input firmware files provided\n");
  156. return EXIT_FAILURE;
  157. }
  158. if (output_path && argc - optind > 1) {
  159. fprintf(stderr, "Only single input firmware supported\n");
  160. return EXIT_FAILURE;
  161. }
  162. for (i = optind; i < argc; i++)
  163. convert_file(argv[i], output_path);
  164. return EXIT_SUCCESS;
  165. }