dc79d31eddf807aabf8dcd31f6468d448267a1da
[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   //Set to high voltage.
19   DAC12_0DAT = glitchH;
20   
21   //Reconfigure TACTL.
22   TACTL=0;           //Clear dividers.
23   TACTL|=TACLR;      //Clear TimerA Config
24   TACTL|=
25     TASSEL_SMCLK |   //SMCLK source,
26     MC_1;            //Count up to CCR0
27   //TAIE;            //Enable Interrupt
28   CCTL0 = CCIE;                         // CCR0 interrupt enabled
29   CCR0 = glitchcount;
30   
31   //Enable general interrupts, just in case.
32   _EINT();
33 #endif
34 }
35
36 //! Setup glitching.
37 void glitchsetup(){
38 #ifdef DAC12IR
39   //Set GSEL high to disable glitching.
40
41   P5DIR|=0x80;
42   P6DIR|=0x40;  
43   
44   P5OUT|=0x80;
45   P6OUT|=0x40;
46   
47   glitchsetupdac();
48
49   WDTCTL = WDTPW + WDTHOLD;             // Stop WDT
50   TACTL = TASSEL1 + TACLR;              // SMCLK, clear TAR
51   CCTL0 = CCIE;                         // CCR0 interrupt enabled
52   CCR0 = glitchcount;
53   TACTL |= MC1;                         // Start Timer_A in continuous mode
54   _EINT();                              // Enable interrupts 
55 #endif
56 }
57
58 //! Setup analog chain for glitching.
59 void glitchsetupdac(){
60   glitchvoltages(glitchL,glitchH);
61 }
62
63 // Timer A0 interrupt service routine
64 interrupt(TIMERA0_VECTOR) Timer_A (void)
65 {
66 #ifdef DAC12IR
67   //debugstr("Glitching.");
68   DAC12_0DAT = 0;//glitchL;
69   asm("nop");
70   asm("nop");
71   asm("nop");
72   asm("nop");
73   asm("nop");
74   
75   DAC12_0DAT = glitchH;
76   //DAC12_0DAT = glitchL;
77   /*
78   switch(glitchstate){
79   case 0:
80     P1OUT|=1;
81     glitchstate=1;
82     DAC12_0DAT = glitchH;
83     break;
84   case 1:
85     P1OUT|=1;
86     glitchstate=0;
87     DAC12_0DAT = glitchL;
88     break;
89   default:
90     P1OUT&=~1;
91     //Do nothing.
92     break;
93   }
94   */
95 #endif
96 }
97
98
99
100
101 u16 glitchH=0xfff, glitchL=0xfff,
102   glitchstate=2, glitchcount=0;
103
104 //! Glitch an application.
105 void glitchapp(u8 app){
106   debugstr("That app is not yet supported.");
107 }
108
109
110 //! Set glitching voltages.
111 void glitchvoltages(u16 low, u16 high){
112   int i;
113   glitchH=high;
114   glitchL=low;
115   
116   //debugstr("Set glitching voltages.");
117   
118   #ifdef DAC12IR
119   ADC12CTL0 = REF2_5V + REFON;                  // Internal 2.5V ref on
120   // Delay here for reference to settle.
121   for(i=0;i!=0xFFFF;i++) asm("nop");
122   DAC12_0CTL = DAC12IR + DAC12AMP_5 + DAC12ENC; // Int ref gain 1
123   // 1.0V 0x0666, 2.5V 0x0FFF
124   DAC12_0DAT = high;
125   //DAC12_0DAT = 0x0880;
126   #endif 
127 }
128 //! Set glitching rate.
129 void glitchrate(u16 rate){
130   glitchcount=rate;
131 }
132
133 //! Handles a monitor command.
134 void glitchhandle(unsigned char app,
135                   unsigned char verb,
136                   unsigned long len){
137   switch(verb){
138   case GLITCHVOLTAGES:
139     glitchvoltages(cmddataword[0],
140                    cmddataword[1]);
141     txdata(app,verb,0);
142     break;
143   case GLITCHRATE:
144     glitchrate(cmddataword[0]);
145     txdata(app,verb,0);
146     break;
147   case GLITCHVERB:
148     //FIXME parameters don't work yet.
149     glitchprime();
150     handle(cmddata[0],cmddata[1],0);
151     break;
152   case GLITCHTIME:
153     _DINT();//disable interrupts
154     TACTL=0; //clear dividers
155     TACTL|=TACLR; //clear config
156     TACTL|=TASSEL_SMCLK| //smclk source
157       MC_2; //continuout mode.
158     
159     //perform the function
160     silent++;//Don't want the function to return anything.
161     handle(cmddata[0],cmddata[1],0);
162     silent--;
163     cmddataword[0]=TAR; //Return counter.
164     txdata(app,verb,2);
165     break;
166   case START:
167   case STOP:
168   case GLITCHAPP:
169   default:
170     debugstr("Unknown glitching verb.");
171     txdata(app,NOK,0);
172   }
173 }