]> cvs.zerfleddert.de Git - proxmark3-svn/blob - armsrc/util.c
mifare tools update
[proxmark3-svn] / armsrc / util.c
1 //-----------------------------------------------------------------------------
2 // Utility functions used in many places, not specific to any piece of code.
3 // Jonathan Westhues, Sept 2005
4 //-----------------------------------------------------------------------------
5 #include <proxmark3.h>
6 #include "apps.h"
7
8 void *memcpy(void *dest, const void *src, int len)
9 {
10 BYTE *d = dest;
11 const BYTE *s = src;
12 while((len--) > 0) {
13 *d = *s;
14 d++;
15 s++;
16 }
17 return dest;
18 }
19
20 void *memset(void *dest, int c, int len)
21 {
22 BYTE *d = dest;
23 while((len--) > 0) {
24 *d = c;
25 d++;
26 }
27 return dest;
28 }
29
30 int memcmp(const void *av, const void *bv, int len)
31 {
32 const BYTE *a = av;
33 const BYTE *b = bv;
34
35 while((len--) > 0) {
36 if(*a != *b) {
37 return *a - *b;
38 }
39 a++;
40 b++;
41 }
42 return 0;
43 }
44
45 int strlen(char *str)
46 {
47 int l = 0;
48 while(*str) {
49 l++;
50 str++;
51 }
52 return l;
53 }
54
55 char* strncat(char *dest, const char *src, unsigned int n)
56 {
57 unsigned int dest_len = strlen(dest);
58 unsigned int i;
59
60 for (i = 0 ; i < n && src[i] != '\0' ; i++)
61 dest[dest_len + i] = src[i];
62 dest[dest_len + i] = '\0';
63
64 return dest;
65 }
66
67 void num_to_bytes(uint64_t n, size_t len, byte_t* dest)
68 {
69 while (len--) {
70 dest[len] = (byte_t) n;
71 n >>= 8;
72 }
73 }
74
75 uint64_t bytes_to_num(byte_t* src, size_t len)
76 {
77 uint64_t num = 0;
78 while (len--)
79 {
80 num = (num << 8) | (*src);
81 src++;
82 }
83 return num;
84 }
85
86 void LEDsoff()
87 {
88 LED_A_OFF();
89 LED_B_OFF();
90 LED_C_OFF();
91 LED_D_OFF();
92 }
93
94 // LEDs: R(C) O(A) G(B) -- R(D) [1, 2, 4 and 8]
95 void LED(int led, int ms)
96 {
97 if (led & LED_RED)
98 LED_C_ON();
99 if (led & LED_ORANGE)
100 LED_A_ON();
101 if (led & LED_GREEN)
102 LED_B_ON();
103 if (led & LED_RED2)
104 LED_D_ON();
105
106 if (!ms)
107 return;
108
109 SpinDelay(ms);
110
111 if (led & LED_RED)
112 LED_C_OFF();
113 if (led & LED_ORANGE)
114 LED_A_OFF();
115 if (led & LED_GREEN)
116 LED_B_OFF();
117 if (led & LED_RED2)
118 LED_D_OFF();
119 }
120
121
122 // Determine if a button is double clicked, single clicked,
123 // not clicked, or held down (for ms || 1sec)
124 // In general, don't use this function unless you expect a
125 // double click, otherwise it will waste 500ms -- use BUTTON_HELD instead
126 int BUTTON_CLICKED(int ms)
127 {
128 // Up to 500ms in between clicks to mean a double click
129 int ticks = (48000 * (ms ? ms : 1000)) >> 10;
130
131 // If we're not even pressed, forget about it!
132 if (!BUTTON_PRESS())
133 return BUTTON_NO_CLICK;
134
135 // Borrow a PWM unit for my real-time clock
136 AT91C_BASE_PWMC->PWMC_ENA = PWM_CHANNEL(0);
137 // 48 MHz / 1024 gives 46.875 kHz
138 AT91C_BASE_PWMC_CH0->PWMC_CMR = PWM_CH_MODE_PRESCALER(10);
139 AT91C_BASE_PWMC_CH0->PWMC_CDTYR = 0;
140 AT91C_BASE_PWMC_CH0->PWMC_CPRDR = 0xffff;
141
142 WORD start = AT91C_BASE_PWMC_CH0->PWMC_CCNTR;
143
144 int letoff = 0;
145 for(;;)
146 {
147 WORD now = AT91C_BASE_PWMC_CH0->PWMC_CCNTR;
148
149 // We haven't let off the button yet
150 if (!letoff)
151 {
152 // We just let it off!
153 if (!BUTTON_PRESS())
154 {
155 letoff = 1;
156
157 // reset our timer for 500ms
158 start = AT91C_BASE_PWMC_CH0->PWMC_CCNTR;
159 ticks = (48000 * (500)) >> 10;
160 }
161
162 // Still haven't let it off
163 else
164 // Have we held down a full second?
165 if (now == (WORD)(start + ticks))
166 return BUTTON_HOLD;
167 }
168
169 // We already let off, did we click again?
170 else
171 // Sweet, double click!
172 if (BUTTON_PRESS())
173 return BUTTON_DOUBLE_CLICK;
174
175 // Have we ran out of time to double click?
176 else
177 if (now == (WORD)(start + ticks))
178 // At least we did a single click
179 return BUTTON_SINGLE_CLICK;
180
181 WDT_HIT();
182 }
183
184 // We should never get here
185 return BUTTON_ERROR;
186 }
187
188 // Determine if a button is held down
189 int BUTTON_HELD(int ms)
190 {
191 // If button is held for one second
192 int ticks = (48000 * (ms ? ms : 1000)) >> 10;
193
194 // If we're not even pressed, forget about it!
195 if (!BUTTON_PRESS())
196 return BUTTON_NO_CLICK;
197
198 // Borrow a PWM unit for my real-time clock
199 AT91C_BASE_PWMC->PWMC_ENA = PWM_CHANNEL(0);
200 // 48 MHz / 1024 gives 46.875 kHz
201 AT91C_BASE_PWMC_CH0->PWMC_CMR = PWM_CH_MODE_PRESCALER(10);
202 AT91C_BASE_PWMC_CH0->PWMC_CDTYR = 0;
203 AT91C_BASE_PWMC_CH0->PWMC_CPRDR = 0xffff;
204
205 WORD start = AT91C_BASE_PWMC_CH0->PWMC_CCNTR;
206
207 for(;;)
208 {
209 WORD now = AT91C_BASE_PWMC_CH0->PWMC_CCNTR;
210
211 // As soon as our button let go, we didn't hold long enough
212 if (!BUTTON_PRESS())
213 return BUTTON_SINGLE_CLICK;
214
215 // Have we waited the full second?
216 else
217 if (now == (WORD)(start + ticks))
218 return BUTTON_HOLD;
219
220 WDT_HIT();
221 }
222
223 // We should never get here
224 return BUTTON_ERROR;
225 }
226
227 // attempt at high resolution microsecond timer
228 // beware: timer counts in 21.3uS increments (1024/48Mhz)
229 void SpinDelayUs(int us)
230 {
231 int ticks = (48*us) >> 10;
232
233 // Borrow a PWM unit for my real-time clock
234 AT91C_BASE_PWMC->PWMC_ENA = PWM_CHANNEL(0);
235 // 48 MHz / 1024 gives 46.875 kHz
236 AT91C_BASE_PWMC_CH0->PWMC_CMR = PWM_CH_MODE_PRESCALER(10);
237 AT91C_BASE_PWMC_CH0->PWMC_CDTYR = 0;
238 AT91C_BASE_PWMC_CH0->PWMC_CPRDR = 0xffff;
239
240 WORD start = AT91C_BASE_PWMC_CH0->PWMC_CCNTR;
241
242 for(;;) {
243 WORD now = AT91C_BASE_PWMC_CH0->PWMC_CCNTR;
244 if (now == (WORD)(start + ticks))
245 return;
246
247 WDT_HIT();
248 }
249 }
250
251 void SpinDelay(int ms)
252 {
253 // convert to uS and call microsecond delay function
254 SpinDelayUs(ms*1000);
255 }
256
257 /* Similar to FpgaGatherVersion this formats stored version information
258 * into a string representation. It takes a pointer to the struct version_information,
259 * verifies the magic properties, then stores a formatted string, prefixed by
260 * prefix in dst.
261 */
262 void FormatVersionInformation(char *dst, int len, const char *prefix, void *version_information)
263 {
264 struct version_information *v = (struct version_information*)version_information;
265 dst[0] = 0;
266 strncat(dst, prefix, len);
267 if(v->magic != VERSION_INFORMATION_MAGIC) {
268 strncat(dst, "Missing/Invalid version information", len);
269 return;
270 }
271 if(v->versionversion != 1) {
272 strncat(dst, "Version information not understood", len);
273 return;
274 }
275 if(!v->present) {
276 strncat(dst, "Version information not available", len);
277 return;
278 }
279
280 strncat(dst, v->svnversion, len);
281 if(v->clean == 0) {
282 strncat(dst, "-unclean", len);
283 } else if(v->clean == 2) {
284 strncat(dst, "-suspect", len);
285 }
286
287 strncat(dst, " ", len);
288 strncat(dst, v->buildtime, len);
289 }
Impressum, Datenschutz