GRUB: Multiboot Information Structure ?

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.
Perica
Member
Member
Posts: 454
Joined: Sat Nov 25, 2006 12:50 am

GRUB: Multiboot Information Structure ?

Post by Perica »

..
Last edited by Perica on Sun Dec 03, 2006 9:23 pm, edited 1 time in total.
User avatar
df
Member
Member
Posts: 1076
Joined: Fri Oct 22, 2004 11:00 pm
Contact:

Re:GRUB: Multiboot Information Structure ?

Post by df »

unless the multiboot header changed, mine is way way differernt to your structure.

also adding "} __attribute__((packed)) ;" to the end of the struct definition WONT WORK.

either have the attribute on EVERY line ni the struct, or use #pragma pack(1) at the top of your structure and #pragma pack() underneath it.

try this test and see what results you get...

Code: Select all

#include <stdio.h>
#include <stdlib.h>

typedef struct A
{
   char a;
   int b;
   char c;
} A;

typedef struct B
{
   char a __attribute__ ((packed));
   int b __attribute__ ((packed));
   char c __attribute__ ((packed));
} B;

typedef struct C
{
   char a;
   int b;
   char c;
} C __attribute__ ((packed));

#pragma pack(1)
typedef struct D
{
   char a;
   int b;
   char c;
} D;
#pragma pack()

int main(int argc, char *agrv[])
{
   printf("A=%i\n", sizeof(A));
   printf("B=%i\n", sizeof(B));
   printf("C=%i\n", sizeof(C));
   printf("D=%i\n", sizeof(D));
}
-- Stu --
Perica
Member
Member
Posts: 454
Joined: Sat Nov 25, 2006 12:50 am

Re:GRUB: Multiboot Information Structure ?

Post by Perica »

..
Last edited by Perica on Sun Dec 03, 2006 9:23 pm, edited 1 time in total.
User avatar
df
Member
Member
Posts: 1076
Joined: Fri Oct 22, 2004 11:00 pm
Contact:

Re:GRUB: Multiboot Information Structure ?

Post by df »

why wouldnt it work? dunno but i can tell you it doesnt work.

as for the MIS, my cvs last night of grub reads

Code: Select all

/* The Multiboot information.  */
typedef struct multiboot_info
{
  unsigned long flags;
  unsigned long mem_lower;
  unsigned long mem_upper;
  unsigned long boot_device;
  unsigned long cmdline;
  unsigned long mods_count;
  unsigned long mods_addr;
  union
  {
    aout_symbol_table_t aout_sym;
    elf_section_header_table_t elf_sec;
  } u;
  unsigned long mmap_length;
  unsigned long mmap_addr;
} multiboot_info_t;
also, if your curious, here is the output of the test code i posted.

Code: Select all


sgeorge@yakumo ~/source
$ gcc -O2 structs.c -o structs.exe

sgeorge@yakumo ~/source
$ gcc -v
Reading specs from /usr/lib/gcc-lib/i686-pc-cygwin/3.3.1/specs
Configured with: /GCC/gcc-3.3.1-3/configure --with-gcc --with-gnu-ld --with-gnu-
as --prefix=/usr --exec-prefix=/usr --sysconfdir=/etc --libdir=/usr/lib --libexe
cdir=/usr/sbin --mandir=/usr/share/man --infodir=/usr/share/info --enable-langua
ges=c,ada,c++,f77,pascal,java,objc --enable-libgcj --enable-threads=posix --with
-system-zlib --enable-nls --without-included-gettext --enable-interpreter --enab
le-sjlj-exceptions --disable-version-specific-runtime-libs --enable-shared --dis
able-win32-registry --enable-java-gc=boehm --disable-hash-synchronization --verb
ose --target=i686-pc-cygwin --host=i686-pc-cygwin --build=i686-pc-cygwin
Thread model: posix
gcc version 3.3.1 (cygming special)

sgeorge@yakumo ~/source
$ ./structs
A=12
B=6
C=12
D=6
clearly, B and D work, A is unpacked and C doesnt work.
-- Stu --
Perica
Member
Member
Posts: 454
Joined: Sat Nov 25, 2006 12:50 am

Re:GRUB: Multiboot Information Structure ?

Post by Perica »

..
Last edited by Perica on Sun Dec 03, 2006 9:23 pm, edited 1 time in total.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:GRUB: Multiboot Information Structure ?

Post by Solar »

Perica wrote: Also, what is a union ??
That one's for me... ;D

A union is a special datatype, usually used in system programming only because it's error prone. Consider:

Code: Select all

union x
{
    double d;
    int i;
}
If x were a struct, that would mean that sizeof(x) >= sizeof(double) + sizeof(int), because each x would hold both a double and an int.

But x isn't a struct, it's a union, and that means that sizeof(x) == sizeof(double).

Huh?

Well, x holds either a double or an int, depending on what you last wrote to it. Therefore, the size of a union is the largest of its elements, and at any given time, it holds only one element.

The elements "share" the same memory.

unions are very popular among those delighting in truly ugly code, like this:

Code: Select all

union y
{
    int i[2];
    double d;
}
Then they write something into y.d, and read it out using y.i[[0] and y.i[[1] to get to the bit pattern...

:puke:

Well, that's a union for you. I hope the explanation was understandable. :D
Every good solution is obvious once you've found it.
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:GRUB: Multiboot Information Structure ?

Post by Pype.Clicker »

unions were often used in binary file reading because you get a "record" and it could either be person_record or enterprise_record, then you encode the stuff as

Code: Select all

   union record {
       struct person_record person;
       struct enterprise_record enterprise;
   };
If you look at COFF or ELF header files, you'll see they're full of unions.
Perica
Member
Member
Posts: 454
Joined: Sat Nov 25, 2006 12:50 am

Re:GRUB: Multiboot Information Structure ?

Post by Perica »

..
Last edited by Perica on Sun Dec 03, 2006 9:24 pm, edited 1 time in total.
Tim

Re:GRUB: Multiboot Information Structure ?

Post by Tim »

The GRUB download package has a sample kernel, in the docs directory, which does just that.
User avatar
df
Member
Member
Posts: 1076
Joined: Fri Oct 22, 2004 11:00 pm
Contact:

Re:GRUB: Multiboot Information Structure ?

Post by df »

Perica wrote: Thanks for the explanation on unions, i guess you learn something new every day.

Anyway, does anybody have anything to say about putting __attribute__((packed)) after a structure definition being legal or not??

Also, does anybody see anything wrong with my Multiboot Information Structure..... because i don't know what i'm doing wrong, i keep getting faulty values ??? Does anybody know of a kernel that boots using GRUB and sucessfully extracts values from the Multiboot Information Structure?? (I would like to see how other people are doing this and maybe figure out what i'm doing wrong).
well for one thing, test the code i pasted at the top of this thread and see what results you get back. that will etll you if the struct packing works on your gcc or not.

and second, open up the docs/multiboot.h file and look for the header struct, you will find the correct multiboot_info header in there
-- Stu --
Perica
Member
Member
Posts: 454
Joined: Sat Nov 25, 2006 12:50 am

Re:GRUB: Multiboot Information Structure ?

Post by Perica »

..
Last edited by Perica on Sun Dec 03, 2006 9:24 pm, edited 1 time in total.
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:GRUB: Multiboot Information Structure ?

Post by Pype.Clicker »

Perica wrote: i don't regret starting off OS development with writing a boot loader at all, it's taught me alot of low-level things that are necessary for kernel development. Later on i plan on writing a boot loader, but i first need something worth booting ;)
That's speaking wisely! we should put that between <h1> tags on the "post a new thread for unregistered users" form ;-p
Tim

Re:GRUB: Multiboot Information Structure ?

Post by Tim »

Perica, you've summed up the arguments to and for bootloaders better than anyone I've seen. I think that paragraph should be mandatory advice for anyone starting in OS development.
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:GRUB: Multiboot Information Structure ?

Post by Pype.Clicker »

perica: would you mind posting that in the ".:QuickLinkz:." thread aswell ?
User avatar
df
Member
Member
Posts: 1076
Joined: Fri Oct 22, 2004 11:00 pm
Contact:

Re:GRUB: Multiboot Information Structure ?

Post by df »

Perica wrote: I tested (under MinGW, i'm not sure if it works on all distributions of GCC..... but it should) if putting __attribute__((__packed__)) after a structure definition truly worked, and it does; just like the manuals have stated.
hmm what version of GCC is that? i had 3.3.1 and it didnt work..
I visited the GRUB CVS respository and had a look at the example kernel and multiboot structures and i've figured out what i was doing wrong. I didn't insert one field in the multiboot information strucutre (a field before the mmap_* fields), so when acessing the mmap_* variables.... i've been reading the the values of something else.
my cvs last night header file still does not have all those extra fields.. so i still dont know where you got that from...

multiboot.h

none of the structs in there match yours.

(aah i found it in stage2, not the docs directory.. shame on grub for having two differnet versions!!)

mb_info.h

my bad!
-- Stu --
Post Reply