Parsing Memory Card, which gave GRUB.

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
kailot2
Posts: 6
Joined: Fri Oct 02, 2015 1:18 am

Parsing Memory Card, which gave GRUB.

Post by kailot2 »

Hello, I for the first time on your forum, and I have a terrible English =)
I want to know why my code does not work, which must be considered a common memory map provided by GRUB

Code: Select all


typedef struct 
{
	udword size;
	uqword base_addr;
	uqword lengthl;
	udword type;
} MMAP_UNIT;
MMAP_UNIT *mmap_unit;
udword k_mem_size;
int k_mem_init (MI_INF_STRUCT *MI) //MI - pointer to MultiBoot Information structure 
{
	mmap_unit =  MI->mmap_addr;
	while (mmap_unit->type)
	{
		k_mem_size += ((mmap_unit->lengthl)-(mmap_unit->base_addr)) ;
		mmap_unit += ((mmap_unit->size) + 4);		
	}

}
Icee
Member
Member
Posts: 100
Joined: Wed Jan 08, 2014 8:41 am
Location: Moscow, Russia

Re: Parsing Memory Card, which gave GRUB.

Post by Icee »

Because pointer arithmetic. Consult a C book. Also, you are missing an __attribute__((packed)). Also, the expression "(mmap_unit->lengthl)-(mmap_unit->base_addr)" is not what you wanted there.
kailot2
Posts: 6
Joined: Fri Oct 02, 2015 1:18 am

Re: Parsing Memory Card, which gave GRUB.

Post by kailot2 »

I have realized my mistake, I do not have programming experience in C, before I wrote in asm and affect habits. the size of the page table entry is not necessarily equal to 20 bytes, so I can not understand how I should increase the value of the pointer
kailot2
Posts: 6
Joined: Fri Oct 02, 2015 1:18 am

Re: Parsing Memory Card, which gave GRUB.

Post by kailot2 »

Icee wrote: Also, you are missing an __attribute__((packed)).
can you more about this?
Icee
Member
Member
Posts: 100
Joined: Wed Jan 08, 2014 8:41 am
Location: Moscow, Russia

Re: Parsing Memory Card, which gave GRUB.

Post by Icee »

kailot2 wrote:I can not understand how I should increase the value of the pointer
Something like this:

Code: Select all

ptr = (const memmap_item *) (((uintptr_t) ptr) + ptr->size + 4);
kailot2 wrote:
Icee wrote: Also, you are missing an __attribute__((packed)).
can you more about this?
By default, fields are aligned so that accesses are efficient (and on some platforms misaligned access is not available at all). If you come from x86 assembly background, consider MOVDQU vs MOVDQA. In your case, there must be no extra padding between the fields, so an attribute is necessary to indicate that to the compiler:

Code: Select all

struct MyStruct { ... } __attribute__((packed));
kailot2
Posts: 6
Joined: Fri Oct 02, 2015 1:18 am

Re: Parsing Memory Card, which gave GRUB.

Post by kailot2 »

Thanks for answer,i will understand =)
Post Reply