platform.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2013 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <config.h>
  19. #include <grub/util/install.h>
  20. #include <grub/emu/exec.h>
  21. #include <grub/emu/misc.h>
  22. #include <sys/types.h>
  23. #include <dirent.h>
  24. #include <string.h>
  25. #include <sys/utsname.h>
  26. static int
  27. is_not_empty_directory (const char *dir)
  28. {
  29. DIR *d;
  30. struct dirent *de;
  31. d = opendir (dir);
  32. if (!d)
  33. return 0;
  34. while ((de = readdir (d)))
  35. {
  36. if (strcmp (de->d_name, ".") == 0
  37. || strcmp (de->d_name, "..") == 0)
  38. continue;
  39. closedir (d);
  40. return 1;
  41. }
  42. closedir (d);
  43. return 0;
  44. }
  45. static int
  46. is_64_kernel (void)
  47. {
  48. struct utsname un;
  49. if (uname (&un) < 0)
  50. return 0;
  51. return strcmp (un.machine, "x86_64") == 0;
  52. }
  53. static int
  54. read_platform_size (void)
  55. {
  56. FILE *fp;
  57. char *buf = NULL;
  58. size_t len = 0;
  59. int ret = 0;
  60. /* Newer kernels can tell us directly about the size of the
  61. * underlying firmware - let's see if that interface is there. */
  62. fp = grub_util_fopen ("/sys/firmware/efi/fw_platform_size", "r");
  63. if (fp != NULL)
  64. {
  65. if (getline (&buf, &len, fp) >= 3) /* 2 digits plus newline */
  66. {
  67. if (strncmp (buf, "32", 2) == 0)
  68. ret = 32;
  69. else if (strncmp (buf, "64", 2) == 0)
  70. ret = 64;
  71. }
  72. free (buf);
  73. fclose (fp);
  74. }
  75. if (ret == 0)
  76. {
  77. /* Unrecognised - fall back to matching the kernel size
  78. * instead */
  79. if (is_64_kernel ())
  80. ret = 64;
  81. else
  82. ret = 32;
  83. }
  84. return ret;
  85. }
  86. /* Are we running on an EFI-based system? */
  87. static int
  88. is_efi_system (void)
  89. {
  90. /*
  91. * Linux uses efivarfs (mounted on /sys/firmware/efi/efivars) to access the
  92. * EFI variable store. Some legacy systems may still use the deprecated
  93. * efivars interface (accessed through /sys/firmware/efi/vars). Where both
  94. * are present, libefivar will use the former in preference, so attempting
  95. * to load efivars will not interfere with later operations.
  96. */
  97. grub_util_exec_redirect_all ((const char * []){ "modprobe", "efivars", NULL },
  98. NULL, NULL, "/dev/null");
  99. grub_util_info ("Looking for /sys/firmware/efi ..");
  100. if (is_not_empty_directory ("/sys/firmware/efi"))
  101. {
  102. grub_util_info ("...found");
  103. return 1;
  104. }
  105. else
  106. {
  107. grub_util_info ("... not found");
  108. return 0;
  109. }
  110. }
  111. const char *
  112. grub_install_get_default_arm_platform (void)
  113. {
  114. if (is_efi_system())
  115. return "arm-efi";
  116. else
  117. return "arm-uboot";
  118. }
  119. const char *
  120. grub_install_get_default_x86_platform (void)
  121. {
  122. if (is_efi_system())
  123. {
  124. if (read_platform_size() == 64)
  125. return "x86_64-efi";
  126. else
  127. return "i386-efi";
  128. }
  129. grub_util_info ("Looking for /proc/device-tree ..");
  130. if (is_not_empty_directory ("/proc/device-tree"))
  131. {
  132. grub_util_info ("...found");
  133. return "i386-ieee1275";
  134. }
  135. grub_util_info ("... not found");
  136. return "i386-pc";
  137. }