efiutil.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*-
  2. * Copyright (c) 2017-2019 Netflix, Inc.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  14. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  16. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  17. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  19. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  20. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  22. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  23. * SUCH DAMAGE.
  24. */
  25. #include <sys/cdefs.h>
  26. #include <ctype.h>
  27. #include <efivar.h>
  28. #include <efivar-dp.h>
  29. #include <err.h>
  30. #include <errno.h>
  31. #include <getopt.h>
  32. #include <stddef.h>
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include <unistd.h>
  37. #include "efiutil.h"
  38. #include "efichar.h"
  39. #include <efivar-dp.h>
  40. /*
  41. * Dump the data as ASCII data, which is a pretty
  42. * printed form
  43. */
  44. void
  45. asciidump(uint8_t *data, size_t datalen)
  46. {
  47. size_t i;
  48. int len;
  49. len = 0;
  50. for (i = 0; i < datalen; i++) {
  51. if (isprint(data[i])) {
  52. len++;
  53. if (len > 80) {
  54. len = 0;
  55. printf("\n");
  56. }
  57. printf("%c", data[i]);
  58. } else {
  59. len +=3;
  60. if (len > 80) {
  61. len = 0;
  62. printf("\n");
  63. }
  64. printf("%%%02x", data[i]);
  65. }
  66. }
  67. printf("\n");
  68. }
  69. void
  70. utf8dump(uint8_t *data, size_t datalen)
  71. {
  72. char *utf8 = NULL;
  73. efi_char *ucs2;
  74. /*
  75. * NUL terminate the string. Not all strings need it, but some
  76. * do and an extra NUL won't change what's printed.
  77. */
  78. ucs2 = malloc(datalen + sizeof(efi_char));
  79. memcpy(ucs2, data, datalen);
  80. ucs2[datalen / sizeof(efi_char)] = 0;
  81. ucs2_to_utf8(ucs2, &utf8);
  82. printf("%s\n", utf8);
  83. free(utf8);
  84. free(ucs2);
  85. }
  86. void
  87. hexdump(uint8_t *data, size_t datalen)
  88. {
  89. size_t i;
  90. for (i = 0; i < datalen; i++) {
  91. if (i % 16 == 0) {
  92. if (i != 0)
  93. printf("\n");
  94. printf("%04x: ", (int)i);
  95. }
  96. printf("%02x ", data[i]);
  97. }
  98. printf("\n");
  99. }
  100. void
  101. bindump(uint8_t *data, size_t datalen)
  102. {
  103. write(1, data, datalen);
  104. }
  105. #define LOAD_OPTION_ACTIVE 1
  106. #define SIZE(dp, edp) (size_t)((intptr_t)(void *)edp - (intptr_t)(void *)dp)
  107. void
  108. efi_print_load_option(uint8_t *data, size_t datalen, int Aflag, int bflag, int uflag)
  109. {
  110. char *dev, *relpath, *abspath;
  111. uint8_t *ep = data + datalen;
  112. uint8_t *walker = data;
  113. uint32_t attr;
  114. uint16_t fplen;
  115. efi_char *descr;
  116. efidp dp, edp;
  117. char *str = NULL;
  118. char buf[1024];
  119. int len;
  120. void *opt;
  121. int optlen;
  122. int rv;
  123. if (datalen < sizeof(attr) + sizeof(fplen) + sizeof(efi_char))
  124. return;
  125. // First 4 bytes are attribute flags
  126. attr = le32dec(walker);
  127. walker += sizeof(attr);
  128. // Next two bytes are length of the file paths
  129. fplen = le16dec(walker);
  130. walker += sizeof(fplen);
  131. // Next we have a 0 terminated UCS2 string that we know to be aligned
  132. descr = (efi_char *)(intptr_t)(void *)walker;
  133. len = ucs2len(descr); // XXX need to sanity check that len < (datalen - (ep - walker) / 2)
  134. walker += (len + 1) * sizeof(efi_char);
  135. if (walker > ep)
  136. return;
  137. // Now we have fplen bytes worth of file path stuff
  138. dp = (efidp)walker;
  139. walker += fplen;
  140. if (walker > ep)
  141. return;
  142. edp = (efidp)walker;
  143. // Everything left is the binary option args
  144. opt = walker;
  145. optlen = ep - walker;
  146. // We got to here, everything is good
  147. printf("%c ", attr & LOAD_OPTION_ACTIVE ? '*' : ' ');
  148. ucs2_to_utf8(descr, &str);
  149. printf("%s\n", str);
  150. free(str);
  151. if (fplen <= 4) {
  152. printf("Empty path\n");
  153. } else {
  154. while (dp < edp && SIZE(dp, edp) > sizeof(efidp_header)) {
  155. efidp_format_device_path(buf, sizeof(buf), dp, SIZE(dp, edp));
  156. rv = efivar_device_path_to_unix_path(dp, &dev, &relpath, &abspath);
  157. dp = (efidp)((char *)dp + efidp_size(dp));
  158. printf(" %s\n", buf);
  159. if (rv == 0) {
  160. printf(" %*s:%s\n", len + (int)strlen(dev), dev, relpath);
  161. free(dev);
  162. free(relpath);
  163. free(abspath);
  164. }
  165. }
  166. }
  167. if (optlen == 0)
  168. return;
  169. printf("Option:\n");
  170. if (Aflag)
  171. asciidump(opt, optlen);
  172. else if (bflag)
  173. bindump(opt, optlen);
  174. else if (uflag)
  175. utf8dump(opt, optlen);
  176. else
  177. hexdump(opt, optlen);
  178. }