1 /* $NetBSD: vtoc.c,v 1.4 2008/04/28 20:23:18 martin 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 <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: vtoc.c,v 1.4 2008/04/28 20:23:18 martin Exp $");
34
35 #include <sys/types.h>
36 #include <sys/systm.h>
37
38 #if !defined(_KERNEL) && defined(_STANDALONE)
39 #include <lib/libsa/stand.h>
40 #include <lib/libkern/libkern.h>
41 #endif
42
43 #ifdef VTOC_DEBUG
44 #define DPRINTF(fmt, args...) printf(fmt, ##args)
45 #else
46 #define DPRINTF(arg...) ((void)0)
47 #endif
48
49 #include <machine/sector.h>
50 #include <machine/vtoc.h>
51
52 #ifdef VTOC_DEBUG
53 void vtoc_print_partition_table(const struct ux_partition *);
54 #endif /* VTOC_DEBUG */
55
56 bool
vtoc_sector(void * rwops,struct vtoc_sector * vtoc,int start)57 vtoc_sector(void *rwops, struct vtoc_sector *vtoc, int start)
58 {
59
60 if (!sector_read(rwops, (void *)vtoc, start + VTOC_SECTOR))
61 return false;
62
63 if (!vtoc_sanity(vtoc))
64 return false;
65
66 return true;
67 }
68
69 const struct ux_partition *
vtoc_find_bfs(const struct vtoc_sector * vtoc)70 vtoc_find_bfs(const struct vtoc_sector *vtoc)
71 {
72 int i;
73
74 if (!vtoc_valid(vtoc)) {
75 printf("invalid VTOC\n");
76 return 0;
77 }
78
79 for (i = 0; i < vtoc->npartitions; i++)
80 if (vtoc->partition[i].tag == VTOC_TAG_STAND)
81 break;
82
83 if (i == vtoc->npartitions) {
84 printf("no bfs partition found.\n");
85 return 0;
86 }
87
88 return &vtoc->partition[i];
89 }
90
91 bool
vtoc_valid(const struct vtoc_sector * vtoc)92 vtoc_valid(const struct vtoc_sector *vtoc)
93 {
94
95 return (vtoc->magic == VTOC_MAGIC) && (vtoc->version == VTOC_VERSION);
96 }
97
98 bool
vtoc_sanity(const struct vtoc_sector * vtoc)99 vtoc_sanity(const struct vtoc_sector *vtoc)
100 {
101
102 if (!vtoc_valid(vtoc)) {
103 DPRINTF("Invalid VTOC.\n");
104 return false;
105 }
106
107 DPRINTF("[VTOC] (%d byte)\n", sizeof *vtoc);
108 DPRINTF("Bootinfo = %08x %08x %08x\n",
109 vtoc->bootinfo[0], vtoc->bootinfo[1], vtoc->bootinfo[2]);
110 DPRINTF("Magic = %08x\n", vtoc->magic);
111 DPRINTF("Version = %d\n", vtoc->version);
112 DPRINTF("Volume = %s\n", vtoc->volume);
113 DPRINTF("Sector size = %d\n", vtoc->sector_size_byte);
114 DPRINTF("# of partitions = %d\n", vtoc->npartitions);
115 #ifdef VTOC_DEBUG
116 vtoc_print_partition_table(vtoc->partition);
117 #endif
118 return true;
119 }
120
121 #ifdef VTOC_DEBUG
122 void
vtoc_print_partition_table(const struct ux_partition * p)123 vtoc_print_partition_table(const struct ux_partition *p)
124 {
125 int i;
126
127 DPRINTF(" Partition Tag Flags Start Count Last\n");
128 for (i = 0; i < 16; i++, p++) {
129 DPRINTF(" %x %d %02x %8d %8d %8d\n",
130 i, p->tag, p->flags, p->start_sector, p->nsectors,
131 p->start_sector + p->nsectors - 1);
132 }
133 }
134 #endif /* VTOC_DEBUG */
135