Home | History | Annotate | Line # | Download | only in common
ustarfs.c revision 1.6.10.1
      1  1.6.10.1      yamt /*	$NetBSD: ustarfs.c,v 1.6.10.1 2008/05/16 02:22:20 yamt 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  *
     19       1.1   tsutsui  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20       1.1   tsutsui  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.1   tsutsui  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22       1.1   tsutsui  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23       1.1   tsutsui  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.1   tsutsui  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.1   tsutsui  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.1   tsutsui  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.1   tsutsui  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.1   tsutsui  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.1   tsutsui  * POSSIBILITY OF SUCH DAMAGE.
     30       1.1   tsutsui  */
     31       1.1   tsutsui 
     32       1.1   tsutsui #include <lib/libsa/stand.h>
     33       1.1   tsutsui #include <lib/libkern/libkern.h>
     34       1.1   tsutsui 
     35       1.1   tsutsui #include <machine/param.h>
     36       1.1   tsutsui #include <machine/sbd.h>
     37       1.1   tsutsui #include <machine/sector.h>
     38       1.1   tsutsui 
     39       1.1   tsutsui #include "local.h"
     40       1.1   tsutsui #include "common.h"
     41       1.1   tsutsui 
     42       1.4   thorpej bool __ustarfs_file(int, char *, size_t *);
     43       1.4   thorpej bool __block_read(uint8_t *, int);
     44       1.4   thorpej bool __block_read_n(uint8_t *, int, int);
     45       1.1   tsutsui void __change_volume(int);
     46       1.1   tsutsui 
     47       1.1   tsutsui enum { USTAR_BLOCK_SIZE = 8192 };/* Check src/distrib/common/buildfloppies.sh */
     48       1.1   tsutsui struct volume {
     49       1.1   tsutsui 	int max_block;
     50       1.1   tsutsui 	int current_volume;
     51       1.1   tsutsui 	int block_offset;
     52       1.1   tsutsui } __volume;
     53       1.1   tsutsui 
     54       1.4   thorpej bool
     55       1.1   tsutsui ustarfs_load(const char *file, void **addrp, size_t *sizep)
     56       1.1   tsutsui {
     57       1.1   tsutsui 	char fname[16];
     58       1.1   tsutsui 	int block = 16;
     59       1.1   tsutsui 	size_t sz;
     60       1.1   tsutsui 	int maxblk;
     61       1.1   tsutsui 
     62       1.1   tsutsui 	if (DEVICE_CAPABILITY.active_device == NVSRAM_BOOTDEV_HARDDISK)
     63       1.1   tsutsui 		maxblk = 0x1fffffff;	/* no limit */
     64       1.1   tsutsui 	else if (DEVICE_CAPABILITY.active_device == NVSRAM_BOOTDEV_FLOPPYDISK)
     65       1.1   tsutsui 		/*
     66       1.1   tsutsui 		 * Although phisical format isn't 2D, volume size is
     67       1.1   tsutsui 		 * limited to size of 2D.
     68       1.1   tsutsui 		 */
     69       1.1   tsutsui 		maxblk = (77 + 76) * 13;
     70       1.1   tsutsui 	else {
     71       1.1   tsutsui 		printf("not supported device.\n");
     72       1.5   thorpej 		return false;
     73       1.1   tsutsui 	}
     74       1.1   tsutsui 
     75       1.1   tsutsui 	/* Truncate to ustar block boundary */
     76       1.1   tsutsui 	__volume.max_block = (maxblk / (USTAR_BLOCK_SIZE >> DEV_BSHIFT)) *
     77       1.1   tsutsui 	    (USTAR_BLOCK_SIZE >> DEV_BSHIFT);
     78       1.1   tsutsui 	__volume.block_offset = 0;
     79       1.1   tsutsui 	__volume.current_volume = 0;
     80       1.1   tsutsui 
     81       1.1   tsutsui 	/* Find file */
     82       1.1   tsutsui 	while (/*CONSTCOND*/1) {
     83       1.1   tsutsui 		if (!__ustarfs_file(block, fname, &sz))
     84       1.5   thorpej 			return false;
     85       1.1   tsutsui 
     86       1.1   tsutsui 		if (strcmp(file, fname) == 0)
     87       1.1   tsutsui 			break;
     88       1.1   tsutsui 		block += (ROUND_SECTOR(sz) >> DEV_BSHIFT) + 1;
     89       1.1   tsutsui 	}
     90       1.1   tsutsui 	block++;	/* skip tar header */
     91       1.1   tsutsui 	*sizep = sz;
     92       1.1   tsutsui 
     93       1.1   tsutsui 	/* Load file */
     94       1.1   tsutsui 	sz = ROUND_SECTOR(sz);
     95       1.1   tsutsui 	if ((*addrp = alloc(sz)) == 0) {
     96       1.6     perry 		printf("%s: can't allocate memory.\n", __func__);
     97       1.5   thorpej 		return false;
     98       1.1   tsutsui 	}
     99       1.1   tsutsui 
    100       1.1   tsutsui 	if (!__block_read_n(*addrp, block, sz >> DEV_BSHIFT)) {
    101       1.6     perry 		printf("%s: can't load file.\n", __func__);
    102       1.2  christos 		dealloc(*addrp, sz);
    103       1.5   thorpej 		return false;
    104       1.1   tsutsui 	}
    105       1.1   tsutsui 
    106       1.5   thorpej 	return true;
    107       1.1   tsutsui }
    108       1.1   tsutsui 
    109       1.4   thorpej bool
    110       1.1   tsutsui __ustarfs_file(int start_block, char *file, size_t *size)
    111       1.1   tsutsui {
    112       1.1   tsutsui 	uint8_t buf[512];
    113       1.1   tsutsui 
    114       1.1   tsutsui 	if (!__block_read(buf, start_block)) {
    115       1.1   tsutsui 		printf("can't read tar header.\n");
    116       1.5   thorpej 		return false;
    117       1.1   tsutsui 	}
    118       1.1   tsutsui 	if (((*(uint32_t *)(buf + 256)) & 0xffffff) != 0x757374) {
    119       1.1   tsutsui 		printf("bad tar magic.\n");
    120       1.5   thorpej 		return false;
    121       1.1   tsutsui 	}
    122       1.3   tsutsui 	*size = strtoul((char *)buf + 124, 0, 0);
    123       1.3   tsutsui 	strncpy(file, (char *)buf, 16);
    124       1.1   tsutsui 
    125       1.5   thorpej 	return true;
    126       1.1   tsutsui }
    127       1.1   tsutsui 
    128       1.4   thorpej bool
    129       1.1   tsutsui __block_read_n(uint8_t *buf, int blk, int count)
    130       1.1   tsutsui {
    131       1.1   tsutsui 	int i;
    132       1.1   tsutsui 
    133       1.1   tsutsui 	for (i = 0; i < count; i++, buf += DEV_BSIZE)
    134       1.1   tsutsui 		if (!__block_read(buf, blk + i))
    135       1.5   thorpej 			return false;
    136       1.1   tsutsui 
    137       1.5   thorpej 	return true;
    138       1.1   tsutsui }
    139       1.1   tsutsui 
    140       1.4   thorpej bool
    141       1.1   tsutsui __block_read(uint8_t *buf, int blk)
    142       1.1   tsutsui {
    143       1.1   tsutsui 	int vol;
    144       1.1   tsutsui 
    145       1.1   tsutsui 	if ((vol = blk / __volume.max_block) != __volume.current_volume) {
    146       1.1   tsutsui 		__change_volume(vol);
    147       1.1   tsutsui 		__volume.current_volume = vol;
    148       1.1   tsutsui 		/* volume offset + ustarfs header (8k) */
    149       1.1   tsutsui 		__volume.block_offset = vol * __volume.max_block - 16;
    150       1.1   tsutsui 	}
    151       1.1   tsutsui 
    152       1.1   tsutsui 	return sector_read(0, buf, blk - __volume.block_offset);
    153       1.1   tsutsui }
    154       1.1   tsutsui 
    155       1.1   tsutsui void
    156       1.1   tsutsui __change_volume(int volume)
    157       1.1   tsutsui {
    158       1.1   tsutsui 	uint8_t buf[512];
    159       1.1   tsutsui 	int i;
    160       1.1   tsutsui 
    161       1.1   tsutsui 	while (/*CONSTCOND*/1) {
    162       1.1   tsutsui 		printf("insert disk %d, and press return...", volume + 1);
    163       1.1   tsutsui 		while (getchar() != '\r')
    164       1.1   tsutsui 			;
    165       1.1   tsutsui 		printf("\n");
    166       1.1   tsutsui 		if (!sector_read(0, buf, 0))
    167       1.1   tsutsui 			continue;
    168       1.1   tsutsui 		if (*(uint32_t *)buf != 0x55535441) {	/* "USTAR" */
    169       1.1   tsutsui 			printf("invalid magic.\n");
    170       1.1   tsutsui 			continue;
    171       1.1   tsutsui 		}
    172       1.1   tsutsui 		if ((i = buf[8] - '0') != volume + 1) {
    173       1.1   tsutsui 			printf("invalid volume number. disk=%d requested=%d\n",
    174       1.1   tsutsui 			    i, volume + 1);
    175       1.1   tsutsui 			continue;
    176       1.1   tsutsui 		}
    177       1.1   tsutsui 		return;
    178       1.1   tsutsui 	}
    179       1.1   tsutsui }
    180