Go Down

Topic: Extracting Code from an Arduino (Read 43571 times) previous topic - next topic


I'm planning to reflash my 3D printer (GT2560 using ATmega 2560 chip like Arduino Mega) with new firmware. It's currently running the default firmware it was programmed with. As such I'd like to make a backup of the default in case something goes wrong, and I can't find enough information online about what exactly it would be running.
Now I'm not expecting to be able to convert it back from Assembly to human readable like C, I just want the data as it is (though if there's a way to convert it to Arduino native that would be great so I can get a better feeling for what settings were used etc etc).
So to sum my question; how do I extract raw program data from the board, how do I reinstall it and if possible, how do I convert it back to Arduino code?



Assuming the 2560 isn't locked, there are avrdude commands you can run to download the flash data from the chip and store it as a hex file.
You will not be able to easily manipulate it.
You can Disassemble it into Assembly code.  You may even be able to decompile into something C++ like. If the original code was created in C++ anyway.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.


May 26, 2016, 10:15 pm Last Edit: May 26, 2016, 10:31 pm by AWOL
Don't forget to grab the contents of the EEPROM too, just in case.


May 27, 2016, 03:20 am Last Edit: May 27, 2016, 03:21 am by mrburnette
If you are on Windows, the commands can all go into a cmd file... example shown is for UNO: Windows firmware duplicator



download the flash data from the chip and store it as a hex file.
Thanks! At this point I'll settle for just having it as a backup. I've taken a look at avrdude commands and have another question: it says I need to address the programmer. I'm assuming that's built into the Arduino, so what would I address it as?


Your avrdude command and options should look somthing like...
avrdude -p atmega2560  -c stk500v2 -P COM10 -b 115200 -U flash:r:flash_backup_file.hex

-c stk500v2 is how the bootloader in MEGA2560 talks
-P is the serial port, that may be the address thing you were seeing (not sure) but you need to know which port
-b 115200 is the baud rate (not sure if it is 57600 or 115200, it should not hurt to try both)
-U flash:r:flash_backup_file.hex is the key to reading ... notice the r ... where the :r: is ... that is for reading.

to backup the eeprom (a good idea)
avrdude -p atmega2560  -c stk500v2 -P COM10 -b 115200 -U eeprom:r:eeprom_backup_file.hex

if you want to write it back to the board
avrdude -p atmega2560  -c stk500v2 -P COM10 -b 115200 -e -U flash:w:flash_backup_file.hex

-e will nuke everything which you probably need to do befor writing.
avrdude -p atmega2560  -c stk500v2 -P COM10 -b 115200 -U eeprom:w:eeprom_backup_file.hex

an explicite erase is not needed with eeprom.
my projects: https://github.com/epccs


Im keen to use this before I possibly 'brick' my 3D printer, but I get the error
avrdude: can't open config file "": Invalid argument

What are the contents of the file this is looking for?
-c stk500v2 is how the bootloader in MEGA2560 talks


it is one of avrdude options

Code: [Select]
-c programmer-id

Specify the programmer to be used. AVRDUDE knows about several common programmers. Use this option to specify which one to use. The programmer-id parameter is the programmer's id listed in the configuration file. Specify -c ? to list all programmers in the configuration file.

my projects: https://github.com/epccs



I had the same error trying to read the OSCCAL values from an attiny13a (using linux).
After adding the type of file format, :h for hex, it worked like this:

avrdude -p t13 -c usbasp -U calibration:r:calibration.txt:h

See the documentation for other formats.

Go Up