how to copy a file from firmware storage to CIRCUITPY

Adafruit's tiny microcontroller platform. Please tell us which board you are using.
For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
asdffdsa6132
 
Posts: 28
Joined: Sat Feb 18, 2023 5:00 pm

how to copy a file from firmware storage to CIRCUITPY

Post by asdffdsa6132 »

hello, thanks, first time posting.
Model: NeoPixel Trinkey M0 Board-ID: SAMD21E18A-NeoTrinkey-v0

in TRINKEYBOOT, there is a file named 'INFO_UF2.TXT'

when is normal mode, by that i mean, non-firmware mode.
i need my python script to
copy TRINKEYBOOT/INFO_UF2.TXT to CIRCUITPY/INFO_UF2.TXT

thanks, david

User avatar
dastels
 
Posts: 15667
Joined: Tue Oct 20, 2015 3:22 pm

Re: how to copy a file from firmware storage to CIRCUITPY

Post by dastels »

You have no access to TRINKEYBOOT in non-bootloader mode.

Why do you need to do this? There might be a better/possible way.

Dave

User avatar
asdffdsa6132
 
Posts: 28
Joined: Sat Feb 18, 2023 5:00 pm

Re: how to copy a file from firmware storage to CIRCUITPY

Post by asdffdsa6132 »

Why do you need to do this? There might be a better/possible way.
i am sure there is ;wink

basically, TRINKEYBOOT has over 7.0MiB free.
so i thought that i could store files there,
then from code.py, save to CIRCUITPY or push that file over usb keyboard.

or can we reserve a chunk of TRINKEYBOOT so that it is accessible to code.py

User avatar
neradoc
 
Posts: 542
Joined: Wed Apr 27, 2016 2:38 pm

Re: how to copy a file from firmware storage to CIRCUITPY

Post by neradoc »

Hi, the BOOT drive does not exist, it's simulated for the single purpose of flashing the firmware with a UF2 file.
It presents a large size so that host operating systems don't reject UF2 files because of their size, regardless of the actual available flash.
As the product page mentions, the Trinkey has a SAMD21 chip with 256kB of flash, and no external flash. Most of it is occupied by Circuitpython itself, leaving 48kB for the user's files as CIRCUITPY.

User avatar
asdffdsa6132
 
Posts: 28
Joined: Sat Feb 18, 2023 5:00 pm

Re: how to copy a file from firmware storage to CIRCUITPY

Post by asdffdsa6132 »

thanks,

my goal is that when i plugged in the key into a computer,
that CIRCUITPY is empty, no files at all.

then via code.py, copy a file from firmware to CIRCUITPY.
for example,
.bek, used to unlock bitlocker on boot.
.key used to unlock keepass.

and about `microcontroller.nvm`, seems there is 256Bytes.
is that something boot.py and code.py can use? or is it reserved for the firmware?

User avatar
dastels
 
Posts: 15667
Joined: Tue Oct 20, 2015 3:22 pm

Re: how to copy a file from firmware storage to CIRCUITPY

Post by dastels »

First, as neradoc said, TRINKEYBOOT (or any of the BOOT "drives") aren't flash drives. They are the bootloader exposed as a fake flash drive so you can copy a UF2 firmware file to the board rather than have to use a special programmer (e.g. J-LINK). IT ISN'T A DRIVE, it just looks like one to trick your OS.

Second, as I said, you can't access one from the other. If you have a BOOT "drive" the bootloader is running. If you have the CIRCUITPY drive CircuitPython is running.

End of discussion.

Dave

User avatar
neradoc
 
Posts: 542
Joined: Wed Apr 27, 2016 2:38 pm

Re: how to copy a file from firmware storage to CIRCUITPY

Post by neradoc »

You want the files to be hidden when the board mounts ?
Like some sneaky protection feature by pretending it's an empty USB key ?
And make them appear, how would you want to trigger that ?

That's not something that you could do without making a custom build of Circuitpython, code.py at least would have to be there, and then there are other problems with writing files and having them seen by the PC. You can hide the drive altogether from boot.py, and only mount it in some circumstances, although the board doesn't have a lot of inputs to trigger something like that. If you want a sneaky drive it might possible to write that in Arduino with tinyusb, by swapping between two partitions.

User avatar
asdffdsa6132
 
Posts: 28
Joined: Sat Feb 18, 2023 5:00 pm

Re: how to copy a file from firmware storage to CIRCUITPY

Post by asdffdsa6132 »

good idea, mounting on the fly could work, but i think would be difficult to debug.

the board doesn't have a lot of inputs to trigger something like that
true, basically two buttons.
with code.py, if i press the correct morse code sequnce, then the triggers some action.

i think for now, store the files in CIRCUITPY in crypted format.
then use code.py to decrypt them.

thanks much,
david

User avatar
asdffdsa6132
 
Posts: 28
Joined: Sat Feb 18, 2023 5:00 pm

Re: how to copy a file from firmware storage to CIRCUITPY

Post by asdffdsa6132 »

tried to delete this post, but was not allowed
Last edited by asdffdsa6132 on Sun Feb 19, 2023 5:39 pm, edited 1 time in total.

User avatar
dastels
 
Posts: 15667
Joined: Tue Oct 20, 2015 3:22 pm

Re: how to copy a file from firmware storage to CIRCUITPY

Post by dastels »


User avatar
asdffdsa6132
 
Posts: 28
Joined: Sat Feb 18, 2023 5:00 pm

Re: how to copy a file from firmware storage to CIRCUITPY

Post by asdffdsa6132 »

thanks,
when i reset the device by pressing the reset button. i get the expected behavior.
--- boot.py creates a text file named 'boot.ext', that works fine.
--- code.py creates a text file, named 'code.ext' that works fine.
but after that,
if i delete 'code.ext' and re-run code.py, code.py does NOT recreate the text file.

only after hard reset, will code.py create the text file

boot.py

Code: Select all

supervisor.runtime.rgb_status_brightness = 5
storage.remount("/", readonly=False, disable_concurrent_write_protection=True)
with open('/boot.ext', 'w') as f:
    f.write('boot')
code.py

Code: Select all

with open('/code.ext', 'w') as f:
    f.write('code')

User avatar
dastels
 
Posts: 15667
Joined: Tue Oct 20, 2015 3:22 pm

Re: how to copy a file from firmware storage to CIRCUITPY

Post by dastels »

I believe you risk corrupting the filesystem if both CircuitPython and your computer have write access at the same time, I.e. if boot.py mounts it as writable, you shouldn't write to it from your computer.

This is noted explicitly in a bright red warning block on that page I inked to:
You can only have either your computer edit the CIRCUITPY drive files, or CircuitPython. You cannot have both write to the drive at the same time. (Bad Things Will Happen so we do not allow you to do it!)
Ignore that at your own risk.

The typical usecase is for boot.py to make it writable when it's running without USB connected: to be able to log readings and such. That's why it's usually done conditionally on some input. I.e. you boot it in "running mode" or "programming mode".

Dave

User avatar
asdffdsa6132
 
Posts: 28
Joined: Sat Feb 18, 2023 5:00 pm

Re: how to copy a file from firmware storage to CIRCUITPY

Post by asdffdsa6132 »

ok, first of all, the trinkkey is fantastic and so is the tech support here.

so i figured out what is going on,
when using this command
`storage.remount("/", readonly=False, disable_concurrent_write_protection=True)`

if {boot|code}.py creates a file, the file is created, but not visible to the host.
and that is ok, i can work around that.

User avatar
neradoc
 
Posts: 542
Joined: Wed Apr 27, 2016 2:38 pm

Re: how to copy a file from firmware storage to CIRCUITPY

Post by neradoc »

asdffdsa6132 wrote: Sun Feb 19, 2023 7:04 pm if i delete 'code.ext' and re-run code.py, code.py does NOT recreate the text file.
USB drives are not supposed to change on their own, so operating systems and drivers on the host side can cache the content, and not show changes that you made until the board is unmounted (by "ejecting" it in the OS for safety) and remounted (with a reset).
Even worse, it could overwrite the changes if you put it in a non safe mode, by writing the cache back if, say, it saves some metadata like the access time of the file when it reads it. That's one of that way that the unsafe mode can corrupt the drive, which can also happen if both side write at the same time, overwriting each other.

boot.py runs before the USB drive is mounted, so the changes would be visible to the PC, but code.py runs after that, so if the PC accessed the drive and cached it, there is a possibility that the changes are not seen.

User avatar
dastels
 
Posts: 15667
Joined: Tue Oct 20, 2015 3:22 pm

Re: how to copy a file from firmware storage to CIRCUITPY

Post by dastels »

That warning wasn't just for fun, you know.

Dave

Locked
Please be positive and constructive with your questions and comments.

Return to “Trinket ATTiny, Trinket M0”