Linux 3.12.28
[linux/fpc-iii.git] / scripts / dtc / util.h
blobc8eb45d9f04b718d12c8315356960f5d2976779f
1 #ifndef _UTIL_H
2 #define _UTIL_H
4 #include <stdarg.h>
6 /*
7 * Copyright 2011 The Chromium Authors, All Rights Reserved.
8 * Copyright 2008 Jon Loeliger, Freescale Semiconductor, Inc.
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.
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.
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
26 static inline void __attribute__((noreturn)) die(char * str, ...)
28 va_list ap;
30 va_start(ap, str);
31 fprintf(stderr, "FATAL ERROR: ");
32 vfprintf(stderr, str, ap);
33 exit(1);
36 static inline void *xmalloc(size_t len)
38 void *new = malloc(len);
40 if (!new)
41 die("malloc() failed\n");
43 return new;
46 static inline void *xrealloc(void *p, size_t len)
48 void *new = realloc(p, len);
50 if (!new)
51 die("realloc() failed (len=%d)\n", len);
53 return new;
56 extern char *xstrdup(const char *s);
57 extern char *join_path(const char *path, const char *name);
59 /**
60 * Check a string of a given length to see if it is all printable and
61 * has a valid terminator.
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 */
66 int util_is_printable_string(const void *data, int len);
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.
74 char get_escape_char(const char *s, int *i);
76 /**
77 * Read a device tree file into a buffer. This will report any errors on
78 * stderr.
80 * @param filename The filename to read, or - for stdin
81 * @return Pointer to allocated buffer containing fdt, or NULL on error
83 char *utilfdt_read(const char *filename);
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.
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
94 int utilfdt_read_err(const char *filename, char **buffp);
97 /**
98 * Write a device tree buffer to a file. This will report any errors on
99 * stderr.
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
105 int utilfdt_write(const char *filename, const void *blob);
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.
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
116 int utilfdt_write_err(const char *filename, const void *blob);
119 * Decode a data type string. The purpose of this string
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
127 * Type character:
128 * s string
129 * i signed integer
130 * u unsigned integer
131 * x hex
133 * TODO: Implement ll modifier (8 bytes)
134 * TODO: Implement o type (octal)
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)
141 int utilfdt_decode_type(const char *fmt, int *type, int *size);
144 * This is a usage message fragment for the -t option. It is the format
145 * supported by utilfdt_decode_type.
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";
153 #endif /* _UTIL_H */