Debian Codegraph @

lib/shell.c

raw
1 // SPDX-License-Identifier: BSD-2-Clause-Patent
2 /*
3 * Copyright 2012 <[email protected]>
4 *
5 * misc shell helper functions
6 */
7 #include "shim.h"
8
9 EFI_STATUS
10 argsplit(EFI_HANDLE image, int *argc, CHAR16*** ARGV)
11 {
12 unsigned int i, count = 1;
13 EFI_STATUS efi_status;
14 EFI_LOADED_IMAGE *info;
15 CHAR16 *start;
16
17 *argc = 0;
18
19 efi_status = BS->HandleProtocol(image, &LoadedImageProtocol,
20 (VOID **) &info);
21 if (EFI_ERROR(efi_status)) {
22 console_print(L"Failed to get arguments\n");
23 return efi_status;
24 }
25
26 for (i = 0; i < info->LoadOptionsSize; i += 2) {
27 CHAR16 *c = (CHAR16 *)(info->LoadOptions + i);
28 if (*c == L' ' && *(c+1) != '\0') {
29 (*argc)++;
30 }
31 }
32
33 /* we counted spaces, so add one for initial */
34 (*argc)++;
35
36 *ARGV = AllocatePool(*argc * sizeof(**ARGV));
37 if (!*ARGV) {
38 return EFI_OUT_OF_RESOURCES;
39 }
40 (*ARGV)[0] = (CHAR16 *)info->LoadOptions;
41 for (i = 0; i < info->LoadOptionsSize; i += 2) {
42 CHAR16 *c = (CHAR16 *)(info->LoadOptions + i);
43 if (*c == L' ') {
44 *c = L'\0';
45 if (*(c + 1) == '\0')
46 /* strip trailing space */
47 break;
48 start = c + 1;
49 (*ARGV)[count++] = start;
50 }
51 }
52
53 return EFI_SUCCESS;
54 }
55