nokfw.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 <errno.h>
  16. #include <fcntl.h>
  17. #include <unistd.h>
  18. #include <string.h>
  19. #include <getopt.h>
  20. #include <stdlib.h>
  21. #include <stdint.h>
  22. #include <sys/stat.h>
  23. #include <sys/mman.h>
  24. struct neg_cmd {
  25. uint8_t ack;
  26. uint16_t baud;
  27. uint16_t unused1;
  28. uint8_t proto;
  29. uint16_t sys_clk;
  30. uint16_t unused2;
  31. } __attribute__ ((packed));
  32. struct alive_pkt {
  33. uint8_t mid;
  34. uint8_t unused;
  35. } __attribute__ ((packed));
  36. static void print_cmd(uint16_t opcode, const uint8_t *buf, uint8_t plen)
  37. {
  38. switch (opcode) {
  39. case 0x0c43:
  40. printf(" Write_Inquiry_Scan_Type [type=%u]", buf[0]);
  41. break;
  42. case 0x0c47:
  43. printf(" Write_Page_Scan_Type [type=%u]", buf[0]);
  44. break;
  45. case 0xfc01:
  46. printf(" Write_BD_ADDR [bdaddr=%02x:%02x:%02x:%02x:%02x:%02x]",
  47. buf[5], buf[4], buf[3], buf[2], buf[1], buf[0]);
  48. break;
  49. case 0xfc0b:
  50. printf(" Write_Local_Supported_Features");
  51. printf(" [features=%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x]",
  52. buf[0], buf[1], buf[2], buf[3],
  53. buf[4], buf[5], buf[6], buf[7]);
  54. break;
  55. case 0xfc0a:
  56. printf(" Super_Peek_Poke [type=%u]", buf[0]);
  57. break;
  58. case 0xfc15:
  59. printf(" FM_RDS_Command [register=0x%02x,mode=%u]",
  60. buf[0], buf[1]);
  61. break;
  62. case 0xfc18:
  63. printf(" Update_UART_Baud_Rate");
  64. break;
  65. case 0xfc1c:
  66. printf(" Write_SCO_PCM_Int_Param");
  67. break;
  68. case 0xfc1e:
  69. printf(" Write_PCM_Data_Format_Param");
  70. break;
  71. case 0xfc22:
  72. printf(" Write_SCO_Time_Slot [slot=%u]", buf[0]);
  73. break;
  74. case 0xfc41:
  75. printf(" Write_Collaboration_Mode");
  76. break;
  77. case 0xfc4c:
  78. printf(" Write_RAM [address=0x%08x]",
  79. buf[0] | buf[1] << 8 | buf[2] << 16 | buf[3] << 24);
  80. break;
  81. case 0xfc4e:
  82. printf(" Launch_RAM [address=0x%08x]",
  83. buf[0] | buf[1] << 8 | buf[2] << 16 | buf[3] << 24);
  84. break;
  85. case 0xfc61:
  86. printf(" Write_PCM_Pins");
  87. break;
  88. }
  89. }
  90. static void analyze_memory(const uint8_t *buf, size_t len)
  91. {
  92. const uint8_t *ptr = buf;
  93. const struct neg_cmd *neg;
  94. const struct alive_pkt *alive;
  95. uint16_t pkt_len, opcode;
  96. uint8_t pkt_type, plen;
  97. while (ptr < buf + len) {
  98. pkt_len = ptr[0] | ptr[1] << 8;
  99. pkt_type = ptr[2];
  100. printf("len=%-3u type=%u,", pkt_len, pkt_type);
  101. switch (pkt_type) {
  102. case 0x01:
  103. opcode = ptr[3] | ptr[4] << 8;
  104. plen = ptr[5];
  105. printf("%-5s opcode=0x%04x plen=%-3u", "cmd",
  106. opcode, plen);
  107. print_cmd(opcode, ptr + 6, plen);
  108. break;
  109. case 0x06:
  110. plen = ptr[3];
  111. printf("%-5s plen=%-2u", "neg", plen);
  112. neg = (void *) (ptr + 4);
  113. printf(" [ack=%u baud=%u proto=0x%02x sys_clk=%u]",
  114. neg->ack, neg->baud, neg->proto, neg->sys_clk);
  115. break;
  116. case 0x07:
  117. plen = ptr[3];
  118. printf("%-5s plen=%-2u", "alive", plen);
  119. alive = (void *) (ptr + 4);
  120. printf(" [mid=0x%02x]", alive->mid);
  121. break;
  122. case 0x08:
  123. opcode = ptr[3] | ptr[4] << 8;
  124. plen = ptr[5];
  125. printf("%-5s opcode=0x%04x plen=%-3u", "radio",
  126. opcode, plen);
  127. print_cmd(opcode, ptr + 6, plen);
  128. break;
  129. default:
  130. printf("unknown");
  131. break;
  132. }
  133. printf("\n");
  134. ptr += pkt_len + 2;
  135. }
  136. }
  137. static void analyze_file(const char *pathname)
  138. {
  139. struct stat st;
  140. void *map;
  141. int fd;
  142. printf("Analyzing %s\n", pathname);
  143. fd = open(pathname, O_RDONLY | O_CLOEXEC);
  144. if (fd < 0) {
  145. perror("Failed to open file");
  146. return;
  147. }
  148. if (fstat(fd, &st) < 0) {
  149. fprintf(stderr, "Failed get file size\n");
  150. close(fd);
  151. return;
  152. }
  153. if (st.st_size == 0) {
  154. fprintf(stderr, "Empty file\n");
  155. close(fd);
  156. return;
  157. }
  158. map = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
  159. if (!map || map == MAP_FAILED) {
  160. fprintf(stderr, "Failed to map file\n");
  161. close(fd);
  162. return;
  163. }
  164. analyze_memory(map, st.st_size);
  165. munmap(map, st.st_size);
  166. close(fd);
  167. }
  168. static void usage(void)
  169. {
  170. printf("Nokia Bluetooth firmware analyzer\n"
  171. "Usage:\n");
  172. printf("\tnokfw [options] <file>\n");
  173. printf("Options:\n"
  174. "\t-h, --help Show help options\n");
  175. }
  176. static const struct option main_options[] = {
  177. { "version", no_argument, NULL, 'v' },
  178. { "help", no_argument, NULL, 'h' },
  179. { }
  180. };
  181. int main(int argc, char *argv[])
  182. {
  183. int i;
  184. for (;;) {
  185. int opt;
  186. opt = getopt_long(argc, argv, "vh", main_options, NULL);
  187. if (opt < 0)
  188. break;
  189. switch (opt) {
  190. case 'v':
  191. printf("%s\n", VERSION);
  192. return EXIT_SUCCESS;
  193. case 'h':
  194. usage();
  195. return EXIT_SUCCESS;
  196. default:
  197. return EXIT_FAILURE;
  198. }
  199. }
  200. if (argc - optind < 1) {
  201. fprintf(stderr, "No input firmware files provided\n");
  202. return EXIT_FAILURE;
  203. }
  204. for (i = optind; i < argc; i++)
  205. analyze_file(argv[i]);
  206. return EXIT_SUCCESS;
  207. }