Programming The Lock Bits Using Arduino ISP with AVRDUDE

Intoduction: Protecting The Arduino Uploaded Program Codes

If you want to sell the final product developed with Arduino, or you want to sell the embedded software by selling the programmed microcontroller chip, you need to protect your software from copying the code from your programmed microcontroller chip. Fortunately, there is a lock bits fuse that can be programmed using the ICSP interface that prevent the reading of your codes from the flash memory. The cheapest and easiest way to program the microcontroller chip is using ArduinoISP, since it has already included in the Arduino software installation as ArduinoISP sketch. How to program the microcontroller chip using ArduinoISP as the programmer is beyond the discussion, and here I will just present the method on how to use it for programming the protection fuse bits.

Figure 1. Arduino Upload Error Message


Figure 2. Avrdude Command Line and Its Parameters

Creating The Avrdude Lock Bit Protection Fuse Programming  Setup Easily in Arduino

Every Ardunio development tool installation includes avrdude tools for uploading the sketch, so we need not to install a separate avrdude application. I have tried this simple method in Windows operating system, but I think the same method can be applied in Linux environment since they have equivalent shell script commands for every Windows console commands described in one of  these steps:

    1. First, it is assumed that you have already a running ArduinoISP board (the arduino board loaded and run with ArduinoISP sketch) to program other ATMEGA chip on other application board (or just on a programming board) with you application sketch.
    2. Make sure the verbose output for uploading is set in the Aduino development software. To do this, go to File->Preference, and in check the upload checkbox in the option of “Show verbose output during: compilation, upload” in the Settings tab.
    3. Upload the application sketch (which will be protected) to the application board by menu Sketch->Upload Using Programmer (Ctrl+shift+U), but with the target (application) board disconnected or powered off to force an error on the uploading process. This trick is used force the Arduino interface to show a “copy error message” pop up menu in the right-top-corner of the verbose output window (Figure 1). Without this error, all displayed message in the output terminal can’t copied to the OS’s clipboard for pasting to another app.
    4. Open your sketch folder, and create new text file, save as “writelockbit.bat”. You might need to uncheck the “hide extension for known file type” of the file explorer or operating system’s preference to save as “.bat” or to edit/rename the extension from “.txt” to “.bat”.
    5. Edit the file “writelockbit.bat” (right click on the file name and select “edit”),  then paste the error message. Search the avrdude command by search “avrdude” keyword, and you should find something like shown as the blue selection in the Figure 2.
    6. Delete all texts other than the avrdude command line, leaving the line as follows (in your system it might be different):
      C:\Users\ACER\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino9/bin/avrdude -CC:\Users\ACER\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino9/etc/avrdude.conf -v -patmega8 -cstk500v1 -PCOM6 -b19200 -Uflash:w:C:\Users\ACER\AppData\Local\Temp\arduino_build_796890/pidsscontrollerfixpoint.ino.hex:i                          

      That command runs the avrdude from its intallation path “…bin/avrdude” (it might be different in your system), with the -C option for config file “…/etc/avrdude.conf”, -v option for verbose output, -p option for themicrocontroller device (amtmega8), -c option for the programmer type (stk500v1), -P option for the port (COM6), -b option for the baudrate setting (19200), and -U option for the target memory to be read or write (flash) along with the hex file name that contain the codes to upload.

    7. Edit the command to change the target memory from “flash” to “lock”, change the uploaded code file into direct 1 byte immediate locking code 0x00 (other values can be chosen, see the ATMEGA microcontroller’s manual),  and add -u option to disable safe-mode. Add additional new line of “pause” command to halt the execution, preventing auto exit after finishing the lock bits writing, so we can manually checking the response of the avrdude’s execution messages. The edited code now should look like the following:
      C:\Users\ACER\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino9/bin/avrdude -CC:\Users\ACER\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino9/etc/avrdude.conf -v -patmega8 -cstk500v1 -PCOM6 -b19200 -Ulock:w:0x00:m -u                                .

Save the “writelockbit.bat” file, and now you can execute the avrdude to program the lock bit fuse by double-clicking this “bat” file after uploading the sketch using the ArduinoISP. That’s all the steps that needed to program the protection lock bits, securing your code from being cloned. Please be aware that this protection might be hacked by some some hacking technics, ranging from power or signal glitching to disrupt the internal fuse bit data transfer, laser beam injection that targets a specific point of the chip to manipulate the protection bit, or even decapitating the chip to read the codes visually by observing the microstructure of the semiconductor layers using optical or electron microscopes. The assumption is that, after securing by programming this protection fuse bits, the cost for stealing your codes by any hacking methods should be more expensive than hiring a programmer to reverse-engineer your system and write their own codes.