summaryrefslogtreecommitdiff
path: root/src/boot/filo.c
blob: f5c5bda420308d37c8e4022017ad4fabe369c97a (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
/*
 * Copyright (C) 2003 by SONE Takeshi <ts1@tsn.or.jp> and others.
 * This program is licensed under the terms of GNU General Public License.
 *
 * Modified for LinuxBIOS by Greg Watson <gwatson@lanl.gov>
 */

#include <console/console.h>
#include <delay.h>
#include <string.h>
#include <boot/tables.h>
#include <boot/elf.h>

#define ENTER '\r'
#define ESCAPE '\x1b'

#ifndef AUTOBOOT_CMDLINE
#define autoboot(mem)
#endif

#ifndef AUTOBOOT_DELAY
#define autoboot_delay() 0 /* success */
#endif

#define havechar() console_tst_byte()
#define putchar(c) console_tx_byte(c)
#define getchar(c) console_rx_byte(c)

extern char *boot_file;

int getline(char *buf, int max)
{
    int cur, ch, nonspace_seen;

    cur = 0;
    while (buf[cur]) {
	putchar(buf[cur]);
	cur++;
    }
    for (;;) {
	ch = getchar();
	switch (ch) {
	/* end of line */
	case '\r':
	case '\n':
	    putchar('\n');
	    goto out;
	/* backspace */
	case '\b':
	case '\x7f':
	    if (cur > 0) {
		cur--;
		putchar('\b');
		putchar(' ');
		putchar('\b');
	    }
	    break;
	/* word erase */
	case 'W' & 0x1f: /* ^W */
	    nonspace_seen = 0;
	    while (cur) {
		if (buf[cur-1] != ' ')
		    nonspace_seen = 1;
		putchar('\b');
		putchar(' ');
		putchar('\b');
		cur--;
		if (nonspace_seen && cur < max-1 && cur > 0 && buf[cur-1]==' ')
		    break;
	    }
	    break;
	/* line erase */
	case 'U' & 0x1f: /* ^U */
	    while (cur) {
		putchar('\b');
		putchar(' ');
		putchar('\b');
		cur--;
	    }
	    cur = 0;
	    break;
	default:
	    if (ch < 0x20)
		break; /* ignore control char */
	    if (ch >= 0x7f)
		break;
	    if (cur + 1 < max) {
		putchar(ch); /* echo back */
		buf[cur] = ch;
		cur++;
	    }
	}
    }
out:
    if (cur >= max)
	cur = max - 1;
    buf[cur] = '\0';
    return cur;
}

static void boot(struct lb_memory *mem, const char *line)
{
    char *param;

    /* Split filename and parameter */
    boot_file = strdup(line);
    param = strchr(boot_file, ' ');
    if (param) {
	*param = '\0';
	param++;
    }

    if (!elfboot(mem))
	printk_info("Unsupported image format\n");
    free(boot_file);
}

#ifdef AUTOBOOT_CMDLINE
#if AUTOBOOT_DELAY
static inline int autoboot_delay(void)
{
    unsigned int timeout;
    int sec, tmp;
    char key;
    
    key = 0;

    printk_info("Press <Enter> for default boot, or <Esc> for boot prompt... ");
    for (sec = AUTOBOOT_DELAY; sec>0 && key==0; sec--) {
	printk_info("%d", sec);
	timeout = 10;
	while (timeout-- > 0) {
	    if (havechar()) {
		key = getchar();
		if (key==ENTER || key==ESCAPE)
		    break;
	    }
	    mdelay(100);
	}
	for (tmp = sec; tmp; tmp /= 10)
	    printk_info("\b \b");
    }
    if (key == 0) {
	printk_info("timed out\n");
	return 0; /* success */
    } else {
	putchar('\n');
	if (key == ESCAPE)
	    return -1; /* canceled */
	else
	    return 0; /* default accepted */
    }
}
#endif /* AUTOBOOT_DELAY */

static void autoboot(struct lb_memory *mem)
{
    /* If Escape key is pressed already, skip autoboot */
    if (havechar() && getchar()==ESCAPE)
	return;

    if (autoboot_delay()==0) {
	printk_info("boot: %s\n", AUTOBOOT_CMDLINE);
	boot(mem, AUTOBOOT_CMDLINE);
    }
}
#endif /* AUTOBOOT_CMDLINE */

/* The main routine */
int filo(struct lb_memory *mem)
{
    char line[256];

    printk_info("FILO version 0.4.1\n");

    /* Try default image */
    autoboot(mem);

    /* The above didn't work, ask user */
    while (havechar())
	getchar();
#ifdef AUTOBOOT_CMDLINE
    strncpy(line, AUTOBOOT_CMDLINE, sizeof(line)-1);
    line[sizeof(line)-1] = '\0';
#else
    line[0] = '\0';
#endif
    for (;;) {
	printk_info("boot: ");
	getline(line, sizeof line);
	if (line[0])
	    boot(mem, line);
    }
}