public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
From: Stephen Warren <swarren@wwwdotorg.org>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH v4 6/7] gpt: Support for new "gpt" command
Date: Mon, 19 Nov 2012 14:34:48 -0700	[thread overview]
Message-ID: <50AAA5F8.4070207@wwwdotorg.org> (raw)
In-Reply-To: <1352458087-20462-3-git-send-email-p.wilczek@samsung.com>

On 11/09/2012 03:48 AM, Piotr Wilczek wrote:
> New command - "gpt" is supported. It restores the GPT partition table.
> It looks into the "partitions" environment variable for partitions definition.
> It can be enabled at target configuration file with CONFIG_CMD_GPT.
> Simple UUID generator has been implemented. It uses the the gd->start_addr_sp
> for entrophy pool. Moreover the pool address is used as crc32 seed.

> diff --git a/common/cmd_gpt.c b/common/cmd_gpt.c

> +U_BOOT_CMD(gpt, CONFIG_SYS_MAXARGS, 1, do_gpt,
> +	"GUID Partition Table",
> +	"<interface> <dev> <partions list>\n"
> +	" partions list is in format: name=..,size=..,uuid=..;...\n"
> +	" and can be passed as env or string ex.:\n"
> +	"    gpt mmc 0 partitions\n"

I don't think that form makes sense. The user should just pass
"${partitions}" instead. The command can't know for certain whether the
user actually intended to pass the text "partitions" and made a mistake,
or whether they passed an environment variable. If you really want to be
able to pass an environment variable name, an explicit command-line
option such as:

gpt mmc 0 name=...                      # definition on cmd-line
gpt mmc 0 --from-environment partitions # definition in environment

seems best.

> +	"    gpt mmc 0 \"name=..,size=..;name=..,size=..;...\"\n"
> +	"    gpt mmc 0 \"name=${part1_name},size=..;name=..,size=..;...\"\n"
> +	" - GUID partition table restoration\n"
> +	" Restore GPT information on a device connected\n"
> +	" to interface\n"

Is writing a GPT to a device the only thing the gpt command will ever
do. It seems best to require the user to write "gpt write mmc 0 ..."
from the very start, so that if e.g. "gpt fix-crcs" or "gpt
interactive-edit" or "gpt delete-partition 5" are implemented in the
future, existing scripts won't have to change to add the "write" parameter.

> +/**
> + * extract_env(): Convert string from '&{env_name}' to 'env_name'

s/&/$/

It's doing more than that; it locates that syntax within an arbitrary
string and ignores anything before "${" or after "}". Is that intentional?

> +static int extract_env(char *p)

> +	p1 = strstr(p, "${");
> +	p2 = strstr(p, "}");
> +
> +	if (p1 && p2) {
> +		*p2 = '\0';
> +		memmove(p, p+2, p2-p1-1);

s/-1/-2/ I think, since the length of "${" is 2 not 1.

Spaces around operators? s/p+2/p + 2/ for example.

> +/**
> + * extract_val(): Extract value from a key=value pair
> + *
> + * @param p - pointer to string

Pointer to pointer to string, given its type?

> + * @param tab - table to store extracted value
> + * @param i - actual tab element to work on

Table? Why not just pass in char **tab and get rid of "i".

> +static int extract_val(char **p, char *tab[], int i, char *key)
> +{
> +	char *t, *e, *tok = *p;
> +	char *k;

Those variable names are not exactly descriptive.

> +	t = strsep(&tok, ",");
> +	k = t;
> +	strsep(&t, "=");
> +
> +	if (key && strcmp(k, key))
> +		return -2;
> +
> +	if (extract_env(t) == 0) {

Hmm. That only allows key=${value}. What about key=text${envothertext or
key=${env1}foo${env2}? Isn't there some generic code that can already
expand environment variables within strings so we don't have to
re-invent it here?

> +	tab[i] = calloc(strlen(t) + 1, 1);
> +	if (tab[i] == NULL) {
> +		printf("%s: calloc failed!\n", __func__);
> +		return -1;
> +	}
> +	strcpy(tab[i], t);

Isn't strdup() available?

> +static int set_gpt_info(block_dev_desc_t *dev_desc, char *str_part,
> +	disk_partition_t *partitions[], const int parts_count)
> +{
> +	char *ps[parts_count];

Can we call this sizes? Can't we call strtoul() and store int sizes[]
rather than storing the strings first and then converting to integers in
a separate piece of disconnected code?

> +	printf("PARTITIONS: %s\n", s);

Why print that?

> +	ss = calloc(strlen(s) + 1, 1);
> +	if (ss == NULL) {
> +		printf("%s: calloc failed!\n", __func__);
> +		return -1;
> +	}
> +	memcpy(ss, s, strlen(s) + 1);

Use strdup().

That duplicates the strdup() in do_gpt() some of the time.

> +	for (i = 0, p = ss; i < parts_count; i++) {

Why not calculate parts_count here, rather than splitting the parsing
logic between this function and gpt_mmc_default()?

> +		tok = strsep(&p, ";");
> +		if (tok == NULL)
> +			break;
> +
> +		if (extract_val(&tok, name, i, "name")) {
> +			ret = -1;
> +			goto err;
> +		}
> +
> +		if (extract_val(&tok, ps, i, "size")) {
> +			ret = -1;
> +			free(name[i]);
> +			goto err;
> +		}

I think that requires the parameters to be passed in order
name=foo,size=5,uuid=xxx. That seems inflexible. The syntax may as well
just be value,value,value rather than key=value,key=value,key=value in
that case (although the keys are useful in order to understand the data,
so I'd prefer parsing flexibility rather than removing key=).

> +		if (extract_val(&tok, uuid, i, "uuid")) {
> +			/* uuid string length equals 37 */
> +			uuid[i] = calloc(37, 1);

Shouldn't storage for the UUID always be allocated? After all, one must
always be written even if the user didn't explicitly specify one, so
U-Boot makes it up.

> +		p = ps[i];
> +		size[i] = ustrtoul(p, &p, 0);
> +		size[i] /= dev_desc->blksz;

What if the size isn't rounded correctly?

> +	for (i = 0; i < parts_count; i++) {
> +		partitions[i]->size = size[i];
> +		partitions[i]->blksz = dev_desc->blksz;

Why not just write to partitions[] directly in the first place instead
of using temporary variables and then copying them?

> +static int gpt_mmc_default(int dev, char *str_part)

> +	struct mmc *mmc = find_mmc_device(dev);
> +
> +	if (mmc == NULL) {
> +		printf("%s: mmc dev %d NOT available\n", __func__, dev);
> +		return CMD_RET_FAILURE;
> +	}

Why is this tied to MMC; shouldn't it work for e.g. USB storage as well?
Use get_device_and_partition() instead.

> +	puts("Using default GPT UUID\n");

Even when the user explicitly supplied a partition layout on the
command-line? Why print anything at all?

> +	/* allocate memory for partitions */
> +	disk_partition_t *partitions[part_count];

Don't variable declarations have to be at the start of a block in U-Boot?

> +static int do_gpt(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
> +{
> +	int ret = CMD_RET_SUCCESS;
> +	char *str_part = NULL;
> +	int dev = 0;
> +
> +	if (argc < 3)
> +		return CMD_RET_USAGE;
> +
> +	if (argc == 4) {
> +		str_part = strdup(argv[3]);
> +		if (!str_part) {
> +			printf("%s: malloc failed!\n", __func__);
> +			return CMD_RET_FAILURE;
> +		}
> +	}

The help text doesn't indicate that any of the command parameters are
optional...

Why does this need to strdup() anything anyway?

  reply	other threads:[~2012-11-19 21:34 UTC|newest]

Thread overview: 95+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-08-24  8:13 [U-Boot] [PATCH 0/6] gpt: GUID Partition Table (GPT) restoration Lukasz Majewski
2012-08-24  8:13 ` [U-Boot] [PATCH 1/6] gpt:doc: GPT (GUID Partition Table) documentation Lukasz Majewski
2012-09-05 19:31   ` Stephen Warren
2012-09-06  9:22     ` Lukasz Majewski
2012-08-24  8:13 ` [U-Boot] [PATCH 2/6] gpt: Replace the leXX_to_int() calls with ones defined at <compiler.h> Lukasz Majewski
2012-09-05 19:35   ` Stephen Warren
2012-09-06 10:20     ` Lukasz Majewski
2012-09-06 18:53       ` Stephen Warren
     [not found]         ` <SNT002-W181FE3CE14BE68990CDF80CCB920@phx.gbl>
2012-09-12 14:42           ` Lukasz Majewski
2012-09-05 19:45   ` Stephen Warren
2012-09-06 10:15     ` Lukasz Majewski
2012-08-24  8:13 ` [U-Boot] [PATCH 3/6] gpt: Replacement of GPT structures members with ones indicating endianness and size Lukasz Majewski
2012-09-05 19:41   ` Stephen Warren
2012-09-06 10:24     ` Lukasz Majewski
2012-08-24  8:13 ` [U-Boot] [PATCH 4/6] gpt: Support for GPT (GUID Partition Table) restoration Lukasz Majewski
2012-09-05 19:49   ` Stephen Warren
2012-09-06 11:19     ` Lukasz Majewski
2012-09-06 18:55       ` Stephen Warren
2012-09-05 20:19   ` Stephen Warren
2012-10-03 23:00   ` [U-Boot] [U-Boot, " Tom Rini
2012-10-04  7:18     ` [U-Boot] [u-boot] Adding missing CONFIG_SYS_CACHELINE_SIZE to boards definitions Lukasz Majewski
2012-10-04  8:28       ` esw
2012-10-18 18:48         ` Albert ARIBAUD
2012-10-04  9:02       ` Helmut Raiger
2012-08-24  8:13 ` [U-Boot] [PATCH 5/6] gpt: Support for new "gpt" command Lukasz Majewski
2012-09-05 20:08   ` Stephen Warren
2012-09-06 14:01     ` Lukasz Majewski
2012-08-24  8:13 ` [U-Boot] [PATCH 6/6] gpt: Enable support for GPT partition table restoration at Samsung's Trats Lukasz Majewski
2012-08-24  8:48 ` [U-Boot] gpt: GUID/UUID - GPT restoration - open questions Lukasz Majewski
2012-09-05 20:21   ` Stephen Warren
2012-09-06 11:27     ` Lukasz Majewski
2012-09-06 12:27   ` Rob Herring
2012-09-06 14:14     ` Lukasz Majewski
2012-09-06 18:57       ` Stephen Warren
2012-09-07  6:45         ` Lukasz Majewski
2012-09-03  9:28 ` [U-Boot] gpt: GUID/UUID - GPT restoration - open questions - RESEND Lukasz Majewski
2012-10-02 13:46   ` Simon Glass
2012-10-02 16:39     ` Lukasz Majewski
2012-09-03  9:30 ` [U-Boot] [PATCH 0/6] gpt: GUID Partition Table (GPT) restoration Lukasz Majewski
2012-09-12 14:50 ` [U-Boot] [PATCH v2 0/7] " Lukasz Majewski
2012-09-12 14:50   ` [U-Boot] [PATCH v2 1/7] vsprintf:fix: Change type returned by ustrtoul Lukasz Majewski
2012-09-12 14:50   ` [U-Boot] [PATCH v2 2/7] part:efi: Move part_efi.h file to ./include Lukasz Majewski
2012-09-12 14:50   ` [U-Boot] [PATCH v2 3/7] gpt:doc: GPT (GUID Partition Table) documentation Lukasz Majewski
2012-09-12 14:50   ` [U-Boot] [PATCH v2 4/7] gpt: The leXX_to_int() calls replaced with ones defined at <compiler.h> Lukasz Majewski
2012-09-12 14:50   ` [U-Boot] [PATCH v2 5/7] gpt: Support for GPT (GUID Partition Table) restoration Lukasz Majewski
2012-09-12 17:22     ` Tom Rini
2012-09-12 14:50   ` [U-Boot] [PATCH v2 6/7] gpt: Support for new "gpt" command Lukasz Majewski
2012-09-12 17:21     ` Tom Rini
2012-09-12 14:50   ` [U-Boot] [PATCH v2 7/7] gpt: Enable support for GPT partition table restoration at Samsung's Trats Lukasz Majewski
2012-09-12 17:23   ` [U-Boot] [PATCH v2 0/7] gpt: GUID Partition Table (GPT) restoration Tom Rini
2012-09-13  6:20     ` Lukasz Majewski
2012-09-13 17:39       ` Tom Rini
2012-09-13  8:09 ` [U-Boot] [PATCH v3 " Lukasz Majewski
2012-09-13  8:09   ` [U-Boot] [PATCH v3 1/7] vsprintf:fix: Change type returned by ustrtoul Lukasz Majewski
2012-09-13  8:10   ` [U-Boot] [PATCH v3 2/7] part:efi: Move part_efi.h file to ./include Lukasz Majewski
2012-09-13 18:54     ` Kim Phillips
2012-09-13  8:10   ` [U-Boot] [PATCH v3 3/7] gpt:doc: GPT (GUID Partition Table) documentation Lukasz Majewski
2012-09-17 17:58     ` Stephen Warren
2012-10-05 10:35       ` Lukasz Majewski
2012-10-05 15:19         ` Lukasz Majewski
2012-10-05 16:05         ` Stephen Warren
2012-09-13  8:10   ` [U-Boot] [PATCH v3 4/7] gpt: The leXX_to_int() calls replaced with ones defined at <compiler.h> Lukasz Majewski
2012-09-13  8:10   ` [U-Boot] [PATCH v3 5/7] gpt: Support for GPT (GUID Partition Table) restoration Lukasz Majewski
2012-09-17 18:07     ` Stephen Warren
2012-10-05 10:50       ` Lukasz Majewski
2012-09-13  8:10   ` [U-Boot] [PATCH v3 6/7] gpt: Support for new "gpt" command Lukasz Majewski
2012-09-13  8:10   ` [U-Boot] [PATCH v3 7/7] gpt: Enable support for GPT partition table restoration at Samsung's Trats Lukasz Majewski
2012-11-09  9:22 ` [U-Boot] [PATCH v4 0/7] gpt: GUID Partition Table (GPT) restoration Piotr Wilczek
2012-11-09  9:22   ` [U-Boot] [PATCH v4 1/7] vsprintf:fix: Change type returned by ustrtoul Piotr Wilczek
2012-11-19 19:19     ` Stephen Warren
2012-11-09  9:22   ` [U-Boot] [PATCH v4 2/7] part:efi: Move part_efi.h file to ./include Piotr Wilczek
2012-11-19 19:30     ` Stephen Warren
2012-11-19 20:41       ` Tom Rini
2012-11-20 17:46         ` Lukasz Majewski
2012-11-09  9:22   ` [U-Boot] [PATCH v4 3/7] gpt:doc: GPT (GUID Partition Table) documentation Piotr Wilczek
2012-11-19 19:28     ` Stephen Warren
2012-11-09 10:48   ` [U-Boot] [PATCH v4 4/7] gpt: The leXX_to_int() calls replaced with ones defined at <compiler.h> Piotr Wilczek
2012-11-09 10:48     ` [U-Boot] [PATCH v4 5/7] gpt: Support for GPT (GUID Partition Table) restoration Piotr Wilczek
2012-11-19 20:16       ` Stephen Warren
2012-11-21 13:18         ` Piotr Wilczek
2012-11-22 12:16           ` Lukasz Majewski
2012-11-26 23:46             ` Stephen Warren
2012-11-24 18:05           ` Stephen Warren
2012-11-26 13:08             ` Piotr Wilczek
2012-11-09 10:48     ` [U-Boot] [PATCH v4 6/7] gpt: Support for new "gpt" command Piotr Wilczek
2012-11-19 21:34       ` Stephen Warren [this message]
2012-11-21 11:22         ` Piotr Wilczek
2012-11-24 18:00           ` Stephen Warren
2012-11-26 13:08             ` Piotr Wilczek
2012-11-26 23:49               ` Stephen Warren
2012-11-09 10:48     ` [U-Boot] [PATCH v4 7/7] gpt: Enable support for GPT partition table restoration at Samsung's Trats Piotr Wilczek
2012-11-19 19:39     ` [U-Boot] [PATCH v4 4/7] gpt: The leXX_to_int() calls replaced with ones defined at <compiler.h> Stephen Warren
2012-11-19 20:43   ` [U-Boot] [PATCH v4 0/7] gpt: GUID Partition Table (GPT) restoration Tom Rini
2012-12-06 20:40   ` Tom Rini
2012-12-07  9:03     ` Piotr Wilczek

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=50AAA5F8.4070207@wwwdotorg.org \
    --to=swarren@wwwdotorg.org \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox