I've been trying to fetch a memory map with E820, but I am doing something wrong I guess.
I have a C structure based on how E820 entries are supposed to be, and E820 runs successfully, but I end up getting no entries for some reason.
Here are the relevant codes:
Code: Select all
global do_e820
do_e820:
pushad
xor ax,ax
;mov ax, 0x0000
mov es,ax
xor ebp, ebp ;/* entry number */
xor ebx, ebx ; /* ebx must be 0 to start */
mov edi, 0xA000 ; /* The E820 entries are supposed to be saved in es:edi, in our case 0x0A000. I made several attempts with different addresses. */
.loopy_e820:
mov edx, 0x0534D4150 ;/* magical number: "SMAP" */
mov eax, 0xe820
mov ecx, 24
int 0x15
jc short .end_e820
cmp eax, edx
jne short .fail_e820 ;/* eax != SMAP for error */
jcxz .next_e820 ;/* ignore 0-length entry */
inc ebp ;/* increase entry number */
add edi, 24 ;/* edi points next entry space */
.next_e820:
test ebx, ebx ;/* if ebx equals 0, list ends */
je short .end_e820
mov eax, ebp
cmp eax, 128
je short .fail_e820 ;/* entry number >= E820_MAX for error */
jmp short .loopy_e820
.end_e820:
cmp eax, 0x01 ;/* entry number <= 1 for error */
je short .fail_e820
xor eax, eax
mov [ 0x00000dfd0], ebp ; /* The total number of entries. Always 6 */
xor ebp,ebp
popad
ret
.fail_e820:
stc
jmp .fail_e820
Code: Select all
void boot_memory_init()
{
int address = 0x00000dfd0;
int* a = (int*)address;
int content = *a;
printlnVGA(itoa(content, ' ', 10)); //Prints the content of 0x00000dfd0 as decimal. Always 6.
struct boot_param *myboot_param = (struct boot_param *)(E820_ADDRESS); //The address of es:edi. I tried with edi too, just in case.
struct e820_entry *entry = myboot_param->memMapp;
unsigned long i, npage, num = myboot_param->e820_num;
printlnVGA(itoa(num, ' d', 10)); //Number of entries. Always 0.
unsigned long memory_end = 0;
printlnVGA("Memory map: ");
for (i = 0; i < num; i++) {
if (entry[i].type != E820_TYPE_FREE)
continue;
if (entry[i].addr + entry[i].length > memory_end){
memory_end = entry[i].addr + entry[i].length;}
}
npage = memory_end / PGSIZE; /* memory pages */
printlnVGA(itoa(npage * (4 / 1024), ' ', 10)); /* A function turning converting numbers to chars, once one gives the base (decimal in our case). Supposed to show available memory in MBs. Always 0. */
return;
}
typedef struct e820_entry {
unsigned int addr;
unsigned int addr_high;
unsigned int length;
unsigned int length_high;
unsigned int type;
unsigned int pad;
};
struct boot_param {
struct e820_entry memMapp[128];
unsigned long e820_num;
};
I know I am doing something wrong, but after many hours of googling as well as retarded trial and error , I think I've hit a dead end.
This may seem n00bish to most of you, but I am just a fullstack dev who tries to do some low-level programming at the weekends.
Thank you in advance!
PS: The only thing that was wrong was me going on to make this without much knowledge of C.
It just needed a '&' in this line: '
unsigned long i, npage, num = myboot_param->e820_num; ------> unsigned long i, npage, num = &myboot_param->e820_num