disk.c revision 1.4.34.1       1  1.4.34.1   bouyer /*	$NetBSD: disk.c,v 1.4.34.1 2008/01/02 21:47:42 bouyer Exp $	*/
      2       1.1  tsutsui 
      3       1.1  tsutsui /*-
      4       1.1  tsutsui  * Copyright (c) 2004, 2005 The NetBSD Foundation, Inc.
      5       1.1  tsutsui  * All rights reserved.
      6       1.1  tsutsui  *
      7       1.1  tsutsui  * This code is derived from software contributed to The NetBSD Foundation
      8       1.1  tsutsui  * by UCHIYAMA Yasushi.
      9       1.1  tsutsui  *
     10       1.1  tsutsui  * Redistribution and use in source and binary forms, with or without
     11       1.1  tsutsui  * modification, are permitted provided that the following conditions
     12       1.1  tsutsui  * are met:
     13       1.1  tsutsui  * 1. Redistributions of source code must retain the above copyright
     14       1.1  tsutsui  *    notice, this list of conditions and the following disclaimer.
     15       1.1  tsutsui  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1  tsutsui  *    notice, this list of conditions and the following disclaimer in the
     17       1.1  tsutsui  *    documentation and/or other materials provided with the distribution.
     18       1.1  tsutsui  * 3. All advertising materials mentioning features or use of this software
     19       1.1  tsutsui  *    must display the following acknowledgement:
     20       1.1  tsutsui  *        This product includes software developed by the NetBSD
     21       1.1  tsutsui  *        Foundation, Inc. and its contributors.
     22       1.1  tsutsui  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23       1.1  tsutsui  *    contributors may be used to endorse or promote products derived
     24       1.1  tsutsui  *    from this software without specific prior written permission.
     25       1.1  tsutsui  *
     26       1.1  tsutsui  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27       1.1  tsutsui  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28       1.1  tsutsui  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29       1.1  tsutsui  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30       1.1  tsutsui  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31       1.1  tsutsui  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32       1.1  tsutsui  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33       1.1  tsutsui  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34       1.1  tsutsui  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35       1.1  tsutsui  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36       1.1  tsutsui  * POSSIBILITY OF SUCH DAMAGE.
     37       1.1  tsutsui  */
     38       1.1  tsutsui 
     39       1.1  tsutsui #include <lib/libsa/stand.h>
     40       1.1  tsutsui #include <lib/libkern/libkern.h>
     41       1.1  tsutsui 
     42       1.1  tsutsui #include <machine/param.h>
     43       1.1  tsutsui #include <machine/sbd.h>
     44       1.1  tsutsui #include <machine/sector.h>
     45       1.1  tsutsui 
     46       1.1  tsutsui #include "local.h"
     47       1.1  tsutsui #include "common.h"
     48       1.1  tsutsui 
     49       1.1  tsutsui int dkopen(struct open_file *, ...);
     50       1.1  tsutsui int dkclose(struct open_file *);
     51       1.1  tsutsui int dkstrategy(void *, int, daddr_t, size_t, void *, size_t *);
     52       1.1  tsutsui 
     53       1.1  tsutsui struct devsw dkdevsw = {
     54       1.1  tsutsui 	"dk", dkstrategy, dkopen, dkclose, noioctl
     55       1.1  tsutsui };
     56       1.1  tsutsui 
     57       1.1  tsutsui struct disk {
     58       1.3  thorpej 	bool active;
     59       1.1  tsutsui 	int type;	/* FD/HD */
     60       1.1  tsutsui 	int unit;
     61       1.1  tsutsui 	int format;	/* 2D/2HD */
     62       1.1  tsutsui 	int partition;
     63       1.1  tsutsui 	int offset;
     64       1.1  tsutsui 	int (*rw)(uint8_t *, int, int, int);
     65       1.1  tsutsui } __disk;
     66       1.1  tsutsui 
     67       1.1  tsutsui void sector_init(void);
     68       1.3  thorpej bool __sector_rw(uint8_t *, int, int, int);
     69       1.1  tsutsui int __hd_rw(uint8_t *, int, int, int);
     70       1.1  tsutsui int __fd_2d_rw(uint8_t *, int, int, int);
     71       1.1  tsutsui int __fd_2hd_rw(uint8_t *, int, int, int);
     72       1.1  tsutsui void __fd_progress_msg(int);
     73       1.1  tsutsui 
     74       1.3  thorpej bool
     75       1.1  tsutsui device_attach(int type, int unit, int partition)
     76       1.1  tsutsui {
     77       1.1  tsutsui 
     78       1.1  tsutsui 	/* Inquire boot device type and unit from NVSRAM */
     79       1.1  tsutsui 	boot_device(&__disk.type, &__disk.unit, &__disk.format);
     80       1.1  tsutsui 
     81       1.1  tsutsui 	if (type >= 0)
     82       1.1  tsutsui 		__disk.type = type;
     83       1.1  tsutsui 	if (unit >= 0)
     84       1.1  tsutsui 		__disk.unit = unit;
     85       1.1  tsutsui 
     86       1.1  tsutsui 	__disk.partition = partition;
     87       1.1  tsutsui 
     88       1.4  thorpej 	__disk.active = true;
     89       1.1  tsutsui 	__disk.offset = 0;
     90       1.1  tsutsui 
     91       1.1  tsutsui 	if (partition >= 0) {
     92       1.1  tsutsui 		if (!find_partition_start(__disk.partition, &__disk.offset)) {
     93       1.1  tsutsui 			printf("type %d, unit %d partition %d not found.\n",
     94       1.1  tsutsui 			    __disk.type, __disk.unit, __disk.partition);
     95       1.4  thorpej 			return false;
     96       1.1  tsutsui 		}
     97       1.1  tsutsui 	}
     98       1.1  tsutsui 	DEVICE_CAPABILITY.active_device = type;
     99       1.1  tsutsui 
    100       1.1  tsutsui 	/* Set actual read/write routine */
    101       1.1  tsutsui 	if (__disk.type == NVSRAM_BOOTDEV_HARDDISK) {
    102       1.1  tsutsui 		__disk.rw = __hd_rw;
    103       1.1  tsutsui 	} else if (__disk.type == NVSRAM_BOOTDEV_FLOPPYDISK) {
    104       1.1  tsutsui 		if (__disk.format == FD_FORMAT_2HD) {
    105       1.1  tsutsui 			__disk.rw = __fd_2hd_rw;
    106       1.1  tsutsui 		} else if (__disk.format == FD_FORMAT_2D) {
    107       1.1  tsutsui 			__disk.rw = __fd_2d_rw;
    108       1.1  tsutsui 		} else {
    109       1.1  tsutsui 			printf("unknown floppy disk format %d.\n",
    110       1.1  tsutsui 			    __disk.format);
    111       1.4  thorpej 			return false;
    112       1.1  tsutsui 		}
    113       1.1  tsutsui 	} else {
    114       1.1  tsutsui 		printf("unknown disk type %d.\n", __disk.type);
    115       1.4  thorpej 		return false;
    116       1.1  tsutsui 	}
    117       1.1  tsutsui 
    118       1.4  thorpej 	return true;
    119       1.1  tsutsui }
    120       1.1  tsutsui 
    121       1.1  tsutsui int
    122       1.1  tsutsui dkopen(struct open_file *f, ...)
    123       1.1  tsutsui {
    124       1.1  tsutsui 
    125       1.1  tsutsui 	return 0;
    126       1.1  tsutsui }
    127       1.1  tsutsui 
    128       1.1  tsutsui int
    129       1.1  tsutsui dkclose(struct open_file *f)
    130       1.1  tsutsui {
    131       1.1  tsutsui 
    132       1.1  tsutsui 	return 0;
    133       1.1  tsutsui }
    134       1.1  tsutsui 
    135       1.1  tsutsui int
    136       1.1  tsutsui dkstrategy(void *devdata, int rw, daddr_t blk, size_t size, void *buf,
    137       1.1  tsutsui     size_t *rsize)
    138       1.1  tsutsui {
    139       1.1  tsutsui 	int n;
    140       1.1  tsutsui 
    141       1.1  tsutsui 	if ((int)size < 0) {
    142       1.1  tsutsui 		printf("%s: invalid request block %d size %d base %d\n",
    143  1.4.34.1   bouyer 		    __func__, blk, size, __disk.offset);
    144       1.1  tsutsui 		return -1;
    145       1.1  tsutsui 	}
    146       1.1  tsutsui 
    147       1.1  tsutsui 	n = ROUND_SECTOR(size) >> DEV_BSHIFT;
    148       1.1  tsutsui 	if (!sector_read_n(0, buf, __disk.offset + blk, n))
    149       1.1  tsutsui 		return -1;
    150       1.1  tsutsui 
    151       1.1  tsutsui 	*rsize = size;
    152       1.1  tsutsui 
    153       1.1  tsutsui 	return 0;
    154       1.1  tsutsui }
    155       1.1  tsutsui 
    156       1.1  tsutsui void
    157       1.1  tsutsui sector_init(void)
    158       1.1  tsutsui {
    159       1.1  tsutsui 
    160       1.1  tsutsui 	if (!__disk.active)
    161       1.1  tsutsui 		device_attach(-1, -1, -1);
    162       1.1  tsutsui }
    163       1.1  tsutsui 
    164       1.1  tsutsui void
    165       1.1  tsutsui sector_fini(void *self)
    166       1.1  tsutsui {
    167       1.1  tsutsui 
    168       1.4  thorpej 	__disk.active = false;
    169       1.1  tsutsui }
    170       1.1  tsutsui 
    171       1.3  thorpej bool
    172       1.1  tsutsui sector_read_n(void *self, uint8_t *buf, int sector, int count)
    173       1.1  tsutsui {
    174       1.1  tsutsui 
    175       1.1  tsutsui 	if (!__sector_rw(buf, sector, 0, count))
    176       1.4  thorpej 		return false;
    177       1.4  thorpej 	return true;
    178       1.1  tsutsui }
    179       1.1  tsutsui 
    180       1.3  thorpej bool
    181       1.1  tsutsui sector_read(void *self, uint8_t *buf, int sector)
    182       1.1  tsutsui {
    183       1.1  tsutsui 
    184       1.1  tsutsui 	return __sector_rw(buf, sector, 0, 1);
    185       1.1  tsutsui }
    186       1.1  tsutsui 
    187       1.3  thorpej bool
    188       1.1  tsutsui sector_write_n(void *self, uint8_t *buf, int sector, int count)
    189       1.1  tsutsui {
    190       1.1  tsutsui 
    191       1.1  tsutsui 	if (!__sector_rw(buf, sector, 0x1000, count))
    192       1.4  thorpej 		return false;
    193       1.4  thorpej 	return true;
    194       1.1  tsutsui }
    195       1.1  tsutsui 
    196       1.3  thorpej bool
    197       1.1  tsutsui sector_write(void *self, uint8_t *buf, int sector)
    198       1.1  tsutsui {
    199       1.1  tsutsui 
    200       1.1  tsutsui 	return __sector_rw(buf, sector, 0x1000, 1);
    201       1.1  tsutsui }
    202       1.1  tsutsui 
    203       1.3  thorpej bool
    204       1.1  tsutsui __sector_rw(uint8_t *buf, int block, int flag, int count)
    205       1.1  tsutsui {
    206       1.1  tsutsui 	int err;
    207       1.1  tsutsui 
    208       1.1  tsutsui 	if (!__disk.active)
    209       1.1  tsutsui 		sector_init();
    210       1.1  tsutsui 
    211       1.1  tsutsui 	if ((err = __disk.rw(buf, block, flag, count)) != 0)
    212       1.1  tsutsui 		printf("%s: type=%d unit=%d offset=%d block=%d err=%d\n",
    213  1.4.34.1   bouyer 		    __func__, __disk.type, __disk.unit, __disk.offset,
    214       1.1  tsutsui 		    block, err);
    215       1.1  tsutsui 
    216       1.1  tsutsui 	return err == 0;
    217       1.1  tsutsui }
    218       1.1  tsutsui 
    219       1.1  tsutsui int
    220       1.1  tsutsui __hd_rw(uint8_t *buf, int block, int flag, int count)
    221       1.1  tsutsui {
    222       1.1  tsutsui 
    223       1.1  tsutsui 	return (ROM_DK_RW(flag | __disk.unit, block, count, buf) & 0x7f);
    224       1.1  tsutsui }
    225       1.1  tsutsui 
    226       1.1  tsutsui int
    227       1.1  tsutsui __fd_2d_rw(uint8_t *buf, int block, int flag, int count)
    228       1.1  tsutsui {
    229       1.2  tsutsui 	int cnt, i, err;
    230       1.2  tsutsui 	uint32_t pos;
    231       1.1  tsutsui 
    232       1.1  tsutsui 	if (!blk_to_2d_position(block, &pos, &cnt)) {
    233  1.4.34.1   bouyer 		printf("%s: invalid block #%d.\n", __func__, block);
    234       1.1  tsutsui 		return -1;
    235       1.1  tsutsui 	}
    236       1.1  tsutsui 	__fd_progress_msg(pos);
    237       1.1  tsutsui 
    238       1.1  tsutsui 	for (i = 0; i < count; i++, buf += DEV_BSIZE) {
    239       1.1  tsutsui 		err = ROM_FD_RW(flag | __disk.unit, pos, cnt, buf);
    240       1.1  tsutsui 		if (err)
    241       1.1  tsutsui 			return err;
    242       1.1  tsutsui 	}
    243       1.1  tsutsui 	return 0;
    244       1.1  tsutsui }
    245       1.1  tsutsui 
    246       1.1  tsutsui int
    247       1.1  tsutsui __fd_2hd_rw(uint8_t *buf, int block, int flag, int count)
    248       1.1  tsutsui {
    249       1.2  tsutsui 	int cnt, i, err;
    250       1.2  tsutsui 	uint32_t pos;
    251       1.1  tsutsui 
    252       1.1  tsutsui 	if (!blk_to_2hd_position(block, &pos, &cnt)) {
    253  1.4.34.1   bouyer 		printf("%s: invalid block #%d.\n", __func__, block);
    254       1.1  tsutsui 		return -1;
    255       1.1  tsutsui 	}
    256       1.1  tsutsui 	__fd_progress_msg(pos);
    257       1.1  tsutsui 
    258       1.1  tsutsui 	for (i = 0; i < count; i++, buf += DEV_BSIZE) {
    259       1.1  tsutsui 		err = ROM_FD_RW(flag | __disk.unit | 0x1000000, pos, cnt, buf);
    260       1.1  tsutsui 		if (err)
    261       1.1  tsutsui 			return err;
    262       1.1  tsutsui 	}
    263       1.1  tsutsui 	return 0;
    264       1.1  tsutsui }
    265       1.1  tsutsui 
    266       1.1  tsutsui void
    267       1.1  tsutsui __fd_progress_msg(int pos)
    268       1.1  tsutsui {
    269       1.1  tsutsui 	char msg[16];
    270       1.1  tsutsui 
    271       1.1  tsutsui 	memset(msg, 0, sizeof msg);
    272       1.1  tsutsui 	sprintf(msg, "C%d H%d S%d\r", (pos >> 16) & 0xff, (pos >> 8) & 0xff,
    273       1.1  tsutsui 	    pos & 0xff);
    274       1.1  tsutsui 	printf("%s", msg);
    275       1.1  tsutsui }
    276