1161370Simp/******************************************************************************
2161370Simp *
3161370Simp * Filename: env_vars.c
4161370Simp *
5161370Simp * Instantiation of environment variables, structures, and other globals.
6161370Simp *
7161370Simp * Revision information:
8161370Simp *
9161370Simp * 20AUG2004	kb_admin	initial creation
10161370Simp *
11161370Simp * BEGIN_KBDD_BLOCK
12161370Simp * No warranty, expressed or implied, is included with this software.  It is
13161370Simp * provided "AS IS" and no warranty of any kind including statutory or aspects
14161370Simp * relating to merchantability or fitness for any purpose is provided.  All
15161370Simp * intellectual property rights of others is maintained with the respective
16161370Simp * owners.  This software is not copyrighted and is intended for reference
17161370Simp * only.
18161370Simp * END_BLOCK
19161370Simp *
20161370Simp * $FreeBSD: releng/10.2/sys/boot/arm/at91/bootspi/env_vars.c 163597 2006-10-21 22:44:26Z imp $
21161370Simp *****************************************************************************/
22161370Simp
23161370Simp#include "env_vars.h"
24161370Simp#include "loader_prompt.h"
25161370Simp#include "lib.h"
26161370Simp
27161370Simp/******************************* GLOBALS *************************************/
28161370Simpchar	boot_commands[MAX_BOOT_COMMANDS][MAX_INPUT_SIZE];
29161370Simp
30161370Simpchar	env_table[MAX_ENV_SIZE_BYTES];
31161370Simp
32161370Simpextern char	BootCommandSection;
33161370Simp
34161370Simp/************************** PRIVATE FUNCTIONS ********************************/
35161370Simp
36161370Simp
37161370Simpstatic int	currentIndex;
38161370Simpstatic int	currentOffset;
39161370Simp
40161370Simp
41161370Simp/*
42161370Simp * .KB_C_FN_DEFINITION_START
43161370Simp * int ReadCharFromEnvironment(char *)
44161370Simp *  This private function reads characters from the enviroment variables
45161370Simp * to service the command prompt during auto-boot or just to setup the
46161370Simp * default environment.  Returns positive value if valid character was
47161370Simp * set in the pointer.  Returns negative value to signal input stream
48161370Simp * terminated.  Returns 0 to indicate _wait_ condition.
49161370Simp * .KB_C_FN_DEFINITION_END
50161370Simp */
51161370Simpstatic int
52161370SimpReadCharFromEnvironment(int timeout)
53161370Simp{
54161370Simp	int ch;
55161370Simp
56161370Simp	if (currentIndex < MAX_BOOT_COMMANDS) {
57161370Simp		ch = boot_commands[currentIndex][currentOffset++];
58161370Simp		if (ch == '\0' || (currentOffset >= MAX_INPUT_SIZE)) {
59161370Simp			currentOffset = 0;
60161370Simp			++currentIndex;
61161370Simp			ch = '\r';
62161370Simp		}
63161370Simp		return (ch);
64161370Simp	}
65161370Simp
66161370Simp	return (-1);
67161370Simp}
68161370Simp
69161370Simp
70161370Simp/*************************** GLOBAL FUNCTIONS ********************************/
71161370Simp
72161370Simp
73161370Simp/*
74161370Simp * .KB_C_FN_DEFINITION_START
75161370Simp * void DumpBootCommands(void)
76161370Simp *  This global function displays the current boot commands.
77161370Simp * .KB_C_FN_DEFINITION_END
78161370Simp */
79161370Simpvoid
80161370SimpDumpBootCommands(void)
81161370Simp{
82161370Simp	int	i;
83161370Simp
84161370Simp	for (i = 0; boot_commands[i][0]; i++)
85163597Simp		printf("0x%x : %s[E]\n", i, boot_commands[i]);
86161370Simp}
87161370Simp
88161370Simp
89161370Simp/*
90161370Simp * .KB_C_FN_DEFINITION_START
91161370Simp * void LoadBootCommands(void)
92161370Simp *  This global function loads the existing boot commands from raw format and
93161370Simp * coverts it to the standard, command-index format.  Notice, the processed
94161370Simp * boot command table has much more space allocated than the actual table
95161370Simp * stored in non-volatile memory.  This is because the processed table
96161370Simp * exists in RAM which is larger than the non-volatile space.
97161370Simp * .KB_C_FN_DEFINITION_END
98161370Simp */
99161370Simpvoid
100161370SimpLoadBootCommands(void)
101161370Simp{
102161370Simp	int	index, j;
103161370Simp	char	*cptr;
104161370Simp
105161370Simp	p_memset((char*)boot_commands, 0, sizeof(boot_commands));
106161370Simp	cptr = &BootCommandSection;
107161370Simp	for (index = 0; *cptr; index++) {
108161370Simp		for (j = 0; *cptr; j++)
109161370Simp			boot_commands[index][j] = *cptr++;
110161370Simp		cptr++;
111161370Simp	}
112161370Simp}
113161370Simp
114161370Simp
115161370Simp/*
116161370Simp * .KB_C_FN_DEFINITION_START
117161370Simp * void ExecuteEnvironmentFunctions(void)
118161370Simp *  This global function executes applicable entries in the environment.
119161370Simp * .KB_C_FN_DEFINITION_END
120161370Simp */
121161370Simpvoid
122161370SimpExecuteEnvironmentFunctions(void)
123161370Simp{
124161370Simp	currentIndex = 0;
125161370Simp	currentOffset = 0;
126161370Simp
127161370Simp	DumpBootCommands();
128163597Simp	printf("Autoboot...\n");
129161370Simp	Bootloader(ReadCharFromEnvironment);
130161370Simp}
131