d59db3f32fda03709b6d278336392dd1d5fe4f49
[mw/micromonitor-lm32.git] / umon_apps / demo / monlib.c
1 /* monlib.c:
2  *      This file is part of the monitor code, but it is actually linked into
3  *      the application.  It is built with (but not linked with) the monitor,
4  *      then the monlib.o file is linked with the application.
5  *      The only requirement on the application is that it know where the address
6  *      of the monCom function is in the monitor's space.  
7  *      The monCom function will be accessible in some "well-known" way (processor
8  *      and platform dependent) so that this will not be a problem.
9  *
10  *      This monlib.c file is a replacement for the older mechanism that was
11  *      a bit more error-prone...  A table of function pointers existed at some
12  *      well-known location in the monitor, and the content of that table was
13  *      assumed to also be "well-known".  This new version only assumes that the
14  *      pointer to monCom is well-known; everything else will work based on the
15  *      fact that the monitor and application will share the monlib.h header
16  *      file.
17  *
18  *      General notice:
19  *      This code is part of a boot-monitor package developed as a generic base
20  *      platform for embedded system designs.  As such, it is likely to be
21  *      distributed to various projects beyond the control of the original
22  *      author.  Please notify the author of any enhancements made or bugs found
23  *      so that all may benefit from the changes.  In addition, notification back
24  *      to the author will allow the new user to pick up changes that may have
25  *      been made by other users after this version of the code was distributed.
26  *
27  *      Note1: the majority of this code was edited with 4-space tabs.
28  *      Note2: as more and more contributions are accepted, the term "author"
29  *                 is becoming a mis-representation of credit.
30  *
31  *      Original author:        Ed Sutter
32  *      Email:                          esutter@lucent.com
33  *      Phone:                          908-582-2351
34  */
35 #include "monlib.h"
36
37 static int              (*_tfsseek)(int,int,int);
38 static int              (*_tfsgetline)(int,char *,int);
39 static int              (*_tfsipmod)(char *,char *,int,int);
40 static int              (*_tfsinit)(void);
41 static int              (*_tfsadd)(char *,char *,char *,unsigned char *,int);
42 static int              (*_tfsunlink)(char *);
43 static int              (*_tfsrun)(char **,int);
44 static int              (*_tfsread)(int,char *,int);
45 static int              (*_tfswrite)(int,char *,int);
46 static int              (*_tfsopen)(char *,long,char *);
47 static int              (*_tfsclose)(int,char *);
48 static int              (*_printf)();
49 static int              (*_cprintf)();
50 static int              (*_sprintf)();
51 static int              (*_monrestart)(int);
52 static int              (*_rputchar)(unsigned char c);
53 static int              (*_getchar)(void);
54 static int              (*_gotachar)(void);
55 static int              (*_getbytes)(char *,int,int);
56 static int              (*_addcommand)(struct monCommand *,char *);
57 static int              (*_docommand)(char *,int);
58 static int              (*_getline)(char *,int,int);
59 static int              (*_tfsfstat)(char *,struct tfshdr *);
60 static int              (*_tfseof)(int);
61 static int              (*_decompress)(char *,int,char *);
62 static int              (*_tfstruncate)(int,long);
63 static int              (*_heapextend)(char *,int);
64 static int              (*_tfslink)(char *,char *);
65 static int              (*_pcicfgwrite)(int,int,int,int,int,unsigned long);
66 static int              (*_i2cwrite)(int,int,unsigned char *,int);
67 static int              (*_i2cread)(int,int,unsigned char *,int);
68 static int              (*_flashwrite)(char *,char *,int);
69 static int              (*_flasherase)(int);
70 static int              (*_flashinfo)(int,int *,char **);
71 static int              (*_sendenet)(char *,int);
72 static int              (*_recvenet)(char *,int);
73 static int              (*_printpkt)(char *,int,int);
74 static int              (*_setenv)(char *,char *);
75 static int              (*_watchdog)(void);
76 static int              (*_timeofday)(int,void *);
77 static int              (*_montimer)(int cmd, void *arg);
78
79 static char             *(*_getenv)(char *);
80 static char             *(*_version)(void);
81 static char             *(*_getenvp)(void);
82 #ifdef MALLOC_DEBUG
83 static char             *(*_malloc)(int,char *,int);
84 static char             *(*_realloc)(char *buf,int,char *,int);
85 #else
86 static char             *(*_malloc)(int);
87 static char             *(*_realloc)(char *,int);
88 #endif
89 static char             *(*_getsym)(char *,char *,int);
90
91 static void             (*_intsrestore)(unsigned long);
92 static void             (*_appexit)(int);
93 static void             (*_free)(char *);
94 static void             (*_getargv)(int *,char ***);
95 static void             (*_profiler)(void *);
96 static void             (*_bbc)(char *,int);
97 static void             (*_memtrace)();
98 static void             (*_appwarmstart)(unsigned long);
99 static void             (*_mondelay)(long);
100 static void             (*_printmem)(char *,int,int);
101
102 static long             (*_tfsctrl)(int,long,long);
103 static long             (*_tfstell)(int);
104 static long             (*_portcmd)(int,void *);
105
106 static struct   tfshdr *(*_tfsnext)(struct tfshdr *);
107 static struct   tfshdr *(*_tfsstat)(char *);
108
109 static unsigned long    (*_i2cctrl)(int,int,unsigned long,unsigned long);
110 static unsigned long    (*_pcicfgread)(int,int,int,int,int);
111 static unsigned long    (*_pcictrl)(int,int,unsigned long,unsigned long);
112 static unsigned long    (*_crc32)(unsigned char *,unsigned long);
113 static unsigned long    (*_intsoff)(void);
114 static unsigned long    (*_assign_handler)(long,unsigned long,unsigned long);
115
116 static unsigned short   (*_xcrc16)(unsigned char *,unsigned long);
117
118
119 static void (*_monlock)(void);
120 static void (*_monunlock)(void);
121 static int      (*_moncom)(int,void *,void *, void *);
122
123 /**************************************************************************
124  *
125  * The following macros support the default monitor lock/unlock mechanism when
126  * they point to monLock and monUnlock.  If something other than the default
127  * is to be used, then simply redefine them here.  Refer to the monitor
128  * app note that discusses multi-tasking access to the monitor API for more
129  * information.
130  * 
131  * TFS_MONLOCK/UNLOCK:
132  * Lock/unlock for functions that access TFS flash space:
133  */
134 #define TFS_MONLOCK                     monLock
135 #define TFS_MONUNLOCK           monUnlock
136
137 /* ENV_MONLOCK/UNLOCK:
138  * Lock/unlock for functions that access monitor shell variables:
139  */
140 #define ENV_MONLOCK                     monLock
141 #define ENV_MONUNLOCK           monUnlock
142
143 /* CONSOLE_MONLOCK/UNLOCK:
144  * Lock/unlock for functions in the monitor that deal with console output.
145  */
146 #define CONSOLE_MONLOCK         monLock
147 #define CONSOLE_MONUNLOCK       monUnlock
148
149 /* HEAP_MONLOCK/UNLOCK:
150  * Lock/unlock for functions in the monitor that deal with the heap.
151  */
152 #define HEAP_MONLOCK            monLock
153 #define HEAP_MONUNLOCK          monUnlock
154
155 /* BLOCKING_MONLOCK/UNLOCK:
156  * Lock/unlock for functions in the monitor that block waiting for 
157  * console input.
158  */
159 #define BLOCKING_MONLOCK        monLock
160 #define BLOCKING_MONUNLOCK      monUnlock
161
162 /* GENERIC_MONLOCK/UNLOCK:
163  * Lock/unlock for all functions not covered by the above macros.
164  */
165 #define GENERIC_MONLOCK         monLock
166 #define GENERIC_MONUNLOCK       monUnlock
167
168 /**************************************************************************
169  *
170  * monConnect():
171  *      This must be the first call by the application code to talk to the
172  *      monitor.  It is expecting three incoming function pointers:
173  *
174  *      mon:    Points to the monitor's _moncom function;
175  *                      This is a "well-known" address because the monitor and
176  *                      application code (two separately linked binaries) must
177  *                      know it.
178  *      lock:   Points to a function in the application code that will be
179  *                      used by the monitor as a lock-out function (some kind of
180  *                      semaphore in the application).  
181  *      unlock: Points to a function in the application code that will be
182  *                      used by the monitor as an un-lock-out function (undo whatever
183  *                      lock-out mechanism was done by lock).
184  */
185 int
186 monConnect(int (*mon)(int,void *,void *,void *),
187         void (*lock)(void), void (*unlock)(void))
188 {
189         int rc = 0;
190
191         /* Assign incoming lock and unlock functions... */
192         _monlock = lock;
193         _monunlock = unlock;
194
195         /* If the mon pointer is non-zero, then make the mon_ connections... */
196         if (mon) {
197
198                 _moncom = mon;
199
200                 /* Make the connections between "mon_" functions that are                       */
201                 /* symbolically accessible by the application and the corresponding     */
202                 /* functions that exists in the monitor.                                                        */
203                 rc += _moncom(GETMONFUNC_PUTCHAR,&_rputchar,0,0);
204                 rc += _moncom(GETMONFUNC_GETCHAR,&_getchar,0,0);
205                 rc += _moncom(GETMONFUNC_GOTACHAR,&_gotachar,0,0);
206                 rc += _moncom(GETMONFUNC_GETBYTES,&_getbytes,0,0);
207                 rc += _moncom(GETMONFUNC_PRINTF,&_printf,0,0);
208                 rc += _moncom(GETMONFUNC_CPRINTF,&_cprintf,0,0);
209                 rc += _moncom(GETMONFUNC_SPRINTF,&_sprintf,0,0);
210                 rc += _moncom(GETMONFUNC_RESTART,&_monrestart,0,0);
211                 rc += _moncom(GETMONFUNC_GETENV,&_getenv,0,0);
212                 rc += _moncom(GETMONFUNC_SETENV,&_setenv,0,0);
213                 rc += _moncom(GETMONFUNC_TFSINIT,&_tfsinit,0,0);
214                 rc += _moncom(GETMONFUNC_TFSADD,&_tfsadd,0,0);
215                 rc += _moncom(GETMONFUNC_TFSUNLINK,&_tfsunlink,0,0);
216                 rc += _moncom(GETMONFUNC_TFSRUN,&_tfsrun,0,0);
217                 rc += _moncom(GETMONFUNC_TFSNEXT,&_tfsnext,0,0);
218                 rc += _moncom(GETMONFUNC_TFSSTAT,&_tfsstat,0,0);
219                 rc += _moncom(GETMONFUNC_TFSREAD,&_tfsread,0,0);
220                 rc += _moncom(GETMONFUNC_TFSWRITE,&_tfswrite,0,0);
221                 rc += _moncom(GETMONFUNC_TFSOPEN,&_tfsopen,0,0);
222                 rc += _moncom(GETMONFUNC_TFSCLOSE,&_tfsclose,0,0);
223                 rc += _moncom(GETMONFUNC_TFSSEEK,&_tfsseek,0,0);
224                 rc += _moncom(GETMONFUNC_TFSGETLINE,&_tfsgetline,0,0);
225                 rc += _moncom(GETMONFUNC_TFSIPMOD,&_tfsipmod,0,0);
226                 rc += _moncom(GETMONFUNC_TFSCTRL,&_tfsctrl,0,0);
227                 rc += _moncom(GETMONFUNC_ADDCOMMAND,&_addcommand,0,0);
228                 rc += _moncom(GETMONFUNC_DOCOMMAND,&_docommand,0,0);
229                 rc += _moncom(GETMONFUNC_GETARGV,&_getargv,0,0);
230                 rc += _moncom(GETMONFUNC_CRC16,&_xcrc16,0,0);
231                 rc += _moncom(GETMONFUNC_CRC32,&_crc32,0,0);
232                 rc += _moncom(GETMONFUNC_INTSOFF,&_intsoff,0,0);
233                 rc += _moncom(GETMONFUNC_INTSRESTORE,&_intsrestore,0,0);
234                 rc += _moncom(GETMONFUNC_APPEXIT,&_appexit,0,0);
235                 rc += _moncom(GETMONFUNC_MALLOC,&_malloc,0,0);
236                 rc += _moncom(GETMONFUNC_FREE,&_free,0,0);
237                 rc += _moncom(GETMONFUNC_GETLINE,&_getline,0,0);
238                 rc += _moncom(GETMONFUNC_TFSFSTAT,&_tfsfstat,0,0);
239                 rc += _moncom(GETMONFUNC_TFSEOF,&_tfseof,0,0);
240                 rc += _moncom(GETMONFUNC_DECOMPRESS,&_decompress,0,0);
241                 rc += _moncom(GETMONFUNC_TFSTRUNCATE,&_tfstruncate,0,0);
242                 rc += _moncom(GETMONFUNC_HEAPXTEND,&_heapextend,0,0);
243                 rc += _moncom(GETMONFUNC_PROFILER,&_profiler,0,0);
244                 rc += _moncom(GETMONFUNC_TFSLINK,&_tfslink,0,0);
245                 rc += _moncom(GETMONFUNC_BBC,&_bbc,0,0);
246                 rc += _moncom(GETMONFUNC_MEMTRACE,&_memtrace,0,0);
247                 rc += _moncom(GETMONFUNC_TFSTELL,&_tfstell,0,0);
248                 rc += _moncom(GETMONFUNC_VERSION,&_version,0,0);
249                 rc += _moncom(GETMONFUNC_WARMSTART,&_appwarmstart,0,0);
250                 rc += _moncom(GETMONFUNC_PCICFGREAD,&_pcicfgread,0,0);
251                 rc += _moncom(GETMONFUNC_PCICFGWRITE,&_pcicfgwrite,0,0);
252                 rc += _moncom(GETMONFUNC_PCICONTROL,&_pcictrl,0,0);
253                 rc += _moncom(GETMONFUNC_I2CREAD,&_i2cread,0,0);
254                 rc += _moncom(GETMONFUNC_I2CWRITE,&_i2cwrite,0,0);
255                 rc += _moncom(GETMONFUNC_I2CCONTROL,&_i2cctrl,0,0);
256                 rc += _moncom(GETMONFUNC_MONDELAY,&_mondelay,0,0);
257                 rc += _moncom(GETMONFUNC_GETENVP,&_getenvp,0,0);
258                 rc += _moncom(GETMONFUNC_REALLOC,&_realloc,0,0);
259                 rc += _moncom(GETMONFUNC_SENDENETPKT,&_sendenet,0,0);
260                 rc += _moncom(GETMONFUNC_RECVENETPKT,&_recvenet,0,0);
261                 rc += _moncom(GETMONFUNC_GETSYM,&_getsym,0,0);
262                 rc += _moncom(GETMONFUNC_PRINTPKT,&_printpkt,0,0);
263                 rc += _moncom(GETMONFUNC_FLASHWRITE,&_flashwrite,0,0);
264                 rc += _moncom(GETMONFUNC_FLASHERASE,&_flasherase,0,0);
265                 rc += _moncom(GETMONFUNC_FLASHINFO,&_flashinfo,0,0);
266                 rc += _moncom(GETMONFUNC_ASSIGNHDLR,&_assign_handler,0,0);
267                 rc += _moncom(GETMONFUNC_WATCHDOG,&_watchdog,0,0);
268                 rc += _moncom(GETMONFUNC_PRINTMEM,&_printmem,0,0);
269                 rc += _moncom(GETMONFUNC_PORTCMD,&_portcmd,0,0);
270                 rc += _moncom(GETMONFUNC_TIMEOFDAY,&_timeofday,0,0);
271                 rc += _moncom(GETMONFUNC_TIMER,&_montimer,0,0);
272         }
273         return(rc);
274 }
275
276 /* ignorelock:
277  * Used as a back-door to disable the monLock()/monUnlock() stuff.
278  * This is useful if the application CLI falls through to the monitor's
279  * CLI and you are using the "call" command in the monitor to execute some
280  * function that has a mon_xxx function in it.  In this case, the fact that
281  * the application has fallen through to the monitor means that the lock
282  * is already active, so when the function tries to call some other mon_xxx
283  * function it won't be able to because of the lock already being set.
284  *
285  * With these functions in the application space, the user can do the 
286  * following:
287  *      call %DisableLock
288  *  call %Func_with_monXXX_in_it
289  *  call %EnableLock
290  *
291  * Note that this is NOT to be used by application code, it is simply a
292  * back-door mechanism to allow "call" from the CLI to invoke functions
293  * that have mon_XXX functionality in them.
294  */
295 static int ignorelock = 0;
296
297 void
298 DisableMonLock(void)
299 {
300         ignorelock = 2;
301 }
302
303 void
304 EnableMonLock(void)
305 {
306         ignorelock = 0;
307 }
308
309 /* monLock() & monUnlock():
310  * Used by all of the wrapper functions below this point to call
311  * the function pointed to by _monlock & _monunlock function pointers
312  * (if set).
313  * These functions must test both the function pointer and the state
314  * of the ignorelock variable.  The function DisableMonLock() sets the
315  * ignorelock variable to 2 because it is being executed through "call"
316  * which means that the lock is active.  
317  */
318 static void
319 monLock()
320 {
321         if (_monlock) {
322                 switch(ignorelock) {
323                         case 1:
324                                 break;
325                         case 2:
326                                 ignorelock--;
327                                 break;
328                         default:
329                                 _monlock();
330                                 break;
331                 }
332         }
333 }
334
335 static void
336 monUnlock()
337 {
338         if (_monunlock) {
339                 switch(ignorelock) {
340                         case 1:
341                                 break;
342                         case 2:
343                                 ignorelock--;
344                         default:
345                                 _monunlock();
346                                 break;
347                 }
348         }
349 }
350
351 int
352 mon_com(int cmd, void *arg1, void *arg2, void *arg3)
353 {
354         int     ret;
355         
356         GENERIC_MONLOCK();
357         ret = _moncom(cmd,arg1,arg2,arg3);
358         GENERIC_MONUNLOCK();
359         return(ret);
360 }
361
362 int
363 mon_putchar(char c)
364 {
365         int     ret;
366         
367         CONSOLE_MONLOCK();
368         ret = _rputchar(c);
369         CONSOLE_MONUNLOCK();
370         return(ret);
371 }
372
373 int
374 mon_getchar(void)
375 {
376         int     ret;
377
378         BLOCKING_MONLOCK();
379         ret = _getchar();
380         BLOCKING_MONUNLOCK();
381         return(ret);
382 }
383
384 int
385 mon_gotachar(void)
386 {
387         int     ret;
388
389         GENERIC_MONLOCK();
390         ret = _gotachar();
391         GENERIC_MONUNLOCK();
392         return(ret);
393 }
394
395 int
396 mon_getbytes(char *buf,int cnt,int block)
397 {
398         int     ret;
399
400         BLOCKING_MONLOCK();
401         ret = _getbytes(buf,cnt,block);
402         BLOCKING_MONUNLOCK();
403         return(ret);
404 }
405
406 int
407 mon_printf(fmt,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12)
408 char *fmt;
409 int a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12;
410 {
411         int     ret;
412
413         CONSOLE_MONLOCK();
414         ret = _printf(fmt,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12);
415         CONSOLE_MONUNLOCK();
416         return(ret);
417 }
418
419 int
420 mon_cprintf(fmt,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12)
421 char *fmt;
422 int a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12;
423 {
424         int     ret;
425
426         CONSOLE_MONLOCK();
427         ret = _cprintf(fmt,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12);
428         CONSOLE_MONUNLOCK();
429         return(ret);
430 }
431
432 int
433 mon_sprintf(buf,fmt,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12)
434 char *fmt, *buf;
435 int a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12;
436 {
437         int     ret;
438
439         GENERIC_MONLOCK();
440         ret = _sprintf(buf,fmt,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12);
441         GENERIC_MONUNLOCK();
442         return(ret);
443 }
444
445 int
446 mon_restart(int val)
447 {
448         int     ret;
449
450         GENERIC_MONLOCK();
451         ret = _monrestart(val);
452         GENERIC_MONUNLOCK();
453         return(ret);
454 }
455
456 char *
457 mon_getenvp(void)
458 {
459         char *ret;
460
461         ENV_MONLOCK();
462         ret = _getenvp();
463         ENV_MONUNLOCK();
464         return(ret);
465 }
466
467 char *
468 mon_getenv(char *name)
469 {
470         char *ret;
471
472         ENV_MONLOCK();
473         ret = _getenv(name);
474         ENV_MONUNLOCK();
475         return(ret);
476 }
477
478 int
479 mon_setenv(char *name,char *val)
480 {
481         int ret;
482
483         ENV_MONLOCK();
484         ret = _setenv(name,val);
485         ENV_MONUNLOCK();
486         return(ret);
487 }
488
489 char *
490 mon_getsym(char *name,char *buf, int bufsize)
491 {
492         char *ret;
493
494         ENV_MONLOCK();
495         ret = _getsym(name,buf,bufsize);
496         ENV_MONUNLOCK();
497         return(ret);
498 }
499
500 int
501 mon_tfsinit(void)
502 {
503         int     ret;
504
505         TFS_MONLOCK();
506         ret = _tfsinit();
507         TFS_MONUNLOCK();
508         return(ret);
509 }
510
511 int
512 mon_tfsadd(char *name, char *info, char *flags, unsigned char *src, int size)
513 {
514         int     ret;
515
516         TFS_MONLOCK();
517         ret = _tfsadd(name,info,flags,src,size);
518         TFS_MONUNLOCK();
519         return(ret);
520 }
521
522 int
523 mon_tfslink(char *src, char *target)
524 {
525         int     ret;
526
527         TFS_MONLOCK();
528         ret = _tfslink(src,target);
529         TFS_MONUNLOCK();
530         return(ret);
531 }
532
533 int
534 mon_tfsunlink(char *name)
535 {
536         int     ret;
537
538         TFS_MONLOCK();
539         ret = _tfsunlink(name);
540         TFS_MONUNLOCK();
541         return(ret);
542 }
543
544 int
545 mon_tfsrun(char **name,int verbose)
546 {
547         int     ret;
548
549         TFS_MONLOCK();
550         ret = _tfsrun(name,verbose);
551         TFS_MONUNLOCK();
552         return(ret);
553 }
554
555 struct tfshdr *
556 mon_tfsnext(struct tfshdr *fp)
557 {
558         struct tfshdr *ret;
559
560         TFS_MONLOCK();
561         ret = _tfsnext(fp);
562         TFS_MONUNLOCK();
563         return(ret);
564 }
565
566 int
567 mon_tfstruncate(int tfd, long len)
568 {
569         int ret;
570
571         TFS_MONLOCK();
572         ret = _tfstruncate(tfd,len);
573         TFS_MONUNLOCK();
574         return(ret);
575 }
576
577 int
578 mon_tfseof(int tfd)
579 {
580         int ret;
581
582         TFS_MONLOCK();
583         ret = _tfseof(tfd);
584         TFS_MONUNLOCK();
585         return(ret);
586 }
587
588 int
589 mon_tfsfstat(char *name, struct tfshdr *fp)
590 {
591         int ret;
592
593         TFS_MONLOCK();
594         ret = _tfsfstat(name,fp);
595         TFS_MONUNLOCK();
596         return(ret);
597 }
598
599 struct tfshdr *
600 mon_tfsstat(char *name)
601 {
602         struct tfshdr *ret;
603
604         TFS_MONLOCK();
605         ret = _tfsstat(name);
606         TFS_MONUNLOCK();
607         return(ret);
608 }
609
610 int
611 mon_tfsread(int fd, char *buf, int cnt)
612 {
613         int     ret;
614
615         TFS_MONLOCK();
616         ret = _tfsread(fd,buf,cnt);
617         TFS_MONUNLOCK();
618         return(ret);
619 }
620
621 int
622 mon_tfswrite(int fd, char *buf, int cnt)
623 {
624         int     ret;
625
626         TFS_MONLOCK();
627         ret = _tfswrite(fd,buf,cnt);
628         TFS_MONUNLOCK();
629         return(ret);
630 }
631
632 int
633 mon_tfsopen(char *file,long flagmode,char *buf)
634 {
635         int     ret;
636
637         TFS_MONLOCK();
638         ret = _tfsopen(file,flagmode,buf);
639         TFS_MONUNLOCK();
640         return(ret);
641 }
642
643 int
644 mon_tfsclose(int fd,char *info)
645 {
646         int     ret;
647
648         TFS_MONLOCK();
649         ret = _tfsclose(fd,info);
650         TFS_MONUNLOCK();
651         return(ret);
652 }
653
654 int
655 mon_tfsseek(int fd, int offset, int whence)
656 {
657         int     ret;
658
659         TFS_MONLOCK();
660         ret = _tfsseek(fd,offset,whence);
661         TFS_MONUNLOCK();
662         return(ret);
663 }
664
665 int
666 mon_tfsgetline(int fd,char *bp,int max)
667 {
668         int     ret;
669
670         TFS_MONLOCK();
671         ret = _tfsgetline(fd,bp,max);
672         TFS_MONUNLOCK();
673         return(ret);
674 }
675
676 int
677 mon_tfsipmod(char *name,char *buf,int offset,int size)
678 {
679         int     ret;
680
681         TFS_MONLOCK();
682         ret = _tfsipmod(name,buf,offset,size);
683         TFS_MONUNLOCK();
684         return(ret);
685 }
686
687 long
688 mon_tfsctrl(int rqst,long arg1,long arg2)
689 {
690         long    ret;
691
692         TFS_MONLOCK();
693         ret = _tfsctrl(rqst,arg1,arg2);
694         TFS_MONUNLOCK();
695         return(ret);
696 }
697
698 long
699 mon_tfstell(int fd)
700 {
701         long    ret;
702
703         TFS_MONLOCK();
704         ret = _tfstell(fd);
705         TFS_MONUNLOCK();
706         return(ret);
707 }
708
709 int
710 mon_addcommand(struct monCommand *cmdlist, char *cmdlvl)
711 {
712         int     ret;
713
714         GENERIC_MONLOCK();
715         ret = _addcommand(cmdlist,cmdlvl);
716         GENERIC_MONUNLOCK();
717         return(ret);
718 }
719
720 int
721 mon_docommand(char *cmdline,int verbose)
722 {
723         int     ret;
724
725         GENERIC_MONLOCK();
726         ret = _docommand(cmdline,verbose);
727         GENERIC_MONUNLOCK();
728         return(ret);
729 }
730
731 void
732 mon_getargv(int *argc,char ***argv)
733 {
734         GENERIC_MONLOCK();
735         _getargv(argc,argv);
736         GENERIC_MONUNLOCK();
737 }
738
739 unsigned short
740 mon_xcrc16(char *buf,long nbytes)
741 {
742         unsigned short ret;
743
744         GENERIC_MONLOCK();
745         ret = _xcrc16((unsigned char *)buf,nbytes);
746         GENERIC_MONUNLOCK();
747         return(ret);
748 }
749
750 unsigned long
751 mon_intsoff(void)
752 {
753         unsigned long ret;
754
755         GENERIC_MONLOCK();
756         ret = _intsoff();
757         GENERIC_MONUNLOCK();
758         return(ret);
759 }
760
761 void
762 mon_intsrestore(unsigned long msr)
763 {
764         GENERIC_MONLOCK();
765         _intsrestore(msr);
766         GENERIC_MONUNLOCK();
767 }
768
769 void
770 mon_appexit(int val)
771 {
772         GENERIC_MONLOCK();
773         _appexit(val);
774         GENERIC_MONUNLOCK();
775 }
776
777 #ifdef MALLOC_DEBUG
778 char *
779 mon_malloc(int size,char *fname,int fline)
780 {
781         char *ret;
782
783         HEAP_MONLOCK();
784         ret = _malloc(size,fname,fline);
785         HEAP_MONUNLOCK();
786         return(ret);
787 }
788
789 char *
790 mon_realloc(char *buf, int size, char *fname, int fline)
791 {
792         char *ret;
793
794         HEAP_MONLOCK();
795         ret = _realloc(buf,size, fname, fline);
796         HEAP_MONUNLOCK();
797         return(ret);
798 }
799 #else
800 char *
801 mon_malloc(int size)
802 {
803         char *ret;
804
805         HEAP_MONLOCK();
806         ret = _malloc(size);
807         HEAP_MONUNLOCK();
808         return(ret);
809 }
810
811 char *
812 mon_realloc(char *buf, int size)
813 {
814         char *ret;
815
816         HEAP_MONLOCK();
817         ret = _realloc(buf,size);
818         HEAP_MONUNLOCK();
819         return(ret);
820 }
821 #endif
822
823 void
824 mon_free(char *cp)
825 {
826         HEAP_MONLOCK();
827         _free(cp);
828         HEAP_MONUNLOCK();
829 }
830
831 int
832 mon_getline(char *buf,int max,int ledit)
833 {
834         int     ret;
835
836         BLOCKING_MONLOCK();
837         ret = _getline(buf,max,ledit);
838         BLOCKING_MONUNLOCK();
839         return(ret);
840 }
841
842 int
843 mon_decompress(char *src,int srcsize,char *dest)
844 {
845         int     ret;
846
847         GENERIC_MONLOCK();
848         ret = _decompress(src,srcsize,dest);
849         GENERIC_MONUNLOCK();
850         return(ret);
851 }
852
853 int
854 mon_heapextend(char *base,int size)
855 {
856         int     ret;
857
858         GENERIC_MONLOCK();
859         ret = _heapextend(base,size);
860         GENERIC_MONUNLOCK();
861         return(ret);
862 }
863
864 void
865 mon_bbc(char *filename, int lineno)
866 {
867         _bbc(filename, lineno);
868 }
869
870 void
871 mon_profiler(void *pdata)
872 {
873         _profiler(pdata);
874 }
875
876 void
877 mon_memtrace(fmt,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12)
878 char *fmt;
879 int a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12;
880 {
881         _memtrace(fmt,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12);
882 }
883
884 char *
885 mon_version(void)
886 {
887         char *ret;
888
889         GENERIC_MONLOCK();
890         ret = _version();
891         GENERIC_MONUNLOCK();
892         return(ret);
893 }
894
895 void
896 mon_warmstart(unsigned long mask)
897 {
898         GENERIC_MONLOCK();
899         _appwarmstart(mask);
900         GENERIC_MONUNLOCK();
901 }
902
903 int
904 mon_pcicfgwrite(int interface,int bus,int dev,int func,int reg,
905         unsigned long val)
906 {
907         int     retval;
908
909         GENERIC_MONLOCK();
910         retval = _pcicfgwrite(interface,bus,dev,func,reg,val);
911         GENERIC_MONUNLOCK();
912         return(retval);
913 }
914
915 unsigned long
916 mon_pcicfgread(int interface,int bus,int dev, int func,int reg)
917 {
918         unsigned long retval;
919
920         GENERIC_MONLOCK();
921         retval = _pcicfgread(interface,bus,dev,func,reg);
922         GENERIC_MONUNLOCK();
923         return(retval);
924 }
925
926 unsigned long
927 mon_pcictrl(int interface, int cmd, unsigned long arg1, unsigned long arg2)
928 {
929         unsigned long val;
930
931         GENERIC_MONLOCK();
932         val = _pcictrl(interface,cmd,arg1,arg2);
933         GENERIC_MONUNLOCK();
934         return(val);
935 }
936
937 unsigned long
938 mon_i2cctrl(int interface, int cmd, unsigned long arg1, unsigned long arg2)
939 {
940         unsigned long val;
941
942         GENERIC_MONLOCK();
943         val = _i2cctrl(interface,cmd,arg1,arg2);
944         GENERIC_MONUNLOCK();
945         return(val);
946 }
947
948 int
949 mon_i2cwrite(int interface, int bigaddr, unsigned char *data, int len)
950 {
951         int     val;
952
953         GENERIC_MONLOCK();
954         val = _i2cwrite(interface,bigaddr,data,len);
955         GENERIC_MONUNLOCK();
956         return(val);
957 }
958
959 int
960 mon_i2cread(int interface, int bigaddr, unsigned char *data, int len)
961 {
962         int     val;
963
964         GENERIC_MONLOCK();
965         val = _i2cread(interface,bigaddr,data,len);
966         GENERIC_MONUNLOCK();
967         return(val);
968 }
969
970 void
971 mon_delay(long msec)
972 {
973         GENERIC_MONLOCK();
974         _mondelay(msec);
975         GENERIC_MONUNLOCK();
976 }
977
978 int
979 mon_timer(int cmd, void *arg)
980 {
981         int ret;
982
983         GENERIC_MONLOCK();
984         ret = _montimer(cmd, arg);
985         GENERIC_MONUNLOCK();
986         return(ret);
987 }
988
989 int
990 mon_sendenetpkt(char *pkt,int size)
991 {
992         int     ret;
993
994         GENERIC_MONLOCK();
995         ret = _sendenet(pkt,size);
996         GENERIC_MONUNLOCK();
997         return(ret);
998 }
999
1000 int
1001 mon_recvenetpkt(char *pkt,int size)
1002 {
1003         int     ret;
1004
1005         GENERIC_MONLOCK();
1006         ret = _recvenet(pkt,size);
1007         GENERIC_MONUNLOCK();
1008         return(ret);
1009 }
1010
1011 void
1012 mon_printpkt(char *buf,int size, int incoming)
1013 {
1014         GENERIC_MONLOCK();
1015         _printpkt(buf,size,incoming);
1016         GENERIC_MONUNLOCK();
1017 }
1018
1019 int
1020 mon_flashwrite(char *dest,char *src,int bytecnt)
1021 {
1022         int     ret;
1023
1024         TFS_MONLOCK();
1025         ret = _flashwrite(dest,src,bytecnt);
1026         TFS_MONUNLOCK();
1027         return(ret);
1028 }
1029
1030 int
1031 mon_flasherase(int snum)
1032 {
1033         int     ret;
1034
1035         TFS_MONLOCK();
1036         ret = _flasherase(snum);
1037         TFS_MONUNLOCK();
1038         return(ret);
1039 }
1040
1041 int
1042 mon_flashinfo(int snum, int *size, char **base)
1043 {
1044         int     ret;
1045
1046         TFS_MONLOCK();
1047         ret = _flashinfo(snum,size,base);
1048         TFS_MONUNLOCK();
1049         return(ret);
1050 }
1051
1052 unsigned long
1053 mon_assignhandler(long hnum, unsigned long arg1, unsigned long arg2)
1054 {
1055         int     ret;
1056
1057         GENERIC_MONLOCK();
1058         ret = _assign_handler(hnum,arg1,arg2);
1059         GENERIC_MONUNLOCK();
1060         return(ret);
1061 }
1062
1063 int
1064 mon_watchdog(void)
1065 {
1066         int     ret;
1067
1068         GENERIC_MONLOCK();
1069         ret = _watchdog();
1070         GENERIC_MONUNLOCK();
1071         return(ret);
1072 }
1073
1074 void
1075 mon_printmem(char *mem, int size, int ascii)
1076 {
1077         GENERIC_MONLOCK();
1078         _printmem(mem,size,ascii);
1079         GENERIC_MONUNLOCK();
1080 }
1081
1082 long
1083 mon_portcmd(int cmd, void *arg)
1084 {
1085         long ret;
1086
1087         GENERIC_MONLOCK();
1088         ret = _portcmd(cmd,arg);
1089         GENERIC_MONUNLOCK();
1090         return(ret);
1091 }
1092
1093 int
1094 mon_timeofday(int cmd, void *arg)
1095 {
1096         int ret;
1097
1098         GENERIC_MONLOCK();
1099         ret = _timeofday(cmd,arg);
1100         GENERIC_MONUNLOCK();
1101         return(ret);
1102 }