Convert to using a 32-bit installer framework, in preparation for
[profile/ivi/syslinux.git] / memdisk / msetup.c
1 #ident "$Id$"
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  *   Bostom 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 }
72
73 static inline int get_e801(void)
74 {
75   int err;
76   com32sys_t regs;
77
78   memset(&regs, 0, sizeof regs);
79
80   regs.eax.w[0] = 0xe801;
81   syscall(0x15, &regs, &regs);
82
83   if ( !(err = regs.eflags.l & 1) ) {
84     if ( regs.eax.w[0] ) {
85       insertrange(0x100000, (uint64_t)((uint32_t)regs.eax.w[0] << 10), 1);
86     }
87     if ( regs.ebx.w[0] ) {
88       insertrange(0x1000000, (uint64_t)((uint32_t)regs.ebx.w[0] << 16), 1);
89     }
90   }
91
92   return err;
93 }
94
95 static inline int get_88(void)
96 {
97   com32sys_t regs;
98   int err;
99
100   memset(&regs, 0, sizeof regs);
101
102   regs.eax.b[1] = 0x88;
103   syscall(0x15, &regs, &regs);
104
105
106   if ( !(err = regs.eflags.l & 1) ) {
107     if ( regs.eax.w[0] ) {
108       insertrange(0x100000, (uint64_t)((uint32_t)regs.eax.w[0] << 10), 1);
109     }
110   }
111
112   return err;
113 }
114
115 uint32_t dos_mem  = 0;          /* 0-1MB */
116 uint32_t low_mem  = 0;          /* 1-16MB */
117 uint32_t high_mem = 0;          /* 16+ MB */
118
119 void get_mem(void)
120 {
121   if ( get_e820() ) {
122     get_dos_mem();
123     if ( get_e801() ) {
124       if ( get_88() ) {
125         puts("MEMDISK: Unable to obtain memory map\n");
126         die();
127       }
128     }
129   }
130 }
131
132 #define PW(x) (1ULL << (x))
133
134 void parse_mem(void)
135 {
136   struct e820range *ep;
137
138   dos_mem = low_mem = high_mem = 0;
139
140   /* Derive "dos mem", "high mem", and "low mem" from the range array */
141   for ( ep = ranges ; ep->type != -1 ; ep++ ) {
142     if ( ep->type == 1 ) {
143       /* Only look at memory ranges */
144       if ( ep->start == 0 ) {
145         if ( ep[1].start > PW(20) )
146           dos_mem = PW(20);
147         else
148           dos_mem = ep[1].start;
149       }
150       if ( ep->start <= PW(20) && ep[1].start > PW(20) ) {
151         if ( ep[1].start > PW(24) )
152           low_mem = PW(24) - PW(20);
153         else
154           low_mem = ep[1].start - PW(20);
155       }
156       if ( ep->start <= PW(24) && ep[1].start > PW(24) ) {
157         if ( ep[1].start > PW(32) )
158           high_mem = PW(32) - PW(24);
159         else
160           high_mem = ep[1].start - PW(24);
161       }
162     }
163   }
164 }