msdos.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. /*
  2. * fs/partitions/msdos.c
  3. *
  4. * Code extracted from drivers/block/genhd.c
  5. * Copyright (C) 1991-1998 Linus Torvalds
  6. *
  7. * Thanks to Branko Lankester, lankeste@fwi.uva.nl, who found a bug
  8. * in the early extended-partition checks and added DM partitions
  9. *
  10. * Support for DiskManager v6.0x added by Mark Lord,
  11. * with information provided by OnTrack. This now works for linux fdisk
  12. * and LILO, as well as loadlin and bootln. Note that disks other than
  13. * /dev/hda *must* have a "DOS" type 0x51 partition in the first slot (hda1).
  14. *
  15. * More flexible handling of extended partitions - aeb, 950831
  16. *
  17. * Check partition table on IDE disks for common CHS translations
  18. *
  19. * Re-organised Feb 1998 Russell King
  20. */
  21. #include "check.h"
  22. #include "msdos.h"
  23. #include "efi.h"
  24. /*
  25. * Many architectures don't like unaligned accesses, while
  26. * the nr_sects and start_sect partition table entries are
  27. * at a 2 (mod 4) address.
  28. */
  29. #include <asm/unaligned.h>
  30. #define SYS_IND(p) (get_unaligned(&p->sys_ind))
  31. #define NR_SECTS(p) ({ __typeof__(p->nr_sects) __a = \
  32. get_unaligned(&p->nr_sects); \
  33. le32_to_cpu(__a); \
  34. })
  35. #define START_SECT(p) ({ __typeof__(p->start_sect) __a = \
  36. get_unaligned(&p->start_sect); \
  37. le32_to_cpu(__a); \
  38. })
  39. static inline int is_extended_partition(struct partition *p)
  40. {
  41. return (SYS_IND(p) == DOS_EXTENDED_PARTITION ||
  42. SYS_IND(p) == WIN98_EXTENDED_PARTITION ||
  43. SYS_IND(p) == LINUX_EXTENDED_PARTITION);
  44. }
  45. #define MSDOS_LABEL_MAGIC1 0x55
  46. #define MSDOS_LABEL_MAGIC2 0xAA
  47. static inline int
  48. msdos_magic_present(unsigned char *p)
  49. {
  50. return (p[0] == MSDOS_LABEL_MAGIC1 && p[1] == MSDOS_LABEL_MAGIC2);
  51. }
  52. /*
  53. * Create devices for each logical partition in an extended partition.
  54. * The logical partitions form a linked list, with each entry being
  55. * a partition table with two entries. The first entry
  56. * is the real data partition (with a start relative to the partition
  57. * table start). The second is a pointer to the next logical partition
  58. * (with a start relative to the entire extended partition).
  59. * We do not create a Linux partition for the partition tables, but
  60. * only for the actual data partitions.
  61. */
  62. static void
  63. parse_extended(struct parsed_partitions *state, struct block_device *bdev,
  64. u32 first_sector, u32 first_size)
  65. {
  66. struct partition *p;
  67. Sector sect;
  68. unsigned char *data;
  69. u32 this_sector, this_size;
  70. int sector_size = bdev_hardsect_size(bdev) / 512;
  71. int loopct = 0; /* number of links followed
  72. without finding a data partition */
  73. int i;
  74. this_sector = first_sector;
  75. this_size = first_size;
  76. while (1) {
  77. if (++loopct > 100)
  78. return;
  79. if (state->next == state->limit)
  80. return;
  81. data = read_dev_sector(bdev, this_sector, &sect);
  82. if (!data)
  83. return;
  84. if (!msdos_magic_present(data + 510))
  85. goto done;
  86. p = (struct partition *) (data + 0x1be);
  87. /*
  88. * Usually, the first entry is the real data partition,
  89. * the 2nd entry is the next extended partition, or empty,
  90. * and the 3rd and 4th entries are unused.
  91. * However, DRDOS sometimes has the extended partition as
  92. * the first entry (when the data partition is empty),
  93. * and OS/2 seems to use all four entries.
  94. */
  95. /*
  96. * First process the data partition(s)
  97. */
  98. for (i=0; i<4; i++, p++) {
  99. u32 offs, size, next;
  100. if (!NR_SECTS(p) || is_extended_partition(p))
  101. continue;
  102. /* Check the 3rd and 4th entries -
  103. these sometimes contain random garbage */
  104. offs = START_SECT(p)*sector_size;
  105. size = NR_SECTS(p)*sector_size;
  106. next = this_sector + offs;
  107. if (i >= 2) {
  108. if (offs + size > this_size)
  109. continue;
  110. if (next < first_sector)
  111. continue;
  112. if (next + size > first_sector + first_size)
  113. continue;
  114. }
  115. put_partition(state, state->next, next, size);
  116. if (SYS_IND(p) == LINUX_RAID_PARTITION)
  117. state->parts[state->next].flags = 1;
  118. loopct = 0;
  119. if (++state->next == state->limit)
  120. goto done;
  121. }
  122. /*
  123. * Next, process the (first) extended partition, if present.
  124. * (So far, there seems to be no reason to make
  125. * parse_extended() recursive and allow a tree
  126. * of extended partitions.)
  127. * It should be a link to the next logical partition.
  128. */
  129. p -= 4;
  130. for (i=0; i<4; i++, p++)
  131. if (NR_SECTS(p) && is_extended_partition(p))
  132. break;
  133. if (i == 4)
  134. goto done; /* nothing left to do */
  135. this_sector = first_sector + START_SECT(p) * sector_size;
  136. this_size = NR_SECTS(p) * sector_size;
  137. put_dev_sector(sect);
  138. }
  139. done:
  140. put_dev_sector(sect);
  141. }
  142. /* james@bpgc.com: Solaris has a nasty indicator: 0x82 which also
  143. indicates linux swap. Be careful before believing this is Solaris. */
  144. static void
  145. parse_solaris_x86(struct parsed_partitions *state, struct block_device *bdev,
  146. u32 offset, u32 size, int origin)
  147. {
  148. #ifdef CONFIG_SOLARIS_X86_PARTITION
  149. Sector sect;
  150. struct solaris_x86_vtoc *v;
  151. int i;
  152. v = (struct solaris_x86_vtoc *)read_dev_sector(bdev, offset+1, &sect);
  153. if (!v)
  154. return;
  155. if (le32_to_cpu(v->v_sanity) != SOLARIS_X86_VTOC_SANE) {
  156. put_dev_sector(sect);
  157. return;
  158. }
  159. printk(" %s%d: <solaris:", state->name, origin);
  160. if (le32_to_cpu(v->v_version) != 1) {
  161. printk(" cannot handle version %d vtoc>\n",
  162. le32_to_cpu(v->v_version));
  163. put_dev_sector(sect);
  164. return;
  165. }
  166. for (i=0; i<SOLARIS_X86_NUMSLICE && state->next<state->limit; i++) {
  167. struct solaris_x86_slice *s = &v->v_slice[i];
  168. if (s->s_size == 0)
  169. continue;
  170. printk(" [s%d]", i);
  171. /* solaris partitions are relative to current MS-DOS
  172. * one; must add the offset of the current partition */
  173. put_partition(state, state->next++,
  174. le32_to_cpu(s->s_start)+offset,
  175. le32_to_cpu(s->s_size));
  176. }
  177. put_dev_sector(sect);
  178. printk(" >\n");
  179. #endif
  180. }
  181. #if defined(CONFIG_BSD_DISKLABEL)
  182. /*
  183. * Create devices for BSD partitions listed in a disklabel, under a
  184. * dos-like partition. See parse_extended() for more information.
  185. */
  186. static void
  187. parse_bsd(struct parsed_partitions *state, struct block_device *bdev,
  188. u32 offset, u32 size, int origin, char *flavour,
  189. int max_partitions)
  190. {
  191. Sector sect;
  192. struct bsd_disklabel *l;
  193. struct bsd_partition *p;
  194. l = (struct bsd_disklabel *)read_dev_sector(bdev, offset+1, &sect);
  195. if (!l)
  196. return;
  197. if (le32_to_cpu(l->d_magic) != BSD_DISKMAGIC) {
  198. put_dev_sector(sect);
  199. return;
  200. }
  201. printk(" %s%d: <%s:", state->name, origin, flavour);
  202. if (le16_to_cpu(l->d_npartitions) < max_partitions)
  203. max_partitions = le16_to_cpu(l->d_npartitions);
  204. for (p = l->d_partitions; p - l->d_partitions < max_partitions; p++) {
  205. u32 bsd_start, bsd_size;
  206. if (state->next == state->limit)
  207. break;
  208. if (p->p_fstype == BSD_FS_UNUSED)
  209. continue;
  210. bsd_start = le32_to_cpu(p->p_offset);
  211. bsd_size = le32_to_cpu(p->p_size);
  212. if (offset == bsd_start && size == bsd_size)
  213. /* full parent partition, we have it already */
  214. continue;
  215. if (offset > bsd_start || offset+size < bsd_start+bsd_size) {
  216. printk("bad subpartition - ignored\n");
  217. continue;
  218. }
  219. put_partition(state, state->next++, bsd_start, bsd_size);
  220. }
  221. put_dev_sector(sect);
  222. if (le16_to_cpu(l->d_npartitions) > max_partitions)
  223. printk(" (ignored %d more)",
  224. le16_to_cpu(l->d_npartitions) - max_partitions);
  225. printk(" >\n");
  226. }
  227. #endif
  228. static void
  229. parse_freebsd(struct parsed_partitions *state, struct block_device *bdev,
  230. u32 offset, u32 size, int origin)
  231. {
  232. #ifdef CONFIG_BSD_DISKLABEL
  233. parse_bsd(state, bdev, offset, size, origin,
  234. "bsd", BSD_MAXPARTITIONS);
  235. #endif
  236. }
  237. static void
  238. parse_netbsd(struct parsed_partitions *state, struct block_device *bdev,
  239. u32 offset, u32 size, int origin)
  240. {
  241. #ifdef CONFIG_BSD_DISKLABEL
  242. parse_bsd(state, bdev, offset, size, origin,
  243. "netbsd", BSD_MAXPARTITIONS);
  244. #endif
  245. }
  246. static void
  247. parse_openbsd(struct parsed_partitions *state, struct block_device *bdev,
  248. u32 offset, u32 size, int origin)
  249. {
  250. #ifdef CONFIG_BSD_DISKLABEL
  251. parse_bsd(state, bdev, offset, size, origin,
  252. "openbsd", OPENBSD_MAXPARTITIONS);
  253. #endif
  254. }
  255. /*
  256. * Create devices for Unixware partitions listed in a disklabel, under a
  257. * dos-like partition. See parse_extended() for more information.
  258. */
  259. static void
  260. parse_unixware(struct parsed_partitions *state, struct block_device *bdev,
  261. u32 offset, u32 size, int origin)
  262. {
  263. #ifdef CONFIG_UNIXWARE_DISKLABEL
  264. Sector sect;
  265. struct unixware_disklabel *l;
  266. struct unixware_slice *p;
  267. l = (struct unixware_disklabel *)read_dev_sector(bdev, offset+29, &sect);
  268. if (!l)
  269. return;
  270. if (le32_to_cpu(l->d_magic) != UNIXWARE_DISKMAGIC ||
  271. le32_to_cpu(l->vtoc.v_magic) != UNIXWARE_DISKMAGIC2) {
  272. put_dev_sector(sect);
  273. return;
  274. }
  275. printk(" %s%d: <unixware:", state->name, origin);
  276. p = &l->vtoc.v_slice[1];
  277. /* I omit the 0th slice as it is the same as whole disk. */
  278. while (p - &l->vtoc.v_slice[0] < UNIXWARE_NUMSLICE) {
  279. if (state->next == state->limit)
  280. break;
  281. if (p->s_label != UNIXWARE_FS_UNUSED)
  282. put_partition(state, state->next++,
  283. START_SECT(p), NR_SECTS(p));
  284. p++;
  285. }
  286. put_dev_sector(sect);
  287. printk(" >\n");
  288. #endif
  289. }
  290. /*
  291. * Minix 2.0.0/2.0.2 subpartition support.
  292. * Anand Krishnamurthy <anandk@wiproge.med.ge.com>
  293. * Rajeev V. Pillai <rajeevvp@yahoo.com>
  294. */
  295. static void
  296. parse_minix(struct parsed_partitions *state, struct block_device *bdev,
  297. u32 offset, u32 size, int origin)
  298. {
  299. #ifdef CONFIG_MINIX_SUBPARTITION
  300. Sector sect;
  301. unsigned char *data;
  302. struct partition *p;
  303. int i;
  304. data = read_dev_sector(bdev, offset, &sect);
  305. if (!data)
  306. return;
  307. p = (struct partition *)(data + 0x1be);
  308. /* The first sector of a Minix partition can have either
  309. * a secondary MBR describing its subpartitions, or
  310. * the normal boot sector. */
  311. if (msdos_magic_present (data + 510) &&
  312. SYS_IND(p) == MINIX_PARTITION) { /* subpartition table present */
  313. printk(" %s%d: <minix:", state->name, origin);
  314. for (i = 0; i < MINIX_NR_SUBPARTITIONS; i++, p++) {
  315. if (state->next == state->limit)
  316. break;
  317. /* add each partition in use */
  318. if (SYS_IND(p) == MINIX_PARTITION)
  319. put_partition(state, state->next++,
  320. START_SECT(p), NR_SECTS(p));
  321. }
  322. printk(" >\n");
  323. }
  324. put_dev_sector(sect);
  325. #endif /* CONFIG_MINIX_SUBPARTITION */
  326. }
  327. static struct {
  328. unsigned char id;
  329. void (*parse)(struct parsed_partitions *, struct block_device *,
  330. u32, u32, int);
  331. } subtypes[] = {
  332. {FREEBSD_PARTITION, parse_freebsd},
  333. {NETBSD_PARTITION, parse_netbsd},
  334. {OPENBSD_PARTITION, parse_openbsd},
  335. {MINIX_PARTITION, parse_minix},
  336. {UNIXWARE_PARTITION, parse_unixware},
  337. {SOLARIS_X86_PARTITION, parse_solaris_x86},
  338. {NEW_SOLARIS_X86_PARTITION, parse_solaris_x86},
  339. {0, NULL},
  340. };
  341. int msdos_partition(struct parsed_partitions *state, struct block_device *bdev)
  342. {
  343. int sector_size = bdev_hardsect_size(bdev) / 512;
  344. Sector sect;
  345. unsigned char *data;
  346. struct partition *p;
  347. int slot;
  348. data = read_dev_sector(bdev, 0, &sect);
  349. if (!data)
  350. return -1;
  351. if (!msdos_magic_present(data + 510)) {
  352. put_dev_sector(sect);
  353. return 0;
  354. }
  355. /*
  356. * Now that the 55aa signature is present, this is probably
  357. * either the boot sector of a FAT filesystem or a DOS-type
  358. * partition table. Reject this in case the boot indicator
  359. * is not 0 or 0x80.
  360. */
  361. p = (struct partition *) (data + 0x1be);
  362. for (slot = 1; slot <= 4; slot++, p++) {
  363. if (p->boot_ind != 0 && p->boot_ind != 0x80) {
  364. put_dev_sector(sect);
  365. return 0;
  366. }
  367. }
  368. #ifdef CONFIG_EFI_PARTITION
  369. p = (struct partition *) (data + 0x1be);
  370. for (slot = 1 ; slot <= 4 ; slot++, p++) {
  371. /* If this is an EFI GPT disk, msdos should ignore it. */
  372. if (SYS_IND(p) == EFI_PMBR_OSTYPE_EFI_GPT) {
  373. put_dev_sector(sect);
  374. return 0;
  375. }
  376. }
  377. #endif
  378. p = (struct partition *) (data + 0x1be);
  379. /*
  380. * Look for partitions in two passes:
  381. * First find the primary and DOS-type extended partitions.
  382. * On the second pass look inside *BSD, Unixware and Solaris partitions.
  383. */
  384. state->next = 5;
  385. for (slot = 1 ; slot <= 4 ; slot++, p++) {
  386. u32 start = START_SECT(p)*sector_size;
  387. u32 size = NR_SECTS(p)*sector_size;
  388. if (!size)
  389. continue;
  390. if (is_extended_partition(p)) {
  391. /* prevent someone doing mkfs or mkswap on an
  392. extended partition, but leave room for LILO */
  393. put_partition(state, slot, start, size == 1 ? 1 : 2);
  394. printk(" <");
  395. parse_extended(state, bdev, start, size);
  396. printk(" >");
  397. continue;
  398. }
  399. put_partition(state, slot, start, size);
  400. if (SYS_IND(p) == LINUX_RAID_PARTITION)
  401. state->parts[slot].flags = 1;
  402. if (SYS_IND(p) == DM6_PARTITION)
  403. printk("[DM]");
  404. if (SYS_IND(p) == EZD_PARTITION)
  405. printk("[EZD]");
  406. }
  407. printk("\n");
  408. /* second pass - output for each on a separate line */
  409. p = (struct partition *) (0x1be + data);
  410. for (slot = 1 ; slot <= 4 ; slot++, p++) {
  411. unsigned char id = SYS_IND(p);
  412. int n;
  413. if (!NR_SECTS(p))
  414. continue;
  415. for (n = 0; subtypes[n].parse && id != subtypes[n].id; n++)
  416. ;
  417. if (!subtypes[n].parse)
  418. continue;
  419. subtypes[n].parse(state, bdev, START_SECT(p)*sector_size,
  420. NR_SECTS(p)*sector_size, slot);
  421. }
  422. put_dev_sector(sect);
  423. return 1;
  424. }