udf.c 4.4 KB

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