dmi.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. /*
  2. * This file is part of the flashrom project.
  3. *
  4. * Copyright (C) 2000-2002 Alan Cox <alan@redhat.com>
  5. * Copyright (C) 2002-2010 Jean Delvare <khali@linux-fr.org>
  6. * Copyright (C) 2009,2010 Michael Karcher
  7. * Copyright (C) 2011-2013 Stefan Tauner
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. /* strnlen is in POSIX but was a GNU extension up to glibc 2.10 */
  24. #if (__GLIBC__ == 2 && __GLIBC_MINOR__ < 10) || __GLIBC__ < 2
  25. #define _GNU_SOURCE
  26. #else
  27. #define _POSIX_C_SOURCE 200809L
  28. #endif
  29. #include <strings.h>
  30. #include <string.h>
  31. #include <ctype.h>
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include "flash.h"
  35. #include "programmer.h"
  36. #if defined(__i386__) || defined(__x86_64__)
  37. /* Enable SMBIOS decoding. Currently legacy DMI decoding is enough. */
  38. #define SM_SUPPORT 0
  39. /* Strings longer than 4096 in DMI are just insane. */
  40. #define DMI_MAX_ANSWER_LEN 4096
  41. int has_dmi_support = 0;
  42. static struct {
  43. const char *const keyword;
  44. const uint8_t type;
  45. const uint8_t offset;
  46. char *value;
  47. } dmi_strings[] = {
  48. { "system-manufacturer", 1, 0x04, NULL },
  49. { "system-product-name", 1, 0x05, NULL },
  50. { "system-version", 1, 0x06, NULL },
  51. { "baseboard-manufacturer", 2, 0x04, NULL },
  52. { "baseboard-product-name", 2, 0x05, NULL },
  53. { "baseboard-version", 2, 0x06, NULL },
  54. };
  55. /* This list is used to identify supposed laptops. The is_laptop field has the
  56. * following meaning:
  57. * - 0: in all likelihood not a laptop
  58. * - 1: in all likelihood a laptop
  59. * - 2: chassis-type is not specific enough
  60. * A full list of chassis types can be found in the System Management BIOS
  61. * (SMBIOS) Reference Specification 2.7.0 section 7.4.1 "Chassis Types" at
  62. * http://www.dmtf.org/sites/default/files/standards/documents/DSP0134_2.7.0.pdf
  63. * The types below are the most common ones.
  64. */
  65. static const struct {
  66. uint8_t type;
  67. uint8_t is_laptop;
  68. char *name;
  69. } dmi_chassis_types[] = {
  70. {0x01, 2, "Other"},
  71. {0x02, 2, "Unknown"},
  72. {0x03, 0, "Desktop"},
  73. {0x04, 0, "Low Profile Desktop"},
  74. {0x06, 0, "Mini Tower"},
  75. {0x07, 0, "Tower"},
  76. {0x08, 1, "Portable"},
  77. {0x09, 1, "Laptop"},
  78. {0x0a, 1, "Notebook"},
  79. {0x0b, 1, "Hand Held"},
  80. {0x0e, 1, "Sub Notebook"},
  81. {0x11, 0, "Main Server Chassis"},
  82. {0x17, 0, "Rack Mount Chassis"},
  83. {0x18, 0, "Sealed-case PC"}, /* used by Supermicro (X8SIE) */
  84. {0x19, 0, "Multi-system"}, /* used by Supermicro (X7DWT) */
  85. };
  86. #if CONFIG_INTERNAL_DMI == 1
  87. static bool dmi_checksum(const uint8_t * const buf, size_t len)
  88. {
  89. uint8_t sum = 0;
  90. size_t a;
  91. for (a = 0; a < len; a++)
  92. sum += buf[a];
  93. return (sum == 0);
  94. }
  95. /** Retrieve a DMI string.
  96. *
  97. * See SMBIOS spec. section 6.1.3 "Text strings".
  98. * The table will be unmapped ASAP, hence return a duplicated & sanitized string that needs to be freed later.
  99. *
  100. * \param buf the buffer to search through (usually appended directly to a DMI structure)
  101. * \param string_id index of the string to look for
  102. * \param limit pointer to the first byte beyond \em buf
  103. */
  104. static char *dmi_string(const char *buf, uint8_t string_id, const char *limit)
  105. {
  106. size_t i, len;
  107. if (string_id == 0)
  108. return strdup("Not Specified");
  109. while (string_id > 1 && string_id--) {
  110. if (buf >= limit) {
  111. msg_perr("DMI table is broken (string portion out of bounds)!\n");
  112. return strdup("<OUT OF BOUNDS>");
  113. }
  114. buf += strnlen(buf, limit - buf) + 1;
  115. }
  116. if (!*buf) /* as long as the current byte we're on isn't null */
  117. return strdup("<BAD INDEX>");
  118. len = strnlen(buf, limit - buf);
  119. char *newbuf = malloc(len + 1);
  120. if (newbuf == NULL) {
  121. msg_perr("Out of memory!\n");
  122. return NULL;
  123. }
  124. /* fix junk bytes in the string */
  125. for (i = 0; i < len && buf[i] != '\0'; i++) {
  126. if (isprint((unsigned char)buf[i]))
  127. newbuf[i] = buf[i];
  128. else
  129. newbuf[i] = ' ';
  130. }
  131. newbuf[i] = '\0';
  132. return newbuf;
  133. }
  134. static void dmi_chassis_type(uint8_t code)
  135. {
  136. int i;
  137. code &= 0x7f; /* bits 6:0 are chassis type, 7th bit is the lock bit */
  138. is_laptop = 2;
  139. for (i = 0; i < ARRAY_SIZE(dmi_chassis_types); i++) {
  140. if (code == dmi_chassis_types[i].type) {
  141. msg_pdbg("DMI string chassis-type: \"%s\"\n", dmi_chassis_types[i].name);
  142. is_laptop = dmi_chassis_types[i].is_laptop;
  143. break;
  144. }
  145. }
  146. }
  147. static void dmi_table(uint32_t base, uint16_t len, uint16_t num)
  148. {
  149. int i = 0, j = 0;
  150. uint8_t *dmi_table_mem = physmap_ro("DMI Table", base, len);
  151. if (dmi_table_mem == NULL) {
  152. msg_perr("Unable to access DMI Table\n");
  153. return;
  154. }
  155. uint8_t *data = dmi_table_mem;
  156. uint8_t *limit = dmi_table_mem + len;
  157. /* SMBIOS structure header is always 4 B long and contains:
  158. * - uint8_t type; // see dmi_chassis_types's type
  159. * - uint8_t length; // data section w/ header w/o strings
  160. * - uint16_t handle;
  161. */
  162. while (i < num && data + 4 < limit) {
  163. /* - If a short entry is found (less than 4 bytes), not only it
  164. * is invalid, but we cannot reliably locate the next entry.
  165. * - If the length value indicates that this structure spreads
  166. * across the table border, something is fishy too.
  167. * Better stop at this point, and let the user know his/her
  168. * table is broken.
  169. */
  170. if (data[1] < 4 || data + data[1] >= limit) {
  171. msg_perr("DMI table is broken (bogus header)!\n");
  172. break;
  173. }
  174. if(data[0] == 3) {
  175. if (data + 5 < limit)
  176. dmi_chassis_type(data[5]);
  177. else /* the table is broken, but laptop detection is optional, hence continue. */
  178. msg_pwarn("DMI table is broken (chassis_type out of bounds)!\n");
  179. } else
  180. for (j = 0; j < ARRAY_SIZE(dmi_strings); j++) {
  181. uint8_t offset = dmi_strings[j].offset;
  182. uint8_t type = dmi_strings[j].type;
  183. if (data[0] != type)
  184. continue;
  185. if (data[1] <= offset || data + offset >= limit) {
  186. msg_perr("DMI table is broken (offset out of bounds)!\n");
  187. goto out;
  188. }
  189. dmi_strings[j].value = dmi_string((const char *)(data + data[1]), data[offset],
  190. (const char *)limit);
  191. }
  192. /* Find next structure by skipping data and string sections */
  193. data += data[1];
  194. while (data + 1 <= limit) {
  195. if (data[0] == 0 && data[1] == 0)
  196. break;
  197. data++;
  198. }
  199. data += 2;
  200. i++;
  201. }
  202. out:
  203. physunmap(dmi_table_mem, len);
  204. }
  205. #if SM_SUPPORT
  206. static int smbios_decode(uint8_t *buf)
  207. {
  208. /* TODO: other checks mentioned in the conformance guidelines? */
  209. if (!dmi_checksum(buf, buf[0x05]) ||
  210. (memcmp(buf + 0x10, "_DMI_", 5) != 0) ||
  211. !dmi_checksum(buf + 0x10, 0x0F))
  212. return 0;
  213. dmi_table(mmio_readl(buf + 0x18), mmio_readw(buf + 0x16), mmio_readw(buf + 0x1C));
  214. return 1;
  215. }
  216. #endif
  217. static int legacy_decode(uint8_t *buf)
  218. {
  219. if (!dmi_checksum(buf, 0x0F))
  220. return 1;
  221. dmi_table(mmio_readl(buf + 0x08), mmio_readw(buf + 0x06), mmio_readw(buf + 0x0C));
  222. return 0;
  223. }
  224. int dmi_fill(void)
  225. {
  226. size_t fp;
  227. uint8_t *dmi_mem;
  228. int ret = 1;
  229. msg_pdbg("Using Internal DMI decoder.\n");
  230. /* There are two ways specified to gain access to the SMBIOS table:
  231. * - EFI's configuration table contains a pointer to the SMBIOS table. On linux it can be obtained from
  232. * sysfs. EFI's SMBIOS GUID is: {0xeb9d2d31,0x2d88,0x11d3,0x9a,0x16,0x0,0x90,0x27,0x3f,0xc1,0x4d}
  233. * - Scanning physical memory address range 0x000F0000h to 0x000FFFFF for the anchor-string(s). */
  234. dmi_mem = physmap_ro("DMI", 0xF0000, 0x10000);
  235. if (dmi_mem == ERROR_PTR)
  236. return ret;
  237. for (fp = 0; fp <= 0xFFF0; fp += 16) {
  238. #if SM_SUPPORT
  239. if (memcmp(dmi_mem + fp, "_SM_", 4) == 0 && fp <= 0xFFE0) {
  240. if (smbios_decode(dmi_mem + fp)) // FIXME: length check
  241. goto out;
  242. } else
  243. #endif
  244. if (memcmp(dmi_mem + fp, "_DMI_", 5) == 0)
  245. if (legacy_decode(dmi_mem + fp) == 0) {
  246. ret = 0;
  247. goto out;
  248. }
  249. }
  250. msg_pinfo("No DMI table found.\n");
  251. out:
  252. physunmap(dmi_mem, 0x10000);
  253. return ret;
  254. }
  255. #else /* CONFIG_INTERNAL_DMI */
  256. #define DMI_COMMAND_LEN_MAX 300
  257. static const char *dmidecode_command = "dmidecode";
  258. static char *get_dmi_string(const char *string_name)
  259. {
  260. FILE *dmidecode_pipe;
  261. char *result;
  262. char answerbuf[DMI_MAX_ANSWER_LEN];
  263. char commandline[DMI_COMMAND_LEN_MAX];
  264. snprintf(commandline, sizeof(commandline),
  265. "%s -s %s", dmidecode_command, string_name);
  266. dmidecode_pipe = popen(commandline, "r");
  267. if (!dmidecode_pipe) {
  268. msg_perr("Opening DMI pipe failed!\n");
  269. return NULL;
  270. }
  271. /* Kill lines starting with '#', as recent dmidecode versions
  272. * have the quirk to emit a "# SMBIOS implementations newer..."
  273. * message even on "-s" if the SMBIOS declares a
  274. * newer-than-supported version number, while it *should* only print
  275. * the requested string.
  276. */
  277. do {
  278. if (!fgets(answerbuf, DMI_MAX_ANSWER_LEN, dmidecode_pipe)) {
  279. if (ferror(dmidecode_pipe)) {
  280. msg_perr("DMI pipe read error\n");
  281. pclose(dmidecode_pipe);
  282. return NULL;
  283. } else {
  284. answerbuf[0] = 0; /* Hit EOF */
  285. }
  286. }
  287. } while (answerbuf[0] == '#');
  288. /* Discard all output exceeding DMI_MAX_ANSWER_LEN to prevent deadlock on pclose. */
  289. while (!feof(dmidecode_pipe))
  290. getc(dmidecode_pipe);
  291. if (pclose(dmidecode_pipe) != 0) {
  292. msg_pwarn("dmidecode execution unsuccessful - continuing without DMI info\n");
  293. return NULL;
  294. }
  295. /* Chomp trailing newline. */
  296. if (answerbuf[0] != 0 && answerbuf[strlen(answerbuf) - 1] == '\n')
  297. answerbuf[strlen(answerbuf) - 1] = 0;
  298. result = strdup(answerbuf);
  299. if (result == NULL)
  300. msg_pwarn("Warning: Out of memory - DMI support fails");
  301. return result;
  302. }
  303. int dmi_fill(void)
  304. {
  305. int i;
  306. char *chassis_type;
  307. msg_pdbg("Using External DMI decoder.\n");
  308. for (i = 0; i < ARRAY_SIZE(dmi_strings); i++) {
  309. dmi_strings[i].value = get_dmi_string(dmi_strings[i].keyword);
  310. if (dmi_strings[i].value == NULL)
  311. return 1;
  312. }
  313. chassis_type = get_dmi_string("chassis-type");
  314. if (chassis_type == NULL)
  315. return 0; /* chassis-type handling is optional anyway */
  316. msg_pdbg("DMI string chassis-type: \"%s\"\n", chassis_type);
  317. is_laptop = 2;
  318. for (i = 0; i < ARRAY_SIZE(dmi_chassis_types); i++) {
  319. if (strcasecmp(chassis_type, dmi_chassis_types[i].name) == 0) {
  320. is_laptop = dmi_chassis_types[i].is_laptop;
  321. break;
  322. }
  323. }
  324. free(chassis_type);
  325. return 0;
  326. }
  327. #endif /* CONFIG_INTERNAL_DMI */
  328. static int dmi_shutdown(void *data)
  329. {
  330. int i;
  331. for (i = 0; i < ARRAY_SIZE(dmi_strings); i++) {
  332. free(dmi_strings[i].value);
  333. dmi_strings[i].value = NULL;
  334. }
  335. return 0;
  336. }
  337. void dmi_init(void)
  338. {
  339. /* Register shutdown function before we allocate anything. */
  340. if (register_shutdown(dmi_shutdown, NULL)) {
  341. msg_pwarn("Warning: Could not register DMI shutdown function - continuing without DMI info.\n");
  342. return;
  343. }
  344. /* dmi_fill fills the dmi_strings array, and if possible sets the global is_laptop variable. */
  345. if (dmi_fill() != 0)
  346. return;
  347. switch (is_laptop) {
  348. case 1:
  349. msg_pdbg("Laptop detected via DMI.\n");
  350. break;
  351. case 2:
  352. msg_pdbg("DMI chassis-type is not specific enough.\n");
  353. break;
  354. }
  355. has_dmi_support = 1;
  356. int i;
  357. for (i = 0; i < ARRAY_SIZE(dmi_strings); i++) {
  358. msg_pdbg("DMI string %s: \"%s\"\n", dmi_strings[i].keyword,
  359. (dmi_strings[i].value == NULL) ? "" : dmi_strings[i].value);
  360. }
  361. }
  362. /**
  363. * Does an substring/prefix/postfix/whole-string match.
  364. *
  365. * The pattern is matched as-is. The only metacharacters supported are '^'
  366. * at the beginning and '$' at the end. So you can look for "^prefix",
  367. * "suffix$", "substring" or "^complete string$".
  368. *
  369. * @param value The non-NULL string to check.
  370. * @param pattern The non-NULL pattern.
  371. * @return Nonzero if pattern matches.
  372. */
  373. static int dmi_compare(const char *value, const char *pattern)
  374. {
  375. int anchored = 0;
  376. int patternlen;
  377. msg_pspew("matching %s against %s\n", value, pattern);
  378. /* The empty string is part of all strings! */
  379. if (pattern[0] == 0)
  380. return 1;
  381. if (pattern[0] == '^') {
  382. anchored = 1;
  383. pattern++;
  384. }
  385. patternlen = strlen(pattern);
  386. if (pattern[patternlen - 1] == '$') {
  387. int valuelen = strlen(value);
  388. patternlen--;
  389. if (patternlen > valuelen)
  390. return 0;
  391. /* full string match: require same length */
  392. if (anchored && (valuelen != patternlen))
  393. return 0;
  394. /* start character to make ends match */
  395. value += valuelen - patternlen;
  396. anchored = 1;
  397. }
  398. if (anchored)
  399. return strncmp(value, pattern, patternlen) == 0;
  400. else
  401. return strstr(value, pattern) != NULL;
  402. }
  403. int dmi_match(const char *pattern)
  404. {
  405. int i;
  406. if (!has_dmi_support)
  407. return 0;
  408. for (i = 0; i < ARRAY_SIZE(dmi_strings); i++) {
  409. if (dmi_strings[i].value == NULL)
  410. continue;
  411. if (dmi_compare(dmi_strings[i].value, pattern))
  412. return 1;
  413. }
  414. return 0;
  415. }
  416. #endif // defined(__i386__) || defined(__x86_64__)