msdos.c 13 KB

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