unused_msdos.c 5.3 KB

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