3
0

unused_msdos.c 5.3 KB

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