]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - wimboot/wimboot-2.7.3/src/memmap.c
1.1.07 release
[Ventoy.git] / wimboot / wimboot-2.7.3 / src / memmap.c
1 /*
2 * Copyright (C) 2021 Michael Brown <mbrown@fensystems.co.uk>.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
18 */
19
20 /**
21 * @file
22 *
23 * Memory map
24 *
25 */
26
27 #include <stddef.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include "wimboot.h"
31 #include "memmap.h"
32
33 /** Buffer for INT 15,e820 calls */
34 static struct e820_entry e820_buf __attribute__ (( section ( ".bss16" ) ));
35
36 /** Continuation value for next INT 15,e820 call */
37 static uint32_t e820_ebx;
38
39 /**
40 * Get system memory map entry
41 *
42 * @v prev Previous system memory map entry, or NULL at start
43 * @v next Next system memory map entry, or NULL at end
44 */
45 struct e820_entry * memmap_next ( struct e820_entry *prev ) {
46 struct bootapp_callback_params params;
47
48 /* Reset buffer and continuation value if restarting */
49 if ( ! prev ) {
50 memset ( &e820_buf, 0, sizeof ( e820_buf ) );
51 e820_ebx = 0;
52 } else if ( e820_ebx == 0 ) {
53 /* Reach the end */
54 return NULL;
55 }
56
57 /* Read system memory map */
58 memset ( &params, 0, sizeof ( params ) );
59 do {
60
61 /* Call INT 15,e820 */
62 params.vector.interrupt = 0x15;
63 params.eax = 0xe820;
64 params.ebx = e820_ebx;
65 params.ecx = sizeof ( e820_buf );
66 params.edx = E820_SMAP;
67 params.es = BASE_SEG;
68 params.edi = ( ( ( void * ) &e820_buf ) -
69 ( ( void * ) BASE_ADDRESS ) );
70 call_interrupt ( &params );
71
72 /* Record continuation value */
73 e820_ebx = params.ebx;
74
75 /* Check result */
76 if ( params.eflags & CF ) {
77 DBG ( "INT 15,e820 failed: error %02x\n", params.ah );
78 break;
79 }
80 if ( params.eax != E820_SMAP ) {
81 DBG ( "INT 15,e820 invalid SMAP signature %08x\n",
82 params.eax );
83 break;
84 }
85 DBG2 ( "INT 15,e820 region [%llx,%llx) type %d\n",
86 e820_buf.start, ( e820_buf.start + e820_buf.len ),
87 e820_buf.type );
88
89 /* Skip non-RAM regions */
90 if ( e820_buf.type != E820_TYPE_RAM )
91 continue;
92 if ( params.ecx > offsetof ( typeof ( e820_buf ), attrs ) ) {
93 if ( ! ( e820_buf.attrs & E820_ATTR_ENABLED ) )
94 continue;
95 if ( e820_buf.attrs & E820_ATTR_NONVOLATILE )
96 continue;
97 }
98
99 /* Return this region */
100 return &e820_buf;
101
102 } while ( e820_ebx != 0 );
103
104 return NULL;
105 }