Go Down

Topic: lock bits questions (Read 446 times) previous topic - next topic

joedodo

Jan 03, 2021, 05:22 am Last Edit: Jan 03, 2021, 06:13 am by joedodo
Hi,
I am working on a 328p based system, and would like to protect the application code from reading (externally). I don't need the bootloader so I programed the 328p with an uno as an isp programmer.

I would like to set the lock bits.

I did some search for a week or so and found there are a lot information about fuses but little about the lock bits.

From what I have got, I am a little bit confused about the lock bits of this chip. In the datasheet, it said there are 8 bits and there are two bits in charge of the protection of the application part (lb1 lb2), and will form 3 modes.

But in most threads I have read using avrdue to set them, like this -Ulock:w:0x00:m -u. And some people said one need to modify the value from 0x3f to 0x0f to lock it, and others mentioned that unlock is 0xFF, lock is 0xCF.

Could some one give more details about this? Why there are two Hex number to set two bits? What's the right way to do it?

I am using a uno as isp programmer. And I would like to test if I can still read the hex file from it or not once I have set the lock bits.

What I have tried is the following:

C:\avr\bin>c:\avr/bin/avrdude -Cc:\avr/etc/avrdude.conf -v -patmega328p -cstk500v1 -PCOM8 -b19200 -e -Ulock:w:0x0f:m -u

and the system gets back as:
avrdude: Version 6.3-20190619
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
         Copyright (c) 2007-2014 Joerg Wunsch

         System wide configuration file is "c:\avr/etc/avrdude.conf"

         Using Port                    : COM8
         Using Programmer              : stk500v1
         Overriding Baud Rate          : 19200
         AVR Part                      : ATmega328P
         Chip Erase delay              : 9000 us
         PAGEL                         : PD7
         BS2                           : PC2
         RESET disposition             : dedicated
         RETRY pulse                   : SCK
         serial program mode           : yes
         parallel program mode         : yes
         Timeout                       : 200
         StabDelay                     : 100
         CmdexeDelay                   : 25
         SyncLoops                     : 32
         ByteDelay                     : 0
         PollIndex                     : 3
         PollValue                     : 0x53
         Memory Detail                 :

                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           eeprom        65    20     4    0 no       1024    4      0  3600  3600 0xff 0xff
           flash         65     6   128    0 yes     32768  128    256  4500  4500 0xff 0xff
           lfuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           hfuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           efuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           lock           0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           calibration    0     0     0    0 no          1    0      0     0     0 0x00 0x00
           signature      0     0     0    0 no          3    0      0     0     0 0x00 0x00

         Programmer Type : STK500
         Description     : Atmel STK500 Version 1.x firmware
         Hardware Version: 2
         Firmware Version: 1.18
         Topcard         : Unknown
         Vtarget         : 0.0 V
         Varef           : 0.0 V
         Oscillator      : Off
         SCK period      : 0.1 us

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.03s

avrdude: Device signature = 0x1e950f (probably m328p)
avrdude: erasing chip
avrdude: reading input file "0x0f"
avrdude: writing lock (1 bytes):

Writing | ################################################## | 100% 0.03s

avrdude: 1 bytes of lock written
avrdude: verifying lock memory against 0x0f:
avrdude: load data lock data from input file 0x0f:
avrdude: input file 0x0f contains 1 bytes
avrdude: reading on-chip lock data:

Reading | ################################################## | 100% 0.02s

avrdude: verifying ...
avrdude: 1 bytes of lock verified

avrdude done.  Thank you.
_____________________
So did I set the lock bits succeefully?

After that I downloaded the blink example to the 328p chip using the same wiring and the same uno as isp prorammer in the arduino ide, and the blinking led is back on the chip again. I checked the output of the ide:

d:\Program Files (x86)\Arduino\hardware\tools\avr/bin/avrdude -Cd:\Program Files (x86)\Arduino\hardware\tools\avr/etc/avrdude.conf -v -patmega328p -cstk500v1 -PCOM8 -b19200 -Uflash:w:C:\Users\xxxx\AppData\Local\Temp\arduino_build_996885/Blink.ino.hex:i

Looks to me there is no erase operation (-e). I assume if I had set the lock bit to lock(0f), the only way to write the flash is to erase it first...

jremington

#1
Jan 03, 2021, 05:51 am Last Edit: Jan 03, 2021, 05:54 am by jremington
Your best source of information is the data sheet section on the lock bits. Obviously, some of the people whose posts you quote are confused.

It is not entirely clear what you want to do, but to disable program memory reads from the bootloader code, that would appear to be BLB0 mode 4.

In this mode:
Quote
LPM executing from the Boot Loader section is not allowed to
read from the Application section. If Interrupt Vectors are placed
in the Boot Loader section, interrupts are disabled while
executing from the Application section.

joedodo

Your best source of information is the data sheet section on the lock bits. Obviously, some of the people whose posts you quote are confused.

It is not entirely clear what you want to do, but to disable program memory reads from the bootloader code, that would appear to be BLB0 mode 4.

In this mode:
Thanks, I just want to avoid my applicatioin code from reading from external because I don't have a bootloader. So you mean the right way is to set the lock to 0x0F?

jremington

#3
Jan 03, 2021, 06:43 am Last Edit: Jan 03, 2021, 06:46 am by jremington
My recommendation concerned reading program memory from the bootloader memory section, which evidently does not apply to your case.

If this is a commercial venture, you need to do some experimentation to fully understand the functionality of the lock bits, then decide what is best for your application.  Also, consult AVRFreaks forum members. Those people live and breathe details like these.

Quote
And I would like to test if I can still read the hex file from it or not once I have set the lock bits.
To test that, attempt to read the program memory and check whether the expected values are returned.


joedodo

My recommendation concerned reading program memory from the bootloader memory section, which evidently does not apply to your case.

If this is a commercial venture, you need to do some experimentation to fully understand the functionality of the lock bits, then decide what is best for your application.  Also, consult AVRFreaks forum members. Those people live and breathe details like these.
To test that, attempt to read the program memory and check whether the expected values are returned.
I am learning how to use mcus and might find a related job later. I know arduino is for beginners and I will certainly learn more including stm32 series. To be honest my goal is to learn fpga later.

jremington

#5
Jan 03, 2021, 05:51 pm Last Edit: Jan 03, 2021, 05:54 pm by jremington
Then why are you worrying about protecting the machine code?  Keep in mind that Arduino was created in the spirit of open source, as a learning and teaching tool.

In any case, a reasonably skilled programmer can reverse engineer an Arduino application program, and write the equivalent C/C++ more quickly than interpreting a machine code dump.

hzrnbgy

#6
Jan 03, 2021, 06:55 pm Last Edit: Jan 03, 2021, 06:56 pm by hzrnbgy
Here's what the lockbits look like from my Atmega328P









joedodo

Here's what the lockbits look like from my Atmega328P









That's very clear! Thanks. I now understand the relation between the hex code and the 8 bits. May I ask the name of this tool you are using? Looks like you can edit the bits from the drawdown list?

hzrnbgy

I'm using Atmel Studio. It's Microchip/Atmel professional IDE for AVR microcontrollers

https://www.microchip.com/mplab/avr-support

You do need to have an ISP programmer. Not sure if "Arduino as ISP" would work with the IDE. The dropdowns make everything easy. You don't need to know the details, it automatically adjust the "HEX code" for whatever option you select

yasinzaii

Here's what the lockbits look like from my Atmega328P




I know how to access lock bytes using avrdude but I didn't know about this UI application. Whats the name of this app
Author of Adduino.com. Loves to talk and share about Microcontrollers.

hzrnbgy


westfw

Quote
Code: [Select]
Atmel Studio
How'd you get Studio to be in "dark mode" ?

westfw

#12
Jan 05, 2021, 02:31 am Last Edit: Jan 05, 2021, 02:31 am by westfw
Quote
So you mean the right way is to set the lock to 0x0F?
Fuses default to ones ("unprogrammed"), which is minimal protection of the chip.
It follows logically that "maximum" protection is going to be programming ALL of the fuses, ie value 0x0.
This matches the datasheet:


Note that since you do not have a bootloader, and have erased the bootloader by uploading via ISP, the settings of the other 4 bits of "Boot Lock Bits" (BLBxx) are irrelevant.  All you really care about are the two "lock bits." (LBx)


hzrnbgy

 

Code: [Select]
How'd you get Studio to be in "dark mode" ?



There's a built-in theme under Tools > Options




yasinzaii

Code: [Select]
How'd you get Studio to be in "dark mode" ?



There's a built-in theme under Tools > Options




Thanks
Author of Adduino.com. Loves to talk and share about Microcontrollers.

Go Up