aboutsummaryrefslogtreecommitdiffstats
path: root/com32/mboot/mem.c
blob: e9d8bbdbb4b43ab859ab7e7423340b8af67419b5 (plain)
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/* ----------------------------------------------------------------------- *
 *
 *   Copyright 2007-2009 H. Peter Anvin - All Rights Reserved
 *   Copyright 2009 H. Peter Anvin - All Rights Reserved
 *
 *   Permission is hereby granted, free of charge, to any person
 *   obtaining a copy of this software and associated documentation
 *   files (the "Software"), to deal in the Software without
 *   restriction, including without limitation the rights to use,
 *   copy, modify, merge, publish, distribute, sublicense, and/or
 *   sell copies of the Software, and to permit persons to whom
 *   the Software is furnished to do so, subject to the following
 *   conditions:
 *
 *   The above copyright notice and this permission notice shall
 *   be included in all copies or substantial portions of the Software.
 *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 *   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 *   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 *   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 *   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 *   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 *   OTHER DEALINGS IN THE SOFTWARE.
 *
 * ----------------------------------------------------------------------- */

/*
 * mem.c
 *
 * Obtain a memory map for a Multiboot OS
 *
 * This differs from the libcom32 memory map functions in that it doesn't
 * attempt to filter out memory regions...
 */

#include "mboot.h"
#include <com32.h>

struct e820_entry {
  uint64_t start;
  uint64_t len;
  uint32_t type;
  uint32_t extattr;
};

#define RANGE_ALLOC_BLOCK	128

static int mboot_scan_memory(struct AddrRangeDesc **ardp, uint32_t *dosmem)
{
  com32sys_t ireg, oreg;
  struct e820_entry *e820buf = __com32.cs_bounce;
  struct AddrRangeDesc *ard;
  size_t ard_count, ard_space;

  /* Use INT 12h to get DOS memory */
  __intcall(0x12, &__com32_zero_regs, &oreg);
  *dosmem = oreg.eax.w[0] << 10;
  if (*dosmem < 32*1024 || *dosmem > 640*1024) {
    /* INT 12h reports nonsense... now what? */
    uint16_t ebda_seg = *(uint16_t *)0x40e;
    if (ebda_seg >= 0x8000 && ebda_seg < 0xa000)
      *dosmem = ebda_seg << 4;
    else
      *dosmem = 640*1024;	/* Hope for the best... */
  }

  /* Allocate initial space */
  *ardp = ard = malloc(RANGE_ALLOC_BLOCK * sizeof *ard);
  if (!ard)
    return 0;

  ard_count = 0;
  ard_space = RANGE_ALLOC_BLOCK;

  /* First try INT 15h AX=E820h */
  memset(&ireg, 0, sizeof ireg);
  ireg.eax.l    = 0xe820;
  ireg.edx.l    = 0x534d4150;
  /* ireg.ebx.l    = 0; */
  ireg.ecx.l    = sizeof(*e820buf);
  ireg.es       = SEG(e820buf);
  ireg.edi.w[0] = OFFS(e820buf);
  memset(e820buf, 0, sizeof *e820buf);
  /* Set this in case the BIOS doesn't, but doesn't change %ecx to match. */
  e820buf->extattr = 1;

  do {
    __intcall(0x15, &ireg, &oreg);

    if ((oreg.eflags.l & EFLAGS_CF) ||
	(oreg.eax.l != 0x534d4150) ||
	(oreg.ecx.l < 20))
      break;

    if (oreg.ecx.l < 24)
      e820buf->extattr = 1;	/* Enabled, normal */

    if (!(e820buf->extattr & 1))
      continue;

    if (ard_count >= ard_space) {
      ard_space += RANGE_ALLOC_BLOCK;
      *ardp = ard = realloc(ard, ard_space*sizeof *ard);
      if (!ard)
	return ard_count;
    }

    ard[ard_count].size     = 20;
    ard[ard_count].BaseAddr = e820buf->start;
    ard[ard_count].Length   = e820buf->len;
    ard[ard_count].Type     = e820buf->type;
    ard_count++;

    ireg.ebx.l = oreg.ebx.l;
  } while (oreg.ebx.l);

  if (ard_count)
    return ard_count;

  ard[0].size = 20;
  ard[0].BaseAddr = 0;
  ard[0].Length = *dosmem << 10;
  ard[0].Type = 1;

  /* Next try INT 15h AX=E801h */
  ireg.eax.w[0] = 0xe801;
  __intcall(0x15, &ireg, &oreg);

  if (!(oreg.eflags.l & EFLAGS_CF) && oreg.ecx.w[0]) {
    ard[1].size = 20;
    ard[1].BaseAddr = 1 << 20;
    ard[1].Length = oreg.ecx.w[0] << 10;
    ard[1].Type = 1;

    if (oreg.edx.w[0]) {
      ard[2].size = 20;
      ard[2].BaseAddr = 16 << 20;
      ard[2].Length = oreg.edx.w[0] << 16;
      ard[2].Type = 1;
      return 3;
    } else {
      return 2;
    }
  }

  /* Finally try INT 15h AH=88h */
  ireg.eax.w[0] = 0x8800;
  if (!(oreg.eflags.l & EFLAGS_CF) && oreg.eax.w[0]) {
    ard[1].size = 20;
    ard[1].BaseAddr = 1 << 20;
    ard[1].Length = oreg.ecx.w[0] << 10;
    ard[1].Type = 1;
    return 2;
  }

  return 1;			/* ... problematic ... */
}

void mboot_make_memmap(void)
{
  int i, nmap;
  struct AddrRangeDesc *ard;
  uint32_t lowmem, highmem;
  uint32_t highrsvd;

  /* Always report DOS memory as "lowmem", this may be overly conservative
     (e.g. if we're dropping PXE), but it should be *safe*... */

  nmap = mboot_scan_memory(&ard, &lowmem);

  highmem  = 0x100000;
  highrsvd = 0xfff00000;

 again:
  for (i = 0; i < nmap; i++) {
    uint64_t start, end;

    start = ard[i].BaseAddr;
    end = start + ard[i].Length;

    if (end < start)
      end = ~0ULL;

    if (start & 0xffffffff00000000ULL)
      continue;			/* Not interested in 64-bit memory */

    if (start < highmem)
      start = highmem;

    if (end <= start)
      continue;

    if (ard[i].Type == 1 && start == highmem) {
      highmem = end;
      goto again;
    } else if (ard[i].Type != 1 && start < highrsvd)
      highrsvd = start;
  }

  if (highmem > highrsvd)
    highmem = highrsvd;

  mbinfo.mem_lower = lowmem >> 10;
  mbinfo.mem_upper = (highmem - 0x100000) >> 10;
  mbinfo.flags |= MB_INFO_MEMORY;

  /* The spec says this address should be +4, but Grub disagrees */
  mbinfo.mmap_addr = map_data(ard, nmap*sizeof *ard, 4, false);
  if (mbinfo.mmap_addr) {
    mbinfo.mmap_length = nmap*sizeof *ard;
    mbinfo.flags |= MB_INFO_MEM_MAP;
  }
}