Home | History | Annotate | Line # | Download | only in common
devopen.c revision 1.2.22.1
      1  1.2.22.1     matt /*	$NetBSD: devopen.c,v 1.2.22.1 2007/11/06 23:16:33 matt 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 <netinet/in.h>
     43       1.1  tsutsui #include <lib/libsa/dev_net.h>
     44       1.1  tsutsui #include <lib/libsa/ufs.h>
     45       1.1  tsutsui #include <lib/libsa/nfs.h>
     46  1.2.22.1     matt #include <lib/libsa/dev_net.h>
     47       1.1  tsutsui #include <machine/sbd.h>
     48       1.1  tsutsui 
     49       1.1  tsutsui #include "local.h"
     50       1.1  tsutsui 
     51       1.1  tsutsui extern uint8_t kernel_binary[];
     52       1.1  tsutsui extern int kernel_binary_size;
     53       1.1  tsutsui 
     54       1.1  tsutsui extern struct fs_ops datafs_ops;
     55       1.1  tsutsui extern struct fs_ops bfs_ops;
     56       1.1  tsutsui struct fs_ops ufs_ops = {
     57       1.1  tsutsui 	ufs_open, ufs_close, ufs_read, ufs_write, ufs_seek, ufs_stat
     58       1.1  tsutsui };
     59       1.1  tsutsui struct fs_ops nfs_ops = {
     60       1.1  tsutsui 	nfs_open, nfs_close, nfs_read, nfs_write, nfs_seek, nfs_stat
     61       1.1  tsutsui };
     62       1.1  tsutsui 
     63       1.1  tsutsui extern struct devsw netdevsw;
     64       1.1  tsutsui extern struct devsw dkdevsw;
     65       1.1  tsutsui char fname[16];
     66       1.1  tsutsui 
     67       1.1  tsutsui /* Referenced by libsa/open.c */
     68       1.1  tsutsui struct fs_ops file_system[1];
     69       1.1  tsutsui int nfsys = 1;
     70       1.1  tsutsui struct devsw devsw[1];
     71       1.1  tsutsui int ndevs = 1;
     72       1.1  tsutsui 
     73       1.1  tsutsui int
     74       1.1  tsutsui devopen(struct open_file *f, const char *request, char **file)
     75       1.1  tsutsui {
     76       1.1  tsutsui 	char *p, *filename;
     77       1.1  tsutsui 	int disk, partition;
     78       1.1  tsutsui 	void *addr;
     79       1.1  tsutsui 	size_t size;
     80       1.1  tsutsui 
     81       1.1  tsutsui 	strcpy(fname, request);
     82       1.1  tsutsui 
     83       1.1  tsutsui 	filename = 0;
     84       1.1  tsutsui 	for (p = fname; *p; p++) {
     85       1.1  tsutsui 		if (*p == ':') {
     86       1.1  tsutsui 			filename = p + 1;
     87       1.1  tsutsui 			*p = '\0';
     88       1.1  tsutsui 			break;
     89       1.1  tsutsui 		}
     90       1.1  tsutsui 	}
     91       1.1  tsutsui 
     92       1.1  tsutsui 	if (filename == 0) {	/* not a loader's request. probably ufs_ls() */
     93       1.1  tsutsui 		printf("request=%s\n", request);
     94       1.1  tsutsui 		f->f_dev = &dkdevsw;
     95       1.1  tsutsui 		file_system[0] = ufs_ops;
     96       1.1  tsutsui 		devsw[0] = dkdevsw;
     97       1.1  tsutsui 		*file = "/";
     98       1.1  tsutsui 		return 0;
     99       1.1  tsutsui 	}
    100       1.1  tsutsui 
    101       1.1  tsutsui 	/* Data section */
    102       1.1  tsutsui 	if (strcmp(fname, "mem") == 0) {
    103       1.1  tsutsui 		data_attach(kernel_binary, kernel_binary_size);
    104       1.1  tsutsui 		*file = "noname";
    105       1.1  tsutsui 		f->f_flags |= F_NODEV;
    106       1.1  tsutsui 		file_system[0] = datafs_ops;
    107       1.1  tsutsui 		printf("data(compiled):noname\n");
    108       1.1  tsutsui 		return 0;
    109       1.1  tsutsui 	}
    110       1.1  tsutsui 
    111       1.1  tsutsui 	/* NFS boot */
    112       1.1  tsutsui 	if (strcmp(fname, "nfs") == 0) {
    113       1.1  tsutsui 		if (!DEVICE_CAPABILITY.network_enabled) {
    114       1.1  tsutsui 			printf("Network disabled.\n");
    115       1.1  tsutsui 			return -1;
    116       1.1  tsutsui 		}
    117       1.2  thorpej 		try_bootp = true;
    118       1.1  tsutsui 		file_system[0] = nfs_ops;
    119       1.1  tsutsui 		f->f_dev = &netdevsw;
    120       1.1  tsutsui 		if (*filename == '\0') {
    121       1.1  tsutsui 			printf("set kernel filename. ex.) nfs:netbsd\n");
    122       1.1  tsutsui 			return 1;
    123       1.1  tsutsui 		}
    124       1.1  tsutsui 		*--filename = '/';
    125       1.1  tsutsui 		*file = filename;
    126       1.1  tsutsui 		printf("nfs:/%s\n", filename);
    127       1.1  tsutsui 		net_open(f);
    128       1.1  tsutsui 		return 0;
    129       1.1  tsutsui 	}
    130       1.1  tsutsui 
    131       1.1  tsutsui 	/* FD boot */
    132       1.1  tsutsui 	if (strcmp(fname, "fd") == 0) {
    133       1.1  tsutsui 		printf("floppy(boot):/%s (ustarfs)\n", filename);
    134       1.1  tsutsui 		f->f_dev = &dkdevsw;
    135       1.1  tsutsui 		file_system[0] = datafs_ops;
    136       1.1  tsutsui 		devsw[0] = dkdevsw;
    137       1.1  tsutsui 		device_attach(NVSRAM_BOOTDEV_FLOPPYDISK, -1, -1);
    138       1.1  tsutsui 		if (!ustarfs_load(filename, &addr, &size))
    139       1.1  tsutsui 			return -1;
    140       1.1  tsutsui 		data_attach(addr, size);
    141       1.1  tsutsui 		*file = filename;
    142       1.1  tsutsui 		return 0;
    143       1.1  tsutsui 	}
    144       1.1  tsutsui 
    145       1.1  tsutsui 	/* Disk boot */
    146       1.1  tsutsui 	if (strncmp(fname, "sd", 2) == 0) {
    147       1.1  tsutsui 		enum fstype fs;
    148       1.1  tsutsui 		if (!DEVICE_CAPABILITY.disk_enabled) {
    149       1.1  tsutsui 			printf("Disk disabled.\n");
    150       1.1  tsutsui 			return -1;
    151       1.1  tsutsui 		}
    152       1.1  tsutsui 
    153       1.1  tsutsui 		disk = fname[2] - '0';
    154       1.1  tsutsui 		partition = fname[3] - 'a';
    155       1.1  tsutsui 		if (disk < 0 || disk > 9 || partition < 0 || partition > 15) {
    156       1.1  tsutsui 			fs = FSTYPE_USTARFS;
    157       1.1  tsutsui 			printf("disk(boot):%s ", filename);
    158       1.1  tsutsui 			device_attach(NVSRAM_BOOTDEV_HARDDISK, -1, -1);
    159       1.1  tsutsui 		} else {
    160       1.1  tsutsui 			fs = fstype(partition);
    161       1.1  tsutsui 			printf("disk(%d,%d):/%s ",
    162       1.1  tsutsui 			    disk, partition, filename);
    163       1.1  tsutsui 			device_attach(NVSRAM_BOOTDEV_HARDDISK, disk, partition);
    164       1.1  tsutsui 		}
    165       1.1  tsutsui 
    166       1.1  tsutsui 		switch (fs) {
    167       1.1  tsutsui 		case FSTYPE_UFS:
    168       1.1  tsutsui 			printf(" (ufs)\n");
    169       1.1  tsutsui 			f->f_dev = &dkdevsw;
    170       1.1  tsutsui 			file_system[0] = ufs_ops;
    171       1.1  tsutsui 			devsw[0] = dkdevsw;
    172       1.1  tsutsui 			break;
    173       1.1  tsutsui 		case FSTYPE_BFS:
    174       1.1  tsutsui 			printf(" (bfs)\n");
    175       1.1  tsutsui 			f->f_flags |= F_NODEV;
    176       1.1  tsutsui 			file_system[0] = bfs_ops;
    177       1.1  tsutsui 			break;
    178       1.1  tsutsui 		case FSTYPE_USTARFS:
    179       1.1  tsutsui 			printf(" (ustarfs)\n");
    180       1.1  tsutsui 			f->f_dev = &dkdevsw;
    181       1.1  tsutsui 			file_system[0] = datafs_ops;
    182       1.1  tsutsui 			devsw[0] = dkdevsw;
    183       1.1  tsutsui 			if (!ustarfs_load(filename, &addr, &size))
    184       1.1  tsutsui 				return -1;
    185       1.1  tsutsui 			data_attach(addr, size);
    186       1.1  tsutsui 			break;
    187       1.1  tsutsui 		}
    188       1.1  tsutsui 		*file = filename;
    189       1.1  tsutsui 		return 0;
    190       1.1  tsutsui 	}
    191       1.1  tsutsui 
    192       1.1  tsutsui 	printf("%s invalid.\n", fname);
    193       1.1  tsutsui 
    194       1.1  tsutsui 	return -1;
    195       1.1  tsutsui }
    196