4c4c6cf63d30416e26eb1d09b7a29f148b12453b
[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
15 //! Call this before the function to be glitched.
16 void glitchprime(){
17 #ifdef DAC12IR
18   WDTCTL = WDTPW + WDTHOLD;             // Stop WDT
19   
20   glitchsetup();
21   _EINT();
22   return;
23 #endif
24 }
25
26 //! Setup glitching.
27 void glitchsetup(){
28 #ifdef DAC12IR
29   //Set GSEL high to disable glitching.
30   
31   //Normal voltage, use resistors instead of output.
32   //P5DIR=0x80;   //ONLY glitch pin is output.
33   P5OUT|=0x80;  //It MUST begin high.
34   //P5REN|=0x7F;  //Resistors pull high and low weakly.
35   
36   P6DIR|=BIT6+BIT5;
37   P6OUT|=BIT6+BIT5;
38   
39   WDTCTL = WDTPW + WDTHOLD;             // Stop WDT
40   TACTL = TASSEL1 + TACLR;              // SMCLK, clear TAR
41   CCTL0 = CCIE;                         // CCR0 interrupt enabled
42   CCR0 = glitchcount+0x15;              // Compare Value
43   TACTL |= MC_2;                        // continuous mode.
44 #endif
45 }
46
47 // Timer A0 interrupt service routine
48 interrupt(TIMERA0_VECTOR) Timer_A (void){
49   P5OUT&=~BIT7;//Glitch
50   //P5DIR=BIT7; //All else high impedance.
51   P5OUT|=BIT7;//Normal
52   TACTL |= MC0;// Stop Timer_A;
53   return;
54 }
55
56
57 u16 glitchcount=0;
58
59 //! Glitch an application.
60 void glitchapp(u8 app){
61   debugstr("That app is not yet supported.");
62 }
63
64
65 //! Set glitching voltages.
66 void glitchvoltages(u16 gnd, u16 vcc){
67   int i;
68   //debugstr("Set glitching voltages: GND and VCC");
69   //debughex(gnd);
70   //debughex(vcc);
71   
72   /** N.B., because this is confusing as hell.  As per Page 86 of
73       SLAS541F, P6SEL is not what controls the use of the DAC0/DAC1
74       functions on P6.6 and P6.5.  Instead, CAPD or DAC12AMP>0 sets
75       the state.
76   */
77   
78   #ifdef DAC12IR
79   ADC12CTL0 = REF2_5V + REFON;                  // Internal 2.5V ref on
80   // Delay here for reference to settle.
81   for(i=0;i!=0xFFFF;i++) asm("nop");
82   DAC12_0CTL = DAC12IR + DAC12AMP_5 + DAC12ENC; // Int ref gain 1
83   DAC12_1CTL = DAC12IR + DAC12AMP_5 + DAC12ENC; // Int ref gain 1
84   // 1.0V 0x0666, 2.5V 0x0FFF
85   DAC12_0DAT = vcc; //high;
86   DAC12_1DAT = gnd; //low;
87   #endif 
88 }
89 //! Set glitching rate.
90 void glitchrate(u16 rate){
91   glitchcount=rate;
92 }
93
94 //! Handles a monitor command.
95 void glitchhandle(unsigned char app,
96                   unsigned char verb,
97                   unsigned long len){
98   P1OUT&=~1;
99   switch(verb){
100   case GLITCHVOLTAGES:
101     glitchvoltages(cmddataword[0],
102                    cmddataword[1]);
103     txdata(app,verb,0);
104     break;
105   case GLITCHRATE:
106     glitchrate(cmddataword[0]);
107     txdata(app,verb,0);
108     break;
109   case GLITCHVERB:
110     //FIXME parameters don't work yet.
111     glitchprime();
112     handle(cmddata[0],cmddata[1],0);
113     break;
114   case GLITCHTIME:
115     _DINT();//disable interrupts
116     TACTL=0; //clear dividers
117     TACTL|=TACLR; //clear config
118     TACTL|=TASSEL_SMCLK| //smclk source
119       MC_2; //continuous mode.
120     
121     //perform the function
122     silent++;//Don't want the function to return anything.
123     handle(cmddata[0],cmddata[1],0);
124     silent--;
125     cmddataword[0]=TAR; //Return counter.
126     txdata(app,verb,2);
127     break;
128   case START:
129     //Testing mode, for looking at the glitch waveform.
130     glitchvoltages(0xFFF,0);//Inverted VCC and GND.
131     P5OUT|=BIT7;//Normal
132     P5DIR|=BIT7;
133     while(1){
134       P5OUT&=~BIT7;//Glitch
135       //asm("nop");//asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");
136       asm("nop"); //Not necessary.
137       P5OUT|=BIT7;//Normal
138       asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");
139       asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");
140     }
141     txdata(app,verb,0);
142     break;
143   case STOP:
144   case GLITCHAPP:
145   default:
146     debugstr("Unknown glitching verb.");
147     txdata(app,NOK,0);
148   }
149 }