util.h 4.4 KB

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