Add new bootstub source.

This is the inline bootloader that will be embedded in the signed kernel
partition.

Review URL: http://codereview.chromium.org/2071024
diff --git a/files/bootstub.c b/files/bootstub.c
new file mode 100644
index 0000000..e98dd36
--- /dev/null
+++ b/files/bootstub.c
@@ -0,0 +1,186 @@
+// Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+#include <efi.h>
+#include <efilib.h>
+
+// Here's a struct to locate the pointers we need to update in the zeropage
+// structure. Everything else should already be hardcoded at compile time.
+// See arch/x86/include/asm/bootparam.h in the kernel source for the real deal.
+struct hacked_params {
+    uint8_t  pad0[0x1c0];
+    uint32_t v0206_efi_signature;          /* 1c0 */
+    uint32_t v0206_efi_system_table;       /* 1c4 */
+    uint32_t v0206_efi_mem_desc_size;      /* 1c8 */
+    uint32_t v0206_efi_mem_desc_version;   /* 1cc */
+    uint32_t v0206_efi_mmap;               /* 1d0 */
+    uint32_t v0206_efi_mmap_size;          /* 1d4 */
+    uint32_t v0206_efi_system_table_hi;    /* 1d8 */
+    uint32_t v0206_efi_mmap_hi;            /* 1dc */
+    uint8_t  pad1[0x214 - 0x1e0];
+    uint32_t code32_start;                 /* 214 */
+    uint8_t  pad2[0x228 - 0x218];
+    uint32_t cmd_line_ptr;                 /* 228 */
+} __attribute__ ((packed));
+
+
+// Find where the preloaded params struct is located in RAM. At the moment
+// we're assuming that it immediately precedes the start of the bootstub,
+// aligned to a 4K boundary, because that's where our build system puts it.
+struct hacked_params *find_params_struct(UINTN bootstub_location)
+{
+    return (struct hacked_params *)(bootstub_location - 0x1000);
+}
+
+// Replace any %D with the device letter, and replace any %P with the partition
+// number. For example, ("root=/dev/sd%D%P",2,3) gives "root=/dev/sdc3". The
+// input string must be mutable and end with a trailing '\0'.
+void update_cmdline_inplace(char *src, int devnum, int partnum)
+{
+    char *dst;
+
+    // Use sane values (sda3) for ridiculous inputs.
+    if (devnum < 0 || devnum > 25 || partnum < 1 || partnum > 99)
+    {
+        devnum = 0;
+        partnum = 3;
+    }
+
+    for( dst = src; *src; src++, dst++ )
+    {
+        if ( src[0] == '%' )
+        {
+            switch (src[1])
+            {
+            case 'P':
+                if (partnum > 9)
+                    *dst++ = '0' + (partnum / 10);
+                *dst = '0' + partnum % 10;
+                src++;
+                break;
+            case 'D':
+                *dst = 'a' + devnum;
+                src++;
+                break;
+            default:
+                *dst = *src;
+            }
+        }
+        else if (dst != src)
+            *dst = *src;
+    }
+    *dst = '\0';
+}
+
+// This is handy to write status codes to the LEDs for debugging.
+static __inline void port80w (unsigned short int value)
+{
+    __asm__ __volatile__ ("outw %w0,$0x80": :"a" (value));
+}
+
+
+// The code to switch to 32-bit mode and start the kernel.
+extern void trampoline(unsigned long, void *);
+
+
+// Reserve some space for the EFI memory map. 
+// Danger Will Robinson: this is just a guess at the size and alignment. If
+// it's too small, the EFI GetMemoryMap() call will fail.
+// FIXME: Make the size dynamic? Retry with larger size on failure?
+static unsigned char mmap_buf[0x2000] __attribute__ ((aligned(0x200)));
+
+// Parameters that we're given by the BIOS
+typedef struct cros_boot_info {
+    UINTN drive_number;                 // 0 - 25
+    UINTN partition_number;             // 1 - 99
+    UINTN original_address;             // our RAM address prior to execution
+} cros_boot_info_t;
+
+
+// Here's the entry point. It will be loaded by the BIOS as a standard EFI
+// application, which means it will be relocated.
+EFI_STATUS efi_main (EFI_HANDLE image, EFI_SYSTEM_TABLE *systab)
+{
+    UINTN mmap_size = sizeof(mmap_buf);
+    UINTN mmap_key = 0;
+    UINTN desc_size = 0;
+    UINT32 desc_version = 0;
+    EFI_LOADED_IMAGE *loaded_image;
+    EFI_GUID loaded_image_protocol = LOADED_IMAGE_PROTOCOL;
+
+    // I'm here.
+    port80w(0xc0de);
+
+    // Find the parameters that the BIOS has passed to us.
+    if (uefi_call_wrapper(systab->BootServices->HandleProtocol, 3,
+                          image,
+                          &loaded_image_protocol,
+                          &loaded_image) != 0)
+    {
+        uefi_call_wrapper(systab->ConOut->OutputString, 3, systab->ConOut,
+                          L"Can't locate protocol\r\n");
+        goto fail;
+    }
+    cros_boot_info_t *booting = loaded_image->LoadOptions;
+
+    // Find the parameters that we're passing to the kernel.
+    struct hacked_params *params = find_params_struct(booting->original_address);
+
+    // Update the kernel command-line string with the correct rootfs device
+    update_cmdline_inplace((char *)(unsigned long)(params->cmd_line_ptr),
+                           booting->drive_number,
+                           booting->partition_number + 1);
+
+    // Obtain the EFI memory map.
+    if (uefi_call_wrapper(systab->BootServices->GetMemoryMap, 5,
+                          &mmap_size, mmap_buf, &mmap_key,
+                          &desc_size, &desc_version) != 0)
+    {
+        uefi_call_wrapper(systab->ConOut->OutputString, 2, systab->ConOut,
+                          L"Can't get memory map\r\n");
+        goto fail;
+    }
+
+    // Update the pointers to the EFI memory map and system table.
+    params->v0206_efi_signature = ('4' << 24 | '6' << 16 | 'L' << 8 | 'E');
+    params->v0206_efi_system_table = (uint32_t) (unsigned long)systab;
+    params->v0206_efi_mem_desc_size = desc_size;
+    params->v0206_efi_mem_desc_version = desc_version;
+    params->v0206_efi_mmap = (uint32_t) (unsigned long)mmap_buf;
+    params->v0206_efi_mmap_size = mmap_size;
+    params->v0206_efi_mmap_hi = (uint32_t)((uint64_t)mmap_buf >> 32);
+    params->v0206_efi_system_table_hi = (uint32_t) ((uint64_t)systab >> 32);
+
+
+    // Done with BIOS.
+    if (uefi_call_wrapper(systab->BootServices->ExitBootServices, 2,
+                          image, mmap_key) != 0)
+    {
+        uefi_call_wrapper(systab->ConOut->OutputString, 2, systab->ConOut,
+                          L"Can't exit boot services\r\n");
+        goto fail;
+    }
+
+
+    // Trampoline to 32-bit entry point. Should never return.
+    trampoline(params->code32_start, params);
+
+fail:
+
+    // Bad Things happened.
+    port80w(0xdead);
+
+    return EFI_LOAD_ERROR;
+}