dmi.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * This file is part of the flashrom project.
  3. *
  4. * Copyright (C) 2009,2010 Michael Karcher
  5. *
  6. * This program 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 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program 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 this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <string.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include "flash.h"
  24. #include "programmer.h"
  25. int has_dmi_support = 0;
  26. #if STANDALONE
  27. /* Stub to indicate missing DMI functionality.
  28. * has_dmi_support is 0 by default, so nothing to do here.
  29. * Because dmidecode is not available on all systems, the goal is to implement
  30. * the DMI subset we need directly in this file.
  31. */
  32. void dmi_init(void)
  33. {
  34. }
  35. int dmi_match(const char *pattern)
  36. {
  37. return 0;
  38. }
  39. #else /* STANDALONE */
  40. static const char *dmidecode_names[] = {
  41. "system-manufacturer",
  42. "system-product-name",
  43. "system-version",
  44. "baseboard-manufacturer",
  45. "baseboard-product-name",
  46. "baseboard-version",
  47. };
  48. /* This list is used to identify supposed laptops. The is_laptop field has the
  49. * following meaning:
  50. * - 0: in all likelihood not a laptop
  51. * - 1: in all likelihood a laptop
  52. * - 2: chassis-type is not specific enough
  53. * A full list of chassis types can be found in the System Management BIOS
  54. * (SMBIOS) Reference Specification 2.7.0 section 7.4.1 "Chassis Types" at
  55. * http://www.dmtf.org/sites/default/files/standards/documents/DSP0134_2.7.0.pdf
  56. * The types below are the most common ones.
  57. */
  58. static const struct {
  59. unsigned char type;
  60. unsigned char is_laptop;
  61. const char *name;
  62. } dmi_chassis_types[] = {
  63. {0x01, 2, "Other"},
  64. {0x02, 2, "Unknown"},
  65. {0x03, 0, "Desktop",},
  66. {0x06, 0, "Mini Tower"},
  67. {0x07, 0, "Tower"},
  68. {0x08, 1, "Portable"},
  69. {0x09, 1, "Laptop"},
  70. {0x0a, 1, "Notebook"},
  71. {0x0b, 1, "Hand Held"},
  72. {0x0e, 1, "Sub Notebook"},
  73. {0x11, 0, "Main Server Chassis"},
  74. {0x17, 0, "Rack Mount Chassis"},
  75. {0x18, 0, "Sealed-case PC"}, /* used by Supermicro (X8SIE) */
  76. };
  77. #define DMI_COMMAND_LEN_MAX 260
  78. static const char *dmidecode_command = "dmidecode";
  79. static char *dmistrings[ARRAY_SIZE(dmidecode_names)];
  80. /* Strings longer than 4096 in DMI are just insane. */
  81. #define DMI_MAX_ANSWER_LEN 4096
  82. static char *get_dmi_string(const char *string_name)
  83. {
  84. FILE *dmidecode_pipe;
  85. char *result;
  86. char answerbuf[DMI_MAX_ANSWER_LEN];
  87. char commandline[DMI_COMMAND_LEN_MAX + 40];
  88. snprintf(commandline, sizeof(commandline),
  89. "%s -s %s", dmidecode_command, string_name);
  90. dmidecode_pipe = popen(commandline, "r");
  91. if (!dmidecode_pipe) {
  92. msg_perr("DMI pipe open error\n");
  93. return NULL;
  94. }
  95. /* Kill lines starting with '#', as recent dmidecode versions
  96. * have the quirk to emit a "# SMBIOS implementations newer..."
  97. * message even on "-s" if the SMBIOS declares a
  98. * newer-than-supported version number, while it *should* only print
  99. * the requested string.
  100. */
  101. do {
  102. if (!fgets(answerbuf, DMI_MAX_ANSWER_LEN, dmidecode_pipe)) {
  103. if (ferror(dmidecode_pipe)) {
  104. msg_perr("DMI pipe read error\n");
  105. pclose(dmidecode_pipe);
  106. return NULL;
  107. } else {
  108. answerbuf[0] = 0; /* Hit EOF */
  109. }
  110. }
  111. } while (answerbuf[0] == '#');
  112. /* Toss all output above DMI_MAX_ANSWER_LEN away to prevent
  113. deadlock on pclose. */
  114. while (!feof(dmidecode_pipe))
  115. getc(dmidecode_pipe);
  116. if (pclose(dmidecode_pipe) != 0) {
  117. msg_perr("dmidecode execution unsuccessful - continuing "
  118. "without DMI info\n");
  119. return NULL;
  120. }
  121. /* Chomp trailing newline. */
  122. if (answerbuf[0] != 0 && answerbuf[strlen(answerbuf) - 1] == '\n')
  123. answerbuf[strlen(answerbuf) - 1] = 0;
  124. msg_pdbg("DMI string %s: \"%s\"\n", string_name, answerbuf);
  125. result = strdup(answerbuf);
  126. if (!result)
  127. puts("WARNING: Out of memory - DMI support fails");
  128. return result;
  129. }
  130. void dmi_init(void)
  131. {
  132. int i;
  133. char *chassis_type;
  134. has_dmi_support = 1;
  135. for (i = 0; i < ARRAY_SIZE(dmidecode_names); i++) {
  136. dmistrings[i] = get_dmi_string(dmidecode_names[i]);
  137. if (!dmistrings[i]) {
  138. has_dmi_support = 0;
  139. return;
  140. }
  141. }
  142. chassis_type = get_dmi_string("chassis-type");
  143. if (chassis_type == NULL)
  144. return;
  145. is_laptop = 2;
  146. for (i = 0; i < ARRAY_SIZE(dmi_chassis_types); i++) {
  147. if (strcasecmp(chassis_type, dmi_chassis_types[i].name) == 0) {
  148. is_laptop = dmi_chassis_types[i].is_laptop;
  149. break;
  150. }
  151. }
  152. switch (is_laptop) {
  153. case 1:
  154. msg_pdbg("Laptop detected via DMI.\n");
  155. break;
  156. case 2:
  157. msg_pdbg("DMI chassis-type is not specific enough.\n");
  158. break;
  159. }
  160. free(chassis_type);
  161. }
  162. /**
  163. * Does an substring/prefix/postfix/whole-string match.
  164. *
  165. * The pattern is matched as-is. The only metacharacters supported are '^'
  166. * at the beginning and '$' at the end. So you can look for "^prefix",
  167. * "suffix$", "substring" or "^complete string$".
  168. *
  169. * @param value The string to check.
  170. * @param pattern The pattern.
  171. * @return Nonzero if pattern matches.
  172. */
  173. static int dmi_compare(const char *value, const char *pattern)
  174. {
  175. int anchored = 0;
  176. int patternlen;
  177. msg_pspew("matching %s against %s\n", value, pattern);
  178. /* The empty string is part of all strings! */
  179. if (pattern[0] == 0)
  180. return 1;
  181. if (pattern[0] == '^') {
  182. anchored = 1;
  183. pattern++;
  184. }
  185. patternlen = strlen(pattern);
  186. if (pattern[patternlen - 1] == '$') {
  187. int valuelen = strlen(value);
  188. patternlen--;
  189. if (patternlen > valuelen)
  190. return 0;
  191. /* full string match: require same length */
  192. if (anchored && (valuelen != patternlen))
  193. return 0;
  194. /* start character to make ends match */
  195. value += valuelen - patternlen;
  196. anchored = 1;
  197. }
  198. if (anchored)
  199. return strncmp(value, pattern, patternlen) == 0;
  200. else
  201. return strstr(value, pattern) != NULL;
  202. }
  203. int dmi_match(const char *pattern)
  204. {
  205. int i;
  206. if (!has_dmi_support)
  207. return 0;
  208. for (i = 0; i < ARRAY_SIZE(dmidecode_names); i++)
  209. if (dmi_compare(dmistrings[i], pattern))
  210. return 1;
  211. return 0;
  212. }
  213. #endif /* STANDALONE */