OpenOCD
replacements.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
3 /***************************************************************************
4  * Copyright (C) 2006 by Dominic Rath *
5  * Dominic.Rath@gmx.de *
6  * *
7  * Copyright (C) 2007,2008 Øyvind Harboe *
8  * oyvind.harboe@zylin.com *
9  * *
10  * Copyright (C) 2008 by Spencer Oliver *
11  * spen@spen-soft.co.uk *
12  ***************************************************************************/
13 
14 #ifndef OPENOCD_HELPER_REPLACEMENTS_H
15 #define OPENOCD_HELPER_REPLACEMENTS_H
16 
17 #include <stdint.h>
18 #include <helper/system.h>
19 
20 /* MIN,MAX macros */
21 #ifndef MIN
22 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
23 #endif
24 #ifndef MAX
25 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
26 #endif
27 
28 /* for systems that do not support ENOTSUP
29  * win32 being one of them */
30 #ifndef ENOTSUP
31 #define ENOTSUP 134 /* Not supported */
32 #endif
33 
34 /* for systems that do not support O_BINARY
35  * linux being one of them */
36 #ifndef O_BINARY
37 #define O_BINARY 0
38 #endif
39 
40 #ifndef HAVE_SYS_TIME_H
41 
42 #ifndef _TIMEVAL_DEFINED
43 #define _TIMEVAL_DEFINED
44 
45 struct timeval {
46  long tv_sec;
47  long tv_usec;
48 };
49 
50 #endif /* _TIMEVAL_DEFINED */
51 
52 #endif
53 
54 /* gettimeofday() */
55 #ifndef HAVE_GETTIMEOFDAY
56 
57 #ifdef _WIN32
58 struct timezone {
59  int tz_minuteswest;
60  int tz_dsttime;
61 };
62 #endif
63 struct timezone;
64 
65 int gettimeofday(struct timeval *tv, struct timezone *tz);
66 
67 #endif
68 
69 void *clear_malloc(size_t size);
70 void *fill_malloc(size_t size);
71 
72 #ifndef IN_REPLACEMENTS_C
73 /*
74  * Now you have 3 ways for the malloc function:
75  *
76  * 1. Do not change anything, use the original malloc
77  *
78  * 2. Use the clear_malloc function instead of the original malloc.
79  * In this case you must use the following define:
80  * #define malloc((_a)) clear_malloc((_a))
81  *
82  * 3. Use the fill_malloc function instead of the original malloc.
83  * In this case you must use the following define:
84  * #define malloc((_a)) fill_malloc((_a))
85  *
86  * We have figured out that there could exist some malloc problems
87  * where variables are using without to be initialise. To find this
88  * places, use the fill_malloc function. With this function we want
89  * to initialize memory to some known bad state. This is quite easily
90  * spotted in the debugger and will trap to an invalid address.
91  *
92  * clear_malloc can be used if you want to set not initialise
93  * variable to 0.
94  *
95  * If you do not want to change the malloc function, to not use one of
96  * the following macros. Which is the default way.
97  */
98 
99 /* #define malloc(_a) clear_malloc(_a)
100  * #define malloc(_a) fill_malloc(_a) */
101 #endif /* IN_REPLACEMENTS_C */
102 
103 /* GNU extensions to the C library that may be missing on some systems */
104 #ifndef HAVE_STRNDUP
105 char *strndup(const char *s, size_t n);
106 #endif /* HAVE_STRNDUP */
107 
108 #ifndef HAVE_STRNLEN
109 size_t strnlen(const char *s, size_t maxlen);
110 #endif /* HAVE_STRNLEN */
111 
112 #ifndef HAVE_USLEEP
113 #ifdef _WIN32
114 static inline unsigned int usleep(unsigned int usecs)
115 {
116  Sleep((usecs/1000));
117  return 0;
118 }
119 #else
120 #error no usleep defined for your platform
121 #endif
122 #endif /* HAVE_USLEEP */
123 
124 /* Windows specific */
125 #ifdef _WIN32
126 
127 #include <windows.h>
128 #include <time.h>
129 
130 /* Windows does not declare sockaddr_un */
131 #define UNIX_PATH_LEN 108
132 struct sockaddr_un {
133  uint16_t sun_family;
134  char sun_path[UNIX_PATH_LEN];
135 };
136 
137 /* win32 systems do not support ETIMEDOUT */
138 
139 #ifndef ETIMEDOUT
140 #define ETIMEDOUT WSAETIMEDOUT
141 #endif
142 
143 #if IS_MINGW == 1
144 #if defined(__x86_64__) || defined(__i386__)
145 static inline unsigned char inb(unsigned short int port)
146 {
147  unsigned char _v;
148  __asm__ __volatile__ ("inb %w1,%0" : "=a" (_v) : "Nd" (port));
149  return _v;
150 }
151 
152 static inline void outb(unsigned char value, unsigned short int port)
153 {
154  __asm__ __volatile__ ("outb %b0,%w1" : : "a" (value), "Nd" (port));
155 }
156 #endif
157 
158 /* mingw does not have ffs, so use gcc builtin types */
159 #define ffs __builtin_ffs
160 
161 #endif /* IS_MINGW */
162 
163 int win_select(int max_fd, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *tv);
164 
165 #endif /* _WIN32 */
166 
167 /* Socket descriptor is unsigned on Windows.
168  * Add a cast to prevent sign mismatch warnings (-Wsign-compare). */
169 #ifdef _WIN32
170 #define OCD_FD_SET(fd, set) FD_SET((SOCKET)(fd), (set))
171 #define OCD_FD_CLR(fd, set) FD_CLR((SOCKET)(fd), (set))
172 #define OCD_FD_ISSET(fd, set) FD_ISSET((SOCKET)(fd), (set))
173 #else
174 #define OCD_FD_SET(fd, set) FD_SET((fd), (set))
175 #define OCD_FD_CLR(fd, set) FD_CLR((fd), (set))
176 #define OCD_FD_ISSET(fd, set) FD_ISSET((fd), (set))
177 #endif
178 
179 /* generic socket functions for Windows and Posix */
180 static inline int write_socket(int handle, const void *buffer, unsigned int count)
181 {
182 #ifdef _WIN32
183  return send(handle, buffer, count, 0);
184 #else
185  return write(handle, buffer, count);
186 #endif
187 }
188 
189 static inline int read_socket(int handle, void *buffer, unsigned int count)
190 {
191 #ifdef _WIN32
192  return recv(handle, buffer, count, 0);
193 #else
194  return read(handle, buffer, count);
195 #endif
196 }
197 
198 static inline int close_socket(int sock)
199 {
200 #ifdef _WIN32
201  return closesocket(sock);
202 #else
203  return close(sock);
204 #endif
205 }
206 
207 static inline void socket_block(int fd)
208 {
209 #ifdef _WIN32
210  unsigned long nonblock = 0;
211  ioctlsocket(fd, FIONBIO, &nonblock);
212 #else
213  int oldopts = fcntl(fd, F_GETFL, 0);
214  fcntl(fd, F_SETFL, oldopts & ~O_NONBLOCK);
215 #endif
216 }
217 
218 static inline void socket_nonblock(int fd)
219 {
220 #ifdef _WIN32
221  unsigned long nonblock = 1;
222  ioctlsocket(fd, FIONBIO, &nonblock);
223 #else
224  int oldopts = fcntl(fd, F_GETFL, 0);
225  fcntl(fd, F_SETFL, oldopts | O_NONBLOCK);
226 #endif
227 }
228 
229 static inline int socket_select(int max_fd,
230  fd_set *rfds,
231  fd_set *wfds,
232  fd_set *efds,
233  struct timeval *tv)
234 {
235 #ifdef _WIN32
236  return win_select(max_fd, rfds, wfds, efds, tv);
237 #else
238  return select(max_fd, rfds, wfds, efds, tv);
239 #endif
240 }
241 
242 static inline int socket_recv_timeout(int fd, unsigned long timeout_msec)
243 {
244 #ifdef _WIN32
245  DWORD timeout = timeout_msec;
246  return setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (const char *)&timeout,
247  sizeof(timeout));
248 #else
249  struct timeval tv;
250  tv.tv_sec = timeout_msec / 1000;
251  tv.tv_usec = (timeout_msec % 1000) * 1000;
252  return setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (const char *)&tv, sizeof(tv));
253 #endif
254 }
255 
256 #ifndef HAVE_ELF_H
257 
258 typedef uint32_t Elf32_Addr;
259 typedef uint16_t Elf32_Half;
260 typedef uint32_t Elf32_Off;
261 typedef uint32_t Elf32_Word;
262 typedef uint32_t Elf32_Size;
263 
264 #define EI_NIDENT 16
265 
266 typedef struct {
267  unsigned char e_ident[EI_NIDENT]; /* Magic number and other info */
268  Elf32_Half e_type; /* Object file type */
269  Elf32_Half e_machine; /* Architecture */
270  Elf32_Word e_version; /* Object file version */
271  Elf32_Addr e_entry; /* Entry point virtual address */
272  Elf32_Off e_phoff; /* Program header table file offset */
273  Elf32_Off e_shoff; /* Section header table file offset */
274  Elf32_Word e_flags; /* Processor-specific flags */
275  Elf32_Half e_ehsize; /* ELF header size in bytes */
276  Elf32_Half e_phentsize; /* Program header table entry size */
277  Elf32_Half e_phnum; /* Program header table entry count */
278  Elf32_Half e_shentsize; /* Section header table entry size */
279  Elf32_Half e_shnum; /* Section header table entry count */
280  Elf32_Half e_shstrndx; /* Section header string table index */
281 } Elf32_Ehdr;
282 
283 #define ELFMAG "\177ELF"
284 #define SELFMAG 4
285 
286 #define EI_CLASS 4 /* File class byte index */
287 #define ELFCLASS32 1 /* 32-bit objects */
288 #define ELFCLASS64 2 /* 64-bit objects */
289 
290 #define EI_DATA 5 /* Data encoding byte index */
291 #define ELFDATA2LSB 1 /* 2's complement, little endian */
292 #define ELFDATA2MSB 2 /* 2's complement, big endian */
293 
294 typedef struct {
295  Elf32_Word p_type; /* Segment type */
296  Elf32_Off p_offset; /* Segment file offset */
297  Elf32_Addr p_vaddr; /* Segment virtual address */
298  Elf32_Addr p_paddr; /* Segment physical address */
299  Elf32_Size p_filesz; /* Segment size in file */
300  Elf32_Size p_memsz; /* Segment size in memory */
301  Elf32_Word p_flags; /* Segment flags */
302  Elf32_Size p_align; /* Segment alignment */
303 } Elf32_Phdr;
304 
305 #define PT_LOAD 1 /* Loadable program segment */
306 
307 #endif /* HAVE_ELF_H */
308 
309 #ifndef HAVE_ELF64
310 
311 typedef uint64_t Elf64_Addr;
312 typedef uint16_t Elf64_Half;
313 typedef uint64_t Elf64_Off;
314 typedef uint32_t Elf64_Word;
315 typedef uint64_t Elf64_Xword;
316 
317 typedef struct {
318  unsigned char e_ident[EI_NIDENT]; /* Magic number and other info */
319  Elf64_Half e_type; /* Object file type */
320  Elf64_Half e_machine; /* Architecture */
321  Elf64_Word e_version; /* Object file version */
322  Elf64_Addr e_entry; /* Entry point virtual address */
323  Elf64_Off e_phoff; /* Program header table file offset */
324  Elf64_Off e_shoff; /* Section header table file offset */
325  Elf64_Word e_flags; /* Processor-specific flags */
326  Elf64_Half e_ehsize; /* ELF header size in bytes */
327  Elf64_Half e_phentsize; /* Program header table entry size */
328  Elf64_Half e_phnum; /* Program header table entry count */
329  Elf64_Half e_shentsize; /* Section header table entry size */
330  Elf64_Half e_shnum; /* Section header table entry count */
331  Elf64_Half e_shstrndx; /* Section header string table index */
332 } Elf64_Ehdr;
333 
334 typedef struct {
335  Elf64_Word p_type; /* Segment type */
336  Elf64_Word p_flags; /* Segment flags */
337  Elf64_Off p_offset; /* Segment file offset */
338  Elf64_Addr p_vaddr; /* Segment virtual address */
339  Elf64_Addr p_paddr; /* Segment physical address */
340  Elf64_Xword p_filesz; /* Segment size in file */
341  Elf64_Xword p_memsz; /* Segment size in memory */
342  Elf64_Xword p_align; /* Segment alignment */
343 } Elf64_Phdr;
344 
345 #endif /* HAVE_ELF64 */
346 
347 #endif /* OPENOCD_HELPER_REPLACEMENTS_H */
uint64_t buffer
Pointer to data buffer to send over SPI.
Definition: dw-spi-helper.h:0
uint32_t size
Size of dw_spi_transaction::buffer.
Definition: dw-spi-helper.h:4
static int socket_select(int max_fd, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *tv)
Definition: replacements.h:229
uint16_t Elf32_Half
Definition: replacements.h:259
uint32_t Elf32_Addr
Definition: replacements.h:258
static void socket_block(int fd)
Definition: replacements.h:207
static int read_socket(int handle, void *buffer, unsigned int count)
Definition: replacements.h:189
uint64_t Elf64_Xword
Definition: replacements.h:315
static int socket_recv_timeout(int fd, unsigned long timeout_msec)
Definition: replacements.h:242
static int close_socket(int sock)
Definition: replacements.h:198
uint32_t Elf32_Off
Definition: replacements.h:260
uint64_t Elf64_Off
Definition: replacements.h:313
char * strndup(const char *s, size_t n)
Definition: replacements.c:116
static int write_socket(int handle, const void *buffer, unsigned int count)
Definition: replacements.h:180
uint32_t Elf64_Word
Definition: replacements.h:314
void * clear_malloc(size_t size)
Definition: replacements.c:31
int gettimeofday(struct timeval *tv, struct timezone *tz)
uint32_t Elf32_Size
Definition: replacements.h:262
uint16_t Elf64_Half
Definition: replacements.h:312
#define EI_NIDENT
Definition: replacements.h:264
static void socket_nonblock(int fd)
Definition: replacements.h:218
uint64_t Elf64_Addr
Definition: replacements.h:311
void * fill_malloc(size_t size)
Definition: replacements.c:39
uint32_t Elf32_Word
Definition: replacements.h:261
size_t strnlen(const char *s, size_t maxlen)
Definition: replacements.c:108
Elf32_Off e_shoff
Definition: replacements.h:273
Elf32_Half e_ehsize
Definition: replacements.h:275
Elf32_Half e_shnum
Definition: replacements.h:279
Elf32_Half e_machine
Definition: replacements.h:269
Elf32_Off e_phoff
Definition: replacements.h:272
Elf32_Half e_phnum
Definition: replacements.h:277
Elf32_Half e_shstrndx
Definition: replacements.h:280
Elf32_Half e_type
Definition: replacements.h:268
Elf32_Word e_flags
Definition: replacements.h:274
Elf32_Word e_version
Definition: replacements.h:270
Elf32_Half e_shentsize
Definition: replacements.h:278
Elf32_Addr e_entry
Definition: replacements.h:271
Elf32_Half e_phentsize
Definition: replacements.h:276
Elf32_Addr p_vaddr
Definition: replacements.h:297
Elf32_Size p_align
Definition: replacements.h:302
Elf32_Word p_flags
Definition: replacements.h:301
Elf32_Size p_memsz
Definition: replacements.h:300
Elf32_Word p_type
Definition: replacements.h:295
Elf32_Size p_filesz
Definition: replacements.h:299
Elf32_Off p_offset
Definition: replacements.h:296
Elf32_Addr p_paddr
Definition: replacements.h:298
Elf64_Half e_type
Definition: replacements.h:319
Elf64_Half e_shentsize
Definition: replacements.h:329
Elf64_Half e_shnum
Definition: replacements.h:330
Elf64_Word e_version
Definition: replacements.h:321
Elf64_Half e_ehsize
Definition: replacements.h:326
Elf64_Off e_shoff
Definition: replacements.h:324
Elf64_Addr e_entry
Definition: replacements.h:322
Elf64_Half e_phentsize
Definition: replacements.h:327
Elf64_Off e_phoff
Definition: replacements.h:323
Elf64_Half e_machine
Definition: replacements.h:320
Elf64_Word e_flags
Definition: replacements.h:325
Elf64_Half e_shstrndx
Definition: replacements.h:331
Elf64_Half e_phnum
Definition: replacements.h:328
Elf64_Xword p_memsz
Definition: replacements.h:341
Elf64_Addr p_vaddr
Definition: replacements.h:338
Elf64_Addr p_paddr
Definition: replacements.h:339
Elf64_Off p_offset
Definition: replacements.h:337
Elf64_Xword p_align
Definition: replacements.h:342
Elf64_Word p_flags
Definition: replacements.h:336
Elf64_Word p_type
Definition: replacements.h:335
Elf64_Xword p_filesz
Definition: replacements.h:340
Definition: psoc6.c:83
long tv_sec
Definition: replacements.h:46
long tv_usec
Definition: replacements.h:47
uint8_t count[4]
Definition: vdebug.c:22
#define DWORD
Definition: x86_32_common.h:33