public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
From: Stefano Babic <sbabic@denx.de>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH] Tools: set multiple variable with fw_setenv utility
Date: Fri, 21 May 2010 17:15:13 +0200	[thread overview]
Message-ID: <4BF6A381.7020200@denx.de> (raw)
In-Reply-To: <20100521113841.6243BCCF026@gemini.denx.de>

Wolfgang Denk wrote:

>> a config file with a list of pairs <variable, value> to be set,
>> separated by a TAB character.
> 
> I think we should be less restrictive here. Please split at the first
> white space, and allow for multiple white spaces.

There is a reason why I set to this format. There should be a case where
a variable is set with a leading (or more as one) white space. For
example, I have seen something related to board names, and adding some
spaces makes a sort of formatting without changing the code.

Because the TAB character is not allowed inside a variable, this assures
that everything except TAB is taken as part of the variables's value.

>> +	/* Allocate enough place to the data string */
>>  	for (i = 2; i < argc; ++i) {
>>  		char *val = argv[i];
>> +		if (!value) {
>> +			value = (char *)malloc(len - strlen(name));
>> +			memset(value, 0, len - strlen(name));
> 
> Kaboom! when malloc() fails.

This is an error, thanks. And I see I have not checked the return value
of malloc at all in my patch. I will fix it globally.

>>  	/* write environment back to flash */
>> -	if (flash_io (O_RDWR)) {
>> +	if (flash_io(O_RDWR)) {
>>  		fprintf (stderr, "Error: can't write fw_env to flash\n");
>>  		return -1;
> 
> We probably should not repeat this code, but factor it out.

Good idea, sure !

> 
>> +/*
>> + * Parse script to generate list of variables to set
>> + * The script file has a very simple format, as follows:
>> + *
>> + * Each line has a couple with name, value:
>> + * variable_name<TAB>variable_value
> 
> Please make this:
> 
> 	name<white space>value

See my comment above. With my proposal, something as

board_name<TAB>    my product made by my company

works flawlessy. We can add some delimiters (as ', "), but we could have
such delimiters inside the value itself and we have then to use escapes.
Because <TAB> is not allowed as character inside a variable (or I have
not yet seen this case...), it makes thing easier.

>> +	int i = 0;
>> +	char dump[128];
> 
> Ouch! That's short! Why do we need such an arbitrary limit?

We do not need a small value, but I have to set a value for fgets. A
bigger value should be enough, reporting an error if the line is too long.

> 
>> +	fp = fopen(fname, "r");
>> +	if (fp == NULL)
>> +		return -1;
> 
> How about a useful error message?

Surely.

>> +		strncpy(list[i].name, name, sizeof(list[i].name) - 1);
> 
> Error message for too long names?

Yes !

> Hm... Isn't strtok() a bit of overkill here, when all we want to do is
> split at the first white space?

Well, I have not thought it is a problem. Of course the simple way could
be to check the whole bytes inside the string, but why do you think I
should not use strtok ?

> 
>> +typedef struct {
>> +	char name[255];
>> +	char value[255];
>> +} fw_env_list;
> 
> Again we have arbitrary limits. And even different ones from the one
> above (128).

Another possibility is to dinamically allocate memory for name/value on
demand. Theoretically a value could be a very long string and the only
limit I see in u-boot is the environment size.

> 
> 
>> +static struct option long_options[] = {
>> +	{"script", required_argument, NULL, 's'},
>> +	{NULL, 0, NULL, 0}
>> +};
> 
> The command should also accept '-' as file name, and then read from
> stdin.

Ok, understood. So we can allow something like "fw_setenv -s - <
variables_file".

Regards,
Stefano

-- 
=====================================================================
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: +49-8142-66989-0 Fax: +49-8142-66989-80  Email: office@denx.de
=====================================================================

  reply	other threads:[~2010-05-21 15:15 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-05-21 10:52 [U-Boot] [PATCH] Tools: set multiple variable with fw_setenv utility Stefano Babic
2010-05-21 11:38 ` Wolfgang Denk
2010-05-21 15:15   ` Stefano Babic [this message]
2010-05-21 16:03     ` Wolfgang Denk
2010-05-21 16:39       ` Stefano Babic
2010-05-21 19:11         ` Wolfgang Denk
2010-05-23 14:29 ` [U-Boot] [PATCH V2] " Stefano Babic
2010-05-24  2:47   ` Peter Tyser
2010-05-24  8:18     ` Stefano Babic
2010-05-24 10:08   ` [U-Boot] [PATCH V3] " Stefano Babic
2010-06-29 20:43     ` Wolfgang Denk

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=4BF6A381.7020200@denx.de \
    --to=sbabic@denx.de \
    --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