Skip to content

Added rp2_flash ioctl to get the flash fs_base_address #12357

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

sinihappo
Copy link

There is no way to get the flash file system base address. I added an ioctl 0x1000 to get that address.

Please feel free to modify the implementation.

@dpgeorge
Copy link
Member

dpgeorge commented Sep 4, 2023

Thanks for the contribution.

What do you intend to do with this value? Maybe it needs to have XIP_BASE added to it?

I actually implemented the same thing as part of #8381.

@sinihappo
Copy link
Author

What do you intend to do with this value? Maybe it needs to have XIP_BASE added to it?

I want to automate generating compact littlefs uf2 files to make it easier for a beginner to bootstrap both Micropython and initial file system stub. To create uf2 files, the absolute address is needed. Although now the address is fixed, it would be practical to be able to query it programmatically. So the parameter would be there when it is needed. And if anyone produced a Micropython version with different flash layout, the address would be obtainable, as well.

And the needed amount of code is tiny.

If anyone feels different approach, be free to modify!

And sorry for the poor pull request quality, this is my first for micropython!

@Gadgetoid
Copy link
Contributor

And if anyone produced a Micropython version with different flash layout, the address would be obtainable, as well.

This should, IMO, be explicitly defined in the linker script, or at least inherited and processed from the board config such that the symbol is easy to obtain. There's some discussion around this here - #8761 - but it ran into a roadblock with the binary descriptors bi_decl not being compatible with dynamic linker symbols.

Currently this bi_decl stuff probably does what you want- since XIP_BASE + MICROPY_HW_FLASH_STORAE_BASE is explicitly tagged in the binary as readable/writable here:

// Tag the flash drive in the binary as readable/writable (but not reformatable)
bi_decl(bi_block_device(
BINARY_INFO_TAG_MICROPYTHON,
"MicroPython",
XIP_BASE + MICROPY_HW_FLASH_STORAGE_BASE,
MICROPY_HW_FLASH_STORAGE_BYTES,
NULL,
BINARY_INFO_BLOCK_DEV_FLAG_READ |
BINARY_INFO_BLOCK_DEV_FLAG_WRITE |
BINARY_INFO_BLOCK_DEV_FLAG_PT_UNKNOWN));

You can read this information directly from a .uf2 file with: picotool info -a. Eg:

picotool info -a pimoroni-tufty2040-v1.20.5-micropython.uf2
File pimoroni-tufty2040-v1.20.5-micropython.uf2:

Program Information
 name:            MicroPython
 version:         856e08b1931b88271816a2f60648f6ff332235b2, tufty2040 v1.20.5
 features:        USB REPL
                  thread support
 frozen modules:  _boot_fat, rp2, _boot, version, uasyncio, uasyncio/core, uasyncio/event, uasyncio/funcs, uasyncio/lock, uasyncio/stream, onewire, ds18x20, dht, neopixel, pimoroni, boot
 binary start:    0x10000000
 binary end:      0x100ab2cc
 embedded drive:  0x10100000-0x10800000 (7168K): MicroPython

Fixed Pin Information
 none

Build Information
 sdk version:       1.5.1
 pico_board:        pimoroni_tufty2040
 boot2_name:        boot2_w25q080
 build date:        Sep  6 2023
 build attributes:  MinSizeRel

Note the embedded drive: 0x10100000-0x10800000 (7168K): MicroPython line. Yes, Tufty2040 has a 7MB LittleFS drive.

I have a utility - dir2uf2 - for packing up a uf2 filesystem for MicroPython and currently hard-code address offsets into my CI so I'm very interested in where you're going with this.

One big problem I encountered was pruning the LittleFS filesystem to the size of the contained files, and ensuring all the occupied blocks are contiguous so I don't have to include the entire flash of the MicroPython build in a single uf2. This is just passable for 2MB boards, but anything larger is painful. Did you find a solution to this?

@dpgeorge
Copy link
Member

This functionality to get the flash address was implemented differently in 50a7362: it's now possible to do memoryview(flash) and it will return a memoryview that has the base address of the memory-mapped flash, and its size. Then you could use uctypes.addressof(memoryview(flash)) if you want the address.

@dpgeorge dpgeorge closed this Mar 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants