(root)/
util-linux-2.39/
libblkid/
src/
partitions/
minix.c
       1  /*
       2   * Minix 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  
      14  #include "partitions.h"
      15  #include "minix.h"
      16  
      17  static int probe_minix_pt(blkid_probe pr,
      18  		const struct blkid_idmag *mag __attribute__((__unused__)))
      19  {
      20  	struct dos_partition *p;
      21  	blkid_parttable tab = NULL;
      22  	blkid_partition parent;
      23  	blkid_partlist ls;
      24  	unsigned char *data;
      25  	int i;
      26  
      27  	data = blkid_probe_get_sector(pr, 0);
      28  	if (!data) {
      29  		if (errno)
      30  			return -errno;
      31  		goto nothing;
      32  	}
      33  
      34  	ls = blkid_probe_get_partlist(pr);
      35  	if (!ls)
      36  		goto nothing;
      37  
      38  	/* Parent is required, because Minix uses the same PT as DOS and
      39  	 * difference is only in primary partition (parent) type.
      40  	 */
      41  	parent = blkid_partlist_get_parent(ls);
      42  	if (!parent)
      43  		goto nothing;
      44  
      45  	if (blkid_partition_get_type(parent) != MBR_MINIX_PARTITION)
      46  		goto nothing;
      47  
      48  	if (blkid_partitions_need_typeonly(pr))
      49  		/* caller does not ask for details about partitions */
      50  		return BLKID_PROBE_OK;
      51  
      52  	tab = blkid_partlist_new_parttable(ls, "minix", MBR_PT_OFFSET);
      53  	if (!tab)
      54  		goto err;
      55  
      56  	for (i = 0, p = mbr_get_partition(data, 0);
      57  			i < MINIX_MAXPARTITIONS; i++, p++) {
      58  
      59  		uint32_t start, size;
      60  		blkid_partition par;
      61  
      62  		if (p->sys_ind != MBR_MINIX_PARTITION)
      63  			continue;
      64  
      65  		start = dos_partition_get_start(p);
      66  		size = dos_partition_get_size(p);
      67  
      68  		if (parent && !blkid_is_nested_dimension(parent, start, size)) {
      69  			DBG(LOWPROBE, ul_debug(
      70  				"WARNING: minix partition (%d) overflow "
      71  				"detected, ignore", i));
      72  			continue;
      73  		}
      74  
      75  		par = blkid_partlist_add_partition(ls, tab, start, size);
      76  		if (!par)
      77  			goto err;
      78  
      79  		blkid_partition_set_type(par, p->sys_ind);
      80  		blkid_partition_set_flags(par, p->boot_ind);
      81  	}
      82  
      83  	return BLKID_PROBE_OK;
      84  
      85  nothing:
      86  	return BLKID_PROBE_NONE;
      87  err:
      88  	return -ENOMEM;
      89  }
      90  
      91  /* same as DOS */
      92  const struct blkid_idinfo minix_pt_idinfo =
      93  {
      94  	.name		= "minix",
      95  	.probefunc	= probe_minix_pt,
      96  	.magics		=
      97  	{
      98  		{ .magic = "\x55\xAA", .len = 2, .sboff = 510 },
      99  		{ NULL }
     100  	}
     101  };
     102