(root)/
util-linux-2.39/
libblkid/
src/
partitions/
sun.c
       1  /*
       2   * sun (solaris-sparc) partition parsing code
       3   *
       4   * Copyright (C) 2009 Karel Zak <kzak@redhat.com>
       5   *
       6   * This file may be redistributed under the terms of the
       7   * GNU Lesser General Public License.
       8   */
       9  #include <stdio.h>
      10  #include <string.h>
      11  #include <stdlib.h>
      12  #include <stdint.h>
      13  #include <stddef.h>
      14  
      15  #include "pt-sun.h"
      16  #include "partitions.h"
      17  
      18  static int probe_sun_pt(blkid_probe pr,
      19  		const struct blkid_idmag *mag __attribute__((__unused__)))
      20  {
      21  	struct sun_disklabel *l;
      22  	struct sun_partition *p;
      23  	blkid_parttable tab = NULL;
      24  	blkid_partlist ls;
      25  	uint16_t nparts;
      26  	uint64_t spc;
      27  	int i, use_vtoc;
      28  
      29  	l = (struct sun_disklabel *) blkid_probe_get_sector(pr, 0);
      30  	if (!l) {
      31  		if (errno)
      32  			return -errno;
      33  		goto nothing;
      34  	}
      35  
      36  	if (!blkid_probe_verify_csum(pr, sun_pt_checksum(l), 0)) {
      37  		DBG(LOWPROBE, ul_debug(
      38  			"detected corrupted sun disk label -- ignore"));
      39  		goto nothing;
      40  	}
      41  
      42  	if (blkid_partitions_need_typeonly(pr))
      43  		/* caller does not ask for details about partitions */
      44  		return BLKID_PROBE_OK;
      45  
      46  	ls = blkid_probe_get_partlist(pr);
      47  	if (!ls)
      48  		goto nothing;
      49  
      50  	tab = blkid_partlist_new_parttable(ls, "sun", 0);
      51  	if (!tab)
      52  		goto err;
      53  
      54  	/* sectors per cylinder (partition offset is in cylinders...) */
      55  	spc = (uint64_t) be16_to_cpu(l->nhead) * be16_to_cpu(l->nsect);
      56  
      57  	DBG(LOWPROBE, ul_debug("Sun VTOC sanity=%u version=%u nparts=%u",
      58  			be32_to_cpu(l->vtoc.sanity),
      59  			be32_to_cpu(l->vtoc.version),
      60  			be16_to_cpu(l->vtoc.nparts)));
      61  
      62  	/* Check to see if we can use the VTOC table */
      63  	use_vtoc = ((be32_to_cpu(l->vtoc.sanity) == SUN_VTOC_SANITY) &&
      64  		    (be32_to_cpu(l->vtoc.version) == SUN_VTOC_VERSION) &&
      65  		    (be16_to_cpu(l->vtoc.nparts) <= SUN_MAXPARTITIONS));
      66  
      67  	/* Use 8 partition entries if not specified in validated VTOC */
      68  	nparts = use_vtoc ? be16_to_cpu(l->vtoc.nparts) : SUN_MAXPARTITIONS;
      69  
      70  	/*
      71  	 * So that old Linux-Sun partitions continue to work,
      72  	 * allow the VTOC to be used under the additional condition ...
      73  	 */
      74  	use_vtoc = use_vtoc || !(l->vtoc.sanity || l->vtoc.version || l->vtoc.nparts);
      75  
      76  	for (i = 0, p = l->partitions; i < nparts; i++, p++) {
      77  
      78  		uint64_t start, size;
      79  		uint16_t type = 0, flags = 0;
      80  		blkid_partition par;
      81  
      82  		start = be32_to_cpu(p->start_cylinder) * spc;
      83  		size = be32_to_cpu(p->num_sectors);
      84  		if (use_vtoc) {
      85  			type = be16_to_cpu(l->vtoc.infos[i].id);
      86  			flags = be16_to_cpu(l->vtoc.infos[i].flags);
      87  		}
      88  
      89  		if (type == SUN_TAG_WHOLEDISK || !size) {
      90  			blkid_partlist_increment_partno(ls);
      91  			continue;
      92  		}
      93  		par = blkid_partlist_add_partition(ls, tab, start, size);
      94  		if (!par)
      95  			goto err;
      96  
      97  		if (type)
      98  			blkid_partition_set_type(par, type);
      99  		if (flags)
     100  			blkid_partition_set_flags(par, flags);
     101  	}
     102  	return BLKID_PROBE_OK;
     103  
     104  nothing:
     105  	return BLKID_PROBE_NONE;
     106  err:
     107  	return -ENOMEM;
     108  }
     109  
     110  
     111  const struct blkid_idinfo sun_pt_idinfo =
     112  {
     113  	.name		= "sun",
     114  	.probefunc	= probe_sun_pt,
     115  	.magics		=
     116  	{
     117  		{
     118  		  .magic = "\xDA\xBE",		/* big-endian magic string */
     119  		  .len = 2,
     120  		  .sboff = offsetof(struct sun_disklabel, magic)
     121  		},
     122  		{ NULL }
     123  	}
     124  };
     125