CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

code protection

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Linuxbuilders



Joined: 20 Mar 2010
Posts: 193
Location: Auckland NZ

View user's profile Send private message

code protection
PostPosted: Sat May 19, 2012 1:25 am     Reply with quote

Hi,

chip 18f4620
ccs 4.114


Code:
#FUSES PROTECT


I understand that if I would like to try to read my program then I should get all zeros, at least that is what I could find around. Is it correct? After I have set the fuse the option "-Y" in "C:\PK2CMDv1-20\pk2cmd.exe" "-M" "-PPIC18F4620" "-FG:\FIRMWARE\main.hex" "-H5" "-BC:\PK2CMDv1-20" "-R" has stopped working. When I try to read program with use of MPLAB I get reading from 0x0000 to 0x0800 then gap of NOPs, when I check configuration bits with mplab I get this:


http://www.nzstonemason.com/images/configbits.jpg

May I ask someone to give me some good explanation how all of it works and what the protected areas of the program are? I am not sure now if the data read from 0x0000-0x0800 is my code or not, why can I read it? I have looked into datasheet but it is not making much more sense to me. How can I protect data/EEPROM memory too?

Thank you for all help in advance.
_________________
Help "d" others and then you shell receive some help from "d" others.
Ttelmah



Joined: 11 Mar 2010
Posts: 19496

View user's profile Send private message

PostPosted: Sat May 19, 2012 2:56 am     Reply with quote

Now, I have no idea what programmer you are using (you don't tell us), or which bit actually gets set by what you are trying... For what the fuses do, _read the data sheet_. There are perhaps a dozen fuses controlling individual 'parts' of the code protection. For example, you can specify that other parts of your _code_ can read a block, but it is protected from reads by the external programmer. There are several fuses allowing this to be done for individual blocks of memory. This is a _vital_ ability, since _all_ constant data, and the values used to initialise variables are stored in the ROM, and the code _must_ be able to access this. I suspect you have turned on the 'read protection' bit for the main code area, but not the 'boot block' (the low 800 addresses selectable separately, for use by a bootloader) - (which will result on '0' being read), but have not enabled the code to be able to read the block you have protected, hence it won't work....

Assuming you haven't got a bootloader, you need to set EBTR0 to EBTR3 to '1', EBTRB to '1', CP0 to CP3 to '0', CPB to '0', WRT0 to WRT3 to '0' (assuming you are not using program memory to store values yourself from the code), WRTB to '0', and WRTC to '0'.
If you are writing values to program memory from the code, then you will need to enable the WRT bit corresponding to the area involved. I have also ignored the EEPROM at this point.

Now, the 'danger' of allowing table reads, and writes, comes if you have a bootloader present, and this is not itself using an encryption check. What people can do (since the bootloader _must_ be able to write other blocks), is write a small program into the code somewhere, which then reads the rest of the code back. The route round this, is to not allow table _reads_ from the bootloader area (prevents it from being able to verify the code - but also prevents this route 'in'....).

Best Wishes
Linuxbuilders



Joined: 20 Mar 2010
Posts: 193
Location: Auckland NZ

View user's profile Send private message

PostPosted: Sat May 19, 2012 4:04 am     Reply with quote

Thnx, pickit2 I use. I do not use boot loader, I plan in the future but not at this stage. Thanx for help. I need to do some digging now and work out how to protect entire program then. I do not self write into program memory because I have EEPROM for configuration. I would like to protect it too if it is possible.

As per datasheet I need to set register 23-6 and 23-7 and then 23-10 and 23-11 and then ID bits, etc. How do I set it manually? I could not find where CCS is defining those registers under "protect fuse", it is not in the device file. Could someone please let me know how to manually set registers then? I need to set READ PROTECTION bits then, I want to be able to override program and eeprom if required or wipe the chip out and load new soft on it. I just need to make impossible to read the program from it and from the EEPROM.

Thank you for help.
_________________
Help "d" others and then you shell receive some help from "d" others.
Ttelmah



Joined: 11 Mar 2010
Posts: 19496

View user's profile Send private message

PostPosted: Sat May 19, 2012 7:45 am     Reply with quote

The fuses are all there in CCS, just with names (supposedly) designed to make them easier to understand. So you have 'CPB' - code protect bootblock, 'PROTECT' - protects the main code block, then the write protect ones, as 'WRTx' where 'x' is the area to protect, and 'NOWRTx' disable write protection on area 'x', 'TR' - table reads allowed (this is the one I suspect you need 'on'), etc. etc..
You can always look at the end of the LST file to see exactly what bits are set/cleared by the CCS fuses, and the .h file has a list of the ones for your processor.

Best Wishes
Linuxbuilders



Joined: 20 Mar 2010
Posts: 193
Location: Auckland NZ

View user's profile Send private message

PostPosted: Sat May 19, 2012 4:45 pm     Reply with quote

thnx for that, will play with it and let you know how it goes.
_________________
Help "d" others and then you shell receive some help from "d" others.
Linuxbuilders



Joined: 20 Mar 2010
Posts: 193
Location: Auckland NZ

View user's profile Send private message

PostPosted: Sat May 26, 2012 2:52 am     Reply with quote

Hi, (Icannot put whole progam here because nobody have weeks to read it and I cannot do it for commercial reasons too so please try to understand what I am writing here)

[I am not using boot loader]

done my testing and:

Code:
#FUSES NOPROTECT    // code protect
#FUSES NOCPB           // boot block code protect
#FUSES NOCPD           // data eeprom code protect
#FUSES NOEBTRB        // boot block table code protect
#FUSES NOEBTR          // table code protect


However:

On the top of my program I have such thing like this:

Code:
// INCLUDE ==========================           
//#include <flex_lcd.c> -> included above
#include "variables.c" 
#include "setsystem.c"
#include "display_messages.c"
#include "romrw.c"
#include "timer.c"         //loop timer and set flag each time
#include "delays.c"               
#include "commstx.c"                   
#include "crc.c"
#include "encryption.c"         
#include "decryption.c"
#include "commsrx.c"
#include "printing.c"
#include "i2c.c"
#include "rtc.c"
#include "emergency_stop.c" 
#include "airflow.c"
#include "io.c"     
#include "process_tx.c"
#include "lcd.c"
#include "adc.c"       
#include "enckeys.c"
#include "addressing.c"
#include "health_check.c"
#include "process_rx.c"
#include "opmode.c"
#include "bootup.c"
#include "LCD_messages.c"
#include "errors.c"                                 
#include "logics.c"
                                                     
//#include <>         
//===================================


where:

display_messages.c is:

Code:
// LCD messages

void press_and_hold_reset_button(void) {
   printf (lcd_putc, "\fPRESS and HOLD  ");
   printf (lcd_putc, "\nRESET button!   ");
}

void error_cleared_stopped_now(void) {
   printf (lcd_putc, "\fERROR Cleared   ");
   printf (lcd_putc, "\nSTOPPED NOW!    ");               
}   

void press_and_hold_start_button(void) {               
   printf (lcd_putc, "\fPRESS and HOLD  ");
   printf (lcd_putc, "\nSTART BUTTON!   ");               
}

void starting_reset_now(void) {
   printf (lcd_putc, "\fSTARTING...     ");             
   printf (lcd_putc, "\nRESTART NOW!    ");
}

void cabinet_stopped_now(void) {
   printf (lcd_putc, "\fSTOPPED NOW!    ");
   printf (lcd_putc, "\nPRESS START!    ");
}   



so when I turn on EBTRB protection then above is not executed at all, it gets compiled but is not executed. When I turn off protection then it does work :(

in this scenario:
Code:

#FUSES PROTECT    // code protect
#FUSES CPB        // boot block code protect
#FUSES CPD        // data eeprom code protect
#FUSES NOEBTRB    // boot block table code protect
#FUSES NOEBTR     // table code protect


I cannot read program memory but I can read my EEPROM, why?

Any idea why? How is this possible that processor cannot execute the part of program or compiler is not putting it somehow in there?

thnx for help in advance.
_________________
Help "d" others and then you shell receive some help from "d" others.
temtronic



Joined: 01 Jul 2010
Posts: 9221
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sat May 26, 2012 4:59 am     Reply with quote

Assuming you're using MPLAB to compile ...

MPLAB has options for the 'fuses' that might be overwriting the CCS C code 'options'

or

your programming software might be doing that(don't know which programmer you're using).

You should cut a really,really small program that shows the problem that others can cut/paste/test.probably the quickest way to figure out this head scratcher.
Linuxbuilders



Joined: 20 Mar 2010
Posts: 193
Location: Auckland NZ

View user's profile Send private message

PostPosted: Sat May 26, 2012 5:05 am     Reply with quote

I am using native CCS PCWHD + IDE 4.114 and pk2cmd.exe app plus PICkit2 for programming because I am running W7 64 bit.

Very hard to paste small program because I have dual processor PCB with so much code that it would be a killer. Small program may not show the problem too because of its size.

What I suspect is:
1. problem is somehow related to bootloader reserved space which I use for normal program because I do not use boot loader at all at this stage
2. ccs bug most probably

a fact that I cannot protect EEPROM from reading even when FUSE is on would suggest some dodgy thing happenig during compilation.

I do not know assembler very well so I am not able to debug it this way.
Maybe someone could point me for what to look in the assembly code so I could find it and post here.

thnx
_________________
Help "d" others and then you shell receive some help from "d" others.
dyeatman



Joined: 06 Sep 2003
Posts: 1933
Location: Norman, OK

View user's profile Send private message

PostPosted: Sat May 26, 2012 8:04 am     Reply with quote

TTelmah mentioned the fuses breakout at the end of the listing. How about showing us what you have?

Example:
Code:
Configuration Fuses:
   Word  1: 2FE2   HS NOWDT PUT MCLR NOPROTECT NOCPD BROWNOUT IESO FCMEN NOLVP NODEBUG
   Word  2: 3FFF   BORV40 NOWRT

_________________
Google and Forum Search are some of your best tools!!!!
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Sat May 26, 2012 12:55 pm     Reply with quote

You're not giving us anything to work on so that we can try to help.

A really short section of compilable code,which shows the problem, is the way forward.

Mike
Linuxbuilders



Joined: 20 Mar 2010
Posts: 193
Location: Auckland NZ

View user's profile Send private message

PostPosted: Sat May 26, 2012 7:46 pm     Reply with quote

dyeatman wrote:
TTelmah mentioned the fuses breakout at the end of the listing. How about showing us what you have?

Example:
Code:
Configuration Fuses:
   Word  1: 2FE2   HS NOWDT PUT MCLR NOPROTECT NOCPD BROWNOUT IESO FCMEN NOLVP NODEBUG
   Word  2: 3FFF   BORV40 NOWRT


Code:
Configuration Fuses:
   Word  1: C600   H4 FCMEN IESO
   Word  2: 161E   PUT BROWNOUT BORV20 NOWDT WDT2048
   Word  3: 8700   CCP2C1 PBADEN LPT1OSC MCLR
   Word  4: 0081   STVREN NOLVP NOXINST NODEBUG
   Word  5: 8000   PROTECT CPB NOCPD
   Word  6: E00F   NOWRT NOWRTC NOWRTB NOWRTD
   Word  7: 400F   NOEBTR NOEBTRB


Code:
#FUSES PROTECT    // code protect
#FUSES CPB        // boot block code protect
#FUSES CPD        // data eeprom code protect
#FUSES NOEBTRB    // boot block table code protect
#FUSES NOEBTR     // table code protect


found a fault with CPD - had another entry few lines below. I will try to replicate problem with LCD messages in some short program. Thnx

Thnx
_________________
Help "d" others and then you shell receive some help from "d" others.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group