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