syslinux-3.08-2 sources from FC4
[bootcd.git] / syslinux / memdisk / msetup.c
1 #ident "$Id: msetup.c,v 1.12 2004/12/14 22:46:25 hpa Exp $"
2 /* ----------------------------------------------------------------------- *
3  *   
4  *   Copyright 2001-2003 H. Peter Anvin - All Rights Reserved
5  *
6  *   This program is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation, Inc., 53 Temple Place Ste 330,
9  *   Boston MA 02111-1307, USA; either version 2 of the License, or
10  *   (at your option) any later version; incorporated herein by reference.
11  *
12  * ----------------------------------------------------------------------- */
13
14 /*
15  * msetup.c
16  *
17  * Initialization code for memory-based disk
18  */
19
20 #include <stdint.h>
21 #include "memdisk.h"
22 #include "conio.h"
23 #include "e820.h"
24
25 static inline int get_e820(void)
26 {
27   struct e820_info {
28     uint64_t base;
29     uint64_t len;
30     uint32_t type;
31   } *buf = sys_bounce;
32   uint32_t copied;
33   int range_count = 0;
34   com32sys_t regs;
35
36   memset(&regs, 0, sizeof regs);
37
38   do {
39     regs.eax.l = 0x0000e820;
40     regs.ecx.l = sizeof(*buf);
41     regs.edx.l = 0x534d4150;
42     regs.edi.w[0] = OFFS(buf);
43     regs.es = SEG(buf);
44   
45     syscall(0x15, &regs, &regs);
46     copied = (regs.eflags.l & 1) ? 0 : regs.ecx.l;
47     
48     if ( regs.eax.l != 0x534d4150 || copied < 20 )
49       break;
50     
51     printf("e820: %08x%08x %08x%08x %d\n",
52            (uint32_t)(buf->base >> 32), (uint32_t)buf->base,
53            (uint32_t)(buf->len >> 32), (uint32_t)buf->len,
54            buf->type);
55
56     insertrange(buf->base, buf->len, buf->type);
57     range_count++;
58
59   } while ( regs.ebx.l );
60
61   return !range_count;
62 }
63
64 static inline void get_dos_mem(void)
65 {
66   com32sys_t regs;
67
68   memset(&regs, 0, sizeof regs);
69   syscall(0x12, &regs, &regs);
70   insertrange(0, (uint64_t)((uint32_t)regs.eax.w[0] << 10), 1);
71   printf(" DOS: %d K\n", regs.eax.w[0]);
72 }
73
74 static inline int get_e801(void)
75 {
76   int err;
77   com32sys_t regs;
78
79   memset(&regs, 0, sizeof regs);
80
81   regs.eax.w[0] = 0xe801;
82   syscall(0x15, &regs, &regs);
83
84   if ( !(err = regs.eflags.l & 1) ) {
85     if ( regs.eax.w[0] ) {
86       insertrange(0x100000, (uint64_t)((uint32_t)regs.eax.w[0] << 10), 1);
87     }
88     if ( regs.ebx.w[0] ) {
89       insertrange(0x1000000, (uint64_t)((uint32_t)regs.ebx.w[0] << 16), 1);
90     }
91
92     printf("e801: %04x %04x\n", regs.eax.w[0], regs.ebx.w[0]);
93   }
94
95   return err;
96 }
97
98 static inline int get_88(void)
99 {
100   com32sys_t regs;
101   int err;
102
103   memset(&regs, 0, sizeof regs);
104
105   regs.eax.b[1] = 0x88;
106   syscall(0x15, &regs, &regs);
107
108
109   if ( !(err = regs.eflags.l & 1) ) {
110     if ( regs.eax.w[0] ) {
111       insertrange(0x100000, (uint64_t)((uint32_t)regs.eax.w[0] << 10), 1);
112     }
113
114     printf("  88: %04x\n", regs.eax.w[0]);
115   }
116
117   return err;
118 }
119
120 uint32_t dos_mem  = 0;          /* 0-1MB */
121 uint32_t low_mem  = 0;          /* 1-16MB */
122 uint32_t high_mem = 0;          /* 16+ MB */
123
124 void get_mem(void)
125 {
126   if ( get_e820() ) {
127     get_dos_mem();
128     if ( get_e801() ) {
129       if ( get_88() ) {
130         puts("MEMDISK: Unable to obtain memory map\n");
131         die();
132       }
133     }
134   }
135 }
136
137 #define PW(x) (1ULL << (x))
138
139 void parse_mem(void)
140 {
141   struct e820range *ep;
142
143   dos_mem = low_mem = high_mem = 0;
144
145   /* Derive "dos mem", "high mem", and "low mem" from the range array */
146   for ( ep = ranges ; ep->type != -1 ; ep++ ) {
147     if ( ep->type == 1 ) {
148       /* Only look at memory ranges */
149       if ( ep->start == 0 ) {
150         if ( ep[1].start > PW(20) )
151           dos_mem = PW(20);
152         else
153           dos_mem = ep[1].start;
154       }
155       if ( ep->start <= PW(20) && ep[1].start > PW(20) ) {
156         if ( ep[1].start > PW(24) )
157           low_mem = PW(24) - PW(20);
158         else
159           low_mem = ep[1].start - PW(20);
160       }
161       if ( ep->start <= PW(24) && ep[1].start > PW(24) ) {
162         if ( ep[1].start > PW(32) )
163           high_mem = PW(32) - PW(24);
164         else
165           high_mem = ep[1].start - PW(24);
166       }
167     }
168   }
169 }