xref: /netbsd-src/sys/arch/ews4800mips/stand/common/diskutil.c (revision c2e8ce644bcf0a6856a25f9a8b5bf45c845b918e)
1 /*	$NetBSD: diskutil.c,v 1.5 2011/12/25 06:09: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  *
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
cmd_disklabel(int argc,char * argp[],int interactive)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
cmd_ls(int argc,char * argp[],int interactive)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_BFS:
89 		bfs_ls();
90 		break;
91 	default:
92 		ls("/");
93 		break;
94 	}
95 
96 	return 0;
97 }
98 
99 void
bfs_ls(void)100 bfs_ls(void)
101 {
102 	struct bfs *bfs;
103 	struct bfs_dirent *file;
104 	struct bfs_inode *inode;
105 	int i;
106 
107 	if (!DEVICE_CAPABILITY.disk_enabled)
108 		return;
109 
110 	if (bfs_init(&bfs) != 0)
111 		return;
112 
113 	for (file = bfs->dirent, i = 0; i < bfs->max_dirent; i++, file++) {
114 		if (file->inode != 0) {
115 			inode = &bfs->inode[file->inode - BFS_ROOT_INODE];
116 			printf("%s\t%d (%d-%d)\n", file->name,
117 			    bfs_file_size(inode), inode->start_sector,
118 			    inode->end_sector);
119 		}
120 	}
121 
122 	bfs_fini(bfs);
123 }
124 
125 int
fstype(int partition)126 fstype(int partition)
127 {
128 	struct ux_partition *p;
129 
130 	if (!read_vtoc())
131 		return -1;
132 
133 	if (partition < 0 || partition >= VTOC_MAXPARTITIONS)
134 		return -1;
135 
136 	p = &vtoc.partition[partition];
137 	if (p->tag == VTOC_TAG_STAND)
138 		return FSTYPE_BFS;
139 
140 	if ((p->flags & VTOC_FLAG_UNMOUNT) == 0)
141 		return FSTYPE_UFS; /* possibly */
142 
143 	return -1;
144 }
145 
146 bool
find_partition_start(int partition,int * sector)147 find_partition_start(int partition,  int *sector)
148 {
149 
150 	if (!read_vtoc())
151 		return false;
152 
153 	*sector = pdinfo.logical_sector +
154 	    vtoc.partition[partition].start_sector;
155 	printf("[partition=%d, start sector=%d]", partition, *sector);
156 
157 	return true;
158 }
159 
160 bool
read_vtoc(void)161 read_vtoc(void)
162 {
163 
164 	if (!DEVICE_CAPABILITY.disk_enabled)
165 		return false;
166 
167 	if (vtoc_readed)
168 		return true;
169 
170 	if (!pdinfo_sector(0, &pdinfo)) {
171 		printf("no PDINFO\n");
172 		return false;
173 	}
174 
175 	if (!vtoc_sector(0, &vtoc, pdinfo.logical_sector)) {
176 		printf("no VTOC\n");
177 		return false;
178 	}
179 	vtoc_readed = true;
180 
181 	return true;
182 }
183