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@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/tftp.h>
38 #include <lib/libsa/ext2fs.h>
39 #include <lib/libsa/dosfs.h>
40 #include <lib/libsa/ufs.h>
41 
42 #include "dev_sdmmc.h"
43 #include <arch/evbarm/mini2440/mini2440_bootinfo.h>
44 
45 /* Implemented in dev_net.c */
46 int net_open(struct open_file *, ...);
47 int net_close(struct open_file *);
48 int net_strategy(void *, int, daddr_t, size_t, void *, size_t *);
49 
50 /* Implemented in dev_sdmmc.c */
51 int sdmmc_open(struct open_file *, ...);
52 int sdmmc_close(struct open_file *);
53 int sdmmc_strategy(void *, int, daddr_t, size_t, void *, size_t *);
54 int sdmmc_get_fstype(void*);
55 
56 struct devsw devsw[] = {
57           { "dm9000", net_strategy, net_open, net_close, noioctl },
58           { "s3c sdio", sdmmc_strategy, sdmmc_open, sdmmc_close, noioctl }
59 };
60 
61 struct fs_ops ops_nfs = FS_OPS(nfs);
62 struct fs_ops ops_tftp = FS_OPS(tftp);
63 struct fs_ops ops_ext2fs = FS_OPS(ext2fs);
64 struct fs_ops ops_dosfs = FS_OPS(dosfs);
65 struct fs_ops ops_bsdfs = FS_OPS(ufs);
66 struct fs_ops file_system[1];
67 int nfsys = 1;
68 
69 extern struct btinfo_bootpath           bi_path;
70 extern struct btinfo_rootdevice                   bi_rdev;
71 
72 static void parseunit(const char *name, int *unitp, int *partp, char **pathp);
73 
74 int
devopen(struct open_file * of,const char * name,char ** file)75 devopen(struct open_file *of, const char *name, char **file)
76 {
77           int error;
78           extern char bootfile[];
79 
80           if (strncmp("net:", name, 4) == 0 ||
81               strncmp("nfs:", name, 4) == 0 ) {
82                     of->f_dev = &devsw[0];
83                     if ((error = net_open(of, name+4, "nfs")) != 0)
84                               return error;
85                     file_system[0] = ops_nfs;
86                     *file = bootfile;
87                     strncpy(bi_path.bootpath, bootfile, sizeof(bi_path.bootpath));
88 
89                     return 0;
90           } else if (strncmp("tftp:", name, 5) == 0) {
91                     of->f_dev = &devsw[0];
92                     if ((error = net_open(of, name+5, "tftp")) != 0) {
93                               return error;
94                     }
95                     file_system[0] = ops_tftp;
96                     *file = bootfile;
97                     strncpy(bi_path.bootpath, bootfile, sizeof(bi_path.bootpath));
98 
99                     return 0;
100           } else if (name[0] == 'l' && name[1] == 'd') {
101                     int unit, part;
102                     parseunit(&name[2], &unit, &part, file);
103                     if (*file == NULL || strlen(*file) == 0) {
104                               strcpy(*file, "netbsd");
105                     }
106                     of->f_dev = &devsw[1];
107                     if ((error = sdmmc_open(of, unit, part)) != 0)
108                               return error;
109 
110                     strncpy(bi_path.bootpath, *file, sizeof(bi_path.bootpath));
111 
112                     snprintf(bi_rdev.devname, sizeof(bi_rdev.devname), "ld");
113                     bi_rdev.cookie = unit;
114                     bi_rdev.partition = part;
115                     switch(sdmmc_get_fstype(of->f_devdata)) {
116                     case FS_EX2FS:
117                               file_system[0] = ops_ext2fs;
118                               break;
119                     case FS_MSDOS:
120                               file_system[0] = ops_dosfs;
121                               break;
122                     case FS_BSDFFS:
123                               file_system[0] = ops_bsdfs;
124                               break;
125                     default:
126                               return ENOENT;
127                     }
128 
129                     return 0;
130           }
131           return ENOENT;
132 }
133 
134 static void
parseunit(const char * name,int * unitp,int * partp,char ** pathp)135 parseunit(const char *name, int *unitp, int *partp, char **pathp)
136 {
137           const char *p = name;
138           int unit, part;
139 
140           unit = part = -1;
141           while (*p != ':' && *p != '\0') {
142                     if (unit == -1 && *p >= '0' && *p <= '9')
143                               unit = *p - '0';
144                     if (part == -1 && *p >= 'a' && *p < 'a' + 16)
145                               part = *p - 'a';
146                     p += 1;
147           }
148           *unitp = (unit == -1) ? 0 : unit;
149           *partp = (part == -1) ? 0 : part;
150           *pathp = (*p == ':') ? (char *)p + 1 : NULL;
151 }
152