Mercurial > pihelp
annotate main.c @ 46:b1c27f1d6289
bootid hmac challenge
prog hmac
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Sun, 30 Jun 2013 23:34:24 +0800 |
parents | a0f2fcc6d9dd |
children | 747695bd4e0d |
rev | line source |
---|---|
0 | 1 #include <stdio.h> |
2 #include <string.h> | |
3 #include <stddef.h> | |
4 #include <stdbool.h> | |
5 #include <stdlib.h> | |
6 #include <avr/io.h> | |
7 #include <avr/interrupt.h> | |
8 #include <avr/sleep.h> | |
9 #include <util/delay.h> | |
10 #include <avr/pgmspace.h> | |
11 #include <avr/eeprom.h> | |
12 #include <avr/wdt.h> | |
13 #include <util/atomic.h> | |
14 #include <util/crc16.h> | |
15 | |
45 | 16 #include "buildid.h" |
17 | |
2 | 18 #include "hmac-sha1.h" |
5 | 19 #include "aes.h" |
2 | 20 |
43 | 21 #include "fat.h" |
22 #include "fat_config.h" | |
23 #include "partition.h" | |
24 #include "sd_raw.h" | |
25 #include "sd_raw_config.h" | |
26 | |
1 | 27 //#include "simple_ds18b20.h" |
28 //#include "onewire.h" | |
0 | 29 |
20 | 30 // not set via bootloader |
31 //LOCKBITS = (LB_MODE_3 & BLB0_MODE_4 & BLB1_MODE_4); | |
5 | 32 |
0 | 33 #define MIN(X,Y) ((X) < (Y) ? (X) : (Y)) |
34 #define MAX(X,Y) ((X) > (Y) ? (X) : (Y)) | |
35 | |
36 // TICK should be 8 or less (8 untested). all timers need | |
37 // to be a multiple. | |
38 | |
1 | 39 #define TICK 1 |
17 | 40 #define SLEEP_COMPARE (F_CPU/256) // == 19200 for 4915200mhz |
5 | 41 #define NKEYS 10 |
42 #define HMACLEN 20 | |
43 #define AESLEN 16 | |
44 #define KEYLEN HMACLEN | |
45 | 45 // 64 bits is enough for a realtime challenge |
46 #define CHALLEN 8 | |
1 | 47 |
27 | 48 #define BAUD 115200 |
20 | 49 #define UBRR ((F_CPU)/(16*(BAUD))-1) |
0 | 50 |
2 | 51 #define PORT_PI_BOOT PORTD |
52 #define DDR_PI_BOOT DDRD | |
4 | 53 #define PIN_PI_BOOT PD5 |
2 | 54 |
55 #define PORT_PI_RESET PORTD | |
56 #define DDR_PI_RESET DDRD | |
57 #define PIN_PI_RESET PD6 | |
58 | |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
59 #define PORT_PI_WARNING PORTD |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
60 #define DDR_PI_WARNING DDRD |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
61 #define PIN_PI_WARNING PD7 |
0 | 62 |
63 // #define HAVE_UART_ECHO | |
64 | |
7 | 65 // stores a value of clock_epoch combined with the remainder of TCNT1, |
0 | 66 // for 1/32 second accuracy |
67 struct epoch_ticks | |
68 { | |
69 uint32_t ticks; | |
70 // remainder | |
20 | 71 uint16_t rem; |
0 | 72 }; |
73 | |
16 | 74 #define WATCHDOG_LONG_MIN (60L*40) // 40 mins |
75 #define WATCHDOG_LONG_MAX (60L*60*72) // 72 hours | |
76 #define WATCHDOG_LONG_DEFAULT (60L*60*6) // 6 hours | |
77 | |
78 #define WATCHDOG_SHORT_MIN (60L*15) // 15 mins | |
79 | |
80 #define NEWBOOT_DEFAULT (60*10) // 10 minutes | |
81 #define NEWBOOT_MIN (60*2) // 2 minutes | |
82 #define NEWBOOT_MAX (60*30) // 30 mins | |
83 | |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
84 #define WARNING_TIME 10 |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
85 |
1 | 86 // eeprom-settable parameters, default values defined here. |
87 // all timeouts should be a multiple of TICK | |
16 | 88 static uint32_t watchdog_long_limit = WATCHDOG_LONG_DEFAULT; |
1 | 89 static uint32_t watchdog_short_limit = 0; |
16 | 90 static uint32_t newboot_limit = NEWBOOT_DEFAULT; |
0 | 91 |
1 | 92 // avr proves itself |
2 | 93 static uint8_t avr_keys[NKEYS][KEYLEN] = {{0}}; |
0 | 94 |
95 // ---- Atomic guards required accessing these variables | |
96 // clock_epoch in seconds | |
97 static uint32_t clock_epoch; | |
1 | 98 // watchdog counts up |
99 static uint32_t watchdog_long_count; | |
100 static uint32_t watchdog_short_count; | |
2 | 101 // newboot counts down |
1 | 102 static uint32_t newboot_count; |
2 | 103 // oneshot counts down |
104 static uint32_t oneshot_count; | |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
105 // countdown after the warning. |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
106 static uint8_t reboot_count; |
27 | 107 // set by adc completion interrupt |
108 static uint8_t adc_done; | |
2 | 109 |
0 | 110 // ---- End atomic guards required |
111 | |
112 // boolean flags | |
1 | 113 static uint8_t watchdog_long_hit; |
114 static uint8_t watchdog_short_hit; | |
115 static uint8_t newboot_hit; | |
2 | 116 static uint8_t oneshot_hit; |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
117 static uint8_t reboot_hit; |
0 | 118 |
22 | 119 // informational for status messages |
120 static uint8_t boot_normal_status; | |
121 | |
15
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
122 // flips between 0 and 1 each watchdog_long_hit, so eventually a |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
123 // working firmware should boot. set back to 0 for each 'alive' |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
124 // command |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
125 static uint8_t long_reboot_mode = 0; |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
126 |
45 | 127 static uint8_t boot_id_set = 0; |
128 static uint8_t boot_id[HMACLEN]; | |
129 | |
0 | 130 static uint8_t readpos; |
20 | 131 static char readbuf[150]; |
0 | 132 static uint8_t have_cmd; |
133 | |
134 int uart_putchar(char c, FILE *stream); | |
22 | 135 static void long_delay(uint16_t ms); |
0 | 136 static void blink(); |
137 static uint16_t adc_vcc(); | |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
138 static uint16_t adc_5v(uint16_t vcc); |
20 | 139 static uint16_t adc_temp(); |
2 | 140 static void set_pi_boot_normal(uint8_t normal); |
45 | 141 static void adc_random(uint8_t admux, |
142 uint8_t *out, uint16_t num, uint32_t *tries); | |
17 | 143 |
0 | 144 static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL, |
145 _FDEV_SETUP_WRITE); | |
146 | |
147 // thanks to http://projectgus.com/2010/07/eeprom-access-with-arduino/ | |
148 #define eeprom_read_to(dst_p, eeprom_field, dst_size) eeprom_read_block((dst_p), (void *)offsetof(struct __eeprom_data, eeprom_field), (dst_size)) | |
149 #define eeprom_read(dst, eeprom_field) eeprom_read_to((&dst), eeprom_field, sizeof(dst)) | |
150 #define eeprom_write_from(src_p, eeprom_field, src_size) eeprom_write_block((src_p), (void *)offsetof(struct __eeprom_data, eeprom_field), (src_size)) | |
151 #define eeprom_write(src, eeprom_field) { eeprom_write_from(&src, eeprom_field, sizeof(src)); } | |
152 | |
1 | 153 #define EXPECT_MAGIC 0xdf83 |
0 | 154 |
155 struct __attribute__ ((__packed__)) __eeprom_data { | |
1 | 156 uint32_t watchdog_long_limit; |
157 uint32_t watchdog_short_limit; | |
158 uint32_t newboot_limit; | |
0 | 159 |
2 | 160 uint8_t avr_keys[NKEYS][KEYLEN]; |
0 | 161 |
162 uint16_t magic; | |
163 }; | |
164 | |
165 // Very first setup | |
166 static void | |
167 setup_chip() | |
168 { | |
169 cli(); | |
170 | |
15
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
171 // set to 8 seconds, in case sha1 is slow etc. |
1 | 172 wdt_enable(WDTO_8S); |
173 | |
15
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
174 // Set scaler to /1, -> clock to 8mhz |
0 | 175 CLKPR = _BV(CLKPCE); |
15
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
176 CLKPR = 0; |
0 | 177 |
178 // enable pullups | |
1 | 179 // XXX matt pihelp |
5 | 180 //PORTB = 0xff; // XXX change when using SPI |
181 //PORTD = 0xff; | |
182 //PORTC = 0xff; | |
0 | 183 |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
184 DDR_PI_WARNING |= _BV(PIN_PI_WARNING); |
0 | 185 |
41 | 186 DDR_PI_RESET &= ~_BV(PIN_PI_RESET); |
187 | |
5 | 188 #if 0 |
0 | 189 // set pullup |
190 PORTD |= _BV(PD2); | |
191 // INT0 setup | |
192 EICRA = (1<<ISC01); // falling edge - data sheet says it won't work? | |
193 EIMSK = _BV(INT0); | |
5 | 194 #endif |
0 | 195 |
196 // comparator disable | |
197 ACSR = _BV(ACD); | |
198 | |
199 // disable adc pin input buffers | |
200 DIDR0 = 0x3F; // acd0-adc5 | |
201 DIDR1 = (1<<AIN1D)|(1<<AIN0D); // ain0/ain1 | |
202 | |
203 sei(); | |
204 } | |
205 | |
206 static void | |
207 get_epoch_ticks(struct epoch_ticks *t) | |
208 { | |
209 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) | |
210 { | |
211 t->ticks = clock_epoch; | |
7 | 212 t->rem = TCNT1; |
0 | 213 } |
214 } | |
215 | |
216 static void | |
217 setup_tick_counter() | |
218 { | |
2 | 219 // set up counter1 |
220 | |
0 | 221 // set up counter2. |
222 // COM21 COM20 Set OC2 on Compare Match (p116) | |
223 // WGM21 Clear counter on compare | |
224 //TCCR2A = _BV(COM2A1) | _BV(COM2A0) | _BV(WGM21); | |
225 // toggle on match | |
2 | 226 TCCR1A = _BV(COM1A0); |
8 | 227 #ifdef SIM_DEBUG |
228 // systemclock/8 | |
229 TCCR1B = _BV(CS11); | |
230 #else | |
20 | 231 // systemclock/256 |
232 TCCR1B = _BV(CS12); | |
8 | 233 #endif |
2 | 234 TCNT1 = 0; |
235 OCR1A = SLEEP_COMPARE; | |
0 | 236 // interrupt |
2 | 237 TIMSK1 = _BV(OCIE1A); |
0 | 238 } |
239 | |
240 static void | |
241 uart_on() | |
242 { | |
243 // Power reduction register | |
244 PRR &= ~_BV(PRUSART0); | |
245 | |
246 // All of this needs to be done each time after turning off the PRR | |
247 // baud rate | |
248 UBRR0H = (unsigned char)(UBRR >> 8); | |
249 UBRR0L = (unsigned char)UBRR; | |
250 UCSR0B = _BV(RXCIE0) | _BV(RXEN0) | _BV(TXEN0); | |
251 //8N1 | |
252 UCSR0C = _BV(UCSZ01) | _BV(UCSZ00); | |
253 } | |
254 | |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
255 #ifdef SIM_DEBUG |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
256 static char sim_out[140]; |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
257 static uint8_t sim_idx = 0; |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
258 static uint8_t last_sim_idx = 0; |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
259 #endif |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
260 |
0 | 261 int |
262 uart_putchar(char c, FILE *stream) | |
263 { | |
264 // XXX could perhaps sleep in the loop for power. | |
265 if (c == '\n') | |
266 { | |
267 loop_until_bit_is_set(UCSR0A, UDRE0); | |
268 UDR0 = '\r'; | |
269 } | |
270 loop_until_bit_is_set(UCSR0A, UDRE0); | |
271 UDR0 = c; | |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
272 #ifdef SIM_DEBUG |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
273 sim_out[sim_idx] = c; |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
274 sim_idx++; |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
275 sim_idx %= sizeof(sim_out); |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
276 #endif |
0 | 277 if (c == '\r') |
278 { | |
279 loop_until_bit_is_set(UCSR0A, UDRE0); | |
280 UDR0 = '\n'; | |
281 } | |
282 return (unsigned char)c; | |
283 } | |
284 | |
45 | 285 #if 0 |
286 | |
43 | 287 uint8_t find_file_in_dir(struct fat_fs_struct* fs, struct fat_dir_struct* dd, const char* name, struct fat_dir_entry_struct* dir_entry) |
288 { | |
289 while(fat_read_dir(dd, dir_entry)) | |
290 { | |
291 if(strcmp(dir_entry->long_name, name) == 0) | |
292 { | |
293 fat_reset_dir(dd); | |
294 return 1; | |
295 } | |
296 } | |
297 | |
298 return 0; | |
299 } | |
300 | |
301 struct fat_file_struct* | |
302 open_file_in_dir(struct fat_fs_struct* fs, struct fat_dir_struct* dd, const char* name) | |
303 { | |
304 struct fat_dir_entry_struct file_entry; | |
305 if(!find_file_in_dir(fs, dd, name, &file_entry)) | |
306 return 0; | |
307 | |
308 return fat_open_file(fs, &file_entry); | |
309 } | |
310 | |
311 static uint32_t sd_serial = 0; | |
312 static char conf_start[30]; | |
313 | |
314 static void | |
315 hmac_file(const char* fn) | |
316 { | |
317 uint8_t res; | |
318 | |
319 struct sd_raw_info disk_info; | |
320 sd_raw_get_info(&disk_info); | |
321 sd_serial = disk_info.serial; | |
322 printf_P(PSTR("serial %lx\n"), sd_serial); | |
323 | |
324 struct partition_struct* partition = partition_open(sd_raw_read, sd_raw_read_interval, sd_raw_write, sd_raw_write_interval, 1); | |
325 | |
326 if (!partition) | |
327 { | |
328 sprintf(conf_start, "part"); | |
329 return; | |
330 } | |
331 | |
332 struct fat_fs_struct* fs = fat_open(partition); | |
333 if (!fs) | |
334 { | |
335 sprintf(conf_start, "bad fs"); | |
336 return; | |
337 } | |
338 struct fat_dir_entry_struct directory; | |
339 res = fat_get_dir_entry_of_path(fs, "/", &directory); | |
340 if (!res) | |
341 { | |
342 sprintf(conf_start, "bad direc"); | |
343 return; | |
344 } | |
345 | |
346 struct fat_dir_struct* dd = fat_open_dir(fs, &directory); | |
347 if (!dd) | |
348 { | |
349 sprintf(conf_start, "bad dd"); | |
350 return; | |
351 } | |
352 struct fat_file_struct* fd = open_file_in_dir(fs, dd, fn); | |
353 if (!fd) | |
354 { | |
355 sprintf(conf_start, "bad fd"); | |
356 return; | |
357 } | |
358 | |
359 fat_read_file(fd, (uint8_t*)conf_start, sizeof(conf_start)-1); | |
360 conf_start[sizeof(conf_start)-1] = '\0'; | |
361 | |
362 fat_close_file(fd); | |
363 fd = NULL; | |
364 fat_close_dir(dd); | |
365 dd = NULL; | |
366 fat_close(fs); | |
367 fs = NULL; | |
368 partition_close(partition); | |
369 partition = NULL; | |
370 | |
371 #if 0 | |
372 char c = 0; | |
373 char buf[512]; | |
374 for (int i = 0; i < 10; i++) | |
375 { | |
376 fat_read_file(fd, buf, sizeof(buf)); | |
377 c ^= buf[0]; | |
378 } | |
379 printf("total %d\n", c); | |
380 #endif | |
381 } | |
382 | |
383 | |
384 static void | |
385 cmd_testsd(const char *param) | |
386 { | |
45 | 387 boot_id_set = 0; |
43 | 388 PORT_PI_RESET &= ~_BV(PIN_PI_RESET); |
389 DDR_PI_RESET |= _BV(PIN_PI_RESET); | |
390 long_delay(200); | |
391 | |
392 printf_P(PSTR("about to raw init\n")); | |
393 | |
394 sd_raw_init(); | |
395 printf_P(PSTR("done raw init\n")); | |
396 hmac_file(param); | |
397 printf_P(PSTR("conf_start '%s'\n"), conf_start); | |
398 sd_raw_deinit(); | |
399 | |
400 long_delay(200); | |
401 | |
402 DDR_PI_RESET &= ~_BV(PIN_PI_RESET); | |
403 } | |
45 | 404 #endif |
43 | 405 |
20 | 406 static void cmd_reset() __attribute__ ((noreturn)); |
0 | 407 static void |
20 | 408 cmd_reset() |
0 | 409 { |
410 printf_P(PSTR("reset\n")); | |
45 | 411 long_delay(100); |
0 | 412 cli(); // disable interrupts |
413 wdt_enable(WDTO_15MS); // enable watchdog | |
414 while(1); // wait for watchdog to reset processor | |
415 } | |
416 | |
2 | 417 static void |
418 cmd_newboot() | |
419 { | |
420 set_pi_boot_normal(1); | |
421 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) | |
422 { | |
423 newboot_count = newboot_limit; | |
424 } | |
20 | 425 printf_P(PSTR("newboot for %d secs\n"), newboot_limit); |
2 | 426 } |
427 | |
15
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
428 static void |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
429 cmd_oldboot() |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
430 { |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
431 set_pi_boot_normal(0); |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
432 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
433 { |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
434 newboot_count = 0; |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
435 } |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
436 printf_P(PSTR("back to old boot\n")); |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
437 } |
1 | 438 |
0 | 439 |
440 static void | |
20 | 441 cmd_status() |
0 | 442 { |
2 | 443 uint32_t cur_watchdog_long, cur_watchdog_short, cur_newboot, cur_oneshot; |
20 | 444 struct epoch_ticks t; |
445 | |
446 get_epoch_ticks(&t); | |
447 | |
1 | 448 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) |
449 { | |
2 | 450 cur_watchdog_long = watchdog_long_count; |
451 cur_watchdog_short = watchdog_short_count; | |
452 cur_newboot = newboot_count; | |
453 cur_oneshot = oneshot_count; | |
1 | 454 } |
455 | |
20 | 456 printf_P(PSTR("limit (current)\n" |
457 "watchdog_long %lu (%lu) watchdog newboot mode %d\n" | |
458 "watchdog_short %lu (%lu)\n" | |
459 "newboot %lu (%lu)\n" | |
460 "oneshot (%lu)\n" | |
461 "uptime %lu rem %u\n" | |
22 | 462 "boot normal %hhu\n" |
20 | 463 ), |
464 watchdog_long_limit, cur_watchdog_long, long_reboot_mode, | |
465 watchdog_short_limit, cur_watchdog_short, | |
466 newboot_limit, cur_newboot, | |
467 cur_oneshot, | |
22 | 468 t.ticks, t.rem, |
45 | 469 boot_normal_status); |
0 | 470 } |
471 | |
472 static void | |
1 | 473 cmd_set_params(const char *params) |
0 | 474 { |
1 | 475 uint32_t new_watchdog_long_limit; |
476 uint32_t new_watchdog_short_limit; | |
477 uint32_t new_newboot_limit; | |
478 | |
479 int ret = sscanf_P(params, PSTR("%lu %lu %lu"), | |
480 &new_watchdog_long_limit, | |
481 &new_watchdog_short_limit, | |
482 &new_newboot_limit); | |
483 | |
484 if (ret != 3) | |
0 | 485 { |
1 | 486 printf_P(PSTR("Bad values\n")); |
487 } | |
488 else | |
489 { | |
490 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) | |
491 { | |
492 eeprom_write(new_watchdog_long_limit, watchdog_long_limit); | |
493 eeprom_write(new_watchdog_short_limit, watchdog_short_limit); | |
494 eeprom_write(new_newboot_limit, newboot_limit); | |
495 uint16_t magic = EXPECT_MAGIC; | |
496 eeprom_write(magic, magic); | |
0 | 497 } |
1 | 498 printf_P(PSTR("set_params for next boot\n")); |
499 printf_P(PSTR("watchdog_long %lu watchdog_short %lu newboot %lu\n"), | |
500 new_watchdog_long_limit, | |
501 new_watchdog_short_limit, | |
502 new_newboot_limit); | |
503 } | |
504 } | |
0 | 505 |
35 | 506 // returns 1 if they are equal, timing independent |
507 static uint8_t | |
508 safe_mem_eq(const uint8_t *a, const uint8_t *b, int len) | |
509 { | |
510 uint8_t result = 0; | |
511 for (int i = 0; i < len; i++) | |
512 { | |
513 result |= a[i] ^ b[i]; | |
514 } | |
515 return result == 0; | |
516 } | |
517 | |
518 // returns 1 if they are equal | |
519 static uint8_t | |
520 safe_str_eq(const char *a, const char *b) | |
521 { | |
522 int la = strlen(a); | |
523 if (la != strlen(b)) | |
524 { | |
525 return 0; | |
526 } | |
527 return safe_mem_eq((const uint8_t*)a, (const uint8_t*)b, la); | |
528 } | |
529 | |
1 | 530 uint8_t from_hex(char c) |
531 { | |
532 if (c >= '0' && c <= '9') { | |
533 return c-'0'; | |
534 } | |
535 if (c >= 'a' && c <= 'f') { | |
536 return c-'a' + 0xa; | |
537 } | |
538 if (c >= 'A' && c <= 'F') { | |
539 return c-'A' + 0xa; | |
540 } | |
541 return 0; | |
542 } | |
543 | |
2 | 544 static void |
545 printhex_nibble(const unsigned char b, FILE *stream) | |
546 { | |
547 unsigned char c = b & 0x0f; | |
548 if ( c > 9 ) { | |
549 c += 'A'-10; | |
550 } | |
551 else { | |
552 c += '0'; | |
553 } | |
554 fputc(c, stream); | |
555 } | |
556 | |
557 void | |
558 printhex_byte(const unsigned char b, FILE *stream) | |
559 { | |
560 printhex_nibble( b >> 4, stream); | |
561 printhex_nibble( b, stream); | |
562 } | |
563 | |
564 void | |
565 printhex(uint8_t *id, uint8_t n, FILE *stream) | |
566 { | |
567 for (uint8_t i = 0; i < n; i++) | |
568 { | |
569 printhex_byte(id[i], stream); | |
570 } | |
571 } | |
572 | |
573 static int8_t | |
5 | 574 parse_key(const char *params, uint8_t *key_index, uint8_t *bytes, |
575 uint8_t bytes_len) | |
2 | 576 { |
577 // "N HEXKEY" | |
5 | 578 if (strlen(params) != bytes_len*2 + 2) { |
20 | 579 printf_P(PSTR("Wrong length key. wanted %d, got %d, '%s'\n"), |
580 bytes_len*2+2, strlen(params), params); | |
2 | 581 return -1; |
582 } | |
583 | |
584 if (params[1] != ' ') | |
585 { | |
586 printf_P(PSTR("Missing space\n")); | |
587 return -1; | |
588 } | |
589 | |
590 *key_index = from_hex(params[0]); | |
591 if (*key_index >= NKEYS) | |
592 { | |
593 printf_P(PSTR("Bad key index %d, max %d\n"), *key_index, NKEYS); | |
594 return -1; | |
595 } | |
596 | |
5 | 597 for (int i = 0, p = 0; i < bytes_len; i++, p += 2) |
2 | 598 { |
599 bytes[i] = (from_hex(params[p+2]) << 4) | from_hex(params[p+3]); | |
600 } | |
601 return 0; | |
602 } | |
603 | |
1 | 604 static void |
605 cmd_set_avr_key(const char *params) | |
606 { | |
2 | 607 uint8_t new_key[KEYLEN]; |
608 uint8_t key_index; | |
5 | 609 if (parse_key(params, &key_index, new_key, sizeof(new_key)) != 0) |
2 | 610 { |
1 | 611 return; |
0 | 612 } |
2 | 613 memcpy(avr_keys[key_index], new_key, sizeof(new_key)); |
8 | 614 #ifndef SIM_DEBUG |
2 | 615 eeprom_write(avr_keys, avr_keys); |
8 | 616 #endif |
20 | 617 printf_P(PSTR("Set key %d: "), key_index); |
618 printhex(new_key, sizeof(new_key), stdout); | |
619 putchar('\n'); | |
2 | 620 } |
621 | |
622 static void | |
623 cmd_hmac(const char *params) | |
624 { | |
42
082c8294c86b
get rid of hmac message padding and aes hmac
Matt Johnston <matt@ucc.asn.au>
parents:
41
diff
changeset
|
625 uint8_t indata[HMACLEN]; |
7 | 626 uint8_t outdata[HMACLEN]; |
2 | 627 uint8_t key_index; |
42
082c8294c86b
get rid of hmac message padding and aes hmac
Matt Johnston <matt@ucc.asn.au>
parents:
41
diff
changeset
|
628 if (parse_key(params, &key_index, indata, HMACLEN) != 0) |
2 | 629 { |
630 printf_P(PSTR("FAIL: Bad input\n")); | |
5 | 631 return; |
2 | 632 } |
0 | 633 |
8 | 634 #ifndef SIM_DEBUG |
5 | 635 long_delay(200); |
8 | 636 #endif |
5 | 637 |
11
e83b35e864d7
hmac and decrypt keys differ now
Matt Johnston <matt@ucc.asn.au>
parents:
8
diff
changeset
|
638 hmac_sha1(outdata, avr_keys[key_index], KEYLEN*8, indata, sizeof(indata)*8); |
2 | 639 printf_P(PSTR("HMAC: ")); |
7 | 640 printhex(outdata, HMACLEN, stdout); |
20 | 641 putchar('\n'); |
5 | 642 } |
643 | |
644 static void | |
645 cmd_decrypt(const char *params) | |
646 { | |
42
082c8294c86b
get rid of hmac message padding and aes hmac
Matt Johnston <matt@ucc.asn.au>
parents:
41
diff
changeset
|
647 uint8_t indata[AESLEN]; |
082c8294c86b
get rid of hmac message padding and aes hmac
Matt Johnston <matt@ucc.asn.au>
parents:
41
diff
changeset
|
648 uint8_t output[AESLEN]; |
5 | 649 uint8_t key_index; |
11
e83b35e864d7
hmac and decrypt keys differ now
Matt Johnston <matt@ucc.asn.au>
parents:
8
diff
changeset
|
650 if (parse_key(params, &key_index, indata, sizeof(indata)) != 0) |
5 | 651 { |
652 printf_P(PSTR("FAIL: Bad input\n")); | |
653 return; | |
654 } | |
655 | |
8 | 656 #ifndef SIM_DEBUG |
5 | 657 long_delay(200); |
8 | 658 #endif |
7 | 659 |
11
e83b35e864d7
hmac and decrypt keys differ now
Matt Johnston <matt@ucc.asn.au>
parents:
8
diff
changeset
|
660 uint8_t tmpbuf[256]; |
27 | 661 aesInit(avr_keys[key_index], tmpbuf); |
42
082c8294c86b
get rid of hmac message padding and aes hmac
Matt Johnston <matt@ucc.asn.au>
parents:
41
diff
changeset
|
662 aesDecrypt(indata, NULL); |
5 | 663 |
664 printf_P(PSTR("DECRYPTED: ")); | |
665 printhex(output, AESLEN, stdout); | |
20 | 666 putchar('\n'); |
2 | 667 } |
668 | |
669 static void | |
670 cmd_oneshot_reboot(const char *params) | |
671 { | |
672 uint32_t new_delay = strtoul(params, NULL, 10); | |
673 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) | |
1 | 674 { |
2 | 675 oneshot_count = new_delay; |
1 | 676 } |
15
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
677 printf_P(PSTR("oneshot new delay %lu\n"), new_delay); |
0 | 678 } |
679 | |
680 static void | |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
681 clamp_params() |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
682 { |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
683 if (watchdog_long_limit < WATCHDOG_LONG_MIN |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
684 || watchdog_long_limit > WATCHDOG_LONG_MAX) |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
685 { |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
686 watchdog_long_limit = WATCHDOG_LONG_DEFAULT; |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
687 } |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
688 |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
689 if (watchdog_short_limit != 0 |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
690 && watchdog_short_limit < WATCHDOG_SHORT_MIN) |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
691 { |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
692 watchdog_short_limit = 0; |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
693 } |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
694 |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
695 if (newboot_limit < NEWBOOT_MIN || newboot_limit > NEWBOOT_MAX) |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
696 { |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
697 newboot_limit = NEWBOOT_DEFAULT; |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
698 } |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
699 } |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
700 |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
701 static void |
0 | 702 load_params() |
703 { | |
704 uint16_t magic; | |
705 eeprom_read(magic, magic); | |
706 if (magic == EXPECT_MAGIC) | |
707 { | |
1 | 708 eeprom_read(watchdog_long_limit, watchdog_long_limit); |
709 eeprom_read(watchdog_short_limit, watchdog_short_limit); | |
2 | 710 eeprom_read(newboot_limit, newboot_limit); |
1 | 711 } |
2 | 712 |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
713 clamp_params(); |
16 | 714 |
2 | 715 eeprom_read(avr_keys, avr_keys); |
0 | 716 } |
717 | |
2 | 718 static void |
15
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
719 cmd_alive() |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
720 { |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
721 printf_P(PSTR("Ah, good.\n")); |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
722 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
723 { |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
724 watchdog_long_count = 0; |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
725 watchdog_short_count = 0; |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
726 } |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
727 long_reboot_mode = 0; |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
728 } |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
729 |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
730 static void |
35 | 731 cmd_poke() |
732 { | |
733 printf_P(PSTR("Ah, good.\n")); | |
734 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) | |
735 { | |
736 watchdog_short_count = 0; | |
737 } | |
738 } | |
739 | |
740 static void | |
2 | 741 cmd_vcc() |
0 | 742 { |
2 | 743 uint16_t vcc = adc_vcc(); |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
744 uint16_t v5 = adc_5v(vcc); |
21 | 745 uint16_t temp = adc_temp(); |
746 // roughly? | |
747 uint16_t temp_deg = temp - 290; | |
748 printf_P(PSTR("vcc: %u mV\n" | |
749 "5v: %u mV\n" | |
26 | 750 "temp: %u mV (%dºC)\n" |
751 ), | |
27 | 752 vcc, v5, temp, temp_deg); |
0 | 753 } |
754 | |
45 | 755 static void |
756 get_random(uint8_t *out) | |
757 { | |
758 uint32_t tries; | |
759 uint8_t rnd[20]; | |
760 adc_random(0x44, rnd, sizeof(rnd), &tries); | |
761 hmac_sha1(out, buildid, sizeof(buildid)*8, rnd, sizeof(rnd)*8); | |
762 } | |
763 | |
764 static void | |
765 cmd_bootid(const char *arg) | |
766 { | |
767 uint8_t hmac[HMACLEN]; | |
768 uint8_t input[CHALLEN+sizeof(boot_id)]; | |
46 | 769 |
770 if (!boot_id_set) | |
771 { | |
772 _Static_assert(sizeof(boot_id) == HMACLEN, "boot_id size correct"); | |
773 get_random(boot_id); | |
774 boot_id_set = 1; | |
775 } | |
45 | 776 |
777 if (strlen(arg) != CHALLEN*2) | |
778 { | |
779 printf_P(PSTR("Bad challenge\n")); | |
780 } | |
781 for (int i = 0, p = 0; i < CHALLEN; i++, p += 2) | |
782 { | |
783 input[i] = (from_hex(arg[p]) << 4) | from_hex(arg[p+1]); | |
784 } | |
785 memcpy(&input[CHALLEN], boot_id, sizeof(boot_id)); | |
786 | |
787 hmac_sha1(hmac, avr_keys[0], KEYLEN*8, input, sizeof(input)*8); | |
788 printf_P(PSTR("bootid: ")); | |
789 printhex(boot_id, sizeof(boot_id), stdout); | |
790 putchar(' '); | |
791 printhex(hmac, sizeof(hmac), stdout); | |
792 putchar('\n'); | |
793 } | |
20 | 794 |
27 | 795 void(*bootloader)() __attribute__ ((noreturn)) = (void*)0x7800; |
796 | |
20 | 797 static void |
798 cmd_prog(const char* arg) | |
799 { | |
46 | 800 uint8_t pw_hmac[HMACLEN]; |
801 uint8_t good_hmac[HMACLEN]; | |
802 | |
803 const static char prog_hmac[HMACLEN] PROGMEM = { | |
804 0x73, 0x4d, 0xa6, 0x3f, 0x3b, 0x7e, 0x4d, 0xa4, 0x65, 0xae, 0xea, 0xf9, 0x19, 0xbc, 0x4f, 0x45, 0xa7, 0x8d, 0x5a, 0xce, | |
805 }; | |
806 | |
807 memcpy_P(good_hmac, prog_hmac, HMACLEN); | |
808 hmac_sha1(pw_hmac, arg, strlen(arg)*8, "pihelp", strlen("pihelp")*8); | |
809 if (!safe_mem_eq(pw_hmac, good_hmac, HMACLEN)) | |
20 | 810 { |
811 printf_P(PSTR("Bad prog password\n")); | |
812 return; | |
813 } | |
814 | |
36 | 815 printf_P(PSTR("Programming...\n")); |
816 long_delay(100); | |
817 | |
20 | 818 // disable wdt |
819 wdt_disable(); | |
41 | 820 MCUSR = 0; |
20 | 821 |
822 // disable interrupts | |
823 TIMSK0 = 0; | |
824 TIMSK1 = 0; | |
825 TIMSK2 = 0; | |
826 EIMSK = 0; | |
827 PCMSK0 = 0; | |
828 PCMSK1 = 0; | |
829 PCMSK2 = 0; | |
830 ACSR &= ~_BV(ACIE); | |
831 ADCSRA &= ~_BV(ADIE); | |
832 UCSR0B &= ~_BV(RXCIE0); | |
833 UCSR0B &= _BV(TXCIE0); | |
834 // doesn't do TWI, other uart, probably others | |
835 | |
836 _delay_ms(20); | |
837 | |
838 bootloader(); | |
839 } | |
27 | 840 |
841 | |
842 static void | |
843 adc_sleep() | |
844 { | |
845 set_sleep_mode(SLEEP_MODE_IDLE); | |
846 sleep_mode(); | |
847 } | |
848 | |
849 #define BITSET(v, n) (((v) >> (n)) & 1) | |
850 | |
851 static inline uint8_t | |
852 popcnt(uint8_t v) | |
853 { | |
854 return BITSET(v, 0) | |
855 + BITSET(v, 1) | |
856 + BITSET(v, 2) | |
857 + BITSET(v, 3) | |
858 + BITSET(v, 4) | |
859 + BITSET(v, 5) | |
860 + BITSET(v, 6) | |
861 + BITSET(v, 7); | |
862 } | |
863 | |
864 static uint8_t | |
865 adc_bit() | |
866 { | |
867 ADCSRA |= _BV(ADSC); | |
868 loop_until_bit_is_clear(ADCSRA, ADSC); | |
869 uint8_t low = ADCL; | |
870 uint8_t high = ADCH; | |
35 | 871 uint8_t ret = (popcnt(low)&1) ^ (popcnt(high)&1); |
872 return ret; | |
27 | 873 } |
874 | |
875 static void | |
876 adc_random(uint8_t admux, | |
877 uint8_t *out, uint16_t num, uint32_t *tries) | |
878 { | |
879 PRR &= ~_BV(PRADC); | |
880 // /16 prescaler for 691mhz, no interrupt | |
881 ADCSRA = _BV(ADEN) | _BV(ADPS2); | |
882 | |
35 | 883 ADMUX = admux; |
884 | |
27 | 885 *tries = 0; |
886 for (int i = 0; i < num; i++) | |
887 { | |
35 | 888 uint8_t ret = 0; |
889 uint8_t count = 0; | |
890 | |
27 | 891 while (count <= 7) |
892 { | |
893 (*tries)++; | |
894 | |
895 // Von Neumann extractor | |
896 uint8_t one = adc_bit(); | |
897 uint8_t two = adc_bit(); | |
898 if (one == two) | |
899 { | |
900 continue; | |
901 } | |
902 ret |= one << count; | |
903 count++; | |
904 } | |
905 out[i] = ret; | |
906 } | |
907 ADCSRA = 0; | |
908 PRR |= _BV(PRADC); | |
909 } | |
910 | |
911 ISR(ADC_vect) | |
912 { | |
913 adc_done = 1; | |
914 } | |
915 | |
916 static void | |
917 adc_generic(uint8_t admux, uint8_t *ret_num, uint16_t *ret_sum) | |
918 { | |
919 PRR &= ~_BV(PRADC); | |
920 | |
31 | 921 // /128 prescaler (86kHz), interrupt |
922 ADCSRA = _BV(ADEN) | |
923 | _BV(ADPS2) | _BV(ADPS1) | _BV(ADPS0) | |
924 | _BV(ADIE); | |
27 | 925 |
926 // set to measure 1.1 reference | |
927 ADMUX = admux; | |
928 | |
929 // delay after setting reference etc, allow settling | |
930 long_delay(300); | |
931 // average a number of samples | |
932 uint16_t sum = 0; | |
933 uint8_t num = 0; | |
934 for (uint8_t n = 0; n < 20; n++) | |
935 { | |
936 while (1) | |
937 { | |
938 adc_done = 0; | |
939 ADCSRA |= _BV(ADSC); | |
940 adc_sleep(); | |
941 | |
942 uint8_t done; | |
943 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) | |
944 { | |
945 done = adc_done; | |
946 } | |
947 if (done) | |
948 { | |
949 break; | |
950 } | |
951 } | |
952 | |
953 uint8_t low_11 = ADCL; | |
954 uint8_t high_11 = ADCH; | |
955 uint16_t val = low_11 + (high_11 << 8); | |
956 | |
957 if (n >= 4) | |
958 { | |
959 sum += val; | |
960 num++; | |
961 } | |
962 } | |
963 ADCSRA = 0; | |
964 PRR |= _BV(PRADC); | |
965 | |
966 *ret_num = num; | |
967 *ret_sum = sum; | |
968 } | |
969 | |
970 static uint16_t | |
971 adc_vcc() | |
972 { | |
973 const uint8_t mux = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1); | |
974 uint16_t sum; | |
975 uint8_t num; | |
976 | |
977 adc_generic(mux, &num, &sum); | |
978 | |
979 //float res_volts = 1.1 * 1024 * num / sum; | |
980 //return 1000 * res_volts; | |
981 return ((uint32_t)1100*1024*num) / sum; | |
982 } | |
983 | |
984 #define SCALER_5V 2 | |
985 | |
986 static uint16_t | |
987 adc_5v(uint16_t vcc) | |
988 { | |
989 // set to measure ADC4 against AVCC | |
990 const uint8_t mux = _BV(REFS0) | _BV(MUX2); | |
991 uint16_t sum; | |
992 uint8_t num; | |
993 | |
994 adc_generic(mux, &num, &sum); | |
995 | |
996 return ((uint32_t)vcc*sum*SCALER_5V/(num*1024)); | |
997 } | |
998 | |
999 static uint16_t | |
1000 adc_temp() | |
1001 { | |
1002 // set to measure temperature against 1.1v reference. | |
1003 const uint8_t mux = _BV(REFS0) | _BV(REFS1) | _BV(MUX3); | |
1004 uint16_t sum; | |
1005 uint8_t num; | |
1006 | |
1007 adc_generic(mux, &num, &sum); | |
1008 | |
1009 // return the voltage | |
1010 | |
1011 return ((uint32_t)1100*sum) / (num*1024); | |
1012 } | |
1013 | |
1014 static void | |
1015 cmd_random(const char* params) | |
1016 { | |
1017 uint8_t admux; | |
1018 uint16_t num; | |
1019 uint8_t buf[100]; | |
1020 | |
1021 int ret = sscanf_P(params, PSTR("%hhu %u"), | |
1022 &admux, &num); | |
1023 if (!ret) | |
1024 { | |
1025 printf_P(PSTR("Bad arguments\n")); | |
1026 return; | |
1027 } | |
1028 uint32_t tries = 0; | |
1029 printf_P(PSTR("output: ")); | |
1030 for (int i = 0; i < num; i+= sizeof(buf)) | |
1031 { | |
1032 uint32_t t; | |
1033 uint16_t nr = MIN(num-i, sizeof(buf)); | |
1034 adc_random(admux, buf, nr, &t); | |
1035 printhex(buf, nr, stdout); | |
1036 tries += t; | |
1037 } | |
1038 putchar('\n'); | |
1039 printf_P(PSTR("%ld tries\n"), tries); | |
1040 } | |
1041 | |
1042 | |
20 | 1043 |
0 | 1044 static void |
1045 read_handler() | |
1046 { | |
17 | 1047 #define LOCAL_PSTR(x) const static char x ## _str[] PROGMEM = #x; |
1048 #define LOCAL_HELP(x, d) const static char x ## _help[] PROGMEM = d; | |
1049 | |
1050 LOCAL_PSTR(set_params); | |
1051 LOCAL_PSTR(set_key); | |
1052 LOCAL_PSTR(oneshot); | |
1053 LOCAL_PSTR(hmac); | |
1054 LOCAL_PSTR(decrypt); | |
1055 LOCAL_PSTR(alive); | |
35 | 1056 LOCAL_PSTR(poke); |
17 | 1057 LOCAL_PSTR(vcc); |
1058 LOCAL_PSTR(reset); | |
1059 LOCAL_PSTR(newboot); | |
1060 LOCAL_PSTR(oldboot); | |
20 | 1061 LOCAL_PSTR(status); |
27 | 1062 LOCAL_PSTR(random); |
1063 LOCAL_PSTR(prog); | |
45 | 1064 LOCAL_PSTR(bootid); |
17 | 1065 LOCAL_HELP(set_params, "<long_limit> <short_limit> <newboot_limit>"); |
1066 LOCAL_HELP(set_key, "20_byte_hex>"); | |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1067 LOCAL_HELP(oneshot, "<timeout>"); |
27 | 1068 LOCAL_HELP(prog, "<password>"); |
1069 LOCAL_HELP(random, "<admux> <nbytes>"); | |
17 | 1070 LOCAL_HELP(hmac, "<key_index> <20_byte_hex_data>"); |
1071 LOCAL_HELP(decrypt, "<key_index> <20_byte_hmac|16_byte_aes_block>"); | |
45 | 1072 LOCAL_HELP(bootid, "<8_byte_challenge>") |
17 | 1073 |
1074 static const struct handler { | |
1075 PGM_P name; | |
1076 void(*cmd)(const char *param); | |
1077 // existence of arg_help indicates if the cmd takes a parameter. | |
1078 PGM_P arg_help; | |
20 | 1079 } handlers[] PROGMEM = |
17 | 1080 { |
20 | 1081 {alive_str, cmd_alive, NULL}, |
35 | 1082 {poke_str, cmd_poke, NULL}, |
20 | 1083 {newboot_str, cmd_newboot, NULL}, |
1084 {oldboot_str, cmd_oldboot, NULL}, | |
1085 {oneshot_str, cmd_oneshot_reboot, oneshot_help}, | |
1086 {status_str, cmd_status, NULL}, | |
1087 {hmac_str, cmd_hmac, hmac_help}, | |
1088 {decrypt_str, cmd_decrypt, decrypt_help}, | |
17 | 1089 {set_params_str, cmd_set_params, set_params_help}, |
1090 {set_key_str, cmd_set_avr_key, set_key_help}, | |
27 | 1091 {random_str, cmd_random, random_help}, |
17 | 1092 {vcc_str, cmd_vcc, NULL}, |
1093 {reset_str, cmd_reset, NULL}, | |
27 | 1094 {prog_str, cmd_prog, prog_help}, |
45 | 1095 {bootid_str, cmd_bootid, bootid_help}, |
17 | 1096 }; |
1097 | |
1098 if (readbuf[0] == '\0') | |
1099 { | |
1100 return; | |
1101 } | |
1102 | |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1103 if (strcmp_P(readbuf, PSTR("help")) == 0) |
17 | 1104 { |
1105 printf_P(PSTR("Commands:---\n")); | |
1106 for (int i = 0; i < sizeof(handlers) / sizeof(handlers[0]); i++) | |
1107 { | |
1108 struct handler h; | |
1109 memcpy_P(&h, &handlers[i], sizeof(h)); | |
1110 printf_P(h.name); | |
1111 if (h.arg_help) | |
1112 { | |
1113 putchar(' '); | |
1114 printf_P(h.arg_help); | |
1115 } | |
1116 putchar('\n'); | |
1117 }; | |
1118 printf_P(PSTR("---\n")); | |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1119 return; |
17 | 1120 } |
1121 | |
1122 for (int i = 0; i < sizeof(handlers) / sizeof(handlers[0]); i++) | |
1123 { | |
1124 struct handler h; | |
1125 memcpy_P(&h, &handlers[i], sizeof(h)); | |
1126 | |
1127 const int h_len = strlen_P(h.name); | |
1128 if (strncmp_P(readbuf, h.name, h_len) == 0) | |
1129 { | |
1130 if (h.arg_help) | |
1131 { | |
1132 if (readbuf[h_len] == ' ') | |
1133 { | |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1134 h.cmd(&readbuf[h_len+1]); |
17 | 1135 return; |
1136 } | |
1137 } | |
1138 else | |
1139 { | |
1140 if (readbuf[h_len] == '\0') | |
1141 { | |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1142 void(*void_cmd)() = h.cmd; |
17 | 1143 void_cmd(); |
1144 return; | |
1145 } | |
1146 } | |
1147 } | |
1148 } | |
1149 | |
1150 printf_P(PSTR("Bad command '%s'\n"), readbuf); | |
0 | 1151 } |
1152 | |
1153 ISR(INT0_vect) | |
1154 { | |
1155 blink(); | |
45 | 1156 long_delay(100); |
0 | 1157 blink(); |
1158 } | |
1159 | |
1160 ISR(USART_RX_vect) | |
1161 { | |
1162 char c = UDR0; | |
1163 #ifdef HAVE_UART_ECHO | |
1164 uart_putchar(c, NULL); | |
1165 #endif | |
1166 if (c == '\r' || c == '\n') | |
1167 { | |
1168 if (readpos > 0) | |
1169 { | |
1170 readbuf[readpos] = '\0'; | |
1171 have_cmd = 1; | |
1172 readpos = 0; | |
1173 } | |
1174 } | |
1175 else | |
1176 { | |
1177 readbuf[readpos] = c; | |
1178 readpos++; | |
1179 if (readpos >= sizeof(readbuf)) | |
1180 { | |
1181 readpos = 0; | |
1182 } | |
1183 } | |
1184 } | |
1185 | |
2 | 1186 ISR(TIMER1_COMPA_vect) |
0 | 1187 { |
2 | 1188 TCNT1 = 0; |
0 | 1189 |
1190 clock_epoch += TICK; | |
1191 | |
1 | 1192 // watchdogs count up, continuous |
1193 if (watchdog_long_limit > 0) { | |
2 | 1194 watchdog_long_count += TICK; |
1 | 1195 if (watchdog_long_count >= watchdog_long_limit) |
1196 { | |
1197 watchdog_long_count = 0; | |
1198 watchdog_long_hit = 1; | |
1199 } | |
0 | 1200 } |
1201 | |
1 | 1202 if (watchdog_short_limit > 0) { |
2 | 1203 watchdog_short_count += TICK; |
1 | 1204 if (watchdog_short_count >= watchdog_short_limit) |
1205 { | |
1206 watchdog_short_count = 0; | |
1207 watchdog_short_hit = 1; | |
1208 } | |
0 | 1209 } |
1210 | |
2 | 1211 // newboot counts down |
1 | 1212 if (newboot_count > 0) |
0 | 1213 { |
2 | 1214 newboot_count-=TICK; |
1215 if (newboot_count <= 0) | |
1 | 1216 { |
1217 newboot_hit = 1; | |
2 | 1218 newboot_count = 0; |
1 | 1219 } |
0 | 1220 } |
1 | 1221 |
2 | 1222 if (oneshot_count > 0) |
1223 { | |
1224 oneshot_count-=TICK; | |
1225 if (oneshot_count <= 0) | |
1226 { | |
1227 oneshot_hit = 1; | |
1228 oneshot_count = 0; | |
1229 } | |
1230 } | |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1231 |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1232 if (reboot_count > 0) |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1233 { |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1234 reboot_count -= TICK; |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1235 if (reboot_count <= 0) |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1236 { |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1237 reboot_hit = 1; |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1238 reboot_count = 0; |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1239 } |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1240 } |
0 | 1241 } |
1242 | |
1243 static void | |
1244 idle_sleep() | |
1245 { | |
1246 set_sleep_mode(SLEEP_MODE_IDLE); | |
1247 sleep_mode(); | |
1248 } | |
1249 | |
21 | 1250 static void |
2 | 1251 reboot_pi() |
1252 { | |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1253 printf_P(PSTR("Real reboot now\n")); |
45 | 1254 boot_id_set = 0; |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1255 // pull it low for 200ms |
2 | 1256 PORT_PI_RESET &= ~_BV(PIN_PI_RESET); |
1257 DDR_PI_RESET |= _BV(PIN_PI_RESET); | |
45 | 1258 long_delay(200); |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1259 |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1260 PORT_PI_WARNING &= ~_BV(PIN_PI_WARNING); |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1261 DDR_PI_RESET &= ~_BV(PIN_PI_RESET); |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1262 } |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1263 |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1264 static void |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1265 wait_reboot_pi() |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1266 { |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1267 PORT_PI_WARNING |= _BV(PIN_PI_WARNING); |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1268 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1269 { |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1270 reboot_count = WARNING_TIME; |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1271 } |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1272 printf_P(PSTR("Rebooting in %hhu seconds\n"), reboot_count); |
2 | 1273 } |
1274 | |
1275 static void | |
1276 set_pi_boot_normal(uint8_t normal) | |
1277 { | |
22 | 1278 boot_normal_status = normal; |
2 | 1279 PORT_PI_BOOT &= ~_BV(PIN_PI_BOOT); |
1280 if (normal) | |
1281 { | |
1282 // tristate | |
1283 DDR_PI_BOOT &= ~_BV(PIN_PI_BOOT); | |
1284 } | |
1285 else | |
1286 { | |
1287 // pull it low | |
1288 DDR_PI_RESET |= _BV(PIN_PI_BOOT); | |
1289 } | |
1290 } | |
1291 | |
1292 static void | |
1293 check_flags() | |
1294 { | |
15
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
1295 if (watchdog_long_hit) |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
1296 { |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
1297 // alternate between booting normal and emergency |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
1298 if (long_reboot_mode) |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
1299 { |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
1300 cmd_newboot(); |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
1301 } |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
1302 long_reboot_mode ^= 1; |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
1303 } |
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
1304 |
2 | 1305 if (watchdog_long_hit |
1306 || watchdog_short_hit | |
1307 || oneshot_hit) | |
1308 { | |
12 | 1309 printf_P(PSTR("Rebooting! long %d, short %d, oneshot %d\n"), |
1310 watchdog_long_hit, watchdog_short_hit, oneshot_hit); | |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1311 wait_reboot_pi(); |
2 | 1312 } |
1313 | |
1314 if (newboot_hit) { | |
1315 set_pi_boot_normal(0); | |
1316 } | |
1317 | |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1318 if (reboot_hit) { |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1319 reboot_pi(); |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1320 } |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1321 |
2 | 1322 watchdog_long_hit = 0; |
1323 watchdog_short_hit = 0; | |
1324 newboot_hit = 0; | |
1325 oneshot_hit = 0; | |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1326 reboot_hit = 0; |
2 | 1327 } |
1328 | |
1329 static void | |
0 | 1330 do_comms() |
1331 { | |
1332 // avoid receiving rubbish, perhaps | |
1333 uart_on(); | |
1334 | |
1335 // write sd card here? same 3.3v regulator... | |
1336 | |
1 | 1337 while (1) |
0 | 1338 { |
1 | 1339 wdt_reset(); |
2 | 1340 |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1341 #ifdef SIM_DEBUG |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1342 if (sim_idx != last_sim_idx) |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1343 { |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1344 last_sim_idx = sim_idx; |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1345 } |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1346 #endif |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1347 |
2 | 1348 check_flags(); |
1349 | |
0 | 1350 if (have_cmd) |
1351 { | |
1352 have_cmd = 0; | |
1353 read_handler(); | |
1354 continue; | |
1355 } | |
1356 | |
1357 // wait for commands from the master | |
1358 idle_sleep(); | |
1359 } | |
1360 } | |
1361 | |
1362 static void | |
1363 blink() | |
1364 { | |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1365 #if 0 |
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1366 PORT_ &= ~_BV(PIN_LED); |
0 | 1367 _delay_ms(1); |
1368 PORT_LED |= _BV(PIN_LED); | |
18
021e6e0006f4
debug printing, 5v adc, fixes
Matt Johnston <matt@ucc.asn.au>
parents:
17
diff
changeset
|
1369 #endif |
0 | 1370 } |
1371 | |
1372 static void | |
22 | 1373 long_delay(uint16_t ms) |
0 | 1374 { |
22 | 1375 uint16_t iter = ms / 10; |
0 | 1376 |
22 | 1377 for (uint16_t i = 0; i < iter; i++) |
0 | 1378 { |
22 | 1379 _delay_ms(10); |
0 | 1380 } |
1381 } | |
1382 | |
1383 ISR(BADISR_vect) | |
1384 { | |
1385 //uart_on(); | |
1386 printf_P(PSTR("Bad interrupt\n")); | |
1387 } | |
1388 | |
20 | 1389 // disable watchdog on boot |
1390 void wdt_init(void) __attribute__((naked)) __attribute__((section(".init3"))); | |
1391 void wdt_init(void) | |
1392 { | |
1393 MCUSR = 0; | |
1394 wdt_disable(); | |
1395 } | |
1396 | |
0 | 1397 int main(void) |
1398 { | |
17 | 1399 _Static_assert(F_CPU % 256 == 0, "clock prescaler remainder 0"); |
1400 _Static_assert(NEWBOOT_MAX < WATCHDOG_LONG_MIN, "newboot max shorter than watchdog min"); | |
20 | 1401 _Static_assert((F_CPU)%(16*(BAUD)) == 0, "baud rate good multiple"); |
15
915be6f0ff13
fix for 8mhz, add flip/flop long watchdog
Matt Johnston <matt@ucc.asn.au>
parents:
12
diff
changeset
|
1402 |
0 | 1403 setup_chip(); |
1404 blink(); | |
1405 | |
1406 stdout = &mystdout; | |
1407 uart_on(); | |
1408 | |
20 | 1409 long_delay(500); |
1410 printf_P(PSTR("Pi Watchdog\nMatt Johnston [email protected]\n")); | |
0 | 1411 |
2 | 1412 set_pi_boot_normal(0); |
1413 | |
0 | 1414 load_params(); |
1415 | |
1416 setup_tick_counter(); | |
1417 | |
1418 sei(); | |
1419 | |
8 | 1420 #if 0 |
1421 // encryption test | |
1422 cmd_set_avr_key("1 6161626263636464656566666767686800000000"); | |
1423 cmd_set_avr_key("2 7979757569696f6f646465656666717164646969"); | |
12 | 1424 //cmd_decrypt("1 ecd858ee07a8e16575723513d2d072a7565865e40ba302059bfc650d4491268448102119"); |
1425 cmd_decrypt("1 5a587b50fd48688bbda1b510cf9a3fab6fd4737b" "0ba302059bfc650d4491268448102119"); | |
1426 cmd_hmac("2 7979757569696f6f646465656666717164646969"); | |
8 | 1427 #endif |
1428 | |
1 | 1429 // doesn't return |
1430 do_comms(); | |
0 | 1431 |
1432 return 0; /* never reached */ | |
1433 } |