view bootx64.c @ 10:0c55588d01b0

add get kernel name from option
author taiki
date Sun, 30 Mar 2014 03:37:30 +0900
parents d6a8d676a1ac
children 7d337b6fb379
line wrap: on
line source

#include <efi.h>
#include <efilib.h>

#include "registers.h"

CHAR16 *DEFAULT_KERNEL_NAME = L"my_kernel";
CHAR16 SPACE = ' ';

CHAR16*
get_kernel_name(CHAR16 *options, UINT32 options_size)
{
    /* 
     * skip first option,
     * first option is file name of this efi executable.
     */ 

    UINT32 count = options_size;

    while(options_size && (*options != SPACE)) {
        options++;
        count--;
    }
    if (options_size <=  0) {
        return DEFAULT_KERNEL_NAME;
    }

    if (*options == SPACE) {
        options++;
    }
    CHAR16 *kernel_name = (CHAR16 *)AllocatePool((options_size - count) * sizeof(CHAR16));

    RtCopyMem(kernel_name, options, (options_size - count) * sizeof(CHAR16));

    return kernel_name;
    
}

EFI_STATUS
load_kernel(CHAR16 *kernel_name)
{
    Print(L"Start load kernel: %s\n", kernel_name);

    EFI_STATUS status;
    UINTN handle_count;
    EFI_HANDLE *handle_buffer;
    SIMPLE_READ_FILE read_handle = NULL;

    status = uefi_call_wrapper(BS->LocateHandleBuffer,
                5,
                ByProtocol,
                &FileSystemProtocol,
                NULL,
                &handle_count,
                &handle_buffer);

    if (EFI_ERROR(status)) {
        Print(L"LocateHandleBuffer is %r\n", status);
    }

    EFI_DEVICE_PATH *path;
    UINTN handle_idx = 0;

    for (handle_idx = 0; handle_idx < handle_count; handle_idx++) {
        EFI_HANDLE device_handle;

        Print(L"handle_idx %d\n", handle_idx);
        path = FileDevicePath(handle_buffer[handle_idx], kernel_name);

        if (!path) {
            status = EFI_NOT_FOUND;
            break;
        }

        status = OpenSimpleReadFile(TRUE, NULL, 0, &path, &device_handle, &read_handle);

        if (!EFI_ERROR(status)) {
            break;
        }

        FreePool(path);
        path = NULL;
    }

    if (!EFI_ERROR(status)) {
        //status = LoadImage(read_handle);
    }

    if (read_handle) {
        CloseSimpleReadFile(read_handle);
    }

    Print(L"c\n");
    if (path) {
        FreePool(path);
    }

    Print(L"d\n");
    FreePool(handle_buffer);

    return status;
}

EFI_STATUS
efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *systab)
{
    EFI_LOADED_IMAGE *loaded_image;

    InitializeLib(image, systab);

    uefi_call_wrapper(systab->ConOut->Reset, 2, systab->ConOut, FALSE);

    Print(L"---- start ----\n");

    EFI_STATUS status;
    
    Print(L"Open LoadedImage Protocol\n");
    status = uefi_call_wrapper(
        BS->OpenProtocol,
        6,
        image,
        &LoadedImageProtocol,
        (void **)&loaded_image,
        image,
        NULL,
        EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);

    if (EFI_ERROR(status)) {
        Print(L"OpenProtocol is %r\n", status);
    }

    Print(L"%s\n", loaded_image->LoadOptions);

    CHAR16 *kernel_name = get_kernel_name(loaded_image->LoadOptions, loaded_image->LoadOptionsSize);

    Print(L"%s\n", kernel_name);

    load_kernel(kernel_name);

    Print(L"Close LoadedImage Protocol\n");
    status = uefi_call_wrapper(
        BS->CloseProtocol,
        4,
        image,
        &LoadedImageProtocol,
        image,
        NULL);

    if (EFI_ERROR(status)) {
        Print(L"CloseProtocol is %r\n", status);
    }

    Print(L"---- Faild start kernel ----\n");

    return EFI_SUCCESS;
}