Skip to content

Commit d9909f9

Browse files
committed
Added printing of extra family IDs
1 parent ce5b409 commit d9909f9

File tree

4 files changed

+145
-14
lines changed

4 files changed

+145
-14
lines changed

flash/partition_info/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
add_executable(partition_info partition_info.c)
1+
add_executable(partition_info partition_info.c uf2_family_ids.c)
22

33
target_link_libraries(partition_info PRIVATE
44
pico_stdlib

flash/partition_info/partition_info.c

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@
99
#include "pico/bootrom.h"
1010
#include "boot/picobin.h"
1111
#include "hardware/flash.h"
12+
#include "uf2_family_ids.h"
1213

1314
#define PART_LOC_FIRST(x) ( ((x) & PICOBIN_PARTITION_LOCATION_FIRST_SECTOR_BITS) >> PICOBIN_PARTITION_LOCATION_FIRST_SECTOR_LSB )
1415
#define PART_LOC_LAST(x) ( ((x) & PICOBIN_PARTITION_LOCATION_LAST_SECTOR_BITS) >> PICOBIN_PARTITION_LOCATION_LAST_SECTOR_LSB )
1516

1617
#define PARTITION_LOCATION_AND_FLAGS_SIZE 2
1718
#define PARTITION_ID_SIZE 2
1819
#define PARTITION_NAME_MAX 127
20+
#define PARTITION_EXTRA_FAMILY_ID_MAX 3
1921
#define PARTITION_TABLE_FIXED_INFO_SIZE (4 + PARTITION_TABLE_MAX_PARTITIONS * (PARTITION_LOCATION_AND_FLAGS_SIZE + PARTITION_ID_SIZE))
2022

2123
/*
@@ -43,6 +45,8 @@ typedef struct {
4345
uint32_t flags_and_permissions;
4446
uint64_t partition_id;
4547
char name[PARTITION_NAME_MAX + 1]; // name length is indicated by 7 bits
48+
uint32_t extra_family_id_count;
49+
uint32_t extra_family_ids[PARTITION_EXTRA_FAMILY_ID_MAX];
4650
} pico_partition_t;
4751

4852

@@ -101,25 +105,34 @@ bool read_next_partition(pico_partition_table_t *pt, pico_partition_t *p) {
101105
}
102106
pt->pos = pos;
103107

104-
if (p->flags_and_permissions & PICOBIN_PARTITION_FLAGS_HAS_NAME_BITS) {
108+
p->extra_family_id_count = (p->flags_and_permissions & PICOBIN_PARTITION_FLAGS_ACCEPTS_NUM_EXTRA_FAMILIES_BITS)
109+
>> PICOBIN_PARTITION_FLAGS_ACCEPTS_NUM_EXTRA_FAMILIES_LSB;
110+
if (p->extra_family_id_count | (p->flags_and_permissions & PICOBIN_PARTITION_FLAGS_HAS_NAME_BITS)) {
105111
// Read variable length fields
106-
uint32_t name_buf[((PARTITION_NAME_MAX + 1) / sizeof(uint32_t)) + 1] = {0};
107-
uint32_t flags = PT_INFO_SINGLE_PARTITION | PT_INFO_PARTITION_NAME;
108-
int rc = rom_get_partition_table_info(name_buf, sizeof(name_buf),
112+
uint32_t extra_family_id_and_name[PARTITION_EXTRA_FAMILY_ID_MAX + (((PARTITION_NAME_MAX + 1) / sizeof(uint32_t)) + 1)];
113+
uint32_t flags = PT_INFO_SINGLE_PARTITION | PT_INFO_PARTITION_FAMILY_IDS | PT_INFO_PARTITION_NAME;
114+
int rc = rom_get_partition_table_info(extra_family_id_and_name, sizeof(extra_family_id_and_name),
109115
(pt->current_partition << 24 | flags));
110116
if (rc < 0) {
111117
pt->status = rc;
112118
return false;
113119
}
114-
uint32_t __attribute__((unused)) fields = name_buf[0];
120+
size_t pos = 0;
121+
uint32_t __attribute__((unused)) fields = extra_family_id_and_name[pos++];
115122
assert(fields == flags);
116-
uint8_t *name_buf_u8 = (uint8_t *)&name_buf[1];
117-
uint8_t name_length = *name_buf_u8++ & 0x7F;
118-
memcpy(p->name, name_buf_u8, name_length);
119-
p->name[name_length] = '\0';
120-
} else {
121-
p->name[0] = '\0';
123+
for (size_t i = 0; i < p->extra_family_id_count; i++, pos++) {
124+
p->extra_family_ids[i] = extra_family_id_and_name[pos];
125+
}
126+
127+
if (p->flags_and_permissions & PICOBIN_PARTITION_FLAGS_HAS_NAME_BITS) {
128+
uint8_t *name_buf = (uint8_t *)&extra_family_id_and_name[pos];
129+
uint8_t name_length = *name_buf++ & 0x7F;
130+
memcpy(p->name, name_buf, name_length);
131+
p->name[name_length] = '\0';
132+
}
122133
}
134+
if (!(p->flags_and_permissions & PICOBIN_PARTITION_FLAGS_HAS_NAME_BITS))
135+
p->name[0] = '\0';
123136

124137
pt->current_partition++;
125138
return true;
@@ -139,13 +152,20 @@ int main() {
139152
} else if (pt.partition_count == 0) {
140153
printf("the partition table is empty\n");
141154
}
142-
printf("un-partitioned_space: S(%s%s) NSBOOT(%s%s) NS(%s%s)\n",
155+
156+
uf2_family_ids_t *family_ids = uf2_family_ids_new();
157+
uf2_family_ids_add_default_families(family_ids, pt.flags_and_permissions);
158+
char *str_family_ids = uf2_family_ids_join(family_ids, ", ");
159+
printf("un-partitioned_space: S(%s%s) NSBOOT(%s%s) NS(%s%s) uf2 { %s }\n",
143160
(pt.flags_and_permissions & PICOBIN_PARTITION_PERMISSION_S_R_BITS ? "r" : ""),
144161
(pt.flags_and_permissions & PICOBIN_PARTITION_PERMISSION_S_W_BITS ? "w" : ""),
145162
(pt.flags_and_permissions & PICOBIN_PARTITION_PERMISSION_NSBOOT_R_BITS ? "r" : ""),
146163
(pt.flags_and_permissions & PICOBIN_PARTITION_PERMISSION_NSBOOT_W_BITS ? "w" : ""),
147164
(pt.flags_and_permissions & PICOBIN_PARTITION_PERMISSION_NS_R_BITS ? "r" : ""),
148-
(pt.flags_and_permissions & PICOBIN_PARTITION_PERMISSION_NS_W_BITS ? "w" : ""));
165+
(pt.flags_and_permissions & PICOBIN_PARTITION_PERMISSION_NS_W_BITS ? "w" : ""),
166+
str_family_ids);
167+
free(str_family_ids);
168+
uf2_family_ids_free(family_ids);
149169

150170
if (pt.partition_count == 0) {
151171
return 0;
@@ -154,6 +174,7 @@ int main() {
154174
pico_partition_t p;
155175
while (read_next_partition(&pt, &p)) {
156176
printf("%3d:", pt.current_partition - 1);
177+
157178
printf(" %08x->%08x S(%s%s) NSBOOT(%s%s) NS(%s%s)",
158179
p.first_sector * FLASH_SECTOR_SIZE, (p.last_sector + 1) * FLASH_SECTOR_SIZE,
159180
(p.flags_and_permissions & PICOBIN_PARTITION_PERMISSION_S_R_BITS ? "r" : ""),
@@ -168,6 +189,18 @@ int main() {
168189
if (p.flags_and_permissions & PICOBIN_PARTITION_FLAGS_HAS_NAME_BITS) {
169190
printf(", \"%s\"", p.name);
170191
}
192+
193+
// print UF2 family ID
194+
family_ids = uf2_family_ids_new();
195+
uf2_family_ids_add_default_families(family_ids, p.flags_and_permissions);
196+
for (size_t i = 0; i < p.extra_family_id_count; i++) {
197+
uf2_family_ids_add_extra_family_id(family_ids, p.extra_family_ids[i]);
198+
}
199+
str_family_ids = uf2_family_ids_join(family_ids, ", ");
200+
printf(", uf2 { %s }", str_family_ids);
201+
uf2_family_ids_free(family_ids);
202+
free(str_family_ids);
203+
171204
printf("\n");
172205
}
173206
if (pt.status != 0) {
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#include "uf2_family_ids.h"
2+
3+
#define UF2_FAMILY_ID_HEX_SIZE (2 + 8 * 2 + 1)
4+
5+
uf2_family_ids_t *uf2_family_ids_new(void) {
6+
uf2_family_ids_t *ids = malloc(sizeof(uf2_family_ids_t));
7+
ids->count = 0;
8+
ids->items = NULL;
9+
return ids;
10+
}
11+
12+
void uf2_family_ids_add(uf2_family_ids_t *ids, const char *str) {
13+
ids->items = realloc(ids->items, (ids->count + 1) * sizeof(char *));
14+
ids->items[ids->count] = strdup(str);
15+
if (ids->items[ids->count] == NULL) {
16+
perror("strdup");
17+
return;
18+
}
19+
ids->count++;
20+
}
21+
22+
char *uf2_family_ids_join(const uf2_family_ids_t *ids, const char *sep) {
23+
size_t total_length = 0;
24+
size_t sep_length = strlen(sep);
25+
26+
for (size_t i = 0; i < ids->count; i++) {
27+
total_length += strlen(ids->items[i]);
28+
if (i < ids->count - 1)
29+
total_length += sep_length;
30+
}
31+
32+
char *result = calloc(1, total_length + 1);
33+
if (!result) {
34+
perror("calloc");
35+
return NULL;
36+
}
37+
38+
result[0] = '\0';
39+
for (size_t i = 0; i < ids->count; i++) {
40+
strcat(result, ids->items[i]);
41+
if (i < ids->count - 1)
42+
strcat(result, sep);
43+
}
44+
45+
return result;
46+
}
47+
48+
void uf2_family_ids_free(uf2_family_ids_t *ids) {
49+
for (size_t i = 0; i < ids->count; i++) {
50+
free(ids->items[i]);
51+
}
52+
free(ids->items);
53+
free(ids);
54+
}
55+
56+
void uf2_family_ids_add_default_families(uf2_family_ids_t *ids, uint32_t flags) {
57+
if (flags & PICOBIN_PARTITION_FLAGS_ACCEPTS_DEFAULT_FAMILY_ABSOLUTE_BITS)
58+
uf2_family_ids_add(ids, "absolute");
59+
if (flags & PICOBIN_PARTITION_FLAGS_ACCEPTS_DEFAULT_FAMILY_RP2040_BITS)
60+
uf2_family_ids_add(ids, "rp2040");
61+
if (flags & PICOBIN_PARTITION_FLAGS_ACCEPTS_DEFAULT_FAMILY_RP2350_ARM_S_BITS)
62+
uf2_family_ids_add(ids, "rp2350-arm-s");
63+
if (flags & PICOBIN_PARTITION_FLAGS_ACCEPTS_DEFAULT_FAMILY_RP2350_ARM_NS_BITS)
64+
uf2_family_ids_add(ids, "rp2350-arm-ns");
65+
if (flags & PICOBIN_PARTITION_FLAGS_ACCEPTS_DEFAULT_FAMILY_RP2350_RISCV_BITS)
66+
uf2_family_ids_add(ids, "rp2350-riscv");
67+
if (flags & PICOBIN_PARTITION_FLAGS_ACCEPTS_DEFAULT_FAMILY_DATA_BITS)
68+
uf2_family_ids_add(ids, "data");
69+
}
70+
71+
void uf2_family_ids_add_extra_family_id(uf2_family_ids_t *ids, uint32_t family_id) {
72+
char hex_id[UF2_FAMILY_ID_HEX_SIZE];
73+
sprintf(hex_id, "0x%08x", family_id);
74+
uf2_family_ids_add(ids, hex_id);
75+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#pragma once
2+
3+
#include <stdio.h>
4+
#include <stdlib.h>
5+
#include <string.h>
6+
7+
#include "pico/stdlib.h"
8+
#include "boot/picobin.h"
9+
10+
11+
typedef struct {
12+
size_t count;
13+
char **items;
14+
} uf2_family_ids_t;
15+
16+
17+
uf2_family_ids_t *uf2_family_ids_new(void);
18+
void uf2_family_ids_add(uf2_family_ids_t *ids, const char *str);
19+
char *uf2_family_ids_join(const uf2_family_ids_t *ids, const char *sep);
20+
void uf2_family_ids_free(uf2_family_ids_t *ids);
21+
22+
void uf2_family_ids_add_default_families(uf2_family_ids_t *ids, uint32_t flags);
23+
void uf2_family_ids_add_extra_family_id(uf2_family_ids_t *ids, uint32_t family_id);

0 commit comments

Comments
 (0)