msdos.c 14 KB

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