Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
Przemyslaw Marczak | d718ded | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2014 Samsung Electronics |
| 4 | * Przemyslaw Marczak <p.marczak@samsung.com> |
Abdellatif El Khlifi | 7048f26 | 2023-08-04 14:33:38 +0100 | [diff] [blame] | 5 | * Copyright 2022-2023 Arm Limited and/or its affiliates <open-source-office@arm.com> |
| 6 | * |
| 7 | * Authors: |
| 8 | * Abdellatif El Khlifi <abdellatif.elkhlifi@arm.com> |
Przemyslaw Marczak | d718ded | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 9 | */ |
| 10 | #ifndef __UUID_H__ |
| 11 | #define __UUID_H__ |
| 12 | |
Heinrich Schuchardt | 3bad256 | 2019-04-29 08:08:43 +0200 | [diff] [blame] | 13 | #include <linux/bitops.h> |
| 14 | |
Simon Glass | 8d6337e | 2023-08-24 13:55:36 -0600 | [diff] [blame] | 15 | /* |
| 16 | * UUID - Universally Unique IDentifier - 128 bits unique number. |
| 17 | * There are 5 versions and one variant of UUID defined by RFC4122 |
| 18 | * specification. A UUID contains a set of fields. The set varies |
| 19 | * depending on the version of the UUID, as shown below: |
| 20 | * - time, MAC address(v1), |
| 21 | * - user ID(v2), |
| 22 | * - MD5 of name or URL(v3), |
| 23 | * - random data(v4), |
| 24 | * - SHA-1 of name or URL(v5), |
| 25 | * |
| 26 | * Layout of UUID: |
| 27 | * timestamp - 60-bit: time_low, time_mid, time_hi_and_version |
| 28 | * version - 4 bit (bit 4 through 7 of the time_hi_and_version) |
| 29 | * clock seq - 14 bit: clock_seq_hi_and_reserved, clock_seq_low |
| 30 | * variant: - bit 6 and 7 of clock_seq_hi_and_reserved |
| 31 | * node - 48 bit |
| 32 | * |
| 33 | * source: https://www.ietf.org/rfc/rfc4122.txt |
| 34 | * |
| 35 | * UUID binary format (16 bytes): |
| 36 | * |
| 37 | * 4B-2B-2B-2B-6B (big endian - network byte order) |
| 38 | * |
| 39 | * UUID string is 36 length of characters (36 bytes): |
| 40 | * |
| 41 | * 0 9 14 19 24 |
| 42 | * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |
| 43 | * be be be be be |
| 44 | * |
| 45 | * where x is a hexadecimal character. Fields are separated by '-'s. |
| 46 | * When converting to a binary UUID, le means the field should be converted |
| 47 | * to little endian and be means it should be converted to big endian. |
| 48 | * |
| 49 | * UUID is also used as GUID (Globally Unique Identifier) with the same binary |
| 50 | * format but it differs in string format like below. |
| 51 | * |
| 52 | * GUID: |
| 53 | * 0 9 14 19 24 |
| 54 | * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |
| 55 | * le le le be be |
| 56 | * |
| 57 | * GUID is used e.g. in GPT (GUID Partition Table) as a partiions unique id. |
| 58 | */ |
| 59 | |
Przemyslaw Marczak | 4e4815f | 2014-04-02 10:20:04 +0200 | [diff] [blame] | 60 | /* This is structure is in big-endian */ |
| 61 | struct uuid { |
| 62 | unsigned int time_low; |
| 63 | unsigned short time_mid; |
| 64 | unsigned short time_hi_and_version; |
| 65 | unsigned char clock_seq_hi_and_reserved; |
| 66 | unsigned char clock_seq_low; |
| 67 | unsigned char node[6]; |
| 68 | } __packed; |
| 69 | |
Heinrich Schuchardt | 3bad256 | 2019-04-29 08:08:43 +0200 | [diff] [blame] | 70 | /* Bits of a bitmask specifying the output format for GUIDs */ |
| 71 | #define UUID_STR_FORMAT_STD 0 |
| 72 | #define UUID_STR_FORMAT_GUID BIT(0) |
| 73 | #define UUID_STR_UPPER_CASE BIT(1) |
Przemyslaw Marczak | d718ded | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 74 | |
Simon Glass | f8a2d19 | 2021-01-13 20:29:51 -0700 | [diff] [blame] | 75 | /* Use UUID_STR_LEN + 1 for string space */ |
Przemyslaw Marczak | d718ded | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 76 | #define UUID_STR_LEN 36 |
Przemyslaw Marczak | 4e4815f | 2014-04-02 10:20:04 +0200 | [diff] [blame] | 77 | #define UUID_BIN_LEN sizeof(struct uuid) |
| 78 | |
| 79 | #define UUID_VERSION_MASK 0xf000 |
| 80 | #define UUID_VERSION_SHIFT 12 |
| 81 | #define UUID_VERSION 0x4 |
| 82 | |
| 83 | #define UUID_VARIANT_MASK 0xc0 |
| 84 | #define UUID_VARIANT_SHIFT 7 |
| 85 | #define UUID_VARIANT 0x1 |
Przemyslaw Marczak | d718ded | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 86 | |
| 87 | int uuid_str_valid(const char *uuid); |
Simon Glass | 8d6337e | 2023-08-24 13:55:36 -0600 | [diff] [blame] | 88 | |
| 89 | /* |
| 90 | * uuid_str_to_bin() - convert string UUID or GUID to big endian binary data. |
| 91 | * |
| 92 | * @param uuid_str - pointer to UUID or GUID string [37B] or GUID shorcut |
| 93 | * @param uuid_bin - pointer to allocated array for big endian output [16B] |
| 94 | * @str_format - UUID string format: 0 - UUID; 1 - GUID |
| 95 | * Return: 0 if OK, -EINVAL if the string is not a valid UUID |
| 96 | */ |
Simon Glass | 2c2ca20 | 2020-04-08 08:32:58 -0600 | [diff] [blame] | 97 | int uuid_str_to_bin(const char *uuid_str, unsigned char *uuid_bin, |
| 98 | int str_format); |
Simon Glass | 8d6337e | 2023-08-24 13:55:36 -0600 | [diff] [blame] | 99 | |
| 100 | /* |
| 101 | * uuid_bin_to_str() - convert big endian binary data to string UUID or GUID. |
| 102 | * |
| 103 | * @param uuid_bin: pointer to binary data of UUID (big endian) [16B] |
| 104 | * @param uuid_str: pointer to allocated array for output string [37B] |
| 105 | * @str_format: bit 0: 0 - UUID; 1 - GUID |
| 106 | * bit 1: 0 - lower case; 2 - upper case |
| 107 | */ |
Simon Glass | 2c2ca20 | 2020-04-08 08:32:58 -0600 | [diff] [blame] | 108 | void uuid_bin_to_str(const unsigned char *uuid_bin, char *uuid_str, |
| 109 | int str_format); |
Simon Glass | 8d6337e | 2023-08-24 13:55:36 -0600 | [diff] [blame] | 110 | |
| 111 | /* |
| 112 | * uuid_guid_get_bin() - this function get GUID bin for string |
| 113 | * |
| 114 | * @param guid_str - pointer to partition type string |
| 115 | * @param guid_bin - pointer to allocated array for big endian output [16B] |
| 116 | */ |
Patrick Delaunay | bcb41dc | 2015-10-27 11:00:28 +0100 | [diff] [blame] | 117 | int uuid_guid_get_bin(const char *guid_str, unsigned char *guid_bin); |
Simon Glass | 8d6337e | 2023-08-24 13:55:36 -0600 | [diff] [blame] | 118 | |
| 119 | /* |
| 120 | * uuid_guid_get_str() - this function get string for GUID. |
| 121 | * |
| 122 | * @param guid_bin - pointer to string with partition type guid [16B] |
| 123 | * |
| 124 | * Returns NULL if the type GUID is not known. |
| 125 | */ |
Rasmus Villemoes | 31ce367 | 2020-11-20 11:45:35 +0100 | [diff] [blame] | 126 | const char *uuid_guid_get_str(const unsigned char *guid_bin); |
Simon Glass | 8d6337e | 2023-08-24 13:55:36 -0600 | [diff] [blame] | 127 | |
| 128 | /* |
| 129 | * gen_rand_uuid() - this function generates a random binary UUID version 4. |
| 130 | * In this version all fields beside 4 bits of version and |
| 131 | * 2 bits of variant are randomly generated. |
| 132 | * |
| 133 | * @param uuid_bin - pointer to allocated array [16B]. Output is in big endian. |
| 134 | */ |
Przemyslaw Marczak | 4e4815f | 2014-04-02 10:20:04 +0200 | [diff] [blame] | 135 | void gen_rand_uuid(unsigned char *uuid_bin); |
Simon Glass | 8d6337e | 2023-08-24 13:55:36 -0600 | [diff] [blame] | 136 | |
| 137 | /* |
| 138 | * gen_rand_uuid_str() - this function generates UUID v4 (random) in two string |
| 139 | * formats UUID or GUID. |
| 140 | * |
| 141 | * @param uuid_str - pointer to allocated array [37B]. |
| 142 | * @param - uuid output type: UUID - 0, GUID - 1 |
| 143 | */ |
Przemyslaw Marczak | 4e4815f | 2014-04-02 10:20:04 +0200 | [diff] [blame] | 144 | void gen_rand_uuid_str(char *uuid_str, int str_format); |
Abdellatif El Khlifi | 7048f26 | 2023-08-04 14:33:38 +0100 | [diff] [blame] | 145 | |
| 146 | /** |
| 147 | * uuid_str_to_le_bin() - Convert string UUID to little endian binary data. |
| 148 | * @uuid_str: pointer to UUID string |
| 149 | * @uuid_bin: pointer to allocated array for little endian output [16B] |
Simon Glass | 8d6337e | 2023-08-24 13:55:36 -0600 | [diff] [blame] | 150 | * |
| 151 | * UUID string is 36 characters (36 bytes): |
| 152 | * |
| 153 | * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |
| 154 | * |
| 155 | * where x is a hexadecimal character. Fields are separated by '-'s. |
| 156 | * When converting to a little endian binary UUID, the string fields are reversed. |
| 157 | * |
Abdellatif El Khlifi | 7048f26 | 2023-08-04 14:33:38 +0100 | [diff] [blame] | 158 | * Return: |
Simon Glass | 8d6337e | 2023-08-24 13:55:36 -0600 | [diff] [blame] | 159 | * |
Abdellatif El Khlifi | 7048f26 | 2023-08-04 14:33:38 +0100 | [diff] [blame] | 160 | * uuid_bin filled with little endian UUID data |
| 161 | * On success 0 is returned. Otherwise, failure code. |
| 162 | */ |
| 163 | int uuid_str_to_le_bin(const char *uuid_str, unsigned char *uuid_bin); |
| 164 | |
Przemyslaw Marczak | d718ded | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 165 | #endif |