unused_msdos.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 msdos_partition_entry {
  22. uint8_t boot_ind;
  23. uint8_t head;
  24. uint8_t sector;
  25. uint8_t cyl;
  26. uint8_t sys_ind;
  27. uint8_t end_head;
  28. uint8_t end_sector;
  29. uint8_t end_cyl;
  30. uint32_t start_sect;
  31. uint32_t nr_sects;
  32. } PACKED;
  33. #define MSDOS_PARTTABLE_OFFSET 0x1be
  34. #define MSDOS_SIG_OFF 0x1fe
  35. #define BSIZE 0x200
  36. #define DOS_EXTENDED_PARTITION 0x05
  37. #define LINUX_EXTENDED_PARTITION 0x85
  38. #define WIN98_EXTENDED_PARTITION 0x0f
  39. #define LINUX_RAID_PARTITION 0xfd
  40. #define is_extended(type) \
  41. (type == DOS_EXTENDED_PARTITION || \
  42. type == WIN98_EXTENDED_PARTITION || \
  43. type == LINUX_EXTENDED_PARTITION)
  44. #define is_raid(type) \
  45. (type == LINUX_RAID_PARTITION)
  46. int FAST_FUNC volume_id_probe_msdos_part_table(struct volume_id *id, uint64_t off)
  47. {
  48. const uint8_t *buf;
  49. int i;
  50. uint64_t poff;
  51. uint64_t plen;
  52. uint64_t extended = 0;
  53. uint64_t current;
  54. uint64_t next;
  55. int limit;
  56. int empty = 1;
  57. struct msdos_partition_entry *part;
  58. struct volume_id_partition *p;
  59. dbg("probing at offset 0x%llx", (unsigned long long) off);
  60. buf = volume_id_get_buffer(id, off, 0x200);
  61. if (buf == NULL)
  62. return -1;
  63. if (buf[MSDOS_SIG_OFF] != 0x55 || buf[MSDOS_SIG_OFF + 1] != 0xaa)
  64. return -1;
  65. /* check flags on all entries for a valid partition table */
  66. part = (struct msdos_partition_entry*) &buf[MSDOS_PARTTABLE_OFFSET];
  67. for (i = 0; i < 4; i++) {
  68. if (part[i].boot_ind != 0
  69. && part[i].boot_ind != 0x80
  70. ) {
  71. return -1;
  72. }
  73. if (part[i].nr_sects != 0)
  74. empty = 0;
  75. }
  76. if (empty == 1)
  77. return -1;
  78. if (id->partitions != NULL)
  79. free(id->partitions);
  80. id->partitions = xzalloc(VOLUME_ID_PARTITIONS_MAX *
  81. sizeof(struct volume_id_partition));
  82. for (i = 0; i < 4; i++) {
  83. poff = (uint64_t) le32_to_cpu(part[i].start_sect) * BSIZE;
  84. plen = (uint64_t) le32_to_cpu(part[i].nr_sects) * BSIZE;
  85. if (plen == 0)
  86. continue;
  87. p = &id->partitions[i];
  88. // p->pt_type_raw = part[i].sys_ind;
  89. if (is_extended(part[i].sys_ind)) {
  90. dbg("found extended partition at 0x%llx", (unsigned long long) poff);
  91. // volume_id_set_usage_part(p, VOLUME_ID_PARTITIONTABLE);
  92. // p->type = "msdos_extended_partition";
  93. if (extended == 0)
  94. extended = off + poff;
  95. } else {
  96. dbg("found 0x%x data partition at 0x%llx, len 0x%llx",
  97. part[i].sys_ind, (unsigned long long) poff, (unsigned long long) plen);
  98. // if (is_raid(part[i].sys_ind))
  99. // volume_id_set_usage_part(p, VOLUME_ID_RAID);
  100. // else
  101. // volume_id_set_usage_part(p, VOLUME_ID_UNPROBED);
  102. }
  103. // p->pt_off = off + poff;
  104. // p->pt_len = plen;
  105. id->partition_count = i+1;
  106. }
  107. next = extended;
  108. current = extended;
  109. limit = 50;
  110. /* follow extended partition chain and add data partitions */
  111. while (next != 0) {
  112. if (limit-- == 0) {
  113. dbg("extended chain limit reached");
  114. break;
  115. }
  116. buf = volume_id_get_buffer(id, current, 0x200);
  117. if (buf == NULL)
  118. break;
  119. part = (struct msdos_partition_entry*) &buf[MSDOS_PARTTABLE_OFFSET];
  120. if (buf[MSDOS_SIG_OFF] != 0x55 || buf[MSDOS_SIG_OFF + 1] != 0xaa)
  121. break;
  122. next = 0;
  123. for (i = 0; i < 4; i++) {
  124. poff = (uint64_t) le32_to_cpu(part[i].start_sect) * BSIZE;
  125. plen = (uint64_t) le32_to_cpu(part[i].nr_sects) * BSIZE;
  126. if (plen == 0)
  127. continue;
  128. if (is_extended(part[i].sys_ind)) {
  129. dbg("found extended partition at 0x%llx", (unsigned long long) poff);
  130. if (next == 0)
  131. next = extended + poff;
  132. } else {
  133. dbg("found 0x%x data partition at 0x%llx, len 0x%llx",
  134. part[i].sys_ind, (unsigned long long) poff, (unsigned long long) plen);
  135. /* we always start at the 5th entry */
  136. // while (id->partition_count < 4)
  137. // volume_id_set_usage_part(&id->partitions[id->partition_count++], VOLUME_ID_UNUSED);
  138. if (id->partition_count < 4)
  139. id->partition_count = 4;
  140. p = &id->partitions[id->partition_count];
  141. // if (is_raid(part[i].sys_ind))
  142. // volume_id_set_usage_part(p, VOLUME_ID_RAID);
  143. // else
  144. // volume_id_set_usage_part(p, VOLUME_ID_UNPROBED);
  145. // p->pt_off = current + poff;
  146. // p->pt_len = plen;
  147. id->partition_count++;
  148. // p->pt_type_raw = part[i].sys_ind;
  149. if (id->partition_count >= VOLUME_ID_PARTITIONS_MAX) {
  150. dbg("too many partitions");
  151. next = 0;
  152. }
  153. }
  154. }
  155. current = next;
  156. }
  157. // volume_id_set_usage(id, VOLUME_ID_PARTITIONTABLE);
  158. // id->type = "msdos_partition_table";
  159. return 0;
  160. }