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
208
209
210
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <sys/promif.h>
#include <sys/salib.h>
#define MAX_CMDLINE 1600 /* from GRUB source */
char **titles;
char **datasets;
int menu_entry_count;
int menu_table_size;
int in_menu_entry;
#define ENTRY_ALLOC_COUNT 10
extern void set_default_fs(char *fsw_name);
extern int mountroot(char *str);
void
init_table(void)
{
menu_entry_count = 0;
titles = (char **)calloc(ENTRY_ALLOC_COUNT, sizeof (char *));
datasets = (char **)calloc(ENTRY_ALLOC_COUNT, sizeof (char *));
if (titles == NULL || datasets == NULL)
prom_panic("out of mem");
menu_table_size = ENTRY_ALLOC_COUNT;
in_menu_entry = 0;
}
void
add_title_entry(char *title_str)
{
/* skip leading white space */
while (isspace(*title_str))
title_str++;
if (menu_entry_count == menu_table_size) {
printf("Reallocating at count %d\n", menu_table_size);
titles = (char **)realloc(titles,
ENTRY_ALLOC_COUNT * sizeof (char *));
datasets = (char **)realloc(datasets,
ENTRY_ALLOC_COUNT * sizeof (char *));
if (titles == NULL || datasets == NULL)
prom_panic("out of mem");
menu_table_size += ENTRY_ALLOC_COUNT;
}
if (in_menu_entry)
free(titles[menu_entry_count]);
if ((titles[menu_entry_count] = strdup(title_str)) == NULL)
prom_panic("out of mem");
in_menu_entry = 1;
}
void
add_dataset_entry(char *dataset_str)
{
char *cp;
/* skip leading white space */
while (isspace(*dataset_str))
dataset_str++;
/* if there is still any white space in the line, it's invalid */
for (cp = dataset_str; *cp; cp++)
if (isspace(*cp))
break;
if (*cp)
return; /* dataset name was invalid */
if (!in_menu_entry)
return; /* dataset line was not preceded by a title */
if ((datasets[menu_entry_count] = strdup(dataset_str)) == NULL)
prom_panic("out of mem");
menu_entry_count++;
in_menu_entry = 0;
}
char *
trim_white_space(char *cp)
{
char *ep;
/* skip leading white space */
while (isspace(*cp))
cp++;
/*
* if the string contained nothing but white space, return a
* null string.
*/
if (*cp == '\0')
return (cp);
/* truncate trailing white space */
for (ep = cp + strlen(cp) - 1; isspace(*ep); ep--)
;
ep++;
*ep = '\0';
return (cp);
}
char *cons_gets(char *, int);
void
main(void *cif)
{
char linebuf[MAX_CMDLINE];
FILE *file;
char *cp, *ep;
int n;
unsigned long choice;
prom_init("bootlst", cif);
set_default_fs("promfs");
if (mountroot("bootfs") != 0)
prom_panic("can't mount root");
if ((file = fopen("/boot/menu.lst", "r")) == NULL)
prom_panic("can't open menu.lst");
init_table();
while (fgets(linebuf, MAX_CMDLINE, file)) {
cp = trim_white_space(linebuf);
/* skip comments and blank lines */
if (*cp == '#' || *cp == '\0')
continue;
/* find end of first keyword on line */
for (ep = cp; !isspace(*ep) && *ep; ep++)
;
/* if at the end of the line, the line had no arguments */
if (*ep == '\0')
continue;
*ep = '\0';
if (strcmp(cp, "title") == 0) {
add_title_entry(ep + 1);
continue;
}
if (strcmp(cp, "bootfs") == 0) {
add_dataset_entry(ep + 1);
continue;
}
}
if (menu_entry_count == 0)
prom_panic("no menu entries found");
for (n = 0; n < menu_entry_count; n++) {
printf("%d %s\n", n + 1, titles[n]);
}
printf("Select environment to boot: [ 1 - %d ]: ", menu_entry_count);
while (cons_gets(linebuf, MAX_CMDLINE)) {
/* cut off leading and trailing white space */
cp = trim_white_space(linebuf);
choice = strtoul(cp, NULL, 0);
/*
* If the input is totally invalid, the return value of
* strtoul() will be 0 or ULONG_MAX. Either way, it's
* of the acceptable range.
*/
if (choice == 0 || choice > menu_entry_count) {
printf("Invalid entry.\n");
continue;
}
/* XXX here is the result */
printf("\nTo boot the selected entry, invoke:\n");
printf("boot [<root-device>] -Z %s\n\n", datasets[choice - 1]);
prom_exit_to_mon();
}
}
|