f337e42cbb25b7631b50d56beaf61f5a6a39da98
[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|=0xFF;  //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; //clock divider
43   TACTL |= MC_3;
44   _EINT();                              // Enable interrupts 
45 #endif
46 }
47
48 // Timer A0 interrupt service routine
49 interrupt(TIMERA0_VECTOR) Timer_A (void){
50   P5OUT&=~BIT7;//Glitch
51   //P5DIR=BIT7; //All else high impedance.
52   P5OUT|=BIT7;//Normal
53   TACTL |= MC0;// Stop Timer_A;
54   
55   return;
56 }
57
58
59 u16 glitchcount=0;
60
61 //! Glitch an application.
62 void glitchapp(u8 app){
63   debugstr("That app is not yet supported.");
64 }
65
66
67 //! Set glitching voltages.
68 void glitchvoltages(u16 gnd, u16 vcc){
69   int i;
70   //debugstr("Set glitching voltages: GND and VCC");
71   //debughex(gnd);
72   //debughex(vcc);
73   
74   /** N.B., because this is confusing as hell.  As per Page 86 of
75       SLAS541F, P6SEL is not what controls the use of the DAC0/DAC1
76       functions on P6.6 and P6.5.  Instead, CAPD or DAC12AMP>0 sets
77       the state.
78   */
79   
80   #ifdef DAC12IR
81   ADC12CTL0 = REF2_5V + REFON;                  // Internal 2.5V ref on
82   // Delay here for reference to settle.
83   for(i=0;i!=0xFFFF;i++) asm("nop");
84   DAC12_0CTL = DAC12IR + DAC12AMP_5 + DAC12ENC; // Int ref gain 1
85   DAC12_1CTL = DAC12IR + DAC12AMP_5 + DAC12ENC; // Int ref gain 1
86   // 1.0V 0x0666, 2.5V 0x0FFF
87   DAC12_0DAT = vcc; //high;
88   DAC12_1DAT = gnd; //low;
89   #endif 
90 }
91 //! Set glitching rate.
92 void glitchrate(u16 rate){
93   glitchcount=rate;
94 }
95
96 //! Handles a monitor command.
97 void glitchhandle(unsigned char app,
98                   unsigned char verb,
99                   unsigned long len){
100   P1OUT&=~1;
101   switch(verb){
102   case GLITCHVOLTAGES:
103     glitchvoltages(cmddataword[0],
104                    cmddataword[1]);
105     txdata(app,verb,0);
106     break;
107   case GLITCHRATE:
108     glitchrate(cmddataword[0]);
109     txdata(app,verb,0);
110     break;
111   case GLITCHVERB:
112     //FIXME parameters don't work yet.
113     glitchprime();
114     handle(cmddata[0],cmddata[1],0);
115     break;
116   case GLITCHTIME:
117     _DINT();//disable interrupts
118     TACTL=0; //clear dividers
119     TACTL|=TACLR; //clear config
120     TACTL|=TASSEL_SMCLK| //smclk source
121       MC_2; //continuout mode.
122     
123     //perform the function
124     silent++;//Don't want the function to return anything.
125     handle(cmddata[0],cmddata[1],0);
126     silent--;
127     cmddataword[0]=TAR; //Return counter.
128     txdata(app,verb,2);
129     break;
130   case START:
131     glitchvoltages(0xFFF,0);//Inverted VCC and GND.
132     P5OUT|=BIT7;//Normal
133     P5DIR|=BIT7;
134     while(1){
135       P5OUT&=~BIT7;//Glitch
136       //asm("nop");//asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");
137       asm("nop"); //Not necessary.
138       P5OUT|=BIT7;//Normal
139       asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");
140       asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");
141     }
142     txdata(app,verb,0);
143     break;
144   case STOP:
145   case GLITCHAPP:
146   default:
147     debugstr("Unknown glitching verb.");
148     txdata(app,NOK,0);
149   }
150 }