msdos.c 16 KB

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