Adding upstream version 3.86+dfsg.
[syslinux-debian/hramrach.git] / com32 / modules / ifcpu.c
blob8a9a5f4f2b122a82ae24f3c301b895f0bef3a456
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
16 * Run one command (boot_entry_1) if the booted system match some CPU features
17 * and another (boot_entry_2) if it doesn't.
18 * Eventually this and other features should get folded into some kind
19 * of scripting engine.
21 * Usage:
23 * label test
24 * com32 ifcpu.c32
25 * append <option> <cpu_features> -- boot_entry_1 -- boot_entry_2
26 * label boot_entry_1
27 * kernel vmlinuz
28 * append ...
29 * label boot_entry_2
30 * kernel vmlinuz_64
31 * append ...
33 * options could be :
34 * debug : display some debugging messages
35 * dry-run : just do the detection, don't boot
37 * cpu_features could be:
38 * 64 : CPU have to be x86_64 compatible
39 * hvm : Processor must have hardware virtualization (hvm or svm)
40 * multicore : Processor must be multi-core
41 * smp : System have to be SMP
43 * if you want to match many cpu features, just separate them with a single space
46 #include <alloca.h>
47 #include <stdlib.h>
48 #include <stdio.h>
49 #include <string.h>
50 #include <cpuid.h>
51 #include <syslinux/boot.h>
52 #include <com32.h>
53 #include <consoles.h>
55 #define REG_AH(x) ((x).eax.b[1])
56 #define REG_CX(x) ((x).ecx.w[0])
57 #define REG_DX(x) ((x).edx.w[0])
59 static unsigned char sleep(unsigned int msec)
61 unsigned long micro = 1000 * msec;
62 com32sys_t inreg, outreg;
64 REG_AH(inreg) = 0x86;
65 REG_CX(inreg) = (micro >> 16);
66 REG_DX(inreg) = (micro & 0xFFFF);
67 __intcall(0x15, &inreg, &outreg);
68 return REG_AH(outreg);
71 /* XXX: this really should be librarized */
72 static void boot_args(char **args)
74 int len = 0;
75 char **pp;
76 const char *p;
77 char c, *q, *str;
79 for (pp = args; *pp; pp++)
80 len += strlen(*pp);
82 q = str = alloca(len + 1);
83 for (pp = args; *pp; pp++) {
84 p = *pp;
85 while ((c = *p++))
86 *q++ = c;
88 *q = '\0';
90 if (!str[0])
91 syslinux_run_default();
92 else
93 syslinux_run_command(str);
96 #define show_bool(mybool) mybool ? "found":"not found"
98 int main(int argc, char *argv[])
100 char **args[3];
101 int i;
102 int n;
103 bool hardware_matches = true;
104 bool multicore = false;
105 bool dryrun = false;
106 bool debug = false;
108 s_cpu cpu;
109 console_ansi_raw();
110 detect_cpu(&cpu);
111 n = 0;
112 for (i = 1; i < argc; i++) {
113 if (!strcmp(argv[i], "--")) {
114 argv[i] = NULL;
115 args[n++] = &argv[i + 1];
116 } else if (!strcmp(argv[i], "64")) {
117 if (debug)
118 printf(" 64bit : %s on this system\n",
119 show_bool(cpu.flags.lm));
120 hardware_matches = cpu.flags.lm && hardware_matches;
121 } else if (!strcmp(argv[i], "hvm")) {
122 if (debug)
123 printf(" hvm : %s on this system\n",
124 show_bool((cpu.flags.vmx || cpu.flags.svm)));
125 hardware_matches = (cpu.flags.vmx || cpu.flags.svm)
126 && hardware_matches;
127 } else if (!strcmp(argv[i], "multicore")) {
128 if (debug)
129 printf(" multicore : %d cores on this system\n", cpu.num_cores);
130 if (cpu.num_cores > 1)
131 multicore = true;
132 hardware_matches = multicore && hardware_matches;
133 } else if (!strcmp(argv[i], "smp")) {
134 if (debug)
135 printf(" smp : %s on this system\n", show_bool(cpu.flags.smp));
136 hardware_matches = cpu.flags.smp && hardware_matches;
137 } else if (!strcmp(argv[i], "dry-run")) {
138 dryrun = true;
139 } else if (!strcmp(argv[i], "debug")) {
140 debug = true;
142 if (n >= 2)
143 break;
145 while (n < 2) {
146 args[n] = args[n - 1];
147 n++;
149 if (debug) {
150 printf("\nBooting labels are : '%s' or '%s'\n", *args[0], *args[1]);
151 printf("Hardware requirements%smatch this system, let's booting '%s'\n",
152 hardware_matches ? " " : " doesn't ",
153 hardware_matches ? *args[0] : *args[1]);
154 printf("Sleeping 5sec before booting\n");
155 if (!dryrun)
156 sleep(5000);
159 if (!dryrun)
160 boot_args(hardware_matches ? args[0] : args[1]);
161 else
162 printf("Dry-run mode, let's exiting\n");
164 return -1;