blob: f07fe80c0c82ea020816833160d1fd071513ba78 [file] [log] [blame]
Uwe Hermann515ab3d2009-05-15 17:02:34 +00001/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2009 Uwe Hermann <uwe@hermann-uwe.de>
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
21#include <stdlib.h>
22#include <string.h>
23#include <fcntl.h>
24#include <sys/types.h>
25#include <sys/stat.h>
26#include <errno.h>
27#include "flash.h"
28
Uwe Hermann40595982009-05-16 01:30:48 +000029#define PCI_IO_BASE_ADDRESS 0x10
30
Uwe Hermann515ab3d2009-05-15 17:02:34 +000031uint32_t io_base_addr;
32struct pci_access *pacc;
33struct pci_filter filter;
34char *pcidev_bdf = NULL;
35
36uint32_t pcidev_validate(struct pci_dev *dev, struct pcidev_status *devs)
37{
38 int i;
39 uint32_t addr;
40
41 for (i = 0; devs[i].device_name != NULL; i++) {
42 if (dev->device_id != devs[i].device_id)
43 continue;
44
Uwe Hermann40595982009-05-16 01:30:48 +000045 /* Don't use dev->base_addr[0], won't work on older libpci. */
46 addr = pci_read_long(dev, PCI_IO_BASE_ADDRESS) & ~0x03;
Uwe Hermann515ab3d2009-05-15 17:02:34 +000047
48 printf("Found \"%s %s\" (%04x:%04x, BDF %02x:%02x.%x)\n",
49 devs[i].vendor_name, devs[i].device_name, dev->vendor_id,
50 dev->device_id, dev->bus, dev->dev, dev->func);
51
52 if (devs[i].status == PCI_NT) {
53 printf("===\nThis PCI device is UNTESTED. Please email "
54 "a report including the 'flashrom -p xxxxxx'\n"
55 "output to flashrom@coreboot.org if it works "
56 "for you. Thank you for your help!\n===\n");
57 }
58
59 return addr;
60 }
61
62 return 0;
63}
64
65uint32_t pcidev_init(uint16_t vendor_id, struct pcidev_status *devs)
66{
67 struct pci_dev *dev;
68 char *msg = NULL;
69 int found = 0;
70 uint32_t addr = 0;
71
72 pacc = pci_alloc(); /* Get the pci_access structure */
73 pci_init(pacc); /* Initialize the PCI library */
74 pci_scan_bus(pacc); /* We want to get the list of devices */
75 pci_filter_init(pacc, &filter);
76
77 /* Filter by vendor and also bb:dd.f (if supplied by the user). */
78 filter.vendor = vendor_id;
79 if (pcidev_bdf != NULL) {
80 if ((msg = pci_filter_parse_slot(&filter, pcidev_bdf))) {
81 fprintf(stderr, "Error: %s\n", msg);
82 exit(1);
83 }
84 }
85
86 for (dev = pacc->devices; dev; dev = dev->next) {
87 if (pci_filter_match(&filter, dev)) {
88 if ((addr = pcidev_validate(dev, devs)) != 0)
89 found++;
90 }
91 }
92
93 /* Only continue if exactly one supported PCI dev has been found. */
94 if (found == 0) {
95 fprintf(stderr, "Error: No supported PCI device found.\n");
96 exit(1);
97 } else if (found > 1) {
98 fprintf(stderr, "Error: Multiple supported PCI devices found. "
99 "Please use 'flashrom -p xxxxxx=bb:dd.f' \n"
100 "to explicitly select the card with the given BDF "
101 "(PCI bus, device, function).\n");
102 exit(1);
103 }
104
105 return addr;
106}
107
108void print_supported_pcidevs(struct pcidev_status *devs)
109{
110 int i;
111
112 for (i = 0; devs[i].vendor_name != NULL; i++) {
113 printf("%s %s [%02x:%02x]%s\n", devs[i].vendor_name,
114 devs[i].device_name, devs[i].vendor_id,
115 devs[i].device_id,
116 (devs[i].status == PCI_NT) ? " (untested)" : "");
117 }
118}