msdos.c 13 KB

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