xref: /libpciaccess/src/linux_devmem.c (revision d898072e)
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 <[email protected]>
34  */
35 
36 #define _GNU_SOURCE
37 
38 #include <stdlib.h>
39 #include <string.h>
40 #include <stdio.h>
41 #include <unistd.h>
42 #include <sys/types.h>
43 #include <sys/stat.h>
44 #include <fcntl.h>
45 #include <sys/mman.h>
46 #include <dirent.h>
47 #include <errno.h>
48 
49 #include "pciaccess.h"
50 #include "pciaccess_private.h"
51 #include "linux_devmem.h"
52 
53 /**
54  * Read a device's expansion ROM using /dev/mem.
55  *
56  * \note
57  * This function could probably be used, as-is, on other platforms that have
58  * a /dev/mem interface.
59  *
60  * \bugs
61  * Before using the VGA special case code, this function should check that
62  * VGA access are routed to the device.  Right?
63  */
64 _pci_hidden int
65 pci_device_linux_devmem_read_rom(struct pci_device *dev, void *buffer)
66 {
67     struct pci_device_private *priv = (struct pci_device_private *) dev;
68     int fd;
69     int err = 0;
70     uint32_t rom_base_tmp;
71     pciaddr_t rom_base;
72     pciaddr_t rom_size;
73     int PCI_ROM;
74 
75 
76     /* Handle some special cases of legacy devices.
77      */
78     if (priv->base.rom_size == 0) {
79 	/* VGA ROMs are supposed to be at 0xC0000.
80 	 */
81 	if ((priv->base.device_class & 0x00ffff00) == 0x000030000) {
82 	    rom_base = 0x000C0000;
83 	    rom_size = 0x00010000;
84 	    PCI_ROM = 0;
85 	}
86 	else {
87 	    /* "Function not implemented."
88 	     */
89 	    return ENOSYS;
90 	}
91     }
92     else {
93 	rom_base = priv->rom_base;
94 	rom_size = priv->base.rom_size;
95 	PCI_ROM = 1;
96     }
97 
98 
99 
100     /* Enable the device's ROM.
101      */
102     if (PCI_ROM) {
103 	err = pci_device_cfg_read_u32(& priv->base, & rom_base_tmp, 48);
104 	if (err) {
105 	    return err;
106 	}
107 
108 	if ((rom_base_tmp & 0x000000001) == 0) {
109 	    err = pci_device_cfg_write_u32(& priv->base,
110 					   rom_base_tmp | 1, 48);
111 	    if (err) {
112 		return err;
113 	    }
114 	}
115     }
116 
117 
118     /* Read the portion of /dev/mem that corresponds to the device's ROM.
119      */
120     fd = open("/dev/mem", O_RDONLY, 0);
121     if (fd < 0) {
122 	err = errno;
123     }
124     else {
125 	size_t bytes;
126 
127 	for (bytes = 0; bytes < priv->base.rom_size; /* empty */) {
128 	    const ssize_t got = pread(fd, buffer, rom_size - bytes,
129 				      rom_base + bytes);
130 	    if (got == -1) {
131 		err = errno;
132 		break;
133 	    }
134 
135 	    bytes += got;
136 	}
137 
138 	close(fd);
139     }
140 
141 
142     /* Disable the device's ROM.
143      */
144     if (PCI_ROM && ((rom_base_tmp & 0x000000001) == 0)) {
145 	const int tmp_err = pci_device_cfg_write_u32(& priv->base,
146 						     rom_base_tmp, 48);
147 
148 	/* Prefer to return the first error that occured.
149 	 */
150 	if (err == 0) {
151 	    err = tmp_err;
152 	}
153     }
154 
155     return err;
156 }
157