1/*  *********************************************************************
2    *  Broadcom Common Firmware Environment (CFE)
3    *
4    *  Null device mode driver				File: download.c
5    *
6    *  Small program (placeholder) to download to a 1250 in PCI Device Mode
7    *
8    *********************************************************************
9    *
10    *  Copyright 2000,2001,2002,2003
11    *  Broadcom Corporation. All rights reserved.
12    *
13    *  This software is furnished under license and may be used and
14    *  copied only in accordance with the following terms and
15    *  conditions.  Subject to these conditions, you may download,
16    *  copy, install, use, modify and distribute modified or unmodified
17    *  copies of this software in source and/or binary form.  No title
18    *  or ownership is transferred hereby.
19    *
20    *  1) Any source code used, modified or distributed must reproduce
21    *     and retain this copyright notice and list of conditions
22    *     as they appear in the source file.
23    *
24    *  2) No right is granted to use any trade name, trademark, or
25    *     logo of Broadcom Corporation.  The "Broadcom Corporation"
26    *     name may not be used to endorse or promote products derived
27    *     from this software without the prior written permission of
28    *     Broadcom Corporation.
29    *
30    *  3) THIS SOFTWARE IS PROVIDED "AS-IS" AND ANY EXPRESS OR
31    *     IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, ANY IMPLIED
32    *     WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
33    *     PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT
34    *     SHALL BROADCOM BE LIABLE FOR ANY DAMAGES WHATSOEVER, AND IN
35    *     PARTICULAR, BROADCOM SHALL NOT BE LIABLE FOR DIRECT, INDIRECT,
36    *     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
37    *     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
38    *     GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
39    *     BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
40    *     OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
41    *     TORT (INCLUDING NEGLIGENCE OR OTHERWISE), EVEN IF ADVISED OF
42    *     THE POSSIBILITY OF SUCH DAMAGE.
43    ********************************************************************* */
44
45
46#include "lib_types.h"
47#include "lib_printf.h"
48#include "lib_string.h"
49#include "cfe_api.h"
50
51int conhandle;
52
53
54void appletmain(long handle,long vector,
55		long reserved,long seal);
56
57
58
59
60/*  *********************************************************************
61    *  console_write(buffer,length)
62    *
63    *  Write text to the console.  If the console is not open and
64    *  we're "saving" text, put the text on the in-memory queue
65    *
66    *  Input parameters:
67    *  	   buffer - pointer to data
68    *  	   length - number of characters to write
69    *
70    *  Return value:
71    *  	   number of characters written or <0 if error
72    ********************************************************************* */
73
74static int console_write(unsigned char *buffer,int length)
75{
76    int res;
77
78    /*
79     * Do nothing if no console
80     */
81
82    if (conhandle == -1) return -1;
83
84    /*
85     * Write text to device
86     */
87
88    for (;;) {
89	res = cfe_write(conhandle,buffer,length);
90	if (res < 0) break;
91	buffer += res;
92	length -= res;
93	if (length == 0) break;
94	}
95
96    if (res < 0) return -1;
97    return 0;
98}
99
100
101/*  *********************************************************************
102    *  console_xprint(str)
103    *
104    *  printf callback for the console device.  the "xprintf"
105    *  routine ends up calling this.  This routine also cooks the
106    *  output, turning "\n" into "\r\n"
107    *
108    *  Input parameters:
109    *  	   str - string to write
110    *
111    *  Return value:
112    *  	   number of characters written
113    ********************************************************************* */
114
115static int console_xprint(const char *str)
116{
117    int count = 0;
118    int len;
119    const char *p;
120
121    /* Convert CR to CRLF as we write things out */
122
123    while ((p = strchr(str,'\n'))) {
124	console_write((unsigned char *) str,p-str);
125	console_write((unsigned char *)"\r\n",2);
126	count += (p-str);
127	str = p + 1;
128	}
129
130    len = strlen(str);
131    console_write((unsigned char *) str, len);
132    count += len;
133
134    return count;
135}
136
137
138void appletmain(long handle,long vector,
139		long ept,long seal)
140{
141    void (*reboot)(void) = (void *) (uintptr_t) (int) 0xBFC00000;
142    char str[100];
143
144    xprinthook = console_xprint;
145
146    cfe_init(handle,ept);
147
148    conhandle = cfe_getstdhandle(CFE_STDHANDLE_CONSOLE);
149
150    str[0] = 0;
151    cfe_getenv("BOOT_CONSOLE",str,sizeof(str));
152
153    xprintf("\nHello, world.  Console = %s\n",str);
154
155    xprintf("\nThis is a null device driver that just restarts CFE\n");
156    xprintf("Rebuild the host's CFE to replace it with your driver.\n\n");
157
158    xprintf("Exiting to CFE\n\n");
159
160    cfe_exit(CFE_FLG_WARMSTART,0);
161
162    (*reboot)();
163}
164