]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - GRUB2/MOD_SRC/grub-2.04/include/grub/misc.h
1.1.07 release
[Ventoy.git] / GRUB2 / MOD_SRC / grub-2.04 / include / grub / misc.h
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
20 #ifndef GRUB_MISC_HEADER
21 #define GRUB_MISC_HEADER 1
22
23 #include <stdarg.h>
24 #include <grub/types.h>
25 #include <grub/symbol.h>
26 #include <grub/err.h>
27 #include <grub/i18n.h>
28 #include <grub/compiler.h>
29
30 #define ALIGN_UP(addr, align) \
31 ((addr + (typeof (addr)) align - 1) & ~((typeof (addr)) align - 1))
32 #define ALIGN_UP_OVERHEAD(addr, align) ((-(addr)) & ((typeof (addr)) (align) - 1))
33 #define ALIGN_DOWN(addr, align) \
34 ((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 void *EXPORT_FUNC(grub_memmove) (void *dest, const void *src, grub_size_t n);
41 char *EXPORT_FUNC(grub_strcpy) (char *dest, const char *src);
42
43 static inline char *
44 grub_strncpy (char *dest, const char *src, int c)
45 {
46 char *p = dest;
47
48 while ((*p++ = *src++) != '\0' && --c)
49 ;
50
51 return dest;
52 }
53
54 static inline char *
55 grub_stpcpy (char *dest, const char *src)
56 {
57 char *d = dest;
58 const char *s = src;
59
60 do
61 *d++ = *s;
62 while (*s++ != '\0');
63
64 return d - 1;
65 }
66
67 /* XXX: If grub_memmove is too slow, we must implement grub_memcpy. */
68 static inline void *
69 grub_memcpy (void *dest, const void *src, grub_size_t n)
70 {
71 return grub_memmove (dest, src, n);
72 }
73
74 #if defined(__x86_64__) && !defined (GRUB_UTIL)
75 #if defined (__MINGW32__) || defined (__CYGWIN__) || defined (__MINGW64__)
76 #define GRUB_ASM_ATTR __attribute__ ((sysv_abi))
77 #else
78 #define GRUB_ASM_ATTR
79 #endif
80 #endif
81
82 int EXPORT_FUNC(grub_memcmp) (const void *s1, const void *s2, grub_size_t n);
83 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 char *EXPORT_FUNC(grub_strchr) (const char *s, int c);
87 char *EXPORT_FUNC(grub_strrchr) (const char *s, int c);
88 int EXPORT_FUNC(grub_strword) (const char *s, const char *w);
89
90 /* Copied from gnulib.
91 Written by Bruno Haible <bruno@clisp.org>, 2005. */
92 static inline char *
93 grub_strstr (const char *haystack, const char *needle)
94 {
95 /* Be careful not to look at the entire extent of haystack or needle
96 until needed. This is useful because of these two cases:
97 - haystack may be very long, and a match of needle found early,
98 - needle may be very long, and not even a short initial segment of
99 needle may be found in haystack. */
100 if (*needle != '\0')
101 {
102 /* Speed up the following searches of needle by caching its first
103 character. */
104 char b = *needle++;
105
106 for (;; haystack++)
107 {
108 if (*haystack == '\0')
109 /* No match. */
110 return 0;
111 if (*haystack == b)
112 /* The first character matches. */
113 {
114 const char *rhaystack = haystack + 1;
115 const char *rneedle = needle;
116
117 for (;; rhaystack++, rneedle++)
118 {
119 if (*rneedle == '\0')
120 /* Found a match. */
121 return (char *) haystack;
122 if (*rhaystack == '\0')
123 /* No match. */
124 return 0;
125 if (*rhaystack != *rneedle)
126 /* Nothing in this round. */
127 break;
128 }
129 }
130 }
131 }
132 else
133 return (char *) haystack;
134 }
135
136 int EXPORT_FUNC(grub_isspace) (int c);
137
138 static inline int
139 grub_isprint (int c)
140 {
141 return (c >= ' ' && c <= '~');
142 }
143
144 static inline int
145 grub_iscntrl (int c)
146 {
147 return (c >= 0x00 && c <= 0x1F) || c == 0x7F;
148 }
149
150 static inline int
151 grub_isalpha (int c)
152 {
153 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
154 }
155
156 static inline int
157 grub_islower (int c)
158 {
159 return (c >= 'a' && c <= 'z');
160 }
161
162 static inline int
163 grub_isupper (int c)
164 {
165 return (c >= 'A' && c <= 'Z');
166 }
167
168 static inline int
169 grub_isgraph (int c)
170 {
171 return (c >= '!' && c <= '~');
172 }
173
174 static inline int
175 grub_isdigit (int c)
176 {
177 return (c >= '0' && c <= '9');
178 }
179
180 static inline int
181 grub_isxdigit (int c)
182 {
183 return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
184 }
185
186 static inline int
187 grub_isalnum (int c)
188 {
189 return grub_isalpha (c) || grub_isdigit (c);
190 }
191
192 static inline int
193 grub_tolower (int c)
194 {
195 if (c >= 'A' && c <= 'Z')
196 return c - 'A' + 'a';
197
198 return c;
199 }
200
201 static inline int
202 grub_toupper (int c)
203 {
204 if (c >= 'a' && c <= 'z')
205 return c - 'a' + 'A';
206
207 return c;
208 }
209
210 static inline int
211 grub_strcasecmp (const char *s1, const char *s2)
212 {
213 while (*s1 && *s2)
214 {
215 if (grub_tolower ((grub_uint8_t) *s1)
216 != grub_tolower ((grub_uint8_t) *s2))
217 break;
218
219 s1++;
220 s2++;
221 }
222
223 return (int) grub_tolower ((grub_uint8_t) *s1)
224 - (int) grub_tolower ((grub_uint8_t) *s2);
225 }
226
227 static inline int
228 grub_strncasecmp (const char *s1, const char *s2, grub_size_t n)
229 {
230 if (n == 0)
231 return 0;
232
233 while (*s1 && *s2 && --n)
234 {
235 if (grub_tolower (*s1) != grub_tolower (*s2))
236 break;
237
238 s1++;
239 s2++;
240 }
241
242 return (int) grub_tolower ((grub_uint8_t) *s1)
243 - (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 static inline long
250 grub_strtol (const char *str, char **end, int base)
251 {
252 int negative = 0;
253 unsigned long long magnitude;
254
255 while (*str && grub_isspace (*str))
256 str++;
257
258 if (*str == '-')
259 {
260 negative = 1;
261 str++;
262 }
263
264 magnitude = grub_strtoull (str, end, base);
265 if (negative)
266 {
267 if (magnitude > (unsigned long) GRUB_LONG_MAX + 1)
268 {
269 grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow is detected"));
270 return GRUB_LONG_MIN;
271 }
272 return -((long) magnitude);
273 }
274 else
275 {
276 if (magnitude > GRUB_LONG_MAX)
277 {
278 grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow is detected"));
279 return GRUB_LONG_MAX;
280 }
281 return (long) magnitude;
282 }
283 }
284
285 char *EXPORT_FUNC(grub_strdup) (const char *s) WARN_UNUSED_RESULT;
286 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 int EXPORT_FUNC(grub_printf) (const char *fmt, ...) __attribute__ ((format (GNU_PRINTF, 1, 2)));
290 int EXPORT_FUNC(grub_printf_) (const char *fmt, ...) __attribute__ ((format (GNU_PRINTF, 1, 2)));
291
292 /* Replace all `ch' characters of `input' with `with' and copy the
293 result into `output'; return EOS address of `output'. */
294 static inline char *
295 grub_strchrsub (char *output, const char *input, char ch, const char *with)
296 {
297 while (*input)
298 {
299 if (*input == ch)
300 {
301 grub_strcpy (output, with);
302 output += grub_strlen (with);
303 input++;
304 continue;
305 }
306 *output++ = *input++;
307 }
308 *output = '\0';
309 return output;
310 }
311
312 extern void (*EXPORT_VAR (grub_xputs)) (const char *str);
313
314 static inline int
315 grub_puts (const char *s)
316 {
317 const char nl[2] = "\n";
318 grub_xputs (s);
319 grub_xputs (nl);
320
321 return 1; /* Cannot fail. */
322 }
323
324 int EXPORT_FUNC(grub_puts_) (const char *s);
325 void EXPORT_FUNC(grub_real_dprintf) (const char *file,
326 const int line,
327 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 int EXPORT_FUNC(grub_vsnprintf) (char *str, grub_size_t n, const char *fmt,
333 va_list args);
334 char *EXPORT_FUNC(grub_xasprintf) (const char *fmt, ...)
335 __attribute__ ((format (GNU_PRINTF, 1, 2))) WARN_UNUSED_RESULT;
336 char *EXPORT_FUNC(grub_xvasprintf) (const char *fmt, va_list args) WARN_UNUSED_RESULT;
337 void EXPORT_FUNC(grub_exit) (void) __attribute__ ((noreturn));
338 grub_uint64_t EXPORT_FUNC(grub_divmod64) (grub_uint64_t n,
339 grub_uint64_t d,
340 grub_uint64_t *r);
341
342 /* Must match softdiv group in gentpl.py. */
343 #if !defined(GRUB_MACHINE_EMU) && (defined(__arm__) || defined(__ia64__) || \
344 (defined(__riscv) && (__riscv_xlen == 32)))
345 #define GRUB_DIVISION_IN_SOFTWARE 1
346 #else
347 #define GRUB_DIVISION_IN_SOFTWARE 0
348 #endif
349
350 /* Some division functions need to be in kernel if compiler generates calls
351 to them. Otherwise we still need them for consistent tests but they go
352 into a separate module. */
353 #if GRUB_DIVISION_IN_SOFTWARE
354 #define EXPORT_FUNC_IF_SOFTDIV EXPORT_FUNC
355 #else
356 #define EXPORT_FUNC_IF_SOFTDIV(x) x
357 #endif
358
359 grub_int64_t
360 EXPORT_FUNC_IF_SOFTDIV(grub_divmod64s) (grub_int64_t n,
361 grub_int64_t d,
362 grub_int64_t *r);
363
364 grub_uint32_t
365 EXPORT_FUNC_IF_SOFTDIV (grub_divmod32) (grub_uint32_t n,
366 grub_uint32_t d,
367 grub_uint32_t *r);
368
369 grub_int32_t
370 EXPORT_FUNC_IF_SOFTDIV (grub_divmod32s) (grub_int32_t n,
371 grub_int32_t d,
372 grub_int32_t *r);
373
374 /* Inline functions. */
375
376 static inline char *
377 grub_memchr (const void *p, int c, grub_size_t len)
378 {
379 const char *s = (const char *) p;
380 const char *e = s + len;
381
382 for (; s < e; s++)
383 if (*s == c)
384 return (char *) s;
385
386 return 0;
387 }
388
389
390 static inline unsigned int
391 grub_abs (int x)
392 {
393 if (x < 0)
394 return (unsigned int) (-x);
395 else
396 return (unsigned int) x;
397 }
398
399 /* Reboot the machine. */
400 #if defined (GRUB_MACHINE_EMU) || defined (GRUB_MACHINE_QEMU_MIPS) || \
401 defined (GRUB_MACHINE_EFI)
402 void EXPORT_FUNC(grub_reboot) (void) __attribute__ ((noreturn));
403 #else
404 void grub_reboot (void) __attribute__ ((noreturn));
405 #endif
406
407 #if defined (__clang__) && !defined (GRUB_UTIL)
408 void __attribute__ ((noreturn)) EXPORT_FUNC (abort) (void);
409 #endif
410
411 #ifdef GRUB_MACHINE_PCBIOS
412 /* Halt the system, using APM if possible. If NO_APM is true, don't
413 * use APM even if it is available. */
414 void grub_halt (int no_apm) __attribute__ ((noreturn));
415 #elif (defined (__mips__) && (_MIPS_SIM != _ABI64)) && !defined (GRUB_MACHINE_EMU)
416 void EXPORT_FUNC (grub_halt) (void) __attribute__ ((noreturn));
417 #else
418 void grub_halt (void) __attribute__ ((noreturn));
419 #endif
420
421 #ifdef GRUB_MACHINE_EMU
422 /* Flag to check if module loading is available. */
423 extern const int EXPORT_VAR(grub_no_modules);
424 #else
425 #define grub_no_modules 0
426 #endif
427
428 static inline void
429 grub_error_save (struct grub_error_saved *save)
430 {
431 grub_memcpy (save->errmsg, grub_errmsg, sizeof (save->errmsg));
432 save->grub_errno = grub_errno;
433 grub_errno = GRUB_ERR_NONE;
434 }
435
436 static inline void
437 grub_error_load (const struct grub_error_saved *save)
438 {
439 grub_memcpy (grub_errmsg, save->errmsg, sizeof (grub_errmsg));
440 grub_errno = save->grub_errno;
441 }
442
443 #if BOOT_TIME_STATS
444 struct grub_boot_time
445 {
446 struct grub_boot_time *next;
447 grub_uint64_t tp;
448 const char *file;
449 int line;
450 char *msg;
451 };
452
453 extern struct grub_boot_time *EXPORT_VAR(grub_boot_time_head);
454
455 void EXPORT_FUNC(grub_real_boot_time) (const char *file,
456 const int line,
457 const char *fmt, ...) __attribute__ ((format (GNU_PRINTF, 3, 4)));
458 #define grub_boot_time(...) grub_real_boot_time(GRUB_FILE, __LINE__, __VA_ARGS__)
459 #else
460 #define grub_boot_time(...)
461 #endif
462
463 #define grub_max(a, b) (((a) > (b)) ? (a) : (b))
464 #define grub_min(a, b) (((a) < (b)) ? (a) : (b))
465
466 #endif /* ! GRUB_MISC_HEADER */