|
現: 2022-10-19 (水) 10:00:30 yuji  |
| + | * bootloaderのプロテクト [#de6aafd7] |
| + | ATMEL AVRには,Flash ROMに書き込んだプログラムやEEPROMを保護するためにその設定が行えるロックビットがあります。~ |
| + | このロックビットを操作することによって,プログラム領域,EEPROM領域,ブートローダー領域に対して,読み書きの保護(プロテクト)をすることが出来る。~ |
| | | |
| + | もしこのロックビットの設定によりフラッシュ領域などの書き込みをプロテクトした場合,再度プログラムすることが出来なくなる。~ |
| + | この場合は,''デバイスを消去する''ことによりロックビットやプログラム領域をもとに戻すことで,再プログラミング出来るようになる。~ |
| + | |
| + | ** ロックビットの仕様 [#n4e0f141] |
| + | ロックビットの詳細はAVRデバイスのデータシートを参照する。~ |
| + | |
| + | ATmega328/ATmega328Pのロックビットは以下のようになっている。~ |
| + | |名称 |bit|内容 |既定値|h |
| + | |- |7 | |0 | |
| + | |- |6 | |0 | |
| + | |BLB12|5 |ブートローダー領域保護bit |1 | |
| + | |BLB11|4 |~|1 | |
| + | |BLB02|3 |応用領域保護bit |1 | |
| + | |BLB01|2 |~|1 | |
| + | |LB2 |1 |Flash ROMとEEPROMに対する保護用bit|1 | |
| + | |LB1 |0 |~|1 | |
| + | ~ |
| + | |LB種別|LB2 |LB1 |内容 |h |
| + | |1 |1 |1 |機能なし | |
| + | |2 |1 |0 |Flash ROM, EEPROMの書き込みプロテクト。FUSE Bit固定される。 | |
| + | |3 |0 |0 |Flash ROM, EEPROMの書き込み・読み出しプロテクト。FUSE Bitとロックビットが固定される| |
| + | ~ |
| + | |LB0種別|BLB02 |BLB01 |内容 |h |
| + | |1 |1 |1 |LPM, SPM命令が応用領域をアクセスすることに対してプロテクトなし。 | |
| + | |2 |1 |0 |応用領域へのSPM命令はプロテクト。 | |
| + | |3 |0 |0 |応用領域へのSPM命令はプロテクト。ブートローダー領域のLPM命令による応用領域からの読み込みがプロテクト。| |
| + | |4 |0 |1 |ブートローダー領域でのLPM命令による応用領域からの読み込みが制限される。 | |
| + | ~ |
| + | |LB1種別|BLB12 |BLB11 |内容 |h |
| + | |1 |1 |1 |LPM,SPM命令がブートローダー領域をアクセスすることに対してプロテクトなし。 | |
| + | |2 |1 |0 |SPM命令によるブートローダー領域に書き込むことがプロテクトされる。 | |
| + | |3 |0 |0 |SPM命令によるブートローダー領域に書き込むことがプロテクトされる。応用領域でのLPM命令によるブートローダー領域の読み込みがプロテクトされる。| |
| + | |4 |0 |1 |応用領域でのLPM命令によるブートローダー領域からの読み込みがプロテクトされる。 | |
| + | |
| + | ** ロックビットを使ってFlash ROMの読み書きを禁止する手順 [#xcff1350] |
| + | ロックビットを操作するために,なんらかのAVR Writerを用意する。~ |
| + | 用意したAVR WriterとターゲットのボードのISPコネクタを接続する。(ターゲットボードに書き込まれているbootloaderではロックビットの操作はできない。)~ |
| + | |
| + | avrdudeを使用して接続ができているか確認する(COMポートなどは環境に合わせる)。~ |
| + | > avrdude -c stk500v2 -P COM1 -b 115200 -p m328 |
| + | avrdude.exe: AVR device initialized and ready to accept instructions |
| + | Reading | ################################################## | 100% 0.09s |
| + | avrdude.exe: Device signature = 0x1e9514 (probably m328) |
| + | avrdude.exe: safemode: Fuses OK (E:FD, H:D8, L:C2) |
| + | avrdude.exe done. Thank you. |
| + | FUSEの現在の設定が表示される。 |
| + | |
| + | ロックビットの状態を確認してみる。~ |
| + | > avrdude -c stk500v2 -P COM1 -b 115200 -p m328 -U lock:r:con:h |
| + | avrdude.exe: AVR device initialized and ready to accept instructions |
| + | Reading | ################################################## | 100% 0.07s |
| + | avrdude.exe: Device signature = 0x1e9514 (probably m328) |
| + | avrdude.exe: reading lock memory: |
| + | Reading | ################################################## | 100% 0.01s |
| + | avrdude.exe: writing output file "con" |
| + | 0x2f |
| + | avrdude.exe: safemode: Fuses OK (E:FD, H:D8, L:C2) |
| + | avrdude.exe done. Thank you. |
| + | この場合ロックビットは0x2fに設定されていて,ブートローダー領域への書き込みがプロテクトされている。~ |
| + | デフォルト状態ではロックビットは0x3fで,なんのプロテクトもかかっていない。~ |
| + | |
| + | Flash ROMを読んでファイルに書き出してみる。~ |
| + | > avrdude -c stk500v2 -P COM1 -b 115200 -p m328 -U flash:r:"program.hex":i |
| + | avrdude.exe: AVR device initialized and ready to accept instructions |
| + | Reading | ################################################## | 100% 0.03s |
| + | avrdude.exe: Device signature = 0x1e9514 (probably m328) |
| + | avrdude.exe: reading flash memory: |
| + | Reading | ################################################## | 100% 19.98s |
| + | avrdude.exe: writing output file "program.hex" |
| + | avrdude.exe: safemode: Fuses OK (E:FD, H:D8, L:C2) |
| + | avrdude.exe done. Thank you. |
| + | うまく書き出せている。(読み込みはプロテクトされていない。)~ |
| + | |
| + | ロックビットを0x00(全領域書き込みも読み込みもプロテクト)に書き換えてみる。 |
| + | > avrdude.exe -c stk500v2 -P COM1 -b 115200 -p m328 -U lock:w:0x00:m |
| + | avrdude.exe: AVR device initialized and ready to accept instructions |
| + | Reading | ################################################## | 100% 0.03s |
| + | avrdude.exe: Device signature = 0x1e9514 (probably m328) |
| + | avrdude.exe: reading input file "0x00" |
| + | avrdude.exe: writing lock (1 bytes): |
| + | Writing | ################################################## | 100% 0.03s |
| + | avrdude.exe: 1 bytes of lock written |
| + | avrdude.exe: verifying lock memory against 0x00: |
| + | avrdude.exe: load data lock data from input file 0x00: |
| + | avrdude.exe: input file 0x00 contains 1 bytes |
| + | avrdude.exe: reading on-chip lock data: |
| + | Reading | ################################################## | 100% 0.01s |
| + | avrdude.exe: verifying ... |
| + | avrdude.exe: 1 bytes of lock verified |
| + | avrdude.exe: safemode: Fuses OK (E:FD, H:D8, L:C2) |
| + | avrdude.exe done. Thank you. |
| + | |
| + | Flash ROMを再度読んで書き出してみる。~ |
| + | > avrdude.exe -c stk500v2 -P COM1 -b 115200 -p m328 -U flash:r:"program2.hex":i |
| + | avrdude.exe: AVR device initialized and ready to accept instructions |
| + | Reading | ################################################## | 100% 0.03s |
| + | avrdude.exe: Device signature = 0x1e9514 (probably m328) |
| + | avrdude.exe: reading flash memory: |
| + | Reading | ################################################## | 100% 19.94s |
| + | avrdude.exe: Flash is empty, resulting file has no contents. |
| + | avrdude.exe: writing output file "program2.hex" |
| + | avrdude.exe: safemode: Fuses OK (E:FD, H:D8, L:C2) |
| + | avrdude.exe done. Thank you. |
| + | program2.hexはファイルサイズが小さく,読み出しが出来ていない。~ |
| + | |
| + | ** 再度プログラムの読み書きが出来るようにする [#w77d56b1] |
| + | ロックビットを操作してプロテクトされている状態を解除するには,AVRデバイスを''チップ消去''する必要がある。~ |
| + | |
| + | *** Arduino IDEを使う場合 [#gb55045e] |
| + | Arduino IDEを使って「ブートローダーを書き込む」作業を行うと,''チップ消去''を実行した後にブートローダーが書き込まれる。~ |
| + | Arduino IDEを使う場合でも,なんらかのAVR Writerを使用する。~ |
| + | |
| + | *** avrdudeを使ってチップ消去を行う [#qd295d28] |
| + | avrdudeで''チップ消去''を行うには,-eオプションを使う。~ |
| + | |
| + | 接続する。~ |
| + | > avrdude -c stk500v2 -P COM1 -b 115200 -p m328 |
| + | avrdude.exe: AVR device initialized and ready to accept instructions |
| + | Reading | ################################################## | 100% 0.09s |
| + | avrdude.exe: Device signature = 0x1e9514 (probably m328) |
| + | avrdude.exe: safemode: Fuses OK (E:FD, H:D8, L:C2) |
| + | avrdude.exe done. Thank you. |
| + | FUSEの現在の設定が表示される。~ |
| + | |
| + | -eオプションを使って''チップ消去''する。~ |
| + | > avrdude.exe -c stk500v2 -P COM1 -b 115200 -p m328 -e |
| + | avrdude.exe: AVR device initialized and ready to accept instructions |
| + | Reading | ################################################## | 100% 0.03s |
| + | avrdude.exe: Device signature = 0x1e9514 (probably m328) |
| + | avrdude.exe: erasing chip |
| + | avrdude.exe: safemode: Fuses OK (E:FD, H:D8, L:C2) |
| + | avrdude.exe done. Thank you. |
| + | |
| + | ロックビットを確認する。~ |
| + | > avrdude.exe -c stk500v2 -P COM1 -b 115200 -p m328 -U lock:r:con:h |
| + | avrdude.exe: AVR device initialized and ready to accept instructions |
| + | Reading | ################################################## | 100% 0.03s |
| + | avrdude.exe: Device signature = 0x1e9514 (probably m328) |
| + | avrdude.exe: reading lock memory: |
| + | Reading | ################################################## | 100% 0.01s |
| + | avrdude.exe: writing output file "con" |
| + | 0x3f |
| + | avrdude.exe: safemode: Fuses OK (E:FD, H:D8, L:C2) |
| + | avrdude.exe done. Thank you. |
| + | これで''チップ消去''された。ロックビットがデフォルトの0x3fに戻っている。~ |