]> cvs.zerfleddert.de Git - proxmark3-svn/blame - armsrc/util.c
chg: moved to header file
[proxmark3-svn] / armsrc / util.c
CommitLineData
e30c654b 1//-----------------------------------------------------------------------------
e30c654b 2// Jonathan Westhues, Sept 2005
bd20f8f4 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.
e30c654b 9//-----------------------------------------------------------------------------
f7e3ed82 10#include "util.h"
e30c654b 11
f38a1528 12void print_result(char *name, uint8_t *buf, size_t len) {
41863885 13 uint8_t *p = buf;
f38a1528 14
41863885 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",
f38a1528 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]
41863885 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 }
f38a1528 32}
33
195af472 34size_t nbytes(size_t nbits) {
665775c8 35 return (nbits >> 3)+((nbits % 8) > 0);
195af472 36}
37
81cd0474 38uint32_t SwapBits(uint32_t value, int nrbits) {
81cd0474 39 uint32_t newvalue = 0;
5192a0a6 40 for(int i = 0; i < nrbits; i++) {
81cd0474 41 newvalue ^= ((value >> i) & 1) << (nrbits - 1 - i);
42 }
43 return newvalue;
44}
45
ab3af4fe 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*/
51uint32_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);
5d15891e 58 t >>= 1;
ab3af4fe 59 }
60 return v;
61}
62
41863885 63void num_to_bytes(uint64_t n, size_t len, uint8_t* dest) {
e30c654b 64 while (len--) {
f7e3ed82 65 dest[len] = (uint8_t) n;
e30c654b 66 n >>= 8;
67 }
68}
69
41863885 70uint64_t bytes_to_num(uint8_t* src, size_t len) {
e30c654b 71 uint64_t num = 0;
2abdfa49 72 while (len--) {
e30c654b 73 num = (num << 8) | (*src);
74 src++;
75 }
76 return num;
77}
78
f38a1528 79// RotateLeft - Ultralight, Desfire
41863885 80void rol(uint8_t *data, const size_t len) {
f38a1528 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}
dd79e03a 87
f38a1528 88void 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
ab3af4fe 95int32_t le24toh (uint8_t data[3]) {
f38a1528 96 return (data[2] << 16) | (data[1] << 8) | data[0];
97}
98
ab3af4fe 99void LEDsoff() {
e30c654b 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]
ab3af4fe 107void LED(int led, int ms) {
e30c654b 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
e30c654b 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
ab3af4fe 136int BUTTON_CLICKED(int ms) {
e30c654b 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
f7e3ed82 151 uint16_t start = AT91C_BASE_PWMC_CH0->PWMC_CCNTR;
e30c654b 152
153 int letoff = 0;
154 for(;;)
155 {
f7e3ed82 156 uint16_t now = AT91C_BASE_PWMC_CH0->PWMC_CCNTR;
e30c654b 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?
f7e3ed82 174 if (now == (uint16_t)(start + ticks))
e30c654b 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
f7e3ed82 186 if (now == (uint16_t)(start + ticks))
e30c654b 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
ab3af4fe 198int BUTTON_HELD(int ms) {
e30c654b 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
f7e3ed82 213 uint16_t start = AT91C_BASE_PWMC_CH0->PWMC_CCNTR;
e30c654b 214
215 for(;;)
216 {
f7e3ed82 217 uint16_t now = AT91C_BASE_PWMC_CH0->PWMC_CCNTR;
e30c654b 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
f7e3ed82 225 if (now == (uint16_t)(start + ticks))
e30c654b 226 return BUTTON_HOLD;
227
228 WDT_HIT();
229 }
230
231 // We should never get here
232 return BUTTON_ERROR;
233}
234
e30c654b 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 */
ab3af4fe 240void FormatVersionInformation(char *dst, int len, const char *prefix, void *version_information) {
e30c654b 241 struct version_information *v = (struct version_information*)version_information;
242 dst[0] = 0;
a61b4976 243 strncat(dst, prefix, len-1);
e30c654b 244 if(v->magic != VERSION_INFORMATION_MAGIC) {
9783989b 245 strncat(dst, "Missing/Invalid version information\n", len - strlen(dst) - 1);
e30c654b 246 return;
247 }
248 if(v->versionversion != 1) {
9783989b 249 strncat(dst, "Version information not understood\n", len - strlen(dst) - 1);
e30c654b 250 return;
251 }
252 if(!v->present) {
9783989b 253 strncat(dst, "Version information not available\n", len - strlen(dst) - 1);
e30c654b 254 return;
255 }
256
cba867f2 257 strncat(dst, v->gitversion, len - strlen(dst) - 1);
e30c654b 258 if(v->clean == 0) {
cba867f2 259 strncat(dst, "-unclean", len - strlen(dst) - 1);
e30c654b 260 } else if(v->clean == 2) {
cba867f2 261 strncat(dst, "-suspect", len - strlen(dst) - 1);
e30c654b 262 }
263
cba867f2
MHS
264 strncat(dst, " ", len - strlen(dst) - 1);
265 strncat(dst, v->buildtime, len - strlen(dst) - 1);
9783989b 266 strncat(dst, "\n", len - strlen(dst) - 1);
e30c654b 267}
Impressum, Datenschutz