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