efidisk_ll.c revision 1.1.4.2 1 1.1.4.2 pgoyette /* $NetBSD: efidisk_ll.c,v 1.1.4.2 2017/03/20 06:57:15 pgoyette Exp $ */
2 1.1.4.2 pgoyette /* NetBSD: biosdisk_ll.c,v 1.31 2011/02/21 02:58:02 jakllsch Exp */
3 1.1.4.2 pgoyette
4 1.1.4.2 pgoyette /*-
5 1.1.4.2 pgoyette * Copyright (c) 2005 The NetBSD Foundation, Inc.
6 1.1.4.2 pgoyette * All rights reserved.
7 1.1.4.2 pgoyette *
8 1.1.4.2 pgoyette * This code is derived from software contributed to The NetBSD Foundation
9 1.1.4.2 pgoyette * by Bang Jun-Young.
10 1.1.4.2 pgoyette *
11 1.1.4.2 pgoyette * Redistribution and use in source and binary forms, with or without
12 1.1.4.2 pgoyette * modification, are permitted provided that the following conditions
13 1.1.4.2 pgoyette * are met:
14 1.1.4.2 pgoyette * 1. Redistributions of source code must retain the above copyright
15 1.1.4.2 pgoyette * notice, this list of conditions and the following disclaimer.
16 1.1.4.2 pgoyette * 2. Redistributions in binary form must reproduce the above copyright
17 1.1.4.2 pgoyette * notice, this list of conditions and the following disclaimer in the
18 1.1.4.2 pgoyette * documentation and/or other materials provided with the distribution.
19 1.1.4.2 pgoyette *
20 1.1.4.2 pgoyette * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 1.1.4.2 pgoyette * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 1.1.4.2 pgoyette * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 1.1.4.2 pgoyette * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 1.1.4.2 pgoyette * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 1.1.4.2 pgoyette * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 1.1.4.2 pgoyette * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 1.1.4.2 pgoyette * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 1.1.4.2 pgoyette * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 1.1.4.2 pgoyette * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 1.1.4.2 pgoyette * POSSIBILITY OF SUCH DAMAGE.
31 1.1.4.2 pgoyette */
32 1.1.4.2 pgoyette
33 1.1.4.2 pgoyette /*
34 1.1.4.2 pgoyette * Copyright (c) 1996
35 1.1.4.2 pgoyette * Matthias Drochner. All rights reserved.
36 1.1.4.2 pgoyette * Copyright (c) 1996
37 1.1.4.2 pgoyette * Perry E. Metzger. All rights reserved.
38 1.1.4.2 pgoyette *
39 1.1.4.2 pgoyette * Redistribution and use in source and binary forms, with or without
40 1.1.4.2 pgoyette * modification, are permitted provided that the following conditions
41 1.1.4.2 pgoyette * are met:
42 1.1.4.2 pgoyette * 1. Redistributions of source code must retain the above copyright
43 1.1.4.2 pgoyette * notice, this list of conditions and the following disclaimer.
44 1.1.4.2 pgoyette * 2. Redistributions in binary form must reproduce the above copyright
45 1.1.4.2 pgoyette * notice, this list of conditions and the following disclaimer in the
46 1.1.4.2 pgoyette * documentation and/or other materials provided with the distribution.
47 1.1.4.2 pgoyette * 3. All advertising materials mentioning features or use of this software
48 1.1.4.2 pgoyette * must display the following acknowledgements:
49 1.1.4.2 pgoyette * This product includes software developed for the NetBSD Project
50 1.1.4.2 pgoyette * by Matthias Drochner.
51 1.1.4.2 pgoyette * This product includes software developed for the NetBSD Project
52 1.1.4.2 pgoyette * by Perry E. Metzger.
53 1.1.4.2 pgoyette * 4. The names of the authors may not be used to endorse or promote products
54 1.1.4.2 pgoyette * derived from this software without specific prior written permission.
55 1.1.4.2 pgoyette *
56 1.1.4.2 pgoyette * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
57 1.1.4.2 pgoyette * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
58 1.1.4.2 pgoyette * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
59 1.1.4.2 pgoyette * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
60 1.1.4.2 pgoyette * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
61 1.1.4.2 pgoyette * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
62 1.1.4.2 pgoyette * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
63 1.1.4.2 pgoyette * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
64 1.1.4.2 pgoyette * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
65 1.1.4.2 pgoyette * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
66 1.1.4.2 pgoyette */
67 1.1.4.2 pgoyette
68 1.1.4.2 pgoyette /*
69 1.1.4.2 pgoyette * shared by bootsector startup (bootsectmain) and biosdisk.c
70 1.1.4.2 pgoyette * needs lowlevel parts from bios_disk.S
71 1.1.4.2 pgoyette */
72 1.1.4.2 pgoyette
73 1.1.4.2 pgoyette #include "efiboot.h"
74 1.1.4.2 pgoyette
75 1.1.4.2 pgoyette #include "biosdisk_ll.h"
76 1.1.4.2 pgoyette #include "diskbuf.h"
77 1.1.4.2 pgoyette #include "efidisk.h"
78 1.1.4.2 pgoyette
79 1.1.4.2 pgoyette static int do_read(struct biosdisk_ll *, daddr_t, int, char *);
80 1.1.4.2 pgoyette
81 1.1.4.2 pgoyette #ifndef BIOSDISK_RETRIES
82 1.1.4.2 pgoyette #define BIOSDISK_RETRIES 5
83 1.1.4.2 pgoyette #endif
84 1.1.4.2 pgoyette
85 1.1.4.2 pgoyette int
86 1.1.4.2 pgoyette set_geometry(struct biosdisk_ll *d, struct biosdisk_extinfo *ed)
87 1.1.4.2 pgoyette {
88 1.1.4.2 pgoyette const struct efidiskinfo *edi;
89 1.1.4.2 pgoyette EFI_BLOCK_IO_MEDIA *media;
90 1.1.4.2 pgoyette
91 1.1.4.2 pgoyette edi = efidisk_getinfo(d->dev);
92 1.1.4.2 pgoyette if (edi == NULL)
93 1.1.4.2 pgoyette return 1;
94 1.1.4.2 pgoyette
95 1.1.4.2 pgoyette media = edi->bio->Media;
96 1.1.4.2 pgoyette
97 1.1.4.2 pgoyette d->secsize = media->BlockSize;
98 1.1.4.2 pgoyette d->type = BIOSDISK_TYPE_HD;
99 1.1.4.2 pgoyette d->flags = BIOSDISK_INT13EXT;
100 1.1.4.2 pgoyette
101 1.1.4.2 pgoyette ed->totsec = media->LastBlock + 1;
102 1.1.4.2 pgoyette ed->sbytes = media->BlockSize;
103 1.1.4.2 pgoyette ed->flags = 0;
104 1.1.4.2 pgoyette if (media->RemovableMedia)
105 1.1.4.2 pgoyette ed->flags |= EXTINFO_REMOVABLE;
106 1.1.4.2 pgoyette
107 1.1.4.2 pgoyette return 0;
108 1.1.4.2 pgoyette }
109 1.1.4.2 pgoyette
110 1.1.4.2 pgoyette /*
111 1.1.4.2 pgoyette * Global shared "diskbuf" is used as read ahead buffer. For reading from
112 1.1.4.2 pgoyette * floppies, the bootstrap has to be loaded on a 64K boundary to ensure that
113 1.1.4.2 pgoyette * this buffer doesn't cross a 64K DMA boundary.
114 1.1.4.2 pgoyette */
115 1.1.4.2 pgoyette static int ra_dev;
116 1.1.4.2 pgoyette static daddr_t ra_end;
117 1.1.4.2 pgoyette static daddr_t ra_first;
118 1.1.4.2 pgoyette
119 1.1.4.2 pgoyette static int
120 1.1.4.2 pgoyette do_read(struct biosdisk_ll *d, daddr_t dblk, int num, char *buf)
121 1.1.4.2 pgoyette {
122 1.1.4.2 pgoyette EFI_STATUS status;
123 1.1.4.2 pgoyette const struct efidiskinfo *edi;
124 1.1.4.2 pgoyette
125 1.1.4.2 pgoyette edi = efidisk_getinfo(d->dev);
126 1.1.4.2 pgoyette if (edi == NULL)
127 1.1.4.2 pgoyette return -1;
128 1.1.4.2 pgoyette
129 1.1.4.2 pgoyette status = uefi_call_wrapper(edi->bio->ReadBlocks, 5, edi->bio,
130 1.1.4.2 pgoyette edi->media_id, dblk, num * d->secsize, buf);
131 1.1.4.2 pgoyette if (EFI_ERROR(status))
132 1.1.4.2 pgoyette return -1;
133 1.1.4.2 pgoyette return num;
134 1.1.4.2 pgoyette }
135 1.1.4.2 pgoyette
136 1.1.4.2 pgoyette /*
137 1.1.4.2 pgoyette * NB if 'cold' is set below not all of the program is loaded, so
138 1.1.4.2 pgoyette * mustn't use data segment, bss, call library functions or do read-ahead.
139 1.1.4.2 pgoyette */
140 1.1.4.2 pgoyette int
141 1.1.4.2 pgoyette readsects(struct biosdisk_ll *d, daddr_t dblk, int num, char *buf, int cold)
142 1.1.4.2 pgoyette {
143 1.1.4.2 pgoyette while (num) {
144 1.1.4.2 pgoyette int nsec;
145 1.1.4.2 pgoyette
146 1.1.4.2 pgoyette /* check for usable data in read-ahead buffer */
147 1.1.4.2 pgoyette if (cold || diskbuf_user != &ra_dev || d->dev != ra_dev
148 1.1.4.2 pgoyette || dblk < ra_first || dblk >= ra_end) {
149 1.1.4.2 pgoyette
150 1.1.4.2 pgoyette /* no, read from disk */
151 1.1.4.2 pgoyette char *trbuf;
152 1.1.4.2 pgoyette int maxsecs;
153 1.1.4.2 pgoyette int retries = BIOSDISK_RETRIES;
154 1.1.4.2 pgoyette
155 1.1.4.2 pgoyette if (cold) {
156 1.1.4.2 pgoyette /* transfer directly to buffer */
157 1.1.4.2 pgoyette trbuf = buf;
158 1.1.4.2 pgoyette maxsecs = num;
159 1.1.4.2 pgoyette } else {
160 1.1.4.2 pgoyette /* fill read-ahead buffer */
161 1.1.4.2 pgoyette trbuf = alloc_diskbuf(0); /* no data yet */
162 1.1.4.2 pgoyette maxsecs = DISKBUFSIZE / d->secsize;
163 1.1.4.2 pgoyette }
164 1.1.4.2 pgoyette
165 1.1.4.2 pgoyette while ((nsec = do_read(d, dblk, maxsecs, trbuf)) < 0) {
166 1.1.4.2 pgoyette #ifdef DISK_DEBUG
167 1.1.4.2 pgoyette if (!cold)
168 1.1.4.2 pgoyette printf("read error dblk %"PRId64"-%"PRId64"\n",
169 1.1.4.2 pgoyette dblk, (dblk + maxsecs - 1));
170 1.1.4.2 pgoyette #endif
171 1.1.4.2 pgoyette if (--retries >= 0)
172 1.1.4.2 pgoyette continue;
173 1.1.4.2 pgoyette return -1; /* XXX cannot output here if
174 1.1.4.2 pgoyette * (cold) */
175 1.1.4.2 pgoyette }
176 1.1.4.2 pgoyette if (!cold) {
177 1.1.4.2 pgoyette ra_dev = d->dev;
178 1.1.4.2 pgoyette ra_first = dblk;
179 1.1.4.2 pgoyette ra_end = dblk + nsec;
180 1.1.4.2 pgoyette diskbuf_user = &ra_dev;
181 1.1.4.2 pgoyette }
182 1.1.4.2 pgoyette } else /* can take blocks from end of read-ahead
183 1.1.4.2 pgoyette * buffer */
184 1.1.4.2 pgoyette nsec = ra_end - dblk;
185 1.1.4.2 pgoyette
186 1.1.4.2 pgoyette if (!cold) {
187 1.1.4.2 pgoyette /* copy data from read-ahead to user buffer */
188 1.1.4.2 pgoyette if (nsec > num)
189 1.1.4.2 pgoyette nsec = num;
190 1.1.4.2 pgoyette memcpy(buf,
191 1.1.4.2 pgoyette diskbufp + (dblk - ra_first) * d->secsize,
192 1.1.4.2 pgoyette nsec * d->secsize);
193 1.1.4.2 pgoyette }
194 1.1.4.2 pgoyette buf += nsec * d->secsize;
195 1.1.4.2 pgoyette num -= nsec;
196 1.1.4.2 pgoyette dblk += nsec;
197 1.1.4.2 pgoyette }
198 1.1.4.2 pgoyette
199 1.1.4.2 pgoyette return 0;
200 1.1.4.2 pgoyette }
201