From mboxrd@z Thu Jan 1 00:00:00 1970 From: Lukasz Majewski Date: Wed, 31 May 2017 16:07:12 +0200 Subject: [U-Boot] [PATCH v2 4/6] GPT: read partition table from device into a data structure In-Reply-To: <20170531094845.6f5c9ac2@jawa> References: <20170526123845.GI10782@bill-the-cat> <1496076573-14495-1-git-send-email-alison@peloton-tech.com> <1496076573-14495-5-git-send-email-alison@peloton-tech.com> <20170531094845.6f5c9ac2@jawa> Message-ID: <20170531160712.06d75a94@jawa> List-Id: MIME-Version: 1.0 Content-Type: text/plain; charset="windows-1252" Content-Transfer-Encoding: quoted-printable To: u-boot@lists.denx.de Hi Alison, > Hi Alison, >=20 > > From: Alison Chaiken > >=20 > > 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. >=20 > Acked-by: Lukasz Majewski Sorry, but I was to fast. Please fix problems pointed out by Lothar. >=20 > >=20 > > Signed-off-by: Alison Chaiken > > --- > > cmd/gpt.c | 112 > > +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ > > include/part.h | 8 +++++ 2 files changed, 120 insertions(+) > >=20 > > diff --git a/cmd/gpt.c b/cmd/gpt.c > > index 3b7d929..c61d2b1 100644 > > --- a/cmd/gpt.c > > +++ b/cmd/gpt.c > > @@ -19,6 +19,9 @@ > > #include > > #include > > #include > > +#include > > +Lothar Wa=C3=9Fmann > > +static LIST_HEAD(disk_partitions); > > =20 > > /** > > * 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; > > } > > =20 > > +static void del_gpt_info(void) > > +{ > > + struct list_head *pos =3D &disk_partitions; > > + struct disk_part *curr; > > + while (!list_empty(pos)) { > > + curr =3D 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 =3D (struct disk_part *)malloc(sizeof(*newpart)); > > + memset(newpart, '\0', sizeof(newpart)); > > + if (!newpart) > > + return ERR_PTR(-ENOMEM); > > + > > + newpart->gpt_part_info.start =3D info->start; > > + newpart->gpt_part_info.size =3D info->size; > > + newpart->gpt_part_info.blksz =3D info->blksz; > > + strncpy((char *)newpart->gpt_part_info.name, (const char > > *)info->name, PART_NAME_LEN); > > + memset(newpart->gpt_part_info.name + 31, '\0', 1); > > + strncpy((char *)newpart->gpt_part_info.type, (const char > > *)info->type, PART_TYPE_LEN); > > + memset(newpart->gpt_part_info.type + 31, '\0', 1); > > + newpart->gpt_part_info.bootable =3D info->bootable; > > +#ifdef CONFIG_PARTITION_UUIDS > > + strncpy(newpart->gpt_part_info.uuid, (const char > > *)info->uuid, > > + UUID_STR_LEN); > > +#endif > > + newpart->partnum =3D partnum; > > + > > + return newpart; > > +} > > + > > +static void print_gpt_info(void) > > +{ > > + struct list_head *pos; > > + struct disk_part *curr; > > + > > + list_for_each(pos, &disk_partitions) { > > + curr =3D 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 =3D 1, p, ret =3D 0; > > + disk_partition_t info; > > + struct disk_part *new_disk_part; > > + > > + if (disk_partitions.next =3D=3D NULL) > > + INIT_LIST_HEAD(&disk_partitions); > > + > > + for (p =3D 1; p <=3D MAX_SEARCH_PARTITIONS; p++) { > > + ret =3D part_get_info(dev_desc, p, &info); > > + if (ret) > > + continue; > > + > > + new_disk_part =3D allocate_disk_part(&info, > > valid_parts); > > + if (IS_ERR(new_disk_part) && (valid_parts >=3D 2)) > > + return -1; > > + > > + list_add_tail(&new_disk_part->list, > > &disk_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 =3D 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! > > @@ -457,6 +565,8 @@ static int do_gpt(cmd_tbl_t *cmdtp, int flag, > > int argc, char * const argv[]) if (argc =3D=3D 5) > > strcpy(varname, argv[4]); > > return do_disk_guid(blk_dev_desc, varname); > > + } else if (strcmp(argv[1], "read") =3D=3D 0) { > > + return do_get_gpt_info(blk_dev_desc); > > } else { > > return CMD_RET_USAGE; > > } > > @@ -479,6 +589,8 @@ U_BOOT_CMD(gpt, CONFIG_SYS_MAXARGS, 1, do_gpt, > > " Example usage:\n" > > " gpt write mmc 0 $partitions\n" > > " gpt verify mmc 0 $partitions\n" > > + " read \n" > > + " - read GPT into a data structure for manipulation\n" > > " guid \n" > > " - print disk GUID\n" > > " guid \n" > > diff --git a/include/part.h b/include/part.h > > index 16c4a46..7d7052a 100644 > > --- a/include/part.h > > +++ b/include/part.h > > @@ -10,6 +10,7 @@ > > #include > > #include > > #include > > +#include > > =20 > > struct block_drvr { > > char *name; > > @@ -49,6 +50,7 @@ struct block_drvr { > > =20 > > #define PART_NAME_LEN 32 > > #define PART_TYPE_LEN 32 > > +#define MAX_SEARCH_PARTITIONS 16 > > =20 > > typedef struct disk_partition { > > lbaint_t start; /* # of first block in > > partition */ @@ -68,6 +70,12 @@ typedef struct > > disk_partition { #endif > > } disk_partition_t; > > =20 > > +struct disk_part { > > + int partnum; > > + disk_partition_t gpt_part_info; > > + struct list_head list; > > +}; > > + > > /* Misc _get_dev functions */ > > #ifdef CONFIG_PARTITIONS > > /** >=20 >=20 >=20 >=20 > Best regards, >=20 > Lukasz Majewski >=20 > -- >=20 > DENX Software Engineering GmbH, Managing Director: Wolfgang Denk > HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany > Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de > _______________________________________________ > U-Boot mailing list > U-Boot at lists.denx.de > https://lists.denx.de/listinfo/u-boot Best regards, Lukasz Majewski -- DENX Software Engineering GmbH, Managing Director: Wolfgang Denk HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de