GRUB on USB-Stick

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
zack

GRUB on USB-Stick

Post by zack »

Hello Peoples

Long times ago i coded PIRAT: http://www.domae.ch
Now, i will try to code it from scratch new.
Actually i'll make it Grub compatible and i'll put it on my usb stick.

Did anybody know how i can install grub on a usb stick?

thx a lot.
and sorry for my english.
greez
zack

Re:GRUB on USB-Stick

Post by zack »

if i try the command on my debian linux "grub-install /dev/sda" this error message apears:

/dev/sda does not have any corresponding BIOS drive?

what this means?

thx
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 on USB-Stick

Post by Pype.Clicker »

during its setup, GRUB only has access to BIOS interrupts to load files. That means even if you ask it to run on CDROM, it doesn't do ATAPI stuff directly: it uses INT13h with the device number that corresponds to the floppy-emulation of the CDROM.

You need a similar number to use your USB stick as a boot device for GRUB. Now, maybe later release of GRUB do include OCHI/UHCI driver and USB-storage support, but quite frankly i doubt of it. My guess would be that you'll have to check first that your BIOS can offer INT13h on USB devices before you try to install GRUB on it.
zack

Re:GRUB on USB-Stick

Post by zack »

thx.
i found a tutorial:
i first must partitionate it and set the bootflag. then i copied all *stage* files from /boot/grub to /mnt/usb/boot/grub then i made i device.map with "(hd0) /dev/sda" and the grub-install works. :D

do you actually know a good tutorial for a multiboot grub kernel. ?

thx alot
bluecode

Re:GRUB on USB-Stick

Post by bluecode »

I think the multiboot specification should suffice...
distantvoices
Member
Member
Posts: 1600
Joined: Wed Oct 18, 2006 11:59 am
Location: Vienna/Austria
Contact:

Re:GRUB on USB-Stick

Post by distantvoices »

There's a multiboot kernel example in our OSFAQ iirc.
... the osdever formerly known as beyond infinity ...
BlueillusionOS iso image
zack

Re:GRUB on USB-Stick

Post by zack »

well, i wrote now a little test "kernel":

Code: Select all

[bits 32]
[global entry]

entry:

MULTIBOOT_PAGE_ALIGN   equ 1<<0
MULTIBOOT_MEMORY_INFO  equ 1<<1
MULTIBOOT_HEADER_MAGIC equ 0x1BADB002
MULTIBOOT_HEADER_FLAGS equ MULTIBOOT_PAGE_ALIGN | MULTIBOOT_MEMORY_INFO
CHECKSUM equ -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS)

align 4
mboot:
   dd MULTIBOOT_HEADER_MAGIC
   dd MULTIBOOT_HEADER_FLAGS
   dd CHECKSUM
   dd MULTIBOOT_HEADER_MAGIC
   dd entry
   dd 0
   dd 0
   dd start

start:
   mov    ax, 0x08
   mov      ds, ax

   mov byte [ds:0B8000h], 'P'
   mov byte [ds:0B8001h], 8Fh
   mov byte [ds:0B8002h], 'I'
   mov byte [ds:0B8003h], 8Fh
   mov byte [ds:0B8004h], 'R'
   mov byte [ds:0B8005h], 8Fh
   mov byte [ds:0B8006h], 'A'
   mov byte [ds:0B8007h], 8Fh
   mov byte [ds:0B8008h], 'T'
   mov byte [ds:0B8009h], 8Fh




   .stop:
      jmp .stop
when i compile it with:

Code: Select all

nasm -f elf -o pkernel pkernel.asm
and try to boot it, it doesent works. grub says:

Code: Select all

Invalid or unsupported executable format.
and when i try it together with a linkscript:

Code: Select all

OUTPUT_FORMAT("elf32-i386")
        ENTRY(entry)
        virt = 0xC0000000; /* 3 gig */
        phys = 0x100000; /* 1 meg */
        SECTIONS
        {   .text virt : AT(phys)
            {   code = .;
                *(.text)
                . = ALIGN(4096); }
            .data :  AT(phys + (data - code))
            {   data = .;
                *(.data)
                . = ALIGN(4096); }
            .bss :  AT(phys + (bss - code))
            {   bss = .;
                *(.bss)
                *(COMMON)
                . = ALIGN(4096); }
            end = .; }
and a nothing-doing-c-file the error doesn't appear and it works sometimes... but sometimes it don't works. why this?

ps: grub works good. the splashscreen works.

thx
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 on USB-Stick

Post by Pype.Clicker »

See higher-half BareBones, it might help.

You also have a common trouble and their solution in the "BareBones" page of the FAQ.
zack

Re:GRUB on USB-Stick

Post by zack »

i solved this by copying the kernel at 1 mb.
however all works except when i try to put out e string.
i think the linkscript or the makescript is wrong. what could it be?

Linkscript:

Code: Select all

OUTPUT_FORMAT("elf32-i386")
ENTRY(start)
phys = 0x00100000;
SECTIONS
{
  .text 0x00100000 : {
   . = ALIGN(4096);
    code = .;
    *(.text)
    
  }


  .data : {
   . = ALIGN(4096);
    data = .;
    *(.data)
    
  }

  .bss : {
   . = ALIGN(4096);
    bss = .;
    *(.bss)
    
  }
 
  
   .rodata : {
         . = ALIGN(4096);
         rodata = .;
        *(.rodata)
         
    }
 end = .;
}
Makescript:

Code: Select all

SRC     =       pkernel.c
OBJ     =       pkernelasm.o pkernel.o
LD_OPTIONS=-T linksrc.ld -Map Mapfile.txt -o pkernel
all:   as c link clean
as:
   nasm -f aout -o pkernelasm.o pkernel.asm

c:
   gcc -std=c99 -ffreestanding -fno-builtin -nostdinc -nostdlib -nostartfiles -nodefaultlibs -c $(SRC) -I ../include

link:
   ld $(LD_OPTIONS) $(OBJ)

clean: 
   rm *.o 
distantvoices
Member
Member
Posts: 1600
Joined: Wed Oct 18, 2006 11:59 am
Location: Vienna/Austria
Contact:

Re:GRUB on USB-Stick

Post by distantvoices »

shouldn't rodata be inside the declaration for data? (in the linker script)
... the osdever formerly known as beyond infinity ...
BlueillusionOS iso image
zack

Re:GRUB on USB-Stick

Post by zack »

tried it...
resulted linkscript:

Code: Select all

OUTPUT_FORMAT("elf32-i386")
ENTRY(start)
phys = 0x00100000;
SECTIONS
{
  .text 0x00100000 : {
   . = ALIGN(4096);
    code = .;
    *(.text)
   
  }


  .data : {
   . = ALIGN(4096);
    data = .;
    *(.data)
    *(.rodata)
   
  }

  .bss : {
   . = ALIGN(4096);
    bss = .;
    *(.bss)
   
  }
end = .;
}
now the grub says again:
Invalid or unsupported executable format... hm

thx
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:GRUB on USB-Stick

Post by Solar »

Check the BareBones tutorials Pype linked a couple of posts above. Those are "known good" implementations which you can build upon. (I'm not sure if it makes a difference, but they put .rodata into .text.)
Every good solution is obvious once you've found it.
distantvoices
Member
Member
Posts: 1600
Joined: Wed Oct 18, 2006 11:59 am
Location: Vienna/Austria
Contact:

Re:GRUB on USB-Stick

Post by distantvoices »

Ah, that's maybe the way it's to be done. I just didn't remember correctly. Well ... should rather look up stuff ere posting something fallible in a hurry between two tasks.
... the osdever formerly known as beyond infinity ...
BlueillusionOS iso image
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 on USB-Stick

Post by Pype.Clicker »

You've been through the BareBones page, right? so you've read
I still get Error 13: Invalid or unsupported executable format from GRUB. What's going on ?

Chances are the multiboot header is missing from the final executable.

If you are using some other format than ELF (such as PE), you should specify the AOUT kludge in the multiboot header. The mbchk program (coming with GRUB) and "objdump -h" should give you more hints about what is going on.

It may also happen if you use an ELF object file instead of an executable (e.g. you have an ELF file with unresolved symbols or unfixable relocations). Try to link your ELF file to a binary executable to get more accurate error messages.
and you're pretty sure you're not experiencing anything described there.

Well ...
1. placing any section _after_ .bss is a bad idea in general, and it might not please GRUB.
2. if you're placing .data and .rodata together, make sure the .data section is still writable in your final .o
3. there's not .rodata alone! gcc loves to put small strings in a separate section from large strings and you might e.g. have .rodata.str32 ... you can be covered against that using *(.rodata*) in your script.

So please, _do_ read the "BareBones" even if you don't intend to use it, and _do_ use "objdump" tool to scrutinize your object files. That's the only way you can actually understand what the linker does ... and why it might not please GRUB.
Post Reply