7de962c4554e4839a60e87eb361d848767150d73
[goodfet] / firmware / apps / glitch / glitch.c
1 /*! \file glitch.c
2   \author Travis Goodspeed
3   \brief Glitching Support for GoodFET20
4   
5   See the TI example MSP430x261x_dac12_01.c for usage of the DAC.
6   This module sends odd and insufficient voltages on P6.6/DAC0
7   in order to bypass security restrictions of target devices.
8 */
9
10 #include "platform.h"
11 #include "command.h"
12 #include "glitch.h"
13
14 //! Disable glitch state at init.
15 void glitchsetup(){
16 #ifdef DAC12IR
17   //Set GSEL high to disable glitching.
18
19   P5DIR|=0x80;
20   P6DIR|=0x40;  
21   
22   P5OUT|=0x80;
23   P6OUT|=0x40;
24   
25   glitchsetupdac();
26
27   WDTCTL = WDTPW + WDTHOLD;             // Stop WDT
28   TACTL = TASSEL1 + TACLR;              // SMCLK, clear TAR
29   CCTL0 = CCIE;                         // CCR0 interrupt enabled
30   CCR0 = glitchcount;
31   TACTL |= MC1;                         // Start Timer_A in continuous mode
32   _EINT();                              // Enable interrupts 
33 #endif
34 }
35
36 //! Setup analog chain for glitching.
37 void glitchsetupdac(){
38   glitchvoltages(glitchL,glitchH);
39 }
40
41 // Timer A0 interrupt service routine
42 interrupt(TIMERA0_VECTOR) Timer_A (void)
43 {
44 #ifdef DAC12IR
45   switch(glitchstate){
46   case 0:
47     P1OUT|=1;
48     glitchstate=1;
49     DAC12_0DAT = glitchH;
50     break;
51   case 1:
52     P1OUT|=1;
53     glitchstate=0;
54     DAC12_0DAT = glitchL;
55     break;
56   default:
57     P1OUT&=~1;
58     //Do nothing.
59     break;
60   }
61   CCR0 += glitchcount;                        // Add Offset to CCR0
62 #endif
63 }
64
65
66
67
68 u16 glitchH=0xfff, glitchL=0xfff,
69   glitchstate=2, glitchcount=0;
70
71 //! Glitch an application.
72 void glitchapp(u8 app){
73   debugstr("That app is not yet supported.");
74 }
75
76
77 //! Set glitching voltages.
78 void glitchvoltages(u16 low, u16 high){
79   int i;
80   glitchH=high;
81   glitchL=low;
82   
83   
84   #ifdef DAC12IR
85   ADC12CTL0 = REF2_5V + REFON;                  // Internal 2.5V ref on
86   // Delay here for reference to settle.
87   for(i=0;i!=0xFFFF;i++) asm("nop");
88   DAC12_0CTL = DAC12IR + DAC12AMP_5 + DAC12ENC; // Int ref gain 1
89   // 1.0V 0x0666, 2.5V 0x0FFF
90   DAC12_0DAT = high;
91   //DAC12_0DAT = 0x0880;
92   #endif 
93 }
94 //! Set glitching rate.
95 void glitchrate(u16 rate){
96   glitchcount=rate;
97 }
98
99
100 //! Handles a monitor command.
101 void glitchhandle(unsigned char app,
102                   unsigned char verb,
103                   unsigned long len){
104   switch(verb){
105   case GLITCHVOLTAGES:
106     glitchvoltages(cmddataword[0],
107                    cmddataword[1]);
108     txdata(app,verb,0);
109     break;
110   case GLITCHRATE:
111     glitchrate(cmddataword[0]);
112     txdata(app,verb,0);
113     break;
114   case START:
115   case STOP:
116   case GLITCHAPP:
117   case GLITCHVERB:
118   default:
119     debugstr("Unknown glitching verb.");
120     txdata(app,NOK,0);
121   }
122 }