msdos.c 14 KB

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