shell.c 1.15 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/*
 * Copyright 2012 <James.Bottomley@HansenPartnership.com>
 *
 * see COPYING file
 *
 * misc shell helper functions
 */
#include <efi.h>
#include <efilib.h>

11
#include "shim.h"
12 13 14 15

EFI_STATUS
argsplit(EFI_HANDLE image, int *argc, CHAR16*** ARGV)
{
16
	unsigned int i, count = 1;
17
	EFI_STATUS efi_status;
18 19 20 21 22
	EFI_LOADED_IMAGE *info;
	CHAR16 *start;

	*argc = 0;

23 24 25 26 27
	efi_status = gBS->HandleProtocol(image, &LoadedImageProtocol,
					 (VOID **) &info);
	if (EFI_ERROR(efi_status)) {
		console_print(L"Failed to get arguments\n");
		return efi_status;
28 29 30 31 32 33 34 35 36
	}

	for (i = 0; i < info->LoadOptionsSize; i += 2) {
		CHAR16 *c = (CHAR16 *)(info->LoadOptions + i);
		if (*c == L' ' && *(c+1) != '\0') {
			(*argc)++;
		}
	}

37 38
	/* we counted spaces, so add one for initial */
	(*argc)++;
39

Peter Jones's avatar
Peter Jones committed
40
	*ARGV = AllocatePool(*argc * sizeof(**ARGV));
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
	if (!*ARGV) {
		return EFI_OUT_OF_RESOURCES;
	}
	(*ARGV)[0] = (CHAR16 *)info->LoadOptions;
	for (i = 0; i < info->LoadOptionsSize; i += 2) {
		CHAR16 *c = (CHAR16 *)(info->LoadOptions + i);
		if (*c == L' ') {
			*c = L'\0';
			if (*(c + 1) == '\0')
				/* strip trailing space */
				break;
			start = c + 1;
			(*ARGV)[count++] = start;
		}
	}

	return EFI_SUCCESS;
}