• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* ----------------------------------------------------------------------- *
2  *
3  *   Copyright 2010 Gert Hulselmans - All Rights Reserved
4  *
5  *   This program is free software; you can redistribute it and/or modify
6  *   it under the terms of the GNU General Public License as published by
7  *   the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
8  *   Boston MA 02110-1301, USA; either version 2 of the License, or
9  *   (at your option) any later version; incorporated herein by reference.
10  *
11  * ----------------------------------------------------------------------- */
12 
13 /*
14  * whichsys.c
15  *
16  * Detemine which command to execute, based on the Syslinux bootloader variant
17  * from which you run it.
18  *
19  * Usage:    whichsys.c32 [-iso- command] [-pxe- command] [-sys- command]
20  * Examples: whichsys.c32 -iso- chain.c32 hd0 -sys- chain.c32 hd1 swap
21  *           whichsys.c32 -iso- config iso.cfg -pxe- config pxe.cfg
22  *
23  */
24 
25 #include <stdio.h>
26 #include <alloca.h>
27 #include <console.h>
28 #include <string.h>
29 #include <syslinux/boot.h>
30 #include "syslinux/config.h"
31 
32 
33 static struct syslinux_parameter {
34     char **arg[1];
35     bool option;
36 } isolinux, pxelinux, syslinux;
37 
38 /* XXX: this really should be librarized */
boot_args(char ** args)39 static void boot_args(char **args)
40 {
41     int len = 0, a = 0;
42     char **pp;
43     const char *p;
44     char c, *q, *str;
45 
46     for (pp = args; *pp; pp++)
47 	len += strlen(*pp) + 1;
48 
49     q = str = alloca(len);
50     for (pp = args; *pp; pp++) {
51 	p = *pp;
52 	while ((c = *p++))
53 	    *q++ = c;
54 	*q++ = ' ';
55 	a = 1;
56     }
57     q -= a;
58     *q = '\0';
59 
60     if (!str[0])
61 	syslinux_run_default();
62     else
63 	syslinux_run_command(str);
64 }
65 
usage(void)66 static void usage(void)
67 {
68     static const char usage[] = "\
69 Usage:    whichsys.c32 [-iso- command] [-pxe- command] [-sys- command]\n\
70 Examples: whichsys.c32 -iso- chain.c32 hd0 -sys- chain.c32 hd1 swap\n\
71           whichsys.c32 -iso- config iso.cfg -pxe- config pxe.cfg\n";
72     fprintf(stderr, usage);
73 }
74 
main(int argc,char * argv[])75 int main(int argc, char *argv[])
76 {
77     const union syslinux_derivative_info *sdi;
78 
79     int arg = 0;
80 
81     /* If no argument got passed, let's show the usage */
82     if (argc == 1) {
83 	    usage();
84 	    return 0;
85     }
86 
87     arg++;
88 
89     while (arg < argc) {
90 	if (!strcmp(argv[arg], "-iso-")) {
91 	    argv[arg] = NULL;
92 	    isolinux.arg[0] = &argv[arg + 1];
93 	    isolinux.option = true;
94 	}
95 	if (!strcmp(argv[arg], "-pxe-")) {
96 	    argv[arg] = NULL;
97 	    pxelinux.arg[0] = &argv[arg + 1];
98 	    pxelinux.option = true;
99 	}
100 	if (!strcmp(argv[arg], "-sys-")) {
101 	    argv[arg] = NULL;
102 	    syslinux.arg[0] = &argv[arg + 1];
103 	    syslinux.option = true;
104 	}
105 	arg++;
106     }
107 
108     sdi = syslinux_derivative_info();
109 
110     switch (sdi->c.filesystem) {
111 	case SYSLINUX_FS_ISOLINUX:
112 	    isolinux.option ? boot_args(isolinux.arg[0]) : fprintf(stderr, "No command specified for ISOLINUX.\n\n"); usage();
113 	    break;
114 	case SYSLINUX_FS_PXELINUX:
115 	    pxelinux.option ? boot_args(pxelinux.arg[0]) : fprintf(stderr, "No command specified for PXELINUX.\n\n"); usage();
116 	    break;
117 	case SYSLINUX_FS_SYSLINUX:
118 	    syslinux.option ? boot_args(syslinux.arg[0]) : fprintf(stderr, "No command specified for SYSLINUX.\n\n"); usage();
119 	    break;
120 	case SYSLINUX_FS_UNKNOWN:
121 	default:
122 	    fprintf(stderr, "Unknown Syslinux filesystem\n\n");
123     }
124 
125     return -1;
126 }
127