public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
From: Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH] new tool mkenvimage: generates an env image from an arbitrary config file
Date: Fri, 5 Aug 2011 17:23:56 +0200	[thread overview]
Message-ID: <20110805172356.105c0283@skate> (raw)
In-Reply-To: <1312555798-29542-1-git-send-email-david.wagner@free-electrons.com>

Hello,

Le Fri,  5 Aug 2011 16:49:58 +0200,
David Wagner <david.wagner@free-electrons.com> a ?crit :

> This tool takes a key=value configuration file (same as would a `printenv' show)
> and generates the corresponding environnment image, ready to be flashed.

Nice tool. I'm currently using a crappy shell-based equivalent of this,
but it'd be a lot better to have a clean and nice version integrated in
U-Boot. However, see some comments below:

> +	       "\t-r : the environnment is redundand\n"

"the environment has two copies in flash" might be clearer that this
"redundand" word maybe.

> +static int make_binary_config(FILE* txt_file, unsigned char *envptr, int envsize)
> +{
> +	int i;
> +	int ret;
> +
> +	ret = fread(envptr, envsize, 1, txt_file);
> +	for (i = 0 ; i < envsize ; i++)
> +		if (envptr[i] == '\n')
> +			envptr[i] = '\0';
> +
> +	return 0;
> +}

The name of the function sounds a bit strange, and the function's job
is really small. Maybe it should just be merged into the main function.

> +int main(int argc, char **argv)
> +{
> +	uint32_t crc;
> +	char *txt_filename = NULL, *bin_filename = NULL;
> +	FILE *txt_file, *bin_file;
> +	unsigned char *dataptr, *envptr;
> +	unsigned int envsize, datasize = 0;
> +	int bigendian = 0;
> +	int redundant = 0;
> +
> +	int option;
> +	int ret = EXIT_SUCCESS;
> +
> +	opterr = 0;
> +
> +
> +	/* Parse the cmdline */
> +	while ((option = getopt(argc, argv, "s:o:rbh")) != -1)

I'd prefer to have an opening brace here, even if there is technically
a single statement inside the while() loop.

> +		switch (option)
> +		{
> +		case 's':
> +			datasize = atoi(optarg);
> +			break;

It'd be nicer if sizes could be given in decimal *or* hexadecimal
formats. 0x20000 is much easier to type than 131072.

> +		default:
> +			if (bin_filename)
> +				free(bin_filename);

Don't try to do needless cleanup, and let the operating system do it
for you. It's a short-lived program, I don't think it's worth worrying
about cleaning-up things.

> +			fprintf(stderr, "Wrong option\n");
> +			usage();
> +			return EXIT_FAILURE;
> +		}
> +
> +	if (datasize == 0) {
> +		printf("Please specify the size of the envrionnment partition.\n");

                                                       ^^^^^ typo
The message should probably be printed to stderr:

		fprintf(stderr, "blabla\n");

> +		usage();
> +		ret = EXIT_FAILURE;
> +		goto out;

just return EXIT_FAILURE, not need to clean up.

> +	}
> +	
> +
> +	txt_filename = strdup(argv[optind]);
> +	if (!txt_filename) {
> +		ret = ENOMEM;
> +		goto out;

no need to clean up.

> +	}
> +
> +	txt_file = fopen(txt_filename, "r");
> +	if (!txt_file)
> +		goto out;

You should check whether the size of the environment given in the file
doesn't exceed the size of the environment passed on the command line.

> +	/* Read the raw configuration file and transform it */
> +	dataptr = calloc(datasize, 1);
> +	if (!dataptr)
> +		goto out;

No need to clean up.

> +	envsize = datasize - (CRC_SIZE + redundant);
> +	envptr = dataptr + CRC_SIZE + redundant;
> +
> +	ret = make_binary_config(txt_file, envptr, envsize);
> +	ret = fclose(txt_file);
> +
> +	crc = crc32(0, envptr, envsize);
> +	printf("crc: 0x%08X\n", crc);

I don't think printing the CRC is useful, just drop.

> +	*((uint32_t*) dataptr) = bigendian ? htobe32(crc) : htole32(crc);

I think it'd be better to have :

struct env_normal {
	uint32_t	crc;
	char		data[0];
}

struct env_redund {
	uint32_t	crc;
	char		flags;
	char		data[0];
}

rather than this cast.

> +	bin_file = fopen(bin_filename, "w");
> +	if (fwrite(dataptr, 1, datasize, bin_file) != datasize)
> +		fprintf(stderr, "fwrite() failed: %s\n", strerror(errno));

Missing exit with error here.

> +	ret = fclose(bin_file);
> +
> +out:
> +	if (txt_filename)
> +		free(txt_filename);
> +	if (bin_filename)
> +		free(bin_filename);
> +	return ret;

No need to do useless clean up.

Regards,

Thomas
-- 
Thomas Petazzoni, Free Electrons
Kernel, drivers, real-time and embedded Linux
development, consulting, training and support.
http://free-electrons.com

  reply	other threads:[~2011-08-05 15:23 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-08-05 14:49 [U-Boot] [PATCH] new tool mkenvimage: generates an env image from an arbitrary config file David Wagner
2011-08-05 15:23 ` Thomas Petazzoni [this message]
2011-08-05 16:23 ` [U-Boot] [PATCHv2] " David Wagner
2011-08-06 11:18   ` Mike Frysinger
2011-08-08  8:16     ` David Wagner
2011-08-08  8:46       ` Albert ARIBAUD
2011-08-08  8:56         ` David Wagner
2011-08-08 18:53       ` Mike Frysinger
2011-08-09 10:31 ` [U-Boot] [PATCH] " David Wagner
2011-08-22  1:09   ` Mike Frysinger
2011-08-24 22:12   ` Wolfgang Denk
2011-08-29 12:06 ` [U-Boot] [PATCHv4] " David Wagner
2011-08-30 19:12   ` Mike Frysinger
2011-08-30 19:34   ` Wolfgang Denk
2011-09-01 15:57 ` [U-Boot] [PATCHv5] " David Wagner
2011-09-01 17:47   ` Mike Frysinger
2011-09-02  8:47 ` [U-Boot] [PATCH] " David Wagner
2011-09-02  8:48   ` David Wagner
2011-09-02  8:48 ` [U-Boot] [PATCHv6] " David Wagner
2011-09-02 14:35   ` Mike Frysinger
2011-09-26 12:10   ` Thomas Petazzoni
2011-09-26 13:26 ` [U-Boot] [PATCHv7] " David Wagner
2011-09-30  7:31 ` [U-Boot] [PATCHv8] " David Wagner
2011-10-06 21:17   ` Wolfgang Denk
2011-10-13 18:45 ` [U-Boot] [PATCHv9] " David Wagner
2011-10-14  8:21   ` Detlev Zundel
2011-10-14 17:16 ` [U-Boot] [PATCHv10] " David Wagner
2011-10-19  8:22   ` Thomas Petazzoni
2011-10-23 20:44   ` Wolfgang Denk
2011-10-31  0:49     ` Mike Frysinger
2011-11-22  8:48   ` Stefano Babic

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=20110805172356.105c0283@skate \
    --to=thomas.petazzoni@free-electrons.com \
    --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