comparison main.c @ 43:69cbf9ce72b5

bring back testsd
author Matt Johnston <matt@ucc.asn.au>
date Sat, 29 Jun 2013 10:42:34 +0800
parents 082c8294c86b
children a0f2fcc6d9dd
comparison
equal deleted inserted replaced
42:082c8294c86b 43:69cbf9ce72b5
13 #include <util/atomic.h> 13 #include <util/atomic.h>
14 #include <util/crc16.h> 14 #include <util/crc16.h>
15 15
16 #include "hmac-sha1.h" 16 #include "hmac-sha1.h"
17 #include "aes.h" 17 #include "aes.h"
18
19 #include "fat.h"
20 #include "fat_config.h"
21 #include "partition.h"
22 #include "sd_raw.h"
23 #include "sd_raw_config.h"
18 24
19 //#include "simple_ds18b20.h" 25 //#include "simple_ds18b20.h"
20 //#include "onewire.h" 26 //#include "onewire.h"
21 27
22 // not set via bootloader 28 // not set via bootloader
266 UDR0 = '\n'; 272 UDR0 = '\n';
267 } 273 }
268 return (unsigned char)c; 274 return (unsigned char)c;
269 } 275 }
270 276
277 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)
278 {
279 while(fat_read_dir(dd, dir_entry))
280 {
281 if(strcmp(dir_entry->long_name, name) == 0)
282 {
283 fat_reset_dir(dd);
284 return 1;
285 }
286 }
287
288 return 0;
289 }
290
291 struct fat_file_struct*
292 open_file_in_dir(struct fat_fs_struct* fs, struct fat_dir_struct* dd, const char* name)
293 {
294 struct fat_dir_entry_struct file_entry;
295 if(!find_file_in_dir(fs, dd, name, &file_entry))
296 return 0;
297
298 return fat_open_file(fs, &file_entry);
299 }
300
301 static uint32_t sd_serial = 0;
302 static char conf_start[30];
303
304 static void
305 hmac_file(const char* fn)
306 {
307 uint8_t res;
308
309 struct sd_raw_info disk_info;
310 sd_raw_get_info(&disk_info);
311 sd_serial = disk_info.serial;
312 printf_P(PSTR("serial %lx\n"), sd_serial);
313
314 struct partition_struct* partition = partition_open(sd_raw_read, sd_raw_read_interval, sd_raw_write, sd_raw_write_interval, 1);
315
316 if (!partition)
317 {
318 sprintf(conf_start, "part");
319 return;
320 }
321
322 struct fat_fs_struct* fs = fat_open(partition);
323 if (!fs)
324 {
325 sprintf(conf_start, "bad fs");
326 return;
327 }
328 struct fat_dir_entry_struct directory;
329 res = fat_get_dir_entry_of_path(fs, "/", &directory);
330 if (!res)
331 {
332 sprintf(conf_start, "bad direc");
333 return;
334 }
335
336 struct fat_dir_struct* dd = fat_open_dir(fs, &directory);
337 if (!dd)
338 {
339 sprintf(conf_start, "bad dd");
340 return;
341 }
342 struct fat_file_struct* fd = open_file_in_dir(fs, dd, fn);
343 if (!fd)
344 {
345 sprintf(conf_start, "bad fd");
346 return;
347 }
348
349 fat_read_file(fd, (uint8_t*)conf_start, sizeof(conf_start)-1);
350 conf_start[sizeof(conf_start)-1] = '\0';
351
352 fat_close_file(fd);
353 fd = NULL;
354 fat_close_dir(dd);
355 dd = NULL;
356 fat_close(fs);
357 fs = NULL;
358 partition_close(partition);
359 partition = NULL;
360
361 #if 0
362 char c = 0;
363 char buf[512];
364 for (int i = 0; i < 10; i++)
365 {
366 fat_read_file(fd, buf, sizeof(buf));
367 c ^= buf[0];
368 }
369 printf("total %d\n", c);
370 #endif
371 }
372
373
374 static void
375 cmd_testsd(const char *param)
376 {
377 PORT_PI_RESET &= ~_BV(PIN_PI_RESET);
378 DDR_PI_RESET |= _BV(PIN_PI_RESET);
379 long_delay(200);
380
381 printf_P(PSTR("about to raw init\n"));
382
383 sd_raw_init();
384 printf_P(PSTR("done raw init\n"));
385 hmac_file(param);
386 printf_P(PSTR("conf_start '%s'\n"), conf_start);
387 sd_raw_deinit();
388
389 long_delay(200);
390
391 DDR_PI_RESET &= ~_BV(PIN_PI_RESET);
392 }
393
271 static void cmd_reset() __attribute__ ((noreturn)); 394 static void cmd_reset() __attribute__ ((noreturn));
272 static void 395 static void
273 cmd_reset() 396 cmd_reset()
274 { 397 {
275 printf_P(PSTR("reset\n")); 398 printf_P(PSTR("reset\n"));
323 "watchdog_short %lu (%lu)\n" 446 "watchdog_short %lu (%lu)\n"
324 "newboot %lu (%lu)\n" 447 "newboot %lu (%lu)\n"
325 "oneshot (%lu)\n" 448 "oneshot (%lu)\n"
326 "uptime %lu rem %u\n" 449 "uptime %lu rem %u\n"
327 "boot normal %hhu\n" 450 "boot normal %hhu\n"
451 "disk serial %lx\n"
452 "disk start '%s'\n"
328 ), 453 ),
329 watchdog_long_limit, cur_watchdog_long, long_reboot_mode, 454 watchdog_long_limit, cur_watchdog_long, long_reboot_mode,
330 watchdog_short_limit, cur_watchdog_short, 455 watchdog_short_limit, cur_watchdog_short,
331 newboot_limit, cur_newboot, 456 newboot_limit, cur_newboot,
332 cur_oneshot, 457 cur_oneshot,
333 t.ticks, t.rem, 458 t.ticks, t.rem,
334 boot_normal_status 459 boot_normal_status,
335 ); 460 sd_serial,
461 conf_start);
336 } 462 }
337 463
338 static void 464 static void
339 cmd_set_params(const char *params) 465 cmd_set_params(const char *params)
340 { 466 {
881 LOCAL_PSTR(newboot); 1007 LOCAL_PSTR(newboot);
882 LOCAL_PSTR(oldboot); 1008 LOCAL_PSTR(oldboot);
883 LOCAL_PSTR(status); 1009 LOCAL_PSTR(status);
884 LOCAL_PSTR(random); 1010 LOCAL_PSTR(random);
885 LOCAL_PSTR(prog); 1011 LOCAL_PSTR(prog);
1012 LOCAL_PSTR(testsd);
886 LOCAL_HELP(set_params, "<long_limit> <short_limit> <newboot_limit>"); 1013 LOCAL_HELP(set_params, "<long_limit> <short_limit> <newboot_limit>");
887 LOCAL_HELP(set_key, "20_byte_hex>"); 1014 LOCAL_HELP(set_key, "20_byte_hex>");
888 LOCAL_HELP(oneshot, "<timeout>"); 1015 LOCAL_HELP(oneshot, "<timeout>");
889 LOCAL_HELP(prog, "<password>"); 1016 LOCAL_HELP(prog, "<password>");
890 LOCAL_HELP(random, "<admux> <nbytes>"); 1017 LOCAL_HELP(random, "<admux> <nbytes>");
891 LOCAL_HELP(hmac, "<key_index> <20_byte_hex_data>"); 1018 LOCAL_HELP(hmac, "<key_index> <20_byte_hex_data>");
892 LOCAL_HELP(decrypt, "<key_index> <20_byte_hmac|16_byte_aes_block>"); 1019 LOCAL_HELP(decrypt, "<key_index> <20_byte_hmac|16_byte_aes_block>");
1020 LOCAL_HELP(testsd, "<filename>");
893 1021
894 static const struct handler { 1022 static const struct handler {
895 PGM_P name; 1023 PGM_P name;
896 void(*cmd)(const char *param); 1024 void(*cmd)(const char *param);
897 // existence of arg_help indicates if the cmd takes a parameter. 1025 // existence of arg_help indicates if the cmd takes a parameter.
909 {set_params_str, cmd_set_params, set_params_help}, 1037 {set_params_str, cmd_set_params, set_params_help},
910 {set_key_str, cmd_set_avr_key, set_key_help}, 1038 {set_key_str, cmd_set_avr_key, set_key_help},
911 {random_str, cmd_random, random_help}, 1039 {random_str, cmd_random, random_help},
912 {vcc_str, cmd_vcc, NULL}, 1040 {vcc_str, cmd_vcc, NULL},
913 {reset_str, cmd_reset, NULL}, 1041 {reset_str, cmd_reset, NULL},
1042 {testsd_str, cmd_testsd, testsd_help},
914 {prog_str, cmd_prog, prog_help}, 1043 {prog_str, cmd_prog, prog_help},
915 }; 1044 };
916 1045
917 if (readbuf[0] == '\0') 1046 if (readbuf[0] == '\0')
918 { 1047 {