Using avrdude?

Hi Everybody,

I am quite new to Arduino and am currently using it for a new project. I had to get a new computer and forgot to save my C++ file from the old one. I was wondering if there was any way to download or recover a hex file or other type of file I could convert/decompile to C++? I know about avrdude and that seems like a viable option, however I have no idea how to use it because, as I said, I am quite new to all of this. The board I am currently using is the elegoo Arduino Mega 2560 R3 and I am using a Mac computer, although I have access to Windows if needed.

Any help, preferably quick, would be much appreciated!

Thank you so much!

Duplicate post deleted. Post once, have some patience.

Yes, you can download code out of a chip (never did it myself without using a Programmer), and attempt to dis-assemble it (go from hex to assembler) and then decompile it into C or possibly C++, but it will be very difficult to something you could proceed from.

You'll be better off re-writing it, and will undoubtably find things you can improve as you go. I always did.

Check out IDA Pro for disassembling decompiling.

So sorry about the duplicate post! I was trying to find my first post and couldn’t so I did not know if it had gone through! Apparently it did and I am so sorry about that. Either way, thank you so much for the quick response even after my rude duplicate.

Sorry to bother you again. I am pretty new at this kind of thing and I don't really have a clue how I would go about getting the .hex files from my Arduino Mega and only a slight idea of how to decompile it. Again any sort of guidance on this would be much appreciated.

Thanks so much!

TinyCan: I don't really have a clue how I would go about getting the .hex files from my Arduino Mega

Here are the instructions:

First, we’re going to do a dummy upload in the Arduino IDE in order to get it to help us generate the avrdude command used to read the program from the Arduino board:

Plug your Arduino board into your computer.

Select your board from the Arduino IDE’s Tools > Board menu.

Select your board’s port from the Tools > Port menu.

VERY IMPORTANT: Unplug your board from your computer.

File > Preferences

Check the box next to “Show verbose output during > upload”.

Click “OK”.

Sketch > Upload

Wait for the upload to fail.

Scroll up the black console window at the bottom of the Arduino IDE window until you see the avrdude command that was generated for the upload. It will look something like this:

E:\Arduino\hardware\tools\avr\bin\avrdude -CE:\Arduino\hardware\tools\avr/etc/avrdude.conf -v -patmega328p -carduino -PCOM17 -b115200 -D -Uflash:w:C:\Users\per\AppData\Local\Temp\arduino_build_91864/sketch_jan22b.ino.hex:i

Select the full text of the upload command.

Press Ctrl + C. This will copy the upload command to the clipboard.

Next, you need to modify the upload command to read the hex file out of your Arduino board:

Start a text editor program.

In the text editor window, press Ctrl + V. This will paste the command into the text editor. The end of the command will look something like this:

-D -Uflash:w:C:\Users\per\AppData\Local\Temp\arduino_build_91864/sketch_jan22b.ino.hex:i

That is the part of the command that tells it to write.

Replace that part of the command with the command that tells AVRDUDE to read:


That will cause the read file to be named “readfile.hex”, which will be saved to whichever folder you run the command from. So now the full command looks something like this:

E:\Arduino\hardware\tools\avr\bin\avrdude -CE:\Arduino\hardware\tools\avr/etc/avrdude.conf -v -patmega328p -carduino -PCOM17 -b115200 -D -Uflash:r:readfile.hex:i

If the paths in the command contain spaces, wrap the paths in quotes.

Plug your Arduino board into your computer.

Copy and paste the command from the text editor to the command line and run it.

Wait for the command to finish successfully.

TinyCan: only a slight idea of how to decompile it.

So now you have a .hex file. If you only want to be able to upload the same program to other Mega boards, there is no need to decompile. You can use AVRDUDE from the command line to upload the .hex file to other boards. If you want instructions for that, I'm happy to provide them.

I'll admit I'm not an expert on "decompiling", but I can tell you how to get the disassembly listing of that .hex file, which gives you the assembly code. The command you need to run is:

{Arduino IDE installation folder}/hardware/tools/avr/bin/avr-objdump --disassemble --section=.sec1 --architecture=avr6 readfile.hex > disassembly.txt

You'll need to replace {Arduino IDE installation folder} in the above command with the path to your Arduino IDE installation.

After that runs, you'll have a new file in the current folder named disassembly.txt, which contains the disassembly listing.

As for turning that back into C++, maybe someone else can give some guidance. You might have a look at radare2: which is a free open source reversing tool. I have never used it so I can't say if it will be of any help for your project.

Thank you so much!

You're welcome. I'm glad if I was able to be of assistance. Enjoy! Per

So sorry, one more quick thing. At the end of your first response about getting the .hex file you talked about wrapping the paths in quotes if they contained spaces. I know this will make me sound like quite a novice, but I was slightly confused about that part as I have no idea what it might entail.

It is just requirement from OS. The path containing spaces must be written in quotes. Like in DOS window under Win: dir "C:\Program Files" ;)

Let's say your command looks something like this:

C:\Program Files (x86)\Arduino\hardware\tools\avr\bin\avrdude -CC:\Program Files (x86)\Arduino\hardware\tools\avr/etc/avrdude.conf -v -patmega328p -carduino -PCOM17 -b115200 -D -Uflash:r:readfile.hex:i

If you run that, you'll get an error:

'C:\Program' is not recognized as an internal or external command,
operable program or batch file.

so you need to wrap the paths with spaces in quotes:

"C:\Program Files (x86)\Arduino\hardware\tools\avr\bin\avrdude" -C"C:\Program Files (x86)\Arduino\hardware\tools\avr/etc/avrdude.conf" -v -patmega328p -carduino -PCOM17 -b115200 -D -Uflash:r:readfile.hex:i

Thank you all so much! So would this be the code I would want to run"

"/Applications/ -C/Applications/ -v -patmega2560 -cwiring -P/dev/cu.usbmodem1421 -b115200 -D -Uflash:r:readfile.hex:i"

Remove the quotes. No quotes are needed because your paths don't contain a space. If there were spaces, you would need to wrap each path with quotes, not the entire command.

Other than that, it looks good. I am not very knowledgeable about macOS, so I can't be sure that's the correct path, but if that's the path the Arduino IDE used in its upload command, then it's correct.

Wait, so sorry again this will make me sound quite bad at this, but which part exactly is the command line? If it is just the place where you write code then I tried it and came up with this error. Blink was the name of the sketch. If the command line is not this place, then could you possibly walk me through how I might go about this step?

Blink:1:1: error: expected unqualified-id before '/' token /Applications/ -C/Applications/ -v -patmega2560 -cwiring -P/dev/cu.usbmodem1421 -b115200 -D -Uflash:r:readfile.hex:i ^ exit status 1 expected unqualified-id before '/' token

Again, thank you all so much!

Nevermind, I figured that out pretty fast. However, I am now stuck on decompiling. I followed the instructions but keep coming up with the error message:

-bash: /Applications/ No such file or directory

Any ideas?

It should be /Applications/

Again, my fault. A slight pathing error that I fixed and now I have the disassembly.txt! All of your instructions worked perfectly, so thank you so much!!! If anyone has an idea on how to take this disassembly.txt and convert it into C++ (either using radare or not because I could not figure out how to use it) that would be great!

Thank you all for being so helpful and kind!

TinyCan’s dedicated topic about converting the disassembly into C++:

Thanks for redirecting people, it should help a lot in receiving an answer!

That, and also help anyone else who later has the same question and finds this thread during their research to easily get all the relevant information.