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 DumpBootCommands(void)
76 *  This global function displays the current boot commands.
77 * .KB_C_FN_DEFINITION_END
78 */
79void
80DumpBootCommands(void)
81{
82	int	i;
83
84	for (i = 0; boot_commands[i][0]; i++)
85		printf("0x%x : %s[E]\n", i, boot_commands[i]);
86}
87
88
89/*
90 * .KB_C_FN_DEFINITION_START
91 * void LoadBootCommands(void)
92 *  This global function loads the existing boot commands from raw format and
93 * coverts it to the standard, command-index format.  Notice, the processed
94 * boot command table has much more space allocated than the actual table
95 * stored in non-volatile memory.  This is because the processed table
96 * exists in RAM which is larger than the non-volatile space.
97 * .KB_C_FN_DEFINITION_END
98 */
99void
100LoadBootCommands(void)
101{
102	int	index, j;
103	char	*cptr;
104
105	p_memset((char*)boot_commands, 0, sizeof(boot_commands));
106	cptr = &BootCommandSection;
107	for (index = 0; *cptr; index++) {
108		for (j = 0; *cptr; j++)
109			boot_commands[index][j] = *cptr++;
110		cptr++;
111	}
112}
113
114
115/*
116 * .KB_C_FN_DEFINITION_START
117 * void ExecuteEnvironmentFunctions(void)
118 *  This global function executes applicable entries in the environment.
119 * .KB_C_FN_DEFINITION_END
120 */
121void
122ExecuteEnvironmentFunctions(void)
123{
124	currentIndex = 0;
125	currentOffset = 0;
126
127	DumpBootCommands();
128	printf("Autoboot...\n");
129	Bootloader(ReadCharFromEnvironment);
130}
131