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