You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

286 lines
8.2 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2006 by Dominic Rath *
  3. * Dominic.Rath@gmx.de *
  4. * *
  5. * Copyright (C) 2007,2008 Øyvind Harboe *
  6. * oyvind.harboe@zylin.com *
  7. * *
  8. * Copyright (C) 2008 by Spencer Oliver *
  9. * spen@spen-soft.co.uk *
  10. * *
  11. * This program is free software; you can redistribute it and/or modify *
  12. * it under the terms of the GNU General Public License as published by *
  13. * the Free Software Foundation; either version 2 of the License, or *
  14. * (at your option) any later version. *
  15. * *
  16. * This program is distributed in the hope that it will be useful, *
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  19. * GNU General Public License for more details. *
  20. * *
  21. * You should have received a copy of the GNU General Public License *
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>. *
  23. ***************************************************************************/
  24. #ifndef OPENOCD_HELPER_REPLACEMENTS_H
  25. #define OPENOCD_HELPER_REPLACEMENTS_H
  26. #include <stdint.h>
  27. /* MIN,MAX macros */
  28. #ifndef MIN
  29. #define MIN(a, b) (((a) < (b)) ? (a) : (b))
  30. #endif
  31. #ifndef MAX
  32. #define MAX(a, b) (((a) > (b)) ? (a) : (b))
  33. #endif
  34. /* for systems that do not support ENOTSUP
  35. * win32 being one of them */
  36. #ifndef ENOTSUP
  37. #define ENOTSUP 134 /* Not supported */
  38. #endif
  39. /* for systems that do not support O_BINARY
  40. * linux being one of them */
  41. #ifndef O_BINARY
  42. #define O_BINARY 0
  43. #endif
  44. #ifndef HAVE_SYS_TIME_H
  45. #ifndef _TIMEVAL_DEFINED
  46. #define _TIMEVAL_DEFINED
  47. struct timeval {
  48. long tv_sec;
  49. long tv_usec;
  50. };
  51. #endif /* _TIMEVAL_DEFINED */
  52. #endif
  53. /* gettimeofday() */
  54. #ifndef HAVE_GETTIMEOFDAY
  55. #ifdef _WIN32
  56. struct timezone {
  57. int tz_minuteswest;
  58. int tz_dsttime;
  59. };
  60. #endif
  61. struct timezone;
  62. int gettimeofday(struct timeval *tv, struct timezone *tz);
  63. #endif
  64. #ifndef IN_REPLACEMENTS_C
  65. /**** clear_malloc & fill_malloc ****/
  66. void *clear_malloc(size_t size);
  67. void *fill_malloc(size_t size);
  68. #endif
  69. /*
  70. * Now you have 3 ways for the malloc function:
  71. *
  72. * 1. Do not change anything, use the original malloc
  73. *
  74. * 2. Use the clear_malloc function instead of the original malloc.
  75. * In this case you must use the following define:
  76. * #define malloc((_a)) clear_malloc((_a))
  77. *
  78. * 3. Use the fill_malloc function instead of the original malloc.
  79. * In this case you must use the following define:
  80. * #define malloc((_a)) fill_malloc((_a))
  81. *
  82. * We have figured out that there could exist some malloc problems
  83. * where variables are using without to be initialise. To find this
  84. * places, use the fill_malloc function. With this function we want
  85. * to initialize memory to some known bad state. This is quite easily
  86. * spotted in the debugger and will trap to an invalid address.
  87. *
  88. * clear_malloc can be used if you want to set not initialise
  89. * variable to 0.
  90. *
  91. * If you do not want to change the malloc function, to not use one of
  92. * the following macros. Which is the default way.
  93. */
  94. /* #define malloc(_a) clear_malloc(_a)
  95. * #define malloc(_a) fill_malloc(_a) */
  96. /* GNU extensions to the C library that may be missing on some systems */
  97. #ifndef HAVE_STRNDUP
  98. char *strndup(const char *s, size_t n);
  99. #endif /* HAVE_STRNDUP */
  100. #ifndef HAVE_STRNLEN
  101. size_t strnlen(const char *s, size_t maxlen);
  102. #endif /* HAVE_STRNLEN */
  103. #ifndef HAVE_USLEEP
  104. #ifdef _WIN32
  105. static inline unsigned usleep(unsigned int usecs)
  106. {
  107. Sleep((usecs/1000));
  108. return 0;
  109. }
  110. #else
  111. #error no usleep defined for your platform
  112. #endif
  113. #endif /* HAVE_USLEEP */
  114. /* Windows specific */
  115. #ifdef _WIN32
  116. #include <windows.h>
  117. #include <time.h>
  118. /* Windows does not declare sockaddr_un */
  119. #define UNIX_PATH_LEN 108
  120. struct sockaddr_un {
  121. uint16_t sun_family;
  122. char sun_path[UNIX_PATH_LEN];
  123. };
  124. /* win32 systems do not support ETIMEDOUT */
  125. #ifndef ETIMEDOUT
  126. #define ETIMEDOUT WSAETIMEDOUT
  127. #endif
  128. #if IS_MINGW == 1
  129. static inline unsigned char inb(unsigned short int port)
  130. {
  131. unsigned char _v;
  132. __asm__ __volatile__ ("inb %w1,%0" : "=a" (_v) : "Nd" (port));
  133. return _v;
  134. }
  135. static inline void outb(unsigned char value, unsigned short int port)
  136. {
  137. __asm__ __volatile__ ("outb %b0,%w1" : : "a" (value), "Nd" (port));
  138. }
  139. /* mingw does not have ffs, so use gcc builtin types */
  140. #define ffs __builtin_ffs
  141. #endif /* IS_MINGW */
  142. int win_select(int max_fd, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *tv);
  143. #endif /* _WIN32 */
  144. /* generic socket functions for Windows and Posix */
  145. static inline int write_socket(int handle, const void *buffer, unsigned int count)
  146. {
  147. #ifdef _WIN32
  148. return send(handle, buffer, count, 0);
  149. #else
  150. return write(handle, buffer, count);
  151. #endif
  152. }
  153. static inline int read_socket(int handle, void *buffer, unsigned int count)
  154. {
  155. #ifdef _WIN32
  156. return recv(handle, buffer, count, 0);
  157. #else
  158. return read(handle, buffer, count);
  159. #endif
  160. }
  161. static inline int close_socket(int sock)
  162. {
  163. #ifdef _WIN32
  164. return closesocket(sock);
  165. #else
  166. return close(sock);
  167. #endif
  168. }
  169. static inline void socket_nonblock(int fd)
  170. {
  171. #ifdef _WIN32
  172. unsigned long nonblock = 1;
  173. ioctlsocket(fd, FIONBIO, &nonblock);
  174. #else
  175. int oldopts = fcntl(fd, F_GETFL, 0);
  176. fcntl(fd, F_SETFL, oldopts | O_NONBLOCK);
  177. #endif
  178. }
  179. static inline int socket_select(int max_fd,
  180. fd_set *rfds,
  181. fd_set *wfds,
  182. fd_set *efds,
  183. struct timeval *tv)
  184. {
  185. #ifdef _WIN32
  186. return win_select(max_fd, rfds, wfds, efds, tv);
  187. #else
  188. return select(max_fd, rfds, wfds, efds, tv);
  189. #endif
  190. }
  191. #ifndef HAVE_ELF_H
  192. typedef uint32_t Elf32_Addr;
  193. typedef uint16_t Elf32_Half;
  194. typedef uint32_t Elf32_Off;
  195. typedef int32_t Elf32_Sword;
  196. typedef uint32_t Elf32_Word;
  197. typedef uint32_t Elf32_Size;
  198. typedef Elf32_Off Elf32_Hashelt;
  199. typedef struct {
  200. unsigned char e_ident[16]; /* Magic number and other info */
  201. Elf32_Half e_type; /* Object file type */
  202. Elf32_Half e_machine; /* Architecture */
  203. Elf32_Word e_version; /* Object file version */
  204. Elf32_Addr e_entry; /* Entry point virtual address */
  205. Elf32_Off e_phoff; /* Program header table file offset */
  206. Elf32_Off e_shoff; /* Section header table file offset */
  207. Elf32_Word e_flags; /* Processor-specific flags */
  208. Elf32_Half e_ehsize; /* ELF header size in bytes */
  209. Elf32_Half e_phentsize; /* Program header table entry size */
  210. Elf32_Half e_phnum; /* Program header table entry count */
  211. Elf32_Half e_shentsize; /* Section header table entry size */
  212. Elf32_Half e_shnum; /* Section header table entry count */
  213. Elf32_Half e_shstrndx; /* Section header string table index */
  214. } Elf32_Ehdr;
  215. #define ELFMAG "\177ELF"
  216. #define SELFMAG 4
  217. #define EI_CLASS 4 /* File class byte index */
  218. #define ELFCLASS32 1 /* 32-bit objects */
  219. #define ELFCLASS64 2 /* 64-bit objects */
  220. #define EI_DATA 5 /* Data encoding byte index */
  221. #define ELFDATA2LSB 1 /* 2's complement, little endian */
  222. #define ELFDATA2MSB 2 /* 2's complement, big endian */
  223. typedef struct {
  224. Elf32_Word p_type; /* Segment type */
  225. Elf32_Off p_offset; /* Segment file offset */
  226. Elf32_Addr p_vaddr; /* Segment virtual address */
  227. Elf32_Addr p_paddr; /* Segment physical address */
  228. Elf32_Size p_filesz; /* Segment size in file */
  229. Elf32_Size p_memsz; /* Segment size in memory */
  230. Elf32_Word p_flags; /* Segment flags */
  231. Elf32_Size p_align; /* Segment alignment */
  232. } Elf32_Phdr;
  233. #define PT_LOAD 1 /* Loadable program segment */
  234. #endif /* HAVE_ELF_H */
  235. #if defined HAVE_LIBUSB1 && !defined HAVE_LIBUSB_ERROR_NAME
  236. const char *libusb_error_name(int error_code);
  237. #endif /* defined HAVE_LIBUSB1 && !defined HAVE_LIBUSB_ERROR_NAME */
  238. #endif /* OPENOCD_HELPER_REPLACEMENTS_H */