msdos.c 16 KB

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