msdos.c 15 KB

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