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