Find the answer to your Linux question:
Results 1 to 4 of 4
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    Question Can a 512 byte bootloader start a kernel with a compiled in initrd?

    The post title's the question, really.

    I have a kind of little goal to see how small I can get a Linux kernel that boots X if I go absolutely bananas over size - in other words, to see if projects like Xwoaf ( or 2diskXwin ( and are the lowest you can go. :P

    To achieve this, I'm basically taking existing software (and making a bit of my own) and hacking and hewing away at the source code until I can't remove code without crippling the functionality I want.

    GRUB's pretty big - 0.97, the last stable version of GRUB Legacy, is what I'm currently working with, and while it makes a fun hacking base (I made it say "hello!" and hang! ), carefully whittling away all the excess code (unnecessary internal commands, GUI, prompt, and so on) won't be an easy task to say the least, especially considering the fact that there's no underlying OS so everything's tied to everything else to a greater extent than normal. stage2 checks in at a whopping 98KB and while I could probably bring that number down to quite a small number (maybe even get it under double figures) it'd be nothing like MBR -[direct path]-> kernel.

    So I was wondering, since the actual asm code that boots Linux kernels seems to be quite small, could I just coerce GRUB's core asm framework to just skip to the actual "boot kernel" part, or find and modify another bootloader to do what I want?

    Let me quote the code in question here for the curious (from grub-0.97/stage2/asm.S):
     * linux_boot()
     * Does some funky things (including on the stack!), then jumps to the
     * entry point of the Linux setup code.
    	.long	0
    	.long	0
    	.long	0
    	/* don't worry about saving anything, we're committed at this point */
    	cld	/* forward copying */
    	/* copy kernel */
    	movl	EXT_C(linux_text_len), %ecx
    	addl	$3, %ecx
    	shrl	$2, %ecx
    	movl	$LINUX_BZIMAGE_ADDR, %esi
    	movl	$LINUX_ZIMAGE_ADDR, %edi
    	movl	EXT_C(linux_data_real_addr), %ebx
    	/* copy the real mode part */
    	movl	EXT_C(linux_data_tmp_addr), %esi
    	movl	%ebx, %edi
    	movl	$LINUX_SETUP_MOVE_SIZE, %ecx
    	/* change %ebx to the segment address */
    	shrl	$4, %ebx
    	movl	%ebx, %eax
    	addl	$0x20, %eax
    	movl	%eax, linux_setup_seg
    	/* XXX new stack pointer in safe area for calling functions */
    	movl	$0x4000, %esp
    	call	EXT_C(stop_floppy)
    	/* final setup for linux boot */
    	call	EXT_C(prot_to_real)
    	/* final setup for linux boot */
    	movw	%bx, %ss
    	movw	$LINUX_SETUP_STACK, %sp
    	movw	%bx, %ds
    	movw	%bx, %es
    	movw	%bx, %fs
    	movw	%bx, %gs
    	/* jump to start */
    	/* ljmp */
    	.byte	0xea
    	.word	0
    	.word	0
    Thanks in advance!

  2. #2
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    3rd rock from sun - Often seen near moon


    Do we have hybrid kernel? I know about micro kernel and monolithic - But not ready accept the claims of M$ for hybrid kernel.

    Hybrid kernel - Wikipedia, the free encyclopedia
    Linus Torvalds said of this issue:

    As to the whole Ďhybrid kernelí thing ó itís just marketing. Itís Ďoh, those microkernels had good PR, how can we try to get good PR for our working kernel? Oh, I know, letís use a cool name and try to imply that it has all the PR advantages that that other system hasí.[5]
    For your grub question,I guess it's better suited for grub developers
    (I'm not sure whether you will get an answer from here)
    GNU GRUB - Development

    while I could probably bring that number down to quite a small number (maybe even get it under double figures) it'd be nothing like MBR -[direct path]-> kernel
    that would be great,my stage2 is about 103KB. All the best with your hacking
    First they ignore you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    FOSS India Award winning ext3fs Undelete tool Online Linux Terminal

  3. #3
    Thanks for your fast reply!

    "hybrid" appears to be the wrong word to be using in this context... I simply meant a kernel that has an initrd image compiled into it. My reason for noting this particularity is because a kernel with a built in initrd wouldn't require anything beyond the basic code required to chain to the kernel.

    I'll keep my hopes up that I can boot such a kernel straight from an MBR, but for somewhat more complex setups that couldn't live without GRUB I'll continue whittling away at what I have. Who knows, I might actually leave it in a stable yet leaner state!

    <Title changed from "Can a 512 byte bootloader start a hybrid kernel/initrd?" to "Can a 512 byte bootloader start a kernel with a compiled in initrd?">

  4. $spacer_open
  5. #4
    Here's a slightly smaller GRUB 0.97 that includes the GUI et al. and only the following commands:

    blocklist boot chainloader configfile debug halt initrd ioprobe kernel module modulenounzip pager pause read reboot root rootnoverify unhide

    The standard disclaimer applies - I'm not responsible for whatever it does to your PC, such as causing the circuitry in your display to gain consciousness. :P

    NOTE: Be sure your menu.lst doesn't use commands not in the above list!

    I've made it available here:

    On my system it creates a 64KB stage2, but it may be bigger or smaller on your system.

    I compiled with:
    ./configure --disable-{ffs,ufs2,minix,reiserfs,vstafs,\
    Enjoy! I plan to do more - such as making the GUI completely "invisible" - but right now this is pretty much a code dump of where I'm at before I dive in to a point that my work is only useful for myself.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts