misc.h 11.6 KB
Newer Older
okuji's avatar
okuji committed
1 2
/* 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.
okuji's avatar
okuji committed
5
 *
6
 *  GRUB is free software: you can redistribute it and/or modify
okuji's avatar
okuji committed
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
okuji's avatar
okuji committed
9 10
 *  (at your option) any later version.
 *
11
 *  GRUB is distributed in the hope that it will be useful,
okuji's avatar
okuji committed
12 13 14 15 16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
17
 *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
okuji's avatar
okuji committed
18 19
 */

20 21
#ifndef GRUB_MISC_HEADER
#define GRUB_MISC_HEADER	1
okuji's avatar
okuji committed
22 23

#include <stdarg.h>
24 25 26
#include <grub/types.h>
#include <grub/symbol.h>
#include <grub/err.h>
27
#include <grub/i18n.h>
28
#include <grub/compiler.h>
29

30 31
#define ALIGN_UP(addr, align) \
	((addr + (typeof (addr)) align - 1) & ~((typeof (addr)) align - 1))
32
#define ALIGN_UP_OVERHEAD(addr, align) ((-(addr)) & ((typeof (addr)) (align) - 1))
33 34
#define ALIGN_DOWN(addr, align) \
	((addr) & ~((typeof (addr)) align - 1))
35
#define ARRAY_SIZE(array) (sizeof (array) / sizeof (array[0]))
36
#define COMPILE_TIME_ASSERT(cond) switch (0) { case 1: case !(cond): ; }
37

38
#define grub_dprintf(condition, ...) grub_real_dprintf(GRUB_FILE, __LINE__, condition, __VA_ARGS__)
39

40 41
void *EXPORT_FUNC(grub_memmove) (void *dest, const void *src, grub_size_t n);
char *EXPORT_FUNC(grub_strcpy) (char *dest, const char *src);
42 43 44 45 46 47 48 49 50 51 52 53

static inline char *
grub_strncpy (char *dest, const char *src, int c)
{
  char *p = dest;

  while ((*p++ = *src++) != '\0' && --c)
    ;

  return dest;
}

54 55 56 57 58 59 60 61 62 63 64 65
static inline char *
grub_stpcpy (char *dest, const char *src)
{
  char *d = dest;
  const char *s = src;

  do
    *d++ = *s;
  while (*s++ != '\0');

  return d - 1;
}
66

67 68 69 70 71 72 73
/* XXX: If grub_memmove is too slow, we must implement grub_memcpy.  */
static inline void *
grub_memcpy (void *dest, const void *src, grub_size_t n)
{
  return grub_memmove (dest, src, n);
}

74 75 76 77 78 79 80 81
#if defined(__x86_64__) && !defined (GRUB_UTIL)
#if defined (__MINGW32__) || defined (__CYGWIN__) || defined (__MINGW64__)
#define GRUB_ASM_ATTR __attribute__ ((sysv_abi))
#else
#define GRUB_ASM_ATTR
#endif
#endif

82 83
int EXPORT_FUNC(grub_memcmp) (const void *s1, const void *s2, grub_size_t n);
int EXPORT_FUNC(grub_strcmp) (const char *s1, const char *s2);
84
int EXPORT_FUNC(grub_strncmp) (const char *s1, const char *s2, grub_size_t n);
85

86 87
char *EXPORT_FUNC(grub_strchr) (const char *s, int c);
char *EXPORT_FUNC(grub_strrchr) (const char *s, int c);
88
int EXPORT_FUNC(grub_strword) (const char *s, const char *w);
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135

/* Copied from gnulib.
   Written by Bruno Haible <bruno@clisp.org>, 2005. */
static inline char *
grub_strstr (const char *haystack, const char *needle)
{
  /* Be careful not to look at the entire extent of haystack or needle
     until needed.  This is useful because of these two cases:
       - haystack may be very long, and a match of needle found early,
       - needle may be very long, and not even a short initial segment of
       needle may be found in haystack.  */
  if (*needle != '\0')
    {
      /* Speed up the following searches of needle by caching its first
	 character.  */
      char b = *needle++;

      for (;; haystack++)
	{
	  if (*haystack == '\0')
	    /* No match.  */
	    return 0;
	  if (*haystack == b)
	    /* The first character matches.  */
	    {
	      const char *rhaystack = haystack + 1;
	      const char *rneedle = needle;

	      for (;; rhaystack++, rneedle++)
		{
		  if (*rneedle == '\0')
		    /* Found a match.  */
		    return (char *) haystack;
		  if (*rhaystack == '\0')
		    /* No match.  */
		    return 0;
		  if (*rhaystack != *rneedle)
		    /* Nothing in this round.  */
		    break;
		}
	    }
	}
    }
  else
    return (char *) haystack;
}

136
int EXPORT_FUNC(grub_isspace) (int c);
137 138 139 140 141 142

static inline int
grub_isprint (int c)
{
  return (c >= ' ' && c <= '~');
}
143

144 145 146 147 148 149
static inline int
grub_iscntrl (int c)
{
  return (c >= 0x00 && c <= 0x1F) || c == 0x7F;
}

150 151 152 153 154 155
static inline int
grub_isalpha (int c)
{
  return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
}

156 157 158 159 160 161 162 163 164 165 166 167
static inline int
grub_islower (int c)
{
  return (c >= 'a' && c <= 'z');
}

static inline int
grub_isupper (int c)
{
  return (c >= 'A' && c <= 'Z');
}

168 169 170 171 172 173 174 175 176 177 178 179
static inline int
grub_isgraph (int c)
{
  return (c >= '!' && c <= '~');
}

static inline int
grub_isdigit (int c)
{
  return (c >= '0' && c <= '9');
}

180 181 182 183 184 185
static inline int
grub_isxdigit (int c)
{
  return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
}

186 187 188 189 190 191
static inline int
grub_isalnum (int c)
{
  return grub_isalpha (c) || grub_isdigit (c);
}

192 193 194 195 196 197 198 199 200
static inline int
grub_tolower (int c)
{
  if (c >= 'A' && c <= 'Z')
    return c - 'A' + 'a';

  return c;
}

201 202 203 204 205 206 207 208 209
static inline int
grub_toupper (int c)
{
  if (c >= 'a' && c <= 'z')
    return c - 'a' + 'A';

  return c;
}

210 211 212 213 214
static inline int
grub_strcasecmp (const char *s1, const char *s2)
{
  while (*s1 && *s2)
    {
215 216
      if (grub_tolower ((grub_uint8_t) *s1)
	  != grub_tolower ((grub_uint8_t) *s2))
217 218 219 220 221 222
	break;

      s1++;
      s2++;
    }

223 224
  return (int) grub_tolower ((grub_uint8_t) *s1)
    - (int) grub_tolower ((grub_uint8_t) *s2);
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
}

static inline int
grub_strncasecmp (const char *s1, const char *s2, grub_size_t n)
{
  if (n == 0)
    return 0;

  while (*s1 && *s2 && --n)
    {
      if (grub_tolower (*s1) != grub_tolower (*s2))
	break;

      s1++;
      s2++;
    }

242 243
  return (int) grub_tolower ((grub_uint8_t) *s1)
    - (int) grub_tolower ((grub_uint8_t) *s2);
244 245
}

246
unsigned long EXPORT_FUNC(grub_strtoul) (const char *str, char **end, int base);
247
unsigned long long EXPORT_FUNC(grub_strtoull) (const char *str, char **end, int base);
248 249 250 251 252

static inline long
grub_strtol (const char *str, char **end, int base)
{
  int negative = 0;
253
  unsigned long long magnitude;
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268

  while (*str && grub_isspace (*str))
    str++;

  if (*str == '-')
    {
      negative = 1;
      str++;
    }

  magnitude = grub_strtoull (str, end, base);
  if (negative)
    {
      if (magnitude > (unsigned long) GRUB_LONG_MAX + 1)
        {
269
          grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow is detected"));
270 271 272 273 274 275 276 277
          return GRUB_LONG_MIN;
        }
      return -((long) magnitude);
    }
  else
    {
      if (magnitude > GRUB_LONG_MAX)
        {
278
          grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow is detected"));
279 280 281 282 283 284
          return GRUB_LONG_MAX;
        }
      return (long) magnitude;
    }
}

285 286
char *EXPORT_FUNC(grub_strdup) (const char *s) WARN_UNUSED_RESULT;
char *EXPORT_FUNC(grub_strndup) (const char *s, grub_size_t n) WARN_UNUSED_RESULT;
287
void *EXPORT_FUNC(grub_memset) (void *s, int c, grub_size_t n);
288
grub_size_t EXPORT_FUNC(grub_strlen) (const char *s) WARN_UNUSED_RESULT;
289 290
int EXPORT_FUNC(grub_printf) (const char *fmt, ...) __attribute__ ((format (GNU_PRINTF, 1, 2)));
int EXPORT_FUNC(grub_printf_) (const char *fmt, ...) __attribute__ ((format (GNU_PRINTF, 1, 2)));
291

292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
/* Replace all `ch' characters of `input' with `with' and copy the
   result into `output'; return EOS address of `output'. */
static inline char *
grub_strchrsub (char *output, const char *input, char ch, const char *with)
{
  while (*input)
    {
      if (*input == ch)
	{
	  grub_strcpy (output, with);
	  output += grub_strlen (with);
	  input++;
	  continue;
	}
      *output++ = *input++;
    }
  *output = '\0';
  return output;
}

312 313 314 315 316 317 318 319 320 321 322 323
extern void (*EXPORT_VAR (grub_xputs)) (const char *str);

static inline int
grub_puts (const char *s)
{
  const char nl[2] = "\n";
  grub_xputs (s);
  grub_xputs (nl);

  return 1;	/* Cannot fail.  */
}

324
int EXPORT_FUNC(grub_puts_) (const char *s);
325 326 327
void EXPORT_FUNC(grub_real_dprintf) (const char *file,
                                     const int line,
                                     const char *condition,
328
                                     const char *fmt, ...) __attribute__ ((format (GNU_PRINTF, 4, 5)));
329
int EXPORT_FUNC(grub_vprintf) (const char *fmt, va_list args);
330
int EXPORT_FUNC(grub_snprintf) (char *str, grub_size_t n, const char *fmt, ...)
331
     __attribute__ ((format (GNU_PRINTF, 3, 4)));
332 333
int EXPORT_FUNC(grub_vsnprintf) (char *str, grub_size_t n, const char *fmt,
				 va_list args);
334
char *EXPORT_FUNC(grub_xasprintf) (const char *fmt, ...)
335 336
     __attribute__ ((format (GNU_PRINTF, 1, 2))) WARN_UNUSED_RESULT;
char *EXPORT_FUNC(grub_xvasprintf) (const char *fmt, va_list args) WARN_UNUSED_RESULT;
337
void EXPORT_FUNC(grub_exit) (void) __attribute__ ((noreturn));
338 339 340
grub_uint64_t EXPORT_FUNC(grub_divmod64) (grub_uint64_t n,
					  grub_uint64_t d,
					  grub_uint64_t *r);
341

342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
/* Must match softdiv group in gentpl.py.  */
#if !defined(GRUB_MACHINE_EMU) && (defined(__arm__) || defined(__ia64__))
#define GRUB_DIVISION_IN_SOFTWARE 1
#else
#define GRUB_DIVISION_IN_SOFTWARE 0
#endif

/* Some division functions need to be in kernel if compiler generates calls
   to them. Otherwise we still need them for consistent tests but they go
   into a separate module.  */
#if GRUB_DIVISION_IN_SOFTWARE
#define EXPORT_FUNC_IF_SOFTDIV EXPORT_FUNC
#else
#define EXPORT_FUNC_IF_SOFTDIV(x) x
#endif

grub_int64_t
EXPORT_FUNC_IF_SOFTDIV(grub_divmod64s) (grub_int64_t n,
					grub_int64_t d,
					grub_int64_t *r);
362

363 364 365 366 367 368 369 370 371 372
grub_uint32_t
EXPORT_FUNC_IF_SOFTDIV (grub_divmod32) (grub_uint32_t n,
					grub_uint32_t d,
					grub_uint32_t *r);

grub_int32_t
EXPORT_FUNC_IF_SOFTDIV (grub_divmod32s) (grub_int32_t n,
					 grub_int32_t d,
					 grub_int32_t *r);

373
/* Inline functions.  */
374

375 376 377
static inline char *
grub_memchr (const void *p, int c, grub_size_t len)
{
378
  const char *s = (const char *) p;
379 380 381 382 383 384 385 386 387 388
  const char *e = s + len;

  for (; s < e; s++)
    if (*s == c)
      return (char *) s;

  return 0;
}


389 390 391 392
static inline unsigned int
grub_abs (int x)
{
  if (x < 0)
393
    return (unsigned int) (-x);
394
  else
395
    return (unsigned int) x;
396 397
}

398
/* Reboot the machine.  */
399 400 401
#if defined (GRUB_MACHINE_EMU) || defined (GRUB_MACHINE_QEMU_MIPS)
void EXPORT_FUNC(grub_reboot) (void) __attribute__ ((noreturn));
#else
402
void grub_reboot (void) __attribute__ ((noreturn));
403
#endif
404

405 406 407 408
#if defined (__clang__) && !defined (GRUB_UTIL)
void __attribute__ ((noreturn)) EXPORT_FUNC (abort) (void);
#endif

409 410 411
#ifdef GRUB_MACHINE_PCBIOS
/* Halt the system, using APM if possible. If NO_APM is true, don't
 * use APM even if it is available.  */
412
void grub_halt (int no_apm) __attribute__ ((noreturn));
413
#elif defined (__mips__) && !defined (GRUB_MACHINE_EMU)
414
void EXPORT_FUNC (grub_halt) (void) __attribute__ ((noreturn));
415
#else
416
void grub_halt (void) __attribute__ ((noreturn));
417 418
#endif

BVK Chaitanya's avatar
BVK Chaitanya committed
419
#ifdef GRUB_MACHINE_EMU
420 421
/* Flag to check if module loading is available.  */
extern const int EXPORT_VAR(grub_no_modules);
BVK Chaitanya's avatar
BVK Chaitanya committed
422
#else
423
#define grub_no_modules 0
BVK Chaitanya's avatar
BVK Chaitanya committed
424 425
#endif

426 427 428 429 430
static inline void
grub_error_save (struct grub_error_saved *save)
{
  grub_memcpy (save->errmsg, grub_errmsg, sizeof (save->errmsg));
  save->grub_errno = grub_errno;
431
  grub_errno = GRUB_ERR_NONE;
432 433 434 435 436 437 438 439 440
}

static inline void
grub_error_load (const struct grub_error_saved *save)
{
  grub_memcpy (grub_errmsg, save->errmsg, sizeof (grub_errmsg));
  grub_errno = save->grub_errno;
}

441 442 443 444 445 446 447 448 449 450 451 452 453 454
#if BOOT_TIME_STATS
struct grub_boot_time
{
  struct grub_boot_time *next;
  grub_uint64_t tp;
  const char *file;
  int line;
  char *msg;
};

extern struct grub_boot_time *EXPORT_VAR(grub_boot_time_head);

void EXPORT_FUNC(grub_real_boot_time) (const char *file,
				       const int line,
455
				       const char *fmt, ...) __attribute__ ((format (GNU_PRINTF, 3, 4)));
456
#define grub_boot_time(...) grub_real_boot_time(GRUB_FILE, __LINE__, __VA_ARGS__)
457
#else
458
#define grub_boot_time(...)
459 460
#endif

461 462 463
#define grub_max(a, b) (((a) > (b)) ? (a) : (b))
#define grub_min(a, b) (((a) < (b)) ? (a) : (b))

464
#endif /* ! GRUB_MISC_HEADER */