openblt

a hobby OS from the late 90s
git clone http://frotz.net/git/openblt.git
Log | Files | Refs | LICENSE

disk.c (4595B)


      1 /* $Id: //depot/blt/lib/libblt/disk.c#4 $
      2 **
      3 ** Copyright 1999 Sidney Cammeresi
      4 ** All rights reserved.
      5 **
      6 ** Redistribution and use in source and binary forms, with or without
      7 ** modification, are permitted provided that the following conditions
      8 ** are met:
      9 ** 1. Redistributions of source code must retain the above copyright
     10 **    notice, this list of conditions, and the following disclaimer.
     11 ** 2. Redistributions in binary form must reproduce the above copyright
     12 **    notice, this list of conditions, and the following disclaimer in the
     13 **    documentation and/or other materials provided with the distribution.
     14 ** 3. The name of the author may not be used to endorse or promote products
     15 **    derived from this software without specific prior written permission.
     16 **
     17 ** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18 ** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19 ** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20 ** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21 ** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22 ** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     26 ** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27 */
     28 
     29 #include <stdio.h>
     30 #include <stdlib.h>
     31 #include <blt/disk.h>
     32 
     33 static int blt_internal_code (int fdisk, int fstype)
     34 {
     35 	if (fdisk == FDISK_TYPE_FREEBSD)
     36 		if (fstype == BSD_FS_BSDFFS)
     37 			return FREEBSD_FFS;
     38 		else if (fstype == BSD_FS_SWAP)
     39 			return FREEBSD_SWAP;
     40 		else
     41 			return 0;
     42 	else if (fdisk == FDISK_TYPE_OPENBSD)
     43 		if (fstype == BSD_FS_BSDFFS)
     44 			return OPENBSD_FFS;
     45 		else if (fstype == BSD_FS_SWAP)
     46 			return OPENBSD_SWAP;
     47 		else
     48 			return 0;
     49 	else
     50 		return 0;
     51 }
     52 
     53 disk_t *disk_alloc (blkdev_t *dev)
     54 {
     55 	unsigned char *mbr, *label;
     56 	int i, j, x;
     57 	disk_t *disk;
     58 	fdisk_partition *fp;
     59 	bsd_disklabel *slice;
     60 
     61 	disk = malloc (sizeof (disk_t));
     62 	disk->dev = dev;
     63 	disk->numparts = 0;
     64 	mbr = malloc (dev->blksize);
     65 	label = malloc (dev->blksize);
     66 	slice = (bsd_disklabel *) label;
     67 
     68 	/* count fdisk partitions */
     69 	blk_read (dev, mbr, 0, 1);
     70 	for (i = 0; i < 4; i++)
     71 	{
     72 		fp = (fdisk_partition *) (mbr + 0x1be) + i;
     73 		if (fp->type)
     74 		switch (fp->type)
     75 		{
     76 			case FDISK_TYPE_EMPTY:
     77 				break;
     78 
     79 			case FDISK_TYPE_FREEBSD:
     80 			case FDISK_TYPE_OPENBSD:
     81 				disk->numparts++;
     82 				blk_read (dev, label, fp->start_sect_num + 1, 1);
     83 				for (j = 0; j < BSD_MAXSLICES; j++)
     84 					if (slice->d_partitions[j].p_fstype != BSD_FS_UNUSED)
     85 						disk->numparts++;
     86 				break;
     87 
     88 			default:
     89 				disk->numparts++;
     90 				break;
     91 		}
     92 	}
     93 	disk->partition = malloc (sizeof (partition_t) * disk->numparts);
     94 
     95 	/* create partition data, fdisk partitions first */
     96 	for (i = x = 0; i < 4; i++)
     97 	{
     98 		fp = (fdisk_partition *) (mbr + 0x1be) + i;
     99 		if (fp->type)
    100 		{
    101 			disk->partition[x].name[0] = '0' + i;
    102 			disk->partition[x].name[1] = 0;
    103 			disk->partition[x].start = fp->start_sect_num;
    104 			disk->partition[x].size = fp->num_sects;
    105 			disk->partition[x].type = fp->type;
    106 			x++;
    107 		}
    108 	}
    109 
    110 	/* create data for bsd slices */
    111 	for (i = 0; i < 4; i++)
    112 	{
    113 		fp = (fdisk_partition *) (mbr + 0x1be) + i;
    114 		switch (fp->type)
    115 		{
    116 			case FDISK_TYPE_FREEBSD:
    117 			case FDISK_TYPE_OPENBSD:
    118 				blk_read (dev, label, fp->start_sect_num + 1, 1);
    119 				for (j = 0; j < BSD_MAXSLICES; j++)
    120 					if (slice->d_partitions[j].p_fstype != BSD_FS_UNUSED)
    121 					{
    122 						disk->partition[x].name[0] = '0' + i;
    123 						disk->partition[x].name[1] = 'a' + j;
    124 						disk->partition[x].name[2] = 0;
    125 						disk->partition[x].start =
    126 							slice->d_partitions[j].p_offset;
    127 						disk->partition[x].size =
    128 							slice->d_partitions[j].p_size;
    129 						disk->partition[x].type = blt_internal_code (fp->type,
    130 							slice->d_partitions[j].p_fstype);
    131 						x++;
    132 					}
    133 				break;
    134 		}
    135 	}
    136 
    137 	free (mbr);
    138 	free (label);
    139 	return disk;
    140 }
    141 
    142 void disk_free (disk_t *disk)
    143 {
    144 	free (disk->partition);
    145 	free (disk);
    146 }
    147 
    148 const char *disk_partition_name (disk_t *disk, int partition)
    149 {
    150 	return disk->partition[partition].name;
    151 }
    152 
    153 unsigned long long disk_partition_start (disk_t *disk, int partition)
    154 {
    155 	return disk->partition[partition].start;
    156 }
    157 
    158 unsigned long long disk_partition_size (disk_t *disk, int partition)
    159 {
    160 	return disk->partition[partition].size;
    161 }
    162 
    163 unsigned int disk_partition_type (disk_t *disk, int partition)
    164 {
    165 	return disk->partition[partition].type;
    166 }
    167