Can you get the code back out of the arduino? - Page 1
Forum section: electronics / general

author: raspberrypi
author_rank: Frequent Contributor
timestamp: March 10, 2017, 04:14:58 am
content: I realize that C to assembly language is a one way conversion, but I have a working program inside the arduino right now and I screwed up the sketch and didn't save.Or if I just wanted the program its running and didn't care that it was in assembly can you get it out? Say I just wanted to copy the program inside of it to put on another arduino?

author: Ian.M
author_rank: Super Contributor
timestamp: March 10, 2017, 05:11:09 am
content: You can get back the hex file, which can be used to program another chip but its a PITA involving using AVRDUDE from the command line to talk to the Arduino bootloader, and if you make any mistakes, you'll probably 'brick' the Arduino, either by blowing away the bootloader or by messing up the fuses and will need an AVR ISP programmer to recover from that, and may even need an external clock oscillator and/or a HV capable programmer if you bork the fuses badly enough.You cannot get back the sourcecode from the chip in any useful form*.It will take you longer to learn how to do it that it would to recreate any program at the current level of complexity you are coding at.However if you are running a 'vanilla' installation of the Arduino IDE under Windows, it compiles to the user's temp folder (Start menu: Run:%temp%) and if you are lucky, you should see a bunch of folders with names like build133091154269987573.tmp, and in each of them is a bunch of files including .cpp files. one of the .cpp files will have the same name as your sketch, and will contain the sourcecode at the time of that build, slightly munged by the IDE's build process, and stripped of all comments, but still valid C that could be copy/pasted to a new sketch and cleaned up. It creates a new build folder every time you open a new sketch and build it, so unless you did all the editing in one session without closing the IDE, there's a fair chance of finding your old version.* It it however possible to disassemble the hex file to get an assembler listing that can be - with a lot of work - turned into an assembly language .s file that AVR GCC can reassemble (although not under the Arduino IDE). That's an act of absolute desperation if you need to make a non-trivial patch to a large program you've lost the source to and don't have the time/money to rewrite.

author: rstofer
author_rank: Super Contributor
timestamp: March 10, 2017, 05:13:51 am
content: Under some conditions, you might be able to get the machine code (just binary data), you can't get assembly code and you certainly can't get C code.In general, the answer is no, there is no recovery.Even if you did dump the memory to a file, most of the binary bytes aren't even yours, they belong to the library functions. It could be challenging just figuring out where your code starts.ETA: The best Fortran programs were written after the box of cards was dropped.

author: Richard Crowley
author_rank: Super Contributor
timestamp: March 10, 2017, 05:23:34 am
content: There are disassemblers which take raw machine code and "reverse-engineer" it back into assembler. It is unlikely that there is any kind of disassembler to recover the source C-code that produced the assembly language.There is also the matter of whether the chip is protected or not. There are "fuses" which can be set in most microcontrollers to prevent reading-out the code. This is commonly done in commercial products to prevent (or at least discourage) cloning and counterfeiting.And then there is also the matter of the code that you wrote depending on a large library of routines which were cross-linked during assembly. So that code in the microcontroller is only partly what you wrote. The rest is the libraries that support the high-level commands you wrote. So you would have to sort out which bits of code are yours, and which parts are the Arduino library.Ref:https://en.wikipedia.org/wiki/Disassemblerhttp://hackaday.com/2012/08/30/avr-fuse-bits-explained/https://www.arduino.cc/en/hacking/libraries

author: Mechatrommer
author_rank: Super Contributor
timestamp: March 10, 2017, 05:25:50 am
content: Quote from: rstofer on March 10, 2017, 05:13:51 aminfact you can, since machine code is a 1 to 1 conversion from assembly code, with a right dissambler tool or focus, you can get back the assembly code... to the OP, buy AVR ISP Mk2 to make this job easier, not sure if avrdude can do the job.Quote from: Richard Crowley on March 10, 2017, 05:23:34 ammacros and inline functions will do this, but unlikely for heavy routines, which the code will make far jump and memory swapping (housekeeping) beforehand.btw i found out IDA Pro disassembler is good at separating machine/assembly codes into logical chunks/routines, but not sure if it support avr binary, istr it can dissasemble arm code.https://www.hex-rays.com/products/ida/

author: Rick Law
author_rank: Super Contributor
timestamp: March 10, 2017, 06:15:37 am
content: Hope of recovering the C code is slim, butdon't let this mistake goes to waste. Consider it a lesson that backup copies is more than just a good idea but a should do. It would also help to add a text file with each saved copy on modifications/progress made.As suggested, the Arduino IDE can save the temporary files. Turning the "save temporary..." in Arduino IDE is a good thing. You can also look at intermediate files and learn a lot from them - particularly the one (.lst, I think) that give you c-code line and what assembly that line converts to. As you run into limitations (flash/ram), those files will give you much needed information on how to trim resources or how to speed up code segments to speed things up.

author: djnz
author_rank: Regular Contributor
timestamp: March 10, 2017, 08:26:34 am
content: Provided security fuses are not set (and they are not, on an arduino), it is possible to get the hex file (and hence assembly listing) of programs on the Arduino (and many other micros).Something else to consider is the use of a version control system to help prevent such problems. There are many free and good options available, 'git' being the one that I often use.

author: dgtl
author_rank: Regular Contributor
timestamp: March 10, 2017, 09:34:40 am
content: Use a source code version control tool, ie git. After every reasonable change, commit your work. For added safety, push regularly to some external server, ie github.If you mess up, you have the full history to go back to. You can diff the changes and see what has been changed after the last good version. If a bug comes out, you can bisect the work and pinpoint the change that caused it.

author: SL4P
author_rank: Super Contributor
timestamp: March 10, 2017, 10:25:38 am
content: Just an unhelpful aside...Often, the effort to reproduce all or any of the above is wasted time.The work required to completely rewrite (assuming you wrote the original), is often only around 40% of the effort that it took to generate the first pass.Usually, you learned, and imagined some improvements that you can add to the new V2 code.And do a backup!

author: amyk
author_rank: Super Contributor
timestamp: March 10, 2017, 12:02:25 pm
content: Quote from: Mechatrommer on March 10, 2017, 05:25:50 amAVR is supported:https://www.hex-rays.com/products/ida/processors.shtmlThey do have a decompiler but it doesn't support AVR (why, I have no idea --- it should be based on the disassembler):https://www.hex-rays.com/products/decompiler/index.shtmlDownside: IDA is Not Cheap. There are free alternatives, however.

author: rrinker
author_rank: Super Contributor
timestamp: March 10, 2017, 01:40:51 pm
content: Is it just me? The only changes I made to the Arduino IDE was to add the additional files to support ATTiny85 chips, I've otherwise not really customized any UI settings, and if I type in a sketch and click Upload without saving, it first FORCES me to save it. So it should be saved somewhere. If you open an existing sketch and make no changes, it will upload without saving, or, as I just tried, if you start the IDE and not put in ANY code, and try to upload the blank sketch with just empty setup() and loop() functions, it does not force a save. But put just 1 line of code in, hit upload, and I get a save dialog.

author: rstofer
author_rank: Super Contributor
timestamp: March 10, 2017, 06:48:11 pm
content: Disassembly of the byte code to some form of assembler code is not entirely worthwhile. BTDT...The problem is, there are no meaningful labels or constants. Sure, the disassembler will make up labels for branch targets but they certainly won't make sense. So then there is the task of figuring out what the bogus labels refer to and doing text substitutions on every label. No comments to provide a path...Constants are a separate issue. The disassembler can put the value on the code line but not the context. Worse, every use of a particular constant doesn't always refer to the same idea. Sometimes 4 is a loop limit, sometimes it is an index and other times it is just a number.For a small project, like an Arduino sketch, it is far better to just rewrite the thing. For stealing IP, it might be worth the cost of editing the disassembler output but most of those apps will have the code memory protected.

author: Rick Law
author_rank: Super Contributor
timestamp: March 11, 2017, 05:26:02 am
content: Quote from: rstofer on March 10, 2017, 06:48:11 pmI can't imagine someone stealing IP would take the trouble to edit de-compiled output for code modification. IP thief would likely just duplicate the program unchanged. Complexity/work is something a thief is not likely going to want.A simple program is not worth the effort. Much beyond that, re-developing is probably less work than de-comple + understand_the_code + modify + re-compile process.

author: Mechatrommer
author_rank: Super Contributor
timestamp: March 11, 2017, 07:34:14 am
content: Quote from: Rick Law on March 11, 2017, 05:26:02 amexcept if the firmware has about box showing the original company name.

author: rstofer
author_rank: Super Contributor
timestamp: March 11, 2017, 03:55:50 pm
content: Quote from: Rick Law on March 11, 2017, 05:26:02 amThink crypto gear, self-driving vehicles (maybe), military hardware, cutting edge algorithms, etc. There are a lot of applications where it actually is worth disassembling the code. That's why the chip vendors have been upping their game on code protection. People are still decapping chips to see how they are laid out.

author: Rick Law
author_rank: Super Contributor
timestamp: March 11, 2017, 06:35:39 pm
content: Quote from: Mechatrommer on March 11, 2017, 07:34:14 amThat is just simple patching. That doesn't require full (almost full) understanding of the decompiled code. I've actually done that - not to use the code else where but to use that few dozen bytes for some other function I wanted on the original HW.Quote from: rstofer on March 11, 2017, 03:55:50 pmI can understand that some code may be stolen. That is (rumored to be) how lots of Chinese products got started. I was (unsuccessfully) trying to delineatedecompiling to understandand steal the knowledge vsdecompiling to modifyand then use the code.My estimation is that highly complex code is not likely to be stolen just for modification. Highly complex code/systems are difficult enough to modify even if you have the original source let alone just the compiled binary. So, I would think it would be easier to re-develop after stealing the knowledge.Than again, run-by-night type of operation doesn't need to maintain their products. They only need to patch it enough to run. Likelihood of my statement being inaccurate is increasing...

author: raspberrypi
author_rank: Frequent Contributor
timestamp: March 12, 2017, 12:20:19 pm
content: Sainsmart even puts a copy write on their shitty code. I have been programming for a few days now and thats enough to realize how lazy it is. No one in their right mind would reuse their code. Although it was probably written by some six year old chinese kid in a sweat shop so its really not that bad, hard to write code with a gun in your back for bowl of rice a day.