Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/rp2_common/pico_bootrom/bootrom.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ void __attribute__((noreturn)) rom_reset_usb_boot_extra(int usb_activity_gpio_pi
#if !PICO_RP2040
bool rom_get_boot_random(uint32_t out[4]) {
uint32_t result[5];
rom_get_sys_info_fn func = (rom_get_sys_info_fn) rom_func_lookup_inline(ROM_FUNC_GET_SYS_INFO);
if (5 == func(result, count_of(result), SYS_INFO_BOOT_RANDOM)) {
int words_returned = rom_get_sys_info(result, 5, SYS_INFO_BOOT_RANDOM);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think using count_of might be clearer - and then the uses in bootrom.h could be updated to count_of too

Suggested change
int words_returned = rom_get_sys_info(result, 5, SYS_INFO_BOOT_RANDOM);
int words_returned = rom_get_sys_info(result, count_of(result), SYS_INFO_BOOT_RANDOM);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Okay, I can change all of these. Is it still appropriate for count_of to continue being used in the words_returned == count_of(result) check on the next line too? This would mean that the number 5 only appears in the uint32_t result[5] line. (Which would be fine with me, I'm always happy to see the elimination of duplicated magic-numbers; I'm just double-checking that this is what's desired.)

And in that case I guess it might make sense to fold #3089 into this PR too.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think it's still appropriate - I also agree it's nice to remove the magic numbers.

Re folding the PRs, I don't mind - you could make the changes to use count_of in the existing bootrom.h functions in that PR, or fold it into this one and do it here

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Taking that idea of removing magic-numbers even further, perhaps it would also make sense to add e.g.

#define SYS_INFO_BOOT_RANDOM_WORDS_RETURNED     _u(4)

to bootrom_constants.h, and then we could do

uint32_t result[SYS_INFO_BOOT_RANDOM_WORDS_RETURNED + 1];

?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yeah, that sounds good too - for all the SYS_INFO_XXX defines having a corresponding SYS_INFO_XXX_WORDS_RETURNED rather than just having it in the comment above.

if (words_returned == count_of(result) && result[0] == SYS_INFO_BOOT_RANDOM) {
for(uint i=0;i<4;i++) {
out[i] = result[i+1];
}
Expand Down Expand Up @@ -209,4 +209,4 @@ int rom_pick_ab_partition_during_update(uint32_t *workarea_base, uint32_t workar

return rc;
}
#endif
#endif
33 changes: 22 additions & 11 deletions src/rp2_common/pico_unique_id/unique_id.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
#include "pico/bootrom.h"
#include "pico/unique_id.h"

static_assert(PICO_UNIQUE_BOARD_ID_SIZE_BYTES <= FLASH_UNIQUE_ID_SIZE_BYTES, "Board ID size must at least be the size of flash ID");
#if PICO_RP2040
static_assert(PICO_UNIQUE_BOARD_ID_SIZE_BYTES <= FLASH_UNIQUE_ID_SIZE_BYTES, "Board ID size cannot be greater than the size of flash ID");
#else
#define SYS_INFO_DEVICE_ID_SIZE_BYTES 8
static_assert(PICO_UNIQUE_BOARD_ID_SIZE_BYTES <= SYS_INFO_DEVICE_ID_SIZE_BYTES, "Board ID size cannot be greater than the size of device ID");
#endif

static pico_unique_board_id_t retrieved_id;

Expand Down Expand Up @@ -38,16 +43,22 @@ static void __attribute__((PICO_UNIQUE_BOARD_ID_INIT_ATTRIBUTES)) _retrieve_uniq
#error unique board ID size is greater than flash unique ID size
#endif
#else
rom_get_sys_info_fn func = (rom_get_sys_info_fn) rom_func_lookup(ROM_FUNC_GET_SYS_INFO);
union {
uint32_t words[9];
uint8_t bytes[9 * 4];
} out;
__unused int rc = func(out.words, 9, SYS_INFO_CHIP_INFO);
assert(rc == 4);
for (int i = 0; i < PICO_UNIQUE_BOARD_ID_SIZE_BYTES; i++) {
retrieved_id.id[i] = out.bytes[PICO_UNIQUE_BOARD_ID_SIZE_BYTES - 1 + 2 * 4 - i];
}
#if PICO_UNIQUE_BOARD_ID_SIZE_BYTES <= SYS_INFO_DEVICE_ID_SIZE_BYTES
union {
uint32_t words[4];
uint8_t bytes[4 * 4];
} out;
int words_returned = rom_get_sys_info(out.words, 4, SYS_INFO_CHIP_INFO);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ditto on count_of

Suggested change
int words_returned = rom_get_sys_info(out.words, 4, SYS_INFO_CHIP_INFO);
int words_returned = rom_get_sys_info(out.words, count_of(out.words), SYS_INFO_CHIP_INFO);

assert(words_returned == 4);
if (words_returned == count_of(out.words) && out.words[0] == SYS_INFO_CHIP_INFO) {
for (int i = 0; i < PICO_UNIQUE_BOARD_ID_SIZE_BYTES; i++) {
// The device ID is in words 3 and 4, so skip the first two words (i.e. the first 8 bytes)
retrieved_id.id[i] = out.bytes[PICO_UNIQUE_BOARD_ID_SIZE_BYTES - 1 + 2 * 4 - i];
}
}
#else
#error unique board ID size is greater than device unique ID size
#endif
#endif
}

Expand Down
Loading