Store/Retrieve file from 328's memory

I would like to know if it is theoretically possible to store a file on the onboard memory (32kb flash for example) and then somehow get it off, onto a computer and run it on the computer.

Say I had a 10kb exe file that runs on windows just like any normal exe would. Can I somehow store it on the arduino and then pull it off later?

I realize it may not be possible to do this as an exe form... might it be possible to store the binary data from the exe file on the arduino and then get it off via usb cable (serial) to the computer and then re-assemble it into an exe file on the computer side?

The goal is to transfer a file using the nano in this case, without any special external hardware, using only the capabilities of the nano... and a computer.

Thank you in advance.

Wouldn't it be far far simpler to use a USB stick?

Whether you use a Nano or a USB stick you will need a program running on the PC to accept the data and store it as a file on the PC.

...R

Of course but I'm not asking this question because I don't have a USB stick. This isn't meant to be easy or simple, merely to explore the capabilities of the 328. I want to know if it's possible... "just cuz".

To clarify the scope of the question, I want to start with a fully functioning exe file on computer A, convert it into its raw content (machine code? Hex?), store that on the nano's onboard memory (flash memory?) and then get it off somehow (usb serial?) onto computer B, where I can re-assemble the exe file and run it in the same way that it ran on computer A.

Yes, that is possible.

K... could you perhaps explain how to do it?

IFIAK on the AVRs you can only write into flash from a bootloader, so you would have to write a custom bootloader that grabbed the data from (for example) USB and saved it, then at a later date transmitted it back out the USB.

You could use serial, SPI etc as well with an appropriate converter for the PC, but USB is already in place.

There are several open-source boot loaders around, if you grab one I would say that's 70% of the job already done.

No need to use a custom bootloader. Use AvrISP to read and write flash with any data you like.

From the looks of this program, it appears to re-flash the memory and overwrite whatever's there with your new content. What if I want to retain the current bootloader (because let's say there's already a program on the micro that I want) but I just want to piggyback this exe file on the unused portion of it. I guess my thinking is to not have to make the entire memory block dedicated to just file transfer but to use just the free space for temp storage.

Also, if an exe contains nothing more than command lines or hex entries or something, can't I just use PROGMEM to copy/paste the exe's contents directly into my program space and then later dump it to the computer using a usb cable and the serial output feature or something like that?

No need to use a custom bootloader. Use AvrISP to read and write flash with any data you like.

True, but that will need programming hardware and software won't it?

No need to use a custom bootloader. Use AvrISP to read and write flash with any data you like.

This is different to the original question.

If the file exported from the IDE is in a standard HEX format you could easily add data to the end of the file but you'd have to write a program to do that (unless there is one around already).

Another way would be to write a program to convert the EXE data into a C array in a text file and add that to your Arduino program. This is a very simple thing to do if you can use one of the 1000 languages that run on a PC.

This is the question I was answering:

Say I had a 10kb exe file that runs on windows just like any normal exe would. Can I somehow store it on the arduino and then pull it off later?

That was my fault for mis-forming the question. While it is interesting to know how to re-purpose the entire flash memory block to directly store files, what I really wanted to know was how to take advantage of only the free portion of flash, not currently being used by any bootloader and/or programs to store the contents of an exe file in some sort of plain text format like hex or binary or something. Thus you would not be actually storing the program but storing its contents as a form of data currency that could be re-assembled on the other end. I just downloaded some sort of program that turns any exe file into hex but I'm having trouble converting it back.

I've succeeded in using a hex editor to turn an exe file into hex and back again. Question now is, what sort of variable should I use to store this massive string of hex code in the flash memory? Something tells me if I simply define a string it will overflow. Then the next question is whether the serial library will allow me to simply print the entire thing in 1 shot so I can copy it out of the serial window.

You could place you exe-file-hex-image as a byte array in PROGMEN by generating a C-definition from your hex.
This would allow one file to be combined with the Arduino program and flashed with it.

At least this would give you an environment to experiment with the Arduino to PC problem.

I see. That would be quite helpful. Just because that was a heck-of-a-lot-of-words all at once, if I were to provide this snippet as an example,

:10003000000000000000000000000000F0000000D0
:100040000E1FBA0E00B409CD21B8014CCD21546861
:1000500069732070726F6772616D2063616E6E6F7D
:10006000742062652072756E20696E20444F5320A3

How would I declare the byte array so as to store this snippet? The way I'm used to defining arrays is something like:

char array = {a, b, c, d, e};

something like that, but this doesn't look like it's going to work that way.

The Intel hex Format has much more than the netto data in it, I would not store the whole records in PROGMEM,
but only the raw data (a hex dump of the file).

But let's just ignore the colons and the format to see a possible syntax for a PROGMEM byte array.

const byte exeFile[] PROGMEM = {
  0x10, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x00, 0x00, 0x00, 0xD0,
  0x10, 0x00, 0x40, 0x00, 0x0E, 0x1F, 0xBA, 0x0E, 0x00, 0xB4, 0x09, 0xCD, 0x21, 0xB8, 0x01, 0x4C, 0xCD, 0x21, 0x54, 0x68, 0x61
  0x10, 0x00, 0x50, 0x00, 0x69, 0x73, 0x20, 0x70, 0x72, 0x6F, 0x67, 0x72, 0x61, 0x6D, 0x20, 0x63, 0x61, 0x6E, 0x6E, 0x6F, 0x7D
  0x10, 0x00, 0x60, 0x00, 0x74, 0x20, 0x62, 0x65, 0x20, 0x72, 0x75, 0x6E, 0x20, 0x69, 0x6E, 0x20, 0x44, 0x4F, 0x53, 0x20, 0xA3
};

Aha! Just as I read this I discovered an export option to "c", which pretty much spits out the exact same thing you just posted. Yes, I can work with this! So long as there is no limit to the array size (up to the available space in flash), I can just export in the c format and loop through the array elements and spit them out the serial port, copy, paste, run through excel as space delimited, convert to comma delimited and re-import back into hex editor and save as exe. That should just about do it.

The only thing that sucks is that you need to have the SDK on both computers to do this (yes?)

You could transfer the file (even in binary format with some framing) and write it directly to an exe from the receiving program.

Now I can be the coolest kid in the playground, transferring 10k files via my Arduino nano... plus 30 minutes of screwing around with software on 2 separate machines BUT IT'S OK cuz it's coooool.

[EDIT] Is what you're suggesting that I can create a program to re-construct the exe from binary data without the need for the Arduino serial window? Come to think of it, as long as the arduino is programmed to output serial data to the TX pin, it doesn't really matter what software picks it up right? Anything capable of reading a com port ought to work.