0

This question already has an answer here:

Recently bought a arduino device which already have program,

it is model AR-293D and I have already connect it with my PC(windows 7) through usb

when I open the IDE , It seems there is no function that read the uploaded program?

And after searching a while , like some beginner tutorial , it only tell you how to upload the program but not how to get the uploadded program

http://www.forefront.io/a/beginners-guide-to-arduino

So, I wonder how to get the code? Thanks a lot for helping, I worried erase the uploadded program so I need to take caution about this

marked as duplicate by The Guy with The Hat, VE7JRO Sep 19 at 18:49

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

migrated from electronics.stackexchange.com Dec 14 '14 at 8:15

This question came from our site for electronics and electrical engineering professionals, students, and enthusiasts.

  • 1
    The problem is the uploaded code will be machine code and while you might be able to disassemble it there won't be a way to get the C++ code back out of it. – PeterJ Dec 14 '14 at 8:00
  • sorry for being new for this, Is that mean if I write the new code then it will overlap and erase the old code? thanks – user782104 Dec 14 '14 at 8:02
  • 1
    Yes that's right it will erase the old code and replace it, so you just keep your C++ code on the PC. – PeterJ Dec 14 '14 at 8:03
  • You can use khazama to read the chip, and then write it to a hex file. There are tons of other ways, but that's the one I've used previously, that was pretty easy. – Gerben Dec 14 '14 at 14:36
2

You can download the hex machine code from the Arduino by using an ISP programmer, like this one:

ISP programmer

A command like this could be used to read the flash memory and save it into myfile.hex:

avrdude -c avrisp -p m328p -U flash:r:myfile.hex:i

However be warned that this file will look like this:

:100000000C945C000C946E000C946E000C946E00CA
:100010000C946E000C946E000C946E000C946E00A8
:100020000C946E000C946E000C946E000C946E0098
:100030000C946E000C946E000C946E000C946E0088
:100040000C9488000C946E000C946E000C946E005E
:100050000C946E000C946E000C946E000C946E0068
:100060000C946E000C946E00000000080002010069
:100070000003040700000000000000000102040863
:100080001020408001020408102001020408102002
:10009000040404040404040402020202020203032E
:1000A0000303030300000000250028002B000000CC
:1000B0000000240027002A0011241FBECFEFD8E043
:1000C000DEBFCDBF21E0A0E0B1E001C01D92A930AC
:1000D000B207E1F70E94F1010C9401020C940000B8

As you can see, you haven't really got "the program" back.


As Mikael Patel points out, you can at least turn it into assembler by doing this:

avr-objdump -j .sec1 -d -m avr5 myfile.hex

However the output is still not C code like you see in the IDE:

  d4:   0e 94 f1 01     call    0x3e2   ;  0x3e2
  d8:   0c 94 01 02     jmp 0x402   ;  0x402
  dc:   0c 94 00 00     jmp 0   ;  0x0
  e0:   61 e0           ldi r22, 0x01   ; 1
  e2:   8d e0           ldi r24, 0x0D   ; 13
  e4:   0c 94 81 01     jmp 0x302   ;  0x302
  e8:   61 e0           ldi r22, 0x01   ; 1
  ea:   8d e0           ldi r24, 0x0D   ; 13
  ec:   0e 94 ba 01     call    0x374   ;  0x374
  f0:   68 ee           ldi r22, 0xE8   ; 232
  f2:   73 e0           ldi r23, 0x03   ; 3
  f4:   80 e0           ldi r24, 0x00   ; 0
  f6:   90 e0           ldi r25, 0x00   ; 0
  f8:   0e 94 f5 00     call    0x1ea   ;  0x1ea
  fc:   60 e0           ldi r22, 0x00   ; 0
  fe:   8d e0           ldi r24, 0x0D   ; 13
 100:   0e 94 ba 01     call    0x374   ;  0x374
 104:   68 ee           ldi r22, 0xE8   ; 232
 106:   73 e0           ldi r23, 0x03   ; 3
 108:   80 e0           ldi r24, 0x00   ; 0
 10a:   90 e0           ldi r25, 0x00   ; 0
 10c:   0c 94 f5 00     jmp 0x1ea   ;  0x1ea

Unless you are an assembler expert, making sense of that would be pretty hard, and the time taken to do it would be better spent simply rewriting the code from scratch.

  • 2
    The magical command for converting from hex to asm seems to be "avr-objdump -j .sec1 -d -m avr5 myfile.hex". Found this in avrfreaks.net/comment/158180#forum-topic-top – Mikael Patel Feb 5 '16 at 13:43
  • Amended to incorporate your comment, thanks! – Nick Gammon Feb 5 '16 at 20:58
  • But there are decompilers that can convert machine code to C, but I assume those do not work with this kind of assembler? – Avamander Feb 5 '16 at 21:34
  • 1
    I don't see why they wouldn't work with this type, however there are limitations to what they can achieve. For one thing, they can't possibly know the variable names (apart from processor registers, I suppose) so your variables would become v1, v2, etc. The C compiler possibly inlines some function calls, so you might see functions turned into inline code. The compiler moves things around (or omits them, even) for efficiency, so that would be hard to get back. Anyway, re-reading the question, it doesn't seem like some very important stuff is there. – Nick Gammon Feb 5 '16 at 22:13
  • 1
    For a more complete backup of everything on the ATmega328p chip, avrdude -v -p m328p -c avrisp -U flash:r:m328p_flash.hex:i -U efuse:r:m328p_efuse.hex:h -U hfuse:r:m328p_hfuse.hex:h -U lfuse:r:m328p_lfuse.hex:h -U lock:r:m328p_lock.hex:h -U eeprom:r:m328p_eeprom.hex:i captures EEPROM data and fuse settings, as well as the executable flash object code. Restore backup by replacing all of the :r: with :w:. – MarkU Feb 6 '16 at 0:27

Not the answer you're looking for? Browse other questions tagged or ask your own question.