msdos.c 16 KB

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