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