1 /* misc.h - prototypes for misc functions */
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2002,2003,2005,2006,2007,2008,2009,2010 Free Software Foundation, Inc.
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.
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.
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/>.
20 #ifndef GRUB_MISC_HEADER
21 #define GRUB_MISC_HEADER 1
24 #include <grub/types.h>
25 #include <grub/symbol.h>
27 #include <grub/i18n.h>
29 /* GCC version checking borrowed from glibc. */
30 #if defined(__GNUC__) && defined(__GNUC_MINOR__)
31 # define GNUC_PREREQ(maj,min) \
32 ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
34 # define GNUC_PREREQ(maj,min) 0
37 /* Does this compiler support compile-time error attributes? */
39 # define ATTRIBUTE_ERROR(msg) \
40 __attribute__ ((__error__ (msg)))
42 # define ATTRIBUTE_ERROR(msg) __attribute__ ((noreturn))
45 #define ALIGN_UP(addr, align) \
46 ((addr + (typeof (addr)) align - 1) & ~((typeof (addr)) align - 1))
47 #define ALIGN_UP_OVERHEAD(addr, align) ((-(addr)) & ((typeof (addr)) (align) - 1))
48 #define ALIGN_DOWN(addr, align) \
49 ((addr) & ~((typeof (addr)) align - 1))
50 #define ARRAY_SIZE(array) (sizeof (array) / sizeof (array[0]))
51 #define COMPILE_TIME_ASSERT(cond) switch (0) { case 1: case !(cond): ; }
53 #define grub_dprintf(condition, fmt, args...) grub_real_dprintf(GRUB_FILE, __LINE__, condition, fmt, ## args)
55 void *EXPORT_FUNC(grub_memmove
) (void *dest
, const void *src
, grub_size_t n
);
56 char *EXPORT_FUNC(grub_strcpy
) (char *dest
, const char *src
);
57 char *EXPORT_FUNC(grub_strncpy
) (char *dest
, const char *src
, int c
);
59 grub_stpcpy (char *dest
, const char *src
)
71 /* XXX: If grub_memmove is too slow, we must implement grub_memcpy. */
73 grub_memcpy (void *dest
, const void *src
, grub_size_t n
)
75 return grub_memmove (dest
, src
, n
);
79 grub_strcat (char *dest
, const char *src
)
86 while ((*p
= *src
) != '\0')
96 grub_strncat (char *dest
, const char *src
, int c
)
103 while (c
-- && (*p
= *src
) != '\0')
114 /* Prototypes for aliases. */
117 int __attribute__ ((regparm(0))) EXPORT_FUNC(memcmp
) (const void *s1
, const void *s2
, grub_size_t n
);
118 void *__attribute__ ((regparm(0))) EXPORT_FUNC(memmove
) (void *dest
, const void *src
, grub_size_t n
);
119 void *__attribute__ ((regparm(0))) EXPORT_FUNC(memcpy
) (void *dest
, const void *src
, grub_size_t n
);
120 void *__attribute__ ((regparm(0))) EXPORT_FUNC(memset
) (void *s
, int c
, grub_size_t n
);
122 int EXPORT_FUNC(memcmp
) (const void *s1
, const void *s2
, grub_size_t n
);
123 void *EXPORT_FUNC(memmove
) (void *dest
, const void *src
, grub_size_t n
);
124 void *EXPORT_FUNC(memcpy
) (void *dest
, const void *src
, grub_size_t n
);
125 void *EXPORT_FUNC(memset
) (void *s
, int c
, grub_size_t n
);
129 int EXPORT_FUNC(grub_memcmp
) (const void *s1
, const void *s2
, grub_size_t n
);
130 int EXPORT_FUNC(grub_strcmp
) (const char *s1
, const char *s2
);
131 int EXPORT_FUNC(grub_strncmp
) (const char *s1
, const char *s2
, grub_size_t n
);
133 char *EXPORT_FUNC(grub_strchr
) (const char *s
, int c
);
134 char *EXPORT_FUNC(grub_strrchr
) (const char *s
, int c
);
135 int EXPORT_FUNC(grub_strword
) (const char *s
, const char *w
);
137 /* Copied from gnulib.
138 Written by Bruno Haible <bruno@clisp.org>, 2005. */
140 grub_strstr (const char *haystack
, const char *needle
)
142 /* Be careful not to look at the entire extent of haystack or needle
143 until needed. This is useful because of these two cases:
144 - haystack may be very long, and a match of needle found early,
145 - needle may be very long, and not even a short initial segment of
146 needle may be found in haystack. */
149 /* Speed up the following searches of needle by caching its first
155 if (*haystack
== '\0')
159 /* The first character matches. */
161 const char *rhaystack
= haystack
+ 1;
162 const char *rneedle
= needle
;
164 for (;; rhaystack
++, rneedle
++)
166 if (*rneedle
== '\0')
168 return (char *) haystack
;
169 if (*rhaystack
== '\0')
172 if (*rhaystack
!= *rneedle
)
173 /* Nothing in this round. */
180 return (char *) haystack
;
183 int EXPORT_FUNC(grub_isspace
) (int c
);
184 int EXPORT_FUNC(grub_isprint
) (int c
);
189 return (c
>= 0x00 && c
<= 0x1F) || c
== 0x7F;
195 return (c
>= 'a' && c
<= 'z') || (c
>= 'A' && c
<= 'Z');
201 return (c
>= 'a' && c
<= 'z');
207 return (c
>= 'A' && c
<= 'Z');
213 return (c
>= '!' && c
<= '~');
219 return (c
>= '0' && c
<= '9');
223 grub_isxdigit (int c
)
225 return (c
>= '0' && c
<= '9') || (c
>= 'a' && c
<= 'f') || (c
>= 'A' && c
<= 'F');
231 return grub_isalpha (c
) || grub_isdigit (c
);
237 if (c
>= 'A' && c
<= 'Z')
238 return c
- 'A' + 'a';
246 if (c
>= 'a' && c
<= 'z')
247 return c
- 'a' + 'A';
253 grub_strcasecmp (const char *s1
, const char *s2
)
257 if (grub_tolower ((grub_uint8_t
) *s1
)
258 != grub_tolower ((grub_uint8_t
) *s2
))
265 return (int) grub_tolower ((grub_uint8_t
) *s1
)
266 - (int) grub_tolower ((grub_uint8_t
) *s2
);
270 grub_strncasecmp (const char *s1
, const char *s2
, grub_size_t n
)
275 while (*s1
&& *s2
&& --n
)
277 if (grub_tolower (*s1
) != grub_tolower (*s2
))
284 return (int) grub_tolower ((grub_uint8_t
) *s1
)
285 - (int) grub_tolower ((grub_uint8_t
) *s2
);
288 unsigned long EXPORT_FUNC(grub_strtoul
) (const char *str
, char **end
, int base
);
289 unsigned long long EXPORT_FUNC(grub_strtoull
) (const char *str
, char **end
, int base
);
292 grub_strtol (const char *str
, char **end
, int base
)
295 unsigned long magnitude
;
297 while (*str
&& grub_isspace (*str
))
306 magnitude
= grub_strtoull (str
, end
, base
);
309 if (magnitude
> (unsigned long) GRUB_LONG_MAX
+ 1)
311 grub_error (GRUB_ERR_OUT_OF_RANGE
, N_("overflow is detected"));
312 return GRUB_LONG_MIN
;
314 return -((long) magnitude
);
318 if (magnitude
> GRUB_LONG_MAX
)
320 grub_error (GRUB_ERR_OUT_OF_RANGE
, N_("overflow is detected"));
321 return GRUB_LONG_MAX
;
323 return (long) magnitude
;
327 char *EXPORT_FUNC(grub_strdup
) (const char *s
) __attribute__ ((warn_unused_result
));
328 char *EXPORT_FUNC(grub_strndup
) (const char *s
, grub_size_t n
) __attribute__ ((warn_unused_result
));
329 void *EXPORT_FUNC(grub_memset
) (void *s
, int c
, grub_size_t n
);
330 grub_size_t
EXPORT_FUNC(grub_strlen
) (const char *s
) __attribute__ ((warn_unused_result
));
331 int EXPORT_FUNC(grub_printf
) (const char *fmt
, ...) __attribute__ ((format (printf
, 1, 2)));
332 int EXPORT_FUNC(grub_printf_
) (const char *fmt
, ...) __attribute__ ((format (printf
, 1, 2)));
334 /* Replace all `ch' characters of `input' with `with' and copy the
335 result into `output'; return EOS address of `output'. */
337 grub_strchrsub (char *output
, const char *input
, char ch
, const char *with
)
343 grub_strcpy (output
, with
);
344 output
+= grub_strlen (with
);
348 *output
++ = *input
++;
354 extern void (*EXPORT_VAR (grub_xputs
)) (const char *str
);
357 grub_puts (const char *s
)
359 const char nl
[2] = "\n";
363 return 1; /* Cannot fail. */
366 int EXPORT_FUNC(grub_puts_
) (const char *s
);
367 void EXPORT_FUNC(grub_real_dprintf
) (const char *file
,
369 const char *condition
,
370 const char *fmt
, ...) __attribute__ ((format (printf
, 4, 5)));
371 int EXPORT_FUNC(grub_vprintf
) (const char *fmt
, va_list args
);
372 int EXPORT_FUNC(grub_snprintf
) (char *str
, grub_size_t n
, const char *fmt
, ...)
373 __attribute__ ((format (printf
, 3, 4)));
374 int EXPORT_FUNC(grub_vsnprintf
) (char *str
, grub_size_t n
, const char *fmt
,
376 char *EXPORT_FUNC(grub_xasprintf
) (const char *fmt
, ...)
377 __attribute__ ((format (printf
, 1, 2))) __attribute__ ((warn_unused_result
));
378 char *EXPORT_FUNC(grub_xvasprintf
) (const char *fmt
, va_list args
) __attribute__ ((warn_unused_result
));
379 void EXPORT_FUNC(grub_exit
) (void) __attribute__ ((noreturn
));
380 void EXPORT_FUNC(grub_abort
) (void) __attribute__ ((noreturn
));
381 grub_uint64_t
EXPORT_FUNC(grub_divmod64
) (grub_uint64_t n
,
385 #if !defined(GRUB_UTIL) && NEED_ENABLE_EXECUTE_STACK
386 void EXPORT_FUNC(__enable_execute_stack
) (void *addr
);
389 #if !defined(GRUB_UTIL) && NEED_REGISTER_FRAME_INFO
390 void EXPORT_FUNC (__register_frame_info
) (void);
391 void EXPORT_FUNC (__deregister_frame_info
) (void);
394 /* Inline functions. */
397 grub_memchr (const void *p
, int c
, grub_size_t len
)
400 const char *e
= s
+ len
;
410 static inline unsigned int
414 return (unsigned int) (-x
);
416 return (unsigned int) x
;
419 /* Rounded-up division */
420 static inline unsigned int
421 grub_div_roundup (unsigned int x
, unsigned int y
)
423 return (x
+ y
- 1) / y
;
426 /* Reboot the machine. */
427 #if defined (GRUB_MACHINE_EMU) || defined (GRUB_MACHINE_QEMU_MIPS)
428 void EXPORT_FUNC(grub_reboot
) (void) __attribute__ ((noreturn
));
430 void grub_reboot (void) __attribute__ ((noreturn
));
433 #ifdef GRUB_MACHINE_PCBIOS
434 /* Halt the system, using APM if possible. If NO_APM is true, don't
435 * use APM even if it is available. */
436 void grub_halt (int no_apm
) __attribute__ ((noreturn
));
437 #elif defined (__mips__)
438 void EXPORT_FUNC (grub_halt
) (void) __attribute__ ((noreturn
));
440 void grub_halt (void) __attribute__ ((noreturn
));
443 #ifdef GRUB_MACHINE_EMU
444 /* Flag to control module autoloading in normal mode. */
445 extern int EXPORT_VAR(grub_no_autoload
);
447 #define grub_no_autoload 0
451 grub_error_save (struct grub_error_saved
*save
)
453 grub_memcpy (save
->errmsg
, grub_errmsg
, sizeof (save
->errmsg
));
454 save
->grub_errno
= grub_errno
;
455 grub_errno
= GRUB_ERR_NONE
;
459 grub_error_load (const struct grub_error_saved
*save
)
461 grub_memcpy (grub_errmsg
, save
->errmsg
, sizeof (grub_errmsg
));
462 grub_errno
= save
->grub_errno
;
465 #endif /* ! GRUB_MISC_HEADER */