199767f8919635c4928607450d9e0abb932109ceToomas Soome/******************************************************************************
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Filename: env_vars.c
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Instantiation of environment variables, structures, and other globals.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Revision information:
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 20AUG2004 kb_admin initial creation
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * BEGIN_KBDD_BLOCK
199767f8919635c4928607450d9e0abb932109ceToomas Soome * No warranty, expressed or implied, is included with this software. It is
199767f8919635c4928607450d9e0abb932109ceToomas Soome * provided "AS IS" and no warranty of any kind including statutory or aspects
199767f8919635c4928607450d9e0abb932109ceToomas Soome * relating to merchantability or fitness for any purpose is provided. All
199767f8919635c4928607450d9e0abb932109ceToomas Soome * intellectual property rights of others is maintained with the respective
199767f8919635c4928607450d9e0abb932109ceToomas Soome * owners. This software is not copyrighted and is intended for reference
199767f8919635c4928607450d9e0abb932109ceToomas Soome * only.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * END_BLOCK
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * $FreeBSD$
199767f8919635c4928607450d9e0abb932109ceToomas Soome *****************************************************************************/
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "env_vars.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "loader_prompt.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "lib.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/******************************* GLOBALS *************************************/
199767f8919635c4928607450d9e0abb932109ceToomas Soomechar boot_commands[MAX_BOOT_COMMANDS][MAX_INPUT_SIZE];
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomechar env_table[MAX_ENV_SIZE_BYTES];
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeextern char BootCommandSection;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/************************** PRIVATE FUNCTIONS ********************************/
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int currentIndex;
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int currentOffset;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * .KB_C_FN_DEFINITION_START
199767f8919635c4928607450d9e0abb932109ceToomas Soome * int ReadCharFromEnvironment(char *)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * This private function reads characters from the enviroment variables
199767f8919635c4928607450d9e0abb932109ceToomas Soome * to service the command prompt during auto-boot or just to setup the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * default environment. Returns positive value if valid character was
199767f8919635c4928607450d9e0abb932109ceToomas Soome * set in the pointer. Returns negative value to signal input stream
199767f8919635c4928607450d9e0abb932109ceToomas Soome * terminated. Returns 0 to indicate _wait_ condition.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * .KB_C_FN_DEFINITION_END
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas SoomeReadCharFromEnvironment(int timeout)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int ch;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (currentIndex < MAX_BOOT_COMMANDS) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome ch = boot_commands[currentIndex][currentOffset++];
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ch == '\0' || (currentOffset >= MAX_INPUT_SIZE)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome currentOffset = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome ++currentIndex;
199767f8919635c4928607450d9e0abb932109ceToomas Soome ch = '\r';
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (ch);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*************************** GLOBAL FUNCTIONS ********************************/
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * .KB_C_FN_DEFINITION_START
199767f8919635c4928607450d9e0abb932109ceToomas Soome * void WriteCommandTable(void)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * This global function write the current command table to the non-volatile
199767f8919635c4928607450d9e0abb932109ceToomas Soome * memory.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * .KB_C_FN_DEFINITION_END
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoid
199767f8919635c4928607450d9e0abb932109ceToomas SoomeWriteCommandTable(void)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int i, size = MAX_ENV_SIZE_BYTES, copySize;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *cPtr = env_table;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome p_memset(env_table, 0, sizeof(env_table));
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < MAX_BOOT_COMMANDS; ++i) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome copySize = p_strlen(boot_commands[i]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome size -= copySize + 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (size < 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome continue;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome memcpy(cPtr, boot_commands[i], copySize);
199767f8919635c4928607450d9e0abb932109ceToomas Soome cPtr += copySize;
199767f8919635c4928607450d9e0abb932109ceToomas Soome *cPtr++ = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* We're executing in low RAM so addr in ram == offset in eeprom */
199767f8919635c4928607450d9e0abb932109ceToomas Soome WriteEEPROM((unsigned)&BootCommandSection, env_table,
199767f8919635c4928607450d9e0abb932109ceToomas Soome sizeof(env_table));
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * .KB_C_FN_DEFINITION_START
199767f8919635c4928607450d9e0abb932109ceToomas Soome * void SetBootCommand(int index, char *command)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * This global function replaces the specified index with the string residing
199767f8919635c4928607450d9e0abb932109ceToomas Soome * at command. Execute this function with a NULL string to clear the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * associated command index.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * .KB_C_FN_DEFINITION_END
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoid
199767f8919635c4928607450d9e0abb932109ceToomas SoomeSetBootCommand(int index, char *command)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int i;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((unsigned)index < MAX_BOOT_COMMANDS) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome p_memset(boot_commands[index], 0, MAX_INPUT_SIZE);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!command)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return ;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < MAX_INPUT_SIZE; ++i) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome boot_commands[index][i] = command[i];
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!(boot_commands[index][i]))
199767f8919635c4928607450d9e0abb932109ceToomas Soome return;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * .KB_C_FN_DEFINITION_START
199767f8919635c4928607450d9e0abb932109ceToomas Soome * void DumpBootCommands(void)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * This global function displays the current boot commands.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * .KB_C_FN_DEFINITION_END
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoid
199767f8919635c4928607450d9e0abb932109ceToomas SoomeDumpBootCommands(void)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int i, j;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < MAX_BOOT_COMMANDS; ++i) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("0x%x : ", i);
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (j = 0; j < MAX_INPUT_SIZE; ++j) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome putchar(boot_commands[i][j]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!(boot_commands[i][j]))
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("[E]\n\r");
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * .KB_C_FN_DEFINITION_START
199767f8919635c4928607450d9e0abb932109ceToomas Soome * void LoadBootCommands(void)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * This global function loads the existing boot commands from raw format and
199767f8919635c4928607450d9e0abb932109ceToomas Soome * coverts it to the standard, command-index format. Notice, the processed
199767f8919635c4928607450d9e0abb932109ceToomas Soome * boot command table has much more space allocated than the actual table
199767f8919635c4928607450d9e0abb932109ceToomas Soome * stored in non-volatile memory. This is because the processed table
199767f8919635c4928607450d9e0abb932109ceToomas Soome * exists in RAM which is larger than the non-volatile space.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * .KB_C_FN_DEFINITION_END
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoid
199767f8919635c4928607450d9e0abb932109ceToomas SoomeLoadBootCommands(void)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int index, j, size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *cPtr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome p_memset((char*)boot_commands, 0, sizeof(boot_commands));
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome cPtr = &BootCommandSection;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome size = MAX_ENV_SIZE_BYTES;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (index = 0; (index < MAX_BOOT_COMMANDS) && size; ++index) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (j = 0; (j < MAX_INPUT_SIZE) && size; ++j) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome size--;
199767f8919635c4928607450d9e0abb932109ceToomas Soome boot_commands[index][j] = *cPtr++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!(boot_commands[index][j])) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * .KB_C_FN_DEFINITION_START
199767f8919635c4928607450d9e0abb932109ceToomas Soome * void ExecuteEnvironmentFunctions(void)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * This global function executes applicable entries in the environment.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * .KB_C_FN_DEFINITION_END
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoid
199767f8919635c4928607450d9e0abb932109ceToomas SoomeExecuteEnvironmentFunctions(void)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome currentIndex = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome currentOffset = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome DumpBootCommands();
199767f8919635c4928607450d9e0abb932109ceToomas Soome Bootloader(ReadCharFromEnvironment);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}