wok view syslinux/stuff/extra/ifmem.c @ rev 3869
syslinux/ifmem: more generic algorithm for cmdline
author | Pascal Bellard <pascal.bellard@slitaz.org> |
---|---|
date | Sat Aug 08 16:33:21 2009 +0200 (2009-08-08) |
parents | b6b64851e9aa |
children | 773f5d910570 |
line source
1 /* ----------------------------------------------------------------------- *
2 *
3 * Copyright 2009 Pascal Bellard - 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 * ----------------------------------------------------------------------- */
13 /*
14 * ifmem.c
15 *
16 * Run one command if the memory is large enought, and another if it isn't.
17 *
18 * Usage:
19 *
20 * label boot_kernel
21 * kernel ifmem.c
22 * append size_in_KB boot_large [size_in_KB boot_medium] boot_small
23 *
24 * label boot_large
25 * kernel vmlinuz_large_memory
26 * append ...
27 *
28 * label boot_small
29 * kernel vmlinuz_small_memory
30 * append ...
31 */
33 #include <inttypes.h>
34 #include <com32.h>
35 #include <console.h>
36 #include <stdio.h>
37 #include <string.h>
38 #include <alloca.h>
39 #include <stdlib.h>
40 #include <syslinux/boot.h>
42 static unsigned long memory_size(void)
43 {
44 com32sys_t ireg, oreg;
46 memset(&ireg, 0, sizeof ireg);
48 ireg.eax.w[0] = 0xe801;
49 __intcall(0x15, &ireg, &oreg);
51 return oreg.ecx.w[0] + ( oreg.edx.w[0] << 6);
52 }
54 int main(int argc, char *argv[])
55 {
56 char *s;
57 int i, j;
59 for (s = argv[1]; *s && (*s < '0' || *s > '9'); s++);
61 if (argc < 4 || !*s) {
62 openconsole(&dev_null_r, &dev_stdcon_w);
63 perror("\nUsage: ifmem.c32 size_KB boot_large_memory boot_small_memory\n");
64 return 1;
65 }
67 // find target according to ram size
68 for (i = 1; i + 2 < argc; ) {
69 j = i++; // size
70 if (memory_size() >= strtoul(s, NULL, 0)) break;
71 s = argv[++i];
72 }
74 // find and copy extra parameters to command line
75 // assume the command line ends with two words (not number)
76 for (s = argv[i++]; i < argc; i++) {
77 char c = argv[i];
78 if (c >= '0' && c <= '9') j = i;
79 if (i - j > 2 && ++i < argc) {
80 #define SZ 512
81 static char cmdline[SZ];
82 char *p = cmdline, *q = s;
83 int j;
84 for (j = i; j <= argc; j++) {
85 while (*q && p < cmdline + SZ -1) *p++ = *q++;
86 if (p < cmdline + SZ -1) *p++ = ' ';
87 q = argv[j];
88 }
89 *p++ = 0;
90 s = cmdline;
91 }
92 }
94 if (s) syslinux_run_command(s);
95 else syslinux_run_default();
96 return -1;
97 }