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