Skip to content

Instantly share code, notes, and snippets.

@osnr
Created January 25, 2021 07:51
Show Gist options
  • Save osnr/d5a7450065f488ea14d6f5a6a6fd44a3 to your computer and use it in GitHub Desktop.
Save osnr/d5a7450065f488ea14d6f5a6a6fd44a3 to your computer and use it in GitHub Desktop.

A few years ago, I tried to turn a Pi Zero into a ‘synthetic USB stick’ – where you’d plug it into your laptop and it’d appear like an ordinary flash drive, but the program on the Zero could intercept reads and writes and dynamically generate content. Like a FUSE filesystem, but as a piece of hardware, sort of.

So you could edit a Python file on your laptop, save it to the USB drive, and then the output of your Python code would magically appear as a sibling file on the drive; things like that. I thought it would be particularly cool as a way to get some dynamism and programmability into limited pseudo-computers (TVs, game consoles, phones, tablets) that know how to talk to USB sticks. (I did find a paper that uses this kind of fake USB stick as a way to hack a smart TV!)

I actually got it to work pretty quick (combine the Mass Storage Gadget built into Linux with BUSE), but I found that the results were disappointing: first, my Mac just refused to tell the flash drive when I did any write until it absolutely had to (e.g. when I ejected), and second (and worse), my Mac would pretty much never read the drive again after it had read it the first time, since it assumed nobody else would write to the drive. So my synthetic drive couldn’t, for example, make up a new file in a directory once my Mac had read the root directory listing, because it just wouldn’t show up.

@GoodClover
Copy link

GoodClover commented Sep 6, 2023

Reminds me of UF2 - from the user's perspective their Pi Pico, Micro Bit, etc opens as a thumb drive, with a few doc files. They drag a UF2 file in there and the device ejects itself, (usually) gets flashed with the program, and runs it.
Aside from the doc files this file system is fake - UF2 is a streamed network protocol1, packets with a memory location to write to, and a
payload to put there. This means it's possible to write directly to RAM rather than flash2.
Creating a .uf2 file is pre-authoring a message to send, before posting it.

I don't know if this works from Macs, but I imagine it must as this's become somewhat standard? It could be that creating a new file tells the device immediately, but the file's contents aren't written? So, the device claims it "wants to eject" leading to the data being written?

Footnotes

  1. as is any file (format) that can be read linearly, hello UNIX & Plan 9

  2. that's an idea: can you write to peripherals via it? could you directly write to a device's GPIO?

@GoodClover
Copy link

Ah, seems I'm a bit wrong saying "streamed", (usually in-order, usually non-repeated) 512 byte packets is maybe more correct.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment