2002-12-27 08:53:07 +00:00
|
|
|
/* misc.h - prototypes for misc functions */
|
|
|
|
/*
|
2004-04-04 13:46:03 +00:00
|
|
|
* GRUB -- GRand Unified Bootloader
|
2010-01-03 21:50:53 +00:00
|
|
|
* Copyright (C) 2002,2003,2005,2006,2007,2008,2009,2010 Free Software Foundation, Inc.
|
2002-12-27 08:53:07 +00:00
|
|
|
*
|
2007-07-21 23:32:33 +00:00
|
|
|
* GRUB is free software: you can redistribute it and/or modify
|
2002-12-27 08:53:07 +00:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
2007-07-21 23:32:33 +00:00
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
2002-12-27 08:53:07 +00:00
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
2007-07-21 23:32:33 +00:00
|
|
|
* GRUB is distributed in the hope that it will be useful,
|
2002-12-27 08:53:07 +00:00
|
|
|
* 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
|
2007-07-21 23:32:33 +00:00
|
|
|
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
|
2002-12-27 08:53:07 +00:00
|
|
|
*/
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
#ifndef GRUB_MISC_HEADER
|
|
|
|
#define GRUB_MISC_HEADER 1
|
2002-12-27 08:53:07 +00:00
|
|
|
|
|
|
|
#include <stdarg.h>
|
2004-04-04 13:46:03 +00:00
|
|
|
#include <grub/types.h>
|
|
|
|
#include <grub/symbol.h>
|
|
|
|
#include <grub/err.h>
|
2012-02-12 14:25:25 +00:00
|
|
|
#include <grub/i18n.h>
|
2002-12-27 08:53:07 +00:00
|
|
|
|
2010-01-03 21:50:53 +00:00
|
|
|
/* GCC version checking borrowed from glibc. */
|
|
|
|
#if defined(__GNUC__) && defined(__GNUC_MINOR__)
|
|
|
|
# define GNUC_PREREQ(maj,min) \
|
|
|
|
((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
|
|
|
|
#else
|
|
|
|
# define GNUC_PREREQ(maj,min) 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Does this compiler support compile-time error attributes? */
|
|
|
|
#if GNUC_PREREQ(4,3)
|
|
|
|
# define ATTRIBUTE_ERROR(msg) \
|
|
|
|
__attribute__ ((__error__ (msg)))
|
|
|
|
#else
|
2012-05-28 15:45:53 +00:00
|
|
|
# define ATTRIBUTE_ERROR(msg) __attribute__ ((noreturn))
|
2010-01-03 21:50:53 +00:00
|
|
|
#endif
|
|
|
|
|
2013-10-13 19:17:54 +00:00
|
|
|
#if GNUC_PREREQ(4,4)
|
|
|
|
# define GNU_PRINTF gnu_printf
|
|
|
|
#else
|
|
|
|
# define GNU_PRINTF printf
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2009-07-23 16:41:29 +00:00
|
|
|
#define ALIGN_UP(addr, align) \
|
|
|
|
((addr + (typeof (addr)) align - 1) & ~((typeof (addr)) align - 1))
|
2012-03-05 00:17:55 +00:00
|
|
|
#define ALIGN_UP_OVERHEAD(addr, align) ((-(addr)) & ((typeof (addr)) (align) - 1))
|
2010-01-09 23:30:33 +00:00
|
|
|
#define ALIGN_DOWN(addr, align) \
|
|
|
|
((addr) & ~((typeof (addr)) align - 1))
|
2009-04-07 00:48:57 +00:00
|
|
|
#define ARRAY_SIZE(array) (sizeof (array) / sizeof (array[0]))
|
2009-10-26 18:04:37 +00:00
|
|
|
#define COMPILE_TIME_ASSERT(cond) switch (0) { case 1: case !(cond): ; }
|
2007-02-21 23:22:20 +00:00
|
|
|
|
2010-02-09 14:32:42 +00:00
|
|
|
#define grub_dprintf(condition, fmt, args...) grub_real_dprintf(GRUB_FILE, __LINE__, condition, fmt, ## args)
|
2003-01-06 00:01:35 +00:00
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
void *EXPORT_FUNC(grub_memmove) (void *dest, const void *src, grub_size_t n);
|
|
|
|
char *EXPORT_FUNC(grub_strcpy) (char *dest, const char *src);
|
|
|
|
char *EXPORT_FUNC(grub_strncpy) (char *dest, const char *src, int c);
|
2012-02-26 22:55:18 +00:00
|
|
|
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;
|
|
|
|
}
|
2009-08-24 19:40:40 +00:00
|
|
|
|
2011-10-23 21:25:06 +00:00
|
|
|
/* 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);
|
|
|
|
}
|
|
|
|
|
2009-08-24 19:40:40 +00:00
|
|
|
static inline char *
|
|
|
|
grub_strcat (char *dest, const char *src)
|
|
|
|
{
|
|
|
|
char *p = dest;
|
|
|
|
|
|
|
|
while (*p)
|
|
|
|
p++;
|
|
|
|
|
|
|
|
while ((*p = *src) != '\0')
|
|
|
|
{
|
|
|
|
p++;
|
|
|
|
src++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return dest;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline char *
|
|
|
|
grub_strncat (char *dest, const char *src, int c)
|
|
|
|
{
|
|
|
|
char *p = dest;
|
|
|
|
|
|
|
|
while (*p)
|
|
|
|
p++;
|
|
|
|
|
2011-11-09 13:47:45 +00:00
|
|
|
while (c-- && (*p = *src) != '\0')
|
2009-08-24 19:40:40 +00:00
|
|
|
{
|
|
|
|
p++;
|
|
|
|
src++;
|
|
|
|
}
|
|
|
|
|
|
|
|
*p = '\0';
|
|
|
|
|
|
|
|
return dest;
|
|
|
|
}
|
2003-10-29 18:44:30 +00:00
|
|
|
|
2005-01-29 22:01:54 +00:00
|
|
|
/* Prototypes for aliases. */
|
2009-10-28 22:55:27 +00:00
|
|
|
#ifndef GRUB_UTIL
|
2012-05-28 15:37:18 +00:00
|
|
|
#ifdef __APPLE__
|
|
|
|
int __attribute__ ((regparm(0))) EXPORT_FUNC(memcmp) (const void *s1, const void *s2, grub_size_t n);
|
|
|
|
void *__attribute__ ((regparm(0))) EXPORT_FUNC(memmove) (void *dest, const void *src, grub_size_t n);
|
|
|
|
void *__attribute__ ((regparm(0))) EXPORT_FUNC(memcpy) (void *dest, const void *src, grub_size_t n);
|
|
|
|
void *__attribute__ ((regparm(0))) EXPORT_FUNC(memset) (void *s, int c, grub_size_t n);
|
|
|
|
#else
|
2009-10-24 23:26:42 +00:00
|
|
|
int EXPORT_FUNC(memcmp) (const void *s1, const void *s2, grub_size_t n);
|
2005-01-29 22:01:54 +00:00
|
|
|
void *EXPORT_FUNC(memmove) (void *dest, const void *src, grub_size_t n);
|
|
|
|
void *EXPORT_FUNC(memcpy) (void *dest, const void *src, grub_size_t n);
|
2009-10-25 15:14:25 +00:00
|
|
|
void *EXPORT_FUNC(memset) (void *s, int c, grub_size_t n);
|
2009-06-04 21:17:05 +00:00
|
|
|
#endif
|
2012-05-28 15:37:18 +00:00
|
|
|
#endif
|
2003-10-29 18:44:30 +00:00
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
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);
|
2005-08-12 19:53:33 +00:00
|
|
|
int EXPORT_FUNC(grub_strncmp) (const char *s1, const char *s2, grub_size_t n);
|
2009-08-24 19:40:40 +00:00
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
char *EXPORT_FUNC(grub_strchr) (const char *s, int c);
|
|
|
|
char *EXPORT_FUNC(grub_strrchr) (const char *s, int c);
|
2005-05-09 01:47:37 +00:00
|
|
|
int EXPORT_FUNC(grub_strword) (const char *s, const char *w);
|
2011-11-11 19:02:51 +00:00
|
|
|
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
int EXPORT_FUNC(grub_isspace) (int c);
|
|
|
|
int EXPORT_FUNC(grub_isprint) (int c);
|
2009-08-24 19:40:40 +00:00
|
|
|
|
2009-11-20 14:09:48 +00:00
|
|
|
static inline int
|
|
|
|
grub_iscntrl (int c)
|
|
|
|
{
|
|
|
|
return (c >= 0x00 && c <= 0x1F) || c == 0x7F;
|
|
|
|
}
|
|
|
|
|
2009-08-24 19:40:40 +00:00
|
|
|
static inline int
|
|
|
|
grub_isalpha (int c)
|
|
|
|
{
|
|
|
|
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
|
|
|
|
}
|
|
|
|
|
2011-12-12 23:50:49 +00:00
|
|
|
static inline int
|
|
|
|
grub_islower (int c)
|
|
|
|
{
|
|
|
|
return (c >= 'a' && c <= 'z');
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int
|
|
|
|
grub_isupper (int c)
|
|
|
|
{
|
|
|
|
return (c >= 'A' && c <= 'Z');
|
|
|
|
}
|
|
|
|
|
2009-08-24 19:40:40 +00:00
|
|
|
static inline int
|
|
|
|
grub_isgraph (int c)
|
|
|
|
{
|
|
|
|
return (c >= '!' && c <= '~');
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int
|
|
|
|
grub_isdigit (int c)
|
|
|
|
{
|
|
|
|
return (c >= '0' && c <= '9');
|
|
|
|
}
|
|
|
|
|
2011-10-14 17:16:37 +00:00
|
|
|
static inline int
|
|
|
|
grub_isxdigit (int c)
|
|
|
|
{
|
|
|
|
return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
|
|
|
|
}
|
|
|
|
|
2009-11-20 14:09:48 +00:00
|
|
|
static inline int
|
|
|
|
grub_isalnum (int c)
|
|
|
|
{
|
|
|
|
return grub_isalpha (c) || grub_isdigit (c);
|
|
|
|
}
|
|
|
|
|
2009-08-24 19:40:40 +00:00
|
|
|
static inline int
|
|
|
|
grub_tolower (int c)
|
|
|
|
{
|
|
|
|
if (c >= 'A' && c <= 'Z')
|
|
|
|
return c - 'A' + 'a';
|
|
|
|
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2009-06-15 22:57:39 +00:00
|
|
|
static inline int
|
|
|
|
grub_toupper (int c)
|
|
|
|
{
|
|
|
|
if (c >= 'a' && c <= 'z')
|
|
|
|
return c - 'a' + 'A';
|
|
|
|
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2009-08-24 19:40:40 +00:00
|
|
|
static inline int
|
|
|
|
grub_strcasecmp (const char *s1, const char *s2)
|
|
|
|
{
|
|
|
|
while (*s1 && *s2)
|
|
|
|
{
|
2012-05-04 10:08:22 +00:00
|
|
|
if (grub_tolower ((grub_uint8_t) *s1)
|
|
|
|
!= grub_tolower ((grub_uint8_t) *s2))
|
2009-08-24 19:40:40 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
s1++;
|
|
|
|
s2++;
|
|
|
|
}
|
|
|
|
|
2012-05-04 10:08:22 +00:00
|
|
|
return (int) grub_tolower ((grub_uint8_t) *s1)
|
|
|
|
- (int) grub_tolower ((grub_uint8_t) *s2);
|
2009-08-24 19:40:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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++;
|
|
|
|
}
|
|
|
|
|
2012-05-04 10:08:22 +00:00
|
|
|
return (int) grub_tolower ((grub_uint8_t) *s1)
|
|
|
|
- (int) grub_tolower ((grub_uint8_t) *s2);
|
2009-08-24 19:40:40 +00:00
|
|
|
}
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
unsigned long EXPORT_FUNC(grub_strtoul) (const char *str, char **end, int base);
|
2006-06-04 Yoshinori K. Okuji <okuji@enbug.org>
Clean up the code to support 64-bit addressing in disks and
files. This change is not enough for filesystems yet.
* util/i386/pc/grub-setup.c (struct boot_blocklist): Change the
type of "start" to grub_uint64_t.
(setup): Change the types of KERNEL_SECTOR and FIRST_SECTOR to
grub_disk_addr_t * and grub_disk_addr_t. Fix the format string in
save_first_sector and save_blocklists. Use grub_le_to_cpu64 to
convert addresses.
* util/i386/pc/biosdisk.c (open_device): Change the type of SECTOR
to grub_disk_addr_t.
* partmap/gpt.c (gpt_partition_map_iterate): Fix the format
string.
* partmap/pc.c (pc_partition_map_iterate): Likewise.
* partmap/amiga.c (amiga_partition_map_iterate): Cast RDSK.MAGIC
to char *.
* normal/script.c (grub_script_parse): Remove unused MEMFREE.
* normal/parser.y (YYLTYPE_IS_TRIVIAL): New macro.
* normal/lexer.c (grub_script_yyerror): Specify unused to LEX.
* loader/i386/pc/multiboot.c (grub_multiboot_load_elf64): Cast -1
to grub_off_t, to detect an error from grub_file_seek.
(grub_multiboot_load_elf32): Likewise.
* kern/misc.c (grub_strtoul): Use grub_strtoull. Return the
maximum unsigned long value when an overflow is detected.
(grub_strtoull): New function.
(grub_divmod64): Likewise.
(grub_lltoa): use grub_divmod64.
* kern/fs.c (struct grub_fs_block): Change the type of "offset" to
grub_disk_addr_t.
(grub_fs_blocklist_open): Increase P if P is not NULL to advance
the pointer to next character. Use grub_strtoull instead of
grub_strtoul.
(grub_fs_blocklist_read): Change the types of SECTOR, OFFSET and
SIZE to grub_disk_addr_t, grub_off_t and grub_size_t,
respectively.
* kern/file.c (grub_file_read): Prevent an oveflow of LEN, as the
return value is signed.
(grub_file_seek): Change the type of OLD to grub_off_t. Do not
test if OFFSET is less than zero, as OFFSET is unsigned now.
* kern/disk.c (struct grub_disk_cache): Change the type of
"sector" to grub_disk_addr_t.
(grub_disk_cache_get_index): Change the type of SECTOR to
grub_disk_addr_t. Calculate the hash with SECTOR casted to
unsigned after shifting.
(grub_disk_cache_invalidate): Change the type of SECTOR to
grub_disk_addr_t.
(grub_disk_cache_unlock): Likewise.
(grub_disk_cache_store): Likewise.
(grub_disk_check_range): Change the types of SECTOR, OFFSET, SIZE,
START and LEN to grub_disk_addr_t *, grub_off_t *, grub_size_t,
grub_disk_addr_t and grub_uint64_t, respectively.
(grub_disk_read): Use an unsigned variable REAL_OFFSET for the
body, as the value of OFFSET is tweaked by
grub_disk_check_range. Change the types of START_SECTOR, LEN and
POS to grub_disk_addr_t, grub_size_t and grub_size_t,
respectively.
(grub_disk_write): Use an unsigned variable REAL_OFFSET for the
body, as the value of OFFSET is tweaked by
grub_disk_check_range. Change the types of LEN and N to
grub_size_t.
* io/gzio.c (struct grub_gzio): Change the types of "data_offset"
and "saved_offset" to grub_off_t.
(test_header): Cast BUF to char *.
(get_byte): Cast GZIO->DATA_OFFSET to grub_off_t. Cast GZIO->INBUF
to char *.
(grub_gzio_read): Change the types of OFFSET and SIZE to
grub_off_t and grub_size_t, respectively.
* include/grub/i386/pc/boot.h (GRUB_BOOT_MACHINE_FORCE_LBA):
Removed.
(GRUB_BOOT_MACHINE_BOOT_DRIVE): Changed to 0x4c.
(GRUB_BOOT_MACHINE_KERNEL_ADDRESS): Changed to 0x40.
(GRUB_BOOT_MACHINE_KERNEL_SEGMENT): Changed to 0x42.
(GRUB_BOOT_MACHINE_DRIVE_CHECK): Changed to 0x4e.
(GRUB_BOOT_MACHINE_LIST_SIZE): Increased to 12.
* include/grub/types.h (grub_off_t): Unconditionally set to
grub_uint64_t.
(grub_disk_addr_t): Changed to grub_uint64_t.
* include/grub/partition.h (struct grub_partition): Change the
types of "start", "len" and "offset" to grub_disk_addr_t,
grub_uint64_t and grub_disk_addr_t, respectively.
(grub_partition_get_start): Return grub_disk_addr_t.
(grub_partition_get_len): Return grub_uint64_t.
* include/grub/misc.h (grub_strtoull): New prototype.
(grub_divmod64): Likewise.
* include/grub/fshelp.h (grub_fshelp_read_file): Change the types
of SECTOR, LEN and FILESIZE to grub_disk_addr_t, grub_size_t and
grub_off_t, respectively.
All callers and references changed.
* include/grub/fs.h (struct grub_fs): Change the type of LEN to
grub_size_t in "read".
All callers and references changed.
* include/grub/file.h (struct grub_file): Change the types of
"offset" and "size" to grub_off_t and grub_off_t,
respectively. Change the type of SECTOR to grub_disk_addr_t in
"read_hook".
(grub_file_read): Change the type of LEN to grub_size_t.
(grub_file_seek): Return grub_off_t. Change the type of OFFSET to
grub_off_t.
(grub_file_size): Return grub_off_t.
(grub_file_tell): Likewise.
All callers and references changed.
* include/grub/disk.h (struct grub_disk_dev): Change the types of
SECTOR and SIZE to grub_disk_addr_t and grub_size_t in "read" and
"write".
(struct grub_disk): Change the type of "total_sectors" to
grub_uint64_t. Change the type of SECTOR to grub_disk_addr_t in
"read_hook".
(grub_disk_read): Change the types of SECTOR, OFFSET and SIZE to
grub_disk_addr_t, grub_off_t and grub_size_t, respectively.
(grub_disk_write): Likewise.
All callers and references changed.
* fs/iso9660.c (grub_iso9660_susp_iterate): Cast parameters to
char * for grub_strncmp to silence gcc.
(grub_iso9660_mount): Likewise.
(grub_iso9660_mount): Likewise.
(grub_iso9660_read_symlink): Likewise. Also, remove the nonsense
return statement.
(grub_iso9660_iterate_dir): Likewise.
(grub_iso9660_label): Cast DATA->VOLDESC.VOLNAME to char *.
* fs/hfs.c (grub_hfs_read_file): Change the types of SECTOR and
LEN to grub_disk_addr_t and grub_size_t, respectively.
* fs/hfsplus.c (grub_hfsplus_read_file): Likewise.
* fs/jfs.c (grub_jfs_read_file): Likewise.
* fs/minix.c (grub_jfs_read_file): Likewise.
* fs/sfs.c (grub_jfs_read_file): Likewise.
* fs/ufs.c (grub_jfs_read_file): Likewise.
* fs/xfs.c (grub_jfs_read_file): Likewise.
* fs/fat.c (grub_fat_read_data): Change the types of SECTOR, LEN
and SIZE to grub_disk_addr_t, grub_size_t and grub_size_t,
respectively.
* fs/ext2.c (grub_ext2_read_block): When an error happens, set
BLKNR to -1 instead of returning GRUB_ERRNO.
(grub_ext2_read_file): Change the types of SECTOR and
LEN to grub_disk_addr_t and grub_size_t, respectively.
* fs/affs.c (grub_affs_read_file): Change the types of SECTOR and
LEN to grub_disk_addr_t and grub_size_t, respectively.
* font/manager.c (grub_font_get_glyph): Cast BITMAP to char * for
grub_file_read.
* disk/ieee1275/ofdisk.c (grub_ofdisk_read): Fix the format
string. Do not cast SECTOR explicitly.
* disk/i386/pc/biosdisk.c (grub_biosdisk_open): Change the type of
TOTAL_SECTORS to grub_uint64_t. Do not mask DRP->TOTAL_SECTORS.
(grub_biosdisk_rw): Change the types of SECTOR and SIZE to
grub_disk_addr_t and grub_size_t, respectively. If the sector is
over 2TB and LBA mode is not supported, raise an error.
(get_safe_sectors): New function.
(grub_biosdisk_read): Use get_safe_sectors.
(grub_biosdisk_write): Likewise.
* disk/efi/efidisk.c (grub_efidisk_read): Fix the format string.
(grub_efidisk_write): Likewise.
* disk/loopback.c (delete_loopback): Cosmetic changes.
(grub_cmd_loopback): Likewise. Also, test NEWDEV->FILENAME
correctly.
(grub_loopback_open): Likewise.
(grub_loopback_read): Likewise. Also, change the type of POS to
grub_off_t, and fix the usage of grub_memset.
* commands/i386/pc/play.c: Include grub/machine/time.h.
* commands/ls.c (grub_ls_list_files): Use "llu" instead of "d" to
print FILE->SIZE.
* commands/configfile.c: Include grub/env.h.
* commands/cmp.c (grub_cmd_cmp): Do not use ERR, but use
GRUB_ERRNO directly instead. Change the type of POS to
grub_off_t. Follow the coding standard.
* commands/blocklist.c: Include grub/partition.h.
(grub_cmd_blocklist): Return an error if the underlying device is
not a disk. Take the starting sector of a partition into account,
if a partition is used.
* boot/i386/pc/diskboot.S (bootloop): Adapted to the new offset of
a length field.
(lba_mode): Support 64-bit addresses.
(chs_mode): Likewise.
(copy_buffer): Adapted to the new offsets of a length field and a
segment field.
(blocklist_default_start): Allocate 64-bit space.
* boot/i386/pc/boot.S (force_lba): Removed.
(boot_drive): Moved to under KERNEL_SECTOR.
(kernel_sector): Moved to under KENREL_SEGMENT. Allocate 64-bit
space.
(real_start): Set %si earlier. Remove code for FORCE_LBA, since it
is useless.
(lba_mode): Refactored to support a 64-bit address. More size
optimization.
(setup_sectors): Likewise.
2006-06-04 15:56:55 +00:00
|
|
|
unsigned long long EXPORT_FUNC(grub_strtoull) (const char *str, char **end, int base);
|
2009-11-20 14:09:48 +00:00
|
|
|
|
|
|
|
static inline long
|
|
|
|
grub_strtol (const char *str, char **end, int base)
|
|
|
|
{
|
|
|
|
int negative = 0;
|
|
|
|
unsigned long magnitude;
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2012-02-12 14:25:25 +00:00
|
|
|
grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow is detected"));
|
2009-11-20 14:09:48 +00:00
|
|
|
return GRUB_LONG_MIN;
|
|
|
|
}
|
|
|
|
return -((long) magnitude);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (magnitude > GRUB_LONG_MAX)
|
|
|
|
{
|
2012-02-12 14:25:25 +00:00
|
|
|
grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow is detected"));
|
2009-11-20 14:09:48 +00:00
|
|
|
return GRUB_LONG_MAX;
|
|
|
|
}
|
|
|
|
return (long) magnitude;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-30 09:27:02 +00:00
|
|
|
char *EXPORT_FUNC(grub_strdup) (const char *s) __attribute__ ((warn_unused_result));
|
|
|
|
char *EXPORT_FUNC(grub_strndup) (const char *s, grub_size_t n) __attribute__ ((warn_unused_result));
|
2004-04-04 13:46:03 +00:00
|
|
|
void *EXPORT_FUNC(grub_memset) (void *s, int c, grub_size_t n);
|
2010-07-30 09:27:02 +00:00
|
|
|
grub_size_t EXPORT_FUNC(grub_strlen) (const char *s) __attribute__ ((warn_unused_result));
|
2013-10-13 19:17:54 +00:00
|
|
|
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)));
|
2010-03-15 10:49:27 +00:00
|
|
|
|
2011-12-12 23:50:49 +00:00
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2010-03-15 10:49:27 +00:00
|
|
|
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. */
|
|
|
|
}
|
|
|
|
|
2009-12-13 19:51:08 +00:00
|
|
|
int EXPORT_FUNC(grub_puts_) (const char *s);
|
2005-05-09 01:47:37 +00:00
|
|
|
void EXPORT_FUNC(grub_real_dprintf) (const char *file,
|
|
|
|
const int line,
|
|
|
|
const char *condition,
|
2013-10-13 19:17:54 +00:00
|
|
|
const char *fmt, ...) __attribute__ ((format (GNU_PRINTF, 4, 5)));
|
2004-04-04 13:46:03 +00:00
|
|
|
int EXPORT_FUNC(grub_vprintf) (const char *fmt, va_list args);
|
2009-12-29 09:04:06 +00:00
|
|
|
int EXPORT_FUNC(grub_snprintf) (char *str, grub_size_t n, const char *fmt, ...)
|
2013-10-13 19:17:54 +00:00
|
|
|
__attribute__ ((format (GNU_PRINTF, 3, 4)));
|
2009-12-29 09:04:06 +00:00
|
|
|
int EXPORT_FUNC(grub_vsnprintf) (char *str, grub_size_t n, const char *fmt,
|
|
|
|
va_list args);
|
2010-01-20 08:12:47 +00:00
|
|
|
char *EXPORT_FUNC(grub_xasprintf) (const char *fmt, ...)
|
2013-10-13 19:17:54 +00:00
|
|
|
__attribute__ ((format (GNU_PRINTF, 1, 2))) __attribute__ ((warn_unused_result));
|
2010-07-30 09:27:02 +00:00
|
|
|
char *EXPORT_FUNC(grub_xvasprintf) (const char *fmt, va_list args) __attribute__ ((warn_unused_result));
|
2006-04-23 13:37:36 +00:00
|
|
|
void EXPORT_FUNC(grub_exit) (void) __attribute__ ((noreturn));
|
|
|
|
void EXPORT_FUNC(grub_abort) (void) __attribute__ ((noreturn));
|
2011-05-18 13:35:19 +00:00
|
|
|
grub_uint64_t EXPORT_FUNC(grub_divmod64) (grub_uint64_t n,
|
|
|
|
grub_uint64_t d,
|
|
|
|
grub_uint64_t *r);
|
2004-08-28 13:14:29 +00:00
|
|
|
|
2012-02-10 11:45:32 +00:00
|
|
|
#if !defined(GRUB_UTIL) && NEED_REGISTER_FRAME_INFO
|
2010-04-11 19:24:21 +00:00
|
|
|
void EXPORT_FUNC (__register_frame_info) (void);
|
|
|
|
void EXPORT_FUNC (__deregister_frame_info) (void);
|
|
|
|
#endif
|
|
|
|
|
2006-03-14 19:08:34 +00:00
|
|
|
/* Inline functions. */
|
2007-10-22 19:59:33 +00:00
|
|
|
|
2011-07-23 01:49:02 +00:00
|
|
|
static inline char *
|
|
|
|
grub_memchr (const void *p, int c, grub_size_t len)
|
|
|
|
{
|
2013-05-07 09:30:48 +00:00
|
|
|
const char *s = (const char *) p;
|
2011-07-23 01:49:02 +00:00
|
|
|
const char *e = s + len;
|
|
|
|
|
|
|
|
for (; s < e; s++)
|
|
|
|
if (*s == c)
|
|
|
|
return (char *) s;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-03-14 19:08:34 +00:00
|
|
|
static inline unsigned int
|
|
|
|
grub_abs (int x)
|
|
|
|
{
|
|
|
|
if (x < 0)
|
2006-04-23 13:37:36 +00:00
|
|
|
return (unsigned int) (-x);
|
2006-03-14 19:08:34 +00:00
|
|
|
else
|
2006-04-23 13:37:36 +00:00
|
|
|
return (unsigned int) x;
|
2006-03-14 19:08:34 +00:00
|
|
|
}
|
|
|
|
|
2007-10-22 19:59:33 +00:00
|
|
|
/* Rounded-up division */
|
|
|
|
static inline unsigned int
|
|
|
|
grub_div_roundup (unsigned int x, unsigned int y)
|
|
|
|
{
|
2007-11-05 14:54:00 +00:00
|
|
|
return (x + y - 1) / y;
|
2007-10-22 19:59:33 +00:00
|
|
|
}
|
|
|
|
|
2009-12-03 23:07:29 +00:00
|
|
|
/* Reboot the machine. */
|
2011-12-13 13:51:41 +00:00
|
|
|
#if defined (GRUB_MACHINE_EMU) || defined (GRUB_MACHINE_QEMU_MIPS)
|
|
|
|
void EXPORT_FUNC(grub_reboot) (void) __attribute__ ((noreturn));
|
|
|
|
#else
|
2011-10-19 14:53:18 +00:00
|
|
|
void grub_reboot (void) __attribute__ ((noreturn));
|
2011-12-13 13:51:41 +00:00
|
|
|
#endif
|
2009-12-03 23:07:29 +00:00
|
|
|
|
|
|
|
#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. */
|
2010-08-30 14:25:39 +00:00
|
|
|
void grub_halt (int no_apm) __attribute__ ((noreturn));
|
2011-12-13 13:51:41 +00:00
|
|
|
#elif defined (__mips__)
|
|
|
|
void EXPORT_FUNC (grub_halt) (void) __attribute__ ((noreturn));
|
2009-12-03 23:07:29 +00:00
|
|
|
#else
|
2010-08-30 14:25:39 +00:00
|
|
|
void grub_halt (void) __attribute__ ((noreturn));
|
2009-12-03 23:07:29 +00:00
|
|
|
#endif
|
|
|
|
|
2010-06-02 09:15:07 +00:00
|
|
|
#ifdef GRUB_MACHINE_EMU
|
2013-04-27 00:00:16 +00:00
|
|
|
/* Flag to check if module loading is available. */
|
|
|
|
extern const int EXPORT_VAR(grub_no_modules);
|
2010-06-02 09:15:07 +00:00
|
|
|
#else
|
2013-04-27 00:00:16 +00:00
|
|
|
#define grub_no_modules 0
|
2010-06-02 09:15:07 +00:00
|
|
|
#endif
|
|
|
|
|
2012-02-05 09:24:53 +00:00
|
|
|
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;
|
2012-02-12 16:21:09 +00:00
|
|
|
grub_errno = GRUB_ERR_NONE;
|
2012-02-05 09:24:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-03-19 19:25:09 +00:00
|
|
|
#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,
|
2013-10-13 19:17:54 +00:00
|
|
|
const char *fmt, ...) __attribute__ ((format (GNU_PRINTF, 3, 4)));
|
2013-03-19 19:25:09 +00:00
|
|
|
#define grub_boot_time(fmt, args...) grub_real_boot_time(GRUB_FILE, __LINE__, fmt, ## args)
|
|
|
|
#else
|
|
|
|
#define grub_boot_time(fmt, args...)
|
|
|
|
#endif
|
|
|
|
|
2013-04-03 07:20:29 +00:00
|
|
|
#define grub_max(a, b) (((a) > (b)) ? (a) : (b))
|
|
|
|
#define grub_min(a, b) (((a) < (b)) ? (a) : (b))
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
#endif /* ! GRUB_MISC_HEADER */
|