All of lore.kernel.org
 help / color / mirror / Atom feed
From: Simon Glass <sjg-F7+t8E8rja9g9hUCZPvPmw@public.gmane.org>
To: Devicetree Discuss
	<devicetree-discuss-uLR06cmDAlY/bJ5BZ2RsiQ@public.gmane.org>
Subject: [PATCH v2 1/6] Add utilfdt for common functions
Date: Wed,  7 Sep 2011 12:54:15 -0700	[thread overview]
Message-ID: <1315425260-2711-2-git-send-email-sjg@chromium.org> (raw)
In-Reply-To: <1315425260-2711-1-git-send-email-sjg-F7+t8E8rja9g9hUCZPvPmw@public.gmane.org>

This adds a new utility library for performing libfdt operations.

Signed-off-by: Simon Glass <sjg-F7+t8E8rja9g9hUCZPvPmw@public.gmane.org>
---
Changes in v2:
- Remove util_decode_key
- Add utilfdt_decode_type to be used by fdtget/put
- Remove limits on device tree binary size

 utilfdt.c |  120 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 utilfdt.h |   58 +++++++++++++++++++++++++++++
 2 files changed, 178 insertions(+), 0 deletions(-)
 create mode 100644 utilfdt.c
 create mode 100644 utilfdt.h

diff --git a/utilfdt.c b/utilfdt.c
new file mode 100644
index 0000000..5c3682e
--- /dev/null
+++ b/utilfdt.c
@@ -0,0 +1,120 @@
+/*
+ * Copyright 2011 The Chromium Authors, All Rights Reserved.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ *  General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
+ *                                                                   USA
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+
+#include "libfdt.h"
+#include "utilfdt.h"
+
+char *util_read_fdt(const char *filename)
+{
+	FILE *fp;
+	char *buf = NULL;
+	struct stat stat_buf;
+	off_t size, upto = 0, toread;
+	int err = 0;
+
+	/* Open the file, or stdin, and work out how many bytes to read */
+	if (strcmp(filename, "-") == 0) {
+		fp = stdin;
+		toread = 64 * 1024;	/* Suitable likely maximum */
+	} else {
+		fp = fopen(filename, "rb");
+		if (fp == NULL) {
+			fprintf(stderr, "unable to open '%s'\n", filename);
+			return NULL;
+		}
+		if (fstat(fileno(fp), &stat_buf)) {
+			fprintf(stderr, "couldn't get size of file\n");
+			return NULL;
+		}
+		toread = stat_buf.st_size;
+	}
+
+	/* Loop until we have everything */
+	size = toread;
+	while (!feof(fp)) {
+		buf = realloc(buf, size);
+		if (!buf) {
+			fprintf(stderr, "couldn't allocate %ld byte buffer\n",
+				size);
+			return NULL;
+		}
+
+		upto += fread(buf + upto, 1, toread, fp);
+		if (ferror(fp)) {
+			fprintf(stderr, "file read error\n");
+			err = -1;
+			break;
+		}
+
+		/* Expand the buffer in case we need to read more */
+		size += toread;
+	}
+
+	/* Clean up, returning NULL on error */
+	if (fp != stdin)
+		fclose(fp);
+	if (err) {
+		free(buf);
+		buf = NULL;
+	}
+	return buf;
+}
+
+int util_write_fdt(const char *buf, const char *filename)
+{
+	FILE *fp;
+	size_t size, written;
+
+	if (strcmp(filename, "-") == 0) {
+		fp = stdout;
+	} else {
+		fp = fopen(filename, "wb");
+		if (fp == NULL) {
+			fprintf(stderr, "unable to open %s\n", filename);
+			return -1;
+		}
+	}
+
+	size = fdt_totalsize(buf);
+	written = fwrite(buf, 1, size, fp);
+	if (size != written) {
+		fprintf(stderr, "file write failed\n");
+		return -1;
+	}
+	return 0;
+}
+
+int utilfdt_decode_type(const char *arg, int *type, int *format)
+{
+	const char *s;
+
+	for (s = arg; *s; s++) {
+		if (strchr("iubs", *s))
+			*type = *s;
+		else if (*s == 'x')
+			*format = *s;
+		else
+			return -1;
+	}
+	return 0;
+}
diff --git a/utilfdt.h b/utilfdt.h
new file mode 100644
index 0000000..7c2828a
--- /dev/null
+++ b/utilfdt.h
@@ -0,0 +1,58 @@
+#ifndef _UTILFDT_H
+#define _UTILFDT_H
+
+/*
+ * Copyright 2011 The Chromium Authors, All Rights Reserved.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ *  General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
+ *                                                                   USA
+ */
+
+/**
+ * Read a device tree file into a buffer.
+ *
+ * @param filename	The filename to read, or - for stdin
+ * @return		Poiner to allocated buffer containing fdt, or NULL
+ *			on error
+ */
+char *util_read_fdt(const char *filename);
+
+/**
+ * Write a device tree buffer to a file.
+ *
+ * @param filename	The filename to write, or - for stdout
+ * @param buf		Poiner to buffer containing fdt
+ * @return 0 if ok, -1 on error
+ */
+int util_write_fdt(const char *buf, const char *filename);
+
+/**
+ * Decode a data type string.
+ *
+ * The string consists of:
+ *	One optional character (s=string, i=int, u=unsigned b=byte)
+ *	Optional format character (x=hex)
+ *
+ * If either of type or format is not found, then that variable is not
+ * updated, and the caller's default will be used.
+ *
+ * @param arg		Argument string to process
+ * @param type		Returns type found, if any
+ * @param format	Returns format fonud, if any
+ * @return 0 if ok, -1 on error
+ */
+int utilfdt_decode_type(const char *arg, int *type, int *format);
+
+#endif /* _UTILFDT_H */
-- 
1.7.3.1

  parent reply	other threads:[~2011-09-07 19:54 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-09-07 19:54 [PATCH v2 0/6] Add fdtget and fdtput for access to fdt from build system Simon Glass
     [not found] ` <1315425260-2711-1-git-send-email-sjg-F7+t8E8rja9g9hUCZPvPmw@public.gmane.org>
2011-09-07 19:54   ` Simon Glass [this message]
     [not found]     ` <1315425260-2711-2-git-send-email-sjg-F7+t8E8rja9g9hUCZPvPmw@public.gmane.org>
2011-09-08  5:20       ` [PATCH v2 1/6] Add utilfdt for common functions David Gibson
     [not found]         ` <20110908052028.GQ30278-787xzQ0H9iQXU02nzanrWNbf9cGiqdzd@public.gmane.org>
2011-09-08 12:37           ` Simon Glass
     [not found]             ` <CAPnjgZ1J0k93vo1A5ns5OxW5=nesr9pwLMMPAc+gyaOWmtmwuQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2011-09-09  2:34               ` David Gibson
2011-09-07 19:54   ` [PATCH v2 2/6] ftdump: use util_read_fdt Simon Glass
2011-09-07 19:54   ` [PATCH v2 3/6] Add fdtget utility to read property values from device tree Simon Glass
     [not found]     ` <1315425260-2711-4-git-send-email-sjg-F7+t8E8rja9g9hUCZPvPmw@public.gmane.org>
2011-09-08  5:25       ` David Gibson
     [not found]         ` <20110908052547.GR30278-787xzQ0H9iQXU02nzanrWNbf9cGiqdzd@public.gmane.org>
2011-09-08 12:47           ` Simon Glass
     [not found]             ` <CAPnjgZ0qnr8VVOX7kCq6wEZ-OxAsxmHkPcTGE7wu1B_9ME_SPA-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2011-09-09  4:49               ` David Gibson
     [not found]                 ` <20110909044945.GF21002-787xzQ0H9iQXU02nzanrWNbf9cGiqdzd@public.gmane.org>
2011-09-09  5:44                   ` Simon Glass
     [not found]                     ` <CAPnjgZ3xw7ByV4Yzeot3zgs4oo0zciX2OV_V4vfQ8tGsgLcPvw-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2011-09-12  0:53                       ` David Gibson
     [not found]                         ` <20110912005357.GI9025-787xzQ0H9iQXU02nzanrWNbf9cGiqdzd@public.gmane.org>
2011-09-12  4:34                           ` Simon Glass
     [not found]                             ` <CAPnjgZ1vevcwCQU+uyjAA3YZd--RhU9MgAF8O8G4Hr5e4CYo_g-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2011-09-16  8:18                               ` David Gibson
     [not found]                                 ` <20110916081804.GF9025-787xzQ0H9iQXU02nzanrWNbf9cGiqdzd@public.gmane.org>
2011-09-16 16:25                                   ` Simon Glass
     [not found]                                     ` <CAPnjgZ1LUPm9mRft5q=KHGe2h2+Masdh0kGXQ-7j1VTUDsMP7g-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2011-09-19  2:04                                       ` David Gibson
     [not found]                                         ` <20110919020440.GA15001-787xzQ0H9iQXU02nzanrWNbf9cGiqdzd@public.gmane.org>
2011-09-19  5:46                                           ` Simon Glass
     [not found]                                             ` <CAPnjgZ09mO06uruZtC=86BuitUWtQkGQfaHTf25HXK9KzoiD+w-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2011-09-19  8:05                                               ` David Gibson
     [not found]                                                 ` <20110919080557.GA29197-787xzQ0H9iQXU02nzanrWNbf9cGiqdzd@public.gmane.org>
2011-09-19 15:11                                                   ` Simon Glass
2011-09-07 19:54   ` [PATCH v2 4/6] fdtget: Add basic tests Simon Glass
     [not found]     ` <1315425260-2711-5-git-send-email-sjg-F7+t8E8rja9g9hUCZPvPmw@public.gmane.org>
2011-09-08  5:27       ` David Gibson
2011-09-07 19:54   ` [PATCH v2 5/6] Add new fdtput utility to write values to fdt Simon Glass
     [not found]     ` <1315425260-2711-6-git-send-email-sjg-F7+t8E8rja9g9hUCZPvPmw@public.gmane.org>
2011-09-08  5:32       ` David Gibson
     [not found]         ` <20110908053209.GT30278-787xzQ0H9iQXU02nzanrWNbf9cGiqdzd@public.gmane.org>
2011-09-08 12:51           ` Simon Glass
     [not found]             ` <CAPnjgZ2UNjxTz9=sWJ8JFq=AXF1NS4dG6C_BkyHvfDf=ZMVpmg-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2011-09-08 13:00               ` David Gibson
2011-09-07 19:54   ` [PATCH v2 6/6] fdtput: Add basic tests Simon Glass
     [not found]     ` <1315425260-2711-7-git-send-email-sjg-F7+t8E8rja9g9hUCZPvPmw@public.gmane.org>
2011-09-08  5:32       ` David Gibson
     [not found]         ` <20110908053235.GU30278-787xzQ0H9iQXU02nzanrWNbf9cGiqdzd@public.gmane.org>
2011-09-08 12:55           ` Simon Glass

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=1315425260-2711-2-git-send-email-sjg@chromium.org \
    --to=sjg-f7+t8e8rja9g9huczpvpmw@public.gmane.org \
    --cc=devicetree-discuss-uLR06cmDAlY/bJ5BZ2RsiQ@public.gmane.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.