misc.c 20 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154
  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. union printf_arg
  27. {
  28. /* Yes, type is also part of union as the moment we fill the value
  29. we don't need to store its type anymore (when we'll need it, we'll
  30. have format spec again. So save some space. */
  31. enum
  32. {
  33. INT, LONG, LONGLONG,
  34. UNSIGNED_INT = 3, UNSIGNED_LONG, UNSIGNED_LONGLONG
  35. } type;
  36. long long ll;
  37. };
  38. struct printf_args
  39. {
  40. union printf_arg prealloc[32];
  41. union printf_arg *ptr;
  42. grub_size_t count;
  43. };
  44. static void
  45. parse_printf_args (const char *fmt0, struct printf_args *args,
  46. va_list args_in);
  47. static int
  48. grub_vsnprintf_real (char *str, grub_size_t max_len, const char *fmt0,
  49. struct printf_args *args);
  50. static void
  51. free_printf_args (struct printf_args *args)
  52. {
  53. if (args->ptr != args->prealloc)
  54. grub_free (args->ptr);
  55. }
  56. static int
  57. grub_iswordseparator (int c)
  58. {
  59. return (grub_isspace (c) || c == ',' || c == ';' || c == '|' || c == '&');
  60. }
  61. /* grub_gettext_dummy is not translating anything. */
  62. static const char *
  63. grub_gettext_dummy (const char *s)
  64. {
  65. return s;
  66. }
  67. const char* (*grub_gettext) (const char *s) = grub_gettext_dummy;
  68. void *
  69. grub_memmove (void *dest, const void *src, grub_size_t n)
  70. {
  71. char *d = (char *) dest;
  72. const char *s = (const char *) src;
  73. if (d < s)
  74. while (n--)
  75. *d++ = *s++;
  76. else
  77. {
  78. d += n;
  79. s += n;
  80. while (n--)
  81. *--d = *--s;
  82. }
  83. return dest;
  84. }
  85. char *
  86. grub_strcpy (char *dest, const char *src)
  87. {
  88. char *p = dest;
  89. while ((*p++ = *src++) != '\0')
  90. ;
  91. return dest;
  92. }
  93. int
  94. grub_printf (const char *fmt, ...)
  95. {
  96. va_list ap;
  97. int ret;
  98. va_start (ap, fmt);
  99. ret = grub_vprintf (fmt, ap);
  100. va_end (ap);
  101. return ret;
  102. }
  103. int
  104. grub_printf_ (const char *fmt, ...)
  105. {
  106. va_list ap;
  107. int ret;
  108. va_start (ap, fmt);
  109. ret = grub_vprintf (_(fmt), ap);
  110. va_end (ap);
  111. return ret;
  112. }
  113. int
  114. grub_puts_ (const char *s)
  115. {
  116. return grub_puts (_(s));
  117. }
  118. #if defined (__APPLE__) && ! defined (GRUB_UTIL)
  119. int
  120. grub_err_printf (const char *fmt, ...)
  121. {
  122. va_list ap;
  123. int ret;
  124. va_start (ap, fmt);
  125. ret = grub_vprintf (fmt, ap);
  126. va_end (ap);
  127. return ret;
  128. }
  129. #endif
  130. #if ! defined (__APPLE__) && ! defined (GRUB_UTIL)
  131. int grub_err_printf (const char *fmt, ...)
  132. __attribute__ ((alias("grub_printf")));
  133. #endif
  134. void
  135. grub_real_dprintf (const char *file, const int line, const char *condition,
  136. const char *fmt, ...)
  137. {
  138. va_list args;
  139. const char *debug = grub_env_get ("debug");
  140. if (! debug)
  141. return;
  142. if (grub_strword (debug, "all") || grub_strword (debug, condition))
  143. {
  144. grub_printf ("%s:%d: ", file, line);
  145. va_start (args, fmt);
  146. grub_vprintf (fmt, args);
  147. va_end (args);
  148. grub_refresh ();
  149. }
  150. }
  151. #define PREALLOC_SIZE 255
  152. int
  153. grub_vprintf (const char *fmt, va_list ap)
  154. {
  155. grub_size_t s;
  156. static char buf[PREALLOC_SIZE + 1];
  157. char *curbuf = buf;
  158. struct printf_args args;
  159. parse_printf_args (fmt, &args, ap);
  160. s = grub_vsnprintf_real (buf, PREALLOC_SIZE, fmt, &args);
  161. if (s > PREALLOC_SIZE)
  162. {
  163. curbuf = grub_malloc (s + 1);
  164. if (!curbuf)
  165. {
  166. grub_errno = GRUB_ERR_NONE;
  167. buf[PREALLOC_SIZE - 3] = '.';
  168. buf[PREALLOC_SIZE - 2] = '.';
  169. buf[PREALLOC_SIZE - 1] = '.';
  170. buf[PREALLOC_SIZE] = 0;
  171. curbuf = buf;
  172. }
  173. else
  174. s = grub_vsnprintf_real (curbuf, s, fmt, &args);
  175. }
  176. free_printf_args (&args);
  177. grub_xputs (curbuf);
  178. if (curbuf != buf)
  179. grub_free (curbuf);
  180. return s;
  181. }
  182. int
  183. grub_memcmp (const void *s1, const void *s2, grub_size_t n)
  184. {
  185. const grub_uint8_t *t1 = s1;
  186. const grub_uint8_t *t2 = s2;
  187. while (n--)
  188. {
  189. if (*t1 != *t2)
  190. return (int) *t1 - (int) *t2;
  191. t1++;
  192. t2++;
  193. }
  194. return 0;
  195. }
  196. int
  197. grub_strcmp (const char *s1, const char *s2)
  198. {
  199. while (*s1 && *s2)
  200. {
  201. if (*s1 != *s2)
  202. break;
  203. s1++;
  204. s2++;
  205. }
  206. return (int) (grub_uint8_t) *s1 - (int) (grub_uint8_t) *s2;
  207. }
  208. int
  209. grub_strncmp (const char *s1, const char *s2, grub_size_t n)
  210. {
  211. if (n == 0)
  212. return 0;
  213. while (*s1 && *s2 && --n)
  214. {
  215. if (*s1 != *s2)
  216. break;
  217. s1++;
  218. s2++;
  219. }
  220. return (int) (grub_uint8_t) *s1 - (int) (grub_uint8_t) *s2;
  221. }
  222. char *
  223. grub_strchr (const char *s, int c)
  224. {
  225. do
  226. {
  227. if (*s == c)
  228. return (char *) s;
  229. }
  230. while (*s++);
  231. return 0;
  232. }
  233. char *
  234. grub_strrchr (const char *s, int c)
  235. {
  236. char *p = NULL;
  237. do
  238. {
  239. if (*s == c)
  240. p = (char *) s;
  241. }
  242. while (*s++);
  243. return p;
  244. }
  245. int
  246. grub_strword (const char *haystack, const char *needle)
  247. {
  248. const char *n_pos = needle;
  249. while (grub_iswordseparator (*haystack))
  250. haystack++;
  251. while (*haystack)
  252. {
  253. /* Crawl both the needle and the haystack word we're on. */
  254. while(*haystack && !grub_iswordseparator (*haystack)
  255. && *haystack == *n_pos)
  256. {
  257. haystack++;
  258. n_pos++;
  259. }
  260. /* If we reached the end of both words at the same time, the word
  261. is found. If not, eat everything in the haystack that isn't the
  262. next word (or the end of string) and "reset" the needle. */
  263. if ( (!*haystack || grub_iswordseparator (*haystack))
  264. && (!*n_pos || grub_iswordseparator (*n_pos)))
  265. return 1;
  266. else
  267. {
  268. n_pos = needle;
  269. while (*haystack && !grub_iswordseparator (*haystack))
  270. haystack++;
  271. while (grub_iswordseparator (*haystack))
  272. haystack++;
  273. }
  274. }
  275. return 0;
  276. }
  277. int
  278. grub_isspace (int c)
  279. {
  280. return (c == '\n' || c == '\r' || c == ' ' || c == '\t');
  281. }
  282. unsigned long
  283. grub_strtoul (const char *str, char **end, int base)
  284. {
  285. unsigned long long num;
  286. num = grub_strtoull (str, end, base);
  287. #if GRUB_CPU_SIZEOF_LONG != 8
  288. if (num > ~0UL)
  289. {
  290. grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow is detected"));
  291. return ~0UL;
  292. }
  293. #endif
  294. return (unsigned long) num;
  295. }
  296. unsigned long long
  297. grub_strtoull (const char *str, char **end, int base)
  298. {
  299. unsigned long long num = 0;
  300. int found = 0;
  301. /* Skip white spaces. */
  302. /* grub_isspace checks that *str != '\0'. */
  303. while (grub_isspace (*str))
  304. str++;
  305. /* Guess the base, if not specified. The prefix `0x' means 16, and
  306. the prefix `0' means 8. */
  307. if (str[0] == '0')
  308. {
  309. if (str[1] == 'x')
  310. {
  311. if (base == 0 || base == 16)
  312. {
  313. base = 16;
  314. str += 2;
  315. }
  316. }
  317. else if (base == 0 && str[1] >= '0' && str[1] <= '7')
  318. base = 8;
  319. }
  320. if (base == 0)
  321. base = 10;
  322. while (*str)
  323. {
  324. unsigned long digit;
  325. digit = grub_tolower (*str) - '0';
  326. if (digit >= 'a' - '0')
  327. digit += '0' - 'a' + 10;
  328. else if (digit > 9)
  329. break;
  330. if (digit >= (unsigned long) base)
  331. break;
  332. found = 1;
  333. /* NUM * BASE + DIGIT > ~0ULL */
  334. if (num > grub_divmod64 (~0ULL - digit, base, 0))
  335. {
  336. grub_error (GRUB_ERR_OUT_OF_RANGE,
  337. N_("overflow is detected"));
  338. return ~0ULL;
  339. }
  340. num = num * base + digit;
  341. str++;
  342. }
  343. if (! found)
  344. {
  345. grub_error (GRUB_ERR_BAD_NUMBER,
  346. N_("unrecognized number"));
  347. return 0;
  348. }
  349. if (end)
  350. *end = (char *) str;
  351. return num;
  352. }
  353. char *
  354. grub_strdup (const char *s)
  355. {
  356. grub_size_t len;
  357. char *p;
  358. len = grub_strlen (s) + 1;
  359. p = (char *) grub_malloc (len);
  360. if (! p)
  361. return 0;
  362. return grub_memcpy (p, s, len);
  363. }
  364. char *
  365. grub_strndup (const char *s, grub_size_t n)
  366. {
  367. grub_size_t len;
  368. char *p;
  369. len = grub_strlen (s);
  370. if (len > n)
  371. len = n;
  372. p = (char *) grub_malloc (len + 1);
  373. if (! p)
  374. return 0;
  375. grub_memcpy (p, s, len);
  376. p[len] = '\0';
  377. return p;
  378. }
  379. /* clang detects that we're implementing here a memset so it decides to
  380. optimise and calls memset resulting in infinite recursion. With volatile
  381. we make it not optimise in this way. */
  382. #ifdef __clang__
  383. #define VOLATILE_CLANG volatile
  384. #else
  385. #define VOLATILE_CLANG
  386. #endif
  387. void *
  388. grub_memset (void *s, int c, grub_size_t len)
  389. {
  390. void *p = s;
  391. grub_uint8_t pattern8 = c;
  392. if (len >= 3 * sizeof (unsigned long))
  393. {
  394. unsigned long patternl = 0;
  395. grub_size_t i;
  396. for (i = 0; i < sizeof (unsigned long); i++)
  397. patternl |= ((unsigned long) pattern8) << (8 * i);
  398. while (len > 0 && (((grub_addr_t) p) & (sizeof (unsigned long) - 1)))
  399. {
  400. *(VOLATILE_CLANG grub_uint8_t *) p = pattern8;
  401. p = (grub_uint8_t *) p + 1;
  402. len--;
  403. }
  404. while (len >= sizeof (unsigned long))
  405. {
  406. *(VOLATILE_CLANG unsigned long *) p = patternl;
  407. p = (unsigned long *) p + 1;
  408. len -= sizeof (unsigned long);
  409. }
  410. }
  411. while (len > 0)
  412. {
  413. *(VOLATILE_CLANG grub_uint8_t *) p = pattern8;
  414. p = (grub_uint8_t *) p + 1;
  415. len--;
  416. }
  417. return s;
  418. }
  419. grub_size_t
  420. grub_strlen (const char *s)
  421. {
  422. const char *p = s;
  423. while (*p)
  424. p++;
  425. return p - s;
  426. }
  427. static inline void
  428. grub_reverse (char *str)
  429. {
  430. char *p = str + grub_strlen (str) - 1;
  431. while (str < p)
  432. {
  433. char tmp;
  434. tmp = *str;
  435. *str = *p;
  436. *p = tmp;
  437. str++;
  438. p--;
  439. }
  440. }
  441. /* Divide N by D, return the quotient, and store the remainder in *R. */
  442. grub_uint64_t
  443. grub_divmod64 (grub_uint64_t n, grub_uint64_t d, grub_uint64_t *r)
  444. {
  445. /* This algorithm is typically implemented by hardware. The idea
  446. is to get the highest bit in N, 64 times, by keeping
  447. upper(N * 2^i) = (Q * D + M), where upper
  448. represents the high 64 bits in 128-bits space. */
  449. unsigned bits = 64;
  450. grub_uint64_t q = 0;
  451. grub_uint64_t m = 0;
  452. /* ARM and IA64 don't have a fast 32-bit division.
  453. Using that code would just make us use software division routines, calling
  454. ourselves indirectly and hence getting infinite recursion.
  455. */
  456. #if !GRUB_DIVISION_IN_SOFTWARE
  457. /* Skip the slow computation if 32-bit arithmetic is possible. */
  458. if (n < 0xffffffff && d < 0xffffffff)
  459. {
  460. if (r)
  461. *r = ((grub_uint32_t) n) % (grub_uint32_t) d;
  462. return ((grub_uint32_t) n) / (grub_uint32_t) d;
  463. }
  464. #endif
  465. while (bits--)
  466. {
  467. m <<= 1;
  468. if (n & (1ULL << 63))
  469. m |= 1;
  470. q <<= 1;
  471. n <<= 1;
  472. if (m >= d)
  473. {
  474. q |= 1;
  475. m -= d;
  476. }
  477. }
  478. if (r)
  479. *r = m;
  480. return q;
  481. }
  482. /* Convert a long long value to a string. This function avoids 64-bit
  483. modular arithmetic or divisions. */
  484. static inline char *
  485. grub_lltoa (char *str, int c, unsigned long long n)
  486. {
  487. unsigned base = (c == 'x') ? 16 : 10;
  488. char *p;
  489. if ((long long) n < 0 && c == 'd')
  490. {
  491. n = (unsigned long long) (-((long long) n));
  492. *str++ = '-';
  493. }
  494. p = str;
  495. if (base == 16)
  496. do
  497. {
  498. unsigned d = (unsigned) (n & 0xf);
  499. *p++ = (d > 9) ? d + 'a' - 10 : d + '0';
  500. }
  501. while (n >>= 4);
  502. else
  503. /* BASE == 10 */
  504. do
  505. {
  506. grub_uint64_t m;
  507. n = grub_divmod64 (n, 10, &m);
  508. *p++ = m + '0';
  509. }
  510. while (n);
  511. *p = 0;
  512. grub_reverse (str);
  513. return p;
  514. }
  515. static void
  516. parse_printf_args (const char *fmt0, struct printf_args *args,
  517. va_list args_in)
  518. {
  519. const char *fmt;
  520. char c;
  521. grub_size_t n = 0;
  522. args->count = 0;
  523. COMPILE_TIME_ASSERT (sizeof (int) == sizeof (grub_uint32_t));
  524. COMPILE_TIME_ASSERT (sizeof (int) <= sizeof (long long));
  525. COMPILE_TIME_ASSERT (sizeof (long) <= sizeof (long long));
  526. COMPILE_TIME_ASSERT (sizeof (long long) == sizeof (void *)
  527. || sizeof (int) == sizeof (void *));
  528. fmt = fmt0;
  529. while ((c = *fmt++) != 0)
  530. {
  531. if (c != '%')
  532. continue;
  533. if (*fmt =='-')
  534. fmt++;
  535. while (grub_isdigit (*fmt))
  536. fmt++;
  537. if (*fmt == '$')
  538. fmt++;
  539. if (*fmt =='-')
  540. fmt++;
  541. while (grub_isdigit (*fmt))
  542. fmt++;
  543. if (*fmt =='.')
  544. fmt++;
  545. while (grub_isdigit (*fmt))
  546. fmt++;
  547. c = *fmt++;
  548. if (c == 'l')
  549. c = *fmt++;
  550. if (c == 'l')
  551. c = *fmt++;
  552. switch (c)
  553. {
  554. case 'p':
  555. case 'x':
  556. case 'u':
  557. case 'd':
  558. case 'c':
  559. case 'C':
  560. case 's':
  561. args->count++;
  562. break;
  563. }
  564. }
  565. if (args->count <= ARRAY_SIZE (args->prealloc))
  566. args->ptr = args->prealloc;
  567. else
  568. {
  569. args->ptr = grub_malloc (args->count * sizeof (args->ptr[0]));
  570. if (!args->ptr)
  571. {
  572. grub_errno = GRUB_ERR_NONE;
  573. args->ptr = args->prealloc;
  574. args->count = ARRAY_SIZE (args->prealloc);
  575. }
  576. }
  577. grub_memset (args->ptr, 0, args->count * sizeof (args->ptr[0]));
  578. fmt = fmt0;
  579. n = 0;
  580. while ((c = *fmt++) != 0)
  581. {
  582. int longfmt = 0;
  583. grub_size_t curn;
  584. const char *p;
  585. if (c != '%')
  586. continue;
  587. curn = n++;
  588. if (*fmt =='-')
  589. fmt++;
  590. p = fmt;
  591. while (grub_isdigit (*fmt))
  592. fmt++;
  593. if (*fmt == '$')
  594. {
  595. curn = grub_strtoull (p, 0, 10) - 1;
  596. fmt++;
  597. }
  598. if (*fmt =='-')
  599. fmt++;
  600. while (grub_isdigit (*fmt))
  601. fmt++;
  602. if (*fmt =='.')
  603. fmt++;
  604. while (grub_isdigit (*fmt))
  605. fmt++;
  606. c = *fmt++;
  607. if (c == '%')
  608. {
  609. n--;
  610. continue;
  611. }
  612. if (c == 'l')
  613. {
  614. c = *fmt++;
  615. longfmt = 1;
  616. }
  617. if (c == 'l')
  618. {
  619. c = *fmt++;
  620. longfmt = 2;
  621. }
  622. if (curn >= args->count)
  623. continue;
  624. switch (c)
  625. {
  626. case 'x':
  627. case 'u':
  628. args->ptr[curn].type = UNSIGNED_INT + longfmt;
  629. break;
  630. case 'd':
  631. args->ptr[curn].type = INT + longfmt;
  632. break;
  633. case 'p':
  634. case 's':
  635. if (sizeof (void *) == sizeof (long long))
  636. args->ptr[curn].type = UNSIGNED_LONGLONG;
  637. else
  638. args->ptr[curn].type = UNSIGNED_INT;
  639. break;
  640. case 'C':
  641. case 'c':
  642. args->ptr[curn].type = INT;
  643. break;
  644. }
  645. }
  646. for (n = 0; n < args->count; n++)
  647. switch (args->ptr[n].type)
  648. {
  649. case INT:
  650. args->ptr[n].ll = va_arg (args_in, int);
  651. break;
  652. case LONG:
  653. args->ptr[n].ll = va_arg (args_in, long);
  654. break;
  655. case UNSIGNED_INT:
  656. args->ptr[n].ll = va_arg (args_in, unsigned int);
  657. break;
  658. case UNSIGNED_LONG:
  659. args->ptr[n].ll = va_arg (args_in, unsigned long);
  660. break;
  661. case LONGLONG:
  662. case UNSIGNED_LONGLONG:
  663. args->ptr[n].ll = va_arg (args_in, long long);
  664. break;
  665. }
  666. }
  667. static inline void __attribute__ ((always_inline))
  668. write_char (char *str, grub_size_t *count, grub_size_t max_len, unsigned char ch)
  669. {
  670. if (*count < max_len)
  671. str[*count] = ch;
  672. (*count)++;
  673. }
  674. static int
  675. grub_vsnprintf_real (char *str, grub_size_t max_len, const char *fmt0,
  676. struct printf_args *args)
  677. {
  678. char c;
  679. grub_size_t n = 0;
  680. grub_size_t count = 0;
  681. const char *fmt;
  682. fmt = fmt0;
  683. while ((c = *fmt++) != 0)
  684. {
  685. unsigned int format1 = 0;
  686. unsigned int format2 = ~ 0U;
  687. char zerofill = ' ';
  688. char rightfill = 0;
  689. grub_size_t curn;
  690. if (c != '%')
  691. {
  692. write_char (str, &count, max_len,c);
  693. continue;
  694. }
  695. curn = n++;
  696. rescan:;
  697. if (*fmt =='-')
  698. {
  699. rightfill = 1;
  700. fmt++;
  701. }
  702. /* Read formatting parameters. */
  703. if (grub_isdigit (*fmt))
  704. {
  705. if (fmt[0] == '0')
  706. zerofill = '0';
  707. format1 = grub_strtoul (fmt, (char **) &fmt, 10);
  708. }
  709. if (*fmt == '.')
  710. fmt++;
  711. if (grub_isdigit (*fmt))
  712. format2 = grub_strtoul (fmt, (char **) &fmt, 10);
  713. if (*fmt == '$')
  714. {
  715. curn = format1 - 1;
  716. fmt++;
  717. format1 = 0;
  718. format2 = ~ 0U;
  719. zerofill = ' ';
  720. rightfill = 0;
  721. goto rescan;
  722. }
  723. c = *fmt++;
  724. if (c == 'l')
  725. c = *fmt++;
  726. if (c == 'l')
  727. c = *fmt++;
  728. if (c == '%')
  729. {
  730. write_char (str, &count, max_len,c);
  731. n--;
  732. continue;
  733. }
  734. if (curn >= args->count)
  735. continue;
  736. long long curarg = args->ptr[curn].ll;
  737. switch (c)
  738. {
  739. case 'p':
  740. write_char (str, &count, max_len, '0');
  741. write_char (str, &count, max_len, 'x');
  742. c = 'x';
  743. /* Fall through. */
  744. case 'x':
  745. case 'u':
  746. case 'd':
  747. {
  748. char tmp[32];
  749. const char *p = tmp;
  750. grub_size_t len;
  751. grub_size_t fill;
  752. len = grub_lltoa (tmp, c, curarg) - tmp;
  753. fill = len < format1 ? format1 - len : 0;
  754. if (! rightfill)
  755. while (fill--)
  756. write_char (str, &count, max_len, zerofill);
  757. while (*p)
  758. write_char (str, &count, max_len, *p++);
  759. if (rightfill)
  760. while (fill--)
  761. write_char (str, &count, max_len, zerofill);
  762. }
  763. break;
  764. case 'c':
  765. write_char (str, &count, max_len,curarg & 0xff);
  766. break;
  767. case 'C':
  768. {
  769. grub_uint32_t code = curarg;
  770. int shift;
  771. unsigned mask;
  772. if (code <= 0x7f)
  773. {
  774. shift = 0;
  775. mask = 0;
  776. }
  777. else if (code <= 0x7ff)
  778. {
  779. shift = 6;
  780. mask = 0xc0;
  781. }
  782. else if (code <= 0xffff)
  783. {
  784. shift = 12;
  785. mask = 0xe0;
  786. }
  787. else if (code <= 0x10ffff)
  788. {
  789. shift = 18;
  790. mask = 0xf0;
  791. }
  792. else
  793. {
  794. code = '?';
  795. shift = 0;
  796. mask = 0;
  797. }
  798. write_char (str, &count, max_len,mask | (code >> shift));
  799. for (shift -= 6; shift >= 0; shift -= 6)
  800. write_char (str, &count, max_len,0x80 | (0x3f & (code >> shift)));
  801. }
  802. break;
  803. case 's':
  804. {
  805. grub_size_t len = 0;
  806. grub_size_t fill;
  807. const char *p = ((char *) (grub_addr_t) curarg) ? : "(null)";
  808. grub_size_t i;
  809. while (len < format2 && p[len])
  810. len++;
  811. fill = len < format1 ? format1 - len : 0;
  812. if (!rightfill)
  813. while (fill--)
  814. write_char (str, &count, max_len, zerofill);
  815. for (i = 0; i < len; i++)
  816. write_char (str, &count, max_len,*p++);
  817. if (rightfill)
  818. while (fill--)
  819. write_char (str, &count, max_len, zerofill);
  820. }
  821. break;
  822. default:
  823. write_char (str, &count, max_len,c);
  824. break;
  825. }
  826. }
  827. if (count < max_len)
  828. str[count] = '\0';
  829. else
  830. str[max_len] = '\0';
  831. return count;
  832. }
  833. int
  834. grub_vsnprintf (char *str, grub_size_t n, const char *fmt, va_list ap)
  835. {
  836. grub_size_t ret;
  837. struct printf_args args;
  838. if (!n)
  839. return 0;
  840. n--;
  841. parse_printf_args (fmt, &args, ap);
  842. ret = grub_vsnprintf_real (str, n, fmt, &args);
  843. free_printf_args (&args);
  844. return ret < n ? ret : n;
  845. }
  846. int
  847. grub_snprintf (char *str, grub_size_t n, const char *fmt, ...)
  848. {
  849. va_list ap;
  850. int ret;
  851. va_start (ap, fmt);
  852. ret = grub_vsnprintf (str, n, fmt, ap);
  853. va_end (ap);
  854. return ret;
  855. }
  856. char *
  857. grub_xvasprintf (const char *fmt, va_list ap)
  858. {
  859. grub_size_t s, as = PREALLOC_SIZE;
  860. char *ret;
  861. struct printf_args args;
  862. parse_printf_args (fmt, &args, ap);
  863. while (1)
  864. {
  865. ret = grub_malloc (as + 1);
  866. if (!ret)
  867. {
  868. free_printf_args (&args);
  869. return NULL;
  870. }
  871. s = grub_vsnprintf_real (ret, as, fmt, &args);
  872. if (s <= as)
  873. {
  874. free_printf_args (&args);
  875. return ret;
  876. }
  877. grub_free (ret);
  878. as = s;
  879. }
  880. }
  881. char *
  882. grub_xasprintf (const char *fmt, ...)
  883. {
  884. va_list ap;
  885. char *ret;
  886. va_start (ap, fmt);
  887. ret = grub_xvasprintf (fmt, ap);
  888. va_end (ap);
  889. return ret;
  890. }
  891. /* Abort GRUB. This function does not return. */
  892. static void __attribute__ ((noreturn))
  893. grub_abort (void)
  894. {
  895. grub_printf ("\nAborted.");
  896. #ifndef GRUB_UTIL
  897. if (grub_term_inputs)
  898. #endif
  899. {
  900. grub_printf (" Press any key to exit.");
  901. grub_getkey ();
  902. }
  903. grub_exit ();
  904. }
  905. void
  906. grub_fatal (const char *fmt, ...)
  907. {
  908. va_list ap;
  909. va_start (ap, fmt);
  910. grub_vprintf (_(fmt), ap);
  911. va_end (ap);
  912. grub_refresh ();
  913. grub_abort ();
  914. }
  915. #if BOOT_TIME_STATS
  916. #include <grub/time.h>
  917. struct grub_boot_time *grub_boot_time_head;
  918. static struct grub_boot_time **boot_time_last = &grub_boot_time_head;
  919. void
  920. grub_real_boot_time (const char *file,
  921. const int line,
  922. const char *fmt, ...)
  923. {
  924. struct grub_boot_time *n;
  925. va_list args;
  926. grub_error_push ();
  927. n = grub_malloc (sizeof (*n));
  928. if (!n)
  929. {
  930. grub_errno = 0;
  931. grub_error_pop ();
  932. return;
  933. }
  934. n->file = file;
  935. n->line = line;
  936. n->tp = grub_get_time_ms ();
  937. n->next = 0;
  938. va_start (args, fmt);
  939. n->msg = grub_xvasprintf (fmt, args);
  940. va_end (args);
  941. *boot_time_last = n;
  942. boot_time_last = &n->next;
  943. grub_errno = 0;
  944. grub_error_pop ();
  945. }
  946. #endif