Re: [U-Boot] [PATCH v3 3/5] GPT: read partition table from device into a data structure
Hi, On Fri, 2 Jun 2017 19:22:32 -0700 ali...@peloton-tech.com wrote: > From: Alison Chaiken> > Make the partition table available for modification by reading it from > the user-specified device into a linked list. Provide an accessor > function for command-line testing. > > Signed-off-by: Alison Chaiken > --- > cmd/gpt.c | 112 > + > include/part.h | 7 > 2 files changed, 119 insertions(+) > > diff --git a/cmd/gpt.c b/cmd/gpt.c > index 4d00a35..5c2651f 100644 > --- a/cmd/gpt.c > +++ b/cmd/gpt.c > @@ -19,6 +19,9 @@ > #include > #include > #include > +#include > + > +static LIST_HEAD(disk_partitions); > > /** > * extract_env(): Expand env name from string format '&{env_name}' > @@ -151,6 +154,111 @@ static bool found_key(const char *str, const char *key) > return result; > } > > +static void del_gpt_info(void) > +{ > + struct list_head *pos = _partitions; > + struct disk_part *curr; > + while (!list_empty(pos)) { > + curr = list_entry(pos->next, struct disk_part, list); > + list_del(pos->next); > + free(curr); > + } > +} > + > +static struct disk_part *allocate_disk_part(disk_partition_t *info, int > partnum) > +{ > + struct disk_part *newpart; > + newpart = (struct disk_part *)malloc(sizeof(*newpart)); > + if (!newpart) > + return ERR_PTR(-ENOMEM); > + memset(newpart, '\0', sizeof(newpart)); > + > + newpart->gpt_part_info.start = info->start; > + newpart->gpt_part_info.size = info->size; > + newpart->gpt_part_info.blksz = info->blksz; > + strncpy((char *)newpart->gpt_part_info.name, (const char *)info->name, > PART_NAME_LEN); > + newpart->gpt_part_info.name[PART_NAME_LEN - 1] = '\0'; > + strncpy((char *)newpart->gpt_part_info.type, (const char *)info->type, > PART_TYPE_LEN); > + newpart->gpt_part_info.type[PART_TYPE_LEN - 1] = '\0'; > + newpart->gpt_part_info.bootable = info->bootable; > +#ifdef CONFIG_PARTITION_UUIDS > + strncpy(newpart->gpt_part_info.uuid, (const char *)info->uuid, > + UUID_STR_LEN); > +#endif > + newpart->partnum = partnum; > + > + return newpart; > +} > + > +static void print_gpt_info(void) > +{ > + struct list_head *pos; > + struct disk_part *curr; > + > + list_for_each(pos, _partitions) { > + curr = list_entry(pos, struct disk_part, list); > + printf("Partition %d:\n", curr->partnum); > + printf("1st block %x, size %x\n", > (unsigned)curr->gpt_part_info.start, > +(unsigned)curr->gpt_part_info.size); > + printf("Block size %lu, name %s\n", curr->gpt_part_info.blksz, > +curr->gpt_part_info.name); > + printf("Type %s, bootable %d\n", curr->gpt_part_info.type, > +curr->gpt_part_info.bootable); > +#ifdef CONFIG_PARTITION_UUIDS > + printf("UUID %s\n", curr->gpt_part_info.uuid); > +#endif > + printf("\n"); > + } > +} > + > +/* > + * read partition info into disk_partitions list where > + * it can be printed or modified > + */ > +static int get_gpt_info(struct blk_desc *dev_desc) > +{ > + /* start partition numbering at 1, as U-Boot does */ > + int valid_parts = 1, p, ret; > + disk_partition_t info; > + struct disk_part *new_disk_part; > + > + if (disk_partitions.next == NULL) > + INIT_LIST_HEAD(_partitions); > + > + for (p = 1; p <= MAX_SEARCH_PARTITIONS; p++) { > + ret = part_get_info(dev_desc, p, ); > + if (ret) > + continue; > + > + new_disk_part = allocate_disk_part(, valid_parts); > + if (IS_ERR(new_disk_part) && valid_parts >= 2) > + return -ENODEV; > + > + list_add_tail(_disk_part->list, _partitions); > + valid_parts++; > + } > + if (!valid_parts) { > + printf("** No valid partitions found **\n"); > + del_gpt_info(); > + return -ENODEV; > + } > + return --valid_parts; > +} > + > +/* a wrapper to test get_gpt_info */ > +static int do_get_gpt_info(struct blk_desc *dev_desc) > +{ > + int ret; > + > + ret = get_gpt_info(dev_desc); > + if (ret > 0) { > + print_gpt_info(); > + del_gpt_info(); > + } > + return ret; > +} > + > + > /** > * set_gpt_info(): Fill partition information from string > * function allocates memory, remember to free! > @@ -455,6 +563,8 @@ static int do_gpt(cmd_tbl_t *cmdtp, int flag, int argc, > char * const argv[]) > printf("Verify GPT: "); > } else if (strcmp(argv[1], "guid") == 0) { > return do_disk_guid(blk_dev_desc, argv[4]); > + } else if (strcmp(argv[1], "read") == 0) { > + return do_get_gpt_info(blk_dev_desc); >
Re: [U-Boot] [PATCH v3 3/5] GPT: read partition table from device into a data structure
Hi, On Fri, 2 Jun 2017 19:22:32 -0700 ali...@peloton-tech.com wrote: > From: Alison Chaiken> > Make the partition table available for modification by reading it from > the user-specified device into a linked list. Provide an accessor > function for command-line testing. > > Signed-off-by: Alison Chaiken > --- > cmd/gpt.c | 112 > + > include/part.h | 7 > 2 files changed, 119 insertions(+) > > diff --git a/cmd/gpt.c b/cmd/gpt.c > index 4d00a35..5c2651f 100644 > --- a/cmd/gpt.c > +++ b/cmd/gpt.c > @@ -19,6 +19,9 @@ > #include > #include > #include > +#include > + > +static LIST_HEAD(disk_partitions); > > /** > * extract_env(): Expand env name from string format '&{env_name}' > @@ -151,6 +154,111 @@ static bool found_key(const char *str, const char *key) > return result; > } > > +static void del_gpt_info(void) > +{ > + struct list_head *pos = _partitions; > + struct disk_part *curr; > + while (!list_empty(pos)) { > + curr = list_entry(pos->next, struct disk_part, list); > + list_del(pos->next); > + free(curr); > + } > +} > + > +static struct disk_part *allocate_disk_part(disk_partition_t *info, int > partnum) > +{ > + struct disk_part *newpart; > + newpart = (struct disk_part *)malloc(sizeof(*newpart)); > + if (!newpart) > + return ERR_PTR(-ENOMEM); > + memset(newpart, '\0', sizeof(newpart)); > + > + newpart->gpt_part_info.start = info->start; > + newpart->gpt_part_info.size = info->size; > + newpart->gpt_part_info.blksz = info->blksz; > + strncpy((char *)newpart->gpt_part_info.name, (const char *)info->name, > PART_NAME_LEN); > + newpart->gpt_part_info.name[PART_NAME_LEN - 1] = '\0'; > + strncpy((char *)newpart->gpt_part_info.type, (const char *)info->type, > PART_TYPE_LEN); > + newpart->gpt_part_info.type[PART_TYPE_LEN - 1] = '\0'; > + newpart->gpt_part_info.bootable = info->bootable; > +#ifdef CONFIG_PARTITION_UUIDS > + strncpy(newpart->gpt_part_info.uuid, (const char *)info->uuid, > + UUID_STR_LEN); > +#endif > + newpart->partnum = partnum; > + > + return newpart; > +} > + > +static void print_gpt_info(void) > +{ > + struct list_head *pos; > + struct disk_part *curr; > + > + list_for_each(pos, _partitions) { > + curr = list_entry(pos, struct disk_part, list); > + printf("Partition %d:\n", curr->partnum); > + printf("1st block %x, size %x\n", > (unsigned)curr->gpt_part_info.start, > +(unsigned)curr->gpt_part_info.size); > + printf("Block size %lu, name %s\n", curr->gpt_part_info.blksz, > +curr->gpt_part_info.name); > + printf("Type %s, bootable %d\n", curr->gpt_part_info.type, > +curr->gpt_part_info.bootable); > +#ifdef CONFIG_PARTITION_UUIDS > + printf("UUID %s\n", curr->gpt_part_info.uuid); > +#endif > + printf("\n"); > + } > +} > + > +/* > + * read partition info into disk_partitions list where > + * it can be printed or modified > + */ > +static int get_gpt_info(struct blk_desc *dev_desc) > +{ > + /* start partition numbering at 1, as U-Boot does */ > + int valid_parts = 1, p, ret; > + disk_partition_t info; > + struct disk_part *new_disk_part; > + > + if (disk_partitions.next == NULL) > + INIT_LIST_HEAD(_partitions); > + > + for (p = 1; p <= MAX_SEARCH_PARTITIONS; p++) { > + ret = part_get_info(dev_desc, p, ); > + if (ret) > + continue; > + > + new_disk_part = allocate_disk_part(, valid_parts); > + if (IS_ERR(new_disk_part) && valid_parts >= 2) > + return -ENODEV; > del_gpt_info()? This calls for a common error exit which does the cleanup. > + > + list_add_tail(_disk_part->list, _partitions); > + valid_parts++; > + } > + if (!valid_parts) { > + printf("** No valid partitions found **\n"); > + del_gpt_info(); > + return -ENODEV; > + } > + return --valid_parts; > +} > + Lothar Waßmann ___ U-Boot mailing list U-Boot@lists.denx.de https://lists.denx.de/listinfo/u-boot
[U-Boot] [PATCH v3 3/5] GPT: read partition table from device into a data structure
From: Alison ChaikenMake the partition table available for modification by reading it from the user-specified device into a linked list. Provide an accessor function for command-line testing. Signed-off-by: Alison Chaiken --- cmd/gpt.c | 112 + include/part.h | 7 2 files changed, 119 insertions(+) diff --git a/cmd/gpt.c b/cmd/gpt.c index 4d00a35..5c2651f 100644 --- a/cmd/gpt.c +++ b/cmd/gpt.c @@ -19,6 +19,9 @@ #include #include #include +#include + +static LIST_HEAD(disk_partitions); /** * extract_env(): Expand env name from string format '&{env_name}' @@ -151,6 +154,111 @@ static bool found_key(const char *str, const char *key) return result; } +static void del_gpt_info(void) +{ + struct list_head *pos = _partitions; + struct disk_part *curr; + while (!list_empty(pos)) { + curr = list_entry(pos->next, struct disk_part, list); + list_del(pos->next); + free(curr); + } +} + +static struct disk_part *allocate_disk_part(disk_partition_t *info, int partnum) +{ + struct disk_part *newpart; + newpart = (struct disk_part *)malloc(sizeof(*newpart)); + if (!newpart) + return ERR_PTR(-ENOMEM); + memset(newpart, '\0', sizeof(newpart)); + + newpart->gpt_part_info.start = info->start; + newpart->gpt_part_info.size = info->size; + newpart->gpt_part_info.blksz = info->blksz; + strncpy((char *)newpart->gpt_part_info.name, (const char *)info->name, PART_NAME_LEN); + newpart->gpt_part_info.name[PART_NAME_LEN - 1] = '\0'; + strncpy((char *)newpart->gpt_part_info.type, (const char *)info->type, PART_TYPE_LEN); + newpart->gpt_part_info.type[PART_TYPE_LEN - 1] = '\0'; + newpart->gpt_part_info.bootable = info->bootable; +#ifdef CONFIG_PARTITION_UUIDS + strncpy(newpart->gpt_part_info.uuid, (const char *)info->uuid, + UUID_STR_LEN); +#endif + newpart->partnum = partnum; + + return newpart; +} + +static void print_gpt_info(void) +{ + struct list_head *pos; + struct disk_part *curr; + + list_for_each(pos, _partitions) { + curr = list_entry(pos, struct disk_part, list); + printf("Partition %d:\n", curr->partnum); + printf("1st block %x, size %x\n", (unsigned)curr->gpt_part_info.start, + (unsigned)curr->gpt_part_info.size); + printf("Block size %lu, name %s\n", curr->gpt_part_info.blksz, + curr->gpt_part_info.name); + printf("Type %s, bootable %d\n", curr->gpt_part_info.type, + curr->gpt_part_info.bootable); +#ifdef CONFIG_PARTITION_UUIDS + printf("UUID %s\n", curr->gpt_part_info.uuid); +#endif + printf("\n"); + } +} + +/* + * read partition info into disk_partitions list where + * it can be printed or modified + */ +static int get_gpt_info(struct blk_desc *dev_desc) +{ + /* start partition numbering at 1, as U-Boot does */ + int valid_parts = 1, p, ret; + disk_partition_t info; + struct disk_part *new_disk_part; + + if (disk_partitions.next == NULL) + INIT_LIST_HEAD(_partitions); + + for (p = 1; p <= MAX_SEARCH_PARTITIONS; p++) { + ret = part_get_info(dev_desc, p, ); + if (ret) + continue; + + new_disk_part = allocate_disk_part(, valid_parts); + if (IS_ERR(new_disk_part) && valid_parts >= 2) + return -ENODEV; + + list_add_tail(_disk_part->list, _partitions); + valid_parts++; + } + if (!valid_parts) { + printf("** No valid partitions found **\n"); + del_gpt_info(); + return -ENODEV; + } + return --valid_parts; +} + +/* a wrapper to test get_gpt_info */ +static int do_get_gpt_info(struct blk_desc *dev_desc) +{ + int ret; + + ret = get_gpt_info(dev_desc); + if (ret > 0) { + print_gpt_info(); + del_gpt_info(); + } + return ret; +} + + /** * set_gpt_info(): Fill partition information from string * function allocates memory, remember to free! @@ -455,6 +563,8 @@ static int do_gpt(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) printf("Verify GPT: "); } else if (strcmp(argv[1], "guid") == 0) { return do_disk_guid(blk_dev_desc, argv[4]); + } else if (strcmp(argv[1], "read") == 0) { + return do_get_gpt_info(blk_dev_desc); } else { return CMD_RET_USAGE; } @@ -477,6 +587,8 @@ U_BOOT_CMD(gpt, CONFIG_SYS_MAXARGS, 1, do_gpt, " Example usage:\n" " gpt write mmc 0 $partitions\n" " gpt verify mmc 0