kern

view src/bdev.c @ 98:921a264297a4

merged the filesystem stuff
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 17 Apr 2014 17:03:30 +0300
parents f83f50c17c3b
children
line source
1 #include <stdlib.h>
2 #include <string.h>
3 #include <assert.h>
4 #include "bdev.h"
5 #include "ata.h"
6 #include "part.h"
8 #define MKMINOR(disk, part) ((((disk) & 0xf) << 4) | ((part) & 0xf))
9 #define MINOR_DISK(x) (((x) >> 4) & 0xf)
10 #define MINOR_PART(x) ((x) & 0xf)
12 struct block_device *blk_open(dev_t dev)
13 {
14 struct block_device *bdev;
15 int i, minor, devno, part;
17 /* XXX for now ignore the major number as we only have ata devices */
18 minor = DEV_MINOR(dev);
19 devno = MINOR_DISK(minor);
20 part = MINOR_PART(minor);
22 bdev = malloc(sizeof *bdev);
23 assert(bdev);
25 bdev->ata_dev = devno;
27 if(part) {
28 struct partition *plist = get_part_list(devno);
29 assert(plist);
31 for(i=1; i<part; i++) {
32 if(!plist) break;
33 plist = plist->next;
34 }
35 if(!plist) {
36 free(bdev);
37 free_part_list(plist);
38 return 0;
39 }
41 bdev->offset = SECT_TO_BLK(plist->start_sect);
42 bdev->size = SECT_TO_BLK(plist->size_sect);
43 bdev->ptype = get_part_type(plist);
45 free_part_list(plist);
46 } else {
47 bdev->offset = 0;
48 bdev->size = SECT_TO_BLK(ata_num_sectors(devno));
49 bdev->ptype = 0;
50 }
52 return bdev;
53 }
55 void blk_close(struct block_device *bdev)
56 {
57 free(bdev);
58 }
60 #define NSECT (BLKSZ / 512)
62 int blk_read(struct block_device *bdev, uint32_t blk, int count, void *buf)
63 {
64 int i;
65 char *ptr = buf;
66 uint32_t sect = blk * NSECT + bdev->offset;
68 for(i=0; i<NSECT * count; i++) {
69 if(ata_read_pio(bdev->ata_dev, sect++, ptr) == -1) {
70 return -1;
71 }
72 ptr += 512;
73 }
74 return 0;
75 }
77 int blk_write(struct block_device *bdev, uint32_t blk, int count, void *buf)
78 {
79 int i;
80 char *ptr = buf;
81 uint32_t sect = blk * NSECT + bdev->offset;
83 for(i=0; i<NSECT * count; i++) {
84 if(ata_write_pio(bdev->ata_dev, sect++, ptr) == -1) {
85 return -1;
86 }
87 ptr += 512;
88 }
89 return 0;
90 }
92 dev_t bdev_by_name(const char *name)
93 {
94 int minor;
95 int atadev, part = 0;
97 char *tmp = strrchr(name, '/');
98 if(tmp) {
99 name = tmp + 1;
100 }
102 if(strstr(name, "ata") != name) {
103 return 0;
104 }
105 name += 3;
107 atadev = strtol(name, &tmp, 10);
108 if(tmp == name) {
109 return 0;
110 }
111 name = tmp;
113 if(*name++ == 'p') {
114 part = strtol(name, &tmp, 10) + 1;
115 if(tmp == name) {
116 return 0;
117 }
118 }
120 minor = MKMINOR(atadev, part);
121 return DEVNO(1, minor);
122 }