Skip to content

Add Wi-Fi firmware partition support for Pico 2 W #1969

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

Open
wants to merge 20 commits into
base: develop
Choose a base branch
from

Conversation

will-v-pi
Copy link
Contributor

@will-v-pi will-v-pi commented Oct 3, 2024

This adds the ability to store and load the Wi-Fi firmware for Pico 2 W in a partition. It can be enabled by adding pico_use_wifi_firmware_partition(<exe_name>) to your CMakeLists.txt, which will embed a compatible partition table in the binary, and output two firmware UF2s to use (<exe_name>_firmware.uf2 for regular, and <exe_name>_firmware_tbyb.uf2 for TBYB). You can also create your own partition table and use that.

A Wi-Fi firmware partition is detected as having the ID 0x776966696669726d, and the UF2 family ID for Wi-Fi firmware blobs is 0xe48bff55. The default firmware partition starts at 3500K into the flash - should this be changed to depend on PICO_FLASH_SIZE_BYTES? The default firmware partition is also duplicated with A/B partitions in the same location in flash - this is required to ensure a signature check is performed before loading the Wi-Fi firmware, as there's no way to call the bootrom to check the signature of a single partition (unless chaining into it), you can only call pick_ab_partition.

The Wi-Fi firmware blob is marked in it's image_def as an RP2350 Risc-V executable, and the partition is marked as ignored_during_riscv_boot - this ensures that it can work with TBYB (as TBYB only works for executable image_defs), and that signature checks are performed before loading the firmware when Secure Boot is enabled (because signature checks are performed for all executable image_defs in a partition that is not marked ignored_during_arm_boot). This is slightly clunky, but seems to work robustly.

@lurch
Copy link
Contributor

lurch commented Oct 3, 2024

pinging @peterharperuk as he did a lot of the Wifi-related stuff for Pico 1.

@will-v-pi will-v-pi marked this pull request as ready for review October 17, 2024 09:01
@will-v-pi will-v-pi added this to the 2.0.1 milestone Oct 30, 2024
@peterharperuk
Copy link
Contributor

Works nicely. I think we need to fix the Pico W (rp2040) build errors in src/rp2_common/pico_cyw43_driver/cyw43_driver.c
It would be nice if it gave an error if you're a numpty like me and put pico_use_partition_firmware after pico_add_extra_outputs, but that could be improved later?

@will-v-pi
Copy link
Contributor Author

It would be nice if it gave an error if you're a numpty like me and put pico_use_partition_firmware after pico_add_extra_outputs, but that could be improved later?

Have added in separate PR #2054

@will-v-pi
Copy link
Contributor Author

I think we need to fix the Pico W (rp2040) build errors in src/rp2_common/pico_cyw43_driver/cyw43_driver.c

I've added a check that it's not RP2040 in the CMake function, so it'll throw a fatal error at that point if you try to build a binary with pico_use_partition_firmware for RP2040

@armandomontanez
Copy link
Contributor

armandomontanez commented Nov 19, 2024

If the Bazel checks are getting in your way, you can add the two new files to a filegroup (which will silence the error) and file an issue+leave a TODO to support this in Bazel and assign it to me.

Copy link
Contributor

@peterharperuk peterharperuk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested this and it seems good.

peterharperuk
peterharperuk previously approved these changes Nov 19, 2024
@will-v-pi
Copy link
Contributor Author

If the Bazel checks are getting in your way, you can add the two new files to a filegroup (which will silence the error) and file an issue+leave a TODO to support this in Bazel and assign it to me.

Thanks, have done - I couldn't actually assign the issue to you, but have tagged you in it

@lurch
Copy link
Contributor

lurch commented Nov 19, 2024

A Wi-Fi firmware partition is detected as having the ID 0x123456789abcdef0, and the UF2 family_is for Wi-Fi firmware blobs is 0x12345678 - these should probably both be changed to something else before merging?

Is that still going to be happening? I guess choosing randomly-generated IDs is much less likely to produce an accidental collision than a "nice" ID like 0x123456789abcdef0 ? And should those IDs be added to a header-file, to allow other UF2s to make use of this Wi-Fi firmware partition?

@will-v-pi
Copy link
Contributor Author

A Wi-Fi firmware partition is detected as having the ID 0x123456789abcdef0, and the UF2 family_is for Wi-Fi firmware blobs is 0x12345678 - these should probably both be changed to something else before merging?

Is that still going to be happening? I guess choosing randomly-generated IDs is much less likely to produce an accidental collision than a "nice" ID like 0x123456789abcdef0 ? And should those IDs be added to a header-file, to allow other UF2s to make use of this Wi-Fi firmware partition?

Yes - I was looking for ideas. We could go with 0x776966696669726d (hex for wififirm) for the partition ID, and just use the data family_id we already have?

@lurch
Copy link
Contributor

lurch commented Nov 20, 2024

Yes - I was looking for ideas. We could go with 0x776966696669726d (hex for wififirm) for the partition ID, and just use the data family_id we already have?

I've got no opinions on this so it's probably something that you and @peterharperuk ought to decide. Would MicroPython be able to use the same Wifi firmware partition as C-SDK code? (or is that another one of my stupidly naive questions? 😆 )

@richard9999999999
Copy link

I've fixed this, so you can now pass NO_EMBEDDED_PT to the function and it will not embed the partition table

Thank you, I am looking forward to see all those great changes in develop and in new SDK release later..

@kilograham kilograham modified the milestones: 2.2.0, 2.1.2 Jun 18, 2025
@kilograham
Copy link
Contributor

i had imagined that we would use a new family-id so we can target specifically (even if it is just for generic wifi firmware)

@will-v-pi
Copy link
Contributor Author

Would we want to use the next family ID for this then (0xe48bff5c), and add a define into the SDK and picotool for it (WIFI_FIRMWARE_FAMILY_ID)?

@kilograham
Copy link
Contributor

Yes, we can do that since it is well known

@kilograham
Copy link
Contributor

@peterharperuk any suggestions on naming - i assume firmware does and is always likely to include both Wifi and BT (or at least there wouldn't usually be two separates)

@peterharperuk
Copy link
Contributor

any suggestions on naming

Sorry, naming of what?

Unfortunately there are two versions of the firmware w and wb. If you use bluetooth it uses the wb version. This was just done because I didn't want to break wifi when we introduced bluetooth support - if there was an issue. Now we can be fairly confident it's ok, I think we should just use the wb firmware all the time.

@will-v-pi
Copy link
Contributor Author

Yes, this PR already only uses the wb firmware - the question was the naming of the family ID WIFI_FIRMWARE_FAMILY_ID, or something else as it also contains bluetooth firmware (eg CYW43_FIRMWARE_FAMILY_ID or something)?

@peterharperuk
Copy link
Contributor

ah right. I think CYW43_FIRMWARE_FAMILY_ID might be better - ie. have a reference to cyw in there. I wouldn't mention bluetooth

@will-v-pi
Copy link
Contributor Author

Note that this now requires a picotool PR as well (raspberrypi/picotool#247) for support of the new family ID

peterharperuk
peterharperuk previously approved these changes Jun 20, 2025
Copy link
Contributor

@peterharperuk peterharperuk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spent some time playing with this. Seems to work nicely

@lurch
Copy link
Contributor

lurch commented Jun 25, 2025

Looks like parts of your original comment in this PR need updating too? Not that important, but IMHO useful for future reference.

Remove reference to WB vs W blob

Tidy up python script and remove printout
if "#define CYW43_WIFI_FW_LEN" in line:
cyw43_wifi_fw_len = int(line.split(")")[0].split("(")[-1])
matches = re.search(r"#define\s+\S+\s+\((\S+)\)", line)
cyw43_wifi_fw_len = int(matches[1])
if "#define CYW43_CLM_LEN" in line:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this could be eilf 😉

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if "#define CYW43_CLM_LEN" in line:
elif "#define CYW43_CLM_LEN" in line:

#if CYW43_USE_FIRMWARE_PARTITION
// PICO_CONFIG: CYW43_FIRMWARE_PARTITION_ID, ID of Wi-Fi firmware partition which must match the ID used in the partition table JSON, type=int, default=0x776966696669726d, group=pico_cyw43_driver
#ifndef CYW43_FIRMWARE_PARTITION_ID
#define CYW43_FIRMWARE_PARTITION_ID 0x776966696669726d // wififirm
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the comment after the define is breaking the config check here

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we could modify the config-check to ignore comments at the end of lines; or an easier fix might be to move the comment to the line above the define? e.g.

Suggested change
#define CYW43_FIRMWARE_PARTITION_ID 0x776966696669726d // wififirm
// The default 0x776966696669726d value is the ASCII encoding of "wififirm"
#define CYW43_FIRMWARE_PARTITION_ID 0x776966696669726d

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants