Go Down

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

SJMaybury

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?

Thanks!

CrossRoads

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.
https://www.onlinedisassembler.com/odaweb/
https://derevenets.com/examples.html
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.

AWOL

#2
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.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

mrburnette

#3
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

Ray

SJMaybury

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?

ron_sutherland

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.

Go Up