aboutsummaryrefslogtreecommitdiff
path: root/util.h
AgeCommit message (Collapse)AuthorFilesLines
2023-02-27Add -Wsuggest-attribute=format warning, correct warnings thus generatedDavid Gibson1-1/+1
Add this new warning to the default build flags. It suggests adding a ((format)) attribute to xavsprintf_append(), so do that. Suggested-by: Marc-André Lureau <marcandre.lureau@redhat.com> Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2022-01-25util: introduce xstrndup helperAhmad Fatoum1-0/+1
We already have xstrdup, add xstrndup as well to make it straight-forward to clone part of a string. Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
2021-12-09Support 'r' format for printing raw bytes with fdtgetRafał Miłecki1-1/+2
FT is sometimes used for storing raw data. That is quite common for U-Boot FIT images. Extracting such data is not trivial currently. Using type 's' (string) will replace every 0x00 (NUL) with 0x20 (space). Using type 'x' will print bytes but in xxd incompatible format. This commit adds support for 'r' (raw) format. Example usage: fdtget -t r firmware.itb /images/foo data > image.raw Support for encoding isn't added as there isn't any clean way of passing binary data as command line argument. Signed-off-by: Rafał Miłecki <rafal@milecki.pl> Message-Id: <20211209061420.29466-1-zajec5@gmail.com> Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2021-02-07util: limit gnu_printf format attribute to gcc >= 4.4.0Jonathan Gray1-3/+3
The gnu_printf format attribute was introduced in gcc 4.4.0 https://gcc.gnu.org/legacy-ml/gcc-help/2012-02/msg00225.html. Use the printf format attribute on earlier versions of gcc and clang (which claims to be gcc 4.2.1 in builtin defines) to fix the build with gcc 4.2.1. Fixes: 588a29f ("util: use gnu_printf format attribute") Signed-off-by: Jonathan Gray <jsg@jsg.id.au> Message-Id: <20210206100110.75228-1-jsg@jsg.id.au> Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2020-07-22dtc: Include stdlib.h in util.hAndrei Ziureaev1-0/+1
If used on its own, util.h needs stdlib.h for exit(), malloc() and realloc(). Signed-off-by: Andrei Ziureaev <andrei.ziureaev@arm.com> Message-Id: <20200721155900.9147-2-andrei.ziureaev@arm.com> Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2019-11-21Avoid gnu_printf attribute when using ClangEd Maste1-0/+4
Clang does not support gnu_printf, so just use printf when using it to compile. Signed-off-by: Ed Maste <emaste@freebsd.org> Message-Id: <20191120210422.61327-1-emaste@freefall.freebsd.org> Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2019-10-14util: use gnu_printf format attributeMarc-André Lureau1-1/+1
dtc uses non-portable formats. Using gnu_printf attributes (for warnings) in combination with __USE_MINGW_ANSI_STDIO allows to build for win32. Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com> Message-Id: <20191009102025.10179-8-marcandre.lureau@redhat.com> Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2019-06-21dtc: Replace GPLv2 boilerplate/reference with SPDX tagsRob Herring1-15/+1
Replace instances of GPLv2 or later boilerplate with SPDX tags. Signed-off-by: Rob Herring <robh@kernel.org> Message-Id: <20190620211944.9378-2-robh@kernel.org> Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2019-05-21Fix typos in various documentation and source filesThomas Huth1-2/+2
The typos have been discovered with the "codespell" utility. Signed-off-by: Thomas Huth <thuth@redhat.com> Message-Id: <20190520081209.20415-1-thuth@redhat.com> Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2018-11-25util: Add xa{v}sprintf_append functionsRob Herring1-0/+2
Add variadic and va_list functions, xa{v}sprintf, which appends a formatted string to an existing string and re-allocate the string buffer if necessary. xasprintf becomes just a special case of xasprintf_append with a NULL starting string. Rather than looping to get a big enough buffer, simply the implementation by assuming we have a C99 compliant vsnprintf implementation to return the necessary size. A side effect is glibc 2.0 support is dropped which seems unnecessary. Signed-off-by: Rob Herring <robh@kernel.org> Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2018-06-07Use size_t for blob lengths in utilfdt_read*David Gibson1-2/+2
It's more appropriate than off_t since it is, after all, a size not an offset. Signed-off-by: David Gibson <david@gibson.dropbear.id.au> Reviewed-by: Simon Glass <sjg@chromium.org> Tested-by: Alexey Kardashevskiy <aik@ozlabs.ru> Reviewed-by: Alexey Kardashevskiy <aik@ozlabs.ru>
2018-06-07Consolidate utilfdt_read_len() variantsDavid Gibson1-16/+4
There are no less than _four_ variants on utilfdt_read() which is a bit excessive. The _len() variants are particularly pointless, since we can achieve the same thing with very little extra verbosity by using the usual convention of ignoring return parameters if they're NULL. So, get rid of them (we keep the shorter names without _len, but add now-optional len parameters). Signed-off-by: David Gibson <david@gibson.dropbear.id.au> Reviewed-by: Simon Glass <sjg@chromium.org> Tested-by: Alexey Kardashevskiy <aik@ozlabs.ru> Reviewed-by: Alexey Kardashevskiy <aik@ozlabs.ru>
2017-10-26Remove leading underscores from identifiersDavid Gibson1-3/+3
In a number of places, dtc and associated tools and test code use leading _ characters on identifiers to flag them as "internal", an idiom taken from the Linux kernel. This is a bad idea in a userspace program, because identifiers with a leading _ are reserved for the C library / system. In some cases, the extra _ served no real purpose, so simply drop it. In others move to the end of the identifier, which is a convention we're free to use for our own purposes. Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2017-10-26Replace FDT_VERSION() with stringify()David Gibson1-0/+3
The FDT_VERSION() and _FDT_VERSION() macros don't really have anything specific to do with the fdt version. Rather, they're the common CPP "stringify" idiom. Move to util.h and rename to stringify() to reflect this. Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2017-03-06Clean up gcc attributesDavid Gibson1-16/+14
We have a number of explicit __GNUC__ conditionals to tell if we want to use some gcc extensions for extra warnings. This cleans this up to use a single conditional, defining convenience macros for those attributes. Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2017-02-28Add printf format attributesDavid Gibson1-1/+13
When compiling with gcc, we already include the attribute on check_msg() to give compiler warnings about mismatches between printf() like format strings and the corresponding arguments. This patch adds similar attributes for lexical_error() and die(). Suggested-by: Nicolas Iooss <nicolas.iooss_linux@m4x.org> Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2017-02-28Correct some broken printf() like format mismatchesDavid Gibson1-1/+1
Fix two places where a printf()-style format string does not match the arguments passed. Reported-by: Nicolas Iooss <nicolas.iooss_linux@m4x.org> Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2016-05-25util: Add xasprintf portable asprintf variantPantelis Antoniou1-0/+1
Include a portable asprintf variant that works on any C99 conforming platform. Signed-off-by: Pantelis Antoniou <pantelis.antoniou@konsulko.com> Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2015-01-13dtc: Use va_end to match corresponding va_startColin Ian King1-0/+1
Although on some systems va_end is a no-op, it is good practice to use va_end, especially since the manual states: "Each invocation of va_start() must be matched by a corresponding invocation of va_end() in the same function." Signed-off-by: Colin Ian King <colin.king@canonical.com>
2013-10-28Use stdbool more widelyDavid Gibson1-1/+2
We already use the C99 bool type from stdbool.h in a few places. However there are many other places we represent boolean values as plain ints. This patch changes that. Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2013-05-24util: add common ARRAY_SIZE defineMike Frysinger1-0/+2
I want to use this in more places, so put it in util.h rather than copying & pasting it into another file. Signed-off-by: Mike Frysinger <vapier@gentoo.org> Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2013-05-24util: drop "long" from usage helpersMike Frysinger1-9/+9
Now that all utils have converted to the new usage framework, we can rename to just plain "usage()" and avoid naming conflicts. Signed-off-by: Mike Frysinger <vapier@gentoo.org> Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2013-05-24dtc/fdt{get, put}/convert-dtsv0-lexer: convert to new usage helpersMike Frysinger1-1/+1
This helps standardize the flag processing and the usage screens. Only lightly tested; would be great if someone who uses these utils could double check. Signed-off-by: Mike Frysinger <vapier@gentoo.org> Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2013-05-24fdtdump: make usage a bit more friendlyMike Frysinger1-0/+61
This starts a new usage framework and then cuts fdtdump over to it. Now we can do `fdtdump -h` and get something useful back. Signed-off-by: Mike Frysinger <vapier@gentoo.org> Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2013-04-22utilfdt_read: pass back up the length of data readMike Frysinger1-0/+13
For a follow up commit, we want to be able to scan the buffer that was returned to us. In order to do that safely, we need to know how big the buffer actually is, so create a new set of funcs to pass that back. Acked-by: David Gibson <david@gibson.dropbear.id.au> Signed-off-by: Mike Frysinger <vapier@gentoo.org>
2013-04-22Revert "utilfdt_read: pass back up the length of data read"Jon Loeliger1-4/+2
This reverts commit cc2c178727cdeca4eb9756637c2e09e50e0856e7. It was the wrong version of the patch.
2013-04-21util_version: new helper for displaying version infoMike Frysinger1-0/+5
This is so all utilities can have this flag and not just dtc. Acked-by: David Gibson <david@gibson.dropbear.id.au> Signed-off-by: Mike Frysinger <vapier@gentoo.org>
2013-04-21die: constify format string argMike Frysinger1-1/+1
We only display this string, so there's no need for it to be writable. Constify away! Acked-by: David Gibson <David@gibson.dropbear.id.au> Signed-off-by: Mike Frysinger <vapier@gentoo.org>
2013-04-21utilfdt_read: pass back up the length of data readMike Frysinger1-2/+4
For a follow up commit, we want to be able to scan the buffer that was returned to us. In order to do that safely, we need to know how big the buffer actually is, so pass that back if requested. Signed-off-by: Mike Frysinger <vapier@gentoo.org>
2013-01-27Move property-printing into utilSimon Glass1-0/+14
The function that prints a property can be useful to other programs, so move it into util. Signed-off-by: Simon Glass <sjg@chromium.org> Acked-by: David Gibson <david@gibson.dropbear.id.au>
2013-01-27Adjust util_is_printable_string() comment and fix testSimon Glass1-3/+5
This commit which changed the behaviour of this function broke one of the tests. Also the comment should be updated to reflect its new behaviour. Signed-off-by: Simon Glass <sjg@chromium.org> Acked-by: David Gibson <david@gibson.dropbear.id.au>
2012-01-21Add fdtget utility to read property values from a device treeSimon Glass1-0/+10
This simply utility makes it easy for scripts to read values from the device tree. It is written in C and uses the same libfdt as the rest of the dtc package. What is it for: - Reading fdt values from scripts - Extracting fdt information within build systems - Looking at particular values without having to dump the entire tree To use it, specify the fdt binary file on command line followed by a list of node, property pairs. The utility then looks up each node, finds the property and displays the value. Each value is printed on a new line. fdtget tries to guess the type of each property based on its contents. This is not always reliable, so you can use the -t option to force fdtget to decode the value as a string, or byte, etc. To read from stdin, use - as the file. Usage: fdtget <options> <dt file> [<node> <property>]... Options: -t <type> Type of data -h Print this help <type> s=string, i=int, u=unsigned, x=hex Optional modifier prefix: hh or b=byte, h=2 byte, l=4 byte (default) Signed-off-by: Simon Glass <sjg@chromium.org>
2011-09-22Add fdt read/write utility functionsSimon Glass1-0/+68
This adds higher-level libfdt operations for reading/writing an fdt blob from/to a file, as well as a function to decode a data type string as will be used by fdtget, fdtput. This also adds a few tests for the simple type argument supported by utilfdt_decode_type. Signed-off-by: Simon Glass <sjg@chromium.org> Acked-by: David Gibson <david@gibson.dropbear.id.au>
2011-09-09dtc: Refactor character literal parsing codeAnton Staaf1-0/+8
Move the parsing of hex, octal and escaped characters from data.c to util.c where it can be used for character literal parsing within strings as well as for stand alone C style character literals. Signed-off-by: Anton Staaf <robotboy@chromium.org> Acked-by: David Gibson <david@gibson.dropbear.id.au>
2011-07-17Split out is_printable_string() into util.cSimon Glass1-0/+11
This useful function is split out so it will be available to programs other than ftdump. Signed-off-by: Simon Glass <sjg@chromium.org> Acked-by: David Gibson <david@gibson.dropbear.id.au>
2010-01-14dtc: Simpler interface to source file managementDavid Gibson1-0/+1
This patch cleans up our handling of input files, particularly dts source files, but also (to an extent) other input files such as those used by /incbin/ and those used in -I dtb and -I fs modes. We eliminate the current clunky mechanism which combines search paths (which we don't actually use at present) with the open relative to current source file behaviour, which we do. Instead there's a single srcfile_relative_open() entry point for callers which opens a new input file relative to the current source file (which the srcpos code tracks internally). It doesn't currently do search paths, but we can add that later without messing with the callers, by drawing the search path from a global (which makes sense anyway, rather than shuffling it around the rest of the processing code). That suffices for non-dts input files. For the actual dts files, srcfile_push() and srcfile_pop() wrappers open the file while also keeping track of it as the current source file for future opens. Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2009-01-07dtc: Move some functions to util.[ch]David Gibson1-0/+35
Now that we have a util.[ch] file shared between dtc and convert-dtsv0, move some functions which are currently duplicated in the two to util files. Specifically we move the die(), xmalloc() and xrealloc() functions. While we're at it, add standard double-include protection to util.h Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2008-10-03Implement and use an xstrdup() functionJon Loeliger1-0/+20
Many places in dtc use strdup(), but none of them actually check the return value to see if the implied allocation succeeded. This is a potential bug, which we fix in the patch below by replacing strdup() with an xstrdup() which in analogy to xmalloc() will quit with a fatal error if the allocation fails. I felt the introduciton of util.[ch] was a better choice for utility oriented code than directly using srcpos.c for the new string function. This patch is a re-factoring of Dave Gibson's similar patch. Signed-off-by: Jon Loeliger <jdl@freescale.com>