Import upstream u-boot 1.1.4
[u-boot.git] / board / MAI / bios_emulator / scitech / src / common / galib.c
1 /****************************************************************************
2 *
3 *                   SciTech Nucleus Graphics Architecture
4 *
5 *               Copyright (C) 1991-1998 SciTech Software, Inc.
6 *                            All rights reserved.
7 *
8 *  ======================================================================
9 *  |REMOVAL OR MODIFICATION OF THIS HEADER IS STRICTLY PROHIBITED BY LAW|
10 *  |                                                                    |
11 *  |This copyrighted computer code contains proprietary technology      |
12 *  |owned by SciTech Software, Inc., located at 505 Wall Street,        |
13 *  |Chico, CA 95928 USA (http://www.scitechsoft.com).                   |
14 *  |                                                                    |
15 *  |The contents of this file are subject to the SciTech Nucleus        |
16 *  |License; you may *not* use this file or related software except in  |
17 *  |compliance with the License. You may obtain a copy of the License   |
18 *  |at http://www.scitechsoft.com/nucleus-license.txt                   |
19 *  |                                                                    |
20 *  |Software distributed under the License is distributed on an         |
21 *  |"AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or      |
22 *  |implied. See the License for the specific language governing        |
23 *  |rights and limitations under the License.                           |
24 *  |                                                                    |
25 *  |REMOVAL OR MODIFICATION OF THIS HEADER IS STRICTLY PROHIBITED BY LAW|
26 *  ======================================================================
27 *
28 * Language:     ANSI C
29 * Environment:  Any 32-bit protected mode environment
30 *
31 * Description:  C module for the Graphics Accelerator Driver API. Uses
32 *               the SciTech PM library for interfacing with DOS
33 *               extender specific functions.
34 *
35 ****************************************************************************/
36
37 #include "nucleus/graphics.h"
38 #if defined(__WIN32_VXD__) || defined(__NT_DRIVER__)
39 #include "sdd/sddhelp.h"
40 #else
41 #include <stdio.h>
42 #include <stdlib.h>
43 #endif
44
45 /*---------------------------- Global Variables ---------------------------*/
46
47 #ifndef TEST_HARNESS
48 GA_exports  _VARAPI __GA_exports;
49 static int          loaded = false;
50 static PE_MODULE    *hModBPD = NULL;
51
52 static N_imports _N_imports = {
53     sizeof(N_imports),
54     _OS_delay,
55     };
56
57 static GA_imports _GA_imports = {
58     sizeof(GA_imports),
59     GA_getSharedInfo,
60     GA_TimerInit,
61     GA_TimerRead,
62     GA_TimerDifference,
63     };
64 #endif
65
66 /*----------------------------- Implementation ----------------------------*/
67
68 #define DLL_NAME        "graphics.bpd"
69
70 /****************************************************************************
71 REMARKS:
72 This function is no longer used but we must implement it and return NULL
73 for compatibility with older binary drivers.
74 ****************************************************************************/
75 GA_sharedInfo * NAPI GA_getSharedInfo(
76     int device)
77 {
78     return NULL;
79 }
80
81 #ifndef TEST_HARNESS
82 /****************************************************************************
83 REMARKS:
84 Fatal error handler for non-exported GA_exports.
85 ****************************************************************************/
86 static void _GA_fatalErrorHandler(void)
87 {
88     PM_fatalError("Unsupported Nucleus export function called! Please upgrade your copy of Nucleus!\n");
89 }
90
91 /****************************************************************************
92 PARAMETERS:
93 shared  - True to load the driver into shared memory.
94
95 REMARKS:
96 Loads the Nucleus binary portable DLL into memory and initilises it.
97 ****************************************************************************/
98 static ibool LoadDriver(
99     ibool shared)
100 {
101     GA_initLibrary_t    GA_initLibrary;
102     GA_exports          *gaExp;
103     char                filename[PM_MAX_PATH];
104     char                bpdpath[PM_MAX_PATH];
105     int                 i,max;
106     ulong               *p;
107
108     /* Check if we have already loaded the driver */
109     if (loaded)
110         return true;
111     PM_init();
112
113     /* First try to see if we can find the system wide shared exports
114      * if they are available. Under OS/2 this connects to our global
115      * shared Nucleus loader in SDDPMI.DLL.
116      */
117     __GA_exports.dwSize = sizeof(__GA_exports);
118     if (GA_getSharedExports(&__GA_exports,shared))
119         return loaded = true;
120
121     /* Open the BPD file */
122     if (!PM_findBPD(DLL_NAME,bpdpath))
123         return false;
124     strcpy(filename,bpdpath);
125     strcat(filename,DLL_NAME);
126     if ((hModBPD = PE_loadLibrary(filename,shared)) == NULL)
127         return false;
128     if ((GA_initLibrary = (GA_initLibrary_t)PE_getProcAddress(hModBPD,"_GA_initLibrary")) == NULL)
129         return false;
130     bpdpath[strlen(bpdpath)-1] = 0;
131     if (strcmp(bpdpath,PM_getNucleusPath()) == 0)
132         strcpy(bpdpath,PM_getNucleusConfigPath());
133     else {
134         PM_backslash(bpdpath);
135         strcat(bpdpath,"config");
136         }
137     if ((gaExp = GA_initLibrary(shared,bpdpath,filename,GA_getSystemPMImports(),&_N_imports,&_GA_imports)) == NULL)
138         PM_fatalError("GA_initLibrary failed!\n");
139
140     /* Initialize all default imports to point to fatal error handler
141      * for upwards compatibility, and copy the exported functions.
142      */
143     max = sizeof(__GA_exports)/sizeof(GA_initLibrary_t);
144     for (i = 0,p = (ulong*)&__GA_exports; i < max; i++)
145         *p++ = (ulong)_GA_fatalErrorHandler;
146     memcpy(&__GA_exports,gaExp,MIN(sizeof(__GA_exports),gaExp->dwSize));
147     loaded = true;
148     return true;
149 }
150
151 /* The following are stub entry points that the application calls to
152  * initialise the Nucleus loader library, and we use this to load our
153  * driver DLL from disk and initialise the library using it.
154  */
155
156 /* {secret} */
157 int NAPI GA_status(void)
158 {
159     if (!loaded)
160         return nDriverNotFound;
161     return __GA_exports.GA_status();
162 }
163
164 /* {secret} */
165 const char * NAPI GA_errorMsg(
166     N_int32 status)
167 {
168     if (!loaded)
169         return "Unable to load Nucleus device driver!";
170     return __GA_exports.GA_errorMsg(status);
171 }
172
173 /* {secret} */
174 int NAPI GA_getDaysLeft(N_int32 shared)
175 {
176     if (!LoadDriver(shared))
177         return -1;
178     return __GA_exports.GA_getDaysLeft(shared);
179 }
180
181 /* {secret} */
182 int NAPI GA_registerLicense(uchar *license,N_int32 shared)
183 {
184     if (!LoadDriver(shared))
185         return 0;
186     return __GA_exports.GA_registerLicense(license,shared);
187 }
188
189 /* {secret} */
190 ibool NAPI GA_loadInGUI(N_int32 shared)
191 {
192     if (!LoadDriver(shared))
193         return false;
194     return __GA_exports.GA_loadInGUI(shared);
195 }
196
197 /* {secret} */
198 int NAPI GA_enumerateDevices(N_int32 shared)
199 {
200     if (!LoadDriver(shared))
201         return 0;
202     return __GA_exports.GA_enumerateDevices(shared);
203 }
204
205 /* {secret} */
206 GA_devCtx * NAPI GA_loadDriver(N_int32 deviceIndex,N_int32 shared)
207 {
208     if (!LoadDriver(shared))
209         return NULL;
210     return __GA_exports.GA_loadDriver(deviceIndex,shared);
211 }
212
213 /* {secret} */
214 void NAPI GA_getGlobalOptions(
215     GA_globalOptions *options,
216     ibool shared)
217 {
218     if (LoadDriver(shared))
219         __GA_exports.GA_getGlobalOptions(options,shared);
220 }
221
222 /* {secret} */
223 PE_MODULE * NAPI GA_loadLibrary(
224     const char *szBPDName,
225     ulong *size,
226     ibool shared)
227 {
228     if (!LoadDriver(shared))
229         return NULL;
230     return __GA_exports.GA_loadLibrary(szBPDName,size,shared);
231 }
232
233 /* {secret} */
234 GA_devCtx * NAPI GA_getCurrentDriver(
235     N_int32 deviceIndex)
236 {
237     /* Bail for older drivers that didn't export this function! */
238     if (!__GA_exports.GA_getCurrentDriver)
239         return NULL;
240     return __GA_exports.GA_getCurrentDriver(deviceIndex);
241 }
242
243 /* {secret} */
244 REF2D_driver * NAPI GA_getCurrentRef2d(
245     N_int32 deviceIndex)
246 {
247     /* Bail for older drivers that didn't export this function! */
248     if (!__GA_exports.GA_getCurrentRef2d)
249         return NULL;
250     return __GA_exports.GA_getCurrentRef2d(deviceIndex);
251 }
252
253 /* {secret} */
254 int NAPI GA_isOEMVersion(ibool shared)
255 {
256     if (!LoadDriver(shared))
257         return 0;
258     return __GA_exports.GA_isOEMVersion(shared);
259 }
260
261 /* {secret} */
262 N_uint32 * NAPI GA_getLicensedDevices(ibool shared)
263 {
264     if (!LoadDriver(shared))
265         return 0;
266     return __GA_exports.GA_getLicensedDevices(shared);
267 }
268 #endif