raw.c revision 1.1.8.2 1 1.1.8.2 jruoho /* $NetBSD: raw.c,v 1.1.8.2 2011/06/06 09:05:21 jruoho Exp $ */
2 1.1.8.2 jruoho
3 1.1.8.2 jruoho /*-
4 1.1.8.2 jruoho * Copyright (c) 2010 The NetBSD Foundation, Inc.
5 1.1.8.2 jruoho * All rights reserved.
6 1.1.8.2 jruoho *
7 1.1.8.2 jruoho * This code was written by Alessandro Forin and Neil Pittman
8 1.1.8.2 jruoho * at Microsoft Research and contributed to The NetBSD Foundation
9 1.1.8.2 jruoho * by Microsoft Corporation.
10 1.1.8.2 jruoho *
11 1.1.8.2 jruoho * Redistribution and use in source and binary forms, with or without
12 1.1.8.2 jruoho * modification, are permitted provided that the following conditions
13 1.1.8.2 jruoho * are met:
14 1.1.8.2 jruoho * 1. Redistributions of source code must retain the above copyright
15 1.1.8.2 jruoho * notice, this list of conditions and the following disclaimer.
16 1.1.8.2 jruoho * 2. Redistributions in binary form must reproduce the above copyright
17 1.1.8.2 jruoho * notice, this list of conditions and the following disclaimer in the
18 1.1.8.2 jruoho * documentation and/or other materials provided with the distribution.
19 1.1.8.2 jruoho *
20 1.1.8.2 jruoho * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 1.1.8.2 jruoho * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 1.1.8.2 jruoho * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 1.1.8.2 jruoho * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 1.1.8.2 jruoho * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 1.1.8.2 jruoho * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 1.1.8.2 jruoho * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 1.1.8.2 jruoho * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 1.1.8.2 jruoho * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 1.1.8.2 jruoho * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 1.1.8.2 jruoho * POSSIBILITY OF SUCH DAMAGE.
31 1.1.8.2 jruoho */
32 1.1.8.2 jruoho
33 1.1.8.2 jruoho #include <lib/libsa/stand.h>
34 1.1.8.2 jruoho #include <lib/libkern/libkern.h>
35 1.1.8.2 jruoho #include <machine/stdarg.h>
36 1.1.8.2 jruoho #include <machine/emipsreg.h>
37 1.1.8.2 jruoho
38 1.1.8.2 jruoho #include <sys/param.h>
39 1.1.8.2 jruoho #include <sys/disklabel.h>
40 1.1.8.2 jruoho #include <sys/endian.h>
41 1.1.8.2 jruoho
42 1.1.8.2 jruoho #include "common.h"
43 1.1.8.2 jruoho #include "raw.h"
44 1.1.8.2 jruoho #include "ace.h"
45 1.1.8.2 jruoho
46 1.1.8.2 jruoho /* Used to parse strings of the form "a0/99/badface" all hex
47 1.1.8.2 jruoho */
48 1.1.8.2 jruoho
49 1.1.8.2 jruoho static inline int hexnum(char c)
50 1.1.8.2 jruoho {
51 1.1.8.2 jruoho if (c >= '0' && c <= '9')
52 1.1.8.2 jruoho return c - '0';
53 1.1.8.2 jruoho if (c >= 'a' && c <= 'f')
54 1.1.8.2 jruoho return c - 'a';
55 1.1.8.2 jruoho if (c >= 'A' && c <= 'F')
56 1.1.8.2 jruoho return c - 'A';
57 1.1.8.2 jruoho return -1;
58 1.1.8.2 jruoho }
59 1.1.8.2 jruoho
60 1.1.8.2 jruoho static char *getxnum(char *s, uint32_t *dest)
61 1.1.8.2 jruoho {
62 1.1.8.2 jruoho uint32_t u = 0;
63 1.1.8.2 jruoho char c;
64 1.1.8.2 jruoho int v = -1;
65 1.1.8.2 jruoho
66 1.1.8.2 jruoho if ((c = *s++) == '/')
67 1.1.8.2 jruoho c = *s++;
68 1.1.8.2 jruoho while (c && c != '/') {
69 1.1.8.2 jruoho v = hexnum(c);
70 1.1.8.2 jruoho if (v < 0)
71 1.1.8.2 jruoho break;
72 1.1.8.2 jruoho u = (u << 4) + v;
73 1.1.8.2 jruoho c = *s++;
74 1.1.8.2 jruoho }
75 1.1.8.2 jruoho /* did we get any */
76 1.1.8.2 jruoho if (v < 0)
77 1.1.8.2 jruoho return NULL;
78 1.1.8.2 jruoho *dest = u;
79 1.1.8.2 jruoho return s-1;
80 1.1.8.2 jruoho }
81 1.1.8.2 jruoho
82 1.1.8.2 jruoho /* rawopen("", ctlr, unit, part);
83 1.1.8.2 jruoho */
84 1.1.8.2 jruoho int
85 1.1.8.2 jruoho rawopen(struct open_file *f, ...)
86 1.1.8.2 jruoho {
87 1.1.8.2 jruoho int ctlr, unit, part;
88 1.1.8.2 jruoho char *file, *cp;
89 1.1.8.2 jruoho uint32_t start_sector, sector_count, load_address;
90 1.1.8.2 jruoho int er;
91 1.1.8.2 jruoho size_t cnt;
92 1.1.8.2 jruoho va_list ap;
93 1.1.8.2 jruoho
94 1.1.8.2 jruoho va_start(ap, f);
95 1.1.8.2 jruoho
96 1.1.8.2 jruoho ctlr = va_arg(ap, int);
97 1.1.8.2 jruoho unit = va_arg(ap, int);
98 1.1.8.2 jruoho part = va_arg(ap, int);
99 1.1.8.2 jruoho file = va_arg(ap, char *);
100 1.1.8.2 jruoho va_end(ap);
101 1.1.8.2 jruoho
102 1.1.8.2 jruoho /* See if we have that controller */
103 1.1.8.2 jruoho er = aceopen(f,ctlr,unit,part);
104 1.1.8.2 jruoho if (er != 0)
105 1.1.8.2 jruoho return er;
106 1.1.8.2 jruoho
107 1.1.8.2 jruoho /* The string in FILE must tell us three things.. */
108 1.1.8.2 jruoho cp = file;
109 1.1.8.2 jruoho cp = getxnum(cp,&start_sector);
110 1.1.8.2 jruoho if (cp == NULL) goto Bad;
111 1.1.8.2 jruoho cp = getxnum(cp,§or_count);
112 1.1.8.2 jruoho if (cp == NULL) goto Bad;
113 1.1.8.2 jruoho cp = getxnum(cp,&load_address);
114 1.1.8.2 jruoho if (cp == NULL) goto Bad;
115 1.1.8.2 jruoho
116 1.1.8.2 jruoho //printf("%s -> %u %u %p\n", file, start_sector, sector_count, load_address);
117 1.1.8.2 jruoho
118 1.1.8.2 jruoho /* Read them sectors */
119 1.1.8.2 jruoho er = acestrategy(f->f_devdata, F_READ, start_sector, DEV_BSIZE * sector_count,
120 1.1.8.2 jruoho (void *) load_address, &cnt);
121 1.1.8.2 jruoho #ifndef LIBSA_NO_DEV_CLOSE
122 1.1.8.2 jruoho /* regardless, close the disk */
123 1.1.8.2 jruoho aceclose(f);
124 1.1.8.2 jruoho #endif
125 1.1.8.2 jruoho /* How did it go, are we still alive */
126 1.1.8.2 jruoho if (er != 0)
127 1.1.8.2 jruoho return er;
128 1.1.8.2 jruoho
129 1.1.8.2 jruoho /* Ok, say it and do it then. */
130 1.1.8.2 jruoho printf("Read %u sectors from sector %u at %x, jumping to it..\n",
131 1.1.8.2 jruoho sector_count, start_sector, load_address);
132 1.1.8.2 jruoho
133 1.1.8.2 jruoho call_kernel(load_address,"raw","",0,NULL);
134 1.1.8.2 jruoho
135 1.1.8.2 jruoho Bad:
136 1.1.8.2 jruoho #ifndef LIBSA_NO_DEV_CLOSE
137 1.1.8.2 jruoho /* regardless, close it */
138 1.1.8.2 jruoho aceclose(f);
139 1.1.8.2 jruoho #endif
140 1.1.8.2 jruoho return (ENXIO);
141 1.1.8.2 jruoho }
142 1.1.8.2 jruoho
143 1.1.8.2 jruoho #ifndef LIBSA_NO_DEV_CLOSE
144 1.1.8.2 jruoho int
145 1.1.8.2 jruoho rawclose(struct open_file *f)
146 1.1.8.2 jruoho {
147 1.1.8.2 jruoho /* Never gets here */
148 1.1.8.2 jruoho return (0);
149 1.1.8.2 jruoho }
150 1.1.8.2 jruoho #endif
151 1.1.8.2 jruoho
152 1.1.8.2 jruoho int
153 1.1.8.2 jruoho rawstrategy(
154 1.1.8.2 jruoho void *devdata,
155 1.1.8.2 jruoho int rw,
156 1.1.8.2 jruoho daddr_t bn,
157 1.1.8.2 jruoho size_t reqcnt,
158 1.1.8.2 jruoho void *addr,
159 1.1.8.2 jruoho size_t *cnt) /* out: number of bytes transfered */
160 1.1.8.2 jruoho {
161 1.1.8.2 jruoho /* Never gets here */
162 1.1.8.2 jruoho return (EIO);
163 1.1.8.2 jruoho }
164 1.1.8.2 jruoho
165