pm.c
来自「适合KS8695X」· C语言 代码 · 共 892 行 · 第 1/2 页
C
892 行
/****************************************************************************
*
* SciTech OS Portability Manager Library
*
* ========================================================================
*
* The contents of this file are subject to the SciTech MGL Public
* License Version 1.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.scitechsoft.com/mgl-license.txt
*
* Software distributed under the License is distributed on an
* "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is Copyright (C) 1991-1998 SciTech Software, Inc.
*
* The Initial Developer of the Original Code is SciTech Software, Inc.
* All Rights Reserved.
*
* ========================================================================
*
* Language: ANSI C
* Environment: QNX
*
* Description: Implementation for the OS Portability Manager Library, which
* contains functions to implement OS specific services in a
* generic, cross platform API. Porting the OS Portability
* Manager library is the first step to porting any SciTech
* products to a new platform.
*
****************************************************************************/
#include "pmapi.h"
#include "drvlib/os/os.h"
#include "mtrr.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <termios.h>
#include <fcntl.h>
#include <malloc.h>
#include <sys/mman.h>
#include "qnx/vbios.h"
#ifndef __QNXNTO__
#include <sys/seginfo.h>
#include <sys/console.h>
#include <conio.h>
#include <i86.h>
#else
#include <sys/neutrino.h>
#include <sys/dcmd_chr.h>
#endif
/*--------------------------- Global variables ----------------------------*/
static uint VESABuf_len = 1024; /* Length of the VESABuf buffer */
static void *VESABuf_ptr = NULL; /* Near pointer to VESABuf */
static uint VESABuf_rseg; /* Real mode segment of VESABuf */
static uint VESABuf_roff; /* Real mode offset of VESABuf */
static VBIOSregs_t *VRegs = NULL; /* Pointer to VBIOS registers */
static int raw_count = 0;
static struct _console_ctrl *cc = NULL;
static int console_count = 0;
static int rmbuf_inuse = 0;
static void (PMAPIP fatalErrorCleanup)(void) = NULL;
/*----------------------------- Implementation ----------------------------*/
void PMAPI PM_init(void)
{
char *force;
if (VRegs == NULL) {
#ifdef __QNXNTO__
ThreadCtl(_NTO_TCTL_IO, 0); /* Get IO privilidge */
#endif
force = getenv("VBIOS_METHOD");
VRegs = VBIOSinit(force ? atoi(force) : 0);
}
#ifndef __QNXNTO__
MTRR_init();
#endif
}
ibool PMAPI PM_haveBIOSAccess(void)
{ return VRegs != NULL; }
long PMAPI PM_getOSType(void)
{ return _OS_QNX; }
int PMAPI PM_getModeType(void)
{ return PM_386; }
void PMAPI PM_backslash(char *s)
{
uint pos = strlen(s);
if (s[pos-1] != '/') {
s[pos] = '/';
s[pos+1] = '\0';
}
}
void PMAPI PM_setFatalErrorCleanup(
void (PMAPIP cleanup)(void))
{
fatalErrorCleanup = cleanup;
}
void PMAPI PM_fatalError(const char *msg)
{
if (fatalErrorCleanup)
fatalErrorCleanup();
fprintf(stderr,"%s\n", msg);
exit(1);
}
static void ExitVBEBuf(void)
{
if (VESABuf_ptr)
PM_freeRealSeg(VESABuf_ptr);
VESABuf_ptr = 0;
}
void * PMAPI PM_getVESABuf(uint *len,uint *rseg,uint *roff)
{
if (!VESABuf_ptr) {
/* Allocate a global buffer for communicating with the VESA VBE */
if ((VESABuf_ptr = PM_allocRealSeg(VESABuf_len, &VESABuf_rseg, &VESABuf_roff)) == NULL)
return NULL;
atexit(ExitVBEBuf);
}
*len = VESABuf_len;
*rseg = VESABuf_rseg;
*roff = VESABuf_roff;
return VESABuf_ptr;
}
static int term_raw(void)
{
struct termios termios_p;
if (raw_count++ > 0)
return 0;
/* Go into "raw" input mode */
if (tcgetattr(STDIN_FILENO, &termios_p))
return -1;
termios_p.c_cc[VMIN] = 1;
termios_p.c_cc[VTIME] = 0;
termios_p.c_lflag &= ~( ECHO|ICANON|ISIG|ECHOE|ECHOK|ECHONL);
tcsetattr(STDIN_FILENO, TCSADRAIN, &termios_p);
return 0;
}
static void term_restore(void)
{
struct termios termios_p;
if (raw_count-- != 1)
return;
tcgetattr(STDIN_FILENO, &termios_p);
termios_p.c_lflag |= (ECHO|ICANON|ISIG|ECHOE|ECHOK|ECHONL);
termios_p.c_oflag |= (OPOST);
tcsetattr(STDIN_FILENO, TCSADRAIN, &termios_p);
}
int PMAPI PM_kbhit(void)
{
int blocking, c;
if (term_raw() == -1)
return 0;
/* Go into non blocking mode */
blocking = fcntl(STDIN_FILENO, F_GETFL) | O_NONBLOCK;
fcntl(STDIN_FILENO, F_SETFL, blocking);
c = getc(stdin);
/* restore blocking mode */
fcntl(STDIN_FILENO, F_SETFL, blocking & ~O_NONBLOCK);
term_restore();
if (c != EOF) {
ungetc(c, stdin);
return c;
}
clearerr(stdin);
return 0;
}
int PMAPI PM_getch(void)
{
int c;
if (term_raw() == -1)
return (0);
c = getc(stdin);
#if defined(__QNX__) && !defined(__QNXNTO__)
if (c == 0xA)
c = 0x0D;
else if (c == 0x7F)
c = 0x08;
#endif
term_restore();
return c;
}
PM_HWND PMAPI PM_openConsole(
PM_HWND hwndUser,
int device,
int xRes,
int yRes,
int bpp,
ibool fullScreen)
{
#ifndef __QNXNTO__
int fd;
if (console_count++)
return 0;
if ((fd = open("/dev/con1", O_RDWR)) == -1)
return -1;
cc = console_open(fd, O_RDWR);
close(fd);
if (cc == NULL)
return -1;
#endif
return 1;
}
int PMAPI PM_getConsoleStateSize(void)
{
return PM_getVGAStateSize() + sizeof(int) * 3;
}
void PMAPI PM_saveConsoleState(void *stateBuf,int console_id)
{
#ifdef __QNXNTO__
int fd;
int flags;
if ((fd = open("/dev/con1", O_RDWR)) == -1)
return;
flags = _CONCTL_INVISIBLE_CHG | _CONCTL_INVISIBLE;
devctl(fd, DCMD_CHR_SERCTL, &flags, sizeof flags, 0);
close(fd);
#else
uchar *buf = &((uchar*)stateBuf)[PM_getVGAStateSize()];
/* Save QNX 4 console state */
console_read(cc, -1, 0, NULL, 0,
(int *)buf+1, (int *)buf+2, NULL);
*(int *)buf = console_ctrl(cc, -1,
CONSOLE_NORESIZE | CONSOLE_NOSWITCH | CONSOLE_INVISIBLE,
CONSOLE_NORESIZE | CONSOLE_NOSWITCH | CONSOLE_INVISIBLE);
/* Save state of VGA registers */
PM_saveVGAState(stateBuf);
#endif
}
void PMAPI PM_setSuspendAppCallback(int (_ASMAPIP saveState)(int flags))
{
/* TODO: Implement support for console switching if possible */
}
void PMAPI PM_restoreConsoleState(const void *stateBuf,PM_HWND hwndConsole)
{
#ifdef __QNXNTO__
int fd;
int flags;
if ((fd = open("/dev/con1", O_RDWR)) == -1)
return;
flags = _CONCTL_INVISIBLE_CHG;
devctl(fd, DCMD_CHR_SERCTL, &flags, sizeof flags, 0);
close(fd);
#else
uchar *buf = &((uchar*)stateBuf)[PM_getVGAStateSize()];
/* Restore the state of the VGA compatible registers */
PM_restoreVGAState(stateBuf);
/* Restore QNX 4 console state */
console_ctrl(cc, -1, *(int *)buf,
CONSOLE_NORESIZE | CONSOLE_NOSWITCH | CONSOLE_INVISIBLE);
console_write(cc, -1, 0, NULL, 0,
(int *)buf+1, (int *)buf+2, NULL);
#endif
}
void PMAPI PM_closeConsole(PM_HWND hwndConsole)
{
#ifndef __QNXNTO__
if (--console_count == 0) {
console_close(cc);
cc = NULL;
}
#endif
}
void PM_setOSCursorLocation(int x,int y)
{
if (!cc)
return;
#ifndef __QNXNTO__
console_write(cc, -1, 0, NULL, 0, &y, &x, NULL);
#endif
}
void PM_setOSScreenWidth(int width,int height)
{
}
ibool PMAPI PM_setRealTimeClockHandler(PM_intHandler ih, int frequency)
{
/* TODO: Implement this for QNX */
return false;
}
void PMAPI PM_setRealTimeClockFrequency(int frequency)
{
/* TODO: Implement this for QNX */
}
void PMAPI PM_restoreRealTimeClockHandler(void)
{
/* TODO: Implement this for QNX */
}
char * PMAPI PM_getCurrentPath(
char *path,
int maxLen)
{
return getcwd(path,maxLen);
}
char PMAPI PM_getBootDrive(void)
{ return '/'; }
const char * PMAPI PM_getVBEAFPath(void)
{ return PM_getNucleusConfigPath(); }
const char * PMAPI PM_getNucleusPath(void)
{
char *env = getenv("NUCLEUS_PATH");
#ifdef __QNXNTO__
#ifdef __X86__
return env ? env : "/nto/scitech/x86/bin";
#elif defined (__PPC__)
return env ? env : "/nto/scitech/ppcbe/bin";
#elif defined (__MIPS__)
#ifdef __BIGENDIAN__
return env ? env : "/nto/scitech/mipsbe/bin";
#else
return env ? env : "/nto/scitech/mipsle/bin";
#endif
#elif defined (__SH__)
#ifdef __BIGENDIAN__
return env ? env : "/nto/scitech/shbe/bin";
#else
return env ? env : "/nto/scitech/shle/bin";
#endif
#elif defined (__ARM__)
return env ? env : "/nto/scitech/armle/bin";
#endif
#else /* QNX 4 */
return env ? env : "/qnx4/scitech/bin";
#endif
}
const char * PMAPI PM_getNucleusConfigPath(void)
{
static char path[512];
char *env;
#ifdef __QNXNTO__
char temp[64];
gethostname(temp, sizeof (temp));
temp[sizeof (temp) - 1] = '\0'; /* Paranoid */
sprintf(path,"/etc/config/scitech/%s/config", temp);
#else
sprintf(path,"/etc/config/scitech/%d/config", getnid());
#endif
if ((env = getenv("NUCLEUS_PATH")) != NULL) {
strcpy(path,env);
PM_backslash(path);
strcat(path,"config");
}
return path;
}
const char * PMAPI PM_getUniqueID(void)
{
static char buf[128];
#ifdef __QNXNTO__
gethostname(buf, sizeof (buf));
#else
sprintf(buf,"node%d", getnid());
#endif
return buf;
}
const char * PMAPI PM_getMachineName(void)
{
static char buf[128];
#ifdef __QNXNTO__
gethostname(buf, sizeof (buf));
#else
sprintf(buf,"node%d", getnid());
#endif
return buf;
}
void * PMAPI PM_getBIOSPointer(void)
{
return PM_mapRealPointer(0, 0x400);
}
void * PMAPI PM_getA0000Pointer(void)
{
static void *ptr = NULL;
void *freeptr;
unsigned offset, i, maplen;
if (ptr != NULL)
return ptr;
/* Some trickery is required to get the linear address 64K aligned */
for (i = 0; i < 5; i++) {
ptr = PM_mapPhysicalAddr(0xA0000,0xFFFF,true);
offset = 0x10000 - ((unsigned)ptr % 0x10000);
if (!offset)
break;
munmap(ptr, 0x10000);
maplen = 0x10000 + offset;
freeptr = PM_mapPhysicalAddr(0xA0000-offset, maplen-1,true);
ptr = (void *)(offset + (unsigned)freeptr);
if (0x10000 - ((unsigned)ptr % 0x10000))
break;
munmap(freeptr, maplen);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?