Improve loaders' kernel command line handling.

* grub-core/lib/cmdline.c: New file.
	* include/grub/lib/cmdline.h: Likewise.
	* grub-core/loader/i386/linux.c (grub_cmd_linux): Use
	grub_create_loader_cmdline to create kernel command line.
	* grub-core/loader/i386/pc/linux.c (grub_cmd_linux): Likewise.
	* grub-core/loader/powerpc/ieee1275/linux.c (grub_cmd_linux): Likewise.
	* grub-core/loader/sparc64/ieee1275/linux.c (grub_cmd_linux): Likewise.
	* grub-core/Makefile.core.def (linux16): Add lib/cmdline.c on i386_pc.
	(linux): Add lib/cmdline.c on common.
This commit is contained in:
Szymon Janc 2011-01-07 17:09:39 +01:00 committed by Vladimir 'phcoder' Serbinenko
parent e72d259fe1
commit 25953e1055
8 changed files with 184 additions and 64 deletions

View file

@ -1,3 +1,17 @@
2011-01-07 Szymon Janc <szymon@janc.net.pl>
Improve loaders' kernel command line handling.
* grub-core/lib/cmdline.c: New file.
* include/grub/lib/cmdline.h: Likewise.
* grub-core/loader/i386/linux.c (grub_cmd_linux): Use
grub_create_loader_cmdline to create kernel command line.
* grub-core/loader/i386/pc/linux.c (grub_cmd_linux): Likewise.
* grub-core/loader/powerpc/ieee1275/linux.c (grub_cmd_linux): Likewise.
* grub-core/loader/sparc64/ieee1275/linux.c (grub_cmd_linux): Likewise.
* grub-core/Makefile.core.def (linux16): Add lib/cmdline.c on i386_pc.
(linux): Add lib/cmdline.c on common.
2011-01-07 Vladimir Serbinenko <phcoder@gmail.com> 2011-01-07 Vladimir Serbinenko <phcoder@gmail.com>
* grub-core/fs/xfs.c (grub_xfs_iterate_dir): Take into account that * grub-core/fs/xfs.c (grub_xfs_iterate_dir): Take into account that

View file

@ -1149,6 +1149,7 @@ module = {
module = { module = {
name = linux16; name = linux16;
i386_pc = loader/i386/pc/linux.c; i386_pc = loader/i386/pc/linux.c;
i386_pc = lib/cmdline.c;
enable = i386_pc; enable = i386_pc;
}; };
@ -1183,6 +1184,7 @@ module = {
mips = loader/mips/linux.c; mips = loader/mips/linux.c;
powerpc_ieee1275 = loader/powerpc/ieee1275/linux.c; powerpc_ieee1275 = loader/powerpc/ieee1275/linux.c;
sparc64_ieee1275 = loader/sparc64/ieee1275/linux.c; sparc64_ieee1275 = loader/sparc64/ieee1275/linux.c;
common = lib/cmdline.c;
enable = noemu; enable = noemu;
}; };

105
grub-core/lib/cmdline.c Normal file
View file

@ -0,0 +1,105 @@
/* 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>
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. */
}
return size;
}
int grub_create_loader_cmdline (int argc, char *argv[], char *buf,
grub_size_t size)
{
int i, space;
unsigned int arg_size;
char *c;
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;
return i;
}

View file

@ -33,6 +33,7 @@
#include <grub/command.h> #include <grub/command.h>
#include <grub/i386/relocator.h> #include <grub/i386/relocator.h>
#include <grub/i18n.h> #include <grub/i18n.h>
#include <grub/lib/cmdline.h>
#ifdef GRUB_MACHINE_PCBIOS #ifdef GRUB_MACHINE_PCBIOS
#include <grub/i386/pc/vesa_modes_table.h> #include <grub/i386/pc/vesa_modes_table.h>
@ -575,7 +576,6 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
grub_size_t real_size, prot_size; grub_size_t real_size, prot_size;
grub_ssize_t len; grub_ssize_t len;
int i; int i;
char *dest;
grub_dl_ref (my_mod); grub_dl_ref (my_mod);
@ -836,22 +836,14 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
params->loadflags |= GRUB_LINUX_FLAG_QUIET; params->loadflags |= GRUB_LINUX_FLAG_QUIET;
} }
/* Create kernel command line. */
/* Specify the boot file. */ grub_memcpy ((char *)real_mode_mem + GRUB_LINUX_CL_OFFSET, LINUX_IMAGE,
dest = grub_stpcpy ((char *) real_mode_mem + GRUB_LINUX_CL_OFFSET, sizeof (LINUX_IMAGE));
"BOOT_IMAGE="); grub_create_loader_cmdline (argc, argv,
dest = grub_stpcpy (dest, argv[0]); (char *)real_mode_mem + GRUB_LINUX_CL_OFFSET
+ sizeof (LINUX_IMAGE) - 1,
/* Copy kernel parameters. */ GRUB_LINUX_CL_END_OFFSET - GRUB_LINUX_CL_OFFSET
for (i = 1; - (sizeof (LINUX_IMAGE) - 1));
i < argc
&& dest + grub_strlen (argv[i]) + 1 < ((char *) real_mode_mem
+ GRUB_LINUX_CL_END_OFFSET);
i++)
{
*dest++ = ' ';
dest = grub_stpcpy (dest, argv[i]);
}
len = prot_size; len = prot_size;
if (grub_file_read (file, prot_mode_mem, len) != len) if (grub_file_read (file, prot_mode_mem, len) != len)

View file

@ -34,6 +34,7 @@
#include <grub/cpu/relocator.h> #include <grub/cpu/relocator.h>
#include <grub/video.h> #include <grub/video.h>
#include <grub/i386/floppy.h> #include <grub/i386/floppy.h>
#include <grub/lib/cmdline.h>
#define GRUB_LINUX_CL_OFFSET 0x9000 #define GRUB_LINUX_CL_OFFSET 0x9000
#define GRUB_LINUX_CL_END_OFFSET 0x90FF #define GRUB_LINUX_CL_END_OFFSET 0x90FF
@ -86,7 +87,6 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
grub_size_t real_size; grub_size_t real_size;
grub_ssize_t len; grub_ssize_t len;
int i; int i;
char *dest;
char *grub_linux_prot_chunk; char *grub_linux_prot_chunk;
int grub_linux_is_bzimage; int grub_linux_is_bzimage;
grub_addr_t grub_linux_prot_target; grub_addr_t grub_linux_prot_target;
@ -286,21 +286,14 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
((GRUB_LINUX_MAX_SETUP_SECTS - setup_sects - 1) ((GRUB_LINUX_MAX_SETUP_SECTS - setup_sects - 1)
<< GRUB_DISK_SECTOR_BITS)); << GRUB_DISK_SECTOR_BITS));
/* Specify the boot file. */ /* Create kernel command line. */
dest = grub_stpcpy (grub_linux_real_chunk + GRUB_LINUX_CL_OFFSET, grub_memcpy ((char *)grub_linux_real_chunk + GRUB_LINUX_CL_OFFSET,
"BOOT_IMAGE="); LINUX_IMAGE, sizeof (LINUX_IMAGE));
dest = grub_stpcpy (dest, argv[0]); grub_create_loader_cmdline (argc, argv,
(char *)grub_linux_real_chunk
/* Copy kernel parameters. */ + GRUB_LINUX_CL_OFFSET + sizeof (LINUX_IMAGE) - 1,
for (i = 1; GRUB_LINUX_CL_END_OFFSET - GRUB_LINUX_CL_OFFSET
i < argc - (sizeof (LINUX_IMAGE) - 1));
&& dest + grub_strlen (argv[i]) + 1 < (grub_linux_real_chunk
+ GRUB_LINUX_CL_END_OFFSET);
i++)
{
*dest++ = ' ';
dest = grub_stpcpy (dest, argv[i]);
}
if (grub_linux_is_bzimage) if (grub_linux_is_bzimage)
grub_linux_prot_target = GRUB_LINUX_BZIMAGE_ADDR; grub_linux_prot_target = GRUB_LINUX_BZIMAGE_ADDR;

View file

@ -27,6 +27,7 @@
#include <grub/command.h> #include <grub/command.h>
#include <grub/i18n.h> #include <grub/i18n.h>
#include <grub/memory.h> #include <grub/memory.h>
#include <grub/lib/cmdline.h>
#define ELF32_LOADMASK (0xc0000000UL) #define ELF32_LOADMASK (0xc0000000UL)
#define ELF64_LOADMASK (0xc000000000000000ULL) #define ELF64_LOADMASK (0xc000000000000000ULL)
@ -238,9 +239,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
int argc, char *argv[]) int argc, char *argv[])
{ {
grub_elf_t elf = 0; grub_elf_t elf = 0;
int i;
int size; int size;
char *dest;
grub_dl_ref (my_mod); grub_dl_ref (my_mod);
@ -275,23 +274,15 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
goto out; goto out;
} }
size = sizeof ("BOOT_IMAGE=") + grub_strlen (argv[0]); size = grub_loader_cmdline_size(argc, argv);
for (i = 0; i < argc; i++) linux_args = grub_malloc (size + sizeof (LINUX_IMAGE));
size += grub_strlen (argv[i]) + 1;
linux_args = grub_malloc (size);
if (! linux_args) if (! linux_args)
goto out; goto out;
/* Specify the boot file. */ /* Create kernel command line. */
dest = grub_stpcpy (linux_args, "BOOT_IMAGE="); grub_memcpy (linux_args, LINUX_IMAGE, sizeof (LINUX_IMAGE));
dest = grub_stpcpy (dest, argv[0]); grub_create_loader_cmdline (argc, argv, linux_args + sizeof (LINUX_IMAGE) - 1,
size);
for (i = 1; i < argc; i++)
{
*dest++ = ' ';
dest = grub_stpcpy (dest, argv[i]);
}
out: out:

View file

@ -27,6 +27,7 @@
#include <grub/command.h> #include <grub/command.h>
#include <grub/i18n.h> #include <grub/i18n.h>
#include <grub/memory.h> #include <grub/memory.h>
#include <grub/lib/cmdline.h>
static grub_dl_t my_mod; static grub_dl_t my_mod;
@ -295,9 +296,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
{ {
grub_file_t file = 0; grub_file_t file = 0;
grub_elf_t elf = 0; grub_elf_t elf = 0;
int i;
int size; int size;
char *dest;
grub_dl_ref (my_mod); grub_dl_ref (my_mod);
@ -333,23 +332,16 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
goto out; goto out;
} }
size = sizeof ("BOOT_IMAGE=") + grub_strlen (argv[0]); size = grub_loader_cmdline_size(argc, argv);
for (i = 0; i < argc; i++)
size += grub_strlen (argv[i]) + 1;
linux_args = grub_malloc (size); linux_args = grub_malloc (size + sizeof (LINUX_IMAGE));
if (! linux_args) if (! linux_args)
goto out; goto out;
/* Specify the boot file. */ /* Create kernel command line. */
dest = grub_stpcpy (linux_args, "BOOT_IMAGE="); grub_memcpy (linux_args, LINUX_IMAGE, sizeof (LINUX_IMAGE));
dest = grub_stpcpy (dest, argv[0]); grub_create_loader_cmdline (argc, argv, linux_args + sizeof (LINUX_IMAGE) - 1,
size);
for (i = 1; i < argc; i++)
{
*dest++ = ' ';
dest = grub_stpcpy (dest, argv[i]);
}
out: out:
if (elf) if (elf)

View file

@ -0,0 +1,31 @@
/* cmdline.h - 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/>.
*/
#ifndef GRUB_CMDLINE_HEADER
#define GRUB_CMDLINE_HEADER 1
#include <grub/types.h>
#define LINUX_IMAGE "BOOT_IMAGE="
unsigned int grub_loader_cmdline_size (int argc, char *argv[]);
int grub_create_loader_cmdline (int argc, char *argv[], char *buf,
grub_size_t size);
#endif /* ! GRUB_CMDLINE_HEADER */