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