- renamed M5960 USB JTAG to "flyswatter"
[fw/openocd] / src / helper / replacements.h
1 /***************************************************************************
2  *   Copyright (C) 2006 by Dominic Rath                                    *
3  *   Dominic.Rath@gmx.de                                                   *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20 #ifndef REPLACEMENTS_H
21 #define REPLACEMENTS_H
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include "types.h"
28
29 /* include necessary headers for socket functionality */
30 #ifdef _WIN32
31 #include <winsock2.h>
32 #else
33 #include <sys/socket.h>
34 #include <sys/poll.h>
35 #include <netinet/in.h>
36 #include <unistd.h>
37 #include <fcntl.h>
38 #endif
39
40 #ifdef HAVE_SYS_PARAM_H
41 #include <sys/param.h> /* for MIN/MAX macros */
42 #endif
43
44 /* MIN,MAX macros */
45 #ifndef MIN
46 #define MIN(a,b) (((a)<(b))?(a):(b))
47 #endif
48 #ifndef MAX
49 #define MAX(a,b) (((a)>(b))?(a):(b))
50 #endif
51                                                                                                                                  
52 /* gettimeofday() */
53 #ifndef HAVE_GETTIMEOFDAY
54
55 #ifndef _TIMEVAL_DEFINED
56 #define _TIMEVAL_DEFINED
57
58 struct timeval {
59         long tv_sec;
60         long tv_usec;
61 };
62 #endif /* _TIMEVAL_DEFINED */
63
64 struct timezone {
65     int tz_minuteswest;
66         int tz_dsttime;
67 };
68
69 extern int gettimeofday(struct timeval *tv, struct timezone *tz);
70 #endif
71
72 /* GNU extensions to the C library that may be missing on some systems */
73 #ifndef HAVE_STRNDUP
74 extern char* strndup(const char *s, size_t n);
75 #endif /* HAVE_STRNDUP */
76
77 #ifndef HAVE_STRNLEN
78 extern size_t strnlen(const char *s, size_t maxlen);
79 #endif /* HAVE_STRNLEN */
80
81 #ifndef HAVE_USLEEP
82 static __inline unsigned usleep(unsigned int usecs)
83 {
84 #ifdef _WIN32
85         Sleep((usecs/1000));
86         return 0;
87 #else
88 #error no usleep defined for your platform
89 #endif
90 }
91 #endif /* HAVE_USLEEP */
92
93 /* Windows specific */
94 #ifdef _WIN32
95
96 #define WIN32_LEAN_AND_MEAN
97 #include <windows.h>
98 #include <time.h>
99
100 #undef ERROR
101
102 #if IS_MINGW == 1
103 static __inline unsigned char inb(unsigned short int port)
104 {
105         unsigned char _v;
106         __asm__ __volatile__ ("inb %w1,%0":"=a" (_v):"Nd" (port));
107         return _v;
108 }
109
110 static __inline void outb(unsigned char value, unsigned short int port)
111 {
112         __asm__ __volatile__ ("outb %b0,%w1": :"a" (value), "Nd" (port));
113 }
114
115 #endif /* IS_MINGW */
116 #endif  /* _WIN32 */
117
118 /* generic socket functions for Windows and Posix */
119 static __inline int write_socket( int handle, const void *buffer, unsigned int count )
120 {
121 #ifdef _WIN32
122     return send(handle, buffer, count, 0);
123 #else
124     return write(handle, buffer, count);
125 #endif
126 }
127
128 static __inline int read_socket( int handle, void *buffer, unsigned int count )
129 {
130 #ifdef _WIN32
131     return recv(handle, buffer, count, 0);
132 #else
133     return read(handle, buffer, count);
134 #endif
135 }
136
137 static __inline int close_socket(int sock)
138 {
139 #ifdef _WIN32
140     return closesocket(sock);
141 #else
142     return close(sock);
143 #endif
144 }
145
146 static __inline void socket_nonblock(int fd)
147 {
148 #ifdef _WIN32
149         long nonblock = 1;
150         ioctlsocket(fd, FIONBIO, &nonblock );
151 #else
152         int oldopts = fcntl(fd, F_GETFL, 0);
153         fcntl(fd, F_SETFL, oldopts | O_NONBLOCK);
154 #endif
155 }
156
157 #ifndef HAVE_ELF_H
158
159 typedef struct
160 {
161         unsigned char   e_ident[16];    /* Magic number and other info */
162         u16     e_type;                 /* Object file type */
163         u16     e_machine;              /* Architecture */
164         u32     e_version;              /* Object file version */
165         u32 e_entry;            /* Entry point virtual address */
166         u32 e_phoff;            /* Program header table file offset */
167         u32     e_shoff;                /* Section header table file offset */
168         u32     e_flags;                /* Processor-specific flags */
169         u16     e_ehsize;               /* ELF header size in bytes */
170         u16     e_phentsize;            /* Program header table entry size */
171         u16     e_phnum;                /* Program header table entry count */
172         u16     e_shentsize;            /* Section header table entry size */
173         u16     e_shnum;                /* Section header table entry count */
174         u16     e_shstrndx;             /* Section header string table index */
175 } Elf32_Ehdr;
176
177 #define ELFMAG          "\177ELF"
178 #define SELFMAG         4
179
180 #define EI_CLASS        4               /* File class byte index */
181 #define ELFCLASS32      1               /* 32-bit objects */
182 #define ELFCLASS64      2               /* 64-bit objects */
183
184 #define EI_DATA         5               /* Data encoding byte index */
185 #define ELFDATA2LSB     1               /* 2's complement, little endian */
186 #define ELFDATA2MSB     2               /* 2's complement, big endian */
187
188 typedef struct
189 {
190         u32 p_type;                     /* Segment type */
191         u32 p_offset;           /* Segment file offset */
192         u32 p_vaddr;            /* Segment virtual address */
193         u32 p_paddr;            /* Segment physical address */
194         u32 p_filesz;           /* Segment size in file */
195         u32 p_memsz;            /* Segment size in memory */
196         u32 p_flags;            /* Segment flags */
197         u32 p_align;            /* Segment alignment */
198 } Elf32_Phdr;
199
200 #define PT_LOAD         1               /* Loadable program segment */
201
202 #endif /* HAVE_ELF_H */
203
204 #endif /* REPLACEMENTS_H */