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