uuid.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2011 Calxeda, Inc.
  4. */
  5. #include <common.h>
  6. #include <command.h>
  7. #include <efi_api.h>
  8. #include <env.h>
  9. #include <rand.h>
  10. #include <time.h>
  11. #include <uuid.h>
  12. #include <linux/ctype.h>
  13. #include <errno.h>
  14. #include <common.h>
  15. #include <asm/io.h>
  16. #include <part_efi.h>
  17. #include <malloc.h>
  18. #include <dm/uclass.h>
  19. #include <rng.h>
  20. /*
  21. * UUID - Universally Unique IDentifier - 128 bits unique number.
  22. * There are 5 versions and one variant of UUID defined by RFC4122
  23. * specification. A UUID contains a set of fields. The set varies
  24. * depending on the version of the UUID, as shown below:
  25. * - time, MAC address(v1),
  26. * - user ID(v2),
  27. * - MD5 of name or URL(v3),
  28. * - random data(v4),
  29. * - SHA-1 of name or URL(v5),
  30. *
  31. * Layout of UUID:
  32. * timestamp - 60-bit: time_low, time_mid, time_hi_and_version
  33. * version - 4 bit (bit 4 through 7 of the time_hi_and_version)
  34. * clock seq - 14 bit: clock_seq_hi_and_reserved, clock_seq_low
  35. * variant: - bit 6 and 7 of clock_seq_hi_and_reserved
  36. * node - 48 bit
  37. *
  38. * source: https://www.ietf.org/rfc/rfc4122.txt
  39. *
  40. * UUID binary format (16 bytes):
  41. *
  42. * 4B-2B-2B-2B-6B (big endian - network byte order)
  43. *
  44. * UUID string is 36 length of characters (36 bytes):
  45. *
  46. * 0 9 14 19 24
  47. * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
  48. * be be be be be
  49. *
  50. * where x is a hexadecimal character. Fields are separated by '-'s.
  51. * When converting to a binary UUID, le means the field should be converted
  52. * to little endian and be means it should be converted to big endian.
  53. *
  54. * UUID is also used as GUID (Globally Unique Identifier) with the same binary
  55. * format but it differs in string format like below.
  56. *
  57. * GUID:
  58. * 0 9 14 19 24
  59. * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
  60. * le le le be be
  61. *
  62. * GUID is used e.g. in GPT (GUID Partition Table) as a partiions unique id.
  63. */
  64. int uuid_str_valid(const char *uuid)
  65. {
  66. int i, valid;
  67. if (uuid == NULL)
  68. return 0;
  69. for (i = 0, valid = 1; uuid[i] && valid; i++) {
  70. switch (i) {
  71. case 8: case 13: case 18: case 23:
  72. valid = (uuid[i] == '-');
  73. break;
  74. default:
  75. valid = isxdigit(uuid[i]);
  76. break;
  77. }
  78. }
  79. if (i != UUID_STR_LEN || !valid)
  80. return 0;
  81. return 1;
  82. }
  83. static const struct {
  84. const char *string;
  85. efi_guid_t guid;
  86. } list_guid[] = {
  87. #ifdef CONFIG_PARTITION_TYPE_GUID
  88. {"system", PARTITION_SYSTEM_GUID},
  89. {"mbr", LEGACY_MBR_PARTITION_GUID},
  90. {"msft", PARTITION_MSFT_RESERVED_GUID},
  91. {"data", PARTITION_BASIC_DATA_GUID},
  92. {"linux", PARTITION_LINUX_FILE_SYSTEM_DATA_GUID},
  93. {"raid", PARTITION_LINUX_RAID_GUID},
  94. {"swap", PARTITION_LINUX_SWAP_GUID},
  95. {"lvm", PARTITION_LINUX_LVM_GUID},
  96. {"u-boot-env", PARTITION_U_BOOT_ENVIRONMENT},
  97. #endif
  98. #ifdef CONFIG_CMD_EFIDEBUG
  99. {
  100. "Device Path",
  101. EFI_DEVICE_PATH_PROTOCOL_GUID,
  102. },
  103. {
  104. "Device Path To Text",
  105. EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID,
  106. },
  107. {
  108. "Device Path Utilities",
  109. EFI_DEVICE_PATH_UTILITIES_PROTOCOL_GUID,
  110. },
  111. {
  112. "Unicode Collation 2",
  113. EFI_UNICODE_COLLATION_PROTOCOL2_GUID,
  114. },
  115. {
  116. "Driver Binding",
  117. EFI_DRIVER_BINDING_PROTOCOL_GUID,
  118. },
  119. {
  120. "Simple Text Input",
  121. EFI_SIMPLE_TEXT_INPUT_PROTOCOL_GUID,
  122. },
  123. {
  124. "Simple Text Input Ex",
  125. EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID,
  126. },
  127. {
  128. "Simple Text Output",
  129. EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID,
  130. },
  131. {
  132. "Block IO",
  133. EFI_BLOCK_IO_PROTOCOL_GUID,
  134. },
  135. {
  136. "Simple File System",
  137. EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID,
  138. },
  139. {
  140. "Loaded Image",
  141. EFI_LOADED_IMAGE_PROTOCOL_GUID,
  142. },
  143. {
  144. "Graphics Output",
  145. EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID,
  146. },
  147. {
  148. "HII String",
  149. EFI_HII_STRING_PROTOCOL_GUID,
  150. },
  151. {
  152. "HII Database",
  153. EFI_HII_DATABASE_PROTOCOL_GUID,
  154. },
  155. {
  156. "HII Config Routing",
  157. EFI_HII_CONFIG_ROUTING_PROTOCOL_GUID,
  158. },
  159. {
  160. "Load File2",
  161. EFI_LOAD_FILE2_PROTOCOL_GUID,
  162. },
  163. {
  164. "Random Number Generator",
  165. EFI_RNG_PROTOCOL_GUID,
  166. },
  167. {
  168. "Simple Network",
  169. EFI_SIMPLE_NETWORK_PROTOCOL_GUID,
  170. },
  171. {
  172. "PXE Base Code",
  173. EFI_PXE_BASE_CODE_PROTOCOL_GUID,
  174. },
  175. {
  176. "Device-Tree Fixup",
  177. EFI_DT_FIXUP_PROTOCOL_GUID,
  178. },
  179. {
  180. "TCG2",
  181. EFI_TCG2_PROTOCOL_GUID,
  182. },
  183. {
  184. "System Partition",
  185. PARTITION_SYSTEM_GUID
  186. },
  187. {
  188. "Firmware Management",
  189. EFI_FIRMWARE_MANAGEMENT_PROTOCOL_GUID
  190. },
  191. /* Configuration table GUIDs */
  192. {
  193. "ACPI table",
  194. EFI_ACPI_TABLE_GUID,
  195. },
  196. {
  197. "EFI System Resource Table",
  198. EFI_SYSTEM_RESOURCE_TABLE_GUID,
  199. },
  200. {
  201. "device tree",
  202. EFI_FDT_GUID,
  203. },
  204. {
  205. "SMBIOS table",
  206. SMBIOS_TABLE_GUID,
  207. },
  208. {
  209. "Runtime properties",
  210. EFI_RT_PROPERTIES_TABLE_GUID,
  211. },
  212. {
  213. "TCG2 Final Events Table",
  214. EFI_TCG2_FINAL_EVENTS_TABLE_GUID,
  215. },
  216. #endif
  217. #ifdef CONFIG_CMD_NVEDIT_EFI
  218. /* signature database */
  219. {
  220. "EFI_GLOBAL_VARIABLE_GUID",
  221. EFI_GLOBAL_VARIABLE_GUID,
  222. },
  223. {
  224. "EFI_IMAGE_SECURITY_DATABASE_GUID",
  225. EFI_IMAGE_SECURITY_DATABASE_GUID,
  226. },
  227. /* certificate types */
  228. {
  229. "EFI_CERT_SHA256_GUID",
  230. EFI_CERT_SHA256_GUID,
  231. },
  232. {
  233. "EFI_CERT_X509_GUID",
  234. EFI_CERT_X509_GUID,
  235. },
  236. {
  237. "EFI_CERT_TYPE_PKCS7_GUID",
  238. EFI_CERT_TYPE_PKCS7_GUID,
  239. },
  240. #endif
  241. };
  242. /*
  243. * uuid_guid_get_bin() - this function get GUID bin for string
  244. *
  245. * @param guid_str - pointer to partition type string
  246. * @param guid_bin - pointer to allocated array for big endian output [16B]
  247. */
  248. int uuid_guid_get_bin(const char *guid_str, unsigned char *guid_bin)
  249. {
  250. int i;
  251. for (i = 0; i < ARRAY_SIZE(list_guid); i++) {
  252. if (!strcmp(list_guid[i].string, guid_str)) {
  253. memcpy(guid_bin, &list_guid[i].guid, 16);
  254. return 0;
  255. }
  256. }
  257. return -ENODEV;
  258. }
  259. /*
  260. * uuid_guid_get_str() - this function get string for GUID.
  261. *
  262. * @param guid_bin - pointer to string with partition type guid [16B]
  263. *
  264. * Returns NULL if the type GUID is not known.
  265. */
  266. const char *uuid_guid_get_str(const unsigned char *guid_bin)
  267. {
  268. int i;
  269. for (i = 0; i < ARRAY_SIZE(list_guid); i++) {
  270. if (!memcmp(list_guid[i].guid.b, guid_bin, 16)) {
  271. return list_guid[i].string;
  272. }
  273. }
  274. return NULL;
  275. }
  276. /*
  277. * uuid_str_to_bin() - convert string UUID or GUID to big endian binary data.
  278. *
  279. * @param uuid_str - pointer to UUID or GUID string [37B] or GUID shorcut
  280. * @param uuid_bin - pointer to allocated array for big endian output [16B]
  281. * @str_format - UUID string format: 0 - UUID; 1 - GUID
  282. */
  283. int uuid_str_to_bin(const char *uuid_str, unsigned char *uuid_bin,
  284. int str_format)
  285. {
  286. uint16_t tmp16;
  287. uint32_t tmp32;
  288. uint64_t tmp64;
  289. if (!uuid_str_valid(uuid_str)) {
  290. #ifdef CONFIG_PARTITION_TYPE_GUID
  291. if (!uuid_guid_get_bin(uuid_str, uuid_bin))
  292. return 0;
  293. #endif
  294. return -EINVAL;
  295. }
  296. if (str_format == UUID_STR_FORMAT_STD) {
  297. tmp32 = cpu_to_be32(hextoul(uuid_str, NULL));
  298. memcpy(uuid_bin, &tmp32, 4);
  299. tmp16 = cpu_to_be16(hextoul(uuid_str + 9, NULL));
  300. memcpy(uuid_bin + 4, &tmp16, 2);
  301. tmp16 = cpu_to_be16(hextoul(uuid_str + 14, NULL));
  302. memcpy(uuid_bin + 6, &tmp16, 2);
  303. } else {
  304. tmp32 = cpu_to_le32(hextoul(uuid_str, NULL));
  305. memcpy(uuid_bin, &tmp32, 4);
  306. tmp16 = cpu_to_le16(hextoul(uuid_str + 9, NULL));
  307. memcpy(uuid_bin + 4, &tmp16, 2);
  308. tmp16 = cpu_to_le16(hextoul(uuid_str + 14, NULL));
  309. memcpy(uuid_bin + 6, &tmp16, 2);
  310. }
  311. tmp16 = cpu_to_be16(hextoul(uuid_str + 19, NULL));
  312. memcpy(uuid_bin + 8, &tmp16, 2);
  313. tmp64 = cpu_to_be64(simple_strtoull(uuid_str + 24, NULL, 16));
  314. memcpy(uuid_bin + 10, (char *)&tmp64 + 2, 6);
  315. return 0;
  316. }
  317. /*
  318. * uuid_bin_to_str() - convert big endian binary data to string UUID or GUID.
  319. *
  320. * @param uuid_bin: pointer to binary data of UUID (big endian) [16B]
  321. * @param uuid_str: pointer to allocated array for output string [37B]
  322. * @str_format: bit 0: 0 - UUID; 1 - GUID
  323. * bit 1: 0 - lower case; 2 - upper case
  324. */
  325. void uuid_bin_to_str(const unsigned char *uuid_bin, char *uuid_str,
  326. int str_format)
  327. {
  328. const u8 uuid_char_order[UUID_BIN_LEN] = {0, 1, 2, 3, 4, 5, 6, 7, 8,
  329. 9, 10, 11, 12, 13, 14, 15};
  330. const u8 guid_char_order[UUID_BIN_LEN] = {3, 2, 1, 0, 5, 4, 7, 6, 8,
  331. 9, 10, 11, 12, 13, 14, 15};
  332. const u8 *char_order;
  333. const char *format;
  334. int i;
  335. /*
  336. * UUID and GUID bin data - always in big endian:
  337. * 4B-2B-2B-2B-6B
  338. * be be be be be
  339. */
  340. if (str_format & UUID_STR_FORMAT_GUID)
  341. char_order = guid_char_order;
  342. else
  343. char_order = uuid_char_order;
  344. if (str_format & UUID_STR_UPPER_CASE)
  345. format = "%02X";
  346. else
  347. format = "%02x";
  348. for (i = 0; i < 16; i++) {
  349. sprintf(uuid_str, format, uuid_bin[char_order[i]]);
  350. uuid_str += 2;
  351. switch (i) {
  352. case 3:
  353. case 5:
  354. case 7:
  355. case 9:
  356. *uuid_str++ = '-';
  357. break;
  358. }
  359. }
  360. }
  361. /*
  362. * gen_rand_uuid() - this function generates a random binary UUID version 4.
  363. * In this version all fields beside 4 bits of version and
  364. * 2 bits of variant are randomly generated.
  365. *
  366. * @param uuid_bin - pointer to allocated array [16B]. Output is in big endian.
  367. */
  368. #if defined(CONFIG_RANDOM_UUID) || defined(CONFIG_CMD_UUID)
  369. void gen_rand_uuid(unsigned char *uuid_bin)
  370. {
  371. u32 ptr[4];
  372. struct uuid *uuid = (struct uuid *)ptr;
  373. int i, ret;
  374. struct udevice *devp;
  375. u32 randv = 0;
  376. if (IS_ENABLED(CONFIG_DM_RNG)) {
  377. ret = uclass_get_device(UCLASS_RNG, 0, &devp);
  378. if (!ret) {
  379. ret = dm_rng_read(devp, &randv, sizeof(randv));
  380. if (ret < 0)
  381. randv = 0;
  382. }
  383. }
  384. if (randv)
  385. srand(randv);
  386. else
  387. srand(get_ticks() + rand());
  388. /* Set all fields randomly */
  389. for (i = 0; i < 4; i++)
  390. ptr[i] = rand();
  391. clrsetbits_be16(&uuid->time_hi_and_version,
  392. UUID_VERSION_MASK,
  393. UUID_VERSION << UUID_VERSION_SHIFT);
  394. clrsetbits_8(&uuid->clock_seq_hi_and_reserved,
  395. UUID_VARIANT_MASK,
  396. UUID_VARIANT << UUID_VARIANT_SHIFT);
  397. memcpy(uuid_bin, uuid, 16);
  398. }
  399. /*
  400. * gen_rand_uuid_str() - this function generates UUID v4 (random) in two string
  401. * formats UUID or GUID.
  402. *
  403. * @param uuid_str - pointer to allocated array [37B].
  404. * @param - uuid output type: UUID - 0, GUID - 1
  405. */
  406. void gen_rand_uuid_str(char *uuid_str, int str_format)
  407. {
  408. unsigned char uuid_bin[UUID_BIN_LEN];
  409. /* Generate UUID (big endian) */
  410. gen_rand_uuid(uuid_bin);
  411. /* Convert UUID bin to UUID or GUID formated STRING */
  412. uuid_bin_to_str(uuid_bin, uuid_str, str_format);
  413. }
  414. #if !defined(CONFIG_SPL_BUILD) && defined(CONFIG_CMD_UUID)
  415. int do_uuid(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
  416. {
  417. char uuid[UUID_STR_LEN + 1];
  418. int str_format;
  419. if (!strcmp(argv[0], "uuid"))
  420. str_format = UUID_STR_FORMAT_STD;
  421. else
  422. str_format = UUID_STR_FORMAT_GUID;
  423. if (argc > 2)
  424. return CMD_RET_USAGE;
  425. gen_rand_uuid_str(uuid, str_format);
  426. if (argc == 1)
  427. printf("%s\n", uuid);
  428. else
  429. env_set(argv[1], uuid);
  430. return CMD_RET_SUCCESS;
  431. }
  432. U_BOOT_CMD(uuid, CONFIG_SYS_MAXARGS, 1, do_uuid,
  433. "UUID - generate random Universally Unique Identifier",
  434. "[<varname>]\n"
  435. "Argument:\n"
  436. "varname: for set result in a environment variable\n"
  437. "e.g. uuid uuid_env"
  438. );
  439. U_BOOT_CMD(guid, CONFIG_SYS_MAXARGS, 1, do_uuid,
  440. "GUID - generate Globally Unique Identifier based on random UUID",
  441. "[<varname>]\n"
  442. "Argument:\n"
  443. "varname: for set result in a environment variable\n"
  444. "e.g. guid guid_env"
  445. );
  446. #endif /* CONFIG_CMD_UUID */
  447. #endif /* CONFIG_RANDOM_UUID || CONFIG_CMD_UUID */