udf.c 4.5 KB

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