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