misc.c 23 KB

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