<story><title>Writing kernels that boot with Qemu and Grub – a tutorial</title><url>http://www.cs.vu.nl/~herbertb/misc/writingkernels.txt</url></story><parent_chain></parent_chain><comment><author>exDM69</author><text>Nice but very incomplete article. Some observations below...&lt;p&gt;You &lt;i&gt;will&lt;/i&gt; need to build a cross compiler toolchain (binutils and GCC). I.e. i686-pc-elf-gcc for 32 bit or x86_64-pc-elf-gcc for 64 bit. The compiler that comes with your OS is built for a different target and may have downstream patches, esp. if you&amp;#x27;re on Ubuntu. Things may seem to work initially but you &lt;i&gt;will&lt;/i&gt; encounter strange bugs sooner or later.&lt;p&gt;Build a bootable and debuggable ELF file, not a flat binary. All it takes is a slightly different linker script. You won&amp;#x27;t get very far without a debugger on bare metal projects.&lt;p&gt;You need to zero fill .bss section in the boot code before jumping to C code.&lt;p&gt;I recommend using gas instead of nasm. You might like Intel syntax but you&amp;#x27;ll need inline (at&amp;amp;t) assembly in C code anyway. Nicer to have just one syntax. It&amp;#x27;s just an assembler syntax, you will get used to it in a matter of hours&lt;p&gt;Use a makefile. Fancy build systems (e.g. cmake) aren&amp;#x27;t nice with kernel images.&lt;p&gt;See below for examples. The first one isn&amp;#x27;t my project but I contributed the linker script, Makefile and README for making bootable ELFs. The latter is my x86_64 toy kernel project from years ago.&lt;p&gt;&lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;github.com&amp;#x2F;Overv&amp;#x2F;MineAssemble&quot; rel=&quot;nofollow&quot;&gt;https:&amp;#x2F;&amp;#x2F;github.com&amp;#x2F;Overv&amp;#x2F;MineAssemble&lt;/a&gt;&lt;p&gt;&lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;github.com&amp;#x2F;rikusalminen&amp;#x2F;danjeros&quot; rel=&quot;nofollow&quot;&gt;https:&amp;#x2F;&amp;#x2F;github.com&amp;#x2F;rikusalminen&amp;#x2F;danjeros&lt;/a&gt;</text></comment>
<story><title>Writing kernels that boot with Qemu and Grub – a tutorial</title><url>http://www.cs.vu.nl/~herbertb/misc/writingkernels.txt</url></story><parent_chain></parent_chain><comment><author>deathanatos</author><text>I&amp;#x27;m impressed they got grub onto a hard disk image without needing root; I didn&amp;#x27;t think you could do that. I struggled with that for a while, before I found grub2-mkrescue, which I think is much easier if you&amp;#x27;re just starting out. The entire sequence to create a CD ISO is,&lt;p&gt;&lt;pre&gt;&lt;code&gt;  # Stage the data we want in our image:
  mkdir -p isofiles&amp;#x2F;boot&amp;#x2F;grub
  cp grub.cfg isofiles&amp;#x2F;boot&amp;#x2F;grub&amp;#x2F;.
  cp kernel.bin isofiles&amp;#x2F;boot&amp;#x2F;.

  # Make the image:
  grub2-mkrescue -o os.iso isofiles

  # Clean up:
  rm -rf isofiles
&lt;/code&gt;&lt;/pre&gt;
Which I got from the tutorial here[1], which is also excellent.&lt;p&gt;[1]: &lt;a href=&quot;http:&amp;#x2F;&amp;#x2F;os.phil-opp.com&amp;#x2F;multiboot-kernel.html&quot; rel=&quot;nofollow&quot;&gt;http:&amp;#x2F;&amp;#x2F;os.phil-opp.com&amp;#x2F;multiboot-kernel.html&lt;/a&gt;</text></comment>