Integrate Hex into sketch?

Hi, I have got a .hex file with no source. If flashes just fine, works just fine, but I want to use it as a function within a normal arduino sketch. Is this possible?

Think about it; a complete sketch contains all the pre-main initialisation, "main()" with "init()", "setup()" and "loop()", plus user functions.
What were you going to call?

This particular HEX file only does one thing...it runs a complex script for about 5 seconds, and then it just sits doing nothing until you hit reset on the arduino. I would like to be able to just encapsulate this 5 second script, so that I could call it like it was a library function.

Contact the author and ask for the source code, object file, or library.

I would if I could; but he posted this hex anonymously; I can't even send a private message via a forum. I posted a plea for the source to be posted, but got no response.

If this were a PC app, I would just decompile it...but I don't know of any decompilers for Arduino hex files...so I am in a tough place. I don't want to need to use two seperate arduinos for this single project, and I certainly would not need to if I had source...even if it was the confusing, almost useless source you get from a decompiler.

You could put a 555 on the reset line

I'm afraid that won't work...perhaps I need to be more specific...

The HEX in question allows the arduino to simulate a certain USB device (this is 100% legal by local laws, but I would still rather not go into specifics...I'll just say it is related to the PS3)...when you plug the arduino into the system using the virutal USB port created from pins, the arduino identifies itself as this USB device, issues a few commands just like this device would, confirms that a condition 0 is reached, and then turns on a LED to indicate success...at this point, it is doing nothing; you can even unplug it from the USB at this point.

I want to use this as a function...so that I can write a sketch that does about 10 different things, runs the hex, and then does about 10 different other things, before being dropped to a loop that does a few other things. Note that while the hex does send a few status updates by serial communication through the onboard USB port, everything I really need it to do is all done through the virtual USB port.

Perhaps by now you've gotten the impression that what you want to do can't be done.

Can't be done? Can't? That is a 4-letter word worse than anything banned from TV. Anything can be done; that is what it means to be human!

Anything can be done; that is what it means to be human!

So if you are human why aren't you doing it, along with squaring the circle, travelling faster than light, writing down the whole of Pye. Lots of stuff can't be done and us humans can prove it can't be done as well.

I am not doing it because I am a beginner that does not know how do to it...and I was hoping that someone else already did it for me. I have no question that this can be done...and I am amazed that it has not been done already (actually, I don't believe that at all...I just have not found where it was done in the past)

They can calculate pi to an almost infinite number of places...clearly there is a mathematical formula...it just does not fit into our 10-base system very well.

I am currently traveling faster than light, as the part of the space that I reside in is traveling faster than the speed of light. This means that with sufficient technology, energy, and knowledge, one could reach any speed...even a speed as extreme as a stop.

There are many things that humans don't yet know how to do...but I strongly believe that nothing is impossible, except an impossibility.

I have no question that this can be done.

Well, since there can only be one hex file at a time loaded on the Arduino, you are going to be very hard pressed to figure out how to invoke another hex file from the current one.

If you want to change the only-one-sketch restriction, you have a lot of work to do.

But, since it's not impossible, don't let us stop you.

A simple decompiler would do the job regardless of any arduino hardware limitations...if there really is no such beast for the arduino, I might scrap my current project and start on that.

There have been many requests for a decompiler. Please keep us informed of your progress.

Keep in mind, though, that compiling is lot like encryption. You can't necessarily get back to the source code since there might be more than one way to achieve the same hex code, since the compiler optimizes the code during the compile process.

You'll be especially challenged when it comes to reconstructing a class from the hex file.

But, since anything is possible, don't let these minor challenges stand in your way.

it isn't impossible, but it is tricky. One approach that might work:

  1. convert the hex to assembler

  2. figure out how to build the assembler, then you have some semblance of source control. It might just be a big asm block in your script, wrap it in a function, or it might be horribly convoluted requiring address translations and etc..

  3. Examine the assembler and see if it goes into a continuous loop at the end of execution and remove the loop. avrstudio might help here.

  4. see if you can call the function from a script

  5. good luck :slight_smile:

A simple decompiler is less useful in embedded systems, as you don't have any operating system calls as such to give you some interfacing points to known functions. Your hex file is just one big chunk which does everything. If a slightly older version of your standard library was used, you can't even compare the calls to the one you have.

avr-objdump can disassemble hex files for you, although you need to give it a few hints. If you know for which architecture it is, you can compile a random project for the same processor and then do:

avr-objdump -f ramdomproject.cpp.elf

This will give you the right architecture (eg avr:5) to use then on the hex file with:

avr-objdump -D --architecture=avr:5 mystery.hex
avr-objdump -s --architecture=avr:5  mystery.hex

The first will output everything assuming it's code, the second just dumps the hex values. With that and some poking around, you'll be able to assemble the code fragments. A good understanding of what compiler generated assemble looks like helps a lot. It will make it a lot easier to spot the code and library calls. No use in wasting time on Serial.println, the fact it's called is enough. If you have a suspicion what function a block of code could be, add it to a random project and compare the generated assembler with what you have. That also speeds up the work. You'll also have to unmangle the variables used.

However, this process is quite a pain and in most cases you're quicker by rewriting the code, perhaps with some poking in the disassembler to get hold of hidden data.

If you go that way, try to identify the Virtual USB library function in the hex, then you're probably on a good way to see what happens. As there aren't that many libraries for this out there, if you find the right one, a lot of things become easier.

The code, most likely is a bunch of:

do something
call setupusb
do something
call usbfunction
do something
call anotheusbfunction
...
turn led on

The code where the led is turned on is pretty easy to spot, so i guess you best move form there upstream until you reach the first usb-function.

As an alternative, you might also hack the hex file to add some you your code after the led has been turned on - perhaps waiting for input on an unrelated pin and then jump back to the start.

And as a final thought, if it relates to the PS3 hack, look for code published for other microprocessors (atmega90U, pic, etc). That will tell you what the thing is supposed to be doing with the USB. Given the correct library, things won't be too hard re-implementing.

Korman

How about...

Two Arduinos. One contains your new code and the other, your mystery sketch. You wire the finished led on the second to an input on the first.

On power up, Arduino 1 either holds the second in reset or uses a relay to control its power. When part one is done it powers on the second and then loops waiting for the "led" signal. It can then finish its own code.

Might work? No idea.

What's wrong with a few simple tweaks to the bootloader?

simple?!? I like Eights approach, from a "git 'er done" perspective, you could have something working in no-time.