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