msdos.c 13 KB

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