MEMDISK: default to "safeint" mode
[syslinux.git] / memdisk / msetup.c
bloba2067011fa977d5c93072de70acab14f8d4a4153
1 /* ----------------------------------------------------------------------- *
3 * Copyright 2001-2008 H. Peter Anvin - All Rights Reserved
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.
11 * ----------------------------------------------------------------------- */
14 * msetup.c
16 * Initialization code for memory-based disk
19 #include <stdint.h>
20 #ifdef TEST
21 # include <string.h>
22 # include <stdio.h>
23 #else
24 # include "memdisk.h"
25 # include "conio.h"
26 #endif
27 #include "e820.h"
29 uint32_t dos_mem = 0; /* 0-1MB */
30 uint32_t low_mem = 0; /* 1-16MB */
31 uint32_t high_mem = 0; /* 16+ MB */
33 #ifndef TEST
35 static inline int get_e820(void)
37 struct e820_info {
38 uint64_t base;
39 uint64_t len;
40 uint32_t type;
41 uint32_t extattr;
42 } *buf = sys_bounce;
43 uint32_t copied;
44 int range_count = 0;
45 com32sys_t regs;
47 memset(&regs, 0, sizeof regs);
48 memset(buf, 0, sizeof *buf);
49 buf->extattr = 1;
51 do {
52 regs.eax.l = 0x0000e820;
53 regs.ecx.l = sizeof(*buf);
54 regs.edx.l = 0x534d4150;
55 regs.edi.w[0] = OFFS(buf);
56 regs.es = SEG(buf);
58 syscall(0x15, &regs, &regs);
59 copied = (regs.eflags.l & 1) ? 0 : regs.ecx.l;
61 if ( regs.eax.l != 0x534d4150 || copied < 20 )
62 break;
64 if ( copied < 24 )
65 buf->extattr = 1;
67 printf("e820: %08x%08x %08x%08x %d [%x]\n",
68 (uint32_t)(buf->base >> 32), (uint32_t)buf->base,
69 (uint32_t)(buf->len >> 32), (uint32_t)buf->len,
70 buf->type, buf->extattr);
72 if ( !(buf->extattr & 1) )
73 continue; /* Disabled range, just ignore */
75 insertrange(buf->base, buf->len, buf->type, buf->extattr);
76 range_count++;
78 } while ( regs.ebx.l );
80 return !range_count;
83 static inline void get_dos_mem(void)
85 com32sys_t regs;
87 memset(&regs, 0, sizeof regs);
88 syscall(0x12, &regs, &regs);
89 insertrange(0, (uint64_t)((uint32_t)regs.eax.w[0] << 10), 1, 1);
90 printf(" DOS: %d K\n", regs.eax.w[0]);
93 static inline int get_e801(void)
95 int err;
96 com32sys_t regs;
98 memset(&regs, 0, sizeof regs);
100 regs.eax.w[0] = 0xe801;
101 syscall(0x15, &regs, &regs);
103 if ( !(err = regs.eflags.l & 1) ) {
104 if ( regs.eax.w[0] ) {
105 insertrange(0x100000, (uint64_t)((uint32_t)regs.eax.w[0] << 10), 1, 1);
107 if ( regs.ebx.w[0] ) {
108 insertrange(0x1000000, (uint64_t)((uint32_t)regs.ebx.w[0] << 16), 1, 1);
111 printf("e801: %04x %04x\n", regs.eax.w[0], regs.ebx.w[0]);
114 return err;
117 static inline int get_88(void)
119 com32sys_t regs;
120 int err;
122 memset(&regs, 0, sizeof regs);
124 regs.eax.b[1] = 0x88;
125 syscall(0x15, &regs, &regs);
128 if ( !(err = regs.eflags.l & 1) ) {
129 if ( regs.eax.w[0] ) {
130 insertrange(0x100000, (uint64_t)((uint32_t)regs.eax.w[0] << 10), 1, 1);
133 printf(" 88: %04x\n", regs.eax.w[0]);
136 return err;
139 void get_mem(void)
141 if ( get_e820() ) {
142 get_dos_mem();
143 if ( get_e801() ) {
144 if ( get_88() ) {
145 puts("MEMDISK: Unable to obtain memory map\n");
146 die();
152 #endif /* TEST */
154 #define PW(x) (1ULL << (x))
156 void parse_mem(void)
158 struct e820range *ep;
160 dos_mem = low_mem = high_mem = 0;
162 /* Derive "dos mem", "high mem", and "low mem" from the range array */
163 for ( ep = ranges ; ep->type != -1U ; ep++ ) {
164 if ( ep->type == 1 ) {
165 /* Only look at memory ranges */
166 if ( ep->start == 0 ) {
167 if ( ep[1].start > PW(20) )
168 dos_mem = PW(20);
169 else
170 dos_mem = ep[1].start;
172 if ( ep->start <= PW(20) && ep[1].start > PW(20) ) {
173 if ( ep[1].start > PW(24) )
174 low_mem = PW(24) - PW(20);
175 else
176 low_mem = ep[1].start - PW(20);
178 if ( ep->start <= PW(24) && ep[1].start > PW(24) ) {
179 if ( ep[1].start > PW(32) )
180 high_mem = PW(32) - PW(24);
181 else
182 high_mem = ep[1].start - PW(24);