RP2 Bootloader Emulator ?
One of the challenges of using UF2 files with RP2 devices is you can see what's in them, make assumptions about what that will deliver, but can't easily tell what they will actually do, what will ultimately be put in Flash or where. From dragging and dropping a UF2, or uploading with 'picotool', it's a completely invisible process, a black box system.
The best one can do is dump the entirety of Flash after uploading and see if that matches expectations. Then you have to figure out why not if it doesn't.
What would be useful is some sort of RP2 Bootloader Emulator with Virtual Flash which doesn't require a physical RP2, can be used to better see what's going on.
A fast-track for that may be a Bootloader Emulator, supporting MSD and USB upload modes, which runs as the executable on an RP2. But a pure software solution would be best,
I had considered doing that myself but I'm not a proficient C programmer and don't know C++ at all. Trying to do it in Python is a non-starter given the effort of replicating the 'picotool' parts and the Boot ROM accurately and keeping up to date with any changes there may be.
A Python-based UF2 uploader might have some utility in itself but, unless it replicates 'picotool' exactly, it's not much of an Emulator.
Has anyone seen anything like this already or has any ideas on how to deliver something useful ?
The best one can do is dump the entirety of Flash after uploading and see if that matches expectations. Then you have to figure out why not if it doesn't.
What would be useful is some sort of RP2 Bootloader Emulator with Virtual Flash which doesn't require a physical RP2, can be used to better see what's going on.
A fast-track for that may be a Bootloader Emulator, supporting MSD and USB upload modes, which runs as the executable on an RP2. But a pure software solution would be best,
I had considered doing that myself but I'm not a proficient C programmer and don't know C++ at all. Trying to do it in Python is a non-starter given the effort of replicating the 'picotool' parts and the Boot ROM accurately and keeping up to date with any changes there may be.
A Python-based UF2 uploader might have some utility in itself but, unless it replicates 'picotool' exactly, it's not much of an Emulator.
Has anyone seen anything like this already or has any ideas on how to deliver something useful ?
Re: RP2 Bootloader Emulator ?
Where are you getting these unknown UF2 files from?
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter
Pi Interests: Home Automation, IOT, Python and Tkinter
Re: RP2 Bootloader Emulator ?
They usually appear as a result of the development process, when merging UF2 or creating a UF2 with other than a single image plus an E10 Fix. The most common example will be a 'universal UF2' which contains an RP2040 and RP235X image, but may contain executable plus data partitions, and can be more advanced as here - viewtopic.php?t=388069
As there, it's not just the UF2 but how it interacts with any partitioning regime in place in the target device.
Partitioning allows some pretty clever stuff to be done but the more complicated a partitioning scheme is the harder it can be to craft a UF2 which suits that.
Having something which helps verify a UF2 and partitioning scheme will do what is required can currently only be done by trying it and seeing, with little insight available as to why things have ended up as they have. Or asking an expert who already knows.
There's little opportunity for gaining a comprehensive understanding of the upload capabilities without studying and understanding the documentation, Boot ROM code and 'picotool' code, trying it on real hardware.
I also think it's useful to be able to try things without it having to be potentially destructive testing with real hardware. I'd like to mess about with all manner of partitions, Family ID, code protections, signings, hashing, and OTP settings, to see what affects they have, to better understand things. I'd rather prove something does what is expected under emulation than on a real chip and then have to throw that in a bin.
When tests may be destructive there is a tendency to test only that something appears to work. It introduces reluctance to test what doesn't work or edge cases. Things which one might ideally want to test or should be testing.
One could argue, if such an RP2 Bootloader Emulator had been created as part of the RP235X development process, the bug which the E10 Fix is needed to work around on A2 silicon would have been identified, found and corrected, before silicon was finalised and no one would have had to deal with the complications and consequences of that.
It may also have allowed identification during development of the E18 'bricked device' issue, the E22 'will need to upload via SWD to allow things to work again' issue, plus E13, E14, E15, E19, E23 and E25 issues.
But that would depend on how closely and accurately an emulator matches the reality of the physical chip.
It's generally accepted that having emulators and simulators allows more extensive and comprehensive testing than 'try it and see' on physical chips, is usually easier and cheaper, and can pay dividends when it does identify an issue. Plus it's useful for gaining a better understanding of things, appreciation for what something can do.
As there, it's not just the UF2 but how it interacts with any partitioning regime in place in the target device.
Partitioning allows some pretty clever stuff to be done but the more complicated a partitioning scheme is the harder it can be to craft a UF2 which suits that.
Having something which helps verify a UF2 and partitioning scheme will do what is required can currently only be done by trying it and seeing, with little insight available as to why things have ended up as they have. Or asking an expert who already knows.
There's little opportunity for gaining a comprehensive understanding of the upload capabilities without studying and understanding the documentation, Boot ROM code and 'picotool' code, trying it on real hardware.
I also think it's useful to be able to try things without it having to be potentially destructive testing with real hardware. I'd like to mess about with all manner of partitions, Family ID, code protections, signings, hashing, and OTP settings, to see what affects they have, to better understand things. I'd rather prove something does what is expected under emulation than on a real chip and then have to throw that in a bin.
When tests may be destructive there is a tendency to test only that something appears to work. It introduces reluctance to test what doesn't work or edge cases. Things which one might ideally want to test or should be testing.
One could argue, if such an RP2 Bootloader Emulator had been created as part of the RP235X development process, the bug which the E10 Fix is needed to work around on A2 silicon would have been identified, found and corrected, before silicon was finalised and no one would have had to deal with the complications and consequences of that.
It may also have allowed identification during development of the E18 'bricked device' issue, the E22 'will need to upload via SWD to allow things to work again' issue, plus E13, E14, E15, E19, E23 and E25 issues.
But that would depend on how closely and accurately an emulator matches the reality of the physical chip.
It's generally accepted that having emulators and simulators allows more extensive and comprehensive testing than 'try it and see' on physical chips, is usually easier and cheaper, and can pay dividends when it does identify an issue. Plus it's useful for gaining a better understanding of things, appreciation for what something can do.
Last edited by hippy on Fri May 23, 2025 1:34 pm, edited 1 time in total.
Re: RP2 Bootloader Emulator ?
Can use SWD debugger to see how the BOOTROM works, it's not destructive, also can see Flash (live), peripherals etc.
The only irreversible thing would be OTP settings, though some settings can be simulated without permanently writing to it.
The only irreversible thing would be OTP settings, though some settings can be simulated without permanently writing to it.
Re: RP2 Bootloader Emulator ?
A software solution allows testing without any hardware, and is usually simpler to do. It's avoiding the convoluted nature of how it might otherwise be done which is the key purpose of emulation. It makes it easier, more straight-forward, lowers the bar for entry.
Single-stepping or run-to-breakpoint, noting where execution got to, what registers and variables contain, having to figure out what that means is usually inferior to a bunch of 'if' and 'printf' which can do that for you. It's not possible to add that to the ROM code, but it is usually trivial to add to an emulation of the same
Also, when SWD debugging, I believe USB is disabled ( or soon breaks anyway ), so I am not sure how one would debug Bootloading via MSD or Bootloading via USB. I am not convinced it would allow the testing I would like to do.
Re: RP2 Bootloader Emulator ?
Actually USB is working well if you don't interrupt it in its critical moments, even though it's just the Windows driver complaining, managed to recover also that, might be annoying, but it's not a show stopper.
Look, picotool in BOOTSEL mode under debugger:
Can use watchpoints, realtime tracing... bootrom is available with debug info (thanks to @kilograham).
And you can use the debugger itself (console, or programmatic) to make live Flash operations (sector erase, write, read etc.).
I think RPi used FPGA version to test.
Look, picotool in BOOTSEL mode under debugger:
Code: Select all
Program Information
name: picow_wifi_scan_background
features: UART stdin / stdout
binary start: 0x10000000
binary end: 0x1004b6cc
target chip: RP2350
image type: ARM SecureAnd you can use the debugger itself (console, or programmatic) to make live Flash operations (sector erase, write, read etc.).
I think RPi used FPGA version to test.
Re: RP2 Bootloader Emulator ?
All sounds like a lot of effort and bit complicated to me. Fine if prepared to get to grips with that but there has got to be an easier way.gmx wrote: ↑Fri May 23, 2025 3:33 pmActually USB is working well if you don't interrupt it in its critical moments, even though it's just the Windows driver complaining, managed to recover also that, might be annoying, but it's not a show stopper.
Can use watchpoints, realtime tracing... bootrom is available with debug info (thanks to @kilograham).
And you can use the debugger itself (console, or programmatic) to make live Flash operations (sector erase, write, read etc.).
I suppose my goal is 'dumbing it down', reducing user effort, making things simpler to test and saving having to think whether a solution will work. Reducing the entry level as said. I don't believe it should be necessary for anyone to buy a Debug Probe or an RP2 board with the appropriate amount of Flash to do what can be done with just software.
Take this typical example of an executable image with an E10 Fix -
Code: Select all
┌─────┬─────┬─────┬─────┬─────┬─────┬─────┬─── ─┬─────┬─────╥─────╖
E10 │ │ │ │ │ │ │ │ │ │ ║ E10 ║
└─────┴─────┴─────┴─────┴─────┴─────┴─────┴─ ───┴─────┴─────╨─────╜
┌─────┬─────┬─────┬─────┬─────╥─────┬─────┬─── ─┬─────┬─────┬─────┐
IMG │ A │ B │ C │ D │ E ║ │ │ │ │ │ │
└─────┴─────┴─────┴─────┴─────╨─────┴─────┴─ ───┴─────┴─────┴─────┘
Code: Select all
pi@Pi4B:~/tmp $ rp2-emulate rp235x 4mb msd xyzzy.uf2
E10 0x10FFFF00 -> Flash 0x103FFF00 - Wrapped on 4MB Flash
IMG 0x10000000 -> Flash 0x10000000
IMG 0x10001000 -> Flash 0x10001000
IMG 0x10002000 -> Flash 0x10002000
IMG 0x10003000 -> Flash 0x10003000
IMG 0x10004000 -> Flash 0x10004000
Wrote 0x10000000 .. 0x10004FFF - IMG
Wrote 0x103FF000 .. 0x103FFFFF - E10
The write to end of Flash might surprise some but those of us who know were expecting that, already know that will corrupt any data held at the end of Flash. And know the E10 Fix sector can be moved.
So what if we have the E10 Fix provide the last sector of the executable which saves one erase-write, and avoids corrupting the end of Flash ?
Code: Select all
┌─────┬─────┬─────┬─────╥─────╥─────┬─────┬─── ─┬─────┬─────┬─────┐
E10 │ │ │ │ ║ E ║ │ │ │ │ │ │
└─────┴─────┴─────┴─────╨─────╨─────┴─────┴─ ───┴─────┴─────┴─────┘
┌─────┬─────┬─────┬─────╥─────┬─────┬─────┬─── ─┬─────┬─────┬─────┐
IMG │ A │ B │ C │ D ║ │ │ │ │ │ │ │
└─────┴─────┴─────┴─────╨─────┴─────┴─────┴─ ───┴─────┴─────┴─────┘
Code: Select all
pi@Pi4B:~/tmp $ rp2-emulate rp235x 4mb msd hacked-xyzzy.uf2
ABS 0x10004000 -> Flash 0x10004000
IMG 0x10000000 -> Flash 0x10000000
IMG 0x10001000 -> Flash 0x10001000
IMG 0x10002000 -> Flash 0x10002000
IMG 0x10003000 -> Flash 0x10003000
Wrote 0x10000000 .. 0x10004FFF - IMG + ABS
But then someone throws water on our parade by reminding us of partitions, tells us what we have done is flawed, won't work if we have partitions.
And we can test that claim -
Code: Select all
pi@Pi4B:~/tmp $ rp2-emulate rp235x 4mb erase msd pt.uf2
0 Partition Table Definition Flash 0x10000000 .. 0x10000FFF
1 DAT 0x10000000 .. 0x1006EFFF -> Flash 0x10001000 .. 0x1006FFFF
2 IMG 0x10000000 .. 0x1000FFFF -> Flash 0x10070000 .. 0x1007FFFF
3 IMG 0x10000000 .. 0x1000FFFF -> Flash 0x10080000 .. 0x1008FFFF
Code: Select all
pi@Pi4B:~/tmp $ rp2-emulate rp235x 4mb msd hacked-xyzzy.uf2
ABS 0x10004000 -> Flash 0x10004000
IMG 0x10000000 -> Flash 0x10070000 - Partition 2 0x10000000 -> 0x10070000
IMG 0x10001000 -> Flash 0x10071000 - Partition 2 0x10000000 -> 0x10070000
IMG 0x10002000 -> Flash 0x10072000 - Partition 2 0x10000000 -> 0x10070000
IMG 0x10003000 -> Flash 0x10073000 - Partition 2 0x10000000 -> 0x10070000
Wrote 0x10004000 .. 0100004FFF - ABS
Wrote 0x10070000 .. 0x10073FFF - IMG
That should have been obvious but we can try all sorts of ideas and test those without wasting time using hardware, until we accept it can only work without partitions, not with.
If we had attempted this with hardware it would likely have been a miserable experience. The incomplete executable would probably have hung or in some way crashed. We would have had to download or examine the Flash to see that part of the executable was 'missing', was somewhere other than it needs to be, and maybe have to figure out why that was.
With the emulator I am imagining it pretty much tells us what happened and why, and near instantly.
This was a trivial example. Imagine if someone has a more complicated scenario and comes to the forum to say "I've signed and secured my Pico 2 code but now it won't accept my drag-and-drop downloads".
What do we tell them; "dunno" and wish them luck with figuring it out ?
Without there being any insight into what's happening that's probably the best we can do unless prepared to indulge in debugging something which could be tortuous to get to the bottom of, something we might not relish doing even if it were our own problem.
An emulator would be a great help in determining if it should still be accepting those downloads or explaining why it isn't. It might not be perfect for all cases but seems to me better than a shrug and "no idea".
I believe use as a diagnostic aid, while providing the ability to play around and try things out, with no risk of detrimental consequences would make it worthwhile.
Re: RP2 Bootloader Emulator ?
Ah, so you need something imaginary without knowing how it really works.
Re: RP2 Bootloader Emulator ?
How do you mean by imaginary ?
The output above from my 'rp2-emulate' app is what I have. It's just not very good in accurately replicating what the Boot ROM and 'picotool' does.
And what is it that I don't understand how it works ?
Re: RP2 Bootloader Emulator ?
The real hardware running the real BootROM, using a real Flash chip (which can behave differently).
And I said 'without knowing', not that you understand or not.
Maybe a higher level partitioning tool would be more useful and easier to implement.
If you have an aversion to SWD, can use:
And I said 'without knowing', not that you understand or not.
Maybe a higher level partitioning tool would be more useful and easier to implement.
If you have an aversion to SWD, can use:
I think E10 erasure can be mitigated by reading the previous content of the entire sector, then written back as it was.5.6. USB PICOBOOT Interface
The PICOBOOT interface is a low level USB protocol for interacting with the RP2350 while it is in BOOTSEL mode. This
interface may be used concurrently with the USB Mass Storage Interface.
It provides for flexible reading from and writing to RAM or Flash, rebooting, executing code on the device and a handful
of other management functions.
Constants and structures related to the interface can be found in the SDK header picoboot.h in the SDK
Re: RP2 Bootloader Emulator ?
At some level all Flash chips are equivalent or the RP2 won't work with them. That level will be Sector Erase, Page Write, Page Read and Read Unique ID. I don't believe it will be necessary to handle any differences below that level, at least not to start with.
Possibly but that's not Flash emulation. Flash emulation would IMO be the useful tool for verifying that such a tool had crafted a partitioning scheme and UF2 which worked with it.
The point is to be able to emulate drag-and-drop uploads and those done using 'picotool', to reveal and better understand what the outcome will be, and to do it purely with software.
Maybe but the emulator has to emulate drag-and-drop uploads, has to emulate what the Bootloader MSD interface does. It has to emulate what we have, not what it could be.
That also applies to the Bootloader USB interface. And 'picotool' and whatever it does.
This emulation isn't intended to deliver improvements directly. It is purely for revealing what happens. It might however enable the application of such techniques to be tested and verified.
Jump to
- Community
- General discussion
- Announcements
- Other languages
- Deutsch
- Español
- Français
- Italiano
- Nederlands
- 日本語
- Polski
- Português
- Русский
- Türkçe
- User groups and events
- Raspberry Pi Official Magazine
- Using the Raspberry Pi
- Beginners
- Troubleshooting
- Advanced users
- Assistive technology and accessibility
- Education
- Picademy
- Teaching and learning resources
- Staffroom, classroom and projects
- Astro Pi
- Mathematica
- High Altitude Balloon
- Weather station
- Programming
- C/C++
- Java
- Python
- Scratch
- Other programming languages
- Windows 10 for IoT
- Wolfram Language
- Bare metal, Assembly language
- Graphics programming
- OpenGLES
- OpenVG
- OpenMAX
- General programming discussion
- Projects
- Networking and servers
- Automation, sensing and robotics
- Graphics, sound and multimedia
- Other projects
- Media centres
- Gaming
- AIY Projects
- Hardware and peripherals
- Camera board
- Compute Module
- Official Display
- HATs and other add-ons
- Device Tree
- Interfacing (DSI, CSI, I2C, etc.)
- Keyboard computers (400, 500, 500+)
- Raspberry Pi Pico
- General
- SDK
- MicroPython
- Other RP2040 boards
- Zephyr
- Rust
- AI Accelerator
- AI Camera - IMX500
- Hailo
- Software
- Raspberry Pi OS
- Raspberry Pi Connect
- Raspberry Pi Desktop for PC and Mac
- Beta testing
- Other
- Android
- Debian
- FreeBSD
- Gentoo
- Linux Kernel
- NetBSD
- openSUSE
- Plan 9
- Puppy
- Arch
- Pidora / Fedora
- RISCOS
- Ubuntu
- Ye Olde Pi Shoppe
- For sale
- Wanted
- Off topic
- Off topic discussion