Sync usage with man page.
[netbsd-mini2440.git] / sys / arch / ews4800mips / stand / common / diskutil.c
blob187a1eda83aa2163fc362c2edc2c0a92710d75c7
1 /* $NetBSD: diskutil.c,v 1.3 2007/02/22 05:31:53 thorpej Exp $ */
3 /*-
4 * Copyright (c) 2004 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by UCHIYAMA Yasushi.
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.
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.
32 #include <lib/libsa/stand.h>
33 #include <lib/libsa/ufs.h>
34 #include <lib/libkern/libkern.h>
36 #include <machine/pdinfo.h>
37 #include <machine/vtoc.h>
38 #include <machine/bfs.h>
40 #include "cmd.h"
41 #include "local.h"
43 struct pdinfo_sector pdinfo;
44 struct vtoc_sector vtoc;
45 int vtoc_readed;
47 void bfs_ls(void);
49 int
50 cmd_disklabel(int argc, char *argp[], int interactive)
52 struct ux_partition *partition;
53 int i;
55 if (!read_vtoc())
56 return 1;
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);
65 return 0;
68 int
69 cmd_ls(int argc, char *argp[], int interactive)
71 int i;
73 if (argc < 2) {
74 printf("ls partition\n");
75 return 1;
78 if (!read_vtoc())
79 return 1;
81 i = strtoul(argp[1], 0, 0);
82 if (i < 0 || i >= VTOC_MAXPARTITIONS)
83 return 1;
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;
98 return 0;
101 void
102 bfs_ls(void)
104 struct bfs *bfs;
105 struct bfs_dirent *file;
106 struct bfs_inode *inode;
107 int i;
109 if (!DEVICE_CAPABILITY.disk_enabled)
110 return;
112 if (bfs_init(&bfs) != 0)
113 return;
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);
124 bfs_fini(bfs);
128 fstype(int partition)
130 struct ux_partition *p;
132 if (!read_vtoc())
133 return -1;
135 if (partition < 0 || partition >= VTOC_MAXPARTITIONS)
136 return -1;
138 p = &vtoc.partition[partition];
139 if (p->tag == VTOC_TAG_STAND)
140 return FSTYPE_BFS;
142 if ((p->flags & VTOC_FLAG_UNMOUNT) == 0)
143 return FSTYPE_UFS; /* possibly */
145 return -1;
148 bool
149 find_partition_start(int partition, int *sector)
152 if (!read_vtoc())
153 return false;
155 *sector = pdinfo.logical_sector +
156 vtoc.partition[partition].start_sector;
157 printf("[partition=%d, start sector=%d]", partition, *sector);
159 return true;
162 bool
163 read_vtoc(void)
166 if (!DEVICE_CAPABILITY.disk_enabled)
167 return false;
169 if (vtoc_readed)
170 return true;
172 if (!pdinfo_sector(0, &pdinfo)) {
173 printf("no PDINFO\n");
174 return false;
177 if (!vtoc_sector(0, &vtoc, pdinfo.logical_sector)) {
178 printf("no VTOC\n");
179 return false;
181 vtoc_readed = true;
183 return true;