msdos.c 21 KB

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