linuxppc-dev.lists.ozlabs.org archive mirror
 help / color / mirror / Atom feed
From: David Gibson <david@gibson.dropbear.id.au>
To: Scott Wood <scottwood@freescale.com>
Cc: linuxppc-dev@ozlabs.org, jdl@jdl.com
Subject: Re: [PATCH] Add support for binary includes.
Date: Tue, 26 Feb 2008 11:39:55 +1100	[thread overview]
Message-ID: <20080226003955.GA24382@localhost.localdomain> (raw)
In-Reply-To: <20080220191941.GA2062@ld0162-tx32.am.freescale.net>

On Wed, Feb 20, 2008 at 01:19:41PM -0600, Scott Wood wrote:
> A property's data can be populated with a file's contents
> as follows:
> 
> node {
> 	prop = /incbin/("path/to/data");
> };
> 
> A subset of a file can be included by passing start and size parameters.
> For example, to include bytes 8 through 23:
> 
> node {
> 	prop = /incbin/("path/to/data", 8, 16);
> };
> 
> As with /include/, non-absolute paths are looked for in the directory
> of the source file that includes them.

Well, while I discuss the syntax with Jon, here's some comments on the
implementation.


> diff --git a/Makefile b/Makefile
> index 8a47c34..d4d935c 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -16,7 +16,7 @@ LOCAL_VERSION =
>  CONFIG_LOCALVERSION =
>  
>  CPPFLAGS = -I libfdt
> -CFLAGS = -Wall -g -Os
> +CFLAGS = -Wall -g -Os -D_FILE_OFFSET_BITS=64

Is this portable?

>  
>  BISON = bison
>  LEX = flex
> diff --git a/data.c b/data.c
> index a94718c..f9464bf 100644
> --- a/data.c
> +++ b/data.c
> @@ -19,6 +19,7 @@
>   */
>  
>  #include "dtc.h"
> +#include "srcpos.h"
>  
>  void data_free(struct data d)
>  {
> @@ -189,7 +190,41 @@ struct data data_copy_file(FILE *f, size_t len)
>  	d = data_grow_for(empty_data, len);
>  
>  	d.len = len;
> -	fread(d.val, len, 1, f);
> +	if (fread(d.val, len, 1, f) != 1) {
> +		yyerrorf("Couldn't read %zu bytes from file: %s",
> +		         len, feof(f) ? "end-of-file" : strerror(errno));
> +		return empty_data;

Ugh.
	1) Error messages direct to the user really don't belong in
data.c's low-level support routines.  This does mean we might need to
substantially change this function so there's some other way of
reporting the error to the caller who can report it.
	2) The horrid name 'yyerror()' exists only to match what bison
expects.  It should never be used outside the parse code (we probably
should create more generally usable error/warning functions, though).
	3) Is %zu portable?

> +	}
> +
> +	return d;
> +}
> +
> +struct data data_copy_file_all(FILE *f)

It should be possible to combine these two functions.

> +{
> +	char buf[4096];
> +	struct data d = empty_data;
> +
> +	while (1) {
> +		size_t ret = fread(buf, 1, sizeof(buf), f);
> +		if (ret == 0) {
> +			if (!feof(f))
> +				yyerrorf("Error reading file: %s", strerror(errno));
> +
> +			break;
> +		}
> +
> +		assert(ret <= sizeof(buf));
> +
> +		d = data_grow_for(d, ret);
> +		memcpy(d.val + d.len, buf, ret);
> +
> +		if (d.len + ret < d.len) {
> +			yyerror("Binary include too large");

Hrm.  A test which will cover this case should go into
data_grow_for(), I think.

-- 
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson

  parent reply	other threads:[~2008-02-26  0:39 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-02-20 19:19 [PATCH] Add support for binary includes Scott Wood
2008-02-22  5:34 ` David Gibson
2008-02-22 18:12   ` Jon Loeliger
2008-02-25  3:38     ` David Gibson
2008-02-22  6:05 ` Grant Likely
2008-02-22  8:34   ` Bartlomiej Sieka
2008-02-22  9:02   ` David Woodhouse
2008-02-22 15:50     ` Grant Likely
2008-02-22 16:08   ` Scott Wood
2008-02-22 16:24     ` Jon Loeliger
2008-02-22 16:28       ` Scott Wood
2008-02-26  0:39 ` David Gibson [this message]
2008-02-26 17:26   ` Scott Wood
2008-05-27 15:27   ` Kumar Gala
2008-05-27 17:59     ` Jon Loeliger
2008-05-28 23:58       ` David Gibson
2008-05-29  0:02         ` David Gibson
2008-05-30 18:54           ` Scott Wood
2008-06-04  4:13             ` David Gibson
2008-06-04 12:36               ` Bartlomiej Sieka
2008-06-04 14:26               ` Jon Loeliger
2008-06-05  3:11                 ` Josh Boyer
2008-06-11  1:58                 ` dtc: " David Gibson
2008-06-12 16:43                   ` Scott Wood
2008-06-13  0:01                     ` David Gibson
2008-06-13  0:46                       ` Jon Loeliger
2008-06-13  3:41                         ` David Gibson
2008-05-29 14:04         ` [PATCH] " Jon Loeliger
2008-05-30  1:34           ` David Gibson
2008-06-02 20:22             ` Jon Loeliger

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=20080226003955.GA24382@localhost.localdomain \
    --to=david@gibson.dropbear.id.au \
    --cc=jdl@jdl.com \
    --cc=linuxppc-dev@ozlabs.org \
    --cc=scottwood@freescale.com \
    /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;
as well as URLs for NNTP newsgroup(s).