/branches/dd/uspace/srv/bd/file_bd/file_bd.c |
---|
0,0 → 1,217 |
/* |
* Copyright (c) 2009 Jiri Svoboda |
* All rights reserved. |
* |
* Redistribution and use in source and binary forms, with or without |
* modification, are permitted provided that the following conditions |
* are met: |
* |
* - Redistributions of source code must retain the above copyright |
* notice, this list of conditions and the following disclaimer. |
* - Redistributions in binary form must reproduce the above copyright |
* notice, this list of conditions and the following disclaimer in the |
* documentation and/or other materials provided with the distribution. |
* - The name of the author may not be used to endorse or promote products |
* derived from this software without specific prior written permission. |
* |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
*/ |
/** @addtogroup bd |
* @{ |
*/ |
/** |
* @file |
* @brief File-backed block device driver |
* |
* Allows accessing a file as a block device. Useful for, e.g., mounting |
* a disk image. |
*/ |
#include <stdio.h> |
#include <unistd.h> |
#include <ipc/ipc.h> |
#include <ipc/bd.h> |
#include <async.h> |
#include <as.h> |
#include <futex.h> |
#include <devmap.h> |
#include <sys/types.h> |
#include <errno.h> |
#include <bool.h> |
#define NAME "file_bd" |
static size_t comm_size; |
static FILE *img; |
static dev_handle_t dev_handle; |
static atomic_t dev_futex = FUTEX_INITIALIZER; |
static int file_bd_init(const char *fname); |
static void file_bd_connection(ipc_callid_t iid, ipc_call_t *icall); |
static int file_bd_read(off_t blk_idx, off_t size, void *buf); |
static int file_bd_write(off_t blk_idx, off_t size, void *buf); |
int main(int argc, char **argv) |
{ |
int rc; |
printf(NAME ": File-backed block device driver\n"); |
if (argc != 3) { |
printf("Expected two arguments (image name, device name).\n"); |
return -1; |
} |
if (file_bd_init(argv[1]) != EOK) |
return -1; |
rc = devmap_device_register(argv[2], &dev_handle); |
if (rc != EOK) { |
devmap_hangup_phone(DEVMAP_DRIVER); |
printf(NAME ": Unable to register device %s.\n", |
argv[2]); |
return rc; |
} |
printf(NAME ": Accepting connections\n"); |
async_manager(); |
/* Not reached */ |
return 0; |
} |
static int file_bd_init(const char *fname) |
{ |
int rc; |
rc = devmap_driver_register(NAME, file_bd_connection); |
if (rc < 0) { |
printf(NAME ": Unable to register driver.\n"); |
return rc; |
} |
img = fopen(fname, "rb+"); |
if (img == NULL) |
return EINVAL; |
return EOK; |
} |
static void file_bd_connection(ipc_callid_t iid, ipc_call_t *icall) |
{ |
void *fs_va = NULL; |
ipc_callid_t callid; |
ipc_call_t call; |
ipcarg_t method; |
int flags; |
int retval; |
off_t idx; |
off_t size; |
/* Answer the IPC_M_CONNECT_ME_TO call. */ |
ipc_answer_0(iid, EOK); |
if (!ipc_share_out_receive(&callid, &comm_size, &flags)) { |
ipc_answer_0(callid, EHANGUP); |
return; |
} |
fs_va = as_get_mappable_page(comm_size); |
if (fs_va == NULL) { |
ipc_answer_0(callid, EHANGUP); |
return; |
} |
(void) ipc_share_out_finalize(callid, fs_va); |
while (1) { |
callid = async_get_call(&call); |
method = IPC_GET_METHOD(call); |
switch (method) { |
case IPC_M_PHONE_HUNGUP: |
/* The other side has hung up. */ |
ipc_answer_0(callid, EOK); |
return; |
case BD_READ_BLOCK: |
case BD_WRITE_BLOCK: |
idx = IPC_GET_ARG1(call); |
size = IPC_GET_ARG2(call); |
if (size > comm_size) { |
retval = EINVAL; |
break; |
} |
if (method == BD_READ_BLOCK) |
retval = file_bd_read(idx, size, fs_va); |
else |
retval = file_bd_write(idx, size, fs_va); |
break; |
default: |
retval = EINVAL; |
break; |
} |
ipc_answer_0(callid, retval); |
} |
} |
static int file_bd_read(off_t blk_idx, off_t size, void *buf) |
{ |
size_t n_rd; |
printf("file_bd_read\n"); |
futex_down(&dev_futex); |
printf("seek\n"); |
fseek(img, blk_idx * size, SEEK_SET); |
printf("read\n"); |
n_rd = fread(buf, 1, size, img); |
printf("done\n"); |
printf("done\n"); |
if (ferror(img)) { |
futex_up(&dev_futex); |
return EIO; /* Read error */ |
} |
futex_up(&dev_futex); |
if (n_rd < size) |
return EINVAL; /* Read beyond end of disk */ |
return EOK; |
} |
static int file_bd_write(off_t blk_idx, off_t size, void *buf) |
{ |
size_t n_wr; |
futex_down(&dev_futex); |
fseek(img, blk_idx * size, SEEK_SET); |
n_wr = fread(buf, 1, size, img); |
if (ferror(img) || n_wr < size) { |
futex_up(&dev_futex); |
return EIO; /* Write error */ |
} |
futex_up(&dev_futex); |
return EOK; |
} |
/** |
* @} |
*/ |
/branches/dd/uspace/srv/bd/file_bd/Makefile |
---|
0,0 → 1,76 |
# |
# Copyright (c) 2006 Martin Decky |
# All rights reserved. |
# |
# Redistribution and use in source and binary forms, with or without |
# modification, are permitted provided that the following conditions |
# are met: |
# |
# - Redistributions of source code must retain the above copyright |
# notice, this list of conditions and the following disclaimer. |
# - Redistributions in binary form must reproduce the above copyright |
# notice, this list of conditions and the following disclaimer in the |
# documentation and/or other materials provided with the distribution. |
# - The name of the author may not be used to endorse or promote products |
# derived from this software without specific prior written permission. |
# |
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
# |
## Setup toolchain |
# |
LIBC_PREFIX = ../../../lib/libc |
SOFTINT_PREFIX = ../../../lib/softint |
include $(LIBC_PREFIX)/Makefile.toolchain |
LIBS = $(LIBC_PREFIX)/libc.a |
## Sources |
# |
OUTPUT = file_bd |
SOURCES = \ |
file_bd.c |
OBJECTS := $(addsuffix .o,$(basename $(SOURCES))) |
.PHONY: all clean depend disasm |
all: $(OUTPUT) $(OUTPUT).disasm |
-include Makefile.depend |
clean: |
-rm -f $(OUTPUT) $(OUTPUT).map $(OUTPUT).disasm Makefile.depend $(OBJECTS) |
depend: |
$(CC) $(DEFS) $(CFLAGS) -M $(SOURCES) > Makefile.depend |
$(OUTPUT): $(OBJECTS) $(LIBS) |
$(LD) -T $(LIBC_PREFIX)/arch/$(UARCH)/_link.ld $(OBJECTS) $(LIBS) $(LFLAGS) -o $@ -Map $(OUTPUT).map |
disasm: $(OUTPUT).disasm |
$(OUTPUT).disasm: $(OUTPUT) |
$(OBJDUMP) -d $< > $@ |
%.o: %.S |
$(CC) $(DEFS) $(AFLAGS) $(CFLAGS) -D__ASM__ -c $< -o $@ |
%.o: %.s |
$(AS) $(AFLAGS) $< -o $@ |
%.o: %.c |
$(CC) $(DEFS) $(CFLAGS) -c $< -o $@ |
/branches/dd/uspace/srv/bd/gxe_bd/gxe_bd.c |
---|
58,6 → 58,10 |
STATUS_FAILURE = 0 |
}; |
enum { |
MAX_DISKS = 2 |
}; |
typedef struct { |
uint32_t offset_lo; |
uint32_t pad0; |
72,17 → 76,12 |
uint32_t status; |
/* FIXME: Need to fix pio_enable() to support >= page_size areas. |
uint32_t pad4[3]; |
uint32_t pad5[0x3fc0]; |
uint8_t pad5[0x3fc0]; |
uint8_t buffer[512];*/ |
uint8_t buffer[512]; |
} gxe_bd_t; |
typedef struct { |
uint8_t buffer[512]; |
} gxe_buf_t; |
static const size_t block_size = 512; |
static size_t comm_size; |
89,17 → 88,19 |
static uintptr_t dev_physical = 0x13000000; |
static gxe_bd_t *dev; |
static gxe_buf_t *devbuf; |
static uint32_t disk_id = 0; |
static dev_handle_t dev_handle[MAX_DISKS]; |
static atomic_t dev_futex = FUTEX_INITIALIZER; |
static int gxe_bd_init(void); |
static void gxe_bd_connection(ipc_callid_t iid, ipc_call_t *icall); |
static int gx_bd_rdwr(ipcarg_t method, off_t offset, off_t size, void *buf); |
static int gxe_bd_read_block(uint64_t offset, size_t size, void *buf); |
static int gxe_bd_write_block(uint64_t offset, size_t size, const void *buf); |
static int gx_bd_rdwr(int disk_id, ipcarg_t method, off_t offset, off_t size, |
void *buf); |
static int gxe_bd_read_block(int disk_id, uint64_t offset, size_t size, |
void *buf); |
static int gxe_bd_write_block(int disk_id, uint64_t offset, size_t size, |
const void *buf); |
int main(int argc, char **argv) |
{ |
117,9 → 118,9 |
static int gxe_bd_init(void) |
{ |
dev_handle_t dev_handle; |
void *vaddr; |
int rc; |
int rc, i; |
char name[16]; |
rc = devmap_driver_register(NAME, gxe_bd_connection); |
if (rc < 0) { |
135,20 → 136,16 |
dev = vaddr; |
rc = pio_enable((void *) dev_physical + 0x4000, sizeof(gxe_buf_t), &vaddr); |
for (i = 0; i < MAX_DISKS; i++) { |
snprintf(name, 16, "disk%d", i); |
rc = devmap_device_register(name, &dev_handle[i]); |
if (rc != EOK) { |
printf(NAME ": Could not initialize device I/O space.\n"); |
return rc; |
} |
devbuf = vaddr; |
rc = devmap_device_register("disk0", &dev_handle); |
if (rc != EOK) { |
devmap_hangup_phone(DEVMAP_DRIVER); |
printf(NAME ": Unable to register device.\n"); |
printf(NAME ": Unable to register device %s.\n", |
name); |
return rc; |
} |
} |
return EOK; |
} |
159,11 → 156,27 |
ipc_callid_t callid; |
ipc_call_t call; |
ipcarg_t method; |
dev_handle_t dh; |
int flags; |
int retval; |
off_t idx; |
off_t size; |
int disk_id, i; |
/* Get the device handle. */ |
dh = IPC_GET_ARG1(*icall); |
/* Determine which disk device is the client connecting to. */ |
disk_id = -1; |
for (i = 0; i < MAX_DISKS; i++) |
if (dev_handle[i] == dh) |
disk_id = i; |
if (disk_id < 0) { |
ipc_answer_0(iid, EINVAL); |
return; |
} |
/* Answer the IPC_M_CONNECT_ME_TO call. */ |
ipc_answer_0(iid, EOK); |
196,7 → 209,8 |
retval = EINVAL; |
break; |
} |
retval = gx_bd_rdwr(method, idx * size, size, fs_va); |
retval = gx_bd_rdwr(disk_id, method, idx * size, |
size, fs_va); |
break; |
default: |
retval = EINVAL; |
206,7 → 220,8 |
} |
} |
static int gx_bd_rdwr(ipcarg_t method, off_t offset, off_t size, void *buf) |
static int gx_bd_rdwr(int disk_id, ipcarg_t method, off_t offset, off_t size, |
void *buf) |
{ |
int rc; |
size_t now; |
215,9 → 230,9 |
now = size < block_size ? size : block_size; |
if (method == BD_READ_BLOCK) |
rc = gxe_bd_read_block(offset, now, buf); |
rc = gxe_bd_read_block(disk_id, offset, now, buf); |
else |
rc = gxe_bd_write_block(offset, now, buf); |
rc = gxe_bd_write_block(disk_id, offset, now, buf); |
if (rc != EOK) |
return rc; |
234,7 → 249,8 |
return EOK; |
} |
static int gxe_bd_read_block(uint64_t offset, size_t size, void *buf) |
static int gxe_bd_read_block(int disk_id, uint64_t offset, size_t size, |
void *buf) |
{ |
uint32_t status; |
size_t i; |
252,8 → 268,7 |
} |
for (i = 0; i < size; i++) { |
((uint8_t *) buf)[i] = w = |
pio_read_8(&devbuf->buffer[i]); |
((uint8_t *) buf)[i] = w = pio_read_8(&dev->buffer[i]); |
} |
futex_up(&dev_futex); |
260,14 → 275,14 |
return EOK; |
} |
static int gxe_bd_write_block(uint64_t offset, size_t size, const void *buf) |
static int gxe_bd_write_block(int disk_id, uint64_t offset, size_t size, |
const void *buf) |
{ |
uint32_t status; |
size_t i; |
uint32_t w; |
for (i = 0; i < size; i++) { |
pio_write_8(&devbuf->buffer[i], ((const uint8_t *) buf)[i]); |
pio_write_8(&dev->buffer[i], ((const uint8_t *) buf)[i]); |
} |
futex_down(&dev_futex); |
/branches/dd/uspace/srv/bd/ata_bd/ata_bd.c |
---|
0,0 → 1,440 |
/* |
* Copyright (c) 2009 Jiri Svoboda |
* All rights reserved. |
* |
* Redistribution and use in source and binary forms, with or without |
* modification, are permitted provided that the following conditions |
* are met: |
* |
* - Redistributions of source code must retain the above copyright |
* notice, this list of conditions and the following disclaimer. |
* - Redistributions in binary form must reproduce the above copyright |
* notice, this list of conditions and the following disclaimer in the |
* documentation and/or other materials provided with the distribution. |
* - The name of the author may not be used to endorse or promote products |
* derived from this software without specific prior written permission. |
* |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
*/ |
/** @addtogroup bd |
* @{ |
*/ |
/** |
* @file |
* @brief ATA disk driver |
* |
* This driver currently works only with CHS addressing and uses PIO. |
* Currently based on the (now obsolete) ANSI X3.221-1994 (ATA-1) standard. |
* At this point only reading is possible, not writing. |
*/ |
#include <stdio.h> |
#include <libarch/ddi.h> |
#include <ddi.h> |
#include <ipc/ipc.h> |
#include <ipc/bd.h> |
#include <async.h> |
#include <as.h> |
#include <futex.h> |
#include <devmap.h> |
#include <sys/types.h> |
#include <errno.h> |
#include <bool.h> |
#include "ata_bd.h" |
#define NAME "ata_bd" |
static const size_t block_size = 512; |
static size_t comm_size; |
static uintptr_t cmd_physical = 0x1f0; |
static uintptr_t ctl_physical = 0x170; |
static ata_cmd_t *cmd; |
static ata_ctl_t *ctl; |
static dev_handle_t dev_handle[MAX_DISKS]; |
static atomic_t dev_futex = FUTEX_INITIALIZER; |
static disk_t disk[MAX_DISKS]; |
static int ata_bd_init(void); |
static void ata_bd_connection(ipc_callid_t iid, ipc_call_t *icall); |
static int ata_bd_rdwr(int disk_id, ipcarg_t method, off_t offset, off_t size, |
void *buf); |
static int ata_bd_read_block(int disk_id, uint64_t blk_idx, size_t blk_cnt, |
void *buf); |
static int ata_bd_write_block(int disk_id, uint64_t blk_idx, size_t blk_cnt, |
const void *buf); |
static int drive_identify(int drive_id, disk_t *d); |
int main(int argc, char **argv) |
{ |
uint8_t status; |
char name[16]; |
int i, rc; |
int n_disks; |
printf(NAME ": ATA disk driver\n"); |
printf("cmd_physical = 0x%x\n", cmd_physical); |
printf("ctl_physical = 0x%x\n", ctl_physical); |
if (ata_bd_init() != EOK) |
return -1; |
/* Put drives to reset, disable interrupts. */ |
printf("Reset drives...\n"); |
pio_write_8(&ctl->device_control, DCR_SRST); |
/* FIXME: Find out how to do this properly. */ |
async_usleep(100); |
pio_write_8(&ctl->device_control, 0); |
do { |
status = pio_read_8(&cmd->status); |
} while ((status & SR_BSY) != 0); |
printf("Done\n"); |
printf("Status = 0x%x\n", pio_read_8(&cmd->status)); |
(void) drive_identify(0, &disk[0]); |
(void) drive_identify(1, &disk[1]); |
n_disks = 0; |
for (i = 0; i < MAX_DISKS; i++) { |
/* Skip unattached drives. */ |
if (disk[i].present == false) |
continue; |
snprintf(name, 16, "disk%d", i); |
rc = devmap_device_register(name, &dev_handle[i]); |
if (rc != EOK) { |
devmap_hangup_phone(DEVMAP_DRIVER); |
printf(NAME ": Unable to register device %s.\n", |
name); |
return rc; |
} |
++n_disks; |
} |
if (n_disks == 0) { |
printf("No disks detected.\n"); |
return -1; |
} |
printf(NAME ": Accepting connections\n"); |
async_manager(); |
/* Not reached */ |
return 0; |
} |
static int drive_identify(int disk_id, disk_t *d) |
{ |
uint16_t data; |
uint8_t status; |
int i; |
printf("Identify drive %d\n", disk_id); |
pio_write_8(&cmd->drive_head, ((disk_id != 0) ? DHR_DRV : 0)); |
async_usleep(100); |
pio_write_8(&cmd->command, CMD_IDENTIFY_DRIVE); |
status = pio_read_8(&cmd->status); |
printf("Status = 0x%x\n", status); |
d->present = false; |
/* |
* Detect if drive is present. This is Qemu only! Need to |
* do the right thing to work with real drives. |
*/ |
if ((status & SR_DRDY) == 0) { |
printf("None attached.\n"); |
return ENOENT; |
} |
for (i = 0; i < block_size / 2; i++) { |
do { |
status = pio_read_8(&cmd->status); |
} while ((status & SR_DRDY) == 0); |
data = pio_read_16(&cmd->data_port); |
switch (i) { |
case 1: d->cylinders = data; break; |
case 3: d->heads = data; break; |
case 6: d->sectors = data; break; |
} |
} |
printf("\n\nStatus = 0x%x\n", pio_read_8(&cmd->status)); |
d->blocks = d->cylinders * d->heads * d->sectors; |
printf("Geometry: %u cylinders, %u heads, %u sectors\n", |
d->cylinders, d->heads, d->sectors); |
d->present = true; |
return EOK; |
} |
static int ata_bd_init(void) |
{ |
void *vaddr; |
int rc; |
rc = devmap_driver_register(NAME, ata_bd_connection); |
if (rc < 0) { |
printf(NAME ": Unable to register driver.\n"); |
return rc; |
} |
rc = pio_enable((void *) cmd_physical, sizeof(ata_cmd_t), &vaddr); |
if (rc != EOK) { |
printf(NAME ": Could not initialize device I/O space.\n"); |
return rc; |
} |
cmd = vaddr; |
rc = pio_enable((void *) ctl_physical, sizeof(ata_ctl_t), &vaddr); |
if (rc != EOK) { |
printf(NAME ": Could not initialize device I/O space.\n"); |
return rc; |
} |
ctl = vaddr; |
return EOK; |
} |
static void ata_bd_connection(ipc_callid_t iid, ipc_call_t *icall) |
{ |
void *fs_va = NULL; |
ipc_callid_t callid; |
ipc_call_t call; |
ipcarg_t method; |
dev_handle_t dh; |
int flags; |
int retval; |
off_t idx; |
off_t size; |
int disk_id, i; |
/* Get the device handle. */ |
dh = IPC_GET_ARG1(*icall); |
/* Determine which disk device is the client connecting to. */ |
disk_id = -1; |
for (i = 0; i < MAX_DISKS; i++) |
if (dev_handle[i] == dh) |
disk_id = i; |
if (disk_id < 0 || disk[disk_id].present == false) { |
ipc_answer_0(iid, EINVAL); |
return; |
} |
/* Answer the IPC_M_CONNECT_ME_TO call. */ |
ipc_answer_0(iid, EOK); |
if (!ipc_share_out_receive(&callid, &comm_size, &flags)) { |
ipc_answer_0(callid, EHANGUP); |
return; |
} |
fs_va = as_get_mappable_page(comm_size); |
if (fs_va == NULL) { |
ipc_answer_0(callid, EHANGUP); |
return; |
} |
(void) ipc_share_out_finalize(callid, fs_va); |
while (1) { |
callid = async_get_call(&call); |
method = IPC_GET_METHOD(call); |
switch (method) { |
case IPC_M_PHONE_HUNGUP: |
/* The other side has hung up. */ |
ipc_answer_0(callid, EOK); |
return; |
case BD_READ_BLOCK: |
case BD_WRITE_BLOCK: |
idx = IPC_GET_ARG1(call); |
size = IPC_GET_ARG2(call); |
if (size > comm_size) { |
retval = EINVAL; |
break; |
} |
retval = ata_bd_rdwr(disk_id, method, idx, |
size, fs_va); |
break; |
default: |
retval = EINVAL; |
break; |
} |
ipc_answer_0(callid, retval); |
} |
} |
static int ata_bd_rdwr(int disk_id, ipcarg_t method, off_t blk_idx, off_t size, |
void *buf) |
{ |
int rc; |
off_t now; |
while (size > 0) { |
now = size < block_size ? size : (off_t) block_size; |
if (now != block_size) |
return EINVAL; |
if (method == BD_READ_BLOCK) |
rc = ata_bd_read_block(disk_id, blk_idx, 1, buf); |
else |
rc = ata_bd_write_block(disk_id, blk_idx, 1, buf); |
if (rc != EOK) |
return rc; |
buf += block_size; |
blk_idx++; |
if (size > block_size) |
size -= block_size; |
else |
size = 0; |
} |
return EOK; |
} |
static int ata_bd_read_block(int disk_id, uint64_t blk_idx, size_t blk_cnt, |
void *buf) |
{ |
size_t i; |
uint16_t data; |
uint8_t status; |
uint64_t c, h, s; |
uint64_t idx; |
uint8_t drv_head; |
disk_t *d; |
d = &disk[disk_id]; |
/* Check device bounds. */ |
if (blk_idx >= d->blocks) |
return EINVAL; |
/* Compute CHS. */ |
c = blk_idx / (d->heads * d->sectors); |
idx = blk_idx % (d->heads * d->sectors); |
h = idx / d->sectors; |
s = 1 + (idx % d->sectors); |
/* New value for Drive/Head register */ |
drv_head = |
((disk_id != 0) ? DHR_DRV : 0) | |
(h & 0x0f); |
futex_down(&dev_futex); |
/* Program a Read Sectors operation. */ |
pio_write_8(&cmd->drive_head, drv_head); |
pio_write_8(&cmd->sector_count, 1); |
pio_write_8(&cmd->sector_number, s); |
pio_write_8(&cmd->cylinder_low, c & 0xff); |
pio_write_8(&cmd->cylinder_high, c >> 16); |
pio_write_8(&cmd->command, CMD_READ_SECTORS); |
/* Read data from the disk buffer. */ |
for (i = 0; i < block_size / 2; i++) { |
do { |
status = pio_read_8(&cmd->status); |
} while ((status & SR_DRDY) == 0); |
data = pio_read_16(&cmd->data_port); |
((uint16_t *) buf)[i] = data; |
} |
futex_up(&dev_futex); |
return EOK; |
} |
static int ata_bd_write_block(int disk_id, uint64_t blk_idx, size_t blk_cnt, |
const void *buf) |
{ |
size_t i; |
uint8_t status; |
uint64_t c, h, s; |
uint64_t idx; |
uint8_t drv_head; |
disk_t *d; |
d = &disk[disk_id]; |
/* Check device bounds. */ |
if (blk_idx >= d->blocks) |
return EINVAL; |
/* Compute CHS. */ |
c = blk_idx / (d->heads * d->sectors); |
idx = blk_idx % (d->heads * d->sectors); |
h = idx / d->sectors; |
s = 1 + (idx % d->sectors); |
/* New value for Drive/Head register */ |
drv_head = |
((disk_id != 0) ? DHR_DRV : 0) | |
(h & 0x0f); |
futex_down(&dev_futex); |
/* Program a Read Sectors operation. */ |
pio_write_8(&cmd->drive_head, drv_head); |
pio_write_8(&cmd->sector_count, 1); |
pio_write_8(&cmd->sector_number, s); |
pio_write_8(&cmd->cylinder_low, c & 0xff); |
pio_write_8(&cmd->cylinder_high, c >> 16); |
pio_write_8(&cmd->command, CMD_WRITE_SECTORS); |
/* Write data to the disk buffer. */ |
for (i = 0; i < block_size / 2; i++) { |
do { |
status = pio_read_8(&cmd->status); |
} while ((status & SR_DRDY) == 0); |
pio_write_16(&cmd->data_port, ((uint16_t *) buf)[i]); |
} |
futex_up(&dev_futex); |
return EOK; |
} |
/** |
* @} |
*/ |
/branches/dd/uspace/srv/bd/ata_bd/ata_bd.h |
---|
0,0 → 1,137 |
/* |
* Copyright (c) 2009 Jiri Svoboda |
* All rights reserved. |
* |
* Redistribution and use in source and binary forms, with or without |
* modification, are permitted provided that the following conditions |
* are met: |
* |
* - Redistributions of source code must retain the above copyright |
* notice, this list of conditions and the following disclaimer. |
* - Redistributions in binary form must reproduce the above copyright |
* notice, this list of conditions and the following disclaimer in the |
* documentation and/or other materials provided with the distribution. |
* - The name of the author may not be used to endorse or promote products |
* derived from this software without specific prior written permission. |
* |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
*/ |
/** @addtogroup bd |
* @{ |
*/ |
/** @file |
*/ |
#ifndef __ATA_BD_H__ |
#define __ATA_BD_H__ |
#include <sys/types.h> |
enum { |
CTL_READ_START = 0, |
CTL_WRITE_START = 1, |
}; |
enum { |
STATUS_FAILURE = 0 |
}; |
enum { |
MAX_DISKS = 2 |
}; |
typedef union { |
/* Read */ |
struct { |
uint8_t data_port; |
uint8_t error; |
uint8_t sector_count; |
uint8_t sector_number; |
uint8_t cylinder_low; |
uint8_t cylinder_high; |
uint8_t drive_head; |
uint8_t status; |
}; |
/* Write */ |
struct { |
uint8_t pad0[7]; |
uint8_t command; |
}; |
} ata_cmd_t; |
typedef union { |
/* Read */ |
struct { |
uint8_t pad0[6]; |
uint8_t alt_status; |
uint8_t drive_address; |
}; |
/* Write */ |
struct { |
uint8_t pad1[6]; |
uint8_t device_control; |
uint8_t pad2; |
}; |
} ata_ctl_t; |
enum devctl_bits { |
DCR_SRST = 0x04, /**< Software Reset */ |
DCR_nIEN = 0x02 /**< Interrupt Enable (negated) */ |
}; |
enum status_bits { |
SR_BSY = 0x80, /**< Busy */ |
SR_DRDY = 0x40, /**< Drive Ready */ |
SR_DWF = 0x20, /**< Drive Write Fault */ |
SR_DSC = 0x10, /**< Drive Seek Complete */ |
SR_DRQ = 0x08, /**< Data Request */ |
SR_CORR = 0x04, /**< Corrected Data */ |
SR_IDX = 0x02, /**< Index */ |
SR_ERR = 0x01 /**< Error */ |
}; |
enum drive_head_bits { |
DHR_DRV = 0x10 |
}; |
enum error_bits { |
ER_BBK = 0x80, /**< Bad Block Detected */ |
ER_UNC = 0x40, /**< Uncorrectable Data Error */ |
ER_MC = 0x20, /**< Media Changed */ |
ER_IDNF = 0x10, /**< ID Not Found */ |
ER_MCR = 0x08, /**< Media Change Request */ |
ER_ABRT = 0x04, /**< Aborted Command */ |
ER_TK0NF = 0x02, /**< Track 0 Not Found */ |
ER_AMNF = 0x01 /**< Address Mark Not Found */ |
}; |
enum ata_command { |
CMD_IDENTIFY_DRIVE = 0xEC, |
CMD_READ_SECTORS = 0x20, |
CMD_WRITE_SECTORS = 0x30 |
}; |
typedef struct { |
bool present; |
unsigned heads; |
unsigned cylinders; |
unsigned sectors; |
uint64_t blocks; |
} disk_t; |
#endif |
/** @} |
*/ |
/branches/dd/uspace/srv/bd/ata_bd/Makefile |
---|
0,0 → 1,76 |
# |
# Copyright (c) 2006 Martin Decky |
# All rights reserved. |
# |
# Redistribution and use in source and binary forms, with or without |
# modification, are permitted provided that the following conditions |
# are met: |
# |
# - Redistributions of source code must retain the above copyright |
# notice, this list of conditions and the following disclaimer. |
# - Redistributions in binary form must reproduce the above copyright |
# notice, this list of conditions and the following disclaimer in the |
# documentation and/or other materials provided with the distribution. |
# - The name of the author may not be used to endorse or promote products |
# derived from this software without specific prior written permission. |
# |
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
# |
## Setup toolchain |
# |
LIBC_PREFIX = ../../../lib/libc |
SOFTINT_PREFIX = ../../../lib/softint |
include $(LIBC_PREFIX)/Makefile.toolchain |
LIBS = $(LIBC_PREFIX)/libc.a |
## Sources |
# |
OUTPUT = ata_bd |
SOURCES = \ |
ata_bd.c |
OBJECTS := $(addsuffix .o,$(basename $(SOURCES))) |
.PHONY: all clean depend disasm |
all: $(OUTPUT) $(OUTPUT).disasm |
-include Makefile.depend |
clean: |
-rm -f $(OUTPUT) $(OUTPUT).map $(OUTPUT).disasm Makefile.depend $(OBJECTS) |
depend: |
$(CC) $(DEFS) $(CFLAGS) -M $(SOURCES) > Makefile.depend |
$(OUTPUT): $(OBJECTS) $(LIBS) |
$(LD) -T $(LIBC_PREFIX)/arch/$(UARCH)/_link.ld $(OBJECTS) $(LIBS) $(LFLAGS) -o $@ -Map $(OUTPUT).map |
disasm: $(OUTPUT).disasm |
$(OUTPUT).disasm: $(OUTPUT) |
$(OBJDUMP) -d $< > $@ |
%.o: %.S |
$(CC) $(DEFS) $(AFLAGS) $(CFLAGS) -D__ASM__ -c $< -o $@ |
%.o: %.s |
$(AS) $(AFLAGS) $< -o $@ |
%.o: %.c |
$(CC) $(DEFS) $(CFLAGS) -c $< -o $@ |