internal.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. /*
  2. * This file is part of the flashrom project.
  3. *
  4. * Copyright (C) 2009 Carl-Daniel Hailfinger
  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 <stdio.h>
  21. #include <string.h>
  22. #include <stdlib.h>
  23. #include <stdarg.h>
  24. #include "flash.h"
  25. #include "programmer.h"
  26. #if NEED_PCI == 1
  27. struct pci_dev *pci_dev_find_filter(struct pci_filter filter)
  28. {
  29. struct pci_dev *temp;
  30. for (temp = pacc->devices; temp; temp = temp->next)
  31. if (pci_filter_match(&filter, temp))
  32. return temp;
  33. return NULL;
  34. }
  35. struct pci_dev *pci_dev_find_vendorclass(uint16_t vendor, uint16_t devclass)
  36. {
  37. struct pci_dev *temp;
  38. struct pci_filter filter;
  39. uint16_t tmp2;
  40. pci_filter_init(NULL, &filter);
  41. filter.vendor = vendor;
  42. for (temp = pacc->devices; temp; temp = temp->next)
  43. if (pci_filter_match(&filter, temp)) {
  44. /* Read PCI class */
  45. tmp2 = pci_read_word(temp, 0x0a);
  46. if (tmp2 == devclass)
  47. return temp;
  48. }
  49. return NULL;
  50. }
  51. struct pci_dev *pci_dev_find(uint16_t vendor, uint16_t device)
  52. {
  53. struct pci_dev *temp;
  54. struct pci_filter filter;
  55. pci_filter_init(NULL, &filter);
  56. filter.vendor = vendor;
  57. filter.device = device;
  58. for (temp = pacc->devices; temp; temp = temp->next)
  59. if (pci_filter_match(&filter, temp))
  60. return temp;
  61. return NULL;
  62. }
  63. struct pci_dev *pci_card_find(uint16_t vendor, uint16_t device,
  64. uint16_t card_vendor, uint16_t card_device)
  65. {
  66. struct pci_dev *temp;
  67. struct pci_filter filter;
  68. pci_filter_init(NULL, &filter);
  69. filter.vendor = vendor;
  70. filter.device = device;
  71. for (temp = pacc->devices; temp; temp = temp->next)
  72. if (pci_filter_match(&filter, temp)) {
  73. if ((card_vendor ==
  74. pci_read_word(temp, PCI_SUBSYSTEM_VENDOR_ID))
  75. && (card_device ==
  76. pci_read_word(temp, PCI_SUBSYSTEM_ID)))
  77. return temp;
  78. }
  79. return NULL;
  80. }
  81. #endif
  82. #if CONFIG_INTERNAL == 1
  83. int force_boardenable = 0;
  84. int force_boardmismatch = 0;
  85. #if defined(__i386__) || defined(__x86_64__)
  86. void probe_superio(void)
  87. {
  88. probe_superio_ite();
  89. #if 0
  90. /* Winbond Super I/O code is not yet available. */
  91. if (superio.vendor == SUPERIO_VENDOR_NONE)
  92. superio = probe_superio_winbond();
  93. #endif
  94. }
  95. int superio_count = 0;
  96. #define SUPERIO_MAX_COUNT 3
  97. struct superio superios[SUPERIO_MAX_COUNT];
  98. int register_superio(struct superio s)
  99. {
  100. if (superio_count == SUPERIO_MAX_COUNT)
  101. return 1;
  102. superios[superio_count++] = s;
  103. return 0;
  104. }
  105. #endif
  106. int is_laptop = 0;
  107. int laptop_ok = 1; /* FIXME: proper whitelisting hasn't been added yet */
  108. static void internal_chip_writeb(const struct flashctx *flash, uint8_t val,
  109. chipaddr addr);
  110. static void internal_chip_writew(const struct flashctx *flash, uint16_t val,
  111. chipaddr addr);
  112. static void internal_chip_writel(const struct flashctx *flash, uint32_t val,
  113. chipaddr addr);
  114. static uint8_t internal_chip_readb(const struct flashctx *flash,
  115. const chipaddr addr);
  116. static uint16_t internal_chip_readw(const struct flashctx *flash,
  117. const chipaddr addr);
  118. static uint32_t internal_chip_readl(const struct flashctx *flash,
  119. const chipaddr addr);
  120. static void internal_chip_readn(const struct flashctx *flash, uint8_t *buf,
  121. const chipaddr addr, size_t len);
  122. #if __FLASHROM_LITTLE_ENDIAN__
  123. static const struct par_programmer par_programmer_internal = {
  124. .chip_readb = internal_chip_readb,
  125. .chip_readw = internal_chip_readw,
  126. .chip_readl = internal_chip_readl,
  127. .chip_readn = internal_chip_readn,
  128. .chip_writeb = internal_chip_writeb,
  129. .chip_writew = internal_chip_writew,
  130. .chip_writel = internal_chip_writel,
  131. .chip_writen = fallback_chip_writen,
  132. };
  133. #endif
  134. enum chipbustype internal_buses_supported = BUS_NONE;
  135. static int internal_shutdown(void *data)
  136. {
  137. release_io_perms();
  138. return 0;
  139. }
  140. enum chipbustype target_bus;
  141. #if NEED_PCI == 1
  142. #define BUFSIZE 256
  143. static char buffer[BUFSIZE];
  144. static void
  145. pci_error(char *msg, ...)
  146. {
  147. va_list args;
  148. va_start(args, msg);
  149. vsnprintf(buffer, BUFSIZE, msg, args);
  150. va_end(args);
  151. msg_perr("pcilib: %s\n", buffer);
  152. /* libpci requires us to exit. TODO cleanup? */
  153. exit(1);
  154. }
  155. static void
  156. pci_warning(char *msg, ...)
  157. {
  158. va_list args;
  159. va_start(args, msg);
  160. vsnprintf(buffer, BUFSIZE, msg, args);
  161. va_end(args);
  162. msg_pinfo("pcilib: %s\n", buffer);
  163. }
  164. static void
  165. pci_debug(char *msg, ...)
  166. {
  167. va_list args;
  168. va_start(args, msg);
  169. vsnprintf(buffer, BUFSIZE, msg, args);
  170. va_end(args);
  171. msg_pdbg("pcilib: %s\n", buffer);
  172. }
  173. #endif
  174. int internal_init(void)
  175. {
  176. #if __FLASHROM_LITTLE_ENDIAN__
  177. int ret = 0;
  178. #endif
  179. int force_laptop = 0;
  180. int not_a_laptop = 0;
  181. char *arg;
  182. #if defined(__i386__) || defined(__x86_64__) || defined(__arm__)
  183. int probe_target_bus_later = 0;
  184. #endif
  185. arg = extract_programmer_param("boardenable");
  186. if (arg && !strcmp(arg,"force")) {
  187. force_boardenable = 1;
  188. } else if (arg && !strlen(arg)) {
  189. msg_perr("Missing argument for boardenable.\n");
  190. free(arg);
  191. return 1;
  192. } else if (arg) {
  193. msg_perr("Unknown argument for boardenable: %s\n", arg);
  194. free(arg);
  195. return 1;
  196. }
  197. free(arg);
  198. arg = extract_programmer_param("boardmismatch");
  199. if (arg && !strcmp(arg,"force")) {
  200. force_boardmismatch = 1;
  201. } else if (arg && !strlen(arg)) {
  202. msg_perr("Missing argument for boardmismatch.\n");
  203. free(arg);
  204. return 1;
  205. } else if (arg) {
  206. msg_perr("Unknown argument for boardmismatch: %s\n", arg);
  207. free(arg);
  208. return 1;
  209. }
  210. free(arg);
  211. arg = extract_programmer_param("laptop");
  212. if (arg && !strcmp(arg, "force_I_want_a_brick"))
  213. force_laptop = 1;
  214. else if (arg && !strcmp(arg, "this_is_not_a_laptop"))
  215. not_a_laptop = 1;
  216. else if (arg && !strlen(arg)) {
  217. msg_perr("Missing argument for laptop.\n");
  218. free(arg);
  219. return 1;
  220. } else if (arg) {
  221. msg_perr("Unknown argument for laptop: %s\n", arg);
  222. free(arg);
  223. return 1;
  224. }
  225. free(arg);
  226. arg = extract_programmer_param("mainboard");
  227. if (arg && strlen(arg)) {
  228. lb_vendor_dev_from_string(arg);
  229. } else if (arg && !strlen(arg)) {
  230. msg_perr("Missing argument for mainboard.\n");
  231. free(arg);
  232. return 1;
  233. }
  234. free(arg);
  235. arg = extract_programmer_param("bus");
  236. if (arg) {
  237. if (!strcasecmp(arg,"parallel")) {
  238. target_bus = BUS_PARALLEL;
  239. } else if (!strcasecmp(arg,"lpc")) {
  240. target_bus = BUS_LPC;
  241. } else if (!strcasecmp(arg,"fwh")) {
  242. target_bus = BUS_FWH;
  243. } else if (!strcasecmp(arg,"spi")) {
  244. target_bus = BUS_SPI;
  245. } else if (!strcasecmp(arg,"i2c")) {
  246. target_bus = BUS_PROG;
  247. } else {
  248. msg_perr("Unsupported bus: %s\n", arg);
  249. free(arg);
  250. return 1;
  251. }
  252. free(arg);
  253. } else {
  254. #if defined(__i386__) || defined(__x86_64__) || defined(__arm__)
  255. /* The pacc must be initialized before access pci devices. */
  256. probe_target_bus_later = 1;
  257. #endif
  258. }
  259. get_io_perms();
  260. if (register_shutdown(internal_shutdown, NULL))
  261. return 1;
  262. #if defined(__i386__) || defined(__x86_64__)
  263. /* Default to Parallel/LPC/FWH flash devices. If a known host controller
  264. * is found, the host controller init routine sets the
  265. * internal_buses_supported bitfield.
  266. */
  267. internal_buses_supported = BUS_NONSPI;
  268. /* Initialize PCI access for flash enables */
  269. pacc = pci_alloc(); /* Get the pci_access structure */
  270. pacc->error = pci_error;
  271. pacc->warning = pci_warning;
  272. pacc->debug = pci_debug;
  273. /* Set all options you want -- here we stick with the defaults */
  274. pci_init(pacc); /* Initialize the PCI library */
  275. pci_scan_bus(pacc); /* We want to get the list of devices */
  276. #else
  277. internal_buses_supported = BUS_NONE;
  278. #endif
  279. #if defined(__arm__)
  280. /*
  281. * FIXME: CrOS EC probing should not require this "#if defined(__arm__)"
  282. * and should not depend on the target bus. This is only to satisfy
  283. * users and scripts who currently depend on the old "-p internal:bus="
  284. * syntax or some default behavior.
  285. *
  286. * Once everything is finally updated, we should only rely on
  287. * alias == ALIAS_EC in order to call cros_ec_probe_*.
  288. *
  289. * Also, ensure probing does not get confused when removing the
  290. * "#if defined(__arm__)" (see crbug.com/249568).
  291. */
  292. if (!alias && probe_target_bus_later)
  293. target_bus = BUS_SPI;
  294. if (target_bus != BUS_SPI) {
  295. /*
  296. * Give preference to the cros_ec dev interface if it exists
  297. * and passes the "hello" test, otherwise fall back on raw I2C.
  298. */
  299. if (!cros_ec_probe_dev() || !cros_ec_probe_i2c(NULL))
  300. return 0;
  301. }
  302. #endif
  303. #if CONFIG_LINUX_MTD == 1
  304. if (!linux_mtd_init())
  305. return 0;
  306. #endif
  307. #if defined(__arm__) || defined(__mips__) && CONFIG_LINUX_SPI == 1
  308. /* On the ARM platform, we prefer /dev/spidev if it is supported.
  309. * That means, if user specifies
  310. *
  311. * 1. -p internal programmer
  312. * 2. without -p (the default programmer, which is internal too)
  313. *
  314. * This code would try to auto-detect the /dev/spidevX.Y.
  315. * If failed, try processor_flash_enable() then.
  316. *
  317. * The -p linux_spi still works because the programmer_init() would
  318. * call the linux_spi_init() in flashrom.c.
  319. */
  320. if (!programmer_init(PROGRAMMER_LINUX_SPI, NULL)) {
  321. return 0;
  322. } else /* if failed, fall through */
  323. #endif
  324. if (processor_flash_enable()) {
  325. msg_perr("Processor detection/init failed.\n"
  326. "Aborting.\n");
  327. return 1;
  328. }
  329. #if defined(__i386__) || defined(__x86_64__)
  330. /* We look at the cbtable first to see if we need a
  331. * mainboard specific flash enable sequence.
  332. */
  333. coreboot_init();
  334. dmi_init();
  335. if (probe_target_bus_later) {
  336. /* read the target bus value from register. */
  337. if (get_target_bus_from_chipset(&target_bus)) {
  338. msg_perr("Cannot get target bus from programmer.\n");
  339. return 1;
  340. }
  341. msg_pdbg("get_target_bus_from_chipset() returns 0x%x.\n",
  342. target_bus);
  343. }
  344. /* In case Super I/O probing would cause pretty explosions. */
  345. board_handle_before_superio();
  346. /* Probe for the Super I/O chip and fill global struct superio. */
  347. probe_superio();
  348. #elif defined(__arm__)
  349. /* We look at the cbtable first to see if we need a
  350. * mainboard specific flash enable sequence.
  351. */
  352. coreboot_init();
  353. #else
  354. /* FIXME: Enable cbtable searching on all non-x86 platforms supported
  355. * by coreboot.
  356. * FIXME: Find a replacement for DMI on non-x86.
  357. * FIXME: Enable Super I/O probing once port I/O is possible.
  358. */
  359. #endif
  360. /* Check laptop whitelist. */
  361. board_handle_before_laptop();
  362. /* Warn if a non-whitelisted laptop is detected. */
  363. if (is_laptop && !laptop_ok) {
  364. msg_perr("========================================================================\n");
  365. if (is_laptop == 1) {
  366. msg_perr("WARNING! You seem to be running flashrom on an unsupported laptop.\n");
  367. } else {
  368. msg_perr("WARNING! You may be running flashrom on an unsupported laptop. We could\n"
  369. "not detect this for sure because your vendor has not setup the SMBIOS\n"
  370. "tables correctly. You can enforce execution by adding\n"
  371. "'-p internal:laptop=this_is_not_a_laptop' to the command line, but\n"
  372. "please read the following warning if you are not sure.\n\n");
  373. }
  374. msg_perr("Laptops, notebooks and netbooks are difficult to support and we\n"
  375. "recommend to use the vendor flashing utility. The embedded controller\n"
  376. "(EC) in these machines often interacts badly with flashing.\n"
  377. "See http://www.flashrom.org/Laptops for details.\n\n"
  378. "If flash is shared with the EC, erase is guaranteed to brick your laptop\n"
  379. "and write may brick your laptop.\n"
  380. "Read and probe may irritate your EC and cause fan failure, backlight\n"
  381. "failure and sudden poweroff.\n"
  382. "You have been warned.\n"
  383. "========================================================================\n");
  384. if (force_laptop || (not_a_laptop && (is_laptop == 2))) {
  385. msg_perr("Proceeding anyway because user forced us to.\n");
  386. } else {
  387. msg_perr("Aborting.\n");
  388. exit(1);
  389. }
  390. }
  391. #if __FLASHROM_LITTLE_ENDIAN__
  392. #if defined(__i386__) || defined(__x86_64__) || defined (__mips) || defined (__arm__)
  393. /* try to enable it. Failure IS an option, since not all motherboards
  394. * really need this to be done, etc., etc.
  395. */
  396. ret = chipset_flash_enable();
  397. if (ret == -2) {
  398. msg_pdbg("WARNING: No chipset found. Flash detection "
  399. "will most likely fail.\n");
  400. } else if (ret == ERROR_FATAL)
  401. return ret;
  402. register_par_programmer(&par_programmer_internal, internal_buses_supported);
  403. #if defined(__i386__) || defined(__x86_64__)
  404. /* probe for programmers that bridge LPC <--> SPI */
  405. if (target_bus == BUS_LPC || target_bus == BUS_FWH ||
  406. (alias && alias->type == ALIAS_EC)) {
  407. /* Try to probe via kernel device first */
  408. if (!cros_ec_probe_dev()) {
  409. buses_supported &= ~(BUS_LPC|BUS_SPI);
  410. return 0;
  411. }
  412. if (cros_ec_probe_lpc(NULL) &&
  413. wpce775x_probe_spi_flash(NULL) &&
  414. mec1308_probe_spi_flash(NULL) &&
  415. ene_probe_spi_flash(NULL) &&
  416. init_superio_ite())
  417. return 1; /* EC not found */
  418. else
  419. return 0;
  420. }
  421. #endif
  422. board_flash_enable(lb_vendor, lb_part);
  423. if (!(buses_supported & target_bus) &&
  424. (!alias || (alias && alias->type == ALIAS_NONE))) {
  425. /* User specified a target bus which is not supported on the
  426. * platform or specified an alias which does not enable it.
  427. */
  428. msg_perr("Programmer does not support specified bus\n");
  429. return 1;
  430. }
  431. /* Even if chipset init returns an error code, we don't want to abort.
  432. * The error code might have been a warning only.
  433. * Besides that, we don't check the board enable return code either.
  434. */
  435. return 0;
  436. #else
  437. msg_perr("Your platform is not supported yet for the internal "
  438. "programmer due to missing\n"
  439. "flash_base and top/bottom alignment information.\n"
  440. "Aborting.\n");
  441. return 1;
  442. #endif
  443. #else
  444. /* FIXME: Remove this unconditional abort once all PCI drivers are
  445. * converted to use little-endian accesses for memory BARs.
  446. */
  447. msg_perr("Your platform is not supported yet for the internal "
  448. "programmer because it has\n"
  449. "not been converted from native endian to little endian "
  450. "access yet.\n"
  451. "Aborting.\n");
  452. return 1;
  453. #endif
  454. }
  455. #endif
  456. void internal_chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr)
  457. {
  458. mmio_writeb(val, (void *) addr);
  459. }
  460. void internal_chip_writew(const struct flashctx *flash, uint16_t val, chipaddr addr)
  461. {
  462. mmio_writew(val, (void *) addr);
  463. }
  464. void internal_chip_writel(const struct flashctx *flash, uint32_t val, chipaddr addr)
  465. {
  466. mmio_writel(val, (void *) addr);
  467. }
  468. uint8_t internal_chip_readb(const struct flashctx *flash, const chipaddr addr)
  469. {
  470. return mmio_readb((void *) addr);
  471. }
  472. uint16_t internal_chip_readw(const struct flashctx *flash, const chipaddr addr)
  473. {
  474. return mmio_readw((void *) addr);
  475. }
  476. uint32_t internal_chip_readl(const struct flashctx *flash, const chipaddr addr)
  477. {
  478. return mmio_readl((void *) addr);
  479. }
  480. void internal_chip_readn(const struct flashctx *flash, uint8_t *buf, const chipaddr addr, size_t len)
  481. {
  482. memcpy(buf, (void *)addr, len);
  483. return;
  484. }