devopen.c revision 1.2 1 1.1 nisimura /*-
2 1.1 nisimura * Copyright (c) 2012 The NetBSD Foundation, Inc.
3 1.1 nisimura * All rights reserved.
4 1.1 nisimura *
5 1.1 nisimura * This code is derived from software contributed to The NetBSD Foundation
6 1.1 nisimura * by Paul Fleischer <paul (at) xpg.dk>
7 1.1 nisimura *
8 1.1 nisimura * Redistribution and use in source and binary forms, with or without
9 1.1 nisimura * modification, are permitted provided that the following conditions
10 1.1 nisimura * are met:
11 1.1 nisimura * 1. Redistributions of source code must retain the above copyright
12 1.1 nisimura * notice, this list of conditions and the following disclaimer.
13 1.1 nisimura * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 nisimura * notice, this list of conditions and the following disclaimer in the
15 1.1 nisimura * documentation and/or other materials provided with the distribution.
16 1.1 nisimura *
17 1.1 nisimura * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 1.1 nisimura * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 1.1 nisimura * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 1.1 nisimura * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 1.1 nisimura * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 1.1 nisimura * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 1.1 nisimura * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 1.1 nisimura * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 1.1 nisimura * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 1.1 nisimura * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 1.1 nisimura * POSSIBILITY OF SUCH DAMAGE.
28 1.1 nisimura */
29 1.1 nisimura #include <sys/param.h>
30 1.1 nisimura #include <sys/disklabel.h>
31 1.1 nisimura
32 1.1 nisimura #include <netinet/in.h>
33 1.1 nisimura
34 1.1 nisimura #include <lib/libkern/libkern.h>
35 1.1 nisimura #include <lib/libsa/stand.h>
36 1.1 nisimura #include <lib/libsa/nfs.h>
37 1.2 nisimura #include <lib/libsa/tftp.h>
38 1.1 nisimura #include <lib/libsa/ext2fs.h>
39 1.1 nisimura #include <lib/libsa/dosfs.h>
40 1.1 nisimura #include <lib/libsa/ufs.h>
41 1.1 nisimura
42 1.1 nisimura #include "dev_sdmmc.h"
43 1.1 nisimura #include <arch/evbarm/mini2440/mini2440_bootinfo.h>
44 1.1 nisimura
45 1.1 nisimura /* Implemented in dev_net.c */
46 1.1 nisimura int net_open(struct open_file *, ...);
47 1.1 nisimura int net_close(struct open_file *);
48 1.1 nisimura int net_strategy(void *, int, daddr_t, size_t, void *, size_t *);
49 1.1 nisimura
50 1.1 nisimura /* Implemented in dev_sdmmc.c */
51 1.1 nisimura int sdmmc_open(struct open_file *, ...);
52 1.1 nisimura int sdmmc_close(struct open_file *);
53 1.1 nisimura int sdmmc_strategy(void *, int, daddr_t, size_t, void *, size_t *);
54 1.1 nisimura int sdmmc_get_fstype(void*);
55 1.1 nisimura
56 1.1 nisimura struct devsw devsw[] = {
57 1.1 nisimura { "dm9000", net_strategy, net_open, net_close, noioctl },
58 1.1 nisimura { "s3c sdio", sdmmc_strategy, sdmmc_open, sdmmc_close, noioctl }
59 1.1 nisimura };
60 1.1 nisimura
61 1.1 nisimura struct fs_ops ops_nfs = FS_OPS(nfs);
62 1.2 nisimura struct fs_ops ops_tftp = FS_OPS(tftp);
63 1.1 nisimura struct fs_ops ops_ext2fs = FS_OPS(ext2fs);
64 1.1 nisimura struct fs_ops ops_dosfs = FS_OPS(dosfs);
65 1.1 nisimura struct fs_ops ops_bsdfs = FS_OPS(ufs);
66 1.1 nisimura struct fs_ops file_system[1];
67 1.1 nisimura int nfsys = 1;
68 1.1 nisimura
69 1.1 nisimura extern struct btinfo_bootpath bi_path;
70 1.1 nisimura extern struct btinfo_rootdevice bi_rdev;
71 1.1 nisimura
72 1.1 nisimura static void parseunit(const char *name, int *unitp, int *partp, char **pathp);
73 1.1 nisimura
74 1.1 nisimura int
75 1.1 nisimura devopen(struct open_file *of, const char *name, char **file)
76 1.1 nisimura {
77 1.1 nisimura int error;
78 1.1 nisimura extern char bootfile[];
79 1.1 nisimura
80 1.1 nisimura if (strncmp("net:", name, 4) == 0 ||
81 1.1 nisimura strncmp("nfs:", name, 4) == 0 ) {
82 1.1 nisimura of->f_dev = &devsw[0];
83 1.2 nisimura if ((error = net_open(of, name+4, "nfs")) != 0)
84 1.1 nisimura return error;
85 1.1 nisimura file_system[0] = ops_nfs;
86 1.1 nisimura *file = bootfile;
87 1.1 nisimura strncpy(bi_path.bootpath, bootfile, sizeof(bi_path.bootpath));
88 1.1 nisimura
89 1.1 nisimura return 0;
90 1.2 nisimura } else if (strncmp("tftp:", name, 5) == 0) {
91 1.2 nisimura of->f_dev = &devsw[0];
92 1.2 nisimura if ((error = net_open(of, name+5, "tftp")) != 0) {
93 1.2 nisimura return error;
94 1.2 nisimura }
95 1.2 nisimura file_system[0] = ops_tftp;
96 1.2 nisimura *file = bootfile;
97 1.2 nisimura strncpy(bi_path.bootpath, bootfile, sizeof(bi_path.bootpath));
98 1.2 nisimura
99 1.2 nisimura return 0;
100 1.1 nisimura } else if (name[0] == 'l' && name[1] == 'd') {
101 1.1 nisimura int unit, part;
102 1.1 nisimura parseunit(&name[2], &unit, &part, file);
103 1.2 nisimura if (*file == NULL || strlen(*file) == 0) {
104 1.2 nisimura strcpy(*file, "netbsd");
105 1.2 nisimura }
106 1.1 nisimura of->f_dev = &devsw[1];
107 1.1 nisimura if ((error = sdmmc_open(of, unit, part)) != 0)
108 1.1 nisimura return error;
109 1.1 nisimura
110 1.1 nisimura strncpy(bi_path.bootpath, *file, sizeof(bi_path.bootpath));
111 1.1 nisimura
112 1.1 nisimura snprintf(bi_rdev.devname, sizeof(bi_rdev.devname), "ld");
113 1.1 nisimura bi_rdev.cookie = unit;
114 1.1 nisimura bi_rdev.partition = part;
115 1.1 nisimura switch(sdmmc_get_fstype(of->f_devdata)) {
116 1.1 nisimura case FS_EX2FS:
117 1.1 nisimura file_system[0] = ops_ext2fs;
118 1.1 nisimura break;
119 1.1 nisimura case FS_MSDOS:
120 1.1 nisimura file_system[0] = ops_dosfs;
121 1.1 nisimura break;
122 1.1 nisimura case FS_BSDFFS:
123 1.1 nisimura file_system[0] = ops_bsdfs;
124 1.1 nisimura break;
125 1.1 nisimura default:
126 1.1 nisimura return ENOENT;
127 1.1 nisimura }
128 1.1 nisimura
129 1.1 nisimura return 0;
130 1.1 nisimura }
131 1.1 nisimura return ENOENT;
132 1.1 nisimura }
133 1.1 nisimura
134 1.1 nisimura static void
135 1.1 nisimura parseunit(const char *name, int *unitp, int *partp, char **pathp)
136 1.1 nisimura {
137 1.1 nisimura const char *p = name;
138 1.1 nisimura int unit, part;
139 1.1 nisimura
140 1.1 nisimura unit = part = -1;
141 1.1 nisimura while (*p != ':' && *p != '\0') {
142 1.1 nisimura if (unit == -1 && *p >= '0' && *p <= '9')
143 1.1 nisimura unit = *p - '0';
144 1.1 nisimura if (part == -1 && *p >= 'a' && *p < 'a' + 16)
145 1.1 nisimura part = *p - 'a';
146 1.1 nisimura p += 1;
147 1.1 nisimura }
148 1.1 nisimura *unitp = (unit == -1) ? 0 : unit;
149 1.1 nisimura *partp = (part == -1) ? 0 : part;
150 1.1 nisimura *pathp = (*p == ':') ? (char *)p + 1 : NULL;
151 1.1 nisimura }
152