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