aboutsummaryrefslogtreecommitdiff
path: root/indev/psn00bdbg-mk2/testutil/prompt.c
blob: e51e203d20b5909798301171b3df3fe2549fce16 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#ifdef _WIN32

#include <windows.h>
#include <conio.h>

#else

#include <sys/time.h>
#include <unistd.h>
#include <termios.h>
#include <signal.h>

#endif /* _WIN32 */

#include "cmdefs.h"
#include "main.h"

static int prompt_quit;
int UploadExec(const char *exefile);

#ifdef _WIN32

#define _kbhit		kbhit

#else
	
struct termios orig_term;

void enable_raw_mode()
{
    struct termios term;
    tcgetattr(0, &orig_term);
	
	term = orig_term;
    term.c_lflag &= ~(ICANON | ECHO); // Disable echo as well
    tcsetattr(0, TCSANOW, &term);
}

void disable_raw_mode()
{
    tcsetattr(0, TCSANOW, &orig_term);
}

int _kbhit()
{    
	struct timeval tv = { 0L, 0L };
    
	fd_set fds;
    FD_ZERO(&fds);
    FD_SET(0, &fds);
	
    return select(1, &fds, NULL, NULL, &tv);
	
} /* _kbhit */

void term_func(int signum)
{
	disable_raw_mode();
	exit( 0 );
	
} /* term_func */

#endif /* _WIN32 */

static void promptHelp( void )
{
    printf("?                         View help\n");
    printf("up <addr> <len> <file>    Upload memory from <file>\n");
    printf("down <addr> <len> <file>  Download memory to <file>\n");
    printf("rv <8|16|32> <addr>       Read a value from <addr>\n");
    printf("wv <8|16|32> <addr> <val> Write a value to <addr>\n");
	printf("sb <addr> <mode> [count]  Set program breakpoint at <addr>\n");
	printf("cb                        Clear all breakpoints\n");
	printf("lb                        List breakpoints\n");
	printf("sr <regname> <value>      Set register value\n");
    printf("stop                      Stop target\n");
    printf("cont                      Resume target\n");
    printf("stat                      Show target status\n");
    printf("r / regs                  Show registers\n");
    printf("t / trace                 Trace\n");
	printf("rt / runto <addr>         Run until <addr>\n");
	printf("tt / traceto <addr>       Run until <addr> or jump/branch\n");
    printf("reboot                    Reboot target and quit\n");
    printf("q / quit                  Quit\n\n");
    printf("Commands can be stacked as one keystroke like so:\n");
    printf("trace regs - Performs a trace operation then shows registers\n\n");
    
} /* promptHelp */

static char *getarg( const char *msg )
{
    char *arg;
    
    arg = strtok( NULL, " " );
    
    if( !arg )
    {
        printf( "Missing %s argument.\n", msg );
        return( NULL );
    }

    return( arg );
    
} /* getarg */

void promptMode( void )
{
	int		inkey;
	int		bytebuff;
    char    inbuff[100];
	int		inpos;
    int     inlen;
    char    *inptr;
    char    *arg;

    unsigned int addr;
    int len;
    const char *file;

    int wordsz;
    unsigned int value;
	
	int firsttime;
	int pollcount;
	int laststat;
	int pollstat;
	
#ifdef _WIN32
#else
	struct sigaction st;
#endif /* _WIN32 */
    
    printf( "Enter ? for list of directives.\n\n" );

    prompt_quit = 0;
	
#ifdef _WIN32
#else
	st.sa_handler = term_func;
	sigemptyset( &st.sa_mask );
	st.sa_flags = SA_RESTART;
	sigaction( SIGINT, &st, NULL );
	
	enable_raw_mode();
#endif /* _WIN32 */

	if( (laststat = dbGetStatusPoll()) < 0 )
	{
		printf("Error polling target status.\n");
		laststat = 0;
	}
    
    while( !prompt_quit )
    {
        memset( inbuff, 0, 100 );
#if 1//def _WIN32
        //gets( inbuff );
#else
        fgets( inbuff, 100, stdin );
        
        while( arg = strchr( inbuff, '\n' ) )  /* remove newlines from input */
        {
            *arg = 0;
        }
#endif

		printf("nDB>");
		inpos = 0;
		fflush(stdout);
		firsttime = 1;
		pollcount = 0;
		while(1)
		{
			if( commPendingBytes() > 0 )
			{
				/* routine that simply flushes the serial interface */
				bytebuff = commReadByte();
				printf("\rReceived byte: %02x\n", bytebuff);
				
				printf("nDB>%s", inbuff);
				fflush(stdout);
				pollcount = 0;
			}
			
			if( _kbhit() )
			{
#ifdef _WIN32
				inkey = getch();
				if( inkey == 13 )
#else
				inkey = getchar();
				if( inkey == '\n' )
#endif
				{
					fputc('\n', stdout);
					fflush(stdout);
					break;
				}
#ifdef _WIN32
				else if( inkey == 8 )
#else
				else if( inkey == 127 )	/* linux backspace */
#endif /* _WIN32 */
				{
					if( inpos > 0 )
					{
						fputc('\b', stdout);
						fputc(' ', stdout);
						fputc('\b', stdout);
						fflush(stdout);
						inpos--;
						inbuff[inpos] = 0;
						continue;
					}
				}
				else
				{
					if( inpos < 100 )
					{
						fputc(inkey, stdout);
						fflush(stdout);
						inbuff[inpos] = inkey;
						inpos++;
						inbuff[inpos] = 0;
						continue;
					}
				}
			}
#ifdef _WIN32
			Sleep(10);
#else
			usleep(1000);
#endif /* _WIN32 */

			pollcount++;
			if( pollcount > 10 )
			{
				if( (pollstat = dbGetStatusPoll()) < 0 )
				{
					printf("Error polling target status.\n");
				}
				else
				{
					if( pollstat != laststat )
					{
						laststat = pollstat;
						switch(pollstat)
						{
						  case DB_STAT_STOP:
						    printf("\rTarget stopped.\n");
						    break;
						  case DB_STAT_BREAK:
							printf("\rBreakpoint/trace complete.\n");
							break;
						  case DB_STAT_EXCEPT:
							printf("\rUnhandled exception.\n");
							break;
						  default:
							printf("\rUndefined target state (%d).\n", pollstat);
						}
						dbGetRegs();
						printf("nDB>%s", inbuff);
						fflush(stdout);
					}
				}
				pollcount = 0;
			}
		}

        if( ( inlen = strlen( inbuff ) ) == 0 )
            continue;

        inptr = inbuff;
        while( arg = strtok( inptr, " " ) )
        {
            inptr = NULL;
            
            /* quit parsing when end of string is reached */
            
            if( strcasecmp( "?", arg ) == 0 )
            {
                promptHelp();
            }
            else if( strcasecmp( "rv", arg ) == 0 ) /* read value */
            {
                if( !(arg = getarg( "word size" ) ) ) /* get word size arg */
                    break;
                sscanf( arg, "%d", &wordsz );

                if( !(arg = getarg( "address" ) ) )  /* get address arg */
                    break;
                sscanf( arg, "%x", &addr );

                switch( wordsz )
                {
                case 8:
                    wordsz = 0;
                    break;
                case 16:
                    wordsz = 1;
                    break;
                case 32:
                    wordsz = 2;
                    break;
                default:
                    printf( "Unknown word size: %d\n", wordsz );
                    wordsz = -1;
                }

                if( wordsz >= 0 )
                {
                    value = dbReadValue( wordsz, addr );
                    switch( wordsz )
                    {
                    case 0:
                        printf( "%08X=%02X\n", addr, value );
                        break;
                    case 1:
                        printf( "%08X=%04X\n", addr, value );
                        break;
                    case 2:
                        printf( "%08X=%08X\n", addr, value );
                        break;
                    }
                }
            }
            else if( strcasecmp( "wv", arg ) == 0 ) /* read value */
            {
                if( !(arg = getarg( "word size" ) ) ) /* get word size arg */
                    break;
                sscanf( arg, "%d", &wordsz );

                if( !(arg = getarg( "address" ) ) )  /* get address arg */
                    break;
                sscanf( arg, "%x", &addr );

                if( !(arg = getarg( "value" ) ) )  /* get value */
                    break;
                sscanf( arg, "%x", &value );

                switch( wordsz )
                {
                case 8:
                    wordsz = 0;
                    break;
                case 16:
                    wordsz = 1;
                    break;
                case 32:
                    wordsz = 2;
                    break;
                default:
                    printf( "Unknown word size: %d\n", wordsz );
                    wordsz = -1;
                }

                dbWriteValue( wordsz, addr, value );
            }
			else if( strcasecmp( "exe", arg ) == 0 ) /* upload executable */
            {
                if( !(file = getarg( "file name" ) ) )  /* get file arg */
                    break;
                    
                UploadExec(file);
				
				laststat = DB_STAT_STOP;
            }
            else if( strcasecmp( "up", arg ) == 0 ) /* upload memory */
            {
                if( !(arg = getarg( "address" ) ) ) /* get address arg */
                    break;
                sscanf( arg, "%x", &addr );
                
                if( !(arg = getarg( "length" ) ) )  /* get length arg */
                    break;
                sscanf( arg, "%d", &len );

                if( !(file = getarg( "file name" ) ) )  /* get file arg */
                    break;

                printf( "Performing memory upload of %d byte(s) to %08X from %s\n",
                    len, addr, file );
                    
                dbUploadMemFile( addr, file, len );
            }
            else if( strcasecmp( "down", arg ) == 0 ) /* download memory */
            {
                if( !(arg = getarg( "address" ) ) ) /* get address arg */
                    break;
                sscanf( arg, "%x", &addr );
                
                if( !(arg = getarg( "length" ) ) )  /* get length arg */
                    break;
                sscanf( arg, "%d", &len );

                if( !(file = getarg( "file name" ) ) )  /* get file arg */
                    break;

                printf( "Performing memory dump of %d byte(s) from %08X to %s\n",
                    len, addr, file );
                    
                dbGetMem( addr, len, file );
            }
			else if( strcasecmp("sb", arg) == 0 ) /* set breakpoint */
			{
				if( !(arg = getarg("address") ) ) /* get address arg */
                    break;
                sscanf(arg, "%x", &addr);
				if( !(arg = getarg("flag") ) )	/* get flag arg */
                    break;
                sscanf(arg, "%d", &value);
				if( value == 2 )
				{
					if( !(arg = getarg("count") ) )	/* get flag arg */
						break;
					sscanf(arg, "%d", &len);
				}
				else
				{
					len = 0;
				}
				dbSetBreak(addr, value, len);
			}
			else if( strcasecmp("cb", arg) == 0 ) /* clear breakpoints */
			{
				dbClearBreaks();
			}
			else if( strcasecmp("lb", arg) == 0 )
			{
				dbGetBreaks();
			}
            else if( strcasecmp( "stat", arg ) == 0 ) /* get target status */
            {
                dbGetStatus();
            }
            else if( strcasecmp( "stop", arg ) == 0 ) /* stop target */
            {
                dbSetExec( CMD_EXEC_STOP );
            }
            else if( strcasecmp( "cont", arg ) == 0 ) /* continue target */
            {
                dbSetExec( CMD_EXEC_RESUME );
				laststat = DB_STAT_RUN;
            }
            else if( ( strcasecmp( "t", arg ) == 0 ) ||
                ( strcasecmp( "trace", arg ) == 0 ) )
            {
                dbSetExec( CMD_EXEC_STEP );
				laststat = DB_STAT_RUN;
            }
			else if( ( strcasecmp( "rt", arg ) == 0 ) || /* runto address */
                ( strcasecmp( "runto", arg ) == 0 ) )
            {
                if( !(arg = getarg( "program address" ) ) ) /* get address arg */
                    break;
                sscanf( arg, "%x", &addr );
				dbRunTo(addr, 0);
				laststat = DB_STAT_RUN;
            }
			else if( ( strcasecmp( "tt", arg ) == 0 ) || /* traceto address */
                ( strcasecmp( "traceto", arg ) == 0 ) )
            {
                if( !(arg = getarg( "program address" ) ) ) /* get address arg */
                    break;
                sscanf( arg, "%x", &addr );
				dbRunTo(addr, 1);
				laststat = DB_STAT_RUN;
            }
            else if( ( strcasecmp( "r", arg ) == 0 ) || /* get registers */
                ( strcasecmp( "regs", arg ) == 0 ) )
            {
                dbGetRegs();
            }
			else if( strcasecmp("sr", arg) == 0 )	/* set register value */
			{
				if( !(arg = getarg("register") ) ) /* get address arg */
                    break;
                file = arg;
				if( !(arg = getarg("value") ) )	/* get flag arg */
                    break;
                sscanf(arg, "%x", &value);
				dbSetReg(file, value);
			}
            else if( strcasecmp( "reboot", arg ) == 0 )
            {
                dbReboot();
                prompt_quit = 1;
                break;
            }
            else if( ( strcasecmp( "q", arg ) == 0 ) || /* quit program */
                ( strcasecmp( "quit", arg ) == 0 ) )
            {
                prompt_quit = 1;
                break;
            }
            else
            {
                printf( "Unknown directive: %s\n", arg );
            }
			/* delay before executing next directive */
#ifdef _WIN32
			Sleep(100);
#else
			usleep(1000);
#endif /* _WIN32 */
        }
    }

#ifdef _WIN32
#else	
	disable_raw_mode();
#endif /* _WIN32 */
    
} /* promptMode */