io.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <bio.h>
  4. #include "pci.h"
  5. #include "vga.h"
  6. int curprintindex;
  7. static int iobfd = -1;
  8. static int iowfd = -1;
  9. static int iolfd = -1;
  10. static int biosfd = -1;
  11. enum {
  12. Nctlchar = 256,
  13. Nattr = 16,
  14. };
  15. static int ctlfd = -1;
  16. static char ctlbuf[Nctlchar];
  17. static int ctlclean;
  18. static struct {
  19. char* attr;
  20. char* val;
  21. } attr[Nattr];
  22. static int
  23. devopen(char* device, int mode)
  24. {
  25. int fd;
  26. if((fd = open(device, mode)) < 0)
  27. error("devopen(%s, %d): %r\n", device, mode);
  28. return fd;
  29. }
  30. uchar
  31. inportb(long port)
  32. {
  33. uchar data;
  34. if(iobfd == -1)
  35. iobfd = devopen("#P/iob", ORDWR);
  36. if(pread(iobfd, &data, sizeof(data), port) != sizeof(data))
  37. error("inportb(0x%4.4lx): %r\n", port);
  38. return data;
  39. }
  40. void
  41. outportb(long port, uchar data)
  42. {
  43. if(iobfd == -1)
  44. iobfd = devopen("#P/iob", ORDWR);
  45. if(pwrite(iobfd, &data, sizeof(data), port) != sizeof(data))
  46. error("outportb(0x%4.4lx, 0x%2.2uX): %r\n", port, data);
  47. }
  48. ushort
  49. inportw(long port)
  50. {
  51. ushort data;
  52. if(iowfd == -1)
  53. iowfd = devopen("#P/iow", ORDWR);
  54. if(pread(iowfd, &data, sizeof(data), port) != sizeof(data))
  55. error("inportw(0x%4.4lx): %r\n", port);
  56. return data;
  57. }
  58. void
  59. outportw(long port, ushort data)
  60. {
  61. if(iowfd == -1)
  62. iowfd = devopen("#P/iow", ORDWR);
  63. if(pwrite(iowfd, &data, sizeof(data), port) != sizeof(data))
  64. error("outportw(0x%4.4lx, 0x%2.2uX): %r\n", port, data);
  65. }
  66. ulong
  67. inportl(long port)
  68. {
  69. ulong data;
  70. if(iolfd == -1)
  71. iolfd = devopen("#P/iol", ORDWR);
  72. if(pread(iolfd, &data, sizeof(data), port) != sizeof(data))
  73. error("inportl(0x%4.4lx): %r\n", port);
  74. return data;
  75. }
  76. void
  77. outportl(long port, ulong data)
  78. {
  79. if(iolfd == -1)
  80. iolfd = devopen("#P/iol", ORDWR);
  81. if(pwrite(iolfd, &data, sizeof(data), port) != sizeof(data))
  82. error("outportl(0x%4.4lx, 0x%2.2luX): %r\n", port, data);
  83. }
  84. static void
  85. vgactlinit(void)
  86. {
  87. int nattr;
  88. char *nl, *p, *vp;
  89. if(ctlclean)
  90. return;
  91. if(ctlfd == -1){
  92. ctlfd = devopen("#v/vgactl", ORDWR);
  93. memset(attr, 0, sizeof(attr));
  94. }
  95. seek(ctlfd, 0, 0);
  96. nattr = read(ctlfd, ctlbuf, Nctlchar-1);
  97. if(nattr < 0)
  98. error("vgactlr: read: %r\n");
  99. ctlbuf[nattr] = 0;
  100. nattr = 0;
  101. vp = ctlbuf;
  102. for(nl = strchr(ctlbuf, '\n'); nl; nl = strchr(nl, '\n')){
  103. *nl = '\0';
  104. if(p = strchr(vp, ' ')){
  105. *p++ = '\0';
  106. attr[nattr].attr = vp;
  107. if(*p == '\0')
  108. error("vgactlr: bad format: <%s>\n", vp);
  109. attr[nattr].val = p;
  110. }
  111. else
  112. error("vgactlr: bad format: <%s>\n", vp);
  113. if(++nattr >= Nattr-2)
  114. error("vgactlr: too many attributes: %d\n", nattr);
  115. attr[nattr].attr = 0;
  116. vp = ++nl;
  117. }
  118. ctlclean = 1;
  119. }
  120. char*
  121. vgactlr(char* a, char* v)
  122. {
  123. int i;
  124. trace("vgactlr: look for %s\n", a);
  125. vgactlinit();
  126. for(i = 0; attr[i].attr; i++){
  127. if(strcmp(attr[i].attr, a) == 0){
  128. strcpy(v, attr[i].val);
  129. trace("vgactlr: value %s\n", v);
  130. return v;
  131. }
  132. }
  133. trace("vgactlr: %s not found\n", a);
  134. return 0;
  135. }
  136. void
  137. vgactlw(char* attr, char* val)
  138. {
  139. int len;
  140. char buf[128];
  141. if(ctlfd == -1)
  142. ctlfd = devopen("#v/vgactl", ORDWR);
  143. seek(ctlfd, 0, 0);
  144. len = sprint(buf, "%s %s", attr, val);
  145. trace("+vgactlw %s\n", buf);
  146. if(write(ctlfd, buf, len) != len)
  147. error("vgactlw: <%s>: %r\n", buf);
  148. trace("-vgactlw %s\n", buf);
  149. ctlclean = 0;
  150. }
  151. void
  152. setpalette(int p, int r, int g, int b)
  153. {
  154. vgao(PaddrW, p);
  155. vgao(Pdata, r);
  156. vgao(Pdata, g);
  157. vgao(Pdata, b);
  158. }
  159. static long
  160. doreadbios(char* buf, long len, long offset)
  161. {
  162. char file[64];
  163. if(biosfd == -1)
  164. biosfd = open("#v/vgabios", OREAD);
  165. if(biosfd == -1) {
  166. snprint(file, sizeof file, "#p/%d/mem", getpid());
  167. biosfd = devopen(file, OREAD);
  168. }
  169. seek(biosfd, 0x80000000|offset, 0);
  170. return read(biosfd, buf, len);
  171. }
  172. char*
  173. readbios(long len, long offset)
  174. {
  175. static char bios[0x10000];
  176. static long biosoffset;
  177. static long bioslen;
  178. int n;
  179. if(biosoffset <= offset && offset+len <= biosoffset+bioslen)
  180. return bios+(offset - biosoffset);
  181. if(len > sizeof(bios))
  182. error("enormous bios len %ld at %lux\n", len, offset);
  183. n = doreadbios(bios, sizeof(bios), offset);
  184. if(n < len)
  185. error("short bios read %ld at %lux got %d\n", len,offset, n);
  186. biosoffset = offset;
  187. bioslen = n;
  188. return bios;
  189. }
  190. void
  191. dumpbios(long size)
  192. {
  193. uchar *buf;
  194. long offset;
  195. int i, n;
  196. char c;
  197. buf = alloc(size);
  198. offset = 0xC0000;
  199. if(doreadbios((char*)buf, size, offset) != size)
  200. error("short bios read in dumpbios");
  201. if(buf[0] != 0x55 || buf[1] != 0xAA){
  202. offset = 0xE0000;
  203. if(doreadbios((char*)buf, size, offset) != size)
  204. error("short bios read in dumpbios");
  205. if(buf[0] != 0x55 || buf[1] != 0xAA){
  206. free(buf);
  207. return;
  208. }
  209. }
  210. for(i = 0; i < size; i += 16){
  211. Bprint(&stdout, "0x%luX", offset+i);
  212. for(n = 0; n < 16; n++)
  213. Bprint(&stdout, " %2.2uX", buf[i+n]);
  214. Bprint(&stdout, " ");
  215. for(n = 0; n < 16; n++){
  216. c = buf[i+n];
  217. if(c < 0x20 || c >= 0x7F)
  218. c = '.';
  219. Bprint(&stdout, "%c", c);
  220. }
  221. Bprint(&stdout, "\n");
  222. }
  223. free(buf);
  224. }
  225. void*
  226. alloc(ulong nbytes)
  227. {
  228. void *v;
  229. if((v = malloc(nbytes)) == 0)
  230. error("alloc: %lud bytes - %r\n", nbytes);
  231. return memset(v, 0, nbytes);
  232. }
  233. void
  234. printitem(char* ctlr, char* item)
  235. {
  236. int n;
  237. if(curprintindex){
  238. curprintindex = 0;
  239. Bprint(&stdout, "\n");
  240. }
  241. n = 0;
  242. if(ctlr && *ctlr)
  243. n = Bprint(&stdout, "%s ", ctlr);
  244. Bprint(&stdout, "%-*s", 20-n, item);
  245. }
  246. void
  247. printreg(ulong data)
  248. {
  249. int width;
  250. width = 3;
  251. if((curprintindex % 16) == 0 && curprintindex){
  252. Bprint(&stdout, "\n");
  253. curprintindex = 0;
  254. width = 23;
  255. }
  256. if(curprintindex == 8)
  257. Bprint(&stdout, " -");
  258. Bprint(&stdout, "%*.2luX", width, data);
  259. curprintindex++;
  260. }
  261. static char *flagname[32] = {
  262. [0x00] "Fsnarf",
  263. [0x01] "Foptions",
  264. [0x02] "Finit",
  265. [0x03] "Fload",
  266. [0x04] "Fdump",
  267. [0x08] "Hpclk2x8",
  268. [0x09] "Upclk2x8",
  269. [0x0A] "Henhanced",
  270. [0x0B] "Uenhanced",
  271. [0x0C] "Hpvram",
  272. [0x0D] "Upvram",
  273. [0x0E] "Hextsid",
  274. [0x0F] "Uextsid",
  275. [0x10] "Hclk2",
  276. [0x11] "Uclk2",
  277. [0x12] "Hlinear",
  278. [0x13] "Ulinear",
  279. [0x14] "Hclkdiv",
  280. [0x15] "Uclkdiv",
  281. [0x16] "Hsid32",
  282. };
  283. void
  284. printflag(ulong flag)
  285. {
  286. int i;
  287. char first;
  288. first = ' ';
  289. for(i = 31; i >= 0; i--){
  290. if((flag & (1<<i)) == 0)
  291. continue;
  292. if(flagname[i])
  293. Bprint(&stdout, "%c%s", first, flagname[i]);
  294. else
  295. Bprint(&stdout, "%c0x%x", first, 1<<i);
  296. first = '|';
  297. }
  298. }