1 /*
2  * Copyright (c) 2009 Travis Geiselbrecht
3  *
4  * Use of this source code is governed by a MIT-style
5  * license that can be found in the LICENSE file or at
6  * https://opensource.org/licenses/MIT
7  */
8 #include <lk/debug.h>
9 #include <lk/trace.h>
10 #include <string.h>
11 #include <stdlib.h>
12 #include <lib/bio.h>
13 
14 #define LOCAL_TRACE 0
15 
16 #define BLOCKSIZE 512
17 
18 typedef struct mem_bdev {
19     bdev_t dev; // base device
20 
21     void *ptr;
22 } mem_bdev_t;
23 
mem_bdev_read(bdev_t * bdev,void * buf,off_t offset,size_t len)24 static ssize_t mem_bdev_read(bdev_t *bdev, void *buf, off_t offset, size_t len) {
25     mem_bdev_t *mem = (mem_bdev_t *)bdev;
26 
27     LTRACEF("bdev %s, buf %p, offset %lld, len %zu\n", bdev->name, buf, offset, len);
28 
29     memcpy(buf, (uint8_t *)mem->ptr + offset, len);
30 
31     return len;
32 }
33 
mem_bdev_read_block(struct bdev * bdev,void * buf,bnum_t block,uint count)34 static ssize_t mem_bdev_read_block(struct bdev *bdev, void *buf, bnum_t block, uint count) {
35     mem_bdev_t *mem = (mem_bdev_t *)bdev;
36 
37     LTRACEF("bdev %s, buf %p, block %u, count %u\n", bdev->name, buf, block, count);
38 
39     memcpy(buf, (uint8_t *)mem->ptr + block * BLOCKSIZE, count * BLOCKSIZE);
40 
41     return count * BLOCKSIZE;
42 }
43 
mem_bdev_write(bdev_t * bdev,const void * buf,off_t offset,size_t len)44 static ssize_t mem_bdev_write(bdev_t *bdev, const void *buf, off_t offset, size_t len) {
45     mem_bdev_t *mem = (mem_bdev_t *)bdev;
46 
47     LTRACEF("bdev %s, buf %p, offset %lld, len %zu\n", bdev->name, buf, offset, len);
48 
49     memcpy((uint8_t *)mem->ptr + offset, buf, len);
50 
51     return len;
52 }
53 
mem_bdev_write_block(struct bdev * bdev,const void * buf,bnum_t block,uint count)54 static ssize_t mem_bdev_write_block(struct bdev *bdev, const void *buf, bnum_t block, uint count) {
55     mem_bdev_t *mem = (mem_bdev_t *)bdev;
56 
57     LTRACEF("bdev %s, buf %p, block %u, count %u\n", bdev->name, buf, block, count);
58 
59     memcpy((uint8_t *)mem->ptr + block * BLOCKSIZE, buf, count * BLOCKSIZE);
60 
61     return count * BLOCKSIZE;
62 }
63 
create_membdev(const char * name,void * ptr,size_t len)64 int create_membdev(const char *name, void *ptr, size_t len) {
65     mem_bdev_t *mem = malloc(sizeof(mem_bdev_t));
66 
67     /* set up the base device */
68     bio_initialize_bdev(&mem->dev, name, BLOCKSIZE, len / BLOCKSIZE, 0, NULL,
69                         BIO_FLAGS_NONE);
70 
71     /* our bits */
72     mem->ptr = ptr;
73     mem->dev.read = mem_bdev_read;
74     mem->dev.read_block = mem_bdev_read_block;
75     mem->dev.write = mem_bdev_write;
76     mem->dev.write_block = mem_bdev_write_block;
77 
78     /* register it */
79     bio_register_device(&mem->dev);
80 
81     return 0;
82 }
83 
84