udf.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * volume_id - reads filesystem label and uuid
  3. *
  4. * Copyright (C) 2004 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 "volume_id_internal.h"
  21. struct volume_descriptor {
  22. struct descriptor_tag {
  23. uint16_t id;
  24. uint16_t version;
  25. uint8_t checksum;
  26. uint8_t reserved;
  27. uint16_t serial;
  28. uint16_t crc;
  29. uint16_t crc_len;
  30. uint32_t location;
  31. } PACKED tag;
  32. union {
  33. struct anchor_descriptor {
  34. uint32_t length;
  35. uint32_t location;
  36. } PACKED anchor;
  37. struct primary_descriptor {
  38. uint32_t seq_num;
  39. uint32_t desc_num;
  40. struct dstring {
  41. uint8_t clen;
  42. uint8_t c[31];
  43. } PACKED ident;
  44. } PACKED primary;
  45. } PACKED type;
  46. } PACKED;
  47. struct volume_structure_descriptor {
  48. uint8_t type;
  49. uint8_t id[5];
  50. uint8_t version;
  51. } PACKED;
  52. #define UDF_VSD_OFFSET 0x8000
  53. int FAST_FUNC volume_id_probe_udf(struct volume_id *id /*,uint64_t off*/)
  54. {
  55. #define off ((uint64_t)0)
  56. struct volume_descriptor *vd;
  57. struct volume_structure_descriptor *vsd;
  58. unsigned bs;
  59. unsigned b;
  60. unsigned type;
  61. unsigned count;
  62. unsigned loc;
  63. unsigned clen;
  64. dbg("probing at offset 0x%llx", (unsigned long long) off);
  65. vsd = volume_id_get_buffer(id, off + UDF_VSD_OFFSET, 0x200);
  66. if (vsd == NULL)
  67. return -1;
  68. if (memcmp(vsd->id, "NSR02", 5) == 0)
  69. goto blocksize;
  70. if (memcmp(vsd->id, "NSR03", 5) == 0)
  71. goto blocksize;
  72. if (memcmp(vsd->id, "BEA01", 5) == 0)
  73. goto blocksize;
  74. if (memcmp(vsd->id, "BOOT2", 5) == 0)
  75. goto blocksize;
  76. if (memcmp(vsd->id, "CD001", 5) == 0)
  77. goto blocksize;
  78. if (memcmp(vsd->id, "CDW02", 5) == 0)
  79. goto blocksize;
  80. if (memcmp(vsd->id, "TEA03", 5) == 0)
  81. goto blocksize;
  82. return -1;
  83. blocksize:
  84. /* search the next VSD to get the logical block size of the volume */
  85. for (bs = 0x800; bs < 0x8000; bs += 0x800) {
  86. vsd = volume_id_get_buffer(id, off + UDF_VSD_OFFSET + bs, 0x800);
  87. if (vsd == NULL)
  88. return -1;
  89. dbg("test for blocksize: 0x%x", bs);
  90. if (vsd->id[0] != '\0')
  91. goto nsr;
  92. }
  93. return -1;
  94. nsr:
  95. /* search the list of VSDs for a NSR descriptor */
  96. for (b = 0; b < 64; b++) {
  97. vsd = volume_id_get_buffer(id, off + UDF_VSD_OFFSET + (b * bs), 0x800);
  98. if (vsd == NULL)
  99. return -1;
  100. dbg("vsd: %c%c%c%c%c",
  101. vsd->id[0], vsd->id[1], vsd->id[2], vsd->id[3], vsd->id[4]);
  102. if (vsd->id[0] == '\0')
  103. return -1;
  104. if (memcmp(vsd->id, "NSR02", 5) == 0)
  105. goto anchor;
  106. if (memcmp(vsd->id, "NSR03", 5) == 0)
  107. goto anchor;
  108. }
  109. return -1;
  110. anchor:
  111. /* read anchor volume descriptor */
  112. vd = volume_id_get_buffer(id, off + (256 * bs), 0x200);
  113. if (vd == NULL)
  114. return -1;
  115. type = le16_to_cpu(vd->tag.id);
  116. if (type != 2) /* TAG_ID_AVDP */
  117. goto found;
  118. /* get desriptor list address and block count */
  119. count = le32_to_cpu(vd->type.anchor.length) / bs;
  120. loc = le32_to_cpu(vd->type.anchor.location);
  121. dbg("0x%x descriptors starting at logical secor 0x%x", count, loc);
  122. /* pick the primary descriptor from the list */
  123. for (b = 0; b < count; b++) {
  124. vd = volume_id_get_buffer(id, off + ((loc + b) * bs), 0x200);
  125. if (vd == NULL)
  126. return -1;
  127. type = le16_to_cpu(vd->tag.id);
  128. dbg("descriptor type %i", type);
  129. /* check validity */
  130. if (type == 0)
  131. goto found;
  132. if (le32_to_cpu(vd->tag.location) != loc + b)
  133. goto found;
  134. if (type == 1) /* TAG_ID_PVD */
  135. goto pvd;
  136. }
  137. goto found;
  138. pvd:
  139. // volume_id_set_label_raw(id, &(vd->type.primary.ident.clen), 32);
  140. clen = vd->type.primary.ident.clen;
  141. dbg("label string charsize=%i bit", clen);
  142. if (clen == 8)
  143. volume_id_set_label_string(id, vd->type.primary.ident.c, 31);
  144. else if (clen == 16)
  145. volume_id_set_label_unicode16(id, vd->type.primary.ident.c, BE, 31);
  146. found:
  147. // volume_id_set_usage(id, VOLUME_ID_FILESYSTEM);
  148. // id->type = "udf";
  149. return 0;
  150. }