]> git.cryptolib.org Git - avr-crypto-lib.git/blob - test_src/dump.c
forgot hfal_shabval.*
[avr-crypto-lib.git] / test_src / dump.c
1 /* dump.c */
2 /*
3     This file is part of the AVR-Crypto-Lib.
4     Copyright (C) 2008  Daniel Otte (daniel.otte@rub.de)
5
6     This program is free software: you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation, either version 3 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19 /**
20  * \file     dump.c
21  * \email    daniel.otte@rub.de
22  * \author   Daniel Otte 
23  * \date     2009-02-04
24  * \license  GPLv3 or later
25  * 
26  */
27
28 #include <stdint.h>
29 #include <string.h>
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <ctype.h>
33 #include <avr/pgmspace.h>
34 #include "cli.h" 
35 #include "string-extras.h" 
36  
37 #define DUMP_WIDTH 16
38
39 static
40 void dump_chars(uint8_t* buffer){
41         uint8_t i;
42         cli_putc('|');
43         for(i=0; i<DUMP_WIDTH; ++i){
44                 if(isprint(buffer[i])){
45                         cli_putc(buffer[i]);
46                 }else{
47                         cli_putc('.');
48                 }
49         }
50         cli_putc('|');
51 }
52
53
54 void dump(char* s){
55         uint8_t flash=1;
56         uint32_t addr=0;
57         uint32_t size=128;
58         uint8_t i,buffer[DUMP_WIDTH];
59         char tstr[9];
60         s=strstrip(s);
61         if(*s=='r' || *s=='R' || *s=='m' || *s=='M')
62                 flash=0;
63         if(isalpha(*s)){
64                 while(isalpha(*s))
65                         ++s;
66         }
67         char* eptr;
68         if(*s)
69                 addr = strtoul(s, &eptr, 0);
70         if(eptr)
71                 size = strtoul(eptr, NULL, 0);
72         if(!size)
73                 size = 32;      
74         
75         cli_putstr_P(PSTR("\r\ndumping "));     
76         ultoa(size, tstr, 10);
77         cli_putstr(tstr);
78         cli_putstr_P(PSTR(" bytes of "));
79         cli_putstr_P(flash?PSTR("flash"):PSTR("ram"));
80         cli_putstr_P(PSTR(", beginning at 0x"));        
81         ultoa(addr, tstr, 16);
82         cli_putstr(tstr);
83         cli_putstr_P(PSTR(":\r\n"));    
84         while(size>=DUMP_WIDTH){
85                 if(flash){
86                         for(i=0; i<DUMP_WIDTH; ++i){
87 #ifdef pgm_read_byte_far                                
88                                 buffer[i]=pgm_read_byte_far(addr+i);
89 #else
90                                 buffer[i]=pgm_read_byte(addr+i);
91 #endif
92                         }
93                 }else{
94                         memcpy(buffer, (void*)((uint16_t)addr), DUMP_WIDTH);
95                 }
96                 ultoa(addr, tstr, 16);
97                 sprintf(tstr,"%6lX", addr);
98                 cli_putstr(tstr);
99                 cli_putstr_P(PSTR(": "));       
100                 cli_hexdump2(buffer, DUMP_WIDTH);
101                 cli_putc('\t');
102                 dump_chars(buffer);
103                 addr+=DUMP_WIDTH;
104                 size-=DUMP_WIDTH;
105                 cli_putstr_P(PSTR("\r\n"));
106         }
107         if(size){
108                 if(flash){
109                         for(i=0; i<size; ++i){
110 #ifdef pgm_read_byte_far                                
111                                 buffer[i]=pgm_read_byte_far(addr+i);
112 #else
113                                 buffer[i]=pgm_read_byte(addr+i);
114 #endif
115                         }
116                 }else{
117                         memcpy(buffer, (void*)((uint16_t)addr), size);
118                 }
119                 ultoa(addr, tstr, 16);
120                 sprintf(tstr,"%6lX", addr);
121                 cli_putstr(tstr);
122                 cli_putstr_P(PSTR(": "));       
123                 cli_hexdump2(buffer, size);
124                 cli_putstr_P(PSTR("\r\n"));
125         }
126 }
127