1161202Simp/******************************************************************************
2161202Simp *
3161202Simp * Filename: env_vars.c
4161202Simp *
5161202Simp * Instantiation of environment variables, structures, and other globals.
6161202Simp *
7161202Simp * Revision information:
8161202Simp *
9161202Simp * 20AUG2004	kb_admin	initial creation
10161202Simp *
11161202Simp * BEGIN_KBDD_BLOCK
12161202Simp * No warranty, expressed or implied, is included with this software.  It is
13161202Simp * provided "AS IS" and no warranty of any kind including statutory or aspects
14161202Simp * relating to merchantability or fitness for any purpose is provided.  All
15161202Simp * intellectual property rights of others is maintained with the respective
16161202Simp * owners.  This software is not copyrighted and is intended for reference
17161202Simp * only.
18161202Simp * END_BLOCK
19161202Simp *
20161202Simp * $FreeBSD: releng/10.3/sys/boot/arm/at91/bootiic/env_vars.c 163596 2006-10-21 22:43:39Z imp $
21161202Simp *****************************************************************************/
22161202Simp
23161202Simp#include "env_vars.h"
24161202Simp#include "loader_prompt.h"
25161202Simp#include "lib.h"
26161202Simp
27161202Simp/******************************* GLOBALS *************************************/
28161202Simpchar	boot_commands[MAX_BOOT_COMMANDS][MAX_INPUT_SIZE];
29161202Simp
30161202Simpchar	env_table[MAX_ENV_SIZE_BYTES];
31161202Simp
32161202Simpextern char	BootCommandSection;
33161202Simp
34161202Simp/************************** PRIVATE FUNCTIONS ********************************/
35161202Simp
36161202Simp
37161202Simpstatic int	currentIndex;
38161202Simpstatic int	currentOffset;
39161202Simp
40161202Simp
41161202Simp/*
42161202Simp * .KB_C_FN_DEFINITION_START
43161202Simp * int ReadCharFromEnvironment(char *)
44161202Simp *  This private function reads characters from the enviroment variables
45161202Simp * to service the command prompt during auto-boot or just to setup the
46161202Simp * default environment.  Returns positive value if valid character was
47161202Simp * set in the pointer.  Returns negative value to signal input stream
48161202Simp * terminated.  Returns 0 to indicate _wait_ condition.
49161202Simp * .KB_C_FN_DEFINITION_END
50161202Simp */
51161202Simpstatic int
52161202SimpReadCharFromEnvironment(int timeout)
53161202Simp{
54161202Simp	int ch;
55161202Simp
56161202Simp	if (currentIndex < MAX_BOOT_COMMANDS) {
57161202Simp		ch = boot_commands[currentIndex][currentOffset++];
58161202Simp		if (ch == '\0' || (currentOffset >= MAX_INPUT_SIZE)) {
59161202Simp			currentOffset = 0;
60161202Simp			++currentIndex;
61161202Simp			ch = '\r';
62161202Simp		}
63161202Simp		return (ch);
64161202Simp	}
65161202Simp
66161202Simp	return (-1);
67161202Simp}
68161202Simp
69161202Simp
70161202Simp/*************************** GLOBAL FUNCTIONS ********************************/
71161202Simp
72161202Simp
73161202Simp/*
74161202Simp * .KB_C_FN_DEFINITION_START
75161202Simp * void WriteCommandTable(void)
76161202Simp *  This global function write the current command table to the non-volatile
77161202Simp * memory.
78161202Simp * .KB_C_FN_DEFINITION_END
79161202Simp */
80161202Simpvoid
81161202SimpWriteCommandTable(void)
82161202Simp{
83161202Simp	int	i, size = MAX_ENV_SIZE_BYTES, copySize;
84161202Simp	char	*cPtr = env_table;
85161202Simp
86161202Simp	p_memset(env_table, 0, sizeof(env_table));
87161202Simp
88161202Simp	for (i = 0; i < MAX_BOOT_COMMANDS; ++i) {
89161202Simp
90161202Simp		copySize = p_strlen(boot_commands[i]);
91161202Simp		size -= copySize + 1;
92161202Simp
93161202Simp		if (size < 0) {
94161202Simp			continue;
95161202Simp		}
96163596Simp		memcpy(cPtr, boot_commands[i], copySize);
97161202Simp		cPtr += copySize;
98161202Simp		*cPtr++ = 0;
99161202Simp	}
100161202Simp
101161202Simp	/* We're executing in low RAM so addr in ram == offset in eeprom */
102161202Simp	WriteEEPROM((unsigned)&BootCommandSection, env_table,
103161202Simp	    sizeof(env_table));
104161202Simp}
105161202Simp
106161202Simp
107161202Simp/*
108161202Simp * .KB_C_FN_DEFINITION_START
109161202Simp * void SetBootCommand(int index, char *command)
110161202Simp *  This global function replaces the specified index with the string residing
111161202Simp * at command.  Execute this function with a NULL string to clear the
112161202Simp * associated command index.
113161202Simp * .KB_C_FN_DEFINITION_END
114161202Simp */
115161202Simpvoid
116161202SimpSetBootCommand(int index, char *command)
117161202Simp{
118161202Simp	int 	i;
119161202Simp
120161202Simp	if ((unsigned)index < MAX_BOOT_COMMANDS) {
121161202Simp
122161202Simp		p_memset(boot_commands[index], 0, MAX_INPUT_SIZE);
123161202Simp
124161202Simp		if (!command)
125161202Simp			return ;
126161202Simp
127161202Simp		for (i = 0; i < MAX_INPUT_SIZE; ++i) {
128161202Simp			boot_commands[index][i] = command[i];
129161202Simp			if (!(boot_commands[index][i]))
130161202Simp				return;
131161202Simp		}
132161202Simp	}
133161202Simp}
134161202Simp
135161202Simp
136161202Simp/*
137161202Simp * .KB_C_FN_DEFINITION_START
138161202Simp * void DumpBootCommands(void)
139161202Simp *  This global function displays the current boot commands.
140161202Simp * .KB_C_FN_DEFINITION_END
141161202Simp */
142161202Simpvoid
143161202SimpDumpBootCommands(void)
144161202Simp{
145161202Simp	int	i, j;
146161202Simp
147161202Simp	for (i = 0; i < MAX_BOOT_COMMANDS; ++i) {
148161202Simp		printf("0x%x : ", i);
149161202Simp		for (j = 0; j < MAX_INPUT_SIZE; ++j) {
150161202Simp			putchar(boot_commands[i][j]);
151161202Simp			if (!(boot_commands[i][j]))
152161202Simp				break;
153161202Simp		}
154161202Simp		printf("[E]\n\r");
155161202Simp	}
156161202Simp}
157161202Simp
158161202Simp
159161202Simp/*
160161202Simp * .KB_C_FN_DEFINITION_START
161161202Simp * void LoadBootCommands(void)
162161202Simp *  This global function loads the existing boot commands from raw format and
163161202Simp * coverts it to the standard, command-index format.  Notice, the processed
164161202Simp * boot command table has much more space allocated than the actual table
165161202Simp * stored in non-volatile memory.  This is because the processed table
166161202Simp * exists in RAM which is larger than the non-volatile space.
167161202Simp * .KB_C_FN_DEFINITION_END
168161202Simp */
169161202Simpvoid
170161202SimpLoadBootCommands(void)
171161202Simp{
172161202Simp	int	index, j, size;
173161202Simp	char	*cPtr;
174161202Simp
175161202Simp	p_memset((char*)boot_commands, 0, sizeof(boot_commands));
176161202Simp
177161202Simp	cPtr = &BootCommandSection;
178161202Simp
179161202Simp	size = MAX_ENV_SIZE_BYTES;
180161202Simp
181161202Simp	for (index = 0; (index < MAX_BOOT_COMMANDS) && size; ++index) {
182161202Simp		for (j = 0; (j < MAX_INPUT_SIZE) && size; ++j) {
183161202Simp			size--;
184161202Simp			boot_commands[index][j] = *cPtr++;
185161202Simp			if (!(boot_commands[index][j])) {
186161202Simp				break;
187161202Simp			}
188161202Simp		}
189161202Simp	}
190161202Simp}
191161202Simp
192161202Simp
193161202Simp/*
194161202Simp * .KB_C_FN_DEFINITION_START
195161202Simp * void ExecuteEnvironmentFunctions(void)
196161202Simp *  This global function executes applicable entries in the environment.
197161202Simp * .KB_C_FN_DEFINITION_END
198161202Simp */
199161202Simpvoid
200161202SimpExecuteEnvironmentFunctions(void)
201161202Simp{
202161202Simp	currentIndex = 0;
203161202Simp	currentOffset = 0;
204161202Simp
205161202Simp	DumpBootCommands();
206161202Simp	Bootloader(ReadCharFromEnvironment);
207161202Simp}
208