misc.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401
  1. /* misc.c - definitions of misc functions */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 1999,2000,2001,2002,2003,2004,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. #include <grub/misc.h>
  20. #include <grub/err.h>
  21. #include <grub/mm.h>
  22. #include <stdarg.h>
  23. #include <grub/term.h>
  24. #include <grub/env.h>
  25. #include <grub/i18n.h>
  26. #include <grub/types.h>
  27. #include <grub/charset.h>
  28. union printf_arg
  29. {
  30. /* Yes, type is also part of union as the moment we fill the value
  31. we don't need to store its type anymore (when we'll need it, we'll
  32. have format spec again. So save some space. */
  33. enum
  34. {
  35. INT, LONG, LONGLONG,
  36. UNSIGNED_INT = 3, UNSIGNED_LONG, UNSIGNED_LONGLONG,
  37. STRING,
  38. GUID
  39. } type;
  40. long long ll;
  41. };
  42. struct printf_args
  43. {
  44. union printf_arg prealloc[32];
  45. union printf_arg *ptr;
  46. grub_size_t count;
  47. };
  48. static void
  49. parse_printf_args (const char *fmt0, struct printf_args *args,
  50. va_list args_in);
  51. static int
  52. grub_vsnprintf_real (char *str, grub_size_t max_len, const char *fmt0,
  53. struct printf_args *args);
  54. static void
  55. free_printf_args (struct printf_args *args)
  56. {
  57. if (args->ptr != args->prealloc)
  58. grub_free (args->ptr);
  59. }
  60. static int
  61. grub_iswordseparator (int c)
  62. {
  63. return (grub_isspace (c) || c == ',' || c == ';' || c == '|' || c == '&');
  64. }
  65. /* grub_gettext_dummy is not translating anything. */
  66. static const char *
  67. grub_gettext_dummy (const char *s)
  68. {
  69. return s;
  70. }
  71. const char* (*grub_gettext) (const char *s) = grub_gettext_dummy;
  72. void *
  73. grub_memmove (void *dest, const void *src, grub_size_t n)
  74. {
  75. char *d = (char *) dest;
  76. const char *s = (const char *) src;
  77. if (d < s)
  78. while (n--)
  79. *d++ = *s++;
  80. else
  81. {
  82. d += n;
  83. s += n;
  84. while (n--)
  85. *--d = *--s;
  86. }
  87. return dest;
  88. }
  89. char *
  90. grub_strcpy (char *dest, const char *src)
  91. {
  92. char *p = dest;
  93. while ((*p++ = *src++) != '\0')
  94. ;
  95. return dest;
  96. }
  97. int
  98. grub_printf (const char *fmt, ...)
  99. {
  100. va_list ap;
  101. int ret;
  102. #if defined(MM_DEBUG) && !defined(GRUB_UTIL) && !defined (GRUB_MACHINE_EMU)
  103. /*
  104. * To prevent infinite recursion when grub_mm_debug is on, disable it
  105. * when calling grub_vprintf(). One such call loop is:
  106. * grub_vprintf() -> parse_printf_args() -> parse_printf_arg_fmt() ->
  107. * grub_debug_calloc() -> grub_printf() -> grub_vprintf().
  108. */
  109. int grub_mm_debug_save = 0;
  110. if (grub_mm_debug)
  111. {
  112. grub_mm_debug_save = grub_mm_debug;
  113. grub_mm_debug = 0;
  114. }
  115. #endif
  116. va_start (ap, fmt);
  117. ret = grub_vprintf (fmt, ap);
  118. va_end (ap);
  119. #if defined(MM_DEBUG) && !defined(GRUB_UTIL) && !defined (GRUB_MACHINE_EMU)
  120. grub_mm_debug = grub_mm_debug_save;
  121. #endif
  122. return ret;
  123. }
  124. int
  125. grub_printf_ (const char *fmt, ...)
  126. {
  127. va_list ap;
  128. int ret;
  129. va_start (ap, fmt);
  130. ret = grub_vprintf (_(fmt), ap);
  131. va_end (ap);
  132. return ret;
  133. }
  134. int
  135. grub_puts_ (const char *s)
  136. {
  137. return grub_puts (_(s));
  138. }
  139. #if defined (__APPLE__) && ! defined (GRUB_UTIL)
  140. int
  141. grub_err_printf (const char *fmt, ...)
  142. {
  143. va_list ap;
  144. int ret;
  145. va_start (ap, fmt);
  146. ret = grub_vprintf (fmt, ap);
  147. va_end (ap);
  148. return ret;
  149. }
  150. #endif
  151. #if ! defined (__APPLE__) && ! defined (GRUB_UTIL)
  152. int grub_err_printf (const char *fmt, ...)
  153. __attribute__ ((alias("grub_printf")));
  154. #endif
  155. int
  156. grub_debug_enabled (const char * condition)
  157. {
  158. const char *debug, *found;
  159. grub_size_t clen;
  160. int ret = 0;
  161. debug = grub_env_get ("debug");
  162. if (!debug)
  163. return 0;
  164. if (grub_strword (debug, "all"))
  165. {
  166. if (debug[3] == '\0')
  167. return 1;
  168. ret = 1;
  169. }
  170. clen = grub_strlen (condition);
  171. found = debug-1;
  172. while(1)
  173. {
  174. found = grub_strstr (found+1, condition);
  175. if (found == NULL)
  176. break;
  177. /* Found condition is not a whole word, so ignore it. */
  178. if (*(found + clen) != '\0' && *(found + clen) != ','
  179. && !grub_isspace (*(found + clen)))
  180. continue;
  181. /*
  182. * If found condition is at the start of debug or the start is on a word
  183. * boundary, then enable debug. Else if found condition is prefixed with
  184. * '-' and the start is on a word boundary, then disable debug. If none
  185. * of these cases, ignore.
  186. */
  187. if (found == debug || *(found - 1) == ',' || grub_isspace (*(found - 1)))
  188. ret = 1;
  189. else if (*(found - 1) == '-' && ((found == debug + 1) || (*(found - 2) == ','
  190. || grub_isspace (*(found - 2)))))
  191. ret = 0;
  192. }
  193. return ret;
  194. }
  195. void
  196. grub_real_dprintf (const char *file, const int line, const char *condition,
  197. const char *fmt, ...)
  198. {
  199. va_list args;
  200. if (grub_debug_enabled (condition))
  201. {
  202. grub_printf ("%s:%d:%s: ", file, line, condition);
  203. va_start (args, fmt);
  204. grub_vprintf (fmt, args);
  205. va_end (args);
  206. grub_refresh ();
  207. }
  208. }
  209. #define PREALLOC_SIZE 255
  210. int
  211. grub_vprintf (const char *fmt, va_list ap)
  212. {
  213. grub_size_t s;
  214. static char buf[PREALLOC_SIZE + 1];
  215. char *curbuf = buf;
  216. struct printf_args args;
  217. parse_printf_args (fmt, &args, ap);
  218. s = grub_vsnprintf_real (buf, PREALLOC_SIZE, fmt, &args);
  219. if (s > PREALLOC_SIZE)
  220. {
  221. curbuf = grub_malloc (s + 1);
  222. if (!curbuf)
  223. {
  224. grub_errno = GRUB_ERR_NONE;
  225. buf[PREALLOC_SIZE - 3] = '.';
  226. buf[PREALLOC_SIZE - 2] = '.';
  227. buf[PREALLOC_SIZE - 1] = '.';
  228. buf[PREALLOC_SIZE] = 0;
  229. curbuf = buf;
  230. }
  231. else
  232. s = grub_vsnprintf_real (curbuf, s, fmt, &args);
  233. }
  234. free_printf_args (&args);
  235. grub_xputs (curbuf);
  236. if (curbuf != buf)
  237. grub_free (curbuf);
  238. return s;
  239. }
  240. int
  241. grub_memcmp (const void *s1, const void *s2, grub_size_t n)
  242. {
  243. const grub_uint8_t *t1 = s1;
  244. const grub_uint8_t *t2 = s2;
  245. while (n--)
  246. {
  247. if (*t1 != *t2)
  248. return (int) *t1 - (int) *t2;
  249. t1++;
  250. t2++;
  251. }
  252. return 0;
  253. }
  254. int
  255. grub_strcmp (const char *s1, const char *s2)
  256. {
  257. while (*s1 && *s2)
  258. {
  259. if (*s1 != *s2)
  260. break;
  261. s1++;
  262. s2++;
  263. }
  264. return (int) (grub_uint8_t) *s1 - (int) (grub_uint8_t) *s2;
  265. }
  266. int
  267. grub_strncmp (const char *s1, const char *s2, grub_size_t n)
  268. {
  269. if (n == 0)
  270. return 0;
  271. while (*s1 && *s2 && --n)
  272. {
  273. if (*s1 != *s2)
  274. break;
  275. s1++;
  276. s2++;
  277. }
  278. return (int) (grub_uint8_t) *s1 - (int) (grub_uint8_t) *s2;
  279. }
  280. char *
  281. grub_strchr (const char *s, int c)
  282. {
  283. do
  284. {
  285. if (*s == c)
  286. return (char *) s;
  287. }
  288. while (*s++);
  289. return 0;
  290. }
  291. char *
  292. grub_strrchr (const char *s, int c)
  293. {
  294. char *p = NULL;
  295. do
  296. {
  297. if (*s == c)
  298. p = (char *) s;
  299. }
  300. while (*s++);
  301. return p;
  302. }
  303. int
  304. grub_strword (const char *haystack, const char *needle)
  305. {
  306. const char *n_pos = needle;
  307. while (grub_iswordseparator (*haystack))
  308. haystack++;
  309. while (*haystack)
  310. {
  311. /* Crawl both the needle and the haystack word we're on. */
  312. while(*haystack && !grub_iswordseparator (*haystack)
  313. && *haystack == *n_pos)
  314. {
  315. haystack++;
  316. n_pos++;
  317. }
  318. /* If we reached the end of both words at the same time, the word
  319. is found. If not, eat everything in the haystack that isn't the
  320. next word (or the end of string) and "reset" the needle. */
  321. if ( (!*haystack || grub_iswordseparator (*haystack))
  322. && (!*n_pos || grub_iswordseparator (*n_pos)))
  323. return 1;
  324. else
  325. {
  326. n_pos = needle;
  327. while (*haystack && !grub_iswordseparator (*haystack))
  328. haystack++;
  329. while (grub_iswordseparator (*haystack))
  330. haystack++;
  331. }
  332. }
  333. return 0;
  334. }
  335. int
  336. grub_isspace (int c)
  337. {
  338. return (c == '\n' || c == '\r' || c == ' ' || c == '\t');
  339. }
  340. unsigned long
  341. grub_strtoul (const char * restrict str, const char ** const restrict end,
  342. int base)
  343. {
  344. unsigned long long num;
  345. num = grub_strtoull (str, end, base);
  346. #if GRUB_CPU_SIZEOF_LONG != 8
  347. if (num > ~0UL)
  348. {
  349. grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow is detected"));
  350. return ~0UL;
  351. }
  352. #endif
  353. return (unsigned long) num;
  354. }
  355. unsigned long long
  356. grub_strtoull (const char * restrict str, const char ** const restrict end,
  357. int base)
  358. {
  359. unsigned long long num = 0;
  360. int found = 0;
  361. /* Skip white spaces. */
  362. /* grub_isspace checks that *str != '\0'. */
  363. while (grub_isspace (*str))
  364. str++;
  365. /* Guess the base, if not specified. The prefix `0x' means 16, and
  366. the prefix `0' means 8. */
  367. if (str[0] == '0')
  368. {
  369. if (str[1] == 'x')
  370. {
  371. if (base == 0 || base == 16)
  372. {
  373. base = 16;
  374. str += 2;
  375. }
  376. }
  377. else if (base == 0 && str[1] >= '0' && str[1] <= '7')
  378. base = 8;
  379. }
  380. if (base == 0)
  381. base = 10;
  382. while (*str)
  383. {
  384. unsigned long digit;
  385. digit = grub_tolower (*str) - '0';
  386. if (digit >= 'a' - '0')
  387. digit += '0' - 'a' + 10;
  388. else if (digit > 9)
  389. break;
  390. if (digit >= (unsigned long) base)
  391. break;
  392. found = 1;
  393. /* NUM * BASE + DIGIT > ~0ULL */
  394. if (num > grub_divmod64 (~0ULL - digit, base, 0))
  395. {
  396. grub_error (GRUB_ERR_OUT_OF_RANGE,
  397. N_("overflow is detected"));
  398. if (end)
  399. *end = (char *) str;
  400. return ~0ULL;
  401. }
  402. num = num * base + digit;
  403. str++;
  404. }
  405. if (! found)
  406. {
  407. grub_error (GRUB_ERR_BAD_NUMBER,
  408. N_("unrecognized number"));
  409. if (end)
  410. *end = (char *) str;
  411. return 0;
  412. }
  413. if (end)
  414. *end = (char *) str;
  415. return num;
  416. }
  417. char *
  418. grub_strdup (const char *s)
  419. {
  420. grub_size_t len;
  421. char *p;
  422. len = grub_strlen (s) + 1;
  423. p = (char *) grub_malloc (len);
  424. if (! p)
  425. return 0;
  426. return grub_memcpy (p, s, len);
  427. }
  428. char *
  429. grub_strndup (const char *s, grub_size_t n)
  430. {
  431. grub_size_t len;
  432. char *p;
  433. len = grub_strlen (s);
  434. if (len > n)
  435. len = n;
  436. p = (char *) grub_malloc (len + 1);
  437. if (! p)
  438. return 0;
  439. grub_memcpy (p, s, len);
  440. p[len] = '\0';
  441. return p;
  442. }
  443. /* clang detects that we're implementing here a memset so it decides to
  444. optimise and calls memset resulting in infinite recursion. With volatile
  445. we make it not optimise in this way. */
  446. #ifdef __clang__
  447. #define VOLATILE_CLANG volatile
  448. #else
  449. #define VOLATILE_CLANG
  450. #endif
  451. void *
  452. grub_memset (void *s, int c, grub_size_t len)
  453. {
  454. void *p = s;
  455. grub_uint8_t pattern8 = c;
  456. if (len >= 3 * sizeof (unsigned long))
  457. {
  458. unsigned long patternl = 0;
  459. grub_size_t i;
  460. for (i = 0; i < sizeof (unsigned long); i++)
  461. patternl |= ((unsigned long) pattern8) << (8 * i);
  462. while (len > 0 && (((grub_addr_t) p) & (sizeof (unsigned long) - 1)))
  463. {
  464. *(VOLATILE_CLANG grub_uint8_t *) p = pattern8;
  465. p = (grub_uint8_t *) p + 1;
  466. len--;
  467. }
  468. while (len >= sizeof (unsigned long))
  469. {
  470. *(VOLATILE_CLANG unsigned long *) p = patternl;
  471. p = (unsigned long *) p + 1;
  472. len -= sizeof (unsigned long);
  473. }
  474. }
  475. while (len > 0)
  476. {
  477. *(VOLATILE_CLANG grub_uint8_t *) p = pattern8;
  478. p = (grub_uint8_t *) p + 1;
  479. len--;
  480. }
  481. return s;
  482. }
  483. grub_size_t
  484. grub_strlen (const char *s)
  485. {
  486. const char *p = s;
  487. while (*p)
  488. p++;
  489. return p - s;
  490. }
  491. static inline void
  492. grub_reverse (char *str)
  493. {
  494. char *p = str + grub_strlen (str) - 1;
  495. while (str < p)
  496. {
  497. char tmp;
  498. tmp = *str;
  499. *str = *p;
  500. *p = tmp;
  501. str++;
  502. p--;
  503. }
  504. }
  505. /* Divide N by D, return the quotient, and store the remainder in *R. */
  506. grub_uint64_t
  507. grub_divmod64 (grub_uint64_t n, grub_uint64_t d, grub_uint64_t *r)
  508. {
  509. /* This algorithm is typically implemented by hardware. The idea
  510. is to get the highest bit in N, 64 times, by keeping
  511. upper(N * 2^i) = (Q * D + M), where upper
  512. represents the high 64 bits in 128-bits space. */
  513. unsigned bits = 64;
  514. grub_uint64_t q = 0;
  515. grub_uint64_t m = 0;
  516. /* ARM and IA64 don't have a fast 32-bit division.
  517. Using that code would just make us use software division routines, calling
  518. ourselves indirectly and hence getting infinite recursion.
  519. */
  520. #if !GRUB_DIVISION_IN_SOFTWARE
  521. /* Skip the slow computation if 32-bit arithmetic is possible. */
  522. if (n < 0xffffffff && d < 0xffffffff)
  523. {
  524. if (r)
  525. *r = ((grub_uint32_t) n) % (grub_uint32_t) d;
  526. return ((grub_uint32_t) n) / (grub_uint32_t) d;
  527. }
  528. #endif
  529. while (bits--)
  530. {
  531. m <<= 1;
  532. if (n & (1ULL << 63))
  533. m |= 1;
  534. q <<= 1;
  535. n <<= 1;
  536. if (m >= d)
  537. {
  538. q |= 1;
  539. m -= d;
  540. }
  541. }
  542. if (r)
  543. *r = m;
  544. return q;
  545. }
  546. /* Convert a long long value to a string. This function avoids 64-bit
  547. modular arithmetic or divisions. */
  548. static inline char *
  549. grub_lltoa (char *str, int c, unsigned long long n)
  550. {
  551. unsigned base = ((c == 'x') || (c == 'X')) ? 16 : ((c == 'o') ? 8 : 10);
  552. char *p;
  553. if ((long long) n < 0 && c == 'd')
  554. {
  555. n = (unsigned long long) (-((long long) n));
  556. *str++ = '-';
  557. }
  558. p = str;
  559. if (base == 16)
  560. do
  561. {
  562. unsigned d = (unsigned) (n & 0xf);
  563. *p++ = (d > 9) ? (d + ((c == 'x') ? 'a' : 'A') - 10) : d + '0';
  564. }
  565. while (n >>= 4);
  566. else if (base == 8)
  567. do
  568. {
  569. *p++ = ((unsigned) (n & 0x7)) + '0';
  570. }
  571. while (n >>= 3);
  572. else
  573. /* BASE == 10 */
  574. do
  575. {
  576. grub_uint64_t m;
  577. n = grub_divmod64 (n, 10, &m);
  578. *p++ = m + '0';
  579. }
  580. while (n);
  581. *p = 0;
  582. grub_reverse (str);
  583. return p;
  584. }
  585. /*
  586. * Parse printf() fmt0 string into args arguments.
  587. *
  588. * The parsed arguments are either used by a printf() function to format the fmt0
  589. * string or they are used to compare a format string from an untrusted source
  590. * against a format string with expected arguments.
  591. *
  592. * When the fmt_check is set to !0, e.g. 1, then this function is executed in
  593. * printf() format check mode. This enforces stricter rules for parsing the
  594. * fmt0 to limit exposure to possible errors in printf() handling. It also
  595. * disables positional parameters, "$", because some formats, e.g "%s%1$d",
  596. * cannot be validated with the current implementation.
  597. *
  598. * The max_args allows to set a maximum number of accepted arguments. If the fmt0
  599. * string defines more arguments than the max_args then the parse_printf_arg_fmt()
  600. * function returns an error. This is currently used for format check only.
  601. */
  602. static grub_err_t
  603. parse_printf_arg_fmt (const char *fmt0, struct printf_args *args,
  604. int fmt_check, grub_size_t max_args)
  605. {
  606. const char *fmt;
  607. char c;
  608. grub_size_t n = 0;
  609. args->count = 0;
  610. COMPILE_TIME_ASSERT (sizeof (int) == sizeof (grub_uint32_t));
  611. COMPILE_TIME_ASSERT (sizeof (int) <= sizeof (long long));
  612. COMPILE_TIME_ASSERT (sizeof (long) <= sizeof (long long));
  613. COMPILE_TIME_ASSERT (sizeof (long long) == sizeof (void *)
  614. || sizeof (int) == sizeof (void *));
  615. fmt = fmt0;
  616. while ((c = *fmt++) != 0)
  617. {
  618. if (c != '%')
  619. continue;
  620. if (*fmt =='-')
  621. fmt++;
  622. while (grub_isdigit (*fmt))
  623. fmt++;
  624. if (*fmt == '$')
  625. {
  626. if (fmt_check)
  627. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  628. "positional arguments are not supported");
  629. fmt++;
  630. }
  631. if (*fmt =='-')
  632. fmt++;
  633. while (grub_isdigit (*fmt))
  634. fmt++;
  635. if (*fmt =='.')
  636. fmt++;
  637. while (grub_isdigit (*fmt))
  638. fmt++;
  639. c = *fmt++;
  640. if (c == 'l')
  641. c = *fmt++;
  642. if (c == 'l')
  643. c = *fmt++;
  644. switch (c)
  645. {
  646. case 'p':
  647. if (*(fmt) == 'G')
  648. ++fmt;
  649. /* Fall through. */
  650. case 'x':
  651. case 'X':
  652. case 'u':
  653. case 'd':
  654. case 'o':
  655. case 'c':
  656. case 'C':
  657. case 's':
  658. args->count++;
  659. break;
  660. case '%':
  661. /* "%%" is the escape sequence to output "%". */
  662. break;
  663. default:
  664. if (fmt_check)
  665. return grub_error (GRUB_ERR_BAD_ARGUMENT, "unexpected format");
  666. break;
  667. }
  668. }
  669. if (fmt_check && args->count > max_args)
  670. return grub_error (GRUB_ERR_BAD_ARGUMENT, "too many arguments");
  671. if (args->count <= ARRAY_SIZE (args->prealloc))
  672. args->ptr = args->prealloc;
  673. else
  674. {
  675. args->ptr = grub_calloc (args->count, sizeof (args->ptr[0]));
  676. if (!args->ptr)
  677. {
  678. if (fmt_check)
  679. return grub_errno;
  680. grub_errno = GRUB_ERR_NONE;
  681. args->ptr = args->prealloc;
  682. args->count = ARRAY_SIZE (args->prealloc);
  683. }
  684. }
  685. grub_memset (args->ptr, 0, args->count * sizeof (args->ptr[0]));
  686. fmt = fmt0;
  687. n = 0;
  688. while ((c = *fmt++) != 0)
  689. {
  690. int longfmt = 0;
  691. grub_size_t curn;
  692. const char *p;
  693. if (c != '%')
  694. continue;
  695. curn = n++;
  696. if (*fmt =='-')
  697. fmt++;
  698. p = fmt;
  699. while (grub_isdigit (*fmt))
  700. fmt++;
  701. if (*fmt == '$')
  702. {
  703. curn = grub_strtoull (p, 0, 10) - 1;
  704. fmt++;
  705. }
  706. if (*fmt =='-')
  707. fmt++;
  708. while (grub_isdigit (*fmt))
  709. fmt++;
  710. if (*fmt =='.')
  711. fmt++;
  712. while (grub_isdigit (*fmt))
  713. fmt++;
  714. c = *fmt++;
  715. if (c == '%')
  716. {
  717. n--;
  718. continue;
  719. }
  720. if (c == 'l')
  721. {
  722. c = *fmt++;
  723. longfmt = 1;
  724. }
  725. if (c == 'l')
  726. {
  727. c = *fmt++;
  728. longfmt = 2;
  729. }
  730. if (curn >= args->count)
  731. continue;
  732. switch (c)
  733. {
  734. case 'x':
  735. case 'X':
  736. case 'o':
  737. case 'u':
  738. args->ptr[curn].type = UNSIGNED_INT + longfmt;
  739. break;
  740. case 'd':
  741. args->ptr[curn].type = INT + longfmt;
  742. break;
  743. case 'p':
  744. if (sizeof (void *) == sizeof (long long))
  745. args->ptr[curn].type = UNSIGNED_LONGLONG;
  746. else
  747. args->ptr[curn].type = UNSIGNED_INT;
  748. if (*(fmt) == 'G') {
  749. args->ptr[curn].type = GUID;
  750. ++fmt;
  751. }
  752. break;
  753. case 's':
  754. args->ptr[curn].type = STRING;
  755. break;
  756. case 'C':
  757. case 'c':
  758. args->ptr[curn].type = INT;
  759. break;
  760. }
  761. }
  762. return GRUB_ERR_NONE;
  763. }
  764. static void
  765. parse_printf_args (const char *fmt0, struct printf_args *args, va_list args_in)
  766. {
  767. grub_size_t n;
  768. parse_printf_arg_fmt (fmt0, args, 0, 0);
  769. for (n = 0; n < args->count; n++)
  770. switch (args->ptr[n].type)
  771. {
  772. case INT:
  773. args->ptr[n].ll = va_arg (args_in, int);
  774. break;
  775. case LONG:
  776. args->ptr[n].ll = va_arg (args_in, long);
  777. break;
  778. case UNSIGNED_INT:
  779. args->ptr[n].ll = va_arg (args_in, unsigned int);
  780. break;
  781. case UNSIGNED_LONG:
  782. args->ptr[n].ll = va_arg (args_in, unsigned long);
  783. break;
  784. case LONGLONG:
  785. case UNSIGNED_LONGLONG:
  786. args->ptr[n].ll = va_arg (args_in, long long);
  787. break;
  788. case STRING:
  789. case GUID:
  790. if (sizeof (void *) == sizeof (long long))
  791. args->ptr[n].ll = va_arg (args_in, long long);
  792. else
  793. args->ptr[n].ll = va_arg (args_in, unsigned int);
  794. break;
  795. }
  796. }
  797. static inline void __attribute__ ((always_inline))
  798. write_char (char *str, grub_size_t *count, grub_size_t max_len, unsigned char ch)
  799. {
  800. if (*count < max_len)
  801. str[*count] = ch;
  802. (*count)++;
  803. }
  804. static void
  805. write_number (char *str, grub_size_t *count, grub_size_t max_len, grub_size_t format1,
  806. char rightfill, char zerofill, char c, long long value)
  807. {
  808. char tmp[32];
  809. const char *p = tmp;
  810. grub_size_t len;
  811. grub_size_t fill;
  812. len = grub_lltoa (tmp, c, value) - tmp;
  813. fill = len < format1 ? format1 - len : 0;
  814. if (! rightfill)
  815. while (fill--)
  816. write_char (str, count, max_len, zerofill);
  817. while (*p)
  818. write_char (str, count, max_len, *p++);
  819. if (rightfill)
  820. while (fill--)
  821. write_char (str, count, max_len, zerofill);
  822. }
  823. static int
  824. grub_vsnprintf_real (char *str, grub_size_t max_len, const char *fmt0,
  825. struct printf_args *args)
  826. {
  827. char c;
  828. grub_size_t n = 0;
  829. grub_size_t count = 0;
  830. const char *fmt;
  831. fmt = fmt0;
  832. while ((c = *fmt++) != 0)
  833. {
  834. unsigned int format1 = 0;
  835. unsigned int format2 = ~ 0U;
  836. char zerofill = ' ';
  837. char rightfill = 0;
  838. grub_size_t curn;
  839. if (c != '%')
  840. {
  841. write_char (str, &count, max_len, c);
  842. continue;
  843. }
  844. curn = n++;
  845. rescan:;
  846. if (*fmt =='-')
  847. {
  848. rightfill = 1;
  849. fmt++;
  850. }
  851. /* Read formatting parameters. */
  852. if (grub_isdigit (*fmt))
  853. {
  854. if (fmt[0] == '0')
  855. zerofill = '0';
  856. format1 = grub_strtoul (fmt, &fmt, 10);
  857. }
  858. if (*fmt == '.')
  859. fmt++;
  860. if (grub_isdigit (*fmt))
  861. format2 = grub_strtoul (fmt, &fmt, 10);
  862. if (*fmt == '$')
  863. {
  864. curn = format1 - 1;
  865. fmt++;
  866. format1 = 0;
  867. format2 = ~ 0U;
  868. zerofill = ' ';
  869. rightfill = 0;
  870. goto rescan;
  871. }
  872. c = *fmt++;
  873. if (c == 'l')
  874. c = *fmt++;
  875. if (c == 'l')
  876. c = *fmt++;
  877. if (c == '%')
  878. {
  879. write_char (str, &count, max_len, c);
  880. n--;
  881. continue;
  882. }
  883. if (curn >= args->count)
  884. continue;
  885. long long curarg = args->ptr[curn].ll;
  886. switch (c)
  887. {
  888. case 'p':
  889. if (*(fmt) == 'G')
  890. {
  891. ++fmt;
  892. grub_packed_guid_t *guid = (grub_packed_guid_t *)(grub_addr_t) curarg;
  893. write_number (str, &count, max_len, 8, 0, '0', 'x', guid->data1);
  894. write_char (str, &count, max_len, '-');
  895. write_number (str, &count, max_len, 4, 0, '0', 'x', guid->data2);
  896. write_char (str, &count, max_len, '-');
  897. write_number (str, &count, max_len, 4, 0, '0', 'x', guid->data3);
  898. write_char (str, &count, max_len, '-');
  899. write_number (str, &count, max_len, 2, 0, '0', 'x', guid->data4[0]);
  900. write_number (str, &count, max_len, 2, 0, '0', 'x', guid->data4[1]);
  901. write_char (str, &count, max_len, '-');
  902. write_number (str, &count, max_len, 2, 0, '0', 'x', guid->data4[2]);
  903. write_number (str, &count, max_len, 2, 0, '0', 'x', guid->data4[3]);
  904. write_number (str, &count, max_len, 2, 0, '0', 'x', guid->data4[4]);
  905. write_number (str, &count, max_len, 2, 0, '0', 'x', guid->data4[5]);
  906. write_number (str, &count, max_len, 2, 0, '0', 'x', guid->data4[6]);
  907. write_number (str, &count, max_len, 2, 0, '0', 'x', guid->data4[7]);
  908. break;
  909. }
  910. else
  911. {
  912. write_char (str, &count, max_len, '0');
  913. write_char (str, &count, max_len, 'x');
  914. c = 'x';
  915. }
  916. /* Fall through. */
  917. case 'x':
  918. case 'X':
  919. case 'u':
  920. case 'd':
  921. case 'o':
  922. write_number (str, &count, max_len, format1, rightfill, zerofill, c, curarg);
  923. break;
  924. case 'c':
  925. write_char (str, &count, max_len, curarg & 0xff);
  926. break;
  927. case 'C':
  928. {
  929. grub_uint32_t code = curarg;
  930. int shift;
  931. unsigned mask;
  932. if (code <= 0x7f)
  933. {
  934. shift = 0;
  935. mask = 0;
  936. }
  937. else if (code <= 0x7ff)
  938. {
  939. shift = 6;
  940. mask = 0xc0;
  941. }
  942. else if (code <= 0xffff)
  943. {
  944. shift = 12;
  945. mask = 0xe0;
  946. }
  947. else if (code <= 0x10ffff)
  948. {
  949. shift = 18;
  950. mask = 0xf0;
  951. }
  952. else
  953. {
  954. code = '?';
  955. shift = 0;
  956. mask = 0;
  957. }
  958. write_char (str, &count, max_len, mask | (code >> shift));
  959. for (shift -= 6; shift >= 0; shift -= 6)
  960. write_char (str, &count, max_len, 0x80 | (0x3f & (code >> shift)));
  961. }
  962. break;
  963. case 's':
  964. {
  965. grub_size_t len = 0;
  966. grub_size_t fill;
  967. const char *p = ((char *) (grub_addr_t) curarg) ? : "(null)";
  968. grub_size_t i;
  969. while (len < format2 && p[len])
  970. len++;
  971. fill = len < format1 ? format1 - len : 0;
  972. if (!rightfill)
  973. while (fill--)
  974. write_char (str, &count, max_len, zerofill);
  975. for (i = 0; i < len; i++)
  976. write_char (str, &count, max_len, *p++);
  977. if (rightfill)
  978. while (fill--)
  979. write_char (str, &count, max_len, zerofill);
  980. }
  981. break;
  982. default:
  983. write_char (str, &count, max_len, c);
  984. break;
  985. }
  986. }
  987. if (count < max_len)
  988. str[count] = '\0';
  989. else
  990. str[max_len] = '\0';
  991. return count;
  992. }
  993. int
  994. grub_vsnprintf (char *str, grub_size_t n, const char *fmt, va_list ap)
  995. {
  996. grub_size_t ret;
  997. struct printf_args args;
  998. if (!n)
  999. return 0;
  1000. n--;
  1001. parse_printf_args (fmt, &args, ap);
  1002. ret = grub_vsnprintf_real (str, n, fmt, &args);
  1003. free_printf_args (&args);
  1004. return ret;
  1005. }
  1006. int
  1007. grub_snprintf (char *str, grub_size_t n, const char *fmt, ...)
  1008. {
  1009. va_list ap;
  1010. int ret;
  1011. va_start (ap, fmt);
  1012. ret = grub_vsnprintf (str, n, fmt, ap);
  1013. va_end (ap);
  1014. return ret;
  1015. }
  1016. char *
  1017. grub_xvasprintf (const char *fmt, va_list ap)
  1018. {
  1019. grub_size_t s, as = PREALLOC_SIZE;
  1020. char *ret;
  1021. struct printf_args args;
  1022. parse_printf_args (fmt, &args, ap);
  1023. while (1)
  1024. {
  1025. ret = grub_malloc (as + 1);
  1026. if (!ret)
  1027. {
  1028. free_printf_args (&args);
  1029. return NULL;
  1030. }
  1031. s = grub_vsnprintf_real (ret, as, fmt, &args);
  1032. if (s <= as)
  1033. {
  1034. free_printf_args (&args);
  1035. return ret;
  1036. }
  1037. grub_free (ret);
  1038. as = s;
  1039. }
  1040. }
  1041. char *
  1042. grub_xasprintf (const char *fmt, ...)
  1043. {
  1044. va_list ap;
  1045. char *ret;
  1046. va_start (ap, fmt);
  1047. ret = grub_xvasprintf (fmt, ap);
  1048. va_end (ap);
  1049. return ret;
  1050. }
  1051. grub_err_t
  1052. grub_printf_fmt_check (const char *fmt, const char *fmt_expected)
  1053. {
  1054. struct printf_args args_expected, args_fmt;
  1055. grub_err_t ret;
  1056. grub_size_t n;
  1057. if (fmt == NULL || fmt_expected == NULL)
  1058. return grub_error (GRUB_ERR_BAD_ARGUMENT, "invalid format");
  1059. ret = parse_printf_arg_fmt (fmt_expected, &args_expected, 1, GRUB_SIZE_MAX);
  1060. if (ret != GRUB_ERR_NONE)
  1061. return ret;
  1062. /* Limit parsing to the number of expected arguments. */
  1063. ret = parse_printf_arg_fmt (fmt, &args_fmt, 1, args_expected.count);
  1064. if (ret != GRUB_ERR_NONE)
  1065. {
  1066. free_printf_args (&args_expected);
  1067. return ret;
  1068. }
  1069. for (n = 0; n < args_fmt.count; n++)
  1070. if (args_fmt.ptr[n].type != args_expected.ptr[n].type)
  1071. {
  1072. ret = grub_error (GRUB_ERR_BAD_ARGUMENT, "arguments types do not match");
  1073. break;
  1074. }
  1075. free_printf_args (&args_expected);
  1076. free_printf_args (&args_fmt);
  1077. return ret;
  1078. }
  1079. /* Abort GRUB. This function does not return. */
  1080. void __attribute__ ((noreturn))
  1081. grub_abort (void)
  1082. {
  1083. grub_printf ("\nAborted.");
  1084. #ifndef GRUB_UTIL
  1085. if (grub_term_inputs)
  1086. #endif
  1087. {
  1088. grub_printf (" Press any key to exit.");
  1089. grub_getkey ();
  1090. }
  1091. grub_exit ();
  1092. }
  1093. void
  1094. grub_fatal (const char *fmt, ...)
  1095. {
  1096. va_list ap;
  1097. va_start (ap, fmt);
  1098. grub_vprintf (_(fmt), ap);
  1099. va_end (ap);
  1100. grub_refresh ();
  1101. grub_abort ();
  1102. }
  1103. grub_ssize_t
  1104. grub_utf8_to_utf16_alloc (const char *str8, grub_uint16_t **utf16_msg, grub_uint16_t **last_position)
  1105. {
  1106. grub_size_t len;
  1107. grub_size_t len16;
  1108. len = grub_strlen (str8);
  1109. /* Check for integer overflow */
  1110. if (len > GRUB_SSIZE_MAX / GRUB_MAX_UTF16_PER_UTF8 - 1)
  1111. {
  1112. grub_error (GRUB_ERR_BAD_ARGUMENT, N_("string too long"));
  1113. *utf16_msg = NULL;
  1114. return -1;
  1115. }
  1116. len16 = len * GRUB_MAX_UTF16_PER_UTF8;
  1117. *utf16_msg = grub_calloc (len16 + 1, sizeof (*utf16_msg[0]));
  1118. if (*utf16_msg == NULL)
  1119. return -1;
  1120. len16 = grub_utf8_to_utf16 (*utf16_msg, len16, (grub_uint8_t *) str8, len, NULL);
  1121. if (last_position != NULL)
  1122. *last_position = *utf16_msg + len16;
  1123. return len16;
  1124. }
  1125. #if BOOT_TIME_STATS
  1126. #include <grub/time.h>
  1127. struct grub_boot_time *grub_boot_time_head;
  1128. static struct grub_boot_time **boot_time_last = &grub_boot_time_head;
  1129. void
  1130. grub_real_boot_time (const char *file,
  1131. const int line,
  1132. const char *fmt, ...)
  1133. {
  1134. struct grub_boot_time *n;
  1135. va_list args;
  1136. grub_error_push ();
  1137. n = grub_malloc (sizeof (*n));
  1138. if (!n)
  1139. {
  1140. grub_errno = 0;
  1141. grub_error_pop ();
  1142. return;
  1143. }
  1144. n->file = file;
  1145. n->line = line;
  1146. n->tp = grub_get_time_ms ();
  1147. n->next = 0;
  1148. va_start (args, fmt);
  1149. n->msg = grub_xvasprintf (fmt, args);
  1150. va_end (args);
  1151. *boot_time_last = n;
  1152. boot_time_last = &n->next;
  1153. grub_errno = 0;
  1154. grub_error_pop ();
  1155. }
  1156. #endif