• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * (C) Copyright IBM Corporation 2007
3  * All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * on the rights to use, copy, modify, merge, publish, distribute, sub
9  * license, and/or sell copies of the Software, and to permit persons to whom
10  * the Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
19  * IBM AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  */
24 
25 /**
26  * \file linux_devmem.c
27  * Access PCI subsystem using Linux's the old /dev/mem interface.
28  *
29  * \note
30  * This is currently just a skeleton.  It only includes the /dev/mem based
31  * function for reading the device ROM.
32  *
33  * \author Ian Romanick <idr@us.ibm.com>
34  */
35 #ifdef HAVE_CONFIG_H
36 #include "config.h"
37 #endif
38 
39 #include <stdlib.h>
40 #include <string.h>
41 #include <stdio.h>
42 #include <unistd.h>
43 #include <sys/types.h>
44 #include <sys/stat.h>
45 #include <fcntl.h>
46 #include <sys/mman.h>
47 #include <dirent.h>
48 #include <errno.h>
49 
50 #include "pciaccess.h"
51 #include "pciaccess_private.h"
52 #include "linux_devmem.h"
53 
54 /**
55  * Read a device's expansion ROM using /dev/mem.
56  *
57  * \note
58  * This function could probably be used, as-is, on other platforms that have
59  * a /dev/mem interface.
60  *
61  * \bugs
62  * Before using the VGA special case code, this function should check that
63  * VGA access are routed to the device.  Right?
64  */
65 _pci_hidden int
pci_device_linux_devmem_read_rom(struct pci_device * dev,void * buffer)66 pci_device_linux_devmem_read_rom(struct pci_device *dev, void *buffer)
67 {
68     struct pci_device_private *priv = (struct pci_device_private *) dev;
69     int fd;
70     int err = 0;
71     uint32_t rom_base_tmp;
72     pciaddr_t rom_base;
73     pciaddr_t rom_size;
74     int PCI_ROM;
75 
76 
77     /* Handle some special cases of legacy devices.
78      */
79     if (priv->base.rom_size == 0) {
80 	/* VGA ROMs are supposed to be at 0xC0000.
81 	 */
82 	if ((priv->base.device_class & 0x00ffff00) == 0x000030000) {
83 	    rom_base = 0x000C0000;
84 	    rom_size = 0x00010000;
85 	    PCI_ROM = 0;
86 	}
87 	else {
88 	    /* "Function not implemented."
89 	     */
90 	    return ENOSYS;
91 	}
92     }
93     else {
94 	rom_base = priv->rom_base;
95 	rom_size = priv->base.rom_size;
96 	PCI_ROM = 1;
97     }
98 
99 
100 
101     /* Enable the device's ROM.
102      */
103     if (PCI_ROM) {
104 	err = pci_device_cfg_read_u32(& priv->base, & rom_base_tmp, 48);
105 	if (err) {
106 	    return err;
107 	}
108 
109 	if ((rom_base_tmp & 0x000000001) == 0) {
110 	    err = pci_device_cfg_write_u32(& priv->base,
111 					   rom_base_tmp | 1, 48);
112 	    if (err) {
113 		return err;
114 	    }
115 	}
116     }
117 
118 
119     /* Read the portion of /dev/mem that corresponds to the device's ROM.
120      */
121     fd = open("/dev/mem", O_RDONLY, 0);
122     if (fd < 0) {
123 	err = errno;
124     }
125     else {
126 	size_t bytes;
127 
128 	for (bytes = 0; bytes < rom_size; /* empty */) {
129 	    const ssize_t got = pread(fd, buffer, rom_size - bytes,
130 				      rom_base + bytes);
131 	    if (got == -1) {
132 		err = errno;
133 		break;
134 	    }
135 
136 	    bytes += got;
137 	}
138 
139 	close(fd);
140     }
141 
142 
143     /* Disable the device's ROM.
144      */
145     if (PCI_ROM && ((rom_base_tmp & 0x000000001) == 0)) {
146 	const int tmp_err = pci_device_cfg_write_u32(& priv->base,
147 						     rom_base_tmp, 48);
148 
149 	/* Prefer to return the first error that occurred.
150 	 */
151 	if (err == 0) {
152 	    err = tmp_err;
153 	}
154     }
155 
156     return err;
157 }
158