1/******************************************************************************
2 *
3 * Filename: env_vars.c
4 *
5 * Instantiation of environment variables, structures, and other globals.
6 *
7 * Revision information:
8 *
9 * 20AUG2004	kb_admin	initial creation
10 *
11 * BEGIN_KBDD_BLOCK
12 * No warranty, expressed or implied, is included with this software.  It is
13 * provided "AS IS" and no warranty of any kind including statutory or aspects
14 * relating to merchantability or fitness for any purpose is provided.  All
15 * intellectual property rights of others is maintained with the respective
16 * owners.  This software is not copyrighted and is intended for reference
17 * only.
18 * END_BLOCK
19 *
20 * $FreeBSD$
21 *****************************************************************************/
22
23#include "env_vars.h"
24#include "loader_prompt.h"
25#include "lib.h"
26
27/******************************* GLOBALS *************************************/
28char	boot_commands[MAX_BOOT_COMMANDS][MAX_INPUT_SIZE];
29
30char	env_table[MAX_ENV_SIZE_BYTES];
31
32extern char	BootCommandSection;
33
34/************************** PRIVATE FUNCTIONS ********************************/
35
36
37static int	currentIndex;
38static int	currentOffset;
39
40
41/*
42 * .KB_C_FN_DEFINITION_START
43 * int ReadCharFromEnvironment(char *)
44 *  This private function reads characters from the enviroment variables
45 * to service the command prompt during auto-boot or just to setup the
46 * default environment.  Returns positive value if valid character was
47 * set in the pointer.  Returns negative value to signal input stream
48 * terminated.  Returns 0 to indicate _wait_ condition.
49 * .KB_C_FN_DEFINITION_END
50 */
51static int
52ReadCharFromEnvironment(int timeout)
53{
54	int ch;
55
56	if (currentIndex < MAX_BOOT_COMMANDS) {
57		ch = boot_commands[currentIndex][currentOffset++];
58		if (ch == '\0' || (currentOffset >= MAX_INPUT_SIZE)) {
59			currentOffset = 0;
60			++currentIndex;
61			ch = '\r';
62		}
63		return (ch);
64	}
65
66	return (-1);
67}
68
69
70/*************************** GLOBAL FUNCTIONS ********************************/
71
72
73/*
74 * .KB_C_FN_DEFINITION_START
75 * void WriteCommandTable(void)
76 *  This global function write the current command table to the non-volatile
77 * memory.
78 * .KB_C_FN_DEFINITION_END
79 */
80void
81WriteCommandTable(void)
82{
83	int	i, size = MAX_ENV_SIZE_BYTES, copySize;
84	char	*cPtr = env_table;
85
86	p_memset(env_table, 0, sizeof(env_table));
87
88	for (i = 0; i < MAX_BOOT_COMMANDS; ++i) {
89
90		copySize = p_strlen(boot_commands[i]);
91		size -= copySize + 1;
92
93		if (size < 0) {
94			continue;
95		}
96		memcpy(cPtr, boot_commands[i], copySize);
97		cPtr += copySize;
98		*cPtr++ = 0;
99	}
100
101	/* We're executing in low RAM so addr in ram == offset in eeprom */
102	WriteEEPROM((unsigned)&BootCommandSection, env_table,
103	    sizeof(env_table));
104}
105
106
107/*
108 * .KB_C_FN_DEFINITION_START
109 * void SetBootCommand(int index, char *command)
110 *  This global function replaces the specified index with the string residing
111 * at command.  Execute this function with a NULL string to clear the
112 * associated command index.
113 * .KB_C_FN_DEFINITION_END
114 */
115void
116SetBootCommand(int index, char *command)
117{
118	int 	i;
119
120	if ((unsigned)index < MAX_BOOT_COMMANDS) {
121
122		p_memset(boot_commands[index], 0, MAX_INPUT_SIZE);
123
124		if (!command)
125			return ;
126
127		for (i = 0; i < MAX_INPUT_SIZE; ++i) {
128			boot_commands[index][i] = command[i];
129			if (!(boot_commands[index][i]))
130				return;
131		}
132	}
133}
134
135
136/*
137 * .KB_C_FN_DEFINITION_START
138 * void DumpBootCommands(void)
139 *  This global function displays the current boot commands.
140 * .KB_C_FN_DEFINITION_END
141 */
142void
143DumpBootCommands(void)
144{
145	int	i, j;
146
147	for (i = 0; i < MAX_BOOT_COMMANDS; ++i) {
148		printf("0x%x : ", i);
149		for (j = 0; j < MAX_INPUT_SIZE; ++j) {
150			putchar(boot_commands[i][j]);
151			if (!(boot_commands[i][j]))
152				break;
153		}
154		printf("[E]\n\r");
155	}
156}
157
158
159/*
160 * .KB_C_FN_DEFINITION_START
161 * void LoadBootCommands(void)
162 *  This global function loads the existing boot commands from raw format and
163 * coverts it to the standard, command-index format.  Notice, the processed
164 * boot command table has much more space allocated than the actual table
165 * stored in non-volatile memory.  This is because the processed table
166 * exists in RAM which is larger than the non-volatile space.
167 * .KB_C_FN_DEFINITION_END
168 */
169void
170LoadBootCommands(void)
171{
172	int	index, j, size;
173	char	*cPtr;
174
175	p_memset((char*)boot_commands, 0, sizeof(boot_commands));
176
177	cPtr = &BootCommandSection;
178
179	size = MAX_ENV_SIZE_BYTES;
180
181	for (index = 0; (index < MAX_BOOT_COMMANDS) && size; ++index) {
182		for (j = 0; (j < MAX_INPUT_SIZE) && size; ++j) {
183			size--;
184			boot_commands[index][j] = *cPtr++;
185			if (!(boot_commands[index][j])) {
186				break;
187			}
188		}
189	}
190}
191
192
193/*
194 * .KB_C_FN_DEFINITION_START
195 * void ExecuteEnvironmentFunctions(void)
196 *  This global function executes applicable entries in the environment.
197 * .KB_C_FN_DEFINITION_END
198 */
199void
200ExecuteEnvironmentFunctions(void)
201{
202	currentIndex = 0;
203	currentOffset = 0;
204
205	DumpBootCommands();
206	Bootloader(ReadCharFromEnvironment);
207}
208