volume_id_internal.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. // const char *type;
  75. };
  76. struct volume_id* FAST_FUNC volume_id_open_node(int fd);
  77. int FAST_FUNC volume_id_probe_all(struct volume_id *id, /*uint64_t off,*/ uint64_t size);
  78. void FAST_FUNC free_volume_id(struct volume_id *id);
  79. /* util.h */
  80. /* size of superblock buffer, reiserfs block is at 64k */
  81. #define SB_BUFFER_SIZE 0x11000
  82. /* size of seek buffer, FAT cluster is 32k max */
  83. #define SEEK_BUFFER_SIZE 0x10000
  84. #define bswap16(x) (uint16_t) ( \
  85. (((uint16_t)(x) & 0x00ffu) << 8) | \
  86. (((uint16_t)(x) & 0xff00u) >> 8))
  87. #define bswap32(x) (uint32_t) ( \
  88. (((uint32_t)(x) & 0xff000000u) >> 24) | \
  89. (((uint32_t)(x) & 0x00ff0000u) >> 8) | \
  90. (((uint32_t)(x) & 0x0000ff00u) << 8) | \
  91. (((uint32_t)(x) & 0x000000ffu) << 24))
  92. #define bswap64(x) (uint64_t) ( \
  93. (((uint64_t)(x) & 0xff00000000000000ull) >> 56) | \
  94. (((uint64_t)(x) & 0x00ff000000000000ull) >> 40) | \
  95. (((uint64_t)(x) & 0x0000ff0000000000ull) >> 24) | \
  96. (((uint64_t)(x) & 0x000000ff00000000ull) >> 8) | \
  97. (((uint64_t)(x) & 0x00000000ff000000ull) << 8) | \
  98. (((uint64_t)(x) & 0x0000000000ff0000ull) << 24) | \
  99. (((uint64_t)(x) & 0x000000000000ff00ull) << 40) | \
  100. (((uint64_t)(x) & 0x00000000000000ffull) << 56))
  101. #if BB_LITTLE_ENDIAN
  102. #define le16_to_cpu(x) (x)
  103. #define le32_to_cpu(x) (x)
  104. #define le64_to_cpu(x) (x)
  105. #define be16_to_cpu(x) bswap16(x)
  106. #define be32_to_cpu(x) bswap32(x)
  107. #define cpu_to_le16(x) (x)
  108. #define cpu_to_le32(x) (x)
  109. #define cpu_to_be32(x) bswap32(x)
  110. #else
  111. #define le16_to_cpu(x) bswap16(x)
  112. #define le32_to_cpu(x) bswap32(x)
  113. #define le64_to_cpu(x) bswap64(x)
  114. #define be16_to_cpu(x) (x)
  115. #define be32_to_cpu(x) (x)
  116. #define cpu_to_le16(x) bswap16(x)
  117. #define cpu_to_le32(x) bswap32(x)
  118. #define cpu_to_be32(x) (x)
  119. #endif
  120. enum uuid_format {
  121. UUID_DCE_STRING,
  122. UUID_DCE,
  123. UUID_DOS,
  124. UUID_NTFS,
  125. UUID_HFS,
  126. };
  127. enum endian {
  128. LE = 0,
  129. BE = 1
  130. };
  131. void volume_id_set_unicode16(char *str, size_t len, const uint8_t *buf, enum endian endianess, size_t count);
  132. //void volume_id_set_usage(struct volume_id *id, enum volume_id_usage usage_id);
  133. //void volume_id_set_usage_part(struct volume_id_partition *part, enum volume_id_usage usage_id);
  134. //void volume_id_set_label_raw(struct volume_id *id, const uint8_t *buf, size_t count);
  135. void volume_id_set_label_string(struct volume_id *id, const uint8_t *buf, size_t count);
  136. void volume_id_set_label_unicode16(struct volume_id *id, const uint8_t *buf, enum endian endianess, size_t count);
  137. void volume_id_set_uuid(struct volume_id *id, const uint8_t *buf, enum uuid_format format);
  138. void *volume_id_get_buffer(struct volume_id *id, uint64_t off, size_t len);
  139. void volume_id_free_buffer(struct volume_id *id);
  140. /* Probe routines */
  141. /* RAID */
  142. //int FAST_FUNC volume_id_probe_highpoint_37x_raid(struct volume_id *id /*,uint64_t off*/);
  143. //int FAST_FUNC volume_id_probe_highpoint_45x_raid(struct volume_id *id /*,uint64_t off*/, uint64_t size);
  144. //int FAST_FUNC volume_id_probe_intel_software_raid(struct volume_id *id /*,uint64_t off*/, uint64_t size);
  145. int FAST_FUNC volume_id_probe_linux_raid(struct volume_id *id /*,uint64_t off*/, uint64_t size);
  146. //int FAST_FUNC volume_id_probe_lsi_mega_raid(struct volume_id *id /*,uint64_t off*/, uint64_t size);
  147. //int FAST_FUNC volume_id_probe_nvidia_raid(struct volume_id *id /*,uint64_t off*/, uint64_t size);
  148. //int FAST_FUNC volume_id_probe_promise_fasttrack_raid(struct volume_id *id /*,uint64_t off*/, uint64_t size);
  149. //int FAST_FUNC volume_id_probe_silicon_medley_raid(struct volume_id *id /*,uint64_t off*/, uint64_t size);
  150. //int FAST_FUNC volume_id_probe_via_raid(struct volume_id *id /*,uint64_t off*/, uint64_t size);
  151. //int FAST_FUNC volume_id_probe_lvm1(struct volume_id *id /*,uint64_t off*/);
  152. //int FAST_FUNC volume_id_probe_lvm2(struct volume_id *id /*,uint64_t off*/);
  153. /* FS */
  154. int FAST_FUNC volume_id_probe_btrfs(struct volume_id *id /*,uint64_t off*/);
  155. int FAST_FUNC volume_id_probe_cramfs(struct volume_id *id /*,uint64_t off*/);
  156. int FAST_FUNC volume_id_probe_ext(struct volume_id *id /*,uint64_t off*/);
  157. int FAST_FUNC volume_id_probe_vfat(struct volume_id *id /*,uint64_t off*/);
  158. int FAST_FUNC volume_id_probe_hfs_hfsplus(struct volume_id *id /*,uint64_t off*/);
  159. //int FAST_FUNC volume_id_probe_hpfs(struct volume_id *id /*,uint64_t off*/);
  160. int FAST_FUNC volume_id_probe_iso9660(struct volume_id *id /*,uint64_t off*/);
  161. int FAST_FUNC volume_id_probe_jfs(struct volume_id *id /*,uint64_t off*/);
  162. int FAST_FUNC volume_id_probe_linux_swap(struct volume_id *id /*,uint64_t off*/);
  163. int FAST_FUNC volume_id_probe_luks(struct volume_id *id /*,uint64_t off*/);
  164. //int FAST_FUNC volume_id_probe_mac_partition_map(struct volume_id *id /*,uint64_t off*/);
  165. //int FAST_FUNC volume_id_probe_minix(struct volume_id *id /*,uint64_t off*/);
  166. //int FAST_FUNC volume_id_probe_msdos_part_table(struct volume_id *id /*,uint64_t off*/);
  167. int FAST_FUNC volume_id_probe_ntfs(struct volume_id *id /*,uint64_t off*/);
  168. int FAST_FUNC volume_id_probe_ocfs2(struct volume_id *id /*,uint64_t off*/);
  169. int FAST_FUNC volume_id_probe_reiserfs(struct volume_id *id /*,uint64_t off*/);
  170. int FAST_FUNC volume_id_probe_romfs(struct volume_id *id /*,uint64_t off*/);
  171. int FAST_FUNC volume_id_probe_sysv(struct volume_id *id /*,uint64_t off*/);
  172. int FAST_FUNC volume_id_probe_udf(struct volume_id *id /*,uint64_t off*/);
  173. //int FAST_FUNC volume_id_probe_ufs(struct volume_id *id /*,uint64_t off*/);
  174. int FAST_FUNC volume_id_probe_xfs(struct volume_id *id /*,uint64_t off*/);
  175. POP_SAVED_FUNCTION_VISIBILITY