10 static int iobfd = -1;
11 static int iowfd = -1;
12 static int iolfd = -1;
13 static int biosfd = -1;
14 static int msrfd = -1;
15 static ulong biosoffset = 0;
22 static int ctlfd = -1;
23 static char ctlbuf[Nctlchar];
32 devopen(char* device, int mode)
36 if((fd = open(device, mode)) < 0)
37 error("devopen(%s, %d): %r\n", device, mode);
47 iobfd = devopen("#P/iob", ORDWR);
49 if(pread(iobfd, &data, sizeof(data), port) != sizeof(data))
50 error("inportb(0x%4.4lx): %r\n", port);
55 outportb(long port, uchar data)
58 iobfd = devopen("#P/iob", ORDWR);
60 if(pwrite(iobfd, &data, sizeof(data), port) != sizeof(data))
61 error("outportb(0x%4.4lx, 0x%2.2uX): %r\n", port, data);
70 iowfd = devopen("#P/iow", ORDWR);
72 if(pread(iowfd, &data, sizeof(data), port) != sizeof(data))
73 error("inportw(0x%4.4lx): %r\n", port);
78 outportw(long port, ushort data)
81 iowfd = devopen("#P/iow", ORDWR);
83 if(pwrite(iowfd, &data, sizeof(data), port) != sizeof(data))
84 error("outportw(0x%4.4lx, 0x%2.2uX): %r\n", port, data);
93 iolfd = devopen("#P/iol", ORDWR);
95 if(pread(iolfd, &data, sizeof(data), port) != sizeof(data))
96 error("inportl(0x%4.4lx): %r\n", port);
101 outportl(long port, ulong data)
104 iolfd = devopen("#P/iol", ORDWR);
106 if(pwrite(iolfd, &data, sizeof(data), port) != sizeof(data))
107 error("outportl(0x%4.4lx, 0x%2.2luX): %r\n", port, data);
116 msrfd = devopen("#P/msr", ORDWR);
118 if(pread(msrfd, &data, sizeof(data), port) != sizeof(data))
119 error("rdmsr(0x%4.4lx): %r\n", port);
124 wrmsr(long port, uvlong data)
127 msrfd = devopen("#P/msr", ORDWR);
129 if(pwrite(msrfd, &data, sizeof(data), port) != sizeof(data))
130 error("wrmsr(0x%4.4lx, 0x%2.2lluX): %r\n", port, data);
143 ctlfd = devopen("#v/vgactl", ORDWR);
144 memset(attr, 0, sizeof(attr));
148 nattr = read(ctlfd, ctlbuf, Nctlchar-1);
150 error("vgactlr: read: %r\n");
155 for(nl = strchr(ctlbuf, '\n'); nl; nl = strchr(nl, '\n')){
158 if(p = strchr(vp, ' ')){
160 attr[nattr].attr = vp;
162 error("vgactlr: bad format: <%s>\n", vp);
166 error("vgactlr: bad format: <%s>\n", vp);
168 if(++nattr >= Nattr-2)
169 error("vgactlr: too many attributes: %d\n", nattr);
170 attr[nattr].attr = 0;
179 vgactlr(char* a, char* v)
183 trace("vgactlr: look for %s\n", a);
185 for(i = 0; attr[i].attr; i++){
186 if(strcmp(attr[i].attr, a) == 0){
187 strcpy(v, attr[i].val);
188 trace("vgactlr: value %s\n", v);
192 trace("vgactlr: %s not found\n", a);
198 vgactlw(char* attr, char* val)
204 ctlfd = devopen("#v/vgactl", ORDWR);
207 len = sprint(buf, "%s %s", attr, val);
208 trace("+vgactlw %s\n", buf);
209 if(write(ctlfd, buf, len) != len)
210 error("vgactlw: <%s>: %r\n", buf);
211 trace("-vgactlw %s\n", buf);
225 ctlfd = devopen("#v/vgactl", ORDWR);
226 len = snprint(buf, sizeof(buf), "pcidev %lux", (ulong)p->tbdf);
228 /* ignore error for old kernel */
229 write(ctlfd, buf, len);
235 setpalette(int p, int r, int g, int b)
244 doreadbios(char* buf, long len, long offset)
249 biosfd = open("#v/vgabios", OREAD);
253 snprint(file, sizeof file, "#p/%d/mem", getpid());
254 biosfd = devopen(file, OREAD);
255 biosoffset = 0x80000000;
259 seek(biosfd, biosoffset+offset, 0);
260 return read(biosfd, buf, len);
264 readbios(long len, long offset)
266 static char bios[0x10000];
267 static long biosoffset;
271 if(biosoffset <= offset && offset+len <= biosoffset+bioslen)
272 return bios+(offset - biosoffset);
274 if(len > sizeof(bios))
275 error("enormous bios len %ld at %lux\n", len, offset);
277 n = doreadbios(bios, sizeof(bios), offset);
279 error("short bios read %ld at %lux got %d\n", len,offset, n);
296 if(doreadbios((char*)buf, size, offset) != size)
297 error("short bios read in dumpbios\n");
299 if(buf[0] != 0x55 || buf[1] != 0xAA){
301 if(doreadbios((char*)buf, size, offset) != size)
302 error("short bios read in dumpbios\n");
303 if(buf[0] != 0x55 || buf[1] != 0xAA){
309 for(i = 0; i < size; i += 16){
310 Bprint(&stdout, "0x%luX", offset+i);
311 for(n = 0; n < 16; n++)
312 Bprint(&stdout, " %2.2uX", buf[i+n]);
313 Bprint(&stdout, " ");
314 for(n = 0; n < 16; n++){
316 if(c < 0x20 || c >= 0x7F)
318 Bprint(&stdout, "%c", c);
320 Bprint(&stdout, "\n");
330 if((v = malloc(nbytes)) == 0)
331 error("alloc: %lud bytes - %r\n", nbytes);
333 return memset(v, 0, nbytes);
337 printitem(char* ctlr, char* item)
343 Bprint(&stdout, "\n");
348 n = Bprint(&stdout, "%s ", ctlr);
349 Bprint(&stdout, "%-*s", 20-n, item);
358 if((curprintindex % 16) == 0 && curprintindex){
359 Bprint(&stdout, "\n");
363 if(curprintindex == 8)
364 Bprint(&stdout, " -");
365 Bprint(&stdout, "%*.2luX", width, data);
369 static char *flagname[32] = {
394 printflag(ulong flag)
400 for(i = 31; i >= 0; i--){
401 if((flag & (1<<i)) == 0)
404 Bprint(&stdout, "%c%s", first, flagname[i]);
406 Bprint(&stdout, "%c0x%x", first, 1<<i);