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