Rewrite grub-install, grub-mkrescue, grub-mkstandalone and grub-mknetdir

the function of these files exceeds what can be sanely handled in shell
	in posix-comaptible way. Also writing it in C extends the functionality
	to non-UNIX-like OS and minimal environments.
This commit is contained in:
Vladimir Serbinenko 2013-11-16 20:21:16 +01:00
parent 9ef81064a3
commit cd46aa6cef
52 changed files with 5811 additions and 2101 deletions

View file

@ -0,0 +1,57 @@
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 1999,2000,2001,2002,2003,2004,2006,2007,2008,2009,2010,2011,2012,2013 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 <config.h>
#include <config-util.h>
#include <grub/emu/hostfile.h>
#include <grub/emu/config.h>
#include <grub/util/install.h>
#include <grub/util/misc.h>
void
grub_util_load_config (struct grub_util_config *cfg)
{
const char *cfgfile;
FILE *f = NULL;
const char *v;
cfgfile = grub_util_get_config_filename ();
if (!grub_util_is_regular (cfgfile))
return;
memset (cfg, 0, sizeof (*cfg));
v = getenv ("GRUB_ENABLE_CRYPTODISK");
if (v && v[0] == 'y' && v[1] == '\0')
cfg->is_cryptodisk_enabled = 1;
v = getenv ("GRUB_DISTRIBUTOR");
if (v)
cfg->grub_distributor = xstrdup (v);
f = grub_util_fopen (cfgfile, "r");
if (f)
{
grub_util_parse_config (f, cfg, 0);
fclose (f);
}
else
grub_util_warn (_("cannot open config file `%s': %s"),
cfgfile, strerror (errno));
}

View file

@ -46,6 +46,7 @@
#include <windows.h>
#include <winioctl.h>
#include <wincrypt.h>
#if SIZEOF_TCHAR == 1
@ -411,6 +412,89 @@ grub_util_unlink (const char *name)
return ret;
}
int
grub_util_rmdir (const char *name)
{
LPTSTR name_windows;
int ret;
name_windows = grub_util_get_windows_path (name);
ret = !RemoveDirectory (name_windows);
free (name_windows);
return ret;
}
static char *
get_temp_name (void)
{
TCHAR rt[1024];
TCHAR *ptr;
HCRYPTPROV hCryptProv;
grub_uint8_t rnd[5];
const size_t sz = sizeof (rnd) * GRUB_CHAR_BIT / 5;
int i;
GetTempPath (ARRAY_SIZE (rt) - 100, rt);
if (!CryptAcquireContext (&hCryptProv,
NULL,
MS_DEF_PROV,
PROV_RSA_FULL,
CRYPT_VERIFYCONTEXT)
|| !CryptGenRandom (hCryptProv, 5, rnd))
grub_util_error ("%s", _("couldn't retrieve random data"));
CryptReleaseContext (hCryptProv, 0);
for (ptr = rt; *ptr; ptr++);
memcpy (ptr, TEXT("\\GRUB."), sizeof (TEXT("\\GRUB.")));
ptr += sizeof ("\\GRUB.") - 1;
for (i = 0; i < 8; i++)
{
grub_size_t b = i * 5;
grub_uint8_t r;
grub_size_t f1 = GRUB_CHAR_BIT - b % GRUB_CHAR_BIT;
grub_size_t f2;
if (f1 > 5)
f1 = 5;
f2 = 5 - f1;
r = (rnd[b / GRUB_CHAR_BIT] >> (b % GRUB_CHAR_BIT)) & ((1 << f1) - 1);
if (f2)
r |= (rnd[b / GRUB_CHAR_BIT + 1] & ((1 << f2) - 1)) << f1;
if (r < 10)
*ptr++ = '0' + r;
else
*ptr++ = 'a' + (r - 10);
}
*ptr = '\0';
return grub_util_tchar_to_utf8 (rt);
}
char *
grub_util_make_temporary_file (void)
{
char *ret = get_temp_name ();
FILE *f;
f = grub_util_fopen (ret, "wb");
if (f)
fclose (f);
return ret;
}
char *
grub_util_make_temporary_dir (void)
{
char *ret = get_temp_name ();
grub_util_mkdir (ret);
return ret;
}
int
grub_util_is_directory (const char *name)
{
@ -444,6 +528,33 @@ grub_util_is_regular (const char *name)
&& !(attr & FILE_ATTRIBUTE_REPARSE_POINT) && attr;
}
grub_uint32_t
grub_util_get_mtime (const char *path)
{
LPTSTR name_windows;
BOOL b;
WIN32_FILE_ATTRIBUTE_DATA attr;
ULARGE_INTEGER us_ul;
name_windows = grub_util_get_windows_path (path);
if (!name_windows)
return 0;
b = GetFileAttributesEx (name_windows, GetFileExInfoStandard, &attr);
grub_free (name_windows);
if (!b)
return 0;
us_ul.LowPart = attr.ftLastWriteTime.dwLowDateTime;
us_ul.HighPart = attr.ftLastWriteTime.dwHighDateTime;
return (us_ul.QuadPart / 10000000)
- 86400ULL * 365 * (1970 - 1601)
- 86400ULL * ((1970 - 1601) / 4) + 86400ULL * ((1970 - 1601) / 100);
}
#ifdef __MINGW32__
FILE *

View file

@ -21,6 +21,7 @@
#include <grub/util/misc.h>
#include <grub/osdep/hostfile.h>
#include <grub/util/windows.h>
#include <grub/emu/config.h>
#include <wincon.h>
#include <windows.h>
@ -111,10 +112,42 @@ set_console_unicode_font (void)
}
}
static char *grub_util_base_directory;
static char *locale_dir;
const char *
grub_util_get_config_filename (void)
{
static char *value = NULL;
if (!value)
value = grub_util_path_concat (2, grub_util_base_directory, "grub.cfg");
return value;
}
const char *
grub_util_get_pkgdatadir (void)
{
return grub_util_base_directory;
}
const char *
grub_util_get_localedir (void)
{
return locale_dir;
}
const char *
grub_util_get_pkglibdir (void)
{
return grub_util_base_directory;
}
void
grub_util_host_init (int *argc __attribute__ ((unused)),
char ***argv)
{
char *ptr;
SetConsoleOutputCP (CP_UTF8);
SetConsoleCP (CP_UTF8);
@ -137,9 +170,21 @@ grub_util_host_init (int *argc __attribute__ ((unused)),
#error "Unsupported TCHAR size"
#endif
grub_util_base_directory = canonicalize_file_name ((*argv)[0]);
if (!grub_util_base_directory)
grub_util_base_directory = xstrdup ((*argv)[0]);
for (ptr = grub_util_base_directory + strlen (grub_util_base_directory) - 1;
ptr >= grub_util_base_directory && *ptr != '/' && *ptr != '\\'; ptr--);
if (ptr >= grub_util_base_directory)
*ptr = '\0';
locale_dir = grub_util_path_concat (2, grub_util_base_directory, "locale");
set_program_name ((*argv)[0]);
#ifdef GRUB_UTIL
grub_util_init_nls ();
#endif
#if (defined (GRUB_UTIL) && defined(ENABLE_NLS) && ENABLE_NLS)
setlocale (LC_ALL, "");
bindtextdomain (PACKAGE, locale_dir);
textdomain (PACKAGE);
#endif /* (defined(ENABLE_NLS) && ENABLE_NLS) */
}