Home | History | Annotate | Line # | Download | only in common
diskutil.c revision 1.3.44.1
      1 /*	$NetBSD: diskutil.c,v 1.3.44.1 2008/05/18 12:31:55 yamt Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2004 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by UCHIYAMA Yasushi.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <lib/libsa/stand.h>
     33 #include <lib/libsa/ufs.h>
     34 #include <lib/libkern/libkern.h>
     35 
     36 #include <machine/pdinfo.h>
     37 #include <machine/vtoc.h>
     38 #include <machine/bfs.h>
     39 
     40 #include "cmd.h"
     41 #include "local.h"
     42 
     43 struct pdinfo_sector pdinfo;
     44 struct vtoc_sector vtoc;
     45 int vtoc_readed;
     46 
     47 void bfs_ls(void);
     48 
     49 int
     50 cmd_disklabel(int argc, char *argp[], int interactive)
     51 {
     52 	struct ux_partition *partition;
     53 	int i;
     54 
     55 	if (!read_vtoc())
     56 		return 1;
     57 
     58 	partition = vtoc.partition;
     59 	printf("Tag\tFlags\tStart\tCount\n");
     60 	for (i = 0; i < VTOC_MAXPARTITIONS; i++, partition++)
     61 		printf("   %d %d   %d\t%d\t%d\n", i, partition->tag,
     62 		    partition->flags, partition->start_sector,
     63 		    partition->nsectors);
     64 
     65 	return 0;
     66 }
     67 
     68 int
     69 cmd_ls(int argc, char *argp[], int interactive)
     70 {
     71 	int i;
     72 
     73 	if (argc < 2) {
     74 		printf("ls partition\n");
     75 		return 1;
     76 	}
     77 
     78 	if (!read_vtoc())
     79 		return 1;
     80 
     81 	i = strtoul(argp[1], 0, 0);
     82 	if (i < 0 || i >= VTOC_MAXPARTITIONS)
     83 		return 1;
     84 
     85 	if (!device_attach(-1, -1, i))
     86 		return 1;
     87 	switch (fstype(i)) {
     88 	case FSTYPE_UFS:
     89 		ufs_ls("/");
     90 		break;
     91 	case FSTYPE_BFS:
     92 		bfs_ls();
     93 		break;
     94 	default:
     95 		return 1;
     96 	}
     97 
     98 	return 0;
     99 }
    100 
    101 void
    102 bfs_ls(void)
    103 {
    104 	struct bfs *bfs;
    105 	struct bfs_dirent *file;
    106 	struct bfs_inode *inode;
    107 	int i;
    108 
    109 	if (!DEVICE_CAPABILITY.disk_enabled)
    110 		return;
    111 
    112 	if (bfs_init(&bfs) != 0)
    113 		return;
    114 
    115 	for (file = bfs->dirent, i = 0; i < bfs->max_dirent; i++, file++) {
    116 		if (file->inode != 0) {
    117 			inode = &bfs->inode[file->inode - BFS_ROOT_INODE];
    118 			printf("%s\t%d (%d-%d)\n", file->name,
    119 			    bfs_file_size(inode), inode->start_sector,
    120 			    inode->end_sector);
    121 		}
    122 	}
    123 
    124 	bfs_fini(bfs);
    125 }
    126 
    127 int
    128 fstype(int partition)
    129 {
    130 	struct ux_partition *p;
    131 
    132 	if (!read_vtoc())
    133 		return -1;
    134 
    135 	if (partition < 0 || partition >= VTOC_MAXPARTITIONS)
    136 		return -1;
    137 
    138 	p = &vtoc.partition[partition];
    139 	if (p->tag == VTOC_TAG_STAND)
    140 		return FSTYPE_BFS;
    141 
    142 	if ((p->flags & VTOC_FLAG_UNMOUNT) == 0)
    143 		return FSTYPE_UFS; /* possibly */
    144 
    145 	return -1;
    146 }
    147 
    148 bool
    149 find_partition_start(int partition,  int *sector)
    150 {
    151 
    152 	if (!read_vtoc())
    153 		return false;
    154 
    155 	*sector = pdinfo.logical_sector +
    156 	    vtoc.partition[partition].start_sector;
    157 	printf("[partition=%d, start sector=%d]", partition, *sector);
    158 
    159 	return true;
    160 }
    161 
    162 bool
    163 read_vtoc(void)
    164 {
    165 
    166 	if (!DEVICE_CAPABILITY.disk_enabled)
    167 		return false;
    168 
    169 	if (vtoc_readed)
    170 		return true;
    171 
    172 	if (!pdinfo_sector(0, &pdinfo)) {
    173 		printf("no PDINFO\n");
    174 		return false;
    175 	}
    176 
    177 	if (!vtoc_sector(0, &vtoc, pdinfo.logical_sector)) {
    178 		printf("no VTOC\n");
    179 		return false;
    180 	}
    181 	vtoc_readed = true;
    182 
    183 	return true;
    184 }
    185