volume_id_internal.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. #if __GNUC_PREREQ(4,1)
  23. # pragma GCC visibility push(hidden)
  24. #endif
  25. #define dbg(...) ((void)0)
  26. /* #define dbg(...) bb_error_msg(__VA_ARGS__) */
  27. /* volume_id.h */
  28. #define VOLUME_ID_VERSION 48
  29. #define VOLUME_ID_LABEL_SIZE 64
  30. #define VOLUME_ID_UUID_SIZE 36
  31. #define VOLUME_ID_FORMAT_SIZE 32
  32. #define VOLUME_ID_PARTITIONS_MAX 256
  33. enum volume_id_usage {
  34. VOLUME_ID_UNUSED,
  35. VOLUME_ID_UNPROBED,
  36. VOLUME_ID_OTHER,
  37. VOLUME_ID_FILESYSTEM,
  38. VOLUME_ID_PARTITIONTABLE,
  39. VOLUME_ID_RAID,
  40. VOLUME_ID_DISKLABEL,
  41. VOLUME_ID_CRYPTO,
  42. };
  43. #ifdef UNUSED_PARTITION_CODE
  44. struct volume_id_partition {
  45. // const char *type;
  46. // const char *usage;
  47. // smallint usage_id;
  48. // uint8_t pt_type_raw;
  49. // uint64_t pt_off;
  50. // uint64_t pt_len;
  51. };
  52. #endif
  53. struct volume_id {
  54. // uint8_t label_raw[VOLUME_ID_LABEL_SIZE];
  55. // size_t label_raw_len;
  56. char label[VOLUME_ID_LABEL_SIZE+1];
  57. // uint8_t uuid_raw[VOLUME_ID_UUID_SIZE];
  58. // size_t uuid_raw_len;
  59. /* uuid is stored in ASCII (not binary) form here: */
  60. char uuid[VOLUME_ID_UUID_SIZE+1];
  61. // char type_version[VOLUME_ID_FORMAT_SIZE];
  62. // smallint usage_id;
  63. // const char *usage;
  64. // const char *type;
  65. #ifdef UNUSED_PARTITION_CODE
  66. struct volume_id_partition *partitions;
  67. size_t partition_count;
  68. #endif
  69. int fd;
  70. uint8_t *sbbuf;
  71. uint8_t *seekbuf;
  72. size_t sbbuf_len;
  73. uint64_t seekbuf_off;
  74. size_t seekbuf_len;
  75. // int fd_close:1;
  76. };
  77. struct volume_id *volume_id_open_node(const char *path);
  78. int volume_id_probe_all(struct volume_id *id, uint64_t off, uint64_t size);
  79. void free_volume_id(struct volume_id *id);
  80. /* util.h */
  81. /* size of superblock buffer, reiserfs block is at 64k */
  82. #define SB_BUFFER_SIZE 0x11000
  83. /* size of seek buffer, FAT cluster is 32k max */
  84. #define SEEK_BUFFER_SIZE 0x10000
  85. #define bswap16(x) (uint16_t) ( \
  86. (((uint16_t)(x) & 0x00ffu) << 8) | \
  87. (((uint16_t)(x) & 0xff00u) >> 8))
  88. #define bswap32(x) (uint32_t) ( \
  89. (((uint32_t)(x) & 0xff000000u) >> 24) | \
  90. (((uint32_t)(x) & 0x00ff0000u) >> 8) | \
  91. (((uint32_t)(x) & 0x0000ff00u) << 8) | \
  92. (((uint32_t)(x) & 0x000000ffu) << 24))
  93. #define bswap64(x) (uint64_t) ( \
  94. (((uint64_t)(x) & 0xff00000000000000ull) >> 56) | \
  95. (((uint64_t)(x) & 0x00ff000000000000ull) >> 40) | \
  96. (((uint64_t)(x) & 0x0000ff0000000000ull) >> 24) | \
  97. (((uint64_t)(x) & 0x000000ff00000000ull) >> 8) | \
  98. (((uint64_t)(x) & 0x00000000ff000000ull) << 8) | \
  99. (((uint64_t)(x) & 0x0000000000ff0000ull) << 24) | \
  100. (((uint64_t)(x) & 0x000000000000ff00ull) << 40) | \
  101. (((uint64_t)(x) & 0x00000000000000ffull) << 56))
  102. #if BB_LITTLE_ENDIAN
  103. #define le16_to_cpu(x) (x)
  104. #define le32_to_cpu(x) (x)
  105. #define le64_to_cpu(x) (x)
  106. #define be16_to_cpu(x) bswap16(x)
  107. #define be32_to_cpu(x) bswap32(x)
  108. #define cpu_to_le16(x) (x)
  109. #define cpu_to_le32(x) (x)
  110. #define cpu_to_be32(x) bswap32(x)
  111. #else
  112. #define le16_to_cpu(x) bswap16(x)
  113. #define le32_to_cpu(x) bswap32(x)
  114. #define le64_to_cpu(x) bswap64(x)
  115. #define be16_to_cpu(x) (x)
  116. #define be32_to_cpu(x) (x)
  117. #define cpu_to_le16(x) bswap16(x)
  118. #define cpu_to_le32(x) bswap32(x)
  119. #define cpu_to_be32(x) (x)
  120. #endif
  121. enum uuid_format {
  122. UUID_DCE_STRING,
  123. UUID_DCE,
  124. UUID_DOS,
  125. UUID_NTFS,
  126. UUID_HFS,
  127. };
  128. enum endian {
  129. LE = 0,
  130. BE = 1
  131. };
  132. void volume_id_set_unicode16(char *str, size_t len, const uint8_t *buf, enum endian endianess, size_t count);
  133. //void volume_id_set_usage(struct volume_id *id, enum volume_id_usage usage_id);
  134. //void volume_id_set_usage_part(struct volume_id_partition *part, enum volume_id_usage usage_id);
  135. //void volume_id_set_label_raw(struct volume_id *id, const uint8_t *buf, size_t count);
  136. void volume_id_set_label_string(struct volume_id *id, const uint8_t *buf, size_t count);
  137. void volume_id_set_label_unicode16(struct volume_id *id, const uint8_t *buf, enum endian endianess, size_t count);
  138. void volume_id_set_uuid(struct volume_id *id, const uint8_t *buf, enum uuid_format format);
  139. void *volume_id_get_buffer(struct volume_id *id, uint64_t off, size_t len);
  140. void volume_id_free_buffer(struct volume_id *id);
  141. /* Probe routines */
  142. /* RAID */
  143. //int volume_id_probe_highpoint_37x_raid(struct volume_id *id, uint64_t off);
  144. //int volume_id_probe_highpoint_45x_raid(struct volume_id *id, uint64_t off, uint64_t size);
  145. //int volume_id_probe_intel_software_raid(struct volume_id *id, uint64_t off, uint64_t size);
  146. int volume_id_probe_linux_raid(struct volume_id *id, uint64_t off, uint64_t size);
  147. //int volume_id_probe_lsi_mega_raid(struct volume_id *id, uint64_t off, uint64_t size);
  148. //int volume_id_probe_nvidia_raid(struct volume_id *id, uint64_t off, uint64_t size);
  149. //int volume_id_probe_promise_fasttrack_raid(struct volume_id *id, uint64_t off, uint64_t size);
  150. //int volume_id_probe_silicon_medley_raid(struct volume_id *id, uint64_t off, uint64_t size);
  151. //int volume_id_probe_via_raid(struct volume_id *id, uint64_t off, uint64_t size);
  152. //int volume_id_probe_lvm1(struct volume_id *id, uint64_t off);
  153. //int volume_id_probe_lvm2(struct volume_id *id, uint64_t off);
  154. /* FS */
  155. int volume_id_probe_cramfs(struct volume_id *id, uint64_t off);
  156. int volume_id_probe_ext(struct volume_id *id, uint64_t off);
  157. int volume_id_probe_vfat(struct volume_id *id, uint64_t off);
  158. int volume_id_probe_hfs_hfsplus(struct volume_id *id, uint64_t off);
  159. //int volume_id_probe_hpfs(struct volume_id *id, uint64_t off);
  160. int volume_id_probe_iso9660(struct volume_id *id, uint64_t off);
  161. int volume_id_probe_jfs(struct volume_id *id, uint64_t off);
  162. int volume_id_probe_linux_swap(struct volume_id *id, uint64_t off);
  163. int volume_id_probe_luks(struct volume_id *id, uint64_t off);
  164. //int volume_id_probe_mac_partition_map(struct volume_id *id, uint64_t off);
  165. //int volume_id_probe_minix(struct volume_id *id, uint64_t off);
  166. //int volume_id_probe_msdos_part_table(struct volume_id *id, uint64_t off);
  167. int volume_id_probe_ntfs(struct volume_id *id, uint64_t off);
  168. int volume_id_probe_ocfs2(struct volume_id *id, uint64_t off);
  169. int volume_id_probe_reiserfs(struct volume_id *id, uint64_t off);
  170. int volume_id_probe_romfs(struct volume_id *id, uint64_t off);
  171. int volume_id_probe_sysv(struct volume_id *id, uint64_t off);
  172. int volume_id_probe_udf(struct volume_id *id, uint64_t off);
  173. //int volume_id_probe_ufs(struct volume_id *id, uint64_t off);
  174. int volume_id_probe_xfs(struct volume_id *id, uint64_t off);
  175. #if __GNUC_PREREQ(4,1)
  176. # pragma GCC visibility pop
  177. #endif