msdos.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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. v = (struct solaris_x86_vtoc *)read_dev_sector(bdev, offset+1, &sect);
  184. if (!v)
  185. return;
  186. if (le32_to_cpu(v->v_sanity) != SOLARIS_X86_VTOC_SANE) {
  187. put_dev_sector(sect);
  188. return;
  189. }
  190. printk(" %s%d: <solaris:", state->name, origin);
  191. if (le32_to_cpu(v->v_version) != 1) {
  192. printk(" cannot handle version %d vtoc>\n",
  193. le32_to_cpu(v->v_version));
  194. put_dev_sector(sect);
  195. return;
  196. }
  197. for (i=0; i<SOLARIS_X86_NUMSLICE && state->next<state->limit; i++) {
  198. struct solaris_x86_slice *s = &v->v_slice[i];
  199. if (s->s_size == 0)
  200. continue;
  201. printk(" [s%d]", i);
  202. /* solaris partitions are relative to current MS-DOS
  203. * one; must add the offset of the current partition */
  204. put_partition(state, state->next++,
  205. le32_to_cpu(s->s_start)+offset,
  206. le32_to_cpu(s->s_size));
  207. }
  208. put_dev_sector(sect);
  209. printk(" >\n");
  210. #endif
  211. }
  212. #if defined(CONFIG_BSD_DISKLABEL)
  213. /*
  214. * Create devices for BSD partitions listed in a disklabel, under a
  215. * dos-like partition. See parse_extended() for more information.
  216. */
  217. static void
  218. parse_bsd(struct parsed_partitions *state, struct block_device *bdev,
  219. u32 offset, u32 size, int origin, char *flavour,
  220. int max_partitions)
  221. {
  222. Sector sect;
  223. struct bsd_disklabel *l;
  224. struct bsd_partition *p;
  225. l = (struct bsd_disklabel *)read_dev_sector(bdev, offset+1, &sect);
  226. if (!l)
  227. return;
  228. if (le32_to_cpu(l->d_magic) != BSD_DISKMAGIC) {
  229. put_dev_sector(sect);
  230. return;
  231. }
  232. printk(" %s%d: <%s:", state->name, origin, flavour);
  233. if (le16_to_cpu(l->d_npartitions) < max_partitions)
  234. max_partitions = le16_to_cpu(l->d_npartitions);
  235. for (p = l->d_partitions; p - l->d_partitions < max_partitions; p++) {
  236. u32 bsd_start, bsd_size;
  237. if (state->next == state->limit)
  238. break;
  239. if (p->p_fstype == BSD_FS_UNUSED)
  240. continue;
  241. bsd_start = le32_to_cpu(p->p_offset);
  242. bsd_size = le32_to_cpu(p->p_size);
  243. if (offset == bsd_start && size == bsd_size)
  244. /* full parent partition, we have it already */
  245. continue;
  246. if (offset > bsd_start || offset+size < bsd_start+bsd_size) {
  247. printk("bad subpartition - ignored\n");
  248. continue;
  249. }
  250. put_partition(state, state->next++, bsd_start, bsd_size);
  251. }
  252. put_dev_sector(sect);
  253. if (le16_to_cpu(l->d_npartitions) > max_partitions)
  254. printk(" (ignored %d more)",
  255. le16_to_cpu(l->d_npartitions) - max_partitions);
  256. printk(" >\n");
  257. }
  258. #endif
  259. static void
  260. parse_freebsd(struct parsed_partitions *state, struct block_device *bdev,
  261. u32 offset, u32 size, int origin)
  262. {
  263. #ifdef CONFIG_BSD_DISKLABEL
  264. parse_bsd(state, bdev, offset, size, origin,
  265. "bsd", BSD_MAXPARTITIONS);
  266. #endif
  267. }
  268. static void
  269. parse_netbsd(struct parsed_partitions *state, struct block_device *bdev,
  270. u32 offset, u32 size, int origin)
  271. {
  272. #ifdef CONFIG_BSD_DISKLABEL
  273. parse_bsd(state, bdev, offset, size, origin,
  274. "netbsd", BSD_MAXPARTITIONS);
  275. #endif
  276. }
  277. static void
  278. parse_openbsd(struct parsed_partitions *state, struct block_device *bdev,
  279. u32 offset, u32 size, int origin)
  280. {
  281. #ifdef CONFIG_BSD_DISKLABEL
  282. parse_bsd(state, bdev, offset, size, origin,
  283. "openbsd", OPENBSD_MAXPARTITIONS);
  284. #endif
  285. }
  286. /*
  287. * Create devices for Unixware partitions listed in a disklabel, under a
  288. * dos-like partition. See parse_extended() for more information.
  289. */
  290. static void
  291. parse_unixware(struct parsed_partitions *state, struct block_device *bdev,
  292. u32 offset, u32 size, int origin)
  293. {
  294. #ifdef CONFIG_UNIXWARE_DISKLABEL
  295. Sector sect;
  296. struct unixware_disklabel *l;
  297. struct unixware_slice *p;
  298. l = (struct unixware_disklabel *)read_dev_sector(bdev, offset+29, &sect);
  299. if (!l)
  300. return;
  301. if (le32_to_cpu(l->d_magic) != UNIXWARE_DISKMAGIC ||
  302. le32_to_cpu(l->vtoc.v_magic) != UNIXWARE_DISKMAGIC2) {
  303. put_dev_sector(sect);
  304. return;
  305. }
  306. printk(" %s%d: <unixware:", state->name, origin);
  307. p = &l->vtoc.v_slice[1];
  308. /* I omit the 0th slice as it is the same as whole disk. */
  309. while (p - &l->vtoc.v_slice[0] < UNIXWARE_NUMSLICE) {
  310. if (state->next == state->limit)
  311. break;
  312. if (p->s_label != UNIXWARE_FS_UNUSED)
  313. put_partition(state, state->next++,
  314. START_SECT(p), NR_SECTS(p));
  315. p++;
  316. }
  317. put_dev_sector(sect);
  318. printk(" >\n");
  319. #endif
  320. }
  321. /*
  322. * Minix 2.0.0/2.0.2 subpartition support.
  323. * Anand Krishnamurthy <anandk@wiproge.med.ge.com>
  324. * Rajeev V. Pillai <rajeevvp@yahoo.com>
  325. */
  326. static void
  327. parse_minix(struct parsed_partitions *state, struct block_device *bdev,
  328. u32 offset, u32 size, int origin)
  329. {
  330. #ifdef CONFIG_MINIX_SUBPARTITION
  331. Sector sect;
  332. unsigned char *data;
  333. struct partition *p;
  334. int i;
  335. data = read_dev_sector(bdev, offset, &sect);
  336. if (!data)
  337. return;
  338. p = (struct partition *)(data + 0x1be);
  339. /* The first sector of a Minix partition can have either
  340. * a secondary MBR describing its subpartitions, or
  341. * the normal boot sector. */
  342. if (msdos_magic_present (data + 510) &&
  343. SYS_IND(p) == MINIX_PARTITION) { /* subpartition table present */
  344. printk(" %s%d: <minix:", state->name, origin);
  345. for (i = 0; i < MINIX_NR_SUBPARTITIONS; i++, p++) {
  346. if (state->next == state->limit)
  347. break;
  348. /* add each partition in use */
  349. if (SYS_IND(p) == MINIX_PARTITION)
  350. put_partition(state, state->next++,
  351. START_SECT(p), NR_SECTS(p));
  352. }
  353. printk(" >\n");
  354. }
  355. put_dev_sector(sect);
  356. #endif /* CONFIG_MINIX_SUBPARTITION */
  357. }
  358. static struct {
  359. unsigned char id;
  360. void (*parse)(struct parsed_partitions *, struct block_device *,
  361. u32, u32, int);
  362. } subtypes[] = {
  363. {FREEBSD_PARTITION, parse_freebsd},
  364. {NETBSD_PARTITION, parse_netbsd},
  365. {OPENBSD_PARTITION, parse_openbsd},
  366. {MINIX_PARTITION, parse_minix},
  367. {UNIXWARE_PARTITION, parse_unixware},
  368. {SOLARIS_X86_PARTITION, parse_solaris_x86},
  369. {NEW_SOLARIS_X86_PARTITION, parse_solaris_x86},
  370. {0, NULL},
  371. };
  372. int msdos_partition(struct parsed_partitions *state, struct block_device *bdev)
  373. {
  374. int sector_size = bdev_hardsect_size(bdev) / 512;
  375. Sector sect;
  376. unsigned char *data;
  377. struct partition *p;
  378. int slot;
  379. data = read_dev_sector(bdev, 0, &sect);
  380. if (!data)
  381. return -1;
  382. if (!msdos_magic_present(data + 510)) {
  383. put_dev_sector(sect);
  384. return 0;
  385. }
  386. if (aix_magic_present(data, bdev)) {
  387. put_dev_sector(sect);
  388. printk( " [AIX]");
  389. return 0;
  390. }
  391. /*
  392. * Now that the 55aa signature is present, this is probably
  393. * either the boot sector of a FAT filesystem or a DOS-type
  394. * partition table. Reject this in case the boot indicator
  395. * is not 0 or 0x80.
  396. */
  397. p = (struct partition *) (data + 0x1be);
  398. for (slot = 1; slot <= 4; slot++, p++) {
  399. if (p->boot_ind != 0 && p->boot_ind != 0x80) {
  400. put_dev_sector(sect);
  401. return 0;
  402. }
  403. }
  404. #ifdef CONFIG_EFI_PARTITION
  405. p = (struct partition *) (data + 0x1be);
  406. for (slot = 1 ; slot <= 4 ; slot++, p++) {
  407. /* If this is an EFI GPT disk, msdos should ignore it. */
  408. if (SYS_IND(p) == EFI_PMBR_OSTYPE_EFI_GPT) {
  409. put_dev_sector(sect);
  410. return 0;
  411. }
  412. }
  413. #endif
  414. p = (struct partition *) (data + 0x1be);
  415. /*
  416. * Look for partitions in two passes:
  417. * First find the primary and DOS-type extended partitions.
  418. * On the second pass look inside *BSD, Unixware and Solaris partitions.
  419. */
  420. state->next = 5;
  421. for (slot = 1 ; slot <= 4 ; slot++, p++) {
  422. u32 start = START_SECT(p)*sector_size;
  423. u32 size = NR_SECTS(p)*sector_size;
  424. if (!size)
  425. continue;
  426. if (is_extended_partition(p)) {
  427. /* prevent someone doing mkfs or mkswap on an
  428. extended partition, but leave room for LILO */
  429. put_partition(state, slot, start, size == 1 ? 1 : 2);
  430. printk(" <");
  431. parse_extended(state, bdev, start, size);
  432. printk(" >");
  433. continue;
  434. }
  435. put_partition(state, slot, start, size);
  436. if (SYS_IND(p) == LINUX_RAID_PARTITION)
  437. state->parts[slot].flags = 1;
  438. if (SYS_IND(p) == DM6_PARTITION)
  439. printk("[DM]");
  440. if (SYS_IND(p) == EZD_PARTITION)
  441. printk("[EZD]");
  442. }
  443. printk("\n");
  444. /* second pass - output for each on a separate line */
  445. p = (struct partition *) (0x1be + data);
  446. for (slot = 1 ; slot <= 4 ; slot++, p++) {
  447. unsigned char id = SYS_IND(p);
  448. int n;
  449. if (!NR_SECTS(p))
  450. continue;
  451. for (n = 0; subtypes[n].parse && id != subtypes[n].id; n++)
  452. ;
  453. if (!subtypes[n].parse)
  454. continue;
  455. subtypes[n].parse(state, bdev, START_SECT(p)*sector_size,
  456. NR_SECTS(p)*sector_size, slot);
  457. }
  458. put_dev_sector(sect);
  459. return 1;
  460. }