blob: c8eb45d9f04b718d12c8315356960f5d2976779f [file] [log] [blame]
John Bonesio658f29a2010-11-17 15:28:20 -08001#ifndef _UTIL_H
2#define _UTIL_H
3
Stephen Warrencd296722012-09-28 21:25:59 +00004#include <stdarg.h>
5
John Bonesio658f29a2010-11-17 15:28:20 -08006/*
Stephen Warrencd296722012-09-28 21:25:59 +00007 * Copyright 2011 The Chromium Authors, All Rights Reserved.
John Bonesio658f29a2010-11-17 15:28:20 -08008 * Copyright 2008 Jon Loeliger, Freescale Semiconductor, Inc.
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of the
13 * License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 * USA
24 */
25
26static inline void __attribute__((noreturn)) die(char * str, ...)
27{
28 va_list ap;
29
30 va_start(ap, str);
31 fprintf(stderr, "FATAL ERROR: ");
32 vfprintf(stderr, str, ap);
33 exit(1);
34}
35
36static inline void *xmalloc(size_t len)
37{
38 void *new = malloc(len);
39
40 if (!new)
41 die("malloc() failed\n");
42
43 return new;
44}
45
46static inline void *xrealloc(void *p, size_t len)
47{
48 void *new = realloc(p, len);
49
50 if (!new)
51 die("realloc() failed (len=%d)\n", len);
52
53 return new;
54}
55
56extern char *xstrdup(const char *s);
57extern char *join_path(const char *path, const char *name);
58
Stephen Warrencd296722012-09-28 21:25:59 +000059/**
60 * Check a string of a given length to see if it is all printable and
61 * has a valid terminator.
62 *
63 * @param data The string to check
64 * @param len The string length including terminator
65 * @return 1 if a valid printable string, 0 if not */
66int util_is_printable_string(const void *data, int len);
67
68/*
69 * Parse an escaped character starting at index i in string s. The resulting
70 * character will be returned and the index i will be updated to point at the
71 * character directly after the end of the encoding, this may be the '\0'
72 * terminator of the string.
73 */
74char get_escape_char(const char *s, int *i);
75
76/**
77 * Read a device tree file into a buffer. This will report any errors on
78 * stderr.
79 *
80 * @param filename The filename to read, or - for stdin
81 * @return Pointer to allocated buffer containing fdt, or NULL on error
82 */
83char *utilfdt_read(const char *filename);
84
85/**
86 * Read a device tree file into a buffer. Does not report errors, but only
87 * returns them. The value returned can be passed to strerror() to obtain
88 * an error message for the user.
89 *
90 * @param filename The filename to read, or - for stdin
91 * @param buffp Returns pointer to buffer containing fdt
92 * @return 0 if ok, else an errno value representing the error
93 */
94int utilfdt_read_err(const char *filename, char **buffp);
95
96
97/**
98 * Write a device tree buffer to a file. This will report any errors on
99 * stderr.
100 *
101 * @param filename The filename to write, or - for stdout
102 * @param blob Poiner to buffer containing fdt
103 * @return 0 if ok, -1 on error
104 */
105int utilfdt_write(const char *filename, const void *blob);
106
107/**
108 * Write a device tree buffer to a file. Does not report errors, but only
109 * returns them. The value returned can be passed to strerror() to obtain
110 * an error message for the user.
111 *
112 * @param filename The filename to write, or - for stdout
113 * @param blob Poiner to buffer containing fdt
114 * @return 0 if ok, else an errno value representing the error
115 */
116int utilfdt_write_err(const char *filename, const void *blob);
117
118/**
119 * Decode a data type string. The purpose of this string
120 *
121 * The string consists of an optional character followed by the type:
122 * Modifier characters:
123 * hh or b 1 byte
124 * h 2 byte
125 * l 4 byte, default
126 *
127 * Type character:
128 * s string
129 * i signed integer
130 * u unsigned integer
131 * x hex
132 *
133 * TODO: Implement ll modifier (8 bytes)
134 * TODO: Implement o type (octal)
135 *
136 * @param fmt Format string to process
137 * @param type Returns type found(s/d/u/x), or 0 if none
138 * @param size Returns size found(1,2,4,8) or 4 if none
139 * @return 0 if ok, -1 on error (no type given, or other invalid format)
140 */
141int utilfdt_decode_type(const char *fmt, int *type, int *size);
142
143/*
144 * This is a usage message fragment for the -t option. It is the format
145 * supported by utilfdt_decode_type.
146 */
147
148#define USAGE_TYPE_MSG \
149 "<type>\ts=string, i=int, u=unsigned, x=hex\n" \
150 "\tOptional modifier prefix:\n" \
151 "\t\thh or b=byte, h=2 byte, l=4 byte (default)\n";
152
John Bonesio658f29a2010-11-17 15:28:20 -0800153#endif /* _UTIL_H */