|
| 1 | +/** |
| 2 | + * Copyright (c) 2023 Raspberry Pi (Trading) Ltd. |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: BSD-3-Clause |
| 5 | + */ |
| 6 | +#include <assert.h> |
| 7 | +#include <stdio.h> |
| 8 | +#include "pico/stdlib.h" |
| 9 | +#include "pico/bootrom.h" |
| 10 | +#include "boot/picobin.h" |
| 11 | +#include "hardware/flash.h" |
| 12 | +#include "uf2_family_ids.h" |
| 13 | + |
| 14 | +#define PART_LOC_FIRST(x) ( ((x) & PICOBIN_PARTITION_LOCATION_FIRST_SECTOR_BITS) >> PICOBIN_PARTITION_LOCATION_FIRST_SECTOR_LSB ) |
| 15 | +#define PART_LOC_LAST(x) ( ((x) & PICOBIN_PARTITION_LOCATION_LAST_SECTOR_BITS) >> PICOBIN_PARTITION_LOCATION_LAST_SECTOR_LSB ) |
| 16 | + |
| 17 | +#define PARTITION_LOCATION_AND_FLAGS_SIZE 2 |
| 18 | +#define PARTITION_ID_SIZE 2 |
| 19 | +#define PARTITION_NAME_MAX 127 // name length is indicated by 7 bits |
| 20 | +#define PARTITION_TABLE_FIXED_INFO_SIZE (4 + PARTITION_TABLE_MAX_PARTITIONS * (PARTITION_LOCATION_AND_FLAGS_SIZE + PARTITION_ID_SIZE)) |
| 21 | + |
| 22 | +/* |
| 23 | + * Stores partition table information and data read status |
| 24 | + */ |
| 25 | +typedef struct { |
| 26 | + uint32_t table[PARTITION_TABLE_FIXED_INFO_SIZE]; |
| 27 | + uint32_t fields; |
| 28 | + bool has_partition_table; |
| 29 | + int partition_count; |
| 30 | + uint32_t unpartitioned_space_first_sector; |
| 31 | + uint32_t unpartitioned_space_last_sector; |
| 32 | + uint32_t flags_and_permissions; |
| 33 | + int current_partition; |
| 34 | + size_t pos; |
| 35 | + int status; |
| 36 | +} pico_partition_table_t; |
| 37 | + |
| 38 | +/* |
| 39 | + * Stores information on each partition |
| 40 | + */ |
| 41 | +typedef struct { |
| 42 | + uint32_t first_sector; |
| 43 | + uint32_t last_sector; |
| 44 | + uint32_t flags_and_permissions; |
| 45 | + bool has_id; |
| 46 | + uint64_t partition_id; |
| 47 | + bool has_name; |
| 48 | + char name[PARTITION_NAME_MAX + 1]; |
| 49 | + uint32_t extra_family_id_count; |
| 50 | + uint32_t extra_family_ids[PARTITION_EXTRA_FAMILY_ID_MAX]; |
| 51 | +} pico_partition_t; |
| 52 | + |
| 53 | + |
| 54 | +/* |
| 55 | + * Read the partition table information. |
| 56 | + * |
| 57 | + * See the RP2350 datasheet 5.1.2, 5.4.8.16 for flags and structures that can be specified. |
| 58 | + */ |
| 59 | +int read_partition_table(pico_partition_table_t *pt) { |
| 60 | + // Reads fixed size fields |
| 61 | + uint32_t flags = PT_INFO_PT_INFO | PT_INFO_PARTITION_LOCATION_AND_FLAGS | PT_INFO_PARTITION_ID; |
| 62 | + int rc = rom_get_partition_table_info(pt->table, sizeof(pt->table), flags); |
| 63 | + if (rc < 0) { |
| 64 | + pt->partition_count = 0; |
| 65 | + pt->status = rc; |
| 66 | + return rc; |
| 67 | + } |
| 68 | + |
| 69 | + size_t pos = 0; |
| 70 | + pt->fields = pt->table[pos++]; |
| 71 | + assert(pt->fields == flags); |
| 72 | + pt->partition_count = pt->table[pos] & 0x000000FF; |
| 73 | + pt->has_partition_table = pt->table[pos] & 0x00000100; |
| 74 | + pos++; |
| 75 | + uint32_t location = pt->table[pos++]; |
| 76 | + pt->unpartitioned_space_first_sector = PART_LOC_FIRST(location); |
| 77 | + pt->unpartitioned_space_last_sector = PART_LOC_LAST(location); |
| 78 | + pt->flags_and_permissions = pt->table[pos++]; |
| 79 | + pt->current_partition = 0; |
| 80 | + pt->pos = pos; |
| 81 | + pt->status = 0; |
| 82 | + |
| 83 | + return 0; |
| 84 | +} |
| 85 | + |
| 86 | +/* |
| 87 | + * Extract each partition information |
| 88 | + */ |
| 89 | +bool read_next_partition(pico_partition_table_t *pt, pico_partition_t *p) { |
| 90 | + if (pt->current_partition >= pt->partition_count) { |
| 91 | + return false; |
| 92 | + } |
| 93 | + |
| 94 | + size_t pos = pt->pos; |
| 95 | + uint32_t location = pt->table[pos++]; |
| 96 | + p->first_sector = PART_LOC_FIRST(location); |
| 97 | + p->last_sector = PART_LOC_LAST(location); |
| 98 | + p->flags_and_permissions = pt->table[pos++]; |
| 99 | + p->has_name = p->flags_and_permissions & PICOBIN_PARTITION_FLAGS_HAS_NAME_BITS; |
| 100 | + p->has_id = p->flags_and_permissions & PICOBIN_PARTITION_FLAGS_HAS_ID_BITS; |
| 101 | + |
| 102 | + if (p->has_id) { |
| 103 | + uint32_t id_low = pt->table[pos++]; |
| 104 | + uint32_t id_high = pt->table[pos++]; |
| 105 | + p->partition_id = ((uint64_t)id_high << 32) | id_low; |
| 106 | + } else { |
| 107 | + p->partition_id = 0; |
| 108 | + } |
| 109 | + pt->pos = pos; |
| 110 | + |
| 111 | + p->extra_family_id_count = (p->flags_and_permissions & PICOBIN_PARTITION_FLAGS_ACCEPTS_NUM_EXTRA_FAMILIES_BITS) |
| 112 | + >> PICOBIN_PARTITION_FLAGS_ACCEPTS_NUM_EXTRA_FAMILIES_LSB; |
| 113 | + if (p->extra_family_id_count | p->has_name) { |
| 114 | + // Read variable length fields |
| 115 | + uint32_t extra_family_ids_and_name[PARTITION_EXTRA_FAMILY_ID_MAX + (((PARTITION_NAME_MAX + 1) / sizeof(uint32_t)) + 1)]; |
| 116 | + uint32_t flags = PT_INFO_SINGLE_PARTITION | PT_INFO_PARTITION_FAMILY_IDS | PT_INFO_PARTITION_NAME; |
| 117 | + int rc = rom_get_partition_table_info(extra_family_ids_and_name, sizeof(extra_family_ids_and_name), |
| 118 | + (pt->current_partition << 24 | flags)); |
| 119 | + if (rc < 0) { |
| 120 | + pt->status = rc; |
| 121 | + return false; |
| 122 | + } |
| 123 | + size_t pos_ = 0; |
| 124 | + uint32_t __attribute__((unused)) fields = extra_family_ids_and_name[pos_++]; |
| 125 | + assert(fields == flags); |
| 126 | + for (size_t i = 0; i < p->extra_family_id_count; i++, pos_++) { |
| 127 | + p->extra_family_ids[i] = extra_family_ids_and_name[pos_]; |
| 128 | + } |
| 129 | + |
| 130 | + if (p->has_name) { |
| 131 | + uint8_t *name_buf = (uint8_t *)&extra_family_ids_and_name[pos_]; |
| 132 | + uint8_t name_length = *name_buf++ & 0x7F; |
| 133 | + memcpy(p->name, name_buf, name_length); |
| 134 | + p->name[name_length] = '\0'; |
| 135 | + } |
| 136 | + } |
| 137 | + if (!p->has_name) |
| 138 | + p->name[0] = '\0'; |
| 139 | + |
| 140 | + pt->current_partition++; |
| 141 | + return true; |
| 142 | +} |
| 143 | + |
| 144 | +int main() { |
| 145 | + stdio_init_all(); |
| 146 | + |
| 147 | + pico_partition_table_t pt; |
| 148 | + int rc; |
| 149 | + rc = read_partition_table(&pt); |
| 150 | + if (rc != 0) { |
| 151 | + panic("rom_get_partition_table_info returned %d", pt.status); |
| 152 | + } |
| 153 | + if (!pt.has_partition_table) { |
| 154 | + printf("there is no partition table\n"); |
| 155 | + } else if (pt.partition_count == 0) { |
| 156 | + printf("the partition table is empty\n"); |
| 157 | + } |
| 158 | + |
| 159 | + uf2_family_ids_t *family_ids = uf2_family_ids_new(pt.flags_and_permissions); |
| 160 | + char *str_family_ids = uf2_family_ids_join(family_ids, ", "); |
| 161 | + printf("un-partitioned_space: S(%s%s) NSBOOT(%s%s) NS(%s%s) uf2 { %s }\n", |
| 162 | + (pt.flags_and_permissions & PICOBIN_PARTITION_PERMISSION_S_R_BITS ? "r" : ""), |
| 163 | + (pt.flags_and_permissions & PICOBIN_PARTITION_PERMISSION_S_W_BITS ? "w" : ""), |
| 164 | + (pt.flags_and_permissions & PICOBIN_PARTITION_PERMISSION_NSBOOT_R_BITS ? "r" : ""), |
| 165 | + (pt.flags_and_permissions & PICOBIN_PARTITION_PERMISSION_NSBOOT_W_BITS ? "w" : ""), |
| 166 | + (pt.flags_and_permissions & PICOBIN_PARTITION_PERMISSION_NS_R_BITS ? "r" : ""), |
| 167 | + (pt.flags_and_permissions & PICOBIN_PARTITION_PERMISSION_NS_W_BITS ? "w" : ""), |
| 168 | + str_family_ids); |
| 169 | + free(str_family_ids); |
| 170 | + uf2_family_ids_free(family_ids); |
| 171 | + |
| 172 | + if (pt.partition_count == 0) { |
| 173 | + return 0; |
| 174 | + } |
| 175 | + printf("partitions:\n"); |
| 176 | + pico_partition_t p; |
| 177 | + while (read_next_partition(&pt, &p)) { |
| 178 | + printf("%3d:", pt.current_partition - 1); |
| 179 | + |
| 180 | + printf(" %08x->%08x S(%s%s) NSBOOT(%s%s) NS(%s%s)", |
| 181 | + p.first_sector * FLASH_SECTOR_SIZE, (p.last_sector + 1) * FLASH_SECTOR_SIZE, |
| 182 | + (p.flags_and_permissions & PICOBIN_PARTITION_PERMISSION_S_R_BITS ? "r" : ""), |
| 183 | + (p.flags_and_permissions & PICOBIN_PARTITION_PERMISSION_S_W_BITS ? "w" : ""), |
| 184 | + (p.flags_and_permissions & PICOBIN_PARTITION_PERMISSION_NSBOOT_R_BITS ? "r" : ""), |
| 185 | + (p.flags_and_permissions & PICOBIN_PARTITION_PERMISSION_NSBOOT_W_BITS ? "w" : ""), |
| 186 | + (p.flags_and_permissions & PICOBIN_PARTITION_PERMISSION_NS_R_BITS ? "r" : ""), |
| 187 | + (p.flags_and_permissions & PICOBIN_PARTITION_PERMISSION_NS_W_BITS ? "w" : "")); |
| 188 | + if (p.has_id) { |
| 189 | + printf(", id=%016llx", p.partition_id); |
| 190 | + } |
| 191 | + if (p.has_name) { |
| 192 | + printf(", \"%s\"", p.name); |
| 193 | + } |
| 194 | + |
| 195 | + // print UF2 family ID |
| 196 | + family_ids = uf2_family_ids_new(p.flags_and_permissions); |
| 197 | + for (size_t i = 0; i < p.extra_family_id_count; i++) { |
| 198 | + uf2_family_ids_add_extra_family_id(family_ids, p.extra_family_ids[i]); |
| 199 | + } |
| 200 | + str_family_ids = uf2_family_ids_join(family_ids, ", "); |
| 201 | + printf(", uf2 { %s }", str_family_ids); |
| 202 | + free(str_family_ids); |
| 203 | + uf2_family_ids_free(family_ids); |
| 204 | + |
| 205 | + printf("\n"); |
| 206 | + } |
| 207 | + if (pt.status != 0) { |
| 208 | + panic("rom_get_partition_table_info returned %d", pt.status); |
| 209 | + } |
| 210 | + |
| 211 | + return 0; |
| 212 | +} |
0 commit comments