]>
Commit | Line | Data |
---|---|---|
a2c1332b | 1 | /* xnu_uuid.c - transform 64-bit serial number |
693fe637 | 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 | ||
22 | #include <grub/types.h> | |
23 | #include <grub/misc.h> | |
24 | #include <grub/mm.h> | |
25 | #include <grub/err.h> | |
26 | #include <grub/dl.h> | |
27 | #include <grub/device.h> | |
28 | #include <grub/disk.h> | |
29 | #include <grub/fs.h> | |
30 | #include <grub/file.h> | |
31 | #include <grub/misc.h> | |
32 | #include <grub/env.h> | |
33 | #include <grub/command.h> | |
77a79592 | 34 | #include <grub/i18n.h> |
20de16bb | 35 | #include <grub/crypto.h> |
693fe637 | 36 | |
e745cf0c VS |
37 | GRUB_MOD_LICENSE ("GPLv3+"); |
38 | ||
a2c1332b | 39 | /* This prefix is used by xnu and boot-132 to hash |
693fe637 | 40 | together with volume serial. */ |
a2c1332b FZ |
41 | static grub_uint8_t hash_prefix[16] |
42 | = {0xB3, 0xE2, 0x0F, 0x39, 0xF2, 0x92, 0x11, 0xD6, | |
693fe637 | 43 | 0x97, 0xA4, 0x00, 0x30, 0x65, 0x43, 0xEC, 0xAC}; |
44 | ||
693fe637 | 45 | static grub_err_t |
46 | grub_cmd_xnu_uuid (grub_command_t cmd __attribute__ ((unused)), | |
47 | int argc, char **args) | |
48 | { | |
20de16bb VS |
49 | grub_uint64_t serial; |
50 | grub_uint8_t *xnu_uuid; | |
693fe637 | 51 | char uuid_string[sizeof ("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")]; |
52 | char *ptr; | |
44a387e0 | 53 | void *ctx; |
8e32442e | 54 | int low = 0; |
693fe637 | 55 | |
56 | if (argc < 1) | |
57 | return grub_error (GRUB_ERR_BAD_ARGUMENT, "UUID required"); | |
58 | ||
8e32442e VS |
59 | if (argc > 1 && grub_strcmp (args[0], "-l") == 0) |
60 | { | |
61 | low = 1; | |
62 | argc--; | |
63 | args++; | |
64 | } | |
65 | ||
20de16bb VS |
66 | serial = grub_cpu_to_be64 (grub_strtoull (args[0], 0, 16)); |
67 | ||
44a387e0 VS |
68 | ctx = grub_zalloc (GRUB_MD_MD5->contextsize); |
69 | if (!ctx) | |
70 | return grub_errno; | |
71 | GRUB_MD_MD5->init (ctx); | |
72 | GRUB_MD_MD5->write (ctx, hash_prefix, sizeof (hash_prefix)); | |
73 | GRUB_MD_MD5->write (ctx, &serial, sizeof (serial)); | |
74 | GRUB_MD_MD5->final (ctx); | |
75 | xnu_uuid = GRUB_MD_MD5->read (ctx); | |
693fe637 | 76 | |
8b442f3f | 77 | grub_snprintf (uuid_string, sizeof (uuid_string), |
693fe637 | 78 | "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x", |
79 | (unsigned int) xnu_uuid[0], (unsigned int) xnu_uuid[1], | |
80 | (unsigned int) xnu_uuid[2], (unsigned int) xnu_uuid[3], | |
81 | (unsigned int) xnu_uuid[4], (unsigned int) xnu_uuid[5], | |
82 | (unsigned int) ((xnu_uuid[6] & 0xf) | 0x30), | |
83 | (unsigned int) xnu_uuid[7], | |
84 | (unsigned int) ((xnu_uuid[8] & 0x3f) | 0x80), | |
85 | (unsigned int) xnu_uuid[9], | |
86 | (unsigned int) xnu_uuid[10], (unsigned int) xnu_uuid[11], | |
87 | (unsigned int) xnu_uuid[12], (unsigned int) xnu_uuid[13], | |
88 | (unsigned int) xnu_uuid[14], (unsigned int) xnu_uuid[15]); | |
8e32442e VS |
89 | if (!low) |
90 | for (ptr = uuid_string; *ptr; ptr++) | |
91 | *ptr = grub_toupper (*ptr); | |
693fe637 | 92 | if (argc == 1) |
8e32442e | 93 | grub_printf ("%s\n", uuid_string); |
693fe637 | 94 | if (argc > 1) |
95 | grub_env_set (args[1], uuid_string); | |
96 | ||
44a387e0 VS |
97 | grub_free (ctx); |
98 | ||
693fe637 | 99 | return GRUB_ERR_NONE; |
100 | } | |
101 | ||
102 | static grub_command_t cmd; | |
103 | ||
104 | ||
105 | GRUB_MOD_INIT (xnu_uuid) | |
106 | { | |
107 | cmd = grub_register_command ("xnu_uuid", grub_cmd_xnu_uuid, | |
e8e0566b VS |
108 | /* TRANSLATORS: GRUBUUID stands for "filesystem |
109 | UUID as used in GRUB". */ | |
8e32442e | 110 | N_("[-l] GRUBUUID [VARNAME]"), |
77a79592 | 111 | N_("Transform 64-bit UUID to format " |
8e32442e VS |
112 | "suitable for XNU. If -l is given keep " |
113 | "it lowercase as done by blkid.")); | |
693fe637 | 114 | } |
115 | ||
116 | GRUB_MOD_FINI (xnu_uuid) | |
117 | { | |
118 | grub_unregister_command (cmd); | |
119 | } |