How to extract code from Arduino

Thread Starter

Killerbee65

Joined May 15, 2017
256
Hello all, by now some of you have seen my previous threads and noticed I haven't uploaded anything, or rather I uploaded too much in a set time period. All that was due to my project being a school-based project and me needing to finish before a deadline. Now that school is over and I am attending my first year in college, this project has turned from a necessity to a passion project, hence the hiatus. Anyways with that out of the way I recently wanted to get back into building my R4D2 robot but all the code I had recently (at the time) compiled as final products were on my school-issued laptop and I had to turn that in and forgot to upload the code onto my personal computer. I have recently been looking at forums seeing if and how I can somehow reclaim the code by extracting it straight from the Arduino board but most answers and questions were confusing, hard to understand seeing how these were experienced programmers asking and answering the question, and not exactly what I was looking for. Simply put I am very careful how I want to go through with this because if I lose the code then that would mean a whole lot of unnecessary work. Thank you for any reply or advice.
 

MrChips

Joined Oct 2, 2009
30,720
Talk to your course instructor and see if they will give you access to your code.

Lesson to learn: always backup your work.
 

ZCochran98

Joined Jul 24, 2018
303
This link and this link have a suggestion as to a way you might be able to get the Arduino to dump its 32kB flash memory (where the program is stored). They seem to be "simple" solutions, in that they consist of a couple command-line operations (though you may have to set up AVRDude to work outside of the Arduino IDE - it's the compiler/debugger program).

HOWEVER: as a warning, it will very likely be stored as the numeric hex code (opcodes) equivalent of assembly (in other words, you'll just get a text file full of lines that read something like "0x100102BA," as a random example), not the original C/C++ code. It would certainly be a decent challenge to hand-decompile it (and an opportunity to learn assembly along the way...).

I agree with @MrChips : assuming they haven't reformated the drives on the laptops, it would be much easier to request access to the code. Remember to back up future work - it's definitely a lifesaver (I even had to use my own recently, as my previous hard drive gave out after 5 years of constant use and jostling).

Good luck with retrieving your program!
 

Thread Starter

Killerbee65

Joined May 15, 2017
256
Talk to your course instructor and see if they will give you access to your code.

Lesson to learn: always backup your work.
Tried that the day after turning in the computer but my school was very strict and basically didn't want to invest in an engineering course until my last year. That being said, not even my instructor had access to these computers. And yea too bad the lesson was learned the hard way.
 

Thread Starter

Killerbee65

Joined May 15, 2017
256
This link and this link have a suggestion as to a way you might be able to get the Arduino to dump its 32kB flash memory (where the program is stored). They seem to be "simple" solutions, in that they consist of a couple command-line operations (though you may have to set up AVRDude to work outside of the Arduino IDE - it's the compiler/debugger program).

HOWEVER: as a warning, it will very likely be stored as the numeric hex code (opcodes) equivalent of assembly (in other words, you'll just get a text file full of lines that read something like "0x100102BA," as a random example), not the original C/C++ code. It would certainly be a decent challenge to hand-decompile it (and an opportunity to learn assembly along the way...).

I agree with @MrChips : assuming they haven't reformated the drives on the laptops, it would be much easier to request access to the code. Remember to back up future work - it's definitely a lifesaver (I even had to use my own recently, as my previous hard drive gave out after 5 years of constant use and jostling).

Good luck with retrieving your program!
Doesn't the arduino ide translate C into assembly? if so, isn't there a possible way to reverse the code back into C?
 

MrChips

Joined Oct 2, 2009
30,720
It is possible to reverse engineer your hex code to ASM.
That is relatively straight forward.
It is possible to reverse engineer ASM to C but that is much more complicated. The resulting C code will not be anywhere close to your original code. All variable names, function names and code comments will not be there.
 

dl324

Joined Mar 30, 2015
16,846
how I can somehow reclaim the code by extracting it straight from the Arduino board but most answers and questions were confusing, hard to understand seeing how these were experienced programmers asking and answering the question, and not exactly what I was looking for. Simply put I am very careful how I want to go through with this because if I lose the code then that would mean a whole lot of unnecessary work.
Read post #8 in this thread on the Arduino forum. As is typical, the first response by a high post count member was clueless, but someone later provided an appropriate answer.
The gist of the post:
1595290593138.png[/URL]
 

Thread Starter

Killerbee65

Joined May 15, 2017
256
I saw somewhere that you had to pull up the command prompt and enter the quote placed in post #9 but I don't know what to do after that.
 

ZCochran98

Joined Jul 24, 2018
303
If you use the first command in post 9, then the contents of the Arduino's flash memory is pasted directly into a file called "test.hex" (where, I'm not sure - probably the directory your CMD prompt is currently active in, or a folder in that directory called "hexfiles"). The hex file could then be opened with a hex editor, or, if you have the tool, something that can translate AVR's opcodes into assembly (Atmel studio, maybe?).
 

Thread Starter

Killerbee65

Joined May 15, 2017
256
When I type in the command line above it gives me an error saying it wasn't recognized.

let me know if the attachment is hard to see.cmd line.PNG
 

ZCochran98

Joined Jul 24, 2018
303
AVRDude isn't set up in your PATH, so it won't recognize it as a command. The program "avrdude" is located (for me, anyway), in "C:\Program Files (x86)\Arduino\hardware\tools\avr\bin\", so take a look there. If you find a program called "avrdude," add that directory to your PATH variables (look up how to do that if you don't know how - it's not too hard).



Edit: an alternative thing you can do, if you don't want to add anything to your PATH, is to, instead, type (exactly as you see it, including quote marks):

"C:\Program Files (x86)\Arduino\hardware\tools\avr\bin\avrdude" -c arduino -P com20 -p atmega328p -Uflash:r:d:\hexfiles\test.hex:i

This is assuming that avrdude on your machine is located in "C:\Program Files (x86)\Arduino\hardware\tools\avr\bin\".

Also make sure to confirm that your Arduino is actually on COM20. It may be on COM3, COM4, or another serial port (check hardware manager or the Arduino IDE - it'll usually tell you which port the device is connected on)

Final edit: you may want to change the directory part of it. That's the "d:\hexfiles\test.hex:i" part (the ":i" indicates Intel hex, according to the thread). So, if you have a specific folder you want to save this hex file to (for example, in a folder called "Projects" in "My Documents", you'd replace d:\hexfiles\test.hex:i with "C:\Users\[username]\My Documents\Projects\test.hex:i", with the quotes included and your username substituted in appropriately).
 
Last edited:

Thread Starter

Killerbee65

Joined May 15, 2017
256
So this popped upcmd update.PNG

not sure I did it right but other than that how do I find com20? i tried all the usb ports on my laptop but all of them say com5, which is weird considering the school laptop had different numbers for each port.
 

ZCochran98

Joined Jul 24, 2018
303
Try typing com5 instead of com20. In the example this originated from the original person had his Arduino connected to com20. Loosely-speaking, COM# just indicates a particular location of a serial communication or serial port. All your USB ports may say com5 because it's not related to a specific hardware device, but over a "port," for lack of a better term, agreed upon by your computer and the Arduino.
 
Top