firmware.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2013, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. *
  20. * \brief IAX Firmware Support
  21. *
  22. * \author Mark Spencer <markster@digium.com>
  23. */
  24. /*** MODULEINFO
  25. <support_level>core</support_level>
  26. ***/
  27. #include "asterisk.h"
  28. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  29. #include <sys/types.h>
  30. #include <sys/stat.h>
  31. #include <unistd.h>
  32. #include <fcntl.h>
  33. #include <dirent.h>
  34. #include <sys/mman.h>
  35. #include <arpa/inet.h>
  36. #include "asterisk/linkedlists.h"
  37. #include "asterisk/md5.h"
  38. #include "asterisk/paths.h"
  39. #include "asterisk/utils.h"
  40. #include "include/firmware.h"
  41. struct iax_firmware {
  42. AST_LIST_ENTRY(iax_firmware) list;
  43. int fd;
  44. int mmaplen;
  45. int dead;
  46. struct ast_iax2_firmware_header *fwh;
  47. unsigned char *buf;
  48. };
  49. static AST_LIST_HEAD_STATIC(firmwares, iax_firmware);
  50. static int try_firmware(char *s)
  51. {
  52. struct stat stbuf;
  53. struct iax_firmware *cur = NULL;
  54. int ifd, fd, res, len, chunk;
  55. struct ast_iax2_firmware_header *fwh, fwh2;
  56. struct MD5Context md5;
  57. unsigned char sum[16], buf[1024];
  58. char *s2, *last;
  59. s2 = ast_alloca(strlen(s) + 100);
  60. last = strrchr(s, '/');
  61. if (last)
  62. last++;
  63. else
  64. last = s;
  65. snprintf(s2, strlen(s) + 100, "/var/tmp/%s-%ld", last, ast_random());
  66. if (stat(s, &stbuf) < 0) {
  67. ast_log(LOG_WARNING, "Failed to stat '%s': %s\n", s, strerror(errno));
  68. return -1;
  69. }
  70. /* Make sure it's not a directory */
  71. if (S_ISDIR(stbuf.st_mode))
  72. return -1;
  73. ifd = open(s, O_RDONLY);
  74. if (ifd < 0) {
  75. ast_log(LOG_WARNING, "Cannot open '%s': %s\n", s, strerror(errno));
  76. return -1;
  77. }
  78. fd = open(s2, O_RDWR | O_CREAT | O_EXCL, AST_FILE_MODE);
  79. if (fd < 0) {
  80. ast_log(LOG_WARNING, "Cannot open '%s' for writing: %s\n", s2, strerror(errno));
  81. close(ifd);
  82. return -1;
  83. }
  84. /* Unlink our newly created file */
  85. unlink(s2);
  86. /* Now copy the firmware into it */
  87. len = stbuf.st_size;
  88. while(len) {
  89. chunk = len;
  90. if (chunk > sizeof(buf))
  91. chunk = sizeof(buf);
  92. res = read(ifd, buf, chunk);
  93. if (res != chunk) {
  94. ast_log(LOG_WARNING, "Only read %d of %d bytes of data :(: %s\n", res, chunk, strerror(errno));
  95. close(ifd);
  96. close(fd);
  97. return -1;
  98. }
  99. res = write(fd, buf, chunk);
  100. if (res != chunk) {
  101. ast_log(LOG_WARNING, "Only write %d of %d bytes of data :(: %s\n", res, chunk, strerror(errno));
  102. close(ifd);
  103. close(fd);
  104. return -1;
  105. }
  106. len -= chunk;
  107. }
  108. close(ifd);
  109. /* Return to the beginning */
  110. lseek(fd, 0, SEEK_SET);
  111. if ((res = read(fd, &fwh2, sizeof(fwh2))) != sizeof(fwh2)) {
  112. ast_log(LOG_WARNING, "Unable to read firmware header in '%s'\n", s);
  113. close(fd);
  114. return -1;
  115. }
  116. if (ntohl(fwh2.magic) != IAX_FIRMWARE_MAGIC) {
  117. ast_log(LOG_WARNING, "'%s' is not a valid firmware file\n", s);
  118. close(fd);
  119. return -1;
  120. }
  121. if (ntohl(fwh2.datalen) != (stbuf.st_size - sizeof(fwh2))) {
  122. ast_log(LOG_WARNING, "Invalid data length in firmware '%s'\n", s);
  123. close(fd);
  124. return -1;
  125. }
  126. if (fwh2.devname[sizeof(fwh2.devname) - 1] || ast_strlen_zero((char *)fwh2.devname)) {
  127. ast_log(LOG_WARNING, "No or invalid device type specified for '%s'\n", s);
  128. close(fd);
  129. return -1;
  130. }
  131. fwh = (struct ast_iax2_firmware_header*)mmap(NULL, stbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
  132. if (fwh == MAP_FAILED) {
  133. ast_log(LOG_WARNING, "mmap failed: %s\n", strerror(errno));
  134. close(fd);
  135. return -1;
  136. }
  137. MD5Init(&md5);
  138. MD5Update(&md5, fwh->data, ntohl(fwh->datalen));
  139. MD5Final(sum, &md5);
  140. if (memcmp(sum, fwh->chksum, sizeof(sum))) {
  141. ast_log(LOG_WARNING, "Firmware file '%s' fails checksum\n", s);
  142. munmap((void*)fwh, stbuf.st_size);
  143. close(fd);
  144. return -1;
  145. }
  146. AST_LIST_TRAVERSE(&firmwares, cur, list) {
  147. if (!strcmp((const char *) cur->fwh->devname, (const char *) fwh->devname)) {
  148. /* Found a candidate */
  149. if (cur->dead || (ntohs(cur->fwh->version) < ntohs(fwh->version)))
  150. /* The version we have on loaded is older, load this one instead */
  151. break;
  152. /* This version is no newer than what we have. Don't worry about it.
  153. We'll consider it a proper load anyhow though */
  154. munmap((void*)fwh, stbuf.st_size);
  155. close(fd);
  156. return 0;
  157. }
  158. }
  159. if (!cur && ((cur = ast_calloc(1, sizeof(*cur))))) {
  160. cur->fd = -1;
  161. AST_LIST_INSERT_TAIL(&firmwares, cur, list);
  162. }
  163. if (cur) {
  164. if (cur->fwh)
  165. munmap((void*)cur->fwh, cur->mmaplen);
  166. if (cur->fd > -1)
  167. close(cur->fd);
  168. cur->fwh = fwh;
  169. cur->fd = fd;
  170. cur->mmaplen = stbuf.st_size;
  171. cur->dead = 0;
  172. }
  173. return 0;
  174. }
  175. static void destroy_firmware(struct iax_firmware *cur)
  176. {
  177. /* Close firmware */
  178. if (cur->fwh) {
  179. munmap((void*)cur->fwh, ntohl(cur->fwh->datalen) + sizeof(*(cur->fwh)));
  180. }
  181. close(cur->fd);
  182. ast_free(cur);
  183. }
  184. void iax_firmware_reload(void)
  185. {
  186. struct iax_firmware *cur = NULL;
  187. DIR *fwd;
  188. struct dirent *de;
  189. char dir[256], fn[256];
  190. AST_LIST_LOCK(&firmwares);
  191. /* Mark all as dead */
  192. AST_LIST_TRAVERSE(&firmwares, cur, list) {
  193. cur->dead = 1;
  194. }
  195. /* Now that we have marked them dead... load new ones */
  196. snprintf(dir, sizeof(dir), "%s/firmware/iax", ast_config_AST_DATA_DIR);
  197. fwd = opendir(dir);
  198. if (fwd) {
  199. while((de = readdir(fwd))) {
  200. if (de->d_name[0] != '.') {
  201. snprintf(fn, sizeof(fn), "%s/%s", dir, de->d_name);
  202. if (!try_firmware(fn)) {
  203. ast_verb(2, "Loaded firmware '%s'\n", de->d_name);
  204. }
  205. }
  206. }
  207. closedir(fwd);
  208. } else {
  209. ast_log(LOG_WARNING, "Error opening firmware directory '%s': %s\n", dir, strerror(errno));
  210. }
  211. /* Clean up leftovers */
  212. AST_LIST_TRAVERSE_SAFE_BEGIN(&firmwares, cur, list) {
  213. if (!cur->dead)
  214. continue;
  215. AST_LIST_REMOVE_CURRENT(list);
  216. destroy_firmware(cur);
  217. }
  218. AST_LIST_TRAVERSE_SAFE_END;
  219. AST_LIST_UNLOCK(&firmwares);
  220. }
  221. void iax_firmware_unload(void)
  222. {
  223. struct iax_firmware *cur = NULL;
  224. AST_LIST_LOCK(&firmwares);
  225. AST_LIST_TRAVERSE_SAFE_BEGIN(&firmwares, cur, list) {
  226. AST_LIST_REMOVE_CURRENT(list);
  227. destroy_firmware(cur);
  228. }
  229. AST_LIST_TRAVERSE_SAFE_END;
  230. AST_LIST_UNLOCK(&firmwares);
  231. }
  232. int iax_firmware_get_version(const char *dev, uint16_t *version)
  233. {
  234. struct iax_firmware *cur = NULL;
  235. if (ast_strlen_zero(dev))
  236. return 0;
  237. AST_LIST_LOCK(&firmwares);
  238. AST_LIST_TRAVERSE(&firmwares, cur, list) {
  239. if (!strcmp(dev, (const char *) cur->fwh->devname)) {
  240. *version = ntohs(cur->fwh->version);
  241. AST_LIST_UNLOCK(&firmwares);
  242. return 1;
  243. }
  244. }
  245. AST_LIST_UNLOCK(&firmwares);
  246. return 0;
  247. }
  248. int iax_firmware_append(struct iax_ie_data *ied, const char *dev, unsigned int desc)
  249. {
  250. int res = -1;
  251. unsigned int bs = desc & 0xff;
  252. unsigned int start = (desc >> 8) & 0xffffff;
  253. unsigned int bytes;
  254. struct iax_firmware *cur;
  255. if (ast_strlen_zero((char *)dev) || !bs)
  256. return -1;
  257. start *= bs;
  258. AST_LIST_LOCK(&firmwares);
  259. AST_LIST_TRAVERSE(&firmwares, cur, list) {
  260. if (strcmp(dev, (const char *) cur->fwh->devname))
  261. continue;
  262. iax_ie_append_int(ied, IAX_IE_FWBLOCKDESC, desc);
  263. if (start < ntohl(cur->fwh->datalen)) {
  264. bytes = ntohl(cur->fwh->datalen) - start;
  265. if (bytes > bs)
  266. bytes = bs;
  267. iax_ie_append_raw(ied, IAX_IE_FWBLOCKDATA, cur->fwh->data + start, bytes);
  268. } else {
  269. bytes = 0;
  270. iax_ie_append(ied, IAX_IE_FWBLOCKDATA);
  271. }
  272. if (bytes == bs)
  273. res = 0;
  274. else
  275. res = 1;
  276. break;
  277. }
  278. AST_LIST_UNLOCK(&firmwares);
  279. return res;
  280. }
  281. void iax_firmware_traverse(
  282. const char *filter,
  283. int (*callback)(struct ast_iax2_firmware_header *header, void *data),
  284. void *data)
  285. {
  286. struct iax_firmware *cur = NULL;
  287. if (!callback) {
  288. return;
  289. }
  290. AST_LIST_LOCK(&firmwares);
  291. AST_LIST_TRAVERSE(&firmwares, cur, list) {
  292. if (!filter || !strcasecmp(filter, (const char *) cur->fwh->devname)) {
  293. if (callback(cur->fwh, data)) {
  294. break;
  295. }
  296. }
  297. }
  298. AST_LIST_UNLOCK(&firmwares);
  299. }