Travis Geiselbrecht | 5248673 | 2010-05-06 14:04:14 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2009 Travis Geiselbrecht |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining |
| 5 | * a copy of this software and associated documentation files |
| 6 | * (the "Software"), to deal in the Software without restriction, |
| 7 | * including without limitation the rights to use, copy, modify, merge, |
| 8 | * publish, distribute, sublicense, and/or sell copies of the Software, |
| 9 | * and to permit persons to whom the Software is furnished to do so, |
| 10 | * subject to the following conditions: |
| 11 | * |
| 12 | * The above copyright notice and this permission notice shall be |
| 13 | * included in all copies or substantial portions of the Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 18 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 19 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 20 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 21 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 22 | */ |
| 23 | #include <debug.h> |
| 24 | #include <string.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <lib/bio.h> |
| 27 | |
| 28 | #define LOCAL_TRACE 0 |
| 29 | |
| 30 | #define BLOCKSIZE 512 |
| 31 | |
| 32 | typedef struct mem_bdev { |
| 33 | bdev_t dev; // base device |
| 34 | |
| 35 | void *ptr; |
| 36 | } mem_bdev_t; |
| 37 | |
| 38 | static ssize_t mem_bdev_read(bdev_t *bdev, void *buf, off_t offset, size_t len) |
| 39 | { |
| 40 | mem_bdev_t *mem = (mem_bdev_t *)bdev; |
| 41 | |
| 42 | LTRACEF("bdev %s, buf %p, offset %lld, len %zu\n", bdev->name, buf, offset, len); |
| 43 | |
| 44 | memcpy(buf, (uint8_t *)mem->ptr + offset, len); |
| 45 | |
| 46 | return len; |
| 47 | } |
| 48 | |
| 49 | static ssize_t mem_bdev_read_block(struct bdev *bdev, void *buf, bnum_t block, uint count) |
| 50 | { |
| 51 | mem_bdev_t *mem = (mem_bdev_t *)bdev; |
| 52 | |
| 53 | LTRACEF("bdev %s, buf %p, block %u, count %u\n", bdev->name, buf, block, count); |
| 54 | |
| 55 | memcpy(buf, (uint8_t *)mem->ptr + block * BLOCKSIZE, count * BLOCKSIZE); |
| 56 | |
| 57 | return count * BLOCKSIZE; |
| 58 | } |
| 59 | |
| 60 | static ssize_t mem_bdev_write(bdev_t *bdev, const void *buf, off_t offset, size_t len) |
| 61 | { |
| 62 | mem_bdev_t *mem = (mem_bdev_t *)bdev; |
| 63 | |
| 64 | LTRACEF("bdev %s, buf %p, offset %lld, len %zu\n", bdev->name, buf, offset, len); |
| 65 | |
| 66 | memcpy((uint8_t *)mem->ptr + offset, buf, len); |
| 67 | |
| 68 | return len; |
| 69 | } |
| 70 | |
| 71 | static ssize_t mem_bdev_write_block(struct bdev *bdev, const void *buf, bnum_t block, uint count) |
| 72 | { |
| 73 | mem_bdev_t *mem = (mem_bdev_t *)bdev; |
| 74 | |
| 75 | LTRACEF("bdev %s, buf %p, block %u, count %u\n", bdev->name, buf, block, count); |
| 76 | |
| 77 | memcpy((uint8_t *)mem->ptr + block * BLOCKSIZE, buf, count * BLOCKSIZE); |
| 78 | |
| 79 | return count * BLOCKSIZE; |
| 80 | } |
| 81 | |
| 82 | int create_membdev(const char *name, void *ptr, size_t len) |
| 83 | { |
| 84 | mem_bdev_t *mem = malloc(sizeof(mem_bdev_t)); |
| 85 | |
| 86 | /* set up the base device */ |
| 87 | bio_initialize_bdev(&mem->dev, name, BLOCKSIZE, len / BLOCKSIZE); |
| 88 | |
| 89 | /* our bits */ |
| 90 | mem->ptr = ptr; |
| 91 | mem->dev.read = mem_bdev_read; |
| 92 | mem->dev.read_block = mem_bdev_read_block; |
| 93 | mem->dev.write = mem_bdev_write; |
| 94 | mem->dev.write_block = mem_bdev_write_block; |
| 95 | |
| 96 | /* register it */ |
| 97 | bio_register_device(&mem->dev); |
| 98 | |
| 99 | return 0; |
| 100 | } |
| 101 | |