volume_id_internal.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * volume_id - reads filesystem label and uuid
  3. *
  4. * Copyright (C) 2005 Kay Sievers <kay.sievers@vrfy.org>
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include "libbb.h"
  21. #include "volume_id.h"
  22. PUSH_AND_SET_FUNCTION_VISIBILITY_TO_HIDDEN
  23. #define dbg(...) ((void)0)
  24. /* #define dbg(...) bb_error_msg(__VA_ARGS__) */
  25. /* volume_id.h */
  26. #define VOLUME_ID_VERSION 48
  27. #define VOLUME_ID_LABEL_SIZE 64
  28. #define VOLUME_ID_UUID_SIZE 36
  29. #define VOLUME_ID_FORMAT_SIZE 32
  30. #define VOLUME_ID_PARTITIONS_MAX 256
  31. enum volume_id_usage {
  32. VOLUME_ID_UNUSED,
  33. VOLUME_ID_UNPROBED,
  34. VOLUME_ID_OTHER,
  35. VOLUME_ID_FILESYSTEM,
  36. VOLUME_ID_PARTITIONTABLE,
  37. VOLUME_ID_RAID,
  38. VOLUME_ID_DISKLABEL,
  39. VOLUME_ID_CRYPTO,
  40. };
  41. #ifdef UNUSED_PARTITION_CODE
  42. struct volume_id_partition {
  43. // const char *type;
  44. // const char *usage;
  45. // smallint usage_id;
  46. // uint8_t pt_type_raw;
  47. // uint64_t pt_off;
  48. // uint64_t pt_len;
  49. };
  50. #endif
  51. struct volume_id {
  52. int fd;
  53. // int fd_close:1;
  54. int error;
  55. size_t sbbuf_len;
  56. size_t seekbuf_len;
  57. uint8_t *sbbuf;
  58. uint8_t *seekbuf;
  59. uint64_t seekbuf_off;
  60. #ifdef UNUSED_PARTITION_CODE
  61. struct volume_id_partition *partitions;
  62. size_t partition_count;
  63. #endif
  64. // uint8_t label_raw[VOLUME_ID_LABEL_SIZE];
  65. // size_t label_raw_len;
  66. char label[VOLUME_ID_LABEL_SIZE+1];
  67. // uint8_t uuid_raw[VOLUME_ID_UUID_SIZE];
  68. // size_t uuid_raw_len;
  69. /* uuid is stored in ASCII (not binary) form here: */
  70. char uuid[VOLUME_ID_UUID_SIZE+1];
  71. // char type_version[VOLUME_ID_FORMAT_SIZE];
  72. // smallint usage_id;
  73. // const char *usage;
  74. #if ENABLE_FEATURE_BLKID_TYPE
  75. const char *type;
  76. #endif
  77. };
  78. struct volume_id* FAST_FUNC volume_id_open_node(int fd);
  79. int FAST_FUNC volume_id_probe_all(struct volume_id *id, /*uint64_t off,*/ uint64_t size);
  80. void FAST_FUNC free_volume_id(struct volume_id *id);
  81. /* util.h */
  82. /* size of superblock buffer, reiserfs block is at 64k */
  83. #define SB_BUFFER_SIZE 0x11000
  84. /* size of seek buffer, FAT cluster is 32k max */
  85. #define SEEK_BUFFER_SIZE 0x10000
  86. #if BB_LITTLE_ENDIAN
  87. # define le16_to_cpu(x) (uint16_t)(x)
  88. # define le32_to_cpu(x) (uint32_t)(x)
  89. # define le64_to_cpu(x) (uint64_t)(x)
  90. # define be16_to_cpu(x) (uint16_t)(bswap_16(x))
  91. # define be32_to_cpu(x) (uint32_t)(bswap_32(x))
  92. # define cpu_to_le16(x) (uint16_t)(x)
  93. # define cpu_to_le32(x) (uint32_t)(x)
  94. # define cpu_to_be32(x) (uint32_t)(bswap_32(x))
  95. #else
  96. # define le16_to_cpu(x) (uint16_t)(bswap_16(x))
  97. # define le32_to_cpu(x) (uint32_t)(bswap_32(x))
  98. # define le64_to_cpu(x) (uint64_t)(bb_bswap_64(x))
  99. # define be16_to_cpu(x) (uint16_t)(x)
  100. # define be32_to_cpu(x) (uint32_t)(x)
  101. # define cpu_to_le16(x) (uint16_t)(bswap_16(x))
  102. # define cpu_to_le32(x) (uint32_t)(bswap_32(x))
  103. # define cpu_to_be32(x) (uint32_t)(x)
  104. #endif
  105. /* volume_id_set_uuid(id,buf,fmt) assumes size of uuid buf
  106. * by shifting: 4 << fmt, except for fmt == UUID_DCE_STRING.
  107. * The constants below should match sizes.
  108. */
  109. enum uuid_format {
  110. UUID_DOS = 0, /* 4 bytes */
  111. UUID_NTFS = 1, /* 8 bytes */
  112. UUID_DCE = 2, /* 16 bytes */
  113. UUID_DCE_STRING = 3, /* 36 bytes (VOLUME_ID_UUID_SIZE) */
  114. };
  115. enum endian {
  116. LE = 0,
  117. BE = 1
  118. };
  119. void volume_id_set_unicode16(char *str, size_t len, const uint8_t *buf, enum endian endianess, size_t count);
  120. //void volume_id_set_usage(struct volume_id *id, enum volume_id_usage usage_id);
  121. //void volume_id_set_usage_part(struct volume_id_partition *part, enum volume_id_usage usage_id);
  122. //void volume_id_set_label_raw(struct volume_id *id, const uint8_t *buf, size_t count);
  123. void volume_id_set_label_string(struct volume_id *id, const uint8_t *buf, size_t count);
  124. void volume_id_set_label_unicode16(struct volume_id *id, const uint8_t *buf, enum endian endianess, size_t count);
  125. void volume_id_set_uuid(struct volume_id *id, const uint8_t *buf, enum uuid_format format);
  126. void *volume_id_get_buffer(struct volume_id *id, uint64_t off, size_t len);
  127. void volume_id_free_buffer(struct volume_id *id);
  128. /* Probe routines */
  129. /* RAID */
  130. //int FAST_FUNC volume_id_probe_highpoint_37x_raid(struct volume_id *id /*,uint64_t off*/);
  131. //int FAST_FUNC volume_id_probe_highpoint_45x_raid(struct volume_id *id /*,uint64_t off*/, uint64_t size);
  132. //int FAST_FUNC volume_id_probe_intel_software_raid(struct volume_id *id /*,uint64_t off*/, uint64_t size);
  133. int FAST_FUNC volume_id_probe_linux_raid(struct volume_id *id /*,uint64_t off*/, uint64_t size);
  134. //int FAST_FUNC volume_id_probe_lsi_mega_raid(struct volume_id *id /*,uint64_t off*/, uint64_t size);
  135. //int FAST_FUNC volume_id_probe_nvidia_raid(struct volume_id *id /*,uint64_t off*/, uint64_t size);
  136. //int FAST_FUNC volume_id_probe_promise_fasttrack_raid(struct volume_id *id /*,uint64_t off*/, uint64_t size);
  137. //int FAST_FUNC volume_id_probe_silicon_medley_raid(struct volume_id *id /*,uint64_t off*/, uint64_t size);
  138. //int FAST_FUNC volume_id_probe_via_raid(struct volume_id *id /*,uint64_t off*/, uint64_t size);
  139. //int FAST_FUNC volume_id_probe_lvm1(struct volume_id *id /*,uint64_t off*/);
  140. //int FAST_FUNC volume_id_probe_lvm2(struct volume_id *id /*,uint64_t off*/);
  141. /* FS */
  142. int FAST_FUNC volume_id_probe_bcache(struct volume_id *id /*,uint64_t off*/);
  143. int FAST_FUNC volume_id_probe_btrfs(struct volume_id *id /*,uint64_t off*/);
  144. int FAST_FUNC volume_id_probe_cramfs(struct volume_id *id /*,uint64_t off*/);
  145. int FAST_FUNC volume_id_probe_ext(struct volume_id *id /*,uint64_t off*/);
  146. int FAST_FUNC volume_id_probe_vfat(struct volume_id *id /*,uint64_t off*/);
  147. int FAST_FUNC volume_id_probe_hfs_hfsplus(struct volume_id *id /*,uint64_t off*/);
  148. //int FAST_FUNC volume_id_probe_hpfs(struct volume_id *id /*,uint64_t off*/);
  149. int FAST_FUNC volume_id_probe_iso9660(struct volume_id *id /*,uint64_t off*/);
  150. int FAST_FUNC volume_id_probe_jfs(struct volume_id *id /*,uint64_t off*/);
  151. int FAST_FUNC volume_id_probe_lfs(struct volume_id *id /*,uint64_t off*/);
  152. int FAST_FUNC volume_id_probe_linux_swap(struct volume_id *id /*,uint64_t off*/);
  153. int FAST_FUNC volume_id_probe_luks(struct volume_id *id /*,uint64_t off*/);
  154. //int FAST_FUNC volume_id_probe_mac_partition_map(struct volume_id *id /*,uint64_t off*/);
  155. int FAST_FUNC volume_id_probe_minix(struct volume_id *id /*, uint64_t off*/);
  156. //int FAST_FUNC volume_id_probe_msdos_part_table(struct volume_id *id /*,uint64_t off*/);
  157. int FAST_FUNC volume_id_probe_f2fs(struct volume_id *id /*,uint64_t off*/);
  158. int FAST_FUNC volume_id_probe_nilfs(struct volume_id *id /*,uint64_t off*/);
  159. int FAST_FUNC volume_id_probe_ntfs(struct volume_id *id /*,uint64_t off*/);
  160. int FAST_FUNC volume_id_probe_exfat(struct volume_id *id /*,uint64_t off*/);
  161. int FAST_FUNC volume_id_probe_ocfs2(struct volume_id *id /*,uint64_t off*/);
  162. int FAST_FUNC volume_id_probe_reiserfs(struct volume_id *id /*,uint64_t off*/);
  163. int FAST_FUNC volume_id_probe_romfs(struct volume_id *id /*,uint64_t off*/);
  164. int FAST_FUNC volume_id_probe_squashfs(struct volume_id *id /*,uint64_t off*/);
  165. int FAST_FUNC volume_id_probe_sysv(struct volume_id *id /*,uint64_t off*/);
  166. int FAST_FUNC volume_id_probe_udf(struct volume_id *id /*,uint64_t off*/);
  167. //int FAST_FUNC volume_id_probe_ufs(struct volume_id *id /*,uint64_t off*/);
  168. int FAST_FUNC volume_id_probe_xfs(struct volume_id *id /*,uint64_t off*/);
  169. int FAST_FUNC volume_id_probe_ubifs(struct volume_id *id /*,uint64_t off*/);
  170. POP_SAVED_FUNCTION_VISIBILITY