Protecting my program

Go To Last Post
18 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi

How do i lock my program from future parties extracting it from the AVR (Mega162). I know i have to set some fuses/lock bits but which ones?

Thanks justint

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The lock bits. There are good details on this in the data sheet.

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Search for the following in the ATMEGA162 datasheet ( http://www.atmel.com/dyn/resourc... ) and read that section

Quote:
The ATmega162 provides six Lock bits which can be left unprogrammed (“1”) or can be programmed (“0”) to obtain the additional features listed in Table 97.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ah i see what you talking about but in avr studio 4 this must be done by selecting the lock bits.
If i select "further programming disabled" will that prevent any one from extracting the program from the chip...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

If i select "further programming disabled" will that prevent any one from extracting the program from the chip...

If you actually open up the data sheet and read the section you where pointed to, what does it say?

And as my extrasensory powers signal that we are about to enter the subject "How safe are the lock bits?" be advised that this would be instance #3856 or so of that discussion. Think up some nice search terms (eg "protect firmware", "lock bits", "read protected flash" etc) and feed them to the search function in this forum (link at top of page).

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If you don't want the code to be readable from the device the option is "further programming and verification disabled" (that is LB0=0, LB1=0)

Last Edited: Wed. Oct 1, 2008 - 09:38 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

it talks about 6 lock bits and their configuration for different functions.
The avr studio however doesnt have 6 bits in the programming section but has 12 options. It also talks about programming the fuse bits before some of the lock bits leading me to think that i have to do other things before programming the fuse bits.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

thank you

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You can find Cliff's answer as LB Mode 3 in table 97 I referenced.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Following form the previous posts.. until now, I have been setting the fuses and lock bits in AVR studio 4 menu.

I have read in the data sheet about programming to set these bits via programming i.e. LB1 and LB2 both 0 to disable all further programming and verification.

How do I set these bits? I can see what they are called but cannot find what register they are in. I was expecting to be able to read the register, change whatever bits and write back to the same register. Is there a different format for bits of this type?

Im programming in assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The Bootloader Lock Bits (BLB12, BLB11, BLB02, BLB01) can be modified via software running inside the AVR itself. The procedure is briefly described in the datasheet on page 225. Basically, you:

  1. load CPU register R0 with the your desired bootloader lock bit combination,
  2. set the BLBSET and SPMEN bits in register SPMCR
  3. execute the SPM instruction.

(Note that this code will only work if the SPM op-code is fetched from an area of Flash within the Bootloader Section. Also note that if your device doesn't have any bootloader-like functionality, then making changes to the BLB lock bits is a useless gesture anyway.)

However, the overall lock bits (LB2, LB1) are the ones that you're probably more concerned with, because they regulate the ability to read out code using the main external programming interfaces (ISP/HVPP/JTAG). These lock bits can only be modified via an external programmer using a tool such as an AVRISP mkII.

Although the lock bits are indeed physically implemented as 6 individual bits inside the AVR, AVR Studio does not give you the ability to directly program each of the bits individually. Instead, AVR Studio presents you with a series of three drop-down menus presenting only the particular combinations of lock bits, for each of the three categories of lock bits, which have useful real-world applications.

As explained in the datasheet, the three categories of lock bits are:

  • LB2:LB1 controlling external interfaces
  • LB02:01 controlling the ability to use a bootloader to read/write the application section
  • LB12:11, controlling the ability of the bootloader to overwrtite itself, and the ability of the Application section to inspect the bootloader

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks, that has helped.
I am in the process of finalising a program for production but was hoping that the manufacturer of the board would just need to flash the chip with the Hex file. Will he now need to set the fuses and lock bits via studio 4 (as I have been doing) for each board he flashes?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

Will he now need to set the fuses and lock bits via studio 4 (as I have been doing) for each board he flashes?

Wouldn't he be programming fuses to enabled BOD anyway? What about clocks or are you happy with int RC at 1MHz?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
Will he now need to set the fuses and lock bits via studio 4 (as I have been doing) for each board he flashes?

You can use batch file with STK500.exe. It is one click for flash, eeprom and fuses.

George.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

How to I apply a batch file to do this?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

How to I apply a batch file to do this?

Do you mean "what commands do I put in the batch file?" or "how do I invoke the batch file to execute those commands?"

For the commands just drop to a command prompt in \program files\atmel\avr tools\stk500 and type "stk500 -h". As you'll see that gives some example invocations:

Examples:
    STK500.EXE -dATmega128 -e -ifFlash.hex -pf -vf
    STK500.EXE -dATmega128 -fF73A -FF73A -EFF -GFF

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Erm both, think Ill need to do a bit of research on batch files, have never used them...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Just use your favourite editor to create a text fie that contains (for example):

    "\program files\atmel\avr tools\stk500\STK500.EXE" -dATmega128 -e -ifFlash.hex -pf -vf 
    "\program files\atmel\avr tools\stk500\STK500.EXE" -dATmega128 -fF73A -FF73A -EFF -GFF

save this on your hard drive as "program.bat", one easy option would simply be to create/save it on the Desktop in fact. If you now double-click the file it should be the case that Windows recognises the .bat filetype and passes it to a command line process which will then execute each command within the file in turn.

Obviously don't use this exact example as you need to change the -d to the type of AVR you use, the -if to the name of the project .hex file and the -f/-F to the upper/lower fuse bytes you want to use and the -E/-G to the extended fuse byte you want to use

To test out .bat files maybe don't start with something as "dangerous" as actually reprogramming AVRs but instead put something like:

@echo off
echo hello world
pause

in a test.bat and then try double clicking it.

(you can also run .bat files from the command prompt (in fact this is where they were originally designed to be used) so if you start a command prompt and navigate to the directory where the .bat file is located and then give the command "test.bat" you should see it executed at the command prompt)

You can invoke any .exe from within a .bat file just by giving its name (possibly also its location if it won't be found in DOS's PATH). DOS itself also has a number of commands built in (like echo and pause) that can be used.

A line that starts with '@' will not be echoed to the screen when a command is executed and the command "echo off" says that no following commands should be shown on screen. echo followed by any other words WILL output those words to the screen (e.g "echo hello world"). Pause cause DOS to wait for a key to be hit before continuing.

Cliff