2011-01-07 16:09:39 +00:00
|
|
|
/* cmdline.c - linux command line handling */
|
|
|
|
/*
|
|
|
|
* GRUB -- GRand Unified Bootloader
|
|
|
|
* Copyright (C) 2010 Free Software Foundation, Inc.
|
|
|
|
*
|
|
|
|
* GRUB 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.
|
|
|
|
*
|
|
|
|
* GRUB 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 GRUB. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <grub/lib/cmdline.h>
|
|
|
|
#include <grub/misc.h>
|
2015-08-09 23:32:29 +00:00
|
|
|
#include <grub/tpm.h>
|
2011-01-07 16:09:39 +00:00
|
|
|
|
|
|
|
static unsigned int check_arg (char *c, int *has_space)
|
|
|
|
{
|
|
|
|
int space = 0;
|
|
|
|
unsigned int size = 0;
|
|
|
|
|
|
|
|
while (*c)
|
|
|
|
{
|
|
|
|
if (*c == '\\' || *c == '\'' || *c == '"')
|
|
|
|
size++;
|
|
|
|
else if (*c == ' ')
|
|
|
|
space = 1;
|
|
|
|
|
|
|
|
size++;
|
|
|
|
c++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (space)
|
|
|
|
size += 2;
|
|
|
|
|
|
|
|
if (has_space)
|
|
|
|
*has_space = space;
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int grub_loader_cmdline_size (int argc, char *argv[])
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
unsigned int size = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < argc; i++)
|
|
|
|
{
|
|
|
|
size += check_arg (argv[i], 0);
|
|
|
|
size++; /* Separator space or NULL. */
|
|
|
|
}
|
|
|
|
|
2013-11-09 16:31:21 +00:00
|
|
|
if (size == 0)
|
|
|
|
size = 1;
|
|
|
|
|
2011-01-07 16:09:39 +00:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2017-02-07 01:10:14 +00:00
|
|
|
grub_err_t
|
|
|
|
grub_create_loader_cmdline (int argc, char *argv[], char *buf,
|
|
|
|
grub_size_t size, enum grub_verify_string_type type)
|
2011-01-07 16:09:39 +00:00
|
|
|
{
|
|
|
|
int i, space;
|
|
|
|
unsigned int arg_size;
|
2017-02-07 01:10:14 +00:00
|
|
|
char *c, *orig_buf = buf;
|
2011-01-07 16:09:39 +00:00
|
|
|
|
|
|
|
for (i = 0; i < argc; i++)
|
|
|
|
{
|
|
|
|
c = argv[i];
|
|
|
|
arg_size = check_arg(argv[i], &space);
|
|
|
|
arg_size++; /* Separator space or NULL. */
|
|
|
|
|
|
|
|
if (size < arg_size)
|
|
|
|
break;
|
|
|
|
|
|
|
|
size -= arg_size;
|
|
|
|
|
|
|
|
if (space)
|
|
|
|
*buf++ = '"';
|
|
|
|
|
|
|
|
while (*c)
|
|
|
|
{
|
|
|
|
if (*c == '\\' || *c == '\'' || *c == '"')
|
|
|
|
*buf++ = '\\';
|
|
|
|
|
|
|
|
*buf++ = *c;
|
|
|
|
c++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (space)
|
|
|
|
*buf++ = '"';
|
|
|
|
|
|
|
|
*buf++ = ' ';
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Replace last space with null. */
|
|
|
|
if (i)
|
|
|
|
buf--;
|
|
|
|
|
|
|
|
*buf = 0;
|
|
|
|
|
2017-02-07 01:10:14 +00:00
|
|
|
grub_tpm_measure ((void *)orig_buf, grub_strlen (orig_buf), GRUB_ASCII_PCR,
|
|
|
|
"grub_kernel_cmdline", orig_buf);
|
2016-10-13 20:55:26 +00:00
|
|
|
grub_print_error();
|
2015-08-09 23:32:29 +00:00
|
|
|
|
2017-02-07 01:10:14 +00:00
|
|
|
return grub_verify_string (orig_buf, type);
|
2011-01-07 16:09:39 +00:00
|
|
|
}
|