Print this page
bug fix
adding functionality and fixing bugs
adding functions to menuadm
menuadm to replace bootadm menu interaction
hypervisor + bug fix
Split |
Close |
Expand all |
Collapse all |
--- old/bootadm/bootadm_hyper.c
+++ new/bootadm/bootadm_hyper.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
↓ open down ↓ |
15 lines elided |
↑ open up ↑ |
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
24 24 */
25 25
26 +/*
27 + * Copyright 2012 Daniil Lunev. All rights reserved.
28 + */
29 +
26 30 #include <stdio.h>
27 31 #include <errno.h>
28 32 #include <stdlib.h>
29 33 #include <string.h>
30 34 #include <unistd.h>
31 35 #include <alloca.h>
32 36 #include <ctype.h>
33 37 #include <sys/types.h>
34 38
35 39 #include "message.h"
36 40 #include "bootadm.h"
37 41
38 42 #define HYPER_KERNEL_DIR "/platform/i86xpv/kernel"
39 43 #define METAL_KERNEL_DIR "/platform/i86pc/kernel"
40 44
41 45 #define BOOTRC_FILE "/boot/solaris/bootenv.rc"
42 -#define ZFS_BOOTSTR "$ZFS-BOOTFS"
46 +#define ZFS_BOOTSTR "$ZFS_BOOTFS"
43 47
44 48 #define BFLAG "-B"
45 49 #define DEFAULT_SERIAL "9600,8,n,1"
46 50
47 51 #define TTYXMODE_TO_COMNUM(ttyxmode) ((int)(*((ttyxmode) + 3) - '`'))
48 52 #define COMNAME_TO_COMNUM(comname) ((int)(*((comname) + 3) - '0'))
49 53
50 54 #define WHITESPC(x) (x)
51 55
52 56 static char *serial_config[2] = { NULL, NULL };
53 57 static char *console_dev = NULL;
54 58
55 59 static char *bootenv_rc_serial[2] = { NULL, NULL };
56 60 static char *bootenv_rc_console = NULL;
57 61
58 62 static unsigned zfs_boot = 0;
59 63
60 64 /*
61 65 * Append the string pointed to by "str" to the string pointed to by "orig"
62 66 * adding the delimeter "delim" in between.
63 67 *
64 68 * Return a pointer to the new string or NULL, if we were passed a bad string.
65 69 */
66 70 static char *
67 71 append_str(char *orig, char *str, char *delim)
68 72 {
69 73 char *newstr;
70 74 int len;
71 75
72 76 if ((str == NULL) || (delim == NULL))
73 77 return (NULL);
74 78
75 79 if ((orig == NULL) || (*orig == NULL)) {
76 80 /*
77 81 * Return a pointer to a copy of the path so a caller can
78 82 * always rely upon being able to free() a returned pointer.
79 83 */
80 84 return (s_strdup(str));
81 85 }
82 86
83 87 len = strlen(orig) + strlen(str) + strlen(delim) + 1;
84 88 if ((newstr = malloc(len)) == NULL) {
85 89 bam_error(NO_MEM, len);
86 90 bam_exit(1);
87 91 }
88 92
89 93 (void) snprintf(newstr, len, "%s%s%s", orig, delim, str);
90 94 return (newstr);
91 95 }
92 96
93 97 /*
94 98 * Replace the substring "old_str" in a path with the substring "new_str"
95 99 *
96 100 * Return a pointer to the modified string.
97 101 */
98 102 static char *
99 103 modify_path(char *path, char *old_str, char *new_str)
100 104 {
101 105 char *newpath;
102 106 char *pc;
103 107 int len;
104 108
105 109 /*
106 110 * Return a pointer to a copy of the path so a caller can always rely
107 111 * upon being able to free() a returned pointer.
108 112 */
109 113 if ((pc = strstr(path, old_str)) == NULL)
110 114 return (s_strdup(path));
111 115
112 116 /*
113 117 * Allocate space for duplicate of path with name changes and
114 118 * NULL terminating byte
115 119 */
116 120 len = strlen(path) - strlen(old_str) + strlen(new_str) + 1;
117 121
118 122 if ((newpath = malloc(len)) == NULL) {
119 123 bam_error(NO_MEM, len);
120 124 bam_exit(1);
121 125 }
122 126
123 127 (void) strlcpy(newpath, path, (pc - path) + 1);
124 128 pc += strlen(old_str);
125 129
126 130 (void) strcat(newpath, new_str);
127 131 (void) strcat(newpath, pc);
128 132 return (newpath);
129 133 }
130 134
131 135 /*
132 136 * Set "token" to be the the string starting from the pointer "str" delimited
133 137 * by any character in the string "delim" or the end of the string, but IGNORE
134 138 * any characters between single or double quotes.
135 139 *
136 140 * Return a pointer to the next non-whitespace character after the delimiter
137 141 * or NULL if we hit the end of the string. Also return NULL upon failure to
138 142 * find any characters from the delimeter string or upon failure to allocate
139 143 * memory for the new token string.
140 144 */
141 145 static char *
142 146 get_token(char **token, char *str, char *delim)
143 147 {
144 148 char *dp;
145 149 char *start = str;
146 150 unsigned len;
147 151
148 152 *token = NULL;
149 153
150 154 if ((str == NULL) || (*str == NULL))
151 155 return (NULL);
152 156
153 157 do {
154 158 if ((*str == '\'') || (*str == '"')) {
155 159 char quote = *str++;
156 160
157 161 while ((*str != NULL) && (*str != quote))
158 162 str++;
159 163
160 164 /* no matching quote found in string */
161 165 if (*str++ == NULL)
162 166 return (NULL);
163 167 }
164 168
165 169 /* look for a character from the delimiter string */
166 170 for (dp = delim; ((*dp != NULL) && (*dp != *str)); dp++)
167 171 ;
168 172
169 173 if (*dp != NULL) {
170 174 len = str - start + 1;
171 175
172 176 /* found a delimiter, so create a token string */
173 177 if ((*token = malloc(len)) == NULL) {
174 178 bam_error(NO_MEM, len);
175 179 bam_exit(1);
176 180 }
177 181
178 182 (void) strlcpy(*token, start, len);
179 183
180 184 while (isspace((int)*++str))
181 185 ;
182 186
183 187 return (str);
184 188 }
185 189 } while (*str++ != NULL);
186 190
187 191 /* if we hit the end of the string, the token is the whole string */
188 192 *token = s_strdup(start);
189 193 return (NULL);
190 194 }
191 195
192 196 /*
193 197 * Convert a metal "console" device name to an equivalent one suitable for
194 198 * use with the hypervisor.
195 199 *
196 200 * Default to "vga" if we can't parse the console device.
197 201 */
198 202 static void
199 203 console_metal_to_hyper(char *console)
200 204 {
201 205 if ((*console == '\'') || (*console == '"'))
202 206 console++;
203 207
204 208 if (strncmp(console, "ttya", 4) == 0)
205 209 console_dev = "console=com1";
206 210 else if (strncmp(console, "ttyb", 4) == 0)
207 211 console_dev = "console=com2";
208 212 else
209 213 console_dev = "console=vga";
210 214 }
211 215
212 216 static int
213 217 set_serial_rate(int com, char *rate)
214 218 {
215 219 char **rp = &serial_config[com - 1];
216 220
217 221 if ((com < 1) || (com > 2))
218 222 return (-1);
219 223
220 224 /*
221 225 * If rate is a NULL pointer, erase any existing serial configuration
222 226 * for this serial port.
223 227 */
224 228 if (rate == NULL) {
225 229 if (*rp != NULL) {
226 230 free(*rp);
227 231 *rp = NULL;
228 232 }
229 233 return (0);
230 234 }
231 235
232 236 *rp = s_realloc(*rp, strlen(rate) + 1);
233 237 (void) strcpy(*rp, rate);
234 238 return (0);
235 239 }
236 240
237 241 /*
238 242 * Convert "metal" serial port parameters to values compatible with the
239 243 * hypervisor.
240 244 *
241 245 * Return 0 on success, otherwise -1.
242 246 */
243 247 static int
244 248 serial_metal_to_hyper(char *metal_port, char *metal_serial)
245 249 {
246 250 #define COM_RATE_LEN 16 /* strlen("com1=115200,8n1") */
247 251
248 252 char com_rate[COM_RATE_LEN];
249 253
250 254 unsigned com, baud, bits, stop;
251 255 char parity, handshake;
252 256
253 257 if ((strcmp(metal_port, "ttya-mode") == 0) ||
254 258 (strcmp(metal_port, "ttyb-mode") == 0))
255 259 com = TTYXMODE_TO_COMNUM(metal_port);
256 260 else
257 261 return (-1);
258 262
259 263 if ((*metal_serial == '\'') || (*metal_serial == '"'))
260 264 metal_serial++;
261 265
262 266 /*
263 267 * Check if it's specified as the default rate; if so it defaults to
264 268 * "auto" and we need not set it for they hypervisor.
265 269 */
266 270 if (strncmp(metal_serial, DEFAULT_SERIAL,
267 271 strlen(DEFAULT_SERIAL)) == 0) {
268 272 (void) set_serial_rate(com, NULL);
269 273 return (0);
270 274 }
271 275
272 276 /* read the serial port format as set forth in common/io/asy.c */
273 277 if (sscanf(metal_serial, "%u,%u,%c,%u,%c", &baud, &bits, &parity, &stop,
274 278 &handshake) != 5)
275 279 return (-1);
276 280
277 281 /* validate serial port parameters */
278 282 if (((bits < 5) || (bits > 8)) || (stop > 1) ||
279 283 ((parity != 'n') && (parity != 'e') && (parity != 'o')) ||
280 284 ((handshake != '-') && (handshake != 'h') && (handshake != 's')))
281 285 return (-1);
282 286
283 287 /* validate baud rate */
284 288 switch (baud) {
285 289 case 150:
286 290 case 300:
287 291 case 600:
288 292 case 1200:
289 293 case 2400:
290 294 case 4800:
291 295 case 9600:
292 296 case 19200:
293 297 case 38400:
294 298 case 57600:
295 299 case 115200:
296 300 break;
297 301
298 302 default:
299 303 return (-1);
300 304 }
301 305
302 306 /*
303 307 * The hypervisor has no way to specify a handshake method, so it gets
304 308 * quietly dropped in the conversion.
305 309 */
306 310 (void) snprintf(com_rate, COM_RATE_LEN, "com%d=%u,%u%c%u", com, baud,
307 311 bits, parity, stop);
308 312 (void) set_serial_rate(com, com_rate);
309 313 return (0);
310 314 }
311 315
312 316 /*
313 317 * Convert "name=value" metal options to values suitable for use with the
314 318 * hypervisor.
315 319 *
316 320 * Our main concerns are the console device and serial port settings.
317 321 *
318 322 * Return values:
319 323 *
320 324 * -1: Unparseable line
321 325 * 0: Success
322 326 * (n > 0): A property unimportant to us
323 327 */
324 328 static int
325 329 cvt_metal_option(char *optstr)
326 330 {
327 331 char *value;
328 332 unsigned namlen;
329 333
330 334 if (strcmp(optstr, ZFS_BOOTSTR) == 0) {
331 335 zfs_boot = 1;
332 336 return (0);
333 337 }
334 338
335 339 if ((value = strchr(optstr, '=')) == NULL)
336 340 return (-1);
337 341
338 342 namlen = value - optstr;
339 343
340 344 if (*++value == NULL)
341 345 return (1);
342 346
343 347 if (strncmp(optstr, "console", namlen) == 0) {
344 348 console_metal_to_hyper(value);
345 349 return (0);
346 350 }
347 351
348 352 if ((strncmp(optstr, "ttya-mode", namlen) == 0) ||
349 353 (strncmp(optstr, "ttyb-mode", namlen) == 0)) {
350 354 char *port = strndupa(optstr, namlen);
351 355
352 356 return (serial_metal_to_hyper(port, value));
353 357 }
354 358
355 359 return (1);
356 360 }
357 361
358 362 /*
359 363 * Convert "name=value" properties for use with a bare metal kernel
360 364 *
361 365 * Our main concerns are the console setting and serial port modes.
362 366 *
363 367 * Return values:
364 368 *
365 369 * -1: Unparseable line
366 370 * 0: Success
367 371 * (n > 0): A property unimportant to us
368 372 */
369 373 static int
370 374 cvt_hyper_option(char *optstr)
371 375 {
372 376 #define SER_LEN 15 /* strlen("115200,8,n,1,-") + 1 */
373 377
374 378 char ser[SER_LEN];
375 379 char *value;
376 380
377 381 unsigned namlen;
378 382
379 383 unsigned baud;
380 384 char bits, parity, stop;
381 385
382 386 if (strcmp(optstr, ZFS_BOOTSTR) == 0) {
383 387 zfs_boot = 1;
384 388 return (0);
385 389 }
386 390
387 391 /*
388 392 * If there's no "=" in the token, it's likely a standalone
389 393 * hypervisor token we don't care about (e.g. "noreboot" or
390 394 * "nosmp") so we ignore it.
391 395 */
392 396 if ((value = strchr(optstr, '=')) == NULL)
393 397 return (1);
394 398
395 399 namlen = value - optstr;
396 400
397 401 if (*++value == NULL)
398 402 return (1);
399 403
400 404 /*
401 405 * Note that we use strncmp against the values because the
402 406 * hypervisor allows setting console parameters for both the
403 407 * console and debugger via the format:
404 408 *
405 409 * console=cons_dev,debug_dev
406 410 *
407 411 * and we only care about "cons_dev."
408 412 *
409 413 * This also allows us to extract "comN" from hypervisor constructs
410 414 * like "com1H" or "com2L," concepts unsupported on bare metal kernels.
411 415 *
412 416 * Default the console device to "text" if it was "vga" or was
413 417 * unparseable.
414 418 */
415 419 if (strncmp(optstr, "console", namlen) == 0) {
416 420 /* ignore the "console=hypervisor" option */
417 421 if (strcmp(value, "hypervisor") == 0)
418 422 return (0);
419 423
420 424 if (strncmp(value, "com1", 4) == 0)
421 425 console_dev = "ttya";
422 426 else if (strncmp(value, "com2", 4) == 0)
423 427 console_dev = "ttyb";
424 428 else
425 429 console_dev = "text";
426 430 }
427 431
428 432 /* serial port parameter conversion */
429 433
430 434 if ((strncmp(optstr, "com1", namlen) == 0) ||
431 435 (strncmp(optstr, "com2", namlen) == 0)) {
432 436 unsigned com = COMNAME_TO_COMNUM(optstr);
433 437
434 438 /*
435 439 * Check if it's "auto" - if so, use the default setting
436 440 * of "9600,8,n,1,-".
437 441 *
438 442 * We can't just assume the serial port will default to
439 443 * "9600,8,n,1" as there could be a directive in bootenv.rc
440 444 * that would set it to some other value and we want the serial
441 445 * parameters to be the same as that used by the hypervisor.
442 446 */
443 447 if (strcmp(value, "auto") == 0) {
444 448 (void) snprintf(ser, SER_LEN, "9600,8,n,1,-");
445 449 } else {
446 450 /*
447 451 * Extract the "B,PS" setting from the com line; ignore
448 452 * other settings like io_base or IRQ.
449 453 */
450 454 if (sscanf(value, "%u,%c%c%c", &baud, &bits, &parity,
451 455 &stop) != 4)
452 456 return (-1);
453 457
454 458 /* validate serial port parameters */
455 459 if (((stop != '0') && (stop != '1')) ||
456 460 ((bits < '5') && (bits > '8')) ||
457 461 ((parity != 'n') && (parity != 'e') &&
458 462 (parity != 'o')))
459 463 return (-1);
460 464
461 465 /* validate baud rate */
462 466 switch (baud) {
463 467 case 150:
464 468 case 300:
465 469 case 600:
466 470 case 1200:
467 471 case 2400:
468 472 case 4800:
469 473 case 19200:
470 474 case 38400:
471 475 case 57600:
472 476 case 115200:
473 477 break;
474 478
475 479 default:
476 480 return (-1);
477 481 }
478 482
479 483 /*
480 484 * As the hypervisor has no way to denote handshaking
481 485 * in its serial port settings, emit a metal serial
482 486 * port configuration with none as well.
483 487 */
484 488 (void) snprintf(ser, SER_LEN, "%u,%c,%c,%c,-", baud,
485 489 bits, parity, stop);
486 490 }
487 491
488 492 if (set_serial_rate(com, ser) != 0)
489 493 return (-1);
490 494
491 495 return (0);
492 496 }
493 497
494 498 return (1);
495 499 }
496 500
497 501 /*
498 502 * Parse a hardware kernel's "kernel$" specifier into parameters we can then
499 503 * use to construct an appropriate "module$" line that can be used to specify
500 504 * how to boot the hypervisor's dom0.
501 505 *
502 506 * Return values:
503 507 *
504 508 * -1: error parsing kernel path
505 509 * 0: success
506 510 * 1: kernel already a hypervisor kernel
507 511 */
508 512 static int
509 513 cvt_metal_kernel(char *kernstr, char **path)
510 514 {
511 515 char *token, *parsestr;
512 516
513 517 parsestr = get_token(path, kernstr, " \t,");
514 518 if (*path == NULL)
515 519 return (-1);
516 520
517 521 /*
518 522 * If the metal kernel specified contains the name of the hypervisor,
519 523 * we're probably trying to convert an entry already setup to run the
520 524 * hypervisor, so error out now.
521 525 */
522 526 if (strstr(*path, XEN_MENU) != NULL) {
523 527 bam_error(ALREADY_HYPER);
524 528 free(*path);
525 529 *path = NULL;
526 530 return (1);
527 531 }
528 532
529 533 /* if the path was the last item on the line, that's OK. */
530 534 if ((parsestr = get_token(&token, parsestr, " \t,")) == NULL) {
531 535 if (token != NULL)
532 536 free(token);
533 537 return (0);
534 538 }
535 539
536 540 /* if the next token is "-B" process boot options */
537 541 if (strncmp(token, BFLAG, strlen(BFLAG)) != 0) {
538 542 free(token);
539 543 return (0);
540 544 }
541 545
542 546 free(token);
543 547
544 548 while ((parsestr = get_token(&token, parsestr, ",")) != NULL) {
545 549 (void) cvt_metal_option(token);
546 550 free(token);
547 551 }
548 552
549 553 if (token != NULL) {
550 554 (void) cvt_metal_option(token);
551 555 free(token);
552 556 }
553 557
554 558 return (0);
555 559 }
556 560
557 561 /*
558 562 * Parse a hypervisor's "kernel$" line into parameters that can be used to
559 563 * help build an appropriate "kernel$" line for booting a bare metal kernel.
560 564 *
561 565 * Return 0 on success, non-zero on failure.
562 566 */
563 567 static int
564 568 cvt_hyper_kernel(char *kernel)
565 569 {
566 570 char *token, *parsestr;
567 571
568 572 parsestr = get_token(&token, kernel, " \t,");
569 573
570 574 if (token == NULL)
571 575 return (-1);
572 576
573 577 /*
574 578 * If the hypervisor kernel specified lives in the metal kernel
575 579 * directory, we're probably trying to convert an entry already setup
576 580 * to run on bare metal, so error out now.
577 581 */
578 582 if (strncmp(token, METAL_KERNEL_DIR, strlen(METAL_KERNEL_DIR)) == 0) {
579 583 bam_error(ALREADY_METAL);
580 584 free(token);
581 585 return (-1);
582 586 }
583 587
584 588 free(token);
585 589
586 590 /* check for kernel options */
587 591 while ((parsestr = get_token(&token, parsestr, " ")) != NULL) {
588 592 (void) cvt_hyper_option(token);
589 593 free(token);
590 594 }
591 595
592 596 if (token != NULL) {
593 597 (void) cvt_hyper_option(token);
594 598 free(token);
595 599 }
596 600
597 601 return (0);
598 602 }
599 603
600 604 /*
601 605 * Parse a hypervisor's "module$" line into parameters that can be used to
602 606 * help build an appropriate "kernel$" line for booting a bare metal kernel.
603 607 */
604 608 static void
605 609 cvt_hyper_module(char *modstr, char **path)
606 610 {
607 611 char *token = NULL;
608 612 char *parsestr = modstr;
609 613
610 614 /*
611 615 * If multiple pathnames exist on the module$ line, we just want
612 616 * the last one.
613 617 */
614 618 while ((parsestr = get_token(path, parsestr, " \t,")) != NULL) {
615 619 if (*parsestr != '/')
616 620 break;
617 621 else
618 622 free(*path);
619 623 }
620 624
621 625 /* if the path was the last item on the line, that's OK. */
622 626 if ((parsestr == NULL) ||
623 627 ((parsestr = get_token(&token, parsestr, " \t,")) == NULL)) {
624 628 if (token != NULL)
625 629 free(token);
626 630 return;
627 631 }
628 632
629 633 if (token == NULL)
630 634 return;
631 635
632 636 /* check for "-B" option */
633 637 if (strncmp(token, BFLAG, strlen(BFLAG)) != 0) {
634 638 free(token);
635 639 return;
636 640 }
637 641
638 642 free(token);
639 643
640 644 /* check for kernel options */
641 645 while ((parsestr = get_token(&token, parsestr, ",")) != NULL) {
642 646 (void) cvt_hyper_option(token);
643 647 free(token);
644 648 }
645 649
646 650 if (token != NULL) {
647 651 (void) cvt_hyper_option(token);
648 652 free(token);
649 653 }
650 654 }
651 655
652 656 static void
653 657 parse_bootenvrc(char *osroot)
654 658 {
655 659 #define LINEBUF_SZ 1024
656 660
657 661 FILE *fp;
658 662 char *rcpath;
659 663 char line[LINEBUF_SZ]; /* make line buffer large but not ridiculous */
660 664 int len;
661 665
662 666 assert(osroot);
663 667
664 668 len = strlen(osroot) + strlen(BOOTRC_FILE) + 1;
665 669 rcpath = alloca(len);
666 670
667 671 (void) snprintf(rcpath, len, "%s%s", osroot, BOOTRC_FILE);
668 672
669 673 /* if we couldn't open the bootenv.rc file, ignore the issue. */
670 674 if ((fp = fopen(rcpath, "r")) == NULL) {
671 675 BAM_DPRINTF((D_NO_BOOTENVRC, rcpath, strerror(errno)));
672 676 return;
673 677 }
674 678
675 679 while (s_fgets(line, LINEBUF_SZ, fp) != NULL) {
676 680 char *parsestr, *token;
677 681 int port = 0;
678 682
679 683 /* we're only interested in parsing "setprop" directives. */
680 684 if (strncmp(line, "setprop", 7) != NULL)
681 685 continue;
682 686
683 687 /* eat initial "setprop" */
684 688 if ((parsestr = get_token(&token, line, " \t")) == NULL) {
685 689 if (token != NULL)
686 690 free(token);
687 691
688 692 continue;
689 693 }
690 694
691 695 if (strcmp(token, "setprop") != 0) {
692 696 free(token);
693 697 continue;
694 698 }
695 699
696 700 free(token);
697 701
698 702 /* get property name */
699 703 if ((parsestr = get_token(&token, parsestr, " \t")) == NULL) {
700 704 if (token != NULL)
701 705 free(token);
702 706
703 707 continue;
704 708 }
705 709
706 710 if (strcmp(token, "console") == 0) {
707 711 free(token);
708 712
709 713 /* get console property value */
710 714 parsestr = get_token(&token, parsestr, " \t");
711 715 if (token == NULL)
712 716 continue;
713 717
714 718 if (bootenv_rc_console != NULL)
715 719 free(bootenv_rc_console);
716 720
717 721 bootenv_rc_console = s_strdup(token);
718 722 continue;
719 723 }
720 724
721 725 /* check if it's a serial port setting */
722 726 if (strcmp(token, "ttya-mode") == 0) {
723 727 free(token);
724 728 port = 0;
725 729 } else if (strcmp(token, "ttyb-mode") == 0) {
726 730 free(token);
727 731 port = 1;
728 732 } else {
729 733 /* nope, so check the next line */
730 734 free(token);
731 735 continue;
732 736 }
733 737
734 738 /* get serial port setting */
735 739 parsestr = get_token(&token, parsestr, " \t");
736 740
737 741 if (token == NULL)
738 742 continue;
739 743
740 744 if (bootenv_rc_serial[port] != NULL)
741 745 free(bootenv_rc_serial[port]);
742 746
743 747 bootenv_rc_serial[port] = s_strdup(token);
744 748 free(token);
745 749 }
746 750
747 751 (void) fclose(fp);
748 752 }
749 753
750 754 error_t
751 755 cvt_to_hyper(menu_t *mp, char *osroot, char *extra_args)
752 756 {
753 757 const char *fcn = "cvt_to_hyper()";
754 758
755 759 line_t *lp;
↓ open down ↓ |
703 lines elided |
↑ open up ↑ |
756 760 entry_t *ent;
757 761 size_t len, zfslen;
758 762
759 763 char *newstr;
760 764 char *osdev;
761 765
762 766 char *title = NULL;
763 767 char *findroot = NULL;
764 768 char *bootfs = NULL;
765 769 char *kernel = NULL;
770 + char *opts = NULL;
766 771 char *mod_kernel = NULL;
767 772 char *module = NULL;
773 + char *tmp = NULL;
768 774
769 775 char *kern_path = NULL;
770 776 char *kern_bargs = NULL;
771 777
772 778 int curdef, newdef;
773 779 int kp_allocated = 0;
774 780 int ret = BAM_ERROR;
775 781
776 782 assert(osroot);
777 783
778 784 BAM_DPRINTF((D_FUNC_ENTRY2, fcn, osroot, extra_args));
779 785
780 786 /*
781 787 * First just check to verify osroot is a sane directory.
782 788 */
783 789 if ((osdev = get_special(osroot)) == NULL) {
784 790 bam_error(CANT_FIND_SPECIAL, osroot);
785 791 return (BAM_ERROR);
786 792 }
787 793
788 794 free(osdev);
789 795
790 796 /*
791 797 * While the effect is purely cosmetic, if osroot is "/" don't
792 798 * bother prepending it to any paths as they are constructed to
793 799 * begin with "/" anyway.
794 800 */
795 801 if (strcmp(osroot, "/") == 0)
796 802 osroot = "";
797 803
798 804 /*
799 805 * Found the GRUB signature on the target partitions, so now get the
800 806 * default GRUB boot entry number from the menu.lst file
801 807 */
802 808 curdef = atoi(mp->curdefault->arg);
803 809
804 810 /* look for the first line of the matching boot entry */
805 811 for (ent = mp->entries; ((ent != NULL) && (ent->entryNum != curdef));
806 812 ent = ent->next)
807 813 ;
808 814
809 815 /* couldn't find it, so error out */
810 816 if (ent == NULL) {
811 817 bam_error(CANT_FIND_DEFAULT, curdef);
812 818 goto abort;
813 819 }
814 820
815 821 /*
816 822 * We found the proper menu entry, so first we need to process the
817 823 * bootenv.rc file to look for boot options the hypervisor might need
818 824 * passed as kernel start options such as the console device and serial
819 825 * port parameters.
820 826 *
821 827 * If there's no bootenv.rc, it's not an issue.
822 828 */
823 829 parse_bootenvrc(osroot);
824 830
825 831 if (bootenv_rc_console != NULL)
826 832 console_metal_to_hyper(bootenv_rc_console);
827 833
828 834 if (bootenv_rc_serial[0] != NULL)
829 835 (void) serial_metal_to_hyper("ttya-mode", bootenv_rc_serial[0]);
830 836
831 837 if (bootenv_rc_serial[1] != NULL)
832 838 (void) serial_metal_to_hyper("ttyb-mode", bootenv_rc_serial[1]);
833 839
↓ open down ↓ |
56 lines elided |
↑ open up ↑ |
834 840 /*
835 841 * Now process the entry itself.
836 842 */
837 843 for (lp = ent->start; lp != NULL; lp = lp->next) {
838 844 /*
839 845 * Process important lines from menu.lst boot entry.
840 846 */
841 847 if (lp->flags == BAM_TITLE) {
842 848 title = strdupa(lp->arg);
843 849 } else if (lp->cmd != NULL) {
844 - if (strcmp(lp->cmd, "findroot") == 0) {
850 + if (strcmp(lp->cmd, "pool_label") == 0) {
845 851 findroot = strdupa(lp->arg);
846 - } else if (strcmp(lp->cmd, "bootfs") == 0) {
852 + } else if (strcmp(lp->cmd, "data_set") == 0) {
847 853 bootfs = strdupa(lp->arg);
854 + } else if (strcmp(lp->cmd, "kernel_options") == 0) {
855 + opts = strdupa(lp->arg);
848 856 } else if (strcmp(lp->cmd,
849 857 menu_cmds[MODULE_DOLLAR_CMD]) == 0) {
850 858 module = strdupa(lp->arg);
851 859 } else if ((strcmp(lp->cmd,
852 860 menu_cmds[KERNEL_DOLLAR_CMD]) == 0) &&
853 861 (ret = cvt_metal_kernel(lp->arg,
854 862 &kern_path)) != 0) {
855 863 if (ret < 0) {
856 864 ret = BAM_ERROR;
857 865 bam_error(KERNEL_NOT_PARSEABLE, curdef);
858 866 } else
↓ open down ↓ |
1 lines elided |
↑ open up ↑ |
859 867 ret = BAM_NOCHANGE;
860 868
861 869 goto abort;
862 870 }
863 871 }
864 872
865 873 if (lp == ent->end)
866 874 break;
867 875 }
868 876
877 +
869 878 /*
870 879 * If findroot, module or kern_path are NULL, the boot entry is
871 880 * malformed.
872 881 */
873 882 if (findroot == NULL) {
874 883 bam_error(FINDROOT_NOT_FOUND, curdef);
875 884 goto abort;
876 885 }
877 886
878 887 if (module == NULL) {
879 888 bam_error(MODULE_NOT_PARSEABLE, curdef);
880 889 goto abort;
881 890 }
882 891
883 892 if (kern_path == NULL) {
884 893 bam_error(KERNEL_NOT_FOUND, curdef);
885 894 goto abort;
886 895 }
887 896
888 897 /* assemble new kernel and module arguments from parsed values */
889 898 if (console_dev != NULL) {
890 899 kern_bargs = s_strdup(console_dev);
891 900
892 901 if (serial_config[0] != NULL) {
893 902 newstr = append_str(kern_bargs, serial_config[0], " ");
894 903 free(kern_bargs);
895 904 kern_bargs = newstr;
896 905 }
897 906
898 907 if (serial_config[1] != NULL) {
899 908 newstr = append_str(kern_bargs, serial_config[1], " ");
900 909 free(kern_bargs);
901 910 kern_bargs = newstr;
902 911 }
903 912 }
904 913
905 914 if ((extra_args != NULL) && (*extra_args != NULL)) {
906 915 newstr = append_str(kern_bargs, extra_args, " ");
907 916 free(kern_bargs);
908 917 kern_bargs = newstr;
909 918 }
910 919
911 920 len = strlen(osroot) + strlen(XEN_MENU) + strlen(kern_bargs) +
912 921 WHITESPC(1) + 1;
913 922
914 923 kernel = alloca(len);
915 924
916 925 if (kern_bargs != NULL) {
917 926 if (*kern_bargs != NULL)
918 927 (void) snprintf(kernel, len, "%s%s %s", osroot,
919 928 XEN_MENU, kern_bargs);
920 929
921 930 free(kern_bargs);
922 931 } else {
923 932 (void) snprintf(kernel, len, "%s%s", osroot, XEN_MENU);
924 933 }
925 934
926 935 /*
927 936 * Change the kernel directory from the metal version to that needed for
928 937 * the hypervisor. Convert either "direct boot" path to the default
929 938 * path.
930 939 */
931 940 if ((strcmp(kern_path, DIRECT_BOOT_32) == 0) ||
932 941 (strcmp(kern_path, DIRECT_BOOT_64) == 0)) {
933 942 kern_path = HYPERVISOR_KERNEL;
934 943 } else {
935 944 newstr = modify_path(kern_path, METAL_KERNEL_DIR,
936 945 HYPER_KERNEL_DIR);
937 946 free(kern_path);
938 947 kern_path = newstr;
939 948 kp_allocated = 1;
940 949 }
↓ open down ↓ |
62 lines elided |
↑ open up ↑ |
941 950
942 951 /*
943 952 * We need to allocate space for the kernel path (twice) plus an
944 953 * intervening space, possibly the ZFS boot string, and NULL,
945 954 * of course.
946 955 */
947 956 len = (strlen(kern_path) * 2) + WHITESPC(1) + 1;
948 957 zfslen = (zfs_boot ? (WHITESPC(1) + strlen(ZFS_BOOT)) : 0);
949 958
950 959 mod_kernel = alloca(len + zfslen);
951 - (void) snprintf(mod_kernel, len, "%s %s", kern_path, kern_path);
960 + if (opts)
961 + (void) snprintf(mod_kernel, len + strlen(opts) + 1, "%s %s %s", kern_path, kern_path, opts);
962 + else
963 + (void) snprintf(mod_kernel, len, "%s %s", kern_path, kern_path);
952 964
953 965 if (kp_allocated)
954 966 free(kern_path);
955 967
956 968 if (zfs_boot) {
957 969 char *zfsstr = alloca(zfslen + 1);
958 970
959 971 (void) snprintf(zfsstr, zfslen + 1, " %s", ZFS_BOOT);
960 972 (void) strcat(mod_kernel, zfsstr);
961 973 }
962 974
963 975 /* shut off warning messages from the entry line parser */
964 976 if (ent->flags & BAM_ENTRY_BOOTADM)
965 977 ent->flags &= ~BAM_ENTRY_BOOTADM;
966 978
967 979 BAM_DPRINTF((D_CVT_CMD_KERN_DOLLAR, fcn, kernel));
968 980 BAM_DPRINTF((D_CVT_CMD_MOD_DOLLAR, fcn, mod_kernel));
969 981
970 982 if ((newdef = add_boot_entry(mp, title, findroot, kernel, mod_kernel,
971 983 module, bootfs)) == BAM_ERROR)
972 984 return (newdef);
973 985
974 986 /*
975 987 * Now try to delete the current default entry from the menu and add
976 988 * the new hypervisor entry with the parameters we've setup.
977 989 */
978 990 if (delete_boot_entry(mp, curdef, DBE_QUIET) == BAM_SUCCESS)
979 991 newdef--;
980 992 else
981 993 bam_print(NEW_BOOT_ENTRY, title);
982 994
983 995 /*
984 996 * If we successfully created the new entry, set the default boot
985 997 * entry to that entry and let the caller know the new menu should
986 998 * be written out.
987 999 */
988 1000 return (set_global(mp, menu_cmds[DEFAULT_CMD], newdef));
989 1001
990 1002 abort:
991 1003 if (ret != BAM_NOCHANGE)
992 1004 bam_error(HYPER_ABORT, ((*osroot == NULL) ? "/" : osroot));
993 1005
994 1006 return (ret);
995 1007 }
996 1008
997 1009 /*ARGSUSED*/
998 1010 error_t
999 1011 cvt_to_metal(menu_t *mp, char *osroot, char *menu_root)
1000 1012 {
1001 1013 const char *fcn = "cvt_to_metal()";
1002 1014
1003 1015 line_t *lp;
1004 1016 entry_t *ent;
1005 1017 size_t len, zfslen;
1006 1018
1007 1019 char *delim = ",";
1008 1020 char *newstr;
1009 1021 char *osdev;
1010 1022
1011 1023 char *title = NULL;
1012 1024 char *findroot = NULL;
1013 1025 char *bootfs = NULL;
1014 1026 char *kernel = NULL;
1015 1027 char *module = NULL;
1016 1028
1017 1029 char *barchive_path = DIRECT_BOOT_ARCHIVE;
1018 1030 char *kern_path = NULL;
1019 1031
1020 1032 int curdef, newdef;
1021 1033 int emit_bflag = 1;
1022 1034 int ret = BAM_ERROR;
1023 1035
1024 1036 assert(osroot);
1025 1037
1026 1038 BAM_DPRINTF((D_FUNC_ENTRY2, fcn, osroot, ""));
1027 1039
1028 1040 /*
1029 1041 * First just check to verify osroot is a sane directory.
1030 1042 */
1031 1043 if ((osdev = get_special(osroot)) == NULL) {
1032 1044 bam_error(CANT_FIND_SPECIAL, osroot);
1033 1045 return (BAM_ERROR);
1034 1046 }
1035 1047
1036 1048 free(osdev);
1037 1049
1038 1050 /*
1039 1051 * Found the GRUB signature on the target partitions, so now get the
1040 1052 * default GRUB boot entry number from the menu.lst file
1041 1053 */
1042 1054 curdef = atoi(mp->curdefault->arg);
1043 1055
1044 1056 /* look for the first line of the matching boot entry */
1045 1057 for (ent = mp->entries; ((ent != NULL) && (ent->entryNum != curdef));
1046 1058 ent = ent->next)
1047 1059 ;
1048 1060
1049 1061 /* couldn't find it, so error out */
1050 1062 if (ent == NULL) {
1051 1063 bam_error(CANT_FIND_DEFAULT, curdef);
1052 1064 goto abort;
1053 1065 }
1054 1066
↓ open down ↓ |
93 lines elided |
↑ open up ↑ |
1055 1067 /*
1056 1068 * Now process the entry itself.
1057 1069 */
1058 1070 for (lp = ent->start; lp != NULL; lp = lp->next) {
1059 1071 /*
1060 1072 * Process important lines from menu.lst boot entry.
1061 1073 */
1062 1074 if (lp->flags == BAM_TITLE) {
1063 1075 title = strdupa(lp->arg);
1064 1076 } else if (lp->cmd != NULL) {
1065 - if (strcmp(lp->cmd, "findroot") == 0) {
1077 + if (strcmp(lp->cmd, "pool_label") == 0) {
1066 1078 findroot = strdupa(lp->arg);
1067 - } else if (strcmp(lp->cmd, "bootfs") == 0) {
1079 + } else if (strcmp(lp->cmd, "data_set") == 0) {
1068 1080 bootfs = strdupa(lp->arg);
1069 1081 } else if (strcmp(lp->cmd,
1070 1082 menu_cmds[MODULE_DOLLAR_CMD]) == 0) {
1071 1083 if (strstr(lp->arg, "boot_archive") == NULL) {
1072 1084 module = strdupa(lp->arg);
1073 1085 cvt_hyper_module(module, &kern_path);
1074 1086 } else {
1075 1087 barchive_path = strdupa(lp->arg);
1076 1088 }
1077 1089 } else if ((strcmp(lp->cmd,
1078 1090 menu_cmds[KERNEL_DOLLAR_CMD]) == 0) &&
1079 1091 (cvt_hyper_kernel(lp->arg) < 0)) {
1080 1092 ret = BAM_NOCHANGE;
1081 1093 goto abort;
1082 1094 }
1083 1095 }
1084 1096
1085 1097 if (lp == ent->end)
1086 1098 break;
1087 1099 }
1088 1100
1089 1101 /*
1090 1102 * If findroot, module or kern_path are NULL, the boot entry is
1091 1103 * malformed.
1092 1104 */
1093 1105 if (findroot == NULL) {
1094 1106 bam_error(FINDROOT_NOT_FOUND, curdef);
1095 1107 goto abort;
1096 1108 }
1097 1109
1098 1110 if (module == NULL) {
1099 1111 bam_error(MODULE_NOT_PARSEABLE, curdef);
1100 1112 goto abort;
1101 1113 }
1102 1114
1103 1115 if (kern_path == NULL) {
1104 1116 bam_error(KERNEL_NOT_FOUND, curdef);
1105 1117 goto abort;
1106 1118 }
1107 1119
1108 1120 /*
1109 1121 * Assemble new kernel and module arguments from parsed values.
1110 1122 *
1111 1123 * First, change the kernel directory from the hypervisor version to
1112 1124 * that needed for a metal kernel.
1113 1125 */
1114 1126 newstr = modify_path(kern_path, HYPER_KERNEL_DIR, METAL_KERNEL_DIR);
1115 1127 free(kern_path);
1116 1128 kern_path = newstr;
1117 1129
1118 1130 /* allocate initial space for the kernel path */
1119 1131 len = strlen(kern_path) + 1;
1120 1132 zfslen = (zfs_boot ? (WHITESPC(1) + strlen(ZFS_BOOT)) : 0);
1121 1133
1122 1134 if ((kernel = malloc(len + zfslen)) == NULL) {
1123 1135 free(kern_path);
1124 1136 bam_error(NO_MEM, len + zfslen);
1125 1137 bam_exit(1);
1126 1138 }
1127 1139
1128 1140 (void) snprintf(kernel, len, "%s", kern_path);
1129 1141 free(kern_path);
1130 1142
1131 1143 if (zfs_boot) {
1132 1144 char *zfsstr = alloca(zfslen + 1);
1133 1145
1134 1146 (void) snprintf(zfsstr, zfslen + 1, " %s", ZFS_BOOT);
1135 1147 (void) strcat(kernel, zfsstr);
1136 1148 emit_bflag = 0;
1137 1149 }
1138 1150
1139 1151 /*
1140 1152 * Process the bootenv.rc file to look for boot options that would be
1141 1153 * the same as what the hypervisor had manually set, as we need not set
1142 1154 * those explicitly.
1143 1155 *
1144 1156 * If there's no bootenv.rc, it's not an issue.
1145 1157 */
1146 1158 parse_bootenvrc(osroot);
1147 1159
1148 1160 /*
1149 1161 * Don't emit a console setting if it's the same as what would be
1150 1162 * set by bootenv.rc.
1151 1163 */
1152 1164 if ((console_dev != NULL) && (bootenv_rc_console == NULL ||
1153 1165 (strcmp(console_dev, bootenv_rc_console) != 0))) {
1154 1166 if (emit_bflag) {
1155 1167 newstr = append_str(kernel, BFLAG, " ");
1156 1168 free(kernel);
1157 1169 kernel = append_str(newstr, "console=", " ");
1158 1170 free(newstr);
1159 1171 newstr = append_str(kernel, console_dev, "");
1160 1172 free(kernel);
1161 1173 kernel = newstr;
1162 1174 emit_bflag = 0;
1163 1175 } else {
1164 1176 newstr = append_str(kernel, "console=", ",");
1165 1177 free(kernel);
1166 1178 kernel = append_str(newstr, console_dev, "");
1167 1179 free(newstr);
1168 1180 }
1169 1181 }
1170 1182
1171 1183 /*
1172 1184 * We have to do some strange processing here because the hypervisor's
1173 1185 * serial ports default to "9600,8,n,1,-" if "comX=auto" is specified,
1174 1186 * or to "auto" if nothing is specified.
1175 1187 *
1176 1188 * This could result in a serial mode setting string being added when
1177 1189 * it would otherwise not be needed, but it's better to play it safe.
1178 1190 */
1179 1191 if (emit_bflag) {
1180 1192 newstr = append_str(kernel, BFLAG, " ");
1181 1193 free(kernel);
1182 1194 kernel = newstr;
1183 1195 delim = " ";
1184 1196 emit_bflag = 0;
1185 1197 }
1186 1198
1187 1199 if ((serial_config[0] != NULL) && (bootenv_rc_serial[0] == NULL ||
1188 1200 (strcmp(serial_config[0], bootenv_rc_serial[0]) != 0))) {
1189 1201 newstr = append_str(kernel, "ttya-mode='", delim);
1190 1202 free(kernel);
1191 1203
1192 1204 /*
1193 1205 * Pass the serial configuration as the delimiter to
1194 1206 * append_str() as it will be inserted between the current
1195 1207 * string and the string we're appending, in this case the
1196 1208 * closing single quote.
1197 1209 */
1198 1210 kernel = append_str(newstr, "'", serial_config[0]);
1199 1211 free(newstr);
1200 1212 delim = ",";
1201 1213 }
1202 1214
1203 1215 if ((serial_config[1] != NULL) && (bootenv_rc_serial[1] == NULL ||
1204 1216 (strcmp(serial_config[1], bootenv_rc_serial[1]) != 0))) {
1205 1217 newstr = append_str(kernel, "ttyb-mode='", delim);
1206 1218 free(kernel);
1207 1219
1208 1220 /*
1209 1221 * Pass the serial configuration as the delimiter to
1210 1222 * append_str() as it will be inserted between the current
1211 1223 * string and the string we're appending, in this case the
1212 1224 * closing single quote.
1213 1225 */
1214 1226 kernel = append_str(newstr, "'", serial_config[1]);
1215 1227 free(newstr);
1216 1228 delim = ",";
1217 1229 }
1218 1230
1219 1231 /* shut off warning messages from the entry line parser */
1220 1232 if (ent->flags & BAM_ENTRY_BOOTADM)
1221 1233 ent->flags &= ~BAM_ENTRY_BOOTADM;
1222 1234
1223 1235 BAM_DPRINTF((D_CVT_CMD_KERN_DOLLAR, fcn, kernel));
1224 1236 BAM_DPRINTF((D_CVT_CMD_MOD_DOLLAR, fcn, module));
1225 1237
1226 1238 if ((newdef = add_boot_entry(mp, title, findroot, kernel, NULL,
1227 1239 barchive_path, bootfs)) == BAM_ERROR) {
1228 1240 free(kernel);
1229 1241 return (newdef);
1230 1242 }
1231 1243
1232 1244 /*
1233 1245 * Now try to delete the current default entry from the menu and add
1234 1246 * the new hypervisor entry with the parameters we've setup.
1235 1247 */
1236 1248 if (delete_boot_entry(mp, curdef, DBE_QUIET) == BAM_SUCCESS)
1237 1249 newdef--;
1238 1250 else
1239 1251 bam_print(NEW_BOOT_ENTRY, title);
1240 1252
1241 1253 free(kernel);
1242 1254
1243 1255 /*
1244 1256 * If we successfully created the new entry, set the default boot
1245 1257 * entry to that entry and let the caller know the new menu should
1246 1258 * be written out.
1247 1259 */
1248 1260 return (set_global(mp, menu_cmds[DEFAULT_CMD], newdef));
1249 1261
1250 1262 abort:
1251 1263 if (ret != BAM_NOCHANGE)
1252 1264 bam_error(METAL_ABORT, osroot);
1253 1265
1254 1266 return (ret);
1255 1267 }
↓ open down ↓ |
178 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX