1
0

misc.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*++
  2. Copyright (c) 2016 Minoca Corp.
  3. This file is licensed under the terms of the GNU General Public License
  4. version 3. Alternative licensing terms are available. Contact
  5. info@minocacorp.com for details. See the LICENSE file at the root of this
  6. project for complete licensing information.
  7. Module Name:
  8. misc.c
  9. Abstract:
  10. This module implements miscellaneous OS support functions for the setup
  11. application.
  12. Author:
  13. Evan Green 19-Jan-2016
  14. Environment:
  15. User
  16. --*/
  17. //
  18. // ------------------------------------------------------------------- Includes
  19. //
  20. #include <assert.h>
  21. #include <errno.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <sys/types.h>
  26. #include <sys/wait.h>
  27. #include <unistd.h>
  28. #include "../setup.h"
  29. //
  30. // ---------------------------------------------------------------- Definitions
  31. //
  32. //
  33. // ------------------------------------------------------ Data Type Definitions
  34. //
  35. //
  36. // ----------------------------------------------- Internal Function Prototypes
  37. //
  38. //
  39. // -------------------------------------------------------------------- Globals
  40. //
  41. //
  42. // ------------------------------------------------------------------ Functions
  43. //
  44. INT
  45. SetupOsReboot (
  46. VOID
  47. )
  48. /*++
  49. Routine Description:
  50. This routine reboots the machine.
  51. Arguments:
  52. None.
  53. Return Value:
  54. 0 on success.
  55. Non-zero on failure.
  56. --*/
  57. {
  58. pid_t Child;
  59. int Status;
  60. Child = fork();
  61. if (Child == 0) {
  62. execlp("/sbin/reboot", "/sbin/reboot", NULL);
  63. exit(1);
  64. } else if (Child == -1) {
  65. fprintf(stderr, "Failed to fork.\n");
  66. return errno;
  67. } else {
  68. waitpid(Child, &Status, 0);
  69. if (!WIFEXITED(Status)) {
  70. fprintf(stderr, "Failed to execute /sbin/reboot.\n");
  71. }
  72. }
  73. return errno;
  74. }
  75. INT
  76. SetupOsGetPlatformName (
  77. PSTR *Name,
  78. PSETUP_RECIPE_ID Fallback
  79. )
  80. /*++
  81. Routine Description:
  82. This routine gets the platform name.
  83. Arguments:
  84. Name - Supplies a pointer where a pointer to an allocated string containing
  85. the SMBIOS system information product name will be returned if
  86. available. The caller is responsible for freeing this memory when done.
  87. Fallback - Supplies a fallback platform to use if the given platform
  88. string was not returned or did not match a known platform.
  89. Return Value:
  90. 0 on success.
  91. Non-zero on failure.
  92. --*/
  93. {
  94. SETUP_RECIPE_ID FallbackValue;
  95. //
  96. // TODO: Consider common-izing the Minoca code that looks through the
  97. // SMBIOS tables, which can be accessed at on Linux with root access at
  98. // /sys/firmware/dmi/tables/DMI.
  99. //
  100. FallbackValue = SetupRecipeNone;
  101. if (access("/sys/firmware/efi", F_OK) != F_OK) {
  102. if (sizeof(PVOID) == 8) {
  103. *Name = strdup("x86-64 PC");
  104. FallbackValue = SetupRecipePc64;
  105. } else {
  106. *Name = strdup("x86 PC");
  107. FallbackValue = SetupRecipePc32;
  108. }
  109. } else {
  110. if (sizeof(PVOID) == 8) {
  111. *Name = strdup("x86-64 UEFI-based PC");
  112. FallbackValue = SetupRecipePc64Efi;
  113. } else {
  114. *Name = strdup("x86 UEFI-based PC");
  115. FallbackValue = SetupRecipePc32Efi;
  116. }
  117. }
  118. if (Fallback != NULL) {
  119. *Fallback = FallbackValue;
  120. }
  121. if (*Name == NULL) {
  122. return ENOMEM;
  123. }
  124. return 0;
  125. }
  126. INT
  127. SetupOsGetSystemMemorySize (
  128. PULONGLONG Megabytes
  129. )
  130. /*++
  131. Routine Description:
  132. This routine returns the number of megabytes of memory installed on the
  133. currently running system.
  134. Arguments:
  135. Megabytes - Supplies a pointer to where the system memory capacity in
  136. megabytes will be returned on success.
  137. Return Value:
  138. 0 on success.
  139. Non-zero on failure.
  140. --*/
  141. {
  142. long PageCount;
  143. long PageSize;
  144. *Megabytes = 0;
  145. PageSize = sysconf(_SC_PAGE_SIZE);
  146. if (PageSize == -1) {
  147. return errno;
  148. }
  149. PageCount = sysconf(_SC_PHYS_PAGES);
  150. if (PageCount == -1) {
  151. return errno;
  152. }
  153. *Megabytes = ((ULONGLONG)PageSize * (ULONGLONG)PageCount) / 0x100000ULL;
  154. return 0;
  155. }
  156. //
  157. // --------------------------------------------------------- Internal Functions
  158. //