uuid.c 13 KB

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