xref: /netbsd-src/sys/arch/ews4800mips/stand/common/diskutil.c (revision aaf4ece63a859a04e37cf3a7229b5fab0157cc06)
1 /*	$NetBSD: diskutil.c,v 1.1 2005/12/29 15:20:09 tsutsui 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  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #include <lib/libsa/stand.h>
40 #include <lib/libsa/ufs.h>
41 #include <lib/libkern/libkern.h>
42 
43 #include <machine/pdinfo.h>
44 #include <machine/vtoc.h>
45 #include <machine/bfs.h>
46 
47 #include "cmd.h"
48 #include "local.h"
49 
50 struct pdinfo_sector pdinfo;
51 struct vtoc_sector vtoc;
52 int vtoc_readed;
53 
54 void bfs_ls(void);
55 
56 int
57 cmd_disklabel(int argc, char *argp[], int interactive)
58 {
59 	struct ux_partition *partition;
60 	int i;
61 
62 	if (!read_vtoc())
63 		return 1;
64 
65 	partition = vtoc.partition;
66 	printf("Tag\tFlags\tStart\tCount\n");
67 	for (i = 0; i < VTOC_MAXPARTITIONS; i++, partition++)
68 		printf("   %d %d   %d\t%d\t%d\n", i, partition->tag,
69 		    partition->flags, partition->start_sector,
70 		    partition->nsectors);
71 
72 	return 0;
73 }
74 
75 int
76 cmd_ls(int argc, char *argp[], int interactive)
77 {
78 	int i;
79 
80 	if (argc < 2) {
81 		printf("ls partition\n");
82 		return 1;
83 	}
84 
85 	if (!read_vtoc())
86 		return 1;
87 
88 	i = strtoul(argp[1], 0, 0);
89 	if (i < 0 || i >= VTOC_MAXPARTITIONS)
90 		return 1;
91 
92 	if (!device_attach(-1, -1, i))
93 		return 1;
94 	switch (fstype(i)) {
95 	case FSTYPE_UFS:
96 		ufs_ls("/");
97 		break;
98 	case FSTYPE_BFS:
99 		bfs_ls();
100 		break;
101 	default:
102 		return 1;
103 	}
104 
105 	return 0;
106 }
107 
108 void
109 bfs_ls(void)
110 {
111 	struct bfs *bfs;
112 	struct bfs_dirent *file;
113 	struct bfs_inode *inode;
114 	int i;
115 
116 	if (!DEVICE_CAPABILITY.disk_enabled)
117 		return;
118 
119 	if (bfs_init(&bfs) != 0)
120 		return;
121 
122 	for (file = bfs->dirent, i = 0; i < bfs->max_dirent; i++, file++) {
123 		if (file->inode != 0) {
124 			inode = &bfs->inode[file->inode - BFS_ROOT_INODE];
125 			printf("%s\t%d (%d-%d)\n", file->name,
126 			    bfs_file_size(inode), inode->start_sector,
127 			    inode->end_sector);
128 		}
129 	}
130 
131 	bfs_fini(bfs);
132 }
133 
134 int
135 fstype(int partition)
136 {
137 	struct ux_partition *p;
138 
139 	if (!read_vtoc())
140 		return -1;
141 
142 	if (partition < 0 || partition >= VTOC_MAXPARTITIONS)
143 		return -1;
144 
145 	p = &vtoc.partition[partition];
146 	if (p->tag == VTOC_TAG_STAND)
147 		return FSTYPE_BFS;
148 
149 	if ((p->flags & VTOC_FLAG_UNMOUNT) == 0)
150 		return FSTYPE_UFS; /* possibly */
151 
152 	return -1;
153 }
154 
155 boolean_t
156 find_partition_start(int partition,  int *sector)
157 {
158 
159 	if (!read_vtoc())
160 		return FALSE;
161 
162 	*sector = pdinfo.logical_sector +
163 	    vtoc.partition[partition].start_sector;
164 	printf("[partition=%d, start sector=%d]", partition, *sector);
165 
166 	return TRUE;
167 }
168 
169 boolean_t
170 read_vtoc(void)
171 {
172 
173 	if (!DEVICE_CAPABILITY.disk_enabled)
174 		return FALSE;
175 
176 	if (vtoc_readed)
177 		return TRUE;
178 
179 	if (!pdinfo_sector(0, &pdinfo)) {
180 		printf("no PDINFO\n");
181 		return FALSE;
182 	}
183 
184 	if (!vtoc_sector(0, &vtoc, pdinfo.logical_sector)) {
185 		printf("no VTOC\n");
186 		return FALSE;
187 	}
188 	vtoc_readed = TRUE;
189 
190 	return TRUE;
191 }
192