Adding upstream version 4.00~pre61+dfsg.
[syslinux-debian/hramrach.git] / com32 / modules / ifcpu.c
blobcf6282f5897c3647b8d670b9dd2611302b042f2a
1 /* ----------------------------------------------------------------------- *
3 * Copyright 2009 Erwan Velu - All Rights Reserved
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.
11 * ----------------------------------------------------------------------- */
14 * ifcpu.c
18 #include <alloca.h>
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <cpuid.h>
23 #include <syslinux/boot.h>
24 #include <com32.h>
25 #include <consoles.h>
27 #define REG_AH(x) ((x).eax.b[1])
28 #define REG_CX(x) ((x).ecx.w[0])
29 #define REG_DX(x) ((x).edx.w[0])
31 static inline void error(const char *msg)
33 fputs(msg, stderr);
36 static void usage(void)
38 error("Run one command if system match some CPU features, another if it doesn't. \n"
39 "Usage: \n"
40 " label ifcpu \n"
41 " com32 ifcpu.c32 \n"
42 " append <option> <cpu_features> -- boot_entry_1 -- boot_entry_2 \n"
43 " label boot_entry_1 \n"
44 " kernel vmlinuz_entry1 \n"
45 " append ... \n"
46 " label boot_entry_2 \n"
47 " kernel vmlinuz_entry2 \n"
48 " append ... \n"
49 "\n"
50 "options could be :\n"
51 " debug : display some debugging messages \n"
52 " dry-run : just do the detection, don't boot \n"
53 "\n"
54 "cpu_features could be:\n"
55 " 64 : Processor is x86_64 compatible (lm cpu flag)\n"
56 " hvm : Processor features hardware virtualization (hvm or svm cpu flag)\n"
57 " multicore : Processor must be multi-core \n"
58 " smp : System must be multi-processor \n"
59 " pae : Processor features Physical Address Extension (PAE)\n"
60 "\n"
61 "if you want to match many cpu features, just separate them with a single space.\n");
64 static unsigned char sleep(unsigned int msec)
66 unsigned long micro = 1000 * msec;
67 com32sys_t inreg, outreg;
69 REG_AH(inreg) = 0x86;
70 REG_CX(inreg) = (micro >> 16);
71 REG_DX(inreg) = (micro & 0xFFFF);
72 __intcall(0x15, &inreg, &outreg);
73 return REG_AH(outreg);
76 /* XXX: this really should be librarized */
77 static void boot_args(char **args)
79 int len = 0, a = 0;
80 char **pp;
81 const char *p;
82 char c, *q, *str;
84 for (pp = args; *pp; pp++)
85 len += strlen(*pp) + 1;
87 q = str = alloca(len);
88 for (pp = args; *pp; pp++) {
89 p = *pp;
90 while ((c = *p++))
91 *q++ = c;
92 *q++ = ' ';
93 a = 1;
95 q -= a;
96 *q = '\0';
98 if (!str[0])
99 syslinux_run_default();
100 else
101 syslinux_run_command(str);
104 #define show_bool(mybool) mybool ? "found":"not found"
106 int main(int argc, char *argv[])
108 char **args[3];
109 int i=0;
110 int n=0;
111 bool hardware_matches = true;
112 bool multicore = false;
113 bool dryrun = false;
114 bool debug = false;
116 s_cpu cpu;
117 console_ansi_raw();
118 detect_cpu(&cpu);
120 /* If no argument got passed, let's show the usage */
121 if (argc == 1) {
122 usage();
123 return -1;
126 for (i = 1; i < argc; i++) {
127 if (!strcmp(argv[i], "--")) {
128 argv[i] = NULL;
129 args[n++] = &argv[i + 1];
130 } else if (!strcmp(argv[i], "64")) {
131 if (debug)
132 printf(" 64bit : %s on this system\n",
133 show_bool(cpu.flags.lm));
134 hardware_matches = cpu.flags.lm && hardware_matches;
135 } else if (!strcmp(argv[i], "pae")) {
136 if (debug)
137 printf(" pae : %s on this system\n",
138 show_bool(cpu.flags.pae));
139 hardware_matches = cpu.flags.pae && hardware_matches;
140 } else if (!strcmp(argv[i], "hvm")) {
141 if (debug)
142 printf(" hvm : %s on this system\n",
143 show_bool((cpu.flags.vmx || cpu.flags.svm)));
144 hardware_matches = (cpu.flags.vmx || cpu.flags.svm)
145 && hardware_matches;
146 } else if (!strcmp(argv[i], "multicore")) {
147 if (debug)
148 printf(" multicore : %d cores on this system\n", cpu.num_cores);
149 if (cpu.num_cores > 1)
150 multicore = true;
151 hardware_matches = multicore && hardware_matches;
152 } else if (!strcmp(argv[i], "smp")) {
153 if (debug)
154 printf(" smp : %s on this system\n", show_bool(cpu.flags.smp));
155 hardware_matches = cpu.flags.smp && hardware_matches;
156 } else if (!strcmp(argv[i], "dry-run")) {
157 dryrun = true;
158 } else if (!strcmp(argv[i], "debug")) {
159 debug = true;
161 if (n >= 2)
162 break;
164 while (n < 2) {
165 args[n] = args[n - 1];
166 n++;
168 if (debug) {
169 printf("\nBooting labels are : '%s' or '%s'\n", *args[0], *args[1]);
170 printf("Hardware requirements%smatch this system, let's booting '%s'\n",
171 hardware_matches ? " " : " doesn't ",
172 hardware_matches ? *args[0] : *args[1]);
173 printf("Sleeping 5sec before booting\n");
174 if (!dryrun)
175 sleep(5000);
178 if (!dryrun)
179 boot_args(hardware_matches ? args[0] : args[1]);
180 else
181 printf("Dry-run mode, let's exiting\n");
183 return -1;