summaryrefslogtreecommitdiffstats
path: root/sys/boot/arm/at91/bootiic/env_vars.c
blob: 78afae9c1df9daf80e721dc3811dea5b5808c023 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/******************************************************************************
 *
 * Filename: env_vars.c
 *
 * Instantiation of environment variables, structures, and other globals.
 *
 * Revision information:
 *
 * 20AUG2004	kb_admin	initial creation
 *
 * BEGIN_KBDD_BLOCK
 * No warranty, expressed or implied, is included with this software.  It is
 * provided "AS IS" and no warranty of any kind including statutory or aspects
 * relating to merchantability or fitness for any purpose is provided.  All
 * intellectual property rights of others is maintained with the respective
 * owners.  This software is not copyrighted and is intended for reference
 * only.
 * END_BLOCK
 *
 * $FreeBSD$
 *****************************************************************************/

#include "env_vars.h"
#include "loader_prompt.h"
#include "lib.h"

/******************************* GLOBALS *************************************/
char	boot_commands[MAX_BOOT_COMMANDS][MAX_INPUT_SIZE];

char	env_table[MAX_ENV_SIZE_BYTES];

extern char	BootCommandSection;

/************************** PRIVATE FUNCTIONS ********************************/


static int	currentIndex;
static int	currentOffset;


/*
 * .KB_C_FN_DEFINITION_START
 * int ReadCharFromEnvironment(char *)
 *  This private function reads characters from the enviroment variables
 * to service the command prompt during auto-boot or just to setup the
 * default environment.  Returns positive value if valid character was
 * set in the pointer.  Returns negative value to signal input stream
 * terminated.  Returns 0 to indicate _wait_ condition.
 * .KB_C_FN_DEFINITION_END
 */
static int
ReadCharFromEnvironment(int timeout)
{
	int ch;

	if (currentIndex < MAX_BOOT_COMMANDS) {
		ch = boot_commands[currentIndex][currentOffset++];
		if (ch == '\0' || (currentOffset >= MAX_INPUT_SIZE)) {
			currentOffset = 0;
			++currentIndex;
			ch = '\r';
		}
		return (ch);
	}

	return (-1);
}


/*************************** GLOBAL FUNCTIONS ********************************/


/*
 * .KB_C_FN_DEFINITION_START
 * void WriteCommandTable(void)
 *  This global function write the current command table to the non-volatile
 * memory.
 * .KB_C_FN_DEFINITION_END
 */
void
WriteCommandTable(void)
{
	int	i, size = MAX_ENV_SIZE_BYTES, copySize;
	char	*cPtr = env_table;

	p_memset(env_table, 0, sizeof(env_table));

	for (i = 0; i < MAX_BOOT_COMMANDS; ++i) {

		copySize = p_strlen(boot_commands[i]);
		size -= copySize + 1;

		if (size < 0) {
			continue;
		}
		p_memcpy(cPtr, boot_commands[i], copySize);
		cPtr += copySize;
		*cPtr++ = 0;
	}

	/* We're executing in low RAM so addr in ram == offset in eeprom */
	WriteEEPROM((unsigned)&BootCommandSection, env_table,
	    sizeof(env_table));
}


/*
 * .KB_C_FN_DEFINITION_START
 * void SetBootCommand(int index, char *command)
 *  This global function replaces the specified index with the string residing
 * at command.  Execute this function with a NULL string to clear the
 * associated command index.
 * .KB_C_FN_DEFINITION_END
 */
void
SetBootCommand(int index, char *command)
{
	int 	i;

	if ((unsigned)index < MAX_BOOT_COMMANDS) {

		p_memset(boot_commands[index], 0, MAX_INPUT_SIZE);

		if (!command)
			return ;

		for (i = 0; i < MAX_INPUT_SIZE; ++i) {
			boot_commands[index][i] = command[i];
			if (!(boot_commands[index][i]))
				return;
		}
	}
}


/*
 * .KB_C_FN_DEFINITION_START
 * void DumpBootCommands(void)
 *  This global function displays the current boot commands.
 * .KB_C_FN_DEFINITION_END
 */
void
DumpBootCommands(void)
{
	int	i, j;

	for (i = 0; i < MAX_BOOT_COMMANDS; ++i) {
		printf("0x%x : ", i);
		for (j = 0; j < MAX_INPUT_SIZE; ++j) {
			putchar(boot_commands[i][j]);
			if (!(boot_commands[i][j]))
				break;
		}
		printf("[E]\n\r");
	}
}


/*
 * .KB_C_FN_DEFINITION_START
 * void LoadBootCommands(void)
 *  This global function loads the existing boot commands from raw format and
 * coverts it to the standard, command-index format.  Notice, the processed
 * boot command table has much more space allocated than the actual table
 * stored in non-volatile memory.  This is because the processed table
 * exists in RAM which is larger than the non-volatile space.
 * .KB_C_FN_DEFINITION_END
 */
void
LoadBootCommands(void)
{
	int	index, j, size;
	char	*cPtr;

	p_memset((char*)boot_commands, 0, sizeof(boot_commands));

	cPtr = &BootCommandSection;

	size = MAX_ENV_SIZE_BYTES;

	for (index = 0; (index < MAX_BOOT_COMMANDS) && size; ++index) {
		for (j = 0; (j < MAX_INPUT_SIZE) && size; ++j) {
			size--;
			boot_commands[index][j] = *cPtr++;
			if (!(boot_commands[index][j])) {
				break;
			}
		}
	}
}


/*
 * .KB_C_FN_DEFINITION_START
 * void ExecuteEnvironmentFunctions(void)
 *  This global function executes applicable entries in the environment.
 * .KB_C_FN_DEFINITION_END
 */
void
ExecuteEnvironmentFunctions(void)
{
	currentIndex = 0;
	currentOffset = 0;

	DumpBootCommands();
	Bootloader(ReadCharFromEnvironment);
}
OpenPOWER on IntegriCloud