misc.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. /* misc.h - prototypes for misc functions */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2002,2003,2005,2006,2007,2008,2009,2010 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef GRUB_MISC_HEADER
  20. #define GRUB_MISC_HEADER 1
  21. #include <stdarg.h>
  22. #include <grub/types.h>
  23. #include <grub/symbol.h>
  24. #include <grub/err.h>
  25. #include <grub/i18n.h>
  26. #include <grub/compiler.h>
  27. #define ALIGN_UP(addr, align) \
  28. (((addr) + (typeof (addr)) (align) - 1) & ~((typeof (addr)) (align) - 1))
  29. #define ALIGN_UP_OVERHEAD(addr, align) ((-(addr)) & ((typeof (addr)) (align) - 1))
  30. #define ALIGN_DOWN(addr, align) \
  31. ((addr) & ~((typeof (addr)) (align) - 1))
  32. #define ARRAY_SIZE(array) (sizeof (array) / sizeof (array[0]))
  33. #define COMPILE_TIME_ASSERT(cond) switch (0) { case 1: case !(cond): ; }
  34. #define grub_dprintf(condition, ...) grub_real_dprintf(GRUB_FILE, __LINE__, condition, __VA_ARGS__)
  35. void *EXPORT_FUNC(grub_memmove) (void *dest, const void *src, grub_size_t n);
  36. char *EXPORT_FUNC(grub_strcpy) (char *dest, const char *src);
  37. static inline char *
  38. grub_strncpy (char *dest, const char *src, int c)
  39. {
  40. char *p = dest;
  41. while ((*p++ = *src++) != '\0' && --c)
  42. ;
  43. return dest;
  44. }
  45. static inline char *
  46. grub_stpcpy (char *dest, const char *src)
  47. {
  48. char *d = dest;
  49. const char *s = src;
  50. do
  51. *d++ = *s;
  52. while (*s++ != '\0');
  53. return d - 1;
  54. }
  55. /* XXX: If grub_memmove is too slow, we must implement grub_memcpy. */
  56. static inline void *
  57. grub_memcpy (void *dest, const void *src, grub_size_t n)
  58. {
  59. return grub_memmove (dest, src, n);
  60. }
  61. #if defined(__x86_64__) && !defined (GRUB_UTIL)
  62. #if defined (__MINGW32__) || defined (__CYGWIN__) || defined (__MINGW64__)
  63. #define GRUB_ASM_ATTR __attribute__ ((sysv_abi))
  64. #else
  65. #define GRUB_ASM_ATTR
  66. #endif
  67. #endif
  68. int EXPORT_FUNC(grub_memcmp) (const void *s1, const void *s2, grub_size_t n);
  69. int EXPORT_FUNC(grub_strcmp) (const char *s1, const char *s2);
  70. int EXPORT_FUNC(grub_strncmp) (const char *s1, const char *s2, grub_size_t n);
  71. char *EXPORT_FUNC(grub_strchr) (const char *s, int c);
  72. char *EXPORT_FUNC(grub_strrchr) (const char *s, int c);
  73. int EXPORT_FUNC(grub_strword) (const char *s, const char *w);
  74. /* Copied from gnulib.
  75. Written by Bruno Haible <bruno@clisp.org>, 2005. */
  76. static inline char *
  77. grub_strstr (const char *haystack, const char *needle)
  78. {
  79. /* Be careful not to look at the entire extent of haystack or needle
  80. until needed. This is useful because of these two cases:
  81. - haystack may be very long, and a match of needle found early,
  82. - needle may be very long, and not even a short initial segment of
  83. needle may be found in haystack. */
  84. if (*needle != '\0')
  85. {
  86. /* Speed up the following searches of needle by caching its first
  87. character. */
  88. char b = *needle++;
  89. for (;; haystack++)
  90. {
  91. if (*haystack == '\0')
  92. /* No match. */
  93. return 0;
  94. if (*haystack == b)
  95. /* The first character matches. */
  96. {
  97. const char *rhaystack = haystack + 1;
  98. const char *rneedle = needle;
  99. for (;; rhaystack++, rneedle++)
  100. {
  101. if (*rneedle == '\0')
  102. /* Found a match. */
  103. return (char *) haystack;
  104. if (*rhaystack == '\0')
  105. /* No match. */
  106. return 0;
  107. if (*rhaystack != *rneedle)
  108. /* Nothing in this round. */
  109. break;
  110. }
  111. }
  112. }
  113. }
  114. else
  115. return (char *) haystack;
  116. }
  117. int EXPORT_FUNC(grub_isspace) (int c);
  118. static inline int
  119. grub_isprint (int c)
  120. {
  121. return (c >= ' ' && c <= '~');
  122. }
  123. static inline int
  124. grub_iscntrl (int c)
  125. {
  126. return (c >= 0x00 && c <= 0x1F) || c == 0x7F;
  127. }
  128. static inline int
  129. grub_isalpha (int c)
  130. {
  131. return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
  132. }
  133. static inline int
  134. grub_islower (int c)
  135. {
  136. return (c >= 'a' && c <= 'z');
  137. }
  138. static inline int
  139. grub_isupper (int c)
  140. {
  141. return (c >= 'A' && c <= 'Z');
  142. }
  143. static inline int
  144. grub_isgraph (int c)
  145. {
  146. return (c >= '!' && c <= '~');
  147. }
  148. static inline int
  149. grub_isdigit (int c)
  150. {
  151. return (c >= '0' && c <= '9');
  152. }
  153. static inline int
  154. grub_isxdigit (int c)
  155. {
  156. return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
  157. }
  158. static inline int
  159. grub_isalnum (int c)
  160. {
  161. return grub_isalpha (c) || grub_isdigit (c);
  162. }
  163. static inline int
  164. grub_tolower (int c)
  165. {
  166. if (c >= 'A' && c <= 'Z')
  167. return c - 'A' + 'a';
  168. return c;
  169. }
  170. static inline int
  171. grub_toupper (int c)
  172. {
  173. if (c >= 'a' && c <= 'z')
  174. return c - 'a' + 'A';
  175. return c;
  176. }
  177. static inline int
  178. grub_strcasecmp (const char *s1, const char *s2)
  179. {
  180. while (*s1 && *s2)
  181. {
  182. if (grub_tolower ((grub_uint8_t) *s1)
  183. != grub_tolower ((grub_uint8_t) *s2))
  184. break;
  185. s1++;
  186. s2++;
  187. }
  188. return (int) grub_tolower ((grub_uint8_t) *s1)
  189. - (int) grub_tolower ((grub_uint8_t) *s2);
  190. }
  191. static inline int
  192. grub_strncasecmp (const char *s1, const char *s2, grub_size_t n)
  193. {
  194. if (n == 0)
  195. return 0;
  196. while (*s1 && *s2 && --n)
  197. {
  198. if (grub_tolower ((grub_uint8_t) *s1)
  199. != grub_tolower ((grub_uint8_t) *s2))
  200. break;
  201. s1++;
  202. s2++;
  203. }
  204. return (int) grub_tolower ((grub_uint8_t) *s1)
  205. - (int) grub_tolower ((grub_uint8_t) *s2);
  206. }
  207. /*
  208. * Do a case insensitive compare of two UUID strings by ignoring all dashes.
  209. * Note that the parameter n, is the number of significant characters to
  210. * compare, where significant characters are any except the dash.
  211. */
  212. static inline int
  213. grub_uuidcasecmp (const char *uuid1, const char *uuid2, grub_size_t n)
  214. {
  215. if (n == 0)
  216. return 0;
  217. while (*uuid1 && *uuid2 && --n)
  218. {
  219. /* Skip forward to non-dash on both UUIDs. */
  220. while ('-' == *uuid1)
  221. ++uuid1;
  222. while ('-' == *uuid2)
  223. ++uuid2;
  224. if (grub_tolower ((grub_uint8_t) *uuid1) != grub_tolower ((grub_uint8_t) *uuid2))
  225. break;
  226. uuid1++;
  227. uuid2++;
  228. }
  229. return (int) grub_tolower ((grub_uint8_t) *uuid1) - (int) grub_tolower ((grub_uint8_t) *uuid2);
  230. }
  231. /*
  232. * Note that these differ from the C standard's definitions of strtol,
  233. * strtoul(), and strtoull() by the addition of two const qualifiers on the end
  234. * pointer, which make the declaration match the *semantic* requirements of
  235. * their behavior. This means that instead of:
  236. *
  237. * char *s = "1234 abcd";
  238. * char *end;
  239. * unsigned long l;
  240. *
  241. * l = grub_strtoul(s, &end, 10);
  242. *
  243. * We must one of:
  244. *
  245. * const char *end;
  246. * ... or ...
  247. * l = grub_strtoul(s, (const char ** const)&end, 10);
  248. */
  249. unsigned long EXPORT_FUNC(grub_strtoul) (const char * restrict str, const char ** const restrict end, int base);
  250. unsigned long long EXPORT_FUNC(grub_strtoull) (const char * restrict str, const char ** const restrict end, int base);
  251. static inline long
  252. grub_strtol (const char * restrict str, const char ** const restrict end, int base)
  253. {
  254. int negative = 0;
  255. unsigned long long magnitude;
  256. while (*str && grub_isspace (*str))
  257. str++;
  258. if (*str == '-')
  259. {
  260. negative = 1;
  261. str++;
  262. }
  263. magnitude = grub_strtoull (str, end, base);
  264. if (negative)
  265. {
  266. if (magnitude > (unsigned long) GRUB_LONG_MAX + 1)
  267. {
  268. grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow is detected"));
  269. return GRUB_LONG_MIN;
  270. }
  271. return -((long) magnitude);
  272. }
  273. else
  274. {
  275. if (magnitude > GRUB_LONG_MAX)
  276. {
  277. grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow is detected"));
  278. return GRUB_LONG_MAX;
  279. }
  280. return (long) magnitude;
  281. }
  282. }
  283. char *EXPORT_FUNC(grub_strdup) (const char *s) WARN_UNUSED_RESULT;
  284. char *EXPORT_FUNC(grub_strndup) (const char *s, grub_size_t n) WARN_UNUSED_RESULT;
  285. void *EXPORT_FUNC(grub_memset) (void *s, int c, grub_size_t n);
  286. grub_size_t EXPORT_FUNC(grub_strlen) (const char *s) WARN_UNUSED_RESULT;
  287. /* Replace all `ch' characters of `input' with `with' and copy the
  288. result into `output'; return EOS address of `output'. */
  289. static inline char *
  290. grub_strchrsub (char *output, const char *input, char ch, const char *with)
  291. {
  292. while (*input)
  293. {
  294. if (*input == ch)
  295. {
  296. grub_strcpy (output, with);
  297. output += grub_strlen (with);
  298. input++;
  299. continue;
  300. }
  301. *output++ = *input++;
  302. }
  303. *output = '\0';
  304. return output;
  305. }
  306. extern void (*EXPORT_VAR (grub_xputs)) (const char *str);
  307. static inline int
  308. grub_puts (const char *s)
  309. {
  310. const char nl[2] = "\n";
  311. grub_xputs (s);
  312. grub_xputs (nl);
  313. return 1; /* Cannot fail. */
  314. }
  315. int EXPORT_FUNC(grub_puts_) (const char *s);
  316. int EXPORT_FUNC(grub_debug_enabled) (const char *condition);
  317. void EXPORT_FUNC(grub_real_dprintf) (const char *file,
  318. const int line,
  319. const char *condition,
  320. const char *fmt, ...) __attribute__ ((format (GNU_PRINTF, 4, 5)));
  321. int EXPORT_FUNC(grub_printf) (const char *fmt, ...) __attribute__ ((format (GNU_PRINTF, 1, 2)));
  322. int EXPORT_FUNC(grub_printf_) (const char *fmt, ...) __attribute__ ((format (GNU_PRINTF, 1, 2)));
  323. int EXPORT_FUNC(grub_vprintf) (const char *fmt, va_list args);
  324. int EXPORT_FUNC(grub_snprintf) (char *str, grub_size_t n, const char *fmt, ...)
  325. __attribute__ ((format (GNU_PRINTF, 3, 4)));
  326. int EXPORT_FUNC(grub_vsnprintf) (char *str, grub_size_t n, const char *fmt,
  327. va_list args);
  328. char *EXPORT_FUNC(grub_xasprintf) (const char *fmt, ...)
  329. __attribute__ ((format (GNU_PRINTF, 1, 2))) WARN_UNUSED_RESULT;
  330. char *EXPORT_FUNC(grub_xvasprintf) (const char *fmt, va_list args) WARN_UNUSED_RESULT;
  331. void EXPORT_FUNC(grub_exit) (void) __attribute__ ((noreturn));
  332. void EXPORT_FUNC(grub_abort) (void) __attribute__ ((noreturn));
  333. grub_uint64_t EXPORT_FUNC(grub_divmod64) (grub_uint64_t n,
  334. grub_uint64_t d,
  335. grub_uint64_t *r);
  336. extern bool EXPORT_FUNC(grub_is_cli_disabled) (void);
  337. /* Must match softdiv group in gentpl.py. */
  338. #if !defined(GRUB_MACHINE_EMU) && (defined(__arm__) || defined(__ia64__) || \
  339. (defined(__riscv) && (__riscv_xlen == 32)))
  340. #define GRUB_DIVISION_IN_SOFTWARE 1
  341. #else
  342. #define GRUB_DIVISION_IN_SOFTWARE 0
  343. #endif
  344. /* Some division functions need to be in kernel if compiler generates calls
  345. to them. Otherwise we still need them for consistent tests but they go
  346. into a separate module. */
  347. #if GRUB_DIVISION_IN_SOFTWARE
  348. #define EXPORT_FUNC_IF_SOFTDIV EXPORT_FUNC
  349. #else
  350. #define EXPORT_FUNC_IF_SOFTDIV(x) x
  351. #endif
  352. grub_int64_t
  353. EXPORT_FUNC_IF_SOFTDIV(grub_divmod64s) (grub_int64_t n,
  354. grub_int64_t d,
  355. grub_int64_t *r);
  356. grub_uint32_t
  357. EXPORT_FUNC_IF_SOFTDIV (grub_divmod32) (grub_uint32_t n,
  358. grub_uint32_t d,
  359. grub_uint32_t *r);
  360. grub_int32_t
  361. EXPORT_FUNC_IF_SOFTDIV (grub_divmod32s) (grub_int32_t n,
  362. grub_int32_t d,
  363. grub_int32_t *r);
  364. /* Inline functions. */
  365. static inline char *
  366. grub_memchr (const void *p, int c, grub_size_t len)
  367. {
  368. const char *s = (const char *) p;
  369. const char *e = s + len;
  370. for (; s < e; s++)
  371. if (*s == c)
  372. return (char *) s;
  373. return 0;
  374. }
  375. static inline unsigned int
  376. grub_abs (int x)
  377. {
  378. if (x < 0)
  379. return (unsigned int) (-x);
  380. else
  381. return (unsigned int) x;
  382. }
  383. /* Reboot the machine. */
  384. #if defined (GRUB_MACHINE_EMU) || defined (GRUB_MACHINE_QEMU_MIPS) || \
  385. defined (GRUB_MACHINE_EFI)
  386. void EXPORT_FUNC(grub_reboot) (void) __attribute__ ((noreturn));
  387. #else
  388. void grub_reboot (void) __attribute__ ((noreturn));
  389. #endif
  390. #ifdef GRUB_MACHINE_PCBIOS
  391. /* Halt the system, using APM if possible. If NO_APM is true, don't
  392. * use APM even if it is available. */
  393. void grub_halt (int no_apm) __attribute__ ((noreturn));
  394. #elif defined (__mips__) && !defined (GRUB_MACHINE_EMU)
  395. void EXPORT_FUNC (grub_halt) (void) __attribute__ ((noreturn));
  396. #else
  397. void grub_halt (void) __attribute__ ((noreturn));
  398. #endif
  399. #ifdef GRUB_MACHINE_EMU
  400. /* Flag to check if module loading is available. */
  401. extern const int EXPORT_VAR(grub_no_modules);
  402. #else
  403. #define grub_no_modules 0
  404. #endif
  405. static inline void
  406. grub_error_save (struct grub_error_saved *save)
  407. {
  408. grub_memcpy (save->errmsg, grub_errmsg, sizeof (save->errmsg));
  409. save->grub_errno = grub_errno;
  410. grub_errno = GRUB_ERR_NONE;
  411. }
  412. static inline void
  413. grub_error_load (const struct grub_error_saved *save)
  414. {
  415. grub_memcpy (grub_errmsg, save->errmsg, sizeof (grub_errmsg));
  416. grub_errno = save->grub_errno;
  417. }
  418. /*
  419. * grub_printf_fmt_checks() a fmt string for printf() against an expected
  420. * format. It is intended for cases where the fmt string could come from
  421. * an outside source and cannot be trusted.
  422. *
  423. * While expected fmt accepts a printf() format string it should be kept
  424. * as simple as possible. The printf() format strings with positional
  425. * parameters are NOT accepted, neither for fmt nor for fmt_expected.
  426. *
  427. * The fmt is accepted if it has equal or less arguments than fmt_expected
  428. * and if the type of all arguments match.
  429. *
  430. * Returns GRUB_ERR_NONE if fmt is acceptable.
  431. */
  432. grub_err_t EXPORT_FUNC (grub_printf_fmt_check) (const char *fmt, const char *fmt_expected);
  433. #if BOOT_TIME_STATS
  434. struct grub_boot_time
  435. {
  436. struct grub_boot_time *next;
  437. grub_uint64_t tp;
  438. const char *file;
  439. int line;
  440. char *msg;
  441. };
  442. extern struct grub_boot_time *EXPORT_VAR(grub_boot_time_head);
  443. void EXPORT_FUNC(grub_real_boot_time) (const char *file,
  444. const int line,
  445. const char *fmt, ...) __attribute__ ((format (GNU_PRINTF, 3, 4)));
  446. #define grub_boot_time(...) grub_real_boot_time(GRUB_FILE, __LINE__, __VA_ARGS__)
  447. #else
  448. #define grub_boot_time(...)
  449. #endif
  450. #define grub_max(a, b) (((a) > (b)) ? (a) : (b))
  451. #define grub_min(a, b) (((a) < (b)) ? (a) : (b))
  452. #define grub_log2ull(n) (GRUB_TYPE_BITS (grub_uint64_t) - __builtin_clzll (n) - 1)
  453. grub_ssize_t
  454. EXPORT_FUNC(grub_utf8_to_utf16_alloc) (const char *str8, grub_uint16_t **utf16_msg, grub_uint16_t **last_position);
  455. #endif /* ! GRUB_MISC_HEADER */