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

syslinux/ifmem.c32: shrink
author Pascal Bellard <pascal.bellard@slitaz.org>
date Tue Apr 03 13:45:02 2012 +0200 (2012-04-03)
parents 21116dbdd40d
children
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 static com32sys_t ireg, oreg;
54 static struct e820_data ed;
56 ireg.eax.w[0] = 0xe820;
57 ireg.edx.l = 0x534d4150;
58 ireg.ecx.l = sizeof(struct e820_data);
59 ireg.edi.w[0] = OFFS(__com32.cs_bounce);
60 ireg.es = SEG(__com32.cs_bounce);
62 ed.extattr = 1;
64 do {
65 memcpy(__com32.cs_bounce, &ed, sizeof ed);
67 __intcall(0x15, &ireg, &oreg);
68 if (oreg.eflags.l & EFLAGS_CF ||
69 oreg.eax.l != 0x534d4150 ||
70 oreg.ecx.l < 20)
71 break;
73 memcpy(&ed, __com32.cs_bounce, sizeof ed);
75 if (ed.type == 1)
76 bytes += ed.len;
78 ireg.ebx.l = oreg.ebx.l;
79 } while (ireg.ebx.l);
81 if (!bytes) {
82 memset(&ireg, 0, sizeof ireg);
83 ireg.eax.w[0] = 0x8800;
84 __intcall(0x15, &ireg, &oreg);
85 return ireg.eax.w[0];
86 }
87 return bytes >> 10;
88 }
90 int main(int argc, char *argv[])
91 {
92 int i;
93 unsigned long ram_size;
95 openconsole(&dev_null_r, &dev_stdcon_w);
97 if (argc < 4) {
98 perror("\nUsage: ifmem.c32 size_KB boot_large_memory boot_small_memory\n");
99 return 1;
100 }
102 // find target according to ram size
103 ram_size = memory_size();
104 printf("Total memory found %luK.\n",ram_size);
105 ram_size += (1 << 10); // add 1M to round boundaries...
107 i = 1;
108 do {
109 char *s = argv[i];
110 char *p = s;
111 unsigned long scale = 1;
113 while (*p >= '0' && *p <= '9') p++;
114 switch (*p | 0x20) {
115 case 'g': scale <<= 10;
116 case 'm': scale <<= 10;
117 default : *p = 0; break;
118 }
119 i++; // seek to label
120 if (ram_size >= scale * strtoul(s, NULL, 0)) break;
121 i++; // next size or default label
122 } while (i + 1 < argc);
124 if (i != argc) syslinux_run_command(argv[i]);
125 else syslinux_run_default();
126 return -1;
127 }