xref: /aosp_15_r20/external/pciutils/lib/proc.c (revision c2e0c6b56a71da9abe8df5c8348fb3eb5c2c9251)
1 /*
2  *	The PCI Library -- Configuration Access via /proc/bus/pci
3  *
4  *	Copyright (c) 1997--2023 Martin Mares <[email protected]>
5  *
6  *	Can be freely distributed and used under the terms of the GNU GPL v2+.
7  *
8  *	SPDX-License-Identifier: GPL-2.0-or-later
9  */
10 
11 #define _GNU_SOURCE
12 
13 #include <stdio.h>
14 #include <string.h>
15 #include <unistd.h>
16 #include <ctype.h>
17 #include <errno.h>
18 #include <fcntl.h>
19 #include <sys/types.h>
20 
21 #include "internal.h"
22 
23 static void
proc_config(struct pci_access * a)24 proc_config(struct pci_access *a)
25 {
26   pci_define_param(a, "proc.path", PCI_PATH_PROC_BUS_PCI, "Path to the procfs bus tree");
27 }
28 
29 static int
proc_detect(struct pci_access * a)30 proc_detect(struct pci_access *a)
31 {
32   char *name = pci_get_param(a, "proc.path");
33 
34   if (access(name, R_OK))
35     {
36       a->warning("Cannot open %s", name);
37       return 0;
38     }
39   a->debug("...using %s", name);
40   return 1;
41 }
42 
43 static void
proc_init(struct pci_access * a)44 proc_init(struct pci_access *a)
45 {
46   a->fd = -1;
47 }
48 
49 static void
proc_cleanup(struct pci_access * a)50 proc_cleanup(struct pci_access *a)
51 {
52   if (a->fd >= 0)
53     {
54       close(a->fd);
55       a->fd = -1;
56     }
57 }
58 
59 static void
proc_scan(struct pci_access * a)60 proc_scan(struct pci_access *a)
61 {
62   FILE *f;
63   char buf[512];
64 
65   if (snprintf(buf, sizeof(buf), "%s/devices", pci_get_param(a, "proc.path")) == sizeof(buf))
66     a->error("File name too long");
67   f = fopen(buf, "r");
68   if (!f)
69     a->error("Cannot open %s", buf);
70   while (fgets(buf, sizeof(buf)-1, f))
71     {
72       struct pci_dev *d = pci_alloc_dev(a);
73       unsigned int dfn, vend, cnt, known;
74       char *driver;
75       int offset;
76 
77 #define F " " PCIADDR_T_FMT
78       cnt = sscanf(buf, "%x %x %x" F F F F F F F F F F F F F F "%n",
79 	     &dfn,
80 	     &vend,
81 	     &d->irq,
82 	     &d->base_addr[0],
83 	     &d->base_addr[1],
84 	     &d->base_addr[2],
85 	     &d->base_addr[3],
86 	     &d->base_addr[4],
87 	     &d->base_addr[5],
88 	     &d->rom_base_addr,
89 	     &d->size[0],
90 	     &d->size[1],
91 	     &d->size[2],
92 	     &d->size[3],
93 	     &d->size[4],
94 	     &d->size[5],
95 	     &d->rom_size,
96 	     &offset);
97 #undef F
98       if (cnt != 9 && cnt != 10 && cnt != 17)
99 	a->error("proc: parse error (read only %d items)", cnt);
100       d->bus = dfn >> 8U;
101       d->dev = PCI_SLOT(dfn & 0xff);
102       d->func = PCI_FUNC(dfn & 0xff);
103       d->vendor_id = vend >> 16U;
104       d->device_id = vend & 0xffff;
105       known = 0;
106       if (!a->buscentric)
107 	{
108 	  known |= PCI_FILL_IDENT | PCI_FILL_IRQ | PCI_FILL_BASES;
109 	  if (cnt >= 10)
110 	    known |= PCI_FILL_ROM_BASE;
111 	  if (cnt >= 17)
112 	    known |= PCI_FILL_SIZES;
113 	}
114       if (cnt >= 17)
115         {
116           while (buf[offset] && isspace(buf[offset]))
117             ++offset;
118           driver = &buf[offset];
119           while (buf[offset] && !isspace(buf[offset]))
120             ++offset;
121           buf[offset] = '\0';
122           if (driver[0])
123             {
124               pci_set_property(d, PCI_FILL_DRIVER, driver);
125               known |= PCI_FILL_DRIVER;
126             }
127         }
128       d->known_fields = known;
129       pci_link_dev(a, d);
130     }
131   fclose(f);
132 }
133 
134 static int
proc_setup(struct pci_dev * d,int rw)135 proc_setup(struct pci_dev *d, int rw)
136 {
137   struct pci_access *a = d->access;
138 
139   if (a->cached_dev != d || a->fd_rw < rw)
140     {
141       char buf[1024];
142       int e;
143       if (a->fd >= 0)
144 	close(a->fd);
145       e = snprintf(buf, sizeof(buf), "%s/%02x/%02x.%d",
146 		   pci_get_param(a, "proc.path"),
147 		   d->bus, d->dev, d->func);
148       if (e < 0 || e >= (int) sizeof(buf))
149 	a->error("File name too long");
150       a->fd_rw = a->writeable || rw;
151       a->fd = open(buf, a->fd_rw ? O_RDWR : O_RDONLY);
152       if (a->fd < 0)
153 	{
154 	  e = snprintf(buf, sizeof(buf), "%s/%04x:%02x/%02x.%d",
155 		       pci_get_param(a, "proc.path"),
156 		       d->domain, d->bus, d->dev, d->func);
157 	  if (e < 0 || e >= (int) sizeof(buf))
158 	    a->error("File name too long");
159 	  a->fd = open(buf, a->fd_rw ? O_RDWR : O_RDONLY);
160 	}
161       if (a->fd < 0)
162 	a->warning("Cannot open %s", buf);
163       a->cached_dev = d;
164     }
165   return a->fd;
166 }
167 
168 static int
proc_read(struct pci_dev * d,int pos,byte * buf,int len)169 proc_read(struct pci_dev *d, int pos, byte *buf, int len)
170 {
171   int fd = proc_setup(d, 0);
172   int res;
173 
174   if (fd < 0)
175     return 0;
176   res = pread(fd, buf, len, pos);
177   if (res < 0)
178     {
179       d->access->warning("proc_read: read failed: %s", strerror(errno));
180       return 0;
181     }
182   else if (res != len)
183     return 0;
184   return 1;
185 }
186 
187 static int
proc_write(struct pci_dev * d,int pos,byte * buf,int len)188 proc_write(struct pci_dev *d, int pos, byte *buf, int len)
189 {
190   int fd = proc_setup(d, 1);
191   int res;
192 
193   if (fd < 0)
194     return 0;
195   res = pwrite(fd, buf, len, pos);
196   if (res < 0)
197     {
198       d->access->warning("proc_write: write failed: %s", strerror(errno));
199       return 0;
200     }
201   else if (res != len)
202     {
203       d->access->warning("proc_write: tried to write %d bytes at %d, but only %d succeeded", len, pos, res);
204       return 0;
205     }
206   return 1;
207 }
208 
209 static void
proc_cleanup_dev(struct pci_dev * d)210 proc_cleanup_dev(struct pci_dev *d)
211 {
212   if (d->access->cached_dev == d)
213     d->access->cached_dev = NULL;
214 }
215 
216 struct pci_methods pm_linux_proc = {
217   .name = "linux-proc",
218   .help = "The proc file system on Linux",
219   .config = proc_config,
220   .detect = proc_detect,
221   .init = proc_init,
222   .cleanup = proc_cleanup,
223   .scan = proc_scan,
224   .fill_info = pci_generic_fill_info,
225   .read = proc_read,
226   .write = proc_write,
227   .cleanup_dev = proc_cleanup_dev,
228 };
229