msdos.c 13 KB

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