xnu_uuid.c 3.53 KB
Newer Older
1
/* xnu_uuid.c - transform 64-bit serial number
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
   to 128-bit uuid suitable for xnu. */
/*
 *  GRUB  --  GRand Unified Bootloader
 *  Copyright (C) 1995,1996,1998,1999,2001,2002,
 *                2003, 2009  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/types.h>
#include <grub/misc.h>
#include <grub/mm.h>
#include <grub/err.h>
#include <grub/dl.h>
#include <grub/device.h>
#include <grub/disk.h>
#include <grub/fs.h>
#include <grub/file.h>
#include <grub/misc.h>
#include <grub/env.h>
#include <grub/command.h>
34
#include <grub/i18n.h>
35
#include <grub/crypto.h>
36

37 38
GRUB_MOD_LICENSE ("GPLv3+");

39
/* This prefix is used by xnu and boot-132 to hash
40
   together with volume serial. */
41 42
static grub_uint8_t hash_prefix[16]
  = {0xB3, 0xE2, 0x0F, 0x39, 0xF2, 0x92, 0x11, 0xD6,
43 44 45 46 47 48
     0x97, 0xA4, 0x00, 0x30, 0x65, 0x43, 0xEC, 0xAC};

static grub_err_t
grub_cmd_xnu_uuid (grub_command_t cmd __attribute__ ((unused)),
		   int argc, char **args)
{
49 50
  grub_uint64_t serial;
  grub_uint8_t *xnu_uuid;
51 52
  char uuid_string[sizeof ("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")];
  char *ptr;
53
  void *ctx;
54
  int low = 0;
55 56 57 58

  if (argc < 1)
    return grub_error (GRUB_ERR_BAD_ARGUMENT, "UUID required");

59 60 61 62 63 64 65
  if (argc > 1 && grub_strcmp (args[0], "-l") == 0)
    {
      low = 1;
      argc--;
      args++;
    }

66 67
  serial = grub_cpu_to_be64 (grub_strtoull (args[0], 0, 16));

68 69 70 71 72 73 74 75
  ctx = grub_zalloc (GRUB_MD_MD5->contextsize);
  if (!ctx)
    return grub_errno;
  GRUB_MD_MD5->init (ctx);
  GRUB_MD_MD5->write (ctx, hash_prefix, sizeof (hash_prefix));
  GRUB_MD_MD5->write (ctx, &serial, sizeof (serial));
  GRUB_MD_MD5->final (ctx);
  xnu_uuid = GRUB_MD_MD5->read (ctx);
76

77
  grub_snprintf (uuid_string, sizeof (uuid_string),
78 79 80 81 82 83 84 85 86 87 88
		"%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]);
89 90 91
  if (!low)
    for (ptr = uuid_string; *ptr; ptr++)
      *ptr = grub_toupper (*ptr);
92
  if (argc == 1)
93
    grub_printf ("%s\n", uuid_string);
94 95 96
  if (argc > 1)
    grub_env_set (args[1], uuid_string);

97 98
  grub_free (ctx);

99 100 101 102 103 104 105 106 107
  return GRUB_ERR_NONE;
}

static grub_command_t cmd;


GRUB_MOD_INIT (xnu_uuid)
{
  cmd = grub_register_command ("xnu_uuid", grub_cmd_xnu_uuid,
108 109
			       /* TRANSLATORS: GRUBUUID stands for "filesystem
				  UUID as used in GRUB".  */
110
			       N_("[-l] GRUBUUID [VARNAME]"),
111
			       N_("Transform 64-bit UUID to format "
112 113
				  "suitable for XNU. If -l is given keep "
				  "it lowercase as done by blkid."));
114 115 116 117 118 119
}

GRUB_MOD_FINI (xnu_uuid)
{
  grub_unregister_command (cmd);
}