uuid.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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. #ifdef CONFIG_EFI_RISCV_BOOT_PROTOCOL
  217. {
  218. "RISC-V Boot",
  219. RISCV_EFI_BOOT_PROTOCOL_GUID,
  220. },
  221. #endif
  222. #endif /* CONFIG_CMD_EFIDEBUG */
  223. #ifdef CONFIG_CMD_NVEDIT_EFI
  224. /* signature database */
  225. {
  226. "EFI_GLOBAL_VARIABLE_GUID",
  227. EFI_GLOBAL_VARIABLE_GUID,
  228. },
  229. {
  230. "EFI_IMAGE_SECURITY_DATABASE_GUID",
  231. EFI_IMAGE_SECURITY_DATABASE_GUID,
  232. },
  233. /* certificate types */
  234. {
  235. "EFI_CERT_SHA256_GUID",
  236. EFI_CERT_SHA256_GUID,
  237. },
  238. {
  239. "EFI_CERT_X509_GUID",
  240. EFI_CERT_X509_GUID,
  241. },
  242. {
  243. "EFI_CERT_TYPE_PKCS7_GUID",
  244. EFI_CERT_TYPE_PKCS7_GUID,
  245. },
  246. #endif
  247. };
  248. /*
  249. * uuid_guid_get_bin() - this function get GUID bin for string
  250. *
  251. * @param guid_str - pointer to partition type string
  252. * @param guid_bin - pointer to allocated array for big endian output [16B]
  253. */
  254. int uuid_guid_get_bin(const char *guid_str, unsigned char *guid_bin)
  255. {
  256. int i;
  257. for (i = 0; i < ARRAY_SIZE(list_guid); i++) {
  258. if (!strcmp(list_guid[i].string, guid_str)) {
  259. memcpy(guid_bin, &list_guid[i].guid, 16);
  260. return 0;
  261. }
  262. }
  263. return -ENODEV;
  264. }
  265. /*
  266. * uuid_guid_get_str() - this function get string for GUID.
  267. *
  268. * @param guid_bin - pointer to string with partition type guid [16B]
  269. *
  270. * Returns NULL if the type GUID is not known.
  271. */
  272. const char *uuid_guid_get_str(const unsigned char *guid_bin)
  273. {
  274. int i;
  275. for (i = 0; i < ARRAY_SIZE(list_guid); i++) {
  276. if (!memcmp(list_guid[i].guid.b, guid_bin, 16)) {
  277. return list_guid[i].string;
  278. }
  279. }
  280. return NULL;
  281. }
  282. /*
  283. * uuid_str_to_bin() - convert string UUID or GUID to big endian binary data.
  284. *
  285. * @param uuid_str - pointer to UUID or GUID string [37B] or GUID shorcut
  286. * @param uuid_bin - pointer to allocated array for big endian output [16B]
  287. * @str_format - UUID string format: 0 - UUID; 1 - GUID
  288. */
  289. int uuid_str_to_bin(const char *uuid_str, unsigned char *uuid_bin,
  290. int str_format)
  291. {
  292. uint16_t tmp16;
  293. uint32_t tmp32;
  294. uint64_t tmp64;
  295. if (!uuid_str_valid(uuid_str)) {
  296. #ifdef CONFIG_PARTITION_TYPE_GUID
  297. if (!uuid_guid_get_bin(uuid_str, uuid_bin))
  298. return 0;
  299. #endif
  300. return -EINVAL;
  301. }
  302. if (str_format == UUID_STR_FORMAT_STD) {
  303. tmp32 = cpu_to_be32(hextoul(uuid_str, NULL));
  304. memcpy(uuid_bin, &tmp32, 4);
  305. tmp16 = cpu_to_be16(hextoul(uuid_str + 9, NULL));
  306. memcpy(uuid_bin + 4, &tmp16, 2);
  307. tmp16 = cpu_to_be16(hextoul(uuid_str + 14, NULL));
  308. memcpy(uuid_bin + 6, &tmp16, 2);
  309. } else {
  310. tmp32 = cpu_to_le32(hextoul(uuid_str, NULL));
  311. memcpy(uuid_bin, &tmp32, 4);
  312. tmp16 = cpu_to_le16(hextoul(uuid_str + 9, NULL));
  313. memcpy(uuid_bin + 4, &tmp16, 2);
  314. tmp16 = cpu_to_le16(hextoul(uuid_str + 14, NULL));
  315. memcpy(uuid_bin + 6, &tmp16, 2);
  316. }
  317. tmp16 = cpu_to_be16(hextoul(uuid_str + 19, NULL));
  318. memcpy(uuid_bin + 8, &tmp16, 2);
  319. tmp64 = cpu_to_be64(simple_strtoull(uuid_str + 24, NULL, 16));
  320. memcpy(uuid_bin + 10, (char *)&tmp64 + 2, 6);
  321. return 0;
  322. }
  323. /*
  324. * uuid_bin_to_str() - convert big endian binary data to string UUID or GUID.
  325. *
  326. * @param uuid_bin: pointer to binary data of UUID (big endian) [16B]
  327. * @param uuid_str: pointer to allocated array for output string [37B]
  328. * @str_format: bit 0: 0 - UUID; 1 - GUID
  329. * bit 1: 0 - lower case; 2 - upper case
  330. */
  331. void uuid_bin_to_str(const unsigned char *uuid_bin, char *uuid_str,
  332. int str_format)
  333. {
  334. const u8 uuid_char_order[UUID_BIN_LEN] = {0, 1, 2, 3, 4, 5, 6, 7, 8,
  335. 9, 10, 11, 12, 13, 14, 15};
  336. const u8 guid_char_order[UUID_BIN_LEN] = {3, 2, 1, 0, 5, 4, 7, 6, 8,
  337. 9, 10, 11, 12, 13, 14, 15};
  338. const u8 *char_order;
  339. const char *format;
  340. int i;
  341. /*
  342. * UUID and GUID bin data - always in big endian:
  343. * 4B-2B-2B-2B-6B
  344. * be be be be be
  345. */
  346. if (str_format & UUID_STR_FORMAT_GUID)
  347. char_order = guid_char_order;
  348. else
  349. char_order = uuid_char_order;
  350. if (str_format & UUID_STR_UPPER_CASE)
  351. format = "%02X";
  352. else
  353. format = "%02x";
  354. for (i = 0; i < 16; i++) {
  355. sprintf(uuid_str, format, uuid_bin[char_order[i]]);
  356. uuid_str += 2;
  357. switch (i) {
  358. case 3:
  359. case 5:
  360. case 7:
  361. case 9:
  362. *uuid_str++ = '-';
  363. break;
  364. }
  365. }
  366. }
  367. /*
  368. * gen_rand_uuid() - this function generates a random binary UUID version 4.
  369. * In this version all fields beside 4 bits of version and
  370. * 2 bits of variant are randomly generated.
  371. *
  372. * @param uuid_bin - pointer to allocated array [16B]. Output is in big endian.
  373. */
  374. #if defined(CONFIG_RANDOM_UUID) || defined(CONFIG_CMD_UUID)
  375. void gen_rand_uuid(unsigned char *uuid_bin)
  376. {
  377. u32 ptr[4];
  378. struct uuid *uuid = (struct uuid *)ptr;
  379. int i, ret;
  380. struct udevice *devp;
  381. u32 randv = 0;
  382. if (IS_ENABLED(CONFIG_DM_RNG)) {
  383. ret = uclass_get_device(UCLASS_RNG, 0, &devp);
  384. if (!ret) {
  385. ret = dm_rng_read(devp, &randv, sizeof(randv));
  386. if (ret < 0)
  387. randv = 0;
  388. }
  389. }
  390. if (randv)
  391. srand(randv);
  392. else
  393. srand(get_ticks() + rand());
  394. /* Set all fields randomly */
  395. for (i = 0; i < 4; i++)
  396. ptr[i] = rand();
  397. clrsetbits_be16(&uuid->time_hi_and_version,
  398. UUID_VERSION_MASK,
  399. UUID_VERSION << UUID_VERSION_SHIFT);
  400. clrsetbits_8(&uuid->clock_seq_hi_and_reserved,
  401. UUID_VARIANT_MASK,
  402. UUID_VARIANT << UUID_VARIANT_SHIFT);
  403. memcpy(uuid_bin, uuid, 16);
  404. }
  405. /*
  406. * gen_rand_uuid_str() - this function generates UUID v4 (random) in two string
  407. * formats UUID or GUID.
  408. *
  409. * @param uuid_str - pointer to allocated array [37B].
  410. * @param - uuid output type: UUID - 0, GUID - 1
  411. */
  412. void gen_rand_uuid_str(char *uuid_str, int str_format)
  413. {
  414. unsigned char uuid_bin[UUID_BIN_LEN];
  415. /* Generate UUID (big endian) */
  416. gen_rand_uuid(uuid_bin);
  417. /* Convert UUID bin to UUID or GUID formated STRING */
  418. uuid_bin_to_str(uuid_bin, uuid_str, str_format);
  419. }
  420. #if !defined(CONFIG_SPL_BUILD) && defined(CONFIG_CMD_UUID)
  421. int do_uuid(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
  422. {
  423. char uuid[UUID_STR_LEN + 1];
  424. int str_format;
  425. if (!strcmp(argv[0], "uuid"))
  426. str_format = UUID_STR_FORMAT_STD;
  427. else
  428. str_format = UUID_STR_FORMAT_GUID;
  429. if (argc > 2)
  430. return CMD_RET_USAGE;
  431. gen_rand_uuid_str(uuid, str_format);
  432. if (argc == 1)
  433. printf("%s\n", uuid);
  434. else
  435. env_set(argv[1], uuid);
  436. return CMD_RET_SUCCESS;
  437. }
  438. U_BOOT_CMD(uuid, CONFIG_SYS_MAXARGS, 1, do_uuid,
  439. "UUID - generate random Universally Unique Identifier",
  440. "[<varname>]\n"
  441. "Argument:\n"
  442. "varname: for set result in a environment variable\n"
  443. "e.g. uuid uuid_env"
  444. );
  445. U_BOOT_CMD(guid, CONFIG_SYS_MAXARGS, 1, do_uuid,
  446. "GUID - generate Globally Unique Identifier based on random UUID",
  447. "[<varname>]\n"
  448. "Argument:\n"
  449. "varname: for set result in a environment variable\n"
  450. "e.g. guid guid_env"
  451. );
  452. #endif /* CONFIG_CMD_UUID */
  453. #endif /* CONFIG_RANDOM_UUID || CONFIG_CMD_UUID */