Koodin salaus Atmel 328p - palkkio

Moi kaikki

Kyselinkin kuukausi sitten, miten koodin salaus Atmega 328p tehdään, ja sain hyviä
vinkkejä. Eli avrdudella lock bitit kohdalleen. No yritin, ja EHKÄ onnistuin, vaan en
tiedä. Haluaisin asiasta varmuuden, ja siksi olisin valmis maksamaan nimellisen
korvauksen (20-30e?) jos joku antaisi täsmäohjeet ja keinon verifioida suojaus.

Ajan koodin avrdudella sisään seuraavalla komennolla:

avrdude -c usbtiny -p m328p -e -U flash:w:koodi.hex

Ja hyvin menee. Tuo -e pyyhkäisee piirin ensin tyhjäksi ja sitten
ladataan .hex -koodi.

Tämän jälkeen pitäisi asettaa lock bitit paikalleen, em. avrdudella, ja
minun pitäisi päästä tarkistamaan, että näin kävi. Eli haluan ostaa
kaksi avrduden komentoriviä… Olisiko kukaan myymässä halvalla
know how’ta :astonished: ? Lockitus pitäisi saada aikaan, ja minun pitäisi
olla varma, että lukossa on.

Jos joku haluaa vastata, niin suoralla viestillä, tai sitten
kari.stachon@kolumbus.fi (ja mikäli potentiaalinen auttaja sallii,
niin julkaisen ratkaisun kaikille tällä foorumilla, vaikka “pöydän alta”
vähän voisin maksaakin). Tämä saitti meinaan on hyvä!

-kari

Ite en oo avrdudea käyttänyt. Vain AVR Studiota.

Mutta tarkoituksena ohjelmoida nämä bitit nolliksi.
“Jos tarkoitus on suojata Flash muistiin kirjoitettu koodi, niin ohejlmoi bitit LB0 ja LB1, niin et voi ton jälkeen lukea piiriltä etkä kirjoittaa sarja tai rinnakkais liitännöillä.”

Moi,

Salaus taitaa mennä näin:

Pistää lukituksen päälle koodia testcode.hex varten:

C:>avrdude -p m328p -c usbtiny -B10 -D -U flash:w:testcode.hex -U lock:w:0x3f:m

avrdude: Setting SCK period to 10 usec
avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.00s

avrdude: Device signature = 0x1e950f
avrdude: reading input file “testcode.hex”
avrdude: input file testcode.hex auto detected as Intel Hex
avrdude: writing flash (3490 bytes):

Writing | ################################################## | 100% 2.05s

avrdude: 3490 bytes of flash written
avrdude: verifying flash memory against testcode.hex:
avrdude: load data flash data from input file testcode.hex:
avrdude: input file testcode.hex auto detected as Intel Hex
avrdude: input file testcode.hex contains 3490 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 1.83s

avrdude: verifying …
avrdude: 3490 bytes of flash verified
avrdude: reading input file “0x3f”
avrdude: writing lock (1 bytes):

Writing | ################################################## | 100% 0.00s

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

Reading | ################################################## | 100% 0.00s

avrdude: verifying …
avrdude: 1 bytes of lock verified

avrdude: safemode: Fuses OK

avrdude done. Thank you.

Lukituksen jälkeen yritetään ladata piirille jotain muuta, ei onnistu!

C:>avrdude -p m328p -c usbtiny -B10 -D -U flash:w:OwnBlink.cpp.hex

avrdude: Setting SCK period to 10 usec
avrdude: AVR device initialized and ready to accept instructions

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

avrdude: Device signature = 0x1e950f
avrdude: reading input file “OwnBlink.cpp.hex”
avrdude: input file OwnBlink.cpp.hex auto detected as Intel Hex
avrdude: writing flash (1018 bytes):

Writing | ################################################## | 100% 0.59s

avrdude: 1018 bytes of flash written
avrdude: verifying flash memory against OwnBlink.cpp.hex:
avrdude: load data flash data from input file OwnBlink.cpp.hex:
avrdude: input file OwnBlink.cpp.hex auto detected as Intel Hex
avrdude: input file OwnBlink.cpp.hex contains 1018 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 0.53s

avrdude: verifying …
avrdude: verification error, first mismatch at byte 0x0006
0x7e != 0x24
avrdude: verification error; content mismatch

avrdude: safemode: Fuses OK

avrdude done. Thank you.

Onnistuu taas, kun piiri ensin tyhjennetään (e)rase:

C:>avrdude -p m328p -c usbtiny -B10 -e -U flash:w:OwnBlink.cpp.hex

avrdude: Setting SCK period to 10 usec
avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.00s

avrdude: Device signature = 0x1e950f
avrdude: erasing chip
avrdude: Setting SCK period to 10 usec
avrdude: reading input file “OwnBlink.cpp.hex”
avrdude: input file OwnBlink.cpp.hex auto detected as Intel Hex
avrdude: writing flash (1018 bytes):

Writing | ################################################## | 100% 0.59s

avrdude: 1018 bytes of flash written
avrdude: verifying flash memory against OwnBlink.cpp.hex:
avrdude: load data flash data from input file OwnBlink.cpp.hex:
avrdude: input file OwnBlink.cpp.hex auto detected as Intel Hex
avrdude: input file OwnBlink.cpp.hex contains 1018 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 0.53s

avrdude: verifying …
avrdude: 1018 bytes of flash verified

avrdude: safemode: Fuses OK

avrdude done. Thank you.

Ei kai flashiin voi mitään kirjoittaa ilman erasointia vaikka se ei olisikaan lukittu.

Koitappas lukitsemisen jälkeen LUKEA flash. Jos onnistuu, lukitus meni pieleen.

Moi,

C:>avrdude -c usbtiny -p m328p -e -U flash:w:testcode.hex -U lock:w:0x3c:m

Mulla oli lukituksessa väärä arvo. Oikea arvo on 0x3c.
Lukituksen voi tarkastaa yrittämällä lukea flashin ennen
ja jälkeen lukitusta.

En keksinyt tätä itse, vaan pistin postia Joerg Wunschille,
joka antoi ratkaisun. Alla kirjeenvaihtoa.

Hi,

Do you have an idea, how could I read
the chip before locking (loading program wihtout lock option,
of coarse), and then after locking?

Well, right now, you need two separate avrdude commands, I’m afraid.
The first one programs the flash, and verifies it. The second one
programs the lock bits.

This could perhaps be changed in avrdude, so a verify operation is
executed immediately after the corresponding write operation,
preceding the processing of any subsequent -U options. Feel free
to submit an enhancement request for this.

Reading the flash after
locking should be impossible, if I have understood correctly.

Well, I think you could trust the verification process of the
-U lock:w:0x3c:m operation. :wink: If it succeeds, the device should
always be correctly locked.

You could also read the entire flash: -U flash:r:test.bin:r. As the
locked flash on this device reads out as 0xff, and trailing 0xff bytes
are stripped off the output file, it is expected to result in a
zero-length output file.

Reply:

This verifying seems to working. Readings:

C:>avrdude -c usbtiny -p m328p -U flash:r:test.bin:r

With erased chip:
32k written to file,

with no lock upload:
C:>avrdude -c usbtiny -p m328p -e -U flash:w:test.hex
C:>avrdude -c usbtiny -p m328p -U flash:r:test.bin:r

3940 bytes (program size)

with lock upload
C:>avrdude -c usbtiny -p m328p -e -U flash:w:test.hex -U lock:w:0x3c:m
C:>avrdude -c usbtiny -p m328p -U flash:r:test.bin:r

0 bytes