msdos.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  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. * BSD disklabel support by Yossi Gottlieb <yogo@math.tau.ac.il>
  23. * updated by Marc Espie <Marc.Espie@openbsd.org>
  24. *
  25. * Unixware slices support by Andrzej Krzysztofowicz <ankry@mif.pg.gda.pl>
  26. * and Krzysztof G. Baranowski <kgb@knm.org.pl>
  27. */
  28. #include <linux/msdos_fs.h>
  29. #include <linux/msdos_partition.h>
  30. #include "check.h"
  31. #include "efi.h"
  32. /*
  33. * Many architectures don't like unaligned accesses, while
  34. * the nr_sects and start_sect partition table entries are
  35. * at a 2 (mod 4) address.
  36. */
  37. #include <asm/unaligned.h>
  38. static inline sector_t nr_sects(struct msdos_partition *p)
  39. {
  40. return (sector_t)get_unaligned_le32(&p->nr_sects);
  41. }
  42. static inline sector_t start_sect(struct msdos_partition *p)
  43. {
  44. return (sector_t)get_unaligned_le32(&p->start_sect);
  45. }
  46. static inline int is_extended_partition(struct msdos_partition *p)
  47. {
  48. return (p->sys_ind == DOS_EXTENDED_PARTITION ||
  49. p->sys_ind == WIN98_EXTENDED_PARTITION ||
  50. p->sys_ind == LINUX_EXTENDED_PARTITION);
  51. }
  52. #define MSDOS_LABEL_MAGIC1 0x55
  53. #define MSDOS_LABEL_MAGIC2 0xAA
  54. static inline int
  55. msdos_magic_present(unsigned char *p)
  56. {
  57. return (p[0] == MSDOS_LABEL_MAGIC1 && p[1] == MSDOS_LABEL_MAGIC2);
  58. }
  59. /* Value is EBCDIC 'IBMA' */
  60. #define AIX_LABEL_MAGIC1 0xC9
  61. #define AIX_LABEL_MAGIC2 0xC2
  62. #define AIX_LABEL_MAGIC3 0xD4
  63. #define AIX_LABEL_MAGIC4 0xC1
  64. static int aix_magic_present(struct parsed_partitions *state, unsigned char *p)
  65. {
  66. struct msdos_partition *pt = (struct msdos_partition *) (p + 0x1be);
  67. Sector sect;
  68. unsigned char *d;
  69. int slot, ret = 0;
  70. if (!(p[0] == AIX_LABEL_MAGIC1 &&
  71. p[1] == AIX_LABEL_MAGIC2 &&
  72. p[2] == AIX_LABEL_MAGIC3 &&
  73. p[3] == AIX_LABEL_MAGIC4))
  74. return 0;
  75. /*
  76. * Assume the partition table is valid if Linux partitions exists.
  77. * Note that old Solaris/x86 partitions use the same indicator as
  78. * Linux swap partitions, so we consider that a Linux partition as
  79. * well.
  80. */
  81. for (slot = 1; slot <= 4; slot++, pt++) {
  82. if (pt->sys_ind == SOLARIS_X86_PARTITION ||
  83. pt->sys_ind == LINUX_RAID_PARTITION ||
  84. pt->sys_ind == LINUX_DATA_PARTITION ||
  85. pt->sys_ind == LINUX_LVM_PARTITION ||
  86. is_extended_partition(pt))
  87. return 0;
  88. }
  89. d = read_part_sector(state, 7, &sect);
  90. if (d) {
  91. if (d[0] == '_' && d[1] == 'L' && d[2] == 'V' && d[3] == 'M')
  92. ret = 1;
  93. put_dev_sector(sect);
  94. }
  95. return ret;
  96. }
  97. static void set_info(struct parsed_partitions *state, int slot,
  98. u32 disksig)
  99. {
  100. struct partition_meta_info *info = &state->parts[slot].info;
  101. snprintf(info->uuid, sizeof(info->uuid), "%08x-%02x", disksig,
  102. slot);
  103. info->volname[0] = 0;
  104. state->parts[slot].has_info = true;
  105. }
  106. /*
  107. * Create devices for each logical partition in an extended partition.
  108. * The logical partitions form a linked list, with each entry being
  109. * a partition table with two entries. The first entry
  110. * is the real data partition (with a start relative to the partition
  111. * table start). The second is a pointer to the next logical partition
  112. * (with a start relative to the entire extended partition).
  113. * We do not create a Linux partition for the partition tables, but
  114. * only for the actual data partitions.
  115. */
  116. static void parse_extended(struct parsed_partitions *state,
  117. sector_t first_sector, sector_t first_size,
  118. u32 disksig)
  119. {
  120. struct msdos_partition *p;
  121. Sector sect;
  122. unsigned char *data;
  123. sector_t this_sector, this_size;
  124. sector_t sector_size = bdev_logical_block_size(state->bdev) / 512;
  125. int loopct = 0; /* number of links followed
  126. without finding a data partition */
  127. int i;
  128. this_sector = first_sector;
  129. this_size = first_size;
  130. while (1) {
  131. if (++loopct > 100)
  132. return;
  133. if (state->next == state->limit)
  134. return;
  135. data = read_part_sector(state, this_sector, &sect);
  136. if (!data)
  137. return;
  138. if (!msdos_magic_present(data + 510))
  139. goto done;
  140. p = (struct msdos_partition *) (data + 0x1be);
  141. /*
  142. * Usually, the first entry is the real data partition,
  143. * the 2nd entry is the next extended partition, or empty,
  144. * and the 3rd and 4th entries are unused.
  145. * However, DRDOS sometimes has the extended partition as
  146. * the first entry (when the data partition is empty),
  147. * and OS/2 seems to use all four entries.
  148. */
  149. /*
  150. * First process the data partition(s)
  151. */
  152. for (i = 0; i < 4; i++, p++) {
  153. sector_t offs, size, next;
  154. if (!nr_sects(p) || is_extended_partition(p))
  155. continue;
  156. /* Check the 3rd and 4th entries -
  157. these sometimes contain random garbage */
  158. offs = start_sect(p)*sector_size;
  159. size = nr_sects(p)*sector_size;
  160. next = this_sector + offs;
  161. if (i >= 2) {
  162. if (offs + size > this_size)
  163. continue;
  164. if (next < first_sector)
  165. continue;
  166. if (next + size > first_sector + first_size)
  167. continue;
  168. }
  169. put_partition(state, state->next, next, size);
  170. set_info(state, state->next, disksig);
  171. if (p->sys_ind == LINUX_RAID_PARTITION)
  172. state->parts[state->next].flags = ADDPART_FLAG_RAID;
  173. loopct = 0;
  174. if (++state->next == state->limit)
  175. goto done;
  176. }
  177. /*
  178. * Next, process the (first) extended partition, if present.
  179. * (So far, there seems to be no reason to make
  180. * parse_extended() recursive and allow a tree
  181. * of extended partitions.)
  182. * It should be a link to the next logical partition.
  183. */
  184. p -= 4;
  185. for (i = 0; i < 4; i++, p++)
  186. if (nr_sects(p) && is_extended_partition(p))
  187. break;
  188. if (i == 4)
  189. goto done; /* nothing left to do */
  190. this_sector = first_sector + start_sect(p) * sector_size;
  191. this_size = nr_sects(p) * sector_size;
  192. put_dev_sector(sect);
  193. }
  194. done:
  195. put_dev_sector(sect);
  196. }
  197. #define SOLARIS_X86_NUMSLICE 16
  198. #define SOLARIS_X86_VTOC_SANE (0x600DDEEEUL)
  199. struct solaris_x86_slice {
  200. __le16 s_tag; /* ID tag of partition */
  201. __le16 s_flag; /* permission flags */
  202. __le32 s_start; /* start sector no of partition */
  203. __le32 s_size; /* # of blocks in partition */
  204. };
  205. struct solaris_x86_vtoc {
  206. unsigned int v_bootinfo[3]; /* info needed by mboot */
  207. __le32 v_sanity; /* to verify vtoc sanity */
  208. __le32 v_version; /* layout version */
  209. char v_volume[8]; /* volume name */
  210. __le16 v_sectorsz; /* sector size in bytes */
  211. __le16 v_nparts; /* number of partitions */
  212. unsigned int v_reserved[10]; /* free space */
  213. struct solaris_x86_slice
  214. v_slice[SOLARIS_X86_NUMSLICE]; /* slice headers */
  215. unsigned int timestamp[SOLARIS_X86_NUMSLICE]; /* timestamp */
  216. char v_asciilabel[128]; /* for compatibility */
  217. };
  218. /* james@bpgc.com: Solaris has a nasty indicator: 0x82 which also
  219. indicates linux swap. Be careful before believing this is Solaris. */
  220. static void parse_solaris_x86(struct parsed_partitions *state,
  221. sector_t offset, sector_t size, int origin)
  222. {
  223. #ifdef CONFIG_SOLARIS_X86_PARTITION
  224. Sector sect;
  225. struct solaris_x86_vtoc *v;
  226. int i;
  227. short max_nparts;
  228. v = read_part_sector(state, offset + 1, &sect);
  229. if (!v)
  230. return;
  231. if (le32_to_cpu(v->v_sanity) != SOLARIS_X86_VTOC_SANE) {
  232. put_dev_sector(sect);
  233. return;
  234. }
  235. {
  236. char tmp[1 + BDEVNAME_SIZE + 10 + 11 + 1];
  237. snprintf(tmp, sizeof(tmp), " %s%d: <solaris:", state->name, origin);
  238. strlcat(state->pp_buf, tmp, PAGE_SIZE);
  239. }
  240. if (le32_to_cpu(v->v_version) != 1) {
  241. char tmp[64];
  242. snprintf(tmp, sizeof(tmp), " cannot handle version %d vtoc>\n",
  243. le32_to_cpu(v->v_version));
  244. strlcat(state->pp_buf, tmp, PAGE_SIZE);
  245. put_dev_sector(sect);
  246. return;
  247. }
  248. /* Ensure we can handle previous case of VTOC with 8 entries gracefully */
  249. max_nparts = le16_to_cpu(v->v_nparts) > 8 ? SOLARIS_X86_NUMSLICE : 8;
  250. for (i = 0; i < max_nparts && state->next < state->limit; i++) {
  251. struct solaris_x86_slice *s = &v->v_slice[i];
  252. char tmp[3 + 10 + 1 + 1];
  253. if (s->s_size == 0)
  254. continue;
  255. snprintf(tmp, sizeof(tmp), " [s%d]", i);
  256. strlcat(state->pp_buf, tmp, PAGE_SIZE);
  257. /* solaris partitions are relative to current MS-DOS
  258. * one; must add the offset of the current partition */
  259. put_partition(state, state->next++,
  260. le32_to_cpu(s->s_start)+offset,
  261. le32_to_cpu(s->s_size));
  262. }
  263. put_dev_sector(sect);
  264. strlcat(state->pp_buf, " >\n", PAGE_SIZE);
  265. #endif
  266. }
  267. /* check against BSD src/sys/sys/disklabel.h for consistency */
  268. #define BSD_DISKMAGIC (0x82564557UL) /* The disk magic number */
  269. #define BSD_MAXPARTITIONS 16
  270. #define OPENBSD_MAXPARTITIONS 16
  271. #define BSD_FS_UNUSED 0 /* disklabel unused partition entry ID */
  272. struct bsd_disklabel {
  273. __le32 d_magic; /* the magic number */
  274. __s16 d_type; /* drive type */
  275. __s16 d_subtype; /* controller/d_type specific */
  276. char d_typename[16]; /* type name, e.g. "eagle" */
  277. char d_packname[16]; /* pack identifier */
  278. __u32 d_secsize; /* # of bytes per sector */
  279. __u32 d_nsectors; /* # of data sectors per track */
  280. __u32 d_ntracks; /* # of tracks per cylinder */
  281. __u32 d_ncylinders; /* # of data cylinders per unit */
  282. __u32 d_secpercyl; /* # of data sectors per cylinder */
  283. __u32 d_secperunit; /* # of data sectors per unit */
  284. __u16 d_sparespertrack; /* # of spare sectors per track */
  285. __u16 d_sparespercyl; /* # of spare sectors per cylinder */
  286. __u32 d_acylinders; /* # of alt. cylinders per unit */
  287. __u16 d_rpm; /* rotational speed */
  288. __u16 d_interleave; /* hardware sector interleave */
  289. __u16 d_trackskew; /* sector 0 skew, per track */
  290. __u16 d_cylskew; /* sector 0 skew, per cylinder */
  291. __u32 d_headswitch; /* head switch time, usec */
  292. __u32 d_trkseek; /* track-to-track seek, usec */
  293. __u32 d_flags; /* generic flags */
  294. #define NDDATA 5
  295. __u32 d_drivedata[NDDATA]; /* drive-type specific information */
  296. #define NSPARE 5
  297. __u32 d_spare[NSPARE]; /* reserved for future use */
  298. __le32 d_magic2; /* the magic number (again) */
  299. __le16 d_checksum; /* xor of data incl. partitions */
  300. /* filesystem and partition information: */
  301. __le16 d_npartitions; /* number of partitions in following */
  302. __le32 d_bbsize; /* size of boot area at sn0, bytes */
  303. __le32 d_sbsize; /* max size of fs superblock, bytes */
  304. struct bsd_partition { /* the partition table */
  305. __le32 p_size; /* number of sectors in partition */
  306. __le32 p_offset; /* starting sector */
  307. __le32 p_fsize; /* filesystem basic fragment size */
  308. __u8 p_fstype; /* filesystem type, see below */
  309. __u8 p_frag; /* filesystem fragments per block */
  310. __le16 p_cpg; /* filesystem cylinders per group */
  311. } d_partitions[BSD_MAXPARTITIONS]; /* actually may be more */
  312. };
  313. #if defined(CONFIG_BSD_DISKLABEL)
  314. /*
  315. * Create devices for BSD partitions listed in a disklabel, under a
  316. * dos-like partition. See parse_extended() for more information.
  317. */
  318. static void parse_bsd(struct parsed_partitions *state,
  319. sector_t offset, sector_t size, int origin, char *flavour,
  320. int max_partitions)
  321. {
  322. Sector sect;
  323. struct bsd_disklabel *l;
  324. struct bsd_partition *p;
  325. char tmp[64];
  326. l = read_part_sector(state, offset + 1, &sect);
  327. if (!l)
  328. return;
  329. if (le32_to_cpu(l->d_magic) != BSD_DISKMAGIC) {
  330. put_dev_sector(sect);
  331. return;
  332. }
  333. snprintf(tmp, sizeof(tmp), " %s%d: <%s:", state->name, origin, flavour);
  334. strlcat(state->pp_buf, tmp, PAGE_SIZE);
  335. if (le16_to_cpu(l->d_npartitions) < max_partitions)
  336. max_partitions = le16_to_cpu(l->d_npartitions);
  337. for (p = l->d_partitions; p - l->d_partitions < max_partitions; p++) {
  338. sector_t bsd_start, bsd_size;
  339. if (state->next == state->limit)
  340. break;
  341. if (p->p_fstype == BSD_FS_UNUSED)
  342. continue;
  343. bsd_start = le32_to_cpu(p->p_offset);
  344. bsd_size = le32_to_cpu(p->p_size);
  345. /* FreeBSD has relative offset if C partition offset is zero */
  346. if (memcmp(flavour, "bsd\0", 4) == 0 &&
  347. le32_to_cpu(l->d_partitions[2].p_offset) == 0)
  348. bsd_start += offset;
  349. if (offset == bsd_start && size == bsd_size)
  350. /* full parent partition, we have it already */
  351. continue;
  352. if (offset > bsd_start || offset+size < bsd_start+bsd_size) {
  353. strlcat(state->pp_buf, "bad subpartition - ignored\n", PAGE_SIZE);
  354. continue;
  355. }
  356. put_partition(state, state->next++, bsd_start, bsd_size);
  357. }
  358. put_dev_sector(sect);
  359. if (le16_to_cpu(l->d_npartitions) > max_partitions) {
  360. snprintf(tmp, sizeof(tmp), " (ignored %d more)",
  361. le16_to_cpu(l->d_npartitions) - max_partitions);
  362. strlcat(state->pp_buf, tmp, PAGE_SIZE);
  363. }
  364. strlcat(state->pp_buf, " >\n", PAGE_SIZE);
  365. }
  366. #endif
  367. static void parse_freebsd(struct parsed_partitions *state,
  368. sector_t offset, sector_t size, int origin)
  369. {
  370. #ifdef CONFIG_BSD_DISKLABEL
  371. parse_bsd(state, offset, size, origin, "bsd", BSD_MAXPARTITIONS);
  372. #endif
  373. }
  374. static void parse_netbsd(struct parsed_partitions *state,
  375. sector_t offset, sector_t size, int origin)
  376. {
  377. #ifdef CONFIG_BSD_DISKLABEL
  378. parse_bsd(state, offset, size, origin, "netbsd", BSD_MAXPARTITIONS);
  379. #endif
  380. }
  381. static void parse_openbsd(struct parsed_partitions *state,
  382. sector_t offset, sector_t size, int origin)
  383. {
  384. #ifdef CONFIG_BSD_DISKLABEL
  385. parse_bsd(state, offset, size, origin, "openbsd",
  386. OPENBSD_MAXPARTITIONS);
  387. #endif
  388. }
  389. #define UNIXWARE_DISKMAGIC (0xCA5E600DUL) /* The disk magic number */
  390. #define UNIXWARE_DISKMAGIC2 (0x600DDEEEUL) /* The slice table magic nr */
  391. #define UNIXWARE_NUMSLICE 16
  392. #define UNIXWARE_FS_UNUSED 0 /* Unused slice entry ID */
  393. struct unixware_slice {
  394. __le16 s_label; /* label */
  395. __le16 s_flags; /* permission flags */
  396. __le32 start_sect; /* starting sector */
  397. __le32 nr_sects; /* number of sectors in slice */
  398. };
  399. struct unixware_disklabel {
  400. __le32 d_type; /* drive type */
  401. __le32 d_magic; /* the magic number */
  402. __le32 d_version; /* version number */
  403. char d_serial[12]; /* serial number of the device */
  404. __le32 d_ncylinders; /* # of data cylinders per device */
  405. __le32 d_ntracks; /* # of tracks per cylinder */
  406. __le32 d_nsectors; /* # of data sectors per track */
  407. __le32 d_secsize; /* # of bytes per sector */
  408. __le32 d_part_start; /* # of first sector of this partition*/
  409. __le32 d_unknown1[12]; /* ? */
  410. __le32 d_alt_tbl; /* byte offset of alternate table */
  411. __le32 d_alt_len; /* byte length of alternate table */
  412. __le32 d_phys_cyl; /* # of physical cylinders per device */
  413. __le32 d_phys_trk; /* # of physical tracks per cylinder */
  414. __le32 d_phys_sec; /* # of physical sectors per track */
  415. __le32 d_phys_bytes; /* # of physical bytes per sector */
  416. __le32 d_unknown2; /* ? */
  417. __le32 d_unknown3; /* ? */
  418. __le32 d_pad[8]; /* pad */
  419. struct unixware_vtoc {
  420. __le32 v_magic; /* the magic number */
  421. __le32 v_version; /* version number */
  422. char v_name[8]; /* volume name */
  423. __le16 v_nslices; /* # of slices */
  424. __le16 v_unknown1; /* ? */
  425. __le32 v_reserved[10]; /* reserved */
  426. struct unixware_slice
  427. v_slice[UNIXWARE_NUMSLICE]; /* slice headers */
  428. } vtoc;
  429. }; /* 408 */
  430. /*
  431. * Create devices for Unixware partitions listed in a disklabel, under a
  432. * dos-like partition. See parse_extended() for more information.
  433. */
  434. static void parse_unixware(struct parsed_partitions *state,
  435. sector_t offset, sector_t size, int origin)
  436. {
  437. #ifdef CONFIG_UNIXWARE_DISKLABEL
  438. Sector sect;
  439. struct unixware_disklabel *l;
  440. struct unixware_slice *p;
  441. l = read_part_sector(state, offset + 29, &sect);
  442. if (!l)
  443. return;
  444. if (le32_to_cpu(l->d_magic) != UNIXWARE_DISKMAGIC ||
  445. le32_to_cpu(l->vtoc.v_magic) != UNIXWARE_DISKMAGIC2) {
  446. put_dev_sector(sect);
  447. return;
  448. }
  449. {
  450. char tmp[1 + BDEVNAME_SIZE + 10 + 12 + 1];
  451. snprintf(tmp, sizeof(tmp), " %s%d: <unixware:", state->name, origin);
  452. strlcat(state->pp_buf, tmp, PAGE_SIZE);
  453. }
  454. p = &l->vtoc.v_slice[1];
  455. /* I omit the 0th slice as it is the same as whole disk. */
  456. while (p - &l->vtoc.v_slice[0] < UNIXWARE_NUMSLICE) {
  457. if (state->next == state->limit)
  458. break;
  459. if (p->s_label != UNIXWARE_FS_UNUSED)
  460. put_partition(state, state->next++,
  461. le32_to_cpu(p->start_sect),
  462. le32_to_cpu(p->nr_sects));
  463. p++;
  464. }
  465. put_dev_sector(sect);
  466. strlcat(state->pp_buf, " >\n", PAGE_SIZE);
  467. #endif
  468. }
  469. #define MINIX_NR_SUBPARTITIONS 4
  470. /*
  471. * Minix 2.0.0/2.0.2 subpartition support.
  472. * Anand Krishnamurthy <anandk@wiproge.med.ge.com>
  473. * Rajeev V. Pillai <rajeevvp@yahoo.com>
  474. */
  475. static void parse_minix(struct parsed_partitions *state,
  476. sector_t offset, sector_t size, int origin)
  477. {
  478. #ifdef CONFIG_MINIX_SUBPARTITION
  479. Sector sect;
  480. unsigned char *data;
  481. struct msdos_partition *p;
  482. int i;
  483. data = read_part_sector(state, offset, &sect);
  484. if (!data)
  485. return;
  486. p = (struct msdos_partition *)(data + 0x1be);
  487. /* The first sector of a Minix partition can have either
  488. * a secondary MBR describing its subpartitions, or
  489. * the normal boot sector. */
  490. if (msdos_magic_present(data + 510) &&
  491. p->sys_ind == MINIX_PARTITION) { /* subpartition table present */
  492. char tmp[1 + BDEVNAME_SIZE + 10 + 9 + 1];
  493. snprintf(tmp, sizeof(tmp), " %s%d: <minix:", state->name, origin);
  494. strlcat(state->pp_buf, tmp, PAGE_SIZE);
  495. for (i = 0; i < MINIX_NR_SUBPARTITIONS; i++, p++) {
  496. if (state->next == state->limit)
  497. break;
  498. /* add each partition in use */
  499. if (p->sys_ind == MINIX_PARTITION)
  500. put_partition(state, state->next++,
  501. start_sect(p), nr_sects(p));
  502. }
  503. strlcat(state->pp_buf, " >\n", PAGE_SIZE);
  504. }
  505. put_dev_sector(sect);
  506. #endif /* CONFIG_MINIX_SUBPARTITION */
  507. }
  508. static struct {
  509. unsigned char id;
  510. void (*parse)(struct parsed_partitions *, sector_t, sector_t, int);
  511. } subtypes[] = {
  512. {FREEBSD_PARTITION, parse_freebsd},
  513. {NETBSD_PARTITION, parse_netbsd},
  514. {OPENBSD_PARTITION, parse_openbsd},
  515. {MINIX_PARTITION, parse_minix},
  516. {UNIXWARE_PARTITION, parse_unixware},
  517. {SOLARIS_X86_PARTITION, parse_solaris_x86},
  518. {NEW_SOLARIS_X86_PARTITION, parse_solaris_x86},
  519. {0, NULL},
  520. };
  521. int msdos_partition(struct parsed_partitions *state)
  522. {
  523. sector_t sector_size = bdev_logical_block_size(state->bdev) / 512;
  524. Sector sect;
  525. unsigned char *data;
  526. struct msdos_partition *p;
  527. struct fat_boot_sector *fb;
  528. int slot;
  529. u32 disksig;
  530. data = read_part_sector(state, 0, &sect);
  531. if (!data)
  532. return -1;
  533. /*
  534. * Note order! (some AIX disks, e.g. unbootable kind,
  535. * have no MSDOS 55aa)
  536. */
  537. if (aix_magic_present(state, data)) {
  538. put_dev_sector(sect);
  539. #ifdef CONFIG_AIX_PARTITION
  540. return aix_partition(state);
  541. #else
  542. strlcat(state->pp_buf, " [AIX]", PAGE_SIZE);
  543. return 0;
  544. #endif
  545. }
  546. if (!msdos_magic_present(data + 510)) {
  547. put_dev_sector(sect);
  548. return 0;
  549. }
  550. /*
  551. * Now that the 55aa signature is present, this is probably
  552. * either the boot sector of a FAT filesystem or a DOS-type
  553. * partition table. Reject this in case the boot indicator
  554. * is not 0 or 0x80.
  555. */
  556. p = (struct msdos_partition *) (data + 0x1be);
  557. for (slot = 1; slot <= 4; slot++, p++) {
  558. if (p->boot_ind != 0 && p->boot_ind != 0x80) {
  559. /*
  560. * Even without a valid boot inidicator value
  561. * its still possible this is valid FAT filesystem
  562. * without a partition table.
  563. */
  564. fb = (struct fat_boot_sector *) data;
  565. if (slot == 1 && fb->reserved && fb->fats
  566. && fat_valid_media(fb->media)) {
  567. strlcat(state->pp_buf, "\n", PAGE_SIZE);
  568. put_dev_sector(sect);
  569. return 1;
  570. } else {
  571. put_dev_sector(sect);
  572. return 0;
  573. }
  574. }
  575. }
  576. #ifdef CONFIG_EFI_PARTITION
  577. p = (struct msdos_partition *) (data + 0x1be);
  578. for (slot = 1 ; slot <= 4 ; slot++, p++) {
  579. /* If this is an EFI GPT disk, msdos should ignore it. */
  580. if (p->sys_ind == EFI_PMBR_OSTYPE_EFI_GPT) {
  581. put_dev_sector(sect);
  582. return 0;
  583. }
  584. }
  585. #endif
  586. p = (struct msdos_partition *) (data + 0x1be);
  587. disksig = le32_to_cpup((__le32 *)(data + 0x1b8));
  588. /*
  589. * Look for partitions in two passes:
  590. * First find the primary and DOS-type extended partitions.
  591. * On the second pass look inside *BSD, Unixware and Solaris partitions.
  592. */
  593. state->next = 5;
  594. for (slot = 1 ; slot <= 4 ; slot++, p++) {
  595. sector_t start = start_sect(p)*sector_size;
  596. sector_t size = nr_sects(p)*sector_size;
  597. if (!size)
  598. continue;
  599. if (is_extended_partition(p)) {
  600. /*
  601. * prevent someone doing mkfs or mkswap on an
  602. * extended partition, but leave room for LILO
  603. * FIXME: this uses one logical sector for > 512b
  604. * sector, although it may not be enough/proper.
  605. */
  606. sector_t n = 2;
  607. n = min(size, max(sector_size, n));
  608. put_partition(state, slot, start, n);
  609. strlcat(state->pp_buf, " <", PAGE_SIZE);
  610. parse_extended(state, start, size, disksig);
  611. strlcat(state->pp_buf, " >", PAGE_SIZE);
  612. continue;
  613. }
  614. put_partition(state, slot, start, size);
  615. set_info(state, slot, disksig);
  616. if (p->sys_ind == LINUX_RAID_PARTITION)
  617. state->parts[slot].flags = ADDPART_FLAG_RAID;
  618. if (p->sys_ind == DM6_PARTITION)
  619. strlcat(state->pp_buf, "[DM]", PAGE_SIZE);
  620. if (p->sys_ind == EZD_PARTITION)
  621. strlcat(state->pp_buf, "[EZD]", PAGE_SIZE);
  622. }
  623. strlcat(state->pp_buf, "\n", PAGE_SIZE);
  624. /* second pass - output for each on a separate line */
  625. p = (struct msdos_partition *) (0x1be + data);
  626. for (slot = 1 ; slot <= 4 ; slot++, p++) {
  627. unsigned char id = p->sys_ind;
  628. int n;
  629. if (!nr_sects(p))
  630. continue;
  631. for (n = 0; subtypes[n].parse && id != subtypes[n].id; n++)
  632. ;
  633. if (!subtypes[n].parse)
  634. continue;
  635. subtypes[n].parse(state, start_sect(p) * sector_size,
  636. nr_sects(p) * sector_size, slot);
  637. }
  638. put_dev_sector(sect);
  639. return 1;
  640. }