wok view syslinux/stuff/extra/ifmem.c @ rev 3918

syslinux/ifmem: use int15h / 0xe820h
author Pascal Bellard <pascal.bellard@slitaz.org>
date Tue Aug 18 18:45:48 2009 +0200 (2009-08-18)
parents 8c5c15fc1a40
children 044c8848261e
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 struct e820_data {
43 uint64_t base;
44 uint64_t len;
45 uint32_t type;
46 uint32_t extattr;
47 } __attribute__((packed));
49 // Get memory size in Kb
50 static unsigned long memory_size(void)
51 {
52 unsigned long bytes = 0;
53 com32sys_t ireg, oreg;
54 struct e820_data ed;
56 memset(&ireg, 0, sizeof ireg);
58 ireg.eax.w[0] = 0xe820;
59 ireg.edx.l = 0x534d4150;
60 ireg.ecx.l = sizeof(struct e820_data);
61 ireg.edi.w[0] = OFFS(__com32.cs_bounce);
62 ireg.es = SEG(__com32.cs_bounce);
64 memset(&ed, 0, sizeof ed);
65 ed.extattr = 1;
67 do {
68 memcpy(__com32.cs_bounce, &ed, sizeof ed);
70 __intcall(0x15, &ireg, &oreg);
71 if (oreg.eflags.l & EFLAGS_CF ||
72 oreg.eax.l != 0x534d4150 ||
73 oreg.ecx.l < 20)
74 break;
76 memcpy(&ed, __com32.cs_bounce, sizeof ed);
78 if (ed.type == 1)
79 bytes += ed.len;
81 ireg.ebx.l = oreg.ebx.l;
82 } while (ireg.ebx.l);
84 if (!bytes) {
85 memset(&ireg, 0, sizeof ireg);
86 ireg.eax.w[0] = 0x8800;
87 __intcall(0x15, &ireg, &oreg);
88 return ireg.eax.w[0];
89 }
90 return bytes / 1024;
91 }
93 int main(int argc, char *argv[])
94 {
95 char *s;
96 int i, j = 1;
98 for (s = argv[1]; *s && (*s < '0' || *s > '9'); s++);
100 if (argc < 4 || !*s) {
101 openconsole(&dev_null_r, &dev_stdcon_w);
102 perror("\nUsage: ifmem.c32 size_KB boot_large_memory boot_small_memory\n");
103 return 1;
104 }
106 // find target according to ram size
107 for (i = 1; i + 2 < argc; ) {
108 j = i++; // size
109 if (memory_size() >= strtoul(s, NULL, 0)) break;
110 s = argv[++i];
111 }
113 // find and copy extra parameters to command line
114 // assume the command line ends with two words (not number)
115 for (s = argv[i++]; i < argc; i++) {
116 char c = *argv[i];
117 if (c >= '0' && c <= '9') j = i;
118 if (i - j > 2 && i < argc) {
119 #define SZ 512
120 static char cmdline[SZ];
121 char *p = cmdline, *q = s;
122 int j;
123 for (j = i; j <= argc; j++) {
124 while (*q && p < cmdline + SZ -1) *p++ = *q++;
125 if (p < cmdline + SZ -1) *p++ = ' ';
126 q = argv[j];
127 }
128 *p++ = 0;
129 s = cmdline;
130 }
131 }
133 if (s) syslinux_run_command(s);
134 else syslinux_run_default();
135 return -1;
136 }