$NetBSD: patch-ab,v 1.5 2022/05/13 15:46:09 wiz Exp $

--- vbetool.c.orig	2006-07-26 01:27:21.000000000 +0000
+++ vbetool.c
@@ -16,11 +16,31 @@ version 2
 #include <unistd.h>
 #include <sys/ioctl.h>
 #include <sys/types.h>
+#ifdef __linux__
 #include <sys/io.h>
 #include <sys/kd.h>
+#endif
 #include <sys/stat.h>
 #include <errno.h>
 
+#ifdef __NetBSD__
+#include <machine/sysarch.h>
+#endif
+
+static uint8_t
+asm_inb(unsigned port)
+{
+	uint8_t data;
+	__asm volatile("inb %w1,%0" : "=a" (data) : "d" (port));
+	return data;
+}
+
+static __inline void
+asm_outb(uint8_t data, unsigned port)
+{
+	__asm volatile("outb %0,%w1" : : "a" (data), "d" (port));
+}
+
 #include "include/lrmi.h"
 #include "vbetool.h"
 
@@ -42,8 +62,16 @@ int vbetool_init (void) {
 		exit(1);
 	}
 	
+#ifdef __NetBSD__
+# ifdef __i386__
+	i386_iopl(3);
+# else
+	x86_64_iopl(3);
+# endif
+#else
 	ioperm(0, 1024, 1);
 	iopl(3);
+#endif
 	
 	pacc = pci_alloc();
 	pacc->numeric_ids = 1;
@@ -256,7 +284,9 @@ void restore_state_from(char *data)
 
 	LRMI_free_real(data);
 
+#ifdef __linux__
 	ioctl(0, KDSETMODE, KD_TEXT);
+#endif
 
 }
 
@@ -476,23 +506,25 @@ int check_console()
 		return 11;
 	}
 
+#ifdef __linux__
 	ioctl(0, KDSETMODE, KD_GRAPHICS);
+#endif
 	return 0;
 }
 
 int enable_vga() {
-	outb(0x03 | inb(0x3CC),  0x3C2);
-	outb(0x01 | inb(0x3C3),  0x3C3);
-	outb(0x08 | inb(0x46e8), 0x46e8);
-	outb(0x01 | inb(0x102),  0x102);
+	asm_outb(0x03 | asm_inb(0x3CC),  0x3C2);
+	asm_outb(0x01 | asm_inb(0x3C3),  0x3C3);
+	asm_outb(0x08 | asm_inb(0x46e8), 0x46e8);
+	asm_outb(0x01 | asm_inb(0x102),  0x102);
 	return 0;
 }
 
 int disable_vga() {
-	outb(~0x03 & inb(0x3CC),  0x3C2);
-	outb(~0x01 & inb(0x3C3),  0x3C3);
-	outb(~0x08 & inb(0x46e8), 0x46e8);
-	outb(~0x01 & inb(0x102),  0x102);
+	asm_outb(~0x03 & asm_inb(0x3CC),  0x3C2);
+	asm_outb(~0x01 & asm_inb(0x3C3),  0x3C3);
+	asm_outb(~0x08 & asm_inb(0x46e8), 0x46e8);
+	asm_outb(~0x01 & asm_inb(0x102),  0x102);
 	return 0;
 }
 
