ustarfs.c revision 1.2 1 1.2 christos /* $NetBSD: ustarfs.c,v 1.2 2006/01/25 18:28:26 christos Exp $ */
2 1.1 tsutsui
3 1.1 tsutsui /*-
4 1.1 tsutsui * Copyright (c) 2004 The NetBSD Foundation, Inc.
5 1.1 tsutsui * All rights reserved.
6 1.1 tsutsui *
7 1.1 tsutsui * This code is derived from software contributed to The NetBSD Foundation
8 1.1 tsutsui * by UCHIYAMA Yasushi.
9 1.1 tsutsui *
10 1.1 tsutsui * Redistribution and use in source and binary forms, with or without
11 1.1 tsutsui * modification, are permitted provided that the following conditions
12 1.1 tsutsui * are met:
13 1.1 tsutsui * 1. Redistributions of source code must retain the above copyright
14 1.1 tsutsui * notice, this list of conditions and the following disclaimer.
15 1.1 tsutsui * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 tsutsui * notice, this list of conditions and the following disclaimer in the
17 1.1 tsutsui * documentation and/or other materials provided with the distribution.
18 1.1 tsutsui * 3. All advertising materials mentioning features or use of this software
19 1.1 tsutsui * must display the following acknowledgement:
20 1.1 tsutsui * This product includes software developed by the NetBSD
21 1.1 tsutsui * Foundation, Inc. and its contributors.
22 1.1 tsutsui * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 tsutsui * contributors may be used to endorse or promote products derived
24 1.1 tsutsui * from this software without specific prior written permission.
25 1.1 tsutsui *
26 1.1 tsutsui * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 tsutsui * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 tsutsui * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 tsutsui * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 tsutsui * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 tsutsui * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 tsutsui * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 tsutsui * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 tsutsui * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 tsutsui * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 tsutsui * POSSIBILITY OF SUCH DAMAGE.
37 1.1 tsutsui */
38 1.1 tsutsui
39 1.1 tsutsui #include <lib/libsa/stand.h>
40 1.1 tsutsui #include <lib/libkern/libkern.h>
41 1.1 tsutsui
42 1.1 tsutsui #include <machine/param.h>
43 1.1 tsutsui #include <machine/sbd.h>
44 1.1 tsutsui #include <machine/sector.h>
45 1.1 tsutsui
46 1.1 tsutsui #include "local.h"
47 1.1 tsutsui #include "common.h"
48 1.1 tsutsui
49 1.1 tsutsui boolean_t __ustarfs_file(int, char *, size_t *);
50 1.1 tsutsui boolean_t __block_read(uint8_t *, int);
51 1.1 tsutsui boolean_t __block_read_n(uint8_t *, int, int);
52 1.1 tsutsui void __change_volume(int);
53 1.1 tsutsui
54 1.1 tsutsui enum { USTAR_BLOCK_SIZE = 8192 };/* Check src/distrib/common/buildfloppies.sh */
55 1.1 tsutsui struct volume {
56 1.1 tsutsui int max_block;
57 1.1 tsutsui int current_volume;
58 1.1 tsutsui int block_offset;
59 1.1 tsutsui } __volume;
60 1.1 tsutsui
61 1.1 tsutsui boolean_t
62 1.1 tsutsui ustarfs_load(const char *file, void **addrp, size_t *sizep)
63 1.1 tsutsui {
64 1.1 tsutsui char fname[16];
65 1.1 tsutsui int block = 16;
66 1.1 tsutsui size_t sz;
67 1.1 tsutsui int maxblk;
68 1.1 tsutsui
69 1.1 tsutsui if (DEVICE_CAPABILITY.active_device == NVSRAM_BOOTDEV_HARDDISK)
70 1.1 tsutsui maxblk = 0x1fffffff; /* no limit */
71 1.1 tsutsui else if (DEVICE_CAPABILITY.active_device == NVSRAM_BOOTDEV_FLOPPYDISK)
72 1.1 tsutsui /*
73 1.1 tsutsui * Although phisical format isn't 2D, volume size is
74 1.1 tsutsui * limited to size of 2D.
75 1.1 tsutsui */
76 1.1 tsutsui maxblk = (77 + 76) * 13;
77 1.1 tsutsui else {
78 1.1 tsutsui printf("not supported device.\n");
79 1.1 tsutsui return FALSE;
80 1.1 tsutsui }
81 1.1 tsutsui
82 1.1 tsutsui /* Truncate to ustar block boundary */
83 1.1 tsutsui __volume.max_block = (maxblk / (USTAR_BLOCK_SIZE >> DEV_BSHIFT)) *
84 1.1 tsutsui (USTAR_BLOCK_SIZE >> DEV_BSHIFT);
85 1.1 tsutsui __volume.block_offset = 0;
86 1.1 tsutsui __volume.current_volume = 0;
87 1.1 tsutsui
88 1.1 tsutsui /* Find file */
89 1.1 tsutsui while (/*CONSTCOND*/1) {
90 1.1 tsutsui if (!__ustarfs_file(block, fname, &sz))
91 1.1 tsutsui return FALSE;
92 1.1 tsutsui
93 1.1 tsutsui if (strcmp(file, fname) == 0)
94 1.1 tsutsui break;
95 1.1 tsutsui block += (ROUND_SECTOR(sz) >> DEV_BSHIFT) + 1;
96 1.1 tsutsui }
97 1.1 tsutsui block++; /* skip tar header */
98 1.1 tsutsui *sizep = sz;
99 1.1 tsutsui
100 1.1 tsutsui /* Load file */
101 1.1 tsutsui sz = ROUND_SECTOR(sz);
102 1.1 tsutsui if ((*addrp = alloc(sz)) == 0) {
103 1.1 tsutsui printf("%s: can't allocate memory.\n", __FUNCTION__);
104 1.1 tsutsui return FALSE;
105 1.1 tsutsui }
106 1.1 tsutsui
107 1.1 tsutsui if (!__block_read_n(*addrp, block, sz >> DEV_BSHIFT)) {
108 1.1 tsutsui printf("%s: can't load file.\n", __FUNCTION__);
109 1.2 christos dealloc(*addrp, sz);
110 1.1 tsutsui return FALSE;
111 1.1 tsutsui }
112 1.1 tsutsui
113 1.1 tsutsui return TRUE;
114 1.1 tsutsui }
115 1.1 tsutsui
116 1.1 tsutsui boolean_t
117 1.1 tsutsui __ustarfs_file(int start_block, char *file, size_t *size)
118 1.1 tsutsui {
119 1.1 tsutsui uint8_t buf[512];
120 1.1 tsutsui
121 1.1 tsutsui if (!__block_read(buf, start_block)) {
122 1.1 tsutsui printf("can't read tar header.\n");
123 1.1 tsutsui return FALSE;
124 1.1 tsutsui }
125 1.1 tsutsui if (((*(uint32_t *)(buf + 256)) & 0xffffff) != 0x757374) {
126 1.1 tsutsui printf("bad tar magic.\n");
127 1.1 tsutsui return FALSE;
128 1.1 tsutsui }
129 1.1 tsutsui *size = strtoul(buf + 124, 0, 0);
130 1.1 tsutsui strncpy(file, buf, 16);
131 1.1 tsutsui
132 1.1 tsutsui return TRUE;
133 1.1 tsutsui }
134 1.1 tsutsui
135 1.1 tsutsui boolean_t
136 1.1 tsutsui __block_read_n(uint8_t *buf, int blk, int count)
137 1.1 tsutsui {
138 1.1 tsutsui int i;
139 1.1 tsutsui
140 1.1 tsutsui for (i = 0; i < count; i++, buf += DEV_BSIZE)
141 1.1 tsutsui if (!__block_read(buf, blk + i))
142 1.1 tsutsui return FALSE;
143 1.1 tsutsui
144 1.1 tsutsui return TRUE;
145 1.1 tsutsui }
146 1.1 tsutsui
147 1.1 tsutsui boolean_t
148 1.1 tsutsui __block_read(uint8_t *buf, int blk)
149 1.1 tsutsui {
150 1.1 tsutsui int vol;
151 1.1 tsutsui
152 1.1 tsutsui if ((vol = blk / __volume.max_block) != __volume.current_volume) {
153 1.1 tsutsui __change_volume(vol);
154 1.1 tsutsui __volume.current_volume = vol;
155 1.1 tsutsui /* volume offset + ustarfs header (8k) */
156 1.1 tsutsui __volume.block_offset = vol * __volume.max_block - 16;
157 1.1 tsutsui }
158 1.1 tsutsui
159 1.1 tsutsui return sector_read(0, buf, blk - __volume.block_offset);
160 1.1 tsutsui }
161 1.1 tsutsui
162 1.1 tsutsui void
163 1.1 tsutsui __change_volume(int volume)
164 1.1 tsutsui {
165 1.1 tsutsui uint8_t buf[512];
166 1.1 tsutsui int i;
167 1.1 tsutsui
168 1.1 tsutsui while (/*CONSTCOND*/1) {
169 1.1 tsutsui printf("insert disk %d, and press return...", volume + 1);
170 1.1 tsutsui while (getchar() != '\r')
171 1.1 tsutsui ;
172 1.1 tsutsui printf("\n");
173 1.1 tsutsui if (!sector_read(0, buf, 0))
174 1.1 tsutsui continue;
175 1.1 tsutsui if (*(uint32_t *)buf != 0x55535441) { /* "USTAR" */
176 1.1 tsutsui printf("invalid magic.\n");
177 1.1 tsutsui continue;
178 1.1 tsutsui }
179 1.1 tsutsui if ((i = buf[8] - '0') != volume + 1) {
180 1.1 tsutsui printf("invalid volume number. disk=%d requested=%d\n",
181 1.1 tsutsui i, volume + 1);
182 1.1 tsutsui continue;
183 1.1 tsutsui }
184 1.1 tsutsui return;
185 1.1 tsutsui }
186 1.1 tsutsui }
187