I have Orange PI with ubuntu connected to atmega328p through usbasp.
I've developed a program in C, compiled it, translated to hex and uploaded on the atmega, but because of some strange behavior, the file.c is lost.
How can I get my program back from the atmega?
The good news: It is possible, definitively.
The bad news: But it's a lot of work, depending on the size of your application. I did this more than one time with AVR code, written in C, BASCOM, or C++ (Arduino). It takes several hours, for example some 20 hours for a 100-liner in BASCOM.
The approach is:
- Disassemble the HEX file. Use this output as reference. You might need some options to have all constant data in the output.
- Start with the best approximation of the source that your memory still holds.
- Compile, link and convert it into a HEX file, too.
- Disassemble this HEX file, and compare the output with the reference.
- Repeat editing your source until both disassemblies are equal.
Notes:
- You need deep understanding about the translation from C into machine code.
- The names of functions and variables can't be reconstructed exactly. These names are gone after compiling and linking.
- Be aware that the order of functions in the resulting code might not depend on their appearance in the source. Most compilers do this, though.
- Be aware that the order of variables in memory might not depend on their appearance in the source, but on their name. Additionally they are commonly not sorted lexically, for example I found GCC using some kind of hashing algorithm. However, members of structs keep their order, because the standard demands that.
- In a first phase, ignore differences of variable placement.
- Try to identify functions of the C library, and ignore them. Especially the
printf()
family draws a lot of other functions with it. When you own code is finished, the library functions will be there, too, most probably.
Final note: If you happen to have the ELF file, use this for disassembling and looking up names. You will be much faster.