xnu_uuid.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* xnu_uuid.c - transform 64-bit serial number
  2. to 128-bit uuid suitable for xnu. */
  3. /*
  4. * GRUB -- GRand Unified Bootloader
  5. * Copyright (C) 1995,1996,1998,1999,2001,2002,
  6. * 2003, 2009 Free Software Foundation, Inc.
  7. *
  8. * GRUB is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * GRUB is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include <grub/types.h>
  22. #include <grub/misc.h>
  23. #include <grub/mm.h>
  24. #include <grub/err.h>
  25. #include <grub/dl.h>
  26. #include <grub/device.h>
  27. #include <grub/disk.h>
  28. #include <grub/fs.h>
  29. #include <grub/file.h>
  30. #include <grub/misc.h>
  31. #include <grub/env.h>
  32. #include <grub/command.h>
  33. #include <grub/i18n.h>
  34. #include <grub/crypto.h>
  35. GRUB_MOD_LICENSE ("GPLv3+");
  36. /* This prefix is used by xnu and boot-132 to hash
  37. together with volume serial. */
  38. static grub_uint8_t hash_prefix[16]
  39. = {0xB3, 0xE2, 0x0F, 0x39, 0xF2, 0x92, 0x11, 0xD6,
  40. 0x97, 0xA4, 0x00, 0x30, 0x65, 0x43, 0xEC, 0xAC};
  41. static grub_err_t
  42. grub_cmd_xnu_uuid (grub_command_t cmd __attribute__ ((unused)),
  43. int argc, char **args)
  44. {
  45. grub_uint64_t serial;
  46. grub_uint8_t *xnu_uuid;
  47. char uuid_string[sizeof ("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")];
  48. char *ptr;
  49. void *ctx;
  50. int low = 0;
  51. if (argc < 1)
  52. return grub_error (GRUB_ERR_BAD_ARGUMENT, "UUID required");
  53. if (argc > 1 && grub_strcmp (args[0], "-l") == 0)
  54. {
  55. low = 1;
  56. argc--;
  57. args++;
  58. }
  59. serial = grub_cpu_to_be64 (grub_strtoull (args[0], 0, 16));
  60. ctx = grub_zalloc (GRUB_MD_MD5->contextsize);
  61. if (!ctx)
  62. return grub_errno;
  63. GRUB_MD_MD5->init (ctx);
  64. GRUB_MD_MD5->write (ctx, hash_prefix, sizeof (hash_prefix));
  65. GRUB_MD_MD5->write (ctx, &serial, sizeof (serial));
  66. GRUB_MD_MD5->final (ctx);
  67. xnu_uuid = GRUB_MD_MD5->read (ctx);
  68. grub_snprintf (uuid_string, sizeof (uuid_string),
  69. "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
  70. (unsigned int) xnu_uuid[0], (unsigned int) xnu_uuid[1],
  71. (unsigned int) xnu_uuid[2], (unsigned int) xnu_uuid[3],
  72. (unsigned int) xnu_uuid[4], (unsigned int) xnu_uuid[5],
  73. (unsigned int) ((xnu_uuid[6] & 0xf) | 0x30),
  74. (unsigned int) xnu_uuid[7],
  75. (unsigned int) ((xnu_uuid[8] & 0x3f) | 0x80),
  76. (unsigned int) xnu_uuid[9],
  77. (unsigned int) xnu_uuid[10], (unsigned int) xnu_uuid[11],
  78. (unsigned int) xnu_uuid[12], (unsigned int) xnu_uuid[13],
  79. (unsigned int) xnu_uuid[14], (unsigned int) xnu_uuid[15]);
  80. if (!low)
  81. for (ptr = uuid_string; *ptr; ptr++)
  82. *ptr = grub_toupper (*ptr);
  83. if (argc == 1)
  84. grub_printf ("%s\n", uuid_string);
  85. if (argc > 1)
  86. grub_env_set (args[1], uuid_string);
  87. grub_free (ctx);
  88. return GRUB_ERR_NONE;
  89. }
  90. static grub_command_t cmd;
  91. GRUB_MOD_INIT (xnu_uuid)
  92. {
  93. cmd = grub_register_command ("xnu_uuid", grub_cmd_xnu_uuid,
  94. /* TRANSLATORS: GRUBUUID stands for "filesystem
  95. UUID as used in GRUB". */
  96. N_("[-l] GRUBUUID [VARNAME]"),
  97. N_("Transform 64-bit UUID to format "
  98. "suitable for XNU. If -l is given keep "
  99. "it lowercase as done by blkid."));
  100. }
  101. GRUB_MOD_FINI (xnu_uuid)
  102. {
  103. grub_unregister_command (cmd);
  104. }