199767f8919635c4928607450d9e0abb932109ceToomas Soome/*-
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 2008 Semihalf, Rafal Jaworowski
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 2009 Semihalf, Piotr Ziecik
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 2012 Andrey V. Elsukov <ae@FreeBSD.org>
199767f8919635c4928607450d9e0abb932109ceToomas Soome * All rights reserved.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Redistribution and use in source and binary forms, with or without
199767f8919635c4928607450d9e0abb932109ceToomas Soome * modification, are permitted provided that the following conditions
199767f8919635c4928607450d9e0abb932109ceToomas Soome * are met:
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 1. Redistributions of source code must retain the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 2. Redistributions in binary form must reproduce the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer in the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * documentation and/or other materials provided with the distribution.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
199767f8919635c4928607450d9e0abb932109ceToomas Soome * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
199767f8919635c4928607450d9e0abb932109ceToomas Soome * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
199767f8919635c4928607450d9e0abb932109ceToomas Soome * SUCH DAMAGE.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Block storage I/O routines for U-Boot
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/cdefs.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome__FBSDID("$FreeBSD$");
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/param.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/disk.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <machine/stdarg.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <stand.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "api_public.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "bootstrap.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "disk.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "glue.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "libuboot.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define stor_printf(fmt, args...) do { \
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("%s%d: ", dev->d_dev->dv_name, dev->d_unit); \
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf(fmt, ##args); \
199767f8919635c4928607450d9e0abb932109ceToomas Soome} while (0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef DEBUG
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define debugf(fmt, args...) do { printf("%s(): ", __func__); \
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf(fmt,##args); } while (0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome#else
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define debugf(fmt, args...)
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic struct {
199767f8919635c4928607450d9e0abb932109ceToomas Soome int opened; /* device is opened */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int handle; /* storage device handle */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int type; /* storage type */
199767f8919635c4928607450d9e0abb932109ceToomas Soome off_t blocks; /* block count */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int bsize; /* block size */
199767f8919635c4928607450d9e0abb932109ceToomas Soome} stor_info[UB_MAX_DEV];
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define SI(dev) (stor_info[(dev)->d_unit])
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int stor_info_no = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int stor_opendev(struct disk_devdesc *);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int stor_readdev(struct disk_devdesc *, daddr_t, size_t, char *);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* devsw I/F */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int stor_init(void);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int stor_strategy(void *, int, daddr_t, size_t, char *, size_t *);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int stor_open(struct open_file *, ...);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int stor_close(struct open_file *);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int stor_ioctl(struct open_file *f, u_long cmd, void *data);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void stor_print(int);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void stor_cleanup(void);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct devsw uboot_storage = {
199767f8919635c4928607450d9e0abb932109ceToomas Soome "disk",
199767f8919635c4928607450d9e0abb932109ceToomas Soome DEVT_DISK,
199767f8919635c4928607450d9e0abb932109ceToomas Soome stor_init,
199767f8919635c4928607450d9e0abb932109ceToomas Soome stor_strategy,
199767f8919635c4928607450d9e0abb932109ceToomas Soome stor_open,
199767f8919635c4928607450d9e0abb932109ceToomas Soome stor_close,
199767f8919635c4928607450d9e0abb932109ceToomas Soome stor_ioctl,
199767f8919635c4928607450d9e0abb932109ceToomas Soome stor_print,
199767f8919635c4928607450d9e0abb932109ceToomas Soome stor_cleanup
199767f8919635c4928607450d9e0abb932109ceToomas Soome};
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomestor_init(void)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct device_info *di;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int i;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (devs_no == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("No U-Boot devices! Really enumerated?\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < devs_no; i++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome di = ub_dev_get(i);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((di != NULL) && (di->type & DEV_TYP_STOR)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (stor_info_no >= UB_MAX_DEV) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("Too many storage devices: %d\n",
199767f8919635c4928607450d9e0abb932109ceToomas Soome stor_info_no);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome stor_info[stor_info_no].handle = i;
199767f8919635c4928607450d9e0abb932109ceToomas Soome stor_info[stor_info_no].opened = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome stor_info[stor_info_no].type = di->type;
199767f8919635c4928607450d9e0abb932109ceToomas Soome stor_info[stor_info_no].blocks =
199767f8919635c4928607450d9e0abb932109ceToomas Soome di->di_stor.block_count;
199767f8919635c4928607450d9e0abb932109ceToomas Soome stor_info[stor_info_no].bsize =
199767f8919635c4928607450d9e0abb932109ceToomas Soome di->di_stor.block_size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome stor_info_no++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!stor_info_no) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome debugf("No storage devices\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome debugf("storage devices found: %d\n", stor_info_no);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void
199767f8919635c4928607450d9e0abb932109ceToomas Soomestor_cleanup(void)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int i;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < stor_info_no; i++)
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (stor_info[i].opened > 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome ub_dev_close(stor_info[i].handle);
199767f8919635c4928607450d9e0abb932109ceToomas Soome disk_cleanup(&uboot_storage);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomestor_strategy(void *devdata, int rw, daddr_t blk, size_t size, char *buf,
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t *rsize)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct disk_devdesc *dev = (struct disk_devdesc *)devdata;
199767f8919635c4928607450d9e0abb932109ceToomas Soome daddr_t bcount;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int err;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (rw != F_READ) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome stor_printf("write attempt, operation not supported!\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EROFS);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (size % SI(dev).bsize) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome stor_printf("size=%zu not multiple of device "
199767f8919635c4928607450d9e0abb932109ceToomas Soome "block size=%d\n",
199767f8919635c4928607450d9e0abb932109ceToomas Soome size, SI(dev).bsize);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome bcount = size / SI(dev).bsize;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (rsize)
199767f8919635c4928607450d9e0abb932109ceToomas Soome *rsize = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome err = stor_readdev(dev, blk + dev->d_offset, bcount, buf);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!err && rsize)
199767f8919635c4928607450d9e0abb932109ceToomas Soome *rsize = size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (err);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomestor_open(struct open_file *f, ...)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome va_list ap;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct disk_devdesc *dev;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome va_start(ap, f);
199767f8919635c4928607450d9e0abb932109ceToomas Soome dev = va_arg(ap, struct disk_devdesc *);
199767f8919635c4928607450d9e0abb932109ceToomas Soome va_end(ap);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (stor_opendev(dev));
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomestor_opendev(struct disk_devdesc *dev)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int err;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (dev->d_unit < 0 || dev->d_unit >= stor_info_no)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (SI(dev).opened == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome err = ub_dev_open(SI(dev).handle);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (err != 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome stor_printf("device open failed with error=%d, "
199767f8919635c4928607450d9e0abb932109ceToomas Soome "handle=%d\n", err, SI(dev).handle);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (ENXIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome SI(dev).opened++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (disk_open(dev, SI(dev).blocks * SI(dev).bsize,
199767f8919635c4928607450d9e0abb932109ceToomas Soome SI(dev).bsize, 0));
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomestor_close(struct open_file *f)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct disk_devdesc *dev;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome dev = (struct disk_devdesc *)(f->f_devdata);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (disk_close(dev));
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomestor_readdev(struct disk_devdesc *dev, daddr_t blk, size_t size, char *buf)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome lbasize_t real_size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int err;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome debugf("reading blk=%d size=%d @ 0x%08x\n", (int)blk, size, (uint32_t)buf);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome err = ub_dev_read(SI(dev).handle, buf, size, blk, &real_size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (err != 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome stor_printf("read failed, error=%d\n", err);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (real_size != size) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome stor_printf("real size != size\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome err = EIO;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (err);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void
199767f8919635c4928607450d9e0abb932109ceToomas Soomestor_print(int verbose)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct disk_devdesc dev;
199767f8919635c4928607450d9e0abb932109ceToomas Soome static char line[80];
199767f8919635c4928607450d9e0abb932109ceToomas Soome int i;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < stor_info_no; i++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome dev.d_dev = &uboot_storage;
199767f8919635c4928607450d9e0abb932109ceToomas Soome dev.d_unit = i;
199767f8919635c4928607450d9e0abb932109ceToomas Soome dev.d_slice = -1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome dev.d_partition = -1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome sprintf(line, "\tdisk%d (%s)\n", i,
199767f8919635c4928607450d9e0abb932109ceToomas Soome ub_stor_type(SI(&dev).type));
199767f8919635c4928607450d9e0abb932109ceToomas Soome pager_output(line);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (stor_opendev(&dev) == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome sprintf(line, "\tdisk%d", i);
199767f8919635c4928607450d9e0abb932109ceToomas Soome disk_print(&dev, line, verbose);
199767f8919635c4928607450d9e0abb932109ceToomas Soome disk_close(&dev);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomestor_ioctl(struct open_file *f, u_long cmd, void *data)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct disk_devdesc *dev;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome dev = (struct disk_devdesc *)f->f_devdata;
199767f8919635c4928607450d9e0abb932109ceToomas Soome switch (cmd) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome case DIOCGSECTORSIZE:
199767f8919635c4928607450d9e0abb932109ceToomas Soome *(u_int *)data = SI(dev).bsize;
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case DIOCGMEDIASIZE:
199767f8919635c4928607450d9e0abb932109ceToomas Soome *(off_t *)data = SI(dev).bsize * SI(dev).blocks;
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome default:
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (ENOTTY);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Return the device unit number for the given type and type-relative unit
199767f8919635c4928607450d9e0abb932109ceToomas Soome * number.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soomeuboot_diskgetunit(int type, int type_unit)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int local_type_unit;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int i;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome local_type_unit = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < stor_info_no; i++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((stor_info[i].type & type) == type) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (local_type_unit == type_unit) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (i);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome local_type_unit++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}