Page 1 of 2

GRUB: Multiboot Information Structure ?

Posted: Mon Nov 24, 2003 1:38 am
by Perica
..

Re:GRUB: Multiboot Information Structure ?

Posted: Mon Nov 24, 2003 2:27 am
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));
}

Re:GRUB: Multiboot Information Structure ?

Posted: Mon Nov 24, 2003 2:36 am
by Perica
..

Re:GRUB: Multiboot Information Structure ?

Posted: Mon Nov 24, 2003 12:12 pm
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.

Re:GRUB: Multiboot Information Structure ?

Posted: Tue Nov 25, 2003 12:43 am
by Perica
..

Re:GRUB: Multiboot Information Structure ?

Posted: Tue Nov 25, 2003 1:55 am
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

Re:GRUB: Multiboot Information Structure ?

Posted: Tue Nov 25, 2003 5:52 am
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.

Re:GRUB: Multiboot Information Structure ?

Posted: Thu Nov 27, 2003 3:36 am
by Perica
..

Re:GRUB: Multiboot Information Structure ?

Posted: Thu Nov 27, 2003 6:29 am
by Tim
The GRUB download package has a sample kernel, in the docs directory, which does just that.

Re:GRUB: Multiboot Information Structure ?

Posted: Thu Nov 27, 2003 8:36 am
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

Re:GRUB: Multiboot Information Structure ?

Posted: Fri Nov 28, 2003 7:31 am
by Perica
..

Re:GRUB: Multiboot Information Structure ?

Posted: Fri Nov 28, 2003 8:09 am
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

Re:GRUB: Multiboot Information Structure ?

Posted: Fri Nov 28, 2003 8:19 am
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.

Re:GRUB: Multiboot Information Structure ?

Posted: Fri Nov 28, 2003 8:59 am
by Pype.Clicker
perica: would you mind posting that in the ".:QuickLinkz:." thread aswell ?

Re:GRUB: Multiboot Information Structure ?

Posted: Fri Nov 28, 2003 12:07 pm
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!