95653ff958042f2ff42589a9ab778b4d05b3bcdb
[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   //Don't forget to call glitchvoltages().
19   P5OUT|=0x80;
20   //Reconfigure TACTL.
21   TACTL=0;           //Clear dividers.
22   TACTL|=TACLR;      //Clear TimerA Config
23   TACTL|=
24     TASSEL_SMCLK |   //SMCLK source,
25     MC_1 |            //Count up to CCR0
26     TAIE;            //Enable Interrupt
27   CCTL0 = CCIE;                         // CCR0 interrupt enabled
28   CCR0 = glitchcount;
29   
30   //Enable general interrupts, just in case.
31   //_EINT();
32 #endif
33 }
34
35 //! Setup glitching.
36 void glitchsetup(){
37 #ifdef DAC12IR
38   //Set GSEL high to disable glitching.
39
40   P5DIR|=0x80;
41   P6DIR|=BIT6+BIT5;
42   
43   P5OUT|=0x80;
44   P6OUT|=BIT6+BIT5;
45
46   WDTCTL = WDTPW + WDTHOLD;             // Stop WDT
47   TACTL = TASSEL1 + TACLR;              // SMCLK, clear TAR
48   CCTL0 = CCIE;                         // CCR0 interrupt enabled
49   CCR0 = glitchcount;
50   TACTL |= MC1;                         // Start Timer_A in continuous mode
51   //TACTL |= MC0;                         // Stop Timer_A;
52   _EINT();                              // Enable interrupts 
53 #endif
54 }
55
56 // Timer A0 interrupt service routine
57 interrupt(TIMERA0_VECTOR) Timer_A (void)
58 {
59   P5OUT&=~BIT7;//Glitch
60   P5OUT|=BIT7;//Normal
61   TACTL |= MC0;                         // Stop Timer_A;
62   return;
63 }
64
65
66
67
68 u16 glitchcount=0;
69
70 //! Glitch an application.
71 void glitchapp(u8 app){
72   debugstr("That app is not yet supported.");
73 }
74
75
76 //! Set glitching voltages.
77 void glitchvoltages(u16 gnd, u16 vcc){
78   int i;
79   //debugstr("Set glitching voltages.");
80   
81   #ifdef DAC12IR
82   ADC12CTL0 = REF2_5V + REFON;                  // Internal 2.5V ref on
83   // Delay here for reference to settle.
84   for(i=0;i!=0xFFFF;i++) asm("nop");
85   DAC12_0CTL = DAC12IR + DAC12AMP_5 + DAC12ENC; // Int ref gain 1
86   DAC12_1CTL = DAC12IR + DAC12AMP_5 + DAC12ENC; // Int ref gain 1
87   // 1.0V 0x0666, 2.5V 0x0FFF
88   DAC12_0DAT = vcc; //high;
89   DAC12_1DAT = gnd; //low;
90   #endif 
91 }
92 //! Set glitching rate.
93 void glitchrate(u16 rate){
94   glitchcount=rate;
95 }
96
97 //! Handles a monitor command.
98 void glitchhandle(unsigned char app,
99                   unsigned char verb,
100                   unsigned long len){
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 }