PageRenderTime 640ms CodeModel.GetById 331ms app.highlight 95ms RepoModel.GetById 100ms app.codeStats 0ms

/0.3/command.c

https://github.com/ice799/rr0d
C | 820 lines | 590 code | 182 blank | 48 comment | 18 complexity | 3f73b5dc2526b5560bad72b3ae6043ef MD5 | raw file
  1/*
  2 * This file is part of the Rasta Ring 0 debug
  3 *
  4 * Copyright (C) 2004 Droids Corporation.
  5 * based on Deblin debuger
  6 *
  7 * $Id: command.c,v 1.31 2006-06-30 12:25:35 serpilliere Exp $
  8 *
  9 */
 10
 11
 12#include "var_globale.h"
 13#include "command.h"
 14#include "utils.h"
 15#include "import_symb.h"
 16
 17/* without that gcc complains on FreeBSD */
 18
 19
 20char cmd_result[80];			/* buffer for entered command */
 21unsigned int adresse_ret_1;
 22unsigned int adresse_ret_2;
 23
 24
 25unsigned int edit_mode=0;
 26
 27#define STATE_ADD			1000000
 28
 29#define STATE_NO_COMMAND	1
 30#define STATE_COMMAND		2
 31#define STATE_HEXA		3
 32#define STATE_RASTA		4
 33#define STATE_UNRASTA		5
 34#define STATE_INVALID		1337
 35#define STATE_END		666
 36
 37#define CMD_VER				1000000
 38#define CMD_E_DATA			1000001
 39#define CMD_TEST			1000002
 40#define CMD_E_CODE			1000003
 41#define CMD_RASTA			1000004
 42#define CMD_UNRASTA			1000005
 43#define CMD_SET				1000006
 44
 45#define CMD_PARSEREG		        1000100
 46#define CMD_HELP			1000007
 47#define CMD_BPX				1000008
 48#define CMD_BC				1000009
 49#define CMD_BL				1000010
 50#define CMD_IDT				1000011
 51#define CMD_BPM				1000012
 52#define CMD_LOAD_SYMB			1000013
 53#define CMD_PAGE_INFO			1000014
 54#define CMD_SYMB			1000015
 55#define CMD_PRINT			1000016
 56#define CMD_EDITMEM			1000017
 57#define CMD_INTHOOK			1000018
 58#define CMD_RDTSCHOOK			1000019
 59#define CMD_F001			1000020
 60#define CMD_UNF001			1000021
 61
 62#define CMD_E_DATA_END   		1100001
 63#define CMD_E_CODE_END			1100003
 64#define CMD_BPX_END			1100008
 65#define CMD_BC_END			1100009
 66#define CMD_SET_END			1100006
 67#define CMD_BPM_END			1100010
 68#define CMD_PAGE_INFO_END		1100014
 69
 70#define CMD_PRINT_END   		1100016
 71
 72#define CMD_INTHOOK_END			1100018
 73
 74
 75#define CMD_LOAD_SYMB_BIS		1100024
 76#define CMD_LOAD_SYMB_END		1100025
 77
 78int nbr_arg=0;
 79#define MAX_CMD 20
 80
 81/*table des commande possibles                    */
 82
 83char *table_command[]=
 84			{
 85			"VER",
 86			"D",
 87			"DROIDS",
 88			"U",
 89			"RASTA",
 90			"UNRASTA",
 91			"R",
 92			"HELP",
 93			"BPX",
 94			"BC",
 95			"BL",
 96			"IDT",
 97			"BPM",
 98			"LOAD",
 99			"PAGE",
100			"SYMB",
101			"PRINT",
102			"E",
103			"INTHOOK",
104			"RDTSCHOOK",
105 			"f001",
106 			"unf001",
107			0
108			};
109
110/*tables des r�ponses aux commandes possibles     */
111
112char *command_res[]=
113			{
114			"RasTa Ring0 DebUgger v0.3 zeta ",
115			"D adresse, man                 ",
116			"RuLEZ                          ",
117			"U adresse, man                 ",
118			"Rasta mode On (sweet, man)     ",
119			"Rasta mode Off(keep rasta, man)",
120			"SET data, man                  ",
121			"need some help man?            ",
122			"soft break, man                ", 
123			"del break, man                 ",
124			"bp green list, man             ",
125			"idt                            ",
126			"hard break point, man          ",
127			"Load Symbol man                ",
128			"Info on pages man              ",
129			"Info on symbols, man           ",
130			"Print reg                      ",
131			"Edit the memory man            ",
132			"ON/OFF hook interrupt N        ",
133			"ON/OFF hook rdtsc              ",
134 			"���۲�� 0xf001 ON  ���۲��     ",
135 			"���۲�� 0xf001 OFF ���۲��     ",
136
137			};
138
139
140
141
142
143/*table des registres possibles en argument       */
144
145char *table_reg[]=
146			{
147			"EAX",
148			"EBX",
149			"ECX",
150			"EDX",
151			"ESI",
152			"EDI",
153			"EIP",
154			"ESP",
155			"EBP",
156			"DR0",
157			"DR1",
158			"DR2",
159			"DR3",
160			"DR6",
161			"DR7",
162			0
163			};
164
165/*compare 2 chaine de char                        */
166
167/* int cmp_string(char *text,char *buf) */
168/* { */
169/* 	if (! (text && buf) ) */
170/* 		return 0; */
171/* 	/\* */
172/* 	if ( (*text) != (*buf) ) */
173/* 		return 0; */
174/* 	*\/ */
175	
176/* 	while (( (*text) && (*buf)) &&  */
177/* 	       ( ((*text) == (*buf))|| */
178/* 		 ((*text | 0x20) == (*buf | 0x20)) )) */
179/* 	{ */
180/* 		text++; */
181/* 		buf++; */
182/* 	} */
183	
184
185/* 	return ( (*text == *buf) || (*text == *buf)); */
186
187/* } */
188
189/*renvoie le num de commande entr�e               */
190
191int chose_command(char* command)
192{
193	int i=0;
194
195
196	while( table_command[i] != 0 )
197	{
198		if (!rr0d_stricmp(command, table_command[i]))
199		{
200			rr0d_strcpy(cmd_result, command_res[i]);
201			//	printf("pouet 1");
202			return i+STATE_ADD;
203		}
204
205		i++;
206
207	}
208
209	//			printf("BAD 1");
210
211	rr0d_strcpy(cmd_result, "parse: invalid cmd, man");
212	return 0;
213}
214
215/*renvoie le num du registre entr�                */
216
217int chose_reg(char* reg)
218{
219	int i=0;
220
221	while(table_reg[i] != 0)
222	{
223	  if (!rr0d_strnicmp(reg, table_reg[i], 3))
224		{
225			return i;
226		}
227	  i++;
228
229	}
230	return -1;
231}
232
233
234unsigned int chose_symb(char* symb)
235{
236  int i;
237  
238  i = 0;
239  while(tab_import_symb[i].address && (i<MAX_SYMBOL))
240    {
241
242      if (!rr0d_strnicmp(symb, tab_import_symb[i].name, rr0d_strlen(tab_import_symb[i].name)))
243	{
244	  return i;
245	}      
246      
247      i++;
248    }
249  return -1;
250}
251
252
253
254unsigned int adresse_tmp =0;
255unsigned int next_state = 0;
256
257unsigned int num_reg;
258
259/*automate reconnaissant le langage de rr0d       */
260/*parse_ret est l'entier repr�sentant la commande */
261/*pars�e                                          */
262/*adresse_ret1 est l'argument 1 de la commande    */
263/*pars�e (peut ne pas etre utilis�)               */
264/*adresse_ret2 est l'argument 2 de la commande    */
265/*pars�e (peut ne pas etre utilis�)               */
266
267int parse_command(char* command)
268{
269	char c;
270	int i=0;
271	int state = STATE_NO_COMMAND;
272	int ret;
273	int num_s;
274	int rr;
275	int fini=0;
276	int parse_ret=0;
277
278	char* start_cmd = (char*)0;
279
280	char* start_reg = (char*)0;
281
282
283	fini=0;
284	while(!fini)
285	{
286
287		c = command[i];
288		switch(state)
289		{
290			
291
292			case STATE_NO_COMMAND:
293				switch(c)
294				{
295				case ' ':
296				case  0:
297					i++;
298					break;
299
300				default:
301					start_cmd = &command[i];
302					i++;
303					state = STATE_COMMAND;
304					break;
305				}
306				break;
307
308
309			case STATE_COMMAND:
310				switch(c)
311				{
312				case  0:
313				case ' ':
314				  command[i]=0;
315				
316					i++;
317					ret = chose_command(start_cmd);
318					if (!ret)
319					{
320						state = STATE_INVALID;
321						break;
322					}
323
324					state = ret;
325
326					break;
327
328				default:
329					i++;
330					break;
331				}
332				break;
333
334
335
336			case CMD_E_DATA:
337				switch(c)
338				{
339				case ' ':
340					i++;
341					break;
342
343				case  0:
344					state = STATE_INVALID;
345					break;
346
347				default:
348					state = STATE_HEXA;
349					next_state = CMD_E_DATA_END;
350					adresse_tmp=0;
351					break;
352
353				}
354				break;
355
356			case CMD_E_CODE:
357				switch(c)
358				{
359				case ' ':
360					i++;
361					break;
362
363				case  0:
364					state = STATE_INVALID;
365					break;
366
367				default:
368					state = STATE_HEXA;
369					next_state = CMD_E_CODE_END;
370					adresse_tmp=0;
371					break;
372
373				}
374				break;
375
376
377			case STATE_HEXA:
378			  if ((rr = chose_reg(&command[i]))!=-1)
379			    {
380			      switch(rr)
381				{
382				case 0:
383				  adresse_tmp = reg_stack->reg_EAX;
384				  break;
385				case 1:
386				  adresse_tmp = reg_stack->reg_EBX;
387				  break;
388				case 2:
389				  adresse_tmp = reg_stack->reg_ECX;
390				  break;
391				case 3:
392				  adresse_tmp = reg_stack->reg_EDX;
393				  break;
394				case 4:
395				  adresse_tmp = reg_stack->reg_ESI;
396				  break;
397				case 5:
398				  adresse_tmp = reg_stack->reg_EDI;
399				  break;
400				case 6:
401				  adresse_tmp = reg_stack->reg_EIP;
402				  break;
403				case 7:
404				  adresse_tmp = reg_stack->reg_ESP;
405				  break;
406				case 8:
407				  adresse_tmp = reg_stack->reg_EBP;
408				  break;
409
410				default:
411				  adresse_tmp = 0xDEADFACE;
412				  break;
413				}
414
415			      i+=3;
416
417
418			      
419			    }
420			  else 
421			  if ((num_s = chose_symb(&command[i]))!=-1)
422			    {
423			      adresse_tmp = tab_import_symb[num_s].address;
424			      i+=rr0d_strlen(tab_import_symb[num_s].name);
425			      break;
426			      
427			    }else
428			      {
429				if ((c >= 'A') && (c <= 'F') )
430				{
431					c = c - 'A' + 10;
432					adresse_tmp= adresse_tmp<<4;
433					adresse_tmp+=c;
434					i++;
435					break;
436				}
437				if ((c >= 'a') && (c <= 'f') )
438				{
439					c = c - 'a' + 10;
440					adresse_tmp= adresse_tmp<<4;
441					adresse_tmp+=c;
442					i++;
443					break;
444				}
445
446				if ((c >= '0') && (c <= '9') )
447				{
448					c = c - '0';
449					adresse_tmp= adresse_tmp<<4;
450					adresse_tmp+=c;
451					i++;
452					break;
453				}
454			    }
455
456			  state = next_state;
457			  break;
458			
459			
460			case CMD_E_DATA_END:
461				adresse_ret_1 = adresse_tmp;
462				state = STATE_END;
463				parse_ret = 2;
464				break;
465
466			case CMD_E_CODE_END:
467				adresse_ret_2 = adresse_tmp;
468				state = STATE_END;
469				parse_ret = 3;
470				break;
471
472			case CMD_RASTA:
473				fini = 1;
474				parse_ret = 4;
475				break;
476
477			case CMD_UNRASTA:
478				fini = 1;
479				parse_ret = 5;
480				break;
481
482			case CMD_SET:
483				switch(c)
484				{
485				case ' ':
486					i++;
487					break;
488
489				case  0:
490					state = STATE_INVALID;
491					break;
492
493				default:
494					start_reg = &command[i];
495					state = CMD_PARSEREG;
496					next_state = STATE_HEXA;
497					adresse_tmp=0;
498					parse_ret = 6;
499					break;
500
501				}
502				
503
504				break;
505
506			case CMD_PARSEREG:
507				switch(c)
508				{
509				case ' ':
510					command[i]=0;
511					if ( (num_reg=chose_reg(start_reg))==-1)
512					{
513						state = STATE_INVALID;
514						break;
515					}
516
517					adresse_ret_2 = num_reg;
518
519					i++;
520					state = next_state;
521					next_state = CMD_SET_END;
522					
523					break;
524
525					/*
526				case  0:
527					state = STATE_INVALID;
528					break;
529					*/
530
531				default:
532					i++;
533					break;
534
535				}
536				break;
537
538
539
540			case CMD_SET_END:
541				adresse_ret_1 = adresse_tmp;
542				fini =1;
543				break;
544
545			case STATE_INVALID:
546				fini = 1;
547				parse_ret = 0;
548				break;
549
550
551			case STATE_END:
552				fini = 1;
553				break;
554
555			case CMD_VER:
556			case CMD_TEST:
557				fini = 1;
558				parse_ret = 1;
559				break;
560
561			
562
563			case CMD_HELP:
564				fini = 1;
565				parse_ret = 8;
566				break;
567
568			case CMD_BPX:
569				switch(c)
570				{
571				case ' ':
572					i++;
573					break;
574
575				case  0:
576					state = STATE_INVALID;
577					break;
578
579				default:
580					state = STATE_HEXA;
581					next_state = CMD_BPX_END;
582					adresse_tmp=0;
583					break;
584
585				}
586				break;
587
588			case CMD_BPM:
589				switch(c)
590				{
591				case ' ':
592					i++;
593					break;
594
595				case  0:
596					state = STATE_INVALID;
597					break;
598
599				default:
600					state = STATE_HEXA;
601					next_state = CMD_BPM_END;
602					adresse_tmp=0;
603					break;
604
605				}
606				break;
607
608			case CMD_PAGE_INFO:
609				switch(c)
610				{
611				case ' ':
612					i++;
613					break;
614
615				case  0:
616					state = STATE_INVALID;
617					break;
618
619				default:
620					state = STATE_HEXA;
621					next_state = CMD_PAGE_INFO_END;
622					adresse_tmp=0;
623					break;
624
625				}
626				break;
627
628			case CMD_BC:
629				switch(c)
630				{
631				case ' ':
632					i++;
633					break;
634
635				case  0:
636					state = STATE_INVALID;
637					break;
638
639				default:
640					state = STATE_HEXA;
641					next_state = CMD_BC_END;
642					adresse_tmp=0;
643					break;
644
645				}
646					break;
647
648
649			case CMD_LOAD_SYMB:
650				switch(c)
651				{
652				case ' ':
653					i++;
654					break;
655
656				case  0:
657					state = STATE_INVALID;
658					break;
659
660				default:
661					state = STATE_HEXA;
662					next_state = CMD_LOAD_SYMB_BIS;
663					adresse_tmp=0;
664					break;
665
666				}
667				break;
668			case CMD_LOAD_SYMB_BIS:
669				switch(c)
670				{
671				case ' ':
672					i++;
673					break;
674
675				case  0:
676					state = STATE_INVALID;
677					break;
678
679				default:
680					state = STATE_HEXA;
681					adresse_ret_1 = adresse_tmp;
682					next_state = CMD_LOAD_SYMB_END;
683					adresse_tmp=0;
684					break;
685
686				}
687				break;
688
689
690			case CMD_INTHOOK:
691				switch(c)
692				{
693				case ' ':
694					i++;
695					break;
696
697				case  0:
698					state = STATE_INVALID;
699					break;
700
701				default:
702					state = STATE_HEXA;
703					next_state = CMD_INTHOOK_END;
704					adresse_tmp=0;
705					break;
706
707				}
708				break;
709
710
711
712			case CMD_BPX_END:
713				adresse_ret_1 = adresse_tmp;
714				state = STATE_END;
715				parse_ret = 9;
716				break;
717		
718
719			case CMD_BC_END:
720				adresse_ret_1 = adresse_tmp;
721				state = STATE_END;
722				parse_ret = 10;
723				break;
724
725			case CMD_BL:
726				fini = 1;
727				parse_ret = 11;
728				break;
729
730			case CMD_IDT:
731				fini = 1;
732				parse_ret = 12;
733				break;
734
735			case CMD_BPM_END:
736				adresse_ret_1 = adresse_tmp;
737				state = STATE_END;
738				parse_ret = 13;
739				break;
740
741
742
743			case CMD_LOAD_SYMB_END:
744				adresse_ret_2 = adresse_tmp;
745				state = STATE_END;
746				parse_ret = 14;
747				break;
748
749			case CMD_PAGE_INFO_END:
750				adresse_ret_1 = adresse_tmp;
751				state = STATE_END;
752				parse_ret = 15;
753				break;
754
755			case CMD_SYMB:
756				fini = 1;
757				parse_ret = 16;
758				break;
759
760
761			case CMD_PRINT:
762			  fini = 1;
763			  //				command[i]=0;
764			  if ( (num_reg=chose_reg(&command[i]))==-1)
765			    {
766			      state = STATE_INVALID;
767			      break;
768			    }
769			  
770			  parse_ret = 0xff;
771			  adresse_ret_2 = num_reg;
772			  
773			  break;
774
775			case CMD_EDITMEM:
776				fini = 1;
777				parse_ret = 17;
778				edit_mode = !edit_mode;
779				break;
780
781
782			case CMD_INTHOOK_END:
783				adresse_ret_1 = adresse_tmp;
784				state = STATE_END;
785				parse_ret = 18;
786				break;
787
788			case CMD_RDTSCHOOK:
789				fini = 1;
790				parse_ret = 19;
791				break;
792
793 			case CMD_F001:
794			        fini = 1;
795 				parse_ret = 20; /* f001 mode on*/
796				break;
797 			case CMD_UNF001:
798 				fini = 1;
799 				parse_ret = 21; /* f001 mode off */
800 				break;
801
802			  
803
804			default:
805				fini = 1;
806				parse_ret = 0;
807				break;
808
809			
810
811
812
813		}
814	}
815
816
817	return parse_ret;
818
819
820}