120 lines
3.8 KiB
C
120 lines
3.8 KiB
C
/* xnu_uuid.c - transform 64-bit serial number
|
|
to 128-bit uuid suitable for xnu. */
|
|
/*
|
|
* VAS_EBOOT -- GRand Unified Bootloader
|
|
* Copyright (C) 1995,1996,1998,1999,2001,2002,
|
|
* 2003, 2009 Free Software Foundation, Inc.
|
|
*
|
|
* VAS_EBOOT 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.
|
|
*
|
|
* VAS_EBOOT 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 VAS_EBOOT. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include <VasEBoot/types.h>
|
|
#include <VasEBoot/misc.h>
|
|
#include <VasEBoot/mm.h>
|
|
#include <VasEBoot/err.h>
|
|
#include <VasEBoot/dl.h>
|
|
#include <VasEBoot/device.h>
|
|
#include <VasEBoot/disk.h>
|
|
#include <VasEBoot/fs.h>
|
|
#include <VasEBoot/file.h>
|
|
#include <VasEBoot/misc.h>
|
|
#include <VasEBoot/env.h>
|
|
#include <VasEBoot/command.h>
|
|
#include <VasEBoot/i18n.h>
|
|
#include <VasEBoot/crypto.h>
|
|
|
|
VAS_EBOOT_MOD_LICENSE ("GPLv3+");
|
|
|
|
/* This prefix is used by xnu and boot-132 to hash
|
|
together with volume serial. */
|
|
static VasEBoot_uint8_t hash_prefix[16]
|
|
= {0xB3, 0xE2, 0x0F, 0x39, 0xF2, 0x92, 0x11, 0xD6,
|
|
0x97, 0xA4, 0x00, 0x30, 0x65, 0x43, 0xEC, 0xAC};
|
|
|
|
static VasEBoot_err_t
|
|
VasEBoot_cmd_xnu_uuid (VasEBoot_command_t cmd __attribute__ ((unused)),
|
|
int argc, char **args)
|
|
{
|
|
VasEBoot_uint64_t serial;
|
|
VasEBoot_uint8_t *xnu_uuid;
|
|
char uuid_string[sizeof ("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")];
|
|
char *ptr;
|
|
void *ctx;
|
|
int low = 0;
|
|
|
|
if (argc < 1)
|
|
return VasEBoot_error (VAS_EBOOT_ERR_BAD_ARGUMENT, "UUID required");
|
|
|
|
if (argc > 1 && VasEBoot_strcmp (args[0], "-l") == 0)
|
|
{
|
|
low = 1;
|
|
argc--;
|
|
args++;
|
|
}
|
|
|
|
serial = VasEBoot_cpu_to_be64 (VasEBoot_strtoull (args[0], 0, 16));
|
|
|
|
ctx = VasEBoot_zalloc (VAS_EBOOT_MD_MD5->contextsize);
|
|
if (!ctx)
|
|
return VasEBoot_errno;
|
|
VAS_EBOOT_MD_MD5->init (ctx, 0);
|
|
VAS_EBOOT_MD_MD5->write (ctx, hash_prefix, sizeof (hash_prefix));
|
|
VAS_EBOOT_MD_MD5->write (ctx, &serial, sizeof (serial));
|
|
VAS_EBOOT_MD_MD5->final (ctx);
|
|
xnu_uuid = VAS_EBOOT_MD_MD5->read (ctx);
|
|
|
|
VasEBoot_snprintf (uuid_string, sizeof (uuid_string),
|
|
"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
|
|
(unsigned int) xnu_uuid[0], (unsigned int) xnu_uuid[1],
|
|
(unsigned int) xnu_uuid[2], (unsigned int) xnu_uuid[3],
|
|
(unsigned int) xnu_uuid[4], (unsigned int) xnu_uuid[5],
|
|
(unsigned int) ((xnu_uuid[6] & 0xf) | 0x30),
|
|
(unsigned int) xnu_uuid[7],
|
|
(unsigned int) ((xnu_uuid[8] & 0x3f) | 0x80),
|
|
(unsigned int) xnu_uuid[9],
|
|
(unsigned int) xnu_uuid[10], (unsigned int) xnu_uuid[11],
|
|
(unsigned int) xnu_uuid[12], (unsigned int) xnu_uuid[13],
|
|
(unsigned int) xnu_uuid[14], (unsigned int) xnu_uuid[15]);
|
|
if (!low)
|
|
for (ptr = uuid_string; *ptr; ptr++)
|
|
*ptr = VasEBoot_toupper (*ptr);
|
|
if (argc == 1)
|
|
VasEBoot_printf ("%s\n", uuid_string);
|
|
if (argc > 1)
|
|
VasEBoot_env_set (args[1], uuid_string);
|
|
|
|
VasEBoot_free (ctx);
|
|
|
|
return VAS_EBOOT_ERR_NONE;
|
|
}
|
|
|
|
static VasEBoot_command_t cmd;
|
|
|
|
|
|
VAS_EBOOT_MOD_INIT (xnu_uuid)
|
|
{
|
|
cmd = VasEBoot_register_command ("xnu_uuid", VasEBoot_cmd_xnu_uuid,
|
|
/* TRANSLATORS: VAS_EBOOTUUID stands for "filesystem
|
|
UUID as used in VAS_EBOOT". */
|
|
N_("[-l] VAS_EBOOTUUID [VARNAME]"),
|
|
N_("Transform 64-bit UUID to format "
|
|
"suitable for XNU. If -l is given keep "
|
|
"it lowercase as done by blkid."));
|
|
}
|
|
|
|
VAS_EBOOT_MOD_FINI (xnu_uuid)
|
|
{
|
|
VasEBoot_unregister_command (cmd);
|
|
}
|