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

syslinux: fix ifmem.c
author Pascal Bellard <pascal.bellard@slitaz.org>
date Sat Feb 25 11:52:40 2012 +0100 (2012-02-25)
parents 9597bd89ad48
children 21116dbdd40d
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 uint64_t 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 >> 10;
91 }
93 int main(int argc, char *argv[])
94 {
95 char *s;
96 int i;
97 unsigned long ram_size;
99 openconsole(&dev_null_r, &dev_stdcon_w);
101 if (argc < 4) {
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 ram_size = memory_size();
108 printf("Total memory found %luK.\n",ram_size);
109 ram_size += (1 <<= 10); // add 1M to round boundaries...
111 i = 1;
112 s = argv[1];
113 do {
114 char *p = s;
115 unsigned long scale = 1;
117 while (*p >= '0' && *p <= '9') p++;
118 switch (*p | 0x20) {
119 case 'g': scale <<= 10;
120 case 'm': scale <<= 10;
121 default : *p = 0; break;
122 }
123 i++; // size
124 if (ram_size >= scale * strtoul(s, NULL, 0)) break;
125 s = argv[++i];
126 } while (i + 1 < argc);
128 if (i != argc) syslinux_run_command(argv[i]);
129 else syslinux_run_default();
130 return -1;
131 }