sgivol.c revision 1.16 1 1.16 martin /* $NetBSD: sgivol.c,v 1.16 2008/04/28 20:23:34 martin Exp $ */
2 1.1 soren
3 1.1 soren /*-
4 1.1 soren * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 1.1 soren * All rights reserved.
6 1.1 soren *
7 1.1 soren * This code is derived from software contributed to The NetBSD Foundation
8 1.1 soren * by Michael Hitch and Hubert Feyrer.
9 1.1 soren *
10 1.1 soren * Redistribution and use in source and binary forms, with or without
11 1.1 soren * modification, are permitted provided that the following conditions
12 1.1 soren * are met:
13 1.1 soren * 1. Redistributions of source code must retain the above copyright
14 1.1 soren * notice, this list of conditions and the following disclaimer.
15 1.1 soren * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 soren * notice, this list of conditions and the following disclaimer in the
17 1.1 soren * documentation and/or other materials provided with the distribution.
18 1.1 soren *
19 1.1 soren * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 soren * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 soren * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 soren * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 soren * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 soren * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 soren * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 soren * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 soren * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 soren * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 soren * POSSIBILITY OF SUCH DAMAGE.
30 1.1 soren */
31 1.1 soren
32 1.9 jmc #if HAVE_NBTOOL_CONFIG_H
33 1.9 jmc #include "nbtool_config.h"
34 1.9 jmc #endif
35 1.9 jmc
36 1.2 thorpej #include <sys/types.h>
37 1.2 thorpej #include <sys/ioctl.h>
38 1.8 sekiya #include <sys/stat.h>
39 1.9 jmc
40 1.9 jmc #if HAVE_NBTOOL_CONFIG_H
41 1.9 jmc #include "../../../../../sys/sys/bootblock.h"
42 1.9 jmc /* Ficticious geometry for cross tool usage against a file image */
43 1.9 jmc #define SGIVOL_NBTOOL_NSECS 32
44 1.9 jmc #define SGIVOL_NBTOOL_NTRACKS 64
45 1.9 jmc #else
46 1.2 thorpej #include <sys/disklabel.h>
47 1.9 jmc #endif
48 1.2 thorpej
49 1.5 rafal #include <errno.h>
50 1.1 soren #include <stdio.h>
51 1.2 thorpej #include <stdlib.h>
52 1.1 soren #include <unistd.h>
53 1.1 soren #include <string.h>
54 1.1 soren #include <fcntl.h>
55 1.1 soren #include <util.h>
56 1.12 thorpej #ifndef HAVE_NBTOOL_CONFIG_H
57 1.7 sekiya #include <sys/endian.h>
58 1.12 thorpej #endif
59 1.1 soren
60 1.1 soren int fd;
61 1.1 soren int opt_i; /* Initialize volume header */
62 1.1 soren int opt_r; /* Read a file from volume header */
63 1.1 soren int opt_w; /* Write a file to volume header */
64 1.1 soren int opt_d; /* Delete a file from volume header */
65 1.1 soren int opt_p; /* Modify a partition */
66 1.5 rafal int opt_q; /* quiet mode */
67 1.5 rafal int opt_f; /* Don't ask, just do what you're told */
68 1.1 soren int partno, partfirst, partblocks, parttype;
69 1.9 jmc struct sgi_boot_block *volhdr;
70 1.2 thorpej int32_t checksum;
71 1.9 jmc u_int32_t volhdr_size = SGI_BOOT_BLOCK_SIZE_VOLHDR;
72 1.2 thorpej
73 1.2 thorpej const char *vfilename = "";
74 1.2 thorpej const char *ufilename = "";
75 1.1 soren
76 1.9 jmc #if HAVE_NBTOOL_CONFIG_H
77 1.9 jmc struct stat st;
78 1.9 jmc #else
79 1.1 soren struct disklabel lbl;
80 1.9 jmc #endif
81 1.1 soren
82 1.1 soren unsigned char buf[512];
83 1.1 soren
84 1.2 thorpej const char *sgi_types[] = {
85 1.1 soren "Volume Header",
86 1.1 soren "Repl Trks",
87 1.1 soren "Repl Secs",
88 1.1 soren "Raw",
89 1.1 soren "BSD4.2",
90 1.1 soren "SysV",
91 1.1 soren "Volume",
92 1.1 soren "EFS",
93 1.1 soren "LVol",
94 1.1 soren "RLVol",
95 1.1 soren "XFS",
96 1.1 soren "XSFLog",
97 1.1 soren "XLV",
98 1.1 soren "XVM"
99 1.1 soren };
100 1.1 soren
101 1.2 thorpej int main(int, char *[]);
102 1.2 thorpej
103 1.2 thorpej void display_vol(void);
104 1.2 thorpej void init_volhdr(void);
105 1.2 thorpej void read_file(void);
106 1.2 thorpej void write_file(void);
107 1.2 thorpej void delete_file(void);
108 1.2 thorpej void modify_partition(void);
109 1.2 thorpej void write_volhdr(void);
110 1.2 thorpej int allocate_space(int);
111 1.2 thorpej void checksum_vol(void);
112 1.2 thorpej void usage(void);
113 1.2 thorpej
114 1.2 thorpej int
115 1.2 thorpej main(int argc, char *argv[])
116 1.1 soren {
117 1.5 rafal int ch;
118 1.7 sekiya while ((ch = getopt(argc, argv, "irwpdqfh:")) != -1) {
119 1.5 rafal switch (ch) {
120 1.5 rafal /* -i, -r, -w, -d and -p override each other */
121 1.5 rafal /* -q implies -f */
122 1.5 rafal case 'q':
123 1.5 rafal ++opt_q;
124 1.5 rafal ++opt_f;
125 1.5 rafal break;
126 1.5 rafal case 'f':
127 1.5 rafal ++opt_f;
128 1.5 rafal break;
129 1.1 soren case 'i':
130 1.1 soren ++opt_i;
131 1.5 rafal opt_r = opt_w = opt_d = opt_p = 0;
132 1.1 soren break;
133 1.7 sekiya case 'h':
134 1.7 sekiya volhdr_size = atoi(optarg);
135 1.7 sekiya break;
136 1.1 soren case 'r':
137 1.5 rafal ++opt_r;
138 1.5 rafal opt_i = opt_w = opt_d = opt_p = 0;
139 1.5 rafal break;
140 1.1 soren case 'w':
141 1.5 rafal ++opt_w;
142 1.5 rafal opt_i = opt_r = opt_d = opt_p = 0;
143 1.1 soren break;
144 1.1 soren case 'd':
145 1.1 soren ++opt_d;
146 1.5 rafal opt_i = opt_r = opt_w = opt_p = 0;
147 1.5 rafal break;
148 1.1 soren case 'p':
149 1.1 soren ++opt_p;
150 1.5 rafal opt_i = opt_r = opt_w = opt_d = 0;
151 1.5 rafal partno = atoi(argv[0]);
152 1.5 rafal partfirst = atoi(argv[1]);
153 1.5 rafal partblocks = atoi(argv[2]);
154 1.5 rafal parttype = atoi(argv[3]);
155 1.1 soren break;
156 1.5 rafal case '?':
157 1.1 soren default:
158 1.1 soren usage();
159 1.1 soren }
160 1.1 soren }
161 1.5 rafal argc -= optind;
162 1.5 rafal argv += optind;
163 1.5 rafal
164 1.5 rafal if (opt_r || opt_w) {
165 1.5 rafal if (argc != 3)
166 1.5 rafal usage();
167 1.5 rafal vfilename = argv[0];
168 1.5 rafal ufilename = argv[1];
169 1.5 rafal argc -= 2;
170 1.5 rafal argv += 2;
171 1.5 rafal }
172 1.5 rafal if (opt_d) {
173 1.5 rafal if (argc != 2)
174 1.5 rafal usage();
175 1.5 rafal vfilename = argv[0];
176 1.5 rafal argc--;
177 1.5 rafal argv++;
178 1.5 rafal }
179 1.1 soren
180 1.5 rafal if (opt_p) {
181 1.5 rafal if (argc != 5)
182 1.5 rafal usage();
183 1.5 rafal partno = atoi(argv[0]);
184 1.5 rafal partfirst = atoi(argv[1]);
185 1.5 rafal partblocks = atoi(argv[2]);
186 1.5 rafal parttype = atoi(argv[3]);
187 1.5 rafal argc -= 4;
188 1.5 rafal argv += 4;
189 1.5 rafal }
190 1.5 rafal if (argc != 1)
191 1.1 soren usage();
192 1.5 rafal
193 1.5 rafal fd = open(argv[0], (opt_i | opt_w | opt_d | opt_p) ? O_RDWR : O_RDONLY);
194 1.1 soren if (fd < 0) {
195 1.9 jmc #if HAVE_NBTOOL_CONFIG_H
196 1.9 jmc perror("File open");
197 1.9 jmc exit(1);
198 1.9 jmc #else
199 1.13 mrg sprintf((char *)buf, "/dev/r%s%c", argv[0], 'a' + getrawpartition());
200 1.13 mrg fd = open((char *)buf, (opt_i | opt_w | opt_d | opt_p)
201 1.5 rafal ? O_RDWR : O_RDONLY);
202 1.1 soren if (fd < 0) {
203 1.5 rafal printf("Error opening device %s: %s\n",
204 1.5 rafal argv[0], strerror(errno));
205 1.1 soren exit(1);
206 1.1 soren }
207 1.9 jmc #endif
208 1.1 soren }
209 1.2 thorpej if (read(fd, buf, sizeof(buf)) != sizeof(buf)) {
210 1.1 soren perror("read volhdr");
211 1.1 soren exit(1);
212 1.1 soren }
213 1.9 jmc #if HAVE_NBTOOL_CONFIG_H
214 1.9 jmc if (fstat(fd, &st) < 0) {
215 1.9 jmc perror("stat error");
216 1.9 jmc exit(1);
217 1.9 jmc }
218 1.9 jmc if (!S_ISREG(st.st_mode)) {
219 1.9 jmc printf("Must be regular file\n");
220 1.9 jmc exit(1);
221 1.9 jmc }
222 1.9 jmc if (st.st_size % SGI_BOOT_BLOCK_BLOCKSIZE) {
223 1.9 jmc printf("Size must be multiple of %d\n",
224 1.9 jmc SGI_BOOT_BLOCK_BLOCKSIZE);
225 1.9 jmc exit(1);
226 1.9 jmc }
227 1.9 jmc if (st.st_size < (SGIVOL_NBTOOL_NSECS * SGIVOL_NBTOOL_NTRACKS)) {
228 1.9 jmc printf("Minimum size of %d required\n",
229 1.9 jmc SGIVOL_NBTOOL_NSECS * SGIVOL_NBTOOL_NTRACKS);
230 1.9 jmc exit(1);
231 1.9 jmc }
232 1.9 jmc #else
233 1.10 sekiya if (ioctl(fd, DIOCGDINFO, &lbl) < 0) {
234 1.1 soren perror("DIOCGDINFO");
235 1.1 soren exit(1);
236 1.1 soren }
237 1.9 jmc #endif
238 1.9 jmc volhdr = (struct sgi_boot_block *) buf;
239 1.1 soren if (opt_i) {
240 1.1 soren init_volhdr();
241 1.1 soren exit(0);
242 1.1 soren }
243 1.9 jmc if (be32toh(volhdr->magic) != SGI_BOOT_BLOCK_MAGIC) {
244 1.5 rafal printf("No Volume Header found, magic=%x. Use -i first.\n",
245 1.7 sekiya be32toh(volhdr->magic));
246 1.1 soren exit(1);
247 1.1 soren }
248 1.1 soren if (opt_r) {
249 1.1 soren read_file();
250 1.1 soren exit(0);
251 1.1 soren }
252 1.1 soren if (opt_w) {
253 1.1 soren write_file();
254 1.1 soren exit(0);
255 1.1 soren }
256 1.1 soren if (opt_d) {
257 1.1 soren delete_file();
258 1.1 soren exit(0);
259 1.1 soren }
260 1.1 soren if (opt_p) {
261 1.1 soren modify_partition();
262 1.1 soren exit(0);
263 1.1 soren }
264 1.5 rafal
265 1.5 rafal if (!opt_q)
266 1.5 rafal display_vol();
267 1.1 soren
268 1.1 soren return 0;
269 1.1 soren }
270 1.1 soren
271 1.2 thorpej void
272 1.2 thorpej display_vol(void)
273 1.1 soren {
274 1.2 thorpej int32_t *l;
275 1.2 thorpej int i;
276 1.2 thorpej
277 1.9 jmc #if HAVE_NBTOOL_CONFIG_H
278 1.9 jmc printf("disklabel shows %d sectors\n",
279 1.9 jmc st.st_size / SGI_BOOT_BLOCK_BLOCKSIZE);
280 1.9 jmc #else
281 1.1 soren printf("disklabel shows %d sectors\n", lbl.d_secperunit);
282 1.9 jmc #endif
283 1.2 thorpej l = (int32_t *)buf;
284 1.1 soren checksum = 0;
285 1.2 thorpej for (i = 0; i < 512 / 4; ++i)
286 1.7 sekiya checksum += be32toh(l[i]);
287 1.1 soren printf("checksum: %08x%s\n", checksum, checksum == 0 ? "" : " *ERROR*");
288 1.7 sekiya printf("root part: %d\n", be16toh(volhdr->root));
289 1.7 sekiya printf("swap part: %d\n", be16toh(volhdr->swap));
290 1.2 thorpej printf("bootfile: %s\n", volhdr->bootfile);
291 1.1 soren /* volhdr->devparams[0..47] */
292 1.1 soren printf("\nVolume header files:\n");
293 1.2 thorpej for (i = 0; i < 15; ++i)
294 1.2 thorpej if (volhdr->voldir[i].name[0])
295 1.9 jmc printf("%-8s offset %4d blocks, length %8d bytes "
296 1.9 jmc "(%d blocks)\n",
297 1.9 jmc volhdr->voldir[i].name,
298 1.9 jmc be32toh(volhdr->voldir[i].block),
299 1.9 jmc be32toh(volhdr->voldir[i].bytes),
300 1.9 jmc (be32toh(volhdr->voldir[i].bytes) + 511) / 512);
301 1.1 soren printf("\nSGI partitions:\n");
302 1.9 jmc for (i = 0; i < SGI_BOOT_BLOCK_MAXPARTITIONS; ++i) {
303 1.7 sekiya if (be32toh(volhdr->partitions[i].blocks)) {
304 1.2 thorpej printf("%2d:%c blocks %8d first %8d type %2d (%s)\n",
305 1.7 sekiya i, i + 'a', be32toh(volhdr->partitions[i].blocks),
306 1.7 sekiya be32toh(volhdr->partitions[i].first),
307 1.7 sekiya be32toh(volhdr->partitions[i].type),
308 1.7 sekiya be32toh(volhdr->partitions[i].type) > 13 ? "???" :
309 1.7 sekiya sgi_types[be32toh(volhdr->partitions[i].type)]);
310 1.2 thorpej }
311 1.2 thorpej }
312 1.1 soren }
313 1.1 soren
314 1.2 thorpej void
315 1.2 thorpej init_volhdr(void)
316 1.1 soren {
317 1.1 soren memset(buf, 0, sizeof(buf));
318 1.9 jmc volhdr->magic = htobe32(SGI_BOOT_BLOCK_MAGIC);
319 1.7 sekiya volhdr->root = htobe16(0);
320 1.7 sekiya volhdr->swap = htobe16(1);
321 1.2 thorpej strcpy(volhdr->bootfile, "/netbsd");
322 1.9 jmc #if HAVE_NBTOOL_CONFIG_H
323 1.9 jmc volhdr->dp.dp_skew = 0;
324 1.9 jmc #else
325 1.4 simonb volhdr->dp.dp_skew = lbl.d_trackskew;
326 1.9 jmc #endif
327 1.4 simonb volhdr->dp.dp_gap1 = 1; /* XXX */
328 1.4 simonb volhdr->dp.dp_gap2 = 1; /* XXX */
329 1.9 jmc #if HAVE_NBTOOL_CONFIG_H
330 1.9 jmc volhdr->dp.dp_cyls =
331 1.9 jmc htobe16(st.st_size / (SGIVOL_NBTOOL_NSECS * SGIVOL_NBTOOL_NTRACKS));
332 1.9 jmc #else
333 1.7 sekiya volhdr->dp.dp_cyls = htobe16(lbl.d_ncylinders);
334 1.9 jmc #endif
335 1.4 simonb volhdr->dp.dp_shd0 = 0;
336 1.9 jmc #if HAVE_NBTOOL_CONFIG_H
337 1.9 jmc volhdr->dp.dp_trks0 = htobe16(SGIVOL_NBTOOL_NTRACKS);
338 1.9 jmc volhdr->dp.dp_secs = htobe16(SGIVOL_NBTOOL_NSECS);
339 1.9 jmc volhdr->dp.dp_secbytes = htobe16(SGI_BOOT_BLOCK_BLOCKSIZE);
340 1.9 jmc volhdr->dp.dp_interleave = htobe16(1);
341 1.9 jmc #else
342 1.7 sekiya volhdr->dp.dp_trks0 = htobe16(lbl.d_ntracks);
343 1.7 sekiya volhdr->dp.dp_secs = htobe16(lbl.d_nsectors);
344 1.7 sekiya volhdr->dp.dp_secbytes = htobe16(lbl.d_secsize);
345 1.7 sekiya volhdr->dp.dp_interleave = htobe16(lbl.d_interleave);
346 1.9 jmc #endif
347 1.7 sekiya volhdr->dp.dp_nretries = htobe32(22);
348 1.9 jmc #if HAVE_NBTOOL_CONFIG_H
349 1.9 jmc volhdr->partitions[10].blocks =
350 1.9 jmc htobe32(st.st_size / SGI_BOOT_BLOCK_BLOCKSIZE);
351 1.9 jmc #else
352 1.7 sekiya volhdr->partitions[10].blocks = htobe32(lbl.d_secperunit);
353 1.9 jmc #endif
354 1.1 soren volhdr->partitions[10].first = 0;
355 1.7 sekiya volhdr->partitions[10].type = htobe32(SGI_PTYPE_VOLUME);
356 1.7 sekiya volhdr->partitions[8].blocks = htobe32(volhdr_size);
357 1.1 soren volhdr->partitions[8].first = 0;
358 1.7 sekiya volhdr->partitions[8].type = htobe32(SGI_PTYPE_VOLHDR);
359 1.9 jmc #if HAVE_NBTOOL_CONFIG_H
360 1.9 jmc volhdr->partitions[0].blocks =
361 1.9 jmc htobe32((st.st_size / SGI_BOOT_BLOCK_BLOCKSIZE) - volhdr_size);
362 1.9 jmc #else
363 1.7 sekiya volhdr->partitions[0].blocks = htobe32(lbl.d_secperunit - volhdr_size);
364 1.9 jmc #endif
365 1.7 sekiya volhdr->partitions[0].first = htobe32(volhdr_size);
366 1.7 sekiya volhdr->partitions[0].type = htobe32(SGI_PTYPE_BSD);
367 1.1 soren write_volhdr();
368 1.1 soren }
369 1.1 soren
370 1.2 thorpej void
371 1.2 thorpej read_file(void)
372 1.1 soren {
373 1.1 soren FILE *fp;
374 1.2 thorpej int i;
375 1.1 soren
376 1.5 rafal if (!opt_q)
377 1.5 rafal printf("Reading file %s\n", vfilename);
378 1.2 thorpej for (i = 0; i < 15; ++i) {
379 1.3 thorpej if (strncmp(vfilename, volhdr->voldir[i].name,
380 1.7 sekiya strlen(volhdr->voldir[i].name)) == 0)
381 1.1 soren break;
382 1.1 soren }
383 1.2 thorpej if (i >= 15) {
384 1.1 soren printf("file %s not found\n", vfilename);
385 1.1 soren exit(1);
386 1.1 soren }
387 1.1 soren /* XXX assumes volume header starts at 0? */
388 1.7 sekiya lseek(fd, be32toh(volhdr->voldir[i].block) * 512, SEEK_SET);
389 1.1 soren fp = fopen(ufilename, "w");
390 1.1 soren if (fp == NULL) {
391 1.1 soren perror("open write");
392 1.1 soren exit(1);
393 1.1 soren }
394 1.7 sekiya i = be32toh(volhdr->voldir[i].bytes);
395 1.2 thorpej while (i > 0) {
396 1.1 soren if (read(fd, buf, sizeof(buf)) != sizeof(buf)) {
397 1.1 soren perror("read file");
398 1.1 soren exit(1);
399 1.1 soren }
400 1.2 thorpej fwrite(buf, 1, i > sizeof(buf) ? sizeof(buf) : i, fp);
401 1.2 thorpej i -= i > sizeof(buf) ? sizeof(buf) : i;
402 1.1 soren }
403 1.1 soren fclose(fp);
404 1.1 soren }
405 1.1 soren
406 1.2 thorpej void
407 1.2 thorpej write_file(void)
408 1.1 soren {
409 1.1 soren FILE *fp;
410 1.2 thorpej int slot;
411 1.2 thorpej size_t namelen;
412 1.2 thorpej int block, i;
413 1.1 soren struct stat st;
414 1.1 soren char fbuf[512];
415 1.1 soren
416 1.5 rafal if (!opt_q)
417 1.5 rafal printf("Writing file %s\n", ufilename);
418 1.1 soren if (stat(ufilename, &st) < 0) {
419 1.1 soren perror("stat");
420 1.1 soren exit(1);
421 1.1 soren }
422 1.5 rafal if (!opt_q)
423 1.5 rafal printf("File %s has %lld bytes\n", ufilename, st.st_size);
424 1.2 thorpej slot = -1;
425 1.2 thorpej for (i = 0; i < 15; ++i) {
426 1.2 thorpej if (volhdr->voldir[i].name[0] == '\0' && slot < 0)
427 1.2 thorpej slot = i;
428 1.2 thorpej if (strcmp(vfilename, volhdr->voldir[i].name) == 0) {
429 1.2 thorpej slot = i;
430 1.1 soren break;
431 1.1 soren }
432 1.1 soren }
433 1.2 thorpej if (slot == -1) {
434 1.2 thorpej printf("No directory space for file %s\n", vfilename);
435 1.1 soren exit(1);
436 1.1 soren }
437 1.1 soren /* -w can overwrite, -a won't overwrite */
438 1.7 sekiya if (be32toh(volhdr->voldir[slot].block) > 0) {
439 1.5 rafal if (!opt_q)
440 1.5 rafal printf("File %s exists, removing old file\n",
441 1.5 rafal vfilename);
442 1.2 thorpej volhdr->voldir[slot].name[0] = 0;
443 1.2 thorpej volhdr->voldir[slot].block = volhdr->voldir[slot].bytes = 0;
444 1.1 soren }
445 1.1 soren if (st.st_size == 0) {
446 1.5 rafal printf("bad file size\n");
447 1.1 soren exit(1);
448 1.1 soren }
449 1.1 soren /* XXX assumes volume header starts at 0? */
450 1.1 soren block = allocate_space((int)st.st_size);
451 1.1 soren if (block < 0) {
452 1.1 soren printf("No space for file\n");
453 1.1 soren exit(1);
454 1.1 soren }
455 1.1 soren
456 1.2 thorpej /*
457 1.2 thorpej * Make sure the name in the volume header is max. 8 chars,
458 1.2 thorpej * NOT including NUL.
459 1.2 thorpej */
460 1.2 thorpej namelen = strlen(vfilename);
461 1.2 thorpej if (namelen > sizeof(volhdr->voldir[slot].name)) {
462 1.1 soren printf("Warning: '%s' is too long for volume header, ",
463 1.1 soren vfilename);
464 1.2 thorpej namelen = sizeof(volhdr->voldir[slot].name);
465 1.2 thorpej printf("truncating to '%-8s'\n", vfilename);
466 1.1 soren }
467 1.4 simonb
468 1.2 thorpej /* Populate it w/ NULs */
469 1.2 thorpej memset(volhdr->voldir[slot].name, 0,
470 1.2 thorpej sizeof(volhdr->voldir[slot].name));
471 1.2 thorpej /* Then copy the name */
472 1.2 thorpej memcpy(volhdr->voldir[slot].name, vfilename, namelen);
473 1.2 thorpej
474 1.7 sekiya volhdr->voldir[slot].block = htobe32(block);
475 1.7 sekiya volhdr->voldir[slot].bytes = htobe32(st.st_size);
476 1.1 soren
477 1.1 soren write_volhdr();
478 1.1 soren
479 1.1 soren /* write the file itself */
480 1.2 thorpej i = lseek(fd, block * 512, SEEK_SET);
481 1.2 thorpej if (i < 0) {
482 1.1 soren perror("lseek write");
483 1.1 soren exit(1);
484 1.1 soren }
485 1.2 thorpej i = st.st_size;
486 1.1 soren fp = fopen(ufilename, "r");
487 1.2 thorpej while (i > 0) {
488 1.2 thorpej fread(fbuf, 1, i > 512 ? 512 : i, fp);
489 1.1 soren if (write(fd, fbuf, 512) != 512) {
490 1.1 soren perror("write file");
491 1.1 soren exit(1);
492 1.1 soren }
493 1.2 thorpej i -= i > 512 ? 512 : i;
494 1.1 soren }
495 1.1 soren }
496 1.1 soren
497 1.2 thorpej void
498 1.2 thorpej delete_file(void)
499 1.1 soren {
500 1.2 thorpej int i;
501 1.2 thorpej
502 1.2 thorpej for (i = 0; i < 15; ++i) {
503 1.6 sekiya if (strcmp(vfilename, volhdr->voldir[i].name) == 0) {
504 1.1 soren break;
505 1.1 soren }
506 1.1 soren }
507 1.2 thorpej if (i >= 15) {
508 1.1 soren printf("File %s not found\n", vfilename);
509 1.1 soren exit(1);
510 1.1 soren }
511 1.5 rafal
512 1.5 rafal /* XXX: we don't compact the file space, so get fragmentation */
513 1.2 thorpej volhdr->voldir[i].name[0] = '\0';
514 1.2 thorpej volhdr->voldir[i].block = volhdr->voldir[i].bytes = 0;
515 1.1 soren write_volhdr();
516 1.1 soren }
517 1.1 soren
518 1.2 thorpej void
519 1.2 thorpej modify_partition(void)
520 1.1 soren {
521 1.5 rafal if (!opt_q)
522 1.5 rafal printf("Modify partition %d start %d length %d\n",
523 1.5 rafal partno, partfirst, partblocks);
524 1.1 soren if (partno < 0 || partno > 15) {
525 1.5 rafal printf("Invalid partition number: %d\n", partno);
526 1.1 soren exit(1);
527 1.1 soren }
528 1.7 sekiya volhdr->partitions[partno].blocks = htobe32(partblocks);
529 1.7 sekiya volhdr->partitions[partno].first = htobe32(partfirst);
530 1.7 sekiya volhdr->partitions[partno].type = htobe32(parttype);
531 1.1 soren write_volhdr();
532 1.1 soren }
533 1.1 soren
534 1.2 thorpej void
535 1.2 thorpej write_volhdr(void)
536 1.1 soren {
537 1.2 thorpej int i;
538 1.2 thorpej
539 1.1 soren checksum_vol();
540 1.5 rafal
541 1.5 rafal if (!opt_q)
542 1.5 rafal display_vol();
543 1.5 rafal if (!opt_f) {
544 1.5 rafal printf("\nDo you want to update volume (y/n)? ");
545 1.5 rafal i = getchar();
546 1.5 rafal if (i != 'Y' && i != 'y')
547 1.5 rafal exit(1);
548 1.5 rafal }
549 1.7 sekiya i = lseek(fd, 0, SEEK_SET);
550 1.2 thorpej if (i < 0) {
551 1.1 soren perror("lseek 0");
552 1.1 soren exit(1);
553 1.1 soren }
554 1.2 thorpej i = write(fd, buf, 512);
555 1.2 thorpej if (i < 0)
556 1.1 soren perror("write volhdr");
557 1.1 soren }
558 1.1 soren
559 1.2 thorpej int
560 1.2 thorpej allocate_space(int size)
561 1.1 soren {
562 1.1 soren int n, blocks;
563 1.1 soren int first;
564 1.1 soren
565 1.1 soren blocks = (size + 511) / 512;
566 1.1 soren first = 2;
567 1.1 soren n = 0;
568 1.1 soren while (n < 15) {
569 1.1 soren if (volhdr->voldir[n].name[0]) {
570 1.7 sekiya if (first < (be32toh(volhdr->voldir[n].block) +
571 1.7 sekiya (be32toh(volhdr->voldir[n].bytes) + 511) / 512) &&
572 1.7 sekiya (first + blocks) > be32toh(volhdr->voldir[n].block)) {
573 1.7 sekiya first = be32toh(volhdr->voldir[n].block) +
574 1.7 sekiya (be32toh(volhdr->voldir[n].bytes) + 511) / 512;
575 1.1 soren #if 0
576 1.7 sekiya printf("allocate: n=%d first=%d blocks=%d size=%d\n", n, first, blocks, size);
577 1.7 sekiya printf("%s %d %d\n", volhdr->voldir[n].name, volhdr->voldir[n].block, volhdr->voldir[n].bytes);
578 1.7 sekiya printf("first=%d block=%d last=%d end=%d\n", first, volhdr->voldir[n].block,
579 1.7 sekiya first + blocks - 1, volhdr->voldir[n].block + (volhdr->voldir[n].bytes + 511) / 512);
580 1.1 soren #endif
581 1.1 soren n = 0;
582 1.1 soren continue;
583 1.1 soren }
584 1.1 soren }
585 1.1 soren ++n;
586 1.1 soren }
587 1.9 jmc #if HAVE_NBTOOL_CONFIG_H
588 1.9 jmc if (first + blocks > (st.st_size / SGI_BOOT_BLOCK_BLOCKSIZE))
589 1.9 jmc #else
590 1.1 soren if (first + blocks > lbl.d_secperunit)
591 1.9 jmc #endif
592 1.1 soren first = -1;
593 1.1 soren /* XXX assumes volume header is partition 8 */
594 1.1 soren /* XXX assumes volume header starts at 0? */
595 1.7 sekiya if (first + blocks >= be32toh(volhdr->partitions[8].blocks))
596 1.1 soren first = -1;
597 1.7 sekiya return (first);
598 1.1 soren }
599 1.1 soren
600 1.2 thorpej void
601 1.2 thorpej checksum_vol(void)
602 1.1 soren {
603 1.2 thorpej int32_t *l;
604 1.2 thorpej int i;
605 1.2 thorpej
606 1.1 soren volhdr->checksum = checksum = 0;
607 1.2 thorpej l = (int32_t *)buf;
608 1.2 thorpej for (i = 0; i < 512 / 4; ++i)
609 1.7 sekiya checksum += be32toh(l[i]);
610 1.7 sekiya volhdr->checksum = htobe32(-checksum);
611 1.1 soren }
612 1.1 soren
613 1.2 thorpej void
614 1.2 thorpej usage(void)
615 1.1 soren {
616 1.15 rumble printf("Usage: sgivol [-qf] -i [-h vhsize] device\n"
617 1.15 rumble " sgivol [-qf] -r vhfilename diskfilename device\n"
618 1.15 rumble " sgivol [-qf] -w vhfilename diskfilename device\n"
619 1.15 rumble " sgivol [-qf] -d vhfilename device\n"
620 1.15 rumble " sgivol [-qf] -p partno partfirst partblocks "
621 1.15 rumble "parttype device\n"
622 1.1 soren );
623 1.1 soren exit(0);
624 1.1 soren }
625