io.c 6.2 KB

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