Added a test for MUIA_Listview_SelectChange.
[AROS.git] / arch / ppc-chrp / boot / openfirmware / src / menu.c
blob62c3dee86e99b690febc1e8a72c336ac4efbe2aa
1 /*
2 * $Id$
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of
7 * the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
17 * MA 02111-1307 USA
20 #define DEBUG 0
22 #include <inttypes.h>
23 #include <menu.h>
24 #include <support.h>
25 #include <debug.h>
26 #include <of1275.h>
28 static char *menu_file;
29 static int menu_file_size;
30 static char *menu_lines[1024];
31 static int menu_lines_cnt;
33 static menu_entry_t *menu_entries[9];
34 static int menu_entries_cnt;
36 static int default_option = 0;
37 static int timeout_option = 10;
39 extern char *bootpath;
41 menu_entry_t *execute_menu()
43 menu_entry_t *menu = NULL;
45 if (menu_entries_cnt == 1)
47 menu = menu_entries[0];
49 else if (menu_entries_cnt > 1)
51 int i;
52 int selected = default_option;
53 char chr;
55 printf("\r\nAvailable boot configurations:\r\n");
57 for (i=0; i < menu_entries_cnt; i++)
59 printf("%c. %s\r\n", (i+1) < 10 ? i+'1':i+'A', menu_entries[i]->m_title);
62 printf("Boot? >");
63 while (ofw_read(stdin, &chr, 1) < 0) {
64 ofw_interpret("10 ms");
67 if (chr >= '1' && chr <= '9')
68 chr-='0';
69 else if (chr >= 'A' && chr <= 'Z')
70 chr = chr - 'A' + 10;
72 printf("\r\n");
74 if (1)
76 if (chr >= 1 && chr <= menu_entries_cnt) {
77 D(bug("[BOOT] correct key\r\n"));
78 selected = chr-1;
80 else return NULL;
83 menu = menu_entries[selected];
84 D(bug("[BOOT] Selected configuration: \"%s\"\r\n", menu->m_title));
87 return menu;
90 int load_menu(uint8_t *load_base)
92 int err = 0;
93 uint32_t *ptr;
94 int i;
95 char tmp[1024];
97 D(bug("[BOOT] Trying to load menu.lst file\r\n"));
99 /* Clear memory before loading a text file */
100 ptr = (uint32_t *)load_base;
101 for (i=0; i < LOAD_SIZE / 4; i++)
102 ptr[i] = 0;
104 sprintf(tmp, "load %s boot/menu.lst", bootpath);
105 if (ofw_interpret(tmp))
107 sprintf(tmp, "load %s menu.lst", bootpath);
108 if (ofw_interpret(tmp))
109 printf("Failed to load menu file\r\n");
112 menu_file_size = 0;
114 while(load_base[menu_file_size])
115 menu_file_size++;
117 D(bug("[BOOT] Size of menu file: %d bytes\r\n", menu_file_size));
119 /* 64 kilobytes of memory should be enough for simple menu ;) */
120 menu_file = ofw_claim(NULL, menu_file_size, 4);
122 if (menu_file == (char*)-1)
124 err = -1;
126 else
128 memcpy(menu_file, load_base, menu_file_size);
131 return err;
134 void free_menu()
136 int i;
137 ofw_release(menu_file, menu_file_size);
139 for (i=0; i < menu_entries_cnt; i++)
140 ofw_release(menu_entries[i], sizeof(menu_entry_t));
143 char *eat_whitespace(char *c)
145 while (*c && isblank(*c))
146 c++;
148 return c;
151 char *next_word(char *c)
153 while (*c && !isblank(*c))
154 c++;
155 while (*c && isblank(*c))
156 c++;
158 return c;
161 void parse_menu(unsigned long def_virt)
163 int i;
164 menu_lines_cnt = 0;
165 int new_line = 1;
166 menu_entry_t *menu = NULL;
168 for (i=0; i < menu_file_size; i++)
170 if (new_line) {
171 if (menu_file[i]) {
172 menu_lines[menu_lines_cnt++] = &menu_file[i];
173 new_line = 0;
175 else continue;
178 if (menu_file[i] == '\r') {
179 menu_file[i] = 0;
181 else if (menu_file[i] == '\n') {
182 menu_file[i] = 0;
183 new_line = 1;
187 for (i=0; i < menu_lines_cnt; i++)
189 char *str = eat_whitespace(menu_lines[i]);
191 D(bug("[BOOT] Line: "));
192 D(bug(str));
193 D(bug("\r\n"));
195 if (*str == '#')
197 /* Skip comments */
198 continue;
200 else if (!strncasecmp(str, "timeout", 7) && isspace(str[7]))
202 str = next_word(str);
203 timeout_option = atoi(str);
205 D(bug("[BOOT] Found new timeout = %d\r\n", timeout_option));
208 else if (!strncasecmp(str, "default", 7) && isspace(str[7]))
210 str = next_word(str);
211 default_option = atoi(str);
213 D(bug("[BOOT] Found new default = %d\r\n", default_option));
215 else if (!strncasecmp(str, "title", 5) && isspace(str[5]))
217 if (menu_entries_cnt < MAX_MENU_ENTRIES)
219 int j;
221 menu = menu_entries[menu_entries_cnt++] = ofw_claim(NULL, sizeof(menu_entry_t), 4);
223 menu->m_title = next_word(str);
224 menu->m_kernel = NULL;
225 menu->m_modules_cnt = 0;
226 menu->m_virtual = def_virt;
228 j = strlen(menu->m_title);
229 while (isspace(menu->m_title[--j]))
230 menu->m_title[j] = 0;
232 D(bug("[BOOT] Found new title '%s'\r\n", menu->m_title));
234 else return;
236 else if (!strncasecmp(str, "virtual_addr", 12) && isspace(str[12]))
238 if (menu)
240 char *c = next_word(str);
242 menu->m_virtual = atoi(c);
245 else if (!strncasecmp(str, "kernel", 6) && isspace(str[6]))
247 D(bug("[BOOT] Found new kernel\r\n"));
249 if (menu)
251 char *c = next_word(str);
252 menu->m_kernel = c;
253 menu->m_cmdline = NULL;
254 while(*c && !isblank(*c))
255 c++;
257 while (*c) {
258 if (isblank(*c)) {
259 *c++ = 0;
261 else {
262 menu->m_cmdline = c;
263 break;
266 if (menu->m_cmdline && strlen(menu->m_cmdline))
268 char *c = menu->m_cmdline + strlen(menu->m_cmdline);
269 while (--c > menu->m_cmdline)
271 if (isspace(*c))
272 *c = 0;
273 else
274 break;
280 else if (!strncasecmp(str, "module", 6) && isspace(str[6]))
282 D(bug("[BOOT] Found new module\r\n"));
283 if (menu && menu->m_modules_cnt < MAX_MODULES)
285 menu->m_modules[menu->m_modules_cnt++] = next_word(str);