summaryrefslogtreecommitdiffstats
path: root/src/interface/efi/efi_cmdline.c
blob: b33bebd8c67a5e7446fa3b42a8b4e568c99b88f3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
 * Copyright (C) 2023 Michael Brown <mbrown@fensystems.co.uk>.
 *
 * 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 2 of the
 * License, or 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, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 *
 * You can also choose to distribute this program under the terms of
 * the Unmodified Binary Distribution Licence (as given in the file
 * COPYING.UBDL), provided that you have satisfied its requirements.
 */

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );

/** @file
 *
 * EFI command line
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <errno.h>
#include <ipxe/init.h>
#include <ipxe/image.h>
#include <ipxe/script.h>
#include <ipxe/efi/efi.h>
#include <ipxe/efi/efi_cmdline.h>

/** EFI command line (may not be wNUL-terminated */
const wchar_t *efi_cmdline;

/** Length of EFI command line (in bytes) */
size_t efi_cmdline_len;

/** Internal copy of the command line */
static char *efi_cmdline_copy;

/**
 * Free command line image
 *
 * @v refcnt		Reference count
 */
static void efi_cmdline_free ( struct refcnt *refcnt ) {
	struct image *image = container_of ( refcnt, struct image, refcnt );

	DBGC ( image, "CMDLINE freeing command line\n" );
	free ( efi_cmdline_copy );
}

/** Embedded script representing the command line */
static struct image efi_cmdline_image = {
	.refcnt = REF_INIT ( efi_cmdline_free ),
	.name = "<CMDLINE>",
	.type = &script_image_type,
};

/** Colour for debug messages */
#define colour &efi_cmdline_image

/**
 * Initialise EFI command line
 *
 * @ret rc		Return status code
 */
static int efi_cmdline_init ( void ) {
	char *cmdline;
	size_t len;
	int rc;

	/* Do nothing if no command line was specified */
	if ( ! efi_cmdline_len ) {
		DBGC ( colour, "CMDLINE found no command line\n" );
		return 0;
	}

	/* Allocate ASCII copy of command line */
	len = ( ( efi_cmdline_len / sizeof ( efi_cmdline[0] ) ) + 1 /* NUL */ );
	efi_cmdline_copy = malloc ( len );
	if ( ! efi_cmdline_copy ) {
		rc = -ENOMEM;
		goto err_alloc;
	}
	cmdline = efi_cmdline_copy;
	snprintf ( cmdline, len, "%ls", efi_cmdline );
	DBGC ( colour, "CMDLINE found command line \"%s\"\n", cmdline );

	/* Mark command line as consumed */
	efi_cmdline_len = 0;

	/* Strip image name and surrounding whitespace */
	while ( isspace ( *cmdline ) )
		cmdline++;
	while ( *cmdline && ( ! isspace ( *cmdline ) ) )
		cmdline++;
	while ( isspace ( *cmdline ) )
		cmdline++;
	DBGC ( colour, "CMDLINE using command line \"%s\"\n", cmdline );

	/* Prepare and register image */
	efi_cmdline_image.data = virt_to_user ( cmdline );
	efi_cmdline_image.len = strlen ( cmdline );
	if ( efi_cmdline_image.len &&
	     ( ( rc = register_image ( &efi_cmdline_image ) ) != 0 ) ) {
		DBGC ( colour, "CMDLINE could not register command line: %s\n",
		       strerror ( rc ) );
		goto err_register_image;
	}

	/* Drop our reference to the image */
	image_put ( &efi_cmdline_image );

	return 0;

 err_register_image:
	image_put ( &efi_cmdline_image );
 err_alloc:
	return rc;
}

/**
 * EFI command line startup function
 *
 */
static void efi_cmdline_startup ( void ) {
	int rc;

	/* Initialise command line */
	if ( ( rc = efi_cmdline_init() ) != 0 ) {
		/* No way to report failure */
		return;
	}
}

/** Command line and initrd initialisation function */
struct startup_fn efi_cmdline_startup_fn __startup_fn ( STARTUP_NORMAL ) = {
	.name = "efi_cmdline",
	.startup = efi_cmdline_startup,
};